io_emmcdrv.c 4.21 KB
Newer Older
1
/*
2
 * Copyright (c) 2015-2021, Renesas Electronics Corporation. All rights reserved.
3
4
5
6
7
8
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <string.h>

9
10
11
12
#include <common/debug.h>
#include <drivers/io/io_driver.h>
#include <drivers/io/io_storage.h>

13
#include "emmc_config.h"
14
#include "emmc_def.h"
15
16
#include "emmc_hal.h"
#include "emmc_std.h"
17
18
19
#include "io_common.h"
#include "io_emmcdrv.h"
#include "io_private.h"
20
21
22
23
24
25
26
27

static int32_t emmcdrv_dev_open(const uintptr_t spec __attribute__ ((unused)),
				io_dev_info_t **dev_info);
static int32_t emmcdrv_dev_close(io_dev_info_t *dev_info);

typedef struct {
	uint32_t in_use;
	uintptr_t base;
28
	signed long long file_pos;
29
30
31
32
33
34
35
36
37
38
39
40
41
	EMMC_PARTITION_ID partition;
} file_state_t;

static file_state_t current_file = { 0 };

static EMMC_PARTITION_ID emmcdrv_bootpartition = PARTITION_ID_USER;

static io_type_t device_type_emmcdrv(void)
{
	return IO_TYPE_MEMMAP;
}

static int32_t emmcdrv_block_seek(io_entity_t *entity, int32_t mode,
42
				  signed long long offset)
43
{
44
	if (mode != IO_SEEK_SET) {
45
		return IO_FAIL;
46
	}
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62

	((file_state_t *) entity->info)->file_pos = offset;

	return IO_SUCCESS;
}

static int32_t emmcdrv_block_read(io_entity_t *entity, uintptr_t buffer,
				  size_t length, size_t *length_read)
{
	file_state_t *fp = (file_state_t *) entity->info;
	uint32_t sector_add, sector_num, emmc_dma = 0;
	int32_t result = IO_SUCCESS;

	sector_add = current_file.file_pos >> EMMC_SECTOR_SIZE_SHIFT;
	sector_num = (length + EMMC_SECTOR_SIZE - 1U) >> EMMC_SECTOR_SIZE_SHIFT;

63
	NOTICE("BL2: Load dst=0x%lx src=(p:%d)0x%llx(%d) len=0x%lx(%d)\n",
64
65
66
67
	       buffer,
	       current_file.partition, current_file.file_pos,
	       sector_add, length, sector_num);

68
	if ((buffer + length - 1U) <= (uintptr_t)UINT32_MAX) {
69
		emmc_dma = LOADIMAGE_FLAGS_DMA_ENABLE;
70
	}
71
72

	if (emmc_read_sector((uint32_t *) buffer, sector_add, sector_num,
73
			     emmc_dma) != EMMC_SUCCESS) {
74
		result = IO_FAIL;
75
	}
76
77

	*length_read = length;
78
	fp->file_pos += (signed long long)length;
79
80
81
82
83
84
85
86
87

	return result;
}

static int32_t emmcdrv_block_open(io_dev_info_t *dev_info,
				  const uintptr_t spec, io_entity_t *entity)
{
	const io_drv_spec_t *block_spec = (io_drv_spec_t *) spec;

88
	if (current_file.in_use != 0U) {
89
90
91
92
93
94
95
96
97
		WARN("mmc_block: Only one open spec at a time\n");
		return IO_RESOURCES_EXHAUSTED;
	}

	current_file.file_pos = 0;
	current_file.in_use = 1;

	if (emmcdrv_bootpartition == PARTITION_ID_USER) {
		emmcdrv_bootpartition = mmc_drv_obj.boot_partition_en;
98
99
		if ((emmcdrv_bootpartition == PARTITION_ID_BOOT_1) ||
		    (emmcdrv_bootpartition == PARTITION_ID_BOOT_2)) {
100
101
102
103
104
105
106
107
108
			current_file.partition = emmcdrv_bootpartition;

			NOTICE("BL2: eMMC boot from partition %d\n",
			       emmcdrv_bootpartition);
			goto done;
		}
		return IO_FAIL;
	}

109
110
111
	if ((block_spec->partition == PARTITION_ID_USER) ||
	    (block_spec->partition == PARTITION_ID_BOOT_1) ||
	    (block_spec->partition == PARTITION_ID_BOOT_2)) {
112
		current_file.partition = block_spec->partition;
113
	} else {
114
		current_file.partition = emmcdrv_bootpartition;
115
	}
116
117

done:
118
	if (emmc_select_partition(current_file.partition) != EMMC_SUCCESS) {
119
		return IO_FAIL;
120
	}
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173

	entity->info = (uintptr_t) &current_file;

	return IO_SUCCESS;
}

static int32_t emmcdrv_block_close(io_entity_t *entity)
{
	memset((void *)&current_file, 0, sizeof(current_file));
	entity->info = 0U;

	return IO_SUCCESS;
}

static const io_dev_funcs_t emmcdrv_dev_funcs = {
	.type = &device_type_emmcdrv,
	.open = &emmcdrv_block_open,
	.seek = &emmcdrv_block_seek,
	.size = NULL,
	.read = &emmcdrv_block_read,
	.write = NULL,
	.close = &emmcdrv_block_close,
	.dev_init = NULL,
	.dev_close = &emmcdrv_dev_close
};

static const io_dev_info_t emmcdrv_dev_info = {
	.funcs = &emmcdrv_dev_funcs,
	.info = (uintptr_t) 0
};

static const io_dev_connector_t emmcdrv_dev_connector = {
	&emmcdrv_dev_open,
};

static int32_t emmcdrv_dev_open(const uintptr_t spec __attribute__ ((unused)),
				io_dev_info_t **dev_info)
{
	*dev_info = (io_dev_info_t *) &emmcdrv_dev_info;

	return IO_SUCCESS;
}

static int32_t emmcdrv_dev_close(io_dev_info_t *dev_info)
{
	return IO_SUCCESS;
}

int32_t rcar_register_io_dev_emmcdrv(const io_dev_connector_t **dev_con)
{
	int32_t rc;

	rc = io_register_device(&emmcdrv_dev_info);
174
	if (rc == IO_SUCCESS) {
175
		*dev_con = &emmcdrv_dev_connector;
176
	}
177
178
179

	return rc;
}