0001 ============================
0002 DMA with ISA and LPC devices
0003 ============================
0004
0005 :Author: Pierre Ossman <drzeus@drzeus.cx>
0006
0007 This document describes how to do DMA transfers using the old ISA DMA
0008 controller. Even though ISA is more or less dead today the LPC bus
0009 uses the same DMA system so it will be around for quite some time.
0010
0011 Headers and dependencies
0012 ------------------------
0013
0014 To do ISA style DMA you need to include two headers::
0015
0016 #include <linux/dma-mapping.h>
0017 #include <asm/dma.h>
0018
0019 The first is the generic DMA API used to convert virtual addresses to
0020 bus addresses (see Documentation/core-api/dma-api.rst for details).
0021
0022 The second contains the routines specific to ISA DMA transfers. Since
0023 this is not present on all platforms make sure you construct your
0024 Kconfig to be dependent on ISA_DMA_API (not ISA) so that nobody tries
0025 to build your driver on unsupported platforms.
0026
0027 Buffer allocation
0028 -----------------
0029
0030 The ISA DMA controller has some very strict requirements on which
0031 memory it can access so extra care must be taken when allocating
0032 buffers.
0033
0034 (You usually need a special buffer for DMA transfers instead of
0035 transferring directly to and from your normal data structures.)
0036
0037 The DMA-able address space is the lowest 16 MB of _physical_ memory.
0038 Also the transfer block may not cross page boundaries (which are 64
0039 or 128 KiB depending on which channel you use).
0040
0041 In order to allocate a piece of memory that satisfies all these
0042 requirements you pass the flag GFP_DMA to kmalloc.
0043
0044 Unfortunately the memory available for ISA DMA is scarce so unless you
0045 allocate the memory during boot-up it's a good idea to also pass
0046 __GFP_RETRY_MAYFAIL and __GFP_NOWARN to make the allocator try a bit harder.
0047
0048 (This scarcity also means that you should allocate the buffer as
0049 early as possible and not release it until the driver is unloaded.)
0050
0051 Address translation
0052 -------------------
0053
0054 To translate the virtual address to a bus address, use the normal DMA
0055 API. Do _not_ use isa_virt_to_bus() even though it does the same
0056 thing. The reason for this is that the function isa_virt_to_bus()
0057 will require a Kconfig dependency to ISA, not just ISA_DMA_API which
0058 is really all you need. Remember that even though the DMA controller
0059 has its origins in ISA it is used elsewhere.
0060
0061 Note: x86_64 had a broken DMA API when it came to ISA but has since
0062 been fixed. If your arch has problems then fix the DMA API instead of
0063 reverting to the ISA functions.
0064
0065 Channels
0066 --------
0067
0068 A normal ISA DMA controller has 8 channels. The lower four are for
0069 8-bit transfers and the upper four are for 16-bit transfers.
0070
0071 (Actually the DMA controller is really two separate controllers where
0072 channel 4 is used to give DMA access for the second controller (0-3).
0073 This means that of the four 16-bits channels only three are usable.)
0074
0075 You allocate these in a similar fashion as all basic resources:
0076
0077 extern int request_dma(unsigned int dmanr, const char * device_id);
0078 extern void free_dma(unsigned int dmanr);
0079
0080 The ability to use 16-bit or 8-bit transfers is _not_ up to you as a
0081 driver author but depends on what the hardware supports. Check your
0082 specs or test different channels.
0083
0084 Transfer data
0085 -------------
0086
0087 Now for the good stuff, the actual DMA transfer. :)
0088
0089 Before you use any ISA DMA routines you need to claim the DMA lock
0090 using claim_dma_lock(). The reason is that some DMA operations are
0091 not atomic so only one driver may fiddle with the registers at a
0092 time.
0093
0094 The first time you use the DMA controller you should call
0095 clear_dma_ff(). This clears an internal register in the DMA
0096 controller that is used for the non-atomic operations. As long as you
0097 (and everyone else) uses the locking functions then you only need to
0098 reset this once.
0099
0100 Next, you tell the controller in which direction you intend to do the
0101 transfer using set_dma_mode(). Currently you have the options
0102 DMA_MODE_READ and DMA_MODE_WRITE.
0103
0104 Set the address from where the transfer should start (this needs to
0105 be 16-bit aligned for 16-bit transfers) and how many bytes to
0106 transfer. Note that it's _bytes_. The DMA routines will do all the
0107 required translation to values that the DMA controller understands.
0108
0109 The final step is enabling the DMA channel and releasing the DMA
0110 lock.
0111
0112 Once the DMA transfer is finished (or timed out) you should disable
0113 the channel again. You should also check get_dma_residue() to make
0114 sure that all data has been transferred.
0115
0116 Example::
0117
0118 int flags, residue;
0119
0120 flags = claim_dma_lock();
0121
0122 clear_dma_ff();
0123
0124 set_dma_mode(channel, DMA_MODE_WRITE);
0125 set_dma_addr(channel, phys_addr);
0126 set_dma_count(channel, num_bytes);
0127
0128 dma_enable(channel);
0129
0130 release_dma_lock(flags);
0131
0132 while (!device_done());
0133
0134 flags = claim_dma_lock();
0135
0136 dma_disable(channel);
0137
0138 residue = dma_get_residue(channel);
0139 if (residue != 0)
0140 printk(KERN_ERR "driver: Incomplete DMA transfer!"
0141 " %d bytes left!\n", residue);
0142
0143 release_dma_lock(flags);
0144
0145 Suspend/resume
0146 --------------
0147
0148 It is the driver's responsibility to make sure that the machine isn't
0149 suspended while a DMA transfer is in progress. Also, all DMA settings
0150 are lost when the system suspends so if your driver relies on the DMA
0151 controller being in a certain state then you have to restore these
0152 registers upon resume.