boot_mode.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2016 Rockchip Electronics Co., Ltd
  4. */
  5. #include <common.h>
  6. #include <adc.h>
  7. #include <command.h>
  8. #include <env.h>
  9. #include <asm/io.h>
  10. #include <asm/arch-rockchip/boot_mode.h>
  11. #include <dm/device.h>
  12. #include <dm/uclass.h>
  13. #if (CONFIG_ROCKCHIP_BOOT_MODE_REG == 0)
  14. int setup_boot_mode(void)
  15. {
  16. return 0;
  17. }
  18. #else
  19. void set_back_to_bootrom_dnl_flag(void)
  20. {
  21. writel(BOOT_BROM_DOWNLOAD, CONFIG_ROCKCHIP_BOOT_MODE_REG);
  22. }
  23. /*
  24. * detect download key status by adc, most rockchip
  25. * based boards use adc sample the download key status,
  26. * but there are also some use gpio. So it's better to
  27. * make this a weak function that can be override by
  28. * some special boards.
  29. */
  30. #define KEY_DOWN_MIN_VAL 0
  31. #define KEY_DOWN_MAX_VAL 30
  32. __weak int rockchip_dnl_key_pressed(void)
  33. {
  34. unsigned int val;
  35. struct udevice *dev;
  36. struct uclass *uc;
  37. int ret;
  38. ret = uclass_get(UCLASS_ADC, &uc);
  39. if (ret)
  40. return false;
  41. ret = -ENODEV;
  42. uclass_foreach_dev(dev, uc) {
  43. if (!strncmp(dev->name, "saradc", 6)) {
  44. ret = adc_channel_single_shot(dev->name, 1, &val);
  45. break;
  46. }
  47. }
  48. if (ret == -ENODEV) {
  49. pr_warn("%s: no saradc device found\n", __func__);
  50. return false;
  51. } else if (ret) {
  52. pr_err("%s: adc_channel_single_shot fail!\n", __func__);
  53. return false;
  54. }
  55. if ((val >= KEY_DOWN_MIN_VAL) && (val <= KEY_DOWN_MAX_VAL))
  56. return true;
  57. else
  58. return false;
  59. }
  60. void rockchip_dnl_mode_check(void)
  61. {
  62. if (rockchip_dnl_key_pressed()) {
  63. printf("download key pressed, entering download mode...");
  64. set_back_to_bootrom_dnl_flag();
  65. do_reset(NULL, 0, 0, NULL);
  66. }
  67. }
  68. int setup_boot_mode(void)
  69. {
  70. void *reg = (void *)CONFIG_ROCKCHIP_BOOT_MODE_REG;
  71. int boot_mode = readl(reg);
  72. rockchip_dnl_mode_check();
  73. boot_mode = readl(reg);
  74. debug("%s: boot mode 0x%08x\n", __func__, boot_mode);
  75. /* Clear boot mode */
  76. writel(BOOT_NORMAL, reg);
  77. switch (boot_mode) {
  78. case BOOT_FASTBOOT:
  79. debug("%s: enter fastboot!\n", __func__);
  80. env_set("preboot", "setenv preboot; fastboot usb0");
  81. break;
  82. case BOOT_UMS:
  83. debug("%s: enter UMS!\n", __func__);
  84. env_set("preboot", "setenv preboot; ums mmc 0");
  85. break;
  86. }
  87. return 0;
  88. }
  89. #endif