pata_it8213.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * pata_it8213.c - iTE Tech. Inc. IT8213 PATA driver
  3. *
  4. * The IT8213 is a very Intel ICH like device for timing purposes, having
  5. * a similar register layout and the same split clock arrangement. Cable
  6. * detection is different, and it does not have slave channels or all the
  7. * clutter of later ICH/SATA setups.
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/pci.h>
  12. #include <linux/blkdev.h>
  13. #include <linux/delay.h>
  14. #include <linux/device.h>
  15. #include <scsi/scsi_host.h>
  16. #include <linux/libata.h>
  17. #include <linux/ata.h>
  18. #define DRV_NAME "pata_it8213"
  19. #define DRV_VERSION "0.0.3"
  20. /**
  21. * it8213_pre_reset - probe begin
  22. * @link: link
  23. * @deadline: deadline jiffies for the operation
  24. *
  25. * Filter out ports by the enable bits before doing the normal reset
  26. * and probe.
  27. */
  28. static int it8213_pre_reset(struct ata_link *link, unsigned long deadline)
  29. {
  30. static const struct pci_bits it8213_enable_bits[] = {
  31. { 0x41U, 1U, 0x80UL, 0x80UL }, /* port 0 */
  32. };
  33. struct ata_port *ap = link->ap;
  34. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  35. if (!pci_test_config_bits(pdev, &it8213_enable_bits[ap->port_no]))
  36. return -ENOENT;
  37. return ata_sff_prereset(link, deadline);
  38. }
  39. /**
  40. * it8213_cable_detect - check for 40/80 pin
  41. * @ap: Port
  42. *
  43. * Perform cable detection for the 8213 ATA interface. This is
  44. * different to the PIIX arrangement
  45. */
  46. static int it8213_cable_detect(struct ata_port *ap)
  47. {
  48. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  49. u8 tmp;
  50. pci_read_config_byte(pdev, 0x42, &tmp);
  51. if (tmp & 2) /* The initial docs are incorrect */
  52. return ATA_CBL_PATA40;
  53. return ATA_CBL_PATA80;
  54. }
  55. /**
  56. * it8213_set_piomode - Initialize host controller PATA PIO timings
  57. * @ap: Port whose timings we are configuring
  58. * @adev: Device whose timings we are configuring
  59. *
  60. * Set PIO mode for device, in host controller PCI config space.
  61. *
  62. * LOCKING:
  63. * None (inherited from caller).
  64. */
  65. static void it8213_set_piomode (struct ata_port *ap, struct ata_device *adev)
  66. {
  67. unsigned int pio = adev->pio_mode - XFER_PIO_0;
  68. struct pci_dev *dev = to_pci_dev(ap->host->dev);
  69. unsigned int master_port = ap->port_no ? 0x42 : 0x40;
  70. u16 master_data;
  71. int control = 0;
  72. /*
  73. * See Intel Document 298600-004 for the timing programing rules
  74. * for PIIX/ICH. The 8213 is a clone so very similar
  75. */
  76. static const /* ISP RTC */
  77. u8 timings[][2] = { { 0, 0 },
  78. { 0, 0 },
  79. { 1, 0 },
  80. { 2, 1 },
  81. { 2, 3 }, };
  82. if (pio > 1)
  83. control |= 1; /* TIME */
  84. if (ata_pio_need_iordy(adev)) /* PIO 3/4 require IORDY */
  85. control |= 2; /* IE */
  86. /* Bit 2 is set for ATAPI on the IT8213 - reverse of ICH/PIIX */
  87. if (adev->class != ATA_DEV_ATA)
  88. control |= 4; /* PPE */
  89. pci_read_config_word(dev, master_port, &master_data);
  90. /* Set PPE, IE, and TIME as appropriate */
  91. if (adev->devno == 0) {
  92. master_data &= 0xCCF0;
  93. master_data |= control;
  94. master_data |= (timings[pio][0] << 12) |
  95. (timings[pio][1] << 8);
  96. } else {
  97. u8 slave_data;
  98. master_data &= 0xFF0F;
  99. master_data |= (control << 4);
  100. /* Slave timing in separate register */
  101. pci_read_config_byte(dev, 0x44, &slave_data);
  102. slave_data &= 0xF0;
  103. slave_data |= (timings[pio][0] << 2) | timings[pio][1];
  104. pci_write_config_byte(dev, 0x44, slave_data);
  105. }
  106. master_data |= 0x4000; /* Ensure SITRE is set */
  107. pci_write_config_word(dev, master_port, master_data);
  108. }
  109. /**
  110. * it8213_set_dmamode - Initialize host controller PATA DMA timings
  111. * @ap: Port whose timings we are configuring
  112. * @adev: Device to program
  113. *
  114. * Set UDMA/MWDMA mode for device, in host controller PCI config space.
  115. * This device is basically an ICH alike.
  116. *
  117. * LOCKING:
  118. * None (inherited from caller).
  119. */
  120. static void it8213_set_dmamode (struct ata_port *ap, struct ata_device *adev)
  121. {
  122. struct pci_dev *dev = to_pci_dev(ap->host->dev);
  123. u16 master_data;
  124. u8 speed = adev->dma_mode;
  125. int devid = adev->devno;
  126. u8 udma_enable;
  127. static const /* ISP RTC */
  128. u8 timings[][2] = { { 0, 0 },
  129. { 0, 0 },
  130. { 1, 0 },
  131. { 2, 1 },
  132. { 2, 3 }, };
  133. pci_read_config_word(dev, 0x40, &master_data);
  134. pci_read_config_byte(dev, 0x48, &udma_enable);
  135. if (speed >= XFER_UDMA_0) {
  136. unsigned int udma = adev->dma_mode - XFER_UDMA_0;
  137. u16 udma_timing;
  138. u16 ideconf;
  139. int u_clock, u_speed;
  140. /* Clocks follow the PIIX style */
  141. u_speed = min(2 - (udma & 1), udma);
  142. if (udma > 4)
  143. u_clock = 0x1000; /* 100Mhz */
  144. else if (udma > 2)
  145. u_clock = 1; /* 66Mhz */
  146. else
  147. u_clock = 0; /* 33Mhz */
  148. udma_enable |= (1 << devid);
  149. /* Load the UDMA cycle time */
  150. pci_read_config_word(dev, 0x4A, &udma_timing);
  151. udma_timing &= ~(3 << (4 * devid));
  152. udma_timing |= u_speed << (4 * devid);
  153. pci_write_config_word(dev, 0x4A, udma_timing);
  154. /* Load the clock selection */
  155. pci_read_config_word(dev, 0x54, &ideconf);
  156. ideconf &= ~(0x1001 << devid);
  157. ideconf |= u_clock << devid;
  158. pci_write_config_word(dev, 0x54, ideconf);
  159. } else {
  160. /*
  161. * MWDMA is driven by the PIO timings. We must also enable
  162. * IORDY unconditionally along with TIME1. PPE has already
  163. * been set when the PIO timing was set.
  164. */
  165. unsigned int mwdma = adev->dma_mode - XFER_MW_DMA_0;
  166. unsigned int control;
  167. u8 slave_data;
  168. static const unsigned int needed_pio[3] = {
  169. XFER_PIO_0, XFER_PIO_3, XFER_PIO_4
  170. };
  171. int pio = needed_pio[mwdma] - XFER_PIO_0;
  172. control = 3; /* IORDY|TIME1 */
  173. /* If the drive MWDMA is faster than it can do PIO then
  174. we must force PIO into PIO0 */
  175. if (adev->pio_mode < needed_pio[mwdma])
  176. /* Enable DMA timing only */
  177. control |= 8; /* PIO cycles in PIO0 */
  178. if (devid) { /* Slave */
  179. master_data &= 0xFF4F; /* Mask out IORDY|TIME1|DMAONLY */
  180. master_data |= control << 4;
  181. pci_read_config_byte(dev, 0x44, &slave_data);
  182. slave_data &= 0xF0;
  183. /* Load the matching timing */
  184. slave_data |= ((timings[pio][0] << 2) | timings[pio][1]) << (ap->port_no ? 4 : 0);
  185. pci_write_config_byte(dev, 0x44, slave_data);
  186. } else { /* Master */
  187. master_data &= 0xCCF4; /* Mask out IORDY|TIME1|DMAONLY
  188. and master timing bits */
  189. master_data |= control;
  190. master_data |=
  191. (timings[pio][0] << 12) |
  192. (timings[pio][1] << 8);
  193. }
  194. udma_enable &= ~(1 << devid);
  195. pci_write_config_word(dev, 0x40, master_data);
  196. }
  197. pci_write_config_byte(dev, 0x48, udma_enable);
  198. }
  199. static struct scsi_host_template it8213_sht = {
  200. ATA_BMDMA_SHT(DRV_NAME),
  201. };
  202. static struct ata_port_operations it8213_ops = {
  203. .inherits = &ata_bmdma_port_ops,
  204. .cable_detect = it8213_cable_detect,
  205. .set_piomode = it8213_set_piomode,
  206. .set_dmamode = it8213_set_dmamode,
  207. .prereset = it8213_pre_reset,
  208. };
  209. /**
  210. * it8213_init_one - Register 8213 ATA PCI device with kernel services
  211. * @pdev: PCI device to register
  212. * @ent: Entry in it8213_pci_tbl matching with @pdev
  213. *
  214. * Called from kernel PCI layer.
  215. *
  216. * LOCKING:
  217. * Inherited from PCI layer (may sleep).
  218. *
  219. * RETURNS:
  220. * Zero on success, or -ERRNO value.
  221. */
  222. static int it8213_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
  223. {
  224. static const struct ata_port_info info = {
  225. .flags = ATA_FLAG_SLAVE_POSS,
  226. .pio_mask = ATA_PIO4,
  227. .mwdma_mask = ATA_MWDMA12_ONLY,
  228. .udma_mask = ATA_UDMA6,
  229. .port_ops = &it8213_ops,
  230. };
  231. /* Current IT8213 stuff is single port */
  232. const struct ata_port_info *ppi[] = { &info, &ata_dummy_port_info };
  233. ata_print_version_once(&pdev->dev, DRV_VERSION);
  234. return ata_pci_bmdma_init_one(pdev, ppi, &it8213_sht, NULL, 0);
  235. }
  236. static const struct pci_device_id it8213_pci_tbl[] = {
  237. { PCI_VDEVICE(ITE, PCI_DEVICE_ID_ITE_8213), },
  238. { } /* terminate list */
  239. };
  240. static struct pci_driver it8213_pci_driver = {
  241. .name = DRV_NAME,
  242. .id_table = it8213_pci_tbl,
  243. .probe = it8213_init_one,
  244. .remove = ata_pci_remove_one,
  245. #ifdef CONFIG_PM_SLEEP
  246. .suspend = ata_pci_device_suspend,
  247. .resume = ata_pci_device_resume,
  248. #endif
  249. };
  250. module_pci_driver(it8213_pci_driver);
  251. MODULE_AUTHOR("Alan Cox");
  252. MODULE_DESCRIPTION("SCSI low-level driver for the ITE 8213");
  253. MODULE_LICENSE("GPL");
  254. MODULE_DEVICE_TABLE(pci, it8213_pci_tbl);
  255. MODULE_VERSION(DRV_VERSION);