cmd64x.c 13 KB

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