We rework the various board_is_foo() checks to take a pointer to
struct am335x_baseboard_id rather than using a local copy in board.c.
This allows us to make use of the same checks in mux.c as well as fixing
problems when this code could be running from read-only memory.

Signed-off-by: Tom Rini <tr...@ti.com>
---
 board/ti/am335x/board.c |   78 ++++++++++++++++-------------------------------
 board/ti/am335x/board.h |   31 +++++++++++++++++++
 board/ti/am335x/mux.c   |   10 +++---
 3 files changed, 62 insertions(+), 57 deletions(-)

diff --git a/board/ti/am335x/board.c b/board/ti/am335x/board.c
index b371376..6f6b5d0 100644
--- a/board/ti/am335x/board.c
+++ b/board/ti/am335x/board.c
@@ -51,43 +51,10 @@ static struct uart_sys *uart_base = (struct uart_sys 
*)DEFAULT_UART_BASE;
 
 static struct ctrl_dev *cdev = (struct ctrl_dev *)CTRL_DEVICE_BASE;
 
-static struct am335x_baseboard_id __attribute__((section (".data"))) header;
-
-static inline int board_is_bone(void)
-{
-       return !strncmp(header.name, "A335BONE", HDR_NAME_LEN);
-}
-
-static inline int board_is_bone_lt(void)
-{
-       return !strncmp(header.name, "A335BNLT", HDR_NAME_LEN);
-}
-
-static inline int board_is_evm_sk(void)
-{
-       return !strncmp("A335X_SK", header.name, HDR_NAME_LEN);
-}
-
-static inline int board_is_idk(void)
-{
-       return !strncmp(header.config, "SKU#02", 6);
-}
-
-static int __maybe_unused board_is_gp_evm(void)
-{
-       return !strncmp("A33515BB", header.name, 8);
-}
-
-int board_is_evm_15_or_later(void)
-{
-       return (!strncmp("A33515BB", header.name, 8) &&
-               strncmp("1.5", header.version, 3) <= 0);
-}
-
 /*
  * Read header information from EEPROM into global structure.
  */
-static int read_eeprom(void)
+static int read_eeprom(struct am335x_baseboard_id *header)
 {
        /* Check if baseboard eeprom is available */
        if (i2c_probe(CONFIG_SYS_I2C_EEPROM_ADDR)) {
@@ -97,28 +64,28 @@ static int read_eeprom(void)
        }
 
        /* read the eeprom using i2c */
-       if (i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0, 2, (uchar *)&header,
-                                                       sizeof(header))) {
+       if (i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0, 2, (uchar *)header,
+                               sizeof(struct am335x_baseboard_id))) {
                puts("Could not read the EEPROM; something fundamentally"
                        " wrong on the I2C bus.\n");
                return -EIO;
        }
 
-       if (header.magic != 0xEE3355AA) {
+       if (header->magic != 0xEE3355AA) {
                /*
                 * read the eeprom using i2c again,
                 * but use only a 1 byte address
                 */
-               if (i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0, 1,
-                                       (uchar *)&header, sizeof(header))) {
+               if (i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0, 1, (uchar *)header,
+                                       sizeof(struct am335x_baseboard_id))) {
                        puts("Could not read the EEPROM; something "
                                "fundamentally wrong on the I2C bus.\n");
                        return -EIO;
                }
 
-               if (header.magic != 0xEE3355AA) {
+               if (header->magic != 0xEE3355AA) {
                        printf("Incorrect magic number (0x%x) in EEPROM\n",
-                                       header.magic);
+                                       header->magic);
                        return -EINVAL;
                }
        }
@@ -304,6 +271,8 @@ static struct emif_regs ddr3_evm_emif_reg_data = {
  */
 void s_init(void)
 {
+       __maybe_unused struct am335x_baseboard_id header;
+
        /* WDT1 is already running when the bootloader gets control
         * Disable it to avoid "random" resets
         */
@@ -362,11 +331,11 @@ void s_init(void)
        /* Initalize the board header */
        enable_i2c0_pin_mux();
        i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
-       if (read_eeprom() < 0)
+       if (read_eeprom(&header) < 0)
                puts("Could not get board ID.\n");
 
        enable_board_pin_mux(&header);
-       if (board_is_evm_sk()) {
+       if (board_is_evm_sk(&header)) {
                /*
                 * EVM SK 1.2A and later use gpio0_7 to enable DDR3.
                 * This is safe enough to do on older revs.
@@ -375,15 +344,15 @@ void s_init(void)
                gpio_direction_output(GPIO_DDR_VTT_EN, 1);
        }
 
-       if (board_is_evm_sk())
+       if (board_is_evm_sk(&header))
                config_ddr(303, MT41J128MJT125_IOCTRL_VALUE, &ddr3_data,
                           &ddr3_cmd_ctrl_data, &ddr3_emif_reg_data, 0);
-       else if (board_is_bone_lt())
+       else if (board_is_bone_lt(&header))
                config_ddr(400, MT41K256M16HA125E_IOCTRL_VALUE,
                           &ddr3_beagleblack_data,
                           &ddr3_beagleblack_cmd_ctrl_data,
                           &ddr3_beagleblack_emif_reg_data, 0);
-       else if (board_is_evm_15_or_later())
+       else if (board_is_evm_15_or_later(&header))
                config_ddr(303, MT41J512M8RH125_IOCTRL_VALUE, &ddr3_evm_data,
                           &ddr3_evm_cmd_ctrl_data, &ddr3_evm_emif_reg_data, 0);
        else
@@ -397,10 +366,6 @@ void s_init(void)
  */
 int board_init(void)
 {
-       i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
-       if (read_eeprom() < 0)
-               puts("Could not get board ID.\n");
-
        gd->bd->bi_boot_params = PHYS_DRAM_1 + 0x100;
 
        gpmc_init();
@@ -413,6 +378,10 @@ int board_late_init(void)
 {
 #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
        char safe_string[HDR_NAME_LEN + 1];
+       struct am335x_baseboard_id header;
+
+       if (read_eeprom(&header) < 0)
+               puts("Could not get board ID.\n");
 
        /* Now set variables based on the header. */
        strncpy(safe_string, (char *)header.name, sizeof(header.name));
@@ -476,6 +445,7 @@ int board_eth_init(bd_t *bis)
        int rv, n = 0;
        uint8_t mac_addr[6];
        uint32_t mac_hi, mac_lo;
+       __maybe_unused struct am335x_baseboard_id header;
 
        /* try reading mac address from efuse */
        mac_lo = readl(&cdev->macid0l);
@@ -496,7 +466,11 @@ int board_eth_init(bd_t *bis)
                        eth_setenv_enetaddr("ethaddr", mac_addr);
        }
 
-       if (board_is_bone() || board_is_bone_lt() || board_is_idk()) {
+       if (read_eeprom(&header) < 0)
+               puts("Could not get board ID.\n");
+
+       if (board_is_bone(&header) || board_is_bone_lt(&header) ||
+                       board_is_idk(&header)) {
                writel(MII_MODE_ENABLE, &cdev->miisel);
                cpsw_slaves[0].phy_if = cpsw_slaves[1].phy_if =
                                PHY_INTERFACE_MODE_MII;
@@ -524,7 +498,7 @@ int board_eth_init(bd_t *bis)
 #define AR8051_DEBUG_RGMII_CLK_DLY_REG 0x5
 #define AR8051_RGMII_TX_CLK_DLY                0x100
 
-       if (board_is_evm_sk() || board_is_gp_evm()) {
+       if (board_is_evm_sk(&header) || board_is_gp_evm(&header)) {
                const char *devname;
                devname = miiphy_get_current_dev();
 
diff --git a/board/ti/am335x/board.h b/board/ti/am335x/board.h
index 48e112e..36ccaec 100644
--- a/board/ti/am335x/board.h
+++ b/board/ti/am335x/board.h
@@ -37,6 +37,37 @@ struct am335x_baseboard_id {
        char mac_addr[HDR_NO_OF_MAC_ADDR][HDR_ETH_ALEN];
 };
 
+static inline int board_is_bone(struct am335x_baseboard_id *header)
+{
+       return !strncmp(header->name, "A335BONE", HDR_NAME_LEN);
+}
+
+static inline int board_is_bone_lt(struct am335x_baseboard_id *header)
+{
+       return !strncmp(header->name, "A335BNLT", HDR_NAME_LEN);
+}
+
+static inline int board_is_evm_sk(struct am335x_baseboard_id *header)
+{
+       return !strncmp("A335X_SK", header->name, HDR_NAME_LEN);
+}
+
+static inline int board_is_idk(struct am335x_baseboard_id *header)
+{
+       return !strncmp(header->config, "SKU#02", 6);
+}
+
+static inline int board_is_gp_evm(struct am335x_baseboard_id *header)
+{
+       return !strncmp("A33515BB", header->name, HDR_NAME_LEN);
+}
+
+static inline int board_is_evm_15_or_later(struct am335x_baseboard_id *header)
+{
+       return (board_is_gp_evm(header) &&
+               strncmp("1.5", header->version, 3) <= 0);
+}
+
 /*
  * We have three pin mux functions that must exist.  We must be able to enable
  * uart0, for initial output and i2c0 to read the main EEPROM.  We then have a
diff --git a/board/ti/am335x/mux.c b/board/ti/am335x/mux.c
index 0283708..2e09d98 100644
--- a/board/ti/am335x/mux.c
+++ b/board/ti/am335x/mux.c
@@ -262,13 +262,13 @@ static unsigned short detect_daughter_board_profile(void)
 void enable_board_pin_mux(struct am335x_baseboard_id *header)
 {
        /* Do board-specific muxes. */
-       if (!strncmp(header->name, "A335BONE", HDR_NAME_LEN)) {
+       if (board_is_bone(header)) {
                /* Beaglebone pinmux */
                configure_module_pin_mux(i2c1_pin_mux);
                configure_module_pin_mux(mii1_pin_mux);
                configure_module_pin_mux(mmc0_pin_mux);
                configure_module_pin_mux(mmc1_pin_mux);
-       } else if (!strncmp(header->config, "SKU#01", 6)) {
+       } else if (board_is_gp_evm(header)) {
                /* General Purpose EVM */
                unsigned short profile = detect_daughter_board_profile();
                configure_module_pin_mux(rgmii1_pin_mux);
@@ -283,7 +283,7 @@ void enable_board_pin_mux(struct am335x_baseboard_id 
*header)
                        configure_module_pin_mux(mmc1_pin_mux);
                        configure_module_pin_mux(spi0_pin_mux);
                }
-       } else if (!strncmp(header->config, "SKU#02", 6)) {
+       } else if (board_is_idk(header)) {
                /*
                 * Industrial Motor Control (IDK)
                 * note: IDK console is on UART3 by default.
@@ -292,13 +292,13 @@ void enable_board_pin_mux(struct am335x_baseboard_id 
*header)
                 */
                configure_module_pin_mux(mii1_pin_mux);
                configure_module_pin_mux(mmc0_no_cd_pin_mux);
-       } else if (!strncmp(header->name, "A335X_SK", HDR_NAME_LEN)) {
+       } else if (board_is_evm_sk(header)) {
                /* Starter Kit EVM */
                configure_module_pin_mux(i2c1_pin_mux);
                configure_module_pin_mux(gpio0_7_pin_mux);
                configure_module_pin_mux(rgmii1_pin_mux);
                configure_module_pin_mux(mmc0_pin_mux_sk_evm);
-       } else if (!strncmp(header->name, "A335BNLT", HDR_NAME_LEN)) {
+       } else if (board_is_bone_lt(header)) {
                /* Beaglebone LT pinmux */
                configure_module_pin_mux(i2c1_pin_mux);
                configure_module_pin_mux(mii1_pin_mux);
-- 
1.7.9.5

_______________________________________________
U-Boot mailing list
U-Boot@lists.denx.de
http://lists.denx.de/mailman/listinfo/u-boot

Reply via email to