pata_optidma.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * pata_optidma.c - Opti DMA PATA for new ATA layer
  4. * (C) 2006 Red Hat Inc
  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/blkdev.h>
  29. #include <linux/delay.h>
  30. #include <scsi/scsi_host.h>
  31. #include <linux/libata.h>
  32. #define DRV_NAME "pata_optidma"
  33. #define DRV_VERSION "0.3.2"
  34. enum {
  35. READ_REG = 0, /* index of Read cycle timing register */
  36. WRITE_REG = 1, /* index of Write cycle timing register */
  37. CNTRL_REG = 3, /* index of Control register */
  38. STRAP_REG = 5, /* index of Strap register */
  39. MISC_REG = 6 /* index of Miscellaneous register */
  40. };
  41. static int pci_clock; /* 0 = 33 1 = 25 */
  42. /**
  43. * optidma_pre_reset - probe begin
  44. * @link: ATA link
  45. * @deadline: deadline jiffies for the operation
  46. *
  47. * Set up cable type and use generic probe init
  48. */
  49. static int optidma_pre_reset(struct ata_link *link, unsigned long deadline)
  50. {
  51. struct ata_port *ap = link->ap;
  52. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  53. static const struct pci_bits optidma_enable_bits = {
  54. 0x40, 1, 0x08, 0x00
  55. };
  56. if (ap->port_no && !pci_test_config_bits(pdev, &optidma_enable_bits))
  57. return -ENOENT;
  58. return ata_sff_prereset(link, deadline);
  59. }
  60. /**
  61. * optidma_unlock - unlock control registers
  62. * @ap: ATA port
  63. *
  64. * Unlock the control register block for this adapter. Registers must not
  65. * be unlocked in a situation where libata might look at them.
  66. */
  67. static void optidma_unlock(struct ata_port *ap)
  68. {
  69. void __iomem *regio = ap->ioaddr.cmd_addr;
  70. /* These 3 unlock the control register access */
  71. ioread16(regio + 1);
  72. ioread16(regio + 1);
  73. iowrite8(3, regio + 2);
  74. }
  75. /**
  76. * optidma_lock - issue temporary relock
  77. * @ap: ATA port
  78. *
  79. * Re-lock the configuration register settings.
  80. */
  81. static void optidma_lock(struct ata_port *ap)
  82. {
  83. void __iomem *regio = ap->ioaddr.cmd_addr;
  84. /* Relock */
  85. iowrite8(0x83, regio + 2);
  86. }
  87. /**
  88. * optidma_mode_setup - set mode data
  89. * @ap: ATA interface
  90. * @adev: ATA device
  91. * @mode: Mode to set
  92. *
  93. * Called to do the DMA or PIO mode setup. Timing numbers are all
  94. * pre computed to keep the code clean. There are two tables depending
  95. * on the hardware clock speed.
  96. *
  97. * WARNING: While we do this the IDE registers vanish. If we take an
  98. * IRQ here we depend on the host set locking to avoid catastrophe.
  99. */
  100. static void optidma_mode_setup(struct ata_port *ap, struct ata_device *adev, u8 mode)
  101. {
  102. struct ata_device *pair = ata_dev_pair(adev);
  103. int pio = adev->pio_mode - XFER_PIO_0;
  104. int dma = adev->dma_mode - XFER_MW_DMA_0;
  105. void __iomem *regio = ap->ioaddr.cmd_addr;
  106. u8 addr;
  107. /* Address table precomputed with a DCLK of 2 */
  108. static const u8 addr_timing[2][5] = {
  109. { 0x30, 0x20, 0x20, 0x10, 0x10 },
  110. { 0x20, 0x20, 0x10, 0x10, 0x10 }
  111. };
  112. static const u8 data_rec_timing[2][5] = {
  113. { 0x59, 0x46, 0x30, 0x20, 0x20 },
  114. { 0x46, 0x32, 0x20, 0x20, 0x10 }
  115. };
  116. static const u8 dma_data_rec_timing[2][3] = {
  117. { 0x76, 0x20, 0x20 },
  118. { 0x54, 0x20, 0x10 }
  119. };
  120. /* Switch from IDE to control mode */
  121. optidma_unlock(ap);
  122. /*
  123. * As with many controllers the address setup time is shared
  124. * and must suit both devices if present. FIXME: Check if we
  125. * need to look at slowest of PIO/DMA mode of either device
  126. */
  127. if (mode >= XFER_MW_DMA_0)
  128. addr = 0;
  129. else
  130. addr = addr_timing[pci_clock][pio];
  131. if (pair) {
  132. u8 pair_addr;
  133. /* Hardware constraint */
  134. if (ata_dma_enabled(pair))
  135. pair_addr = 0;
  136. else
  137. pair_addr = addr_timing[pci_clock][pair->pio_mode - XFER_PIO_0];
  138. if (pair_addr > addr)
  139. addr = pair_addr;
  140. }
  141. /* Commence primary programming sequence */
  142. /* First we load the device number into the timing select */
  143. iowrite8(adev->devno, regio + MISC_REG);
  144. /* Now we load the data timings into read data/write data */
  145. if (mode < XFER_MW_DMA_0) {
  146. iowrite8(data_rec_timing[pci_clock][pio], regio + READ_REG);
  147. iowrite8(data_rec_timing[pci_clock][pio], regio + WRITE_REG);
  148. } else if (mode < XFER_UDMA_0) {
  149. iowrite8(dma_data_rec_timing[pci_clock][dma], regio + READ_REG);
  150. iowrite8(dma_data_rec_timing[pci_clock][dma], regio + WRITE_REG);
  151. }
  152. /* Finally we load the address setup into the misc register */
  153. iowrite8(addr | adev->devno, regio + MISC_REG);
  154. /* Programming sequence complete, timing 0 dev 0, timing 1 dev 1 */
  155. iowrite8(0x85, regio + CNTRL_REG);
  156. /* Switch back to IDE mode */
  157. optidma_lock(ap);
  158. /* Note: at this point our programming is incomplete. We are
  159. not supposed to program PCI 0x43 "things we hacked onto the chip"
  160. until we've done both sets of PIO/DMA timings */
  161. }
  162. /**
  163. * optiplus_mode_setup - DMA setup for Firestar Plus
  164. * @ap: ATA port
  165. * @adev: device
  166. * @mode: desired mode
  167. *
  168. * The Firestar plus has additional UDMA functionality for UDMA0-2 and
  169. * requires we do some additional work. Because the base work we must do
  170. * is mostly shared we wrap the Firestar setup functionality in this
  171. * one
  172. */
  173. static void optiplus_mode_setup(struct ata_port *ap, struct ata_device *adev, u8 mode)
  174. {
  175. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  176. u8 udcfg;
  177. u8 udslave;
  178. int dev2 = 2 * adev->devno;
  179. int unit = 2 * ap->port_no + adev->devno;
  180. int udma = mode - XFER_UDMA_0;
  181. pci_read_config_byte(pdev, 0x44, &udcfg);
  182. if (mode <= XFER_UDMA_0) {
  183. udcfg &= ~(1 << unit);
  184. optidma_mode_setup(ap, adev, adev->dma_mode);
  185. } else {
  186. udcfg |= (1 << unit);
  187. if (ap->port_no) {
  188. pci_read_config_byte(pdev, 0x45, &udslave);
  189. udslave &= ~(0x03 << dev2);
  190. udslave |= (udma << dev2);
  191. pci_write_config_byte(pdev, 0x45, udslave);
  192. } else {
  193. udcfg &= ~(0x30 << dev2);
  194. udcfg |= (udma << dev2);
  195. }
  196. }
  197. pci_write_config_byte(pdev, 0x44, udcfg);
  198. }
  199. /**
  200. * optidma_set_pio_mode - PIO setup callback
  201. * @ap: ATA port
  202. * @adev: Device
  203. *
  204. * The libata core provides separate functions for handling PIO and
  205. * DMA programming. The architecture of the Firestar makes it easier
  206. * for us to have a common function so we provide wrappers
  207. */
  208. static void optidma_set_pio_mode(struct ata_port *ap, struct ata_device *adev)
  209. {
  210. optidma_mode_setup(ap, adev, adev->pio_mode);
  211. }
  212. /**
  213. * optidma_set_dma_mode - DMA setup callback
  214. * @ap: ATA port
  215. * @adev: Device
  216. *
  217. * The libata core provides separate functions for handling PIO and
  218. * DMA programming. The architecture of the Firestar makes it easier
  219. * for us to have a common function so we provide wrappers
  220. */
  221. static void optidma_set_dma_mode(struct ata_port *ap, struct ata_device *adev)
  222. {
  223. optidma_mode_setup(ap, adev, adev->dma_mode);
  224. }
  225. /**
  226. * optiplus_set_pio_mode - PIO setup callback
  227. * @ap: ATA port
  228. * @adev: Device
  229. *
  230. * The libata core provides separate functions for handling PIO and
  231. * DMA programming. The architecture of the Firestar makes it easier
  232. * for us to have a common function so we provide wrappers
  233. */
  234. static void optiplus_set_pio_mode(struct ata_port *ap, struct ata_device *adev)
  235. {
  236. optiplus_mode_setup(ap, adev, adev->pio_mode);
  237. }
  238. /**
  239. * optiplus_set_dma_mode - DMA setup callback
  240. * @ap: ATA port
  241. * @adev: Device
  242. *
  243. * The libata core provides separate functions for handling PIO and
  244. * DMA programming. The architecture of the Firestar makes it easier
  245. * for us to have a common function so we provide wrappers
  246. */
  247. static void optiplus_set_dma_mode(struct ata_port *ap, struct ata_device *adev)
  248. {
  249. optiplus_mode_setup(ap, adev, adev->dma_mode);
  250. }
  251. /**
  252. * optidma_make_bits43 - PCI setup helper
  253. * @adev: ATA device
  254. *
  255. * Turn the ATA device setup into PCI configuration bits
  256. * for register 0x43 and return the two bits needed.
  257. */
  258. static u8 optidma_make_bits43(struct ata_device *adev)
  259. {
  260. static const u8 bits43[5] = {
  261. 0, 0, 0, 1, 2
  262. };
  263. if (!ata_dev_enabled(adev))
  264. return 0;
  265. if (ata_dma_enabled(adev))
  266. return adev->dma_mode - XFER_MW_DMA_0;
  267. return bits43[adev->pio_mode - XFER_PIO_0];
  268. }
  269. /**
  270. * optidma_set_mode - mode setup
  271. * @link: link to set up
  272. * @r_failed: out parameter for failed device
  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 = ATA_PIO4,
  353. .mwdma_mask = ATA_MWDMA2,
  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 = ATA_PIO4,
  359. .mwdma_mask = ATA_MWDMA2,
  360. .udma_mask = ATA_UDMA2,
  361. .port_ops = &optiplus_port_ops
  362. };
  363. const struct ata_port_info *ppi[] = { &info_82c700, NULL };
  364. int rc;
  365. ata_print_version_once(&dev->dev, DRV_VERSION);
  366. rc = pcim_enable_device(dev);
  367. if (rc)
  368. return rc;
  369. /* Fixed location chipset magic */
  370. inw(0x1F1);
  371. inw(0x1F1);
  372. pci_clock = inb(0x1F5) & 1; /* 0 = 33Mhz, 1 = 25Mhz */
  373. if (optiplus_with_udma(dev))
  374. ppi[0] = &info_82c700_udma;
  375. return ata_pci_bmdma_init_one(dev, ppi, &optidma_sht, NULL, 0);
  376. }
  377. static const struct pci_device_id optidma[] = {
  378. { PCI_VDEVICE(OPTI, 0xD568), }, /* Opti 82C700 */
  379. { },
  380. };
  381. static struct pci_driver optidma_pci_driver = {
  382. .name = DRV_NAME,
  383. .id_table = optidma,
  384. .probe = optidma_init_one,
  385. .remove = ata_pci_remove_one,
  386. #ifdef CONFIG_PM_SLEEP
  387. .suspend = ata_pci_device_suspend,
  388. .resume = ata_pci_device_resume,
  389. #endif
  390. };
  391. module_pci_driver(optidma_pci_driver);
  392. MODULE_AUTHOR("Alan Cox");
  393. MODULE_DESCRIPTION("low-level driver for Opti Firestar/Firestar Plus");
  394. MODULE_LICENSE("GPL");
  395. MODULE_DEVICE_TABLE(pci, optidma);
  396. MODULE_VERSION(DRV_VERSION);