blob: 45bb10f8eb1f9c85a28fe40bd70d08960a1fbe01 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Masahiro Yamadab692c3f2017-02-14 01:24:24 +09002/*
3 * Copyright (C) 2016 Socionext Inc.
4 * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
Masahiro Yamadab692c3f2017-02-14 01:24:24 +09005 */
6
Simon Glassed38aef2020-05-10 11:40:03 -06007#include <command.h>
Simon Glass07dc93c2019-08-01 09:46:47 -06008#include <env.h>
Masahiro Yamadab692c3f2017-02-14 01:24:24 +09009#include <mmc.h>
10#include <linux/errno.h>
11
Masahiro Yamada85a8f212020-02-13 12:27:37 +090012static int find_first_mmc_device(bool is_sd)
Masahiro Yamadab692c3f2017-02-14 01:24:24 +090013{
14 struct mmc *mmc;
15 int i;
16
17 for (i = 0; (mmc = find_mmc_device(i)); i++) {
Masahiro Yamada85a8f212020-02-13 12:27:37 +090018 if (!mmc_init(mmc) &&
19 ((is_sd && IS_SD(mmc)) || (!is_sd && IS_MMC(mmc))))
Masahiro Yamadab692c3f2017-02-14 01:24:24 +090020 return i;
21 }
22
23 return -ENODEV;
24}
25
26int mmc_get_env_dev(void)
27{
Masahiro Yamada85a8f212020-02-13 12:27:37 +090028 return find_first_mmc_device(false);
Masahiro Yamadab692c3f2017-02-14 01:24:24 +090029}
30
Simon Glassed38aef2020-05-10 11:40:03 -060031static int do_mmcsetn(struct cmd_tbl *cmdtp, int flag, int argc,
32 char *const argv[])
Masahiro Yamadab692c3f2017-02-14 01:24:24 +090033{
34 int dev;
35
Masahiro Yamada85a8f212020-02-13 12:27:37 +090036 dev = find_first_mmc_device(false);
Masahiro Yamadab692c3f2017-02-14 01:24:24 +090037 if (dev < 0)
38 return CMD_RET_FAILURE;
39
Simon Glass4d949a22017-08-03 12:22:10 -060040 env_set_ulong("mmc_first_dev", dev);
Masahiro Yamadab692c3f2017-02-14 01:24:24 +090041 return CMD_RET_SUCCESS;
42}
43
44U_BOOT_CMD(
45 mmcsetn, 1, 1, do_mmcsetn,
46 "Set the first MMC (not SD) dev number to \"mmc_first_dev\" environment",
47 ""
48);
Masahiro Yamada85a8f212020-02-13 12:27:37 +090049
Simon Glassed38aef2020-05-10 11:40:03 -060050static int do_sdsetn(struct cmd_tbl *cmdtp, int flag, int argc,
51 char *const argv[])
Masahiro Yamada85a8f212020-02-13 12:27:37 +090052{
53 int dev;
54
55 dev = find_first_mmc_device(true);
56 if (dev < 0)
57 return CMD_RET_FAILURE;
58
59 env_set_ulong("sd_first_dev", dev);
60 return CMD_RET_SUCCESS;
61}
62
63U_BOOT_CMD(
64 sdsetn, 1, 1, do_sdsetn,
65 "Set the first SD dev number to \"sd_first_dev\" environment",
66 ""
67);