pata_efar.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * pata_efar.c - EFAR PIIX clone controller driver
  3. *
  4. * (C) 2005 Red Hat
  5. * (C) 2009-2010 Bartlomiej Zolnierkiewicz
  6. *
  7. * Some parts based on ata_piix.c by Jeff Garzik and others.
  8. *
  9. * The EFAR is a PIIX4 clone with UDMA66 support. Unlike the later
  10. * Intel ICH controllers the EFAR widened the UDMA mode register bits
  11. * and doesn't require the funky clock selection.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/pci.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.5"
  24. /**
  25. * efar_pre_reset - Enable bits
  26. * @link: ATA link
  27. * @deadline: deadline jiffies for the operation
  28. *
  29. * Perform cable detection for the EFAR ATA interface. This is
  30. * different to the PIIX arrangement
  31. */
  32. static int efar_pre_reset(struct ata_link *link, unsigned long deadline)
  33. {
  34. static const struct pci_bits efar_enable_bits[] = {
  35. { 0x41U, 1U, 0x80UL, 0x80UL }, /* port 0 */
  36. { 0x43U, 1U, 0x80UL, 0x80UL }, /* port 1 */
  37. };
  38. struct ata_port *ap = link->ap;
  39. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  40. if (!pci_test_config_bits(pdev, &efar_enable_bits[ap->port_no]))
  41. return -ENOENT;
  42. return ata_sff_prereset(link, deadline);
  43. }
  44. /**
  45. * efar_cable_detect - check for 40/80 pin
  46. * @ap: Port
  47. *
  48. * Perform cable detection for the EFAR ATA interface. This is
  49. * different to the PIIX arrangement
  50. */
  51. static int efar_cable_detect(struct ata_port *ap)
  52. {
  53. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  54. u8 tmp;
  55. pci_read_config_byte(pdev, 0x47, &tmp);
  56. if (tmp & (2 >> ap->port_no))
  57. return ATA_CBL_PATA40;
  58. return ATA_CBL_PATA80;
  59. }
  60. static DEFINE_SPINLOCK(efar_lock);
  61. /**
  62. * efar_set_piomode - Initialize host controller PATA PIO timings
  63. * @ap: Port whose timings we are configuring
  64. * @adev: Device to program
  65. *
  66. * Set PIO mode for device, in host controller PCI config space.
  67. *
  68. * LOCKING:
  69. * None (inherited from caller).
  70. */
  71. static void efar_set_piomode (struct ata_port *ap, struct ata_device *adev)
  72. {
  73. unsigned int pio = adev->pio_mode - XFER_PIO_0;
  74. struct pci_dev *dev = to_pci_dev(ap->host->dev);
  75. unsigned int master_port = ap->port_no ? 0x42 : 0x40;
  76. unsigned long flags;
  77. u16 master_data;
  78. u8 udma_enable;
  79. int control = 0;
  80. /*
  81. * See Intel Document 298600-004 for the timing programing rules
  82. * for PIIX/ICH. The EFAR is a clone so very similar
  83. */
  84. static const /* ISP RTC */
  85. u8 timings[][2] = { { 0, 0 },
  86. { 0, 0 },
  87. { 1, 0 },
  88. { 2, 1 },
  89. { 2, 3 }, };
  90. if (pio > 1)
  91. control |= 1; /* TIME */
  92. if (ata_pio_need_iordy(adev)) /* PIO 3/4 require IORDY */
  93. control |= 2; /* IE */
  94. /* Intel specifies that the prefetch/posting is for disk only */
  95. if (adev->class == ATA_DEV_ATA)
  96. control |= 4; /* PPE */
  97. spin_lock_irqsave(&efar_lock, flags);
  98. pci_read_config_word(dev, master_port, &master_data);
  99. /* Set PPE, IE, and TIME as appropriate */
  100. if (adev->devno == 0) {
  101. master_data &= 0xCCF0;
  102. master_data |= control;
  103. master_data |= (timings[pio][0] << 12) |
  104. (timings[pio][1] << 8);
  105. } else {
  106. int shift = 4 * ap->port_no;
  107. u8 slave_data;
  108. master_data &= 0xFF0F;
  109. master_data |= (control << 4);
  110. /* Slave timing in separate register */
  111. pci_read_config_byte(dev, 0x44, &slave_data);
  112. slave_data &= ap->port_no ? 0x0F : 0xF0;
  113. slave_data |= ((timings[pio][0] << 2) | timings[pio][1]) << shift;
  114. pci_write_config_byte(dev, 0x44, slave_data);
  115. }
  116. master_data |= 0x4000; /* Ensure SITRE is set */
  117. pci_write_config_word(dev, master_port, master_data);
  118. pci_read_config_byte(dev, 0x48, &udma_enable);
  119. udma_enable &= ~(1 << (2 * ap->port_no + adev->devno));
  120. pci_write_config_byte(dev, 0x48, udma_enable);
  121. spin_unlock_irqrestore(&efar_lock, flags);
  122. }
  123. /**
  124. * efar_set_dmamode - Initialize host controller PATA DMA timings
  125. * @ap: Port whose timings we are configuring
  126. * @adev: Device to program
  127. *
  128. * Set UDMA/MWDMA mode for device, in host controller PCI config space.
  129. *
  130. * LOCKING:
  131. * None (inherited from caller).
  132. */
  133. static void efar_set_dmamode (struct ata_port *ap, struct ata_device *adev)
  134. {
  135. struct pci_dev *dev = to_pci_dev(ap->host->dev);
  136. u8 master_port = ap->port_no ? 0x42 : 0x40;
  137. u16 master_data;
  138. u8 speed = adev->dma_mode;
  139. int devid = adev->devno + 2 * ap->port_no;
  140. unsigned long flags;
  141. u8 udma_enable;
  142. static const /* ISP RTC */
  143. u8 timings[][2] = { { 0, 0 },
  144. { 0, 0 },
  145. { 1, 0 },
  146. { 2, 1 },
  147. { 2, 3 }, };
  148. spin_lock_irqsave(&efar_lock, flags);
  149. pci_read_config_word(dev, master_port, &master_data);
  150. pci_read_config_byte(dev, 0x48, &udma_enable);
  151. if (speed >= XFER_UDMA_0) {
  152. unsigned int udma = adev->dma_mode - XFER_UDMA_0;
  153. u16 udma_timing;
  154. udma_enable |= (1 << devid);
  155. /* Load the UDMA mode number */
  156. pci_read_config_word(dev, 0x4A, &udma_timing);
  157. udma_timing &= ~(7 << (4 * devid));
  158. udma_timing |= udma << (4 * devid);
  159. pci_write_config_word(dev, 0x4A, udma_timing);
  160. } else {
  161. /*
  162. * MWDMA is driven by the PIO timings. We must also enable
  163. * IORDY unconditionally along with TIME1. PPE has already
  164. * been set when the PIO timing was set.
  165. */
  166. unsigned int mwdma = adev->dma_mode - XFER_MW_DMA_0;
  167. unsigned int control;
  168. u8 slave_data;
  169. const unsigned int needed_pio[3] = {
  170. XFER_PIO_0, XFER_PIO_3, XFER_PIO_4
  171. };
  172. int pio = needed_pio[mwdma] - XFER_PIO_0;
  173. control = 3; /* IORDY|TIME1 */
  174. /* If the drive MWDMA is faster than it can do PIO then
  175. we must force PIO into PIO0 */
  176. if (adev->pio_mode < needed_pio[mwdma])
  177. /* Enable DMA timing only */
  178. control |= 8; /* PIO cycles in PIO0 */
  179. if (adev->devno) { /* Slave */
  180. master_data &= 0xFF4F; /* Mask out IORDY|TIME1|DMAONLY */
  181. master_data |= control << 4;
  182. pci_read_config_byte(dev, 0x44, &slave_data);
  183. slave_data &= ap->port_no ? 0x0F : 0xF0;
  184. /* Load the matching timing */
  185. slave_data |= ((timings[pio][0] << 2) | timings[pio][1]) << (ap->port_no ? 4 : 0);
  186. pci_write_config_byte(dev, 0x44, slave_data);
  187. } else { /* Master */
  188. master_data &= 0xCCF4; /* Mask out IORDY|TIME1|DMAONLY
  189. and master timing bits */
  190. master_data |= control;
  191. master_data |=
  192. (timings[pio][0] << 12) |
  193. (timings[pio][1] << 8);
  194. }
  195. udma_enable &= ~(1 << devid);
  196. pci_write_config_word(dev, master_port, master_data);
  197. }
  198. pci_write_config_byte(dev, 0x48, udma_enable);
  199. spin_unlock_irqrestore(&efar_lock, flags);
  200. }
  201. static struct scsi_host_template efar_sht = {
  202. ATA_BMDMA_SHT(DRV_NAME),
  203. };
  204. static struct ata_port_operations efar_ops = {
  205. .inherits = &ata_bmdma_port_ops,
  206. .cable_detect = efar_cable_detect,
  207. .set_piomode = efar_set_piomode,
  208. .set_dmamode = efar_set_dmamode,
  209. .prereset = efar_pre_reset,
  210. };
  211. /**
  212. * efar_init_one - Register EFAR ATA PCI device with kernel services
  213. * @pdev: PCI device to register
  214. * @ent: Entry in efar_pci_tbl matching with @pdev
  215. *
  216. * Called from kernel PCI layer.
  217. *
  218. * LOCKING:
  219. * Inherited from PCI layer (may sleep).
  220. *
  221. * RETURNS:
  222. * Zero on success, or -ERRNO value.
  223. */
  224. static int efar_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
  225. {
  226. static const struct ata_port_info info = {
  227. .flags = ATA_FLAG_SLAVE_POSS,
  228. .pio_mask = ATA_PIO4,
  229. .mwdma_mask = ATA_MWDMA12_ONLY,
  230. .udma_mask = ATA_UDMA4,
  231. .port_ops = &efar_ops,
  232. };
  233. const struct ata_port_info *ppi[] = { &info, &info };
  234. ata_print_version_once(&pdev->dev, DRV_VERSION);
  235. return ata_pci_bmdma_init_one(pdev, ppi, &efar_sht, NULL,
  236. ATA_HOST_PARALLEL_SCAN);
  237. }
  238. static const struct pci_device_id efar_pci_tbl[] = {
  239. { PCI_VDEVICE(EFAR, 0x9130), },
  240. { } /* terminate list */
  241. };
  242. static struct pci_driver efar_pci_driver = {
  243. .name = DRV_NAME,
  244. .id_table = efar_pci_tbl,
  245. .probe = efar_init_one,
  246. .remove = ata_pci_remove_one,
  247. #ifdef CONFIG_PM_SLEEP
  248. .suspend = ata_pci_device_suspend,
  249. .resume = ata_pci_device_resume,
  250. #endif
  251. };
  252. module_pci_driver(efar_pci_driver);
  253. MODULE_AUTHOR("Alan Cox");
  254. MODULE_DESCRIPTION("SCSI low-level driver for EFAR PIIX clones");
  255. MODULE_LICENSE("GPL");
  256. MODULE_DEVICE_TABLE(pci, efar_pci_tbl);
  257. MODULE_VERSION(DRV_VERSION);