1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2016 Socionext Inc.
4  *   Author: Masahiro Yamada <yamada.masahiro@socionext.com>
5  */
6 
7 #include <common.h>
8 #include <mmc.h>
9 #include <linux/errno.h>
10 
11 static int find_first_mmc_device(void)
12 {
13 	struct mmc *mmc;
14 	int i;
15 
16 	for (i = 0; (mmc = find_mmc_device(i)); i++) {
17 		if (!mmc_init(mmc) && IS_MMC(mmc))
18 			return i;
19 	}
20 
21 	return -ENODEV;
22 }
23 
24 int mmc_get_env_dev(void)
25 {
26 	return find_first_mmc_device();
27 }
28 
29 static int do_mmcsetn(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
30 {
31 	int dev;
32 
33 	dev = find_first_mmc_device();
34 	if (dev < 0)
35 		return CMD_RET_FAILURE;
36 
37 	env_set_ulong("mmc_first_dev", dev);
38 	return CMD_RET_SUCCESS;
39 }
40 
41 U_BOOT_CMD(
42 	   mmcsetn,	1,	1,	do_mmcsetn,
43 	"Set the first MMC (not SD) dev number to \"mmc_first_dev\" environment",
44 	""
45 );
46