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