it8213.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. * ITE 8213 IDE driver
  3. *
  4. * Copyright (C) 2006 Jack Lee
  5. * Copyright (C) 2006 Alan Cox
  6. * Copyright (C) 2007 Bartlomiej Zolnierkiewicz
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/types.h>
  10. #include <linux/module.h>
  11. #include <linux/pci.h>
  12. #include <linux/delay.h>
  13. #include <linux/hdreg.h>
  14. #include <linux/ide.h>
  15. #include <linux/init.h>
  16. #include <asm/io.h>
  17. /**
  18. * it8213_dma_2_pio - return the PIO mode matching DMA
  19. * @xfer_rate: transfer speed
  20. *
  21. * Returns the nearest equivalent PIO timing for the PIO or DMA
  22. * mode requested by the controller.
  23. */
  24. static u8 it8213_dma_2_pio (u8 xfer_rate) {
  25. switch(xfer_rate) {
  26. case XFER_UDMA_6:
  27. case XFER_UDMA_5:
  28. case XFER_UDMA_4:
  29. case XFER_UDMA_3:
  30. case XFER_UDMA_2:
  31. case XFER_UDMA_1:
  32. case XFER_UDMA_0:
  33. case XFER_MW_DMA_2:
  34. case XFER_PIO_4:
  35. return 4;
  36. case XFER_MW_DMA_1:
  37. case XFER_PIO_3:
  38. return 3;
  39. case XFER_SW_DMA_2:
  40. case XFER_PIO_2:
  41. return 2;
  42. case XFER_MW_DMA_0:
  43. case XFER_SW_DMA_1:
  44. case XFER_SW_DMA_0:
  45. case XFER_PIO_1:
  46. case XFER_PIO_0:
  47. case XFER_PIO_SLOW:
  48. default:
  49. return 0;
  50. }
  51. }
  52. /*
  53. * it8213_tuneproc - tune a drive
  54. * @drive: drive to tune
  55. * @pio: desired PIO mode
  56. *
  57. * Set the interface PIO mode.
  58. */
  59. static void it8213_tuneproc (ide_drive_t *drive, u8 pio)
  60. {
  61. ide_hwif_t *hwif = HWIF(drive);
  62. struct pci_dev *dev = hwif->pci_dev;
  63. int is_slave = drive->dn & 1;
  64. int master_port = 0x40;
  65. int slave_port = 0x44;
  66. unsigned long flags;
  67. u16 master_data;
  68. u8 slave_data;
  69. static DEFINE_SPINLOCK(tune_lock);
  70. int control = 0;
  71. static const u8 timings[][2]= {
  72. { 0, 0 },
  73. { 0, 0 },
  74. { 1, 0 },
  75. { 2, 1 },
  76. { 2, 3 }, };
  77. pio = ide_get_best_pio_mode(drive, pio, 4, NULL);
  78. spin_lock_irqsave(&tune_lock, flags);
  79. pci_read_config_word(dev, master_port, &master_data);
  80. if (pio > 1)
  81. control |= 1; /* Programmable timing on */
  82. if (drive->media != ide_disk)
  83. control |= 4; /* ATAPI */
  84. if (pio > 2)
  85. control |= 2; /* IORDY */
  86. if (is_slave) {
  87. master_data |= 0x4000;
  88. master_data &= ~0x0070;
  89. if (pio > 1)
  90. master_data = master_data | (control << 4);
  91. pci_read_config_byte(dev, slave_port, &slave_data);
  92. slave_data = slave_data & 0xf0;
  93. slave_data = slave_data | (timings[pio][0] << 2) | timings[pio][1];
  94. } else {
  95. master_data &= ~0x3307;
  96. if (pio > 1)
  97. master_data = master_data | control;
  98. master_data = master_data | (timings[pio][0] << 12) | (timings[pio][1] << 8);
  99. }
  100. pci_write_config_word(dev, master_port, master_data);
  101. if (is_slave)
  102. pci_write_config_byte(dev, slave_port, slave_data);
  103. spin_unlock_irqrestore(&tune_lock, flags);
  104. }
  105. /**
  106. * it8213_tune_chipset - set controller timings
  107. * @drive: Drive to set up
  108. * @xferspeed: speed we want to achieve
  109. *
  110. * Tune the ITE chipset for the desired mode. If we can't achieve
  111. * the desired mode then tune for a lower one, but ultimately
  112. * make the thing work.
  113. */
  114. static int it8213_tune_chipset (ide_drive_t *drive, u8 xferspeed)
  115. {
  116. ide_hwif_t *hwif = HWIF(drive);
  117. struct pci_dev *dev = hwif->pci_dev;
  118. u8 maslave = 0x40;
  119. u8 speed = ide_rate_filter(drive, xferspeed);
  120. int a_speed = 3 << (drive->dn * 4);
  121. int u_flag = 1 << drive->dn;
  122. int v_flag = 0x01 << drive->dn;
  123. int w_flag = 0x10 << drive->dn;
  124. int u_speed = 0;
  125. u16 reg4042, reg4a;
  126. u8 reg48, reg54, reg55;
  127. pci_read_config_word(dev, maslave, &reg4042);
  128. pci_read_config_byte(dev, 0x48, &reg48);
  129. pci_read_config_word(dev, 0x4a, &reg4a);
  130. pci_read_config_byte(dev, 0x54, &reg54);
  131. pci_read_config_byte(dev, 0x55, &reg55);
  132. switch(speed) {
  133. case XFER_UDMA_6:
  134. case XFER_UDMA_4:
  135. case XFER_UDMA_2: u_speed = 2 << (drive->dn * 4); break;
  136. case XFER_UDMA_5:
  137. case XFER_UDMA_3:
  138. case XFER_UDMA_1: u_speed = 1 << (drive->dn * 4); break;
  139. case XFER_UDMA_0: u_speed = 0 << (drive->dn * 4); break;
  140. break;
  141. case XFER_MW_DMA_2:
  142. case XFER_MW_DMA_1:
  143. case XFER_SW_DMA_2:
  144. break;
  145. case XFER_PIO_4:
  146. case XFER_PIO_3:
  147. case XFER_PIO_2:
  148. case XFER_PIO_1:
  149. case XFER_PIO_0:
  150. break;
  151. default:
  152. return -1;
  153. }
  154. if (speed >= XFER_UDMA_0) {
  155. if (!(reg48 & u_flag))
  156. pci_write_config_byte(dev, 0x48, reg48 | u_flag);
  157. if (speed >= XFER_UDMA_5) {
  158. pci_write_config_byte(dev, 0x55, (u8) reg55|w_flag);
  159. } else {
  160. pci_write_config_byte(dev, 0x55, (u8) reg55 & ~w_flag);
  161. }
  162. if ((reg4a & a_speed) != u_speed)
  163. pci_write_config_word(dev, 0x4a, (reg4a & ~a_speed) | u_speed);
  164. if (speed > XFER_UDMA_2) {
  165. if (!(reg54 & v_flag))
  166. pci_write_config_byte(dev, 0x54, reg54 | v_flag);
  167. } else
  168. pci_write_config_byte(dev, 0x54, reg54 & ~v_flag);
  169. } else {
  170. if (reg48 & u_flag)
  171. pci_write_config_byte(dev, 0x48, reg48 & ~u_flag);
  172. if (reg4a & a_speed)
  173. pci_write_config_word(dev, 0x4a, reg4a & ~a_speed);
  174. if (reg54 & v_flag)
  175. pci_write_config_byte(dev, 0x54, reg54 & ~v_flag);
  176. if (reg55 & w_flag)
  177. pci_write_config_byte(dev, 0x55, (u8) reg55 & ~w_flag);
  178. }
  179. it8213_tuneproc(drive, it8213_dma_2_pio(speed));
  180. return ide_config_drive_speed(drive, speed);
  181. }
  182. /**
  183. * it8213_configure_drive_for_dma - set up for DMA transfers
  184. * @drive: drive we are going to set up
  185. *
  186. * Set up the drive for DMA, tune the controller and drive as
  187. * required. If the drive isn't suitable for DMA or we hit
  188. * other problems then we will drop down to PIO and set up
  189. * PIO appropriately
  190. */
  191. static int it8213_config_drive_for_dma (ide_drive_t *drive)
  192. {
  193. u8 pio;
  194. if (ide_tune_dma(drive))
  195. return 0;
  196. pio = ide_get_best_pio_mode(drive, 255, 4, NULL);
  197. it8213_tune_chipset(drive, XFER_PIO_0 + pio);
  198. return -1;
  199. }
  200. /**
  201. * init_hwif_it8213 - set up hwif structs
  202. * @hwif: interface to set up
  203. *
  204. * We do the basic set up of the interface structure. The IT8212
  205. * requires several custom handlers so we override the default
  206. * ide DMA handlers appropriately
  207. */
  208. static void __devinit init_hwif_it8213(ide_hwif_t *hwif)
  209. {
  210. u8 reg42h = 0, ata66 = 0;
  211. hwif->speedproc = &it8213_tune_chipset;
  212. hwif->tuneproc = &it8213_tuneproc;
  213. hwif->autodma = 0;
  214. hwif->drives[0].autotune = 1;
  215. hwif->drives[1].autotune = 1;
  216. if (!hwif->dma_base)
  217. return;
  218. hwif->atapi_dma = 1;
  219. hwif->ultra_mask = 0x7f;
  220. hwif->mwdma_mask = 0x06;
  221. hwif->swdma_mask = 0x04;
  222. pci_read_config_byte(hwif->pci_dev, 0x42, &reg42h);
  223. ata66 = (reg42h & 0x02) ? 0 : 1;
  224. hwif->ide_dma_check = &it8213_config_drive_for_dma;
  225. if (!(hwif->udma_four))
  226. hwif->udma_four = ata66;
  227. /*
  228. * The BIOS often doesn't set up DMA on this controller
  229. * so we always do it.
  230. */
  231. if (!noautodma)
  232. hwif->autodma = 1;
  233. hwif->drives[0].autodma = hwif->autodma;
  234. hwif->drives[1].autodma = hwif->autodma;
  235. }
  236. #define DECLARE_ITE_DEV(name_str) \
  237. { \
  238. .name = name_str, \
  239. .init_hwif = init_hwif_it8213, \
  240. .channels = 1, \
  241. .autodma = AUTODMA, \
  242. .enablebits = {{0x41,0x80,0x80}}, \
  243. .bootable = ON_BOARD, \
  244. }
  245. static ide_pci_device_t it8213_chipsets[] __devinitdata = {
  246. /* 0 */ DECLARE_ITE_DEV("IT8213"),
  247. };
  248. /**
  249. * it8213_init_one - pci layer discovery entry
  250. * @dev: PCI device
  251. * @id: ident table entry
  252. *
  253. * Called by the PCI code when it finds an ITE8213 controller. As
  254. * this device follows the standard interfaces we can use the
  255. * standard helper functions to do almost all the work for us.
  256. */
  257. static int __devinit it8213_init_one(struct pci_dev *dev, const struct pci_device_id *id)
  258. {
  259. ide_setup_pci_device(dev, &it8213_chipsets[id->driver_data]);
  260. return 0;
  261. }
  262. static struct pci_device_id it8213_pci_tbl[] = {
  263. { PCI_VENDOR_ID_ITE, 0x8213, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  264. { 0, },
  265. };
  266. MODULE_DEVICE_TABLE(pci, it8213_pci_tbl);
  267. static struct pci_driver driver = {
  268. .name = "ITE8213_IDE",
  269. .id_table = it8213_pci_tbl,
  270. .probe = it8213_init_one,
  271. };
  272. static int __init it8213_ide_init(void)
  273. {
  274. return ide_pci_register_driver(&driver);
  275. }
  276. module_init(it8213_ide_init);
  277. MODULE_AUTHOR("Jack Lee, Alan Cox");
  278. MODULE_DESCRIPTION("PCI driver module for the ITE 8213");
  279. MODULE_LICENSE("GPL");