pata_optidma.c 12 KB

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