pata_it8213.c 9.6 KB

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