blob: 16cc91ebdfd002d845227e457422a63c9c9c3be7 [file] [log] [blame]
Simon Glass1e8eb1b2015-06-23 15:38:48 -06001/*
2 * Copyright (C) 2015 Google, Inc
3 * Written by Simon Glass <sjg@chromium.org>
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7
8#include <common.h>
9#include <mmc.h>
10#include <dm.h>
Simon Glass2f1ea902016-06-12 23:30:16 -060011#include <dm/device-internal.h>
Simon Glass1e8eb1b2015-06-23 15:38:48 -060012#include <dm/lists.h>
13#include <dm/root.h>
Simon Glass2f1ea902016-06-12 23:30:16 -060014#include "mmc_private.h"
Simon Glass1e8eb1b2015-06-23 15:38:48 -060015
Jaehoon Chung2910da82017-02-02 13:41:14 +090016DECLARE_GLOBAL_DATA_PTR;
17
Simon Glass394dfc02016-06-12 23:30:22 -060018#ifdef CONFIG_DM_MMC_OPS
19int dm_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
20 struct mmc_data *data)
21{
22 struct mmc *mmc = mmc_get_mmc_dev(dev);
23 struct dm_mmc_ops *ops = mmc_get_ops(dev);
24 int ret;
25
26 mmmc_trace_before_send(mmc, cmd);
27 if (ops->send_cmd)
28 ret = ops->send_cmd(dev, cmd, data);
29 else
30 ret = -ENOSYS;
31 mmmc_trace_after_send(mmc, cmd, ret);
32
33 return ret;
34}
35
36int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
37{
38 return dm_mmc_send_cmd(mmc->dev, cmd, data);
39}
40
41int dm_mmc_set_ios(struct udevice *dev)
42{
43 struct dm_mmc_ops *ops = mmc_get_ops(dev);
44
45 if (!ops->set_ios)
46 return -ENOSYS;
47 return ops->set_ios(dev);
48}
49
50int mmc_set_ios(struct mmc *mmc)
51{
52 return dm_mmc_set_ios(mmc->dev);
53}
54
55int dm_mmc_get_wp(struct udevice *dev)
56{
57 struct dm_mmc_ops *ops = mmc_get_ops(dev);
58
59 if (!ops->get_wp)
60 return -ENOSYS;
61 return ops->get_wp(dev);
62}
63
64int mmc_getwp(struct mmc *mmc)
65{
66 return dm_mmc_get_wp(mmc->dev);
67}
68
69int dm_mmc_get_cd(struct udevice *dev)
70{
71 struct dm_mmc_ops *ops = mmc_get_ops(dev);
72
73 if (!ops->get_cd)
74 return -ENOSYS;
75 return ops->get_cd(dev);
76}
77
78int mmc_getcd(struct mmc *mmc)
79{
80 return dm_mmc_get_cd(mmc->dev);
81}
82#endif
83
Simon Glass1e8eb1b2015-06-23 15:38:48 -060084struct mmc *mmc_get_mmc_dev(struct udevice *dev)
85{
86 struct mmc_uclass_priv *upriv;
87
88 if (!device_active(dev))
89 return NULL;
90 upriv = dev_get_uclass_priv(dev);
91 return upriv->mmc;
92}
93
Simon Glass4bca6662016-05-01 13:52:39 -060094#ifdef CONFIG_BLK
95struct mmc *find_mmc_device(int dev_num)
96{
97 struct udevice *dev, *mmc_dev;
98 int ret;
99
100 ret = blk_get_device(IF_TYPE_MMC, dev_num, &dev);
101
102 if (ret) {
103#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
104 printf("MMC Device %d not found\n", dev_num);
105#endif
106 return NULL;
107 }
108
109 mmc_dev = dev_get_parent(dev);
110
111 return mmc_get_mmc_dev(mmc_dev);
112}
113
114int get_mmc_num(void)
115{
Kever Yang38456602016-07-22 17:22:50 +0800116 return max((blk_find_max_devnum(IF_TYPE_MMC) + 1), 0);
Simon Glass4bca6662016-05-01 13:52:39 -0600117}
118
119int mmc_get_next_devnum(void)
120{
Masahiro Yamadaa6f07e52016-10-14 00:13:18 +0900121 return blk_find_max_devnum(IF_TYPE_MMC);
Simon Glass4bca6662016-05-01 13:52:39 -0600122}
123
124struct blk_desc *mmc_get_blk_desc(struct mmc *mmc)
125{
126 struct blk_desc *desc;
127 struct udevice *dev;
128
129 device_find_first_child(mmc->dev, &dev);
130 if (!dev)
131 return NULL;
132 desc = dev_get_uclass_platdata(dev);
133
134 return desc;
135}
136
137void mmc_do_preinit(void)
138{
139 struct udevice *dev;
140 struct uclass *uc;
141 int ret;
142
143 ret = uclass_get(UCLASS_MMC, &uc);
144 if (ret)
145 return;
146 uclass_foreach_dev(dev, uc) {
147 struct mmc *m = mmc_get_mmc_dev(dev);
148
149 if (!m)
150 continue;
151#ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
152 mmc_set_preinit(m, 1);
153#endif
154 if (m->preinit)
155 mmc_start_init(m);
156 }
157}
158
159#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
160void print_mmc_devices(char separator)
161{
162 struct udevice *dev;
163 char *mmc_type;
164 bool first = true;
165
166 for (uclass_first_device(UCLASS_MMC, &dev);
167 dev;
Xu Ziyuan77d747e2016-07-23 11:11:22 +0800168 uclass_next_device(&dev), first = false) {
Simon Glass4bca6662016-05-01 13:52:39 -0600169 struct mmc *m = mmc_get_mmc_dev(dev);
170
171 if (!first) {
172 printf("%c", separator);
173 if (separator != '\n')
174 puts(" ");
175 }
176 if (m->has_init)
177 mmc_type = IS_SD(m) ? "SD" : "eMMC";
178 else
179 mmc_type = NULL;
180
181 printf("%s: %d", m->cfg->name, mmc_get_blk_desc(m)->devnum);
182 if (mmc_type)
183 printf(" (%s)", mmc_type);
184 }
185
186 printf("\n");
187}
188
189#else
190void print_mmc_devices(char separator) { }
191#endif
Simon Glass2f1ea902016-06-12 23:30:16 -0600192
193int mmc_bind(struct udevice *dev, struct mmc *mmc, const struct mmc_config *cfg)
194{
195 struct blk_desc *bdesc;
196 struct udevice *bdev;
Jaehoon Chung2910da82017-02-02 13:41:14 +0900197 int ret, devnum = -1;
198
Simon Glass9e720b02017-04-23 20:02:09 -0600199#ifdef CONFIG_DM_MMC_OPS
200 if (!mmc_get_ops(dev))
201 return -ENOSYS;
202#endif
Jaehoon Chung2910da82017-02-02 13:41:14 +0900203#ifndef CONFIG_SPL_BUILD
204 /* Use the fixed index with aliase node's index */
Simon Glass7a494432017-05-17 17:18:09 -0600205 fdtdec_get_alias_seq(gd->fdt_blob, "mmc", dev_of_offset(dev), &devnum);
Jaehoon Chung2910da82017-02-02 13:41:14 +0900206#endif
Simon Glass2f1ea902016-06-12 23:30:16 -0600207
Jaehoon Chung2910da82017-02-02 13:41:14 +0900208 ret = blk_create_devicef(dev, "mmc_blk", "blk", IF_TYPE_MMC,
209 devnum, 512, 0, &bdev);
Simon Glass2f1ea902016-06-12 23:30:16 -0600210 if (ret) {
211 debug("Cannot create block device\n");
212 return ret;
213 }
214 bdesc = dev_get_uclass_platdata(bdev);
215 mmc->cfg = cfg;
216 mmc->priv = dev;
217
218 /* the following chunk was from mmc_register() */
219
220 /* Setup dsr related values */
221 mmc->dsr_imp = 0;
222 mmc->dsr = 0xffffffff;
223 /* Setup the universal parts of the block interface just once */
224 bdesc->removable = 1;
225
226 /* setup initial part type */
227 bdesc->part_type = cfg->part_type;
228 mmc->dev = dev;
229
230 return 0;
231}
232
233int mmc_unbind(struct udevice *dev)
234{
235 struct udevice *bdev;
236
237 device_find_first_child(dev, &bdev);
238 if (bdev) {
Stefan Roese80b5bc92017-03-20 12:51:48 +0100239 device_remove(bdev, DM_REMOVE_NORMAL);
Simon Glass2f1ea902016-06-12 23:30:16 -0600240 device_unbind(bdev);
241 }
242
243 return 0;
244}
245
246static int mmc_select_hwpart(struct udevice *bdev, int hwpart)
247{
248 struct udevice *mmc_dev = dev_get_parent(bdev);
249 struct mmc *mmc = mmc_get_mmc_dev(mmc_dev);
250 struct blk_desc *desc = dev_get_uclass_platdata(bdev);
Simon Glass2f1ea902016-06-12 23:30:16 -0600251
252 if (desc->hwpart == hwpart)
253 return 0;
254
255 if (mmc->part_config == MMCPART_NOAVAILABLE)
256 return -EMEDIUMTYPE;
257
Masahiro Yamadaa6f07e52016-10-14 00:13:18 +0900258 return mmc_switch_part(mmc, hwpart);
Simon Glass2f1ea902016-06-12 23:30:16 -0600259}
260
Fiach Antaw7cdbe502017-01-25 19:00:24 +1000261static int mmc_blk_probe(struct udevice *dev)
262{
Simon Glasse11b2392017-04-23 20:02:10 -0600263 struct udevice *mmc_dev = dev_get_parent(dev);
264 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(mmc_dev);
265 struct mmc *mmc = upriv->mmc;
266 int ret;
267
268 ret = mmc_init(mmc);
269 if (ret) {
270 debug("%s: mmc_init() failed (err=%d)\n", __func__, ret);
271 return ret;
272 }
Fiach Antaw7cdbe502017-01-25 19:00:24 +1000273
Simon Glasse11b2392017-04-23 20:02:10 -0600274 return 0;
Fiach Antaw7cdbe502017-01-25 19:00:24 +1000275}
276
Simon Glass2f1ea902016-06-12 23:30:16 -0600277static const struct blk_ops mmc_blk_ops = {
278 .read = mmc_bread,
279#ifndef CONFIG_SPL_BUILD
280 .write = mmc_bwrite,
Simon Glassfcc53c12016-10-01 14:43:17 -0600281 .erase = mmc_berase,
Simon Glass2f1ea902016-06-12 23:30:16 -0600282#endif
283 .select_hwpart = mmc_select_hwpart,
284};
285
286U_BOOT_DRIVER(mmc_blk) = {
287 .name = "mmc_blk",
288 .id = UCLASS_BLK,
289 .ops = &mmc_blk_ops,
Fiach Antaw7cdbe502017-01-25 19:00:24 +1000290 .probe = mmc_blk_probe,
Simon Glass2f1ea902016-06-12 23:30:16 -0600291};
Simon Glass4bca6662016-05-01 13:52:39 -0600292#endif /* CONFIG_BLK */
293
Simon Glass1e8eb1b2015-06-23 15:38:48 -0600294U_BOOT_DRIVER(mmc) = {
295 .name = "mmc",
296 .id = UCLASS_MMC,
297};
298
299UCLASS_DRIVER(mmc) = {
300 .id = UCLASS_MMC,
301 .name = "mmc",
302 .flags = DM_UC_FLAG_SEQ_ALIAS,
303 .per_device_auto_alloc_size = sizeof(struct mmc_uclass_priv),
304};