Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 1 | /* |
Mike Frysinger | b03f203 | 2009-01-07 23:14:38 +0800 | [diff] [blame] | 2 | * dma.h - Blackfin DMA defines/structures/etc... |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 3 | * |
Mike Frysinger | b03f203 | 2009-01-07 23:14:38 +0800 | [diff] [blame] | 4 | * Copyright 2004-2008 Analog Devices Inc. |
| 5 | * Licensed under the GPL-2 or later. |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #ifndef _BLACKFIN_DMA_H_ |
| 9 | #define _BLACKFIN_DMA_H_ |
| 10 | |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 11 | #include <linux/interrupt.h> |
Mike Frysinger | 4c1ed6a | 2009-01-07 23:14:38 +0800 | [diff] [blame] | 12 | #include <mach/dma.h> |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 13 | #include <asm/blackfin.h> |
Mike Frysinger | 4c1ed6a | 2009-01-07 23:14:38 +0800 | [diff] [blame] | 14 | #include <asm/page.h> |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 15 | |
| 16 | #define MAX_DMA_ADDRESS PAGE_OFFSET |
| 17 | |
| 18 | /***************************************************************************** |
| 19 | * Generic DMA Declarations |
| 20 | * |
| 21 | ****************************************************************************/ |
| 22 | enum dma_chan_status { |
| 23 | DMA_CHANNEL_FREE, |
| 24 | DMA_CHANNEL_REQUESTED, |
| 25 | DMA_CHANNEL_ENABLED, |
| 26 | }; |
| 27 | |
| 28 | /*------------------------- |
| 29 | * config reg bits value |
| 30 | *-------------------------*/ |
| 31 | #define DATA_SIZE_8 0 |
| 32 | #define DATA_SIZE_16 1 |
| 33 | #define DATA_SIZE_32 2 |
| 34 | |
| 35 | #define DMA_FLOW_STOP 0 |
| 36 | #define DMA_FLOW_AUTO 1 |
| 37 | #define DMA_FLOW_ARRAY 4 |
| 38 | #define DMA_FLOW_SMALL 6 |
| 39 | #define DMA_FLOW_LARGE 7 |
| 40 | |
| 41 | #define DIMENSION_LINEAR 0 |
| 42 | #define DIMENSION_2D 1 |
| 43 | |
| 44 | #define DIR_READ 0 |
| 45 | #define DIR_WRITE 1 |
| 46 | |
| 47 | #define INTR_DISABLE 0 |
| 48 | #define INTR_ON_BUF 2 |
| 49 | #define INTR_ON_ROW 3 |
| 50 | |
Michael Hennerich | 2047e40 | 2008-01-22 15:29:18 +0800 | [diff] [blame] | 51 | #define DMA_NOSYNC_KEEP_DMA_BUF 0 |
| 52 | #define DMA_SYNC_RESTART 1 |
| 53 | |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 54 | struct dmasg { |
Mike Frysinger | 6ab729d | 2009-01-07 23:14:38 +0800 | [diff] [blame] | 55 | void *next_desc_addr; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 56 | unsigned long start_addr; |
| 57 | unsigned short cfg; |
| 58 | unsigned short x_count; |
| 59 | short x_modify; |
| 60 | unsigned short y_count; |
| 61 | short y_modify; |
| 62 | } __attribute__((packed)); |
| 63 | |
| 64 | struct dma_register { |
Mike Frysinger | 6ab729d | 2009-01-07 23:14:38 +0800 | [diff] [blame] | 65 | void *next_desc_ptr; /* DMA Next Descriptor Pointer register */ |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 66 | unsigned long start_addr; /* DMA Start address register */ |
| 67 | |
| 68 | unsigned short cfg; /* DMA Configuration register */ |
| 69 | unsigned short dummy1; /* DMA Configuration register */ |
| 70 | |
| 71 | unsigned long reserved; |
| 72 | |
| 73 | unsigned short x_count; /* DMA x_count register */ |
| 74 | unsigned short dummy2; |
| 75 | |
| 76 | short x_modify; /* DMA x_modify register */ |
| 77 | unsigned short dummy3; |
| 78 | |
| 79 | unsigned short y_count; /* DMA y_count register */ |
| 80 | unsigned short dummy4; |
| 81 | |
| 82 | short y_modify; /* DMA y_modify register */ |
| 83 | unsigned short dummy5; |
| 84 | |
Mike Frysinger | 6ab729d | 2009-01-07 23:14:38 +0800 | [diff] [blame] | 85 | void *curr_desc_ptr; /* DMA Current Descriptor Pointer |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 86 | register */ |
Bryan Wu | 452af71 | 2007-10-22 00:02:14 +0800 | [diff] [blame] | 87 | unsigned long curr_addr_ptr; /* DMA Current Address Pointer |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 88 | register */ |
| 89 | unsigned short irq_status; /* DMA irq status register */ |
| 90 | unsigned short dummy6; |
| 91 | |
| 92 | unsigned short peripheral_map; /* DMA peripheral map register */ |
| 93 | unsigned short dummy7; |
| 94 | |
| 95 | unsigned short curr_x_count; /* DMA Current x-count register */ |
| 96 | unsigned short dummy8; |
| 97 | |
| 98 | unsigned long reserved2; |
| 99 | |
| 100 | unsigned short curr_y_count; /* DMA Current y-count register */ |
| 101 | unsigned short dummy9; |
| 102 | |
| 103 | unsigned long reserved3; |
| 104 | |
| 105 | }; |
| 106 | |
Mike Frysinger | 4c1ed6a | 2009-01-07 23:14:38 +0800 | [diff] [blame] | 107 | struct mutex; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 108 | struct dma_channel { |
| 109 | struct mutex dmalock; |
Michael McTernan | 99532fd | 2009-01-07 23:14:38 +0800 | [diff] [blame] | 110 | const char *device_id; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 111 | enum dma_chan_status chan_status; |
Mike Frysinger | 4ce1873 | 2009-01-07 23:14:38 +0800 | [diff] [blame] | 112 | volatile struct dma_register *regs; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 113 | struct dmasg *sg; /* large mode descriptor */ |
Michael Hennerich | a2ba8b1 | 2008-10-28 18:19:29 +0800 | [diff] [blame] | 114 | unsigned int irq; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 115 | void *data; |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 116 | #ifdef CONFIG_PM |
| 117 | unsigned short saved_peripheral_map; |
| 118 | #endif |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 119 | }; |
| 120 | |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 121 | #ifdef CONFIG_PM |
| 122 | int blackfin_dma_suspend(void); |
| 123 | void blackfin_dma_resume(void); |
| 124 | #endif |
| 125 | |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 126 | /******************************************************************************* |
| 127 | * DMA API's |
| 128 | *******************************************************************************/ |
Mike Frysinger | 9c417a4 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 129 | extern struct dma_channel dma_ch[MAX_DMA_CHANNELS]; |
| 130 | extern struct dma_register *dma_io_base_addr[MAX_DMA_CHANNELS]; |
| 131 | extern int channel2irq(unsigned int channel); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 132 | |
Mike Frysinger | 9c417a4 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 133 | static inline void set_dma_start_addr(unsigned int channel, unsigned long addr) |
| 134 | { |
| 135 | dma_ch[channel].regs->start_addr = addr; |
| 136 | } |
Mike Frysinger | 6ab729d | 2009-01-07 23:14:38 +0800 | [diff] [blame] | 137 | static inline void set_dma_next_desc_addr(unsigned int channel, void *addr) |
Mike Frysinger | 9c417a4 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 138 | { |
| 139 | dma_ch[channel].regs->next_desc_ptr = addr; |
| 140 | } |
Mike Frysinger | 6ab729d | 2009-01-07 23:14:38 +0800 | [diff] [blame] | 141 | static inline void set_dma_curr_desc_addr(unsigned int channel, void *addr) |
Mike Frysinger | 9c417a4 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 142 | { |
| 143 | dma_ch[channel].regs->curr_desc_ptr = addr; |
| 144 | } |
| 145 | static inline void set_dma_x_count(unsigned int channel, unsigned short x_count) |
| 146 | { |
| 147 | dma_ch[channel].regs->x_count = x_count; |
| 148 | } |
| 149 | static inline void set_dma_y_count(unsigned int channel, unsigned short y_count) |
| 150 | { |
| 151 | dma_ch[channel].regs->y_count = y_count; |
| 152 | } |
| 153 | static inline void set_dma_x_modify(unsigned int channel, short x_modify) |
| 154 | { |
| 155 | dma_ch[channel].regs->x_modify = x_modify; |
| 156 | } |
| 157 | static inline void set_dma_y_modify(unsigned int channel, short y_modify) |
| 158 | { |
| 159 | dma_ch[channel].regs->y_modify = y_modify; |
| 160 | } |
| 161 | static inline void set_dma_config(unsigned int channel, unsigned short config) |
| 162 | { |
| 163 | dma_ch[channel].regs->cfg = config; |
| 164 | } |
| 165 | static inline void set_dma_curr_addr(unsigned int channel, unsigned long addr) |
| 166 | { |
| 167 | dma_ch[channel].regs->curr_addr_ptr = addr; |
| 168 | } |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 169 | |
Mike Frysinger | 9c417a4 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 170 | static inline unsigned short |
| 171 | set_bfin_dma_config(char direction, char flow_mode, |
| 172 | char intr_mode, char dma_mode, char width, char syncmode) |
| 173 | { |
| 174 | return (direction << 1) | (width << 2) | (dma_mode << 4) | |
| 175 | (intr_mode << 6) | (flow_mode << 12) | (syncmode << 5); |
| 176 | } |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 177 | |
Mike Frysinger | 9c417a4 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 178 | static inline unsigned short get_dma_curr_irqstat(unsigned int channel) |
| 179 | { |
| 180 | return dma_ch[channel].regs->irq_status; |
| 181 | } |
| 182 | static inline unsigned short get_dma_curr_xcount(unsigned int channel) |
| 183 | { |
| 184 | return dma_ch[channel].regs->curr_x_count; |
| 185 | } |
| 186 | static inline unsigned short get_dma_curr_ycount(unsigned int channel) |
| 187 | { |
| 188 | return dma_ch[channel].regs->curr_y_count; |
| 189 | } |
Mike Frysinger | 6ab729d | 2009-01-07 23:14:38 +0800 | [diff] [blame] | 190 | static inline void *get_dma_next_desc_ptr(unsigned int channel) |
Mike Frysinger | 9c417a4 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 191 | { |
| 192 | return dma_ch[channel].regs->next_desc_ptr; |
| 193 | } |
Mike Frysinger | 6ab729d | 2009-01-07 23:14:38 +0800 | [diff] [blame] | 194 | static inline void *get_dma_curr_desc_ptr(unsigned int channel) |
Mike Frysinger | 9c417a4 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 195 | { |
| 196 | return dma_ch[channel].regs->curr_desc_ptr; |
| 197 | } |
Mike Frysinger | 71f5ca3 | 2009-01-07 23:14:38 +0800 | [diff] [blame] | 198 | static inline unsigned short get_dma_config(unsigned int channel) |
| 199 | { |
| 200 | return dma_ch[channel].regs->cfg; |
| 201 | } |
Mike Frysinger | 9c417a4 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 202 | static inline unsigned long get_dma_curr_addr(unsigned int channel) |
| 203 | { |
| 204 | return dma_ch[channel].regs->curr_addr_ptr; |
| 205 | } |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 206 | |
Mike Frysinger | 9c417a4 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 207 | static inline void set_dma_sg(unsigned int channel, struct dmasg *sg, int ndsize) |
| 208 | { |
Mike Frysinger | d41e800 | 2009-01-07 23:14:38 +0800 | [diff] [blame] | 209 | dma_ch[channel].regs->cfg = |
| 210 | (dma_ch[channel].regs->cfg & ~(0xf << 8)) | |
| 211 | ((ndsize & 0xf) << 8); |
Mike Frysinger | 6ab729d | 2009-01-07 23:14:38 +0800 | [diff] [blame] | 212 | dma_ch[channel].regs->next_desc_ptr = sg; |
Mike Frysinger | 9c417a4 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | static inline int dma_channel_active(unsigned int channel) |
| 216 | { |
| 217 | if (dma_ch[channel].chan_status == DMA_CHANNEL_FREE) |
| 218 | return 0; |
| 219 | else |
| 220 | return 1; |
| 221 | } |
| 222 | |
| 223 | static inline void disable_dma(unsigned int channel) |
| 224 | { |
| 225 | dma_ch[channel].regs->cfg &= ~DMAEN; |
| 226 | SSYNC(); |
| 227 | dma_ch[channel].chan_status = DMA_CHANNEL_REQUESTED; |
| 228 | } |
| 229 | static inline void enable_dma(unsigned int channel) |
| 230 | { |
| 231 | dma_ch[channel].regs->curr_x_count = 0; |
| 232 | dma_ch[channel].regs->curr_y_count = 0; |
| 233 | dma_ch[channel].regs->cfg |= DMAEN; |
| 234 | dma_ch[channel].chan_status = DMA_CHANNEL_ENABLED; |
| 235 | } |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 236 | void free_dma(unsigned int channel); |
Michael McTernan | 99532fd | 2009-01-07 23:14:38 +0800 | [diff] [blame] | 237 | int request_dma(unsigned int channel, const char *device_id); |
Mike Frysinger | 9c417a4 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 238 | int set_dma_callback(unsigned int channel, irq_handler_t callback, void *data); |
| 239 | |
| 240 | static inline void dma_disable_irq(unsigned int channel) |
| 241 | { |
| 242 | disable_irq(dma_ch[channel].irq); |
| 243 | } |
| 244 | static inline void dma_enable_irq(unsigned int channel) |
| 245 | { |
| 246 | enable_irq(dma_ch[channel].irq); |
| 247 | } |
| 248 | static inline void clear_dma_irqstat(unsigned int channel) |
| 249 | { |
| 250 | dma_ch[channel].regs->irq_status = DMA_DONE | DMA_ERR; |
| 251 | } |
| 252 | |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 253 | void *dma_memcpy(void *dest, const void *src, size_t count); |
| 254 | void *safe_dma_memcpy(void *dest, const void *src, size_t count); |
Mike Frysinger | dd3dd38 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 255 | void blackfin_dma_early_init(void); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 256 | |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 257 | #endif |