pata_sl82c105.c 9.4 KB

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