Blame view

arch/blackfin/kernel/bfin_dma_5xx.c 13.8 KB
1394f0322   Bryan Wu   blackfin architec...
1
  /*
dd3dd384d   Mike Frysinger   Blackfin arch: re...
2
   * bfin_dma_5xx.c - Blackfin DMA implementation
1394f0322   Bryan Wu   blackfin architec...
3
   *
9c417a432   Mike Frysinger   Blackfin arch: mo...
4
   * Copyright 2004-2008 Analog Devices Inc.
96f1050d3   Robin Getz   Blackfin: mass cl...
5
   *
dd3dd384d   Mike Frysinger   Blackfin arch: re...
6
   * Licensed under the GPL-2 or later.
1394f0322   Bryan Wu   blackfin architec...
7
8
9
   */
  
  #include <linux/errno.h>
dd3dd384d   Mike Frysinger   Blackfin arch: re...
10
11
  #include <linux/interrupt.h>
  #include <linux/kernel.h>
1394f0322   Bryan Wu   blackfin architec...
12
  #include <linux/module.h>
dd3dd384d   Mike Frysinger   Blackfin arch: re...
13
  #include <linux/param.h>
d642a8ad5   Graf Yang   Blackfin arch: im...
14
  #include <linux/proc_fs.h>
1394f0322   Bryan Wu   blackfin architec...
15
  #include <linux/sched.h>
d642a8ad5   Graf Yang   Blackfin arch: im...
16
  #include <linux/seq_file.h>
dd3dd384d   Mike Frysinger   Blackfin arch: re...
17
  #include <linux/spinlock.h>
1394f0322   Bryan Wu   blackfin architec...
18

24a07a124   Roy Huang   Blackfin arch: in...
19
  #include <asm/blackfin.h>
1394f0322   Bryan Wu   blackfin architec...
20
  #include <asm/cacheflush.h>
dd3dd384d   Mike Frysinger   Blackfin arch: re...
21
22
  #include <asm/dma.h>
  #include <asm/uaccess.h>
837ec2d56   Robin Getz   Blackfin: catch h...
23
  #include <asm/early_printk.h>
1394f0322   Bryan Wu   blackfin architec...
24

76068c3c5   Robin Getz   Blackfin: annotat...
25
26
27
28
  /*
   * To make sure we work around 05000119 - we always check DMA_DONE bit,
   * never the DMA_RUN bit
   */
9c417a432   Mike Frysinger   Blackfin arch: mo...
29
30
  struct dma_channel dma_ch[MAX_DMA_CHANNELS];
  EXPORT_SYMBOL(dma_ch);
1394f0322   Bryan Wu   blackfin architec...
31

a161bb051   Mike Frysinger   Blackfin arch: ma...
32
  static int __init blackfin_dma_init(void)
1394f0322   Bryan Wu   blackfin architec...
33
34
35
36
37
  {
  	int i;
  
  	printk(KERN_INFO "Blackfin DMA Controller
  ");
f9691bb96   Steven Miao   Blackfin: work ar...
38
39
40
41
  
  #if ANOMALY_05000480
  	bfin_write_DMAC_TC_PER(0x0111);
  #endif
211daf9d7   Mike Frysinger   Blackfin arch: re...
42
  	for (i = 0; i < MAX_DMA_CHANNELS; i++) {
d2e015d65   Mike Frysinger   Blackfin: convert...
43
  		atomic_set(&dma_ch[i].chan_status, 0);
779556649   Bernd Schmidt   [Blackfin] arch: ...
44
  		dma_ch[i].regs = dma_io_base_addr[i];
1394f0322   Bryan Wu   blackfin architec...
45
  	}
23ee968d9   Michael Hennerich   Blackfin arch: DM...
46
  	/* Mark MEMDMA Channel 0 as requested since we're using it internally */
d642a8ad5   Graf Yang   Blackfin arch: im...
47
48
  	request_dma(CH_MEM_STREAM0_DEST, "Blackfin dma_memcpy");
  	request_dma(CH_MEM_STREAM0_SRC, "Blackfin dma_memcpy");
a924db7c0   Michael Hennerich   Blackfin arch: Ad...
49
50
51
52
53
  
  #if defined(CONFIG_DEB_DMA_URGENT)
  	bfin_write_EBIU_DDRQUE(bfin_read_EBIU_DDRQUE()
  			 | DEB1_URGENT | DEB2_URGENT | DEB3_URGENT);
  #endif
d642a8ad5   Graf Yang   Blackfin arch: im...
54

1394f0322   Bryan Wu   blackfin architec...
55
56
  	return 0;
  }
1394f0322   Bryan Wu   blackfin architec...
57
  arch_initcall(blackfin_dma_init);
d642a8ad5   Graf Yang   Blackfin arch: im...
58
  #ifdef CONFIG_PROC_FS
d642a8ad5   Graf Yang   Blackfin arch: im...
59
60
61
  static int proc_dma_show(struct seq_file *m, void *v)
  {
  	int i;
dd3dd384d   Mike Frysinger   Blackfin arch: re...
62
  	for (i = 0; i < MAX_DMA_CHANNELS; ++i)
d2e015d65   Mike Frysinger   Blackfin: convert...
63
  		if (dma_channel_active(i))
d642a8ad5   Graf Yang   Blackfin arch: im...
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
  			seq_printf(m, "%2d: %s
  ", i, dma_ch[i].device_id);
  
  	return 0;
  }
  
  static int proc_dma_open(struct inode *inode, struct file *file)
  {
  	return single_open(file, proc_dma_show, NULL);
  }
  
  static const struct file_operations proc_dma_operations = {
  	.open		= proc_dma_open,
  	.read		= seq_read,
  	.llseek		= seq_lseek,
  	.release	= single_release,
  };
  
  static int __init proc_dma_init(void)
  {
  	return proc_create("dma", 0, NULL, &proc_dma_operations) != NULL;
  }
  late_initcall(proc_dma_init);
  #endif
55835175a   steven miao   Blackfin: bf54x: ...
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
  static void set_dma_peripheral_map(unsigned int channel, const char *device_id)
  {
  #ifdef CONFIG_BF54x
  	unsigned int per_map;
  
  	switch (channel) {
  		case CH_UART2_RX: per_map = 0xC << 12; break;
  		case CH_UART2_TX: per_map = 0xD << 12; break;
  		case CH_UART3_RX: per_map = 0xE << 12; break;
  		case CH_UART3_TX: per_map = 0xF << 12; break;
  		default:          return;
  	}
  
  	if (strncmp(device_id, "BFIN_UART", 9) == 0)
  		dma_ch[channel].regs->peripheral_map = per_map;
  #endif
  }
9c417a432   Mike Frysinger   Blackfin arch: mo...
105
106
107
108
109
  /**
   *	request_dma - request a DMA channel
   *
   * Request the specific DMA channel from the system if it's available.
   */
99532fd2a   Michael McTernan   Blackfin arch: ad...
110
  int request_dma(unsigned int channel, const char *device_id)
1394f0322   Bryan Wu   blackfin architec...
111
  {
2bc4affe9   Frans Pop   Blackfin: remove ...
112
113
  	pr_debug("request_dma() : BEGIN
  ");
d642a8ad5   Graf Yang   Blackfin arch: im...
114
115
116
117
  
  	if (device_id == NULL)
  		printk(KERN_WARNING "request_dma(%u): no device_id given
  ", channel);
5ce998cf6   Michael Hennerich   Blackfin arch: Ch...
118
119
120
121
122
123
124
125
126
127
128
  
  #if defined(CONFIG_BF561) && ANOMALY_05000182
  	if (channel >= CH_IMEM_STREAM0_DEST && channel <= CH_IMEM_STREAM1_DEST) {
  		if (get_cclk() > 500000000) {
  			printk(KERN_WARNING
  			       "Request IMDMA failed due to ANOMALY 05000182
  ");
  			return -EFAULT;
  		}
  	}
  #endif
d2e015d65   Mike Frysinger   Blackfin: convert...
129
  	if (atomic_cmpxchg(&dma_ch[channel].chan_status, 0, 1)) {
2bc4affe9   Frans Pop   Blackfin: remove ...
130
131
  		pr_debug("DMA CHANNEL IN USE
  ");
1394f0322   Bryan Wu   blackfin architec...
132
  		return -EBUSY;
1394f0322   Bryan Wu   blackfin architec...
133
  	}
55835175a   steven miao   Blackfin: bf54x: ...
134
  	set_dma_peripheral_map(channel, device_id);
1394f0322   Bryan Wu   blackfin architec...
135
  	dma_ch[channel].device_id = device_id;
9b011407d   Mike Frysinger   Blackfin arch: dr...
136
  	dma_ch[channel].irq = 0;
1394f0322   Bryan Wu   blackfin architec...
137
138
139
140
141
  
  	/* This is to be enabled by putting a restriction -
  	 * you have to request DMA, before doing any operations on
  	 * descriptor/channel
  	 */
2bc4affe9   Frans Pop   Blackfin: remove ...
142
143
  	pr_debug("request_dma() : END
  ");
596b565bd   Mike Frysinger   Blackfin arch: re...
144
  	return 0;
1394f0322   Bryan Wu   blackfin architec...
145
146
  }
  EXPORT_SYMBOL(request_dma);
68532bdac   Mike Frysinger   Blackfin arch: dr...
147
  int set_dma_callback(unsigned int channel, irq_handler_t callback, void *data)
1394f0322   Bryan Wu   blackfin architec...
148
  {
e34132f40   Mike Frysinger   Blackfin: reject ...
149
150
151
152
  	int ret;
  	unsigned int irq;
  
  	BUG_ON(channel >= MAX_DMA_CHANNELS || !callback ||
d2e015d65   Mike Frysinger   Blackfin: convert...
153
  			!atomic_read(&dma_ch[channel].chan_status));
1394f0322   Bryan Wu   blackfin architec...
154

e34132f40   Mike Frysinger   Blackfin: reject ...
155
156
157
158
  	irq = channel2irq(channel);
  	ret = request_irq(irq, callback, 0, dma_ch[channel].device_id, data);
  	if (ret)
  		return ret;
1394f0322   Bryan Wu   blackfin architec...
159

e34132f40   Mike Frysinger   Blackfin: reject ...
160
161
  	dma_ch[channel].irq = irq;
  	dma_ch[channel].data = data;
8f1cc2338   Mike Frysinger   Blackfin arch: se...
162

1394f0322   Bryan Wu   blackfin architec...
163
164
165
  	return 0;
  }
  EXPORT_SYMBOL(set_dma_callback);
9c417a432   Mike Frysinger   Blackfin arch: mo...
166
167
168
169
170
171
172
173
174
175
176
177
  /**
   *	clear_dma_buffer - clear DMA fifos for specified channel
   *
   * Set the Buffer Clear bit in the Configuration register of specific DMA
   * channel. This will stop the descriptor based DMA operation.
   */
  static void clear_dma_buffer(unsigned int channel)
  {
  	dma_ch[channel].regs->cfg |= RESTART;
  	SSYNC();
  	dma_ch[channel].regs->cfg &= ~RESTART;
  }
1394f0322   Bryan Wu   blackfin architec...
178
179
  void free_dma(unsigned int channel)
  {
2bc4affe9   Frans Pop   Blackfin: remove ...
180
181
  	pr_debug("freedma() : BEGIN
  ");
ac860751e   Roel Kluin   Blackfin: fix rea...
182
  	BUG_ON(channel >= MAX_DMA_CHANNELS ||
d2e015d65   Mike Frysinger   Blackfin: convert...
183
  			!atomic_read(&dma_ch[channel].chan_status));
1394f0322   Bryan Wu   blackfin architec...
184
185
186
187
  
  	/* Halt the DMA */
  	disable_dma(channel);
  	clear_dma_buffer(channel);
9b011407d   Mike Frysinger   Blackfin arch: dr...
188
  	if (dma_ch[channel].irq)
a2ba8b199   Michael Hennerich   Blackfin arch: lo...
189
  		free_irq(dma_ch[channel].irq, dma_ch[channel].data);
1394f0322   Bryan Wu   blackfin architec...
190
191
  
  	/* Clear the DMA Variable in the Channel */
d2e015d65   Mike Frysinger   Blackfin: convert...
192
  	atomic_set(&dma_ch[channel].chan_status, 0);
1394f0322   Bryan Wu   blackfin architec...
193

2bc4affe9   Frans Pop   Blackfin: remove ...
194
195
  	pr_debug("freedma() : END
  ");
1394f0322   Bryan Wu   blackfin architec...
196
197
  }
  EXPORT_SYMBOL(free_dma);
1efc80b53   Michael Hennerich   Blackfin arch: Fu...
198
  #ifdef CONFIG_PM
c9e0020d4   Mike Frysinger   Blackfin arch: pu...
199
200
201
  # ifndef MAX_DMA_SUSPEND_CHANNELS
  #  define MAX_DMA_SUSPEND_CHANNELS MAX_DMA_CHANNELS
  # endif
1efc80b53   Michael Hennerich   Blackfin arch: Fu...
202
203
204
  int blackfin_dma_suspend(void)
  {
  	int i;
d2e015d65   Mike Frysinger   Blackfin: convert...
205
206
  	for (i = 0; i < MAX_DMA_CHANNELS; ++i) {
  		if (dma_ch[i].regs->cfg & DMAEN) {
1efc80b53   Michael Hennerich   Blackfin arch: Fu...
207
208
209
210
  			printk(KERN_ERR "DMA Channel %d failed to suspend
  ", i);
  			return -EBUSY;
  		}
d2e015d65   Mike Frysinger   Blackfin: convert...
211
212
  		if (i < MAX_DMA_SUSPEND_CHANNELS)
  			dma_ch[i].saved_peripheral_map = dma_ch[i].regs->peripheral_map;
1efc80b53   Michael Hennerich   Blackfin arch: Fu...
213
214
215
216
217
218
219
220
  	}
  
  	return 0;
  }
  
  void blackfin_dma_resume(void)
  {
  	int i;
865bddfbf   Michael Hennerich   Blackfin: fix sus...
221
222
223
224
225
226
227
  
  	for (i = 0; i < MAX_DMA_CHANNELS; ++i) {
  		dma_ch[i].regs->cfg = 0;
  
  		if (i < MAX_DMA_SUSPEND_CHANNELS)
  			dma_ch[i].regs->peripheral_map = dma_ch[i].saved_peripheral_map;
  	}
1efc80b53   Michael Hennerich   Blackfin arch: Fu...
228
229
  }
  #endif
dd3dd384d   Mike Frysinger   Blackfin arch: re...
230
231
232
233
234
235
236
  /**
   *	blackfin_dma_early_init - minimal DMA init
   *
   * Setup a few DMA registers so we can safely do DMA transfers early on in
   * the kernel booting process.  Really this just means using dma_memcpy().
   */
  void __init blackfin_dma_early_init(void)
1394f0322   Bryan Wu   blackfin architec...
237
  {
837ec2d56   Robin Getz   Blackfin: catch h...
238
  	early_shadow_stamp();
1394f0322   Bryan Wu   blackfin architec...
239
  	bfin_write_MDMA_S0_CONFIG(0);
fecbd7366   Robin Getz   Blackfin: fix ear...
240
241
242
243
244
245
246
247
  	bfin_write_MDMA_S1_CONFIG(0);
  }
  
  void __init early_dma_memcpy(void *pdst, const void *psrc, size_t size)
  {
  	unsigned long dst = (unsigned long)pdst;
  	unsigned long src = (unsigned long)psrc;
  	struct dma_register *dst_ch, *src_ch;
837ec2d56   Robin Getz   Blackfin: catch h...
248
  	early_shadow_stamp();
fecbd7366   Robin Getz   Blackfin: fix ear...
249
250
251
252
253
254
  	/* We assume that everything is 4 byte aligned, so include
  	 * a basic sanity check
  	 */
  	BUG_ON(dst % 4);
  	BUG_ON(src % 4);
  	BUG_ON(size % 4);
fecbd7366   Robin Getz   Blackfin: fix ear...
255
256
257
  	src_ch = 0;
  	/* Find an avalible memDMA channel */
  	while (1) {
532f07ca0   Mike Frysinger   Blackfin: fix ear...
258
  		if (src_ch == (struct dma_register *)MDMA_S0_NEXT_DESC_PTR) {
fecbd7366   Robin Getz   Blackfin: fix ear...
259
260
  			dst_ch = (struct dma_register *)MDMA_D1_NEXT_DESC_PTR;
  			src_ch = (struct dma_register *)MDMA_S1_NEXT_DESC_PTR;
532f07ca0   Mike Frysinger   Blackfin: fix ear...
261
262
263
  		} else {
  			dst_ch = (struct dma_register *)MDMA_D0_NEXT_DESC_PTR;
  			src_ch = (struct dma_register *)MDMA_S0_NEXT_DESC_PTR;
fecbd7366   Robin Getz   Blackfin: fix ear...
264
  		}
532f07ca0   Mike Frysinger   Blackfin: fix ear...
265
266
267
268
  		if (!bfin_read16(&src_ch->cfg))
  			break;
  		else if (bfin_read16(&dst_ch->irq_status) & DMA_DONE) {
  			bfin_write16(&src_ch->cfg, 0);
fecbd7366   Robin Getz   Blackfin: fix ear...
269
  			break;
fecbd7366   Robin Getz   Blackfin: fix ear...
270
  		}
fecbd7366   Robin Getz   Blackfin: fix ear...
271
  	}
532f07ca0   Mike Frysinger   Blackfin: fix ear...
272
273
274
275
276
  	/* Force a sync in case a previous config reset on this channel
  	 * occurred.  This is needed so subsequent writes to DMA registers
  	 * are not spuriously lost/corrupted.
  	 */
  	__builtin_bfin_ssync();
fecbd7366   Robin Getz   Blackfin: fix ear...
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
  	/* Destination */
  	bfin_write32(&dst_ch->start_addr, dst);
  	bfin_write16(&dst_ch->x_count, size >> 2);
  	bfin_write16(&dst_ch->x_modify, 1 << 2);
  	bfin_write16(&dst_ch->irq_status, DMA_DONE | DMA_ERR);
  
  	/* Source */
  	bfin_write32(&src_ch->start_addr, src);
  	bfin_write16(&src_ch->x_count, size >> 2);
  	bfin_write16(&src_ch->x_modify, 1 << 2);
  	bfin_write16(&src_ch->irq_status, DMA_DONE | DMA_ERR);
  
  	/* Enable */
  	bfin_write16(&src_ch->cfg, DMAEN | WDSIZE_32);
  	bfin_write16(&dst_ch->cfg, WNR | DI_EN | DMAEN | WDSIZE_32);
  
  	/* Since we are atomic now, don't use the workaround ssync */
  	__builtin_bfin_ssync();
  }
  
  void __init early_dma_memcpy_done(void)
  {
837ec2d56   Robin Getz   Blackfin: catch h...
299
  	early_shadow_stamp();
fecbd7366   Robin Getz   Blackfin: fix ear...
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
  	while ((bfin_read_MDMA_S0_CONFIG() && !(bfin_read_MDMA_D0_IRQ_STATUS() & DMA_DONE)) ||
  	       (bfin_read_MDMA_S1_CONFIG() && !(bfin_read_MDMA_D1_IRQ_STATUS() & DMA_DONE)))
  		continue;
  
  	bfin_write_MDMA_D0_IRQ_STATUS(DMA_DONE | DMA_ERR);
  	bfin_write_MDMA_D1_IRQ_STATUS(DMA_DONE | DMA_ERR);
  	/*
  	 * Now that DMA is done, we would normally flush cache, but
  	 * i/d cache isn't running this early, so we don't bother,
  	 * and just clear out the DMA channel for next time
  	 */
  	bfin_write_MDMA_S0_CONFIG(0);
  	bfin_write_MDMA_S1_CONFIG(0);
  	bfin_write_MDMA_D0_CONFIG(0);
  	bfin_write_MDMA_D1_CONFIG(0);
  
  	__builtin_bfin_ssync();
1394f0322   Bryan Wu   blackfin architec...
317
  }
5f9a3e899   Aubrey Li   Blackfin arch: dm...
318

49946e732   Mike Frysinger   Blackfin arch: ch...
319
  /**
dd3dd384d   Mike Frysinger   Blackfin arch: re...
320
   *	__dma_memcpy - program the MDMA registers
49946e732   Mike Frysinger   Blackfin arch: ch...
321
   *
dd3dd384d   Mike Frysinger   Blackfin arch: re...
322
323
324
325
   * Actually program MDMA0 and wait for the transfer to finish.  Disable IRQs
   * while programming registers so that everything is fully configured.  Wait
   * for DMA to finish with IRQs enabled.  If interrupted, the initial DMA_DONE
   * check will make sure we don't clobber any existing transfer.
49946e732   Mike Frysinger   Blackfin arch: ch...
326
   */
dd3dd384d   Mike Frysinger   Blackfin arch: re...
327
  static void __dma_memcpy(u32 daddr, s16 dmod, u32 saddr, s16 smod, size_t cnt, u32 conf)
23ee968d9   Michael Hennerich   Blackfin arch: DM...
328
  {
dd3dd384d   Mike Frysinger   Blackfin arch: re...
329
  	static DEFINE_SPINLOCK(mdma_lock);
23ee968d9   Michael Hennerich   Blackfin arch: DM...
330
  	unsigned long flags;
1f83b8f14   Mike Frysinger   Blackfin arch: cl...
331

dd3dd384d   Mike Frysinger   Blackfin arch: re...
332
  	spin_lock_irqsave(&mdma_lock, flags);
41245ac59   Mike Frysinger   Blackfin arch: fi...
333
334
335
336
337
338
  	/* Force a sync in case a previous config reset on this channel
  	 * occurred.  This is needed so subsequent writes to DMA registers
  	 * are not spuriously lost/corrupted.  Do it under irq lock and
  	 * without the anomaly version (because we are atomic already).
  	 */
  	__builtin_bfin_ssync();
dd3dd384d   Mike Frysinger   Blackfin arch: re...
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
  	if (bfin_read_MDMA_S0_CONFIG())
  		while (!(bfin_read_MDMA_D0_IRQ_STATUS() & DMA_DONE))
  			continue;
  
  	if (conf & DMA2D) {
  		/* For larger bit sizes, we've already divided down cnt so it
  		 * is no longer a multiple of 64k.  So we have to break down
  		 * the limit here so it is a multiple of the incoming size.
  		 * There is no limitation here in terms of total size other
  		 * than the hardware though as the bits lost in the shift are
  		 * made up by MODIFY (== we can hit the whole address space).
  		 * X: (2^(16 - 0)) * 1 == (2^(16 - 1)) * 2 == (2^(16 - 2)) * 4
  		 */
  		u32 shift = abs(dmod) >> 1;
  		size_t ycnt = cnt >> (16 - shift);
  		cnt = 1 << (16 - shift);
  		bfin_write_MDMA_D0_Y_COUNT(ycnt);
  		bfin_write_MDMA_S0_Y_COUNT(ycnt);
  		bfin_write_MDMA_D0_Y_MODIFY(dmod);
  		bfin_write_MDMA_S0_Y_MODIFY(smod);
  	}
1f83b8f14   Mike Frysinger   Blackfin arch: cl...
360

dd3dd384d   Mike Frysinger   Blackfin arch: re...
361
362
363
  	bfin_write_MDMA_D0_START_ADDR(daddr);
  	bfin_write_MDMA_D0_X_COUNT(cnt);
  	bfin_write_MDMA_D0_X_MODIFY(dmod);
23ee968d9   Michael Hennerich   Blackfin arch: DM...
364
  	bfin_write_MDMA_D0_IRQ_STATUS(DMA_DONE | DMA_ERR);
dd3dd384d   Mike Frysinger   Blackfin arch: re...
365
366
367
  	bfin_write_MDMA_S0_START_ADDR(saddr);
  	bfin_write_MDMA_S0_X_COUNT(cnt);
  	bfin_write_MDMA_S0_X_MODIFY(smod);
23ee968d9   Michael Hennerich   Blackfin arch: DM...
368
  	bfin_write_MDMA_S0_IRQ_STATUS(DMA_DONE | DMA_ERR);
dd3dd384d   Mike Frysinger   Blackfin arch: re...
369
370
371
372
  	bfin_write_MDMA_S0_CONFIG(DMAEN | conf);
  	bfin_write_MDMA_D0_CONFIG(WNR | DI_EN | DMAEN | conf);
  
  	spin_unlock_irqrestore(&mdma_lock, flags);
23ee968d9   Michael Hennerich   Blackfin arch: DM...
373

1a7d91d65   Michael Hennerich   Blackfin arch: fl...
374
  	SSYNC();
dd3dd384d   Mike Frysinger   Blackfin arch: re...
375
376
377
378
379
  	while (!(bfin_read_MDMA_D0_IRQ_STATUS() & DMA_DONE))
  		if (bfin_read_MDMA_S0_CONFIG())
  			continue;
  		else
  			return;
23ee968d9   Michael Hennerich   Blackfin arch: DM...
380
381
382
383
384
  
  	bfin_write_MDMA_D0_IRQ_STATUS(DMA_DONE | DMA_ERR);
  
  	bfin_write_MDMA_S0_CONFIG(0);
  	bfin_write_MDMA_D0_CONFIG(0);
23ee968d9   Michael Hennerich   Blackfin arch: DM...
385
  }
23ee968d9   Michael Hennerich   Blackfin arch: DM...
386

dd3dd384d   Mike Frysinger   Blackfin arch: re...
387
388
389
390
  /**
   *	_dma_memcpy - translate C memcpy settings into MDMA settings
   *
   * Handle all the high level steps before we touch the MDMA registers.  So
7ad883a94   Mike Frysinger   Blackfin arch: pu...
391
   * handle direction, tweaking of sizes, and formatting of addresses.
dd3dd384d   Mike Frysinger   Blackfin arch: re...
392
393
   */
  static void *_dma_memcpy(void *pdst, const void *psrc, size_t size)
23ee968d9   Michael Hennerich   Blackfin arch: DM...
394
  {
dd3dd384d   Mike Frysinger   Blackfin arch: re...
395
396
397
398
  	u32 conf, shift;
  	s16 mod;
  	unsigned long dst = (unsigned long)pdst;
  	unsigned long src = (unsigned long)psrc;
1f83b8f14   Mike Frysinger   Blackfin arch: cl...
399

dd3dd384d   Mike Frysinger   Blackfin arch: re...
400
401
  	if (size == 0)
  		return NULL;
1a7d91d65   Michael Hennerich   Blackfin arch: fl...
402

dd3dd384d   Mike Frysinger   Blackfin arch: re...
403
404
405
406
407
408
409
410
411
412
  	if (dst % 4 == 0 && src % 4 == 0 && size % 4 == 0) {
  		conf = WDSIZE_32;
  		shift = 2;
  	} else if (dst % 2 == 0 && src % 2 == 0 && size % 2 == 0) {
  		conf = WDSIZE_16;
  		shift = 1;
  	} else {
  		conf = WDSIZE_8;
  		shift = 0;
  	}
23ee968d9   Michael Hennerich   Blackfin arch: DM...
413

dd3dd384d   Mike Frysinger   Blackfin arch: re...
414
415
416
417
418
419
420
421
422
423
424
  	/* If the two memory regions have a chance of overlapping, make
  	 * sure the memcpy still works as expected.  Do this by having the
  	 * copy run backwards instead.
  	 */
  	mod = 1 << shift;
  	if (src < dst) {
  		mod *= -1;
  		dst += size + mod;
  		src += size + mod;
  	}
  	size >>= shift;
23ee968d9   Michael Hennerich   Blackfin arch: DM...
425

dd3dd384d   Mike Frysinger   Blackfin arch: re...
426
427
  	if (size > 0x10000)
  		conf |= DMA2D;
23ee968d9   Michael Hennerich   Blackfin arch: DM...
428

dd3dd384d   Mike Frysinger   Blackfin arch: re...
429
  	__dma_memcpy(dst, mod, src, mod, size, conf);
23ee968d9   Michael Hennerich   Blackfin arch: DM...
430

dd3dd384d   Mike Frysinger   Blackfin arch: re...
431
  	return pdst;
23ee968d9   Michael Hennerich   Blackfin arch: DM...
432
  }
23ee968d9   Michael Hennerich   Blackfin arch: DM...
433

dd3dd384d   Mike Frysinger   Blackfin arch: re...
434
435
436
437
438
439
440
  /**
   *	dma_memcpy - DMA memcpy under mutex lock
   *
   * Do not check arguments before starting the DMA memcpy.  Break the transfer
   * up into two pieces.  The first transfer is in multiples of 64k and the
   * second transfer is the piece smaller than 64k.
   */
7ad883a94   Mike Frysinger   Blackfin arch: pu...
441
  void *dma_memcpy(void *pdst, const void *psrc, size_t size)
23ee968d9   Michael Hennerich   Blackfin arch: DM...
442
  {
7ad883a94   Mike Frysinger   Blackfin arch: pu...
443
444
  	unsigned long dst = (unsigned long)pdst;
  	unsigned long src = (unsigned long)psrc;
7ad883a94   Mike Frysinger   Blackfin arch: pu...
445

67834fa93   Jie Zhang   Blackfin: rename ...
446
  	if (bfin_addr_dcacheable(src))
7ad883a94   Mike Frysinger   Blackfin arch: pu...
447
  		blackfin_dcache_flush_range(src, src + size);
67834fa93   Jie Zhang   Blackfin: rename ...
448
  	if (bfin_addr_dcacheable(dst))
7ad883a94   Mike Frysinger   Blackfin arch: pu...
449
  		blackfin_dcache_invalidate_range(dst, dst + size);
d1401e1dc   Michael Hennerich   Blackfin: fix DMA...
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
  	return dma_memcpy_nocache(pdst, psrc, size);
  }
  EXPORT_SYMBOL(dma_memcpy);
  
  /**
   *	dma_memcpy_nocache - DMA memcpy under mutex lock
   *	- No cache flush/invalidate
   *
   * Do not check arguments before starting the DMA memcpy.  Break the transfer
   * up into two pieces.  The first transfer is in multiples of 64k and the
   * second transfer is the piece smaller than 64k.
   */
  void *dma_memcpy_nocache(void *pdst, const void *psrc, size_t size)
  {
  	size_t bulk, rest;
dd3dd384d   Mike Frysinger   Blackfin arch: re...
465
466
467
  	bulk = size & ~0xffff;
  	rest = size - bulk;
  	if (bulk)
7ad883a94   Mike Frysinger   Blackfin arch: pu...
468
469
470
  		_dma_memcpy(pdst, psrc, bulk);
  	_dma_memcpy(pdst + bulk, psrc + bulk, rest);
  	return pdst;
23ee968d9   Michael Hennerich   Blackfin arch: DM...
471
  }
d1401e1dc   Michael Hennerich   Blackfin: fix DMA...
472
  EXPORT_SYMBOL(dma_memcpy_nocache);
23ee968d9   Michael Hennerich   Blackfin arch: DM...
473

dd3dd384d   Mike Frysinger   Blackfin arch: re...
474
475
476
477
478
479
  /**
   *	safe_dma_memcpy - DMA memcpy w/argument checking
   *
   * Verify arguments are safe before heading to dma_memcpy().
   */
  void *safe_dma_memcpy(void *dst, const void *src, size_t size)
23ee968d9   Michael Hennerich   Blackfin arch: DM...
480
  {
dd3dd384d   Mike Frysinger   Blackfin arch: re...
481
482
483
484
485
  	if (!access_ok(VERIFY_WRITE, dst, size))
  		return NULL;
  	if (!access_ok(VERIFY_READ, src, size))
  		return NULL;
  	return dma_memcpy(dst, src, size);
23ee968d9   Michael Hennerich   Blackfin arch: DM...
486
  }
dd3dd384d   Mike Frysinger   Blackfin arch: re...
487
  EXPORT_SYMBOL(safe_dma_memcpy);
23ee968d9   Michael Hennerich   Blackfin arch: DM...
488

dd3dd384d   Mike Frysinger   Blackfin arch: re...
489
490
  static void _dma_out(unsigned long addr, unsigned long buf, unsigned short len,
                       u16 size, u16 dma_size)
23ee968d9   Michael Hennerich   Blackfin arch: DM...
491
  {
dd3dd384d   Mike Frysinger   Blackfin arch: re...
492
493
  	blackfin_dcache_flush_range(buf, buf + len * size);
  	__dma_memcpy(addr, 0, buf, size, len, dma_size);
23ee968d9   Michael Hennerich   Blackfin arch: DM...
494
  }
23ee968d9   Michael Hennerich   Blackfin arch: DM...
495

dd3dd384d   Mike Frysinger   Blackfin arch: re...
496
497
  static void _dma_in(unsigned long addr, unsigned long buf, unsigned short len,
                      u16 size, u16 dma_size)
23ee968d9   Michael Hennerich   Blackfin arch: DM...
498
  {
dd3dd384d   Mike Frysinger   Blackfin arch: re...
499
500
  	blackfin_dcache_invalidate_range(buf, buf + len * size);
  	__dma_memcpy(buf, size, addr, 0, len, dma_size);
23ee968d9   Michael Hennerich   Blackfin arch: DM...
501
  }
dd3dd384d   Mike Frysinger   Blackfin arch: re...
502
503
504
505
506
507
508
509
510
511
512
513
514
  
  #define MAKE_DMA_IO(io, bwl, isize, dmasize, cnst) \
  void dma_##io##s##bwl(unsigned long addr, cnst void *buf, unsigned short len) \
  { \
  	_dma_##io(addr, (unsigned long)buf, len, isize, WDSIZE_##dmasize); \
  } \
  EXPORT_SYMBOL(dma_##io##s##bwl)
  MAKE_DMA_IO(out, b, 1,  8, const);
  MAKE_DMA_IO(in,  b, 1,  8, );
  MAKE_DMA_IO(out, w, 2, 16, const);
  MAKE_DMA_IO(in,  w, 2, 16, );
  MAKE_DMA_IO(out, l, 4, 32, const);
  MAKE_DMA_IO(in,  l, 4, 32, );