memsize.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. *addr = ~cnt;
  37. }
  38. addr = base;
  39. sync();
  40. save[i] = *addr;
  41. sync();
  42. *addr = 0;
  43. sync();
  44. if ((val = *addr) != 0) {
  45. /* Restore the original data before leaving the function. */
  46. sync();
  47. *addr = save[i];
  48. for (cnt = 1; cnt < maxsize / sizeof(long); cnt <<= 1) {
  49. addr = base + cnt;
  50. sync();
  51. *addr = save[--i];
  52. }
  53. return (0);
  54. }
  55. for (cnt = 1; cnt < maxsize / sizeof(long); cnt <<= 1) {
  56. addr = base + cnt; /* pointer arith! */
  57. val = *addr;
  58. *addr = save[--i];
  59. if (val != ~cnt) {
  60. size = cnt * sizeof(long);
  61. /*
  62. * Restore the original data
  63. * before leaving the function.
  64. */
  65. for (cnt <<= 1;
  66. cnt < maxsize / sizeof(long);
  67. cnt <<= 1) {
  68. addr = base + cnt;
  69. *addr = save[--i];
  70. }
  71. return (size);
  72. }
  73. }
  74. return (maxsize);
  75. }
  76. phys_size_t __weak get_effective_memsize(void)
  77. {
  78. #ifndef CONFIG_VERY_BIG_RAM
  79. return gd->ram_size;
  80. #else
  81. /* limit stack to what we can reasonable map */
  82. return ((gd->ram_size > CONFIG_MAX_MEM_MAPPED) ?
  83. CONFIG_MAX_MEM_MAPPED : gd->ram_size);
  84. #endif
  85. }