pata_sl82c105.c 8.9 KB

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