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