pata_artop.c 14 KB

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