Merge branch 'master' of git://git.infradead.org/~dedekind/ubi-2.6
[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_dma_2_pio                -       return the PIO mode matching DMA
22  *      @xfer_rate: transfer speed
23  *
24  *      Returns the nearest equivalent PIO timing for the DMA
25  *      mode requested by the controller.
26  */
27
28 static u8 it8213_dma_2_pio (u8 xfer_rate) {
29         switch(xfer_rate) {
30                 case XFER_UDMA_6:
31                 case XFER_UDMA_5:
32                 case XFER_UDMA_4:
33                 case XFER_UDMA_3:
34                 case XFER_UDMA_2:
35                 case XFER_UDMA_1:
36                 case XFER_UDMA_0:
37                 case XFER_MW_DMA_2:
38                         return 4;
39                 case XFER_MW_DMA_1:
40                         return 3;
41                 case XFER_SW_DMA_2:
42                         return 2;
43                 case XFER_MW_DMA_0:
44                 case XFER_SW_DMA_1:
45                 case XFER_SW_DMA_0:
46                 default:
47                         return 0;
48         }
49 }
50
51 /**
52  *      it8213_set_pio_mode     -       set host controller for PIO mode
53  *      @drive: drive
54  *      @pio: PIO mode number
55  *
56  *      Set the interface PIO mode.
57  */
58
59 static void it8213_set_pio_mode(ide_drive_t *drive, const u8 pio)
60 {
61         ide_hwif_t *hwif        = HWIF(drive);
62         struct pci_dev *dev     = hwif->pci_dev;
63         int is_slave            = drive->dn & 1;
64         int master_port         = 0x40;
65         int slave_port          = 0x44;
66         unsigned long flags;
67         u16 master_data;
68         u8 slave_data;
69         static DEFINE_SPINLOCK(tune_lock);
70         int control = 0;
71
72         static const u8 timings[][2]= {
73                                         { 0, 0 },
74                                         { 0, 0 },
75                                         { 1, 0 },
76                                         { 2, 1 },
77                                         { 2, 3 }, };
78
79         spin_lock_irqsave(&tune_lock, flags);
80         pci_read_config_word(dev, master_port, &master_data);
81
82         if (pio > 1)
83                 control |= 1;   /* Programmable timing on */
84         if (drive->media != ide_disk)
85                 control |= 4;   /* ATAPI */
86         if (pio > 2)
87                 control |= 2;   /* IORDY */
88         if (is_slave) {
89                 master_data |=  0x4000;
90                 master_data &= ~0x0070;
91                 if (pio > 1)
92                         master_data = master_data | (control << 4);
93                 pci_read_config_byte(dev, slave_port, &slave_data);
94                 slave_data = slave_data & 0xf0;
95                 slave_data = slave_data | (timings[pio][0] << 2) | timings[pio][1];
96         } else {
97                 master_data &= ~0x3307;
98                 if (pio > 1)
99                         master_data = master_data | control;
100                 master_data = master_data | (timings[pio][0] << 12) | (timings[pio][1] << 8);
101         }
102         pci_write_config_word(dev, master_port, master_data);
103         if (is_slave)
104                 pci_write_config_byte(dev, slave_port, slave_data);
105         spin_unlock_irqrestore(&tune_lock, flags);
106 }
107
108 /**
109  *      it8213_set_dma_mode     -       set host controller for DMA mode
110  *      @drive: drive
111  *      @speed: DMA mode
112  *
113  *      Tune the ITE chipset for the DMA mode.
114  */
115
116 static void it8213_set_dma_mode(ide_drive_t *drive, const u8 speed)
117 {
118         ide_hwif_t *hwif        = HWIF(drive);
119         struct pci_dev *dev     = hwif->pci_dev;
120         u8 maslave              = 0x40;
121         int a_speed             = 3 << (drive->dn * 4);
122         int u_flag              = 1 << drive->dn;
123         int v_flag              = 0x01 << drive->dn;
124         int w_flag              = 0x10 << drive->dn;
125         int u_speed             = 0;
126         u16                     reg4042, reg4a;
127         u8                      reg48, reg54, reg55;
128
129         pci_read_config_word(dev, maslave, &reg4042);
130         pci_read_config_byte(dev, 0x48, &reg48);
131         pci_read_config_word(dev, 0x4a, &reg4a);
132         pci_read_config_byte(dev, 0x54, &reg54);
133         pci_read_config_byte(dev, 0x55, &reg55);
134
135         switch(speed) {
136                 case XFER_UDMA_6:
137                 case XFER_UDMA_4:
138                 case XFER_UDMA_2:       u_speed = 2 << (drive->dn * 4); break;
139                 case XFER_UDMA_5:
140                 case XFER_UDMA_3:
141                 case XFER_UDMA_1:       u_speed = 1 << (drive->dn * 4); break;
142                 case XFER_UDMA_0:       u_speed = 0 << (drive->dn * 4); break;
143                         break;
144                 case XFER_MW_DMA_2:
145                 case XFER_MW_DMA_1:
146                 case XFER_SW_DMA_2:
147                         break;
148                 default:
149                         return;
150         }
151
152         if (speed >= XFER_UDMA_0) {
153                 if (!(reg48 & u_flag))
154                         pci_write_config_byte(dev, 0x48, reg48 | u_flag);
155                 if (speed >= XFER_UDMA_5) {
156                         pci_write_config_byte(dev, 0x55, (u8) reg55|w_flag);
157                 } else {
158                         pci_write_config_byte(dev, 0x55, (u8) reg55 & ~w_flag);
159                 }
160
161                 if ((reg4a & a_speed) != u_speed)
162                         pci_write_config_word(dev, 0x4a, (reg4a & ~a_speed) | u_speed);
163                 if (speed > XFER_UDMA_2) {
164                         if (!(reg54 & v_flag))
165                                 pci_write_config_byte(dev, 0x54, reg54 | v_flag);
166                 } else
167                         pci_write_config_byte(dev, 0x54, reg54 & ~v_flag);
168         } else {
169                 if (reg48 & u_flag)
170                         pci_write_config_byte(dev, 0x48, reg48 & ~u_flag);
171                 if (reg4a & a_speed)
172                         pci_write_config_word(dev, 0x4a, reg4a & ~a_speed);
173                 if (reg54 & v_flag)
174                         pci_write_config_byte(dev, 0x54, reg54 & ~v_flag);
175                 if (reg55 & w_flag)
176                         pci_write_config_byte(dev, 0x55, (u8) reg55 & ~w_flag);
177         }
178
179         it8213_set_pio_mode(drive, it8213_dma_2_pio(speed));
180 }
181
182 /**
183  *      it8213_configure_drive_for_dma  -       set up for DMA transfers
184  *      @drive: drive we are going to set up
185  *
186  *      Set up the drive for DMA, tune the controller and drive as
187  *      required. If the drive isn't suitable for DMA or we hit
188  *      other problems then we will drop down to PIO and set up
189  *      PIO appropriately
190  */
191
192 static int it8213_config_drive_for_dma (ide_drive_t *drive)
193 {
194         if (ide_tune_dma(drive))
195                 return 0;
196
197         ide_set_max_pio(drive);
198
199         return -1;
200 }
201
202 /**
203  *      init_hwif_it8213        -       set up hwif structs
204  *      @hwif: interface to set up
205  *
206  *      We do the basic set up of the interface structure. The IT8212
207  *      requires several custom handlers so we override the default
208  *      ide DMA handlers appropriately
209  */
210
211 static void __devinit init_hwif_it8213(ide_hwif_t *hwif)
212 {
213         u8 reg42h = 0;
214
215         hwif->set_dma_mode = &it8213_set_dma_mode;
216         hwif->set_pio_mode = &it8213_set_pio_mode;
217
218         hwif->autodma = 0;
219
220         hwif->drives[0].autotune = 1;
221         hwif->drives[1].autotune = 1;
222
223         if (!hwif->dma_base)
224                 return;
225
226         hwif->atapi_dma = 1;
227         hwif->ultra_mask = 0x7f;
228         hwif->mwdma_mask = 0x06;
229         hwif->swdma_mask = 0x04;
230
231         pci_read_config_byte(hwif->pci_dev, 0x42, &reg42h);
232
233         hwif->ide_dma_check = &it8213_config_drive_for_dma;
234
235         if (hwif->cbl != ATA_CBL_PATA40_SHORT)
236                 hwif->cbl = (reg42h & 0x02) ? ATA_CBL_PATA40 : ATA_CBL_PATA80;
237
238         /*
239          *      The BIOS often doesn't set up DMA on this controller
240          *      so we always do it.
241          */
242         if (!noautodma)
243                 hwif->autodma = 1;
244
245         hwif->drives[0].autodma = hwif->autodma;
246         hwif->drives[1].autodma = hwif->autodma;
247 }
248
249
250 #define DECLARE_ITE_DEV(name_str)                       \
251         {                                               \
252                 .name           = name_str,             \
253                 .init_hwif      = init_hwif_it8213,     \
254                 .autodma        = AUTODMA,              \
255                 .enablebits     = {{0x41,0x80,0x80}}, \
256                 .bootable       = ON_BOARD,             \
257                 .host_flags     = IDE_HFLAG_SINGLE,     \
258                 .pio_mask       = ATA_PIO4,             \
259         }
260
261 static ide_pci_device_t it8213_chipsets[] __devinitdata = {
262         /* 0 */ DECLARE_ITE_DEV("IT8213"),
263 };
264
265
266 /**
267  *      it8213_init_one -       pci layer discovery entry
268  *      @dev: PCI device
269  *      @id: ident table entry
270  *
271  *      Called by the PCI code when it finds an ITE8213 controller. As
272  *      this device follows the standard interfaces we can use the
273  *      standard helper functions to do almost all the work for us.
274  */
275
276 static int __devinit it8213_init_one(struct pci_dev *dev, const struct pci_device_id *id)
277 {
278         ide_setup_pci_device(dev, &it8213_chipsets[id->driver_data]);
279         return 0;
280 }
281
282
283 static struct pci_device_id it8213_pci_tbl[] = {
284         { PCI_VENDOR_ID_ITE, 0x8213, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
285         { 0, },
286 };
287
288 MODULE_DEVICE_TABLE(pci, it8213_pci_tbl);
289
290 static struct pci_driver driver = {
291         .name           = "ITE8213_IDE",
292         .id_table       = it8213_pci_tbl,
293         .probe          = it8213_init_one,
294 };
295
296 static int __init it8213_ide_init(void)
297 {
298         return ide_pci_register_driver(&driver);
299 }
300
301 module_init(it8213_ide_init);
302
303 MODULE_AUTHOR("Jack Lee, Alan Cox");
304 MODULE_DESCRIPTION("PCI driver module for the ITE 8213");
305 MODULE_LICENSE("GPL");