blob: 1df12cdda07d04f80632190c09b841e85a774b45 [file] [log] [blame]
Marek Vasutb660df32012-08-26 15:19:06 +00001/*
2 * Generic bounce buffer implementation
3 *
4 * Copyright (C) 2012 Marek Vasut <marex@denx.de>
5 *
6 * See file CREDITS for list of people who contributed to this
7 * project.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
22 * MA 02111-1307 USA
23 */
24
25#include <common.h>
26#include <malloc.h>
27#include <errno.h>
28#include <bouncebuf.h>
29
Stephen Warren84d35b22012-11-06 11:27:29 +000030static int addr_aligned(struct bounce_buffer *state)
Marek Vasutb660df32012-08-26 15:19:06 +000031{
32 const ulong align_mask = ARCH_DMA_MINALIGN - 1;
33
34 /* Check if start is aligned */
Stephen Warren84d35b22012-11-06 11:27:29 +000035 if ((ulong)state->user_buffer & align_mask) {
36 debug("Unaligned buffer address %p\n", state->user_buffer);
Marek Vasutb660df32012-08-26 15:19:06 +000037 return 0;
38 }
39
Stephen Warren84d35b22012-11-06 11:27:29 +000040 /* Check if length is aligned */
41 if (state->len != state->len_aligned) {
42 debug("Unaligned buffer length %d\n", state->len);
Marek Vasutb660df32012-08-26 15:19:06 +000043 return 0;
44 }
45
46 /* Aligned */
47 return 1;
48}
49
Stephen Warren84d35b22012-11-06 11:27:29 +000050int bounce_buffer_start(struct bounce_buffer *state, void *data,
51 size_t len, unsigned int flags)
Marek Vasutb660df32012-08-26 15:19:06 +000052{
Stephen Warren84d35b22012-11-06 11:27:29 +000053 state->user_buffer = data;
54 state->bounce_buffer = data;
55 state->len = len;
56 state->len_aligned = roundup(len, ARCH_DMA_MINALIGN);
57 state->flags = flags;
Marek Vasutb660df32012-08-26 15:19:06 +000058
Stephen Warren84d35b22012-11-06 11:27:29 +000059 if (!addr_aligned(state)) {
60 state->bounce_buffer = memalign(ARCH_DMA_MINALIGN,
61 state->len_aligned);
62 if (!state->bounce_buffer)
63 return -ENOMEM;
64
65 if (state->flags & GEN_BB_READ)
66 memcpy(state->bounce_buffer, state->user_buffer,
67 state->len);
Marek Vasutb660df32012-08-26 15:19:06 +000068 }
69
Stephen Warren84d35b22012-11-06 11:27:29 +000070 /*
71 * Flush data to RAM so DMA reads can pick it up,
72 * and any CPU writebacks don't race with DMA writes
73 */
74 flush_dcache_range((unsigned long)state->bounce_buffer,
75 (unsigned long)(state->bounce_buffer) +
76 state->len_aligned);
Marek Vasutb660df32012-08-26 15:19:06 +000077
78 return 0;
79}
80
Stephen Warren84d35b22012-11-06 11:27:29 +000081int bounce_buffer_stop(struct bounce_buffer *state)
Marek Vasutb660df32012-08-26 15:19:06 +000082{
Stephen Warren84d35b22012-11-06 11:27:29 +000083 if (state->flags & GEN_BB_WRITE) {
84 /* Invalidate cache so that CPU can see any newly DMA'd data */
85 invalidate_dcache_range((unsigned long)state->bounce_buffer,
86 (unsigned long)(state->bounce_buffer) +
87 state->len_aligned);
88 }
Marek Vasutb660df32012-08-26 15:19:06 +000089
Stephen Warren84d35b22012-11-06 11:27:29 +000090 if (state->bounce_buffer == state->user_buffer)
Marek Vasutb660df32012-08-26 15:19:06 +000091 return 0;
92
Stephen Warren84d35b22012-11-06 11:27:29 +000093 if (state->flags & GEN_BB_WRITE)
94 memcpy(state->user_buffer, state->bounce_buffer, state->len);
Marek Vasutb660df32012-08-26 15:19:06 +000095
Stephen Warren84d35b22012-11-06 11:27:29 +000096 free(state->bounce_buffer);
Marek Vasutb660df32012-08-26 15:19:06 +000097
98 return 0;
99}