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