pata_rdc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*
  2. * pata_rdc - Driver for later RDC PATA controllers
  3. *
  4. * This is actually a driver for hardware meeting
  5. * INCITS 370-2004 (1510D): ATA Host Adapter Standards
  6. *
  7. * Based on ata_piix.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2, or (at your option)
  12. * any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; see the file COPYING. If not, write to
  21. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/pci.h>
  26. #include <linux/blkdev.h>
  27. #include <linux/delay.h>
  28. #include <linux/device.h>
  29. #include <linux/gfp.h>
  30. #include <scsi/scsi_host.h>
  31. #include <linux/libata.h>
  32. #include <linux/dmi.h>
  33. #define DRV_NAME "pata_rdc"
  34. #define DRV_VERSION "0.01"
  35. struct rdc_host_priv {
  36. u32 saved_iocfg;
  37. };
  38. /**
  39. * rdc_pata_cable_detect - Probe host controller cable detect info
  40. * @ap: Port for which cable detect info is desired
  41. *
  42. * Read 80c cable indicator from ATA PCI device's PCI config
  43. * register. This register is normally set by firmware (BIOS).
  44. *
  45. * LOCKING:
  46. * None (inherited from caller).
  47. */
  48. static int rdc_pata_cable_detect(struct ata_port *ap)
  49. {
  50. struct rdc_host_priv *hpriv = ap->host->private_data;
  51. u8 mask;
  52. /* check BIOS cable detect results */
  53. mask = 0x30 << (2 * ap->port_no);
  54. if ((hpriv->saved_iocfg & mask) == 0)
  55. return ATA_CBL_PATA40;
  56. return ATA_CBL_PATA80;
  57. }
  58. /**
  59. * rdc_pata_prereset - prereset for PATA host controller
  60. * @link: Target link
  61. * @deadline: deadline jiffies for the operation
  62. *
  63. * LOCKING:
  64. * None (inherited from caller).
  65. */
  66. static int rdc_pata_prereset(struct ata_link *link, unsigned long deadline)
  67. {
  68. struct ata_port *ap = link->ap;
  69. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  70. static const struct pci_bits rdc_enable_bits[] = {
  71. { 0x41U, 1U, 0x80UL, 0x80UL }, /* port 0 */
  72. { 0x43U, 1U, 0x80UL, 0x80UL }, /* port 1 */
  73. };
  74. if (!pci_test_config_bits(pdev, &rdc_enable_bits[ap->port_no]))
  75. return -ENOENT;
  76. return ata_sff_prereset(link, deadline);
  77. }
  78. static DEFINE_SPINLOCK(rdc_lock);
  79. /**
  80. * rdc_set_piomode - Initialize host controller PATA PIO timings
  81. * @ap: Port whose timings we are configuring
  82. * @adev: um
  83. *
  84. * Set PIO mode for device, in host controller PCI config space.
  85. *
  86. * LOCKING:
  87. * None (inherited from caller).
  88. */
  89. static void rdc_set_piomode(struct ata_port *ap, struct ata_device *adev)
  90. {
  91. unsigned int pio = adev->pio_mode - XFER_PIO_0;
  92. struct pci_dev *dev = to_pci_dev(ap->host->dev);
  93. unsigned long flags;
  94. unsigned int is_slave = (adev->devno != 0);
  95. unsigned int master_port= ap->port_no ? 0x42 : 0x40;
  96. unsigned int slave_port = 0x44;
  97. u16 master_data;
  98. u8 slave_data;
  99. u8 udma_enable;
  100. int control = 0;
  101. static const /* ISP RTC */
  102. u8 timings[][2] = { { 0, 0 },
  103. { 0, 0 },
  104. { 1, 0 },
  105. { 2, 1 },
  106. { 2, 3 }, };
  107. if (pio >= 2)
  108. control |= 1; /* TIME1 enable */
  109. if (ata_pio_need_iordy(adev))
  110. control |= 2; /* IE enable */
  111. if (adev->class == ATA_DEV_ATA)
  112. control |= 4; /* PPE enable */
  113. spin_lock_irqsave(&rdc_lock, flags);
  114. /* PIO configuration clears DTE unconditionally. It will be
  115. * programmed in set_dmamode which is guaranteed to be called
  116. * after set_piomode if any DMA mode is available.
  117. */
  118. pci_read_config_word(dev, master_port, &master_data);
  119. if (is_slave) {
  120. /* clear TIME1|IE1|PPE1|DTE1 */
  121. master_data &= 0xff0f;
  122. /* Enable SITRE (separate slave timing register) */
  123. master_data |= 0x4000;
  124. /* enable PPE1, IE1 and TIME1 as needed */
  125. master_data |= (control << 4);
  126. pci_read_config_byte(dev, slave_port, &slave_data);
  127. slave_data &= (ap->port_no ? 0x0f : 0xf0);
  128. /* Load the timing nibble for this slave */
  129. slave_data |= ((timings[pio][0] << 2) | timings[pio][1])
  130. << (ap->port_no ? 4 : 0);
  131. } else {
  132. /* clear ISP|RCT|TIME0|IE0|PPE0|DTE0 */
  133. master_data &= 0xccf0;
  134. /* Enable PPE, IE and TIME as appropriate */
  135. master_data |= control;
  136. /* load ISP and RCT */
  137. master_data |=
  138. (timings[pio][0] << 12) |
  139. (timings[pio][1] << 8);
  140. }
  141. pci_write_config_word(dev, master_port, master_data);
  142. if (is_slave)
  143. pci_write_config_byte(dev, slave_port, slave_data);
  144. /* Ensure the UDMA bit is off - it will be turned back on if
  145. UDMA is selected */
  146. pci_read_config_byte(dev, 0x48, &udma_enable);
  147. udma_enable &= ~(1 << (2 * ap->port_no + adev->devno));
  148. pci_write_config_byte(dev, 0x48, udma_enable);
  149. spin_unlock_irqrestore(&rdc_lock, flags);
  150. }
  151. /**
  152. * rdc_set_dmamode - Initialize host controller PATA PIO timings
  153. * @ap: Port whose timings we are configuring
  154. * @adev: Drive in question
  155. *
  156. * Set UDMA mode for device, in host controller PCI config space.
  157. *
  158. * LOCKING:
  159. * None (inherited from caller).
  160. */
  161. static void rdc_set_dmamode(struct ata_port *ap, struct ata_device *adev)
  162. {
  163. struct pci_dev *dev = to_pci_dev(ap->host->dev);
  164. unsigned long flags;
  165. u8 master_port = ap->port_no ? 0x42 : 0x40;
  166. u16 master_data;
  167. u8 speed = adev->dma_mode;
  168. int devid = adev->devno + 2 * ap->port_no;
  169. u8 udma_enable = 0;
  170. static const /* ISP RTC */
  171. u8 timings[][2] = { { 0, 0 },
  172. { 0, 0 },
  173. { 1, 0 },
  174. { 2, 1 },
  175. { 2, 3 }, };
  176. spin_lock_irqsave(&rdc_lock, flags);
  177. pci_read_config_word(dev, master_port, &master_data);
  178. pci_read_config_byte(dev, 0x48, &udma_enable);
  179. if (speed >= XFER_UDMA_0) {
  180. unsigned int udma = adev->dma_mode - XFER_UDMA_0;
  181. u16 udma_timing;
  182. u16 ideconf;
  183. int u_clock, u_speed;
  184. /*
  185. * UDMA is handled by a combination of clock switching and
  186. * selection of dividers
  187. *
  188. * Handy rule: Odd modes are UDMATIMx 01, even are 02
  189. * except UDMA0 which is 00
  190. */
  191. u_speed = min(2 - (udma & 1), udma);
  192. if (udma == 5)
  193. u_clock = 0x1000; /* 100Mhz */
  194. else if (udma > 2)
  195. u_clock = 1; /* 66Mhz */
  196. else
  197. u_clock = 0; /* 33Mhz */
  198. udma_enable |= (1 << devid);
  199. /* Load the CT/RP selection */
  200. pci_read_config_word(dev, 0x4A, &udma_timing);
  201. udma_timing &= ~(3 << (4 * devid));
  202. udma_timing |= u_speed << (4 * devid);
  203. pci_write_config_word(dev, 0x4A, udma_timing);
  204. /* Select a 33/66/100Mhz clock */
  205. pci_read_config_word(dev, 0x54, &ideconf);
  206. ideconf &= ~(0x1001 << devid);
  207. ideconf |= u_clock << devid;
  208. pci_write_config_word(dev, 0x54, ideconf);
  209. } else {
  210. /*
  211. * MWDMA is driven by the PIO timings. We must also enable
  212. * IORDY unconditionally along with TIME1. PPE has already
  213. * been set when the PIO timing was set.
  214. */
  215. unsigned int mwdma = adev->dma_mode - XFER_MW_DMA_0;
  216. unsigned int control;
  217. u8 slave_data;
  218. const unsigned int needed_pio[3] = {
  219. XFER_PIO_0, XFER_PIO_3, XFER_PIO_4
  220. };
  221. int pio = needed_pio[mwdma] - XFER_PIO_0;
  222. control = 3; /* IORDY|TIME1 */
  223. /* If the drive MWDMA is faster than it can do PIO then
  224. we must force PIO into PIO0 */
  225. if (adev->pio_mode < needed_pio[mwdma])
  226. /* Enable DMA timing only */
  227. control |= 8; /* PIO cycles in PIO0 */
  228. if (adev->devno) { /* Slave */
  229. master_data &= 0xFF4F; /* Mask out IORDY|TIME1|DMAONLY */
  230. master_data |= control << 4;
  231. pci_read_config_byte(dev, 0x44, &slave_data);
  232. slave_data &= (ap->port_no ? 0x0f : 0xf0);
  233. /* Load the matching timing */
  234. slave_data |= ((timings[pio][0] << 2) | timings[pio][1]) << (ap->port_no ? 4 : 0);
  235. pci_write_config_byte(dev, 0x44, slave_data);
  236. } else { /* Master */
  237. master_data &= 0xCCF4; /* Mask out IORDY|TIME1|DMAONLY
  238. and master timing bits */
  239. master_data |= control;
  240. master_data |=
  241. (timings[pio][0] << 12) |
  242. (timings[pio][1] << 8);
  243. }
  244. udma_enable &= ~(1 << devid);
  245. pci_write_config_word(dev, master_port, master_data);
  246. }
  247. pci_write_config_byte(dev, 0x48, udma_enable);
  248. spin_unlock_irqrestore(&rdc_lock, flags);
  249. }
  250. static struct ata_port_operations rdc_pata_ops = {
  251. .inherits = &ata_bmdma32_port_ops,
  252. .cable_detect = rdc_pata_cable_detect,
  253. .set_piomode = rdc_set_piomode,
  254. .set_dmamode = rdc_set_dmamode,
  255. .prereset = rdc_pata_prereset,
  256. };
  257. static struct ata_port_info rdc_port_info = {
  258. .flags = ATA_FLAG_SLAVE_POSS,
  259. .pio_mask = ATA_PIO4,
  260. .mwdma_mask = ATA_MWDMA12_ONLY,
  261. .udma_mask = ATA_UDMA5,
  262. .port_ops = &rdc_pata_ops,
  263. };
  264. static struct scsi_host_template rdc_sht = {
  265. ATA_BMDMA_SHT(DRV_NAME),
  266. };
  267. /**
  268. * rdc_init_one - Register PIIX ATA PCI device with kernel services
  269. * @pdev: PCI device to register
  270. * @ent: Entry in rdc_pci_tbl matching with @pdev
  271. *
  272. * Called from kernel PCI layer. We probe for combined mode (sigh),
  273. * and then hand over control to libata, for it to do the rest.
  274. *
  275. * LOCKING:
  276. * Inherited from PCI layer (may sleep).
  277. *
  278. * RETURNS:
  279. * Zero on success, or -ERRNO value.
  280. */
  281. static int rdc_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  282. {
  283. struct device *dev = &pdev->dev;
  284. struct ata_port_info port_info[2];
  285. const struct ata_port_info *ppi[] = { &port_info[0], &port_info[1] };
  286. struct ata_host *host;
  287. struct rdc_host_priv *hpriv;
  288. int rc;
  289. ata_print_version_once(&pdev->dev, DRV_VERSION);
  290. port_info[0] = rdc_port_info;
  291. port_info[1] = rdc_port_info;
  292. /* enable device and prepare host */
  293. rc = pcim_enable_device(pdev);
  294. if (rc)
  295. return rc;
  296. hpriv = devm_kzalloc(dev, sizeof(*hpriv), GFP_KERNEL);
  297. if (!hpriv)
  298. return -ENOMEM;
  299. /* Save IOCFG, this will be used for cable detection, quirk
  300. * detection and restoration on detach.
  301. */
  302. pci_read_config_dword(pdev, 0x54, &hpriv->saved_iocfg);
  303. rc = ata_pci_bmdma_prepare_host(pdev, ppi, &host);
  304. if (rc)
  305. return rc;
  306. host->private_data = hpriv;
  307. pci_intx(pdev, 1);
  308. host->flags |= ATA_HOST_PARALLEL_SCAN;
  309. pci_set_master(pdev);
  310. return ata_pci_sff_activate_host(host, ata_bmdma_interrupt, &rdc_sht);
  311. }
  312. static void rdc_remove_one(struct pci_dev *pdev)
  313. {
  314. struct ata_host *host = pci_get_drvdata(pdev);
  315. struct rdc_host_priv *hpriv = host->private_data;
  316. pci_write_config_dword(pdev, 0x54, hpriv->saved_iocfg);
  317. ata_pci_remove_one(pdev);
  318. }
  319. static const struct pci_device_id rdc_pci_tbl[] = {
  320. { PCI_DEVICE(0x17F3, 0x1011), },
  321. { PCI_DEVICE(0x17F3, 0x1012), },
  322. { } /* terminate list */
  323. };
  324. static struct pci_driver rdc_pci_driver = {
  325. .name = DRV_NAME,
  326. .id_table = rdc_pci_tbl,
  327. .probe = rdc_init_one,
  328. .remove = rdc_remove_one,
  329. #ifdef CONFIG_PM_SLEEP
  330. .suspend = ata_pci_device_suspend,
  331. .resume = ata_pci_device_resume,
  332. #endif
  333. };
  334. module_pci_driver(rdc_pci_driver);
  335. MODULE_AUTHOR("Alan Cox (based on ata_piix)");
  336. MODULE_DESCRIPTION("SCSI low-level driver for RDC PATA controllers");
  337. MODULE_LICENSE("GPL");
  338. MODULE_DEVICE_TABLE(pci, rdc_pci_tbl);
  339. MODULE_VERSION(DRV_VERSION);