ide-dma-sff.c 9.3 KB

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