pata_sl82c105.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*
  2. * pata_sl82c105.c - SL82C105 PATA for new ATA layer
  3. * (C) 2005 Red Hat Inc
  4. * Alan Cox <alan@redhat.com>
  5. *
  6. * Based in part on linux/drivers/ide/pci/sl82c105.c
  7. * SL82C105/Winbond 553 IDE driver
  8. *
  9. * and in part on the documentation and errata sheet
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/pci.h>
  14. #include <linux/init.h>
  15. #include <linux/blkdev.h>
  16. #include <linux/delay.h>
  17. #include <scsi/scsi_host.h>
  18. #include <linux/libata.h>
  19. #define DRV_NAME "pata_sl82c105"
  20. #define DRV_VERSION "0.2.2"
  21. enum {
  22. /*
  23. * SL82C105 PCI config register 0x40 bits.
  24. */
  25. CTRL_IDE_IRQB = (1 << 30),
  26. CTRL_IDE_IRQA = (1 << 28),
  27. CTRL_LEGIRQ = (1 << 11),
  28. CTRL_P1F16 = (1 << 5),
  29. CTRL_P1EN = (1 << 4),
  30. CTRL_P0F16 = (1 << 1),
  31. CTRL_P0EN = (1 << 0)
  32. };
  33. /**
  34. * sl82c105_pre_reset - probe begin
  35. * @ap: ATA port
  36. *
  37. * Set up cable type and use generic probe init
  38. */
  39. static int sl82c105_pre_reset(struct ata_port *ap)
  40. {
  41. static const struct pci_bits sl82c105_enable_bits[] = {
  42. { 0x40, 1, 0x01, 0x01 },
  43. { 0x40, 1, 0x10, 0x10 }
  44. };
  45. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  46. if (ap->port_no && !pci_test_config_bits(pdev, &sl82c105_enable_bits[ap->port_no])) {
  47. ata_port_disable(ap);
  48. dev_printk(KERN_INFO, &pdev->dev, "port disabled. ignoring.\n");
  49. return 0;
  50. }
  51. ap->cbl = ATA_CBL_PATA40;
  52. return ata_std_prereset(ap);
  53. }
  54. static void sl82c105_error_handler(struct ata_port *ap)
  55. {
  56. ata_bmdma_drive_eh(ap, sl82c105_pre_reset, ata_std_softreset, NULL, ata_std_postreset);
  57. }
  58. /**
  59. * sl82c105_configure_piomode - set chip PIO timing
  60. * @ap: ATA interface
  61. * @adev: ATA device
  62. * @pio: PIO mode
  63. *
  64. * Called to do the PIO mode setup. Our timing registers are shared
  65. * so a configure_dmamode call will undo any work we do here and vice
  66. * versa
  67. */
  68. static void sl82c105_configure_piomode(struct ata_port *ap, struct ata_device *adev, int pio)
  69. {
  70. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  71. static u16 pio_timing[5] = {
  72. 0x50D, 0x407, 0x304, 0x242, 0x240
  73. };
  74. u16 dummy;
  75. int timing = 0x44 + (8 * ap->port_no) + (4 * adev->devno);
  76. pci_write_config_word(pdev, timing, pio_timing[pio]);
  77. /* Can we lose this oddity of the old driver */
  78. pci_read_config_word(pdev, timing, &dummy);
  79. }
  80. /**
  81. * sl82c105_set_piomode - set initial PIO mode data
  82. * @ap: ATA interface
  83. * @adev: ATA device
  84. *
  85. * Called to do the PIO mode setup. Our timing registers are shared
  86. * but we want to set the PIO timing by default.
  87. */
  88. static void sl82c105_set_piomode(struct ata_port *ap, struct ata_device *adev)
  89. {
  90. sl82c105_configure_piomode(ap, adev, adev->pio_mode - XFER_PIO_0);
  91. }
  92. /**
  93. * sl82c105_configure_dmamode - set DMA mode in chip
  94. * @ap: ATA interface
  95. * @adev: ATA device
  96. *
  97. * Load DMA cycle times into the chip ready for a DMA transfer
  98. * to occur.
  99. */
  100. static void sl82c105_configure_dmamode(struct ata_port *ap, struct ata_device *adev)
  101. {
  102. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  103. static u16 dma_timing[3] = {
  104. 0x707, 0x201, 0x200
  105. };
  106. u16 dummy;
  107. int timing = 0x44 + (8 * ap->port_no) + (4 * adev->devno);
  108. int dma = adev->dma_mode - XFER_MW_DMA_0;
  109. pci_write_config_word(pdev, timing, dma_timing[dma]);
  110. /* Can we lose this oddity of the old driver */
  111. pci_read_config_word(pdev, timing, &dummy);
  112. }
  113. /**
  114. * sl82c105_set_dmamode - set initial DMA mode data
  115. * @ap: ATA interface
  116. * @adev: ATA device
  117. *
  118. * Called to do the DMA mode setup. This replaces the PIO timings
  119. * for the device in question. Set appropriate PIO timings not DMA
  120. * timings at this point.
  121. */
  122. static void sl82c105_set_dmamode(struct ata_port *ap, struct ata_device *adev)
  123. {
  124. switch(adev->dma_mode) {
  125. case XFER_MW_DMA_0:
  126. sl82c105_configure_piomode(ap, adev, 1);
  127. break;
  128. case XFER_MW_DMA_1:
  129. sl82c105_configure_piomode(ap, adev, 3);
  130. break;
  131. case XFER_MW_DMA_2:
  132. sl82c105_configure_piomode(ap, adev, 3);
  133. break;
  134. default:
  135. BUG();
  136. }
  137. }
  138. /**
  139. * sl82c105_reset_engine - Reset the DMA engine
  140. * @ap: ATA interface
  141. *
  142. * The sl82c105 has some serious problems with the DMA engine
  143. * when transfers don't run as expected or ATAPI is used. The
  144. * recommended fix is to reset the engine each use using a chip
  145. * test register.
  146. */
  147. static void sl82c105_reset_engine(struct ata_port *ap)
  148. {
  149. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  150. u16 val;
  151. pci_read_config_word(pdev, 0x7E, &val);
  152. pci_write_config_word(pdev, 0x7E, val | 4);
  153. pci_write_config_word(pdev, 0x7E, val & ~4);
  154. }
  155. /**
  156. * sl82c105_bmdma_start - DMA engine begin
  157. * @qc: ATA command
  158. *
  159. * Reset the DMA engine each use as recommended by the errata
  160. * document.
  161. *
  162. * FIXME: if we switch clock at BMDMA start/end we might get better
  163. * PIO performance on DMA capable devices.
  164. */
  165. static void sl82c105_bmdma_start(struct ata_queued_cmd *qc)
  166. {
  167. struct ata_port *ap = qc->ap;
  168. sl82c105_reset_engine(ap);
  169. /* Set the clocks for DMA */
  170. sl82c105_configure_dmamode(ap, qc->dev);
  171. /* Activate DMA */
  172. ata_bmdma_start(qc);
  173. }
  174. /**
  175. * sl82c105_bmdma_end - DMA engine stop
  176. * @qc: ATA command
  177. *
  178. * Reset the DMA engine each use as recommended by the errata
  179. * document.
  180. *
  181. * This function is also called to turn off DMA when a timeout occurs
  182. * during DMA operation. In both cases we need to reset the engine,
  183. * so no actual eng_timeout handler is required.
  184. *
  185. * We assume bmdma_stop is always called if bmdma_start as called. If
  186. * not then we may need to wrap qc_issue.
  187. */
  188. static void sl82c105_bmdma_stop(struct ata_queued_cmd *qc)
  189. {
  190. struct ata_port *ap = qc->ap;
  191. ata_bmdma_stop(qc);
  192. sl82c105_reset_engine(ap);
  193. /* This will redo the initial setup of the DMA device to matching
  194. PIO timings */
  195. sl82c105_set_dmamode(ap, qc->dev);
  196. }
  197. static struct scsi_host_template sl82c105_sht = {
  198. .module = THIS_MODULE,
  199. .name = DRV_NAME,
  200. .ioctl = ata_scsi_ioctl,
  201. .queuecommand = ata_scsi_queuecmd,
  202. .can_queue = ATA_DEF_QUEUE,
  203. .this_id = ATA_SHT_THIS_ID,
  204. .sg_tablesize = LIBATA_MAX_PRD,
  205. .max_sectors = ATA_MAX_SECTORS,
  206. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  207. .emulated = ATA_SHT_EMULATED,
  208. .use_clustering = ATA_SHT_USE_CLUSTERING,
  209. .proc_name = DRV_NAME,
  210. .dma_boundary = ATA_DMA_BOUNDARY,
  211. .slave_configure = ata_scsi_slave_config,
  212. .bios_param = ata_std_bios_param,
  213. };
  214. static struct ata_port_operations sl82c105_port_ops = {
  215. .port_disable = ata_port_disable,
  216. .set_piomode = sl82c105_set_piomode,
  217. .set_dmamode = sl82c105_set_dmamode,
  218. .mode_filter = ata_pci_default_filter,
  219. .tf_load = ata_tf_load,
  220. .tf_read = ata_tf_read,
  221. .check_status = ata_check_status,
  222. .exec_command = ata_exec_command,
  223. .dev_select = ata_std_dev_select,
  224. .error_handler = sl82c105_error_handler,
  225. .bmdma_setup = ata_bmdma_setup,
  226. .bmdma_start = sl82c105_bmdma_start,
  227. .bmdma_stop = sl82c105_bmdma_stop,
  228. .bmdma_status = ata_bmdma_status,
  229. .qc_prep = ata_qc_prep,
  230. .qc_issue = ata_qc_issue_prot,
  231. .eng_timeout = ata_eng_timeout,
  232. .data_xfer = ata_pio_data_xfer,
  233. .irq_handler = ata_interrupt,
  234. .irq_clear = ata_bmdma_irq_clear,
  235. .port_start = ata_port_start,
  236. .port_stop = ata_port_stop,
  237. .host_stop = ata_host_stop
  238. };
  239. /**
  240. * sl82c105_bridge_revision - find bridge version
  241. * @pdev: PCI device for the ATA function
  242. *
  243. * Locates the PCI bridge associated with the ATA function and
  244. * providing it is a Winbond 553 reports the revision. If it cannot
  245. * find a revision or the right device it returns -1
  246. */
  247. static int sl82c105_bridge_revision(struct pci_dev *pdev)
  248. {
  249. struct pci_dev *bridge;
  250. u8 rev;
  251. /*
  252. * The bridge should be part of the same device, but function 0.
  253. */
  254. bridge = pci_get_slot(pdev->bus,
  255. PCI_DEVFN(PCI_SLOT(pdev->devfn), 0));
  256. if (!bridge)
  257. return -1;
  258. /*
  259. * Make sure it is a Winbond 553 and is an ISA bridge.
  260. */
  261. if (bridge->vendor != PCI_VENDOR_ID_WINBOND ||
  262. bridge->device != PCI_DEVICE_ID_WINBOND_83C553 ||
  263. bridge->class >> 8 != PCI_CLASS_BRIDGE_ISA) {
  264. pci_dev_put(bridge);
  265. return -1;
  266. }
  267. /*
  268. * We need to find function 0's revision, not function 1
  269. */
  270. pci_read_config_byte(bridge, PCI_REVISION_ID, &rev);
  271. pci_dev_put(bridge);
  272. return rev;
  273. }
  274. static int sl82c105_init_one(struct pci_dev *dev, const struct pci_device_id *id)
  275. {
  276. static struct ata_port_info info_dma = {
  277. .sht = &sl82c105_sht,
  278. .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST,
  279. .pio_mask = 0x1f,
  280. .mwdma_mask = 0x07,
  281. .port_ops = &sl82c105_port_ops
  282. };
  283. static struct ata_port_info info_early = {
  284. .sht = &sl82c105_sht,
  285. .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST,
  286. .pio_mask = 0x1f,
  287. .port_ops = &sl82c105_port_ops
  288. };
  289. static struct ata_port_info *port_info[2] = { &info_early, &info_early };
  290. u32 val;
  291. int rev;
  292. rev = sl82c105_bridge_revision(dev);
  293. if (rev == -1)
  294. dev_printk(KERN_WARNING, &dev->dev, "pata_sl82c105: Unable to find bridge, disabling DMA.\n");
  295. else if (rev <= 5)
  296. dev_printk(KERN_WARNING, &dev->dev, "pata_sl82c105: Early bridge revision, no DMA available.\n");
  297. else {
  298. port_info[0] = &info_dma;
  299. port_info[1] = &info_dma;
  300. }
  301. pci_read_config_dword(dev, 0x40, &val);
  302. val |= CTRL_P0EN | CTRL_P0F16 | CTRL_P1F16;
  303. pci_write_config_dword(dev, 0x40, val);
  304. return ata_pci_init_one(dev, port_info, 1); /* For now */
  305. }
  306. static struct pci_device_id sl82c105[] = {
  307. { PCI_DEVICE(PCI_VENDOR_ID_WINBOND, PCI_DEVICE_ID_WINBOND_82C105), },
  308. { 0, },
  309. };
  310. static struct pci_driver sl82c105_pci_driver = {
  311. .name = DRV_NAME,
  312. .id_table = sl82c105,
  313. .probe = sl82c105_init_one,
  314. .remove = ata_pci_remove_one
  315. };
  316. static int __init sl82c105_init(void)
  317. {
  318. return pci_register_driver(&sl82c105_pci_driver);
  319. }
  320. static void __exit sl82c105_exit(void)
  321. {
  322. pci_unregister_driver(&sl82c105_pci_driver);
  323. }
  324. MODULE_AUTHOR("Alan Cox");
  325. MODULE_DESCRIPTION("low-level driver for Sl82c105");
  326. MODULE_LICENSE("GPL");
  327. MODULE_DEVICE_TABLE(pci, sl82c105);
  328. MODULE_VERSION(DRV_VERSION);
  329. module_init(sl82c105_init);
  330. module_exit(sl82c105_exit);