~funderscore blog cgit wiki get in touch
aboutsummaryrefslogtreecommitdiff
blob: 0e157672eae0b0707ea333145761435b4916ef74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright (C) 2015 Google, Inc
 * Copyright 2020 NXP
 * Written by Simon Glass <sjg@chromium.org>
 */

#define LOG_CATEGORY UCLASS_MMC

#include <common.h>
#include <bootdev.h>
#include <log.h>
#include <mmc.h>
#include <dm.h>
#include <dm/device-internal.h>
#include <dm/device_compat.h>
#include <dm/lists.h>
#include <linux/compat.h>
#include "mmc_private.h"

static int dm_mmc_get_b_max(struct udevice *dev, void *dst, lbaint_t blkcnt)
{
	struct dm_mmc_ops *ops = mmc_get_ops(dev);
	struct mmc *mmc = mmc_get_mmc_dev(dev);

	if (ops->get_b_max)
		return ops->get_b_max(dev, dst, blkcnt);
	else
		return mmc->cfg->b_max;
}

int mmc_get_b_max(struct mmc *mmc, void *dst, lbaint_t blkcnt)
{
	return dm_mmc_get_b_max(mmc->dev, dst, blkcnt);
}

static int dm_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
		    struct mmc_data *data)
{
	struct mmc *mmc = mmc_get_mmc_dev(dev);
	struct dm_mmc_ops *ops = mmc_get_ops(dev);
	int ret;

	mmmc_trace_before_send(mmc, cmd);
	if (ops->send_cmd)
		ret = ops->send_cmd(dev, cmd, data);
	else
		ret = -ENOSYS;
	mmmc_trace_after_send(mmc, cmd, ret);

	return ret;
}

int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
{
	return dm_mmc_send_cmd(mmc->dev, cmd, data);
}

static int dm_mmc_set_ios(struct udevice *dev)
{
	struct dm_mmc_ops *ops = mmc_get_ops(dev);

	if (!ops->set_ios)
		return -ENOSYS;
	return ops->set_ios(dev);
}

int mmc_set_ios(struct mmc *mmc)
{
	return dm_mmc_set_ios(mmc->dev);
}

static int dm_mmc_wait_dat0(struct udevice *dev, int state, int timeout_us)
{
	struct dm_mmc_ops *ops = mmc_get_ops(dev);

	if (!ops->wait_dat0)
		return -ENOSYS;
	return ops->wait_dat0(dev, state, timeout_us);
}

int mmc_wait_dat0(struct mmc *mmc, int state, int timeout_us)
{
	return dm_mmc_wait_dat0(mmc->dev, state, timeout_us);
}

static int dm_mmc_get_wp(struct udevice *dev)
{
	struct dm_mmc_ops *ops = mmc_get_ops(dev);

	if (!ops->get_wp)
		return -ENOSYS;
	return ops->get_wp(dev);
}

int mmc_getwp(struct mmc *mmc)
{
	return dm_mmc_get_wp(mmc->dev);
}

static int dm_mmc_get_cd(struct udevice *dev)
{
	struct dm_mmc_ops *ops = mmc_get_ops(dev);

	if (!ops->get_cd)
		return -ENOSYS;
	return ops->get_cd(dev);
}

int mmc_getcd(struct mmc *mmc)
{
	return dm_mmc_get_cd(mmc->dev);
}

#ifdef MMC_SUPPORTS_TUNING
static int dm_mmc_execute_tuning(struct udevice *dev, uint opcode)
{
	struct dm_mmc_ops *ops = mmc_get_ops(dev);

	if (!ops->execute_tuning)
		return -ENOSYS;
	return ops->execute_tuning(dev, opcode);
}

int mmc_execute_tuning(struct mmc *mmc, uint opcode)
{
	return dm_mmc_execute_tuning(mmc->dev, opcode);
}
#endif

#if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT)
static int dm_mmc_set_enhanced_strobe(struct udevice *dev)
{
	struct dm_mmc_ops *ops = mmc_get_ops(dev);

	if (ops->set_enhanced_strobe)
		return ops->set_enhanced_strobe(dev);

	return -ENOTSUPP;
}

int mmc_set_enhanced_strobe(struct mmc *mmc)
{
	return dm_mmc_set_enhanced_strobe(mmc->dev);
}
#endif

static int dm_mmc_hs400_prepare_ddr(struct udevice *dev)
{
	struct dm_mmc_ops *ops = mmc_get_ops(dev);

	if (ops->hs400_prepare_ddr)
		return ops->hs400_prepare_ddr(dev);

	return 0;
}

int mmc_hs400_prepare_ddr(struct mmc *mmc)
{
	return dm_mmc_hs400_prepare_ddr(mmc->dev);
}

static int dm_mmc_host_power_cycle(struct udevice *dev)
{
	struct dm_mmc_ops *ops = mmc_get_ops(dev);

	if (ops->host_power_cycle)
		return ops->host_power_cycle(dev);
	return 0;
}

int mmc_host_power_cycle(struct mmc *mmc)
{
	return dm_mmc_host_power_cycle(mmc->dev);
}

static int dm_mmc_deferred_probe(struct udevice *dev)
{
	struct dm_mmc_ops *ops = mmc_get_ops(dev);

	if (ops->deferred_probe)
		return ops->deferred_probe(dev);

	return 0;
}

int mmc_deferred_probe(struct mmc *mmc)
{
	return dm_mmc_deferred_probe(mmc->dev);
}

static int dm_mmc_reinit(struct udevice *dev)
{
	struct dm_mmc_ops *ops = mmc_get_ops(dev);

	if (ops->reinit)
		return ops->reinit(dev);

	return 0;
}

int mmc_reinit(struct mmc *mmc)
{
	return dm_mmc_reinit(mmc->dev);
}

int mmc_of_parse(struct udevice *dev, struct mmc_config *cfg)
{
	int val;

	val = dev_read_u32_default(dev, "bus-width", 1);

	switch (val) {
	case 0x8:
		cfg->host_caps |= MMC_MODE_8BIT;
		/* fall through */
	case 0x4:
		cfg->host_caps |= MMC_MODE_4BIT;
		/* fall through */
	case 0x1:
		cfg->host_caps |= MMC_MODE_1BIT;
		break;
	default:
		dev_err(dev, "Invalid \"bus-width\" value %u!\n", val);
		return -EINVAL;
	}

	/* f_max is obtained from the optional "max-frequency" property */
	dev_read_u32(dev, "max-frequency", &cfg->f_max);

	if (dev_read_bool(dev, "cap-sd-highspeed"))
		cfg->host_caps |= MMC_CAP(SD_HS);
	if (dev_read_bool(dev, "cap-mmc-highspeed"))
		cfg->host_caps |= MMC_CAP(MMC_HS) | MMC_CAP(MMC_HS_52);
	if (dev_read_bool(dev, "sd-uhs-sdr12"))
		cfg->host_caps |= MMC_CAP(UHS_SDR12);
	if (dev_read_bool(dev, "sd-uhs-sdr25"))
		cfg->host_caps |= MMC_CAP(UHS_SDR25);
	if (dev_read_bool(dev, "sd-uhs-sdr50"))
		cfg->host_caps |= MMC_CAP(UHS_SDR50);
	if (dev_read_bool(dev, "sd-uhs-sdr104"))
		cfg->host_caps |= MMC_CAP(UHS_SDR104);
	if (dev_read_bool(dev, "sd-uhs-ddr50"))
		cfg->host_caps |= MMC_CAP(UHS_DDR50);
	if (dev_read_bool(dev, "mmc-ddr-1_8v"))
		cfg->host_caps |= MMC_CAP(MMC_DDR_52);
	if (dev_read_bool(dev, "mmc-ddr-1_2v"))
		cfg->host_caps |= MMC_CAP(MMC_DDR_52);
	if (dev_read_bool(dev, "mmc-hs200-1_8v"))
		cfg->host_caps |= MMC_CAP(MMC_HS_200);
	if (dev_read_bool(dev, "mmc-hs200-1_2v"))
		cfg->host_caps |= MMC_CAP(MMC_HS_200);
	if (dev_read_bool(dev, "mmc-hs400-1_8v"))
		cfg->host_caps |= MMC_CAP(MMC_HS_400);
	if (dev_read_bool(dev, "mmc-hs400-1_2v"))
		cfg->host_caps |= MMC_CAP(MMC_HS_400);
	if (dev_read_bool(dev, "mmc-hs400-enhanced-strobe"))
		cfg->host_caps |= MMC_CAP(MMC_HS_400_ES);

	if (dev_read_bool(dev, "non-removable")) {
		cfg->host_caps |= MMC_CAP_NONREMOVABLE;
	} else {
		if (dev_read_bool(dev, "cd-inverted"))
			cfg->host_caps |= MMC_CAP_CD_ACTIVE_HIGH;
		if (dev_read_bool(dev, "broken-cd"))
			cfg->host_caps |= MMC_CAP_NEEDS_POLL;
	}

	if (dev_read_bool(dev, "no-1-8-v")) {
		cfg->host_caps &= ~(UHS_CAPS | MMC_MODE_HS200 |
				    MMC_MODE_HS400 | MMC_MODE_HS400_ES);
	}

	return 0;
}

struct mmc *mmc_get_mmc_dev(const struct udevice *dev)
{
	struct mmc_uclass_priv *upriv;

	if (!device_active(dev))
		return NULL;
	upriv = dev_get_uclass_priv(dev);
	return upriv->mmc;
}

#if CONFIG_IS_ENABLED(BLK)
struct mmc *find_mmc_device(int dev_num)
{
	struct udevice *dev, *mmc_dev;
	int ret;

	ret = blk_find_device(UCLASS_MMC, dev_num, &dev);

	if (ret) {
#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
		printf("MMC Device %d not found\n", dev_num);
#endif
		return NULL;
	}

	mmc_dev = dev_get_parent(dev);

	struct mmc *mmc = mmc_get_mmc_dev(mmc_dev);

	return mmc;
}

int get_mmc_num(void)
{
	return max((blk_find_max_devnum(UCLASS_MMC) + 1), 0);
}

int mmc_get_next_devnum(void)
{
	return blk_find_max_devnum(UCLASS_MMC);
}

int mmc_get_blk(struct udevice *dev, struct udevice **blkp)
{
	struct udevice *blk;
	int ret;

	device_find_first_child_by_uclass(dev, UCLASS_BLK, &blk);
	ret = device_probe(blk);
	if (ret)
		return ret;
	*blkp = blk;

	return 0;
}

struct blk_desc *mmc_get_blk_desc(struct mmc *mmc)
{
	struct blk_desc *desc;
	struct udevice *dev;

	device_find_first_child_by_uclass(mmc->dev, UCLASS_BLK, &dev);
	if (!dev)
		return NULL;
	desc = dev_get_uclass_plat(dev);

	return desc;
}

void mmc_do_preinit(void)
{
	struct udevice *dev;
	struct uclass *uc;
	int ret;

	ret = uclass_get(UCLASS_MMC, &uc);
	if (ret)
		return;
	uclass_foreach_dev(dev, uc) {
		struct mmc *m = mmc_get_mmc_dev(dev);

		if (!m)
			continue;

		m->user_speed_mode = MMC_MODES_END;  /* Initialising user set speed mode */

		if (m->preinit)
			mmc_start_init(m);
	}
}

#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
void print_mmc_devices(char separator)
{
	struct udevice *dev;
	char *mmc_type;
	bool first = true;

	for (uclass_first_device(UCLASS_MMC, &dev);
	     dev;
	     uclass_next_device(&dev), first = false) {
		struct mmc *m = mmc_get_mmc_dev(dev);

		if (!first) {
			printf("%c", separator);
			if (separator != '\n')
				puts(" ");
		}
		if (m->has_init)
			mmc_type = IS_SD(m) ? "SD" : "eMMC";
		else
			mmc_type = NULL;

		printf("%s: %d", m->cfg->name, mmc_get_blk_desc(m)->devnum);
		if (mmc_type)
			printf(" (%s)", mmc_type);
	}

	printf("\n");
}

#else
void print_mmc_devices(char separator) { }
#endif

int mmc_bind(struct udevice *dev, struct mmc *mmc, const struct mmc_config *cfg)
{
	struct blk_desc *bdesc;
	struct udevice *bdev;
	int ret;

	if (!mmc_get_ops(dev))
		return -ENOSYS;

	/* Use the fixed index with aliases node's index */
	debug("%s: alias devnum=%d\n", __func__, dev_seq(dev));

	ret = blk_create_devicef(dev, "mmc_blk", "blk", UCLASS_MMC,
				 dev_seq(dev), 512, 0, &bdev);
	if (ret) {
		debug("Cannot create block device\n");
		return ret;
	}
	bdesc = dev_get_uclass_plat(bdev);
	mmc->cfg = cfg;
	mmc->priv = dev;

	ret = bootdev_setup_for_sibling_blk(bdev, "mmc_bootdev");
	if (ret)
		return log_msg_ret("bootdev", ret);

	/* the following chunk was from mmc_register() */

	/* Setup dsr related values */
	mmc->dsr_imp = 0;
	mmc->dsr = 0xffffffff;
	/* Setup the universal parts of the block interface just once */
	bdesc->removable = 1;

	/* setup initial part type */
	bdesc->part_type = cfg->part_type;
	mmc->dev = dev;
	mmc->user_speed_mode = MMC_MODES_END;
	return 0;
}

int mmc_unbind(struct udevice *dev)
{
	struct udevice *bdev;
	int ret;

	device_find_first_child_by_uclass(dev, UCLASS_BLK, &bdev);
	if (bdev) {
		device_remove(bdev, DM_REMOVE_NORMAL);
		device_unbind(bdev);
	}
	ret = bootdev_unbind_dev(dev);
	if (ret)
		return log_msg_ret("bootdev", ret);

	return 0;
}

static int mmc_select_hwpart(struct udevice *bdev, int hwpart)
{
	struct udevice *mmc_dev = dev_get_parent(bdev);
	struct mmc *mmc = mmc_get_mmc_dev(mmc_dev);
	struct blk_desc *desc = dev_get_uclass_plat(bdev);
	int ret;

	if (desc->hwpart == hwpart)
		return 0;

	if (mmc->part_config == MMCPART_NOAVAILABLE)
		return -EMEDIUMTYPE;

	ret = mmc_switch_part(mmc, hwpart);
	if (!ret)
		blkcache_invalidate(desc->uclass_id, desc->devnum);

	return ret;
}

static int mmc_blk_probe(struct udevice *dev)
{
	struct udevice *mmc_dev = dev_get_parent(dev);
	struct mmc_uclass_priv *upriv = dev_get_uclass_priv(mmc_dev);
	struct mmc *mmc = upriv->mmc;
	int ret;

	ret = mmc_init(mmc);
	if (ret) {
		debug("%s: mmc_init() failed (err=%d)\n", __func__, ret);
		return ret;
	}

	ret = device_probe(dev);
	if (ret) {
		debug("Probing %s failed (err=%d)\n", dev->name, ret);

		if (CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) ||
		    CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) ||
		    CONFIG_IS_ENABLED(MMC_HS400_SUPPORT))
			mmc_deinit(mmc);

		return ret;
	}

	return 0;
}

#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \
    CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
    CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
static int mmc_blk_remove(struct udevice *dev)
{
	struct udevice *mmc_dev = dev_get_parent(dev);
	struct mmc_uclass_priv *upriv = dev_get_uclass_priv(mmc_dev);
	struct mmc *mmc = upriv->mmc;

	return mmc_deinit(mmc);
}
#endif

static const struct blk_ops mmc_blk_ops = {
	.read	= mmc_bread,
#if CONFIG_IS_ENABLED(MMC_WRITE)
	.write	= mmc_bwrite,
	.erase	= mmc_berase,
#endif
	.select_hwpart	= mmc_select_hwpart,
};

U_BOOT_DRIVER(mmc_blk) = {
	.name		= "mmc_blk",
	.id		= UCLASS_BLK,
	.ops		= &mmc_blk_ops,
	.probe		= mmc_blk_probe,
#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \
    CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
    CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
	.remove		= mmc_blk_remove,
	.flags		= DM_FLAG_OS_PREPARE,
#endif
};
#endif /* CONFIG_BLK */


UCLASS_DRIVER(mmc) = {
	.id		= UCLASS_MMC,
	.name		= "mmc",
	.flags		= DM_UC_FLAG_SEQ_ALIAS,
	.per_device_auto	= sizeof(struct mmc_uclass_priv),
};