pata_rdc.c 11 KB

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