dma-isa-lpc.rst 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. ============================
  2. DMA with ISA and LPC devices
  3. ============================
  4. :Author: Pierre Ossman <drzeus@drzeus.cx>
  5. This document describes how to do DMA transfers using the old ISA DMA
  6. controller. Even though ISA is more or less dead today the LPC bus
  7. uses the same DMA system so it will be around for quite some time.
  8. Headers and dependencies
  9. ------------------------
  10. To do ISA style DMA you need to include two headers::
  11. #include <linux/dma-mapping.h>
  12. #include <asm/dma.h>
  13. The first is the generic DMA API used to convert virtual addresses to
  14. bus addresses (see :doc:`/core-api/dma-api` for details).
  15. The second contains the routines specific to ISA DMA transfers. Since
  16. this is not present on all platforms make sure you construct your
  17. Kconfig to be dependent on ISA_DMA_API (not ISA) so that nobody tries
  18. to build your driver on unsupported platforms.
  19. Buffer allocation
  20. -----------------
  21. The ISA DMA controller has some very strict requirements on which
  22. memory it can access so extra care must be taken when allocating
  23. buffers.
  24. (You usually need a special buffer for DMA transfers instead of
  25. transferring directly to and from your normal data structures.)
  26. The DMA-able address space is the lowest 16 MB of _physical_ memory.
  27. Also the transfer block may not cross page boundaries (which are 64
  28. or 128 KiB depending on which channel you use).
  29. In order to allocate a piece of memory that satisfies all these
  30. requirements you pass the flag GFP_DMA to kmalloc.
  31. Unfortunately the memory available for ISA DMA is scarce so unless you
  32. allocate the memory during boot-up it's a good idea to also pass
  33. __GFP_RETRY_MAYFAIL and __GFP_NOWARN to make the allocator try a bit harder.
  34. (This scarcity also means that you should allocate the buffer as
  35. early as possible and not release it until the driver is unloaded.)
  36. Address translation
  37. -------------------
  38. To translate the virtual address to a bus address, use the normal DMA
  39. API. Do _not_ use isa_virt_to_bus() even though it does the same
  40. thing. The reason for this is that the function isa_virt_to_bus()
  41. will require a Kconfig dependency to ISA, not just ISA_DMA_API which
  42. is really all you need. Remember that even though the DMA controller
  43. has its origins in ISA it is used elsewhere.
  44. Note: x86_64 had a broken DMA API when it came to ISA but has since
  45. been fixed. If your arch has problems then fix the DMA API instead of
  46. reverting to the ISA functions.
  47. Channels
  48. --------
  49. A normal ISA DMA controller has 8 channels. The lower four are for
  50. 8-bit transfers and the upper four are for 16-bit transfers.
  51. (Actually the DMA controller is really two separate controllers where
  52. channel 4 is used to give DMA access for the second controller (0-3).
  53. This means that of the four 16-bits channels only three are usable.)
  54. You allocate these in a similar fashion as all basic resources:
  55. extern int request_dma(unsigned int dmanr, const char * device_id);
  56. extern void free_dma(unsigned int dmanr);
  57. The ability to use 16-bit or 8-bit transfers is _not_ up to you as a
  58. driver author but depends on what the hardware supports. Check your
  59. specs or test different channels.
  60. Transfer data
  61. -------------
  62. Now for the good stuff, the actual DMA transfer. :)
  63. Before you use any ISA DMA routines you need to claim the DMA lock
  64. using claim_dma_lock(). The reason is that some DMA operations are
  65. not atomic so only one driver may fiddle with the registers at a
  66. time.
  67. The first time you use the DMA controller you should call
  68. clear_dma_ff(). This clears an internal register in the DMA
  69. controller that is used for the non-atomic operations. As long as you
  70. (and everyone else) uses the locking functions then you only need to
  71. reset this once.
  72. Next, you tell the controller in which direction you intend to do the
  73. transfer using set_dma_mode(). Currently you have the options
  74. DMA_MODE_READ and DMA_MODE_WRITE.
  75. Set the address from where the transfer should start (this needs to
  76. be 16-bit aligned for 16-bit transfers) and how many bytes to
  77. transfer. Note that it's _bytes_. The DMA routines will do all the
  78. required translation to values that the DMA controller understands.
  79. The final step is enabling the DMA channel and releasing the DMA
  80. lock.
  81. Once the DMA transfer is finished (or timed out) you should disable
  82. the channel again. You should also check get_dma_residue() to make
  83. sure that all data has been transferred.
  84. Example::
  85. int flags, residue;
  86. flags = claim_dma_lock();
  87. clear_dma_ff();
  88. set_dma_mode(channel, DMA_MODE_WRITE);
  89. set_dma_addr(channel, phys_addr);
  90. set_dma_count(channel, num_bytes);
  91. dma_enable(channel);
  92. release_dma_lock(flags);
  93. while (!device_done());
  94. flags = claim_dma_lock();
  95. dma_disable(channel);
  96. residue = dma_get_residue(channel);
  97. if (residue != 0)
  98. printk(KERN_ERR "driver: Incomplete DMA transfer!"
  99. " %d bytes left!\n", residue);
  100. release_dma_lock(flags);
  101. Suspend/resume
  102. --------------
  103. It is the driver's responsibility to make sure that the machine isn't
  104. suspended while a DMA transfer is in progress. Also, all DMA settings
  105. are lost when the system suspends so if your driver relies on the DMA
  106. controller being in a certain state then you have to restore these
  107. registers upon resume.