]> git.sur5r.net Git - u-boot/blobdiff - common/cmd_i2c.c
MIPS: bootm: add missing initramfs relocation
[u-boot] / common / cmd_i2c.c
index 5c60d313d9ac4a04313d2a6ee170598c18d3cae0..c266b88e5b272a1d4e728a362194b17c1993ea8b 100644 (file)
@@ -7,23 +7,7 @@
  * (C) Copyright 2001
  * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com.
  *
- * See file CREDITS for list of people who contributed to this
- * project.
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of
- * the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
- * MA 02111-1307 USA
+ * SPDX-License-Identifier:    GPL-2.0+
  */
 
 /*
@@ -82,6 +66,8 @@
  */
 
 #include <common.h>
+#include <bootretry.h>
+#include <cli.h>
 #include <command.h>
 #include <edid.h>
 #include <environment.h>
@@ -127,8 +113,6 @@ static uchar i2c_no_probes[] = CONFIG_SYS_I2C_NOPROBES;
 #define COMPARE_ADDR(a,i)      (i2c_no_probes[(i)] == (a))
 #define NO_PROBE_ADDR(i)       i2c_no_probes[(i)]
 #endif /* defined(CONFIG_SYS_I2C) */
-
-#define NUM_ELEMENTS_NOPROBE (sizeof(i2c_no_probes)/sizeof(i2c_no_probes[0]))
 #endif
 
 #define DISP_LINE_LEN  16
@@ -214,6 +198,19 @@ static uint get_alen(char *arg)
        return alen;
 }
 
+enum i2c_err_op {
+       I2C_ERR_READ,
+       I2C_ERR_WRITE,
+};
+
+static int i2c_report_err(int ret, enum i2c_err_op op)
+{
+       printf("Error %s the chip: %d\n",
+              op == I2C_ERR_READ ? "reading" : "writing", ret);
+
+       return CMD_RET_FAILURE;
+}
+
 /**
  * do_i2c_read() - Handle the "i2c read" command-line command
  * @cmdtp:     Command data struct pointer
@@ -261,7 +258,7 @@ static int do_i2c_read ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv
        memaddr = (u_char *)simple_strtoul(argv[4], NULL, 16);
 
        if (i2c_read(chip, devaddr, alen, memaddr, length) != 0) {
-               puts ("Error reading the chip.\n");
+               i2c_report_err(-1, I2C_ERR_READ);
                return 1;
        }
        return 0;
@@ -302,8 +299,7 @@ static int do_i2c_write(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[
 
        while (length-- > 0) {
                if (i2c_write(chip, devaddr++, alen, memaddr++, 1) != 0) {
-                       puts("Error writing to the chip.\n");
-                       return 1;
+                       return i2c_report_err(-1, I2C_ERR_WRITE);
                }
 /*
  * No write delay with FRAM devices.
@@ -386,7 +382,7 @@ static int do_i2c_md ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]
                linebytes = (nbytes > DISP_LINE_LEN) ? DISP_LINE_LEN : nbytes;
 
                if (i2c_read(chip, addr, alen, linebuf, linebytes) != 0)
-                       puts ("Error reading the chip.\n");
+                       i2c_report_err(-1, I2C_ERR_READ);
                else {
                        printf("%04x:", addr);
                        cp = linebuf;
@@ -468,7 +464,7 @@ static int do_i2c_mw ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]
 
        while (count-- > 0) {
                if (i2c_write(chip, addr++, alen, &byte, 1) != 0)
-                       puts ("Error writing the chip.\n");
+                       i2c_report_err(-1, I2C_ERR_WRITE);
                /*
                 * Wait for the write to complete.  The write can take
                 * up to 10mSec (we allow a little more time).
@@ -544,7 +540,7 @@ static int do_i2c_crc (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]
                addr++;
        }
        if (err > 0)
-               puts ("Error reading the chip,\n");
+               i2c_report_err(-1, I2C_ERR_READ);
        else
                printf ("%08lx\n", crc);
 
@@ -580,9 +576,7 @@ mod_i2c_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char * const arg
        if (argc != 3)
                return CMD_RET_USAGE;
 
-#ifdef CONFIG_BOOT_RETRY_TIME
-       reset_cmd_timeout();    /* got a good command to get here */
-#endif
+       bootretry_reset_cmd_timeout();  /* got a good command to get here */
        /*
         * We use the last specified parameters, unless new ones are
         * entered.
@@ -619,7 +613,7 @@ mod_i2c_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char * const arg
        do {
                printf("%08lx:", addr);
                if (i2c_read(chip, addr, alen, (uchar *)&data, size) != 0)
-                       puts ("\nError reading the chip,\n");
+                       i2c_report_err(-1, I2C_ERR_READ);
                else {
                        data = cpu_to_be32(data);
                        if (size == 1)
@@ -630,7 +624,7 @@ mod_i2c_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char * const arg
                                printf(" %08lx", data);
                }
 
-               nbytes = readline (" ? ");
+               nbytes = cli_readline(" ? ");
                if (nbytes == 0) {
                        /*
                         * <CR> pressed as only input, don't modify current
@@ -639,9 +633,8 @@ mod_i2c_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char * const arg
                        if (incrflag)
                                addr += size;
                        nbytes = size;
-#ifdef CONFIG_BOOT_RETRY_TIME
-                       reset_cmd_timeout(); /* good enough to not time out */
-#endif
+                       /* good enough to not time out */
+                       bootretry_reset_cmd_timeout();
                }
 #ifdef CONFIG_BOOT_RETRY_TIME
                else if (nbytes == -2)
@@ -658,14 +651,12 @@ mod_i2c_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char * const arg
                        data = be32_to_cpu(data);
                        nbytes = endp - console_buffer;
                        if (nbytes) {
-#ifdef CONFIG_BOOT_RETRY_TIME
                                /*
                                 * good enough to not time out
                                 */
-                               reset_cmd_timeout();
-#endif
+                               bootretry_reset_cmd_timeout();
                                if (i2c_write(chip, addr, alen, (uchar *)&data, size) != 0)
-                                       puts ("Error writing the chip.\n");
+                                       i2c_report_err(-1, I2C_ERR_WRITE);
 #ifdef CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS
                                udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
 #endif
@@ -717,7 +708,7 @@ static int do_i2c_probe (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv
 
 #if defined(CONFIG_SYS_I2C_NOPROBES)
                skip = 0;
-               for (k=0; k < NUM_ELEMENTS_NOPROBE; k++) {
+               for (k = 0; k < ARRAY_SIZE(i2c_no_probes); k++) {
                        if (COMPARE_BUS(bus, k) && COMPARE_ADDR(j, k)) {
                                skip = 1;
                                break;
@@ -735,7 +726,7 @@ static int do_i2c_probe (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv
 
 #if defined(CONFIG_SYS_I2C_NOPROBES)
        puts ("Excluded chip addresses:");
-       for (k=0; k < NUM_ELEMENTS_NOPROBE; k++) {
+       for (k = 0; k < ARRAY_SIZE(i2c_no_probes); k++) {
                if (COMPARE_BUS(bus,k))
                        printf(" %02X", NO_PROBE_ADDR(k));
        }
@@ -804,7 +795,7 @@ static int do_i2c_loop(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]
         */
        while (1) {
                if (i2c_read(chip, addr, alen, bytes, length) != 0)
-                       puts ("Error reading the chip.\n");
+                       i2c_report_err(-1, I2C_ERR_READ);
                udelay(delay);
        }
 
@@ -1362,7 +1353,7 @@ int do_edid(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
 
        chip = simple_strtoul(argv[1], NULL, 16);
        if (i2c_read(chip, 0, 1, (uchar *)&edid, sizeof(edid)) != 0) {
-               puts("Error reading EDID content.\n");
+               i2c_report_err(-1, I2C_ERR_READ);
                return 1;
        }
 
@@ -1387,7 +1378,8 @@ int do_edid(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
  * Returns zero always.
  */
 #if defined(CONFIG_SYS_I2C)
-int do_i2c_show_bus(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+static int do_i2c_show_bus(cmd_tbl_t *cmdtp, int flag, int argc,
+                               char * const argv[])
 {
        int     i;
 #ifndef CONFIG_SYS_I2C_DIRECT_BUS
@@ -1446,7 +1438,8 @@ int do_i2c_show_bus(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  * on error.
  */
 #if defined(CONFIG_SYS_I2C) || defined(CONFIG_I2C_MULTI_BUS)
-int do_i2c_bus_num(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+static int do_i2c_bus_num(cmd_tbl_t *cmdtp, int flag, int argc,
+                               char * const argv[])
 {
        int             ret = 0;
        unsigned int    bus_no;
@@ -1456,10 +1449,12 @@ int do_i2c_bus_num(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
                printf("Current bus is %d\n", i2c_get_bus_num());
        else {
                bus_no = simple_strtoul(argv[1], NULL, 10);
+#if defined(CONFIG_SYS_I2C)
                if (bus_no >= CONFIG_SYS_NUM_I2C_BUSES) {
                        printf("Invalid bus %d\n", bus_no);
                        return -1;
                }
+#endif
                printf("Setting bus to %d\n", bus_no);
                ret = i2c_set_bus_num(bus_no);
                if (ret)