pata_artop.c 15 KB

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