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 struct mmc *find_mmc_device(int dev_num)
19 struct list_head *entry;
21 list_for_each(entry, &mmc_devices) {
22 m = list_entry(entry, struct mmc, link);
24 if (m->block_dev.devnum == dev_num)
28 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
29 printf("MMC Device %d not found\n", dev_num);
35 int mmc_get_next_devnum(void)
40 struct blk_desc *mmc_get_blk_desc(struct mmc *mmc)
42 return &mmc->block_dev;
50 void mmc_do_preinit(void)
53 struct list_head *entry;
55 list_for_each(entry, &mmc_devices) {
56 m = list_entry(entry, struct mmc, link);
58 #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
59 mmc_set_preinit(m, 1);
67 void mmc_list_init(void)
69 INIT_LIST_HEAD(&mmc_devices);
73 void mmc_list_add(struct mmc *mmc)
75 INIT_LIST_HEAD(&mmc->link);
77 list_add_tail(&mmc->link, &mmc_devices);
80 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
81 void print_mmc_devices(char separator)
84 struct list_head *entry;
87 list_for_each(entry, &mmc_devices) {
88 m = list_entry(entry, struct mmc, link);
91 mmc_type = IS_SD(m) ? "SD" : "eMMC";
95 printf("%s: %d", m->cfg->name, m->block_dev.devnum);
97 printf(" (%s)", mmc_type);
99 if (entry->next != &mmc_devices) {
100 printf("%c", separator);
101 if (separator != '\n')
110 void print_mmc_devices(char separator) { }
113 #if CONFIG_IS_ENABLED(MMC_TINY)
114 static struct mmc mmc_static = {
118 .if_type = IF_TYPE_MMC,
121 .block_read = mmc_bread,
122 .block_write = mmc_bwrite,
123 .block_erase = mmc_berase,
128 struct mmc *mmc_create(const struct mmc_config *cfg, void *priv)
130 struct mmc *mmc = &mmc_static;
138 void mmc_destroy(struct mmc *mmc)
142 struct mmc *mmc_create(const struct mmc_config *cfg, void *priv)
144 struct blk_desc *bdesc;
147 /* quick validation */
148 if (cfg == NULL || cfg->f_min == 0 ||
149 cfg->f_max == 0 || cfg->b_max == 0)
152 #if !CONFIG_IS_ENABLED(DM_MMC)
153 if (cfg->ops == NULL || cfg->ops->send_cmd == NULL)
157 mmc = calloc(1, sizeof(*mmc));
164 /* the following chunk was mmc_register() */
166 /* Setup dsr related values */
168 mmc->dsr = 0xffffffff;
169 /* Setup the universal parts of the block interface just once */
170 bdesc = mmc_get_blk_desc(mmc);
171 bdesc->if_type = IF_TYPE_MMC;
172 bdesc->removable = 1;
173 bdesc->devnum = mmc_get_next_devnum();
174 bdesc->block_read = mmc_bread;
175 bdesc->block_write = mmc_bwrite;
176 bdesc->block_erase = mmc_berase;
178 /* setup initial part type */
179 bdesc->part_type = mmc->cfg->part_type;
185 void mmc_destroy(struct mmc *mmc)
187 /* only freeing memory for now */
192 static int mmc_select_hwpartp(struct blk_desc *desc, int hwpart)
194 struct mmc *mmc = find_mmc_device(desc->devnum);
200 if (mmc->block_dev.hwpart == hwpart)
203 if (mmc->part_config == MMCPART_NOAVAILABLE)
206 ret = mmc_switch_part(mmc, hwpart);
213 static int mmc_get_dev(int dev, struct blk_desc **descp)
215 struct mmc *mmc = find_mmc_device(dev);
224 *descp = &mmc->block_dev;
229 U_BOOT_LEGACY_BLK(mmc) = {
230 .if_typename = "mmc",
231 .if_type = IF_TYPE_MMC,
233 .get_dev = mmc_get_dev,
234 .select_hwpart = mmc_select_hwpartp,