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