it8213.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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/ide.h>
  13. #include <linux/init.h>
  14. #define DRV_NAME "it8213"
  15. /**
  16. * it8213_set_pio_mode - set host controller for PIO mode
  17. * @hwif: port
  18. * @drive: drive
  19. *
  20. * Set the interface PIO mode.
  21. */
  22. static void it8213_set_pio_mode(ide_hwif_t *hwif, ide_drive_t *drive)
  23. {
  24. struct pci_dev *dev = to_pci_dev(hwif->dev);
  25. int is_slave = drive->dn & 1;
  26. int master_port = 0x40;
  27. int slave_port = 0x44;
  28. unsigned long flags;
  29. u16 master_data;
  30. u8 slave_data;
  31. static DEFINE_SPINLOCK(tune_lock);
  32. int control = 0;
  33. const u8 pio = drive->pio_mode - XFER_PIO_0;
  34. static const u8 timings[][2] = {
  35. { 0, 0 },
  36. { 0, 0 },
  37. { 1, 0 },
  38. { 2, 1 },
  39. { 2, 3 }, };
  40. spin_lock_irqsave(&tune_lock, flags);
  41. pci_read_config_word(dev, master_port, &master_data);
  42. if (pio > 1)
  43. control |= 1; /* Programmable timing on */
  44. if (drive->media != ide_disk)
  45. control |= 4; /* ATAPI */
  46. if (ide_pio_need_iordy(drive, pio))
  47. control |= 2; /* IORDY */
  48. if (is_slave) {
  49. master_data |= 0x4000;
  50. master_data &= ~0x0070;
  51. if (pio > 1)
  52. master_data = master_data | (control << 4);
  53. pci_read_config_byte(dev, slave_port, &slave_data);
  54. slave_data = slave_data & 0xf0;
  55. slave_data = slave_data | (timings[pio][0] << 2) | timings[pio][1];
  56. } else {
  57. master_data &= ~0x3307;
  58. if (pio > 1)
  59. master_data = master_data | control;
  60. master_data = master_data | (timings[pio][0] << 12) | (timings[pio][1] << 8);
  61. }
  62. pci_write_config_word(dev, master_port, master_data);
  63. if (is_slave)
  64. pci_write_config_byte(dev, slave_port, slave_data);
  65. spin_unlock_irqrestore(&tune_lock, flags);
  66. }
  67. /**
  68. * it8213_set_dma_mode - set host controller for DMA mode
  69. * @hwif: port
  70. * @drive: drive
  71. *
  72. * Tune the ITE chipset for the DMA mode.
  73. */
  74. static void it8213_set_dma_mode(ide_hwif_t *hwif, ide_drive_t *drive)
  75. {
  76. struct pci_dev *dev = to_pci_dev(hwif->dev);
  77. u8 maslave = 0x40;
  78. int a_speed = 3 << (drive->dn * 4);
  79. int u_flag = 1 << drive->dn;
  80. int v_flag = 0x01 << drive->dn;
  81. int w_flag = 0x10 << drive->dn;
  82. int u_speed = 0;
  83. u16 reg4042, reg4a;
  84. u8 reg48, reg54, reg55;
  85. const u8 speed = drive->dma_mode;
  86. pci_read_config_word(dev, maslave, &reg4042);
  87. pci_read_config_byte(dev, 0x48, &reg48);
  88. pci_read_config_word(dev, 0x4a, &reg4a);
  89. pci_read_config_byte(dev, 0x54, &reg54);
  90. pci_read_config_byte(dev, 0x55, &reg55);
  91. if (speed >= XFER_UDMA_0) {
  92. u8 udma = speed - XFER_UDMA_0;
  93. u_speed = min_t(u8, 2 - (udma & 1), udma) << (drive->dn * 4);
  94. if (!(reg48 & u_flag))
  95. pci_write_config_byte(dev, 0x48, reg48 | u_flag);
  96. if (speed >= XFER_UDMA_5)
  97. pci_write_config_byte(dev, 0x55, (u8) reg55|w_flag);
  98. else
  99. pci_write_config_byte(dev, 0x55, (u8) reg55 & ~w_flag);
  100. if ((reg4a & a_speed) != u_speed)
  101. pci_write_config_word(dev, 0x4a, (reg4a & ~a_speed) | u_speed);
  102. if (speed > XFER_UDMA_2) {
  103. if (!(reg54 & v_flag))
  104. pci_write_config_byte(dev, 0x54, reg54 | v_flag);
  105. } else
  106. pci_write_config_byte(dev, 0x54, reg54 & ~v_flag);
  107. } else {
  108. const u8 mwdma_to_pio[] = { 0, 3, 4 };
  109. if (reg48 & u_flag)
  110. pci_write_config_byte(dev, 0x48, reg48 & ~u_flag);
  111. if (reg4a & a_speed)
  112. pci_write_config_word(dev, 0x4a, reg4a & ~a_speed);
  113. if (reg54 & v_flag)
  114. pci_write_config_byte(dev, 0x54, reg54 & ~v_flag);
  115. if (reg55 & w_flag)
  116. pci_write_config_byte(dev, 0x55, (u8) reg55 & ~w_flag);
  117. if (speed >= XFER_MW_DMA_0)
  118. drive->pio_mode =
  119. mwdma_to_pio[speed - XFER_MW_DMA_0] + XFER_PIO_0;
  120. else
  121. drive->pio_mode = XFER_PIO_2; /* for SWDMA2 */
  122. it8213_set_pio_mode(hwif, drive);
  123. }
  124. }
  125. static u8 it8213_cable_detect(ide_hwif_t *hwif)
  126. {
  127. struct pci_dev *dev = to_pci_dev(hwif->dev);
  128. u8 reg42h = 0;
  129. pci_read_config_byte(dev, 0x42, &reg42h);
  130. return (reg42h & 0x02) ? ATA_CBL_PATA40 : ATA_CBL_PATA80;
  131. }
  132. static const struct ide_port_ops it8213_port_ops = {
  133. .set_pio_mode = it8213_set_pio_mode,
  134. .set_dma_mode = it8213_set_dma_mode,
  135. .cable_detect = it8213_cable_detect,
  136. };
  137. static const struct ide_port_info it8213_chipset = {
  138. .name = DRV_NAME,
  139. .enablebits = { {0x41, 0x80, 0x80} },
  140. .port_ops = &it8213_port_ops,
  141. .host_flags = IDE_HFLAG_SINGLE,
  142. .pio_mask = ATA_PIO4,
  143. .swdma_mask = ATA_SWDMA2_ONLY,
  144. .mwdma_mask = ATA_MWDMA12_ONLY,
  145. .udma_mask = ATA_UDMA6,
  146. };
  147. /**
  148. * it8213_init_one - pci layer discovery entry
  149. * @dev: PCI device
  150. * @id: ident table entry
  151. *
  152. * Called by the PCI code when it finds an ITE8213 controller. As
  153. * this device follows the standard interfaces we can use the
  154. * standard helper functions to do almost all the work for us.
  155. */
  156. static int it8213_init_one(struct pci_dev *dev, const struct pci_device_id *id)
  157. {
  158. return ide_pci_init_one(dev, &it8213_chipset, NULL);
  159. }
  160. static const struct pci_device_id it8213_pci_tbl[] = {
  161. { PCI_VDEVICE(ITE, PCI_DEVICE_ID_ITE_8213), 0 },
  162. { 0, },
  163. };
  164. MODULE_DEVICE_TABLE(pci, it8213_pci_tbl);
  165. static struct pci_driver it8213_pci_driver = {
  166. .name = "ITE8213_IDE",
  167. .id_table = it8213_pci_tbl,
  168. .probe = it8213_init_one,
  169. .remove = ide_pci_remove,
  170. .suspend = ide_pci_suspend,
  171. .resume = ide_pci_resume,
  172. };
  173. static int __init it8213_ide_init(void)
  174. {
  175. return ide_pci_register_driver(&it8213_pci_driver);
  176. }
  177. static void __exit it8213_ide_exit(void)
  178. {
  179. pci_unregister_driver(&it8213_pci_driver);
  180. }
  181. module_init(it8213_ide_init);
  182. module_exit(it8213_ide_exit);
  183. MODULE_AUTHOR("Jack Lee, Alan Cox");
  184. MODULE_DESCRIPTION("PCI driver module for the ITE 8213");
  185. MODULE_LICENSE("GPL");