ide-dma-sff.c 8.9 KB

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