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