pata_rdc.c 11 KB

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