blob: b0cb4eaf273cb593c20d37544ee267997278a712 [file] [log] [blame]
Jeff Garzik669a5db2006-08-29 18:12:40 -04001/*
2 * pata_amd.c - AMD PATA for new ATA layer
3 * (C) 2005-2006 Red Hat Inc
4 * Alan Cox <alan@redhat.com>
5 *
6 * Based on pata-sil680. Errata information is taken from data sheets
7 * and the amd74xx.c driver by Vojtech Pavlik. Nvidia SATA devices are
8 * claimed by sata-nv.c.
9 *
10 * TODO:
11 * Variable system clock when/if it makes sense
12 * Power management on ports
13 *
14 *
15 * Documentation publically available.
16 */
17
18#include <linux/kernel.h>
19#include <linux/module.h>
20#include <linux/pci.h>
21#include <linux/init.h>
22#include <linux/blkdev.h>
23#include <linux/delay.h>
24#include <scsi/scsi_host.h>
25#include <linux/libata.h>
26
27#define DRV_NAME "pata_amd"
Bartlomiej Zolnierkiewicz943547a2007-12-02 03:47:01 +010028#define DRV_VERSION "0.3.10"
Jeff Garzik669a5db2006-08-29 18:12:40 -040029
30/**
31 * timing_setup - shared timing computation and load
32 * @ap: ATA port being set up
33 * @adev: drive being configured
34 * @offset: port offset
35 * @speed: target speed
36 * @clock: clock multiplier (number of times 33MHz for this part)
37 *
38 * Perform the actual timing set up for Nvidia or AMD PATA devices.
39 * The actual devices vary so they all call into this helper function
40 * providing the clock multipler and offset (because AMD and Nvidia put
41 * the ports at different locations).
42 */
43
44static void timing_setup(struct ata_port *ap, struct ata_device *adev, int offset, int speed, int clock)
45{
46 static const unsigned char amd_cyc2udma[] = {
47 6, 6, 5, 4, 0, 1, 1, 2, 2, 3, 3, 3, 3, 3, 3, 7
48 };
49
50 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
51 struct ata_device *peer = ata_dev_pair(adev);
52 int dn = ap->port_no * 2 + adev->devno;
53 struct ata_timing at, apeer;
54 int T, UT;
55 const int amd_clock = 33333; /* KHz. */
56 u8 t;
57
58 T = 1000000000 / amd_clock;
59 UT = T / min_t(int, max_t(int, clock, 1), 2);
60
61 if (ata_timing_compute(adev, speed, &at, T, UT) < 0) {
62 dev_printk(KERN_ERR, &pdev->dev, "unknown mode %d.\n", speed);
63 return;
64 }
65
66 if (peer) {
67 /* This may be over conservative */
68 if (peer->dma_mode) {
69 ata_timing_compute(peer, peer->dma_mode, &apeer, T, UT);
70 ata_timing_merge(&apeer, &at, &at, ATA_TIMING_8BIT);
71 }
72 ata_timing_compute(peer, peer->pio_mode, &apeer, T, UT);
73 ata_timing_merge(&apeer, &at, &at, ATA_TIMING_8BIT);
74 }
75
76 if (speed == XFER_UDMA_5 && amd_clock <= 33333) at.udma = 1;
77 if (speed == XFER_UDMA_6 && amd_clock <= 33333) at.udma = 15;
78
79 /*
80 * Now do the setup work
81 */
82
83 /* Configure the address set up timing */
84 pci_read_config_byte(pdev, offset + 0x0C, &t);
85 t = (t & ~(3 << ((3 - dn) << 1))) | ((FIT(at.setup, 1, 4) - 1) << ((3 - dn) << 1));
86 pci_write_config_byte(pdev, offset + 0x0C , t);
87
88 /* Configure the 8bit I/O timing */
89 pci_write_config_byte(pdev, offset + 0x0E + (1 - (dn >> 1)),
90 ((FIT(at.act8b, 1, 16) - 1) << 4) | (FIT(at.rec8b, 1, 16) - 1));
91
92 /* Drive timing */
93 pci_write_config_byte(pdev, offset + 0x08 + (3 - dn),
94 ((FIT(at.active, 1, 16) - 1) << 4) | (FIT(at.recover, 1, 16) - 1));
95
96 switch (clock) {
97 case 1:
98 t = at.udma ? (0xc0 | (FIT(at.udma, 2, 5) - 2)) : 0x03;
99 break;
100
101 case 2:
102 t = at.udma ? (0xc0 | amd_cyc2udma[FIT(at.udma, 2, 10)]) : 0x03;
103 break;
104
105 case 3:
106 t = at.udma ? (0xc0 | amd_cyc2udma[FIT(at.udma, 1, 10)]) : 0x03;
107 break;
108
109 case 4:
110 t = at.udma ? (0xc0 | amd_cyc2udma[FIT(at.udma, 1, 15)]) : 0x03;
111 break;
112
113 default:
114 return;
115 }
116
117 /* UDMA timing */
Bartlomiej Zolnierkiewicz943547a2007-12-02 03:47:01 +0100118 if (at.udma)
119 pci_write_config_byte(pdev, offset + 0x10 + (3 - dn), t);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400120}
121
122/**
Tejun Heocc0680a2007-08-06 18:36:23 +0900123 * amd_pre_reset - perform reset handling
124 * @link: ATA link
Tejun Heod4b2bab2007-02-02 16:50:52 +0900125 * @deadline: deadline jiffies for the operation
Jeff Garzik669a5db2006-08-29 18:12:40 -0400126 *
Alan Coxeb4a2c72007-04-11 00:04:20 +0100127 * Reset sequence checking enable bits to see which ports are
128 * active.
Jeff Garzik669a5db2006-08-29 18:12:40 -0400129 */
130
Tejun Heocc0680a2007-08-06 18:36:23 +0900131static int amd_pre_reset(struct ata_link *link, unsigned long deadline)
Jeff Garzik669a5db2006-08-29 18:12:40 -0400132{
Jeff Garzik669a5db2006-08-29 18:12:40 -0400133 static const struct pci_bits amd_enable_bits[] = {
134 { 0x40, 1, 0x02, 0x02 },
135 { 0x40, 1, 0x01, 0x01 }
136 };
137
Tejun Heocc0680a2007-08-06 18:36:23 +0900138 struct ata_port *ap = link->ap;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400139 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
Jeff Garzik85cd7252006-08-31 00:03:49 -0400140
Alan Coxc9619222006-09-26 17:53:38 +0100141 if (!pci_test_config_bits(pdev, &amd_enable_bits[ap->port_no]))
142 return -ENOENT;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400143
Tejun Heocc0680a2007-08-06 18:36:23 +0900144 return ata_std_prereset(link, deadline);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400145}
146
147static void amd_error_handler(struct ata_port *ap)
148{
Harvey Harrisond98f88c2008-02-13 21:14:20 -0800149 ata_bmdma_drive_eh(ap, amd_pre_reset, ata_std_softreset, NULL,
150 ata_std_postreset);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400151}
152
Alan Coxeb4a2c72007-04-11 00:04:20 +0100153static int amd_cable_detect(struct ata_port *ap)
Jeff Garzik669a5db2006-08-29 18:12:40 -0400154{
Alan Coxeb4a2c72007-04-11 00:04:20 +0100155 static const u32 bitmask[2] = {0x03, 0x0C};
Jeff Garzik669a5db2006-08-29 18:12:40 -0400156 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
Alan Coxeb4a2c72007-04-11 00:04:20 +0100157 u8 ata66;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400158
Alan Coxeb4a2c72007-04-11 00:04:20 +0100159 pci_read_config_byte(pdev, 0x42, &ata66);
160 if (ata66 & bitmask[ap->port_no])
161 return ATA_CBL_PATA80;
162 return ATA_CBL_PATA40;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400163}
164
165/**
166 * amd33_set_piomode - set initial PIO mode data
167 * @ap: ATA interface
168 * @adev: ATA device
169 *
170 * Program the AMD registers for PIO mode.
171 */
172
173static void amd33_set_piomode(struct ata_port *ap, struct ata_device *adev)
174{
175 timing_setup(ap, adev, 0x40, adev->pio_mode, 1);
176}
177
178static void amd66_set_piomode(struct ata_port *ap, struct ata_device *adev)
179{
180 timing_setup(ap, adev, 0x40, adev->pio_mode, 2);
181}
182
183static void amd100_set_piomode(struct ata_port *ap, struct ata_device *adev)
184{
185 timing_setup(ap, adev, 0x40, adev->pio_mode, 3);
186}
187
188static void amd133_set_piomode(struct ata_port *ap, struct ata_device *adev)
189{
190 timing_setup(ap, adev, 0x40, adev->pio_mode, 4);
191}
192
193/**
194 * amd33_set_dmamode - set initial DMA mode data
195 * @ap: ATA interface
196 * @adev: ATA device
197 *
198 * Program the MWDMA/UDMA modes for the AMD and Nvidia
199 * chipset.
200 */
201
202static void amd33_set_dmamode(struct ata_port *ap, struct ata_device *adev)
203{
204 timing_setup(ap, adev, 0x40, adev->dma_mode, 1);
205}
206
207static void amd66_set_dmamode(struct ata_port *ap, struct ata_device *adev)
208{
209 timing_setup(ap, adev, 0x40, adev->dma_mode, 2);
210}
211
212static void amd100_set_dmamode(struct ata_port *ap, struct ata_device *adev)
213{
214 timing_setup(ap, adev, 0x40, adev->dma_mode, 3);
215}
216
217static void amd133_set_dmamode(struct ata_port *ap, struct ata_device *adev)
218{
219 timing_setup(ap, adev, 0x40, adev->dma_mode, 4);
220}
221
Tejun Heoce54d162007-12-18 16:33:07 +0900222/* Both host-side and drive-side detection results are worthless on NV
223 * PATAs. Ignore them and just follow what BIOS configured. Both the
224 * current configuration in PCI config reg and ACPI GTM result are
225 * cached during driver attach and are consulted to select transfer
226 * mode.
227 */
228static unsigned long nv_mode_filter(struct ata_device *dev,
229 unsigned long xfer_mask)
230{
231 static const unsigned int udma_mask_map[] =
232 { ATA_UDMA2, ATA_UDMA1, ATA_UDMA0, 0,
233 ATA_UDMA3, ATA_UDMA4, ATA_UDMA5, ATA_UDMA6 };
234 struct ata_port *ap = dev->link->ap;
235 char acpi_str[32] = "";
236 u32 saved_udma, udma;
237 const struct ata_acpi_gtm *gtm;
238 unsigned long bios_limit = 0, acpi_limit = 0, limit;
239
240 /* find out what BIOS configured */
241 udma = saved_udma = (unsigned long)ap->host->private_data;
242
243 if (ap->port_no == 0)
244 udma >>= 16;
245 if (dev->devno == 0)
246 udma >>= 8;
247
248 if ((udma & 0xc0) == 0xc0)
249 bios_limit = ata_pack_xfermask(0, 0, udma_mask_map[udma & 0x7]);
250
251 /* consult ACPI GTM too */
252 gtm = ata_acpi_init_gtm(ap);
253 if (gtm) {
254 acpi_limit = ata_acpi_gtm_xfermask(dev, gtm);
255
256 snprintf(acpi_str, sizeof(acpi_str), " (%u:%u:0x%x)",
257 gtm->drive[0].dma, gtm->drive[1].dma, gtm->flags);
258 }
259
260 /* be optimistic, EH can take care of things if something goes wrong */
261 limit = bios_limit | acpi_limit;
262
263 /* If PIO or DMA isn't configured at all, don't limit. Let EH
264 * handle it.
265 */
266 if (!(limit & ATA_MASK_PIO))
267 limit |= ATA_MASK_PIO;
268 if (!(limit & (ATA_MASK_MWDMA | ATA_MASK_UDMA)))
269 limit |= ATA_MASK_MWDMA | ATA_MASK_UDMA;
270
271 ata_port_printk(ap, KERN_DEBUG, "nv_mode_filter: 0x%lx&0x%lx->0x%lx, "
272 "BIOS=0x%lx (0x%x) ACPI=0x%lx%s\n",
273 xfer_mask, limit, xfer_mask & limit, bios_limit,
274 saved_udma, acpi_limit, acpi_str);
275
276 return xfer_mask & limit;
277}
Jeff Garzik669a5db2006-08-29 18:12:40 -0400278
279/**
280 * nv_probe_init - cable detection
Tejun Heocc0680a2007-08-06 18:36:23 +0900281 * @lin: ATA link
Jeff Garzik669a5db2006-08-29 18:12:40 -0400282 *
283 * Perform cable detection. The BIOS stores this in PCI config
284 * space for us.
285 */
286
Tejun Heocc0680a2007-08-06 18:36:23 +0900287static int nv_pre_reset(struct ata_link *link, unsigned long deadline)
Tejun Heod4b2bab2007-02-02 16:50:52 +0900288{
Alan Cox76ff3c62006-09-12 17:14:03 +0100289 static const struct pci_bits nv_enable_bits[] = {
290 { 0x50, 1, 0x02, 0x02 },
291 { 0x50, 1, 0x01, 0x01 }
292 };
Jeff Garzik669a5db2006-08-29 18:12:40 -0400293
Tejun Heocc0680a2007-08-06 18:36:23 +0900294 struct ata_port *ap = link->ap;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400295 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400296
Alan Coxc9619222006-09-26 17:53:38 +0100297 if (!pci_test_config_bits(pdev, &nv_enable_bits[ap->port_no]))
298 return -ENOENT;
Alan Cox76ff3c62006-09-12 17:14:03 +0100299
Tejun Heocc0680a2007-08-06 18:36:23 +0900300 return ata_std_prereset(link, deadline);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400301}
302
303static void nv_error_handler(struct ata_port *ap)
304{
305 ata_bmdma_drive_eh(ap, nv_pre_reset,
306 ata_std_softreset, NULL,
307 ata_std_postreset);
308}
Alan Coxeb4a2c72007-04-11 00:04:20 +0100309
Jeff Garzik669a5db2006-08-29 18:12:40 -0400310/**
311 * nv100_set_piomode - set initial PIO mode data
312 * @ap: ATA interface
313 * @adev: ATA device
314 *
315 * Program the AMD registers for PIO mode.
316 */
317
318static void nv100_set_piomode(struct ata_port *ap, struct ata_device *adev)
319{
320 timing_setup(ap, adev, 0x50, adev->pio_mode, 3);
321}
322
323static void nv133_set_piomode(struct ata_port *ap, struct ata_device *adev)
324{
325 timing_setup(ap, adev, 0x50, adev->pio_mode, 4);
326}
327
328/**
329 * nv100_set_dmamode - set initial DMA mode data
330 * @ap: ATA interface
331 * @adev: ATA device
332 *
333 * Program the MWDMA/UDMA modes for the AMD and Nvidia
334 * chipset.
335 */
336
337static void nv100_set_dmamode(struct ata_port *ap, struct ata_device *adev)
338{
339 timing_setup(ap, adev, 0x50, adev->dma_mode, 3);
340}
341
342static void nv133_set_dmamode(struct ata_port *ap, struct ata_device *adev)
343{
344 timing_setup(ap, adev, 0x50, adev->dma_mode, 4);
345}
346
Tejun Heoce54d162007-12-18 16:33:07 +0900347static void nv_host_stop(struct ata_host *host)
348{
349 u32 udma = (unsigned long)host->private_data;
350
351 /* restore PCI config register 0x60 */
352 pci_write_config_dword(to_pci_dev(host->dev), 0x60, udma);
353}
354
Jeff Garzik669a5db2006-08-29 18:12:40 -0400355static struct scsi_host_template amd_sht = {
Tejun Heo68d1d072008-03-25 12:22:49 +0900356 ATA_BMDMA_SHT(DRV_NAME),
Jeff Garzik669a5db2006-08-29 18:12:40 -0400357};
358
Tejun Heo029cfd62008-03-25 12:22:49 +0900359static const struct ata_port_operations amd_base_port_ops = {
360 .inherits = &ata_bmdma_port_ops,
361 .error_handler = amd_error_handler,
362};
363
Jeff Garzik669a5db2006-08-29 18:12:40 -0400364static struct ata_port_operations amd33_port_ops = {
Tejun Heo029cfd62008-03-25 12:22:49 +0900365 .inherits = &amd_base_port_ops,
366 .cable_detect = ata_cable_40wire,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400367 .set_piomode = amd33_set_piomode,
368 .set_dmamode = amd33_set_dmamode,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400369};
370
371static struct ata_port_operations amd66_port_ops = {
Tejun Heo029cfd62008-03-25 12:22:49 +0900372 .inherits = &amd_base_port_ops,
373 .cable_detect = ata_cable_unknown,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400374 .set_piomode = amd66_set_piomode,
375 .set_dmamode = amd66_set_dmamode,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400376};
377
378static struct ata_port_operations amd100_port_ops = {
Tejun Heo029cfd62008-03-25 12:22:49 +0900379 .inherits = &amd_base_port_ops,
380 .cable_detect = ata_cable_unknown,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400381 .set_piomode = amd100_set_piomode,
382 .set_dmamode = amd100_set_dmamode,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400383};
384
385static struct ata_port_operations amd133_port_ops = {
Tejun Heo029cfd62008-03-25 12:22:49 +0900386 .inherits = &amd_base_port_ops,
387 .cable_detect = amd_cable_detect,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400388 .set_piomode = amd133_set_piomode,
389 .set_dmamode = amd133_set_dmamode,
Tejun Heo029cfd62008-03-25 12:22:49 +0900390};
Jeff Garzik669a5db2006-08-29 18:12:40 -0400391
Tejun Heo029cfd62008-03-25 12:22:49 +0900392static const struct ata_port_operations nv_base_port_ops = {
393 .inherits = &ata_bmdma_port_ops,
394 .cable_detect = ata_cable_ignore,
395 .mode_filter = nv_mode_filter,
396 .error_handler = nv_error_handler,
397 .host_stop = nv_host_stop,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400398};
399
400static struct ata_port_operations nv100_port_ops = {
Tejun Heo029cfd62008-03-25 12:22:49 +0900401 .inherits = &nv_base_port_ops,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400402 .set_piomode = nv100_set_piomode,
403 .set_dmamode = nv100_set_dmamode,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400404};
405
406static struct ata_port_operations nv133_port_ops = {
Tejun Heo029cfd62008-03-25 12:22:49 +0900407 .inherits = &nv_base_port_ops,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400408 .set_piomode = nv133_set_piomode,
409 .set_dmamode = nv133_set_dmamode,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400410};
411
412static int amd_init_one(struct pci_dev *pdev, const struct pci_device_id *id)
413{
Tejun Heo1626aeb2007-05-04 12:43:58 +0200414 static const struct ata_port_info info[10] = {
Jeff Garzik669a5db2006-08-29 18:12:40 -0400415 { /* 0: AMD 7401 */
416 .sht = &amd_sht,
Jeff Garzik1d2808f2007-05-28 06:59:48 -0400417 .flags = ATA_FLAG_SLAVE_POSS,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400418 .pio_mask = 0x1f,
419 .mwdma_mask = 0x07, /* No SWDMA */
420 .udma_mask = 0x07, /* UDMA 33 */
421 .port_ops = &amd33_port_ops
422 },
423 { /* 1: Early AMD7409 - no swdma */
424 .sht = &amd_sht,
Jeff Garzik1d2808f2007-05-28 06:59:48 -0400425 .flags = ATA_FLAG_SLAVE_POSS,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400426 .pio_mask = 0x1f,
427 .mwdma_mask = 0x07,
Jeff Garzikbf6263a2007-07-09 12:16:50 -0400428 .udma_mask = ATA_UDMA4, /* UDMA 66 */
Jeff Garzik669a5db2006-08-29 18:12:40 -0400429 .port_ops = &amd66_port_ops
430 },
431 { /* 2: AMD 7409, no swdma errata */
432 .sht = &amd_sht,
Jeff Garzik1d2808f2007-05-28 06:59:48 -0400433 .flags = ATA_FLAG_SLAVE_POSS,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400434 .pio_mask = 0x1f,
435 .mwdma_mask = 0x07,
Jeff Garzikbf6263a2007-07-09 12:16:50 -0400436 .udma_mask = ATA_UDMA4, /* UDMA 66 */
Jeff Garzik669a5db2006-08-29 18:12:40 -0400437 .port_ops = &amd66_port_ops
438 },
439 { /* 3: AMD 7411 */
440 .sht = &amd_sht,
Jeff Garzik1d2808f2007-05-28 06:59:48 -0400441 .flags = ATA_FLAG_SLAVE_POSS,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400442 .pio_mask = 0x1f,
443 .mwdma_mask = 0x07,
Jeff Garzikbf6263a2007-07-09 12:16:50 -0400444 .udma_mask = ATA_UDMA5, /* UDMA 100 */
Jeff Garzik669a5db2006-08-29 18:12:40 -0400445 .port_ops = &amd100_port_ops
446 },
447 { /* 4: AMD 7441 */
448 .sht = &amd_sht,
Jeff Garzik1d2808f2007-05-28 06:59:48 -0400449 .flags = ATA_FLAG_SLAVE_POSS,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400450 .pio_mask = 0x1f,
451 .mwdma_mask = 0x07,
Jeff Garzikbf6263a2007-07-09 12:16:50 -0400452 .udma_mask = ATA_UDMA5, /* UDMA 100 */
Jeff Garzik669a5db2006-08-29 18:12:40 -0400453 .port_ops = &amd100_port_ops
454 },
455 { /* 5: AMD 8111*/
456 .sht = &amd_sht,
Jeff Garzik1d2808f2007-05-28 06:59:48 -0400457 .flags = ATA_FLAG_SLAVE_POSS,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400458 .pio_mask = 0x1f,
459 .mwdma_mask = 0x07,
Jeff Garzikbf6263a2007-07-09 12:16:50 -0400460 .udma_mask = ATA_UDMA6, /* UDMA 133, no swdma */
Jeff Garzik669a5db2006-08-29 18:12:40 -0400461 .port_ops = &amd133_port_ops
462 },
463 { /* 6: AMD 8111 UDMA 100 (Serenade) */
464 .sht = &amd_sht,
Jeff Garzik1d2808f2007-05-28 06:59:48 -0400465 .flags = ATA_FLAG_SLAVE_POSS,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400466 .pio_mask = 0x1f,
467 .mwdma_mask = 0x07,
Jeff Garzikbf6263a2007-07-09 12:16:50 -0400468 .udma_mask = ATA_UDMA5, /* UDMA 100, no swdma */
Jeff Garzik669a5db2006-08-29 18:12:40 -0400469 .port_ops = &amd133_port_ops
470 },
471 { /* 7: Nvidia Nforce */
472 .sht = &amd_sht,
Jeff Garzik1d2808f2007-05-28 06:59:48 -0400473 .flags = ATA_FLAG_SLAVE_POSS,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400474 .pio_mask = 0x1f,
475 .mwdma_mask = 0x07,
Jeff Garzikbf6263a2007-07-09 12:16:50 -0400476 .udma_mask = ATA_UDMA5, /* UDMA 100 */
Jeff Garzik669a5db2006-08-29 18:12:40 -0400477 .port_ops = &nv100_port_ops
478 },
479 { /* 8: Nvidia Nforce2 and later */
480 .sht = &amd_sht,
Jeff Garzik1d2808f2007-05-28 06:59:48 -0400481 .flags = ATA_FLAG_SLAVE_POSS,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400482 .pio_mask = 0x1f,
483 .mwdma_mask = 0x07,
Jeff Garzikbf6263a2007-07-09 12:16:50 -0400484 .udma_mask = ATA_UDMA6, /* UDMA 133, no swdma */
Jeff Garzik669a5db2006-08-29 18:12:40 -0400485 .port_ops = &nv133_port_ops
486 },
487 { /* 9: AMD CS5536 (Geode companion) */
488 .sht = &amd_sht,
Jeff Garzik1d2808f2007-05-28 06:59:48 -0400489 .flags = ATA_FLAG_SLAVE_POSS,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400490 .pio_mask = 0x1f,
491 .mwdma_mask = 0x07,
Jeff Garzikbf6263a2007-07-09 12:16:50 -0400492 .udma_mask = ATA_UDMA5, /* UDMA 100 */
Jeff Garzik669a5db2006-08-29 18:12:40 -0400493 .port_ops = &amd100_port_ops
494 }
495 };
Tejun Heoce54d162007-12-18 16:33:07 +0900496 struct ata_port_info pi;
497 const struct ata_port_info *ppi[] = { &pi, NULL };
Jeff Garzik669a5db2006-08-29 18:12:40 -0400498 static int printed_version;
499 int type = id->driver_data;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400500 u8 fifo;
Tejun Heof08048e2008-03-25 12:22:47 +0900501 int rc;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400502
503 if (!printed_version++)
504 dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
505
Tejun Heof08048e2008-03-25 12:22:47 +0900506 rc = pcim_enable_device(pdev);
507 if (rc)
508 return rc;
509
Jeff Garzik669a5db2006-08-29 18:12:40 -0400510 pci_read_config_byte(pdev, 0x41, &fifo);
511
512 /* Check for AMD7409 without swdma errata and if found adjust type */
Auke Kok44c10132007-06-08 15:46:36 -0700513 if (type == 1 && pdev->revision > 0x7)
Jeff Garzik669a5db2006-08-29 18:12:40 -0400514 type = 2;
515
Tejun Heoce54d162007-12-18 16:33:07 +0900516 /* Serenade ? */
517 if (type == 5 && pdev->subsystem_vendor == PCI_VENDOR_ID_AMD &&
518 pdev->subsystem_device == PCI_DEVICE_ID_AMD_SERENADE)
519 type = 6; /* UDMA 100 only */
520
521 /*
522 * Okay, type is determined now. Apply type-specific workarounds.
523 */
524 pi = info[type];
525
526 if (type < 3)
527 ata_pci_clear_simplex(pdev);
528
Jeff Garzik669a5db2006-08-29 18:12:40 -0400529 /* Check for AMD7411 */
530 if (type == 3)
531 /* FIFO is broken */
532 pci_write_config_byte(pdev, 0x41, fifo & 0x0F);
533 else
534 pci_write_config_byte(pdev, 0x41, fifo | 0xF0);
535
Tejun Heoce54d162007-12-18 16:33:07 +0900536 /* Cable detection on Nvidia chips doesn't work too well,
537 * cache BIOS programmed UDMA mode.
538 */
539 if (type == 7 || type == 8) {
540 u32 udma;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400541
Tejun Heoce54d162007-12-18 16:33:07 +0900542 pci_read_config_dword(pdev, 0x60, &udma);
543 pi.private_data = (void *)(unsigned long)udma;
544 }
Jeff Garzik669a5db2006-08-29 18:12:40 -0400545
546 /* And fire it up */
Tejun Heo1626aeb2007-05-04 12:43:58 +0200547 return ata_pci_init_one(pdev, ppi);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400548}
549
Tejun Heo438ac6d2007-03-02 17:31:26 +0900550#ifdef CONFIG_PM
Alanc3041932006-11-27 16:21:24 +0000551static int amd_reinit_one(struct pci_dev *pdev)
552{
Tejun Heof08048e2008-03-25 12:22:47 +0900553 struct ata_host *host = dev_get_drvdata(&pdev->dev);
554 int rc;
555
556 rc = ata_pci_device_do_resume(pdev);
557 if (rc)
558 return rc;
559
Alanc3041932006-11-27 16:21:24 +0000560 if (pdev->vendor == PCI_VENDOR_ID_AMD) {
561 u8 fifo;
562 pci_read_config_byte(pdev, 0x41, &fifo);
563 if (pdev->device == PCI_DEVICE_ID_AMD_VIPER_7411)
564 /* FIFO is broken */
565 pci_write_config_byte(pdev, 0x41, fifo & 0x0F);
566 else
567 pci_write_config_byte(pdev, 0x41, fifo | 0xF0);
568 if (pdev->device == PCI_DEVICE_ID_AMD_VIPER_7409 ||
569 pdev->device == PCI_DEVICE_ID_AMD_COBRA_7401)
570 ata_pci_clear_simplex(pdev);
571 }
Tejun Heof08048e2008-03-25 12:22:47 +0900572
573 ata_host_resume(host);
574 return 0;
Alanc3041932006-11-27 16:21:24 +0000575}
Tejun Heo438ac6d2007-03-02 17:31:26 +0900576#endif
Alanc3041932006-11-27 16:21:24 +0000577
Jeff Garzik669a5db2006-08-29 18:12:40 -0400578static const struct pci_device_id amd[] = {
Jeff Garzik2d2744f2006-09-28 20:21:59 -0400579 { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_COBRA_7401), 0 },
580 { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_VIPER_7409), 1 },
581 { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_VIPER_7411), 3 },
582 { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_OPUS_7441), 4 },
583 { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_8111_IDE), 5 },
584 { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_IDE), 7 },
585 { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE2_IDE), 8 },
586 { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE2S_IDE), 8 },
587 { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE3_IDE), 8 },
588 { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE3S_IDE), 8 },
589 { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_CK804_IDE), 8 },
590 { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP04_IDE), 8 },
591 { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP51_IDE), 8 },
592 { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP55_IDE), 8 },
593 { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP61_IDE), 8 },
Peer Chen05e28672006-11-02 17:58:21 -0500594 { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP65_IDE), 8 },
595 { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP67_IDE), 8 },
Peer Chen9f789752007-06-07 18:23:12 +0800596 { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP73_IDE), 8 },
597 { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP77_IDE), 8 },
Jeff Garzik2d2744f2006-09-28 20:21:59 -0400598 { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_CS5536_IDE), 9 },
599
600 { },
Jeff Garzik669a5db2006-08-29 18:12:40 -0400601};
602
603static struct pci_driver amd_pci_driver = {
Jeff Garzik2d2744f2006-09-28 20:21:59 -0400604 .name = DRV_NAME,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400605 .id_table = amd,
606 .probe = amd_init_one,
Alanc3041932006-11-27 16:21:24 +0000607 .remove = ata_pci_remove_one,
Tejun Heo438ac6d2007-03-02 17:31:26 +0900608#ifdef CONFIG_PM
Alanc3041932006-11-27 16:21:24 +0000609 .suspend = ata_pci_device_suspend,
610 .resume = amd_reinit_one,
Tejun Heo438ac6d2007-03-02 17:31:26 +0900611#endif
Jeff Garzik669a5db2006-08-29 18:12:40 -0400612};
613
614static int __init amd_init(void)
615{
616 return pci_register_driver(&amd_pci_driver);
617}
618
619static void __exit amd_exit(void)
620{
621 pci_unregister_driver(&amd_pci_driver);
622}
623
Jeff Garzik669a5db2006-08-29 18:12:40 -0400624MODULE_AUTHOR("Alan Cox");
Alan Coxc9544bc2008-02-08 15:22:39 +0000625MODULE_DESCRIPTION("low-level driver for AMD and Nvidia PATA IDE");
Jeff Garzik669a5db2006-08-29 18:12:40 -0400626MODULE_LICENSE("GPL");
627MODULE_DEVICE_TABLE(pci, amd);
628MODULE_VERSION(DRV_VERSION);
629
630module_init(amd_init);
631module_exit(amd_exit);