2 * Generic bounce buffer implementation
4 * Copyright (C) 2012 Marek Vasut <marex@denx.de>
6 * See file CREDITS for list of people who contributed to this
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.
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.
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,
28 #include <bouncebuf.h>
30 static int addr_aligned(struct bounce_buffer *state)
32 const ulong align_mask = ARCH_DMA_MINALIGN - 1;
34 /* Check if start is aligned */
35 if ((ulong)state->user_buffer & align_mask) {
36 debug("Unaligned buffer address %p\n", state->user_buffer);
40 /* Check if length is aligned */
41 if (state->len != state->len_aligned) {
42 debug("Unaligned buffer length %d\n", state->len);
50 int bounce_buffer_start(struct bounce_buffer *state, void *data,
51 size_t len, unsigned int flags)
53 state->user_buffer = data;
54 state->bounce_buffer = data;
56 state->len_aligned = roundup(len, ARCH_DMA_MINALIGN);
59 if (!addr_aligned(state)) {
60 state->bounce_buffer = memalign(ARCH_DMA_MINALIGN,
62 if (!state->bounce_buffer)
65 if (state->flags & GEN_BB_READ)
66 memcpy(state->bounce_buffer, state->user_buffer,
71 * Flush data to RAM so DMA reads can pick it up,
72 * and any CPU writebacks don't race with DMA writes
74 flush_dcache_range((unsigned long)state->bounce_buffer,
75 (unsigned long)(state->bounce_buffer) +
81 int bounce_buffer_stop(struct bounce_buffer *state)
83 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) +
90 if (state->bounce_buffer == state->user_buffer)
93 if (state->flags & GEN_BB_WRITE)
94 memcpy(state->user_buffer, state->bounce_buffer, state->len);
96 free(state->bounce_buffer);