memsize.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. /*
  24. * Check memory range for valid RAM. A simple memory test determines
  25. * the actually available RAM size between addresses `base' and
  26. * `base + maxsize'.
  27. */
  28. long get_ram_size(volatile long *base, long maxsize)
  29. {
  30. volatile long *addr;
  31. long save[32];
  32. long cnt;
  33. long val;
  34. long size;
  35. int i = 0;
  36. for (cnt = (maxsize / sizeof (long)) >> 1; cnt > 0; cnt >>= 1) {
  37. addr = base + cnt; /* pointer arith! */
  38. save[i++] = *addr;
  39. *addr = ~cnt;
  40. }
  41. addr = base;
  42. save[i] = *addr;
  43. *addr = 0;
  44. if ((val = *addr) != 0) {
  45. /* Restore the original data before leaving the function.
  46. */
  47. *addr = save[i];
  48. for (cnt = 1; cnt < maxsize / sizeof(long); cnt <<= 1) {
  49. addr = base + cnt;
  50. *addr = save[--i];
  51. }
  52. return (0);
  53. }
  54. for (cnt = 1; cnt < maxsize / sizeof (long); cnt <<= 1) {
  55. addr = base + cnt; /* pointer arith! */
  56. val = *addr;
  57. *addr = save[--i];
  58. if (val != ~cnt) {
  59. size = cnt * sizeof (long);
  60. /* Restore the original data before leaving the function.
  61. */
  62. for (cnt <<= 1; cnt < maxsize / sizeof (long); cnt <<= 1) {
  63. addr = base + cnt;
  64. *addr = save[--i];
  65. }
  66. return (size);
  67. }
  68. }
  69. return (maxsize);
  70. }