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