atiixp.c 8.6 KB

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