blob: a239b5f3ad8e3cb4129f909327e2bc4ac6f21fae [file] [log] [blame]
Yann Gautier9abbd6a2018-10-15 09:36:44 +02001/*
2 * Copyright (c) 2018, ARM Limited and Contributors. All rights reserved.
3 *
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);
23static int mmc_block_seek(io_entity_t *entity, int mode, ssize_t offset);
24static int mmc_block_read(io_entity_t *entity, uintptr_t buffer, size_t length,
25 size_t *length_read);
26static int mmc_block_close(io_entity_t *entity);
27static int mmc_dev_close(io_dev_info_t *dev_info);
28static io_type_t device_type_mmc(void);
29
30static ssize_t seek_offset;
31
32static const io_dev_connector_t mmc_dev_connector = {
33 .dev_open = mmc_dev_open
34};
35
36static const io_dev_funcs_t mmc_dev_funcs = {
37 .type = device_type_mmc,
38 .open = mmc_block_open,
39 .seek = mmc_block_seek,
40 .size = NULL,
41 .read = mmc_block_read,
42 .write = NULL,
43 .close = mmc_block_close,
44 .dev_init = mmc_dev_init,
45 .dev_close = mmc_dev_close,
46};
47
48static const io_dev_info_t mmc_dev_info = {
49 .funcs = &mmc_dev_funcs,
50 .info = 0,
51};
52
53/* Identify the device type as mmc device */
54static io_type_t device_type_mmc(void)
55{
56 return IO_TYPE_MMC;
57}
58
59/* Open a connection to the mmc device */
60static int mmc_dev_open(const uintptr_t init_params, io_dev_info_t **dev_info)
61{
62 assert(dev_info != NULL);
63 *dev_info = (io_dev_info_t *)&mmc_dev_info;
64
65 return 0;
66}
67
68static int mmc_dev_init(io_dev_info_t *dev_info, const uintptr_t init_params)
69{
70 return 0;
71}
72
73/* Close a connection to the mmc device */
74static int mmc_dev_close(io_dev_info_t *dev_info)
75{
76 return 0;
77}
78
79/* Open a file on the mmc device */
80static int mmc_block_open(io_dev_info_t *dev_info, const uintptr_t spec,
81 io_entity_t *entity)
82{
83 seek_offset = 0;
84 return 0;
85}
86
87/* Seek to a particular file offset on the mmc device */
88static int mmc_block_seek(io_entity_t *entity, int mode, ssize_t offset)
89{
90 seek_offset = offset;
91 return 0;
92}
93
94/* Read data from a file on the mmc device */
95static int mmc_block_read(io_entity_t *entity, uintptr_t buffer,
96 size_t length, size_t *length_read)
97{
98 *length_read = mmc_read_blocks(seek_offset / MMC_BLOCK_SIZE,
99 buffer, length);
100
101 if (*length_read != length) {
102 return -EIO;
103 }
104
105 return 0;
106}
107
108/* Close a file on the mmc device */
109static int mmc_block_close(io_entity_t *entity)
110{
111 return 0;
112}
113
114/* Register the mmc driver with the IO abstraction */
115int register_io_dev_mmc(const io_dev_connector_t **dev_con)
116{
117 int result;
118
119 assert(dev_con != NULL);
120
121 result = io_register_device(&mmc_dev_info);
122 if (result == 0) {
123 *dev_con = &mmc_dev_connector;
124 }
125
126 return result;
127}