blob: 2bf88e6f75c4b8a52718b259b8aa9f6896590d59 [file] [log] [blame]
Yann Gautier9abbd6a2018-10-15 09:36:44 +02001/*
Vyacheslav Yurkovb74313c2021-06-04 10:08:39 +02002 * Copyright (c) 2018-2021, ARM Limited and Contributors. All rights reserved.
Yann Gautier9abbd6a2018-10-15 09:36:44 +02003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <assert.h>
Yann Gautier9abbd6a2018-10-15 09:36:44 +02008#include <errno.h>
Yann Gautier9abbd6a2018-10-15 09:36:44 +02009#include <string.h>
10
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000011#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
Yann Gautier9abbd6a2018-10-15 09:36:44 +020018/* SDMMC device functions */
19static int mmc_dev_open(const uintptr_t init_params, io_dev_info_t **dev_info);
20static int mmc_block_open(io_dev_info_t *dev_info, const uintptr_t spec,
21 io_entity_t *entity);
22static int mmc_dev_init(io_dev_info_t *dev_info, const uintptr_t init_params);
Yann Gautierf30cddc2019-04-16 11:35:19 +020023static int mmc_block_seek(io_entity_t *entity, int mode,
24 signed long long offset);
Yann Gautier9abbd6a2018-10-15 09:36:44 +020025static int mmc_block_read(io_entity_t *entity, uintptr_t buffer, size_t length,
26 size_t *length_read);
27static int mmc_block_close(io_entity_t *entity);
28static int mmc_dev_close(io_dev_info_t *dev_info);
29static io_type_t device_type_mmc(void);
30
Yann Gautierf30cddc2019-04-16 11:35:19 +020031static signed long long seek_offset;
Vyacheslav Yurkovb74313c2021-06-04 10:08:39 +020032static size_t (*_read_blocks)(int lba, uintptr_t buf, size_t size);
Yann Gautier9abbd6a2018-10-15 09:36:44 +020033
34static const io_dev_connector_t mmc_dev_connector = {
35 .dev_open = mmc_dev_open
36};
37
38static 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
50static 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 */
56static io_type_t device_type_mmc(void)
57{
58 return IO_TYPE_MMC;
59}
60
61/* Open a connection to the mmc device */
62static int mmc_dev_open(const uintptr_t init_params, io_dev_info_t **dev_info)
63{
Vyacheslav Yurkovb74313c2021-06-04 10:08:39 +020064 struct io_mmc_dev_spec *device_spec =
65 (struct io_mmc_dev_spec *)init_params;
66
Yann Gautier9abbd6a2018-10-15 09:36:44 +020067 assert(dev_info != NULL);
68 *dev_info = (io_dev_info_t *)&mmc_dev_info;
69
Vyacheslav Yurkovb74313c2021-06-04 10:08:39 +020070 _read_blocks = !device_spec->use_boot_part ?
71 mmc_read_blocks : mmc_boot_part_read_blocks;
72
Yann Gautier9abbd6a2018-10-15 09:36:44 +020073 return 0;
74}
75
76static 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 */
82static int mmc_dev_close(io_dev_info_t *dev_info)
83{
84 return 0;
85}
86
87/* Open a file on the mmc device */
88static 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 */
Yann Gautierf30cddc2019-04-16 11:35:19 +020096static int mmc_block_seek(io_entity_t *entity, int mode,
97 signed long long offset)
Yann Gautier9abbd6a2018-10-15 09:36:44 +020098{
99 seek_offset = offset;
100 return 0;
101}
102
103/* Read data from a file on the mmc device */
104static int mmc_block_read(io_entity_t *entity, uintptr_t buffer,
105 size_t length, size_t *length_read)
106{
Yann Gautierdbb9f572020-06-12 12:17:17 +0200107 uint8_t retries;
Yann Gautier9abbd6a2018-10-15 09:36:44 +0200108
Yann Gautierdbb9f572020-06-12 12:17:17 +0200109 for (retries = 0U; retries < 3U; retries++) {
Vyacheslav Yurkovb74313c2021-06-04 10:08:39 +0200110 *length_read = _read_blocks(seek_offset / MMC_BLOCK_SIZE,
111 buffer, length);
Yann Gautierdbb9f572020-06-12 12:17:17 +0200112
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);
Yann Gautier9abbd6a2018-10-15 09:36:44 +0200119 }
120
Yann Gautierdbb9f572020-06-12 12:17:17 +0200121 return -EIO;
Yann Gautier9abbd6a2018-10-15 09:36:44 +0200122}
123
124/* Close a file on the mmc device */
125static int mmc_block_close(io_entity_t *entity)
126{
127 return 0;
128}
129
130/* Register the mmc driver with the IO abstraction */
131int 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}