it8213.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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_set_pio_mode - set host controller for PIO mode
  48. * @drive: drive
  49. * @pio: PIO mode number
  50. *
  51. * Set the interface PIO mode.
  52. */
  53. static void it8213_set_pio_mode(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. /**
  99. * it8213_set_dma_mode - set host controller for DMA mode
  100. * @drive: drive
  101. * @speed: DMA mode
  102. *
  103. * Tune the ITE chipset for the DMA mode.
  104. */
  105. static void it8213_set_dma_mode(ide_drive_t *drive, const u8 speed)
  106. {
  107. ide_hwif_t *hwif = HWIF(drive);
  108. struct pci_dev *dev = hwif->pci_dev;
  109. u8 maslave = 0x40;
  110. int a_speed = 3 << (drive->dn * 4);
  111. int u_flag = 1 << drive->dn;
  112. int v_flag = 0x01 << drive->dn;
  113. int w_flag = 0x10 << drive->dn;
  114. int u_speed = 0;
  115. u16 reg4042, reg4a;
  116. u8 reg48, reg54, reg55;
  117. pci_read_config_word(dev, maslave, &reg4042);
  118. pci_read_config_byte(dev, 0x48, &reg48);
  119. pci_read_config_word(dev, 0x4a, &reg4a);
  120. pci_read_config_byte(dev, 0x54, &reg54);
  121. pci_read_config_byte(dev, 0x55, &reg55);
  122. switch(speed) {
  123. case XFER_UDMA_6:
  124. case XFER_UDMA_4:
  125. case XFER_UDMA_2: u_speed = 2 << (drive->dn * 4); break;
  126. case XFER_UDMA_5:
  127. case XFER_UDMA_3:
  128. case XFER_UDMA_1: u_speed = 1 << (drive->dn * 4); break;
  129. case XFER_UDMA_0: u_speed = 0 << (drive->dn * 4); break;
  130. break;
  131. case XFER_MW_DMA_2:
  132. case XFER_MW_DMA_1:
  133. case XFER_SW_DMA_2:
  134. break;
  135. default:
  136. return;
  137. }
  138. if (speed >= XFER_UDMA_0) {
  139. if (!(reg48 & u_flag))
  140. pci_write_config_byte(dev, 0x48, reg48 | u_flag);
  141. if (speed >= XFER_UDMA_5) {
  142. pci_write_config_byte(dev, 0x55, (u8) reg55|w_flag);
  143. } else {
  144. pci_write_config_byte(dev, 0x55, (u8) reg55 & ~w_flag);
  145. }
  146. if ((reg4a & a_speed) != u_speed)
  147. pci_write_config_word(dev, 0x4a, (reg4a & ~a_speed) | u_speed);
  148. if (speed > XFER_UDMA_2) {
  149. if (!(reg54 & v_flag))
  150. pci_write_config_byte(dev, 0x54, reg54 | v_flag);
  151. } else
  152. pci_write_config_byte(dev, 0x54, reg54 & ~v_flag);
  153. } else {
  154. if (reg48 & u_flag)
  155. pci_write_config_byte(dev, 0x48, reg48 & ~u_flag);
  156. if (reg4a & a_speed)
  157. pci_write_config_word(dev, 0x4a, reg4a & ~a_speed);
  158. if (reg54 & v_flag)
  159. pci_write_config_byte(dev, 0x54, reg54 & ~v_flag);
  160. if (reg55 & w_flag)
  161. pci_write_config_byte(dev, 0x55, (u8) reg55 & ~w_flag);
  162. }
  163. it8213_set_pio_mode(drive, it8213_dma_2_pio(speed));
  164. }
  165. /**
  166. * it8213_configure_drive_for_dma - set up for DMA transfers
  167. * @drive: drive we are going to set up
  168. *
  169. * Set up the drive for DMA, tune the controller and drive as
  170. * required. If the drive isn't suitable for DMA or we hit
  171. * other problems then we will drop down to PIO and set up
  172. * PIO appropriately
  173. */
  174. static int it8213_config_drive_for_dma (ide_drive_t *drive)
  175. {
  176. if (ide_tune_dma(drive))
  177. return 0;
  178. ide_set_max_pio(drive);
  179. return -1;
  180. }
  181. /**
  182. * init_hwif_it8213 - set up hwif structs
  183. * @hwif: interface to set up
  184. *
  185. * We do the basic set up of the interface structure. The IT8212
  186. * requires several custom handlers so we override the default
  187. * ide DMA handlers appropriately
  188. */
  189. static void __devinit init_hwif_it8213(ide_hwif_t *hwif)
  190. {
  191. u8 reg42h = 0;
  192. hwif->set_dma_mode = &it8213_set_dma_mode;
  193. hwif->set_pio_mode = &it8213_set_pio_mode;
  194. hwif->autodma = 0;
  195. hwif->drives[0].autotune = 1;
  196. hwif->drives[1].autotune = 1;
  197. if (!hwif->dma_base)
  198. return;
  199. hwif->atapi_dma = 1;
  200. hwif->ultra_mask = 0x7f;
  201. hwif->mwdma_mask = 0x06;
  202. hwif->swdma_mask = 0x04;
  203. pci_read_config_byte(hwif->pci_dev, 0x42, &reg42h);
  204. hwif->ide_dma_check = &it8213_config_drive_for_dma;
  205. if (hwif->cbl != ATA_CBL_PATA40_SHORT)
  206. hwif->cbl = (reg42h & 0x02) ? ATA_CBL_PATA40 : ATA_CBL_PATA80;
  207. /*
  208. * The BIOS often doesn't set up DMA on this controller
  209. * so we always do it.
  210. */
  211. if (!noautodma)
  212. hwif->autodma = 1;
  213. hwif->drives[0].autodma = hwif->autodma;
  214. hwif->drives[1].autodma = hwif->autodma;
  215. }
  216. #define DECLARE_ITE_DEV(name_str) \
  217. { \
  218. .name = name_str, \
  219. .init_hwif = init_hwif_it8213, \
  220. .autodma = AUTODMA, \
  221. .enablebits = {{0x41,0x80,0x80}}, \
  222. .bootable = ON_BOARD, \
  223. .host_flags = IDE_HFLAG_SINGLE, \
  224. .pio_mask = ATA_PIO4, \
  225. }
  226. static ide_pci_device_t it8213_chipsets[] __devinitdata = {
  227. /* 0 */ DECLARE_ITE_DEV("IT8213"),
  228. };
  229. /**
  230. * it8213_init_one - pci layer discovery entry
  231. * @dev: PCI device
  232. * @id: ident table entry
  233. *
  234. * Called by the PCI code when it finds an ITE8213 controller. As
  235. * this device follows the standard interfaces we can use the
  236. * standard helper functions to do almost all the work for us.
  237. */
  238. static int __devinit it8213_init_one(struct pci_dev *dev, const struct pci_device_id *id)
  239. {
  240. ide_setup_pci_device(dev, &it8213_chipsets[id->driver_data]);
  241. return 0;
  242. }
  243. static struct pci_device_id it8213_pci_tbl[] = {
  244. { PCI_VENDOR_ID_ITE, 0x8213, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  245. { 0, },
  246. };
  247. MODULE_DEVICE_TABLE(pci, it8213_pci_tbl);
  248. static struct pci_driver driver = {
  249. .name = "ITE8213_IDE",
  250. .id_table = it8213_pci_tbl,
  251. .probe = it8213_init_one,
  252. };
  253. static int __init it8213_ide_init(void)
  254. {
  255. return ide_pci_register_driver(&driver);
  256. }
  257. module_init(it8213_ide_init);
  258. MODULE_AUTHOR("Jack Lee, Alan Cox");
  259. MODULE_DESCRIPTION("PCI driver module for the ITE 8213");
  260. MODULE_LICENSE("GPL");