]> Git Repo - J-u-boot.git/blob - drivers/mmc/mmc-uclass.c
mmc: Imply HS200 cap with mmc-hs400 prop to match linux
[J-u-boot.git] / drivers / mmc / mmc-uclass.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2015 Google, Inc
4  * Copyright 2020 NXP
5  * Written by Simon Glass <[email protected]>
6  */
7
8 #define LOG_CATEGORY UCLASS_MMC
9
10 #include <common.h>
11 #include <bootdev.h>
12 #include <log.h>
13 #include <mmc.h>
14 #include <dm.h>
15 #include <dm/device-internal.h>
16 #include <dm/device_compat.h>
17 #include <dm/lists.h>
18 #include <linux/compat.h>
19 #include "mmc_private.h"
20
21 static int dm_mmc_get_b_max(struct udevice *dev, void *dst, lbaint_t blkcnt)
22 {
23         struct dm_mmc_ops *ops = mmc_get_ops(dev);
24         struct mmc *mmc = mmc_get_mmc_dev(dev);
25
26         if (ops->get_b_max)
27                 return ops->get_b_max(dev, dst, blkcnt);
28         else
29                 return mmc->cfg->b_max;
30 }
31
32 int mmc_get_b_max(struct mmc *mmc, void *dst, lbaint_t blkcnt)
33 {
34         return dm_mmc_get_b_max(mmc->dev, dst, blkcnt);
35 }
36
37 static int dm_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
38                     struct mmc_data *data)
39 {
40         struct mmc *mmc = mmc_get_mmc_dev(dev);
41         struct dm_mmc_ops *ops = mmc_get_ops(dev);
42         int ret;
43
44         mmmc_trace_before_send(mmc, cmd);
45         if (ops->send_cmd)
46                 ret = ops->send_cmd(dev, cmd, data);
47         else
48                 ret = -ENOSYS;
49         mmmc_trace_after_send(mmc, cmd, ret);
50
51         return ret;
52 }
53
54 int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
55 {
56         return dm_mmc_send_cmd(mmc->dev, cmd, data);
57 }
58
59 static int dm_mmc_set_ios(struct udevice *dev)
60 {
61         struct dm_mmc_ops *ops = mmc_get_ops(dev);
62
63         if (!ops->set_ios)
64                 return -ENOSYS;
65         return ops->set_ios(dev);
66 }
67
68 int mmc_set_ios(struct mmc *mmc)
69 {
70         return dm_mmc_set_ios(mmc->dev);
71 }
72
73 static int dm_mmc_wait_dat0(struct udevice *dev, int state, int timeout_us)
74 {
75         struct dm_mmc_ops *ops = mmc_get_ops(dev);
76
77         if (!ops->wait_dat0)
78                 return -ENOSYS;
79         return ops->wait_dat0(dev, state, timeout_us);
80 }
81
82 int mmc_wait_dat0(struct mmc *mmc, int state, int timeout_us)
83 {
84         return dm_mmc_wait_dat0(mmc->dev, state, timeout_us);
85 }
86
87 static int dm_mmc_get_wp(struct udevice *dev)
88 {
89         struct dm_mmc_ops *ops = mmc_get_ops(dev);
90
91         if (!ops->get_wp)
92                 return -ENOSYS;
93         return ops->get_wp(dev);
94 }
95
96 int mmc_getwp(struct mmc *mmc)
97 {
98         return dm_mmc_get_wp(mmc->dev);
99 }
100
101 static int dm_mmc_get_cd(struct udevice *dev)
102 {
103         struct dm_mmc_ops *ops = mmc_get_ops(dev);
104
105         if (!ops->get_cd)
106                 return -ENOSYS;
107         return ops->get_cd(dev);
108 }
109
110 int mmc_getcd(struct mmc *mmc)
111 {
112         return dm_mmc_get_cd(mmc->dev);
113 }
114
115 #ifdef MMC_SUPPORTS_TUNING
116 static int dm_mmc_execute_tuning(struct udevice *dev, uint opcode)
117 {
118         struct dm_mmc_ops *ops = mmc_get_ops(dev);
119
120         if (!ops->execute_tuning)
121                 return -ENOSYS;
122         return ops->execute_tuning(dev, opcode);
123 }
124
125 int mmc_execute_tuning(struct mmc *mmc, uint opcode)
126 {
127         int ret;
128
129         mmc->tuning = true;
130         ret = dm_mmc_execute_tuning(mmc->dev, opcode);
131         mmc->tuning = false;
132
133         return ret;
134 }
135 #endif
136
137 #if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
138 static int dm_mmc_set_enhanced_strobe(struct udevice *dev)
139 {
140         struct dm_mmc_ops *ops = mmc_get_ops(dev);
141
142         if (ops->set_enhanced_strobe)
143                 return ops->set_enhanced_strobe(dev);
144
145         return -ENOTSUPP;
146 }
147
148 int mmc_set_enhanced_strobe(struct mmc *mmc)
149 {
150         return dm_mmc_set_enhanced_strobe(mmc->dev);
151 }
152 #endif
153
154 static int dm_mmc_hs400_prepare_ddr(struct udevice *dev)
155 {
156         struct dm_mmc_ops *ops = mmc_get_ops(dev);
157
158         if (ops->hs400_prepare_ddr)
159                 return ops->hs400_prepare_ddr(dev);
160
161         return 0;
162 }
163
164 int mmc_hs400_prepare_ddr(struct mmc *mmc)
165 {
166         return dm_mmc_hs400_prepare_ddr(mmc->dev);
167 }
168
169 static int dm_mmc_host_power_cycle(struct udevice *dev)
170 {
171         struct dm_mmc_ops *ops = mmc_get_ops(dev);
172
173         if (ops->host_power_cycle)
174                 return ops->host_power_cycle(dev);
175         return 0;
176 }
177
178 int mmc_host_power_cycle(struct mmc *mmc)
179 {
180         return dm_mmc_host_power_cycle(mmc->dev);
181 }
182
183 static int dm_mmc_deferred_probe(struct udevice *dev)
184 {
185         struct dm_mmc_ops *ops = mmc_get_ops(dev);
186
187         if (ops->deferred_probe)
188                 return ops->deferred_probe(dev);
189
190         return 0;
191 }
192
193 int mmc_deferred_probe(struct mmc *mmc)
194 {
195         return dm_mmc_deferred_probe(mmc->dev);
196 }
197
198 static int dm_mmc_reinit(struct udevice *dev)
199 {
200         struct dm_mmc_ops *ops = mmc_get_ops(dev);
201
202         if (ops->reinit)
203                 return ops->reinit(dev);
204
205         return 0;
206 }
207
208 int mmc_reinit(struct mmc *mmc)
209 {
210         return dm_mmc_reinit(mmc->dev);
211 }
212
213 int mmc_of_parse(struct udevice *dev, struct mmc_config *cfg)
214 {
215         int val;
216
217         val = dev_read_u32_default(dev, "bus-width", 1);
218
219         switch (val) {
220         case 0x8:
221                 cfg->host_caps |= MMC_MODE_8BIT;
222                 /* fall through */
223         case 0x4:
224                 cfg->host_caps |= MMC_MODE_4BIT;
225                 /* fall through */
226         case 0x1:
227                 cfg->host_caps |= MMC_MODE_1BIT;
228                 break;
229         default:
230                 dev_err(dev, "Invalid \"bus-width\" value %u!\n", val);
231                 return -EINVAL;
232         }
233
234         /* f_max is obtained from the optional "max-frequency" property */
235         dev_read_u32(dev, "max-frequency", &cfg->f_max);
236
237         if (dev_read_bool(dev, "cap-sd-highspeed"))
238                 cfg->host_caps |= MMC_CAP(SD_HS);
239         if (dev_read_bool(dev, "cap-mmc-highspeed"))
240                 cfg->host_caps |= MMC_CAP(MMC_HS) | MMC_CAP(MMC_HS_52);
241         if (dev_read_bool(dev, "sd-uhs-sdr12"))
242                 cfg->host_caps |= MMC_CAP(UHS_SDR12);
243         if (dev_read_bool(dev, "sd-uhs-sdr25"))
244                 cfg->host_caps |= MMC_CAP(UHS_SDR25);
245         if (dev_read_bool(dev, "sd-uhs-sdr50"))
246                 cfg->host_caps |= MMC_CAP(UHS_SDR50);
247         if (dev_read_bool(dev, "sd-uhs-sdr104"))
248                 cfg->host_caps |= MMC_CAP(UHS_SDR104);
249         if (dev_read_bool(dev, "sd-uhs-ddr50"))
250                 cfg->host_caps |= MMC_CAP(UHS_DDR50);
251         if (dev_read_bool(dev, "mmc-ddr-1_8v"))
252                 cfg->host_caps |= MMC_CAP(MMC_DDR_52);
253         if (dev_read_bool(dev, "mmc-ddr-1_2v"))
254                 cfg->host_caps |= MMC_CAP(MMC_DDR_52);
255         if (dev_read_bool(dev, "mmc-hs200-1_8v"))
256                 cfg->host_caps |= MMC_CAP(MMC_HS_200);
257         if (dev_read_bool(dev, "mmc-hs200-1_2v"))
258                 cfg->host_caps |= MMC_CAP(MMC_HS_200);
259         if (dev_read_bool(dev, "mmc-hs400-1_8v"))
260                 cfg->host_caps |= MMC_CAP(MMC_HS_400) | MMC_CAP(MMC_HS_200);
261         if (dev_read_bool(dev, "mmc-hs400-1_2v"))
262                 cfg->host_caps |= MMC_CAP(MMC_HS_400) | MMC_CAP(MMC_HS_200);
263         if (dev_read_bool(dev, "mmc-hs400-enhanced-strobe"))
264                 cfg->host_caps |= MMC_CAP(MMC_HS_400_ES);
265
266         if (dev_read_bool(dev, "non-removable")) {
267                 cfg->host_caps |= MMC_CAP_NONREMOVABLE;
268         } else {
269                 if (dev_read_bool(dev, "cd-inverted"))
270                         cfg->host_caps |= MMC_CAP_CD_ACTIVE_HIGH;
271                 if (dev_read_bool(dev, "broken-cd"))
272                         cfg->host_caps |= MMC_CAP_NEEDS_POLL;
273         }
274
275         if (dev_read_bool(dev, "no-1-8-v")) {
276                 cfg->host_caps &= ~(UHS_CAPS | MMC_MODE_HS200 |
277                                     MMC_MODE_HS400 | MMC_MODE_HS400_ES);
278         }
279
280         return 0;
281 }
282
283 struct mmc *mmc_get_mmc_dev(const struct udevice *dev)
284 {
285         struct mmc_uclass_priv *upriv;
286
287         if (!device_active(dev))
288                 return NULL;
289         upriv = dev_get_uclass_priv(dev);
290         return upriv->mmc;
291 }
292
293 #if CONFIG_IS_ENABLED(BLK)
294 struct mmc *find_mmc_device(int dev_num)
295 {
296         struct udevice *dev, *mmc_dev;
297         int ret;
298
299         ret = blk_find_device(UCLASS_MMC, dev_num, &dev);
300
301         if (ret) {
302 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
303                 printf("MMC Device %d not found\n", dev_num);
304 #endif
305                 return NULL;
306         }
307
308         mmc_dev = dev_get_parent(dev);
309
310         struct mmc *mmc = mmc_get_mmc_dev(mmc_dev);
311
312         return mmc;
313 }
314
315 int get_mmc_num(void)
316 {
317         return max((blk_find_max_devnum(UCLASS_MMC) + 1), 0);
318 }
319
320 int mmc_get_next_devnum(void)
321 {
322         return blk_find_max_devnum(UCLASS_MMC);
323 }
324
325 int mmc_get_blk(struct udevice *dev, struct udevice **blkp)
326 {
327         struct udevice *blk;
328         int ret;
329
330         device_find_first_child_by_uclass(dev, UCLASS_BLK, &blk);
331         ret = device_probe(blk);
332         if (ret)
333                 return ret;
334         *blkp = blk;
335
336         return 0;
337 }
338
339 struct blk_desc *mmc_get_blk_desc(struct mmc *mmc)
340 {
341         struct blk_desc *desc;
342         struct udevice *dev;
343
344         device_find_first_child_by_uclass(mmc->dev, UCLASS_BLK, &dev);
345         if (!dev)
346                 return NULL;
347         desc = dev_get_uclass_plat(dev);
348
349         return desc;
350 }
351
352 void mmc_do_preinit(void)
353 {
354         struct udevice *dev;
355         struct uclass *uc;
356         int ret;
357
358         ret = uclass_get(UCLASS_MMC, &uc);
359         if (ret)
360                 return;
361         uclass_foreach_dev(dev, uc) {
362                 struct mmc *m = mmc_get_mmc_dev(dev);
363
364                 if (!m)
365                         continue;
366
367                 m->user_speed_mode = MMC_MODES_END;  /* Initialising user set speed mode */
368
369                 if (m->preinit)
370                         mmc_start_init(m);
371         }
372 }
373
374 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
375 void print_mmc_devices(char separator)
376 {
377         struct udevice *dev;
378         char *mmc_type;
379         bool first = true;
380
381         for (uclass_first_device(UCLASS_MMC, &dev);
382              dev;
383              uclass_next_device(&dev), first = false) {
384                 struct mmc *m = mmc_get_mmc_dev(dev);
385
386                 if (!first) {
387                         printf("%c", separator);
388                         if (separator != '\n')
389                                 puts(" ");
390                 }
391                 if (m->has_init)
392                         mmc_type = IS_SD(m) ? "SD" : "eMMC";
393                 else
394                         mmc_type = NULL;
395
396                 printf("%s: %d", m->cfg->name, mmc_get_blk_desc(m)->devnum);
397                 if (mmc_type)
398                         printf(" (%s)", mmc_type);
399         }
400
401         printf("\n");
402 }
403
404 #else
405 void print_mmc_devices(char separator) { }
406 #endif
407
408 int mmc_bind(struct udevice *dev, struct mmc *mmc, const struct mmc_config *cfg)
409 {
410         struct blk_desc *bdesc;
411         struct udevice *bdev;
412         int ret;
413
414         if (!mmc_get_ops(dev))
415                 return -ENOSYS;
416
417         /* Use the fixed index with aliases node's index */
418         debug("%s: alias devnum=%d\n", __func__, dev_seq(dev));
419
420         ret = blk_create_devicef(dev, "mmc_blk", "blk", UCLASS_MMC,
421                                  dev_seq(dev), DEFAULT_BLKSZ, 0, &bdev);
422         if (ret) {
423                 debug("Cannot create block device\n");
424                 return ret;
425         }
426         bdesc = dev_get_uclass_plat(bdev);
427         mmc->cfg = cfg;
428         mmc->priv = dev;
429
430         ret = bootdev_setup_for_sibling_blk(bdev, "mmc_bootdev");
431         if (ret)
432                 return log_msg_ret("bootdev", ret);
433
434         /* the following chunk was from mmc_register() */
435
436         /* Setup dsr related values */
437         mmc->dsr_imp = 0;
438         mmc->dsr = 0xffffffff;
439         /* Setup the universal parts of the block interface just once */
440         bdesc->removable = 1;
441
442         /* setup initial part type */
443         bdesc->part_type = cfg->part_type;
444         mmc->dev = dev;
445         mmc->user_speed_mode = MMC_MODES_END;
446         return 0;
447 }
448
449 int mmc_unbind(struct udevice *dev)
450 {
451         struct udevice *bdev;
452         int ret;
453
454         device_find_first_child_by_uclass(dev, UCLASS_BLK, &bdev);
455         if (bdev) {
456                 device_remove(bdev, DM_REMOVE_NORMAL);
457                 device_unbind(bdev);
458         }
459         ret = bootdev_unbind_dev(dev);
460         if (ret)
461                 return log_msg_ret("bootdev", ret);
462
463         return 0;
464 }
465
466 static int mmc_select_hwpart(struct udevice *bdev, int hwpart)
467 {
468         struct udevice *mmc_dev = dev_get_parent(bdev);
469         struct mmc *mmc = mmc_get_mmc_dev(mmc_dev);
470         struct blk_desc *desc = dev_get_uclass_plat(bdev);
471         int ret;
472
473         if (desc->hwpart == hwpart)
474                 return 0;
475
476         if (mmc->part_config == MMCPART_NOAVAILABLE)
477                 return -EMEDIUMTYPE;
478
479         ret = mmc_switch_part(mmc, hwpart);
480         if (!ret)
481                 blkcache_invalidate(desc->uclass_id, desc->devnum);
482
483         return ret;
484 }
485
486 static int mmc_blk_probe(struct udevice *dev)
487 {
488         struct udevice *mmc_dev = dev_get_parent(dev);
489         struct mmc_uclass_priv *upriv = dev_get_uclass_priv(mmc_dev);
490         struct mmc *mmc = upriv->mmc;
491         int ret;
492
493         ret = mmc_init(mmc);
494         if (ret) {
495                 debug("%s: mmc_init() failed (err=%d)\n", __func__, ret);
496                 return ret;
497         }
498
499         ret = device_probe(dev);
500         if (ret) {
501                 debug("Probing %s failed (err=%d)\n", dev->name, ret);
502
503                 mmc_deinit(mmc);
504
505                 return ret;
506         }
507
508         return 0;
509 }
510
511 static int mmc_blk_remove(struct udevice *dev)
512 {
513         struct udevice *mmc_dev = dev_get_parent(dev);
514         struct mmc_uclass_priv *upriv = dev_get_uclass_priv(mmc_dev);
515         struct mmc *mmc = upriv->mmc;
516
517         return mmc_deinit(mmc);
518 }
519
520 static const struct blk_ops mmc_blk_ops = {
521         .read   = mmc_bread,
522 #if CONFIG_IS_ENABLED(MMC_WRITE)
523         .write  = mmc_bwrite,
524         .erase  = mmc_berase,
525 #endif
526         .select_hwpart  = mmc_select_hwpart,
527 };
528
529 U_BOOT_DRIVER(mmc_blk) = {
530         .name           = "mmc_blk",
531         .id             = UCLASS_BLK,
532         .ops            = &mmc_blk_ops,
533         .probe          = mmc_blk_probe,
534         .remove         = mmc_blk_remove,
535         .flags          = DM_FLAG_OS_PREPARE,
536 };
537 #endif /* CONFIG_BLK */
538
539
540 UCLASS_DRIVER(mmc) = {
541         .id             = UCLASS_MMC,
542         .name           = "mmc",
543         .flags          = DM_UC_FLAG_SEQ_ALIAS,
544         .per_device_auto        = sizeof(struct mmc_uclass_priv),
545 };
This page took 0.057173 seconds and 4 git commands to generate.