Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | e5db115 | 2016-05-01 13:52:35 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2016 Google, Inc |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
Simon Glass | e5db115 | 2016-05-01 13:52:35 -0600 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 8 | #include <log.h> |
Simon Glass | 21875cf | 2016-06-12 23:30:17 -0600 | [diff] [blame] | 9 | #include <malloc.h> |
Simon Glass | e5db115 | 2016-05-01 13:52:35 -0600 | [diff] [blame] | 10 | #include <mmc.h> |
Simon Glass | 21875cf | 2016-06-12 23:30:17 -0600 | [diff] [blame] | 11 | #include "mmc_private.h" |
Simon Glass | e5db115 | 2016-05-01 13:52:35 -0600 | [diff] [blame] | 12 | |
| 13 | static struct list_head mmc_devices; |
| 14 | static int cur_dev_num = -1; |
| 15 | |
Marek Vasut | a318a7a | 2018-04-15 00:37:11 +0200 | [diff] [blame] | 16 | #if CONFIG_IS_ENABLED(MMC_TINY) |
| 17 | static struct mmc mmc_static; |
| 18 | struct mmc *find_mmc_device(int dev_num) |
| 19 | { |
| 20 | return &mmc_static; |
| 21 | } |
| 22 | |
| 23 | void mmc_do_preinit(void) |
| 24 | { |
| 25 | struct mmc *m = &mmc_static; |
| 26 | #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT |
| 27 | mmc_set_preinit(m, 1); |
| 28 | #endif |
| 29 | if (m->preinit) |
| 30 | mmc_start_init(m); |
| 31 | } |
| 32 | |
| 33 | struct blk_desc *mmc_get_blk_desc(struct mmc *mmc) |
| 34 | { |
| 35 | return &mmc->block_dev; |
| 36 | } |
| 37 | #else |
Simon Glass | e5db115 | 2016-05-01 13:52:35 -0600 | [diff] [blame] | 38 | struct mmc *find_mmc_device(int dev_num) |
| 39 | { |
| 40 | struct mmc *m; |
| 41 | struct list_head *entry; |
| 42 | |
| 43 | list_for_each(entry, &mmc_devices) { |
| 44 | m = list_entry(entry, struct mmc, link); |
| 45 | |
| 46 | if (m->block_dev.devnum == dev_num) |
| 47 | return m; |
| 48 | } |
| 49 | |
| 50 | #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT) |
| 51 | printf("MMC Device %d not found\n", dev_num); |
| 52 | #endif |
| 53 | |
| 54 | return NULL; |
| 55 | } |
| 56 | |
| 57 | int mmc_get_next_devnum(void) |
| 58 | { |
| 59 | return cur_dev_num++; |
| 60 | } |
| 61 | |
| 62 | struct blk_desc *mmc_get_blk_desc(struct mmc *mmc) |
| 63 | { |
| 64 | return &mmc->block_dev; |
| 65 | } |
| 66 | |
| 67 | int get_mmc_num(void) |
| 68 | { |
| 69 | return cur_dev_num; |
| 70 | } |
| 71 | |
| 72 | void mmc_do_preinit(void) |
| 73 | { |
| 74 | struct mmc *m; |
| 75 | struct list_head *entry; |
| 76 | |
| 77 | list_for_each(entry, &mmc_devices) { |
| 78 | m = list_entry(entry, struct mmc, link); |
| 79 | |
| 80 | #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT |
| 81 | mmc_set_preinit(m, 1); |
| 82 | #endif |
| 83 | if (m->preinit) |
| 84 | mmc_start_init(m); |
| 85 | } |
| 86 | } |
Marek Vasut | f537e39 | 2016-12-01 02:06:33 +0100 | [diff] [blame] | 87 | #endif |
Simon Glass | e5db115 | 2016-05-01 13:52:35 -0600 | [diff] [blame] | 88 | |
| 89 | void mmc_list_init(void) |
| 90 | { |
| 91 | INIT_LIST_HEAD(&mmc_devices); |
| 92 | cur_dev_num = 0; |
| 93 | } |
| 94 | |
| 95 | void mmc_list_add(struct mmc *mmc) |
| 96 | { |
| 97 | INIT_LIST_HEAD(&mmc->link); |
| 98 | |
| 99 | list_add_tail(&mmc->link, &mmc_devices); |
| 100 | } |
| 101 | |
| 102 | #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT) |
| 103 | void print_mmc_devices(char separator) |
| 104 | { |
| 105 | struct mmc *m; |
| 106 | struct list_head *entry; |
| 107 | char *mmc_type; |
| 108 | |
| 109 | list_for_each(entry, &mmc_devices) { |
| 110 | m = list_entry(entry, struct mmc, link); |
| 111 | |
| 112 | if (m->has_init) |
| 113 | mmc_type = IS_SD(m) ? "SD" : "eMMC"; |
| 114 | else |
| 115 | mmc_type = NULL; |
| 116 | |
| 117 | printf("%s: %d", m->cfg->name, m->block_dev.devnum); |
| 118 | if (mmc_type) |
| 119 | printf(" (%s)", mmc_type); |
| 120 | |
| 121 | if (entry->next != &mmc_devices) { |
| 122 | printf("%c", separator); |
| 123 | if (separator != '\n') |
| 124 | puts(" "); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | printf("\n"); |
| 129 | } |
| 130 | |
| 131 | #else |
| 132 | void print_mmc_devices(char separator) { } |
| 133 | #endif |
Simon Glass | 21875cf | 2016-06-12 23:30:17 -0600 | [diff] [blame] | 134 | |
Marek Vasut | f537e39 | 2016-12-01 02:06:33 +0100 | [diff] [blame] | 135 | #if CONFIG_IS_ENABLED(MMC_TINY) |
| 136 | static struct mmc mmc_static = { |
| 137 | .dsr_imp = 0, |
| 138 | .dsr = 0xffffffff, |
| 139 | .block_dev = { |
| 140 | .if_type = IF_TYPE_MMC, |
| 141 | .removable = 1, |
| 142 | .devnum = 0, |
| 143 | .block_read = mmc_bread, |
| 144 | .block_write = mmc_bwrite, |
| 145 | .block_erase = mmc_berase, |
| 146 | .part_type = 0, |
| 147 | }, |
| 148 | }; |
| 149 | |
| 150 | struct mmc *mmc_create(const struct mmc_config *cfg, void *priv) |
| 151 | { |
| 152 | struct mmc *mmc = &mmc_static; |
| 153 | |
Ezequiel Garcia | 8967664 | 2019-05-25 19:25:22 -0300 | [diff] [blame] | 154 | /* First MMC device registered, fail to register a new one. |
| 155 | * Given users are not expecting this to fail, instead |
| 156 | * of failing let's just return the only MMC device |
| 157 | */ |
| 158 | if (mmc->cfg) { |
| 159 | debug("Warning: MMC_TINY doesn't support multiple MMC devices\n"); |
| 160 | return mmc; |
| 161 | } |
| 162 | |
Marek Vasut | f537e39 | 2016-12-01 02:06:33 +0100 | [diff] [blame] | 163 | mmc->cfg = cfg; |
| 164 | mmc->priv = priv; |
| 165 | |
| 166 | return mmc; |
| 167 | } |
| 168 | |
| 169 | void mmc_destroy(struct mmc *mmc) |
| 170 | { |
| 171 | } |
| 172 | #else |
Simon Glass | 21875cf | 2016-06-12 23:30:17 -0600 | [diff] [blame] | 173 | struct mmc *mmc_create(const struct mmc_config *cfg, void *priv) |
| 174 | { |
| 175 | struct blk_desc *bdesc; |
| 176 | struct mmc *mmc; |
| 177 | |
| 178 | /* quick validation */ |
Jaehoon Chung | 38c2777 | 2016-08-12 11:39:05 +0900 | [diff] [blame] | 179 | if (cfg == NULL || cfg->f_min == 0 || |
| 180 | cfg->f_max == 0 || cfg->b_max == 0) |
| 181 | return NULL; |
| 182 | |
Simon Glass | eba48f9 | 2017-07-29 11:35:31 -0600 | [diff] [blame] | 183 | #if !CONFIG_IS_ENABLED(DM_MMC) |
Jaehoon Chung | 38c2777 | 2016-08-12 11:39:05 +0900 | [diff] [blame] | 184 | if (cfg->ops == NULL || cfg->ops->send_cmd == NULL) |
Simon Glass | 21875cf | 2016-06-12 23:30:17 -0600 | [diff] [blame] | 185 | return NULL; |
Jaehoon Chung | 38c2777 | 2016-08-12 11:39:05 +0900 | [diff] [blame] | 186 | #endif |
Simon Glass | 21875cf | 2016-06-12 23:30:17 -0600 | [diff] [blame] | 187 | |
| 188 | mmc = calloc(1, sizeof(*mmc)); |
| 189 | if (mmc == NULL) |
| 190 | return NULL; |
| 191 | |
| 192 | mmc->cfg = cfg; |
| 193 | mmc->priv = priv; |
| 194 | |
| 195 | /* the following chunk was mmc_register() */ |
| 196 | |
| 197 | /* Setup dsr related values */ |
| 198 | mmc->dsr_imp = 0; |
| 199 | mmc->dsr = 0xffffffff; |
| 200 | /* Setup the universal parts of the block interface just once */ |
| 201 | bdesc = mmc_get_blk_desc(mmc); |
| 202 | bdesc->if_type = IF_TYPE_MMC; |
| 203 | bdesc->removable = 1; |
| 204 | bdesc->devnum = mmc_get_next_devnum(); |
| 205 | bdesc->block_read = mmc_bread; |
| 206 | bdesc->block_write = mmc_bwrite; |
| 207 | bdesc->block_erase = mmc_berase; |
| 208 | |
| 209 | /* setup initial part type */ |
| 210 | bdesc->part_type = mmc->cfg->part_type; |
| 211 | mmc_list_add(mmc); |
| 212 | |
| 213 | return mmc; |
| 214 | } |
| 215 | |
| 216 | void mmc_destroy(struct mmc *mmc) |
| 217 | { |
| 218 | /* only freeing memory for now */ |
| 219 | free(mmc); |
| 220 | } |
Marek Vasut | f537e39 | 2016-12-01 02:06:33 +0100 | [diff] [blame] | 221 | #endif |
Simon Glass | 21875cf | 2016-06-12 23:30:17 -0600 | [diff] [blame] | 222 | |
| 223 | static int mmc_select_hwpartp(struct blk_desc *desc, int hwpart) |
| 224 | { |
| 225 | struct mmc *mmc = find_mmc_device(desc->devnum); |
| 226 | int ret; |
| 227 | |
| 228 | if (!mmc) |
| 229 | return -ENODEV; |
| 230 | |
| 231 | if (mmc->block_dev.hwpart == hwpart) |
| 232 | return 0; |
| 233 | |
| 234 | if (mmc->part_config == MMCPART_NOAVAILABLE) |
| 235 | return -EMEDIUMTYPE; |
| 236 | |
| 237 | ret = mmc_switch_part(mmc, hwpart); |
| 238 | if (ret) |
| 239 | return ret; |
| 240 | |
| 241 | return 0; |
| 242 | } |
| 243 | |
| 244 | static int mmc_get_dev(int dev, struct blk_desc **descp) |
| 245 | { |
| 246 | struct mmc *mmc = find_mmc_device(dev); |
| 247 | int ret; |
| 248 | |
| 249 | if (!mmc) |
| 250 | return -ENODEV; |
| 251 | ret = mmc_init(mmc); |
| 252 | if (ret) |
| 253 | return ret; |
| 254 | |
| 255 | *descp = &mmc->block_dev; |
| 256 | |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | U_BOOT_LEGACY_BLK(mmc) = { |
| 261 | .if_typename = "mmc", |
| 262 | .if_type = IF_TYPE_MMC, |
| 263 | .max_devs = -1, |
| 264 | .get_dev = mmc_get_dev, |
| 265 | .select_hwpart = mmc_select_hwpartp, |
| 266 | }; |