• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * hi3518ev300.c
3  *
4  * The board init for hisilicon
5  *
6  * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
7  *
8  * This program is free software; you can redistribute  it and/or modify it
9  * under  the terms of  the GNU General  Public License as published by the
10  * Free Software Foundation;  either version 2 of the  License, or (at your
11  * option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  *
21  */
22 #include <config.h>
23 #include <common.h>
24 #include <asm/io.h>
25 #include <asm/arch/platform.h>
26 #include <spi_flash.h>
27 #include <linux/mtd/mtd.h>
28 #include <nand.h>
29 #include <netdev.h>
30 #include <mmc.h>
31 #include <asm/sections.h>
32 #include <sdhci.h>
33 #include <hicpu_common.h>
34 #include <asm/mach-types.h>
35 #ifdef CONFIG_AUTO_OTA_UPDATE
36 #include <fat.h>
37 #endif /* CONFIG_AUTO_OTA_UPDATE */
38 
39 #ifndef CONFIG_SYS_DCACHE_OFF
enable_caches(void)40 void enable_caches(void)
41 {
42 	/* Enable D-cache. I-cache is already enabled in start.S */
43 	dcache_enable();
44 }
45 #endif
46 static int boot_media = BOOT_MEDIA_UNKNOWN;
get_boot_media(void)47 int get_boot_media(void)
48 {
49 	unsigned int reg_val, boot_mode, spi_device_mode;
50 	int boot_media = BOOT_MEDIA_UNKNOWN;
51 
52 	reg_val = readl(SYS_CTRL_REG_BASE + REG_SYSSTAT);
53 	boot_mode = get_sys_boot_mode(reg_val);
54 
55 	switch (boot_mode) {
56 	case BOOT_FROM_SPI:
57 		spi_device_mode = get_spi_device_type(reg_val);
58 		if (spi_device_mode)
59 			boot_media = BOOT_MEDIA_NAND;
60 		else
61 			boot_media = BOOT_MEDIA_SPIFLASH;
62 		break;
63 	case BOOT_FROM_EMMC:
64 		boot_media = BOOT_MEDIA_EMMC;
65 		break;
66 	default:
67 		boot_media = BOOT_MEDIA_UNKNOWN;
68 		break;
69 	}
70 
71 	return boot_media;
72 }
73 
74 #if defined(CONFIG_SHOW_BOOT_PROGRESS)
show_boot_progress(int progress)75 void show_boot_progress(int progress)
76 {
77 	printf("Boot reached stage %d\n", progress);
78 }
79 #endif
80 
81 #define COMP_MODE_ENABLE ((unsigned int)0x0000EAEF)
82 
delay(unsigned long loops)83 static inline void delay(unsigned long loops)
84 {
85 	__asm__ volatile("1:\n"
86 			"subs %0, %1, #1\n"
87 			"bne 1b" : "=r"(loops) : "0"(loops));
88 }
89 
90 
get_text_base(void)91 int get_text_base(void)
92 {
93 	return CONFIG_SYS_TEXT_BASE;
94 }
95 
boot_flag_init(void)96 static void boot_flag_init(void)
97 {
98 	unsigned int reg, boot_mode, spi_device_mode;
99 
100 	/* get boot mode */
101 	reg = __raw_readl(SYS_CTRL_REG_BASE + REG_SYSSTAT);
102 	boot_mode = get_sys_boot_mode(reg);
103 
104 	switch (boot_mode) {
105 	case BOOT_FROM_SPI:
106 		spi_device_mode = get_spi_device_type(reg);
107 		if (spi_device_mode)
108 			boot_media = BOOT_MEDIA_NAND;
109 		else
110 			boot_media = BOOT_MEDIA_SPIFLASH;
111 		break;
112 	case BOOT_FROM_EMMC:    /* emmc mode */
113 		boot_media = BOOT_MEDIA_EMMC;
114 		break;
115 	default:
116 		boot_media = BOOT_MEDIA_UNKNOWN;
117 		break;
118 	}
119 }
120 
board_early_init_f(void)121 int board_early_init_f(void)
122 {
123 	return 0;
124 }
125 
126 #define UBOOT_DATA_ADDR     0x41000000UL
127 #define UBOOT_DATA_SIZE     0x80000UL
data_to_spiflash(void)128 int data_to_spiflash(void)
129 {
130 	static struct spi_flash *flash = NULL;
131 	void *buf = NULL;
132 
133 	unsigned int val;
134 
135 	/* 0:bus; 0:cs; 1000000:max_hz; 0x3:spi_mode */
136 	flash = spi_flash_probe(0, 0, 1000000, 0x3);
137 	if (!flash) {
138 		printf("Failed to initialize SPI flash\n");
139 		return -1;  /* -1:failed */
140 	}
141 
142 	/* erase the address range. */
143 	printf("Spi flash erase...\n");
144 	val = spi_flash_erase(flash, NUM_0, UBOOT_DATA_SIZE);
145 	if (val) {
146 		printf("SPI flash sector erase failed\n");
147 		return 1; /* 1:failed */
148 	}
149 
150 	buf = map_physmem((unsigned long)UBOOT_DATA_ADDR,
151 			UBOOT_DATA_SIZE, MAP_WRBACK);
152 	if (!buf) {
153 		puts("Failed to map physical memory\n");
154 		return 1; /* 1:failed */
155 	}
156 
157 	/* copy the data from RAM to FLASH */
158 	printf("Spi flash write...\n");
159 	val = flash->write(flash, NUM_0, UBOOT_DATA_SIZE, buf);
160 	if (val) {
161 		printf("SPI flash write failed, return %u\n",
162 				val);
163 		unmap_physmem(buf, UBOOT_DATA_SIZE);
164 		return 1; /* 1:failed */
165 	}
166 
167 	unmap_physmem(buf, UBOOT_DATA_SIZE);
168 	return 0; /* 0:success */
169 }
170 
data_to_nandflash(void)171 int data_to_nandflash(void)
172 {
173 	struct mtd_info *nand_flash = NULL;
174 	void *buf = NULL;
175 	size_t length = UBOOT_DATA_SIZE;
176 	unsigned int val;
177 
178 	nand_flash = nand_info[0];
179 
180 	printf("Nand flash erase...\n");
181 	val = nand_erase(nand_flash, 0, UBOOT_DATA_SIZE);
182 	if (val) {
183 		printf("Nand flash erase failed\n");
184 		return 1;
185 	}
186 
187 	buf = map_physmem((unsigned long)UBOOT_DATA_ADDR,
188 			UBOOT_DATA_SIZE, MAP_WRBACK);
189 	if (!buf) {
190 		puts("Failed to map physical memory\n");
191 		return 1;
192 	}
193 
194 	printf("Nand flash write...\n");
195 	val = nand_write(nand_flash, 0, &length, buf);
196 	if (val) {
197 		printf("Nand flash write failed, return %u\n",
198 				val);
199 		unmap_physmem(buf, UBOOT_DATA_SIZE);
200 		return 1;
201 	}
202 
203 	unmap_physmem(buf, UBOOT_DATA_SIZE);
204 	return 0;
205 }
206 
data_to_emmc(void)207 int data_to_emmc(void)
208 {
209 	struct mmc *mmc = find_mmc_device(0);
210 	void *buf = NULL;
211 
212 	if (!mmc)
213 		return 1;
214 
215 	(void)mmc_init(mmc);
216 
217 	buf = map_physmem((unsigned long)UBOOT_DATA_ADDR,
218 			UBOOT_DATA_SIZE, MAP_WRBACK);
219 	if (!buf) {
220 		puts("Failed to map physical memory\n");
221 		return 1;
222 	}
223 
224 	printf("MMC write...\n");
225 	blk_dwrite(mmc_get_blk_desc(mmc), 0, (UBOOT_DATA_SIZE >> NUM_9), buf);
226 	unmap_physmem(buf, UBOOT_DATA_SIZE);
227 	return 0;
228 }
save_bootdata_to_flash(void)229 int save_bootdata_to_flash(void)
230 {
231 	unsigned int sd_update_flag = 0;
232 	int ret = 0;
233 	sd_update_flag = readl(REG_BASE_SCTL + REG_SC_GEN4);
234 	if (sd_update_flag == START_MAGIC) {
235 #if defined(CONFIG_HIFMC)
236 		if (boot_media == BOOT_MEDIA_SPIFLASH) {
237 			ret = data_to_spiflash();
238 			if (ret != 0)
239 				return ret;
240 		}
241 		if (boot_media == BOOT_MEDIA_NAND) {
242 			ret = data_to_nandflash();
243 			if (ret != 0)
244 				return ret;
245 		}
246 #endif
247 #if defined(CONFIG_SUPPORT_EMMC_BOOT)
248 		if (boot_media == BOOT_MEDIA_EMMC) {
249 			ret = data_to_emmc();
250 			if (ret != 0)
251 				return ret;
252 		}
253 #endif
254 
255 		printf("update success!\n");
256 	}
257 
258 	return 0;
259 }
260 
261 int auto_update_flag = 0;
262 int bare_chip_program = 0;
263 
264 #define REG_BASE_GPIO0          0x120b0000
265 #define GPIO0_0_DATA_OFST       0x4
266 #define GPIO_DIR_OFST       0x400
267 
268 /** upgrade status register address */
269 #define UPGRADE_STATUS_REG_ADDR 0x120F0048
270 
271 typedef enum tagUPGRADE_STATUS_E {
272 	UPGRADE_STATUS_IDLE = 0,
273 	UPGRADE_STATUS_PROCESSING,
274 	UPGRADE_STATUS_FINISH,
275 	UPGRADE_STATUS_BUTT
276 } upgrade_status_e;
277 
is_bare_program(void)278 int is_bare_program(void)
279 {
280 	return 1;
281 }
282 
283 #ifdef CONFIG_AUTO_OTA_UPDATE
is_mmc_valid(void)284 static bool is_mmc_valid(void)
285 {
286 	struct mmc *mmc = NULL;
287 	int dev_num = -1;
288 
289 #ifdef CONFIG_EMMC
290 	dev_num = 1;
291 #else
292 	dev_num = 0;
293 #endif
294 
295 	mmc = find_mmc_device(dev_num);
296 	if (mmc == NULL) {
297 		printf("No mmc %d driver found!\n", dev_num);
298 		return false;
299 	}
300 
301 	if (((unsigned long)mmc->block_dev.vendor[0] == 0) ||
302 		((unsigned long)mmc->block_dev.product[0] == 0)) {
303 		printf("No SD card found!\n");
304 		return false;
305 	}
306 	return true;
307 }
308 
is_ota_tag_valid(const char * path)309 static bool is_ota_tag_valid(const char *path)
310 {
311 	char buf[64] = {0}; /* 32bytes for max in OTA_TAG_FILE */
312 	const char *info = "package_type:ota";
313 	const int len = strlen(info);
314 	long sz = file_fat_read(path, (void *)buf, sizeof(buf));
315 
316 	if (sz < len) {
317 		printf("%s: not exist, or len %ld invalid\n", path, sz);
318 		return false;
319 	}
320 
321 	if (strncmp(info, buf, len) != 0) {
322 		printf("%s info invalid\n", path);
323 		return false;
324 	}
325 	return true;
326 }
327 
is_ota(void)328 static bool is_ota(void)
329 {
330 	char name[] = "mmc";
331 	int dev = 0;
332 	struct blk_desc *stor_dev = NULL;
333 	bool valid = false;
334 
335 	if (!is_mmc_valid()) {
336 		printf("MMC not valid\n");
337 		return false;
338 	}
339 
340 	stor_dev = blk_get_dev(name, dev);
341 	if (stor_dev == NULL) {
342 		printf("Unknow device type!\n");
343 		return false;
344 	}
345 
346 	if (fat_register_device(stor_dev, 1) != 0) {
347 		printf("Unable to use %s for fat\n", name);
348 		return false;
349 	}
350 
351 	if (file_fat_detectfs() != 0) {
352 		printf("Fat-detectfs failed\n");
353 		return false;
354 	}
355 
356 	valid = is_ota_tag_valid("/update/OTA.tag");
357 	printf("OTA.tag valid %d\n", valid);
358 	return valid;
359 }
360 #endif /* CONFIG_AUTO_OTA_UPDATE */
361 
is_auto_update(void)362 int is_auto_update(void)
363 {
364 #ifdef CONFIG_AUTO_OTA_UPDATE
365 	if (is_ota())
366 		return 1;
367 #else
368 #if (defined CONFIG_AUTO_SD_UPDATE) || (defined CONFIG_AUTO_USB_UPDATE)
369 	/* to add some judgement if neccessary */
370 	unsigned int  val[NUM_3];
371 
372 	unsigned int *puregval = (unsigned int *)UPGRADE_STATUS_REG_ADDR;
373 	if (((*puregval) & (UPGRADE_STATUS_PROCESSING)) != 0)
374 		return 1; /* update enable */
375 
376 	writel(0, REG_BASE_GPIO0 + GPIO_DIR_OFST);
377 
378 	val[NUM_0] = readl(REG_BASE_GPIO0 + GPIO0_0_DATA_OFST);
379 	if (val[NUM_0])
380 		return 0;
381 
382 	udelay(10000); /* delay 10000 us */
383 	val[NUM_1] = readl(REG_BASE_GPIO0 + GPIO0_0_DATA_OFST);
384 	udelay(10000); /* delay 10000 us */
385 	val[NUM_2] = readl(REG_BASE_GPIO0 + GPIO0_0_DATA_OFST);
386 	udelay(10000); /* delay 10000 us */
387 
388 	if (val[NUM_0] == val[NUM_1] && val[NUM_1] == val[NUM_2] && val[NUM_0] == NUM_0)
389 		return 1;    /* update enable */
390 	else
391 		return 0;
392 #endif
393 #endif /* CONFIG_AUTO_OTA_UPDATE */
394 	return 0;
395 }
396 
select_upgrade_dev(void)397 void select_upgrade_dev(void)
398 {
399 #ifdef CONFIG_EMMC
400 	unsigned int uval = readl(UPGRADE_STATUS_REG_ADDR);
401 	unsigned int stor_dev = 0;
402 	unsigned int stor_paration = 0;
403 	stor_dev = (uval >> 16) & 0x03; /* 16,0x03: store dev */
404 	stor_paration = (uval >> 18) & 0x1f; /* 18,0x1f: store paration */
405 	if (((uval) & (UPGRADE_STATUS_PROCESSING)) != NUM_0) {
406 		/* the upgrade startup by linux application */
407 		if (stor_dev == NUM_0) {
408 			/* emmc */
409 			target_dev = NUM_0;
410 			target_paratition = stor_paration;
411 		} else {
412 			/* sd */
413 			target_dev = NUM_1;
414 		}
415 	} else {
416 		/* the upgrade startup by upgrade key on the board, os defaule upgrade style */
417 		target_dev = NUM_1;
418 	}
419 
420 	printf("update dev is %d: paratition is %d\n", target_dev, target_paratition);
421 #endif
422 	return;
423 }
424 
misc_init_r(void)425 int misc_init_r(void)
426 {
427 #ifdef CONFIG_RANDOM_ETHADDR
428 	random_init_r();
429 #endif
430 	env_set("verify", "n");
431 
432 #if (CONFIG_AUTO_UPDATE == 1)
433 	/* auto update flag */
434 	if (is_auto_update())
435 		auto_update_flag = NUM_1;
436 	else
437 		auto_update_flag = NUM_0;
438 
439 	/* bare chip program flag */
440 	if (is_bare_program())
441 		bare_chip_program = NUM_1;
442 	else
443 		bare_chip_program = NUM_0;
444 
445 #ifdef CFG_MMU_HANDLEOK
446 	dcache_stop();
447 #endif
448 
449 #ifdef CFG_MMU_HANDLEOK
450 	dcache_start();
451 #endif
452 
453 #endif
454 
455 #if (CONFIG_AUTO_UPDATE == 1)
456 	int update_flag = -1; /* -1:default failed */
457 	if (auto_update_flag) {
458 		select_upgrade_dev();
459 		update_flag = do_auto_update();
460 		unsigned int *puregval = (unsigned int *)UPGRADE_STATUS_REG_ADDR;
461 		if (((*puregval) & (UPGRADE_STATUS_PROCESSING)) && (update_flag == NUM_0)) {
462 			printf("upgrade status: finish\n");
463 			*puregval = UPGRADE_STATUS_FINISH;
464 		}
465 	}
466 	if (bare_chip_program && !auto_update_flag)
467 		save_bootdata_to_flash();
468 	if (update_flag == NUM_0)
469 		do_reset(NULL, 0, 0, NULL);
470 #endif
471 	return 0;
472 }
473 
board_init(void)474 int board_init(void)
475 {
476 	DECLARE_GLOBAL_DATA_PTR;
477 
478 	gd->bd->bi_arch_number = MACH_TYPE_HI3518EV300;
479 	gd->bd->bi_boot_params = CFG_BOOT_PARAMS;
480 
481 	boot_flag_init();
482 
483 	return 0;
484 }
485 
dram_init(void)486 int dram_init(void)
487 {
488 	DECLARE_GLOBAL_DATA_PTR;
489 
490 	gd->ram_size = PHYS_SDRAM_1_SIZE;
491 	return 0;
492 }
493 
reset_cpu(ulong addr)494 void reset_cpu(ulong addr)
495 {
496 	/* 0x12345678:writing any value will cause a reset. */
497 	writel(0x12345678, REG_BASE_SCTL + REG_SC_SYSRES);
498 	while (1);
499 }
500 
timer_init(void)501 int timer_init(void)
502 {
503 	/*
504 	 * Under uboot, 0xffffffff is set to load register,
505 	 * timer_clk equals BUSCLK/2/256.
506 	 * e.g. BUSCLK equals 50M, it will roll back after 0xffffffff/timer_clk
507 	 * 43980s equals 12hours
508 	 */
509 	__raw_writel(0, CFG_TIMERBASE + REG_TIMER_CONTROL);
510 	__raw_writel(~0, CFG_TIMERBASE + REG_TIMER_RELOAD);
511 
512 	/* 32 bit, periodic */
513 	__raw_writel(CFG_TIMER_CTRL, CFG_TIMERBASE + REG_TIMER_CONTROL);
514 
515 	return 0;
516 }
517 
board_eth_init(bd_t * bis)518 int board_eth_init(bd_t *bis)
519 {
520 	int rc = 0;
521 
522 #ifdef CONFIG_HISFV300_ETH
523 	rc = hieth_initialize(bis);
524 #endif
525 	return rc;
526 }
527 
528 #ifdef CONFIG_GENERIC_MMC
board_mmc_init(bd_t * bis)529 int board_mmc_init(bd_t *bis)
530 {
531 	int ret = 0;
532 
533 #ifdef CONFIG_HISI_SDHCI
534 
535 #ifndef CONFIG_HIFMC
536 	ret = hisi_sdhci_add_port(0, EMMC_BASE_REG, MMC_TYPE_MMC);
537 	if (!ret) {
538 		ret = hisi_mmc_init(0);
539 		if (ret)
540 			printf("No EMMC device found !\n");
541 	}
542 #else
543 
544 #ifdef CONFIG_AUTO_SD_UPDATE
545 	ret = hisi_sdhci_add_port(0, SDIO0_BASE_REG, MMC_TYPE_SD);
546 	if (ret)
547 		return ret;
548 
549 	ret = hisi_mmc_init(0);
550 	if (ret)
551 		printf("No SD device found !\n");
552 #endif
553 
554 #endif
555 #endif
556 
557 	return ret;
558 }
559 #endif
560 #ifdef CONFIG_ARMV7_NONSEC
smp_set_core_boot_addr(unsigned long addr,int corenr)561 void smp_set_core_boot_addr(unsigned long addr, int corenr)
562 {
563 }
564 
smp_kick_all_cpus(void)565 void smp_kick_all_cpus(void)
566 {
567 }
568 
smp_waitloop(unsigned previous_address)569 void smp_waitloop(unsigned previous_address)
570 {
571 }
572 #endif
573 
574