aboutsummaryrefslogtreecommitdiff
path: root/drivers/st/io/io_mmc.c
blob: 0ed71540c3ca564bd0bceb7bce78d763c3d63f0e (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
/*
 * Copyright (c) 2018-2020, 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_storage.h>
#include <drivers/mmc.h>
#include <drivers/st/io_mmc.h>
#include <drivers/st/stm32_sdmmc2.h>

/* SDMMC device functions */
static int mmc_dev_open(const uintptr_t init_params, io_dev_info_t **dev_info);
static int mmc_block_open(io_dev_info_t *dev_info, const uintptr_t spec,
			  io_entity_t *entity);
static int mmc_dev_init(io_dev_info_t *dev_info, const uintptr_t init_params);
static int mmc_block_seek(io_entity_t *entity, int mode,
			  signed long long offset);
static int mmc_block_read(io_entity_t *entity, uintptr_t buffer, size_t length,
			  size_t *length_read);
static int mmc_block_close(io_entity_t *entity);
static int mmc_dev_close(io_dev_info_t *dev_info);
static io_type_t device_type_mmc(void);

static signed long long seek_offset;

static const io_dev_connector_t mmc_dev_connector = {
	.dev_open = mmc_dev_open
};

static const io_dev_funcs_t mmc_dev_funcs = {
	.type = device_type_mmc,
	.open = mmc_block_open,
	.seek = mmc_block_seek,
	.size = NULL,
	.read = mmc_block_read,
	.write = NULL,
	.close = mmc_block_close,
	.dev_init = mmc_dev_init,
	.dev_close = mmc_dev_close,
};

static const io_dev_info_t mmc_dev_info = {
	.funcs = &mmc_dev_funcs,
	.info = 0,
};

/* Identify the device type as mmc device */
static io_type_t device_type_mmc(void)
{
	return IO_TYPE_MMC;
}

/* Open a connection to the mmc device */
static int mmc_dev_open(const uintptr_t init_params, io_dev_info_t **dev_info)
{
	assert(dev_info != NULL);
	*dev_info = (io_dev_info_t *)&mmc_dev_info;

	return 0;
}

static int mmc_dev_init(io_dev_info_t *dev_info, const uintptr_t init_params)
{
	return 0;
}

/* Close a connection to the mmc device */
static int mmc_dev_close(io_dev_info_t *dev_info)
{
	return 0;
}

/* Open a file on the mmc device */
static int mmc_block_open(io_dev_info_t *dev_info, const  uintptr_t spec,
			  io_entity_t *entity)
{
	seek_offset = 0;
	return 0;
}

/* Seek to a particular file offset on the mmc device */
static int mmc_block_seek(io_entity_t *entity, int mode,
			  signed long long offset)
{
	seek_offset = offset;
	return 0;
}

/* Read data from a file on the mmc device */
static int mmc_block_read(io_entity_t *entity, uintptr_t buffer,
			  size_t length, size_t *length_read)
{
	uint8_t retries;

	for (retries = 0U; retries < 3U; retries++) {
		*length_read = mmc_read_blocks(seek_offset / MMC_BLOCK_SIZE,
					       buffer, length);

		if (*length_read == length) {
			return 0;
		}
		WARN("%s: length_read = %lu (!= %lu), retry %u\n", __func__,
		     (unsigned long)*length_read, (unsigned long)length,
		     retries + 1U);
	}

	return -EIO;
}

/* Close a file on the mmc device */
static int mmc_block_close(io_entity_t *entity)
{
	return 0;
}

/* Register the mmc driver with the IO abstraction */
int register_io_dev_mmc(const io_dev_connector_t **dev_con)
{
	int result;

	assert(dev_con != NULL);

	result = io_register_device(&mmc_dev_info);
	if (result == 0) {
		*dev_con = &mmc_dev_connector;
	}

	return result;
}