1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2016 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
10 #include "mmc_private.h"
12 static struct list_head mmc_devices;
13 static int cur_dev_num = -1;
15 #if CONFIG_IS_ENABLED(MMC_TINY)
16 static struct mmc mmc_static;
17 struct mmc *find_mmc_device(int dev_num)
22 void mmc_do_preinit(void)
24 struct mmc *m = &mmc_static;
25 #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
26 mmc_set_preinit(m, 1);
32 struct blk_desc *mmc_get_blk_desc(struct mmc *mmc)
34 return &mmc->block_dev;
37 struct mmc *find_mmc_device(int dev_num)
40 struct list_head *entry;
42 list_for_each(entry, &mmc_devices) {
43 m = list_entry(entry, struct mmc, link);
45 if (m->block_dev.devnum == dev_num)
49 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
50 printf("MMC Device %d not found\n", dev_num);
56 int mmc_get_next_devnum(void)
61 struct blk_desc *mmc_get_blk_desc(struct mmc *mmc)
63 return &mmc->block_dev;
71 void mmc_do_preinit(void)
74 struct list_head *entry;
76 list_for_each(entry, &mmc_devices) {
77 m = list_entry(entry, struct mmc, link);
79 #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
80 mmc_set_preinit(m, 1);
88 void mmc_list_init(void)
90 INIT_LIST_HEAD(&mmc_devices);
94 void mmc_list_add(struct mmc *mmc)
96 INIT_LIST_HEAD(&mmc->link);
98 list_add_tail(&mmc->link, &mmc_devices);
101 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
102 void print_mmc_devices(char separator)
105 struct list_head *entry;
108 list_for_each(entry, &mmc_devices) {
109 m = list_entry(entry, struct mmc, link);
112 mmc_type = IS_SD(m) ? "SD" : "eMMC";
116 printf("%s: %d", m->cfg->name, m->block_dev.devnum);
118 printf(" (%s)", mmc_type);
120 if (entry->next != &mmc_devices) {
121 printf("%c", separator);
122 if (separator != '\n')
131 void print_mmc_devices(char separator) { }
134 #if CONFIG_IS_ENABLED(MMC_TINY)
135 static struct mmc mmc_static = {
139 .if_type = IF_TYPE_MMC,
142 .block_read = mmc_bread,
143 .block_write = mmc_bwrite,
144 .block_erase = mmc_berase,
149 struct mmc *mmc_create(const struct mmc_config *cfg, void *priv)
151 struct mmc *mmc = &mmc_static;
153 /* First MMC device registered, fail to register a new one.
154 * Given users are not expecting this to fail, instead
155 * of failing let's just return the only MMC device
158 debug("Warning: MMC_TINY doesn't support multiple MMC devices\n");
168 void mmc_destroy(struct mmc *mmc)
172 struct mmc *mmc_create(const struct mmc_config *cfg, void *priv)
174 struct blk_desc *bdesc;
177 /* quick validation */
178 if (cfg == NULL || cfg->f_min == 0 ||
179 cfg->f_max == 0 || cfg->b_max == 0)
182 #if !CONFIG_IS_ENABLED(DM_MMC)
183 if (cfg->ops == NULL || cfg->ops->send_cmd == NULL)
187 mmc = calloc(1, sizeof(*mmc));
194 /* the following chunk was mmc_register() */
196 /* Setup dsr related values */
198 mmc->dsr = 0xffffffff;
199 /* Setup the universal parts of the block interface just once */
200 bdesc = mmc_get_blk_desc(mmc);
201 bdesc->if_type = IF_TYPE_MMC;
202 bdesc->removable = 1;
203 bdesc->devnum = mmc_get_next_devnum();
204 bdesc->block_read = mmc_bread;
205 bdesc->block_write = mmc_bwrite;
206 bdesc->block_erase = mmc_berase;
208 /* setup initial part type */
209 bdesc->part_type = mmc->cfg->part_type;
215 void mmc_destroy(struct mmc *mmc)
217 /* only freeing memory for now */
222 static int mmc_select_hwpartp(struct blk_desc *desc, int hwpart)
224 struct mmc *mmc = find_mmc_device(desc->devnum);
230 if (mmc->block_dev.hwpart == hwpart)
233 if (mmc->part_config == MMCPART_NOAVAILABLE)
236 ret = mmc_switch_part(mmc, hwpart);
243 static int mmc_get_dev(int dev, struct blk_desc **descp)
245 struct mmc *mmc = find_mmc_device(dev);
254 *descp = &mmc->block_dev;
259 U_BOOT_LEGACY_BLK(mmc) = {
260 .if_typename = "mmc",
261 .if_type = IF_TYPE_MMC,
263 .get_dev = mmc_get_dev,
264 .select_hwpart = mmc_select_hwpartp,