pata_sc1200.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * New ATA layer SC1200 driver Alan Cox <alan@lxorguk.ukuu.org.uk>
  4. *
  5. * TODO: Mode selection filtering
  6. * TODO: Needs custom DMA cleanup code
  7. *
  8. * Based very heavily on
  9. *
  10. * linux/drivers/ide/pci/sc1200.c Version 0.91 28-Jan-2003
  11. *
  12. * Copyright (C) 2000-2002 Mark Lord <mlord@pobox.com>
  13. * May be copied or modified under the terms of the GNU General Public License
  14. *
  15. * Development of this chipset driver was funded
  16. * by the nice folks at National Semiconductor.
  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_sc1200"
  26. #define DRV_VERSION "0.2.6"
  27. #define SC1200_REV_A 0x00
  28. #define SC1200_REV_B1 0x01
  29. #define SC1200_REV_B3 0x02
  30. #define SC1200_REV_C1 0x03
  31. #define SC1200_REV_D1 0x04
  32. /**
  33. * sc1200_clock - PCI clock
  34. *
  35. * Return the PCI bus clocking for the SC1200 chipset configuration
  36. * in use. We return 0 for 33MHz 1 for 48MHz and 2 for 66Mhz
  37. */
  38. static int sc1200_clock(void)
  39. {
  40. /* Magic registers that give us the chipset data */
  41. u8 chip_id = inb(0x903C);
  42. u8 silicon_rev = inb(0x903D);
  43. u16 pci_clock;
  44. if (chip_id == 0x04 && silicon_rev < SC1200_REV_B1)
  45. return 0; /* 33 MHz mode */
  46. /* Clock generator configuration 0x901E its 8/9 are the PCI clocking
  47. 0/3 is 33Mhz 1 is 48 2 is 66 */
  48. pci_clock = inw(0x901E);
  49. pci_clock >>= 8;
  50. pci_clock &= 0x03;
  51. if (pci_clock == 3)
  52. pci_clock = 0;
  53. return pci_clock;
  54. }
  55. /**
  56. * sc1200_set_piomode - PIO setup
  57. * @ap: ATA interface
  58. * @adev: device on the interface
  59. *
  60. * Set our PIO requirements. This is fairly simple on the SC1200
  61. */
  62. static void sc1200_set_piomode(struct ata_port *ap, struct ata_device *adev)
  63. {
  64. static const u32 pio_timings[4][5] = {
  65. /* format0, 33Mhz */
  66. { 0x00009172, 0x00012171, 0x00020080, 0x00032010, 0x00040010 },
  67. /* format1, 33Mhz */
  68. { 0xd1329172, 0x71212171, 0x30200080, 0x20102010, 0x00100010 },
  69. /* format1, 48Mhz */
  70. { 0xfaa3f4f3, 0xc23232b2, 0x513101c1, 0x31213121, 0x10211021 },
  71. /* format1, 66Mhz */
  72. { 0xfff4fff4, 0xf35353d3, 0x814102f1, 0x42314231, 0x11311131 }
  73. };
  74. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  75. u32 format;
  76. unsigned int reg = 0x40 + 0x10 * ap->port_no;
  77. int mode = adev->pio_mode - XFER_PIO_0;
  78. pci_read_config_dword(pdev, reg + 4, &format);
  79. format >>= 31;
  80. format += sc1200_clock();
  81. pci_write_config_dword(pdev, reg + 8 * adev->devno,
  82. pio_timings[format][mode]);
  83. }
  84. /**
  85. * sc1200_set_dmamode - DMA timing setup
  86. * @ap: ATA interface
  87. * @adev: Device being configured
  88. *
  89. * We cannot mix MWDMA and UDMA without reloading timings each switch
  90. * master to slave.
  91. */
  92. static void sc1200_set_dmamode(struct ata_port *ap, struct ata_device *adev)
  93. {
  94. static const u32 udma_timing[3][3] = {
  95. { 0x00921250, 0x00911140, 0x00911030 },
  96. { 0x00932470, 0x00922260, 0x00922140 },
  97. { 0x009436A1, 0x00933481, 0x00923261 }
  98. };
  99. static const u32 mwdma_timing[3][3] = {
  100. { 0x00077771, 0x00012121, 0x00002020 },
  101. { 0x000BBBB2, 0x00024241, 0x00013131 },
  102. { 0x000FFFF3, 0x00035352, 0x00015151 }
  103. };
  104. int clock = sc1200_clock();
  105. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  106. unsigned int reg = 0x40 + 0x10 * ap->port_no;
  107. int mode = adev->dma_mode;
  108. u32 format;
  109. if (mode >= XFER_UDMA_0)
  110. format = udma_timing[clock][mode - XFER_UDMA_0];
  111. else
  112. format = mwdma_timing[clock][mode - XFER_MW_DMA_0];
  113. if (adev->devno == 0) {
  114. u32 timings;
  115. pci_read_config_dword(pdev, reg + 4, &timings);
  116. timings &= 0x80000000UL;
  117. timings |= format;
  118. pci_write_config_dword(pdev, reg + 4, timings);
  119. } else
  120. pci_write_config_dword(pdev, reg + 12, format);
  121. }
  122. /**
  123. * sc1200_qc_issue - command issue
  124. * @qc: command pending
  125. *
  126. * Called when the libata layer is about to issue a command. We wrap
  127. * this interface so that we can load the correct ATA timings if
  128. * necessary. Specifically we have a problem that there is only
  129. * one MWDMA/UDMA bit.
  130. */
  131. static unsigned int sc1200_qc_issue(struct ata_queued_cmd *qc)
  132. {
  133. struct ata_port *ap = qc->ap;
  134. struct ata_device *adev = qc->dev;
  135. struct ata_device *prev = ap->private_data;
  136. /* See if the DMA settings could be wrong */
  137. if (ata_dma_enabled(adev) && adev != prev && prev != NULL) {
  138. /* Maybe, but do the channels match MWDMA/UDMA ? */
  139. if ((ata_using_udma(adev) && !ata_using_udma(prev)) ||
  140. (ata_using_udma(prev) && !ata_using_udma(adev)))
  141. /* Switch the mode bits */
  142. sc1200_set_dmamode(ap, adev);
  143. }
  144. return ata_bmdma_qc_issue(qc);
  145. }
  146. /**
  147. * sc1200_qc_defer - implement serialization
  148. * @qc: command
  149. *
  150. * Serialize command issue on this controller.
  151. */
  152. static int sc1200_qc_defer(struct ata_queued_cmd *qc)
  153. {
  154. struct ata_host *host = qc->ap->host;
  155. struct ata_port *alt = host->ports[1 ^ qc->ap->port_no];
  156. int rc;
  157. /* First apply the usual rules */
  158. rc = ata_std_qc_defer(qc);
  159. if (rc != 0)
  160. return rc;
  161. /* Now apply serialization rules. Only allow a command if the
  162. other channel state machine is idle */
  163. if (alt && alt->qc_active)
  164. return ATA_DEFER_PORT;
  165. return 0;
  166. }
  167. static struct scsi_host_template sc1200_sht = {
  168. ATA_BMDMA_SHT(DRV_NAME),
  169. .sg_tablesize = LIBATA_DUMB_MAX_PRD,
  170. };
  171. static struct ata_port_operations sc1200_port_ops = {
  172. .inherits = &ata_bmdma_port_ops,
  173. .qc_prep = ata_bmdma_dumb_qc_prep,
  174. .qc_issue = sc1200_qc_issue,
  175. .qc_defer = sc1200_qc_defer,
  176. .cable_detect = ata_cable_40wire,
  177. .set_piomode = sc1200_set_piomode,
  178. .set_dmamode = sc1200_set_dmamode,
  179. };
  180. /**
  181. * sc1200_init_one - Initialise an SC1200
  182. * @dev: PCI device
  183. * @id: Entry in match table
  184. *
  185. * Just throw the needed data at the libata helper and it does all
  186. * our work.
  187. */
  188. static int sc1200_init_one(struct pci_dev *dev, const struct pci_device_id *id)
  189. {
  190. static const struct ata_port_info info = {
  191. .flags = ATA_FLAG_SLAVE_POSS,
  192. .pio_mask = ATA_PIO4,
  193. .mwdma_mask = ATA_MWDMA2,
  194. .udma_mask = ATA_UDMA2,
  195. .port_ops = &sc1200_port_ops
  196. };
  197. const struct ata_port_info *ppi[] = { &info, NULL };
  198. return ata_pci_bmdma_init_one(dev, ppi, &sc1200_sht, NULL, 0);
  199. }
  200. static const struct pci_device_id sc1200[] = {
  201. { PCI_VDEVICE(NS, PCI_DEVICE_ID_NS_SCx200_IDE), },
  202. { },
  203. };
  204. static struct pci_driver sc1200_pci_driver = {
  205. .name = DRV_NAME,
  206. .id_table = sc1200,
  207. .probe = sc1200_init_one,
  208. .remove = ata_pci_remove_one,
  209. #ifdef CONFIG_PM_SLEEP
  210. .suspend = ata_pci_device_suspend,
  211. .resume = ata_pci_device_resume,
  212. #endif
  213. };
  214. module_pci_driver(sc1200_pci_driver);
  215. MODULE_AUTHOR("Alan Cox, Mark Lord");
  216. MODULE_DESCRIPTION("low-level driver for the NS/AMD SC1200");
  217. MODULE_LICENSE("GPL");
  218. MODULE_DEVICE_TABLE(pci, sc1200);
  219. MODULE_VERSION(DRV_VERSION);