atiixp.c 8.1 KB

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