Yann Gautier | 9abbd6a | 2018-10-15 09:36:44 +0200 | [diff] [blame] | 1 | /* |
Vyacheslav Yurkov | b74313c | 2021-06-04 10:08:39 +0200 | [diff] [blame] | 2 | * Copyright (c) 2018-2021, ARM Limited and Contributors. All rights reserved. |
Yann Gautier | 9abbd6a | 2018-10-15 09:36:44 +0200 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include <assert.h> |
Yann Gautier | 9abbd6a | 2018-10-15 09:36:44 +0200 | [diff] [blame] | 8 | #include <errno.h> |
Yann Gautier | 9abbd6a | 2018-10-15 09:36:44 +0200 | [diff] [blame] | 9 | #include <string.h> |
| 10 | |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 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 | |
Yann Gautier | 9abbd6a | 2018-10-15 09:36:44 +0200 | [diff] [blame] | 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); |
Yann Gautier | f30cddc | 2019-04-16 11:35:19 +0200 | [diff] [blame] | 23 | static int mmc_block_seek(io_entity_t *entity, int mode, |
| 24 | signed long long offset); |
Yann Gautier | 9abbd6a | 2018-10-15 09:36:44 +0200 | [diff] [blame] | 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 | |
Yann Gautier | f30cddc | 2019-04-16 11:35:19 +0200 | [diff] [blame] | 31 | static signed long long seek_offset; |
Vyacheslav Yurkov | b74313c | 2021-06-04 10:08:39 +0200 | [diff] [blame] | 32 | static size_t (*_read_blocks)(int lba, uintptr_t buf, size_t size); |
Yann Gautier | 9abbd6a | 2018-10-15 09:36:44 +0200 | [diff] [blame] | 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 */ |
| 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 */ |
| 62 | static int mmc_dev_open(const uintptr_t init_params, io_dev_info_t **dev_info) |
| 63 | { |
Vyacheslav Yurkov | b74313c | 2021-06-04 10:08:39 +0200 | [diff] [blame] | 64 | struct io_mmc_dev_spec *device_spec = |
| 65 | (struct io_mmc_dev_spec *)init_params; |
| 66 | |
Yann Gautier | 9abbd6a | 2018-10-15 09:36:44 +0200 | [diff] [blame] | 67 | assert(dev_info != NULL); |
| 68 | *dev_info = (io_dev_info_t *)&mmc_dev_info; |
| 69 | |
Vyacheslav Yurkov | b74313c | 2021-06-04 10:08:39 +0200 | [diff] [blame] | 70 | _read_blocks = !device_spec->use_boot_part ? |
| 71 | mmc_read_blocks : mmc_boot_part_read_blocks; |
| 72 | |
Yann Gautier | 9abbd6a | 2018-10-15 09:36:44 +0200 | [diff] [blame] | 73 | return 0; |
| 74 | } |
| 75 | |
| 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 */ |
| 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 */ |
| 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 */ |
Yann Gautier | f30cddc | 2019-04-16 11:35:19 +0200 | [diff] [blame] | 96 | static int mmc_block_seek(io_entity_t *entity, int mode, |
| 97 | signed long long offset) |
Yann Gautier | 9abbd6a | 2018-10-15 09:36:44 +0200 | [diff] [blame] | 98 | { |
| 99 | seek_offset = offset; |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | /* Read data from a file on the mmc device */ |
| 104 | static int mmc_block_read(io_entity_t *entity, uintptr_t buffer, |
| 105 | size_t length, size_t *length_read) |
| 106 | { |
Yann Gautier | dbb9f57 | 2020-06-12 12:17:17 +0200 | [diff] [blame] | 107 | uint8_t retries; |
Yann Gautier | 9abbd6a | 2018-10-15 09:36:44 +0200 | [diff] [blame] | 108 | |
Yann Gautier | dbb9f57 | 2020-06-12 12:17:17 +0200 | [diff] [blame] | 109 | for (retries = 0U; retries < 3U; retries++) { |
Vyacheslav Yurkov | b74313c | 2021-06-04 10:08:39 +0200 | [diff] [blame] | 110 | *length_read = _read_blocks(seek_offset / MMC_BLOCK_SIZE, |
| 111 | buffer, length); |
Yann Gautier | dbb9f57 | 2020-06-12 12:17:17 +0200 | [diff] [blame] | 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); |
Yann Gautier | 9abbd6a | 2018-10-15 09:36:44 +0200 | [diff] [blame] | 119 | } |
| 120 | |
Yann Gautier | dbb9f57 | 2020-06-12 12:17:17 +0200 | [diff] [blame] | 121 | return -EIO; |
Yann Gautier | 9abbd6a | 2018-10-15 09:36:44 +0200 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | /* Close a file on the mmc device */ |
| 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 */ |
| 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 | } |