From 842f390d68d1b4ca1ce8dc94b5a526f363100472 Mon Sep 17 00:00:00 2001 From: Francois Berder Date: Wed, 30 Nov 2016 17:32:48 +0000 Subject: [PATCH] Check version register of the OTP The version of register 0 and 1 must be greater or equal to 1, otherwise do not attempt to read any data from the OTP. Signed-off-by: Francois Berder --- board/imgtec/pistachio_bub/Makefile | 1 + board/imgtec/pistachio_bub/fdt.c | 38 ++++++++++++++------------ board/imgtec/pistachio_bub/otp.c | 22 +++++++++++++++ board/imgtec/pistachio_bub/otp.h | 28 +++++++++++++++++++ board/imgtec/pistachio_bub/pistachio.c | 16 ++++++----- include/winbond-otp.h | 2 -- 6 files changed, 81 insertions(+), 26 deletions(-) create mode 100644 board/imgtec/pistachio_bub/otp.c create mode 100644 board/imgtec/pistachio_bub/otp.h diff --git a/board/imgtec/pistachio_bub/Makefile b/board/imgtec/pistachio_bub/Makefile index 072b95c2d9..f202746fd7 100644 --- a/board/imgtec/pistachio_bub/Makefile +++ b/board/imgtec/pistachio_bub/Makefile @@ -21,4 +21,5 @@ obj-y += cmd_scratchpad.o endif ifdef CONFIG_WINBOND_OTP obj-y += fdt.o +obj-y += otp.o endif diff --git a/board/imgtec/pistachio_bub/fdt.c b/board/imgtec/pistachio_bub/fdt.c index 7f5c99a7bc..3b8111b117 100644 --- a/board/imgtec/pistachio_bub/fdt.c +++ b/board/imgtec/pistachio_bub/fdt.c @@ -10,10 +10,7 @@ #if defined(CONFIG_OF_LIBFDT) && defined(CONFIG_OF_BOARD_SETUP) #include - -#define WIFI_STA_MAC_ADDRESS_OFFSET 0x1003 -#define WIFI_AP_MAC_ADDRESS_OFFSET 0x1009 -#define DCXO_OFFSET 0x2003 +#include "otp.h" DECLARE_GLOBAL_DATA_PTR; @@ -24,13 +21,15 @@ static void fixup_wifi_mac(void *blob, int node) memset(wifi_sta_mac_addr, 0, sizeof(wifi_sta_mac_addr)); memset(wifi_ap_mac_addr, 0, sizeof(wifi_ap_mac_addr)); - /* Read MAC addresses from OTP */ - if (read_otp_data(WIFI_STA_MAC_ADDRESS_OFFSET, MAC_ADDR_LEN, - (char *)wifi_sta_mac_addr) - || read_otp_data(WIFI_AP_MAC_ADDRESS_OFFSET, MAC_ADDR_LEN, - (char *)wifi_ap_mac_addr)) { - printf("WARNING: Could not read Wifi MAC addresses from OTP\n"); - return; + if (read_otp_version(VERSION_REG0_OFFSET) >= 1) { + /* Read MAC addresses from OTP */ + if (read_otp_data(WIFI_STA_MAC_ADDRESS_OFFSET, MAC_ADDR_LEN, + (char *)wifi_sta_mac_addr) + || read_otp_data(WIFI_AP_MAC_ADDRESS_OFFSET, MAC_ADDR_LEN, + (char *)wifi_ap_mac_addr)) { + printf("WARNING: Could not read Wifi MAC addresses from OTP\n"); + return; + } } /* Set Wifi STA and AP MAC address in device tree */ @@ -52,11 +51,14 @@ static void fixup_wifi_calibration(void *blob, int node) int len; char dcxo; char *rf_params_prop; - - /* Read calibration data from OTP */ - if (read_otp_data(DCXO_OFFSET, sizeof(dcxo), &dcxo)) { - printf("WARNING: Could not read dcxo from OTP\n"); - return; + int version_reg1 = read_otp_version(VERSION_REG1_OFFSET); + + if (version_reg1 >= 1) { + /* Read calibration data from OTP */ + if (read_otp_data(DCXO_OFFSET, sizeof(dcxo), &dcxo)) { + printf("WARNING: Could not read dcxo from OTP\n"); + return; + } } /* Overwrite first byte of rf-params property with DXCO */ @@ -66,7 +68,9 @@ static void fixup_wifi_calibration(void *blob, int node) return; } - rf_params_prop[0] = dcxo; + if (version_reg1 >= 1) + rf_params_prop[0] = dcxo; + fdt_setprop(blob, node, "rf-params", rf_params_prop, len); } diff --git a/board/imgtec/pistachio_bub/otp.c b/board/imgtec/pistachio_bub/otp.c new file mode 100644 index 0000000000..307dec71a2 --- /dev/null +++ b/board/imgtec/pistachio_bub/otp.c @@ -0,0 +1,22 @@ +/* + * Copyright (C) 2016 Imagination Technologies + * Author: Francois Berder + * + * SPDX-License-Identifier: GPL-2.0+ + */ + +#include +#include +#include "otp.h" + +int read_otp_version(loff_t offset) +{ + u_char version; + + if (read_otp_data(offset, sizeof(version), (char *)&version)) { + printf("WARNING: Could not read register version from OTP.\n"); + return -1; + } + + return version; +} diff --git a/board/imgtec/pistachio_bub/otp.h b/board/imgtec/pistachio_bub/otp.h new file mode 100644 index 0000000000..32d586fd97 --- /dev/null +++ b/board/imgtec/pistachio_bub/otp.h @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2016 Imagination Technologies + * Author: Francois Berder + * + * SPDX-License-Identifier: GPL-2.0+ + */ + +#ifndef _OTP_H +#define _OTP_H + +#define MAC_ADDR_LEN 6 + +#ifdef CONFIG_WINBOND_OTP + +#include + +#define VERSION_REG0_OFFSET 0x1002 +#define VERSION_REG1_OFFSET 0x2002 +#define WIFI_STA_MAC_ADDRESS_OFFSET 0x1003 +#define WIFI_AP_MAC_ADDRESS_OFFSET 0x1009 +#define ETH_MAC_ADDRESS_OFFSET 0x1015 +#define DCXO_OFFSET 0x2003 + +int read_otp_version(loff_t offset); + +#endif + +#endif diff --git a/board/imgtec/pistachio_bub/pistachio.c b/board/imgtec/pistachio_bub/pistachio.c index 262ddf30e6..e0113028ca 100644 --- a/board/imgtec/pistachio_bub/pistachio.c +++ b/board/imgtec/pistachio_bub/pistachio.c @@ -23,8 +23,8 @@ #include #include #include "mfio.h" +#include "otp.h" -#define ETH_MAC_ADDRESS_OFFSET 0x1015 /* Ethernet MAC address offset */ DECLARE_GLOBAL_DATA_PTR; @@ -130,12 +130,14 @@ int board_eth_init(bd_t *bs) #ifdef CONFIG_WINBOND_OTP if (!is_valid_ethaddr(mac_addr)) { - if (!read_otp_data(ETH_MAC_ADDRESS_OFFSET, MAC_ADDR_LEN, - (char *)mac_addr) - && is_valid_ethaddr(mac_addr)) - eth_setenv_enetaddr("ethaddr", (u8 *)mac_addr); - else - printf("Could not read MAC address from OTP\n"); + if (read_otp_version(VERSION_REG0_OFFSET) >= 1) { + if (!read_otp_data(ETH_MAC_ADDRESS_OFFSET, MAC_ADDR_LEN, + (char *)mac_addr) + && is_valid_ethaddr(mac_addr)) + eth_setenv_enetaddr("ethaddr", (u8 *)mac_addr); + else + printf("Could not read MAC address from OTP\n"); + } } #endif #ifdef CONFIG_OF_CONTROL diff --git a/include/winbond-otp.h b/include/winbond-otp.h index c802174047..93d51e958d 100644 --- a/include/winbond-otp.h +++ b/include/winbond-otp.h @@ -8,8 +8,6 @@ #ifndef WINBOND_OTP_H #define WINBOND_OTP_H -#define MAC_ADDR_LEN 6 - int read_otp_data(loff_t from, size_t len, char *data); #endif