2 * Copyright (C) 2016 Google, Inc
3 * Written by Simon Glass <sjg@chromium.org>
5 * SPDX-License-Identifier: GPL-2.0+
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 struct mmc *find_mmc_device(int dev_num)
20 struct list_head *entry;
22 list_for_each(entry, &mmc_devices) {
23 m = list_entry(entry, struct mmc, link);
25 if (m->block_dev.devnum == dev_num)
29 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
30 printf("MMC Device %d not found\n", dev_num);
36 int mmc_get_next_devnum(void)
41 struct blk_desc *mmc_get_blk_desc(struct mmc *mmc)
43 return &mmc->block_dev;
51 void mmc_do_preinit(void)
54 struct list_head *entry;
56 list_for_each(entry, &mmc_devices) {
57 m = list_entry(entry, struct mmc, link);
59 #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
60 mmc_set_preinit(m, 1);
68 void mmc_list_init(void)
70 INIT_LIST_HEAD(&mmc_devices);
74 void mmc_list_add(struct mmc *mmc)
76 INIT_LIST_HEAD(&mmc->link);
78 list_add_tail(&mmc->link, &mmc_devices);
81 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
82 void print_mmc_devices(char separator)
85 struct list_head *entry;
88 list_for_each(entry, &mmc_devices) {
89 m = list_entry(entry, struct mmc, link);
92 mmc_type = IS_SD(m) ? "SD" : "eMMC";
96 printf("%s: %d", m->cfg->name, m->block_dev.devnum);
98 printf(" (%s)", mmc_type);
100 if (entry->next != &mmc_devices) {
101 printf("%c", separator);
102 if (separator != '\n')
111 void print_mmc_devices(char separator) { }
114 #if CONFIG_IS_ENABLED(MMC_TINY)
115 static struct mmc mmc_static = {
119 .if_type = IF_TYPE_MMC,
122 .block_read = mmc_bread,
123 .block_write = mmc_bwrite,
124 .block_erase = mmc_berase,
129 struct mmc *mmc_create(const struct mmc_config *cfg, void *priv)
131 struct mmc *mmc = &mmc_static;
139 void mmc_destroy(struct mmc *mmc)
143 struct mmc *mmc_create(const struct mmc_config *cfg, void *priv)
145 struct blk_desc *bdesc;
148 /* quick validation */
149 if (cfg == NULL || cfg->f_min == 0 ||
150 cfg->f_max == 0 || cfg->b_max == 0)
153 #if !CONFIG_IS_ENABLED(DM_MMC)
154 if (cfg->ops == NULL || cfg->ops->send_cmd == NULL)
158 mmc = calloc(1, sizeof(*mmc));
165 /* the following chunk was mmc_register() */
167 /* Setup dsr related values */
169 mmc->dsr = 0xffffffff;
170 /* Setup the universal parts of the block interface just once */
171 bdesc = mmc_get_blk_desc(mmc);
172 bdesc->if_type = IF_TYPE_MMC;
173 bdesc->removable = 1;
174 bdesc->devnum = mmc_get_next_devnum();
175 bdesc->block_read = mmc_bread;
176 bdesc->block_write = mmc_bwrite;
177 bdesc->block_erase = mmc_berase;
179 /* setup initial part type */
180 bdesc->part_type = mmc->cfg->part_type;
186 void mmc_destroy(struct mmc *mmc)
188 /* only freeing memory for now */
193 static int mmc_select_hwpartp(struct blk_desc *desc, int hwpart)
195 struct mmc *mmc = find_mmc_device(desc->devnum);
201 if (mmc->block_dev.hwpart == hwpart)
204 if (mmc->part_config == MMCPART_NOAVAILABLE)
207 ret = mmc_switch_part(mmc, hwpart);
214 static int mmc_get_dev(int dev, struct blk_desc **descp)
216 struct mmc *mmc = find_mmc_device(dev);
225 *descp = &mmc->block_dev;
230 U_BOOT_LEGACY_BLK(mmc) = {
231 .if_typename = "mmc",
232 .if_type = IF_TYPE_MMC,
234 .get_dev = mmc_get_dev,
235 .select_hwpart = mmc_select_hwpartp,