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