All of lore.kernel.org
 help / color / mirror / Atom feed
From: Bin Meng <bmeng.cn@gmail.com>
To: u-boot@lists.denx.de
Subject: [PATCH] memsize: Make get_ram_size() work with arbitary RAM size
Date: Fri, 10 Jul 2020 03:57:21 -0700	[thread overview]
Message-ID: <1594378641-26360-1-git-send-email-bmeng.cn@gmail.com> (raw)

From: Bin Meng <bin.meng@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.meng@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	other threads:[~2020-07-10 10:57 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-10 10:57 Bin Meng [this message]
2020-07-13 17:18 ` [PATCH] memsize: Make get_ram_size() work with arbitary RAM size Wolfgang Denk
2020-07-14 10:35   ` Bin Meng
2020-07-14 12:28     ` Wolfgang Denk
2020-07-16  9:36       ` Bin Meng
2020-07-14 15:53     ` Heinrich Schuchardt
2020-07-14 16:09       ` Wolfgang Denk
2020-07-16 21:04         ` Heinrich Schuchardt
2020-07-21  6:51           ` Wolfgang Denk

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1594378641-26360-1-git-send-email-bmeng.cn@gmail.com \
    --to=bmeng.cn@gmail.com \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.