pata_artop.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. /*
  2. * pata_artop.c - ARTOP ATA controller driver
  3. *
  4. * (C) 2006 Red Hat <alan@redhat.com>
  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. * 850 serialization once the core supports it
  16. * Investigate no_dsc on 850R
  17. * Clock detect
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/pci.h>
  22. #include <linux/init.h>
  23. #include <linux/blkdev.h>
  24. #include <linux/delay.h>
  25. #include <linux/device.h>
  26. #include <scsi/scsi_host.h>
  27. #include <linux/libata.h>
  28. #include <linux/ata.h>
  29. #define DRV_NAME "pata_artop"
  30. #define DRV_VERSION "0.4.4"
  31. /*
  32. * The ARTOP has 33 Mhz and "over clocked" timing tables. Until we
  33. * get PCI bus speed functionality we leave this as 0. Its a variable
  34. * for when we get the functionality and also for folks wanting to
  35. * test stuff.
  36. */
  37. static int clock = 0;
  38. static int artop6210_pre_reset(struct ata_link *link, unsigned long deadline)
  39. {
  40. struct ata_port *ap = link->ap;
  41. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  42. const struct pci_bits artop_enable_bits[] = {
  43. { 0x4AU, 1U, 0x02UL, 0x02UL }, /* port 0 */
  44. { 0x4AU, 1U, 0x04UL, 0x04UL }, /* port 1 */
  45. };
  46. if (!pci_test_config_bits(pdev, &artop_enable_bits[ap->port_no]))
  47. return -ENOENT;
  48. return ata_std_prereset(link, deadline);
  49. }
  50. /**
  51. * artop6210_error_handler - Probe specified port on PATA host controller
  52. * @ap: Port to probe
  53. *
  54. * LOCKING:
  55. * None (inherited from caller).
  56. */
  57. static void artop6210_error_handler(struct ata_port *ap)
  58. {
  59. ata_bmdma_drive_eh(ap, artop6210_pre_reset,
  60. ata_std_softreset, NULL,
  61. ata_std_postreset);
  62. }
  63. /**
  64. * artop6260_pre_reset - check for 40/80 pin
  65. * @link: link
  66. * @deadline: deadline jiffies for the operation
  67. *
  68. * The ARTOP hardware reports the cable detect bits in register 0x49.
  69. * Nothing complicated needed here.
  70. */
  71. static int artop6260_pre_reset(struct ata_link *link, unsigned long deadline)
  72. {
  73. static const struct pci_bits artop_enable_bits[] = {
  74. { 0x4AU, 1U, 0x02UL, 0x02UL }, /* port 0 */
  75. { 0x4AU, 1U, 0x04UL, 0x04UL }, /* port 1 */
  76. };
  77. struct ata_port *ap = link->ap;
  78. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  79. /* Odd numbered device ids are the units with enable bits (the -R cards) */
  80. if (pdev->device % 1 && !pci_test_config_bits(pdev, &artop_enable_bits[ap->port_no]))
  81. return -ENOENT;
  82. return ata_std_prereset(link, deadline);
  83. }
  84. /**
  85. * artop6260_cable_detect - identify cable type
  86. * @ap: Port
  87. *
  88. * Identify the cable type for the ARTOP interface in question
  89. */
  90. static int artop6260_cable_detect(struct ata_port *ap)
  91. {
  92. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  93. u8 tmp;
  94. pci_read_config_byte(pdev, 0x49, &tmp);
  95. if (tmp & (1 << ap->port_no))
  96. return ATA_CBL_PATA40;
  97. return ATA_CBL_PATA80;
  98. }
  99. /**
  100. * artop6260_error_handler - Probe specified port on PATA host controller
  101. * @ap: Port to probe
  102. *
  103. * LOCKING:
  104. * None (inherited from caller).
  105. */
  106. static void artop6260_error_handler(struct ata_port *ap)
  107. {
  108. ata_bmdma_drive_eh(ap, artop6260_pre_reset,
  109. ata_std_softreset, NULL,
  110. ata_std_postreset);
  111. }
  112. /**
  113. * artop6210_load_piomode - Load a set of PATA PIO timings
  114. * @ap: Port whose timings we are configuring
  115. * @adev: Device
  116. * @pio: PIO mode
  117. *
  118. * Set PIO mode for device, in host controller PCI config space. This
  119. * is used both to set PIO timings in PIO mode and also to set the
  120. * matching PIO clocking for UDMA, as well as the MWDMA timings.
  121. *
  122. * LOCKING:
  123. * None (inherited from caller).
  124. */
  125. static void artop6210_load_piomode(struct ata_port *ap, struct ata_device *adev, unsigned int pio)
  126. {
  127. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  128. int dn = adev->devno + 2 * ap->port_no;
  129. const u16 timing[2][5] = {
  130. { 0x0000, 0x000A, 0x0008, 0x0303, 0x0301 },
  131. { 0x0700, 0x070A, 0x0708, 0x0403, 0x0401 }
  132. };
  133. /* Load the PIO timing active/recovery bits */
  134. pci_write_config_word(pdev, 0x40 + 2 * dn, timing[clock][pio]);
  135. }
  136. /**
  137. * artop6210_set_piomode - Initialize host controller PATA PIO timings
  138. * @ap: Port whose timings we are configuring
  139. * @adev: Device we are configuring
  140. *
  141. * Set PIO mode for device, in host controller PCI config space. For
  142. * ARTOP we must also clear the UDMA bits if we are not doing UDMA. In
  143. * the event UDMA is used the later call to set_dmamode will set the
  144. * bits as required.
  145. *
  146. * LOCKING:
  147. * None (inherited from caller).
  148. */
  149. static void artop6210_set_piomode(struct ata_port *ap, struct ata_device *adev)
  150. {
  151. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  152. int dn = adev->devno + 2 * ap->port_no;
  153. u8 ultra;
  154. artop6210_load_piomode(ap, adev, adev->pio_mode - XFER_PIO_0);
  155. /* Clear the UDMA mode bits (set_dmamode will redo this if needed) */
  156. pci_read_config_byte(pdev, 0x54, &ultra);
  157. ultra &= ~(3 << (2 * dn));
  158. pci_write_config_byte(pdev, 0x54, ultra);
  159. }
  160. /**
  161. * artop6260_load_piomode - Initialize host controller PATA PIO timings
  162. * @ap: Port whose timings we are configuring
  163. * @adev: Device we are configuring
  164. * @pio: PIO mode
  165. *
  166. * Set PIO mode for device, in host controller PCI config space. The
  167. * ARTOP6260 and relatives store the timing data differently.
  168. *
  169. * LOCKING:
  170. * None (inherited from caller).
  171. */
  172. static void artop6260_load_piomode (struct ata_port *ap, struct ata_device *adev, unsigned int pio)
  173. {
  174. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  175. int dn = adev->devno + 2 * ap->port_no;
  176. const u8 timing[2][5] = {
  177. { 0x00, 0x0A, 0x08, 0x33, 0x31 },
  178. { 0x70, 0x7A, 0x78, 0x43, 0x41 }
  179. };
  180. /* Load the PIO timing active/recovery bits */
  181. pci_write_config_byte(pdev, 0x40 + dn, timing[clock][pio]);
  182. }
  183. /**
  184. * artop6260_set_piomode - Initialize host controller PATA PIO timings
  185. * @ap: Port whose timings we are configuring
  186. * @adev: Device we are configuring
  187. *
  188. * Set PIO mode for device, in host controller PCI config space. For
  189. * ARTOP we must also clear the UDMA bits if we are not doing UDMA. In
  190. * the event UDMA is used the later call to set_dmamode will set the
  191. * bits as required.
  192. *
  193. * LOCKING:
  194. * None (inherited from caller).
  195. */
  196. static void artop6260_set_piomode(struct ata_port *ap, struct ata_device *adev)
  197. {
  198. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  199. u8 ultra;
  200. artop6260_load_piomode(ap, adev, adev->pio_mode - XFER_PIO_0);
  201. /* Clear the UDMA mode bits (set_dmamode will redo this if needed) */
  202. pci_read_config_byte(pdev, 0x44 + ap->port_no, &ultra);
  203. ultra &= ~(7 << (4 * adev->devno)); /* One nibble per drive */
  204. pci_write_config_byte(pdev, 0x44 + ap->port_no, ultra);
  205. }
  206. /**
  207. * artop6210_set_dmamode - Initialize host controller PATA PIO timings
  208. * @ap: Port whose timings we are configuring
  209. * @adev: Device whose timings we are configuring
  210. *
  211. * Set DMA mode for device, in host controller PCI config space.
  212. *
  213. * LOCKING:
  214. * None (inherited from caller).
  215. */
  216. static void artop6210_set_dmamode (struct ata_port *ap, struct ata_device *adev)
  217. {
  218. unsigned int pio;
  219. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  220. int dn = adev->devno + 2 * ap->port_no;
  221. u8 ultra;
  222. if (adev->dma_mode == XFER_MW_DMA_0)
  223. pio = 1;
  224. else
  225. pio = 4;
  226. /* Load the PIO timing active/recovery bits */
  227. artop6210_load_piomode(ap, adev, pio);
  228. pci_read_config_byte(pdev, 0x54, &ultra);
  229. ultra &= ~(3 << (2 * dn));
  230. /* Add ultra DMA bits if in UDMA mode */
  231. if (adev->dma_mode >= XFER_UDMA_0) {
  232. u8 mode = (adev->dma_mode - XFER_UDMA_0) + 1 - clock;
  233. if (mode == 0)
  234. mode = 1;
  235. ultra |= (mode << (2 * dn));
  236. }
  237. pci_write_config_byte(pdev, 0x54, ultra);
  238. }
  239. /**
  240. * artop6260_set_dmamode - Initialize host controller PATA PIO timings
  241. * @ap: Port whose timings we are configuring
  242. * @adev: Device we are configuring
  243. *
  244. * Set DMA mode for device, in host controller PCI config space. The
  245. * ARTOP6260 and relatives store the timing data differently.
  246. *
  247. * LOCKING:
  248. * None (inherited from caller).
  249. */
  250. static void artop6260_set_dmamode (struct ata_port *ap, struct ata_device *adev)
  251. {
  252. unsigned int pio = adev->pio_mode - XFER_PIO_0;
  253. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  254. u8 ultra;
  255. if (adev->dma_mode == XFER_MW_DMA_0)
  256. pio = 1;
  257. else
  258. pio = 4;
  259. /* Load the PIO timing active/recovery bits */
  260. artop6260_load_piomode(ap, adev, pio);
  261. /* Add ultra DMA bits if in UDMA mode */
  262. pci_read_config_byte(pdev, 0x44 + ap->port_no, &ultra);
  263. ultra &= ~(7 << (4 * adev->devno)); /* One nibble per drive */
  264. if (adev->dma_mode >= XFER_UDMA_0) {
  265. u8 mode = adev->dma_mode - XFER_UDMA_0 + 1 - clock;
  266. if (mode == 0)
  267. mode = 1;
  268. ultra |= (mode << (4 * adev->devno));
  269. }
  270. pci_write_config_byte(pdev, 0x44 + ap->port_no, ultra);
  271. }
  272. static struct scsi_host_template artop_sht = {
  273. .module = THIS_MODULE,
  274. .name = DRV_NAME,
  275. .ioctl = ata_scsi_ioctl,
  276. .queuecommand = ata_scsi_queuecmd,
  277. .can_queue = ATA_DEF_QUEUE,
  278. .this_id = ATA_SHT_THIS_ID,
  279. .sg_tablesize = LIBATA_MAX_PRD,
  280. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  281. .emulated = ATA_SHT_EMULATED,
  282. .use_clustering = ATA_SHT_USE_CLUSTERING,
  283. .proc_name = DRV_NAME,
  284. .dma_boundary = ATA_DMA_BOUNDARY,
  285. .slave_configure = ata_scsi_slave_config,
  286. .slave_destroy = ata_scsi_slave_destroy,
  287. .bios_param = ata_std_bios_param,
  288. };
  289. static const struct ata_port_operations artop6210_ops = {
  290. .set_piomode = artop6210_set_piomode,
  291. .set_dmamode = artop6210_set_dmamode,
  292. .mode_filter = ata_pci_default_filter,
  293. .tf_load = ata_tf_load,
  294. .tf_read = ata_tf_read,
  295. .check_status = ata_check_status,
  296. .exec_command = ata_exec_command,
  297. .dev_select = ata_std_dev_select,
  298. .freeze = ata_bmdma_freeze,
  299. .thaw = ata_bmdma_thaw,
  300. .error_handler = artop6210_error_handler,
  301. .post_internal_cmd = ata_bmdma_post_internal_cmd,
  302. .cable_detect = ata_cable_40wire,
  303. .bmdma_setup = ata_bmdma_setup,
  304. .bmdma_start = ata_bmdma_start,
  305. .bmdma_stop = ata_bmdma_stop,
  306. .bmdma_status = ata_bmdma_status,
  307. .qc_prep = ata_qc_prep,
  308. .qc_issue = ata_qc_issue_prot,
  309. .data_xfer = ata_data_xfer,
  310. .irq_handler = ata_interrupt,
  311. .irq_clear = ata_bmdma_irq_clear,
  312. .irq_on = ata_irq_on,
  313. .port_start = ata_sff_port_start,
  314. };
  315. static const struct ata_port_operations artop6260_ops = {
  316. .set_piomode = artop6260_set_piomode,
  317. .set_dmamode = artop6260_set_dmamode,
  318. .tf_load = ata_tf_load,
  319. .tf_read = ata_tf_read,
  320. .check_status = ata_check_status,
  321. .exec_command = ata_exec_command,
  322. .dev_select = ata_std_dev_select,
  323. .freeze = ata_bmdma_freeze,
  324. .thaw = ata_bmdma_thaw,
  325. .error_handler = artop6260_error_handler,
  326. .post_internal_cmd = ata_bmdma_post_internal_cmd,
  327. .cable_detect = artop6260_cable_detect,
  328. .bmdma_setup = ata_bmdma_setup,
  329. .bmdma_start = ata_bmdma_start,
  330. .bmdma_stop = ata_bmdma_stop,
  331. .bmdma_status = ata_bmdma_status,
  332. .qc_prep = ata_qc_prep,
  333. .qc_issue = ata_qc_issue_prot,
  334. .data_xfer = ata_data_xfer,
  335. .irq_handler = ata_interrupt,
  336. .irq_clear = ata_bmdma_irq_clear,
  337. .irq_on = ata_irq_on,
  338. .port_start = ata_sff_port_start,
  339. };
  340. /**
  341. * artop_init_one - Register ARTOP ATA PCI device with kernel services
  342. * @pdev: PCI device to register
  343. * @ent: Entry in artop_pci_tbl matching with @pdev
  344. *
  345. * Called from kernel PCI layer.
  346. *
  347. * LOCKING:
  348. * Inherited from PCI layer (may sleep).
  349. *
  350. * RETURNS:
  351. * Zero on success, or -ERRNO value.
  352. */
  353. static int artop_init_one (struct pci_dev *pdev, const struct pci_device_id *id)
  354. {
  355. static int printed_version;
  356. static const struct ata_port_info info_6210 = {
  357. .sht = &artop_sht,
  358. .flags = ATA_FLAG_SLAVE_POSS,
  359. .pio_mask = 0x1f, /* pio0-4 */
  360. .mwdma_mask = 0x07, /* mwdma0-2 */
  361. .udma_mask = ATA_UDMA2,
  362. .port_ops = &artop6210_ops,
  363. };
  364. static const struct ata_port_info info_626x = {
  365. .sht = &artop_sht,
  366. .flags = ATA_FLAG_SLAVE_POSS,
  367. .pio_mask = 0x1f, /* pio0-4 */
  368. .mwdma_mask = 0x07, /* mwdma0-2 */
  369. .udma_mask = ATA_UDMA4,
  370. .port_ops = &artop6260_ops,
  371. };
  372. static const struct ata_port_info info_628x = {
  373. .sht = &artop_sht,
  374. .flags = ATA_FLAG_SLAVE_POSS,
  375. .pio_mask = 0x1f, /* pio0-4 */
  376. .mwdma_mask = 0x07, /* mwdma0-2 */
  377. .udma_mask = ATA_UDMA5,
  378. .port_ops = &artop6260_ops,
  379. };
  380. static const struct ata_port_info info_628x_fast = {
  381. .sht = &artop_sht,
  382. .flags = ATA_FLAG_SLAVE_POSS,
  383. .pio_mask = 0x1f, /* pio0-4 */
  384. .mwdma_mask = 0x07, /* mwdma0-2 */
  385. .udma_mask = ATA_UDMA6,
  386. .port_ops = &artop6260_ops,
  387. };
  388. const struct ata_port_info *ppi[] = { NULL, NULL };
  389. if (!printed_version++)
  390. dev_printk(KERN_DEBUG, &pdev->dev,
  391. "version " DRV_VERSION "\n");
  392. if (id->driver_data == 0) { /* 6210 variant */
  393. ppi[0] = &info_6210;
  394. ppi[1] = &ata_dummy_port_info;
  395. /* BIOS may have left us in UDMA, clear it before libata probe */
  396. pci_write_config_byte(pdev, 0x54, 0);
  397. /* For the moment (also lacks dsc) */
  398. printk(KERN_WARNING "ARTOP 6210 requires serialize functionality not yet supported by libata.\n");
  399. printk(KERN_WARNING "Secondary ATA ports will not be activated.\n");
  400. }
  401. else if (id->driver_data == 1) /* 6260 */
  402. ppi[0] = &info_626x;
  403. else if (id->driver_data == 2) { /* 6280 or 6280 + fast */
  404. unsigned long io = pci_resource_start(pdev, 4);
  405. u8 reg;
  406. ppi[0] = &info_628x;
  407. if (inb(io) & 0x10)
  408. ppi[0] = &info_628x_fast;
  409. /* Mac systems come up with some registers not set as we
  410. will need them */
  411. /* Clear reset & test bits */
  412. pci_read_config_byte(pdev, 0x49, &reg);
  413. pci_write_config_byte(pdev, 0x49, reg & ~ 0x30);
  414. /* PCI latency must be > 0x80 for burst mode, tweak it
  415. * if required.
  416. */
  417. pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &reg);
  418. if (reg <= 0x80)
  419. pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0x90);
  420. /* Enable IRQ output and burst mode */
  421. pci_read_config_byte(pdev, 0x4a, &reg);
  422. pci_write_config_byte(pdev, 0x4a, (reg & ~0x01) | 0x80);
  423. }
  424. BUG_ON(ppi[0] == NULL);
  425. return ata_pci_init_one(pdev, ppi);
  426. }
  427. static const struct pci_device_id artop_pci_tbl[] = {
  428. { PCI_VDEVICE(ARTOP, 0x0005), 0 },
  429. { PCI_VDEVICE(ARTOP, 0x0006), 1 },
  430. { PCI_VDEVICE(ARTOP, 0x0007), 1 },
  431. { PCI_VDEVICE(ARTOP, 0x0008), 2 },
  432. { PCI_VDEVICE(ARTOP, 0x0009), 2 },
  433. { } /* terminate list */
  434. };
  435. static struct pci_driver artop_pci_driver = {
  436. .name = DRV_NAME,
  437. .id_table = artop_pci_tbl,
  438. .probe = artop_init_one,
  439. .remove = ata_pci_remove_one,
  440. };
  441. static int __init artop_init(void)
  442. {
  443. return pci_register_driver(&artop_pci_driver);
  444. }
  445. static void __exit artop_exit(void)
  446. {
  447. pci_unregister_driver(&artop_pci_driver);
  448. }
  449. module_init(artop_init);
  450. module_exit(artop_exit);
  451. MODULE_AUTHOR("Alan Cox");
  452. MODULE_DESCRIPTION("SCSI low-level driver for ARTOP PATA");
  453. MODULE_LICENSE("GPL");
  454. MODULE_DEVICE_TABLE(pci, artop_pci_tbl);
  455. MODULE_VERSION(DRV_VERSION);