pata_artop.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*
  2. * pata_artop.c - ARTOP ATA controller driver
  3. *
  4. * (C) 2006 Red Hat
  5. * (C) 2007 Bartlomiej Zolnierkiewicz
  6. *
  7. * Based in part on drivers/ide/pci/aec62xx.c
  8. * Copyright (C) 1999-2002 Andre Hedrick <andre@linux-ide.org>
  9. * 865/865R fixes for Macintosh card version from a patch to the old
  10. * driver by Thibaut VARENE <varenet@parisc-linux.org>
  11. * When setting the PCI latency we must set 0x80 or higher for burst
  12. * performance Alessandro Zummo <alessandro.zummo@towertech.it>
  13. *
  14. * TODO
  15. * Investigate no_dsc on 850R
  16. * Clock detect
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/pci.h>
  21. #include <linux/init.h>
  22. #include <linux/blkdev.h>
  23. #include <linux/delay.h>
  24. #include <linux/device.h>
  25. #include <scsi/scsi_host.h>
  26. #include <linux/libata.h>
  27. #include <linux/ata.h>
  28. #define DRV_NAME "pata_artop"
  29. #define DRV_VERSION "0.4.5"
  30. /*
  31. * The ARTOP has 33 Mhz and "over clocked" timing tables. Until we
  32. * get PCI bus speed functionality we leave this as 0. Its a variable
  33. * for when we get the functionality and also for folks wanting to
  34. * test stuff.
  35. */
  36. static int clock = 0;
  37. static int artop6210_pre_reset(struct ata_link *link, unsigned long deadline)
  38. {
  39. struct ata_port *ap = link->ap;
  40. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  41. const struct pci_bits artop_enable_bits[] = {
  42. { 0x4AU, 1U, 0x02UL, 0x02UL }, /* port 0 */
  43. { 0x4AU, 1U, 0x04UL, 0x04UL }, /* port 1 */
  44. };
  45. if (!pci_test_config_bits(pdev, &artop_enable_bits[ap->port_no]))
  46. return -ENOENT;
  47. return ata_sff_prereset(link, deadline);
  48. }
  49. /**
  50. * artop6260_pre_reset - check for 40/80 pin
  51. * @link: link
  52. * @deadline: deadline jiffies for the operation
  53. *
  54. * The ARTOP hardware reports the cable detect bits in register 0x49.
  55. * Nothing complicated needed here.
  56. */
  57. static int artop6260_pre_reset(struct ata_link *link, unsigned long deadline)
  58. {
  59. static const struct pci_bits artop_enable_bits[] = {
  60. { 0x4AU, 1U, 0x02UL, 0x02UL }, /* port 0 */
  61. { 0x4AU, 1U, 0x04UL, 0x04UL }, /* port 1 */
  62. };
  63. struct ata_port *ap = link->ap;
  64. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  65. /* Odd numbered device ids are the units with enable bits (the -R cards) */
  66. if (pdev->device % 1 && !pci_test_config_bits(pdev, &artop_enable_bits[ap->port_no]))
  67. return -ENOENT;
  68. return ata_sff_prereset(link, deadline);
  69. }
  70. /**
  71. * artop6260_cable_detect - identify cable type
  72. * @ap: Port
  73. *
  74. * Identify the cable type for the ARTOP interface in question
  75. */
  76. static int artop6260_cable_detect(struct ata_port *ap)
  77. {
  78. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  79. u8 tmp;
  80. pci_read_config_byte(pdev, 0x49, &tmp);
  81. if (tmp & (1 << ap->port_no))
  82. return ATA_CBL_PATA40;
  83. return ATA_CBL_PATA80;
  84. }
  85. /**
  86. * artop6210_load_piomode - Load a set of PATA PIO timings
  87. * @ap: Port whose timings we are configuring
  88. * @adev: Device
  89. * @pio: PIO mode
  90. *
  91. * Set PIO mode for device, in host controller PCI config space. This
  92. * is used both to set PIO timings in PIO mode and also to set the
  93. * matching PIO clocking for UDMA, as well as the MWDMA timings.
  94. *
  95. * LOCKING:
  96. * None (inherited from caller).
  97. */
  98. static void artop6210_load_piomode(struct ata_port *ap, struct ata_device *adev, unsigned int pio)
  99. {
  100. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  101. int dn = adev->devno + 2 * ap->port_no;
  102. const u16 timing[2][5] = {
  103. { 0x0000, 0x000A, 0x0008, 0x0303, 0x0301 },
  104. { 0x0700, 0x070A, 0x0708, 0x0403, 0x0401 }
  105. };
  106. /* Load the PIO timing active/recovery bits */
  107. pci_write_config_word(pdev, 0x40 + 2 * dn, timing[clock][pio]);
  108. }
  109. /**
  110. * artop6210_set_piomode - Initialize host controller PATA PIO timings
  111. * @ap: Port whose timings we are configuring
  112. * @adev: Device we are configuring
  113. *
  114. * Set PIO mode for device, in host controller PCI config space. For
  115. * ARTOP we must also clear the UDMA bits if we are not doing UDMA. In
  116. * the event UDMA is used the later call to set_dmamode will set the
  117. * bits as required.
  118. *
  119. * LOCKING:
  120. * None (inherited from caller).
  121. */
  122. static void artop6210_set_piomode(struct ata_port *ap, struct ata_device *adev)
  123. {
  124. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  125. int dn = adev->devno + 2 * ap->port_no;
  126. u8 ultra;
  127. artop6210_load_piomode(ap, adev, adev->pio_mode - XFER_PIO_0);
  128. /* Clear the UDMA mode bits (set_dmamode will redo this if needed) */
  129. pci_read_config_byte(pdev, 0x54, &ultra);
  130. ultra &= ~(3 << (2 * dn));
  131. pci_write_config_byte(pdev, 0x54, ultra);
  132. }
  133. /**
  134. * artop6260_load_piomode - Initialize host controller PATA PIO timings
  135. * @ap: Port whose timings we are configuring
  136. * @adev: Device we are configuring
  137. * @pio: PIO mode
  138. *
  139. * Set PIO mode for device, in host controller PCI config space. The
  140. * ARTOP6260 and relatives store the timing data differently.
  141. *
  142. * LOCKING:
  143. * None (inherited from caller).
  144. */
  145. static void artop6260_load_piomode (struct ata_port *ap, struct ata_device *adev, unsigned int pio)
  146. {
  147. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  148. int dn = adev->devno + 2 * ap->port_no;
  149. const u8 timing[2][5] = {
  150. { 0x00, 0x0A, 0x08, 0x33, 0x31 },
  151. { 0x70, 0x7A, 0x78, 0x43, 0x41 }
  152. };
  153. /* Load the PIO timing active/recovery bits */
  154. pci_write_config_byte(pdev, 0x40 + dn, timing[clock][pio]);
  155. }
  156. /**
  157. * artop6260_set_piomode - Initialize host controller PATA PIO timings
  158. * @ap: Port whose timings we are configuring
  159. * @adev: Device we are configuring
  160. *
  161. * Set PIO mode for device, in host controller PCI config space. For
  162. * ARTOP we must also clear the UDMA bits if we are not doing UDMA. In
  163. * the event UDMA is used the later call to set_dmamode will set the
  164. * bits as required.
  165. *
  166. * LOCKING:
  167. * None (inherited from caller).
  168. */
  169. static void artop6260_set_piomode(struct ata_port *ap, struct ata_device *adev)
  170. {
  171. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  172. u8 ultra;
  173. artop6260_load_piomode(ap, adev, adev->pio_mode - XFER_PIO_0);
  174. /* Clear the UDMA mode bits (set_dmamode will redo this if needed) */
  175. pci_read_config_byte(pdev, 0x44 + ap->port_no, &ultra);
  176. ultra &= ~(7 << (4 * adev->devno)); /* One nibble per drive */
  177. pci_write_config_byte(pdev, 0x44 + ap->port_no, ultra);
  178. }
  179. /**
  180. * artop6210_set_dmamode - Initialize host controller PATA PIO timings
  181. * @ap: Port whose timings we are configuring
  182. * @adev: Device whose timings we are configuring
  183. *
  184. * Set DMA mode for device, in host controller PCI config space.
  185. *
  186. * LOCKING:
  187. * None (inherited from caller).
  188. */
  189. static void artop6210_set_dmamode (struct ata_port *ap, struct ata_device *adev)
  190. {
  191. unsigned int pio;
  192. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  193. int dn = adev->devno + 2 * ap->port_no;
  194. u8 ultra;
  195. if (adev->dma_mode == XFER_MW_DMA_0)
  196. pio = 1;
  197. else
  198. pio = 4;
  199. /* Load the PIO timing active/recovery bits */
  200. artop6210_load_piomode(ap, adev, pio);
  201. pci_read_config_byte(pdev, 0x54, &ultra);
  202. ultra &= ~(3 << (2 * dn));
  203. /* Add ultra DMA bits if in UDMA mode */
  204. if (adev->dma_mode >= XFER_UDMA_0) {
  205. u8 mode = (adev->dma_mode - XFER_UDMA_0) + 1 - clock;
  206. if (mode == 0)
  207. mode = 1;
  208. ultra |= (mode << (2 * dn));
  209. }
  210. pci_write_config_byte(pdev, 0x54, ultra);
  211. }
  212. /**
  213. * artop6260_set_dmamode - Initialize host controller PATA PIO timings
  214. * @ap: Port whose timings we are configuring
  215. * @adev: Device we are configuring
  216. *
  217. * Set DMA mode for device, in host controller PCI config space. The
  218. * ARTOP6260 and relatives store the timing data differently.
  219. *
  220. * LOCKING:
  221. * None (inherited from caller).
  222. */
  223. static void artop6260_set_dmamode (struct ata_port *ap, struct ata_device *adev)
  224. {
  225. unsigned int pio = adev->pio_mode - XFER_PIO_0;
  226. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  227. u8 ultra;
  228. if (adev->dma_mode == XFER_MW_DMA_0)
  229. pio = 1;
  230. else
  231. pio = 4;
  232. /* Load the PIO timing active/recovery bits */
  233. artop6260_load_piomode(ap, adev, pio);
  234. /* Add ultra DMA bits if in UDMA mode */
  235. pci_read_config_byte(pdev, 0x44 + ap->port_no, &ultra);
  236. ultra &= ~(7 << (4 * adev->devno)); /* One nibble per drive */
  237. if (adev->dma_mode >= XFER_UDMA_0) {
  238. u8 mode = adev->dma_mode - XFER_UDMA_0 + 1 - clock;
  239. if (mode == 0)
  240. mode = 1;
  241. ultra |= (mode << (4 * adev->devno));
  242. }
  243. pci_write_config_byte(pdev, 0x44 + ap->port_no, ultra);
  244. }
  245. /**
  246. * artop_6210_qc_defer - implement serialization
  247. * @qc: command
  248. *
  249. * Issue commands per host on this chip.
  250. */
  251. static int artop6210_qc_defer(struct ata_queued_cmd *qc)
  252. {
  253. struct ata_host *host = qc->ap->host;
  254. struct ata_port *alt = host->ports[1 ^ qc->ap->port_no];
  255. int rc;
  256. /* First apply the usual rules */
  257. rc = ata_std_qc_defer(qc);
  258. if (rc != 0)
  259. return rc;
  260. /* Now apply serialization rules. Only allow a command if the
  261. other channel state machine is idle */
  262. if (alt && alt->qc_active)
  263. return ATA_DEFER_PORT;
  264. return 0;
  265. }
  266. static struct scsi_host_template artop_sht = {
  267. ATA_BMDMA_SHT(DRV_NAME),
  268. };
  269. static struct ata_port_operations artop6210_ops = {
  270. .inherits = &ata_bmdma_port_ops,
  271. .cable_detect = ata_cable_40wire,
  272. .set_piomode = artop6210_set_piomode,
  273. .set_dmamode = artop6210_set_dmamode,
  274. .prereset = artop6210_pre_reset,
  275. .qc_defer = artop6210_qc_defer,
  276. };
  277. static struct ata_port_operations artop6260_ops = {
  278. .inherits = &ata_bmdma_port_ops,
  279. .cable_detect = artop6260_cable_detect,
  280. .set_piomode = artop6260_set_piomode,
  281. .set_dmamode = artop6260_set_dmamode,
  282. .prereset = artop6260_pre_reset,
  283. };
  284. /**
  285. * artop_init_one - Register ARTOP ATA PCI device with kernel services
  286. * @pdev: PCI device to register
  287. * @ent: Entry in artop_pci_tbl matching with @pdev
  288. *
  289. * Called from kernel PCI layer.
  290. *
  291. * LOCKING:
  292. * Inherited from PCI layer (may sleep).
  293. *
  294. * RETURNS:
  295. * Zero on success, or -ERRNO value.
  296. */
  297. static int artop_init_one (struct pci_dev *pdev, const struct pci_device_id *id)
  298. {
  299. static int printed_version;
  300. static const struct ata_port_info info_6210 = {
  301. .flags = ATA_FLAG_SLAVE_POSS,
  302. .pio_mask = ATA_PIO4,
  303. .mwdma_mask = ATA_MWDMA2,
  304. .udma_mask = ATA_UDMA2,
  305. .port_ops = &artop6210_ops,
  306. };
  307. static const struct ata_port_info info_626x = {
  308. .flags = ATA_FLAG_SLAVE_POSS,
  309. .pio_mask = ATA_PIO4,
  310. .mwdma_mask = ATA_MWDMA2,
  311. .udma_mask = ATA_UDMA4,
  312. .port_ops = &artop6260_ops,
  313. };
  314. static const struct ata_port_info info_628x = {
  315. .flags = ATA_FLAG_SLAVE_POSS,
  316. .pio_mask = ATA_PIO4,
  317. .mwdma_mask = ATA_MWDMA2,
  318. .udma_mask = ATA_UDMA5,
  319. .port_ops = &artop6260_ops,
  320. };
  321. static const struct ata_port_info info_628x_fast = {
  322. .flags = ATA_FLAG_SLAVE_POSS,
  323. .pio_mask = ATA_PIO4,
  324. .mwdma_mask = ATA_MWDMA2,
  325. .udma_mask = ATA_UDMA6,
  326. .port_ops = &artop6260_ops,
  327. };
  328. const struct ata_port_info *ppi[] = { NULL, NULL };
  329. int rc;
  330. if (!printed_version++)
  331. dev_printk(KERN_DEBUG, &pdev->dev,
  332. "version " DRV_VERSION "\n");
  333. rc = pcim_enable_device(pdev);
  334. if (rc)
  335. return rc;
  336. if (id->driver_data == 0) { /* 6210 variant */
  337. ppi[0] = &info_6210;
  338. /* BIOS may have left us in UDMA, clear it before libata probe */
  339. pci_write_config_byte(pdev, 0x54, 0);
  340. }
  341. else if (id->driver_data == 1) /* 6260 */
  342. ppi[0] = &info_626x;
  343. else if (id->driver_data == 2) { /* 6280 or 6280 + fast */
  344. unsigned long io = pci_resource_start(pdev, 4);
  345. u8 reg;
  346. ppi[0] = &info_628x;
  347. if (inb(io) & 0x10)
  348. ppi[0] = &info_628x_fast;
  349. /* Mac systems come up with some registers not set as we
  350. will need them */
  351. /* Clear reset & test bits */
  352. pci_read_config_byte(pdev, 0x49, &reg);
  353. pci_write_config_byte(pdev, 0x49, reg & ~ 0x30);
  354. /* PCI latency must be > 0x80 for burst mode, tweak it
  355. * if required.
  356. */
  357. pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &reg);
  358. if (reg <= 0x80)
  359. pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0x90);
  360. /* Enable IRQ output and burst mode */
  361. pci_read_config_byte(pdev, 0x4a, &reg);
  362. pci_write_config_byte(pdev, 0x4a, (reg & ~0x01) | 0x80);
  363. }
  364. BUG_ON(ppi[0] == NULL);
  365. return ata_pci_sff_init_one(pdev, ppi, &artop_sht, NULL);
  366. }
  367. static const struct pci_device_id artop_pci_tbl[] = {
  368. { PCI_VDEVICE(ARTOP, 0x0005), 0 },
  369. { PCI_VDEVICE(ARTOP, 0x0006), 1 },
  370. { PCI_VDEVICE(ARTOP, 0x0007), 1 },
  371. { PCI_VDEVICE(ARTOP, 0x0008), 2 },
  372. { PCI_VDEVICE(ARTOP, 0x0009), 2 },
  373. { } /* terminate list */
  374. };
  375. static struct pci_driver artop_pci_driver = {
  376. .name = DRV_NAME,
  377. .id_table = artop_pci_tbl,
  378. .probe = artop_init_one,
  379. .remove = ata_pci_remove_one,
  380. };
  381. static int __init artop_init(void)
  382. {
  383. return pci_register_driver(&artop_pci_driver);
  384. }
  385. static void __exit artop_exit(void)
  386. {
  387. pci_unregister_driver(&artop_pci_driver);
  388. }
  389. module_init(artop_init);
  390. module_exit(artop_exit);
  391. MODULE_AUTHOR("Alan Cox");
  392. MODULE_DESCRIPTION("SCSI low-level driver for ARTOP PATA");
  393. MODULE_LICENSE("GPL");
  394. MODULE_DEVICE_TABLE(pci, artop_pci_tbl);
  395. MODULE_VERSION(DRV_VERSION);