blob: e2f4f4eb5c7af5760fbfa0b5ac2bf79b74ae1069 [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
7#include <common.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
31static int do_mmcsetn(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
32{
33 int dev;
34
Masahiro Yamada85a8f212020-02-13 12:27:37 +090035 dev = find_first_mmc_device(false);
Masahiro Yamadab692c3f2017-02-14 01:24:24 +090036 if (dev < 0)
37 return CMD_RET_FAILURE;
38
Simon Glass4d949a22017-08-03 12:22:10 -060039 env_set_ulong("mmc_first_dev", dev);
Masahiro Yamadab692c3f2017-02-14 01:24:24 +090040 return CMD_RET_SUCCESS;
41}
42
43U_BOOT_CMD(
44 mmcsetn, 1, 1, do_mmcsetn,
45 "Set the first MMC (not SD) dev number to \"mmc_first_dev\" environment",
46 ""
47);
Masahiro Yamada85a8f212020-02-13 12:27:37 +090048
49static int do_sdsetn(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
50{
51 int dev;
52
53 dev = find_first_mmc_device(true);
54 if (dev < 0)
55 return CMD_RET_FAILURE;
56
57 env_set_ulong("sd_first_dev", dev);
58 return CMD_RET_SUCCESS;
59}
60
61U_BOOT_CMD(
62 sdsetn, 1, 1, do_sdsetn,
63 "Set the first SD dev number to \"sd_first_dev\" environment",
64 ""
65);