atiixp.c 9.7 KB

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