boot_mode.c 2.1 KB

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