Yann Gautier | 9abbd6a | 2018-10-15 09:36:44 +0200 | [diff] [blame] | 1 | /* |
| 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 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); |
| 23 | static int mmc_block_seek(io_entity_t *entity, int mode, ssize_t offset); |
| 24 | static int mmc_block_read(io_entity_t *entity, uintptr_t buffer, size_t length, |
| 25 | size_t *length_read); |
| 26 | static int mmc_block_close(io_entity_t *entity); |
| 27 | static int mmc_dev_close(io_dev_info_t *dev_info); |
| 28 | static io_type_t device_type_mmc(void); |
| 29 | |
| 30 | static ssize_t seek_offset; |
| 31 | |
| 32 | static const io_dev_connector_t mmc_dev_connector = { |
| 33 | .dev_open = mmc_dev_open |
| 34 | }; |
| 35 | |
| 36 | static 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 | |
| 48 | static 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 */ |
| 54 | static io_type_t device_type_mmc(void) |
| 55 | { |
| 56 | return IO_TYPE_MMC; |
| 57 | } |
| 58 | |
| 59 | /* Open a connection to the mmc device */ |
| 60 | static 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 | |
| 68 | static 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 */ |
| 74 | static int mmc_dev_close(io_dev_info_t *dev_info) |
| 75 | { |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | /* Open a file on the mmc device */ |
| 80 | static 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 */ |
| 88 | static 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 */ |
| 95 | static 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 */ |
| 109 | static int mmc_block_close(io_entity_t *entity) |
| 110 | { |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | /* Register the mmc driver with the IO abstraction */ |
| 115 | int 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 | } |