pata_artop.c 14 KB

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