board_late_init.c 2.6 KB

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