1 /*
2  * Copyright (c) 2018-2021, ARM Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <assert.h>
8 #include <errno.h>
9 #include <string.h>
10 
11 #include <common/debug.h>
12 #include <drivers/io/io_driver.h>
13 #include <drivers/io/io_storage.h>
14 #include <drivers/mmc.h>
15 #include <drivers/st/io_mmc.h>
16 #include <drivers/st/stm32_sdmmc2.h>
17 
18 /* SDMMC device functions */
19 static int mmc_dev_open(const uintptr_t init_params, io_dev_info_t **dev_info);
20 static int mmc_block_open(io_dev_info_t *dev_info, const uintptr_t spec,
21 			  io_entity_t *entity);
22 static int mmc_dev_init(io_dev_info_t *dev_info, const uintptr_t init_params);
23 static int mmc_block_seek(io_entity_t *entity, int mode,
24 			  signed long long offset);
25 static int mmc_block_read(io_entity_t *entity, uintptr_t buffer, size_t length,
26 			  size_t *length_read);
27 static int mmc_block_close(io_entity_t *entity);
28 static int mmc_dev_close(io_dev_info_t *dev_info);
29 static io_type_t device_type_mmc(void);
30 
31 static signed long long seek_offset;
32 static size_t (*_read_blocks)(int lba, uintptr_t buf, size_t size);
33 
34 static const io_dev_connector_t mmc_dev_connector = {
35 	.dev_open = mmc_dev_open
36 };
37 
38 static const io_dev_funcs_t mmc_dev_funcs = {
39 	.type = device_type_mmc,
40 	.open = mmc_block_open,
41 	.seek = mmc_block_seek,
42 	.size = NULL,
43 	.read = mmc_block_read,
44 	.write = NULL,
45 	.close = mmc_block_close,
46 	.dev_init = mmc_dev_init,
47 	.dev_close = mmc_dev_close,
48 };
49 
50 static const io_dev_info_t mmc_dev_info = {
51 	.funcs = &mmc_dev_funcs,
52 	.info = 0,
53 };
54 
55 /* Identify the device type as mmc device */
device_type_mmc(void)56 static io_type_t device_type_mmc(void)
57 {
58 	return IO_TYPE_MMC;
59 }
60 
61 /* Open a connection to the mmc device */
mmc_dev_open(const uintptr_t init_params,io_dev_info_t ** dev_info)62 static int mmc_dev_open(const uintptr_t init_params, io_dev_info_t **dev_info)
63 {
64 	struct io_mmc_dev_spec *device_spec =
65 		(struct io_mmc_dev_spec *)init_params;
66 
67 	assert(dev_info != NULL);
68 	*dev_info = (io_dev_info_t *)&mmc_dev_info;
69 
70 	_read_blocks = !device_spec->use_boot_part ?
71 		mmc_read_blocks : mmc_boot_part_read_blocks;
72 
73 	return 0;
74 }
75 
mmc_dev_init(io_dev_info_t * dev_info,const uintptr_t init_params)76 static int mmc_dev_init(io_dev_info_t *dev_info, const uintptr_t init_params)
77 {
78 	return 0;
79 }
80 
81 /* Close a connection to the mmc device */
mmc_dev_close(io_dev_info_t * dev_info)82 static int mmc_dev_close(io_dev_info_t *dev_info)
83 {
84 	return 0;
85 }
86 
87 /* Open a file on the mmc device */
mmc_block_open(io_dev_info_t * dev_info,const uintptr_t spec,io_entity_t * entity)88 static int mmc_block_open(io_dev_info_t *dev_info, const  uintptr_t spec,
89 			  io_entity_t *entity)
90 {
91 	seek_offset = 0;
92 	return 0;
93 }
94 
95 /* Seek to a particular file offset on the mmc device */
mmc_block_seek(io_entity_t * entity,int mode,signed long long offset)96 static int mmc_block_seek(io_entity_t *entity, int mode,
97 			  signed long long offset)
98 {
99 	seek_offset = offset;
100 	return 0;
101 }
102 
103 /* Read data from a file on the mmc device */
mmc_block_read(io_entity_t * entity,uintptr_t buffer,size_t length,size_t * length_read)104 static int mmc_block_read(io_entity_t *entity, uintptr_t buffer,
105 			  size_t length, size_t *length_read)
106 {
107 	uint8_t retries;
108 
109 	for (retries = 0U; retries < 3U; retries++) {
110 		*length_read = _read_blocks(seek_offset / MMC_BLOCK_SIZE,
111 					    buffer, length);
112 
113 		if (*length_read == length) {
114 			return 0;
115 		}
116 		WARN("%s: length_read = %lu (!= %lu), retry %u\n", __func__,
117 		     (unsigned long)*length_read, (unsigned long)length,
118 		     retries + 1U);
119 	}
120 
121 	return -EIO;
122 }
123 
124 /* Close a file on the mmc device */
mmc_block_close(io_entity_t * entity)125 static int mmc_block_close(io_entity_t *entity)
126 {
127 	return 0;
128 }
129 
130 /* Register the mmc driver with the IO abstraction */
register_io_dev_mmc(const io_dev_connector_t ** dev_con)131 int register_io_dev_mmc(const io_dev_connector_t **dev_con)
132 {
133 	int result;
134 
135 	assert(dev_con != NULL);
136 
137 	result = io_register_device(&mmc_dev_info);
138 	if (result == 0) {
139 		*dev_con = &mmc_dev_connector;
140 	}
141 
142 	return result;
143 }
144