blob: 2bb12ceeaf105ad630ebfc7d110c559293fb9676 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glasse5db1152016-05-01 13:52:35 -06002/*
3 * Copyright (C) 2016 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glasse5db1152016-05-01 13:52:35 -06005 */
6
7#include <common.h>
Simon Glass0f2af882020-05-10 11:40:05 -06008#include <log.h>
Simon Glass21875cf2016-06-12 23:30:17 -06009#include <malloc.h>
Simon Glasse5db1152016-05-01 13:52:35 -060010#include <mmc.h>
Simon Glass21875cf2016-06-12 23:30:17 -060011#include "mmc_private.h"
Simon Glasse5db1152016-05-01 13:52:35 -060012
13static struct list_head mmc_devices;
14static int cur_dev_num = -1;
15
Marek Vasuta318a7a2018-04-15 00:37:11 +020016#if CONFIG_IS_ENABLED(MMC_TINY)
17static struct mmc mmc_static;
18struct mmc *find_mmc_device(int dev_num)
19{
20 return &mmc_static;
21}
22
23void mmc_do_preinit(void)
24{
25 struct mmc *m = &mmc_static;
26#ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
27 mmc_set_preinit(m, 1);
28#endif
29 if (m->preinit)
30 mmc_start_init(m);
31}
32
33struct blk_desc *mmc_get_blk_desc(struct mmc *mmc)
34{
35 return &mmc->block_dev;
36}
37#else
Simon Glasse5db1152016-05-01 13:52:35 -060038struct mmc *find_mmc_device(int dev_num)
39{
40 struct mmc *m;
41 struct list_head *entry;
42
43 list_for_each(entry, &mmc_devices) {
44 m = list_entry(entry, struct mmc, link);
45
46 if (m->block_dev.devnum == dev_num)
47 return m;
48 }
49
50#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
51 printf("MMC Device %d not found\n", dev_num);
52#endif
53
54 return NULL;
55}
56
57int mmc_get_next_devnum(void)
58{
59 return cur_dev_num++;
60}
61
62struct blk_desc *mmc_get_blk_desc(struct mmc *mmc)
63{
64 return &mmc->block_dev;
65}
66
67int get_mmc_num(void)
68{
69 return cur_dev_num;
70}
71
72void mmc_do_preinit(void)
73{
74 struct mmc *m;
75 struct list_head *entry;
76
77 list_for_each(entry, &mmc_devices) {
78 m = list_entry(entry, struct mmc, link);
79
80#ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
81 mmc_set_preinit(m, 1);
82#endif
83 if (m->preinit)
84 mmc_start_init(m);
85 }
86}
Marek Vasutf537e392016-12-01 02:06:33 +010087#endif
Simon Glasse5db1152016-05-01 13:52:35 -060088
89void mmc_list_init(void)
90{
91 INIT_LIST_HEAD(&mmc_devices);
92 cur_dev_num = 0;
93}
94
95void mmc_list_add(struct mmc *mmc)
96{
97 INIT_LIST_HEAD(&mmc->link);
98
99 list_add_tail(&mmc->link, &mmc_devices);
100}
101
102#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
103void print_mmc_devices(char separator)
104{
105 struct mmc *m;
106 struct list_head *entry;
107 char *mmc_type;
108
109 list_for_each(entry, &mmc_devices) {
110 m = list_entry(entry, struct mmc, link);
111
112 if (m->has_init)
113 mmc_type = IS_SD(m) ? "SD" : "eMMC";
114 else
115 mmc_type = NULL;
116
117 printf("%s: %d", m->cfg->name, m->block_dev.devnum);
118 if (mmc_type)
119 printf(" (%s)", mmc_type);
120
121 if (entry->next != &mmc_devices) {
122 printf("%c", separator);
123 if (separator != '\n')
124 puts(" ");
125 }
126 }
127
128 printf("\n");
129}
130
131#else
132void print_mmc_devices(char separator) { }
133#endif
Simon Glass21875cf2016-06-12 23:30:17 -0600134
Marek Vasutf537e392016-12-01 02:06:33 +0100135#if CONFIG_IS_ENABLED(MMC_TINY)
136static struct mmc mmc_static = {
137 .dsr_imp = 0,
138 .dsr = 0xffffffff,
139 .block_dev = {
140 .if_type = IF_TYPE_MMC,
141 .removable = 1,
142 .devnum = 0,
143 .block_read = mmc_bread,
144 .block_write = mmc_bwrite,
145 .block_erase = mmc_berase,
146 .part_type = 0,
147 },
148};
149
150struct mmc *mmc_create(const struct mmc_config *cfg, void *priv)
151{
152 struct mmc *mmc = &mmc_static;
153
Ezequiel Garcia89676642019-05-25 19:25:22 -0300154 /* First MMC device registered, fail to register a new one.
155 * Given users are not expecting this to fail, instead
156 * of failing let's just return the only MMC device
157 */
158 if (mmc->cfg) {
159 debug("Warning: MMC_TINY doesn't support multiple MMC devices\n");
160 return mmc;
161 }
162
Marek Vasutf537e392016-12-01 02:06:33 +0100163 mmc->cfg = cfg;
164 mmc->priv = priv;
165
166 return mmc;
167}
168
169void mmc_destroy(struct mmc *mmc)
170{
171}
172#else
Simon Glass21875cf2016-06-12 23:30:17 -0600173struct mmc *mmc_create(const struct mmc_config *cfg, void *priv)
174{
175 struct blk_desc *bdesc;
176 struct mmc *mmc;
177
178 /* quick validation */
Jaehoon Chung38c27772016-08-12 11:39:05 +0900179 if (cfg == NULL || cfg->f_min == 0 ||
180 cfg->f_max == 0 || cfg->b_max == 0)
181 return NULL;
182
Simon Glasseba48f92017-07-29 11:35:31 -0600183#if !CONFIG_IS_ENABLED(DM_MMC)
Jaehoon Chung38c27772016-08-12 11:39:05 +0900184 if (cfg->ops == NULL || cfg->ops->send_cmd == NULL)
Simon Glass21875cf2016-06-12 23:30:17 -0600185 return NULL;
Jaehoon Chung38c27772016-08-12 11:39:05 +0900186#endif
Simon Glass21875cf2016-06-12 23:30:17 -0600187
188 mmc = calloc(1, sizeof(*mmc));
189 if (mmc == NULL)
190 return NULL;
191
192 mmc->cfg = cfg;
193 mmc->priv = priv;
194
195 /* the following chunk was mmc_register() */
196
197 /* Setup dsr related values */
198 mmc->dsr_imp = 0;
199 mmc->dsr = 0xffffffff;
200 /* Setup the universal parts of the block interface just once */
201 bdesc = mmc_get_blk_desc(mmc);
202 bdesc->if_type = IF_TYPE_MMC;
203 bdesc->removable = 1;
204 bdesc->devnum = mmc_get_next_devnum();
205 bdesc->block_read = mmc_bread;
206 bdesc->block_write = mmc_bwrite;
207 bdesc->block_erase = mmc_berase;
208
209 /* setup initial part type */
210 bdesc->part_type = mmc->cfg->part_type;
211 mmc_list_add(mmc);
212
213 return mmc;
214}
215
216void mmc_destroy(struct mmc *mmc)
217{
218 /* only freeing memory for now */
219 free(mmc);
220}
Marek Vasutf537e392016-12-01 02:06:33 +0100221#endif
Simon Glass21875cf2016-06-12 23:30:17 -0600222
223static int mmc_select_hwpartp(struct blk_desc *desc, int hwpart)
224{
225 struct mmc *mmc = find_mmc_device(desc->devnum);
226 int ret;
227
228 if (!mmc)
229 return -ENODEV;
230
231 if (mmc->block_dev.hwpart == hwpart)
232 return 0;
233
234 if (mmc->part_config == MMCPART_NOAVAILABLE)
235 return -EMEDIUMTYPE;
236
237 ret = mmc_switch_part(mmc, hwpart);
238 if (ret)
239 return ret;
240
241 return 0;
242}
243
244static int mmc_get_dev(int dev, struct blk_desc **descp)
245{
246 struct mmc *mmc = find_mmc_device(dev);
247 int ret;
248
249 if (!mmc)
250 return -ENODEV;
251 ret = mmc_init(mmc);
252 if (ret)
253 return ret;
254
255 *descp = &mmc->block_dev;
256
257 return 0;
258}
259
260U_BOOT_LEGACY_BLK(mmc) = {
261 .if_typename = "mmc",
262 .if_type = IF_TYPE_MMC,
263 .max_devs = -1,
264 .get_dev = mmc_get_dev,
265 .select_hwpart = mmc_select_hwpartp,
266};