1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2014      Panasonic Corporation
4  * Copyright (C) 2015-2016 Socionext Inc.
5  *   Author: Masahiro Yamada <yamada.masahiro@socionext.com>
6  */
7 
8 #include <common.h>
9 #include <spl.h>
10 #include <linux/libfdt.h>
11 #include <nand.h>
12 #include <stdio.h>
13 #include <linux/io.h>
14 #include <linux/printk.h>
15 #include <../drivers/mtd/nand/denali.h>
16 
17 #include "init.h"
18 
19 static void nand_denali_wp_disable(void)
20 {
21 #ifdef CONFIG_NAND_DENALI
22 	/*
23 	 * Since the boot rom enables the write protection for NAND boot mode,
24 	 * it must be disabled somewhere for "nand write", "nand erase", etc.
25 	 * The workaround is here to not disturb the Denali NAND controller
26 	 * driver just for a really SoC-specific thing.
27 	 */
28 	void __iomem *denali_reg = (void __iomem *)CONFIG_SYS_NAND_REGS_BASE;
29 
30 	writel(WRITE_PROTECT__FLAG, denali_reg + WRITE_PROTECT);
31 #endif
32 }
33 
34 static int uniphier_set_fdt_file(void)
35 {
36 	DECLARE_GLOBAL_DATA_PTR;
37 	const char *compat;
38 	char dtb_name[256];
39 	int buf_len = sizeof(dtb_name);
40 
41 	if (env_get("fdtfile"))
42 		return 0;	/* do nothing if it is already set */
43 
44 	compat = fdt_stringlist_get(gd->fdt_blob, 0, "compatible", 0, NULL);
45 	if (!compat)
46 		return -EINVAL;
47 
48 	/* rip off the vendor prefix "socionext,"  */
49 	compat = strchr(compat, ',');
50 	if (!compat)
51 		return -EINVAL;
52 	compat++;
53 
54 	strncpy(dtb_name, compat, buf_len);
55 	buf_len -= strlen(compat);
56 
57 	strncat(dtb_name, ".dtb", buf_len);
58 
59 	return env_set("fdtfile", dtb_name);
60 }
61 
62 int board_late_init(void)
63 {
64 	puts("MODE:  ");
65 
66 	switch (uniphier_boot_device_raw()) {
67 	case BOOT_DEVICE_MMC1:
68 		printf("eMMC Boot");
69 		env_set("bootmode", "emmcboot");
70 		break;
71 	case BOOT_DEVICE_NAND:
72 		printf("NAND Boot");
73 		env_set("bootmode", "nandboot");
74 		nand_denali_wp_disable();
75 		break;
76 	case BOOT_DEVICE_NOR:
77 		printf("NOR Boot");
78 		env_set("bootmode", "norboot");
79 		break;
80 	case BOOT_DEVICE_USB:
81 		printf("USB Boot");
82 		env_set("bootmode", "usbboot");
83 		break;
84 	default:
85 		printf("Unknown");
86 		break;
87 	}
88 
89 	if (uniphier_have_internal_stm())
90 		printf(" (STM: %s)",
91 		       uniphier_boot_from_backend() ? "OFF" : "ON");
92 
93 	printf("\n");
94 
95 	if (uniphier_set_fdt_file())
96 		pr_warn("fdt_file environment was not set correctly\n");
97 
98 	return 0;
99 }
100