pata_artop.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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. .slave_destroy = ata_scsi_slave_destroy,
  276. .bios_param = ata_std_bios_param,
  277. };
  278. static const struct ata_port_operations artop6210_ops = {
  279. .port_disable = ata_port_disable,
  280. .set_piomode = artop6210_set_piomode,
  281. .set_dmamode = artop6210_set_dmamode,
  282. .mode_filter = ata_pci_default_filter,
  283. .tf_load = ata_tf_load,
  284. .tf_read = ata_tf_read,
  285. .check_status = ata_check_status,
  286. .exec_command = ata_exec_command,
  287. .dev_select = ata_std_dev_select,
  288. .freeze = ata_bmdma_freeze,
  289. .thaw = ata_bmdma_thaw,
  290. .error_handler = artop6210_error_handler,
  291. .post_internal_cmd = ata_bmdma_post_internal_cmd,
  292. .bmdma_setup = ata_bmdma_setup,
  293. .bmdma_start = ata_bmdma_start,
  294. .bmdma_stop = ata_bmdma_stop,
  295. .bmdma_status = ata_bmdma_status,
  296. .qc_prep = ata_qc_prep,
  297. .qc_issue = ata_qc_issue_prot,
  298. .data_xfer = ata_pio_data_xfer,
  299. .irq_handler = ata_interrupt,
  300. .irq_clear = ata_bmdma_irq_clear,
  301. .port_start = ata_port_start,
  302. .port_stop = ata_port_stop,
  303. .host_stop = ata_host_stop,
  304. };
  305. static const struct ata_port_operations artop6260_ops = {
  306. .port_disable = ata_port_disable,
  307. .set_piomode = artop6260_set_piomode,
  308. .set_dmamode = artop6260_set_dmamode,
  309. .tf_load = ata_tf_load,
  310. .tf_read = ata_tf_read,
  311. .check_status = ata_check_status,
  312. .exec_command = ata_exec_command,
  313. .dev_select = ata_std_dev_select,
  314. .freeze = ata_bmdma_freeze,
  315. .thaw = ata_bmdma_thaw,
  316. .error_handler = artop6260_error_handler,
  317. .post_internal_cmd = ata_bmdma_post_internal_cmd,
  318. .bmdma_setup = ata_bmdma_setup,
  319. .bmdma_start = ata_bmdma_start,
  320. .bmdma_stop = ata_bmdma_stop,
  321. .bmdma_status = ata_bmdma_status,
  322. .qc_prep = ata_qc_prep,
  323. .qc_issue = ata_qc_issue_prot,
  324. .data_xfer = ata_pio_data_xfer,
  325. .irq_handler = ata_interrupt,
  326. .irq_clear = ata_bmdma_irq_clear,
  327. .port_start = ata_port_start,
  328. .port_stop = ata_port_stop,
  329. .host_stop = ata_host_stop,
  330. };
  331. /**
  332. * artop_init_one - Register ARTOP ATA PCI device with kernel services
  333. * @pdev: PCI device to register
  334. * @ent: Entry in artop_pci_tbl matching with @pdev
  335. *
  336. * Called from kernel PCI layer.
  337. *
  338. * LOCKING:
  339. * Inherited from PCI layer (may sleep).
  340. *
  341. * RETURNS:
  342. * Zero on success, or -ERRNO value.
  343. */
  344. static int artop_init_one (struct pci_dev *pdev, const struct pci_device_id *id)
  345. {
  346. static int printed_version;
  347. static struct ata_port_info info_6210 = {
  348. .sht = &artop_sht,
  349. .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST,
  350. .pio_mask = 0x1f, /* pio0-4 */
  351. .mwdma_mask = 0x07, /* mwdma0-2 */
  352. .udma_mask = ATA_UDMA2,
  353. .port_ops = &artop6210_ops,
  354. };
  355. static struct ata_port_info info_626x = {
  356. .sht = &artop_sht,
  357. .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST,
  358. .pio_mask = 0x1f, /* pio0-4 */
  359. .mwdma_mask = 0x07, /* mwdma0-2 */
  360. .udma_mask = ATA_UDMA4,
  361. .port_ops = &artop6260_ops,
  362. };
  363. static struct ata_port_info info_626x_fast = {
  364. .sht = &artop_sht,
  365. .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST,
  366. .pio_mask = 0x1f, /* pio0-4 */
  367. .mwdma_mask = 0x07, /* mwdma0-2 */
  368. .udma_mask = ATA_UDMA5,
  369. .port_ops = &artop6260_ops,
  370. };
  371. struct ata_port_info *port_info[2];
  372. struct ata_port_info *info = NULL;
  373. int ports = 2;
  374. if (!printed_version++)
  375. dev_printk(KERN_DEBUG, &pdev->dev,
  376. "version " DRV_VERSION "\n");
  377. if (id->driver_data == 0) { /* 6210 variant */
  378. info = &info_6210;
  379. /* BIOS may have left us in UDMA, clear it before libata probe */
  380. pci_write_config_byte(pdev, 0x54, 0);
  381. /* For the moment (also lacks dsc) */
  382. printk(KERN_WARNING "ARTOP 6210 requires serialize functionality not yet supported by libata.\n");
  383. printk(KERN_WARNING "Secondary ATA ports will not be activated.\n");
  384. ports = 1;
  385. }
  386. else if (id->driver_data == 1) /* 6260 */
  387. info = &info_626x;
  388. else if (id->driver_data == 2) { /* 6260 or 6260 + fast */
  389. unsigned long io = pci_resource_start(pdev, 4);
  390. u8 reg;
  391. info = &info_626x;
  392. if (inb(io) & 0x10)
  393. info = &info_626x_fast;
  394. /* Mac systems come up with some registers not set as we
  395. will need them */
  396. /* Clear reset & test bits */
  397. pci_read_config_byte(pdev, 0x49, &reg);
  398. pci_write_config_byte(pdev, 0x49, reg & ~ 0x30);
  399. /* PCI latency must be > 0x80 for burst mode, tweak it
  400. * if required.
  401. */
  402. pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &reg);
  403. if (reg <= 0x80)
  404. pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0x90);
  405. /* Enable IRQ output and burst mode */
  406. pci_read_config_byte(pdev, 0x4a, &reg);
  407. pci_write_config_byte(pdev, 0x4a, (reg & ~0x01) | 0x80);
  408. }
  409. BUG_ON(info == NULL);
  410. port_info[0] = port_info[1] = info;
  411. return ata_pci_init_one(pdev, port_info, ports);
  412. }
  413. static const struct pci_device_id artop_pci_tbl[] = {
  414. { PCI_VDEVICE(ARTOP, 0x0005), 0 },
  415. { PCI_VDEVICE(ARTOP, 0x0006), 1 },
  416. { PCI_VDEVICE(ARTOP, 0x0007), 1 },
  417. { PCI_VDEVICE(ARTOP, 0x0008), 2 },
  418. { PCI_VDEVICE(ARTOP, 0x0009), 2 },
  419. { } /* terminate list */
  420. };
  421. static struct pci_driver artop_pci_driver = {
  422. .name = DRV_NAME,
  423. .id_table = artop_pci_tbl,
  424. .probe = artop_init_one,
  425. .remove = ata_pci_remove_one,
  426. };
  427. static int __init artop_init(void)
  428. {
  429. return pci_register_driver(&artop_pci_driver);
  430. }
  431. static void __exit artop_exit(void)
  432. {
  433. pci_unregister_driver(&artop_pci_driver);
  434. }
  435. module_init(artop_init);
  436. module_exit(artop_exit);
  437. MODULE_AUTHOR("Alan Cox");
  438. MODULE_DESCRIPTION("SCSI low-level driver for ARTOP PATA");
  439. MODULE_LICENSE("GPL");
  440. MODULE_DEVICE_TABLE(pci, artop_pci_tbl);
  441. MODULE_VERSION(DRV_VERSION);