atiixp.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. * @speed: 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, const u8 speed)
  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 pio;
  152. if (speed >= XFER_PIO_0 && speed <= XFER_PIO_4) {
  153. atiixp_tune_pio(drive, speed - XFER_PIO_0);
  154. return ide_config_drive_speed(drive, speed);
  155. }
  156. spin_lock_irqsave(&atiixp_lock, flags);
  157. save_mdma_mode[drive->dn] = 0;
  158. if (speed >= XFER_UDMA_0) {
  159. pci_read_config_word(dev, ATIIXP_IDE_UDMA_MODE, &tmp16);
  160. tmp16 &= ~(0x07 << (drive->dn * 4));
  161. tmp16 |= ((speed & 0x07) << (drive->dn * 4));
  162. pci_write_config_word(dev, ATIIXP_IDE_UDMA_MODE, tmp16);
  163. } else {
  164. if ((speed >= XFER_MW_DMA_0) && (speed <= XFER_MW_DMA_2)) {
  165. save_mdma_mode[drive->dn] = speed;
  166. pci_read_config_dword(dev, ATIIXP_IDE_MDMA_TIMING, &tmp32);
  167. tmp32 &= ~(0xff << timing_shift);
  168. tmp32 |= (mdma_timing[speed & 0x03].recover_width << timing_shift) |
  169. (mdma_timing[speed & 0x03].command_width << (timing_shift + 4));
  170. pci_write_config_dword(dev, ATIIXP_IDE_MDMA_TIMING, tmp32);
  171. }
  172. }
  173. spin_unlock_irqrestore(&atiixp_lock, flags);
  174. if (speed >= XFER_SW_DMA_0)
  175. pio = atiixp_dma_2_pio(speed);
  176. else
  177. pio = speed - XFER_PIO_0;
  178. atiixp_tune_pio(drive, pio);
  179. return ide_config_drive_speed(drive, speed);
  180. }
  181. /**
  182. * atiixp_dma_check - set up an IDE device
  183. * @drive: IDE drive to configure
  184. *
  185. * Set up the ATIIXP interface for the best available speed on this
  186. * interface, preferring DMA to PIO.
  187. */
  188. static int atiixp_dma_check(ide_drive_t *drive)
  189. {
  190. drive->init_speed = 0;
  191. if (ide_tune_dma(drive))
  192. return 0;
  193. if (ide_use_fast_pio(drive))
  194. atiixp_tuneproc(drive, 255);
  195. return -1;
  196. }
  197. /**
  198. * init_hwif_atiixp - fill in the hwif for the ATIIXP
  199. * @hwif: IDE interface
  200. *
  201. * Set up the ide_hwif_t for the ATIIXP interface according to the
  202. * capabilities of the hardware.
  203. */
  204. static void __devinit init_hwif_atiixp(ide_hwif_t *hwif)
  205. {
  206. u8 udma_mode = 0;
  207. u8 ch = hwif->channel;
  208. struct pci_dev *pdev = hwif->pci_dev;
  209. if (!hwif->irq)
  210. hwif->irq = ch ? 15 : 14;
  211. hwif->autodma = 0;
  212. hwif->tuneproc = &atiixp_tuneproc;
  213. hwif->speedproc = &atiixp_speedproc;
  214. hwif->drives[0].autotune = 1;
  215. hwif->drives[1].autotune = 1;
  216. if (!hwif->dma_base)
  217. return;
  218. hwif->atapi_dma = 1;
  219. hwif->ultra_mask = 0x3f;
  220. hwif->mwdma_mask = 0x06;
  221. hwif->swdma_mask = 0x04;
  222. pci_read_config_byte(pdev, ATIIXP_IDE_UDMA_MODE + ch, &udma_mode);
  223. if ((udma_mode & 0x07) >= 0x04 || (udma_mode & 0x70) >= 0x40)
  224. hwif->cbl = ATA_CBL_PATA80;
  225. else
  226. hwif->cbl = ATA_CBL_PATA40;
  227. hwif->dma_host_on = &atiixp_dma_host_on;
  228. hwif->dma_host_off = &atiixp_dma_host_off;
  229. hwif->ide_dma_check = &atiixp_dma_check;
  230. if (!noautodma)
  231. hwif->autodma = 1;
  232. hwif->drives[1].autodma = hwif->autodma;
  233. hwif->drives[0].autodma = hwif->autodma;
  234. }
  235. static ide_pci_device_t atiixp_pci_info[] __devinitdata = {
  236. { /* 0 */
  237. .name = "ATIIXP",
  238. .init_hwif = init_hwif_atiixp,
  239. .autodma = AUTODMA,
  240. .enablebits = {{0x48,0x01,0x00}, {0x48,0x08,0x00}},
  241. .bootable = ON_BOARD,
  242. .pio_mask = ATA_PIO4,
  243. },{ /* 1 */
  244. .name = "SB600_PATA",
  245. .init_hwif = init_hwif_atiixp,
  246. .autodma = AUTODMA,
  247. .enablebits = {{0x48,0x01,0x00}, {0x00,0x00,0x00}},
  248. .bootable = ON_BOARD,
  249. .host_flags = IDE_HFLAG_SINGLE,
  250. .pio_mask = ATA_PIO4,
  251. },
  252. };
  253. /**
  254. * atiixp_init_one - called when a ATIIXP is found
  255. * @dev: the atiixp device
  256. * @id: the matching pci id
  257. *
  258. * Called when the PCI registration layer (or the IDE initialization)
  259. * finds a device matching our IDE device tables.
  260. */
  261. static int __devinit atiixp_init_one(struct pci_dev *dev, const struct pci_device_id *id)
  262. {
  263. return ide_setup_pci_device(dev, &atiixp_pci_info[id->driver_data]);
  264. }
  265. static struct pci_device_id atiixp_pci_tbl[] = {
  266. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP200_IDE, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  267. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP300_IDE, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  268. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP400_IDE, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  269. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP600_IDE, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 1},
  270. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP700_IDE, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  271. { 0, },
  272. };
  273. MODULE_DEVICE_TABLE(pci, atiixp_pci_tbl);
  274. static struct pci_driver driver = {
  275. .name = "ATIIXP_IDE",
  276. .id_table = atiixp_pci_tbl,
  277. .probe = atiixp_init_one,
  278. };
  279. static int __init atiixp_ide_init(void)
  280. {
  281. return ide_pci_register_driver(&driver);
  282. }
  283. module_init(atiixp_ide_init);
  284. MODULE_AUTHOR("HUI YU");
  285. MODULE_DESCRIPTION("PCI driver module for ATI IXP IDE");
  286. MODULE_LICENSE("GPL");