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