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 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);
66 void mmc_list_init(void)
68 INIT_LIST_HEAD(&mmc_devices);
72 void mmc_list_add(struct mmc *mmc)
74 INIT_LIST_HEAD(&mmc->link);
76 list_add_tail(&mmc->link, &mmc_devices);
79 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
80 void print_mmc_devices(char separator)
83 struct list_head *entry;
86 list_for_each(entry, &mmc_devices) {
87 m = list_entry(entry, struct mmc, link);
90 mmc_type = IS_SD(m) ? "SD" : "eMMC";
94 printf("%s: %d", m->cfg->name, m->block_dev.devnum);
96 printf(" (%s)", mmc_type);
98 if (entry->next != &mmc_devices) {
99 printf("%c", separator);
100 if (separator != '\n')
109 void print_mmc_devices(char separator) { }
112 struct mmc *mmc_create(const struct mmc_config *cfg, void *priv)
114 struct blk_desc *bdesc;
117 /* quick validation */
118 if (cfg == NULL || cfg->ops == NULL || cfg->ops->send_cmd == NULL ||
119 cfg->f_min == 0 || cfg->f_max == 0 || cfg->b_max == 0)
122 mmc = calloc(1, sizeof(*mmc));
129 /* the following chunk was mmc_register() */
131 /* Setup dsr related values */
133 mmc->dsr = 0xffffffff;
134 /* Setup the universal parts of the block interface just once */
135 bdesc = mmc_get_blk_desc(mmc);
136 bdesc->if_type = IF_TYPE_MMC;
137 bdesc->removable = 1;
138 bdesc->devnum = mmc_get_next_devnum();
139 bdesc->block_read = mmc_bread;
140 bdesc->block_write = mmc_bwrite;
141 bdesc->block_erase = mmc_berase;
143 /* setup initial part type */
144 bdesc->part_type = mmc->cfg->part_type;
150 void mmc_destroy(struct mmc *mmc)
152 /* only freeing memory for now */
156 static int mmc_select_hwpartp(struct blk_desc *desc, int hwpart)
158 struct mmc *mmc = find_mmc_device(desc->devnum);
164 if (mmc->block_dev.hwpart == hwpart)
167 if (mmc->part_config == MMCPART_NOAVAILABLE)
170 ret = mmc_switch_part(mmc, hwpart);
177 static int mmc_get_dev(int dev, struct blk_desc **descp)
179 struct mmc *mmc = find_mmc_device(dev);
188 *descp = &mmc->block_dev;
193 U_BOOT_LEGACY_BLK(mmc) = {
194 .if_typename = "mmc",
195 .if_type = IF_TYPE_MMC,
197 .get_dev = mmc_get_dev,
198 .select_hwpart = mmc_select_hwpartp,