xref: /openbmc/u-boot/arch/arm/mach-rockchip/rk322x-board.c (revision e5fd39c886485e3dec77f4438a6e364c2987cf5f)
183d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
2168eef7aSKever Yang /*
3168eef7aSKever Yang  * (C) Copyright 2017 Rockchip Electronics Co., Ltd.
4168eef7aSKever Yang  */
5168eef7aSKever Yang #include <common.h>
6168eef7aSKever Yang #include <clk.h>
7168eef7aSKever Yang #include <dm.h>
8168eef7aSKever Yang #include <ram.h>
9c964a0dcSKever Yang #include <syscon.h>
10168eef7aSKever Yang #include <asm/io.h>
11*c35f8e50SSimon Glass #include <asm/arch/boot_mode.h>
12168eef7aSKever Yang #include <asm/arch/clock.h>
13168eef7aSKever Yang #include <asm/arch/periph.h>
14168eef7aSKever Yang #include <asm/arch/grf_rk322x.h>
15168eef7aSKever Yang 
16168eef7aSKever Yang DECLARE_GLOBAL_DATA_PTR;
17168eef7aSKever Yang 
rk_board_late_init(void)18168eef7aSKever Yang __weak int rk_board_late_init(void)
19168eef7aSKever Yang {
20168eef7aSKever Yang 	return 0;
21168eef7aSKever Yang }
22168eef7aSKever Yang 
board_late_init(void)23168eef7aSKever Yang int board_late_init(void)
24168eef7aSKever Yang {
25168eef7aSKever Yang 	setup_boot_mode();
26168eef7aSKever Yang 
27168eef7aSKever Yang 	return rk_board_late_init();
28168eef7aSKever Yang }
29168eef7aSKever Yang 
board_init(void)30168eef7aSKever Yang int board_init(void)
31168eef7aSKever Yang {
32168eef7aSKever Yang #include <asm/arch/grf_rk322x.h>
33168eef7aSKever Yang 	/* Enable early UART2 channel 1 on the RK322x */
34168eef7aSKever Yang #define GRF_BASE	0x11000000
35168eef7aSKever Yang 	struct rk322x_grf * const grf = (void *)GRF_BASE;
36424324d3SDavid Wu 	enum {
37424324d3SDavid Wu 		GPIO1B2_SHIFT		= 4,
38424324d3SDavid Wu 		GPIO1B2_MASK		= 3 << GPIO1B2_SHIFT,
39424324d3SDavid Wu 		GPIO1B2_GPIO		= 0,
40424324d3SDavid Wu 		GPIO1B2_UART21_SIN,
41424324d3SDavid Wu 
42424324d3SDavid Wu 		GPIO1B1_SHIFT		= 2,
43424324d3SDavid Wu 		GPIO1B1_MASK		= 3 << GPIO1B1_SHIFT,
44424324d3SDavid Wu 		GPIO1B1_GPIO            = 0,
45424324d3SDavid Wu 		GPIO1B1_UART1_SOUT,
46424324d3SDavid Wu 		GPIO1B1_UART21_SOUT,
47424324d3SDavid Wu 	};
48424324d3SDavid Wu 	enum {
49424324d3SDavid Wu 		CON_IOMUX_UART2SEL_SHIFT= 8,
50424324d3SDavid Wu 		CON_IOMUX_UART2SEL_MASK	= 1 << CON_IOMUX_UART2SEL_SHIFT,
51424324d3SDavid Wu 		CON_IOMUX_UART2SEL_2	= 0,
52424324d3SDavid Wu 		CON_IOMUX_UART2SEL_21,
53424324d3SDavid Wu 	};
54168eef7aSKever Yang 
55168eef7aSKever Yang 	rk_clrsetreg(&grf->gpio1b_iomux,
56168eef7aSKever Yang 		     GPIO1B1_MASK | GPIO1B2_MASK,
57168eef7aSKever Yang 		     GPIO1B2_UART21_SIN << GPIO1B2_SHIFT |
58168eef7aSKever Yang 		     GPIO1B1_UART21_SOUT << GPIO1B1_SHIFT);
59168eef7aSKever Yang 	/* Set channel C as UART2 input */
60168eef7aSKever Yang 	rk_clrsetreg(&grf->con_iomux,
61168eef7aSKever Yang 		     CON_IOMUX_UART2SEL_MASK,
62168eef7aSKever Yang 		     CON_IOMUX_UART2SEL_21 << CON_IOMUX_UART2SEL_SHIFT);
63168eef7aSKever Yang 
644c94aacdSDavid Wu 	/*
654c94aacdSDavid Wu 	* The integrated macphy is enabled by default, disable it
664c94aacdSDavid Wu 	* for saving power consuming.
674c94aacdSDavid Wu 	*/
684c94aacdSDavid Wu 	rk_clrsetreg(&grf->macphy_con[0],
694c94aacdSDavid Wu 		     MACPHY_CFG_ENABLE_MASK,
704c94aacdSDavid Wu 		     0 << MACPHY_CFG_ENABLE_SHIFT);
714c94aacdSDavid Wu 
72168eef7aSKever Yang 	return 0;
73168eef7aSKever Yang }
74168eef7aSKever Yang 
dram_init_banksize(void)75168eef7aSKever Yang int dram_init_banksize(void)
76168eef7aSKever Yang {
7744c5ba55SKever Yang 	gd->bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE;
78168eef7aSKever Yang 	gd->bd->bi_dram[0].size = 0x8400000;
7944c5ba55SKever Yang 	/* Reserve 0x200000 for OPTEE */
8044c5ba55SKever Yang 	gd->bd->bi_dram[1].start = CONFIG_SYS_SDRAM_BASE
8144c5ba55SKever Yang 				+ gd->bd->bi_dram[0].size + 0x200000;
8244c5ba55SKever Yang 	gd->bd->bi_dram[1].size = gd->bd->bi_dram[0].start
8344c5ba55SKever Yang 				+ gd->ram_size - gd->bd->bi_dram[1].start;
84168eef7aSKever Yang 
85168eef7aSKever Yang 	return 0;
86168eef7aSKever Yang }
87168eef7aSKever Yang 
88168eef7aSKever Yang #ifndef CONFIG_SYS_DCACHE_OFF
enable_caches(void)89168eef7aSKever Yang void enable_caches(void)
90168eef7aSKever Yang {
91168eef7aSKever Yang 	/* Enable D-cache. I-cache is already enabled in start.S */
92168eef7aSKever Yang 	dcache_enable();
93168eef7aSKever Yang }
94168eef7aSKever Yang #endif
95168eef7aSKever Yang 
96168eef7aSKever Yang #if defined(CONFIG_USB_GADGET) && defined(CONFIG_USB_GADGET_DWC2_OTG)
97168eef7aSKever Yang #include <usb.h>
98168eef7aSKever Yang #include <usb/dwc2_udc.h>
99168eef7aSKever Yang 
100168eef7aSKever Yang static struct dwc2_plat_otg_data rk322x_otg_data = {
101168eef7aSKever Yang 	.rx_fifo_sz	= 512,
102168eef7aSKever Yang 	.np_tx_fifo_sz	= 16,
103168eef7aSKever Yang 	.tx_fifo_sz	= 128,
104168eef7aSKever Yang };
105168eef7aSKever Yang 
board_usb_init(int index,enum usb_init_type init)106168eef7aSKever Yang int board_usb_init(int index, enum usb_init_type init)
107168eef7aSKever Yang {
108168eef7aSKever Yang 	int node;
109168eef7aSKever Yang 	const char *mode;
110168eef7aSKever Yang 	bool matched = false;
111168eef7aSKever Yang 	const void *blob = gd->fdt_blob;
112168eef7aSKever Yang 
113168eef7aSKever Yang 	/* find the usb_otg node */
114168eef7aSKever Yang 	node = fdt_node_offset_by_compatible(blob, -1,
115168eef7aSKever Yang 					"rockchip,rk3288-usb");
116168eef7aSKever Yang 
117168eef7aSKever Yang 	while (node > 0) {
118168eef7aSKever Yang 		mode = fdt_getprop(blob, node, "dr_mode", NULL);
119168eef7aSKever Yang 		if (mode && strcmp(mode, "otg") == 0) {
120168eef7aSKever Yang 			matched = true;
121168eef7aSKever Yang 			break;
122168eef7aSKever Yang 		}
123168eef7aSKever Yang 
124168eef7aSKever Yang 		node = fdt_node_offset_by_compatible(blob, node,
125168eef7aSKever Yang 					"rockchip,rk3288-usb");
126168eef7aSKever Yang 	}
127168eef7aSKever Yang 	if (!matched) {
128168eef7aSKever Yang 		debug("Not found usb_otg device\n");
129168eef7aSKever Yang 		return -ENODEV;
130168eef7aSKever Yang 	}
131168eef7aSKever Yang 	rk322x_otg_data.regs_otg = fdtdec_get_addr(blob, node, "reg");
132168eef7aSKever Yang 
133168eef7aSKever Yang 	return dwc2_udc_probe(&rk322x_otg_data);
134168eef7aSKever Yang }
135168eef7aSKever Yang 
board_usb_cleanup(int index,enum usb_init_type init)136168eef7aSKever Yang int board_usb_cleanup(int index, enum usb_init_type init)
137168eef7aSKever Yang {
138168eef7aSKever Yang 	return 0;
139168eef7aSKever Yang }
140168eef7aSKever Yang #endif
141c964a0dcSKever Yang 
1428a65bd63SAlex Kiernan #if CONFIG_IS_ENABLED(FASTBOOT)
fastboot_set_reboot_flag(void)1438a65bd63SAlex Kiernan int fastboot_set_reboot_flag(void)
144c964a0dcSKever Yang {
145c964a0dcSKever Yang 	struct rk322x_grf *grf;
146c964a0dcSKever Yang 
147c964a0dcSKever Yang 	printf("Setting reboot to fastboot flag ...\n");
148c964a0dcSKever Yang 	grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
149c964a0dcSKever Yang 	/* Set boot mode to fastboot */
150c964a0dcSKever Yang 	writel(BOOT_FASTBOOT, &grf->os_reg[0]);
151c964a0dcSKever Yang 
152c964a0dcSKever Yang 	return 0;
153c964a0dcSKever Yang }
154c964a0dcSKever Yang #endif
155