dma.h

Go to the documentation of this file.
00001 /**
00002 *
00003  $Id: dma.h,v 1.7 1992/12/14 00:29:34 root Exp root $
00004  * linux/include/asm/dma.h: Defines for using and allocating dma channels.
00005  * Written by Hennus Bergman, 1992.
00006  * High DMA channel support & info by Hannu Savolainen
00007  * and John Boyd, Nov. 1992.
00008 
00009 
00010 */
00011 
00012 #ifndef _ASM_DMA_H
00013 #define _ASM_DMA_H
00014 
00015 #include <asm/io.h>             ///< need byte IO 
00016 
00017 
00018 #ifdef HAVE_REALLY_SLOW_DMA_CONTROLLER
00019 #define outb    outb_p
00020 #endif
00021 
00022 /**
00023 *
00024 
00025  * NOTES about DMA transfers:
00026  *
00027  *  controller 1: channels 0-3, byte operations, ports 00-1F
00028  *  controller 2: channels 4-7, word operations, ports C0-DF
00029  *
00030  *  - ALL registers are 8 bits only, regardless of transfer size
00031  *  - channel 4 is not used - cascades 1 into 2.
00032  *  - channels 0-3 are byte - addresses/counts are for physical bytes
00033  *  - channels 5-7 are word - addresses/counts are for physical words
00034  *  - transfers must not cross physical 64K (0-3) or 128K (5-7) boundaries
00035  *  - transfer count loaded to registers is 1 less than actual count
00036  *  - controller 2 offsets are all even (2x offsets for controller 1)
00037  *  - page registers for 5-7 don't use data bit 0, represent 128K pages
00038  *  - page registers for 0-3 use bit 0, represent 64K pages
00039  *
00040  * DMA transfers are limited to the lower 16MB of _physical_ memory.  
00041  * Note that addresses loaded into registers must be _physical_ addresses,
00042  * not logical addresses (which may differ if paging is active).
00043  *
00044  *  Address mapping for channels 0-3:
00045  *
00046  *   A23 ... A16 A15 ... A8  A7 ... A0    (Physical addresses)
00047  *    |  ...  |   |  ... |   |  ... |
00048  *    |  ...  |   |  ... |   |  ... |
00049  *    |  ...  |   |  ... |   |  ... |
00050  *   P7  ...  P0  A7 ... A0  A7 ... A0   
00051  * |    Page    | Addr MSB | Addr LSB |   (DMA registers)
00052  *
00053  *  Address mapping for channels 5-7:
00054  *
00055  *   A23 ... A17 A16 A15 ... A9 A8 A7 ... A1 A0    (Physical addresses)
00056  *    |  ...  |   \   \   ... \  \  \  ... \  \
00057  *    |  ...  |    \   \   ... \  \  \  ... \  (not used)
00058  *    |  ...  |     \   \   ... \  \  \  ... \
00059  *   P7  ...  P1 (0) A7 A6  ... A0 A7 A6 ... A0   
00060  * |      Page      |  Addr MSB   |  Addr LSB  |   (DMA registers)
00061  *
00062  * Again, channels 5-7 transfer _physical_ words (16 bits), so addresses
00063  * and counts _must_ be word-aligned (the lowest address bit is _ignored_ at
00064  * the hardware level, so odd-byte transfers aren't possible).
00065  *
00066  * Transfer count (_not # bytes_) is limited to 64K, represented as actual
00067  * count - 1 : 64K => 0xFFFF, 1 => 0x0000.  Thus, count is always 1 or more,
00068  * and up to 128K bytes may be transferred on channels 5-7 in one operation. 
00069  *
00070 
00071 
00072 */
00073 
00074 #define MAX_DMA_CHANNELS        8
00075 
00076 /// 8237 DMA controllers 
00077 #define IO_DMA1_BASE    0x00    ///< 8 bit slave DMA, channels 0..3 
00078 #define IO_DMA2_BASE    0xC0    ///< 16 bit master DMA, ch 4(=slave input)..7 
00079 
00080 /// DMA controller registers 
00081 #define DMA1_CMD_REG            0x08    ///< command register (w) 
00082 #define DMA1_STAT_REG           0x08    ///< status register (r) 
00083 #define DMA1_REQ_REG            0x09    ///< request register (w) 
00084 #define DMA1_MASK_REG           0x0A    ///< single-channel mask (w) 
00085 #define DMA1_MODE_REG           0x0B    ///< mode register (w) 
00086 #define DMA1_CLEAR_FF_REG       0x0C    ///< clear pointer flip-flop (w) 
00087 #define DMA1_TEMP_REG           0x0D    ///< Temporary Register (r) 
00088 #define DMA1_RESET_REG          0x0D    ///< Master Clear (w) 
00089 #define DMA1_CLR_MASK_REG       0x0E    ///< Clear Mask 
00090 #define DMA1_MASK_ALL_REG       0x0F    ///< all-channels mask (w) 
00091 
00092 #define DMA2_CMD_REG            0xD0    ///< command register (w) 
00093 #define DMA2_STAT_REG           0xD0    ///< status register (r) 
00094 #define DMA2_REQ_REG            0xD2    ///< request register (w) 
00095 #define DMA2_MASK_REG           0xD4    ///< single-channel mask (w) 
00096 #define DMA2_MODE_REG           0xD6    ///< mode register (w) 
00097 #define DMA2_CLEAR_FF_REG       0xD8    ///< clear pointer flip-flop (w) 
00098 #define DMA2_TEMP_REG           0xDA    ///< Temporary Register (r) 
00099 #define DMA2_RESET_REG          0xDA    ///< Master Clear (w) 
00100 #define DMA2_CLR_MASK_REG       0xDC    ///< Clear Mask 
00101 #define DMA2_MASK_ALL_REG       0xDE    ///< all-channels mask (w) 
00102 
00103 #define DMA_ADDR_0              0x00    ///< DMA address registers 
00104 #define DMA_ADDR_1              0x02
00105 #define DMA_ADDR_2              0x04
00106 #define DMA_ADDR_3              0x06
00107 #define DMA_ADDR_4              0xC0
00108 #define DMA_ADDR_5              0xC4
00109 #define DMA_ADDR_6              0xC8
00110 #define DMA_ADDR_7              0xCC
00111 
00112 #define DMA_CNT_0               0x01    ///< DMA count registers 
00113 #define DMA_CNT_1               0x03
00114 #define DMA_CNT_2               0x05
00115 #define DMA_CNT_3               0x07
00116 #define DMA_CNT_4               0xC2
00117 #define DMA_CNT_5               0xC6
00118 #define DMA_CNT_6               0xCA
00119 #define DMA_CNT_7               0xCE
00120 
00121 #define DMA_PAGE_0              0x87    ///< DMA page registers 
00122 #define DMA_PAGE_1              0x83
00123 #define DMA_PAGE_2              0x81
00124 #define DMA_PAGE_3              0x82
00125 #define DMA_PAGE_5              0x8B
00126 #define DMA_PAGE_6              0x89
00127 #define DMA_PAGE_7              0x8A
00128 
00129 #define DMA_MODE_READ   0x44    ///< I/O to memory, no autoinit, increment, single mode 
00130 #define DMA_MODE_WRITE  0x48    ///< memory to I/O, no autoinit, increment, single mode 
00131 #define DMA_MODE_CASCADE 0xC0   ///< pass thru DREQ->HRQ, DACK<-HLDA only 
00132 
00133 /// enable/disable a specific DMA channel 
00134 static __inline__ void enable_dma(unsigned int dmanr)
00135 {
00136         if (dmanr<=3)
00137                 outb(dmanr,  DMA1_MASK_REG);
00138         else
00139                 outb(dmanr & 3,  DMA2_MASK_REG);
00140 }
00141 
00142 static __inline__ void disable_dma(unsigned int dmanr)
00143 {
00144         if (dmanr<=3)
00145                 outb(dmanr | 4,  DMA1_MASK_REG);
00146         else
00147                 outb((dmanr & 3) | 4,  DMA2_MASK_REG);
00148 }
00149 
00150 /**
00151 *
00152  Clear the 'DMA Pointer Flip Flop'.
00153  * Write 0 for LSB/MSB, 1 for MSB/LSB access.
00154  * Use this once to initialize the FF to a known state.
00155  * After that, keep track of it. :-)
00156  * --- In order to do that, the DMA routines below should ---
00157  * --- only be used while interrupts are disabled! ---
00158 
00159 
00160 */
00161 static __inline__ void clear_dma_ff(unsigned int dmanr)
00162 {
00163         if (dmanr<=3)
00164                 outb(0,  DMA1_CLEAR_FF_REG);
00165         else
00166                 outb(0,  DMA2_CLEAR_FF_REG);
00167 }
00168 
00169 /// set mode (above) for a specific DMA channel 
00170 static __inline__ void set_dma_mode(unsigned int dmanr, char mode)
00171 {
00172         if (dmanr<=3)
00173                 outb(mode | dmanr,  DMA1_MODE_REG);
00174         else
00175                 outb(mode | (dmanr&3),  DMA2_MODE_REG);
00176 }
00177 
00178 /**
00179 *
00180  Set only the page register bits of the transfer address.
00181  * This is used for successive transfers when we know the contents of
00182  * the lower 16 bits of the DMA current address register, but a 64k boundary
00183  * may have been crossed.
00184 
00185 
00186 */
00187 static __inline__ void set_dma_page(unsigned int dmanr, char pagenr)
00188 {
00189         switch(dmanr) {
00190                 case 0:
00191                         outb(pagenr, DMA_PAGE_0);
00192                         break;
00193                 case 1:
00194                         outb(pagenr, DMA_PAGE_1);
00195                         break;
00196                 case 2:
00197                         outb(pagenr, DMA_PAGE_2);
00198                         break;
00199                 case 3:
00200                         outb(pagenr, DMA_PAGE_3);
00201                         break;
00202                 case 5:
00203                         outb(pagenr & 0xfe, DMA_PAGE_5);
00204                         break;
00205                 case 6:
00206                         outb(pagenr & 0xfe, DMA_PAGE_6);
00207                         break;
00208                 case 7:
00209                         outb(pagenr & 0xfe, DMA_PAGE_7);
00210                         break;
00211         }
00212 }
00213 
00214 
00215 /**
00216 *
00217  Set transfer address & page bits for specific DMA channel.
00218  * Assumes dma flipflop is clear.
00219 
00220 
00221 */
00222 static __inline__ void set_dma_addr(unsigned int dmanr, unsigned int a)
00223 {
00224         set_dma_page(dmanr, a>>16);
00225         if (dmanr <= 3)  {
00226             outb( a & 0xff, ((dmanr&3)<<1) + IO_DMA1_BASE );
00227             outb( (a>>8) & 0xff, ((dmanr&3)<<1) + IO_DMA1_BASE );
00228         }  else  {
00229             outb( (a>>1) & 0xff, ((dmanr&3)<<2) + IO_DMA2_BASE );
00230             outb( (a>>9) & 0xff, ((dmanr&3)<<2) + IO_DMA2_BASE );
00231         }
00232 }
00233 
00234 
00235 /**
00236 *
00237  Set transfer size (max 64k for DMA1..3, 128k for DMA5..7) for
00238  * a specific DMA channel.
00239  * You must ensure the parameters are valid.
00240  * NOTE: from a manual: "the number of transfers is one more
00241  * than the initial word count"! This is taken into account.
00242  * Assumes dma flip-flop is clear.
00243  * NOTE 2: "count" represents _bytes_ and must be even for channels 5-7.
00244 
00245 
00246 */
00247 static __inline__ void set_dma_count(unsigned int dmanr, unsigned int count)
00248 {
00249         count--;
00250         if (dmanr <= 3)  {
00251             outb( count & 0xff, ((dmanr&3)<<1) + 1 + IO_DMA1_BASE );
00252             outb( (count>>8) & 0xff, ((dmanr&3)<<1) + 1 + IO_DMA1_BASE );
00253         } else {
00254             outb( (count>>1) & 0xff, ((dmanr&3)<<2) + 2 + IO_DMA2_BASE );
00255             outb( (count>>9) & 0xff, ((dmanr&3)<<2) + 2 + IO_DMA2_BASE );
00256         }
00257 }
00258 
00259 
00260 /**
00261 *
00262  Get DMA residue count. After a DMA transfer, this
00263  * should return zero. Reading this while a DMA transfer is
00264  * still in progress will return unpredictable results.
00265  * If called before the channel has been used, it may return 1.
00266  * Otherwise, it returns the number of _bytes_ left to transfer.
00267  *
00268  * Assumes DMA flip-flop is clear.
00269 
00270 
00271 */
00272 static __inline__ int get_dma_residue(unsigned int dmanr)
00273 {
00274         unsigned int io_port = (dmanr<=3)? ((dmanr&3)<<1) + 1 + IO_DMA1_BASE
00275                                          : ((dmanr&3)<<2) + 2 + IO_DMA2_BASE;
00276 
00277         /// using short to get 16-bit wrap around 
00278         unsigned short count;
00279 
00280         count = 1 + inb(io_port);
00281         count += inb(io_port) << 8;
00282         
00283         return (dmanr<=3)? count : (count<<1);
00284 }
00285 
00286 
00287 /// These are in kernel/dma.c: 
00288 extern int request_dma(unsigned int dmanr);     ///< reserve a DMA channel 
00289 extern void free_dma(unsigned int dmanr);       ///< release it again 
00290 
00291 
00292 #endif // _ASM_DMA_H 

Generated on Mon May 1 21:46:59 2006 for KernelAPI by  doxygen 1.4.6-5