]>
git.ipfire.org Git - thirdparty/u-boot.git/blob - board/gardena/smart-gateway-mt7688/board.c
6fd771a2e2044da8a16736aad631df02d244b9d4
1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2018 Stefan Roese <sr@denx.de>
9 #include <env_internal.h>
17 #include <spi_flash.h>
18 #include <u-boot/crc.h>
20 #include <linux/ctype.h>
23 #define MT76XX_AGPIO_CFG 0x1000003c
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)!"
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))
35 #define FACTORY_DATA_MAGIC 0xCAFEBABE
37 struct factory_data_values
{
39 u8 wifi_mac
[6]; /* offs: 0x004: binary value */
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 */
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 */
52 int board_early_init_f(void)
54 void __iomem
*gpio_mode
;
56 /* Configure digital vs analog GPIOs */
57 gpio_mode
= ioremap_nocache(MT76XX_AGPIO_CFG
, 0x100);
58 iowrite32(0x00fe01ff, gpio_mode
);
63 static bool prepare_uuid_var(const char *fd_ptr
, const char *env_var_name
,
66 char str
[UUID_STR_LEN
+ 1] = { 0 }; /* Enough for UUID stuff */
67 bool env_updated
= false;
71 memcpy(str
, fd_ptr
, UUID_STR_LEN
);
73 /* Convert non-ascii character to 'X' */
74 for (i
= 0; i
< UUID_STR_LEN
; i
++) {
75 if (!(isascii(str
[i
]) && isprint(str
[i
])))
79 env
= env_get(env_var_name
);
80 if (strcmp(env
, str
)) {
81 env_set(env_var_name
, str
);
88 static void factory_data_env_config(void)
90 struct factory_data_values
*fd
;
93 char str
[UUID_STR_LEN
+ 1]; /* Enough for UUID stuff */
100 buf
= malloc(FACTORY_DATA_SIZE
);
102 printf("F-Data:Unable to allocate buffer\n");
107 * Get values from factory-data area in SPI NOR
109 sf
= spi_flash_probe(CONFIG_SF_DEFAULT_BUS
,
110 CONFIG_SF_DEFAULT_CS
,
111 CONFIG_SF_DEFAULT_SPEED
,
112 CONFIG_SF_DEFAULT_MODE
);
114 printf("F-Data:Unable to access SPI NOR flash\n");
118 ret
= spi_flash_read(sf
, FACTORY_DATA_OFFS
, FACTORY_DATA_SIZE
,
121 printf("F-Data:Unable to read factory-data from SPI NOR\n");
125 fd
= (struct factory_data_values
*)buf
;
127 if (fd
->magic
!= FACTORY_DATA_MAGIC
)
128 printf("F-Data:Magic value not correct\n");
130 crc
= crc32(0, (u8
*)&fd
->magic
, FACTORY_DATA_CRC_LEN
);
132 printf("F-Data:CRC not correct\n");
134 printf("F-Data:factory-data version %x detected\n",
137 /* Handle wifi_mac env variable */
139 sprintf(str
, "%pM", ptr
);
140 if (!is_valid_ethaddr(ptr
))
141 printf("F-Data:Invalid MAC addr: wifi_mac %s\n", str
);
143 env
= env_get("wifiaddr");
144 if (strcmp(env
, str
)) {
145 env_set("wifiaddr", str
);
149 /* Handle eth_mac env variable */
151 sprintf(str
, "%pM", ptr
);
152 if (!is_valid_ethaddr(ptr
))
153 printf("F-Data:Invalid MAC addr: eth_mac %s\n", str
);
155 env
= env_get("ethaddr");
156 if (strcmp(env
, str
)) {
157 env_set("ethaddr", str
);
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');
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");
172 debug("F-Data:Values match current env values\n");
182 int board_late_init(void)
184 if (IS_ENABLED(CONFIG_LED
))
187 factory_data_env_config();
192 static void copy_or_generate_uuid(char *fd_ptr
, const char *env_var_name
)
194 char str
[UUID_STR_LEN
+ 1] = { 0 }; /* Enough for UUID stuff */
197 /* Don't use the UUID dest place, as the \0 char won't fit */
198 env
= env_get(env_var_name
);
200 strncpy(str
, env
, UUID_STR_LEN
);
202 gen_rand_uuid_str(str
, UUID_STR_FORMAT_STD
);
204 memcpy(fd_ptr
, str
, UUID_STR_LEN
);
208 * Helper function to provide some sane factory-data values for testing
209 * purpose, when these values are not programmed correctly
211 int do_fd_write(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[])
213 struct factory_data_values
*fd
;
214 struct spi_flash
*sf
;
216 int ret
= CMD_RET_FAILURE
;
218 buf
= malloc(FACTORY_DATA_SECT_SIZE
);
220 printf("F-Data:Unable to allocate buffer\n");
221 return CMD_RET_FAILURE
;
224 sf
= spi_flash_probe(CONFIG_SF_DEFAULT_BUS
,
225 CONFIG_SF_DEFAULT_CS
,
226 CONFIG_SF_DEFAULT_SPEED
,
227 CONFIG_SF_DEFAULT_MODE
);
229 printf("F-Data:Unable to access SPI NOR flash\n");
233 /* Generate the factory-data struct */
235 /* Fist read complete sector into buffer */
236 ret
= spi_flash_read(sf
, FACTORY_DATA_OFFS
, FACTORY_DATA_SECT_SIZE
,
239 printf("F-Data:spi_flash_read failed (%d)\n", ret
);
243 fd
= (struct factory_data_values
*)buf
;
244 fd
->magic
= FACTORY_DATA_MAGIC
;
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 */
254 if (!eth_env_get_enetaddr("ethaddr", fd
->eth_mac
))
255 net_random_ethaddr(fd
->eth_mac
);
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");
261 printf("New factory-data values:\n");
262 printf("wifiaddr=%pM\n", fd
->wifi_mac
);
263 printf("ethaddr=%pM\n", fd
->eth_mac
);
266 * We don't have the \0 char at the end, so we need to specify the
267 * length in the printf format instead
269 printf("linuxmoduleid=%." __stringify(UUID_STR_LEN
) "s\n", fd
->ipr_id
);
270 printf("linuxmodulehqvid=%." __stringify(UUID_STR_LEN
) "s\n",
272 printf("linuxmoduleunielecid=%." __stringify(UUID_STR_LEN
) "s\n",
275 fd
->crc
= crc32(0, (u8
*)&fd
->magic
, FACTORY_DATA_CRC_LEN
);
277 ret
= spi_flash_erase(sf
, FACTORY_DATA_OFFS
, FACTORY_DATA_SECT_SIZE
);
279 printf("F-Data:spi_flash_erase failed (%d)\n", ret
);
283 ret
= spi_flash_write(sf
, FACTORY_DATA_OFFS
, FACTORY_DATA_SECT_SIZE
,
286 printf("F-Data:spi_flash_write failed (%d)\n", ret
);
290 printf("F-Data:factory-data values written to SPI NOR flash\n");
301 #ifndef CONFIG_SPL_BUILD
303 fd_write
, 1, 0, do_fd_write
,
304 "Write test factory-data values to SPI NOR",