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 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 7 | #include <command.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 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 31 | static int do_mmcsetn(struct cmd_tbl *cmdtp, int flag, int argc, |
| 32 | char *const argv[]) |
Masahiro Yamada | b692c3f | 2017-02-14 01:24:24 +0900 | [diff] [blame] | 33 | { |
| 34 | int dev; |
| 35 | |
Masahiro Yamada | 85a8f21 | 2020-02-13 12:27:37 +0900 | [diff] [blame] | 36 | dev = find_first_mmc_device(false); |
Masahiro Yamada | b692c3f | 2017-02-14 01:24:24 +0900 | [diff] [blame] | 37 | if (dev < 0) |
| 38 | return CMD_RET_FAILURE; |
| 39 | |
Simon Glass | 4d949a2 | 2017-08-03 12:22:10 -0600 | [diff] [blame] | 40 | env_set_ulong("mmc_first_dev", dev); |
Masahiro Yamada | b692c3f | 2017-02-14 01:24:24 +0900 | [diff] [blame] | 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 | ); |
Masahiro Yamada | 85a8f21 | 2020-02-13 12:27:37 +0900 | [diff] [blame] | 49 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 50 | static int do_sdsetn(struct cmd_tbl *cmdtp, int flag, int argc, |
| 51 | char *const argv[]) |
Masahiro Yamada | 85a8f21 | 2020-02-13 12:27:37 +0900 | [diff] [blame] | 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 | ); |