pata_optidma.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /*
  2. * pata_optidma.c - Opti DMA PATA for new ATA layer
  3. * (C) 2006 Red Hat Inc
  4. * Alan Cox <alan@redhat.com>
  5. *
  6. * The Opti DMA controllers are related to the older PIO PCI controllers
  7. * and indeed the VLB ones. The main differences are that the timing
  8. * numbers are now based off PCI clocks not VLB and differ, and that
  9. * MWDMA is supported.
  10. *
  11. * This driver should support Viper-N+, FireStar, FireStar Plus.
  12. *
  13. * These devices support virtual DMA for read (aka the CS5520). Later
  14. * chips support UDMA33, but only if the rest of the board logic does,
  15. * so you have to get this right. We don't support the virtual DMA
  16. * but we do handle UDMA.
  17. *
  18. * Bits that are worth knowing
  19. * Most control registers are shadowed into I/O registers
  20. * 0x1F5 bit 0 tells you if the PCI/VLB clock is 33 or 25Mhz
  21. * Virtual DMA registers *move* between rev 0x02 and rev 0x10
  22. * UDMA requires a 66MHz FSB
  23. *
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/pci.h>
  28. #include <linux/init.h>
  29. #include <linux/blkdev.h>
  30. #include <linux/delay.h>
  31. #include <scsi/scsi_host.h>
  32. #include <linux/libata.h>
  33. #define DRV_NAME "pata_optidma"
  34. #define DRV_VERSION "0.3.2"
  35. enum {
  36. READ_REG = 0, /* index of Read cycle timing register */
  37. WRITE_REG = 1, /* index of Write cycle timing register */
  38. CNTRL_REG = 3, /* index of Control register */
  39. STRAP_REG = 5, /* index of Strap register */
  40. MISC_REG = 6 /* index of Miscellaneous register */
  41. };
  42. static int pci_clock; /* 0 = 33 1 = 25 */
  43. /**
  44. * optidma_pre_reset - probe begin
  45. * @link: ATA link
  46. * @deadline: deadline jiffies for the operation
  47. *
  48. * Set up cable type and use generic probe init
  49. */
  50. static int optidma_pre_reset(struct ata_link *link, unsigned long deadline)
  51. {
  52. struct ata_port *ap = link->ap;
  53. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  54. static const struct pci_bits optidma_enable_bits = {
  55. 0x40, 1, 0x08, 0x00
  56. };
  57. if (ap->port_no && !pci_test_config_bits(pdev, &optidma_enable_bits))
  58. return -ENOENT;
  59. return ata_sff_prereset(link, deadline);
  60. }
  61. /**
  62. * optidma_unlock - unlock control registers
  63. * @ap: ATA port
  64. *
  65. * Unlock the control register block for this adapter. Registers must not
  66. * be unlocked in a situation where libata might look at them.
  67. */
  68. static void optidma_unlock(struct ata_port *ap)
  69. {
  70. void __iomem *regio = ap->ioaddr.cmd_addr;
  71. /* These 3 unlock the control register access */
  72. ioread16(regio + 1);
  73. ioread16(regio + 1);
  74. iowrite8(3, regio + 2);
  75. }
  76. /**
  77. * optidma_lock - issue temporary relock
  78. * @ap: ATA port
  79. *
  80. * Re-lock the configuration register settings.
  81. */
  82. static void optidma_lock(struct ata_port *ap)
  83. {
  84. void __iomem *regio = ap->ioaddr.cmd_addr;
  85. /* Relock */
  86. iowrite8(0x83, regio + 2);
  87. }
  88. /**
  89. * optidma_mode_setup - set mode data
  90. * @ap: ATA interface
  91. * @adev: ATA device
  92. * @mode: Mode to set
  93. *
  94. * Called to do the DMA or PIO mode setup. Timing numbers are all
  95. * pre computed to keep the code clean. There are two tables depending
  96. * on the hardware clock speed.
  97. *
  98. * WARNING: While we do this the IDE registers vanish. If we take an
  99. * IRQ here we depend on the host set locking to avoid catastrophe.
  100. */
  101. static void optidma_mode_setup(struct ata_port *ap, struct ata_device *adev, u8 mode)
  102. {
  103. struct ata_device *pair = ata_dev_pair(adev);
  104. int pio = adev->pio_mode - XFER_PIO_0;
  105. int dma = adev->dma_mode - XFER_MW_DMA_0;
  106. void __iomem *regio = ap->ioaddr.cmd_addr;
  107. u8 addr;
  108. /* Address table precomputed with a DCLK of 2 */
  109. static const u8 addr_timing[2][5] = {
  110. { 0x30, 0x20, 0x20, 0x10, 0x10 },
  111. { 0x20, 0x20, 0x10, 0x10, 0x10 }
  112. };
  113. static const u8 data_rec_timing[2][5] = {
  114. { 0x59, 0x46, 0x30, 0x20, 0x20 },
  115. { 0x46, 0x32, 0x20, 0x20, 0x10 }
  116. };
  117. static const u8 dma_data_rec_timing[2][3] = {
  118. { 0x76, 0x20, 0x20 },
  119. { 0x54, 0x20, 0x10 }
  120. };
  121. /* Switch from IDE to control mode */
  122. optidma_unlock(ap);
  123. /*
  124. * As with many controllers the address setup time is shared
  125. * and must suit both devices if present. FIXME: Check if we
  126. * need to look at slowest of PIO/DMA mode of either device
  127. */
  128. if (mode >= XFER_MW_DMA_0)
  129. addr = 0;
  130. else
  131. addr = addr_timing[pci_clock][pio];
  132. if (pair) {
  133. u8 pair_addr;
  134. /* Hardware constraint */
  135. if (pair->dma_mode)
  136. pair_addr = 0;
  137. else
  138. pair_addr = addr_timing[pci_clock][pair->pio_mode - XFER_PIO_0];
  139. if (pair_addr > addr)
  140. addr = pair_addr;
  141. }
  142. /* Commence primary programming sequence */
  143. /* First we load the device number into the timing select */
  144. iowrite8(adev->devno, regio + MISC_REG);
  145. /* Now we load the data timings into read data/write data */
  146. if (mode < XFER_MW_DMA_0) {
  147. iowrite8(data_rec_timing[pci_clock][pio], regio + READ_REG);
  148. iowrite8(data_rec_timing[pci_clock][pio], regio + WRITE_REG);
  149. } else if (mode < XFER_UDMA_0) {
  150. iowrite8(dma_data_rec_timing[pci_clock][dma], regio + READ_REG);
  151. iowrite8(dma_data_rec_timing[pci_clock][dma], regio + WRITE_REG);
  152. }
  153. /* Finally we load the address setup into the misc register */
  154. iowrite8(addr | adev->devno, regio + MISC_REG);
  155. /* Programming sequence complete, timing 0 dev 0, timing 1 dev 1 */
  156. iowrite8(0x85, regio + CNTRL_REG);
  157. /* Switch back to IDE mode */
  158. optidma_lock(ap);
  159. /* Note: at this point our programming is incomplete. We are
  160. not supposed to program PCI 0x43 "things we hacked onto the chip"
  161. until we've done both sets of PIO/DMA timings */
  162. }
  163. /**
  164. * optiplus_mode_setup - DMA setup for Firestar Plus
  165. * @ap: ATA port
  166. * @adev: device
  167. * @mode: desired mode
  168. *
  169. * The Firestar plus has additional UDMA functionality for UDMA0-2 and
  170. * requires we do some additional work. Because the base work we must do
  171. * is mostly shared we wrap the Firestar setup functionality in this
  172. * one
  173. */
  174. static void optiplus_mode_setup(struct ata_port *ap, struct ata_device *adev, u8 mode)
  175. {
  176. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  177. u8 udcfg;
  178. u8 udslave;
  179. int dev2 = 2 * adev->devno;
  180. int unit = 2 * ap->port_no + adev->devno;
  181. int udma = mode - XFER_UDMA_0;
  182. pci_read_config_byte(pdev, 0x44, &udcfg);
  183. if (mode <= XFER_UDMA_0) {
  184. udcfg &= ~(1 << unit);
  185. optidma_mode_setup(ap, adev, adev->dma_mode);
  186. } else {
  187. udcfg |= (1 << unit);
  188. if (ap->port_no) {
  189. pci_read_config_byte(pdev, 0x45, &udslave);
  190. udslave &= ~(0x03 << dev2);
  191. udslave |= (udma << dev2);
  192. pci_write_config_byte(pdev, 0x45, udslave);
  193. } else {
  194. udcfg &= ~(0x30 << dev2);
  195. udcfg |= (udma << dev2);
  196. }
  197. }
  198. pci_write_config_byte(pdev, 0x44, udcfg);
  199. }
  200. /**
  201. * optidma_set_pio_mode - PIO setup callback
  202. * @ap: ATA port
  203. * @adev: Device
  204. *
  205. * The libata core provides separate functions for handling PIO and
  206. * DMA programming. The architecture of the Firestar makes it easier
  207. * for us to have a common function so we provide wrappers
  208. */
  209. static void optidma_set_pio_mode(struct ata_port *ap, struct ata_device *adev)
  210. {
  211. optidma_mode_setup(ap, adev, adev->pio_mode);
  212. }
  213. /**
  214. * optidma_set_dma_mode - DMA setup callback
  215. * @ap: ATA port
  216. * @adev: Device
  217. *
  218. * The libata core provides separate functions for handling PIO and
  219. * DMA programming. The architecture of the Firestar makes it easier
  220. * for us to have a common function so we provide wrappers
  221. */
  222. static void optidma_set_dma_mode(struct ata_port *ap, struct ata_device *adev)
  223. {
  224. optidma_mode_setup(ap, adev, adev->dma_mode);
  225. }
  226. /**
  227. * optiplus_set_pio_mode - PIO setup callback
  228. * @ap: ATA port
  229. * @adev: Device
  230. *
  231. * The libata core provides separate functions for handling PIO and
  232. * DMA programming. The architecture of the Firestar makes it easier
  233. * for us to have a common function so we provide wrappers
  234. */
  235. static void optiplus_set_pio_mode(struct ata_port *ap, struct ata_device *adev)
  236. {
  237. optiplus_mode_setup(ap, adev, adev->pio_mode);
  238. }
  239. /**
  240. * optiplus_set_dma_mode - DMA setup callback
  241. * @ap: ATA port
  242. * @adev: Device
  243. *
  244. * The libata core provides separate functions for handling PIO and
  245. * DMA programming. The architecture of the Firestar makes it easier
  246. * for us to have a common function so we provide wrappers
  247. */
  248. static void optiplus_set_dma_mode(struct ata_port *ap, struct ata_device *adev)
  249. {
  250. optiplus_mode_setup(ap, adev, adev->dma_mode);
  251. }
  252. /**
  253. * optidma_make_bits - PCI setup helper
  254. * @adev: ATA device
  255. *
  256. * Turn the ATA device setup into PCI configuration bits
  257. * for register 0x43 and return the two bits needed.
  258. */
  259. static u8 optidma_make_bits43(struct ata_device *adev)
  260. {
  261. static const u8 bits43[5] = {
  262. 0, 0, 0, 1, 2
  263. };
  264. if (!ata_dev_enabled(adev))
  265. return 0;
  266. if (adev->dma_mode)
  267. return adev->dma_mode - XFER_MW_DMA_0;
  268. return bits43[adev->pio_mode - XFER_PIO_0];
  269. }
  270. /**
  271. * optidma_set_mode - mode setup
  272. * @link: link to set up
  273. *
  274. * Use the standard setup to tune the chipset and then finalise the
  275. * configuration by writing the nibble of extra bits of data into
  276. * the chip.
  277. */
  278. static int optidma_set_mode(struct ata_link *link, struct ata_device **r_failed)
  279. {
  280. struct ata_port *ap = link->ap;
  281. u8 r;
  282. int nybble = 4 * ap->port_no;
  283. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  284. int rc = ata_do_set_mode(link, r_failed);
  285. if (rc == 0) {
  286. pci_read_config_byte(pdev, 0x43, &r);
  287. r &= (0x0F << nybble);
  288. r |= (optidma_make_bits43(&link->device[0]) +
  289. (optidma_make_bits43(&link->device[0]) << 2)) << nybble;
  290. pci_write_config_byte(pdev, 0x43, r);
  291. }
  292. return rc;
  293. }
  294. static struct scsi_host_template optidma_sht = {
  295. ATA_BMDMA_SHT(DRV_NAME),
  296. };
  297. static struct ata_port_operations optidma_port_ops = {
  298. .inherits = &ata_bmdma_port_ops,
  299. .cable_detect = ata_cable_40wire,
  300. .set_piomode = optidma_set_pio_mode,
  301. .set_dmamode = optidma_set_dma_mode,
  302. .set_mode = optidma_set_mode,
  303. .prereset = optidma_pre_reset,
  304. };
  305. static struct ata_port_operations optiplus_port_ops = {
  306. .inherits = &optidma_port_ops,
  307. .set_piomode = optiplus_set_pio_mode,
  308. .set_dmamode = optiplus_set_dma_mode,
  309. };
  310. /**
  311. * optiplus_with_udma - Look for UDMA capable setup
  312. * @pdev; ATA controller
  313. */
  314. static int optiplus_with_udma(struct pci_dev *pdev)
  315. {
  316. u8 r;
  317. int ret = 0;
  318. int ioport = 0x22;
  319. struct pci_dev *dev1;
  320. /* Find function 1 */
  321. dev1 = pci_get_device(0x1045, 0xC701, NULL);
  322. if (dev1 == NULL)
  323. return 0;
  324. /* Rev must be >= 0x10 */
  325. pci_read_config_byte(dev1, 0x08, &r);
  326. if (r < 0x10)
  327. goto done_nomsg;
  328. /* Read the chipset system configuration to check our mode */
  329. pci_read_config_byte(dev1, 0x5F, &r);
  330. ioport |= (r << 8);
  331. outb(0x10, ioport);
  332. /* Must be 66Mhz sync */
  333. if ((inb(ioport + 2) & 1) == 0)
  334. goto done;
  335. /* Check the ATA arbitration/timing is suitable */
  336. pci_read_config_byte(pdev, 0x42, &r);
  337. if ((r & 0x36) != 0x36)
  338. goto done;
  339. pci_read_config_byte(dev1, 0x52, &r);
  340. if (r & 0x80) /* IDEDIR disabled */
  341. ret = 1;
  342. done:
  343. printk(KERN_WARNING "UDMA not supported in this configuration.\n");
  344. done_nomsg: /* Wrong chip revision */
  345. pci_dev_put(dev1);
  346. return ret;
  347. }
  348. static int optidma_init_one(struct pci_dev *dev, const struct pci_device_id *id)
  349. {
  350. static const struct ata_port_info info_82c700 = {
  351. .flags = ATA_FLAG_SLAVE_POSS,
  352. .pio_mask = 0x1f,
  353. .mwdma_mask = 0x07,
  354. .port_ops = &optidma_port_ops
  355. };
  356. static const struct ata_port_info info_82c700_udma = {
  357. .flags = ATA_FLAG_SLAVE_POSS,
  358. .pio_mask = 0x1f,
  359. .mwdma_mask = 0x07,
  360. .udma_mask = 0x07,
  361. .port_ops = &optiplus_port_ops
  362. };
  363. const struct ata_port_info *ppi[] = { &info_82c700, NULL };
  364. static int printed_version;
  365. int rc;
  366. if (!printed_version++)
  367. dev_printk(KERN_DEBUG, &dev->dev, "version " DRV_VERSION "\n");
  368. rc = pcim_enable_device(dev);
  369. if (rc)
  370. return rc;
  371. /* Fixed location chipset magic */
  372. inw(0x1F1);
  373. inw(0x1F1);
  374. pci_clock = inb(0x1F5) & 1; /* 0 = 33Mhz, 1 = 25Mhz */
  375. if (optiplus_with_udma(dev))
  376. ppi[0] = &info_82c700_udma;
  377. return ata_pci_sff_init_one(dev, ppi, &optidma_sht, NULL);
  378. }
  379. static const struct pci_device_id optidma[] = {
  380. { PCI_VDEVICE(OPTI, 0xD568), }, /* Opti 82C700 */
  381. { },
  382. };
  383. static struct pci_driver optidma_pci_driver = {
  384. .name = DRV_NAME,
  385. .id_table = optidma,
  386. .probe = optidma_init_one,
  387. .remove = ata_pci_remove_one,
  388. #ifdef CONFIG_PM
  389. .suspend = ata_pci_device_suspend,
  390. .resume = ata_pci_device_resume,
  391. #endif
  392. };
  393. static int __init optidma_init(void)
  394. {
  395. return pci_register_driver(&optidma_pci_driver);
  396. }
  397. static void __exit optidma_exit(void)
  398. {
  399. pci_unregister_driver(&optidma_pci_driver);
  400. }
  401. MODULE_AUTHOR("Alan Cox");
  402. MODULE_DESCRIPTION("low-level driver for Opti Firestar/Firestar Plus");
  403. MODULE_LICENSE("GPL");
  404. MODULE_DEVICE_TABLE(pci, optidma);
  405. MODULE_VERSION(DRV_VERSION);
  406. module_init(optidma_init);
  407. module_exit(optidma_exit);