Angel Pons | c74dae9 | 2020-04-02 23:48:16 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Julius Werner | db7f6fb | 2019-08-12 16:45:21 -0700 | [diff] [blame] | 2 | |
| 3 | #include <assert.h> |
| 4 | #include <device/mmio.h> |
| 5 | |
| 6 | /* Helper functions for various MMIO access patterns. */ |
| 7 | |
| 8 | void buffer_from_fifo32(void *buffer, size_t size, void *fifo, |
| 9 | int fifo_stride, int fifo_width) |
| 10 | { |
| 11 | u8 *p = buffer; |
| 12 | int i, j; |
| 13 | |
| 14 | assert(fifo_width > 0 && fifo_width <= sizeof(u32) && |
| 15 | fifo_stride % sizeof(u32) == 0); |
| 16 | |
| 17 | for (i = 0; i < size; i += fifo_width, fifo += fifo_stride) { |
| 18 | u32 val = read32(fifo); |
| 19 | for (j = 0; j < MIN(size - i, fifo_width); j++) |
| 20 | *p++ = (u8)(val >> (j * 8)); |
| 21 | } |
| 22 | } |
| 23 | |
Julius Werner | ea03d00 | 2021-09-16 15:53:32 -0700 | [diff] [blame] | 24 | void buffer_to_fifo32_prefix(const void *buffer, u32 prefix, int prefsz, size_t size, |
Julius Werner | db7f6fb | 2019-08-12 16:45:21 -0700 | [diff] [blame] | 25 | void *fifo, int fifo_stride, int fifo_width) |
| 26 | { |
Julius Werner | ea03d00 | 2021-09-16 15:53:32 -0700 | [diff] [blame] | 27 | const u8 *p = buffer; |
Julius Werner | db7f6fb | 2019-08-12 16:45:21 -0700 | [diff] [blame] | 28 | int i, j = prefsz; |
| 29 | |
| 30 | assert(fifo_width > 0 && fifo_width <= sizeof(u32) && |
| 31 | fifo_stride % sizeof(u32) == 0 && prefsz <= fifo_width); |
| 32 | |
| 33 | uint32_t val = prefix; |
| 34 | for (i = 0; i < size; i += fifo_width, fifo += fifo_stride) { |
| 35 | for (; j < MIN(size - i, fifo_width); j++) |
| 36 | val |= *p++ << (j * 8); |
| 37 | write32(fifo, val); |
| 38 | val = 0; |
| 39 | j = 0; |
| 40 | } |
| 41 | |
| 42 | } |