memsize.c 1.7 KB

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