Marek Vasut | b660df3 | 2012-08-26 15:19:06 +0000 | [diff] [blame] | 1 | /* |
| 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 Warren | 84d35b2 | 2012-11-06 11:27:29 +0000 | [diff] [blame^] | 30 | static int addr_aligned(struct bounce_buffer *state) |
Marek Vasut | b660df3 | 2012-08-26 15:19:06 +0000 | [diff] [blame] | 31 | { |
| 32 | const ulong align_mask = ARCH_DMA_MINALIGN - 1; |
| 33 | |
| 34 | /* Check if start is aligned */ |
Stephen Warren | 84d35b2 | 2012-11-06 11:27:29 +0000 | [diff] [blame^] | 35 | if ((ulong)state->user_buffer & align_mask) { |
| 36 | debug("Unaligned buffer address %p\n", state->user_buffer); |
Marek Vasut | b660df3 | 2012-08-26 15:19:06 +0000 | [diff] [blame] | 37 | return 0; |
| 38 | } |
| 39 | |
Stephen Warren | 84d35b2 | 2012-11-06 11:27:29 +0000 | [diff] [blame^] | 40 | /* Check if length is aligned */ |
| 41 | if (state->len != state->len_aligned) { |
| 42 | debug("Unaligned buffer length %d\n", state->len); |
Marek Vasut | b660df3 | 2012-08-26 15:19:06 +0000 | [diff] [blame] | 43 | return 0; |
| 44 | } |
| 45 | |
| 46 | /* Aligned */ |
| 47 | return 1; |
| 48 | } |
| 49 | |
Stephen Warren | 84d35b2 | 2012-11-06 11:27:29 +0000 | [diff] [blame^] | 50 | int bounce_buffer_start(struct bounce_buffer *state, void *data, |
| 51 | size_t len, unsigned int flags) |
Marek Vasut | b660df3 | 2012-08-26 15:19:06 +0000 | [diff] [blame] | 52 | { |
Stephen Warren | 84d35b2 | 2012-11-06 11:27:29 +0000 | [diff] [blame^] | 53 | 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 Vasut | b660df3 | 2012-08-26 15:19:06 +0000 | [diff] [blame] | 58 | |
Stephen Warren | 84d35b2 | 2012-11-06 11:27:29 +0000 | [diff] [blame^] | 59 | 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 Vasut | b660df3 | 2012-08-26 15:19:06 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Stephen Warren | 84d35b2 | 2012-11-06 11:27:29 +0000 | [diff] [blame^] | 70 | /* |
| 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 Vasut | b660df3 | 2012-08-26 15:19:06 +0000 | [diff] [blame] | 77 | |
| 78 | return 0; |
| 79 | } |
| 80 | |
Stephen Warren | 84d35b2 | 2012-11-06 11:27:29 +0000 | [diff] [blame^] | 81 | int bounce_buffer_stop(struct bounce_buffer *state) |
Marek Vasut | b660df3 | 2012-08-26 15:19:06 +0000 | [diff] [blame] | 82 | { |
Stephen Warren | 84d35b2 | 2012-11-06 11:27:29 +0000 | [diff] [blame^] | 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) + |
| 87 | state->len_aligned); |
| 88 | } |
Marek Vasut | b660df3 | 2012-08-26 15:19:06 +0000 | [diff] [blame] | 89 | |
Stephen Warren | 84d35b2 | 2012-11-06 11:27:29 +0000 | [diff] [blame^] | 90 | if (state->bounce_buffer == state->user_buffer) |
Marek Vasut | b660df3 | 2012-08-26 15:19:06 +0000 | [diff] [blame] | 91 | return 0; |
| 92 | |
Stephen Warren | 84d35b2 | 2012-11-06 11:27:29 +0000 | [diff] [blame^] | 93 | if (state->flags & GEN_BB_WRITE) |
| 94 | memcpy(state->user_buffer, state->bounce_buffer, state->len); |
Marek Vasut | b660df3 | 2012-08-26 15:19:06 +0000 | [diff] [blame] | 95 | |
Stephen Warren | 84d35b2 | 2012-11-06 11:27:29 +0000 | [diff] [blame^] | 96 | free(state->bounce_buffer); |
Marek Vasut | b660df3 | 2012-08-26 15:19:06 +0000 | [diff] [blame] | 97 | |
| 98 | return 0; |
| 99 | } |