atiixp.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /*
  2. * linux/drivers/ide/pci/atiixp.c Version 0.01-bart2 Feb. 26, 2004
  3. *
  4. * Copyright (C) 2003 ATI Inc. <hyu@ati.com>
  5. * Copyright (C) 2004 Bartlomiej Zolnierkiewicz
  6. *
  7. */
  8. #include <linux/config.h>
  9. #include <linux/types.h>
  10. #include <linux/module.h>
  11. #include <linux/kernel.h>
  12. #include <linux/ioport.h>
  13. #include <linux/pci.h>
  14. #include <linux/hdreg.h>
  15. #include <linux/ide.h>
  16. #include <linux/delay.h>
  17. #include <linux/init.h>
  18. #include <asm/io.h>
  19. #define ATIIXP_IDE_PIO_TIMING 0x40
  20. #define ATIIXP_IDE_MDMA_TIMING 0x44
  21. #define ATIIXP_IDE_PIO_CONTROL 0x48
  22. #define ATIIXP_IDE_PIO_MODE 0x4a
  23. #define ATIIXP_IDE_UDMA_CONTROL 0x54
  24. #define ATIIXP_IDE_UDMA_MODE 0x56
  25. typedef struct {
  26. u8 command_width;
  27. u8 recover_width;
  28. } atiixp_ide_timing;
  29. static atiixp_ide_timing pio_timing[] = {
  30. { 0x05, 0x0d },
  31. { 0x04, 0x07 },
  32. { 0x03, 0x04 },
  33. { 0x02, 0x02 },
  34. { 0x02, 0x00 },
  35. };
  36. static atiixp_ide_timing mdma_timing[] = {
  37. { 0x07, 0x07 },
  38. { 0x02, 0x01 },
  39. { 0x02, 0x00 },
  40. };
  41. static int save_mdma_mode[4];
  42. /**
  43. * atiixp_ratemask - compute rate mask for ATIIXP IDE
  44. * @drive: IDE drive to compute for
  45. *
  46. * Returns the available modes for the ATIIXP IDE controller.
  47. */
  48. static u8 atiixp_ratemask(ide_drive_t *drive)
  49. {
  50. u8 mode = 3;
  51. if (!eighty_ninty_three(drive))
  52. mode = min(mode, (u8)1);
  53. return mode;
  54. }
  55. /**
  56. * atiixp_dma_2_pio - return the PIO mode matching DMA
  57. * @xfer_rate: transfer speed
  58. *
  59. * Returns the nearest equivalent PIO timing for the PIO or DMA
  60. * mode requested by the controller.
  61. */
  62. static u8 atiixp_dma_2_pio(u8 xfer_rate) {
  63. switch(xfer_rate) {
  64. case XFER_UDMA_6:
  65. case XFER_UDMA_5:
  66. case XFER_UDMA_4:
  67. case XFER_UDMA_3:
  68. case XFER_UDMA_2:
  69. case XFER_UDMA_1:
  70. case XFER_UDMA_0:
  71. case XFER_MW_DMA_2:
  72. case XFER_PIO_4:
  73. return 4;
  74. case XFER_MW_DMA_1:
  75. case XFER_PIO_3:
  76. return 3;
  77. case XFER_SW_DMA_2:
  78. case XFER_PIO_2:
  79. return 2;
  80. case XFER_MW_DMA_0:
  81. case XFER_SW_DMA_1:
  82. case XFER_SW_DMA_0:
  83. case XFER_PIO_1:
  84. case XFER_PIO_0:
  85. case XFER_PIO_SLOW:
  86. default:
  87. return 0;
  88. }
  89. }
  90. static int atiixp_ide_dma_host_on(ide_drive_t *drive)
  91. {
  92. struct pci_dev *dev = drive->hwif->pci_dev;
  93. unsigned long flags;
  94. u16 tmp16;
  95. spin_lock_irqsave(&ide_lock, flags);
  96. pci_read_config_word(dev, ATIIXP_IDE_UDMA_CONTROL, &tmp16);
  97. if (save_mdma_mode[drive->dn])
  98. tmp16 &= ~(1 << drive->dn);
  99. else
  100. tmp16 |= (1 << drive->dn);
  101. pci_write_config_word(dev, ATIIXP_IDE_UDMA_CONTROL, tmp16);
  102. spin_unlock_irqrestore(&ide_lock, flags);
  103. return __ide_dma_host_on(drive);
  104. }
  105. static int atiixp_ide_dma_host_off(ide_drive_t *drive)
  106. {
  107. struct pci_dev *dev = drive->hwif->pci_dev;
  108. unsigned long flags;
  109. u16 tmp16;
  110. spin_lock_irqsave(&ide_lock, flags);
  111. pci_read_config_word(dev, ATIIXP_IDE_UDMA_CONTROL, &tmp16);
  112. tmp16 &= ~(1 << drive->dn);
  113. pci_write_config_word(dev, ATIIXP_IDE_UDMA_CONTROL, tmp16);
  114. spin_unlock_irqrestore(&ide_lock, flags);
  115. return __ide_dma_host_off(drive);
  116. }
  117. /**
  118. * atiixp_tune_drive - tune a drive attached to a ATIIXP
  119. * @drive: drive to tune
  120. * @pio: desired PIO mode
  121. *
  122. * Set the interface PIO mode.
  123. */
  124. static void atiixp_tuneproc(ide_drive_t *drive, u8 pio)
  125. {
  126. struct pci_dev *dev = drive->hwif->pci_dev;
  127. unsigned long flags;
  128. int timing_shift = (drive->dn & 2) ? 16 : 0 + (drive->dn & 1) ? 0 : 8;
  129. u32 pio_timing_data;
  130. u16 pio_mode_data;
  131. spin_lock_irqsave(&ide_lock, flags);
  132. pci_read_config_word(dev, ATIIXP_IDE_PIO_MODE, &pio_mode_data);
  133. pio_mode_data &= ~(0x07 << (drive->dn * 4));
  134. pio_mode_data |= (pio << (drive->dn * 4));
  135. pci_write_config_word(dev, ATIIXP_IDE_PIO_MODE, pio_mode_data);
  136. pci_read_config_dword(dev, ATIIXP_IDE_PIO_TIMING, &pio_timing_data);
  137. pio_timing_data &= ~(0xff << timing_shift);
  138. pio_timing_data |= (pio_timing[pio].recover_width << timing_shift) |
  139. (pio_timing[pio].command_width << (timing_shift + 4));
  140. pci_write_config_dword(dev, ATIIXP_IDE_PIO_TIMING, pio_timing_data);
  141. spin_unlock_irqrestore(&ide_lock, flags);
  142. }
  143. /**
  144. * atiixp_tune_chipset - tune a ATIIXP interface
  145. * @drive: IDE drive to tune
  146. * @xferspeed: speed to configure
  147. *
  148. * Set a ATIIXP interface channel to the desired speeds. This involves
  149. * requires the right timing data into the ATIIXP configuration space
  150. * then setting the drive parameters appropriately
  151. */
  152. static int atiixp_speedproc(ide_drive_t *drive, u8 xferspeed)
  153. {
  154. struct pci_dev *dev = drive->hwif->pci_dev;
  155. unsigned long flags;
  156. int timing_shift = (drive->dn & 2) ? 16 : 0 + (drive->dn & 1) ? 0 : 8;
  157. u32 tmp32;
  158. u16 tmp16;
  159. u8 speed, pio;
  160. speed = ide_rate_filter(atiixp_ratemask(drive), xferspeed);
  161. spin_lock_irqsave(&ide_lock, flags);
  162. save_mdma_mode[drive->dn] = 0;
  163. if (speed >= XFER_UDMA_0) {
  164. pci_read_config_word(dev, ATIIXP_IDE_UDMA_MODE, &tmp16);
  165. tmp16 &= ~(0x07 << (drive->dn * 4));
  166. tmp16 |= ((speed & 0x07) << (drive->dn * 4));
  167. pci_write_config_word(dev, ATIIXP_IDE_UDMA_MODE, tmp16);
  168. } else {
  169. if ((speed >= XFER_MW_DMA_0) && (speed <= XFER_MW_DMA_2)) {
  170. save_mdma_mode[drive->dn] = speed;
  171. pci_read_config_dword(dev, ATIIXP_IDE_MDMA_TIMING, &tmp32);
  172. tmp32 &= ~(0xff << timing_shift);
  173. tmp32 |= (mdma_timing[speed & 0x03].recover_width << timing_shift) |
  174. (mdma_timing[speed & 0x03].command_width << (timing_shift + 4));
  175. pci_write_config_dword(dev, ATIIXP_IDE_MDMA_TIMING, tmp32);
  176. }
  177. }
  178. spin_unlock_irqrestore(&ide_lock, flags);
  179. if (speed >= XFER_SW_DMA_0)
  180. pio = atiixp_dma_2_pio(speed);
  181. else
  182. pio = speed - XFER_PIO_0;
  183. atiixp_tuneproc(drive, pio);
  184. return ide_config_drive_speed(drive, speed);
  185. }
  186. /**
  187. * atiixp_config_drive_for_dma - configure drive for DMA
  188. * @drive: IDE drive to configure
  189. *
  190. * Set up a ATIIXP interface channel for the best available speed.
  191. * We prefer UDMA if it is available and then MWDMA. If DMA is
  192. * not available we switch to PIO and return 0.
  193. */
  194. static int atiixp_config_drive_for_dma(ide_drive_t *drive)
  195. {
  196. u8 speed = ide_dma_speed(drive, atiixp_ratemask(drive));
  197. /* If no DMA speed was available then disable DMA and use PIO. */
  198. if (!speed) {
  199. u8 tspeed = ide_get_best_pio_mode(drive, 255, 5, NULL);
  200. speed = atiixp_dma_2_pio(XFER_PIO_0 + tspeed) + XFER_PIO_0;
  201. }
  202. (void) atiixp_speedproc(drive, speed);
  203. return ide_dma_enable(drive);
  204. }
  205. /**
  206. * atiixp_dma_check - set up an IDE device
  207. * @drive: IDE drive to configure
  208. *
  209. * Set up the ATIIXP interface for the best available speed on this
  210. * interface, preferring DMA to PIO.
  211. */
  212. static int atiixp_dma_check(ide_drive_t *drive)
  213. {
  214. ide_hwif_t *hwif = HWIF(drive);
  215. struct hd_driveid *id = drive->id;
  216. u8 tspeed, speed;
  217. drive->init_speed = 0;
  218. if ((id->capability & 1) && drive->autodma) {
  219. if (ide_use_dma(drive)) {
  220. if (atiixp_config_drive_for_dma(drive))
  221. return hwif->ide_dma_on(drive);
  222. }
  223. goto fast_ata_pio;
  224. } else if ((id->capability & 8) || (id->field_valid & 2)) {
  225. fast_ata_pio:
  226. tspeed = ide_get_best_pio_mode(drive, 255, 5, NULL);
  227. speed = atiixp_dma_2_pio(XFER_PIO_0 + tspeed) + XFER_PIO_0;
  228. hwif->speedproc(drive, speed);
  229. return hwif->ide_dma_off_quietly(drive);
  230. }
  231. /* IORDY not supported */
  232. return 0;
  233. }
  234. /**
  235. * init_hwif_atiixp - fill in the hwif for the ATIIXP
  236. * @hwif: IDE interface
  237. *
  238. * Set up the ide_hwif_t for the ATIIXP interface according to the
  239. * capabilities of the hardware.
  240. */
  241. static void __devinit init_hwif_atiixp(ide_hwif_t *hwif)
  242. {
  243. if (!hwif->irq)
  244. hwif->irq = hwif->channel ? 15 : 14;
  245. hwif->autodma = 0;
  246. hwif->tuneproc = &atiixp_tuneproc;
  247. hwif->speedproc = &atiixp_speedproc;
  248. hwif->drives[0].autotune = 1;
  249. hwif->drives[1].autotune = 1;
  250. if (!hwif->dma_base)
  251. return;
  252. hwif->atapi_dma = 1;
  253. hwif->ultra_mask = 0x3f;
  254. hwif->mwdma_mask = 0x06;
  255. hwif->swdma_mask = 0x04;
  256. /* FIXME: proper cable detection needed */
  257. hwif->udma_four = 1;
  258. hwif->ide_dma_host_on = &atiixp_ide_dma_host_on;
  259. hwif->ide_dma_host_off = &atiixp_ide_dma_host_off;
  260. hwif->ide_dma_check = &atiixp_dma_check;
  261. if (!noautodma)
  262. hwif->autodma = 1;
  263. hwif->drives[1].autodma = hwif->autodma;
  264. hwif->drives[0].autodma = hwif->autodma;
  265. }
  266. static ide_pci_device_t atiixp_pci_info[] __devinitdata = {
  267. { /* 0 */
  268. .name = "ATIIXP",
  269. .init_hwif = init_hwif_atiixp,
  270. .channels = 2,
  271. .autodma = AUTODMA,
  272. .enablebits = {{0x48,0x01,0x00}, {0x48,0x08,0x00}},
  273. .bootable = ON_BOARD,
  274. }
  275. };
  276. /**
  277. * atiixp_init_one - called when a ATIIXP is found
  278. * @dev: the atiixp device
  279. * @id: the matching pci id
  280. *
  281. * Called when the PCI registration layer (or the IDE initialization)
  282. * finds a device matching our IDE device tables.
  283. */
  284. static int __devinit atiixp_init_one(struct pci_dev *dev, const struct pci_device_id *id)
  285. {
  286. return ide_setup_pci_device(dev, &atiixp_pci_info[id->driver_data]);
  287. }
  288. static struct pci_device_id atiixp_pci_tbl[] = {
  289. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP200_IDE, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  290. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP300_IDE, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  291. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP400_IDE, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  292. { 0, },
  293. };
  294. MODULE_DEVICE_TABLE(pci, atiixp_pci_tbl);
  295. static struct pci_driver driver = {
  296. .name = "ATIIXP_IDE",
  297. .id_table = atiixp_pci_tbl,
  298. .probe = atiixp_init_one,
  299. };
  300. static int atiixp_ide_init(void)
  301. {
  302. return ide_pci_register_driver(&driver);
  303. }
  304. module_init(atiixp_ide_init);
  305. MODULE_AUTHOR("HUI YU");
  306. MODULE_DESCRIPTION("PCI driver module for ATI IXP IDE");
  307. MODULE_LICENSE("GPL");