Jorge Ramirez-Ortiz | eaa63b4 | 2018-09-23 09:40:45 +0200 | [diff] [blame] | 1 | /* |
Yann Gautier | f30cddc | 2019-04-16 11:35:19 +0200 | [diff] [blame] | 2 | * Copyright (c) 2015-2019, Renesas Electronics Corporation. All rights reserved. |
Jorge Ramirez-Ortiz | eaa63b4 | 2018-09-23 09:40:45 +0200 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
Jorge Ramirez-Ortiz | eaa63b4 | 2018-09-23 09:40:45 +0200 | [diff] [blame] | 7 | #include <string.h> |
| 8 | |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 9 | #include <common/debug.h> |
| 10 | #include <drivers/io/io_driver.h> |
| 11 | #include <drivers/io/io_storage.h> |
| 12 | |
Jorge Ramirez-Ortiz | eaa63b4 | 2018-09-23 09:40:45 +0200 | [diff] [blame] | 13 | #include "io_common.h" |
| 14 | #include "io_private.h" |
| 15 | #include "io_memdrv.h" |
| 16 | #include "rcar_def.h" |
| 17 | |
| 18 | extern void rcar_dma_exec(uintptr_t dst, uint32_t src, uint32_t len); |
| 19 | |
| 20 | static int32_t memdrv_dev_open(const uintptr_t dev __attribute__ ((unused)), |
| 21 | io_dev_info_t **dev_info); |
| 22 | static int32_t memdrv_dev_close(io_dev_info_t *dev_info); |
| 23 | |
| 24 | /* As we need to be able to keep state for seek, only one file can be open |
| 25 | * at a time. Make this a structure and point to the entity->info. When we |
| 26 | * can malloc memory we can change this to support more open files. |
| 27 | */ |
| 28 | typedef struct { |
| 29 | uint32_t in_use; |
| 30 | uintptr_t base; |
Yann Gautier | f30cddc | 2019-04-16 11:35:19 +0200 | [diff] [blame] | 31 | signed long long file_pos; |
Jorge Ramirez-Ortiz | eaa63b4 | 2018-09-23 09:40:45 +0200 | [diff] [blame] | 32 | } file_state_t; |
| 33 | |
| 34 | static file_state_t current_file = { 0 }; |
| 35 | |
| 36 | static io_type_t device_type_memdrv(void) |
| 37 | { |
| 38 | return IO_TYPE_MEMMAP; |
| 39 | } |
| 40 | |
| 41 | static int32_t memdrv_block_open(io_dev_info_t *dev_info, const uintptr_t spec, |
| 42 | io_entity_t *entity) |
| 43 | { |
| 44 | const io_drv_spec_t *block_spec = (io_drv_spec_t *) spec; |
| 45 | |
| 46 | /* Since we need to track open state for seek() we only allow one open |
| 47 | * spec at a time. When we have dynamic memory we can malloc and set |
| 48 | * entity->info. |
| 49 | */ |
Yann Gautier | f30cddc | 2019-04-16 11:35:19 +0200 | [diff] [blame] | 50 | if (current_file.in_use != 0U) |
Jorge Ramirez-Ortiz | eaa63b4 | 2018-09-23 09:40:45 +0200 | [diff] [blame] | 51 | return IO_RESOURCES_EXHAUSTED; |
| 52 | |
| 53 | /* File cursor offset for seek and incremental reads etc. */ |
| 54 | current_file.base = block_spec->offset; |
| 55 | current_file.file_pos = 0; |
| 56 | current_file.in_use = 1; |
| 57 | |
| 58 | entity->info = (uintptr_t) ¤t_file; |
| 59 | |
| 60 | return IO_SUCCESS; |
| 61 | } |
| 62 | |
| 63 | static int32_t memdrv_block_seek(io_entity_t *entity, int32_t mode, |
Yann Gautier | f30cddc | 2019-04-16 11:35:19 +0200 | [diff] [blame] | 64 | signed long long offset) |
Jorge Ramirez-Ortiz | eaa63b4 | 2018-09-23 09:40:45 +0200 | [diff] [blame] | 65 | { |
| 66 | if (mode != IO_SEEK_SET) |
| 67 | return IO_FAIL; |
| 68 | |
| 69 | ((file_state_t *) entity->info)->file_pos = offset; |
| 70 | |
| 71 | return IO_SUCCESS; |
| 72 | } |
| 73 | |
| 74 | static int32_t memdrv_block_read(io_entity_t *entity, uintptr_t buffer, |
| 75 | size_t length, size_t *cnt) |
| 76 | { |
| 77 | file_state_t *fp; |
| 78 | |
| 79 | fp = (file_state_t *) entity->info; |
| 80 | |
Yann Gautier | f30cddc | 2019-04-16 11:35:19 +0200 | [diff] [blame] | 81 | NOTICE("BL2: dst=0x%lx src=0x%llx len=%ld(0x%lx)\n", |
| 82 | buffer, (unsigned long long)fp->base + |
| 83 | (unsigned long long)fp->file_pos, length, length); |
Jorge Ramirez-Ortiz | eaa63b4 | 2018-09-23 09:40:45 +0200 | [diff] [blame] | 84 | |
Yann Gautier | f30cddc | 2019-04-16 11:35:19 +0200 | [diff] [blame] | 85 | if (FLASH_MEMORY_SIZE < (fp->file_pos + (signed long long)length)) { |
Jorge Ramirez-Ortiz | eaa63b4 | 2018-09-23 09:40:45 +0200 | [diff] [blame] | 86 | ERROR("BL2: check load image (source address)\n"); |
| 87 | return IO_FAIL; |
| 88 | } |
| 89 | |
Yann Gautier | f30cddc | 2019-04-16 11:35:19 +0200 | [diff] [blame] | 90 | rcar_dma_exec(buffer, fp->base + (uintptr_t)fp->file_pos, length); |
| 91 | fp->file_pos += (signed long long)length; |
Jorge Ramirez-Ortiz | eaa63b4 | 2018-09-23 09:40:45 +0200 | [diff] [blame] | 92 | *cnt = length; |
| 93 | |
| 94 | return IO_SUCCESS; |
| 95 | } |
| 96 | |
| 97 | static int32_t memdrv_block_close(io_entity_t *entity) |
| 98 | { |
| 99 | entity->info = 0U; |
| 100 | |
| 101 | memset((void *)¤t_file, 0, sizeof(current_file)); |
| 102 | |
| 103 | return IO_SUCCESS; |
| 104 | } |
| 105 | |
| 106 | static const io_dev_funcs_t memdrv_dev_funcs = { |
| 107 | .type = &device_type_memdrv, |
| 108 | .open = &memdrv_block_open, |
| 109 | .seek = &memdrv_block_seek, |
| 110 | .size = NULL, |
| 111 | .read = &memdrv_block_read, |
| 112 | .write = NULL, |
| 113 | .close = &memdrv_block_close, |
| 114 | .dev_init = NULL, |
| 115 | .dev_close = &memdrv_dev_close, |
| 116 | }; |
| 117 | |
| 118 | static const io_dev_info_t memdrv_dev_info = { |
| 119 | .funcs = &memdrv_dev_funcs, |
| 120 | .info = 0, |
| 121 | }; |
| 122 | |
| 123 | static const io_dev_connector_t memdrv_dev_connector = { |
| 124 | .dev_open = &memdrv_dev_open |
| 125 | }; |
| 126 | |
| 127 | static int32_t memdrv_dev_open(const uintptr_t dev __attribute__ ((unused)), |
| 128 | io_dev_info_t **dev_info) |
| 129 | { |
| 130 | *dev_info = (io_dev_info_t *) &memdrv_dev_info; |
| 131 | |
| 132 | return IO_SUCCESS; |
| 133 | } |
| 134 | |
| 135 | static int32_t memdrv_dev_close(io_dev_info_t *dev_info) |
| 136 | { |
| 137 | return IO_SUCCESS; |
| 138 | } |
| 139 | |
| 140 | int32_t rcar_register_io_dev_memdrv(const io_dev_connector_t **dev_con) |
| 141 | { |
| 142 | int32_t result; |
| 143 | |
| 144 | result = io_register_device(&memdrv_dev_info); |
| 145 | if (result == IO_SUCCESS) |
| 146 | *dev_con = &memdrv_dev_connector; |
| 147 | |
| 148 | return result; |
| 149 | } |