cbf78edfe00b50d5603c204ee84531e87a1ab9ac
[pandora-kernel.git] / drivers / ide / pci / amd74xx.c
1 /*
2  * AMD 755/756/766/8111 and nVidia nForce/2/2s/3/3s/CK804/MCP04
3  * IDE driver for Linux.
4  *
5  * Copyright (c) 2000-2002 Vojtech Pavlik
6  * Copyright (c) 2007 Bartlomiej Zolnierkiewicz
7  *
8  * Based on the work of:
9  *      Andre Hedrick
10  */
11
12 /*
13  * This program is free software; you can redistribute it and/or modify it
14  * under the terms of the GNU General Public License version 2 as published by
15  * the Free Software Foundation.
16  */
17
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/pci.h>
21 #include <linux/init.h>
22 #include <linux/ide.h>
23
24 #define DRV_NAME "amd74xx"
25
26 enum {
27         AMD_IDE_CONFIG          = 0x41,
28         AMD_CABLE_DETECT        = 0x42,
29         AMD_DRIVE_TIMING        = 0x48,
30         AMD_8BIT_TIMING         = 0x4e,
31         AMD_ADDRESS_SETUP       = 0x4c,
32         AMD_UDMA_TIMING         = 0x50,
33 };
34
35 static unsigned int amd_80w;
36 static unsigned int amd_clock;
37
38 static char *amd_dma[] = { "16", "25", "33", "44", "66", "100", "133" };
39 static unsigned char amd_cyc2udma[] = { 6, 6, 5, 4, 0, 1, 1, 2, 2, 3, 3, 3, 3, 3, 3, 7 };
40
41 static inline u8 amd_offset(struct pci_dev *dev)
42 {
43         return (dev->vendor == PCI_VENDOR_ID_NVIDIA) ? 0x10 : 0;
44 }
45
46 /*
47  * amd_set_speed() writes timing values to the chipset registers
48  */
49
50 static void amd_set_speed(struct pci_dev *dev, u8 dn, u8 udma_mask,
51                           struct ide_timing *timing)
52 {
53         u8 t = 0, offset = amd_offset(dev);
54
55         pci_read_config_byte(dev, AMD_ADDRESS_SETUP + offset, &t);
56         t = (t & ~(3 << ((3 - dn) << 1))) | ((clamp_val(timing->setup, 1, 4) - 1) << ((3 - dn) << 1));
57         pci_write_config_byte(dev, AMD_ADDRESS_SETUP + offset, t);
58
59         pci_write_config_byte(dev, AMD_8BIT_TIMING + offset + (1 - (dn >> 1)),
60                 ((clamp_val(timing->act8b, 1, 16) - 1) << 4) | (clamp_val(timing->rec8b, 1, 16) - 1));
61
62         pci_write_config_byte(dev, AMD_DRIVE_TIMING + offset + (3 - dn),
63                 ((clamp_val(timing->active, 1, 16) - 1) << 4) | (clamp_val(timing->recover, 1, 16) - 1));
64
65         switch (udma_mask) {
66         case ATA_UDMA2: t = timing->udma ? (0xc0 | (clamp_val(timing->udma, 2, 5) - 2)) : 0x03; break;
67         case ATA_UDMA4: t = timing->udma ? (0xc0 | amd_cyc2udma[clamp_val(timing->udma, 2, 10)]) : 0x03; break;
68         case ATA_UDMA5: t = timing->udma ? (0xc0 | amd_cyc2udma[clamp_val(timing->udma, 1, 10)]) : 0x03; break;
69         case ATA_UDMA6: t = timing->udma ? (0xc0 | amd_cyc2udma[clamp_val(timing->udma, 1, 15)]) : 0x03; break;
70         default: return;
71         }
72
73         pci_write_config_byte(dev, AMD_UDMA_TIMING + offset + (3 - dn), t);
74 }
75
76 /*
77  * amd_set_drive() computes timing values and configures the chipset
78  * to a desired transfer mode.  It also can be called by upper layers.
79  */
80
81 static void amd_set_drive(ide_drive_t *drive, const u8 speed)
82 {
83         ide_hwif_t *hwif = drive->hwif;
84         struct pci_dev *dev = to_pci_dev(hwif->dev);
85         ide_drive_t *peer = hwif->drives + (~drive->dn & 1);
86         struct ide_timing t, p;
87         int T, UT;
88         u8 udma_mask = hwif->ultra_mask;
89
90         T = 1000000000 / amd_clock;
91         UT = (udma_mask == ATA_UDMA2) ? T : (T / 2);
92
93         ide_timing_compute(drive, speed, &t, T, UT);
94
95         if (peer->present) {
96                 ide_timing_compute(peer, peer->current_speed, &p, T, UT);
97                 ide_timing_merge(&p, &t, &t, IDE_TIMING_8BIT);
98         }
99
100         if (speed == XFER_UDMA_5 && amd_clock <= 33333) t.udma = 1;
101         if (speed == XFER_UDMA_6 && amd_clock <= 33333) t.udma = 15;
102
103         amd_set_speed(dev, drive->dn, udma_mask, &t);
104 }
105
106 /*
107  * amd_set_pio_mode() is a callback from upper layers for PIO-only tuning.
108  */
109
110 static void amd_set_pio_mode(ide_drive_t *drive, const u8 pio)
111 {
112         amd_set_drive(drive, XFER_PIO_0 + pio);
113 }
114
115 static void __devinit amd7409_cable_detect(struct pci_dev *dev,
116                                            const char *name)
117 {
118         /* no host side cable detection */
119         amd_80w = 0x03;
120 }
121
122 static void __devinit amd7411_cable_detect(struct pci_dev *dev,
123                                            const char *name)
124 {
125         int i;
126         u32 u = 0;
127         u8 t = 0, offset = amd_offset(dev);
128
129         pci_read_config_byte(dev, AMD_CABLE_DETECT + offset, &t);
130         pci_read_config_dword(dev, AMD_UDMA_TIMING + offset, &u);
131         amd_80w = ((t & 0x3) ? 1 : 0) | ((t & 0xc) ? 2 : 0);
132         for (i = 24; i >= 0; i -= 8)
133                 if (((u >> i) & 4) && !(amd_80w & (1 << (1 - (i >> 4))))) {
134                         printk(KERN_WARNING "%s %s: BIOS didn't set cable bits "
135                                 "correctly. Enabling workaround.\n",
136                                 name, pci_name(dev));
137                         amd_80w |= (1 << (1 - (i >> 4)));
138                 }
139 }
140
141 /*
142  * The initialization callback.  Initialize drive independent registers.
143  */
144
145 static unsigned int __devinit init_chipset_amd74xx(struct pci_dev *dev,
146                                                    const char *name)
147 {
148         u8 t = 0, offset = amd_offset(dev);
149
150 /*
151  * Check 80-wire cable presence.
152  */
153
154         if (dev->vendor == PCI_VENDOR_ID_AMD &&
155             dev->device == PCI_DEVICE_ID_AMD_COBRA_7401)
156                 ; /* no UDMA > 2 */
157         else if (dev->vendor == PCI_VENDOR_ID_AMD &&
158                  dev->device == PCI_DEVICE_ID_AMD_VIPER_7409)
159                 amd7409_cable_detect(dev, name);
160         else
161                 amd7411_cable_detect(dev, name);
162
163 /*
164  * Take care of prefetch & postwrite.
165  */
166
167         pci_read_config_byte(dev, AMD_IDE_CONFIG + offset, &t);
168         /*
169          * Check for broken FIFO support.
170          */
171         if (dev->vendor == PCI_VENDOR_ID_AMD &&
172             dev->vendor == PCI_DEVICE_ID_AMD_VIPER_7411)
173                 t &= 0x0f;
174         else
175                 t |= 0xf0;
176         pci_write_config_byte(dev, AMD_IDE_CONFIG + offset, t);
177
178         return dev->irq;
179 }
180
181 static u8 __devinit amd_cable_detect(ide_hwif_t *hwif)
182 {
183         if ((amd_80w >> hwif->channel) & 1)
184                 return ATA_CBL_PATA80;
185         else
186                 return ATA_CBL_PATA40;
187 }
188
189 static void __devinit init_hwif_amd74xx(ide_hwif_t *hwif)
190 {
191         struct pci_dev *dev = to_pci_dev(hwif->dev);
192
193         if (hwif->irq == 0) /* 0 is bogus but will do for now */
194                 hwif->irq = pci_get_legacy_ide_irq(dev, hwif->channel);
195 }
196
197 static const struct ide_port_ops amd_port_ops = {
198         .set_pio_mode           = amd_set_pio_mode,
199         .set_dma_mode           = amd_set_drive,
200         .cable_detect           = amd_cable_detect,
201 };
202
203 #define IDE_HFLAGS_AMD \
204         (IDE_HFLAG_PIO_NO_BLACKLIST | \
205          IDE_HFLAG_POST_SET_MODE | \
206          IDE_HFLAG_IO_32BIT | \
207          IDE_HFLAG_UNMASK_IRQS)
208
209 #define DECLARE_AMD_DEV(swdma, udma)                            \
210         {                                                               \
211                 .name           = DRV_NAME,                             \
212                 .init_chipset   = init_chipset_amd74xx,                 \
213                 .init_hwif      = init_hwif_amd74xx,                    \
214                 .enablebits     = {{0x40,0x02,0x02}, {0x40,0x01,0x01}}, \
215                 .port_ops       = &amd_port_ops,                        \
216                 .host_flags     = IDE_HFLAGS_AMD,                       \
217                 .pio_mask       = ATA_PIO5,                             \
218                 .swdma_mask     = swdma,                                \
219                 .mwdma_mask     = ATA_MWDMA2,                           \
220                 .udma_mask      = udma,                                 \
221         }
222
223 #define DECLARE_NV_DEV(udma)                                    \
224         {                                                               \
225                 .name           = DRV_NAME,                             \
226                 .init_chipset   = init_chipset_amd74xx,                 \
227                 .init_hwif      = init_hwif_amd74xx,                    \
228                 .enablebits     = {{0x50,0x02,0x02}, {0x50,0x01,0x01}}, \
229                 .port_ops       = &amd_port_ops,                        \
230                 .host_flags     = IDE_HFLAGS_AMD,                       \
231                 .pio_mask       = ATA_PIO5,                             \
232                 .swdma_mask     = ATA_SWDMA2,                           \
233                 .mwdma_mask     = ATA_MWDMA2,                           \
234                 .udma_mask      = udma,                                 \
235         }
236
237 static const struct ide_port_info amd74xx_chipsets[] __devinitdata = {
238         /* 0: AMD7401 */        DECLARE_AMD_DEV(0x00, ATA_UDMA2),
239         /* 1: AMD7409 */        DECLARE_AMD_DEV(ATA_SWDMA2, ATA_UDMA4),
240         /* 2: AMD7411/7441 */   DECLARE_AMD_DEV(ATA_SWDMA2, ATA_UDMA5),
241         /* 3: AMD8111 */        DECLARE_AMD_DEV(ATA_SWDMA2, ATA_UDMA6),
242
243         /* 4: NFORCE */         DECLARE_NV_DEV(ATA_UDMA5),
244         /* 5: >= NFORCE2 */     DECLARE_NV_DEV(ATA_UDMA6),
245
246         /* 6: AMD5536 */        DECLARE_AMD_DEV(ATA_SWDMA2, ATA_UDMA5),
247 };
248
249 static int __devinit amd74xx_probe(struct pci_dev *dev, const struct pci_device_id *id)
250 {
251         struct ide_port_info d;
252         u8 idx = id->driver_data;
253
254         d = amd74xx_chipsets[idx];
255
256         /*
257          * Check for bad SWDMA and incorrectly wired Serenade mainboards.
258          */
259         if (idx == 1) {
260                 if (dev->revision <= 7)
261                         d.swdma_mask = 0;
262                 d.host_flags |= IDE_HFLAG_CLEAR_SIMPLEX;
263         } else if (idx == 3) {
264                 if (dev->subsystem_vendor == PCI_VENDOR_ID_AMD &&
265                     dev->subsystem_device == PCI_DEVICE_ID_AMD_SERENADE)
266                         d.udma_mask = ATA_UDMA5;
267         }
268
269         printk(KERN_INFO "%s %s: UDMA%s controller\n",
270                 d.name, pci_name(dev), amd_dma[fls(d.udma_mask) - 1]);
271
272         /*
273         * Determine the system bus clock.
274         */
275         amd_clock = (ide_pci_clk ? ide_pci_clk : 33) * 1000;
276
277         switch (amd_clock) {
278         case 33000: amd_clock = 33333; break;
279         case 37000: amd_clock = 37500; break;
280         case 41000: amd_clock = 41666; break;
281         }
282
283         if (amd_clock < 20000 || amd_clock > 50000) {
284                 printk(KERN_WARNING "%s: User given PCI clock speed impossible"
285                                     " (%d), using 33 MHz instead.\n",
286                                     d.name, amd_clock);
287                 amd_clock = 33333;
288         }
289
290         return ide_pci_init_one(dev, &d, NULL);
291 }
292
293 static const struct pci_device_id amd74xx_pci_tbl[] = {
294         { PCI_VDEVICE(AMD,      PCI_DEVICE_ID_AMD_COBRA_7401),           0 },
295         { PCI_VDEVICE(AMD,      PCI_DEVICE_ID_AMD_VIPER_7409),           1 },
296         { PCI_VDEVICE(AMD,      PCI_DEVICE_ID_AMD_VIPER_7411),           2 },
297         { PCI_VDEVICE(AMD,      PCI_DEVICE_ID_AMD_OPUS_7441),            2 },
298         { PCI_VDEVICE(AMD,      PCI_DEVICE_ID_AMD_8111_IDE),             3 },
299         { PCI_VDEVICE(NVIDIA,   PCI_DEVICE_ID_NVIDIA_NFORCE_IDE),        4 },
300         { PCI_VDEVICE(NVIDIA,   PCI_DEVICE_ID_NVIDIA_NFORCE2_IDE),       5 },
301         { PCI_VDEVICE(NVIDIA,   PCI_DEVICE_ID_NVIDIA_NFORCE2S_IDE),      5 },
302 #ifdef CONFIG_BLK_DEV_IDE_SATA
303         { PCI_VDEVICE(NVIDIA,   PCI_DEVICE_ID_NVIDIA_NFORCE2S_SATA),     5 },
304 #endif
305         { PCI_VDEVICE(NVIDIA,   PCI_DEVICE_ID_NVIDIA_NFORCE3_IDE),       5 },
306         { PCI_VDEVICE(NVIDIA,   PCI_DEVICE_ID_NVIDIA_NFORCE3S_IDE),      5 },
307 #ifdef CONFIG_BLK_DEV_IDE_SATA
308         { PCI_VDEVICE(NVIDIA,   PCI_DEVICE_ID_NVIDIA_NFORCE3S_SATA),     5 },
309         { PCI_VDEVICE(NVIDIA,   PCI_DEVICE_ID_NVIDIA_NFORCE3S_SATA2),    5 },
310 #endif
311         { PCI_VDEVICE(NVIDIA,   PCI_DEVICE_ID_NVIDIA_NFORCE_CK804_IDE),  5 },
312         { PCI_VDEVICE(NVIDIA,   PCI_DEVICE_ID_NVIDIA_NFORCE_MCP04_IDE),  5 },
313         { PCI_VDEVICE(NVIDIA,   PCI_DEVICE_ID_NVIDIA_NFORCE_MCP51_IDE),  5 },
314         { PCI_VDEVICE(NVIDIA,   PCI_DEVICE_ID_NVIDIA_NFORCE_MCP55_IDE),  5 },
315         { PCI_VDEVICE(NVIDIA,   PCI_DEVICE_ID_NVIDIA_NFORCE_MCP61_IDE),  5 },
316         { PCI_VDEVICE(NVIDIA,   PCI_DEVICE_ID_NVIDIA_NFORCE_MCP65_IDE),  5 },
317         { PCI_VDEVICE(NVIDIA,   PCI_DEVICE_ID_NVIDIA_NFORCE_MCP67_IDE),  5 },
318         { PCI_VDEVICE(NVIDIA,   PCI_DEVICE_ID_NVIDIA_NFORCE_MCP73_IDE),  5 },
319         { PCI_VDEVICE(NVIDIA,   PCI_DEVICE_ID_NVIDIA_NFORCE_MCP77_IDE),  5 },
320         { PCI_VDEVICE(AMD,      PCI_DEVICE_ID_AMD_CS5536_IDE),           6 },
321         { 0, },
322 };
323 MODULE_DEVICE_TABLE(pci, amd74xx_pci_tbl);
324
325 static struct pci_driver driver = {
326         .name           = "AMD_IDE",
327         .id_table       = amd74xx_pci_tbl,
328         .probe          = amd74xx_probe,
329         .remove         = ide_pci_remove,
330 };
331
332 static int __init amd74xx_ide_init(void)
333 {
334         return ide_pci_register_driver(&driver);
335 }
336
337 static void __exit amd74xx_ide_exit(void)
338 {
339         pci_unregister_driver(&driver);
340 }
341
342 module_init(amd74xx_ide_init);
343 module_exit(amd74xx_ide_exit);
344
345 MODULE_AUTHOR("Vojtech Pavlik");
346 MODULE_DESCRIPTION("AMD PCI IDE driver");
347 MODULE_LICENSE("GPL");