pata_sl82c105.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. * pata_sl82c105.c - SL82C105 PATA for new ATA layer
  3. * (C) 2005 Red Hat Inc
  4. * (C) 2011 Bartlomiej Zolnierkiewicz
  5. *
  6. * Based in part on linux/drivers/ide/pci/sl82c105.c
  7. * SL82C105/Winbond 553 IDE driver
  8. *
  9. * and in part on the documentation and errata sheet
  10. *
  11. *
  12. * Note: The controller like many controllers has shared timings for
  13. * PIO and DMA. We thus flip to the DMA timings in dma_start and flip back
  14. * in the dma_stop function. Thus we actually don't need a set_dmamode
  15. * method as the PIO method is always called and will set the right PIO
  16. * timing parameters.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/pci.h>
  21. #include <linux/blkdev.h>
  22. #include <linux/delay.h>
  23. #include <scsi/scsi_host.h>
  24. #include <linux/libata.h>
  25. #define DRV_NAME "pata_sl82c105"
  26. #define DRV_VERSION "0.3.3"
  27. enum {
  28. /*
  29. * SL82C105 PCI config register 0x40 bits.
  30. */
  31. CTRL_IDE_IRQB = (1 << 30),
  32. CTRL_IDE_IRQA = (1 << 28),
  33. CTRL_LEGIRQ = (1 << 11),
  34. CTRL_P1F16 = (1 << 5),
  35. CTRL_P1EN = (1 << 4),
  36. CTRL_P0F16 = (1 << 1),
  37. CTRL_P0EN = (1 << 0)
  38. };
  39. /**
  40. * sl82c105_pre_reset - probe begin
  41. * @link: ATA link
  42. * @deadline: deadline jiffies for the operation
  43. *
  44. * Set up cable type and use generic probe init
  45. */
  46. static int sl82c105_pre_reset(struct ata_link *link, unsigned long deadline)
  47. {
  48. static const struct pci_bits sl82c105_enable_bits[] = {
  49. { 0x40, 1, 0x01, 0x01 },
  50. { 0x40, 1, 0x10, 0x10 }
  51. };
  52. struct ata_port *ap = link->ap;
  53. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  54. if (ap->port_no && !pci_test_config_bits(pdev, &sl82c105_enable_bits[ap->port_no]))
  55. return -ENOENT;
  56. return ata_sff_prereset(link, deadline);
  57. }
  58. /**
  59. * sl82c105_configure_piomode - set chip PIO timing
  60. * @ap: ATA interface
  61. * @adev: ATA device
  62. * @pio: PIO mode
  63. *
  64. * Called to do the PIO mode setup. Our timing registers are shared
  65. * so a configure_dmamode call will undo any work we do here and vice
  66. * versa
  67. */
  68. static void sl82c105_configure_piomode(struct ata_port *ap, struct ata_device *adev, int pio)
  69. {
  70. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  71. static u16 pio_timing[5] = {
  72. 0x50D, 0x407, 0x304, 0x242, 0x240
  73. };
  74. u16 dummy;
  75. int timing = 0x44 + (8 * ap->port_no) + (4 * adev->devno);
  76. pci_write_config_word(pdev, timing, pio_timing[pio]);
  77. /* Can we lose this oddity of the old driver */
  78. pci_read_config_word(pdev, timing, &dummy);
  79. }
  80. /**
  81. * sl82c105_set_piomode - set initial PIO mode data
  82. * @ap: ATA interface
  83. * @adev: ATA device
  84. *
  85. * Called to do the PIO mode setup. Our timing registers are shared
  86. * but we want to set the PIO timing by default.
  87. */
  88. static void sl82c105_set_piomode(struct ata_port *ap, struct ata_device *adev)
  89. {
  90. sl82c105_configure_piomode(ap, adev, adev->pio_mode - XFER_PIO_0);
  91. }
  92. /**
  93. * sl82c105_configure_dmamode - set DMA mode in chip
  94. * @ap: ATA interface
  95. * @adev: ATA device
  96. *
  97. * Load DMA cycle times into the chip ready for a DMA transfer
  98. * to occur.
  99. */
  100. static void sl82c105_configure_dmamode(struct ata_port *ap, struct ata_device *adev)
  101. {
  102. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  103. static u16 dma_timing[3] = {
  104. 0x707, 0x201, 0x200
  105. };
  106. u16 dummy;
  107. int timing = 0x44 + (8 * ap->port_no) + (4 * adev->devno);
  108. int dma = adev->dma_mode - XFER_MW_DMA_0;
  109. pci_write_config_word(pdev, timing, dma_timing[dma]);
  110. /* Can we lose this oddity of the old driver */
  111. pci_read_config_word(pdev, timing, &dummy);
  112. }
  113. /**
  114. * sl82c105_reset_engine - Reset the DMA engine
  115. * @ap: ATA interface
  116. *
  117. * The sl82c105 has some serious problems with the DMA engine
  118. * when transfers don't run as expected or ATAPI is used. The
  119. * recommended fix is to reset the engine each use using a chip
  120. * test register.
  121. */
  122. static void sl82c105_reset_engine(struct ata_port *ap)
  123. {
  124. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  125. u16 val;
  126. pci_read_config_word(pdev, 0x7E, &val);
  127. pci_write_config_word(pdev, 0x7E, val | 4);
  128. pci_write_config_word(pdev, 0x7E, val & ~4);
  129. }
  130. /**
  131. * sl82c105_bmdma_start - DMA engine begin
  132. * @qc: ATA command
  133. *
  134. * Reset the DMA engine each use as recommended by the errata
  135. * document.
  136. *
  137. * FIXME: if we switch clock at BMDMA start/end we might get better
  138. * PIO performance on DMA capable devices.
  139. */
  140. static void sl82c105_bmdma_start(struct ata_queued_cmd *qc)
  141. {
  142. struct ata_port *ap = qc->ap;
  143. udelay(100);
  144. sl82c105_reset_engine(ap);
  145. udelay(100);
  146. /* Set the clocks for DMA */
  147. sl82c105_configure_dmamode(ap, qc->dev);
  148. /* Activate DMA */
  149. ata_bmdma_start(qc);
  150. }
  151. /**
  152. * sl82c105_bmdma_end - DMA engine stop
  153. * @qc: ATA command
  154. *
  155. * Reset the DMA engine each use as recommended by the errata
  156. * document.
  157. *
  158. * This function is also called to turn off DMA when a timeout occurs
  159. * during DMA operation. In both cases we need to reset the engine,
  160. * so no actual eng_timeout handler is required.
  161. *
  162. * We assume bmdma_stop is always called if bmdma_start as called. If
  163. * not then we may need to wrap qc_issue.
  164. */
  165. static void sl82c105_bmdma_stop(struct ata_queued_cmd *qc)
  166. {
  167. struct ata_port *ap = qc->ap;
  168. ata_bmdma_stop(qc);
  169. sl82c105_reset_engine(ap);
  170. udelay(100);
  171. /* This will redo the initial setup of the DMA device to matching
  172. PIO timings */
  173. sl82c105_set_piomode(ap, qc->dev);
  174. }
  175. /**
  176. * sl82c105_qc_defer - implement serialization
  177. * @qc: command
  178. *
  179. * We must issue one command per host not per channel because
  180. * of the reset bug.
  181. *
  182. * Q: is the scsi host lock sufficient ?
  183. */
  184. static int sl82c105_qc_defer(struct ata_queued_cmd *qc)
  185. {
  186. struct ata_host *host = qc->ap->host;
  187. struct ata_port *alt = host->ports[1 ^ qc->ap->port_no];
  188. int rc;
  189. /* First apply the usual rules */
  190. rc = ata_std_qc_defer(qc);
  191. if (rc != 0)
  192. return rc;
  193. /* Now apply serialization rules. Only allow a command if the
  194. other channel state machine is idle */
  195. if (alt && alt->qc_active)
  196. return ATA_DEFER_PORT;
  197. return 0;
  198. }
  199. static bool sl82c105_sff_irq_check(struct ata_port *ap)
  200. {
  201. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  202. u32 val, mask = ap->port_no ? CTRL_IDE_IRQB : CTRL_IDE_IRQA;
  203. pci_read_config_dword(pdev, 0x40, &val);
  204. return val & mask;
  205. }
  206. static struct scsi_host_template sl82c105_sht = {
  207. ATA_BMDMA_SHT(DRV_NAME),
  208. };
  209. static struct ata_port_operations sl82c105_port_ops = {
  210. .inherits = &ata_bmdma_port_ops,
  211. .qc_defer = sl82c105_qc_defer,
  212. .bmdma_start = sl82c105_bmdma_start,
  213. .bmdma_stop = sl82c105_bmdma_stop,
  214. .cable_detect = ata_cable_40wire,
  215. .set_piomode = sl82c105_set_piomode,
  216. .prereset = sl82c105_pre_reset,
  217. .sff_irq_check = sl82c105_sff_irq_check,
  218. };
  219. /**
  220. * sl82c105_bridge_revision - find bridge version
  221. * @pdev: PCI device for the ATA function
  222. *
  223. * Locates the PCI bridge associated with the ATA function and
  224. * providing it is a Winbond 553 reports the revision. If it cannot
  225. * find a revision or the right device it returns -1
  226. */
  227. static int sl82c105_bridge_revision(struct pci_dev *pdev)
  228. {
  229. struct pci_dev *bridge;
  230. /*
  231. * The bridge should be part of the same device, but function 0.
  232. */
  233. bridge = pci_get_slot(pdev->bus,
  234. PCI_DEVFN(PCI_SLOT(pdev->devfn), 0));
  235. if (!bridge)
  236. return -1;
  237. /*
  238. * Make sure it is a Winbond 553 and is an ISA bridge.
  239. */
  240. if (bridge->vendor != PCI_VENDOR_ID_WINBOND ||
  241. bridge->device != PCI_DEVICE_ID_WINBOND_83C553 ||
  242. bridge->class >> 8 != PCI_CLASS_BRIDGE_ISA) {
  243. pci_dev_put(bridge);
  244. return -1;
  245. }
  246. /*
  247. * We need to find function 0's revision, not function 1
  248. */
  249. pci_dev_put(bridge);
  250. return bridge->revision;
  251. }
  252. static void sl82c105_fixup(struct pci_dev *pdev)
  253. {
  254. u32 val;
  255. pci_read_config_dword(pdev, 0x40, &val);
  256. val |= CTRL_P0EN | CTRL_P0F16 | CTRL_P1F16;
  257. pci_write_config_dword(pdev, 0x40, val);
  258. }
  259. static int sl82c105_init_one(struct pci_dev *dev, const struct pci_device_id *id)
  260. {
  261. static const struct ata_port_info info_dma = {
  262. .flags = ATA_FLAG_SLAVE_POSS,
  263. .pio_mask = ATA_PIO4,
  264. .mwdma_mask = ATA_MWDMA2,
  265. .port_ops = &sl82c105_port_ops
  266. };
  267. static const struct ata_port_info info_early = {
  268. .flags = ATA_FLAG_SLAVE_POSS,
  269. .pio_mask = ATA_PIO4,
  270. .port_ops = &sl82c105_port_ops
  271. };
  272. /* for now use only the first port */
  273. const struct ata_port_info *ppi[] = { &info_early,
  274. NULL };
  275. int rev;
  276. int rc;
  277. rc = pcim_enable_device(dev);
  278. if (rc)
  279. return rc;
  280. rev = sl82c105_bridge_revision(dev);
  281. if (rev == -1)
  282. dev_warn(&dev->dev,
  283. "pata_sl82c105: Unable to find bridge, disabling DMA\n");
  284. else if (rev <= 5)
  285. dev_warn(&dev->dev,
  286. "pata_sl82c105: Early bridge revision, no DMA available\n");
  287. else
  288. ppi[0] = &info_dma;
  289. sl82c105_fixup(dev);
  290. return ata_pci_bmdma_init_one(dev, ppi, &sl82c105_sht, NULL, 0);
  291. }
  292. #ifdef CONFIG_PM_SLEEP
  293. static int sl82c105_reinit_one(struct pci_dev *pdev)
  294. {
  295. struct ata_host *host = pci_get_drvdata(pdev);
  296. int rc;
  297. rc = ata_pci_device_do_resume(pdev);
  298. if (rc)
  299. return rc;
  300. sl82c105_fixup(pdev);
  301. ata_host_resume(host);
  302. return 0;
  303. }
  304. #endif
  305. static const struct pci_device_id sl82c105[] = {
  306. { PCI_VDEVICE(WINBOND, PCI_DEVICE_ID_WINBOND_82C105), },
  307. { },
  308. };
  309. static struct pci_driver sl82c105_pci_driver = {
  310. .name = DRV_NAME,
  311. .id_table = sl82c105,
  312. .probe = sl82c105_init_one,
  313. .remove = ata_pci_remove_one,
  314. #ifdef CONFIG_PM_SLEEP
  315. .suspend = ata_pci_device_suspend,
  316. .resume = sl82c105_reinit_one,
  317. #endif
  318. };
  319. module_pci_driver(sl82c105_pci_driver);
  320. MODULE_AUTHOR("Alan Cox");
  321. MODULE_DESCRIPTION("low-level driver for Sl82c105");
  322. MODULE_LICENSE("GPL");
  323. MODULE_DEVICE_TABLE(pci, sl82c105);
  324. MODULE_VERSION(DRV_VERSION);