pata_efar.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*
  2. * pata_efar.c - EFAR PIIX clone controller driver
  3. *
  4. * (C) 2005 Red Hat <alan@redhat.com>
  5. *
  6. * Some parts based on ata_piix.c by Jeff Garzik and others.
  7. *
  8. * The EFAR is a PIIX4 clone with UDMA66 support. Unlike the later
  9. * Intel ICH controllers the EFAR widened the UDMA mode register bits
  10. * and doesn't require the funky clock selection.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/pci.h>
  15. #include <linux/init.h>
  16. #include <linux/blkdev.h>
  17. #include <linux/delay.h>
  18. #include <linux/device.h>
  19. #include <scsi/scsi_host.h>
  20. #include <linux/libata.h>
  21. #include <linux/ata.h>
  22. #define DRV_NAME "pata_efar"
  23. #define DRV_VERSION "0.4.2"
  24. /**
  25. * efar_pre_reset - check for 40/80 pin
  26. * @ap: Port
  27. *
  28. * Perform cable detection for the EFAR ATA interface. This is
  29. * different to the PIIX arrangement
  30. */
  31. static int efar_pre_reset(struct ata_port *ap)
  32. {
  33. static const struct pci_bits efar_enable_bits[] = {
  34. { 0x41U, 1U, 0x80UL, 0x80UL }, /* port 0 */
  35. { 0x43U, 1U, 0x80UL, 0x80UL }, /* port 1 */
  36. };
  37. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  38. u8 tmp;
  39. if (!pci_test_config_bits(pdev, &efar_enable_bits[ap->port_no]))
  40. return -ENOENT;
  41. pci_read_config_byte(pdev, 0x47, &tmp);
  42. if (tmp & (2 >> ap->port_no))
  43. ap->cbl = ATA_CBL_PATA40;
  44. else
  45. ap->cbl = ATA_CBL_PATA80;
  46. return ata_std_prereset(ap);
  47. }
  48. /**
  49. * efar_probe_reset - Probe specified port on PATA host controller
  50. * @ap: Port to probe
  51. *
  52. * LOCKING:
  53. * None (inherited from caller).
  54. */
  55. static void efar_error_handler(struct ata_port *ap)
  56. {
  57. ata_bmdma_drive_eh(ap, efar_pre_reset, ata_std_softreset, NULL, ata_std_postreset);
  58. }
  59. /**
  60. * efar_set_piomode - Initialize host controller PATA PIO timings
  61. * @ap: Port whose timings we are configuring
  62. * @adev: um
  63. *
  64. * Set PIO mode for device, in host controller PCI config space.
  65. *
  66. * LOCKING:
  67. * None (inherited from caller).
  68. */
  69. static void efar_set_piomode (struct ata_port *ap, struct ata_device *adev)
  70. {
  71. unsigned int pio = adev->pio_mode - XFER_PIO_0;
  72. struct pci_dev *dev = to_pci_dev(ap->host->dev);
  73. unsigned int idetm_port= ap->port_no ? 0x42 : 0x40;
  74. u16 idetm_data;
  75. int control = 0;
  76. /*
  77. * See Intel Document 298600-004 for the timing programing rules
  78. * for PIIX/ICH. The EFAR is a clone so very similar
  79. */
  80. static const /* ISP RTC */
  81. u8 timings[][2] = { { 0, 0 },
  82. { 0, 0 },
  83. { 1, 0 },
  84. { 2, 1 },
  85. { 2, 3 }, };
  86. if (pio > 2)
  87. control |= 1; /* TIME1 enable */
  88. if (ata_pio_need_iordy(adev)) /* PIO 3/4 require IORDY */
  89. control |= 2; /* IE enable */
  90. /* Intel specifies that the PPE functionality is for disk only */
  91. if (adev->class == ATA_DEV_ATA)
  92. control |= 4; /* PPE enable */
  93. pci_read_config_word(dev, idetm_port, &idetm_data);
  94. /* Enable PPE, IE and TIME as appropriate */
  95. if (adev->devno == 0) {
  96. idetm_data &= 0xCCF0;
  97. idetm_data |= control;
  98. idetm_data |= (timings[pio][0] << 12) |
  99. (timings[pio][1] << 8);
  100. } else {
  101. int shift = 4 * ap->port_no;
  102. u8 slave_data;
  103. idetm_data &= 0xCC0F;
  104. idetm_data |= (control << 4);
  105. /* Slave timing in seperate register */
  106. pci_read_config_byte(dev, 0x44, &slave_data);
  107. slave_data &= 0x0F << shift;
  108. slave_data |= ((timings[pio][0] << 2) | timings[pio][1]) << shift;
  109. pci_write_config_byte(dev, 0x44, slave_data);
  110. }
  111. idetm_data |= 0x4000; /* Ensure SITRE is enabled */
  112. pci_write_config_word(dev, idetm_port, idetm_data);
  113. }
  114. /**
  115. * efar_set_dmamode - Initialize host controller PATA DMA timings
  116. * @ap: Port whose timings we are configuring
  117. * @adev: Device to program
  118. *
  119. * Set UDMA/MWDMA mode for device, in host controller PCI config space.
  120. *
  121. * LOCKING:
  122. * None (inherited from caller).
  123. */
  124. static void efar_set_dmamode (struct ata_port *ap, struct ata_device *adev)
  125. {
  126. struct pci_dev *dev = to_pci_dev(ap->host->dev);
  127. u8 master_port = ap->port_no ? 0x42 : 0x40;
  128. u16 master_data;
  129. u8 speed = adev->dma_mode;
  130. int devid = adev->devno + 2 * ap->port_no;
  131. u8 udma_enable;
  132. static const /* ISP RTC */
  133. u8 timings[][2] = { { 0, 0 },
  134. { 0, 0 },
  135. { 1, 0 },
  136. { 2, 1 },
  137. { 2, 3 }, };
  138. pci_read_config_word(dev, master_port, &master_data);
  139. pci_read_config_byte(dev, 0x48, &udma_enable);
  140. if (speed >= XFER_UDMA_0) {
  141. unsigned int udma = adev->dma_mode - XFER_UDMA_0;
  142. u16 udma_timing;
  143. udma_enable |= (1 << devid);
  144. /* Load the UDMA mode number */
  145. pci_read_config_word(dev, 0x4A, &udma_timing);
  146. udma_timing &= ~(7 << (4 * devid));
  147. udma_timing |= udma << (4 * devid);
  148. pci_write_config_word(dev, 0x4A, udma_timing);
  149. } else {
  150. /*
  151. * MWDMA is driven by the PIO timings. We must also enable
  152. * IORDY unconditionally along with TIME1. PPE has already
  153. * been set when the PIO timing was set.
  154. */
  155. unsigned int mwdma = adev->dma_mode - XFER_MW_DMA_0;
  156. unsigned int control;
  157. u8 slave_data;
  158. const unsigned int needed_pio[3] = {
  159. XFER_PIO_0, XFER_PIO_3, XFER_PIO_4
  160. };
  161. int pio = needed_pio[mwdma] - XFER_PIO_0;
  162. control = 3; /* IORDY|TIME1 */
  163. /* If the drive MWDMA is faster than it can do PIO then
  164. we must force PIO into PIO0 */
  165. if (adev->pio_mode < needed_pio[mwdma])
  166. /* Enable DMA timing only */
  167. control |= 8; /* PIO cycles in PIO0 */
  168. if (adev->devno) { /* Slave */
  169. master_data &= 0xFF4F; /* Mask out IORDY|TIME1|DMAONLY */
  170. master_data |= control << 4;
  171. pci_read_config_byte(dev, 0x44, &slave_data);
  172. slave_data &= (0x0F + 0xE1 * ap->port_no);
  173. /* Load the matching timing */
  174. slave_data |= ((timings[pio][0] << 2) | timings[pio][1]) << (ap->port_no ? 4 : 0);
  175. pci_write_config_byte(dev, 0x44, slave_data);
  176. } else { /* Master */
  177. master_data &= 0xCCF4; /* Mask out IORDY|TIME1|DMAONLY
  178. and master timing bits */
  179. master_data |= control;
  180. master_data |=
  181. (timings[pio][0] << 12) |
  182. (timings[pio][1] << 8);
  183. }
  184. udma_enable &= ~(1 << devid);
  185. pci_write_config_word(dev, master_port, master_data);
  186. }
  187. pci_write_config_byte(dev, 0x48, udma_enable);
  188. }
  189. static struct scsi_host_template efar_sht = {
  190. .module = THIS_MODULE,
  191. .name = DRV_NAME,
  192. .ioctl = ata_scsi_ioctl,
  193. .queuecommand = ata_scsi_queuecmd,
  194. .can_queue = ATA_DEF_QUEUE,
  195. .this_id = ATA_SHT_THIS_ID,
  196. .sg_tablesize = LIBATA_MAX_PRD,
  197. .max_sectors = ATA_MAX_SECTORS,
  198. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  199. .emulated = ATA_SHT_EMULATED,
  200. .use_clustering = ATA_SHT_USE_CLUSTERING,
  201. .proc_name = DRV_NAME,
  202. .dma_boundary = ATA_DMA_BOUNDARY,
  203. .slave_configure = ata_scsi_slave_config,
  204. .bios_param = ata_std_bios_param,
  205. };
  206. static const struct ata_port_operations efar_ops = {
  207. .port_disable = ata_port_disable,
  208. .set_piomode = efar_set_piomode,
  209. .set_dmamode = efar_set_dmamode,
  210. .mode_filter = ata_pci_default_filter,
  211. .tf_load = ata_tf_load,
  212. .tf_read = ata_tf_read,
  213. .check_status = ata_check_status,
  214. .exec_command = ata_exec_command,
  215. .dev_select = ata_std_dev_select,
  216. .freeze = ata_bmdma_freeze,
  217. .thaw = ata_bmdma_thaw,
  218. .error_handler = efar_error_handler,
  219. .post_internal_cmd = ata_bmdma_post_internal_cmd,
  220. .bmdma_setup = ata_bmdma_setup,
  221. .bmdma_start = ata_bmdma_start,
  222. .bmdma_stop = ata_bmdma_stop,
  223. .bmdma_status = ata_bmdma_status,
  224. .qc_prep = ata_qc_prep,
  225. .qc_issue = ata_qc_issue_prot,
  226. .data_xfer = ata_pio_data_xfer,
  227. .irq_handler = ata_interrupt,
  228. .irq_clear = ata_bmdma_irq_clear,
  229. .port_start = ata_port_start,
  230. .port_stop = ata_port_stop,
  231. .host_stop = ata_host_stop,
  232. };
  233. /**
  234. * efar_init_one - Register EFAR ATA PCI device with kernel services
  235. * @pdev: PCI device to register
  236. * @ent: Entry in efar_pci_tbl matching with @pdev
  237. *
  238. * Called from kernel PCI layer.
  239. *
  240. * LOCKING:
  241. * Inherited from PCI layer (may sleep).
  242. *
  243. * RETURNS:
  244. * Zero on success, or -ERRNO value.
  245. */
  246. static int efar_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
  247. {
  248. static int printed_version;
  249. static struct ata_port_info info = {
  250. .sht = &efar_sht,
  251. .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST,
  252. .pio_mask = 0x1f, /* pio0-4 */
  253. .mwdma_mask = 0x07, /* mwdma1-2 */
  254. .udma_mask = 0x0f, /* UDMA 66 */
  255. .port_ops = &efar_ops,
  256. };
  257. static struct ata_port_info *port_info[2] = { &info, &info };
  258. if (!printed_version++)
  259. dev_printk(KERN_DEBUG, &pdev->dev,
  260. "version " DRV_VERSION "\n");
  261. return ata_pci_init_one(pdev, port_info, 2);
  262. }
  263. static const struct pci_device_id efar_pci_tbl[] = {
  264. { PCI_VDEVICE(EFAR, 0x9130), },
  265. { } /* terminate list */
  266. };
  267. static struct pci_driver efar_pci_driver = {
  268. .name = DRV_NAME,
  269. .id_table = efar_pci_tbl,
  270. .probe = efar_init_one,
  271. .remove = ata_pci_remove_one,
  272. };
  273. static int __init efar_init(void)
  274. {
  275. return pci_register_driver(&efar_pci_driver);
  276. }
  277. static void __exit efar_exit(void)
  278. {
  279. pci_unregister_driver(&efar_pci_driver);
  280. }
  281. module_init(efar_init);
  282. module_exit(efar_exit);
  283. MODULE_AUTHOR("Alan Cox");
  284. MODULE_DESCRIPTION("SCSI low-level driver for EFAR PIIX clones");
  285. MODULE_LICENSE("GPL");
  286. MODULE_DEVICE_TABLE(pci, efar_pci_tbl);
  287. MODULE_VERSION(DRV_VERSION);