cmd64x.c 14 KB

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