blob: c2b5f7c08936857a935e7843c313b4662b7a2a92 [file] [log] [blame]
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +02001/*
Biju Das50bfdc42020-12-13 20:28:45 +00002 * Copyright (c) 2015-2021, 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 "emmc_config.h"
Biju Das50bfdc42020-12-13 20:28:45 +000014#include "emmc_def.h"
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +020015#include "emmc_hal.h"
16#include "emmc_std.h"
Biju Das50bfdc42020-12-13 20:28:45 +000017#include "io_common.h"
18#include "io_emmcdrv.h"
19#include "io_private.h"
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +020020
21static int32_t emmcdrv_dev_open(const uintptr_t spec __attribute__ ((unused)),
22 io_dev_info_t **dev_info);
23static int32_t emmcdrv_dev_close(io_dev_info_t *dev_info);
24
25typedef struct {
26 uint32_t in_use;
27 uintptr_t base;
Yann Gautierf30cddc2019-04-16 11:35:19 +020028 signed long long file_pos;
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +020029 EMMC_PARTITION_ID partition;
30} file_state_t;
31
32static file_state_t current_file = { 0 };
33
34static EMMC_PARTITION_ID emmcdrv_bootpartition = PARTITION_ID_USER;
35
36static io_type_t device_type_emmcdrv(void)
37{
38 return IO_TYPE_MEMMAP;
39}
40
41static int32_t emmcdrv_block_seek(io_entity_t *entity, int32_t mode,
Yann Gautierf30cddc2019-04-16 11:35:19 +020042 signed long long offset)
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +020043{
Biju Das50bfdc42020-12-13 20:28:45 +000044 if (mode != IO_SEEK_SET) {
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +020045 return IO_FAIL;
Biju Das50bfdc42020-12-13 20:28:45 +000046 }
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +020047
48 ((file_state_t *) entity->info)->file_pos = offset;
49
50 return IO_SUCCESS;
51}
52
53static int32_t emmcdrv_block_read(io_entity_t *entity, uintptr_t buffer,
54 size_t length, size_t *length_read)
55{
56 file_state_t *fp = (file_state_t *) entity->info;
57 uint32_t sector_add, sector_num, emmc_dma = 0;
58 int32_t result = IO_SUCCESS;
59
60 sector_add = current_file.file_pos >> EMMC_SECTOR_SIZE_SHIFT;
61 sector_num = (length + EMMC_SECTOR_SIZE - 1U) >> EMMC_SECTOR_SIZE_SHIFT;
62
Yann Gautierf30cddc2019-04-16 11:35:19 +020063 NOTICE("BL2: Load dst=0x%lx src=(p:%d)0x%llx(%d) len=0x%lx(%d)\n",
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +020064 buffer,
65 current_file.partition, current_file.file_pos,
66 sector_add, length, sector_num);
67
Biju Das50bfdc42020-12-13 20:28:45 +000068 if ((buffer + length - 1U) <= (uintptr_t)UINT32_MAX) {
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +020069 emmc_dma = LOADIMAGE_FLAGS_DMA_ENABLE;
Biju Das50bfdc42020-12-13 20:28:45 +000070 }
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +020071
72 if (emmc_read_sector((uint32_t *) buffer, sector_add, sector_num,
Biju Das50bfdc42020-12-13 20:28:45 +000073 emmc_dma) != EMMC_SUCCESS) {
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +020074 result = IO_FAIL;
Biju Das50bfdc42020-12-13 20:28:45 +000075 }
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +020076
77 *length_read = length;
Yann Gautierf30cddc2019-04-16 11:35:19 +020078 fp->file_pos += (signed long long)length;
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +020079
80 return result;
81}
82
83static int32_t emmcdrv_block_open(io_dev_info_t *dev_info,
84 const uintptr_t spec, io_entity_t *entity)
85{
86 const io_drv_spec_t *block_spec = (io_drv_spec_t *) spec;
87
Yann Gautierf30cddc2019-04-16 11:35:19 +020088 if (current_file.in_use != 0U) {
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +020089 WARN("mmc_block: Only one open spec at a time\n");
90 return IO_RESOURCES_EXHAUSTED;
91 }
92
93 current_file.file_pos = 0;
94 current_file.in_use = 1;
95
96 if (emmcdrv_bootpartition == PARTITION_ID_USER) {
97 emmcdrv_bootpartition = mmc_drv_obj.boot_partition_en;
Biju Das50bfdc42020-12-13 20:28:45 +000098 if ((emmcdrv_bootpartition == PARTITION_ID_BOOT_1) ||
99 (emmcdrv_bootpartition == PARTITION_ID_BOOT_2)) {
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +0200100 current_file.partition = emmcdrv_bootpartition;
101
102 NOTICE("BL2: eMMC boot from partition %d\n",
103 emmcdrv_bootpartition);
104 goto done;
105 }
106 return IO_FAIL;
107 }
108
Biju Das50bfdc42020-12-13 20:28:45 +0000109 if ((block_spec->partition == PARTITION_ID_USER) ||
110 (block_spec->partition == PARTITION_ID_BOOT_1) ||
111 (block_spec->partition == PARTITION_ID_BOOT_2)) {
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +0200112 current_file.partition = block_spec->partition;
Biju Das50bfdc42020-12-13 20:28:45 +0000113 } else {
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +0200114 current_file.partition = emmcdrv_bootpartition;
Biju Das50bfdc42020-12-13 20:28:45 +0000115 }
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +0200116
117done:
Biju Das50bfdc42020-12-13 20:28:45 +0000118 if (emmc_select_partition(current_file.partition) != EMMC_SUCCESS) {
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +0200119 return IO_FAIL;
Biju Das50bfdc42020-12-13 20:28:45 +0000120 }
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +0200121
122 entity->info = (uintptr_t) &current_file;
123
124 return IO_SUCCESS;
125}
126
127static int32_t emmcdrv_block_close(io_entity_t *entity)
128{
129 memset((void *)&current_file, 0, sizeof(current_file));
130 entity->info = 0U;
131
132 return IO_SUCCESS;
133}
134
135static const io_dev_funcs_t emmcdrv_dev_funcs = {
136 .type = &device_type_emmcdrv,
137 .open = &emmcdrv_block_open,
138 .seek = &emmcdrv_block_seek,
139 .size = NULL,
140 .read = &emmcdrv_block_read,
141 .write = NULL,
142 .close = &emmcdrv_block_close,
143 .dev_init = NULL,
144 .dev_close = &emmcdrv_dev_close
145};
146
147static const io_dev_info_t emmcdrv_dev_info = {
148 .funcs = &emmcdrv_dev_funcs,
149 .info = (uintptr_t) 0
150};
151
152static const io_dev_connector_t emmcdrv_dev_connector = {
153 &emmcdrv_dev_open,
154};
155
156static int32_t emmcdrv_dev_open(const uintptr_t spec __attribute__ ((unused)),
157 io_dev_info_t **dev_info)
158{
159 *dev_info = (io_dev_info_t *) &emmcdrv_dev_info;
160
161 return IO_SUCCESS;
162}
163
164static int32_t emmcdrv_dev_close(io_dev_info_t *dev_info)
165{
166 return IO_SUCCESS;
167}
168
169int32_t rcar_register_io_dev_emmcdrv(const io_dev_connector_t **dev_con)
170{
171 int32_t rc;
172
173 rc = io_register_device(&emmcdrv_dev_info);
Biju Das50bfdc42020-12-13 20:28:45 +0000174 if (rc == IO_SUCCESS) {
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +0200175 *dev_con = &emmcdrv_dev_connector;
Biju Das50bfdc42020-12-13 20:28:45 +0000176 }
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +0200177
178 return rc;
179}