pata_artop.c 12 KB

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