board_late_init.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2014 Panasonic Corporation
  4. * Copyright (C) 2015-2016 Socionext Inc.
  5. * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
  6. */
  7. #include <env.h>
  8. #include <init.h>
  9. #include <spl.h>
  10. #include <asm/global_data.h>
  11. #include <linux/libfdt.h>
  12. #include <stdio.h>
  13. #include <linux/printk.h>
  14. #include "init.h"
  15. static void uniphier_set_env_fdt_file(void)
  16. {
  17. DECLARE_GLOBAL_DATA_PTR;
  18. const char *compat;
  19. char dtb_name[256];
  20. int buf_len = sizeof(dtb_name);
  21. int ret;
  22. if (env_get("fdtfile"))
  23. return; /* do nothing if it is already set */
  24. compat = fdt_stringlist_get(gd->fdt_blob, 0, "compatible", 0, NULL);
  25. if (!compat)
  26. goto fail;
  27. /* rip off the vendor prefix "socionext," */
  28. compat = strchr(compat, ',');
  29. if (!compat)
  30. goto fail;
  31. compat++;
  32. strncpy(dtb_name, compat, buf_len);
  33. buf_len -= strlen(compat);
  34. strncat(dtb_name, ".dtb", buf_len);
  35. ret = env_set("fdtfile", dtb_name);
  36. if (ret)
  37. goto fail;
  38. return;
  39. fail:
  40. pr_warn("\"fdt_file\" environment variable was not set correctly\n");
  41. }
  42. static void uniphier_set_env_addr(const char *env, const char *offset_env)
  43. {
  44. DECLARE_GLOBAL_DATA_PTR;
  45. unsigned long offset = 0;
  46. const char *str;
  47. char *end;
  48. int ret;
  49. if (env_get(env))
  50. return; /* do nothing if it is already set */
  51. if (offset_env) {
  52. str = env_get(offset_env);
  53. if (!str)
  54. goto fail;
  55. offset = hextoul(str, &end);
  56. if (*end)
  57. goto fail;
  58. }
  59. ret = env_set_hex(env, gd->ram_base + offset);
  60. if (ret)
  61. goto fail;
  62. return;
  63. fail:
  64. pr_warn("\"%s\" environment variable was not set correctly\n", env);
  65. }
  66. int board_late_init(void)
  67. {
  68. puts("MODE: ");
  69. switch (uniphier_boot_device_raw()) {
  70. case BOOT_DEVICE_MMC1:
  71. printf("eMMC Boot");
  72. env_set("bootdev", "emmc");
  73. break;
  74. case BOOT_DEVICE_MMC2:
  75. printf("SD Boot");
  76. env_set("bootdev", "sd");
  77. break;
  78. case BOOT_DEVICE_NAND:
  79. printf("NAND Boot");
  80. env_set("bootdev", "nand");
  81. break;
  82. case BOOT_DEVICE_NOR:
  83. printf("NOR Boot");
  84. env_set("bootdev", "nor");
  85. break;
  86. case BOOT_DEVICE_USB:
  87. printf("USB Boot");
  88. env_set("bootdev", "usb");
  89. break;
  90. default:
  91. printf("Unknown");
  92. break;
  93. }
  94. if (uniphier_have_internal_stm())
  95. printf(" (STM: %s)",
  96. uniphier_boot_from_backend() ? "OFF" : "ON");
  97. printf("\n");
  98. uniphier_set_env_fdt_file();
  99. uniphier_set_env_addr("dram_base", NULL);
  100. uniphier_set_env_addr("loadaddr", "loadaddr_offset");
  101. uniphier_set_env_addr("kernel_addr_r", "kernel_addr_r_offset");
  102. uniphier_set_env_addr("ramdisk_addr_r", "ramdisk_addr_r_offset");
  103. uniphier_set_env_addr("fdt_addr_r", "fdt_addr_r_offset");
  104. return 0;
  105. }