]>
git.ipfire.org Git - thirdparty/u-boot.git/blob - board/gardena/smart-gateway-mt7688/board.c
1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2018 Stefan Roese <sr@denx.de>
7 #include <environment.h>
11 #include <spi_flash.h>
13 #include <linux/ctype.h>
16 #define MT76XX_AGPIO_CFG 0x1000003c
18 #define FACTORY_DATA_OFFS 0xc0000
19 #define FACTORY_DATA_SECT_SIZE 0x10000
20 #if ((CONFIG_ENV_OFFSET_REDUND + CONFIG_ENV_SIZE_REDUND) > FACTORY_DATA_OFFS)
21 #error "U-Boot image with environment too big (overlapping with factory-data)!"
23 #define FACTORY_DATA_USER_OFFS 0x140
24 #define FACTORY_DATA_SIZE 0x1f0
25 #define FACTORY_DATA_CRC_LEN (FACTORY_DATA_SIZE - \
26 FACTORY_DATA_USER_OFFS - sizeof(u32))
28 #define FACTORY_DATA_MAGIC 0xCAFEBABE
30 struct factory_data_values
{
32 u8 wifi_mac
[6]; /* offs: 0x004: binary value */
34 u8 eth_mac
[6]; /* offs: 0x028: binary value */
35 u8 pad_3
[FACTORY_DATA_USER_OFFS
- 4 - 6 - 30 - 6];
36 /* User values start here at offset 0x140 */
40 char ipr_id
[UUID_STR_LEN
]; /* UUID as string w/o ending \0 */
41 char hqv_id
[UUID_STR_LEN
]; /* UUID as string w/o ending \0 */
42 char unielec_id
[UUID_STR_LEN
]; /* UUID as string w/o ending \0 */
45 int board_early_init_f(void)
47 void __iomem
*gpio_mode
;
49 /* Configure digital vs analog GPIOs */
50 gpio_mode
= ioremap_nocache(MT76XX_AGPIO_CFG
, 0x100);
51 iowrite32(0x00fe01ff, gpio_mode
);
56 static bool prepare_uuid_var(const char *fd_ptr
, const char *env_var_name
,
59 char str
[UUID_STR_LEN
+ 1] = { 0 }; /* Enough for UUID stuff */
60 bool env_updated
= false;
64 memcpy(str
, fd_ptr
, UUID_STR_LEN
);
66 /* Convert non-ascii character to 'X' */
67 for (i
= 0; i
< UUID_STR_LEN
; i
++) {
68 if (!(isascii(str
[i
]) && isprint(str
[i
])))
72 env
= env_get(env_var_name
);
73 if (strcmp(env
, str
)) {
74 env_set(env_var_name
, str
);
81 static void factory_data_env_config(void)
83 struct factory_data_values
*fd
;
86 char str
[UUID_STR_LEN
+ 1]; /* Enough for UUID stuff */
93 buf
= malloc(FACTORY_DATA_SIZE
);
95 printf("F-Data:Unable to allocate buffer\n");
100 * Get values from factory-data area in SPI NOR
102 sf
= spi_flash_probe(CONFIG_SF_DEFAULT_BUS
,
103 CONFIG_SF_DEFAULT_CS
,
104 CONFIG_SF_DEFAULT_SPEED
,
105 CONFIG_SF_DEFAULT_MODE
);
107 printf("F-Data:Unable to access SPI NOR flash\n");
111 ret
= spi_flash_read(sf
, FACTORY_DATA_OFFS
, FACTORY_DATA_SIZE
,
114 printf("F-Data:Unable to read factory-data from SPI NOR\n");
118 fd
= (struct factory_data_values
*)buf
;
120 if (fd
->magic
!= FACTORY_DATA_MAGIC
)
121 printf("F-Data:Magic value not correct\n");
123 crc
= crc32(0, (u8
*)&fd
->magic
, FACTORY_DATA_CRC_LEN
);
125 printf("F-Data:CRC not correct\n");
127 printf("F-Data:factory-data version %x detected\n",
130 /* Handle wifi_mac env variable */
132 sprintf(str
, "%pM", ptr
);
133 if (!is_valid_ethaddr(ptr
))
134 printf("F-Data:Invalid MAC addr: wifi_mac %s\n", str
);
136 env
= env_get("wifiaddr");
137 if (strcmp(env
, str
)) {
138 env_set("wifiaddr", str
);
142 /* Handle eth_mac env variable */
144 sprintf(str
, "%pM", ptr
);
145 if (!is_valid_ethaddr(ptr
))
146 printf("F-Data:Invalid MAC addr: eth_mac %s\n", str
);
148 env
= env_get("ethaddr");
149 if (strcmp(env
, str
)) {
150 env_set("ethaddr", str
);
154 /* Handle UUID env variables */
155 env_updated
|= prepare_uuid_var(fd
->ipr_id
, "linuxmoduleid", 'X');
156 env_updated
|= prepare_uuid_var(fd
->hqv_id
, "linuxmodulehqvid", '\0');
157 env_updated
|= prepare_uuid_var(fd
->unielec_id
,
158 "linuxmoduleunielecid", '\0');
160 /* Check if the environment was updated and needs to get stored */
161 if (env_updated
!= 0) {
162 printf("F-Data:Values don't match env values -> saving\n");
165 debug("F-Data:Values match current env values\n");
175 int board_late_init(void)
177 if (IS_ENABLED(CONFIG_LED
))
180 factory_data_env_config();
185 static void copy_or_generate_uuid(char *fd_ptr
, const char *env_var_name
)
187 char str
[UUID_STR_LEN
+ 1] = { 0 }; /* Enough for UUID stuff */
190 /* Don't use the UUID dest place, as the \0 char won't fit */
191 env
= env_get(env_var_name
);
193 strncpy(str
, env
, UUID_STR_LEN
);
195 gen_rand_uuid_str(str
, UUID_STR_FORMAT_STD
);
197 memcpy(fd_ptr
, str
, UUID_STR_LEN
);
201 * Helper function to provide some sane factory-data values for testing
202 * purpose, when these values are not programmed correctly
204 int do_fd_write(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
206 struct factory_data_values
*fd
;
207 struct spi_flash
*sf
;
209 int ret
= CMD_RET_FAILURE
;
211 buf
= malloc(FACTORY_DATA_SECT_SIZE
);
213 printf("F-Data:Unable to allocate buffer\n");
214 return CMD_RET_FAILURE
;
217 sf
= spi_flash_probe(CONFIG_SF_DEFAULT_BUS
,
218 CONFIG_SF_DEFAULT_CS
,
219 CONFIG_SF_DEFAULT_SPEED
,
220 CONFIG_SF_DEFAULT_MODE
);
222 printf("F-Data:Unable to access SPI NOR flash\n");
226 /* Generate the factory-data struct */
228 /* Fist read complete sector into buffer */
229 ret
= spi_flash_read(sf
, FACTORY_DATA_OFFS
, FACTORY_DATA_SECT_SIZE
,
232 printf("F-Data:spi_flash_read failed (%d)\n", ret
);
236 fd
= (struct factory_data_values
*)buf
;
237 fd
->magic
= FACTORY_DATA_MAGIC
;
240 /* Use existing MAC and UUID values or generate some random ones */
241 if (!eth_env_get_enetaddr("wifiaddr", fd
->wifi_mac
)) {
242 net_random_ethaddr(fd
->wifi_mac
);
243 /* to get a different seed value for the MAC address */
247 if (!eth_env_get_enetaddr("ethaddr", fd
->eth_mac
))
248 net_random_ethaddr(fd
->eth_mac
);
250 copy_or_generate_uuid(fd
->ipr_id
, "linuxmoduleid");
251 copy_or_generate_uuid(fd
->hqv_id
, "linuxmodulehqvid");
252 copy_or_generate_uuid(fd
->unielec_id
, "linuxmoduleunielecid");
254 printf("New factory-data values:\n");
255 printf("wifiaddr=%pM\n", fd
->wifi_mac
);
256 printf("ethaddr=%pM\n", fd
->eth_mac
);
259 * We don't have the \0 char at the end, so we need to specify the
260 * length in the printf format instead
262 printf("linuxmoduleid=%." __stringify(UUID_STR_LEN
) "s\n", fd
->ipr_id
);
263 printf("linuxmodulehqvid=%." __stringify(UUID_STR_LEN
) "s\n",
265 printf("linuxmoduleunielecid=%." __stringify(UUID_STR_LEN
) "s\n",
268 fd
->crc
= crc32(0, (u8
*)&fd
->magic
, FACTORY_DATA_CRC_LEN
);
270 ret
= spi_flash_erase(sf
, FACTORY_DATA_OFFS
, FACTORY_DATA_SECT_SIZE
);
272 printf("F-Data:spi_flash_erase failed (%d)\n", ret
);
276 ret
= spi_flash_write(sf
, FACTORY_DATA_OFFS
, FACTORY_DATA_SECT_SIZE
,
279 printf("F-Data:spi_flash_write failed (%d)\n", ret
);
283 printf("F-Data:factory-data values written to SPI NOR flash\n");
295 fd_write
, 1, 0, do_fd_write
,
296 "Write test factory-data values to SPI NOR",