blob: 7e8c1d3a6570798a57c142733ad900bae15a6866 [file] [log] [blame]
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +02001/*
Yann Gautierf30cddc2019-04-16 11:35:19 +02002 * Copyright (c) 2015-2019, Renesas Electronics Corporation. All rights reserved.
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +02003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +02007#include <string.h>
8
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00009#include <common/debug.h>
10#include <drivers/io/io_driver.h>
11#include <drivers/io/io_storage.h>
12
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +020013#include "io_common.h"
14#include "io_private.h"
15#include "io_memdrv.h"
16#include "rcar_def.h"
17
18extern void rcar_dma_exec(uintptr_t dst, uint32_t src, uint32_t len);
19
20static int32_t memdrv_dev_open(const uintptr_t dev __attribute__ ((unused)),
21 io_dev_info_t **dev_info);
22static 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 */
28typedef struct {
29 uint32_t in_use;
30 uintptr_t base;
Yann Gautierf30cddc2019-04-16 11:35:19 +020031 signed long long file_pos;
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +020032} file_state_t;
33
34static file_state_t current_file = { 0 };
35
36static io_type_t device_type_memdrv(void)
37{
38 return IO_TYPE_MEMMAP;
39}
40
41static 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 Gautierf30cddc2019-04-16 11:35:19 +020050 if (current_file.in_use != 0U)
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +020051 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) &current_file;
59
60 return IO_SUCCESS;
61}
62
63static int32_t memdrv_block_seek(io_entity_t *entity, int32_t mode,
Yann Gautierf30cddc2019-04-16 11:35:19 +020064 signed long long offset)
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +020065{
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
74static 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 Gautierf30cddc2019-04-16 11:35:19 +020081 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-Ortizeaa63b42018-09-23 09:40:45 +020084
Yann Gautierf30cddc2019-04-16 11:35:19 +020085 if (FLASH_MEMORY_SIZE < (fp->file_pos + (signed long long)length)) {
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +020086 ERROR("BL2: check load image (source address)\n");
87 return IO_FAIL;
88 }
89
Yann Gautierf30cddc2019-04-16 11:35:19 +020090 rcar_dma_exec(buffer, fp->base + (uintptr_t)fp->file_pos, length);
91 fp->file_pos += (signed long long)length;
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +020092 *cnt = length;
93
94 return IO_SUCCESS;
95}
96
97static int32_t memdrv_block_close(io_entity_t *entity)
98{
99 entity->info = 0U;
100
101 memset((void *)&current_file, 0, sizeof(current_file));
102
103 return IO_SUCCESS;
104}
105
106static 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
118static const io_dev_info_t memdrv_dev_info = {
119 .funcs = &memdrv_dev_funcs,
120 .info = 0,
121};
122
123static const io_dev_connector_t memdrv_dev_connector = {
124 .dev_open = &memdrv_dev_open
125};
126
127static 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
135static int32_t memdrv_dev_close(io_dev_info_t *dev_info)
136{
137 return IO_SUCCESS;
138}
139
140int32_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}