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(void *data, size_t len)
32 const ulong align_mask = ARCH_DMA_MINALIGN - 1;
34 /* Check if start is aligned */
35 if ((ulong)data & align_mask) {
36 debug("Unaligned start address %p\n", data);
42 /* Check if end is aligned */
43 if ((ulong)data & align_mask) {
44 debug("Unaligned end address %p\n", data);
52 int bounce_buffer_start(void **data, size_t len, void **backup, uint8_t flags)
57 if (addr_aligned(*data, len)) {
62 alen = roundup(len, ARCH_DMA_MINALIGN);
63 tmp = memalign(ARCH_DMA_MINALIGN, alen);
68 if (flags & GEN_BB_READ)
69 memcpy(tmp, *data, len);
77 int bounce_buffer_stop(void **data, size_t len, void **backup, uint8_t flags)
81 /* The buffer was already aligned, since "backup" is NULL. */
85 if (flags & GEN_BB_WRITE)
86 memcpy(*backup, *data, len);