ide-dma-sff.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. #include <linux/types.h>
  2. #include <linux/kernel.h>
  3. #include <linux/ide.h>
  4. #include <linux/scatterlist.h>
  5. #include <linux/dma-mapping.h>
  6. #include <linux/io.h>
  7. /**
  8. * config_drive_for_dma - attempt to activate IDE DMA
  9. * @drive: the drive to place in DMA mode
  10. *
  11. * If the drive supports at least mode 2 DMA or UDMA of any kind
  12. * then attempt to place it into DMA mode. Drives that are known to
  13. * support DMA but predate the DMA properties or that are known
  14. * to have DMA handling bugs are also set up appropriately based
  15. * on the good/bad drive lists.
  16. */
  17. int config_drive_for_dma(ide_drive_t *drive)
  18. {
  19. ide_hwif_t *hwif = drive->hwif;
  20. u16 *id = drive->id;
  21. if (drive->media != ide_disk) {
  22. if (hwif->host_flags & IDE_HFLAG_NO_ATAPI_DMA)
  23. return 0;
  24. }
  25. /*
  26. * Enable DMA on any drive that has
  27. * UltraDMA (mode 0/1/2/3/4/5/6) enabled
  28. */
  29. if ((id[ATA_ID_FIELD_VALID] & 4) &&
  30. ((id[ATA_ID_UDMA_MODES] >> 8) & 0x7f))
  31. return 1;
  32. /*
  33. * Enable DMA on any drive that has mode2 DMA
  34. * (multi or single) enabled
  35. */
  36. if ((id[ATA_ID_MWDMA_MODES] & 0x404) == 0x404 ||
  37. (id[ATA_ID_SWDMA_MODES] & 0x404) == 0x404)
  38. return 1;
  39. /* Consult the list of known "good" drives */
  40. if (ide_dma_good_drive(drive))
  41. return 1;
  42. return 0;
  43. }
  44. u8 ide_dma_sff_read_status(ide_hwif_t *hwif)
  45. {
  46. unsigned long addr = hwif->dma_base + ATA_DMA_STATUS;
  47. if (hwif->host_flags & IDE_HFLAG_MMIO)
  48. return readb((void __iomem *)addr);
  49. else
  50. return inb(addr);
  51. }
  52. EXPORT_SYMBOL_GPL(ide_dma_sff_read_status);
  53. static void ide_dma_sff_write_status(ide_hwif_t *hwif, u8 val)
  54. {
  55. unsigned long addr = hwif->dma_base + ATA_DMA_STATUS;
  56. if (hwif->host_flags & IDE_HFLAG_MMIO)
  57. writeb(val, (void __iomem *)addr);
  58. else
  59. outb(val, addr);
  60. }
  61. /**
  62. * ide_dma_host_set - Enable/disable DMA on a host
  63. * @drive: drive to control
  64. *
  65. * Enable/disable DMA on an IDE controller following generic
  66. * bus-mastering IDE controller behaviour.
  67. */
  68. void ide_dma_host_set(ide_drive_t *drive, int on)
  69. {
  70. ide_hwif_t *hwif = drive->hwif;
  71. u8 unit = drive->dn & 1;
  72. u8 dma_stat = hwif->dma_ops->dma_sff_read_status(hwif);
  73. if (on)
  74. dma_stat |= (1 << (5 + unit));
  75. else
  76. dma_stat &= ~(1 << (5 + unit));
  77. ide_dma_sff_write_status(hwif, dma_stat);
  78. }
  79. EXPORT_SYMBOL_GPL(ide_dma_host_set);
  80. /**
  81. * ide_build_dmatable - build IDE DMA table
  82. *
  83. * ide_build_dmatable() prepares a dma request. We map the command
  84. * to get the pci bus addresses of the buffers and then build up
  85. * the PRD table that the IDE layer wants to be fed.
  86. *
  87. * Most chipsets correctly interpret a length of 0x0000 as 64KB,
  88. * but at least one (e.g. CS5530) misinterprets it as zero (!).
  89. * So we break the 64KB entry into two 32KB entries instead.
  90. *
  91. * Returns the number of built PRD entries if all went okay,
  92. * returns 0 otherwise.
  93. *
  94. * May also be invoked from trm290.c
  95. */
  96. int ide_build_dmatable(ide_drive_t *drive, struct ide_cmd *cmd)
  97. {
  98. ide_hwif_t *hwif = drive->hwif;
  99. __le32 *table = (__le32 *)hwif->dmatable_cpu;
  100. unsigned int count = 0;
  101. int i;
  102. struct scatterlist *sg;
  103. u8 is_trm290 = !!(hwif->host_flags & IDE_HFLAG_TRM290);
  104. for_each_sg(hwif->sg_table, sg, cmd->sg_nents, i) {
  105. u32 cur_addr, cur_len, xcount, bcount;
  106. cur_addr = sg_dma_address(sg);
  107. cur_len = sg_dma_len(sg);
  108. /*
  109. * Fill in the dma table, without crossing any 64kB boundaries.
  110. * Most hardware requires 16-bit alignment of all blocks,
  111. * but the trm290 requires 32-bit alignment.
  112. */
  113. while (cur_len) {
  114. if (count++ >= PRD_ENTRIES)
  115. goto use_pio_instead;
  116. bcount = 0x10000 - (cur_addr & 0xffff);
  117. if (bcount > cur_len)
  118. bcount = cur_len;
  119. *table++ = cpu_to_le32(cur_addr);
  120. xcount = bcount & 0xffff;
  121. if (is_trm290)
  122. xcount = ((xcount >> 2) - 1) << 16;
  123. else if (xcount == 0x0000) {
  124. if (count++ >= PRD_ENTRIES)
  125. goto use_pio_instead;
  126. *table++ = cpu_to_le32(0x8000);
  127. *table++ = cpu_to_le32(cur_addr + 0x8000);
  128. xcount = 0x8000;
  129. }
  130. *table++ = cpu_to_le32(xcount);
  131. cur_addr += bcount;
  132. cur_len -= bcount;
  133. }
  134. }
  135. if (count) {
  136. if (!is_trm290)
  137. *--table |= cpu_to_le32(0x80000000);
  138. return count;
  139. }
  140. use_pio_instead:
  141. printk(KERN_ERR "%s: %s\n", drive->name,
  142. count ? "DMA table too small" : "empty DMA table?");
  143. return 0; /* revert to PIO for this request */
  144. }
  145. EXPORT_SYMBOL_GPL(ide_build_dmatable);
  146. /**
  147. * ide_dma_setup - begin a DMA phase
  148. * @drive: target device
  149. * @cmd: command
  150. *
  151. * Build an IDE DMA PRD (IDE speak for scatter gather table)
  152. * and then set up the DMA transfer registers for a device
  153. * that follows generic IDE PCI DMA behaviour. Controllers can
  154. * override this function if they need to
  155. *
  156. * Returns 0 on success. If a PIO fallback is required then 1
  157. * is returned.
  158. */
  159. int ide_dma_setup(ide_drive_t *drive, struct ide_cmd *cmd)
  160. {
  161. ide_hwif_t *hwif = drive->hwif;
  162. u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
  163. u8 rw = (cmd->tf_flags & IDE_TFLAG_WRITE) ? 0 : ATA_DMA_WR;
  164. u8 dma_stat;
  165. /* fall back to pio! */
  166. if (ide_build_dmatable(drive, cmd) == 0) {
  167. ide_map_sg(drive, cmd);
  168. return 1;
  169. }
  170. /* PRD table */
  171. if (mmio)
  172. writel(hwif->dmatable_dma,
  173. (void __iomem *)(hwif->dma_base + ATA_DMA_TABLE_OFS));
  174. else
  175. outl(hwif->dmatable_dma, hwif->dma_base + ATA_DMA_TABLE_OFS);
  176. /* specify r/w */
  177. if (mmio)
  178. writeb(rw, (void __iomem *)(hwif->dma_base + ATA_DMA_CMD));
  179. else
  180. outb(rw, hwif->dma_base + ATA_DMA_CMD);
  181. /* read DMA status for INTR & ERROR flags */
  182. dma_stat = hwif->dma_ops->dma_sff_read_status(hwif);
  183. /* clear INTR & ERROR flags */
  184. ide_dma_sff_write_status(hwif, dma_stat | ATA_DMA_ERR | ATA_DMA_INTR);
  185. return 0;
  186. }
  187. EXPORT_SYMBOL_GPL(ide_dma_setup);
  188. /**
  189. * ide_dma_sff_timer_expiry - handle a DMA timeout
  190. * @drive: Drive that timed out
  191. *
  192. * An IDE DMA transfer timed out. In the event of an error we ask
  193. * the driver to resolve the problem, if a DMA transfer is still
  194. * in progress we continue to wait (arguably we need to add a
  195. * secondary 'I don't care what the drive thinks' timeout here)
  196. * Finally if we have an interrupt we let it complete the I/O.
  197. * But only one time - we clear expiry and if it's still not
  198. * completed after WAIT_CMD, we error and retry in PIO.
  199. * This can occur if an interrupt is lost or due to hang or bugs.
  200. */
  201. int ide_dma_sff_timer_expiry(ide_drive_t *drive)
  202. {
  203. ide_hwif_t *hwif = drive->hwif;
  204. u8 dma_stat = hwif->dma_ops->dma_sff_read_status(hwif);
  205. printk(KERN_WARNING "%s: %s: DMA status (0x%02x)\n",
  206. drive->name, __func__, dma_stat);
  207. if ((dma_stat & 0x18) == 0x18) /* BUSY Stupid Early Timer !! */
  208. return WAIT_CMD;
  209. hwif->expiry = NULL; /* one free ride for now */
  210. if (dma_stat & ATA_DMA_ERR) /* ERROR */
  211. return -1;
  212. if (dma_stat & ATA_DMA_ACTIVE) /* DMAing */
  213. return WAIT_CMD;
  214. if (dma_stat & ATA_DMA_INTR) /* Got an Interrupt */
  215. return WAIT_CMD;
  216. return 0; /* Status is unknown -- reset the bus */
  217. }
  218. EXPORT_SYMBOL_GPL(ide_dma_sff_timer_expiry);
  219. void ide_dma_start(ide_drive_t *drive)
  220. {
  221. ide_hwif_t *hwif = drive->hwif;
  222. u8 dma_cmd;
  223. /* Note that this is done *after* the cmd has
  224. * been issued to the drive, as per the BM-IDE spec.
  225. * The Promise Ultra33 doesn't work correctly when
  226. * we do this part before issuing the drive cmd.
  227. */
  228. if (hwif->host_flags & IDE_HFLAG_MMIO) {
  229. dma_cmd = readb((void __iomem *)(hwif->dma_base + ATA_DMA_CMD));
  230. writeb(dma_cmd | ATA_DMA_START,
  231. (void __iomem *)(hwif->dma_base + ATA_DMA_CMD));
  232. } else {
  233. dma_cmd = inb(hwif->dma_base + ATA_DMA_CMD);
  234. outb(dma_cmd | ATA_DMA_START, hwif->dma_base + ATA_DMA_CMD);
  235. }
  236. }
  237. EXPORT_SYMBOL_GPL(ide_dma_start);
  238. /* returns 1 on error, 0 otherwise */
  239. int ide_dma_end(ide_drive_t *drive)
  240. {
  241. ide_hwif_t *hwif = drive->hwif;
  242. u8 dma_stat = 0, dma_cmd = 0;
  243. /* stop DMA */
  244. if (hwif->host_flags & IDE_HFLAG_MMIO) {
  245. dma_cmd = readb((void __iomem *)(hwif->dma_base + ATA_DMA_CMD));
  246. writeb(dma_cmd & ~ATA_DMA_START,
  247. (void __iomem *)(hwif->dma_base + ATA_DMA_CMD));
  248. } else {
  249. dma_cmd = inb(hwif->dma_base + ATA_DMA_CMD);
  250. outb(dma_cmd & ~ATA_DMA_START, hwif->dma_base + ATA_DMA_CMD);
  251. }
  252. /* get DMA status */
  253. dma_stat = hwif->dma_ops->dma_sff_read_status(hwif);
  254. /* clear INTR & ERROR bits */
  255. ide_dma_sff_write_status(hwif, dma_stat | ATA_DMA_ERR | ATA_DMA_INTR);
  256. #define CHECK_DMA_MASK (ATA_DMA_ACTIVE | ATA_DMA_ERR | ATA_DMA_INTR)
  257. /* verify good DMA status */
  258. if ((dma_stat & CHECK_DMA_MASK) != ATA_DMA_INTR)
  259. return 0x10 | dma_stat;
  260. return 0;
  261. }
  262. EXPORT_SYMBOL_GPL(ide_dma_end);
  263. /* returns 1 if dma irq issued, 0 otherwise */
  264. int ide_dma_test_irq(ide_drive_t *drive)
  265. {
  266. ide_hwif_t *hwif = drive->hwif;
  267. u8 dma_stat = hwif->dma_ops->dma_sff_read_status(hwif);
  268. return (dma_stat & ATA_DMA_INTR) ? 1 : 0;
  269. }
  270. EXPORT_SYMBOL_GPL(ide_dma_test_irq);
  271. const struct ide_dma_ops sff_dma_ops = {
  272. .dma_host_set = ide_dma_host_set,
  273. .dma_setup = ide_dma_setup,
  274. .dma_start = ide_dma_start,
  275. .dma_end = ide_dma_end,
  276. .dma_test_irq = ide_dma_test_irq,
  277. .dma_lost_irq = ide_dma_lost_irq,
  278. .dma_timer_expiry = ide_dma_sff_timer_expiry,
  279. .dma_sff_read_status = ide_dma_sff_read_status,
  280. };
  281. EXPORT_SYMBOL_GPL(sff_dma_ops);