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;
159 void mmc_destroy(struct mmc *mmc)
163 struct mmc *mmc_create(const struct mmc_config *cfg, void *priv)
165 struct blk_desc *bdesc;
168 /* quick validation */
169 if (cfg == NULL || cfg->f_min == 0 ||
170 cfg->f_max == 0 || cfg->b_max == 0)
173 #if !CONFIG_IS_ENABLED(DM_MMC)
174 if (cfg->ops == NULL || cfg->ops->send_cmd == NULL)
178 mmc = calloc(1, sizeof(*mmc));
185 /* the following chunk was mmc_register() */
187 /* Setup dsr related values */
189 mmc->dsr = 0xffffffff;
190 /* Setup the universal parts of the block interface just once */
191 bdesc = mmc_get_blk_desc(mmc);
192 bdesc->if_type = IF_TYPE_MMC;
193 bdesc->removable = 1;
194 bdesc->devnum = mmc_get_next_devnum();
195 bdesc->block_read = mmc_bread;
196 bdesc->block_write = mmc_bwrite;
197 bdesc->block_erase = mmc_berase;
199 /* setup initial part type */
200 bdesc->part_type = mmc->cfg->part_type;
206 void mmc_destroy(struct mmc *mmc)
208 /* only freeing memory for now */
213 static int mmc_select_hwpartp(struct blk_desc *desc, int hwpart)
215 struct mmc *mmc = find_mmc_device(desc->devnum);
221 if (mmc->block_dev.hwpart == hwpart)
224 if (mmc->part_config == MMCPART_NOAVAILABLE)
227 ret = mmc_switch_part(mmc, hwpart);
234 static int mmc_get_dev(int dev, struct blk_desc **descp)
236 struct mmc *mmc = find_mmc_device(dev);
245 *descp = &mmc->block_dev;
250 U_BOOT_LEGACY_BLK(mmc) = {
251 .if_typename = "mmc",
252 .if_type = IF_TYPE_MMC,
254 .get_dev = mmc_get_dev,
255 .select_hwpart = mmc_select_hwpartp,