board_late_init.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 <../drivers/mtd/nand/raw/denali.h>
  17. #include "init.h"
  18. static void nand_denali_wp_disable(void)
  19. {
  20. #ifdef CONFIG_NAND_DENALI
  21. /*
  22. * Since the boot rom enables the write protection for NAND boot mode,
  23. * it must be disabled somewhere for "nand write", "nand erase", etc.
  24. * The workaround is here to not disturb the Denali NAND controller
  25. * driver just for a really SoC-specific thing.
  26. */
  27. void __iomem *denali_reg = (void __iomem *)CONFIG_SYS_NAND_REGS_BASE;
  28. writel(WRITE_PROTECT__FLAG, denali_reg + WRITE_PROTECT);
  29. #endif
  30. }
  31. static void uniphier_set_env_fdt_file(void)
  32. {
  33. DECLARE_GLOBAL_DATA_PTR;
  34. const char *compat;
  35. char dtb_name[256];
  36. int buf_len = sizeof(dtb_name);
  37. int ret;
  38. if (env_get("fdtfile"))
  39. return; /* do nothing if it is already set */
  40. compat = fdt_stringlist_get(gd->fdt_blob, 0, "compatible", 0, NULL);
  41. if (!compat)
  42. goto fail;
  43. /* rip off the vendor prefix "socionext," */
  44. compat = strchr(compat, ',');
  45. if (!compat)
  46. goto fail;
  47. compat++;
  48. strncpy(dtb_name, compat, buf_len);
  49. buf_len -= strlen(compat);
  50. strncat(dtb_name, ".dtb", buf_len);
  51. ret = env_set("fdtfile", dtb_name);
  52. if (ret)
  53. goto fail;
  54. return;
  55. fail:
  56. pr_warn("\"fdt_file\" environment variable was not set correctly\n");
  57. }
  58. static void uniphier_set_env_addr(const char *env, const char *offset_env)
  59. {
  60. unsigned long offset = 0;
  61. const char *str;
  62. char *end;
  63. int ret;
  64. if (env_get(env))
  65. return; /* do nothing if it is already set */
  66. if (offset_env) {
  67. str = env_get(offset_env);
  68. if (!str)
  69. goto fail;
  70. offset = simple_strtoul(str, &end, 16);
  71. if (*end)
  72. goto fail;
  73. }
  74. ret = env_set_hex(env, gd->ram_base + offset);
  75. if (ret)
  76. goto fail;
  77. return;
  78. fail:
  79. pr_warn("\"%s\" environment variable was not set correctly\n", env);
  80. }
  81. int board_late_init(void)
  82. {
  83. puts("MODE: ");
  84. switch (uniphier_boot_device_raw()) {
  85. case BOOT_DEVICE_MMC1:
  86. printf("eMMC Boot");
  87. env_set("bootdev", "emmc");
  88. break;
  89. case BOOT_DEVICE_MMC2:
  90. printf("SD Boot");
  91. env_set("bootdev", "sd");
  92. break;
  93. case BOOT_DEVICE_NAND:
  94. printf("NAND Boot");
  95. env_set("bootdev", "nand");
  96. nand_denali_wp_disable();
  97. break;
  98. case BOOT_DEVICE_NOR:
  99. printf("NOR Boot");
  100. env_set("bootdev", "nor");
  101. break;
  102. case BOOT_DEVICE_USB:
  103. printf("USB Boot");
  104. env_set("bootdev", "usb");
  105. break;
  106. default:
  107. printf("Unknown");
  108. break;
  109. }
  110. if (uniphier_have_internal_stm())
  111. printf(" (STM: %s)",
  112. uniphier_boot_from_backend() ? "OFF" : "ON");
  113. printf("\n");
  114. uniphier_set_env_fdt_file();
  115. uniphier_set_env_addr("dram_base", NULL);
  116. uniphier_set_env_addr("loadaddr", "loadaddr_offset");
  117. uniphier_set_env_addr("kernel_addr_r", "kernel_addr_r_offset");
  118. uniphier_set_env_addr("ramdisk_addr_r", "ramdisk_addr_r_offset");
  119. uniphier_set_env_addr("fdt_addr_r", "fdt_addr_r_offset");
  120. return 0;
  121. }