it8213.c 5.6 KB

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