sdram.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright (c) 2011 The Chromium OS Authors.
  3. * (C) Copyright 2010,2011
  4. * Graeme Russ, <graeme.russ@gmail.com>
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <common.h>
  9. #include <malloc.h>
  10. #include <asm/e820.h>
  11. #include <asm/u-boot-x86.h>
  12. #include <asm/global_data.h>
  13. #include <asm/processor.h>
  14. #include <asm/sections.h>
  15. #include <asm/arch/sysinfo.h>
  16. #include <asm/arch/tables.h>
  17. DECLARE_GLOBAL_DATA_PTR;
  18. unsigned install_e820_map(unsigned max_entries, struct e820entry *entries)
  19. {
  20. int i;
  21. unsigned num_entries = min((unsigned)lib_sysinfo.n_memranges, max_entries);
  22. if (num_entries < lib_sysinfo.n_memranges) {
  23. printf("Warning: Limiting e820 map to %d entries.\n",
  24. num_entries);
  25. }
  26. for (i = 0; i < num_entries; i++) {
  27. struct memrange *memrange = &lib_sysinfo.memrange[i];
  28. entries[i].addr = memrange->base;
  29. entries[i].size = memrange->size;
  30. entries[i].type = memrange->type;
  31. }
  32. return num_entries;
  33. }
  34. /*
  35. * This function looks for the highest region of memory lower than 4GB which
  36. * has enough space for U-Boot where U-Boot is aligned on a page boundary. It
  37. * overrides the default implementation found elsewhere which simply picks the
  38. * end of ram, wherever that may be. The location of the stack, the relocation
  39. * address, and how far U-Boot is moved by relocation are set in the global
  40. * data structure.
  41. */
  42. ulong board_get_usable_ram_top(ulong total_size)
  43. {
  44. uintptr_t dest_addr = 0;
  45. int i;
  46. for (i = 0; i < lib_sysinfo.n_memranges; i++) {
  47. struct memrange *memrange = &lib_sysinfo.memrange[i];
  48. /* Force U-Boot to relocate to a page aligned address. */
  49. uint64_t start = roundup(memrange->base, 1 << 12);
  50. uint64_t end = memrange->base + memrange->size;
  51. /* Ignore non-memory regions. */
  52. if (memrange->type != CB_MEM_RAM)
  53. continue;
  54. /* Filter memory over 4GB. */
  55. if (end > 0xffffffffULL)
  56. end = 0x100000000ULL;
  57. /* Skip this region if it's too small. */
  58. if (end - start < total_size)
  59. continue;
  60. /* Use this address if it's the largest so far. */
  61. if (end > dest_addr)
  62. dest_addr = end;
  63. }
  64. /* If no suitable area was found, return an error. */
  65. if (!dest_addr)
  66. panic("No available memory found for relocation");
  67. return (ulong)dest_addr;
  68. }
  69. int dram_init_f(void)
  70. {
  71. int i;
  72. phys_size_t ram_size = 0;
  73. for (i = 0; i < lib_sysinfo.n_memranges; i++) {
  74. struct memrange *memrange = &lib_sysinfo.memrange[i];
  75. unsigned long long end = memrange->base + memrange->size;
  76. if (memrange->type == CB_MEM_RAM && end > ram_size)
  77. ram_size = end;
  78. }
  79. gd->ram_size = ram_size;
  80. if (ram_size == 0)
  81. return -1;
  82. return 0;
  83. }
  84. int dram_init_banksize(void)
  85. {
  86. int i, j;
  87. if (CONFIG_NR_DRAM_BANKS) {
  88. for (i = 0, j = 0; i < lib_sysinfo.n_memranges; i++) {
  89. struct memrange *memrange = &lib_sysinfo.memrange[i];
  90. if (memrange->type == CB_MEM_RAM) {
  91. gd->bd->bi_dram[j].start = memrange->base;
  92. gd->bd->bi_dram[j].size = memrange->size;
  93. j++;
  94. if (j >= CONFIG_NR_DRAM_BANKS)
  95. break;
  96. }
  97. }
  98. }
  99. return 0;
  100. }
  101. int dram_init(void)
  102. {
  103. return dram_init_banksize();
  104. }