Merge tag 'u-boot-atmel-fixes-2020.07-a' of https://gitlab.denx.de/u-boot/custodians...
[oweals/u-boot.git] / arch / arm / mach-uniphier / mmc-first-dev.c
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 <command.h>
8 #include <env.h>
9 #include <mmc.h>
10 #include <linux/errno.h>
11
12 static int find_first_mmc_device(bool is_sd)
13 {
14         struct mmc *mmc;
15         int i;
16
17         for (i = 0; (mmc = find_mmc_device(i)); i++) {
18                 if (!mmc_init(mmc) &&
19                     ((is_sd && IS_SD(mmc)) || (!is_sd && IS_MMC(mmc))))
20                         return i;
21         }
22
23         return -ENODEV;
24 }
25
26 int mmc_get_env_dev(void)
27 {
28         return find_first_mmc_device(false);
29 }
30
31 static int do_mmcsetn(struct cmd_tbl *cmdtp, int flag, int argc,
32                       char *const argv[])
33 {
34         int dev;
35
36         dev = find_first_mmc_device(false);
37         if (dev < 0)
38                 return CMD_RET_FAILURE;
39
40         env_set_ulong("mmc_first_dev", dev);
41         return CMD_RET_SUCCESS;
42 }
43
44 U_BOOT_CMD(
45            mmcsetn,     1,      1,      do_mmcsetn,
46         "Set the first MMC (not SD) dev number to \"mmc_first_dev\" environment",
47         ""
48 );
49
50 static int do_sdsetn(struct cmd_tbl *cmdtp, int flag, int argc,
51                      char *const argv[])
52 {
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
63 U_BOOT_CMD(
64         sdsetn, 1,      1,      do_sdsetn,
65         "Set the first SD dev number to \"sd_first_dev\" environment",
66         ""
67 );