summaryrefslogtreecommitdiffstats
path: root/drivers/io/io_mtd.c
blob: 5d86592db9b4cb5a4c8c9fae355839f21b5da988 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
 * Copyright (c) 2019-2022, ARM Limited and Contributors. All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <assert.h>
#include <errno.h>
#include <string.h>

#include <common/debug.h>
#include <drivers/io/io_driver.h>
#include <drivers/io/io_mtd.h>
#include <lib/utils.h>

#include <platform_def.h>

typedef struct {
	io_mtd_dev_spec_t	*dev_spec;
	uintptr_t		base;
	unsigned long long	pos;		/* Offset in bytes */
	unsigned long long	size;		/* Size of device in bytes */
	unsigned long long	extra_offset;	/* Extra offset in bytes */
} mtd_dev_state_t;

io_type_t device_type_mtd(void);

static int mtd_open(io_dev_info_t *dev_info, const uintptr_t spec,
		    io_entity_t *entity);
static int mtd_seek(io_entity_t *entity, int mode, signed long long offset);
static int mtd_read(io_entity_t *entity, uintptr_t buffer, size_t length,
		    size_t *length_read);
static int mtd_close(io_entity_t *entity);
static int mtd_dev_open(const uintptr_t dev_spec, io_dev_info_t **dev_info);
static int mtd_dev_close(io_dev_info_t *dev_info);

static const io_dev_connector_t mtd_dev_connector = {
	.dev_open	= mtd_dev_open
};

static const io_dev_funcs_t mtd_dev_funcs = {
	.type		= device_type_mtd,
	.open		= mtd_open,
	.seek		= mtd_seek,
	.read		= mtd_read,
	.close		= mtd_close,
	.dev_close	= mtd_dev_close,
};

static mtd_dev_state_t state_pool[MAX_IO_MTD_DEVICES];
static io_dev_info_t dev_info_pool[MAX_IO_MTD_DEVICES];

io_type_t device_type_mtd(void)
{
	return IO_TYPE_MTD;
}

/* Locate a MTD state in the pool, specified by address */
static int find_first_mtd_state(const io_mtd_dev_spec_t *dev_spec,
				unsigned int *index_out)
{
	unsigned int index;
	int result = -ENOENT;

	for (index = 0U; index < MAX_IO_MTD_DEVICES; index++) {
		/* dev_spec is used as identifier since it's unique */
		if (state_pool[index].dev_spec == dev_spec) {
			result = 0;
			*index_out = index;
			break;
		}
	}

	return result;
}

/* Allocate a device info from the pool */
static int allocate_dev_info(io_dev_info_t **dev_info)
{
	unsigned int index = 0U;
	int result;

	result = find_first_mtd_state(NULL, &index);
	if (result != 0) {
		return -ENOMEM;
	}

	dev_info_pool[index].funcs = &mtd_dev_funcs;
	dev_info_pool[index].info = (uintptr_t)&state_pool[index];
	*dev_info = &dev_info_pool[index];

	return 0;
}

/* Release a device info from the pool */
static int free_dev_info(io_dev_info_t *dev_info)
{
	int result;
	unsigned int index = 0U;
	mtd_dev_state_t *state;

	state = (mtd_dev_state_t *)dev_info->info;
	result = find_first_mtd_state(state->dev_spec, &index);
	if (result != 0) {
		return result;
	}

	zeromem(state, sizeof(mtd_dev_state_t));
	zeromem(dev_info, sizeof(io_dev_info_t));

	return 0;
}

static int mtd_add_extra_offset(mtd_dev_state_t *cur, size_t *extra_offset)
{
	io_mtd_ops_t *ops = &cur->dev_spec->ops;
	int ret;

	if (ops->seek == NULL) {
		return 0;
	}

	ret = ops->seek(cur->base, cur->pos, extra_offset);
	if (ret != 0) {
		ERROR("%s: Seek error %d\n", __func__, ret);
		return ret;
	}

	return 0;
}

static int mtd_open(io_dev_info_t *dev_info, const uintptr_t spec,
		    io_entity_t *entity)
{
	mtd_dev_state_t *cur;
	io_block_spec_t *region;
	size_t extra_offset = 0U;
	int ret;

	assert((dev_info->info != 0UL) && (entity->info == 0UL));

	region = (io_block_spec_t *)spec;
	cur = (mtd_dev_state_t *)dev_info->info;
	entity->info = (uintptr_t)cur;
	cur->base = region->offset;
	cur->pos = 0U;
	cur->extra_offset = 0U;

	ret = mtd_add_extra_offset(cur, &extra_offset);
	if (ret != 0) {
		return ret;
	}

	cur->base += extra_offset;

	return 0;
}

/* Seek to a specific position using offset */
static int mtd_seek(io_entity_t *entity, int mode, signed long long offset)
{
	mtd_dev_state_t *cur;
	size_t extra_offset = 0U;
	int ret;

	assert((entity->info != (uintptr_t)NULL) && (offset >= 0));

	cur = (mtd_dev_state_t *)entity->info;

	switch (mode) {
	case IO_SEEK_SET:
		if ((offset >= 0) &&
		    ((unsigned long long)offset >= cur->size)) {
			return -EINVAL;
		}

		cur->pos = offset;
		break;
	case IO_SEEK_CUR:
		if (((cur->base + cur->pos + (unsigned long long)offset) >=
		     cur->size) ||
		    ((cur->base + cur->pos + (unsigned long long)offset) <
		     cur->base + cur->pos)) {
			return -EINVAL;
		}

		cur->pos += (unsigned long long)offset;
		break;
	default:
		return -EINVAL;
	}

	ret = mtd_add_extra_offset(cur, &extra_offset);
	if (ret != 0) {
		return ret;
	}

	cur->extra_offset = extra_offset;

	return 0;
}

static int mtd_read(io_entity_t *entity, uintptr_t buffer, size_t length,
		    size_t *out_length)
{
	mtd_dev_state_t *cur;
	io_mtd_ops_t *ops;
	int ret;

	assert(entity->info != (uintptr_t)NULL);
	assert((length > 0U) && (buffer != (uintptr_t)NULL));

	cur = (mtd_dev_state_t *)entity->info;
	ops = &cur->dev_spec->ops;
	assert(ops->read != NULL);

	VERBOSE("Read at %llx into %lx, length %zu\n",
		cur->base + cur->pos, buffer, length);
	if ((cur->base + cur->pos + length) > cur->dev_spec->device_size) {
		return -EINVAL;
	}

	ret = ops->read(cur->base + cur->pos + cur->extra_offset, buffer,
			length, out_length);
	if (ret < 0) {
		return ret;
	}

	assert(*out_length == length);
	cur->pos += *out_length;

	return 0;
}

static int mtd_close(io_entity_t *entity)
{
	entity->info = (uintptr_t)NULL;

	return 0;
}

static int mtd_dev_open(const uintptr_t dev_spec, io_dev_info_t **dev_info)
{
	mtd_dev_state_t *cur;
	io_dev_info_t *info;
	io_mtd_ops_t *ops;
	int result;

	result = allocate_dev_info(&info);
	if (result != 0) {
		return -ENOENT;
	}

	cur = (mtd_dev_state_t *)info->info;
	cur->dev_spec = (io_mtd_dev_spec_t *)dev_spec;
	*dev_info = info;
	ops = &(cur->dev_spec->ops);
	if (ops->init != NULL) {
		result = ops->init(&cur->dev_spec->device_size,
				   &cur->dev_spec->erase_size);
	}

	if (result == 0) {
		cur->size = cur->dev_spec->device_size;
	} else {
		cur->size = 0ULL;
	}

	return result;
}

static int mtd_dev_close(io_dev_info_t *dev_info)
{
	return free_dev_info(dev_info);
}

/* Exported functions */

/* Register the MTD driver in the IO abstraction */
int register_io_dev_mtd(const io_dev_connector_t **dev_con)
{
	int result;

	result = io_register_device(&dev_info_pool[0]);
	if (result == 0) {
		*dev_con = &mtd_dev_connector;
	}

	return result;
}