Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / ata / pata_hpt37x.c
blobd8a4e213fe249183d060c68d700bb79d09b6a452
1 /*
2 * Libata driver for the highpoint 37x and 30x UDMA66 ATA controllers.
4 * This driver is heavily based upon:
6 * linux/drivers/ide/pci/hpt366.c Version 0.36 April 25, 2003
8 * Copyright (C) 1999-2003 Andre Hedrick <andre@linux-ide.org>
9 * Portions Copyright (C) 2001 Sun Microsystems, Inc.
10 * Portions Copyright (C) 2003 Red Hat Inc
11 * Portions Copyright (C) 2005-2007 MontaVista Software, Inc.
13 * TODO
14 * Look into engine reset on timeout errors. Should not be required.
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/pci.h>
20 #include <linux/init.h>
21 #include <linux/blkdev.h>
22 #include <linux/delay.h>
23 #include <scsi/scsi_host.h>
24 #include <linux/libata.h>
26 #define DRV_NAME "pata_hpt37x"
27 <<<<<<< HEAD:drivers/ata/pata_hpt37x.c
28 #define DRV_VERSION "0.6.9"
29 =======
30 #define DRV_VERSION "0.6.11"
31 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/pata_hpt37x.c
33 struct hpt_clock {
34 u8 xfer_speed;
35 u32 timing;
38 struct hpt_chip {
39 const char *name;
40 unsigned int base;
41 struct hpt_clock const *clocks[4];
44 /* key for bus clock timings
45 * bit
46 * 0:3 data_high_time. inactive time of DIOW_/DIOR_ for PIO and MW
47 * DMA. cycles = value + 1
48 * 4:8 data_low_time. active time of DIOW_/DIOR_ for PIO and MW
49 * DMA. cycles = value + 1
50 * 9:12 cmd_high_time. inactive time of DIOW_/DIOR_ during task file
51 * register access.
52 * 13:17 cmd_low_time. active time of DIOW_/DIOR_ during task file
53 * register access.
54 * 18:21 udma_cycle_time. clock freq and clock cycles for UDMA xfer.
55 * during task file register access.
56 * 22:24 pre_high_time. time to initialize 1st cycle for PIO and MW DMA
57 * xfer.
58 * 25:27 cmd_pre_high_time. time to initialize 1st PIO cycle for task
59 * register access.
60 * 28 UDMA enable
61 * 29 DMA enable
62 * 30 PIO_MST enable. if set, the chip is in bus master mode during
63 * PIO.
64 * 31 FIFO enable.
67 static struct hpt_clock hpt37x_timings_33[] = {
68 { XFER_UDMA_6, 0x12446231 }, /* 0x12646231 ?? */
69 { XFER_UDMA_5, 0x12446231 },
70 { XFER_UDMA_4, 0x12446231 },
71 { XFER_UDMA_3, 0x126c6231 },
72 { XFER_UDMA_2, 0x12486231 },
73 { XFER_UDMA_1, 0x124c6233 },
74 { XFER_UDMA_0, 0x12506297 },
76 { XFER_MW_DMA_2, 0x22406c31 },
77 { XFER_MW_DMA_1, 0x22406c33 },
78 { XFER_MW_DMA_0, 0x22406c97 },
80 { XFER_PIO_4, 0x06414e31 },
81 { XFER_PIO_3, 0x06414e42 },
82 { XFER_PIO_2, 0x06414e53 },
83 { XFER_PIO_1, 0x06814e93 },
84 { XFER_PIO_0, 0x06814ea7 }
87 static struct hpt_clock hpt37x_timings_50[] = {
88 { XFER_UDMA_6, 0x12848242 },
89 { XFER_UDMA_5, 0x12848242 },
90 { XFER_UDMA_4, 0x12ac8242 },
91 { XFER_UDMA_3, 0x128c8242 },
92 { XFER_UDMA_2, 0x120c8242 },
93 { XFER_UDMA_1, 0x12148254 },
94 { XFER_UDMA_0, 0x121882ea },
96 { XFER_MW_DMA_2, 0x22808242 },
97 { XFER_MW_DMA_1, 0x22808254 },
98 { XFER_MW_DMA_0, 0x228082ea },
100 { XFER_PIO_4, 0x0a81f442 },
101 { XFER_PIO_3, 0x0a81f443 },
102 { XFER_PIO_2, 0x0a81f454 },
103 { XFER_PIO_1, 0x0ac1f465 },
104 { XFER_PIO_0, 0x0ac1f48a }
107 static struct hpt_clock hpt37x_timings_66[] = {
108 { XFER_UDMA_6, 0x1c869c62 },
109 { XFER_UDMA_5, 0x1cae9c62 }, /* 0x1c8a9c62 */
110 { XFER_UDMA_4, 0x1c8a9c62 },
111 { XFER_UDMA_3, 0x1c8e9c62 },
112 { XFER_UDMA_2, 0x1c929c62 },
113 { XFER_UDMA_1, 0x1c9a9c62 },
114 { XFER_UDMA_0, 0x1c829c62 },
116 { XFER_MW_DMA_2, 0x2c829c62 },
117 { XFER_MW_DMA_1, 0x2c829c66 },
118 { XFER_MW_DMA_0, 0x2c829d2e },
120 { XFER_PIO_4, 0x0c829c62 },
121 { XFER_PIO_3, 0x0c829c84 },
122 { XFER_PIO_2, 0x0c829ca6 },
123 { XFER_PIO_1, 0x0d029d26 },
124 { XFER_PIO_0, 0x0d029d5e }
128 static const struct hpt_chip hpt370 = {
129 "HPT370",
132 hpt37x_timings_33,
133 NULL,
134 NULL,
135 NULL
139 static const struct hpt_chip hpt370a = {
140 "HPT370A",
143 hpt37x_timings_33,
144 NULL,
145 hpt37x_timings_50,
146 NULL
150 static const struct hpt_chip hpt372 = {
151 "HPT372",
154 hpt37x_timings_33,
155 NULL,
156 hpt37x_timings_50,
157 hpt37x_timings_66
161 static const struct hpt_chip hpt302 = {
162 "HPT302",
165 hpt37x_timings_33,
166 NULL,
167 hpt37x_timings_50,
168 hpt37x_timings_66
172 static const struct hpt_chip hpt371 = {
173 "HPT371",
176 hpt37x_timings_33,
177 NULL,
178 hpt37x_timings_50,
179 hpt37x_timings_66
183 static const struct hpt_chip hpt372a = {
184 "HPT372A",
187 hpt37x_timings_33,
188 NULL,
189 hpt37x_timings_50,
190 hpt37x_timings_66
194 static const struct hpt_chip hpt374 = {
195 "HPT374",
198 hpt37x_timings_33,
199 NULL,
200 NULL,
201 NULL
206 * hpt37x_find_mode - reset the hpt37x bus
207 * @ap: ATA port
208 * @speed: transfer mode
210 * Return the 32bit register programming information for this channel
211 * that matches the speed provided.
214 static u32 hpt37x_find_mode(struct ata_port *ap, int speed)
216 struct hpt_clock *clocks = ap->host->private_data;
218 while(clocks->xfer_speed) {
219 if (clocks->xfer_speed == speed)
220 return clocks->timing;
221 clocks++;
223 BUG();
224 return 0xffffffffU; /* silence compiler warning */
227 static int hpt_dma_blacklisted(const struct ata_device *dev, char *modestr, const char *list[])
229 unsigned char model_num[ATA_ID_PROD_LEN + 1];
230 int i = 0;
232 ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
234 while (list[i] != NULL) {
235 if (!strcmp(list[i], model_num)) {
236 printk(KERN_WARNING DRV_NAME ": %s is not supported for %s.\n",
237 modestr, list[i]);
238 return 1;
240 i++;
242 return 0;
245 static const char *bad_ata33[] = {
246 "Maxtor 92720U8", "Maxtor 92040U6", "Maxtor 91360U4", "Maxtor 91020U3", "Maxtor 90845U3", "Maxtor 90650U2",
247 "Maxtor 91360D8", "Maxtor 91190D7", "Maxtor 91020D6", "Maxtor 90845D5", "Maxtor 90680D4", "Maxtor 90510D3", "Maxtor 90340D2",
248 "Maxtor 91152D8", "Maxtor 91008D7", "Maxtor 90845D6", "Maxtor 90840D6", "Maxtor 90720D5", "Maxtor 90648D5", "Maxtor 90576D4",
249 "Maxtor 90510D4",
250 "Maxtor 90432D3", "Maxtor 90288D2", "Maxtor 90256D2",
251 "Maxtor 91000D8", "Maxtor 90910D8", "Maxtor 90875D7", "Maxtor 90840D7", "Maxtor 90750D6", "Maxtor 90625D5", "Maxtor 90500D4",
252 "Maxtor 91728D8", "Maxtor 91512D7", "Maxtor 91303D6", "Maxtor 91080D5", "Maxtor 90845D4", "Maxtor 90680D4", "Maxtor 90648D3", "Maxtor 90432D2",
253 NULL
256 static const char *bad_ata100_5[] = {
257 "IBM-DTLA-307075",
258 "IBM-DTLA-307060",
259 "IBM-DTLA-307045",
260 "IBM-DTLA-307030",
261 "IBM-DTLA-307020",
262 "IBM-DTLA-307015",
263 "IBM-DTLA-305040",
264 "IBM-DTLA-305030",
265 "IBM-DTLA-305020",
266 "IC35L010AVER07-0",
267 "IC35L020AVER07-0",
268 "IC35L030AVER07-0",
269 "IC35L040AVER07-0",
270 "IC35L060AVER07-0",
271 "WDC AC310200R",
272 NULL
276 * hpt370_filter - mode selection filter
277 * @adev: ATA device
279 * Block UDMA on devices that cause trouble with this controller.
282 static unsigned long hpt370_filter(struct ata_device *adev, unsigned long mask)
284 if (adev->class == ATA_DEV_ATA) {
285 if (hpt_dma_blacklisted(adev, "UDMA", bad_ata33))
286 mask &= ~ATA_MASK_UDMA;
287 if (hpt_dma_blacklisted(adev, "UDMA100", bad_ata100_5))
288 <<<<<<< HEAD:drivers/ata/pata_hpt37x.c
289 mask &= ~(0x1F << ATA_SHIFT_UDMA);
290 =======
291 mask &= ~(0xE0 << ATA_SHIFT_UDMA);
292 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/pata_hpt37x.c
294 return ata_pci_default_filter(adev, mask);
298 * hpt370a_filter - mode selection filter
299 * @adev: ATA device
301 * Block UDMA on devices that cause trouble with this controller.
304 static unsigned long hpt370a_filter(struct ata_device *adev, unsigned long mask)
306 if (adev->class == ATA_DEV_ATA) {
307 if (hpt_dma_blacklisted(adev, "UDMA100", bad_ata100_5))
308 <<<<<<< HEAD:drivers/ata/pata_hpt37x.c
309 mask &= ~ (0x1F << ATA_SHIFT_UDMA);
310 =======
311 mask &= ~(0xE0 << ATA_SHIFT_UDMA);
312 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/pata_hpt37x.c
314 return ata_pci_default_filter(adev, mask);
318 * hpt37x_pre_reset - reset the hpt37x bus
319 * @link: ATA link to reset
320 * @deadline: deadline jiffies for the operation
322 * Perform the initial reset handling for the 370/372 and 374 func 0
325 static int hpt37x_pre_reset(struct ata_link *link, unsigned long deadline)
327 u8 scr2, ata66;
328 struct ata_port *ap = link->ap;
329 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
330 static const struct pci_bits hpt37x_enable_bits[] = {
331 { 0x50, 1, 0x04, 0x04 },
332 { 0x54, 1, 0x04, 0x04 }
334 if (!pci_test_config_bits(pdev, &hpt37x_enable_bits[ap->port_no]))
335 return -ENOENT;
337 pci_read_config_byte(pdev, 0x5B, &scr2);
338 pci_write_config_byte(pdev, 0x5B, scr2 & ~0x01);
339 /* Cable register now active */
340 pci_read_config_byte(pdev, 0x5A, &ata66);
341 /* Restore state */
342 pci_write_config_byte(pdev, 0x5B, scr2);
344 if (ata66 & (2 >> ap->port_no))
345 ap->cbl = ATA_CBL_PATA40;
346 else
347 ap->cbl = ATA_CBL_PATA80;
349 /* Reset the state machine */
350 pci_write_config_byte(pdev, 0x50 + 4 * ap->port_no, 0x37);
351 udelay(100);
353 return ata_std_prereset(link, deadline);
357 * hpt37x_error_handler - reset the hpt374
358 * @ap: ATA port to reset
360 * Perform probe for HPT37x, except for HPT374 channel 2
363 static void hpt37x_error_handler(struct ata_port *ap)
365 ata_bmdma_drive_eh(ap, hpt37x_pre_reset, ata_std_softreset, NULL, ata_std_postreset);
368 static int hpt374_pre_reset(struct ata_link *link, unsigned long deadline)
370 static const struct pci_bits hpt37x_enable_bits[] = {
371 { 0x50, 1, 0x04, 0x04 },
372 { 0x54, 1, 0x04, 0x04 }
374 u16 mcr3;
375 u8 ata66;
376 struct ata_port *ap = link->ap;
377 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
378 unsigned int mcrbase = 0x50 + 4 * ap->port_no;
380 if (!pci_test_config_bits(pdev, &hpt37x_enable_bits[ap->port_no]))
381 return -ENOENT;
383 /* Do the extra channel work */
384 pci_read_config_word(pdev, mcrbase + 2, &mcr3);
385 /* Set bit 15 of 0x52 to enable TCBLID as input
387 pci_write_config_word(pdev, mcrbase + 2, mcr3 | 0x8000);
388 pci_read_config_byte(pdev, 0x5A, &ata66);
389 /* Reset TCBLID/FCBLID to output */
390 pci_write_config_word(pdev, mcrbase + 2, mcr3);
392 if (ata66 & (2 >> ap->port_no))
393 ap->cbl = ATA_CBL_PATA40;
394 else
395 ap->cbl = ATA_CBL_PATA80;
397 /* Reset the state machine */
398 pci_write_config_byte(pdev, 0x50 + 4 * ap->port_no, 0x37);
399 udelay(100);
401 return ata_std_prereset(link, deadline);
405 * hpt374_error_handler - reset the hpt374
406 * @classes:
408 * The 374 cable detect is a little different due to the extra
409 * channels. The function 0 channels work like usual but function 1
410 * is special
413 static void hpt374_error_handler(struct ata_port *ap)
415 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
417 if (!(PCI_FUNC(pdev->devfn) & 1))
418 hpt37x_error_handler(ap);
419 else
420 ata_bmdma_drive_eh(ap, hpt374_pre_reset, ata_std_softreset, NULL, ata_std_postreset);
424 * hpt370_set_piomode - PIO setup
425 * @ap: ATA interface
426 * @adev: device on the interface
428 * Perform PIO mode setup.
431 static void hpt370_set_piomode(struct ata_port *ap, struct ata_device *adev)
433 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
434 u32 addr1, addr2;
435 u32 reg;
436 u32 mode;
437 u8 fast;
439 addr1 = 0x40 + 4 * (adev->devno + 2 * ap->port_no);
440 addr2 = 0x51 + 4 * ap->port_no;
442 /* Fast interrupt prediction disable, hold off interrupt disable */
443 pci_read_config_byte(pdev, addr2, &fast);
444 fast &= ~0x02;
445 fast |= 0x01;
446 pci_write_config_byte(pdev, addr2, fast);
448 pci_read_config_dword(pdev, addr1, &reg);
449 mode = hpt37x_find_mode(ap, adev->pio_mode);
450 mode &= ~0x8000000; /* No FIFO in PIO */
451 mode &= ~0x30070000; /* Leave config bits alone */
452 reg &= 0x30070000; /* Strip timing bits */
453 pci_write_config_dword(pdev, addr1, reg | mode);
457 * hpt370_set_dmamode - DMA timing setup
458 * @ap: ATA interface
459 * @adev: Device being configured
461 * Set up the channel for MWDMA or UDMA modes. Much the same as with
462 * PIO, load the mode number and then set MWDMA or UDMA flag.
465 static void hpt370_set_dmamode(struct ata_port *ap, struct ata_device *adev)
467 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
468 u32 addr1, addr2;
469 u32 reg;
470 u32 mode;
471 u8 fast;
473 addr1 = 0x40 + 4 * (adev->devno + 2 * ap->port_no);
474 addr2 = 0x51 + 4 * ap->port_no;
476 /* Fast interrupt prediction disable, hold off interrupt disable */
477 pci_read_config_byte(pdev, addr2, &fast);
478 fast &= ~0x02;
479 fast |= 0x01;
480 pci_write_config_byte(pdev, addr2, fast);
482 pci_read_config_dword(pdev, addr1, &reg);
483 mode = hpt37x_find_mode(ap, adev->dma_mode);
484 mode |= 0x8000000; /* FIFO in MWDMA or UDMA */
485 mode &= ~0xC0000000; /* Leave config bits alone */
486 reg &= 0xC0000000; /* Strip timing bits */
487 pci_write_config_dword(pdev, addr1, reg | mode);
491 * hpt370_bmdma_start - DMA engine begin
492 * @qc: ATA command
494 * The 370 and 370A want us to reset the DMA engine each time we
495 * use it. The 372 and later are fine.
498 static void hpt370_bmdma_start(struct ata_queued_cmd *qc)
500 struct ata_port *ap = qc->ap;
501 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
502 pci_write_config_byte(pdev, 0x50 + 4 * ap->port_no, 0x37);
503 udelay(10);
504 ata_bmdma_start(qc);
508 * hpt370_bmdma_end - DMA engine stop
509 * @qc: ATA command
511 * Work around the HPT370 DMA engine.
514 static void hpt370_bmdma_stop(struct ata_queued_cmd *qc)
516 struct ata_port *ap = qc->ap;
517 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
518 u8 dma_stat = ioread8(ap->ioaddr.bmdma_addr + 2);
519 u8 dma_cmd;
520 void __iomem *bmdma = ap->ioaddr.bmdma_addr;
522 if (dma_stat & 0x01) {
523 udelay(20);
524 dma_stat = ioread8(bmdma + 2);
526 if (dma_stat & 0x01) {
527 /* Clear the engine */
528 pci_write_config_byte(pdev, 0x50 + 4 * ap->port_no, 0x37);
529 udelay(10);
530 /* Stop DMA */
531 dma_cmd = ioread8(bmdma );
532 iowrite8(dma_cmd & 0xFE, bmdma);
533 /* Clear Error */
534 dma_stat = ioread8(bmdma + 2);
535 iowrite8(dma_stat | 0x06 , bmdma + 2);
536 /* Clear the engine */
537 pci_write_config_byte(pdev, 0x50 + 4 * ap->port_no, 0x37);
538 udelay(10);
540 ata_bmdma_stop(qc);
544 * hpt372_set_piomode - PIO setup
545 * @ap: ATA interface
546 * @adev: device on the interface
548 * Perform PIO mode setup.
551 static void hpt372_set_piomode(struct ata_port *ap, struct ata_device *adev)
553 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
554 u32 addr1, addr2;
555 u32 reg;
556 u32 mode;
557 u8 fast;
559 addr1 = 0x40 + 4 * (adev->devno + 2 * ap->port_no);
560 addr2 = 0x51 + 4 * ap->port_no;
562 /* Fast interrupt prediction disable, hold off interrupt disable */
563 pci_read_config_byte(pdev, addr2, &fast);
564 fast &= ~0x07;
565 pci_write_config_byte(pdev, addr2, fast);
567 pci_read_config_dword(pdev, addr1, &reg);
568 mode = hpt37x_find_mode(ap, adev->pio_mode);
570 printk("Find mode for %d reports %X\n", adev->pio_mode, mode);
571 mode &= ~0x80000000; /* No FIFO in PIO */
572 mode &= ~0x30070000; /* Leave config bits alone */
573 reg &= 0x30070000; /* Strip timing bits */
574 pci_write_config_dword(pdev, addr1, reg | mode);
578 * hpt372_set_dmamode - DMA timing setup
579 * @ap: ATA interface
580 * @adev: Device being configured
582 * Set up the channel for MWDMA or UDMA modes. Much the same as with
583 * PIO, load the mode number and then set MWDMA or UDMA flag.
586 static void hpt372_set_dmamode(struct ata_port *ap, struct ata_device *adev)
588 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
589 u32 addr1, addr2;
590 u32 reg;
591 u32 mode;
592 u8 fast;
594 addr1 = 0x40 + 4 * (adev->devno + 2 * ap->port_no);
595 addr2 = 0x51 + 4 * ap->port_no;
597 /* Fast interrupt prediction disable, hold off interrupt disable */
598 pci_read_config_byte(pdev, addr2, &fast);
599 fast &= ~0x07;
600 pci_write_config_byte(pdev, addr2, fast);
602 pci_read_config_dword(pdev, addr1, &reg);
603 mode = hpt37x_find_mode(ap, adev->dma_mode);
604 printk("Find mode for DMA %d reports %X\n", adev->dma_mode, mode);
605 mode &= ~0xC0000000; /* Leave config bits alone */
606 mode |= 0x80000000; /* FIFO in MWDMA or UDMA */
607 reg &= 0xC0000000; /* Strip timing bits */
608 pci_write_config_dword(pdev, addr1, reg | mode);
612 * hpt37x_bmdma_end - DMA engine stop
613 * @qc: ATA command
615 * Clean up after the HPT372 and later DMA engine
618 static void hpt37x_bmdma_stop(struct ata_queued_cmd *qc)
620 struct ata_port *ap = qc->ap;
621 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
622 int mscreg = 0x50 + 4 * ap->port_no;
623 u8 bwsr_stat, msc_stat;
625 pci_read_config_byte(pdev, 0x6A, &bwsr_stat);
626 pci_read_config_byte(pdev, mscreg, &msc_stat);
627 if (bwsr_stat & (1 << ap->port_no))
628 pci_write_config_byte(pdev, mscreg, msc_stat | 0x30);
629 ata_bmdma_stop(qc);
633 static struct scsi_host_template hpt37x_sht = {
634 .module = THIS_MODULE,
635 .name = DRV_NAME,
636 .ioctl = ata_scsi_ioctl,
637 .queuecommand = ata_scsi_queuecmd,
638 .can_queue = ATA_DEF_QUEUE,
639 .this_id = ATA_SHT_THIS_ID,
640 .sg_tablesize = LIBATA_MAX_PRD,
641 .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
642 .emulated = ATA_SHT_EMULATED,
643 .use_clustering = ATA_SHT_USE_CLUSTERING,
644 .proc_name = DRV_NAME,
645 .dma_boundary = ATA_DMA_BOUNDARY,
646 .slave_configure = ata_scsi_slave_config,
647 .slave_destroy = ata_scsi_slave_destroy,
648 .bios_param = ata_std_bios_param,
652 * Configuration for HPT370
655 static struct ata_port_operations hpt370_port_ops = {
656 .set_piomode = hpt370_set_piomode,
657 .set_dmamode = hpt370_set_dmamode,
658 .mode_filter = hpt370_filter,
660 .tf_load = ata_tf_load,
661 .tf_read = ata_tf_read,
662 .check_status = ata_check_status,
663 .exec_command = ata_exec_command,
664 .dev_select = ata_std_dev_select,
666 .freeze = ata_bmdma_freeze,
667 .thaw = ata_bmdma_thaw,
668 .error_handler = hpt37x_error_handler,
669 .post_internal_cmd = ata_bmdma_post_internal_cmd,
671 .bmdma_setup = ata_bmdma_setup,
672 .bmdma_start = hpt370_bmdma_start,
673 .bmdma_stop = hpt370_bmdma_stop,
674 .bmdma_status = ata_bmdma_status,
676 .qc_prep = ata_qc_prep,
677 .qc_issue = ata_qc_issue_prot,
679 .data_xfer = ata_data_xfer,
681 .irq_handler = ata_interrupt,
682 .irq_clear = ata_bmdma_irq_clear,
683 .irq_on = ata_irq_on,
685 .port_start = ata_sff_port_start,
689 * Configuration for HPT370A. Close to 370 but less filters
692 static struct ata_port_operations hpt370a_port_ops = {
693 .set_piomode = hpt370_set_piomode,
694 .set_dmamode = hpt370_set_dmamode,
695 .mode_filter = hpt370a_filter,
697 .tf_load = ata_tf_load,
698 .tf_read = ata_tf_read,
699 .check_status = ata_check_status,
700 .exec_command = ata_exec_command,
701 .dev_select = ata_std_dev_select,
703 .freeze = ata_bmdma_freeze,
704 .thaw = ata_bmdma_thaw,
705 .error_handler = hpt37x_error_handler,
706 .post_internal_cmd = ata_bmdma_post_internal_cmd,
708 .bmdma_setup = ata_bmdma_setup,
709 .bmdma_start = hpt370_bmdma_start,
710 .bmdma_stop = hpt370_bmdma_stop,
711 .bmdma_status = ata_bmdma_status,
713 .qc_prep = ata_qc_prep,
714 .qc_issue = ata_qc_issue_prot,
716 .data_xfer = ata_data_xfer,
718 .irq_handler = ata_interrupt,
719 .irq_clear = ata_bmdma_irq_clear,
720 .irq_on = ata_irq_on,
722 .port_start = ata_sff_port_start,
726 * Configuration for HPT372, HPT371, HPT302. Slightly different PIO
727 * and DMA mode setting functionality.
730 static struct ata_port_operations hpt372_port_ops = {
731 .set_piomode = hpt372_set_piomode,
732 .set_dmamode = hpt372_set_dmamode,
733 .mode_filter = ata_pci_default_filter,
735 .tf_load = ata_tf_load,
736 .tf_read = ata_tf_read,
737 .check_status = ata_check_status,
738 .exec_command = ata_exec_command,
739 .dev_select = ata_std_dev_select,
741 .freeze = ata_bmdma_freeze,
742 .thaw = ata_bmdma_thaw,
743 .error_handler = hpt37x_error_handler,
744 .post_internal_cmd = ata_bmdma_post_internal_cmd,
746 .bmdma_setup = ata_bmdma_setup,
747 .bmdma_start = ata_bmdma_start,
748 .bmdma_stop = hpt37x_bmdma_stop,
749 .bmdma_status = ata_bmdma_status,
751 .qc_prep = ata_qc_prep,
752 .qc_issue = ata_qc_issue_prot,
754 .data_xfer = ata_data_xfer,
756 .irq_handler = ata_interrupt,
757 .irq_clear = ata_bmdma_irq_clear,
758 .irq_on = ata_irq_on,
760 .port_start = ata_sff_port_start,
764 * Configuration for HPT374. Mode setting works like 372 and friends
765 * but we have a different cable detection procedure.
768 static struct ata_port_operations hpt374_port_ops = {
769 .set_piomode = hpt372_set_piomode,
770 .set_dmamode = hpt372_set_dmamode,
771 .mode_filter = ata_pci_default_filter,
773 .tf_load = ata_tf_load,
774 .tf_read = ata_tf_read,
775 .check_status = ata_check_status,
776 .exec_command = ata_exec_command,
777 .dev_select = ata_std_dev_select,
779 .freeze = ata_bmdma_freeze,
780 .thaw = ata_bmdma_thaw,
781 .error_handler = hpt374_error_handler,
782 .post_internal_cmd = ata_bmdma_post_internal_cmd,
784 .bmdma_setup = ata_bmdma_setup,
785 .bmdma_start = ata_bmdma_start,
786 .bmdma_stop = hpt37x_bmdma_stop,
787 .bmdma_status = ata_bmdma_status,
789 .qc_prep = ata_qc_prep,
790 .qc_issue = ata_qc_issue_prot,
792 .data_xfer = ata_data_xfer,
794 .irq_handler = ata_interrupt,
795 .irq_clear = ata_bmdma_irq_clear,
796 .irq_on = ata_irq_on,
798 .port_start = ata_sff_port_start,
802 * htp37x_clock_slot - Turn timing to PC clock entry
803 * @freq: Reported frequency timing
804 * @base: Base timing
806 * Turn the timing data intoa clock slot (0 for 33, 1 for 40, 2 for 50
807 * and 3 for 66Mhz)
810 static int hpt37x_clock_slot(unsigned int freq, unsigned int base)
812 unsigned int f = (base * freq) / 192; /* Mhz */
813 if (f < 40)
814 return 0; /* 33Mhz slot */
815 if (f < 45)
816 return 1; /* 40Mhz slot */
817 if (f < 55)
818 return 2; /* 50Mhz slot */
819 return 3; /* 60Mhz slot */
823 * hpt37x_calibrate_dpll - Calibrate the DPLL loop
824 * @dev: PCI device
826 * Perform a calibration cycle on the HPT37x DPLL. Returns 1 if this
827 * succeeds
830 static int hpt37x_calibrate_dpll(struct pci_dev *dev)
832 u8 reg5b;
833 u32 reg5c;
834 int tries;
836 for(tries = 0; tries < 0x5000; tries++) {
837 udelay(50);
838 pci_read_config_byte(dev, 0x5b, &reg5b);
839 if (reg5b & 0x80) {
840 /* See if it stays set */
841 for(tries = 0; tries < 0x1000; tries ++) {
842 pci_read_config_byte(dev, 0x5b, &reg5b);
843 /* Failed ? */
844 if ((reg5b & 0x80) == 0)
845 return 0;
847 /* Turn off tuning, we have the DPLL set */
848 pci_read_config_dword(dev, 0x5c, &reg5c);
849 pci_write_config_dword(dev, 0x5c, reg5c & ~ 0x100);
850 return 1;
853 /* Never went stable */
854 return 0;
857 static u32 hpt374_read_freq(struct pci_dev *pdev)
859 u32 freq;
860 unsigned long io_base = pci_resource_start(pdev, 4);
861 if (PCI_FUNC(pdev->devfn) & 1) {
862 struct pci_dev *pdev_0;
864 pdev_0 = pci_get_slot(pdev->bus, pdev->devfn - 1);
865 /* Someone hot plugged the controller on us ? */
866 if (pdev_0 == NULL)
867 return 0;
868 io_base = pci_resource_start(pdev_0, 4);
869 freq = inl(io_base + 0x90);
870 pci_dev_put(pdev_0);
871 } else
872 freq = inl(io_base + 0x90);
873 return freq;
877 * hpt37x_init_one - Initialise an HPT37X/302
878 * @dev: PCI device
879 * @id: Entry in match table
881 * Initialise an HPT37x device. There are some interesting complications
882 * here. Firstly the chip may report 366 and be one of several variants.
883 * Secondly all the timings depend on the clock for the chip which we must
884 * detect and look up
886 * This is the known chip mappings. It may be missing a couple of later
887 * releases.
889 * Chip version PCI Rev Notes
890 * HPT366 4 (HPT366) 0 Other driver
891 * HPT366 4 (HPT366) 1 Other driver
892 * HPT368 4 (HPT366) 2 Other driver
893 * HPT370 4 (HPT366) 3 UDMA100
894 * HPT370A 4 (HPT366) 4 UDMA100
895 * HPT372 4 (HPT366) 5 UDMA133 (1)
896 * HPT372N 4 (HPT366) 6 Other driver
897 * HPT372A 5 (HPT372) 1 UDMA133 (1)
898 * HPT372N 5 (HPT372) 2 Other driver
899 * HPT302 6 (HPT302) 1 UDMA133
900 * HPT302N 6 (HPT302) 2 Other driver
901 * HPT371 7 (HPT371) * UDMA133
902 * HPT374 8 (HPT374) * UDMA133 4 channel
903 * HPT372N 9 (HPT372N) * Other driver
905 * (1) UDMA133 support depends on the bus clock
908 static int hpt37x_init_one(struct pci_dev *dev, const struct pci_device_id *id)
910 /* HPT370 - UDMA100 */
911 static const struct ata_port_info info_hpt370 = {
912 .sht = &hpt37x_sht,
913 .flags = ATA_FLAG_SLAVE_POSS,
914 .pio_mask = 0x1f,
915 .mwdma_mask = 0x07,
916 .udma_mask = ATA_UDMA5,
917 .port_ops = &hpt370_port_ops
919 /* HPT370A - UDMA100 */
920 static const struct ata_port_info info_hpt370a = {
921 .sht = &hpt37x_sht,
922 .flags = ATA_FLAG_SLAVE_POSS,
923 .pio_mask = 0x1f,
924 .mwdma_mask = 0x07,
925 .udma_mask = ATA_UDMA5,
926 .port_ops = &hpt370a_port_ops
928 /* HPT370 - UDMA100 */
929 static const struct ata_port_info info_hpt370_33 = {
930 .sht = &hpt37x_sht,
931 .flags = ATA_FLAG_SLAVE_POSS,
932 .pio_mask = 0x1f,
933 .mwdma_mask = 0x07,
934 .udma_mask = ATA_UDMA5,
935 .port_ops = &hpt370_port_ops
937 /* HPT370A - UDMA100 */
938 static const struct ata_port_info info_hpt370a_33 = {
939 .sht = &hpt37x_sht,
940 .flags = ATA_FLAG_SLAVE_POSS,
941 .pio_mask = 0x1f,
942 .mwdma_mask = 0x07,
943 .udma_mask = ATA_UDMA5,
944 .port_ops = &hpt370a_port_ops
946 /* HPT371, 372 and friends - UDMA133 */
947 static const struct ata_port_info info_hpt372 = {
948 .sht = &hpt37x_sht,
949 .flags = ATA_FLAG_SLAVE_POSS,
950 .pio_mask = 0x1f,
951 .mwdma_mask = 0x07,
952 .udma_mask = ATA_UDMA6,
953 .port_ops = &hpt372_port_ops
955 /* HPT374 - UDMA100 */
956 static const struct ata_port_info info_hpt374 = {
957 .sht = &hpt37x_sht,
958 .flags = ATA_FLAG_SLAVE_POSS,
959 .pio_mask = 0x1f,
960 .mwdma_mask = 0x07,
961 .udma_mask = ATA_UDMA5,
962 .port_ops = &hpt374_port_ops
965 static const int MHz[4] = { 33, 40, 50, 66 };
966 const struct ata_port_info *port;
967 void *private_data = NULL;
968 struct ata_port_info port_info;
969 const struct ata_port_info *ppi[] = { &port_info, NULL };
971 u8 irqmask;
972 u32 class_rev;
973 u8 mcr1;
974 u32 freq;
975 int prefer_dpll = 1;
977 unsigned long iobase = pci_resource_start(dev, 4);
979 const struct hpt_chip *chip_table;
980 int clock_slot;
982 pci_read_config_dword(dev, PCI_CLASS_REVISION, &class_rev);
983 class_rev &= 0xFF;
985 if (dev->device == PCI_DEVICE_ID_TTI_HPT366) {
986 /* May be a later chip in disguise. Check */
987 /* Older chips are in the HPT366 driver. Ignore them */
988 if (class_rev < 3)
989 return -ENODEV;
990 /* N series chips have their own driver. Ignore */
991 if (class_rev == 6)
992 return -ENODEV;
994 switch(class_rev) {
995 case 3:
996 port = &info_hpt370;
997 chip_table = &hpt370;
998 prefer_dpll = 0;
999 break;
1000 case 4:
1001 port = &info_hpt370a;
1002 chip_table = &hpt370a;
1003 prefer_dpll = 0;
1004 break;
1005 case 5:
1006 port = &info_hpt372;
1007 chip_table = &hpt372;
1008 break;
1009 default:
1010 printk(KERN_ERR "pata_hpt37x: Unknown HPT366 subtype please report (%d).\n", class_rev);
1011 return -ENODEV;
1013 } else {
1014 switch(dev->device) {
1015 case PCI_DEVICE_ID_TTI_HPT372:
1016 /* 372N if rev >= 2*/
1017 if (class_rev >= 2)
1018 return -ENODEV;
1019 port = &info_hpt372;
1020 chip_table = &hpt372a;
1021 break;
1022 case PCI_DEVICE_ID_TTI_HPT302:
1023 /* 302N if rev > 1 */
1024 if (class_rev > 1)
1025 return -ENODEV;
1026 port = &info_hpt372;
1027 /* Check this */
1028 chip_table = &hpt302;
1029 break;
1030 case PCI_DEVICE_ID_TTI_HPT371:
1031 if (class_rev > 1)
1032 return -ENODEV;
1033 port = &info_hpt372;
1034 chip_table = &hpt371;
1035 /* Single channel device, master is not present
1036 but the BIOS (or us for non x86) must mark it
1037 absent */
1038 pci_read_config_byte(dev, 0x50, &mcr1);
1039 mcr1 &= ~0x04;
1040 pci_write_config_byte(dev, 0x50, mcr1);
1041 break;
1042 case PCI_DEVICE_ID_TTI_HPT374:
1043 chip_table = &hpt374;
1044 port = &info_hpt374;
1045 break;
1046 default:
1047 printk(KERN_ERR "pata_hpt37x: PCI table is bogus please report (%d).\n", dev->device);
1048 return -ENODEV;
1051 /* Ok so this is a chip we support */
1053 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, (L1_CACHE_BYTES / 4));
1054 pci_write_config_byte(dev, PCI_LATENCY_TIMER, 0x78);
1055 pci_write_config_byte(dev, PCI_MIN_GNT, 0x08);
1056 pci_write_config_byte(dev, PCI_MAX_LAT, 0x08);
1058 pci_read_config_byte(dev, 0x5A, &irqmask);
1059 irqmask &= ~0x10;
1060 pci_write_config_byte(dev, 0x5a, irqmask);
1063 * default to pci clock. make sure MA15/16 are set to output
1064 * to prevent drives having problems with 40-pin cables. Needed
1065 * for some drives such as IBM-DTLA which will not enter ready
1066 * state on reset when PDIAG is a input.
1069 pci_write_config_byte(dev, 0x5b, 0x23);
1072 * HighPoint does this for HPT372A.
1073 * NOTE: This register is only writeable via I/O space.
1075 if (chip_table == &hpt372a)
1076 outb(0x0e, iobase + 0x9c);
1078 /* Some devices do not let this value be accessed via PCI space
1079 according to the old driver. In addition we must use the value
1080 from FN 0 on the HPT374 */
1082 if (chip_table == &hpt374) {
1083 freq = hpt374_read_freq(dev);
1084 if (freq == 0)
1085 return -ENODEV;
1086 } else
1087 freq = inl(iobase + 0x90);
1089 if ((freq >> 12) != 0xABCDE) {
1090 int i;
1091 u8 sr;
1092 u32 total = 0;
1094 printk(KERN_WARNING "pata_hpt37x: BIOS has not set timing clocks.\n");
1096 /* This is the process the HPT371 BIOS is reported to use */
1097 for(i = 0; i < 128; i++) {
1098 pci_read_config_byte(dev, 0x78, &sr);
1099 total += sr & 0x1FF;
1100 udelay(15);
1102 freq = total / 128;
1104 freq &= 0x1FF;
1107 * Turn the frequency check into a band and then find a timing
1108 * table to match it.
1111 clock_slot = hpt37x_clock_slot(freq, chip_table->base);
1112 if (chip_table->clocks[clock_slot] == NULL || prefer_dpll) {
1114 * We need to try PLL mode instead
1116 * For non UDMA133 capable devices we should
1117 * use a 50MHz DPLL by choice
1119 unsigned int f_low, f_high;
1120 int dpll, adjust;
1122 /* Compute DPLL */
1123 dpll = (port->udma_mask & 0xC0) ? 3 : 2;
1125 f_low = (MHz[clock_slot] * 48) / MHz[dpll];
1126 f_high = f_low + 2;
1127 if (clock_slot > 1)
1128 f_high += 2;
1130 /* Select the DPLL clock. */
1131 pci_write_config_byte(dev, 0x5b, 0x21);
1132 pci_write_config_dword(dev, 0x5C, (f_high << 16) | f_low | 0x100);
1134 for(adjust = 0; adjust < 8; adjust++) {
1135 if (hpt37x_calibrate_dpll(dev))
1136 break;
1137 /* See if it'll settle at a fractionally different clock */
1138 if (adjust & 1)
1139 f_low -= adjust >> 1;
1140 else
1141 f_high += adjust >> 1;
1142 pci_write_config_dword(dev, 0x5C, (f_high << 16) | f_low | 0x100);
1144 if (adjust == 8) {
1145 printk(KERN_ERR "pata_hpt37x: DPLL did not stabilize!\n");
1146 return -ENODEV;
1148 if (dpll == 3)
1149 private_data = (void *)hpt37x_timings_66;
1150 else
1151 private_data = (void *)hpt37x_timings_50;
1153 printk(KERN_INFO "pata_hpt37x: bus clock %dMHz, using %dMHz DPLL.\n",
1154 MHz[clock_slot], MHz[dpll]);
1155 } else {
1156 private_data = (void *)chip_table->clocks[clock_slot];
1158 * Perform a final fixup. Note that we will have used the
1159 * DPLL on the HPT372 which means we don't have to worry
1160 * about lack of UDMA133 support on lower clocks
1163 if (clock_slot < 2 && port == &info_hpt370)
1164 port = &info_hpt370_33;
1165 if (clock_slot < 2 && port == &info_hpt370a)
1166 port = &info_hpt370a_33;
1167 printk(KERN_INFO "pata_hpt37x: %s using %dMHz bus clock.\n",
1168 chip_table->name, MHz[clock_slot]);
1171 /* Now kick off ATA set up */
1172 port_info = *port;
1173 port_info.private_data = private_data;
1175 return ata_pci_init_one(dev, ppi);
1178 static const struct pci_device_id hpt37x[] = {
1179 { PCI_VDEVICE(TTI, PCI_DEVICE_ID_TTI_HPT366), },
1180 { PCI_VDEVICE(TTI, PCI_DEVICE_ID_TTI_HPT371), },
1181 { PCI_VDEVICE(TTI, PCI_DEVICE_ID_TTI_HPT372), },
1182 { PCI_VDEVICE(TTI, PCI_DEVICE_ID_TTI_HPT374), },
1183 { PCI_VDEVICE(TTI, PCI_DEVICE_ID_TTI_HPT302), },
1185 { },
1188 static struct pci_driver hpt37x_pci_driver = {
1189 .name = DRV_NAME,
1190 .id_table = hpt37x,
1191 .probe = hpt37x_init_one,
1192 .remove = ata_pci_remove_one
1195 static int __init hpt37x_init(void)
1197 return pci_register_driver(&hpt37x_pci_driver);
1200 static void __exit hpt37x_exit(void)
1202 pci_unregister_driver(&hpt37x_pci_driver);
1205 MODULE_AUTHOR("Alan Cox");
1206 MODULE_DESCRIPTION("low-level driver for the Highpoint HPT37x/30x");
1207 MODULE_LICENSE("GPL");
1208 MODULE_DEVICE_TABLE(pci, hpt37x);
1209 MODULE_VERSION(DRV_VERSION);
1211 module_init(hpt37x_init);
1212 module_exit(hpt37x_exit);