it8172.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. *
  3. * BRIEF MODULE DESCRIPTION
  4. * IT8172 IDE controller support
  5. *
  6. * Copyright 2000 MontaVista Software Inc.
  7. * Author: MontaVista Software, Inc.
  8. * stevel@mvista.com or source@mvista.com
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the
  12. * Free Software Foundation; either version 2 of the License, or (at your
  13. * option) any later version.
  14. *
  15. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  16. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  17. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
  18. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  21. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  22. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  24. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. *
  26. * You should have received a copy of the GNU General Public License along
  27. * with this program; if not, write to the Free Software Foundation, Inc.,
  28. * 675 Mass Ave, Cambridge, MA 02139, USA.
  29. */
  30. #include <linux/config.h>
  31. #include <linux/module.h>
  32. #include <linux/types.h>
  33. #include <linux/kernel.h>
  34. #include <linux/ioport.h>
  35. #include <linux/pci.h>
  36. #include <linux/hdreg.h>
  37. #include <linux/ide.h>
  38. #include <linux/delay.h>
  39. #include <linux/init.h>
  40. #include <asm/io.h>
  41. #include <asm/it8172/it8172_int.h>
  42. /*
  43. * Prototypes
  44. */
  45. static u8 it8172_ratemask (ide_drive_t *drive)
  46. {
  47. return 1;
  48. }
  49. static void it8172_tune_drive (ide_drive_t *drive, u8 pio)
  50. {
  51. ide_hwif_t *hwif = HWIF(drive);
  52. struct pci_dev *dev = hwif->pci_dev;
  53. int is_slave = (&hwif->drives[1] == drive);
  54. unsigned long flags;
  55. u16 drive_enables;
  56. u32 drive_timing;
  57. pio = ide_get_best_pio_mode(drive, pio, 4, NULL);
  58. spin_lock_irqsave(&ide_lock, flags);
  59. pci_read_config_word(dev, 0x40, &drive_enables);
  60. pci_read_config_dword(dev, 0x44, &drive_timing);
  61. /*
  62. * FIX! The DIOR/DIOW pulse width and recovery times in port 0x44
  63. * are being left at the default values of 8 PCI clocks (242 nsec
  64. * for a 33 MHz clock). These can be safely shortened at higher
  65. * PIO modes. The DIOR/DIOW pulse width and recovery times only
  66. * apply to PIO modes, not to the DMA modes.
  67. */
  68. /*
  69. * Enable port 0x44. The IT8172G spec is confused; it calls
  70. * this register the "Slave IDE Timing Register", but in fact,
  71. * it controls timing for both master and slave drives.
  72. */
  73. drive_enables |= 0x4000;
  74. if (is_slave) {
  75. drive_enables &= 0xc006;
  76. if (pio > 1)
  77. /* enable prefetch and IORDY sample-point */
  78. drive_enables |= 0x0060;
  79. } else {
  80. drive_enables &= 0xc060;
  81. if (pio > 1)
  82. /* enable prefetch and IORDY sample-point */
  83. drive_enables |= 0x0006;
  84. }
  85. pci_write_config_word(dev, 0x40, drive_enables);
  86. spin_unlock_irqrestore(&ide_lock, flags);
  87. }
  88. static u8 it8172_dma_2_pio (u8 xfer_rate)
  89. {
  90. switch(xfer_rate) {
  91. case XFER_UDMA_5:
  92. case XFER_UDMA_4:
  93. case XFER_UDMA_3:
  94. case XFER_UDMA_2:
  95. case XFER_UDMA_1:
  96. case XFER_UDMA_0:
  97. case XFER_MW_DMA_2:
  98. case XFER_PIO_4:
  99. return 4;
  100. case XFER_MW_DMA_1:
  101. case XFER_PIO_3:
  102. return 3;
  103. case XFER_SW_DMA_2:
  104. case XFER_PIO_2:
  105. return 2;
  106. case XFER_MW_DMA_0:
  107. case XFER_SW_DMA_1:
  108. case XFER_SW_DMA_0:
  109. case XFER_PIO_1:
  110. case XFER_PIO_0:
  111. case XFER_PIO_SLOW:
  112. default:
  113. return 0;
  114. }
  115. }
  116. static int it8172_tune_chipset (ide_drive_t *drive, u8 xferspeed)
  117. {
  118. ide_hwif_t *hwif = HWIF(drive);
  119. struct pci_dev *dev = hwif->pci_dev;
  120. u8 speed = ide_rate_filter(it8172_ratemask(drive), xferspeed);
  121. int a_speed = 3 << (drive->dn * 4);
  122. int u_flag = 1 << drive->dn;
  123. int u_speed = 0;
  124. u8 reg48, reg4a;
  125. pci_read_config_byte(dev, 0x48, &reg48);
  126. pci_read_config_byte(dev, 0x4a, &reg4a);
  127. /*
  128. * Setting the DMA cycle time to 2 or 3 PCI clocks (60 and 91 nsec
  129. * at 33 MHz PCI clock) seems to cause BadCRC errors during DMA
  130. * transfers on some drives, even though both numbers meet the minimum
  131. * ATAPI-4 spec of 73 and 54 nsec for UDMA 1 and 2 respectively.
  132. * So the faster times are just commented out here. The good news is
  133. * that the slower cycle time has very little affect on transfer
  134. * performance.
  135. */
  136. switch(speed) {
  137. case XFER_UDMA_4:
  138. case XFER_UDMA_2: //u_speed = 2 << (drive->dn * 4); break;
  139. case XFER_UDMA_5:
  140. case XFER_UDMA_3:
  141. case XFER_UDMA_1: //u_speed = 1 << (drive->dn * 4); break;
  142. case XFER_UDMA_0: u_speed = 0 << (drive->dn * 4); break;
  143. case XFER_MW_DMA_2:
  144. case XFER_MW_DMA_1:
  145. case XFER_MW_DMA_0:
  146. case XFER_SW_DMA_2: break;
  147. case XFER_PIO_4:
  148. case XFER_PIO_3:
  149. case XFER_PIO_2:
  150. case XFER_PIO_0: break;
  151. default: return -1;
  152. }
  153. if (speed >= XFER_UDMA_0) {
  154. pci_write_config_byte(dev, 0x48, reg48 | u_flag);
  155. reg4a &= ~a_speed;
  156. pci_write_config_byte(dev, 0x4a, reg4a | u_speed);
  157. } else {
  158. pci_write_config_byte(dev, 0x48, reg48 & ~u_flag);
  159. pci_write_config_byte(dev, 0x4a, reg4a & ~a_speed);
  160. }
  161. it8172_tune_drive(drive, it8172_dma_2_pio(speed));
  162. return (ide_config_drive_speed(drive, speed));
  163. }
  164. static int it8172_config_chipset_for_dma (ide_drive_t *drive)
  165. {
  166. u8 speed = ide_dma_speed(drive, it8172_ratemask(drive));
  167. if (!(speed)) {
  168. u8 tspeed = ide_get_best_pio_mode(drive, 255, 4, NULL);
  169. speed = it8172_dma_2_pio(XFER_PIO_0 + tspeed);
  170. }
  171. (void) it8172_tune_chipset(drive, speed);
  172. return ide_dma_enable(drive);
  173. }
  174. static int it8172_config_drive_xfer_rate (ide_drive_t *drive)
  175. {
  176. ide_hwif_t *hwif = HWIF(drive);
  177. struct hd_driveid *id = drive->id;
  178. drive->init_speed = 0;
  179. if (id && (id->capability & 1) && drive->autodma) {
  180. if (ide_use_dma(drive)) {
  181. if (it8172_config_chipset_for_dma(drive))
  182. return hwif->ide_dma_on(drive);
  183. }
  184. goto fast_ata_pio;
  185. } else if ((id->capability & 8) || (id->field_valid & 2)) {
  186. fast_ata_pio:
  187. it8172_tune_drive(drive, 5);
  188. return hwif->ide_dma_off_quietly(drive);
  189. }
  190. /* IORDY not supported */
  191. return 0;
  192. }
  193. static unsigned int __devinit init_chipset_it8172 (struct pci_dev *dev, const char *name)
  194. {
  195. unsigned char progif;
  196. /*
  197. * Place both IDE interfaces into PCI "native" mode
  198. */
  199. pci_read_config_byte(dev, PCI_CLASS_PROG, &progif);
  200. pci_write_config_byte(dev, PCI_CLASS_PROG, progif | 0x05);
  201. return IT8172_IDE_IRQ;
  202. }
  203. static void __devinit init_hwif_it8172 (ide_hwif_t *hwif)
  204. {
  205. struct pci_dev* dev = hwif->pci_dev;
  206. unsigned long cmdBase, ctrlBase;
  207. hwif->autodma = 0;
  208. hwif->tuneproc = &it8172_tune_drive;
  209. hwif->speedproc = &it8172_tune_chipset;
  210. cmdBase = dev->resource[0].start;
  211. ctrlBase = dev->resource[1].start;
  212. ide_init_hwif_ports(&hwif->hw, cmdBase, ctrlBase | 2, NULL);
  213. memcpy(hwif->io_ports, hwif->hw.io_ports, sizeof(hwif->io_ports));
  214. hwif->noprobe = 0;
  215. if (!hwif->dma_base) {
  216. hwif->drives[0].autotune = 1;
  217. hwif->drives[1].autotune = 1;
  218. return;
  219. }
  220. hwif->atapi_dma = 1;
  221. hwif->ultra_mask = 0x07;
  222. hwif->mwdma_mask = 0x06;
  223. hwif->swdma_mask = 0x04;
  224. hwif->ide_dma_check = &it8172_config_drive_xfer_rate;
  225. if (!noautodma)
  226. hwif->autodma = 1;
  227. hwif->drives[0].autodma = hwif->autodma;
  228. hwif->drives[1].autodma = hwif->autodma;
  229. }
  230. static ide_pci_device_t it8172_chipsets[] __devinitdata = {
  231. { /* 0 */
  232. .name = "IT8172G",
  233. .init_chipset = init_chipset_it8172,
  234. .init_hwif = init_hwif_it8172,
  235. .channels = 2,
  236. .autodma = AUTODMA,
  237. .enablebits = {{0x00,0x00,0x00}, {0x40,0x00,0x01}},
  238. .bootable = ON_BOARD,
  239. }
  240. };
  241. static int __devinit it8172_init_one(struct pci_dev *dev, const struct pci_device_id *id)
  242. {
  243. if ((!(PCI_FUNC(dev->devfn) & 1) ||
  244. (!((dev->class >> 8) == PCI_CLASS_STORAGE_IDE))))
  245. return -ENODEV; /* IT8172 is more than an IDE controller */
  246. return ide_setup_pci_device(dev, &it8172_chipsets[id->driver_data]);
  247. }
  248. static struct pci_device_id it8172_pci_tbl[] = {
  249. { PCI_VENDOR_ID_ITE, PCI_DEVICE_ID_ITE_IT8172G, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  250. { 0, },
  251. };
  252. MODULE_DEVICE_TABLE(pci, it8172_pci_tbl);
  253. static struct pci_driver driver = {
  254. .name = "IT8172_IDE",
  255. .id_table = it8172_pci_tbl,
  256. .probe = it8172_init_one,
  257. };
  258. static int it8172_ide_init(void)
  259. {
  260. return ide_pci_register_driver(&driver);
  261. }
  262. module_init(it8172_ide_init);
  263. MODULE_AUTHOR("SteveL@mvista.com");
  264. MODULE_DESCRIPTION("PCI driver module for ITE 8172 IDE");
  265. MODULE_LICENSE("GPL");