blob: ae03f0a434fa1f715f3a226bfed7688ab15847fd [file] [log] [blame]
Stefan Roese115802d2018-08-16 15:27:31 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2018 Stefan Roese <sr@denx.de>
4 */
5
6#include <common.h>
Simon Glass5e6201b2019-08-01 09:46:51 -06007#include <env.h>
Simon Glass9d1f6192019-08-02 09:44:25 -06008#include <env_internal.h>
Simon Glassa7b51302019-11-14 12:57:46 -07009#include <init.h>
Stefan Roesed0fdd672018-10-09 08:59:13 +020010#include <led.h>
Stefan Roese9c550d62018-11-30 07:46:30 +010011#include <net.h>
12#include <spi.h>
13#include <spi_flash.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070014#include <u-boot/crc.h>
Stefan Roese9c550d62018-11-30 07:46:30 +010015#include <uuid.h>
16#include <linux/ctype.h>
Stefan Roese51cb80c2018-10-09 08:59:11 +020017#include <linux/io.h>
18
19#define MT76XX_AGPIO_CFG 0x1000003c
Stefan Roese115802d2018-08-16 15:27:31 +020020
Stefan Roese9c550d62018-11-30 07:46:30 +010021#define FACTORY_DATA_OFFS 0xc0000
22#define FACTORY_DATA_SECT_SIZE 0x10000
Tom Rinib6c9e0d2019-11-18 20:02:06 -050023#if ((CONFIG_ENV_OFFSET_REDUND + CONFIG_ENV_SIZE) > FACTORY_DATA_OFFS)
Stefan Roese9c550d62018-11-30 07:46:30 +010024#error "U-Boot image with environment too big (overlapping with factory-data)!"
25#endif
26#define FACTORY_DATA_USER_OFFS 0x140
27#define FACTORY_DATA_SIZE 0x1f0
28#define FACTORY_DATA_CRC_LEN (FACTORY_DATA_SIZE - \
29 FACTORY_DATA_USER_OFFS - sizeof(u32))
30
31#define FACTORY_DATA_MAGIC 0xCAFEBABE
32
33struct factory_data_values {
34 u8 pad_1[4];
35 u8 wifi_mac[6]; /* offs: 0x004: binary value */
36 u8 pad_2[30];
37 u8 eth_mac[6]; /* offs: 0x028: binary value */
38 u8 pad_3[FACTORY_DATA_USER_OFFS - 4 - 6 - 30 - 6];
39 /* User values start here at offset 0x140 */
40 u32 crc;
41 u32 magic;
42 u32 version;
43 char ipr_id[UUID_STR_LEN]; /* UUID as string w/o ending \0 */
44 char hqv_id[UUID_STR_LEN]; /* UUID as string w/o ending \0 */
45 char unielec_id[UUID_STR_LEN]; /* UUID as string w/o ending \0 */
46};
47
Stefan Roese115802d2018-08-16 15:27:31 +020048int board_early_init_f(void)
49{
Stefan Roese51cb80c2018-10-09 08:59:11 +020050 void __iomem *gpio_mode;
51
52 /* Configure digital vs analog GPIOs */
53 gpio_mode = ioremap_nocache(MT76XX_AGPIO_CFG, 0x100);
54 iowrite32(0x00fe01ff, gpio_mode);
55
Stefan Roese115802d2018-08-16 15:27:31 +020056 return 0;
57}
Stefan Roesed0fdd672018-10-09 08:59:13 +020058
Stefan Roese9c550d62018-11-30 07:46:30 +010059static bool prepare_uuid_var(const char *fd_ptr, const char *env_var_name,
60 char errorchar)
61{
62 char str[UUID_STR_LEN + 1] = { 0 }; /* Enough for UUID stuff */
63 bool env_updated = false;
64 char *env;
65 int i;
66
67 memcpy(str, fd_ptr, UUID_STR_LEN);
68
69 /* Convert non-ascii character to 'X' */
70 for (i = 0; i < UUID_STR_LEN; i++) {
71 if (!(isascii(str[i]) && isprint(str[i])))
72 str[i] = errorchar;
73 }
74
75 env = env_get(env_var_name);
76 if (strcmp(env, str)) {
77 env_set(env_var_name, str);
78 env_updated = true;
79 }
80
81 return env_updated;
82}
83
84static void factory_data_env_config(void)
85{
86 struct factory_data_values *fd;
87 struct spi_flash *sf;
88 int env_updated = 0;
89 char str[UUID_STR_LEN + 1]; /* Enough for UUID stuff */
90 char *env;
91 u8 *buf;
92 u32 crc;
93 int ret;
94 u8 *ptr;
95
96 buf = malloc(FACTORY_DATA_SIZE);
97 if (!buf) {
98 printf("F-Data:Unable to allocate buffer\n");
99 return;
100 }
101
102 /*
103 * Get values from factory-data area in SPI NOR
104 */
105 sf = spi_flash_probe(CONFIG_SF_DEFAULT_BUS,
106 CONFIG_SF_DEFAULT_CS,
107 CONFIG_SF_DEFAULT_SPEED,
108 CONFIG_SF_DEFAULT_MODE);
109 if (!sf) {
110 printf("F-Data:Unable to access SPI NOR flash\n");
111 goto err_free;
112 }
113
114 ret = spi_flash_read(sf, FACTORY_DATA_OFFS, FACTORY_DATA_SIZE,
115 (void *)buf);
116 if (ret) {
117 printf("F-Data:Unable to read factory-data from SPI NOR\n");
118 goto err_spi_flash;
119 }
120
121 fd = (struct factory_data_values *)buf;
122
123 if (fd->magic != FACTORY_DATA_MAGIC)
124 printf("F-Data:Magic value not correct\n");
125
126 crc = crc32(0, (u8 *)&fd->magic, FACTORY_DATA_CRC_LEN);
127 if (crc != fd->crc)
128 printf("F-Data:CRC not correct\n");
129 else
130 printf("F-Data:factory-data version %x detected\n",
131 fd->version);
132
133 /* Handle wifi_mac env variable */
134 ptr = fd->wifi_mac;
135 sprintf(str, "%pM", ptr);
136 if (!is_valid_ethaddr(ptr))
137 printf("F-Data:Invalid MAC addr: wifi_mac %s\n", str);
138
139 env = env_get("wifiaddr");
140 if (strcmp(env, str)) {
141 env_set("wifiaddr", str);
142 env_updated = 1;
143 }
144
145 /* Handle eth_mac env variable */
146 ptr = fd->eth_mac;
147 sprintf(str, "%pM", ptr);
148 if (!is_valid_ethaddr(ptr))
149 printf("F-Data:Invalid MAC addr: eth_mac %s\n", str);
150
151 env = env_get("ethaddr");
152 if (strcmp(env, str)) {
153 env_set("ethaddr", str);
154 env_updated = 1;
155 }
156
157 /* Handle UUID env variables */
158 env_updated |= prepare_uuid_var(fd->ipr_id, "linuxmoduleid", 'X');
159 env_updated |= prepare_uuid_var(fd->hqv_id, "linuxmodulehqvid", '\0');
160 env_updated |= prepare_uuid_var(fd->unielec_id,
161 "linuxmoduleunielecid", '\0');
162
163 /* Check if the environment was updated and needs to get stored */
164 if (env_updated != 0) {
165 printf("F-Data:Values don't match env values -> saving\n");
166 env_save();
167 } else {
168 debug("F-Data:Values match current env values\n");
169 }
170
171err_spi_flash:
172 spi_flash_free(sf);
173
174err_free:
175 free(buf);
176}
177
Stefan Roesed0fdd672018-10-09 08:59:13 +0200178int board_late_init(void)
179{
180 if (IS_ENABLED(CONFIG_LED))
181 led_default_state();
182
Stefan Roese9c550d62018-11-30 07:46:30 +0100183 factory_data_env_config();
184
Stefan Roesed0fdd672018-10-09 08:59:13 +0200185 return 0;
186}
Stefan Roese9c550d62018-11-30 07:46:30 +0100187
188static void copy_or_generate_uuid(char *fd_ptr, const char *env_var_name)
189{
190 char str[UUID_STR_LEN + 1] = { 0 }; /* Enough for UUID stuff */
191 char *env;
192
193 /* Don't use the UUID dest place, as the \0 char won't fit */
194 env = env_get(env_var_name);
195 if (env)
196 strncpy(str, env, UUID_STR_LEN);
197 else
198 gen_rand_uuid_str(str, UUID_STR_FORMAT_STD);
199
200 memcpy(fd_ptr, str, UUID_STR_LEN);
201}
202
203/*
204 * Helper function to provide some sane factory-data values for testing
205 * purpose, when these values are not programmed correctly
206 */
207int do_fd_write(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
208{
209 struct factory_data_values *fd;
210 struct spi_flash *sf;
211 u8 *buf;
212 int ret = CMD_RET_FAILURE;
213
214 buf = malloc(FACTORY_DATA_SECT_SIZE);
215 if (!buf) {
216 printf("F-Data:Unable to allocate buffer\n");
217 return CMD_RET_FAILURE;
218 }
219
220 sf = spi_flash_probe(CONFIG_SF_DEFAULT_BUS,
221 CONFIG_SF_DEFAULT_CS,
222 CONFIG_SF_DEFAULT_SPEED,
223 CONFIG_SF_DEFAULT_MODE);
224 if (!sf) {
225 printf("F-Data:Unable to access SPI NOR flash\n");
226 goto err_free;
227 }
228
229 /* Generate the factory-data struct */
230
231 /* Fist read complete sector into buffer */
232 ret = spi_flash_read(sf, FACTORY_DATA_OFFS, FACTORY_DATA_SECT_SIZE,
233 (void *)buf);
234 if (ret) {
235 printf("F-Data:spi_flash_read failed (%d)\n", ret);
236 goto err_spi_flash;
237 }
238
239 fd = (struct factory_data_values *)buf;
240 fd->magic = FACTORY_DATA_MAGIC;
241 fd->version = 0x1;
242
243 /* Use existing MAC and UUID values or generate some random ones */
244 if (!eth_env_get_enetaddr("wifiaddr", fd->wifi_mac)) {
245 net_random_ethaddr(fd->wifi_mac);
246 /* to get a different seed value for the MAC address */
247 mdelay(10);
248 }
249
250 if (!eth_env_get_enetaddr("ethaddr", fd->eth_mac))
251 net_random_ethaddr(fd->eth_mac);
252
253 copy_or_generate_uuid(fd->ipr_id, "linuxmoduleid");
254 copy_or_generate_uuid(fd->hqv_id, "linuxmodulehqvid");
255 copy_or_generate_uuid(fd->unielec_id, "linuxmoduleunielecid");
256
257 printf("New factory-data values:\n");
258 printf("wifiaddr=%pM\n", fd->wifi_mac);
259 printf("ethaddr=%pM\n", fd->eth_mac);
260
261 /*
262 * We don't have the \0 char at the end, so we need to specify the
263 * length in the printf format instead
264 */
265 printf("linuxmoduleid=%." __stringify(UUID_STR_LEN) "s\n", fd->ipr_id);
266 printf("linuxmodulehqvid=%." __stringify(UUID_STR_LEN) "s\n",
267 fd->hqv_id);
268 printf("linuxmoduleunielecid=%." __stringify(UUID_STR_LEN) "s\n",
269 fd->unielec_id);
270
271 fd->crc = crc32(0, (u8 *)&fd->magic, FACTORY_DATA_CRC_LEN);
272
273 ret = spi_flash_erase(sf, FACTORY_DATA_OFFS, FACTORY_DATA_SECT_SIZE);
274 if (ret) {
275 printf("F-Data:spi_flash_erase failed (%d)\n", ret);
276 goto err_spi_flash;
277 }
278
279 ret = spi_flash_write(sf, FACTORY_DATA_OFFS, FACTORY_DATA_SECT_SIZE,
280 buf);
281 if (ret) {
282 printf("F-Data:spi_flash_write failed (%d)\n", ret);
283 goto err_spi_flash;
284 }
285
286 printf("F-Data:factory-data values written to SPI NOR flash\n");
287
288err_spi_flash:
289 spi_flash_free(sf);
290
291err_free:
292 free(buf);
293
294 return ret;
295}
296
297U_BOOT_CMD(
298 fd_write, 1, 0, do_fd_write,
299 "Write test factory-data values to SPI NOR",
300 "\n"
301);