pata_cs5520.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * IDE tuning and bus mastering support for the CS5510/CS5520
  4. * chipsets
  5. *
  6. * The CS5510/CS5520 are slightly unusual devices. Unlike the
  7. * typical IDE controllers they do bus mastering with the drive in
  8. * PIO mode and smarter silicon.
  9. *
  10. * The practical upshot of this is that we must always tune the
  11. * drive for the right PIO mode. We must also ignore all the blacklists
  12. * and the drive bus mastering DMA information. Also to confuse matters
  13. * further we can do DMA on PIO only drives.
  14. *
  15. * DMA on the 5510 also requires we disable_hlt() during DMA on early
  16. * revisions.
  17. *
  18. * *** This driver is strictly experimental ***
  19. *
  20. * (c) Copyright Red Hat Inc 2002
  21. *
  22. * Documentation:
  23. * Not publicly available.
  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_cs5520"
  33. #define DRV_VERSION "0.6.6"
  34. struct pio_clocks
  35. {
  36. int address;
  37. int assert;
  38. int recovery;
  39. };
  40. static const struct pio_clocks cs5520_pio_clocks[]={
  41. {3, 6, 11},
  42. {2, 5, 6},
  43. {1, 4, 3},
  44. {1, 3, 2},
  45. {1, 2, 1}
  46. };
  47. /**
  48. * cs5520_set_timings - program PIO timings
  49. * @ap: ATA port
  50. * @adev: ATA device
  51. * @pio: PIO ID
  52. *
  53. * Program the PIO mode timings for the controller according to the pio
  54. * clocking table.
  55. */
  56. static void cs5520_set_timings(struct ata_port *ap, struct ata_device *adev, int pio)
  57. {
  58. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  59. int slave = adev->devno;
  60. pio -= XFER_PIO_0;
  61. /* Channel command timing */
  62. pci_write_config_byte(pdev, 0x62 + ap->port_no,
  63. (cs5520_pio_clocks[pio].recovery << 4) |
  64. (cs5520_pio_clocks[pio].assert));
  65. /* FIXME: should these use address ? */
  66. /* Read command timing */
  67. pci_write_config_byte(pdev, 0x64 + 4*ap->port_no + slave,
  68. (cs5520_pio_clocks[pio].recovery << 4) |
  69. (cs5520_pio_clocks[pio].assert));
  70. /* Write command timing */
  71. pci_write_config_byte(pdev, 0x66 + 4*ap->port_no + slave,
  72. (cs5520_pio_clocks[pio].recovery << 4) |
  73. (cs5520_pio_clocks[pio].assert));
  74. }
  75. /**
  76. * cs5520_set_piomode - program PIO timings
  77. * @ap: ATA port
  78. * @adev: ATA device
  79. *
  80. * Program the PIO mode timings for the controller according to the pio
  81. * clocking table.
  82. */
  83. static void cs5520_set_piomode(struct ata_port *ap, struct ata_device *adev)
  84. {
  85. cs5520_set_timings(ap, adev, adev->pio_mode);
  86. }
  87. static struct scsi_host_template cs5520_sht = {
  88. ATA_BASE_SHT(DRV_NAME),
  89. .sg_tablesize = LIBATA_DUMB_MAX_PRD,
  90. .dma_boundary = ATA_DMA_BOUNDARY,
  91. };
  92. static struct ata_port_operations cs5520_port_ops = {
  93. .inherits = &ata_bmdma_port_ops,
  94. .qc_prep = ata_bmdma_dumb_qc_prep,
  95. .cable_detect = ata_cable_40wire,
  96. .set_piomode = cs5520_set_piomode,
  97. };
  98. static int cs5520_init_one(struct pci_dev *pdev, const struct pci_device_id *id)
  99. {
  100. static const unsigned int cmd_port[] = { 0x1F0, 0x170 };
  101. static const unsigned int ctl_port[] = { 0x3F6, 0x376 };
  102. struct ata_port_info pi = {
  103. .flags = ATA_FLAG_SLAVE_POSS,
  104. .pio_mask = ATA_PIO4,
  105. .port_ops = &cs5520_port_ops,
  106. };
  107. const struct ata_port_info *ppi[2];
  108. u8 pcicfg;
  109. void __iomem *iomap[5];
  110. struct ata_host *host;
  111. struct ata_ioports *ioaddr;
  112. int i, rc;
  113. rc = pcim_enable_device(pdev);
  114. if (rc)
  115. return rc;
  116. /* IDE port enable bits */
  117. pci_read_config_byte(pdev, 0x60, &pcicfg);
  118. /* Check if the ATA ports are enabled */
  119. if ((pcicfg & 3) == 0)
  120. return -ENODEV;
  121. ppi[0] = ppi[1] = &ata_dummy_port_info;
  122. if (pcicfg & 1)
  123. ppi[0] = &pi;
  124. if (pcicfg & 2)
  125. ppi[1] = &pi;
  126. if ((pcicfg & 0x40) == 0) {
  127. dev_warn(&pdev->dev, "DMA mode disabled. Enabling.\n");
  128. pci_write_config_byte(pdev, 0x60, pcicfg | 0x40);
  129. }
  130. pi.mwdma_mask = id->driver_data;
  131. host = ata_host_alloc_pinfo(&pdev->dev, ppi, 2);
  132. if (!host)
  133. return -ENOMEM;
  134. /* Perform set up for DMA */
  135. if (pci_enable_device_io(pdev)) {
  136. dev_err(&pdev->dev, "unable to configure BAR2.\n");
  137. return -ENODEV;
  138. }
  139. if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32))) {
  140. dev_err(&pdev->dev, "unable to configure DMA mask.\n");
  141. return -ENODEV;
  142. }
  143. /* Map IO ports and initialize host accordingly */
  144. iomap[0] = devm_ioport_map(&pdev->dev, cmd_port[0], 8);
  145. iomap[1] = devm_ioport_map(&pdev->dev, ctl_port[0], 1);
  146. iomap[2] = devm_ioport_map(&pdev->dev, cmd_port[1], 8);
  147. iomap[3] = devm_ioport_map(&pdev->dev, ctl_port[1], 1);
  148. iomap[4] = pcim_iomap(pdev, 2, 0);
  149. if (!iomap[0] || !iomap[1] || !iomap[2] || !iomap[3] || !iomap[4])
  150. return -ENOMEM;
  151. ioaddr = &host->ports[0]->ioaddr;
  152. ioaddr->cmd_addr = iomap[0];
  153. ioaddr->ctl_addr = iomap[1];
  154. ioaddr->altstatus_addr = iomap[1];
  155. ioaddr->bmdma_addr = iomap[4];
  156. ata_sff_std_ports(ioaddr);
  157. ata_port_desc(host->ports[0],
  158. "cmd 0x%x ctl 0x%x", cmd_port[0], ctl_port[0]);
  159. ata_port_pbar_desc(host->ports[0], 4, 0, "bmdma");
  160. ioaddr = &host->ports[1]->ioaddr;
  161. ioaddr->cmd_addr = iomap[2];
  162. ioaddr->ctl_addr = iomap[3];
  163. ioaddr->altstatus_addr = iomap[3];
  164. ioaddr->bmdma_addr = iomap[4] + 8;
  165. ata_sff_std_ports(ioaddr);
  166. ata_port_desc(host->ports[1],
  167. "cmd 0x%x ctl 0x%x", cmd_port[1], ctl_port[1]);
  168. ata_port_pbar_desc(host->ports[1], 4, 8, "bmdma");
  169. /* activate the host */
  170. pci_set_master(pdev);
  171. rc = ata_host_start(host);
  172. if (rc)
  173. return rc;
  174. for (i = 0; i < 2; i++) {
  175. static const int irq[] = { 14, 15 };
  176. struct ata_port *ap = host->ports[i];
  177. if (ata_port_is_dummy(ap))
  178. continue;
  179. rc = devm_request_irq(&pdev->dev, irq[ap->port_no],
  180. ata_bmdma_interrupt, 0, DRV_NAME, host);
  181. if (rc)
  182. return rc;
  183. ata_port_desc(ap, "irq %d", irq[i]);
  184. }
  185. return ata_host_register(host, &cs5520_sht);
  186. }
  187. #ifdef CONFIG_PM_SLEEP
  188. /**
  189. * cs5520_reinit_one - device resume
  190. * @pdev: PCI device
  191. *
  192. * Do any reconfiguration work needed by a resume from RAM. We need
  193. * to restore DMA mode support on BIOSen which disabled it
  194. */
  195. static int cs5520_reinit_one(struct pci_dev *pdev)
  196. {
  197. struct ata_host *host = pci_get_drvdata(pdev);
  198. u8 pcicfg;
  199. int rc;
  200. rc = ata_pci_device_do_resume(pdev);
  201. if (rc)
  202. return rc;
  203. pci_read_config_byte(pdev, 0x60, &pcicfg);
  204. if ((pcicfg & 0x40) == 0)
  205. pci_write_config_byte(pdev, 0x60, pcicfg | 0x40);
  206. ata_host_resume(host);
  207. return 0;
  208. }
  209. /**
  210. * cs5520_pci_device_suspend - device suspend
  211. * @pdev: PCI device
  212. * @mesg: PM event message
  213. *
  214. * We have to cut and waste bits from the standard method because
  215. * the 5520 is a bit odd and not just a pure ATA device. As a result
  216. * we must not disable it. The needed code is short and this avoids
  217. * chip specific mess in the core code.
  218. */
  219. static int cs5520_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
  220. {
  221. struct ata_host *host = pci_get_drvdata(pdev);
  222. ata_host_suspend(host, mesg);
  223. pci_save_state(pdev);
  224. return 0;
  225. }
  226. #endif /* CONFIG_PM_SLEEP */
  227. /* For now keep DMA off. We can set it for all but A rev CS5510 once the
  228. core ATA code can handle it */
  229. static const struct pci_device_id pata_cs5520[] = {
  230. { PCI_VDEVICE(CYRIX, PCI_DEVICE_ID_CYRIX_5510), },
  231. { PCI_VDEVICE(CYRIX, PCI_DEVICE_ID_CYRIX_5520), },
  232. { },
  233. };
  234. static struct pci_driver cs5520_pci_driver = {
  235. .name = DRV_NAME,
  236. .id_table = pata_cs5520,
  237. .probe = cs5520_init_one,
  238. .remove = ata_pci_remove_one,
  239. #ifdef CONFIG_PM_SLEEP
  240. .suspend = cs5520_pci_device_suspend,
  241. .resume = cs5520_reinit_one,
  242. #endif
  243. };
  244. module_pci_driver(cs5520_pci_driver);
  245. MODULE_AUTHOR("Alan Cox");
  246. MODULE_DESCRIPTION("low-level driver for Cyrix CS5510/5520");
  247. MODULE_LICENSE("GPL");
  248. MODULE_DEVICE_TABLE(pci, pata_cs5520);
  249. MODULE_VERSION(DRV_VERSION);