pata_ns87415.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /*
  2. * pata_ns87415.c - NS87415 (non PARISC) PATA
  3. *
  4. * (C) 2005 Red Hat <alan@lxorguk.ukuu.org.uk>
  5. *
  6. * This is a fairly generic MWDMA controller. It has some limitations
  7. * as it requires timing reloads on PIO/DMA transitions but it is otherwise
  8. * fairly well designed.
  9. *
  10. * This driver assumes the firmware has left the chip in a valid ST506
  11. * compliant state, either legacy IRQ 14/15 or native INTA shared. You
  12. * may need to add platform code if your system fails to do this.
  13. *
  14. * The same cell appears in the 87560 controller used by some PARISC
  15. * systems. This has its own special mountain of errata.
  16. *
  17. * TODO:
  18. * Test PARISC SuperIO
  19. * Get someone to test on SPARC
  20. * Implement lazy pio/dma switching for better performance
  21. * 8bit shared timing.
  22. * See if we need to kill the FIFO for ATAPI
  23. */
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/pci.h>
  27. #include <linux/init.h>
  28. #include <linux/blkdev.h>
  29. #include <linux/delay.h>
  30. #include <linux/device.h>
  31. #include <scsi/scsi_host.h>
  32. #include <linux/libata.h>
  33. #include <linux/ata.h>
  34. #define DRV_NAME "pata_ns87415"
  35. #define DRV_VERSION "0.0.1"
  36. /**
  37. * ns87415_set_mode - Initialize host controller mode timings
  38. * @ap: Port whose timings we are configuring
  39. * @adev: Device whose timings we are configuring
  40. * @mode: Mode to set
  41. *
  42. * Program the mode registers for this controller, channel and
  43. * device. Because the chip is quite an old design we have to do this
  44. * for PIO/DMA switches.
  45. *
  46. * LOCKING:
  47. * None (inherited from caller).
  48. */
  49. static void ns87415_set_mode(struct ata_port *ap, struct ata_device *adev, u8 mode)
  50. {
  51. struct pci_dev *dev = to_pci_dev(ap->host->dev);
  52. int unit = 2 * ap->port_no + adev->devno;
  53. int timing = 0x44 + 2 * unit;
  54. unsigned long T = 1000000000 / 33333; /* PCI clocks */
  55. struct ata_timing t;
  56. u16 clocking;
  57. u8 iordy;
  58. u8 status;
  59. /* Timing register format is 17 - low nybble read timing with
  60. the high nybble being 16 - x for recovery time in PCI clocks */
  61. ata_timing_compute(adev, adev->pio_mode, &t, T, 0);
  62. clocking = 17 - clamp_val(t.active, 2, 17);
  63. clocking |= (16 - clamp_val(t.recover, 1, 16)) << 4;
  64. /* Use the same timing for read and write bytes */
  65. clocking |= (clocking << 8);
  66. pci_write_config_word(dev, timing, clocking);
  67. /* Set the IORDY enable versus DMA enable on or off properly */
  68. pci_read_config_byte(dev, 0x42, &iordy);
  69. iordy &= ~(1 << (4 + unit));
  70. if (mode >= XFER_MW_DMA_0 || !ata_pio_need_iordy(adev))
  71. iordy |= (1 << (4 + unit));
  72. /* Paranoia: We shouldn't ever get here with busy write buffers
  73. but if so wait */
  74. pci_read_config_byte(dev, 0x43, &status);
  75. while (status & 0x03) {
  76. udelay(1);
  77. pci_read_config_byte(dev, 0x43, &status);
  78. }
  79. /* Flip the IORDY/DMA bits now we are sure the write buffers are
  80. clear */
  81. pci_write_config_byte(dev, 0x42, iordy);
  82. /* TODO: Set byte 54 command timing to the best 8bit
  83. mode shared by all four devices */
  84. }
  85. /**
  86. * ns87415_set_piomode - Initialize host controller PATA PIO timings
  87. * @ap: Port whose timings we are configuring
  88. * @adev: Device to program
  89. *
  90. * Set PIO mode for device, in host controller PCI config space.
  91. *
  92. * LOCKING:
  93. * None (inherited from caller).
  94. */
  95. static void ns87415_set_piomode(struct ata_port *ap, struct ata_device *adev)
  96. {
  97. ns87415_set_mode(ap, adev, adev->pio_mode);
  98. }
  99. /**
  100. * ns87415_bmdma_setup - Set up DMA
  101. * @qc: Command block
  102. *
  103. * Set up for bus masterng DMA. We have to do this ourselves
  104. * rather than use the helper due to a chip erratum
  105. */
  106. static void ns87415_bmdma_setup(struct ata_queued_cmd *qc)
  107. {
  108. struct ata_port *ap = qc->ap;
  109. unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
  110. u8 dmactl;
  111. /* load PRD table addr. */
  112. mb(); /* make sure PRD table writes are visible to controller */
  113. iowrite32(ap->bmdma_prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
  114. /* specify data direction, triple-check start bit is clear */
  115. dmactl = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
  116. dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
  117. /* Due to an erratum we need to write these bits to the wrong
  118. place - which does save us an I/O bizarrely */
  119. dmactl |= ATA_DMA_INTR | ATA_DMA_ERR;
  120. if (!rw)
  121. dmactl |= ATA_DMA_WR;
  122. iowrite8(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
  123. /* issue r/w command */
  124. ap->ops->sff_exec_command(ap, &qc->tf);
  125. }
  126. /**
  127. * ns87415_bmdma_start - Begin DMA transfer
  128. * @qc: Command block
  129. *
  130. * Switch the timings for the chip and set up for a DMA transfer
  131. * before the DMA burst begins.
  132. *
  133. * FIXME: We should do lazy switching on bmdma_start versus
  134. * ata_pio_data_xfer for better performance.
  135. */
  136. static void ns87415_bmdma_start(struct ata_queued_cmd *qc)
  137. {
  138. ns87415_set_mode(qc->ap, qc->dev, qc->dev->dma_mode);
  139. ata_bmdma_start(qc);
  140. }
  141. /**
  142. * ns87415_bmdma_stop - End DMA transfer
  143. * @qc: Command block
  144. *
  145. * End DMA mode and switch the controller back into PIO mode
  146. */
  147. static void ns87415_bmdma_stop(struct ata_queued_cmd *qc)
  148. {
  149. ata_bmdma_stop(qc);
  150. ns87415_set_mode(qc->ap, qc->dev, qc->dev->pio_mode);
  151. }
  152. /**
  153. * ns87415_irq_clear - Clear interrupt
  154. * @ap: Channel to clear
  155. *
  156. * Erratum: Due to a chip bug regisers 02 and 0A bit 1 and 2 (the
  157. * error bits) are reset by writing to register 00 or 08.
  158. */
  159. static void ns87415_irq_clear(struct ata_port *ap)
  160. {
  161. void __iomem *mmio = ap->ioaddr.bmdma_addr;
  162. if (!mmio)
  163. return;
  164. iowrite8((ioread8(mmio + ATA_DMA_CMD) | ATA_DMA_INTR | ATA_DMA_ERR),
  165. mmio + ATA_DMA_CMD);
  166. }
  167. /**
  168. * ns87415_check_atapi_dma - ATAPI DMA filter
  169. * @qc: Command block
  170. *
  171. * Disable ATAPI DMA (for now). We may be able to do DMA if we
  172. * kill the prefetching. This isn't clear.
  173. */
  174. static int ns87415_check_atapi_dma(struct ata_queued_cmd *qc)
  175. {
  176. return -EOPNOTSUPP;
  177. }
  178. #if defined(CONFIG_SUPERIO)
  179. /* SUPERIO 87560 is a PoS chip that NatSem denies exists.
  180. * Unfortunately, it's built-in on all Astro-based PA-RISC workstations
  181. * which use the integrated NS87514 cell for CD-ROM support.
  182. * i.e we have to support for CD-ROM installs.
  183. * See drivers/parisc/superio.c for more gory details.
  184. *
  185. * Workarounds taken from drivers/ide/pci/ns87415.c
  186. */
  187. #include <asm/superio.h>
  188. #define SUPERIO_IDE_MAX_RETRIES 25
  189. /**
  190. * ns87560_read_buggy - workaround buggy Super I/O chip
  191. * @port: Port to read
  192. *
  193. * Work around chipset problems in the 87560 SuperIO chip
  194. */
  195. static u8 ns87560_read_buggy(void __iomem *port)
  196. {
  197. u8 tmp;
  198. int retries = SUPERIO_IDE_MAX_RETRIES;
  199. do {
  200. tmp = ioread8(port);
  201. if (tmp != 0)
  202. return tmp;
  203. udelay(50);
  204. } while(retries-- > 0);
  205. return tmp;
  206. }
  207. /**
  208. * ns87560_check_status
  209. * @ap: channel to check
  210. *
  211. * Return the status of the channel working around the
  212. * 87560 flaws.
  213. */
  214. static u8 ns87560_check_status(struct ata_port *ap)
  215. {
  216. return ns87560_read_buggy(ap->ioaddr.status_addr);
  217. }
  218. /**
  219. * ns87560_tf_read - input device's ATA taskfile shadow registers
  220. * @ap: Port from which input is read
  221. * @tf: ATA taskfile register set for storing input
  222. *
  223. * Reads ATA taskfile registers for currently-selected device
  224. * into @tf. Work around the 87560 bugs.
  225. *
  226. * LOCKING:
  227. * Inherited from caller.
  228. */
  229. void ns87560_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
  230. {
  231. struct ata_ioports *ioaddr = &ap->ioaddr;
  232. tf->command = ns87560_check_status(ap);
  233. tf->feature = ioread8(ioaddr->error_addr);
  234. tf->nsect = ioread8(ioaddr->nsect_addr);
  235. tf->lbal = ioread8(ioaddr->lbal_addr);
  236. tf->lbam = ioread8(ioaddr->lbam_addr);
  237. tf->lbah = ioread8(ioaddr->lbah_addr);
  238. tf->device = ns87560_read_buggy(ioaddr->device_addr);
  239. if (tf->flags & ATA_TFLAG_LBA48) {
  240. iowrite8(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
  241. tf->hob_feature = ioread8(ioaddr->error_addr);
  242. tf->hob_nsect = ioread8(ioaddr->nsect_addr);
  243. tf->hob_lbal = ioread8(ioaddr->lbal_addr);
  244. tf->hob_lbam = ioread8(ioaddr->lbam_addr);
  245. tf->hob_lbah = ioread8(ioaddr->lbah_addr);
  246. iowrite8(tf->ctl, ioaddr->ctl_addr);
  247. ap->last_ctl = tf->ctl;
  248. }
  249. }
  250. /**
  251. * ns87560_bmdma_status
  252. * @ap: channel to check
  253. *
  254. * Return the DMA status of the channel working around the
  255. * 87560 flaws.
  256. */
  257. static u8 ns87560_bmdma_status(struct ata_port *ap)
  258. {
  259. return ns87560_read_buggy(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
  260. }
  261. #endif /* 87560 SuperIO Support */
  262. static struct ata_port_operations ns87415_pata_ops = {
  263. .inherits = &ata_bmdma_port_ops,
  264. .check_atapi_dma = ns87415_check_atapi_dma,
  265. .bmdma_setup = ns87415_bmdma_setup,
  266. .bmdma_start = ns87415_bmdma_start,
  267. .bmdma_stop = ns87415_bmdma_stop,
  268. .sff_irq_clear = ns87415_irq_clear,
  269. .cable_detect = ata_cable_40wire,
  270. .set_piomode = ns87415_set_piomode,
  271. };
  272. #if defined(CONFIG_SUPERIO)
  273. static struct ata_port_operations ns87560_pata_ops = {
  274. .inherits = &ns87415_pata_ops,
  275. .sff_tf_read = ns87560_tf_read,
  276. .sff_check_status = ns87560_check_status,
  277. .bmdma_status = ns87560_bmdma_status,
  278. };
  279. #endif
  280. static struct scsi_host_template ns87415_sht = {
  281. ATA_BMDMA_SHT(DRV_NAME),
  282. };
  283. static void ns87415_fixup(struct pci_dev *pdev)
  284. {
  285. /* Select 512 byte sectors */
  286. pci_write_config_byte(pdev, 0x55, 0xEE);
  287. /* Select PIO0 8bit clocking */
  288. pci_write_config_byte(pdev, 0x54, 0xB7);
  289. }
  290. /**
  291. * ns87415_init_one - Register 87415 ATA PCI device with kernel services
  292. * @pdev: PCI device to register
  293. * @ent: Entry in ns87415_pci_tbl matching with @pdev
  294. *
  295. * Called from kernel PCI layer. We probe for combined mode (sigh),
  296. * and then hand over control to libata, for it to do the rest.
  297. *
  298. * LOCKING:
  299. * Inherited from PCI layer (may sleep).
  300. *
  301. * RETURNS:
  302. * Zero on success, or -ERRNO value.
  303. */
  304. static int ns87415_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
  305. {
  306. static const struct ata_port_info info = {
  307. .flags = ATA_FLAG_SLAVE_POSS,
  308. .pio_mask = ATA_PIO4,
  309. .mwdma_mask = ATA_MWDMA2,
  310. .port_ops = &ns87415_pata_ops,
  311. };
  312. const struct ata_port_info *ppi[] = { &info, NULL };
  313. int rc;
  314. #if defined(CONFIG_SUPERIO)
  315. static const struct ata_port_info info87560 = {
  316. .flags = ATA_FLAG_SLAVE_POSS,
  317. .pio_mask = ATA_PIO4,
  318. .mwdma_mask = ATA_MWDMA2,
  319. .port_ops = &ns87560_pata_ops,
  320. };
  321. if (PCI_SLOT(pdev->devfn) == 0x0E)
  322. ppi[0] = &info87560;
  323. #endif
  324. ata_print_version_once(&pdev->dev, DRV_VERSION);
  325. rc = pcim_enable_device(pdev);
  326. if (rc)
  327. return rc;
  328. ns87415_fixup(pdev);
  329. return ata_pci_bmdma_init_one(pdev, ppi, &ns87415_sht, NULL, 0);
  330. }
  331. static const struct pci_device_id ns87415_pci_tbl[] = {
  332. { PCI_VDEVICE(NS, PCI_DEVICE_ID_NS_87415), },
  333. { } /* terminate list */
  334. };
  335. #ifdef CONFIG_PM
  336. static int ns87415_reinit_one(struct pci_dev *pdev)
  337. {
  338. struct ata_host *host = dev_get_drvdata(&pdev->dev);
  339. int rc;
  340. rc = ata_pci_device_do_resume(pdev);
  341. if (rc)
  342. return rc;
  343. ns87415_fixup(pdev);
  344. ata_host_resume(host);
  345. return 0;
  346. }
  347. #endif
  348. static struct pci_driver ns87415_pci_driver = {
  349. .name = DRV_NAME,
  350. .id_table = ns87415_pci_tbl,
  351. .probe = ns87415_init_one,
  352. .remove = ata_pci_remove_one,
  353. #ifdef CONFIG_PM
  354. .suspend = ata_pci_device_suspend,
  355. .resume = ns87415_reinit_one,
  356. #endif
  357. };
  358. static int __init ns87415_init(void)
  359. {
  360. return pci_register_driver(&ns87415_pci_driver);
  361. }
  362. static void __exit ns87415_exit(void)
  363. {
  364. pci_unregister_driver(&ns87415_pci_driver);
  365. }
  366. module_init(ns87415_init);
  367. module_exit(ns87415_exit);
  368. MODULE_AUTHOR("Alan Cox");
  369. MODULE_DESCRIPTION("ATA low-level driver for NS87415 controllers");
  370. MODULE_LICENSE("GPL");
  371. MODULE_DEVICE_TABLE(pci, ns87415_pci_tbl);
  372. MODULE_VERSION(DRV_VERSION);