1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2016 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
11 #include "mmc_private.h"
13 static struct list_head mmc_devices;
14 static int cur_dev_num = -1;
16 #if CONFIG_IS_ENABLED(MMC_TINY)
17 static struct mmc mmc_static;
18 struct mmc *find_mmc_device(int dev_num)
23 void mmc_do_preinit(void)
25 struct mmc *m = &mmc_static;
26 #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
27 mmc_set_preinit(m, 1);
33 struct blk_desc *mmc_get_blk_desc(struct mmc *mmc)
35 return &mmc->block_dev;
38 struct mmc *find_mmc_device(int dev_num)
41 struct list_head *entry;
43 list_for_each(entry, &mmc_devices) {
44 m = list_entry(entry, struct mmc, link);
46 if (m->block_dev.devnum == dev_num)
50 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
51 printf("MMC Device %d not found\n", dev_num);
57 int mmc_get_next_devnum(void)
62 struct blk_desc *mmc_get_blk_desc(struct mmc *mmc)
64 return &mmc->block_dev;
72 void mmc_do_preinit(void)
75 struct list_head *entry;
77 list_for_each(entry, &mmc_devices) {
78 m = list_entry(entry, struct mmc, link);
80 #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
81 mmc_set_preinit(m, 1);
89 void mmc_list_init(void)
91 INIT_LIST_HEAD(&mmc_devices);
95 void mmc_list_add(struct mmc *mmc)
97 INIT_LIST_HEAD(&mmc->link);
99 list_add_tail(&mmc->link, &mmc_devices);
102 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
103 void print_mmc_devices(char separator)
106 struct list_head *entry;
109 list_for_each(entry, &mmc_devices) {
110 m = list_entry(entry, struct mmc, link);
113 mmc_type = IS_SD(m) ? "SD" : "eMMC";
117 printf("%s: %d", m->cfg->name, m->block_dev.devnum);
119 printf(" (%s)", mmc_type);
121 if (entry->next != &mmc_devices) {
122 printf("%c", separator);
123 if (separator != '\n')
132 void print_mmc_devices(char separator) { }
135 #if CONFIG_IS_ENABLED(MMC_TINY)
136 static struct mmc mmc_static = {
140 .if_type = IF_TYPE_MMC,
143 .block_read = mmc_bread,
144 .block_write = mmc_bwrite,
145 .block_erase = mmc_berase,
150 struct mmc *mmc_create(const struct mmc_config *cfg, void *priv)
152 struct mmc *mmc = &mmc_static;
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
159 debug("Warning: MMC_TINY doesn't support multiple MMC devices\n");
169 void mmc_destroy(struct mmc *mmc)
173 struct mmc *mmc_create(const struct mmc_config *cfg, void *priv)
175 struct blk_desc *bdesc;
178 /* quick validation */
179 if (cfg == NULL || cfg->f_min == 0 ||
180 cfg->f_max == 0 || cfg->b_max == 0)
183 #if !CONFIG_IS_ENABLED(DM_MMC)
184 if (cfg->ops == NULL || cfg->ops->send_cmd == NULL)
188 mmc = calloc(1, sizeof(*mmc));
195 /* the following chunk was mmc_register() */
197 /* Setup dsr related values */
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;
209 /* setup initial part type */
210 bdesc->part_type = mmc->cfg->part_type;
216 void mmc_destroy(struct mmc *mmc)
218 /* only freeing memory for now */
223 static int mmc_select_hwpartp(struct blk_desc *desc, int hwpart)
225 struct mmc *mmc = find_mmc_device(desc->devnum);
231 if (mmc->block_dev.hwpart == hwpart)
234 if (mmc->part_config == MMCPART_NOAVAILABLE)
237 ret = mmc_switch_part(mmc, hwpart);
244 static int mmc_get_dev(int dev, struct blk_desc **descp)
246 struct mmc *mmc = find_mmc_device(dev);
255 *descp = &mmc->block_dev;
260 U_BOOT_LEGACY_BLK(mmc) = {
261 .if_typename = "mmc",
262 .if_type = IF_TYPE_MMC,
264 .get_dev = mmc_get_dev,
265 .select_hwpart = mmc_select_hwpartp,