cmd64x.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. /*
  2. * linux/drivers/ide/pci/cmd64x.c Version 1.53 Dec 24, 2007
  3. *
  4. * cmd64x.c: Enable interrupts at initialization time on Ultra/PCI machines.
  5. * Due to massive hardware bugs, UltraDMA is only supported
  6. * on the 646U2 and not on the 646U.
  7. *
  8. * Copyright (C) 1998 Eddie C. Dost (ecd@skynet.be)
  9. * Copyright (C) 1998 David S. Miller (davem@redhat.com)
  10. *
  11. * Copyright (C) 1999-2002 Andre Hedrick <andre@linux-ide.org>
  12. * Copyright (C) 2007 MontaVista Software, Inc. <source@mvista.com>
  13. */
  14. #include <linux/module.h>
  15. #include <linux/types.h>
  16. #include <linux/pci.h>
  17. #include <linux/delay.h>
  18. #include <linux/hdreg.h>
  19. #include <linux/ide.h>
  20. #include <linux/init.h>
  21. #include <asm/io.h>
  22. #define CMD_DEBUG 0
  23. #if CMD_DEBUG
  24. #define cmdprintk(x...) printk(x)
  25. #else
  26. #define cmdprintk(x...)
  27. #endif
  28. /*
  29. * CMD64x specific registers definition.
  30. */
  31. #define CFR 0x50
  32. #define CFR_INTR_CH0 0x04
  33. #define CMDTIM 0x52
  34. #define ARTTIM0 0x53
  35. #define DRWTIM0 0x54
  36. #define ARTTIM1 0x55
  37. #define DRWTIM1 0x56
  38. #define ARTTIM23 0x57
  39. #define ARTTIM23_DIS_RA2 0x04
  40. #define ARTTIM23_DIS_RA3 0x08
  41. #define ARTTIM23_INTR_CH1 0x10
  42. #define DRWTIM2 0x58
  43. #define BRST 0x59
  44. #define DRWTIM3 0x5b
  45. #define BMIDECR0 0x70
  46. #define MRDMODE 0x71
  47. #define MRDMODE_INTR_CH0 0x04
  48. #define MRDMODE_INTR_CH1 0x08
  49. #define UDIDETCR0 0x73
  50. #define DTPR0 0x74
  51. #define BMIDECR1 0x78
  52. #define BMIDECSR 0x79
  53. #define UDIDETCR1 0x7B
  54. #define DTPR1 0x7C
  55. static u8 quantize_timing(int timing, int quant)
  56. {
  57. return (timing + quant - 1) / quant;
  58. }
  59. /*
  60. * This routine calculates active/recovery counts and then writes them into
  61. * the chipset registers.
  62. */
  63. static void program_cycle_times (ide_drive_t *drive, int cycle_time, int active_time)
  64. {
  65. struct pci_dev *dev = HWIF(drive)->pci_dev;
  66. int clock_time = 1000 / system_bus_clock();
  67. u8 cycle_count, active_count, recovery_count, drwtim;
  68. static const u8 recovery_values[] =
  69. {15, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 0};
  70. static const u8 drwtim_regs[4] = {DRWTIM0, DRWTIM1, DRWTIM2, DRWTIM3};
  71. cmdprintk("program_cycle_times parameters: total=%d, active=%d\n",
  72. cycle_time, active_time);
  73. cycle_count = quantize_timing( cycle_time, clock_time);
  74. active_count = quantize_timing(active_time, clock_time);
  75. recovery_count = cycle_count - active_count;
  76. /*
  77. * In case we've got too long recovery phase, try to lengthen
  78. * the active phase
  79. */
  80. if (recovery_count > 16) {
  81. active_count += recovery_count - 16;
  82. recovery_count = 16;
  83. }
  84. if (active_count > 16) /* shouldn't actually happen... */
  85. active_count = 16;
  86. cmdprintk("Final counts: total=%d, active=%d, recovery=%d\n",
  87. cycle_count, active_count, recovery_count);
  88. /*
  89. * Convert values to internal chipset representation
  90. */
  91. recovery_count = recovery_values[recovery_count];
  92. active_count &= 0x0f;
  93. /* Program the active/recovery counts into the DRWTIM register */
  94. drwtim = (active_count << 4) | recovery_count;
  95. (void) pci_write_config_byte(dev, drwtim_regs[drive->dn], drwtim);
  96. cmdprintk("Write 0x%02x to reg 0x%x\n", drwtim, drwtim_regs[drive->dn]);
  97. }
  98. /*
  99. * This routine writes into the chipset registers
  100. * PIO setup/active/recovery timings.
  101. */
  102. static void cmd64x_tune_pio(ide_drive_t *drive, const u8 pio)
  103. {
  104. ide_hwif_t *hwif = HWIF(drive);
  105. struct pci_dev *dev = hwif->pci_dev;
  106. unsigned int cycle_time;
  107. u8 setup_count, arttim = 0;
  108. static const u8 setup_values[] = {0x40, 0x40, 0x40, 0x80, 0, 0xc0};
  109. static const u8 arttim_regs[4] = {ARTTIM0, ARTTIM1, ARTTIM23, ARTTIM23};
  110. cycle_time = ide_pio_cycle_time(drive, pio);
  111. program_cycle_times(drive, cycle_time,
  112. ide_pio_timings[pio].active_time);
  113. setup_count = quantize_timing(ide_pio_timings[pio].setup_time,
  114. 1000 / system_bus_clock());
  115. /*
  116. * The primary channel has individual address setup timing registers
  117. * for each drive and the hardware selects the slowest timing itself.
  118. * The secondary channel has one common register and we have to select
  119. * the slowest address setup timing ourselves.
  120. */
  121. if (hwif->channel) {
  122. ide_drive_t *drives = hwif->drives;
  123. drive->drive_data = setup_count;
  124. setup_count = max(drives[0].drive_data, drives[1].drive_data);
  125. }
  126. if (setup_count > 5) /* shouldn't actually happen... */
  127. setup_count = 5;
  128. cmdprintk("Final address setup count: %d\n", setup_count);
  129. /*
  130. * Program the address setup clocks into the ARTTIM registers.
  131. * Avoid clearing the secondary channel's interrupt bit.
  132. */
  133. (void) pci_read_config_byte (dev, arttim_regs[drive->dn], &arttim);
  134. if (hwif->channel)
  135. arttim &= ~ARTTIM23_INTR_CH1;
  136. arttim &= ~0xc0;
  137. arttim |= setup_values[setup_count];
  138. (void) pci_write_config_byte(dev, arttim_regs[drive->dn], arttim);
  139. cmdprintk("Write 0x%02x to reg 0x%x\n", arttim, arttim_regs[drive->dn]);
  140. }
  141. /*
  142. * Attempts to set drive's PIO mode.
  143. * Special cases are 8: prefetch off, 9: prefetch on (both never worked)
  144. */
  145. static void cmd64x_set_pio_mode(ide_drive_t *drive, const u8 pio)
  146. {
  147. /*
  148. * Filter out the prefetch control values
  149. * to prevent PIO5 from being programmed
  150. */
  151. if (pio == 8 || pio == 9)
  152. return;
  153. cmd64x_tune_pio(drive, pio);
  154. }
  155. static void cmd64x_set_dma_mode(ide_drive_t *drive, const u8 speed)
  156. {
  157. ide_hwif_t *hwif = HWIF(drive);
  158. struct pci_dev *dev = hwif->pci_dev;
  159. u8 unit = drive->dn & 0x01;
  160. u8 regU = 0, pciU = hwif->channel ? UDIDETCR1 : UDIDETCR0;
  161. if (speed >= XFER_SW_DMA_0) {
  162. (void) pci_read_config_byte(dev, pciU, &regU);
  163. regU &= ~(unit ? 0xCA : 0x35);
  164. }
  165. switch(speed) {
  166. case XFER_UDMA_5:
  167. regU |= unit ? 0x0A : 0x05;
  168. break;
  169. case XFER_UDMA_4:
  170. regU |= unit ? 0x4A : 0x15;
  171. break;
  172. case XFER_UDMA_3:
  173. regU |= unit ? 0x8A : 0x25;
  174. break;
  175. case XFER_UDMA_2:
  176. regU |= unit ? 0x42 : 0x11;
  177. break;
  178. case XFER_UDMA_1:
  179. regU |= unit ? 0x82 : 0x21;
  180. break;
  181. case XFER_UDMA_0:
  182. regU |= unit ? 0xC2 : 0x31;
  183. break;
  184. case XFER_MW_DMA_2:
  185. program_cycle_times(drive, 120, 70);
  186. break;
  187. case XFER_MW_DMA_1:
  188. program_cycle_times(drive, 150, 80);
  189. break;
  190. case XFER_MW_DMA_0:
  191. program_cycle_times(drive, 480, 215);
  192. break;
  193. }
  194. if (speed >= XFER_SW_DMA_0)
  195. (void) pci_write_config_byte(dev, pciU, regU);
  196. }
  197. static int cmd648_ide_dma_end (ide_drive_t *drive)
  198. {
  199. ide_hwif_t *hwif = HWIF(drive);
  200. unsigned long base = hwif->dma_base - (hwif->channel * 8);
  201. int err = __ide_dma_end(drive);
  202. u8 irq_mask = hwif->channel ? MRDMODE_INTR_CH1 :
  203. MRDMODE_INTR_CH0;
  204. u8 mrdmode = inb(base + 1);
  205. /* clear the interrupt bit */
  206. outb((mrdmode & ~(MRDMODE_INTR_CH0 | MRDMODE_INTR_CH1)) | irq_mask,
  207. base + 1);
  208. return err;
  209. }
  210. static int cmd64x_ide_dma_end (ide_drive_t *drive)
  211. {
  212. ide_hwif_t *hwif = HWIF(drive);
  213. struct pci_dev *dev = hwif->pci_dev;
  214. int irq_reg = hwif->channel ? ARTTIM23 : CFR;
  215. u8 irq_mask = hwif->channel ? ARTTIM23_INTR_CH1 :
  216. CFR_INTR_CH0;
  217. u8 irq_stat = 0;
  218. int err = __ide_dma_end(drive);
  219. (void) pci_read_config_byte(dev, irq_reg, &irq_stat);
  220. /* clear the interrupt bit */
  221. (void) pci_write_config_byte(dev, irq_reg, irq_stat | irq_mask);
  222. return err;
  223. }
  224. static int cmd648_ide_dma_test_irq (ide_drive_t *drive)
  225. {
  226. ide_hwif_t *hwif = HWIF(drive);
  227. unsigned long base = hwif->dma_base - (hwif->channel * 8);
  228. u8 irq_mask = hwif->channel ? MRDMODE_INTR_CH1 :
  229. MRDMODE_INTR_CH0;
  230. u8 dma_stat = inb(hwif->dma_status);
  231. u8 mrdmode = inb(base + 1);
  232. #ifdef DEBUG
  233. printk("%s: dma_stat: 0x%02x mrdmode: 0x%02x irq_mask: 0x%02x\n",
  234. drive->name, dma_stat, mrdmode, irq_mask);
  235. #endif
  236. if (!(mrdmode & irq_mask))
  237. return 0;
  238. /* return 1 if INTR asserted */
  239. if (dma_stat & 4)
  240. return 1;
  241. return 0;
  242. }
  243. static int cmd64x_ide_dma_test_irq (ide_drive_t *drive)
  244. {
  245. ide_hwif_t *hwif = HWIF(drive);
  246. struct pci_dev *dev = hwif->pci_dev;
  247. int irq_reg = hwif->channel ? ARTTIM23 : CFR;
  248. u8 irq_mask = hwif->channel ? ARTTIM23_INTR_CH1 :
  249. CFR_INTR_CH0;
  250. u8 dma_stat = inb(hwif->dma_status);
  251. u8 irq_stat = 0;
  252. (void) pci_read_config_byte(dev, irq_reg, &irq_stat);
  253. #ifdef DEBUG
  254. printk("%s: dma_stat: 0x%02x irq_stat: 0x%02x irq_mask: 0x%02x\n",
  255. drive->name, dma_stat, irq_stat, irq_mask);
  256. #endif
  257. if (!(irq_stat & irq_mask))
  258. return 0;
  259. /* return 1 if INTR asserted */
  260. if (dma_stat & 4)
  261. return 1;
  262. return 0;
  263. }
  264. /*
  265. * ASUS P55T2P4D with CMD646 chipset revision 0x01 requires the old
  266. * event order for DMA transfers.
  267. */
  268. static int cmd646_1_ide_dma_end (ide_drive_t *drive)
  269. {
  270. ide_hwif_t *hwif = HWIF(drive);
  271. u8 dma_stat = 0, dma_cmd = 0;
  272. drive->waiting_for_dma = 0;
  273. /* get DMA status */
  274. dma_stat = inb(hwif->dma_status);
  275. /* read DMA command state */
  276. dma_cmd = inb(hwif->dma_command);
  277. /* stop DMA */
  278. outb(dma_cmd & ~1, hwif->dma_command);
  279. /* clear the INTR & ERROR bits */
  280. outb(dma_stat | 6, hwif->dma_status);
  281. /* and free any DMA resources */
  282. ide_destroy_dmatable(drive);
  283. /* verify good DMA status */
  284. return (dma_stat & 7) != 4;
  285. }
  286. static unsigned int __devinit init_chipset_cmd64x(struct pci_dev *dev, const char *name)
  287. {
  288. u8 mrdmode = 0;
  289. if (dev->device == PCI_DEVICE_ID_CMD_646) {
  290. switch (dev->revision) {
  291. case 0x07:
  292. case 0x05:
  293. printk("%s: UltraDMA capable\n", name);
  294. break;
  295. case 0x03:
  296. default:
  297. printk("%s: MultiWord DMA force limited\n", name);
  298. break;
  299. case 0x01:
  300. printk("%s: MultiWord DMA limited, "
  301. "IRQ workaround enabled\n", name);
  302. break;
  303. }
  304. }
  305. /* Set a good latency timer and cache line size value. */
  306. (void) pci_write_config_byte(dev, PCI_LATENCY_TIMER, 64);
  307. /* FIXME: pci_set_master() to ensure a good latency timer value */
  308. /*
  309. * Enable interrupts, select MEMORY READ LINE for reads.
  310. *
  311. * NOTE: although not mentioned in the PCI0646U specs,
  312. * bits 0-1 are write only and won't be read back as
  313. * set or not -- PCI0646U2 specs clarify this point.
  314. */
  315. (void) pci_read_config_byte (dev, MRDMODE, &mrdmode);
  316. mrdmode &= ~0x30;
  317. (void) pci_write_config_byte(dev, MRDMODE, (mrdmode | 0x02));
  318. return 0;
  319. }
  320. static u8 __devinit ata66_cmd64x(ide_hwif_t *hwif)
  321. {
  322. struct pci_dev *dev = hwif->pci_dev;
  323. u8 bmidecsr = 0, mask = hwif->channel ? 0x02 : 0x01;
  324. switch (dev->device) {
  325. case PCI_DEVICE_ID_CMD_648:
  326. case PCI_DEVICE_ID_CMD_649:
  327. pci_read_config_byte(dev, BMIDECSR, &bmidecsr);
  328. return (bmidecsr & mask) ? ATA_CBL_PATA80 : ATA_CBL_PATA40;
  329. default:
  330. return ATA_CBL_PATA40;
  331. }
  332. }
  333. static void __devinit init_hwif_cmd64x(ide_hwif_t *hwif)
  334. {
  335. struct pci_dev *dev = hwif->pci_dev;
  336. hwif->set_pio_mode = &cmd64x_set_pio_mode;
  337. hwif->set_dma_mode = &cmd64x_set_dma_mode;
  338. if (!hwif->dma_base)
  339. return;
  340. /*
  341. * UltraDMA only supported on PCI646U and PCI646U2, which
  342. * correspond to revisions 0x03, 0x05 and 0x07 respectively.
  343. * Actually, although the CMD tech support people won't
  344. * tell me the details, the 0x03 revision cannot support
  345. * UDMA correctly without hardware modifications, and even
  346. * then it only works with Quantum disks due to some
  347. * hold time assumptions in the 646U part which are fixed
  348. * in the 646U2.
  349. *
  350. * So we only do UltraDMA on revision 0x05 and 0x07 chipsets.
  351. */
  352. if (dev->device == PCI_DEVICE_ID_CMD_646 && dev->revision < 5)
  353. hwif->ultra_mask = 0x00;
  354. if (hwif->cbl != ATA_CBL_PATA40_SHORT)
  355. hwif->cbl = ata66_cmd64x(hwif);
  356. switch (dev->device) {
  357. case PCI_DEVICE_ID_CMD_648:
  358. case PCI_DEVICE_ID_CMD_649:
  359. alt_irq_bits:
  360. hwif->ide_dma_end = &cmd648_ide_dma_end;
  361. hwif->ide_dma_test_irq = &cmd648_ide_dma_test_irq;
  362. break;
  363. case PCI_DEVICE_ID_CMD_646:
  364. if (dev->revision == 0x01) {
  365. hwif->ide_dma_end = &cmd646_1_ide_dma_end;
  366. break;
  367. } else if (dev->revision >= 0x03)
  368. goto alt_irq_bits;
  369. /* fall thru */
  370. default:
  371. hwif->ide_dma_end = &cmd64x_ide_dma_end;
  372. hwif->ide_dma_test_irq = &cmd64x_ide_dma_test_irq;
  373. break;
  374. }
  375. }
  376. static const struct ide_port_info cmd64x_chipsets[] __devinitdata = {
  377. { /* 0 */
  378. .name = "CMD643",
  379. .init_chipset = init_chipset_cmd64x,
  380. .init_hwif = init_hwif_cmd64x,
  381. .enablebits = {{0x00,0x00,0x00}, {0x51,0x08,0x08}},
  382. .host_flags = IDE_HFLAG_ABUSE_PREFETCH | IDE_HFLAG_BOOTABLE,
  383. .pio_mask = ATA_PIO5,
  384. .mwdma_mask = ATA_MWDMA2,
  385. .udma_mask = 0x00, /* no udma */
  386. },{ /* 1 */
  387. .name = "CMD646",
  388. .init_chipset = init_chipset_cmd64x,
  389. .init_hwif = init_hwif_cmd64x,
  390. .enablebits = {{0x51,0x04,0x04}, {0x51,0x08,0x08}},
  391. .chipset = ide_cmd646,
  392. .host_flags = IDE_HFLAG_ABUSE_PREFETCH | IDE_HFLAG_BOOTABLE,
  393. .pio_mask = ATA_PIO5,
  394. .mwdma_mask = ATA_MWDMA2,
  395. .udma_mask = ATA_UDMA2,
  396. },{ /* 2 */
  397. .name = "CMD648",
  398. .init_chipset = init_chipset_cmd64x,
  399. .init_hwif = init_hwif_cmd64x,
  400. .enablebits = {{0x51,0x04,0x04}, {0x51,0x08,0x08}},
  401. .host_flags = IDE_HFLAG_ABUSE_PREFETCH | IDE_HFLAG_BOOTABLE,
  402. .pio_mask = ATA_PIO5,
  403. .mwdma_mask = ATA_MWDMA2,
  404. .udma_mask = ATA_UDMA4,
  405. },{ /* 3 */
  406. .name = "CMD649",
  407. .init_chipset = init_chipset_cmd64x,
  408. .init_hwif = init_hwif_cmd64x,
  409. .enablebits = {{0x51,0x04,0x04}, {0x51,0x08,0x08}},
  410. .host_flags = IDE_HFLAG_ABUSE_PREFETCH | IDE_HFLAG_BOOTABLE,
  411. .pio_mask = ATA_PIO5,
  412. .mwdma_mask = ATA_MWDMA2,
  413. .udma_mask = ATA_UDMA5,
  414. }
  415. };
  416. static int __devinit cmd64x_init_one(struct pci_dev *dev, const struct pci_device_id *id)
  417. {
  418. struct ide_port_info d;
  419. u8 idx = id->driver_data;
  420. d = cmd64x_chipsets[idx];
  421. /*
  422. * The original PCI0646 didn't have the primary channel enable bit,
  423. * it appeared starting with PCI0646U (i.e. revision ID 3).
  424. */
  425. if (idx == 1 && dev->revision < 3)
  426. d.enablebits[0].reg = 0;
  427. return ide_setup_pci_device(dev, &d);
  428. }
  429. static const struct pci_device_id cmd64x_pci_tbl[] = {
  430. { PCI_VDEVICE(CMD, PCI_DEVICE_ID_CMD_643), 0 },
  431. { PCI_VDEVICE(CMD, PCI_DEVICE_ID_CMD_646), 1 },
  432. { PCI_VDEVICE(CMD, PCI_DEVICE_ID_CMD_648), 2 },
  433. { PCI_VDEVICE(CMD, PCI_DEVICE_ID_CMD_649), 3 },
  434. { 0, },
  435. };
  436. MODULE_DEVICE_TABLE(pci, cmd64x_pci_tbl);
  437. static struct pci_driver driver = {
  438. .name = "CMD64x_IDE",
  439. .id_table = cmd64x_pci_tbl,
  440. .probe = cmd64x_init_one,
  441. };
  442. static int __init cmd64x_ide_init(void)
  443. {
  444. return ide_pci_register_driver(&driver);
  445. }
  446. module_init(cmd64x_ide_init);
  447. MODULE_AUTHOR("Eddie Dost, David Miller, Andre Hedrick");
  448. MODULE_DESCRIPTION("PCI driver module for CMD64x IDE");
  449. MODULE_LICENSE("GPL");