memsize.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2004
  4. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  5. */
  6. #include <common.h>
  7. #include <init.h>
  8. #include <asm/global_data.h>
  9. DECLARE_GLOBAL_DATA_PTR;
  10. #ifdef __PPC__
  11. /*
  12. * At least on G2 PowerPC cores, sequential accesses to non-existent
  13. * memory must be synchronized.
  14. */
  15. # include <asm/io.h> /* for sync() */
  16. #else
  17. # define sync() /* nothing */
  18. #endif
  19. /*
  20. * Check memory range for valid RAM. A simple memory test determines
  21. * the actually available RAM size between addresses `base' and
  22. * `base + maxsize'.
  23. */
  24. long get_ram_size(long *base, long maxsize)
  25. {
  26. volatile long *addr;
  27. long save[BITS_PER_LONG - 1];
  28. long save_base;
  29. long cnt;
  30. long val;
  31. long size;
  32. int i = 0;
  33. for (cnt = (maxsize / sizeof(long)) >> 1; cnt > 0; cnt >>= 1) {
  34. addr = base + cnt; /* pointer arith! */
  35. sync();
  36. save[i++] = *addr;
  37. sync();
  38. *addr = ~cnt;
  39. }
  40. addr = base;
  41. sync();
  42. save_base = *addr;
  43. sync();
  44. *addr = 0;
  45. sync();
  46. if ((val = *addr) != 0) {
  47. /* Restore the original data before leaving the function. */
  48. sync();
  49. *base = save_base;
  50. for (cnt = 1; cnt < maxsize / sizeof(long); cnt <<= 1) {
  51. addr = base + cnt;
  52. sync();
  53. *addr = save[--i];
  54. }
  55. return (0);
  56. }
  57. for (cnt = 1; cnt < maxsize / sizeof(long); cnt <<= 1) {
  58. addr = base + cnt; /* pointer arith! */
  59. val = *addr;
  60. *addr = save[--i];
  61. if (val != ~cnt) {
  62. size = cnt * sizeof(long);
  63. /*
  64. * Restore the original data
  65. * before leaving the function.
  66. */
  67. for (cnt <<= 1;
  68. cnt < maxsize / sizeof(long);
  69. cnt <<= 1) {
  70. addr = base + cnt;
  71. *addr = save[--i];
  72. }
  73. /* warning: don't restore save_base in this case,
  74. * it is already done in the loop because
  75. * base and base+size share the same physical memory
  76. * and *base is saved after *(base+size) modification
  77. * in first loop
  78. */
  79. return (size);
  80. }
  81. }
  82. *base = save_base;
  83. return (maxsize);
  84. }
  85. phys_size_t __weak get_effective_memsize(void)
  86. {
  87. #ifndef CONFIG_VERY_BIG_RAM
  88. return gd->ram_size;
  89. #else
  90. /* limit stack to what we can reasonable map */
  91. return ((gd->ram_size > CONFIG_MAX_MEM_MAPPED) ?
  92. CONFIG_MAX_MEM_MAPPED : gd->ram_size);
  93. #endif
  94. }