stemmy.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2019 Stephan Gerhold <stephan@gerhold.net>
  4. */
  5. #include <common.h>
  6. #include <env.h>
  7. #include <init.h>
  8. #include <log.h>
  9. #include <stdlib.h>
  10. #include <asm/global_data.h>
  11. #include <asm/setup.h>
  12. #include <asm/system.h>
  13. DECLARE_GLOBAL_DATA_PTR;
  14. /* Parse atags provided by Samsung bootloader to get available memory */
  15. static ulong fw_mach __section(".data");
  16. static ulong fw_atags __section(".data");
  17. static const struct tag *fw_atags_copy;
  18. static uint fw_atags_size;
  19. void save_boot_params(ulong r0, ulong r1, ulong r2, ulong r3)
  20. {
  21. fw_mach = r1;
  22. fw_atags = r2;
  23. save_boot_params_ret();
  24. }
  25. static const struct tag *fw_atags_get(void)
  26. {
  27. const struct tag *tags = (const struct tag *)fw_atags;
  28. if (tags->hdr.tag != ATAG_CORE) {
  29. log_err("Invalid atags: tag 0x%x at %p\n", tags->hdr.tag, tags);
  30. return NULL;
  31. }
  32. return tags;
  33. }
  34. int dram_init(void)
  35. {
  36. const struct tag *t, *tags = fw_atags_get();
  37. if (!tags)
  38. return -EINVAL;
  39. for_each_tag(t, tags) {
  40. if (t->hdr.tag != ATAG_MEM)
  41. continue;
  42. debug("Memory: %#x-%#x (size %#x)\n", t->u.mem.start,
  43. t->u.mem.start + t->u.mem.size, t->u.mem.size);
  44. gd->ram_size += t->u.mem.size;
  45. }
  46. return 0;
  47. }
  48. int dram_init_banksize(void)
  49. {
  50. const struct tag *t, *tags = fw_atags_get();
  51. unsigned int bank = 0;
  52. if (!tags)
  53. return -EINVAL;
  54. for_each_tag(t, tags) {
  55. if (t->hdr.tag != ATAG_MEM)
  56. continue;
  57. gd->bd->bi_dram[bank].start = t->u.mem.start;
  58. gd->bd->bi_dram[bank].size = t->u.mem.size;
  59. if (++bank == CONFIG_NR_DRAM_BANKS)
  60. break;
  61. }
  62. return 0;
  63. }
  64. int board_init(void)
  65. {
  66. gd->bd->bi_arch_number = fw_mach;
  67. gd->bd->bi_boot_params = fw_atags;
  68. return 0;
  69. }
  70. static void parse_serial(const struct tag_serialnr *serialnr)
  71. {
  72. char serial[17];
  73. if (env_get("serial#"))
  74. return;
  75. sprintf(serial, "%08x%08x", serialnr->high, serialnr->low);
  76. env_set("serial#", serial);
  77. }
  78. /*
  79. * The downstream/vendor kernel (provided by Samsung) uses ATAGS for booting.
  80. * It also requires an extremely long cmdline provided by the primary bootloader
  81. * that is not suitable for booting mainline.
  82. *
  83. * Since downstream is the only user of ATAGS, we emulate the behavior of the
  84. * Samsung bootloader by generating only the initrd atag in U-Boot, and copying
  85. * all other ATAGS as-is from the primary bootloader.
  86. */
  87. static inline bool skip_atag(u32 tag)
  88. {
  89. return (tag == ATAG_NONE || tag == ATAG_CORE ||
  90. tag == ATAG_INITRD || tag == ATAG_INITRD2);
  91. }
  92. static void copy_atags(const struct tag *tags)
  93. {
  94. const struct tag *t;
  95. struct tag *copy;
  96. if (!tags)
  97. return;
  98. /* Calculate necessary size for tags we want to copy */
  99. for_each_tag(t, tags) {
  100. if (skip_atag(t->hdr.tag))
  101. continue;
  102. if (t->hdr.tag == ATAG_SERIAL)
  103. parse_serial(&t->u.serialnr);
  104. fw_atags_size += t->hdr.size * sizeof(u32);
  105. }
  106. if (!fw_atags_size)
  107. return; /* No tags to copy */
  108. copy = malloc(fw_atags_size);
  109. if (!copy)
  110. return;
  111. fw_atags_copy = copy;
  112. /* Copy tags */
  113. for_each_tag(t, tags) {
  114. if (skip_atag(t->hdr.tag))
  115. continue;
  116. memcpy(copy, t, t->hdr.size * sizeof(u32));
  117. copy = tag_next(copy);
  118. }
  119. }
  120. int misc_init_r(void)
  121. {
  122. copy_atags(fw_atags_get());
  123. return 0;
  124. }
  125. void setup_board_tags(struct tag **in_params)
  126. {
  127. if (!fw_atags_copy)
  128. return;
  129. /*
  130. * fw_atags_copy contains only full "struct tag" (plus data)
  131. * so copying it bytewise here should be fine.
  132. */
  133. memcpy(*in_params, fw_atags_copy, fw_atags_size);
  134. *(u8 **)in_params += fw_atags_size;
  135. }