blob: 7f29b6bccd4643a9e50b2380454c2a6843787a01 [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 Glassed38aef2020-05-10 11:40:03 -06008#include <command.h>
Simon Glass07dc93c2019-08-01 09:46:47 -06009#include <env.h>
Masahiro Yamadab692c3f2017-02-14 01:24:24 +090010#include <mmc.h>
11#include <linux/errno.h>
12
Masahiro Yamada85a8f212020-02-13 12:27:37 +090013static int find_first_mmc_device(bool is_sd)
Masahiro Yamadab692c3f2017-02-14 01:24:24 +090014{
15 struct mmc *mmc;
16 int i;
17
18 for (i = 0; (mmc = find_mmc_device(i)); i++) {
Masahiro Yamada85a8f212020-02-13 12:27:37 +090019 if (!mmc_init(mmc) &&
20 ((is_sd && IS_SD(mmc)) || (!is_sd && IS_MMC(mmc))))
Masahiro Yamadab692c3f2017-02-14 01:24:24 +090021 return i;
22 }
23
24 return -ENODEV;
25}
26
27int mmc_get_env_dev(void)
28{
Masahiro Yamada85a8f212020-02-13 12:27:37 +090029 return find_first_mmc_device(false);
Masahiro Yamadab692c3f2017-02-14 01:24:24 +090030}
31
Simon Glassed38aef2020-05-10 11:40:03 -060032static int do_mmcsetn(struct cmd_tbl *cmdtp, int flag, int argc,
33 char *const argv[])
Masahiro Yamadab692c3f2017-02-14 01:24:24 +090034{
35 int dev;
36
Masahiro Yamada85a8f212020-02-13 12:27:37 +090037 dev = find_first_mmc_device(false);
Masahiro Yamadab692c3f2017-02-14 01:24:24 +090038 if (dev < 0)
39 return CMD_RET_FAILURE;
40
Simon Glass4d949a22017-08-03 12:22:10 -060041 env_set_ulong("mmc_first_dev", dev);
Masahiro Yamadab692c3f2017-02-14 01:24:24 +090042 return CMD_RET_SUCCESS;
43}
44
45U_BOOT_CMD(
46 mmcsetn, 1, 1, do_mmcsetn,
47 "Set the first MMC (not SD) dev number to \"mmc_first_dev\" environment",
48 ""
49);
Masahiro Yamada85a8f212020-02-13 12:27:37 +090050
Simon Glassed38aef2020-05-10 11:40:03 -060051static int do_sdsetn(struct cmd_tbl *cmdtp, int flag, int argc,
52 char *const argv[])
Masahiro Yamada85a8f212020-02-13 12:27:37 +090053{
54 int dev;
55
56 dev = find_first_mmc_device(true);
57 if (dev < 0)
58 return CMD_RET_FAILURE;
59
60 env_set_ulong("sd_first_dev", dev);
61 return CMD_RET_SUCCESS;
62}
63
64U_BOOT_CMD(
65 sdsetn, 1, 1, do_sdsetn,
66 "Set the first SD dev number to \"sd_first_dev\" environment",
67 ""
68);