Merge branch '2021-02-02-drop-asm_global_data-when-unused'
[platform/kernel/u-boot.git] / board / keymile / common / common.c
index 2ddb3da..2ce7462 100644 (file)
@@ -1,22 +1,24 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * (C) Copyright 2008
  * Heiko Schocher, DENX Software Engineering, hs@denx.de.
  *
  * (C) Copyright 2011
  * Holger Brunck, Keymile GmbH Hannover, holger.brunck@keymile.com
- *
- * SPDX-License-Identifier:    GPL-2.0+
  */
 
 #include <common.h>
+#include <env.h>
 #include <ioports.h>
 #include <command.h>
 #include <malloc.h>
 #include <cli_hush.h>
 #include <net.h>
 #include <netdev.h>
+#include <asm/global_data.h>
 #include <asm/io.h>
 #include <linux/ctype.h>
+#include <linux/delay.h>
 
 #if defined(CONFIG_POST)
 #include "post.h"
@@ -40,7 +42,6 @@ DECLARE_GLOBAL_DATA_PTR;
  */
 int set_km_env(void)
 {
-       uchar buf[32];
        unsigned int pnvramaddr;
        unsigned int pram;
        unsigned int varaddr;
@@ -48,27 +49,24 @@ int set_km_env(void)
        char *p;
        unsigned long rootfssize = 0;
 
-       pnvramaddr = gd->ram_size - CONFIG_KM_RESERVED_PRAM - CONFIG_KM_PHRAM
-                       - CONFIG_KM_PNVRAM;
-       sprintf((char *)buf, "0x%x", pnvramaddr);
-       setenv("pnvramaddr", (char *)buf);
+       pnvramaddr = CONFIG_SYS_SDRAM_BASE + gd->ram_size -
+               CONFIG_KM_RESERVED_PRAM - CONFIG_KM_PHRAM - CONFIG_KM_PNVRAM;
+       env_set_hex("pnvramaddr", pnvramaddr);
 
-       /* try to read rootfssize (ram image) from envrionment */
-       p = getenv("rootfssize");
-       if (p != NULL)
+       /* try to read rootfssize (ram image) from environment */
+       p = env_get("rootfssize");
+       if (p)
                strict_strtoul(p, 16, &rootfssize);
        pram = (rootfssize + CONFIG_KM_RESERVED_PRAM + CONFIG_KM_PHRAM +
                CONFIG_KM_PNVRAM) / 0x400;
-       sprintf((char *)buf, "0x%x", pram);
-       setenv("pram", (char *)buf);
+       env_set_ulong("pram", pram);
 
-       varaddr = gd->ram_size - CONFIG_KM_RESERVED_PRAM - CONFIG_KM_PHRAM;
-       sprintf((char *)buf, "0x%x", varaddr);
-       setenv("varaddr", (char *)buf);
+       varaddr = CONFIG_SYS_SDRAM_BASE + gd->ram_size -
+               CONFIG_KM_RESERVED_PRAM - CONFIG_KM_PHRAM;
+       env_set_hex("varaddr", varaddr);
 
        kernelmem = gd->ram_size - 0x400 * pram;
-       sprintf((char *)buf, "0x%x", kernelmem);
-       setenv("kernelmem", (char *)buf);
+       env_set_hex("kernelmem", kernelmem);
 
        return 0;
 }
@@ -143,7 +141,7 @@ void i2c_init_board(void)
 #endif
 
 #if defined(CONFIG_KM_COMMON_ETH_INIT)
-int board_eth_init(bd_t *bis)
+int board_eth_init(struct bd_info *bis)
 {
        if (ethernet_present())
                return cpu_eth_init(bis);
@@ -157,36 +155,36 @@ int board_eth_init(bd_t *bis)
  * read out the board id and the hw key from the intventory EEPROM and set
  * this values as environment variables.
  */
-static int do_setboardid(cmd_tbl_t *cmdtp, int flag, int argc,
-                               char *const argv[])
+static int do_setboardid(struct cmd_tbl *cmdtp, int flag, int argc,
+                        char *const argv[])
 {
        unsigned char buf[32];
        char *p;
 
        p = get_local_var("IVM_BoardId");
-       if (p == NULL) {
+       if (!p) {
                printf("can't get the IVM_Boardid\n");
                return 1;
        }
-       sprintf((char *)buf, "%s", p);
-       setenv("boardid", (char *)buf);
+       strcpy((char *)buf, p);
+       env_set("boardid", (char *)buf);
        printf("set boardid=%s\n", buf);
 
        p = get_local_var("IVM_HWKey");
-       if (p == NULL) {
+       if (!p) {
                printf("can't get the IVM_HWKey\n");
                return 1;
        }
-       sprintf((char *)buf, "%s", p);
-       setenv("hwkey", (char *)buf);
+       strcpy((char *)buf, p);
+       env_set("hwkey", (char *)buf);
        printf("set hwkey=%s\n", buf);
        printf("Execute manually saveenv for persistent storage.\n");
 
        return 0;
 }
 
-U_BOOT_CMD(km_setboardid, 1, 0, do_setboardid, "setboardid", "read out bid and "
-                                "hwkey from IVM and set in environment");
+U_BOOT_CMD(km_setboardid, 1, 0, do_setboardid, "setboardid",
+          "read out bid and hwkey from IVM and set in environment");
 
 /*
  * command km_checkbidhwk
@@ -203,8 +201,8 @@ U_BOOT_CMD(km_setboardid, 1, 0, do_setboardid, "setboardid", "read out bid and "
  *                             application and in the init scripts (?)
  *     return 0 in case of match, 1 if not match or error
  */
-static int do_checkboardidhwk(cmd_tbl_t *cmdtp, int flag, int argc,
-                       char *const argv[])
+static int do_checkboardidhwk(struct cmd_tbl *cmdtp, int flag, int argc,
+                             char *const argv[])
 {
        unsigned long ivmbid = 0, ivmhwkey = 0;
        unsigned long envbid = 0, envhwkey = 0;
@@ -217,14 +215,14 @@ static int do_checkboardidhwk(cmd_tbl_t *cmdtp, int flag, int argc,
         * already stored in the local hush variables
         */
        p = get_local_var("IVM_BoardId");
-       if (p == NULL) {
+       if (!p) {
                printf("can't get the IVM_Boardid\n");
                return 1;
        }
        rc = strict_strtoul(p, 16, &ivmbid);
 
        p = get_local_var("IVM_HWKey");
-       if (p == NULL) {
+       if (!p) {
                printf("can't get the IVM_HWKey\n");
                return 1;
        }
@@ -236,13 +234,12 @@ static int do_checkboardidhwk(cmd_tbl_t *cmdtp, int flag, int argc,
        }
 
        /* now try to read values from environment if available */
-       p = getenv("boardid");
-       if (p != NULL)
+       p = env_get("boardid");
+       if (p)
                rc = strict_strtoul(p, 16, &envbid);
-       p = getenv("hwkey");
-       if (p != NULL)
+       p = env_get("hwkey");
+       if (p)
                rc = strict_strtoul(p, 16, &envhwkey);
-
        if (rc != 0) {
                printf("strict_strtoul returns error: %d", rc);
                return rc;
@@ -253,7 +250,7 @@ static int do_checkboardidhwk(cmd_tbl_t *cmdtp, int flag, int argc,
                 * BoardId/HWkey not available in the environment, so try the
                 * environment variable for BoardId/HWkey list
                 */
-               char *bidhwklist = getenv("boardIdListHex");
+               char *bidhwklist = env_get("boardIdListHex");
 
                if (bidhwklist) {
                        int found = 0;
@@ -262,9 +259,8 @@ static int do_checkboardidhwk(cmd_tbl_t *cmdtp, int flag, int argc,
 
                        if (verbose) {
                                printf("IVM_BoardId: %ld, IVM_HWKey=%ld\n",
-                                       ivmbid, ivmhwkey);
-                               printf("boardIdHwKeyList: %s\n",
-                                       bidhwklist);
+                                      ivmbid, ivmhwkey);
+                               printf("boardIdHwKeyList: %s\n", bidhwklist);
                        }
                        while (!found) {
                                /* loop over each bid/hwkey pair in the list */
@@ -290,13 +286,13 @@ static int do_checkboardidhwk(cmd_tbl_t *cmdtp, int flag, int argc,
                                        while (*rest && !isxdigit(*rest))
                                                rest++;
                                }
-                               if ((!bid) || (!hwkey)) {
+                               if (!bid || !hwkey) {
                                        /* end of list */
                                        break;
                                }
                                if (verbose) {
                                        printf("trying bid=0x%lX, hwkey=%ld\n",
-                                               bid, hwkey);
+                                              bid, hwkey);
                                }
                                /*
                                 * Compare the values of the found entry in the
@@ -304,28 +300,24 @@ static int do_checkboardidhwk(cmd_tbl_t *cmdtp, int flag, int argc,
                                 * in the inventory eeprom. If they are equal
                                 * set the values in environment variables.
                                 */
-                               if ((bid == ivmbid) && (hwkey == ivmhwkey)) {
-                                       char buf[10];
-
+                               if (bid == ivmbid && hwkey == ivmhwkey) {
                                        found = 1;
                                        envbid   = bid;
                                        envhwkey = hwkey;
-                                       sprintf(buf, "%lx", bid);
-                                       setenv("boardid", buf);
-                                       sprintf(buf, "%lx", hwkey);
-                                       setenv("hwkey", buf);
+                                       env_set_hex("boardid", bid);
+                                       env_set_hex("hwkey", hwkey);
                                }
                        } /* end while( ! found ) */
                }
        }
 
        /* compare now the values */
-       if ((ivmbid == envbid) && (ivmhwkey == envhwkey)) {
+       if (ivmbid == envbid && ivmhwkey == envhwkey) {
                printf("boardid=0x%3lX, hwkey=%ld\n", envbid, envhwkey);
                rc = 0; /* match */
        } else {
                printf("Error: env boardid=0x%3lX, hwkey=%ld\n", envbid,
-                       envhwkey);
+                      envhwkey);
                printf("       IVM bId=0x%3lX, hwKey=%ld\n", ivmbid, ivmhwkey);
                rc = 1; /* don't match */
        }
@@ -333,10 +325,8 @@ static int do_checkboardidhwk(cmd_tbl_t *cmdtp, int flag, int argc,
 }
 
 U_BOOT_CMD(km_checkbidhwk, 2, 0, do_checkboardidhwk,
-               "check boardid and hwkey",
-               "[v]\n  - check environment parameter "\
-               "\"boardIdListHex\" against stored boardid and hwkey "\
-               "from the IVM\n    v: verbose output"
+          "check boardid and hwkey",
+          "[v]\n  - check environment parameter \"boardIdListHex\" against stored boardid and hwkey from the IVM\n    v: verbose output"
 );
 
 /*
@@ -344,8 +334,8 @@ U_BOOT_CMD(km_checkbidhwk, 2, 0, do_checkboardidhwk,
  *  if the testpin of the board is asserted, return 1
  *  *  else return 0
  */
-static int do_checktestboot(cmd_tbl_t *cmdtp, int flag, int argc,
-                       char *const argv[])
+static int do_checktestboot(struct cmd_tbl *cmdtp, int flag, int argc,
+                           char *const argv[])
 {
        int testpin = 0;
        char *s = NULL;
@@ -354,12 +344,14 @@ static int do_checktestboot(cmd_tbl_t *cmdtp, int flag, int argc,
 
 #if defined(CONFIG_POST)
        testpin = post_hotkeys_pressed();
-       s = getenv("test_bank");
 #endif
+
+       s = env_get("test_bank");
        /* when test_bank is not set, act as if testpin is not asserted */
        testboot = (testpin != 0) && (s);
        if (verbose) {
                printf("testpin   = %d\n", testpin);
+               /* cppcheck-suppress nullPointer */
                printf("test_bank = %s\n", s ? s : "not set");
                printf("boot test app : %s\n", (testboot) ? "yes" : "no");
        }
@@ -368,6 +360,6 @@ static int do_checktestboot(cmd_tbl_t *cmdtp, int flag, int argc,
 }
 
 U_BOOT_CMD(km_checktestboot, 2, 0, do_checktestboot,
-               "check if testpin is asserted",
-               "[v]\n  v - verbose output"
+          "check if testpin is asserted",
+          "[v]\n  v - verbose output"
 );