]>
git.ipfire.org Git - thirdparty/u-boot.git/blob - board/gardena/smart-gateway-mt7688/board.c
776afa43a66046fdf589d8502652765b022bf045
1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2018 Stefan Roese <sr@denx.de>
8 #include <env_internal.h>
14 #include <spi_flash.h>
15 #include <u-boot/crc.h>
17 #include <linux/ctype.h>
20 #define MT76XX_AGPIO_CFG 0x1000003c
22 #define FACTORY_DATA_OFFS 0xc0000
23 #define FACTORY_DATA_SECT_SIZE 0x10000
24 #if ((CONFIG_ENV_OFFSET_REDUND + CONFIG_ENV_SIZE) > FACTORY_DATA_OFFS)
25 #error "U-Boot image with environment too big (overlapping with factory-data)!"
27 #define FACTORY_DATA_USER_OFFS 0x140
28 #define FACTORY_DATA_SIZE 0x1f0
29 #define FACTORY_DATA_CRC_LEN (FACTORY_DATA_SIZE - \
30 FACTORY_DATA_USER_OFFS - sizeof(u32))
32 #define FACTORY_DATA_MAGIC 0xCAFEBABE
34 struct factory_data_values
{
36 u8 wifi_mac
[6]; /* offs: 0x004: binary value */
38 u8 eth_mac
[6]; /* offs: 0x028: binary value */
39 u8 pad_3
[FACTORY_DATA_USER_OFFS
- 4 - 6 - 30 - 6];
40 /* User values start here at offset 0x140 */
44 char ipr_id
[UUID_STR_LEN
]; /* UUID as string w/o ending \0 */
45 char hqv_id
[UUID_STR_LEN
]; /* UUID as string w/o ending \0 */
46 char unielec_id
[UUID_STR_LEN
]; /* UUID as string w/o ending \0 */
49 int board_early_init_f(void)
51 void __iomem
*gpio_mode
;
53 /* Configure digital vs analog GPIOs */
54 gpio_mode
= ioremap_nocache(MT76XX_AGPIO_CFG
, 0x100);
55 iowrite32(0x00fe01ff, gpio_mode
);
60 static bool prepare_uuid_var(const char *fd_ptr
, const char *env_var_name
,
63 char str
[UUID_STR_LEN
+ 1] = { 0 }; /* Enough for UUID stuff */
64 bool env_updated
= false;
68 memcpy(str
, fd_ptr
, UUID_STR_LEN
);
70 /* Convert non-ascii character to 'X' */
71 for (i
= 0; i
< UUID_STR_LEN
; i
++) {
72 if (!(isascii(str
[i
]) && isprint(str
[i
])))
76 env
= env_get(env_var_name
);
77 if (strcmp(env
, str
)) {
78 env_set(env_var_name
, str
);
85 static void factory_data_env_config(void)
87 struct factory_data_values
*fd
;
90 char str
[UUID_STR_LEN
+ 1]; /* Enough for UUID stuff */
97 buf
= malloc(FACTORY_DATA_SIZE
);
99 printf("F-Data:Unable to allocate buffer\n");
104 * Get values from factory-data area in SPI NOR
106 sf
= spi_flash_probe(CONFIG_SF_DEFAULT_BUS
,
107 CONFIG_SF_DEFAULT_CS
,
108 CONFIG_SF_DEFAULT_SPEED
,
109 CONFIG_SF_DEFAULT_MODE
);
111 printf("F-Data:Unable to access SPI NOR flash\n");
115 ret
= spi_flash_read(sf
, FACTORY_DATA_OFFS
, FACTORY_DATA_SIZE
,
118 printf("F-Data:Unable to read factory-data from SPI NOR\n");
122 fd
= (struct factory_data_values
*)buf
;
124 if (fd
->magic
!= FACTORY_DATA_MAGIC
)
125 printf("F-Data:Magic value not correct\n");
127 crc
= crc32(0, (u8
*)&fd
->magic
, FACTORY_DATA_CRC_LEN
);
129 printf("F-Data:CRC not correct\n");
131 printf("F-Data:factory-data version %x detected\n",
134 /* Handle wifi_mac env variable */
136 sprintf(str
, "%pM", ptr
);
137 if (!is_valid_ethaddr(ptr
))
138 printf("F-Data:Invalid MAC addr: wifi_mac %s\n", str
);
140 env
= env_get("wifiaddr");
141 if (strcmp(env
, str
)) {
142 env_set("wifiaddr", str
);
146 /* Handle eth_mac env variable */
148 sprintf(str
, "%pM", ptr
);
149 if (!is_valid_ethaddr(ptr
))
150 printf("F-Data:Invalid MAC addr: eth_mac %s\n", str
);
152 env
= env_get("ethaddr");
153 if (strcmp(env
, str
)) {
154 env_set("ethaddr", str
);
158 /* Handle UUID env variables */
159 env_updated
|= prepare_uuid_var(fd
->ipr_id
, "linuxmoduleid", 'X');
160 env_updated
|= prepare_uuid_var(fd
->hqv_id
, "linuxmodulehqvid", '\0');
161 env_updated
|= prepare_uuid_var(fd
->unielec_id
,
162 "linuxmoduleunielecid", '\0');
164 /* Check if the environment was updated and needs to get stored */
165 if (env_updated
!= 0) {
166 printf("F-Data:Values don't match env values -> saving\n");
169 debug("F-Data:Values match current env values\n");
179 int board_late_init(void)
181 if (IS_ENABLED(CONFIG_LED
))
184 factory_data_env_config();
189 static void copy_or_generate_uuid(char *fd_ptr
, const char *env_var_name
)
191 char str
[UUID_STR_LEN
+ 1] = { 0 }; /* Enough for UUID stuff */
194 /* Don't use the UUID dest place, as the \0 char won't fit */
195 env
= env_get(env_var_name
);
197 strncpy(str
, env
, UUID_STR_LEN
);
199 gen_rand_uuid_str(str
, UUID_STR_FORMAT_STD
);
201 memcpy(fd_ptr
, str
, UUID_STR_LEN
);
205 * Helper function to provide some sane factory-data values for testing
206 * purpose, when these values are not programmed correctly
208 int do_fd_write(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
210 struct factory_data_values
*fd
;
211 struct spi_flash
*sf
;
213 int ret
= CMD_RET_FAILURE
;
215 buf
= malloc(FACTORY_DATA_SECT_SIZE
);
217 printf("F-Data:Unable to allocate buffer\n");
218 return CMD_RET_FAILURE
;
221 sf
= spi_flash_probe(CONFIG_SF_DEFAULT_BUS
,
222 CONFIG_SF_DEFAULT_CS
,
223 CONFIG_SF_DEFAULT_SPEED
,
224 CONFIG_SF_DEFAULT_MODE
);
226 printf("F-Data:Unable to access SPI NOR flash\n");
230 /* Generate the factory-data struct */
232 /* Fist read complete sector into buffer */
233 ret
= spi_flash_read(sf
, FACTORY_DATA_OFFS
, FACTORY_DATA_SECT_SIZE
,
236 printf("F-Data:spi_flash_read failed (%d)\n", ret
);
240 fd
= (struct factory_data_values
*)buf
;
241 fd
->magic
= FACTORY_DATA_MAGIC
;
244 /* Use existing MAC and UUID values or generate some random ones */
245 if (!eth_env_get_enetaddr("wifiaddr", fd
->wifi_mac
)) {
246 net_random_ethaddr(fd
->wifi_mac
);
247 /* to get a different seed value for the MAC address */
251 if (!eth_env_get_enetaddr("ethaddr", fd
->eth_mac
))
252 net_random_ethaddr(fd
->eth_mac
);
254 copy_or_generate_uuid(fd
->ipr_id
, "linuxmoduleid");
255 copy_or_generate_uuid(fd
->hqv_id
, "linuxmodulehqvid");
256 copy_or_generate_uuid(fd
->unielec_id
, "linuxmoduleunielecid");
258 printf("New factory-data values:\n");
259 printf("wifiaddr=%pM\n", fd
->wifi_mac
);
260 printf("ethaddr=%pM\n", fd
->eth_mac
);
263 * We don't have the \0 char at the end, so we need to specify the
264 * length in the printf format instead
266 printf("linuxmoduleid=%." __stringify(UUID_STR_LEN
) "s\n", fd
->ipr_id
);
267 printf("linuxmodulehqvid=%." __stringify(UUID_STR_LEN
) "s\n",
269 printf("linuxmoduleunielecid=%." __stringify(UUID_STR_LEN
) "s\n",
272 fd
->crc
= crc32(0, (u8
*)&fd
->magic
, FACTORY_DATA_CRC_LEN
);
274 ret
= spi_flash_erase(sf
, FACTORY_DATA_OFFS
, FACTORY_DATA_SECT_SIZE
);
276 printf("F-Data:spi_flash_erase failed (%d)\n", ret
);
280 ret
= spi_flash_write(sf
, FACTORY_DATA_OFFS
, FACTORY_DATA_SECT_SIZE
,
283 printf("F-Data:spi_flash_write failed (%d)\n", ret
);
287 printf("F-Data:factory-data values written to SPI NOR flash\n");
298 #ifndef CONFIG_SPL_BUILD
300 fd_write
, 1, 0, do_fd_write
,
301 "Write test factory-data values to SPI NOR",