Skip to content

Commit 455f049

Browse files
authored
Merge pull request #21 from francois-berder-imgtec/task-15
Set ethernet and wifi MAC address
2 parents 31a08e5 + 842f390 commit 455f049

File tree

9 files changed

+253
-11
lines changed

9 files changed

+253
-11
lines changed

board/imgtec/pistachio_bub/Makefile

+4
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,7 @@ endif
1919
ifdef CONFIG_CMD_PISTACHIO_SCRATCHPAD
2020
obj-y += cmd_scratchpad.o
2121
endif
22+
ifdef CONFIG_WINBOND_OTP
23+
obj-y += fdt.o
24+
obj-y += otp.o
25+
endif

board/imgtec/pistachio_bub/fdt.c

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright (C) 2016 Imagination Technologies
3+
* Author: Francois Berder <[email protected]>
4+
*
5+
* SPDX-License-Identifier: GPL-2.0+
6+
*/
7+
8+
#include <common.h>
9+
10+
#if defined(CONFIG_OF_LIBFDT) && defined(CONFIG_OF_BOARD_SETUP)
11+
12+
#include <winbond-otp.h>
13+
#include "otp.h"
14+
15+
DECLARE_GLOBAL_DATA_PTR;
16+
17+
static void fixup_wifi_mac(void *blob, int node)
18+
{
19+
u_char wifi_sta_mac_addr[MAC_ADDR_LEN], wifi_ap_mac_addr[MAC_ADDR_LEN];
20+
21+
memset(wifi_sta_mac_addr, 0, sizeof(wifi_sta_mac_addr));
22+
memset(wifi_ap_mac_addr, 0, sizeof(wifi_ap_mac_addr));
23+
24+
if (read_otp_version(VERSION_REG0_OFFSET) >= 1) {
25+
/* Read MAC addresses from OTP */
26+
if (read_otp_data(WIFI_STA_MAC_ADDRESS_OFFSET, MAC_ADDR_LEN,
27+
(char *)wifi_sta_mac_addr)
28+
|| read_otp_data(WIFI_AP_MAC_ADDRESS_OFFSET, MAC_ADDR_LEN,
29+
(char *)wifi_ap_mac_addr)) {
30+
printf("WARNING: Could not read Wifi MAC addresses from OTP\n");
31+
return;
32+
}
33+
}
34+
35+
/* Set Wifi STA and AP MAC address in device tree */
36+
if (is_valid_ethaddr(wifi_sta_mac_addr))
37+
fdt_setprop(blob, node, "mac-address0", wifi_sta_mac_addr,
38+
MAC_ADDR_LEN);
39+
else
40+
printf("WARNING: Invalid Wifi sta MAC address.\n");
41+
42+
if (is_valid_ethaddr(wifi_ap_mac_addr))
43+
fdt_setprop(blob, node, "mac-address1", wifi_ap_mac_addr,
44+
MAC_ADDR_LEN);
45+
else
46+
printf("WARNING: Invalid Wifi ap MAC address.\n");
47+
}
48+
49+
static void fixup_wifi_calibration(void *blob, int node)
50+
{
51+
int len;
52+
char dcxo;
53+
char *rf_params_prop;
54+
int version_reg1 = read_otp_version(VERSION_REG1_OFFSET);
55+
56+
if (version_reg1 >= 1) {
57+
/* Read calibration data from OTP */
58+
if (read_otp_data(DCXO_OFFSET, sizeof(dcxo), &dcxo)) {
59+
printf("WARNING: Could not read dcxo from OTP\n");
60+
return;
61+
}
62+
}
63+
64+
/* Overwrite first byte of rf-params property with DXCO */
65+
rf_params_prop = fdt_getprop_w(blob, node, "rf-params", &len);
66+
if (!rf_params_prop) {
67+
printf("WARNING: Could not find rf-params property.\n");
68+
return;
69+
}
70+
71+
if (version_reg1 >= 1)
72+
rf_params_prop[0] = dcxo;
73+
74+
fdt_setprop(blob, node, "rf-params", rf_params_prop, len);
75+
}
76+
77+
int ft_board_setup(void *blob, bd_t *bd)
78+
{
79+
int node = fdt_path_offset(blob, "wifi0");
80+
if (node < 0) {
81+
printf("WARNING: can't find wifi0 alias\n");
82+
return -1;
83+
}
84+
85+
fixup_wifi_mac(blob, node);
86+
fixup_wifi_calibration(blob, node);
87+
88+
return 0;
89+
}
90+
#endif

board/imgtec/pistachio_bub/otp.c

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright (C) 2016 Imagination Technologies
3+
* Author: Francois Berder <[email protected]>
4+
*
5+
* SPDX-License-Identifier: GPL-2.0+
6+
*/
7+
8+
#include <common.h>
9+
#include <winbond-otp.h>
10+
#include "otp.h"
11+
12+
int read_otp_version(loff_t offset)
13+
{
14+
u_char version;
15+
16+
if (read_otp_data(offset, sizeof(version), (char *)&version)) {
17+
printf("WARNING: Could not read register version from OTP.\n");
18+
return -1;
19+
}
20+
21+
return version;
22+
}

board/imgtec/pistachio_bub/otp.h

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (C) 2016 Imagination Technologies
3+
* Author: Francois Berder <[email protected]>
4+
*
5+
* SPDX-License-Identifier: GPL-2.0+
6+
*/
7+
8+
#ifndef _OTP_H
9+
#define _OTP_H
10+
11+
#define MAC_ADDR_LEN 6
12+
13+
#ifdef CONFIG_WINBOND_OTP
14+
15+
#include <linux/types.h>
16+
17+
#define VERSION_REG0_OFFSET 0x1002
18+
#define VERSION_REG1_OFFSET 0x2002
19+
#define WIFI_STA_MAC_ADDRESS_OFFSET 0x1003
20+
#define WIFI_AP_MAC_ADDRESS_OFFSET 0x1009
21+
#define ETH_MAC_ADDRESS_OFFSET 0x1015
22+
#define DCXO_OFFSET 0x2003
23+
24+
int read_otp_version(loff_t offset);
25+
26+
#endif
27+
28+
#endif

board/imgtec/pistachio_bub/pistachio.c

+33-10
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@
2121
#include <asm-generic/sections.h>
2222
#include <watchdog.h>
2323
#include <tpm.h>
24-
24+
#include <winbond-otp.h>
2525
#include "mfio.h"
26+
#include "otp.h"
27+
2628

2729
DECLARE_GLOBAL_DATA_PTR;
28-
const char *enet_dtb_macaddr = 0;
2930

3031
int reloc_tlb_fixup(void)
3132
{
@@ -109,22 +110,44 @@ static const char *get_dtb_macaddr(u32 ifno)
109110
if (mac && is_valid_ethaddr((u8 *)mac))
110111
return mac;
111112

112-
return NULL;
113+
return NULL;
113114
}
114115
#endif
115116

116117
int board_eth_init(bd_t *bs)
117118
{
119+
u_char mac_addr[MAC_ADDR_LEN];
120+
118121
mfio_setup_ethernet();
119122

120-
/* try to get a valid macaddr from dtb */
123+
/* Order of precedence:
124+
* 1. Check for existing ethaddr environment variable
125+
* 2. Read from OTP
126+
* 3. Fallback on dtb
127+
*/
128+
memset(mac_addr, 0, MAC_ADDR_LEN);
129+
eth_getenv_enetaddr("ethaddr", mac_addr);
130+
131+
#ifdef CONFIG_WINBOND_OTP
132+
if (!is_valid_ethaddr(mac_addr)) {
133+
if (read_otp_version(VERSION_REG0_OFFSET) >= 1) {
134+
if (!read_otp_data(ETH_MAC_ADDRESS_OFFSET, MAC_ADDR_LEN,
135+
(char *)mac_addr)
136+
&& is_valid_ethaddr(mac_addr))
137+
eth_setenv_enetaddr("ethaddr", (u8 *)mac_addr);
138+
else
139+
printf("Could not read MAC address from OTP\n");
140+
}
141+
}
142+
#endif
121143
#ifdef CONFIG_OF_CONTROL
122-
enet_dtb_macaddr = get_dtb_macaddr(0);
123-
124-
if (enet_dtb_macaddr)
125-
eth_setenv_enetaddr("ethaddr", (u8 *)enet_dtb_macaddr);
126-
else
127-
printf("No valid Mac-addr found from dtb\n");
144+
if (!is_valid_ethaddr(mac_addr)) {
145+
const char *enet_dtb_macaddr = get_dtb_macaddr(0);
146+
if (enet_dtb_macaddr)
147+
eth_setenv_enetaddr("ethaddr", (u8 *)enet_dtb_macaddr);
148+
else
149+
printf("No valid Mac-addr found from dtb\n");
150+
}
128151
#endif
129152

130153
#ifndef CONFIG_DM_ETH

drivers/mtd/spi/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ endif
1616
obj-$(CONFIG_SPI_FLASH) += sf_probe.o
1717
#endif
1818
obj-$(CONFIG_CMD_SF) += sf.o
19+
obj-$(CONFIG_WINBOND_OTP) += winbond-otp.o
1920
obj-$(CONFIG_SPI_FLASH) += sf_ops.o sf_params.o
2021
obj-$(CONFIG_SPI_FLASH_DATAFLASH) += sf_dataflash.o
2122
obj-$(CONFIG_SPI_FLASH_MTD) += sf_mtd.o

drivers/mtd/spi/winbond-otp.c

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright (C) 2016 Imagination Technologies
3+
* Author: Avinash Tahakik <[email protected]>
4+
*
5+
* SPDX-License-Identifier: GPL-2.0+
6+
*/
7+
8+
#include <common.h>
9+
#include <spi.h>
10+
#include <spi_flash.h>
11+
#include <winbond-otp.h>
12+
13+
#include "sf_internal.h"
14+
15+
/* SPI FLASH opcodes */
16+
17+
#define SPINOR_OP_RD_SECURITY_REG 0x48 /* Read security register */
18+
#define SECURITY_REG_SIZE 256 /* bytes per security register */
19+
20+
#define REG1_START_ADDRESS 0X1000
21+
#define REG2_START_ADDRESS 0X2000
22+
#define REG3_START_ADDRESS 0X3000
23+
24+
#define REG1_END_ADDRESS 0X10FF
25+
#define REG2_END_ADDRESS 0X20FF
26+
#define REG3_END_ADDRESS 0X30FF
27+
28+
static struct spi_flash *flash;
29+
30+
static int check_addr_validity(loff_t from, size_t len)
31+
{
32+
if ((REG1_START_ADDRESS <= from && from + len <= REG1_END_ADDRESS)
33+
|| (REG2_START_ADDRESS <= from && from + len <= REG2_END_ADDRESS)
34+
|| (REG3_START_ADDRESS <= from && from + len <= REG3_END_ADDRESS))
35+
return 0;
36+
37+
return -1;
38+
}
39+
40+
int read_otp_data(loff_t from, size_t len, char *data)
41+
{
42+
if (check_addr_validity(from, len))
43+
return -1;
44+
45+
if (!flash)
46+
flash = spi_flash_probe(1, 0, CONFIG_SF_DEFAULT_SPEED,
47+
CONFIG_SF_DEFAULT_MODE);
48+
49+
if (!flash) {
50+
printf("Failed to initialize SPI flash at 1:0\n");
51+
return -1;
52+
}
53+
54+
flash->read_cmd = SPINOR_OP_RD_SECURITY_REG;
55+
return spi_flash_cmd_read_ops(flash, from, len, data);
56+
}

include/configs/pistachio_bub.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,13 @@
2222
#define PISTACHIO_BOARD_NAME CONFIG_SYS_CONFIG_NAME
2323
#define CONFIG_BOARD_EARLY_INIT_F
2424
#define CONFIG_DISPLAY_BOARDINFO
25-
25+
#define CONFIG_WINBOND_OTP
2626
#define CONFIG_OF_LIBFDT
27+
28+
#ifdef CONFIG_WINBOND_OTP
29+
#define CONFIG_OF_BOARD_SETUP
30+
#endif
31+
2732
/*
2833
* CPU Configuration
2934
*/

include/winbond-otp.h

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* Copyright (C) 2015 Imagination Technologies
3+
* Author: Avinash Tahakik <[email protected]>
4+
*
5+
* SPDX-License-Identifier: GPL-2.0+
6+
*/
7+
8+
#ifndef WINBOND_OTP_H
9+
#define WINBOND_OTP_H
10+
11+
int read_otp_data(loff_t from, size_t len, char *data);
12+
13+
#endif

0 commit comments

Comments
 (0)