]> git.sur5r.net Git - u-boot/blob - drivers/mmc/mmc-uclass.c
mmc: Add a execute_tuning() callback to the mmc operations.
[u-boot] / drivers / mmc / mmc-uclass.c
1 /*
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>
11 #include <dm/device-internal.h>
12 #include <dm/lists.h>
13 #include <dm/root.h>
14 #include "mmc_private.h"
15
16 DECLARE_GLOBAL_DATA_PTR;
17
18 int dm_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
19                     struct mmc_data *data)
20 {
21         struct mmc *mmc = mmc_get_mmc_dev(dev);
22         struct dm_mmc_ops *ops = mmc_get_ops(dev);
23         int ret;
24
25         mmmc_trace_before_send(mmc, cmd);
26         if (ops->send_cmd)
27                 ret = ops->send_cmd(dev, cmd, data);
28         else
29                 ret = -ENOSYS;
30         mmmc_trace_after_send(mmc, cmd, ret);
31
32         return ret;
33 }
34
35 int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
36 {
37         return dm_mmc_send_cmd(mmc->dev, cmd, data);
38 }
39
40 int dm_mmc_set_ios(struct udevice *dev)
41 {
42         struct dm_mmc_ops *ops = mmc_get_ops(dev);
43
44         if (!ops->set_ios)
45                 return -ENOSYS;
46         return ops->set_ios(dev);
47 }
48
49 int mmc_set_ios(struct mmc *mmc)
50 {
51         return dm_mmc_set_ios(mmc->dev);
52 }
53
54 void dm_mmc_send_init_stream(struct udevice *dev)
55 {
56         struct dm_mmc_ops *ops = mmc_get_ops(dev);
57
58         if (ops->send_init_stream)
59                 ops->send_init_stream(dev);
60 }
61
62 void mmc_send_init_stream(struct mmc *mmc)
63 {
64         dm_mmc_send_init_stream(mmc->dev);
65 }
66
67 int dm_mmc_get_wp(struct udevice *dev)
68 {
69         struct dm_mmc_ops *ops = mmc_get_ops(dev);
70
71         if (!ops->get_wp)
72                 return -ENOSYS;
73         return ops->get_wp(dev);
74 }
75
76 int mmc_getwp(struct mmc *mmc)
77 {
78         return dm_mmc_get_wp(mmc->dev);
79 }
80
81 int dm_mmc_get_cd(struct udevice *dev)
82 {
83         struct dm_mmc_ops *ops = mmc_get_ops(dev);
84
85         if (!ops->get_cd)
86                 return -ENOSYS;
87         return ops->get_cd(dev);
88 }
89
90 int mmc_getcd(struct mmc *mmc)
91 {
92         return dm_mmc_get_cd(mmc->dev);
93 }
94
95 int dm_mmc_execute_tuning(struct udevice *dev, uint opcode)
96 {
97         struct dm_mmc_ops *ops = mmc_get_ops(dev);
98
99         if (!ops->execute_tuning)
100                 return -ENOSYS;
101         return ops->execute_tuning(dev, opcode);
102 }
103
104 int mmc_execute_tuning(struct mmc *mmc, uint opcode)
105 {
106         return dm_mmc_execute_tuning(mmc->dev, opcode);
107 }
108
109 struct mmc *mmc_get_mmc_dev(struct udevice *dev)
110 {
111         struct mmc_uclass_priv *upriv;
112
113         if (!device_active(dev))
114                 return NULL;
115         upriv = dev_get_uclass_priv(dev);
116         return upriv->mmc;
117 }
118
119 #if CONFIG_IS_ENABLED(BLK)
120 struct mmc *find_mmc_device(int dev_num)
121 {
122         struct udevice *dev, *mmc_dev;
123         int ret;
124
125         ret = blk_find_device(IF_TYPE_MMC, dev_num, &dev);
126
127         if (ret) {
128 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
129                 printf("MMC Device %d not found\n", dev_num);
130 #endif
131                 return NULL;
132         }
133
134         mmc_dev = dev_get_parent(dev);
135
136         struct mmc *mmc = mmc_get_mmc_dev(mmc_dev);
137
138         return mmc;
139 }
140
141 int get_mmc_num(void)
142 {
143         return max((blk_find_max_devnum(IF_TYPE_MMC) + 1), 0);
144 }
145
146 int mmc_get_next_devnum(void)
147 {
148         return blk_find_max_devnum(IF_TYPE_MMC);
149 }
150
151 struct blk_desc *mmc_get_blk_desc(struct mmc *mmc)
152 {
153         struct blk_desc *desc;
154         struct udevice *dev;
155
156         device_find_first_child(mmc->dev, &dev);
157         if (!dev)
158                 return NULL;
159         desc = dev_get_uclass_platdata(dev);
160
161         return desc;
162 }
163
164 void mmc_do_preinit(void)
165 {
166         struct udevice *dev;
167         struct uclass *uc;
168         int ret;
169
170         ret = uclass_get(UCLASS_MMC, &uc);
171         if (ret)
172                 return;
173         uclass_foreach_dev(dev, uc) {
174                 struct mmc *m = mmc_get_mmc_dev(dev);
175
176                 if (!m)
177                         continue;
178 #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
179                 mmc_set_preinit(m, 1);
180 #endif
181                 if (m->preinit)
182                         mmc_start_init(m);
183         }
184 }
185
186 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
187 void print_mmc_devices(char separator)
188 {
189         struct udevice *dev;
190         char *mmc_type;
191         bool first = true;
192
193         for (uclass_first_device(UCLASS_MMC, &dev);
194              dev;
195              uclass_next_device(&dev), first = false) {
196                 struct mmc *m = mmc_get_mmc_dev(dev);
197
198                 if (!first) {
199                         printf("%c", separator);
200                         if (separator != '\n')
201                                 puts(" ");
202                 }
203                 if (m->has_init)
204                         mmc_type = IS_SD(m) ? "SD" : "eMMC";
205                 else
206                         mmc_type = NULL;
207
208                 printf("%s: %d", m->cfg->name, mmc_get_blk_desc(m)->devnum);
209                 if (mmc_type)
210                         printf(" (%s)", mmc_type);
211         }
212
213         printf("\n");
214 }
215
216 #else
217 void print_mmc_devices(char separator) { }
218 #endif
219
220 int mmc_bind(struct udevice *dev, struct mmc *mmc, const struct mmc_config *cfg)
221 {
222         struct blk_desc *bdesc;
223         struct udevice *bdev;
224         int ret, devnum = -1;
225
226         if (!mmc_get_ops(dev))
227                 return -ENOSYS;
228 #ifndef CONFIG_SPL_BUILD
229         /* Use the fixed index with aliase node's index */
230         ret = dev_read_alias_seq(dev, &devnum);
231         debug("%s: alias ret=%d, devnum=%d\n", __func__, ret, devnum);
232 #endif
233
234         ret = blk_create_devicef(dev, "mmc_blk", "blk", IF_TYPE_MMC,
235                         devnum, 512, 0, &bdev);
236         if (ret) {
237                 debug("Cannot create block device\n");
238                 return ret;
239         }
240         bdesc = dev_get_uclass_platdata(bdev);
241         mmc->cfg = cfg;
242         mmc->priv = dev;
243
244         /* the following chunk was from mmc_register() */
245
246         /* Setup dsr related values */
247         mmc->dsr_imp = 0;
248         mmc->dsr = 0xffffffff;
249         /* Setup the universal parts of the block interface just once */
250         bdesc->removable = 1;
251
252         /* setup initial part type */
253         bdesc->part_type = cfg->part_type;
254         mmc->dev = dev;
255
256         return 0;
257 }
258
259 int mmc_unbind(struct udevice *dev)
260 {
261         struct udevice *bdev;
262
263         device_find_first_child(dev, &bdev);
264         if (bdev) {
265                 device_remove(bdev, DM_REMOVE_NORMAL);
266                 device_unbind(bdev);
267         }
268
269         return 0;
270 }
271
272 static int mmc_select_hwpart(struct udevice *bdev, int hwpart)
273 {
274         struct udevice *mmc_dev = dev_get_parent(bdev);
275         struct mmc *mmc = mmc_get_mmc_dev(mmc_dev);
276         struct blk_desc *desc = dev_get_uclass_platdata(bdev);
277
278         if (desc->hwpart == hwpart)
279                 return 0;
280
281         if (mmc->part_config == MMCPART_NOAVAILABLE)
282                 return -EMEDIUMTYPE;
283
284         return mmc_switch_part(mmc, hwpart);
285 }
286
287 static int mmc_blk_probe(struct udevice *dev)
288 {
289         struct udevice *mmc_dev = dev_get_parent(dev);
290         struct mmc_uclass_priv *upriv = dev_get_uclass_priv(mmc_dev);
291         struct mmc *mmc = upriv->mmc;
292         int ret;
293
294         ret = mmc_init(mmc);
295         if (ret) {
296                 debug("%s: mmc_init() failed (err=%d)\n", __func__, ret);
297                 return ret;
298         }
299
300         return 0;
301 }
302
303 static const struct blk_ops mmc_blk_ops = {
304         .read   = mmc_bread,
305 #ifndef CONFIG_SPL_BUILD
306         .write  = mmc_bwrite,
307         .erase  = mmc_berase,
308 #endif
309         .select_hwpart  = mmc_select_hwpart,
310 };
311
312 U_BOOT_DRIVER(mmc_blk) = {
313         .name           = "mmc_blk",
314         .id             = UCLASS_BLK,
315         .ops            = &mmc_blk_ops,
316         .probe          = mmc_blk_probe,
317 };
318 #endif /* CONFIG_BLK */
319
320 U_BOOT_DRIVER(mmc) = {
321         .name   = "mmc",
322         .id     = UCLASS_MMC,
323 };
324
325 UCLASS_DRIVER(mmc) = {
326         .id             = UCLASS_MMC,
327         .name           = "mmc",
328         .flags          = DM_UC_FLAG_SEQ_ALIAS,
329         .per_device_auto_alloc_size = sizeof(struct mmc_uclass_priv),
330 };