cmd64x.c 12 KB

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