Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Masahiro Yamada | b692c3f | 2017-02-14 01:24:24 +0900 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2016 Socionext Inc. |
| 4 | * Author: Masahiro Yamada <yamada.masahiro@socionext.com> |
Masahiro Yamada | b692c3f | 2017-02-14 01:24:24 +0900 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Simon Glass | 07dc93c | 2019-08-01 09:46:47 -0600 | [diff] [blame] | 8 | #include <env.h> |
Masahiro Yamada | b692c3f | 2017-02-14 01:24:24 +0900 | [diff] [blame] | 9 | #include <mmc.h> |
| 10 | #include <linux/errno.h> |
| 11 | |
Masahiro Yamada | 85a8f21 | 2020-02-13 12:27:37 +0900 | [diff] [blame] | 12 | static int find_first_mmc_device(bool is_sd) |
Masahiro Yamada | b692c3f | 2017-02-14 01:24:24 +0900 | [diff] [blame] | 13 | { |
| 14 | struct mmc *mmc; |
| 15 | int i; |
| 16 | |
| 17 | for (i = 0; (mmc = find_mmc_device(i)); i++) { |
Masahiro Yamada | 85a8f21 | 2020-02-13 12:27:37 +0900 | [diff] [blame] | 18 | if (!mmc_init(mmc) && |
| 19 | ((is_sd && IS_SD(mmc)) || (!is_sd && IS_MMC(mmc)))) |
Masahiro Yamada | b692c3f | 2017-02-14 01:24:24 +0900 | [diff] [blame] | 20 | return i; |
| 21 | } |
| 22 | |
| 23 | return -ENODEV; |
| 24 | } |
| 25 | |
| 26 | int mmc_get_env_dev(void) |
| 27 | { |
Masahiro Yamada | 85a8f21 | 2020-02-13 12:27:37 +0900 | [diff] [blame] | 28 | return find_first_mmc_device(false); |
Masahiro Yamada | b692c3f | 2017-02-14 01:24:24 +0900 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | static int do_mmcsetn(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 32 | { |
| 33 | int dev; |
| 34 | |
Masahiro Yamada | 85a8f21 | 2020-02-13 12:27:37 +0900 | [diff] [blame] | 35 | dev = find_first_mmc_device(false); |
Masahiro Yamada | b692c3f | 2017-02-14 01:24:24 +0900 | [diff] [blame] | 36 | if (dev < 0) |
| 37 | return CMD_RET_FAILURE; |
| 38 | |
Simon Glass | 4d949a2 | 2017-08-03 12:22:10 -0600 | [diff] [blame] | 39 | env_set_ulong("mmc_first_dev", dev); |
Masahiro Yamada | b692c3f | 2017-02-14 01:24:24 +0900 | [diff] [blame] | 40 | return CMD_RET_SUCCESS; |
| 41 | } |
| 42 | |
| 43 | U_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 Yamada | 85a8f21 | 2020-02-13 12:27:37 +0900 | [diff] [blame] | 48 | |
| 49 | static 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 | |
| 61 | U_BOOT_CMD( |
| 62 | sdsetn, 1, 1, do_sdsetn, |
| 63 | "Set the first SD dev number to \"sd_first_dev\" environment", |
| 64 | "" |
| 65 | ); |