From: Bin Meng <bin.m...@windriver.com>

Currently get_ram_size() only works with certain RAM size like 1GiB,
2GiB, 4GiB, 8GiB, etc. Chanage the codes to work with any RAM size.

Signed-off-by: Bin Meng <bin.m...@windriver.com>
---

 common/memsize.c | 28 +++++++++++++++++-----------
 1 file changed, 17 insertions(+), 11 deletions(-)

diff --git a/common/memsize.c b/common/memsize.c
index e95c682..776737a 100644
--- a/common/memsize.c
+++ b/common/memsize.c
@@ -6,6 +6,7 @@
 
 #include <common.h>
 #include <init.h>
+#include <linux/bitops.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
@@ -27,14 +28,18 @@ DECLARE_GLOBAL_DATA_PTR;
 long get_ram_size(long *base, long maxsize)
 {
        volatile long *addr;
-       long           save[BITS_PER_LONG - 1];
-       long           save_base;
-       long           cnt;
-       long           val;
-       long           size;
-       int            i = 0;
+       long save[BITS_PER_LONG - 1];
+       long save_base;
+       long cnt;
+       long val;
+       long size;
+       int i = 0;
+       long n = maxsize / sizeof(long);
 
-       for (cnt = (maxsize / sizeof(long)) >> 1; cnt > 0; cnt >>= 1) {
+       n = __ffs(n);
+       n = BIT(n);
+
+       for (cnt = n >> 1; cnt > 0; cnt >>= 1) {
                addr = base + cnt;      /* pointer arith! */
                sync();
                save[i++] = *addr;
@@ -53,7 +58,7 @@ long get_ram_size(long *base, long maxsize)
                /* Restore the original data before leaving the function. */
                sync();
                *base = save_base;
-               for (cnt = 1; cnt < maxsize / sizeof(long); cnt <<= 1) {
+               for (cnt = 1; cnt < n; cnt <<= 1) {
                        addr  = base + cnt;
                        sync();
                        *addr = save[--i];
@@ -61,7 +66,7 @@ long get_ram_size(long *base, long maxsize)
                return (0);
        }
 
-       for (cnt = 1; cnt < maxsize / sizeof(long); cnt <<= 1) {
+       for (cnt = 1; cnt < n; cnt <<= 1) {
                addr = base + cnt;      /* pointer arith! */
                val = *addr;
                *addr = save[--i];
@@ -72,12 +77,13 @@ long get_ram_size(long *base, long maxsize)
                         * before leaving the function.
                         */
                        for (cnt <<= 1;
-                            cnt < maxsize / sizeof(long);
+                            cnt < n;
                             cnt <<= 1) {
                                addr  = base + cnt;
                                *addr = save[--i];
                        }
-                       /* warning: don't restore save_base in this case,
+                       /*
+                        * warning: don't restore save_base in this case,
                         * it is already done in the loop because
                         * base and base+size share the same physical memory
                         * and *base is saved after *(base+size) modification
-- 
2.7.4

Reply via email to