mmc-first-dev.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2016 Socionext Inc.
  4. * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
  5. */
  6. #include <command.h>
  7. #include <env.h>
  8. #include <mmc.h>
  9. #include <linux/errno.h>
  10. static int find_first_mmc_device(bool is_sd)
  11. {
  12. struct mmc *mmc;
  13. int i;
  14. for (i = 0; (mmc = find_mmc_device(i)); i++) {
  15. if (!mmc_init(mmc) &&
  16. ((is_sd && IS_SD(mmc)) || (!is_sd && IS_MMC(mmc))))
  17. return i;
  18. }
  19. return -ENODEV;
  20. }
  21. int mmc_get_env_dev(void)
  22. {
  23. return find_first_mmc_device(false);
  24. }
  25. static int do_mmcsetn(struct cmd_tbl *cmdtp, int flag, int argc,
  26. char *const argv[])
  27. {
  28. int dev;
  29. dev = find_first_mmc_device(false);
  30. if (dev < 0)
  31. return CMD_RET_FAILURE;
  32. env_set_ulong("mmc_first_dev", dev);
  33. return CMD_RET_SUCCESS;
  34. }
  35. U_BOOT_CMD(
  36. mmcsetn, 1, 1, do_mmcsetn,
  37. "Set the first MMC (not SD) dev number to \"mmc_first_dev\" environment",
  38. ""
  39. );
  40. static int do_sdsetn(struct cmd_tbl *cmdtp, int flag, int argc,
  41. char *const argv[])
  42. {
  43. int dev;
  44. dev = find_first_mmc_device(true);
  45. if (dev < 0)
  46. return CMD_RET_FAILURE;
  47. env_set_ulong("sd_first_dev", dev);
  48. return CMD_RET_SUCCESS;
  49. }
  50. U_BOOT_CMD(
  51. sdsetn, 1, 1, do_sdsetn,
  52. "Set the first SD dev number to \"sd_first_dev\" environment",
  53. ""
  54. );