blob: 5eb29502bbe42961c7150fafd5745cb1d3d27d6c [file] [log] [blame]
Bryan Wu1394f032007-05-06 14:50:22 -07001/*
Mike Frysingerb03f2032009-01-07 23:14:38 +08002 * dma.h - Blackfin DMA defines/structures/etc...
Bryan Wu1394f032007-05-06 14:50:22 -07003 *
Mike Frysingerb03f2032009-01-07 23:14:38 +08004 * Copyright 2004-2008 Analog Devices Inc.
5 * Licensed under the GPL-2 or later.
Bryan Wu1394f032007-05-06 14:50:22 -07006 */
7
8#ifndef _BLACKFIN_DMA_H_
9#define _BLACKFIN_DMA_H_
10
Bryan Wu1394f032007-05-06 14:50:22 -070011#include <linux/interrupt.h>
Mike Frysinger4c1ed6a2009-01-07 23:14:38 +080012#include <mach/dma.h>
Mike Frysingerd2e015d2009-10-09 22:18:12 +000013#include <asm/atomic.h>
Bryan Wu1394f032007-05-06 14:50:22 -070014#include <asm/blackfin.h>
Mike Frysinger4c1ed6a2009-01-07 23:14:38 +080015#include <asm/page.h>
Bryan Wu1394f032007-05-06 14:50:22 -070016
17#define MAX_DMA_ADDRESS PAGE_OFFSET
18
Mike Frysinger00d24602009-10-20 17:20:21 +000019/* DMA_CONFIG Masks */
20#define DMAEN 0x0001 /* DMA Channel Enable */
21#define WNR 0x0002 /* Channel Direction (W/R*) */
22#define WDSIZE_8 0x0000 /* Transfer Word Size = 8 */
23#define WDSIZE_16 0x0004 /* Transfer Word Size = 16 */
24#define WDSIZE_32 0x0008 /* Transfer Word Size = 32 */
25#define DMA2D 0x0010 /* DMA Mode (2D/1D*) */
26#define RESTART 0x0020 /* DMA Buffer Clear */
27#define DI_SEL 0x0040 /* Data Interrupt Timing Select */
28#define DI_EN 0x0080 /* Data Interrupt Enable */
29#define NDSIZE_0 0x0000 /* Next Descriptor Size = 0 (Stop/Autobuffer) */
30#define NDSIZE_1 0x0100 /* Next Descriptor Size = 1 */
31#define NDSIZE_2 0x0200 /* Next Descriptor Size = 2 */
32#define NDSIZE_3 0x0300 /* Next Descriptor Size = 3 */
33#define NDSIZE_4 0x0400 /* Next Descriptor Size = 4 */
34#define NDSIZE_5 0x0500 /* Next Descriptor Size = 5 */
35#define NDSIZE_6 0x0600 /* Next Descriptor Size = 6 */
36#define NDSIZE_7 0x0700 /* Next Descriptor Size = 7 */
37#define NDSIZE_8 0x0800 /* Next Descriptor Size = 8 */
38#define NDSIZE_9 0x0900 /* Next Descriptor Size = 9 */
39#define NDSIZE 0x0f00 /* Next Descriptor Size */
40#define DMAFLOW 0x7000 /* Flow Control */
41#define DMAFLOW_STOP 0x0000 /* Stop Mode */
42#define DMAFLOW_AUTO 0x1000 /* Autobuffer Mode */
43#define DMAFLOW_ARRAY 0x4000 /* Descriptor Array Mode */
44#define DMAFLOW_SMALL 0x6000 /* Small Model Descriptor List Mode */
45#define DMAFLOW_LARGE 0x7000 /* Large Model Descriptor List Mode */
46
47/* DMA_IRQ_STATUS Masks */
48#define DMA_DONE 0x0001 /* DMA Completion Interrupt Status */
49#define DMA_ERR 0x0002 /* DMA Error Interrupt Status */
50#define DFETCH 0x0004 /* DMA Descriptor Fetch Indicator */
51#define DMA_RUN 0x0008 /* DMA Channel Running Indicator */
Bryan Wu1394f032007-05-06 14:50:22 -070052
53/*-------------------------
54 * config reg bits value
55 *-------------------------*/
Mike Frysinger00d24602009-10-20 17:20:21 +000056#define DATA_SIZE_8 0
57#define DATA_SIZE_16 1
58#define DATA_SIZE_32 2
Bryan Wu1394f032007-05-06 14:50:22 -070059
Mike Frysinger00d24602009-10-20 17:20:21 +000060#define DMA_FLOW_STOP 0
61#define DMA_FLOW_AUTO 1
62#define DMA_FLOW_ARRAY 4
63#define DMA_FLOW_SMALL 6
64#define DMA_FLOW_LARGE 7
Bryan Wu1394f032007-05-06 14:50:22 -070065
Mike Frysinger00d24602009-10-20 17:20:21 +000066#define DIMENSION_LINEAR 0
67#define DIMENSION_2D 1
Bryan Wu1394f032007-05-06 14:50:22 -070068
Mike Frysinger00d24602009-10-20 17:20:21 +000069#define DIR_READ 0
70#define DIR_WRITE 1
Bryan Wu1394f032007-05-06 14:50:22 -070071
Mike Frysinger00d24602009-10-20 17:20:21 +000072#define INTR_DISABLE 0
73#define INTR_ON_BUF 2
74#define INTR_ON_ROW 3
Bryan Wu1394f032007-05-06 14:50:22 -070075
Michael Hennerich2047e402008-01-22 15:29:18 +080076#define DMA_NOSYNC_KEEP_DMA_BUF 0
Mike Frysinger00d24602009-10-20 17:20:21 +000077#define DMA_SYNC_RESTART 1
Michael Hennerich2047e402008-01-22 15:29:18 +080078
Bryan Wu1394f032007-05-06 14:50:22 -070079struct dmasg {
Mike Frysinger6ab729d2009-01-07 23:14:38 +080080 void *next_desc_addr;
Bryan Wu1394f032007-05-06 14:50:22 -070081 unsigned long start_addr;
82 unsigned short cfg;
83 unsigned short x_count;
84 short x_modify;
85 unsigned short y_count;
86 short y_modify;
87} __attribute__((packed));
88
89struct dma_register {
Mike Frysinger6ab729d2009-01-07 23:14:38 +080090 void *next_desc_ptr; /* DMA Next Descriptor Pointer register */
Bryan Wu1394f032007-05-06 14:50:22 -070091 unsigned long start_addr; /* DMA Start address register */
92
93 unsigned short cfg; /* DMA Configuration register */
94 unsigned short dummy1; /* DMA Configuration register */
95
96 unsigned long reserved;
97
98 unsigned short x_count; /* DMA x_count register */
99 unsigned short dummy2;
100
101 short x_modify; /* DMA x_modify register */
102 unsigned short dummy3;
103
104 unsigned short y_count; /* DMA y_count register */
105 unsigned short dummy4;
106
107 short y_modify; /* DMA y_modify register */
108 unsigned short dummy5;
109
Mike Frysinger6ab729d2009-01-07 23:14:38 +0800110 void *curr_desc_ptr; /* DMA Current Descriptor Pointer
Bryan Wu1394f032007-05-06 14:50:22 -0700111 register */
Bryan Wu452af712007-10-22 00:02:14 +0800112 unsigned long curr_addr_ptr; /* DMA Current Address Pointer
Bryan Wu1394f032007-05-06 14:50:22 -0700113 register */
114 unsigned short irq_status; /* DMA irq status register */
115 unsigned short dummy6;
116
117 unsigned short peripheral_map; /* DMA peripheral map register */
118 unsigned short dummy7;
119
120 unsigned short curr_x_count; /* DMA Current x-count register */
121 unsigned short dummy8;
122
123 unsigned long reserved2;
124
125 unsigned short curr_y_count; /* DMA Current y-count register */
126 unsigned short dummy9;
127
128 unsigned long reserved3;
129
130};
131
Bryan Wu1394f032007-05-06 14:50:22 -0700132struct dma_channel {
Michael McTernan99532fd2009-01-07 23:14:38 +0800133 const char *device_id;
Mike Frysingerd2e015d2009-10-09 22:18:12 +0000134 atomic_t chan_status;
Mike Frysinger4ce18732009-01-07 23:14:38 +0800135 volatile struct dma_register *regs;
Bryan Wu1394f032007-05-06 14:50:22 -0700136 struct dmasg *sg; /* large mode descriptor */
Michael Hennericha2ba8b12008-10-28 18:19:29 +0800137 unsigned int irq;
Bryan Wu1394f032007-05-06 14:50:22 -0700138 void *data;
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800139#ifdef CONFIG_PM
140 unsigned short saved_peripheral_map;
141#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700142};
143
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800144#ifdef CONFIG_PM
145int blackfin_dma_suspend(void);
146void blackfin_dma_resume(void);
147#endif
148
Bryan Wu1394f032007-05-06 14:50:22 -0700149/*******************************************************************************
150* DMA API's
151*******************************************************************************/
Mike Frysinger9c417a42009-01-07 23:14:39 +0800152extern struct dma_channel dma_ch[MAX_DMA_CHANNELS];
153extern struct dma_register *dma_io_base_addr[MAX_DMA_CHANNELS];
154extern int channel2irq(unsigned int channel);
Bryan Wu1394f032007-05-06 14:50:22 -0700155
Mike Frysinger9c417a42009-01-07 23:14:39 +0800156static inline void set_dma_start_addr(unsigned int channel, unsigned long addr)
157{
158 dma_ch[channel].regs->start_addr = addr;
159}
Mike Frysinger6ab729d2009-01-07 23:14:38 +0800160static inline void set_dma_next_desc_addr(unsigned int channel, void *addr)
Mike Frysinger9c417a42009-01-07 23:14:39 +0800161{
162 dma_ch[channel].regs->next_desc_ptr = addr;
163}
Mike Frysinger6ab729d2009-01-07 23:14:38 +0800164static inline void set_dma_curr_desc_addr(unsigned int channel, void *addr)
Mike Frysinger9c417a42009-01-07 23:14:39 +0800165{
166 dma_ch[channel].regs->curr_desc_ptr = addr;
167}
168static inline void set_dma_x_count(unsigned int channel, unsigned short x_count)
169{
170 dma_ch[channel].regs->x_count = x_count;
171}
172static inline void set_dma_y_count(unsigned int channel, unsigned short y_count)
173{
174 dma_ch[channel].regs->y_count = y_count;
175}
176static inline void set_dma_x_modify(unsigned int channel, short x_modify)
177{
178 dma_ch[channel].regs->x_modify = x_modify;
179}
180static inline void set_dma_y_modify(unsigned int channel, short y_modify)
181{
182 dma_ch[channel].regs->y_modify = y_modify;
183}
184static inline void set_dma_config(unsigned int channel, unsigned short config)
185{
186 dma_ch[channel].regs->cfg = config;
187}
188static inline void set_dma_curr_addr(unsigned int channel, unsigned long addr)
189{
190 dma_ch[channel].regs->curr_addr_ptr = addr;
191}
Bryan Wu1394f032007-05-06 14:50:22 -0700192
Mike Frysinger9c417a42009-01-07 23:14:39 +0800193static inline unsigned short
194set_bfin_dma_config(char direction, char flow_mode,
195 char intr_mode, char dma_mode, char width, char syncmode)
196{
197 return (direction << 1) | (width << 2) | (dma_mode << 4) |
198 (intr_mode << 6) | (flow_mode << 12) | (syncmode << 5);
199}
Bryan Wu1394f032007-05-06 14:50:22 -0700200
Mike Frysinger9c417a42009-01-07 23:14:39 +0800201static inline unsigned short get_dma_curr_irqstat(unsigned int channel)
202{
203 return dma_ch[channel].regs->irq_status;
204}
205static inline unsigned short get_dma_curr_xcount(unsigned int channel)
206{
207 return dma_ch[channel].regs->curr_x_count;
208}
209static inline unsigned short get_dma_curr_ycount(unsigned int channel)
210{
211 return dma_ch[channel].regs->curr_y_count;
212}
Mike Frysinger6ab729d2009-01-07 23:14:38 +0800213static inline void *get_dma_next_desc_ptr(unsigned int channel)
Mike Frysinger9c417a42009-01-07 23:14:39 +0800214{
215 return dma_ch[channel].regs->next_desc_ptr;
216}
Mike Frysinger6ab729d2009-01-07 23:14:38 +0800217static inline void *get_dma_curr_desc_ptr(unsigned int channel)
Mike Frysinger9c417a42009-01-07 23:14:39 +0800218{
219 return dma_ch[channel].regs->curr_desc_ptr;
220}
Mike Frysinger71f5ca32009-01-07 23:14:38 +0800221static inline unsigned short get_dma_config(unsigned int channel)
222{
223 return dma_ch[channel].regs->cfg;
224}
Mike Frysinger9c417a42009-01-07 23:14:39 +0800225static inline unsigned long get_dma_curr_addr(unsigned int channel)
226{
227 return dma_ch[channel].regs->curr_addr_ptr;
228}
Bryan Wu1394f032007-05-06 14:50:22 -0700229
Mike Frysinger9c417a42009-01-07 23:14:39 +0800230static inline void set_dma_sg(unsigned int channel, struct dmasg *sg, int ndsize)
231{
Sonic Zhangea8538a2009-06-01 00:49:32 -0400232 /* Make sure the internal data buffers in the core are drained
233 * so that the DMA descriptors are completely written when the
234 * DMA engine goes to fetch them below.
235 */
236 SSYNC();
237
238 dma_ch[channel].regs->next_desc_ptr = sg;
Mike Frysingerd41e8002009-01-07 23:14:38 +0800239 dma_ch[channel].regs->cfg =
240 (dma_ch[channel].regs->cfg & ~(0xf << 8)) |
241 ((ndsize & 0xf) << 8);
Mike Frysinger9c417a42009-01-07 23:14:39 +0800242}
243
244static inline int dma_channel_active(unsigned int channel)
245{
Mike Frysingerd2e015d2009-10-09 22:18:12 +0000246 return atomic_read(&dma_ch[channel].chan_status);
Mike Frysinger9c417a42009-01-07 23:14:39 +0800247}
248
249static inline void disable_dma(unsigned int channel)
250{
251 dma_ch[channel].regs->cfg &= ~DMAEN;
252 SSYNC();
Mike Frysinger9c417a42009-01-07 23:14:39 +0800253}
254static inline void enable_dma(unsigned int channel)
255{
256 dma_ch[channel].regs->curr_x_count = 0;
257 dma_ch[channel].regs->curr_y_count = 0;
258 dma_ch[channel].regs->cfg |= DMAEN;
Mike Frysinger9c417a42009-01-07 23:14:39 +0800259}
Bryan Wu1394f032007-05-06 14:50:22 -0700260void free_dma(unsigned int channel);
Michael McTernan99532fd2009-01-07 23:14:38 +0800261int request_dma(unsigned int channel, const char *device_id);
Mike Frysinger9c417a42009-01-07 23:14:39 +0800262int set_dma_callback(unsigned int channel, irq_handler_t callback, void *data);
263
264static inline void dma_disable_irq(unsigned int channel)
265{
266 disable_irq(dma_ch[channel].irq);
267}
268static inline void dma_enable_irq(unsigned int channel)
269{
270 enable_irq(dma_ch[channel].irq);
271}
272static inline void clear_dma_irqstat(unsigned int channel)
273{
274 dma_ch[channel].regs->irq_status = DMA_DONE | DMA_ERR;
275}
276
Bryan Wu1394f032007-05-06 14:50:22 -0700277void *dma_memcpy(void *dest, const void *src, size_t count);
278void *safe_dma_memcpy(void *dest, const void *src, size_t count);
Mike Frysingerdd3dd382009-01-07 23:14:39 +0800279void blackfin_dma_early_init(void);
Robin Getzfecbd732009-04-23 20:49:43 +0000280void early_dma_memcpy(void *dest, const void *src, size_t count);
281void early_dma_memcpy_done(void);
Bryan Wu1394f032007-05-06 14:50:22 -0700282
Bryan Wu1394f032007-05-06 14:50:22 -0700283#endif