it8213.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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 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. return 4;
  35. case XFER_MW_DMA_1:
  36. return 3;
  37. case XFER_SW_DMA_2:
  38. return 2;
  39. case XFER_MW_DMA_0:
  40. case XFER_SW_DMA_1:
  41. case XFER_SW_DMA_0:
  42. default:
  43. return 0;
  44. }
  45. }
  46. /*
  47. * it8213_tune_pio - tune a drive
  48. * @drive: drive to tune
  49. * @pio: desired PIO mode
  50. *
  51. * Set the interface PIO mode.
  52. */
  53. static void it8213_tune_pio(ide_drive_t *drive, const u8 pio)
  54. {
  55. ide_hwif_t *hwif = HWIF(drive);
  56. struct pci_dev *dev = hwif->pci_dev;
  57. int is_slave = drive->dn & 1;
  58. int master_port = 0x40;
  59. int slave_port = 0x44;
  60. unsigned long flags;
  61. u16 master_data;
  62. u8 slave_data;
  63. static DEFINE_SPINLOCK(tune_lock);
  64. int control = 0;
  65. static const u8 timings[][2]= {
  66. { 0, 0 },
  67. { 0, 0 },
  68. { 1, 0 },
  69. { 2, 1 },
  70. { 2, 3 }, };
  71. spin_lock_irqsave(&tune_lock, flags);
  72. pci_read_config_word(dev, master_port, &master_data);
  73. if (pio > 1)
  74. control |= 1; /* Programmable timing on */
  75. if (drive->media != ide_disk)
  76. control |= 4; /* ATAPI */
  77. if (pio > 2)
  78. control |= 2; /* IORDY */
  79. if (is_slave) {
  80. master_data |= 0x4000;
  81. master_data &= ~0x0070;
  82. if (pio > 1)
  83. master_data = master_data | (control << 4);
  84. pci_read_config_byte(dev, slave_port, &slave_data);
  85. slave_data = slave_data & 0xf0;
  86. slave_data = slave_data | (timings[pio][0] << 2) | timings[pio][1];
  87. } else {
  88. master_data &= ~0x3307;
  89. if (pio > 1)
  90. master_data = master_data | control;
  91. master_data = master_data | (timings[pio][0] << 12) | (timings[pio][1] << 8);
  92. }
  93. pci_write_config_word(dev, master_port, master_data);
  94. if (is_slave)
  95. pci_write_config_byte(dev, slave_port, slave_data);
  96. spin_unlock_irqrestore(&tune_lock, flags);
  97. }
  98. static void it8213_tuneproc(ide_drive_t *drive, u8 pio)
  99. {
  100. pio = ide_get_best_pio_mode(drive, pio, 4);
  101. it8213_tune_pio(drive, pio);
  102. ide_config_drive_speed(drive, XFER_PIO_0 + pio);
  103. }
  104. /**
  105. * it8213_tune_chipset - set controller timings
  106. * @drive: Drive to set up
  107. * @xferspeed: speed we want to achieve
  108. *
  109. * Tune the ITE chipset for the desired mode. If we can't achieve
  110. * the desired mode then tune for a lower one, but ultimately
  111. * make the thing work.
  112. */
  113. static int it8213_tune_chipset (ide_drive_t *drive, u8 xferspeed)
  114. {
  115. ide_hwif_t *hwif = HWIF(drive);
  116. struct pci_dev *dev = hwif->pci_dev;
  117. u8 maslave = 0x40;
  118. u8 speed = ide_rate_filter(drive, xferspeed);
  119. int a_speed = 3 << (drive->dn * 4);
  120. int u_flag = 1 << drive->dn;
  121. int v_flag = 0x01 << drive->dn;
  122. int w_flag = 0x10 << drive->dn;
  123. int u_speed = 0;
  124. u16 reg4042, reg4a;
  125. u8 reg48, reg54, reg55;
  126. pci_read_config_word(dev, maslave, &reg4042);
  127. pci_read_config_byte(dev, 0x48, &reg48);
  128. pci_read_config_word(dev, 0x4a, &reg4a);
  129. pci_read_config_byte(dev, 0x54, &reg54);
  130. pci_read_config_byte(dev, 0x55, &reg55);
  131. switch(speed) {
  132. case XFER_UDMA_6:
  133. case XFER_UDMA_4:
  134. case XFER_UDMA_2: u_speed = 2 << (drive->dn * 4); break;
  135. case XFER_UDMA_5:
  136. case XFER_UDMA_3:
  137. case XFER_UDMA_1: u_speed = 1 << (drive->dn * 4); break;
  138. case XFER_UDMA_0: u_speed = 0 << (drive->dn * 4); break;
  139. break;
  140. case XFER_MW_DMA_2:
  141. case XFER_MW_DMA_1:
  142. case XFER_SW_DMA_2:
  143. break;
  144. case XFER_PIO_4:
  145. case XFER_PIO_3:
  146. case XFER_PIO_2:
  147. case XFER_PIO_1:
  148. case XFER_PIO_0:
  149. break;
  150. default:
  151. return -1;
  152. }
  153. if (speed >= XFER_UDMA_0) {
  154. if (!(reg48 & u_flag))
  155. pci_write_config_byte(dev, 0x48, reg48 | u_flag);
  156. if (speed >= XFER_UDMA_5) {
  157. pci_write_config_byte(dev, 0x55, (u8) reg55|w_flag);
  158. } else {
  159. pci_write_config_byte(dev, 0x55, (u8) reg55 & ~w_flag);
  160. }
  161. if ((reg4a & a_speed) != u_speed)
  162. pci_write_config_word(dev, 0x4a, (reg4a & ~a_speed) | u_speed);
  163. if (speed > XFER_UDMA_2) {
  164. if (!(reg54 & v_flag))
  165. pci_write_config_byte(dev, 0x54, reg54 | v_flag);
  166. } else
  167. pci_write_config_byte(dev, 0x54, reg54 & ~v_flag);
  168. } else {
  169. if (reg48 & u_flag)
  170. pci_write_config_byte(dev, 0x48, reg48 & ~u_flag);
  171. if (reg4a & a_speed)
  172. pci_write_config_word(dev, 0x4a, reg4a & ~a_speed);
  173. if (reg54 & v_flag)
  174. pci_write_config_byte(dev, 0x54, reg54 & ~v_flag);
  175. if (reg55 & w_flag)
  176. pci_write_config_byte(dev, 0x55, (u8) reg55 & ~w_flag);
  177. }
  178. if (speed > XFER_PIO_4)
  179. it8213_tune_pio(drive, it8213_dma_2_pio(speed));
  180. else
  181. it8213_tune_pio(drive, speed - XFER_PIO_0);
  182. return ide_config_drive_speed(drive, speed);
  183. }
  184. /**
  185. * it8213_configure_drive_for_dma - set up for DMA transfers
  186. * @drive: drive we are going to set up
  187. *
  188. * Set up the drive for DMA, tune the controller and drive as
  189. * required. If the drive isn't suitable for DMA or we hit
  190. * other problems then we will drop down to PIO and set up
  191. * PIO appropriately
  192. */
  193. static int it8213_config_drive_for_dma (ide_drive_t *drive)
  194. {
  195. if (ide_tune_dma(drive))
  196. return 0;
  197. it8213_tuneproc(drive, 255);
  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;
  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. hwif->ide_dma_check = &it8213_config_drive_for_dma;
  224. if (hwif->cbl != ATA_CBL_PATA40_SHORT)
  225. hwif->cbl = (reg42h & 0x02) ? ATA_CBL_PATA40 : ATA_CBL_PATA80;
  226. /*
  227. * The BIOS often doesn't set up DMA on this controller
  228. * so we always do it.
  229. */
  230. if (!noautodma)
  231. hwif->autodma = 1;
  232. hwif->drives[0].autodma = hwif->autodma;
  233. hwif->drives[1].autodma = hwif->autodma;
  234. }
  235. #define DECLARE_ITE_DEV(name_str) \
  236. { \
  237. .name = name_str, \
  238. .init_hwif = init_hwif_it8213, \
  239. .autodma = AUTODMA, \
  240. .enablebits = {{0x41,0x80,0x80}}, \
  241. .bootable = ON_BOARD, \
  242. .host_flags = IDE_HFLAG_SINGLE, \
  243. .pio_mask = ATA_PIO4, \
  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");