From patchwork Fri Jul 10 10:57:21 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bin Meng X-Patchwork-Id: 241297 List-Id: U-Boot discussion From: bmeng.cn at gmail.com (Bin Meng) Date: Fri, 10 Jul 2020 03:57:21 -0700 Subject: [PATCH] memsize: Make get_ram_size() work with arbitary RAM size Message-ID: <1594378641-26360-1-git-send-email-bmeng.cn@gmail.com> From: Bin Meng 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 --- 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 #include +#include 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