memsize.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * (C) Copyright 2004
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <config.h>
  24. #ifdef __PPC__
  25. /*
  26. * At least on G2 PowerPC cores, sequential accesses to non-existent
  27. * memory must be synchronized.
  28. */
  29. # include <asm/io.h> /* for sync() */
  30. #else
  31. # define sync() /* nothing */
  32. #endif
  33. /*
  34. * Check memory range for valid RAM. A simple memory test determines
  35. * the actually available RAM size between addresses `base' and
  36. * `base + maxsize'.
  37. */
  38. long get_ram_size(volatile long *base, long maxsize)
  39. {
  40. volatile long *addr;
  41. long save[32];
  42. long cnt;
  43. long val;
  44. long size;
  45. int i = 0;
  46. for (cnt = (maxsize / sizeof (long)) >> 1; cnt > 0; cnt >>= 1) {
  47. addr = base + cnt; /* pointer arith! */
  48. sync ();
  49. save[i++] = *addr;
  50. sync ();
  51. *addr = ~cnt;
  52. }
  53. addr = base;
  54. sync ();
  55. save[i] = *addr;
  56. sync ();
  57. *addr = 0;
  58. sync ();
  59. if ((val = *addr) != 0) {
  60. /* Restore the original data before leaving the function.
  61. */
  62. sync ();
  63. *addr = save[i];
  64. for (cnt = 1; cnt < maxsize / sizeof(long); cnt <<= 1) {
  65. addr = base + cnt;
  66. sync ();
  67. *addr = save[--i];
  68. }
  69. return (0);
  70. }
  71. for (cnt = 1; cnt < maxsize / sizeof (long); cnt <<= 1) {
  72. addr = base + cnt; /* pointer arith! */
  73. val = *addr;
  74. *addr = save[--i];
  75. if (val != ~cnt) {
  76. size = cnt * sizeof (long);
  77. /* Restore the original data before leaving the function.
  78. */
  79. for (cnt <<= 1; cnt < maxsize / sizeof (long); cnt <<= 1) {
  80. addr = base + cnt;
  81. *addr = save[--i];
  82. }
  83. return (size);
  84. }
  85. }
  86. return (maxsize);
  87. }