pata_it8213.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. *
  25. * Perform cable detection for the 8213 ATA interface. This is
  26. * different to the PIIX arrangement
  27. */
  28. static int it8213_pre_reset(struct ata_port *ap)
  29. {
  30. static const struct pci_bits it8213_enable_bits[] = {
  31. { 0x41U, 1U, 0x80UL, 0x80UL }, /* port 0 */
  32. };
  33. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  34. u8 tmp;
  35. if (!pci_test_config_bits(pdev, &it8213_enable_bits[ap->port_no]))
  36. return -ENOENT;
  37. pci_read_config_byte(pdev, 0x42, &tmp);
  38. if (tmp & 2) /* The initial docs are incorrect */
  39. ap->cbl = ATA_CBL_PATA40;
  40. else
  41. ap->cbl = ATA_CBL_PATA80;
  42. return ata_std_prereset(ap);
  43. }
  44. /**
  45. * it8213_probe_reset - Probe specified port on PATA host controller
  46. * @ap: Port to probe
  47. *
  48. * LOCKING:
  49. * None (inherited from caller).
  50. */
  51. static void it8213_error_handler(struct ata_port *ap)
  52. {
  53. ata_bmdma_drive_eh(ap, it8213_pre_reset, ata_std_softreset, NULL, ata_std_postreset);
  54. }
  55. /**
  56. * it8213_set_piomode - Initialize host controller PATA PIO timings
  57. * @ap: Port whose timings we are configuring
  58. * @adev: um
  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 idetm_port= ap->port_no ? 0x42 : 0x40;
  70. u16 idetm_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 > 2)
  83. control |= 1; /* TIME1 enable */
  84. if (ata_pio_need_iordy(adev)) /* PIO 3/4 require IORDY */
  85. control |= 2; /* IORDY enable */
  86. /* Bit 2 is set for ATAPI on the IT8213 - reverse of ICH/PIIX */
  87. if (adev->class != ATA_DEV_ATA)
  88. control |= 4;
  89. pci_read_config_word(dev, idetm_port, &idetm_data);
  90. /* Enable PPE, IE and TIME as appropriate */
  91. if (adev->devno == 0) {
  92. idetm_data &= 0xCCF0;
  93. idetm_data |= control;
  94. idetm_data |= (timings[pio][0] << 12) |
  95. (timings[pio][1] << 8);
  96. } else {
  97. u8 slave_data;
  98. idetm_data &= 0xCC0F;
  99. idetm_data |= (control << 4);
  100. /* Slave timing in seperate register */
  101. pci_read_config_byte(dev, 0x44, &slave_data);
  102. slave_data &= 0xF0;
  103. slave_data |= ((timings[pio][0] << 2) | timings[pio][1]) << 4;
  104. pci_write_config_byte(dev, 0x44, slave_data);
  105. }
  106. idetm_data |= 0x4000; /* Ensure SITRE is enabled */
  107. pci_write_config_word(dev, idetm_port, idetm_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 == 5)
  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 mode number */
  150. pci_read_config_word(dev, 0x4A, &udma_timing);
  151. udma_timing &= ~(3 << (4 * devid));
  152. udma_timing |= (udma & 3) << (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 &= (0x0F + 0xE1 * ap->port_no);
  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. .module = THIS_MODULE,
  201. .name = DRV_NAME,
  202. .ioctl = ata_scsi_ioctl,
  203. .queuecommand = ata_scsi_queuecmd,
  204. .can_queue = ATA_DEF_QUEUE,
  205. .this_id = ATA_SHT_THIS_ID,
  206. .sg_tablesize = LIBATA_MAX_PRD,
  207. .max_sectors = ATA_MAX_SECTORS,
  208. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  209. .emulated = ATA_SHT_EMULATED,
  210. .use_clustering = ATA_SHT_USE_CLUSTERING,
  211. .proc_name = DRV_NAME,
  212. .dma_boundary = ATA_DMA_BOUNDARY,
  213. .slave_configure = ata_scsi_slave_config,
  214. .bios_param = ata_std_bios_param,
  215. .resume = ata_scsi_device_resume,
  216. .suspend = ata_scsi_device_suspend,
  217. };
  218. static const struct ata_port_operations it8213_ops = {
  219. .port_disable = ata_port_disable,
  220. .set_piomode = it8213_set_piomode,
  221. .set_dmamode = it8213_set_dmamode,
  222. .mode_filter = ata_pci_default_filter,
  223. .tf_load = ata_tf_load,
  224. .tf_read = ata_tf_read,
  225. .check_status = ata_check_status,
  226. .exec_command = ata_exec_command,
  227. .dev_select = ata_std_dev_select,
  228. .freeze = ata_bmdma_freeze,
  229. .thaw = ata_bmdma_thaw,
  230. .error_handler = it8213_error_handler,
  231. .post_internal_cmd = ata_bmdma_post_internal_cmd,
  232. .bmdma_setup = ata_bmdma_setup,
  233. .bmdma_start = ata_bmdma_start,
  234. .bmdma_stop = ata_bmdma_stop,
  235. .bmdma_status = ata_bmdma_status,
  236. .qc_prep = ata_qc_prep,
  237. .qc_issue = ata_qc_issue_prot,
  238. .data_xfer = ata_data_xfer,
  239. .irq_handler = ata_interrupt,
  240. .irq_clear = ata_bmdma_irq_clear,
  241. .irq_on = ata_irq_on,
  242. .irq_ack = ata_irq_ack,
  243. .port_start = ata_port_start,
  244. };
  245. /**
  246. * it8213_init_one - Register 8213 ATA PCI device with kernel services
  247. * @pdev: PCI device to register
  248. * @ent: Entry in it8213_pci_tbl matching with @pdev
  249. *
  250. * Called from kernel PCI layer.
  251. *
  252. * LOCKING:
  253. * Inherited from PCI layer (may sleep).
  254. *
  255. * RETURNS:
  256. * Zero on success, or -ERRNO value.
  257. */
  258. static int it8213_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
  259. {
  260. static int printed_version;
  261. static struct ata_port_info info = {
  262. .sht = &it8213_sht,
  263. .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST,
  264. .pio_mask = 0x1f, /* pio0-4 */
  265. .mwdma_mask = 0x07, /* mwdma0-2 */
  266. .udma_mask = 0x1f, /* UDMA 100 */
  267. .port_ops = &it8213_ops,
  268. };
  269. static struct ata_port_info *port_info[2] = { &info, &info };
  270. if (!printed_version++)
  271. dev_printk(KERN_DEBUG, &pdev->dev,
  272. "version " DRV_VERSION "\n");
  273. /* Current IT8213 stuff is single port */
  274. return ata_pci_init_one(pdev, port_info, 1);
  275. }
  276. static const struct pci_device_id it8213_pci_tbl[] = {
  277. { PCI_VDEVICE(ITE, PCI_DEVICE_ID_ITE_8213), },
  278. { } /* terminate list */
  279. };
  280. static struct pci_driver it8213_pci_driver = {
  281. .name = DRV_NAME,
  282. .id_table = it8213_pci_tbl,
  283. .probe = it8213_init_one,
  284. .remove = ata_pci_remove_one,
  285. .suspend = ata_pci_device_suspend,
  286. .resume = ata_pci_device_resume,
  287. };
  288. static int __init it8213_init(void)
  289. {
  290. return pci_register_driver(&it8213_pci_driver);
  291. }
  292. static void __exit it8213_exit(void)
  293. {
  294. pci_unregister_driver(&it8213_pci_driver);
  295. }
  296. module_init(it8213_init);
  297. module_exit(it8213_exit);
  298. MODULE_AUTHOR("Alan Cox");
  299. MODULE_DESCRIPTION("SCSI low-level driver for the ITE 8213");
  300. MODULE_LICENSE("GPL");
  301. MODULE_DEVICE_TABLE(pci, it8213_pci_tbl);
  302. MODULE_VERSION(DRV_VERSION);