pata_it8213.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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/init.h>
  13. #include <linux/blkdev.h>
  14. #include <linux/delay.h>
  15. #include <linux/device.h>
  16. #include <scsi/scsi_host.h>
  17. #include <linux/libata.h>
  18. #include <linux/ata.h>
  19. #define DRV_NAME "pata_it8213"
  20. #define DRV_VERSION "0.0.2"
  21. /**
  22. * it8213_pre_reset - check for 40/80 pin
  23. * @ap: Port
  24. * @deadline: deadline jiffies for the operation
  25. *
  26. * Filter out ports by the enable bits before doing the normal reset
  27. * and probe.
  28. */
  29. static int it8213_pre_reset(struct ata_port *ap, unsigned long deadline)
  30. {
  31. static const struct pci_bits it8213_enable_bits[] = {
  32. { 0x41U, 1U, 0x80UL, 0x80UL }, /* port 0 */
  33. };
  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_std_prereset(ap, deadline);
  38. }
  39. /**
  40. * it8213_error_handler - Probe specified port on PATA host controller
  41. * @ap: Port to probe
  42. *
  43. * LOCKING:
  44. * None (inherited from caller).
  45. */
  46. static void it8213_error_handler(struct ata_port *ap)
  47. {
  48. ata_bmdma_drive_eh(ap, it8213_pre_reset, ata_std_softreset, NULL, ata_std_postreset);
  49. }
  50. /**
  51. * it8213_cable_detect - check for 40/80 pin
  52. * @ap: Port
  53. *
  54. * Perform cable detection for the 8213 ATA interface. This is
  55. * different to the PIIX arrangement
  56. */
  57. static int it8213_cable_detect(struct ata_port *ap)
  58. {
  59. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  60. u8 tmp;
  61. pci_read_config_byte(pdev, 0x42, &tmp);
  62. if (tmp & 2) /* The initial docs are incorrect */
  63. return ATA_CBL_PATA40;
  64. return ATA_CBL_PATA80;
  65. }
  66. /**
  67. * it8213_set_piomode - Initialize host controller PATA PIO timings
  68. * @ap: Port whose timings we are configuring
  69. * @adev: Device whose timings we are configuring
  70. *
  71. * Set PIO mode for device, in host controller PCI config space.
  72. *
  73. * LOCKING:
  74. * None (inherited from caller).
  75. */
  76. static void it8213_set_piomode (struct ata_port *ap, struct ata_device *adev)
  77. {
  78. unsigned int pio = adev->pio_mode - XFER_PIO_0;
  79. struct pci_dev *dev = to_pci_dev(ap->host->dev);
  80. unsigned int idetm_port= ap->port_no ? 0x42 : 0x40;
  81. u16 idetm_data;
  82. int control = 0;
  83. /*
  84. * See Intel Document 298600-004 for the timing programing rules
  85. * for PIIX/ICH. The 8213 is a clone so very similar
  86. */
  87. static const /* ISP RTC */
  88. u8 timings[][2] = { { 0, 0 },
  89. { 0, 0 },
  90. { 1, 0 },
  91. { 2, 1 },
  92. { 2, 3 }, };
  93. if (pio > 2)
  94. control |= 1; /* TIME1 enable */
  95. if (ata_pio_need_iordy(adev)) /* PIO 3/4 require IORDY */
  96. control |= 2; /* IORDY enable */
  97. /* Bit 2 is set for ATAPI on the IT8213 - reverse of ICH/PIIX */
  98. if (adev->class != ATA_DEV_ATA)
  99. control |= 4;
  100. pci_read_config_word(dev, idetm_port, &idetm_data);
  101. /* Enable PPE, IE and TIME as appropriate */
  102. if (adev->devno == 0) {
  103. idetm_data &= 0xCCF0;
  104. idetm_data |= control;
  105. idetm_data |= (timings[pio][0] << 12) |
  106. (timings[pio][1] << 8);
  107. } else {
  108. u8 slave_data;
  109. idetm_data &= 0xCC0F;
  110. idetm_data |= (control << 4);
  111. /* Slave timing in seperate register */
  112. pci_read_config_byte(dev, 0x44, &slave_data);
  113. slave_data &= 0xF0;
  114. slave_data |= ((timings[pio][0] << 2) | timings[pio][1]) << 4;
  115. pci_write_config_byte(dev, 0x44, slave_data);
  116. }
  117. idetm_data |= 0x4000; /* Ensure SITRE is enabled */
  118. pci_write_config_word(dev, idetm_port, idetm_data);
  119. }
  120. /**
  121. * it8213_set_dmamode - Initialize host controller PATA DMA timings
  122. * @ap: Port whose timings we are configuring
  123. * @adev: Device to program
  124. *
  125. * Set UDMA/MWDMA mode for device, in host controller PCI config space.
  126. * This device is basically an ICH alike.
  127. *
  128. * LOCKING:
  129. * None (inherited from caller).
  130. */
  131. static void it8213_set_dmamode (struct ata_port *ap, struct ata_device *adev)
  132. {
  133. struct pci_dev *dev = to_pci_dev(ap->host->dev);
  134. u16 master_data;
  135. u8 speed = adev->dma_mode;
  136. int devid = adev->devno;
  137. u8 udma_enable;
  138. static const /* ISP RTC */
  139. u8 timings[][2] = { { 0, 0 },
  140. { 0, 0 },
  141. { 1, 0 },
  142. { 2, 1 },
  143. { 2, 3 }, };
  144. pci_read_config_word(dev, 0x40, &master_data);
  145. pci_read_config_byte(dev, 0x48, &udma_enable);
  146. if (speed >= XFER_UDMA_0) {
  147. unsigned int udma = adev->dma_mode - XFER_UDMA_0;
  148. u16 udma_timing;
  149. u16 ideconf;
  150. int u_clock, u_speed;
  151. /* Clocks follow the PIIX style */
  152. u_speed = min(2 - (udma & 1), udma);
  153. if (udma == 5)
  154. u_clock = 0x1000; /* 100Mhz */
  155. else if (udma > 2)
  156. u_clock = 1; /* 66Mhz */
  157. else
  158. u_clock = 0; /* 33Mhz */
  159. udma_enable |= (1 << devid);
  160. /* Load the UDMA mode number */
  161. pci_read_config_word(dev, 0x4A, &udma_timing);
  162. udma_timing &= ~(3 << (4 * devid));
  163. udma_timing |= (udma & 3) << (4 * devid);
  164. pci_write_config_word(dev, 0x4A, udma_timing);
  165. /* Load the clock selection */
  166. pci_read_config_word(dev, 0x54, &ideconf);
  167. ideconf &= ~(0x1001 << devid);
  168. ideconf |= u_clock << devid;
  169. pci_write_config_word(dev, 0x54, ideconf);
  170. } else {
  171. /*
  172. * MWDMA is driven by the PIO timings. We must also enable
  173. * IORDY unconditionally along with TIME1. PPE has already
  174. * been set when the PIO timing was set.
  175. */
  176. unsigned int mwdma = adev->dma_mode - XFER_MW_DMA_0;
  177. unsigned int control;
  178. u8 slave_data;
  179. static const unsigned int needed_pio[3] = {
  180. XFER_PIO_0, XFER_PIO_3, XFER_PIO_4
  181. };
  182. int pio = needed_pio[mwdma] - XFER_PIO_0;
  183. control = 3; /* IORDY|TIME1 */
  184. /* If the drive MWDMA is faster than it can do PIO then
  185. we must force PIO into PIO0 */
  186. if (adev->pio_mode < needed_pio[mwdma])
  187. /* Enable DMA timing only */
  188. control |= 8; /* PIO cycles in PIO0 */
  189. if (devid) { /* Slave */
  190. master_data &= 0xFF4F; /* Mask out IORDY|TIME1|DMAONLY */
  191. master_data |= control << 4;
  192. pci_read_config_byte(dev, 0x44, &slave_data);
  193. slave_data &= (0x0F + 0xE1 * ap->port_no);
  194. /* Load the matching timing */
  195. slave_data |= ((timings[pio][0] << 2) | timings[pio][1]) << (ap->port_no ? 4 : 0);
  196. pci_write_config_byte(dev, 0x44, slave_data);
  197. } else { /* Master */
  198. master_data &= 0xCCF4; /* Mask out IORDY|TIME1|DMAONLY
  199. and master timing bits */
  200. master_data |= control;
  201. master_data |=
  202. (timings[pio][0] << 12) |
  203. (timings[pio][1] << 8);
  204. }
  205. udma_enable &= ~(1 << devid);
  206. pci_write_config_word(dev, 0x40, master_data);
  207. }
  208. pci_write_config_byte(dev, 0x48, udma_enable);
  209. }
  210. static struct scsi_host_template it8213_sht = {
  211. .module = THIS_MODULE,
  212. .name = DRV_NAME,
  213. .ioctl = ata_scsi_ioctl,
  214. .queuecommand = ata_scsi_queuecmd,
  215. .can_queue = ATA_DEF_QUEUE,
  216. .this_id = ATA_SHT_THIS_ID,
  217. .sg_tablesize = LIBATA_MAX_PRD,
  218. .max_sectors = ATA_MAX_SECTORS,
  219. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  220. .emulated = ATA_SHT_EMULATED,
  221. .use_clustering = ATA_SHT_USE_CLUSTERING,
  222. .proc_name = DRV_NAME,
  223. .dma_boundary = ATA_DMA_BOUNDARY,
  224. .slave_configure = ata_scsi_slave_config,
  225. .bios_param = ata_std_bios_param,
  226. };
  227. static const struct ata_port_operations it8213_ops = {
  228. .port_disable = ata_port_disable,
  229. .set_piomode = it8213_set_piomode,
  230. .set_dmamode = it8213_set_dmamode,
  231. .mode_filter = ata_pci_default_filter,
  232. .tf_load = ata_tf_load,
  233. .tf_read = ata_tf_read,
  234. .check_status = ata_check_status,
  235. .exec_command = ata_exec_command,
  236. .dev_select = ata_std_dev_select,
  237. .freeze = ata_bmdma_freeze,
  238. .thaw = ata_bmdma_thaw,
  239. .error_handler = it8213_error_handler,
  240. .post_internal_cmd = ata_bmdma_post_internal_cmd,
  241. .cable_detect = it8213_cable_detect,
  242. .bmdma_setup = ata_bmdma_setup,
  243. .bmdma_start = ata_bmdma_start,
  244. .bmdma_stop = ata_bmdma_stop,
  245. .bmdma_status = ata_bmdma_status,
  246. .qc_prep = ata_qc_prep,
  247. .qc_issue = ata_qc_issue_prot,
  248. .data_xfer = ata_data_xfer,
  249. .irq_handler = ata_interrupt,
  250. .irq_clear = ata_bmdma_irq_clear,
  251. .irq_on = ata_irq_on,
  252. .irq_ack = ata_irq_ack,
  253. .port_start = ata_port_start,
  254. };
  255. /**
  256. * it8213_init_one - Register 8213 ATA PCI device with kernel services
  257. * @pdev: PCI device to register
  258. * @ent: Entry in it8213_pci_tbl matching with @pdev
  259. *
  260. * Called from kernel PCI layer.
  261. *
  262. * LOCKING:
  263. * Inherited from PCI layer (may sleep).
  264. *
  265. * RETURNS:
  266. * Zero on success, or -ERRNO value.
  267. */
  268. static int it8213_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
  269. {
  270. static int printed_version;
  271. static const struct ata_port_info info = {
  272. .sht = &it8213_sht,
  273. .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST,
  274. .pio_mask = 0x1f, /* pio0-4 */
  275. .mwdma_mask = 0x07, /* mwdma0-2 */
  276. .udma_mask = 0x1f, /* UDMA 100 */
  277. .port_ops = &it8213_ops,
  278. };
  279. /* Current IT8213 stuff is single port */
  280. const struct ata_port_info *ppi[] = { &info, &ata_dummy_port_info };
  281. if (!printed_version++)
  282. dev_printk(KERN_DEBUG, &pdev->dev,
  283. "version " DRV_VERSION "\n");
  284. return ata_pci_init_one(pdev, ppi);
  285. }
  286. static const struct pci_device_id it8213_pci_tbl[] = {
  287. { PCI_VDEVICE(ITE, PCI_DEVICE_ID_ITE_8213), },
  288. { } /* terminate list */
  289. };
  290. static struct pci_driver it8213_pci_driver = {
  291. .name = DRV_NAME,
  292. .id_table = it8213_pci_tbl,
  293. .probe = it8213_init_one,
  294. .remove = ata_pci_remove_one,
  295. #ifdef CONFIG_PM
  296. .suspend = ata_pci_device_suspend,
  297. .resume = ata_pci_device_resume,
  298. #endif
  299. };
  300. static int __init it8213_init(void)
  301. {
  302. return pci_register_driver(&it8213_pci_driver);
  303. }
  304. static void __exit it8213_exit(void)
  305. {
  306. pci_unregister_driver(&it8213_pci_driver);
  307. }
  308. module_init(it8213_init);
  309. module_exit(it8213_exit);
  310. MODULE_AUTHOR("Alan Cox");
  311. MODULE_DESCRIPTION("SCSI low-level driver for the ITE 8213");
  312. MODULE_LICENSE("GPL");
  313. MODULE_DEVICE_TABLE(pci, it8213_pci_tbl);
  314. MODULE_VERSION(DRV_VERSION);