Merge branch 'for-linus' of master.kernel.org:/pub/scm/linux/kernel/git/dtor/input
[pandora-kernel.git] / drivers / ide / pci / it8213.c
1 /*
2  * ITE 8213 IDE driver
3  *
4  * Copyright (C) 2006 Jack Lee
5  * Copyright (C) 2006 Alan Cox
6  * Copyright (C) 2007 Bartlomiej Zolnierkiewicz
7  */
8
9 #include <linux/kernel.h>
10 #include <linux/types.h>
11 #include <linux/module.h>
12 #include <linux/pci.h>
13 #include <linux/delay.h>
14 #include <linux/hdreg.h>
15 #include <linux/ide.h>
16 #include <linux/init.h>
17
18 #include <asm/io.h>
19
20 /*
21  *      it8213_ratemask -       Compute available modes
22  *      @drive: IDE drive
23  *
24  *      Compute the available speeds for the devices on the interface. This
25  *      is all modes to ATA133 clipped by drive cable setup.
26  */
27
28 static u8 it8213_ratemask (ide_drive_t *drive)
29 {
30         u8 mode = 4;
31         if (!eighty_ninty_three(drive))
32                 mode = min_t(u8, mode, 1);
33         return mode;
34 }
35
36 /**
37  *      it8213_dma_2_pio                -       return the PIO mode matching DMA
38  *      @xfer_rate: transfer speed
39  *
40  *      Returns the nearest equivalent PIO timing for the PIO or DMA
41  *      mode requested by the controller.
42  */
43
44 static u8 it8213_dma_2_pio (u8 xfer_rate) {
45         switch(xfer_rate) {
46                 case XFER_UDMA_6:
47                 case XFER_UDMA_5:
48                 case XFER_UDMA_4:
49                 case XFER_UDMA_3:
50                 case XFER_UDMA_2:
51                 case XFER_UDMA_1:
52                 case XFER_UDMA_0:
53                 case XFER_MW_DMA_2:
54                 case XFER_PIO_4:
55                         return 4;
56                 case XFER_MW_DMA_1:
57                 case XFER_PIO_3:
58                         return 3;
59                 case XFER_SW_DMA_2:
60                 case XFER_PIO_2:
61                         return 2;
62                 case XFER_MW_DMA_0:
63                 case XFER_SW_DMA_1:
64                 case XFER_SW_DMA_0:
65                 case XFER_PIO_1:
66                 case XFER_PIO_0:
67                 case XFER_PIO_SLOW:
68                 default:
69                         return 0;
70         }
71 }
72
73 /*
74  *      it8213_tuneproc -       tune a drive
75  *      @drive: drive to tune
76  *      @pio: desired PIO mode
77  *
78  *      Set the interface PIO mode.
79  */
80
81 static void it8213_tuneproc (ide_drive_t *drive, u8 pio)
82 {
83         ide_hwif_t *hwif        = HWIF(drive);
84         struct pci_dev *dev     = hwif->pci_dev;
85         int is_slave            = drive->dn & 1;
86         int master_port         = 0x40;
87         int slave_port          = 0x44;
88         unsigned long flags;
89         u16 master_data;
90         u8 slave_data;
91         static DEFINE_SPINLOCK(tune_lock);
92         int control = 0;
93
94         static const u8 timings[][2]= {
95                                         { 0, 0 },
96                                         { 0, 0 },
97                                         { 1, 0 },
98                                         { 2, 1 },
99                                         { 2, 3 }, };
100
101         pio = ide_get_best_pio_mode(drive, pio, 4, NULL);
102
103         spin_lock_irqsave(&tune_lock, flags);
104         pci_read_config_word(dev, master_port, &master_data);
105
106         if (pio > 1)
107                 control |= 1;   /* Programmable timing on */
108         if (drive->media != ide_disk)
109                 control |= 4;   /* ATAPI */
110         if (pio > 2)
111                 control |= 2;   /* IORDY */
112         if (is_slave) {
113                 master_data |=  0x4000;
114                 master_data &= ~0x0070;
115                 if (pio > 1)
116                         master_data = master_data | (control << 4);
117                 pci_read_config_byte(dev, slave_port, &slave_data);
118                 slave_data = slave_data & 0xf0;
119                 slave_data = slave_data | (timings[pio][0] << 2) | timings[pio][1];
120         } else {
121                 master_data &= ~0x3307;
122                 if (pio > 1)
123                         master_data = master_data | control;
124                 master_data = master_data | (timings[pio][0] << 12) | (timings[pio][1] << 8);
125         }
126         pci_write_config_word(dev, master_port, master_data);
127         if (is_slave)
128                 pci_write_config_byte(dev, slave_port, slave_data);
129         spin_unlock_irqrestore(&tune_lock, flags);
130 }
131
132 /**
133  *      it8213_tune_chipset     -       set controller timings
134  *      @drive: Drive to set up
135  *      @xferspeed: speed we want to achieve
136  *
137  *      Tune the ITE chipset for the desired mode. If we can't achieve
138  *      the desired mode then tune for a lower one, but ultimately
139  *      make the thing work.
140  */
141
142 static int it8213_tune_chipset (ide_drive_t *drive, u8 xferspeed)
143 {
144
145         ide_hwif_t *hwif        = HWIF(drive);
146         struct pci_dev *dev     = hwif->pci_dev;
147         u8 maslave              = 0x40;
148         u8 speed                = ide_rate_filter(it8213_ratemask(drive), xferspeed);
149         int a_speed             = 3 << (drive->dn * 4);
150         int u_flag              = 1 << drive->dn;
151         int v_flag              = 0x01 << drive->dn;
152         int w_flag              = 0x10 << drive->dn;
153         int u_speed             = 0;
154         u16                     reg4042, reg4a;
155         u8                      reg48, reg54, reg55;
156
157         pci_read_config_word(dev, maslave, &reg4042);
158         pci_read_config_byte(dev, 0x48, &reg48);
159         pci_read_config_word(dev, 0x4a, &reg4a);
160         pci_read_config_byte(dev, 0x54, &reg54);
161         pci_read_config_byte(dev, 0x55, &reg55);
162
163         switch(speed) {
164                 case XFER_UDMA_6:
165                 case XFER_UDMA_4:
166                 case XFER_UDMA_2:       u_speed = 2 << (drive->dn * 4); break;
167                 case XFER_UDMA_5:
168                 case XFER_UDMA_3:
169                 case XFER_UDMA_1:       u_speed = 1 << (drive->dn * 4); break;
170                 case XFER_UDMA_0:       u_speed = 0 << (drive->dn * 4); break;
171                         break;
172                 case XFER_MW_DMA_2:
173                 case XFER_MW_DMA_1:
174                 case XFER_SW_DMA_2:
175                         break;
176                 case XFER_PIO_4:
177                 case XFER_PIO_3:
178                 case XFER_PIO_2:
179                 case XFER_PIO_1:
180                 case XFER_PIO_0:
181                         break;
182                 default:
183                         return -1;
184         }
185
186         if (speed >= XFER_UDMA_0) {
187                 if (!(reg48 & u_flag))
188                         pci_write_config_byte(dev, 0x48, reg48 | u_flag);
189                 if (speed >= XFER_UDMA_5) {
190                         pci_write_config_byte(dev, 0x55, (u8) reg55|w_flag);
191                 } else {
192                         pci_write_config_byte(dev, 0x55, (u8) reg55 & ~w_flag);
193                 }
194
195                 if ((reg4a & a_speed) != u_speed)
196                         pci_write_config_word(dev, 0x4a, (reg4a & ~a_speed) | u_speed);
197                 if (speed > XFER_UDMA_2) {
198                         if (!(reg54 & v_flag))
199                                 pci_write_config_byte(dev, 0x54, reg54 | v_flag);
200                 } else
201                         pci_write_config_byte(dev, 0x54, reg54 & ~v_flag);
202         } else {
203                 if (reg48 & u_flag)
204                         pci_write_config_byte(dev, 0x48, reg48 & ~u_flag);
205                 if (reg4a & a_speed)
206                         pci_write_config_word(dev, 0x4a, reg4a & ~a_speed);
207                 if (reg54 & v_flag)
208                         pci_write_config_byte(dev, 0x54, reg54 & ~v_flag);
209                 if (reg55 & w_flag)
210                         pci_write_config_byte(dev, 0x55, (u8) reg55 & ~w_flag);
211         }
212         it8213_tuneproc(drive, it8213_dma_2_pio(speed));
213         return ide_config_drive_speed(drive, speed);
214 }
215
216 /*
217  *      config_chipset_for_dma  -       configure for DMA
218  *      @drive: drive to configure
219  *
220  *      Called by the IDE layer when it wants the timings set up.
221  */
222
223 static int config_chipset_for_dma (ide_drive_t *drive)
224 {
225         u8 speed = ide_dma_speed(drive, it8213_ratemask(drive));
226
227         if (!speed)
228                 return 0;
229
230         it8213_tune_chipset(drive, speed);
231
232         return ide_dma_enable(drive);
233 }
234
235 /**
236  *      it8213_configure_drive_for_dma  -       set up for DMA transfers
237  *      @drive: drive we are going to set up
238  *
239  *      Set up the drive for DMA, tune the controller and drive as
240  *      required. If the drive isn't suitable for DMA or we hit
241  *      other problems then we will drop down to PIO and set up
242  *      PIO appropriately
243  */
244
245 static int it8213_config_drive_for_dma (ide_drive_t *drive)
246 {
247         u8 pio;
248
249         if (ide_use_dma(drive) && config_chipset_for_dma(drive))
250                 return 0;
251
252         pio = ide_get_best_pio_mode(drive, 255, 4, NULL);
253         it8213_tune_chipset(drive, XFER_PIO_0 + pio);
254
255         return -1;
256 }
257
258 /**
259  *      init_hwif_it8213        -       set up hwif structs
260  *      @hwif: interface to set up
261  *
262  *      We do the basic set up of the interface structure. The IT8212
263  *      requires several custom handlers so we override the default
264  *      ide DMA handlers appropriately
265  */
266
267 static void __devinit init_hwif_it8213(ide_hwif_t *hwif)
268 {
269         u8 reg42h = 0, ata66 = 0;
270
271         hwif->speedproc = &it8213_tune_chipset;
272         hwif->tuneproc  = &it8213_tuneproc;
273
274         hwif->autodma = 0;
275
276         hwif->drives[0].autotune = 1;
277         hwif->drives[1].autotune = 1;
278
279         if (!hwif->dma_base)
280                 return;
281
282         hwif->atapi_dma = 1;
283         hwif->ultra_mask = 0x7f;
284         hwif->mwdma_mask = 0x06;
285         hwif->swdma_mask = 0x04;
286
287         pci_read_config_byte(hwif->pci_dev, 0x42, &reg42h);
288         ata66 = (reg42h & 0x02) ? 0 : 1;
289
290         hwif->ide_dma_check = &it8213_config_drive_for_dma;
291         if (!(hwif->udma_four))
292                 hwif->udma_four = ata66;
293
294         /*
295          *      The BIOS often doesn't set up DMA on this controller
296          *      so we always do it.
297          */
298         if (!noautodma)
299                 hwif->autodma = 1;
300
301         hwif->drives[0].autodma = hwif->autodma;
302         hwif->drives[1].autodma = hwif->autodma;
303 }
304
305
306 #define DECLARE_ITE_DEV(name_str)                       \
307         {                                               \
308                 .name           = name_str,             \
309                 .init_hwif      = init_hwif_it8213,     \
310                 .channels       = 1,                    \
311                 .autodma        = AUTODMA,              \
312                 .enablebits     = {{0x41,0x80,0x80}}, \
313                 .bootable       = ON_BOARD,             \
314         }
315
316 static ide_pci_device_t it8213_chipsets[] __devinitdata = {
317         /* 0 */ DECLARE_ITE_DEV("IT8213"),
318 };
319
320
321 /**
322  *      it8213_init_one -       pci layer discovery entry
323  *      @dev: PCI device
324  *      @id: ident table entry
325  *
326  *      Called by the PCI code when it finds an ITE8213 controller. As
327  *      this device follows the standard interfaces we can use the
328  *      standard helper functions to do almost all the work for us.
329  */
330
331 static int __devinit it8213_init_one(struct pci_dev *dev, const struct pci_device_id *id)
332 {
333         ide_setup_pci_device(dev, &it8213_chipsets[id->driver_data]);
334         return 0;
335 }
336
337
338 static struct pci_device_id it8213_pci_tbl[] = {
339         { PCI_VENDOR_ID_ITE, 0x8213, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
340         { 0, },
341 };
342
343 MODULE_DEVICE_TABLE(pci, it8213_pci_tbl);
344
345 static struct pci_driver driver = {
346         .name           = "ITE8213_IDE",
347         .id_table       = it8213_pci_tbl,
348         .probe          = it8213_init_one,
349 };
350
351 static int __init it8213_ide_init(void)
352 {
353         return ide_pci_register_driver(&driver);
354 }
355
356 module_init(it8213_ide_init);
357
358 MODULE_AUTHOR("Jack Lee, Alan Cox");
359 MODULE_DESCRIPTION("PCI driver module for the ITE 8213");
360 MODULE_LICENSE("GPL");