xref: /rk3399_rockchip-uboot/arch/arm/mach-rockchip/board.c (revision 63a7578e4e7dc906e75b0bec5a2f3fe41c3720f4)
1 /*
2  * (C) Copyright 2015 Google, Inc
3  *
4  * SPDX-License-Identifier:     GPL-2.0+
5  */
6 
7 #include <common.h>
8 #include <clk.h>
9 #include <dm.h>
10 #include <ram.h>
11 #include <asm/io.h>
12 #include <asm/arch/clock.h>
13 #include <asm/arch/periph.h>
14 #include <asm/gpio.h>
15 #include <dm/pinctrl.h>
16 
17 DECLARE_GLOBAL_DATA_PTR;
18 
19 int board_init(void)
20 {
21 #ifdef CONFIG_ROCKCHIP_SPL_BACK_TO_BROM
22 	struct udevice *pinctrl;
23 	int ret;
24 
25     /*
26      * We need to implement sdcard iomux here for the further
27      * initlization, otherwise, it'll hit sdcard command sending
28      * timeout exception.
29      */
30 	ret = uclass_get_device(UCLASS_PINCTRL, 0, &pinctrl);
31 	if (ret) {
32 		debug("%s: Cannot find pinctrl device\n", __func__);
33 		goto err;
34 	}
35 	ret = pinctrl_request_noflags(pinctrl, PERIPH_ID_SDCARD);
36 	if (ret) {
37 		debug("%s: Failed to set up SD card\n", __func__);
38 		goto err;
39 	}
40 
41 	return 0;
42 err:
43 	printf("board_init: Error %d\n", ret);
44 
45 	/* No way to report error here */
46 	hang();
47 
48 	return -1;
49 #else
50 	return 0;
51 #endif
52 }
53 
54 int dram_init(void)
55 {
56 	struct ram_info ram;
57 	struct udevice *dev;
58 	int ret;
59 
60 	ret = uclass_get_device(UCLASS_RAM, 0, &dev);
61 	if (ret) {
62 		debug("DRAM init failed: %d\n", ret);
63 		return ret;
64 	}
65 	ret = ram_get_info(dev, &ram);
66 	if (ret) {
67 		debug("Cannot get DRAM size: %d\n", ret);
68 		return ret;
69 	}
70 	debug("SDRAM base=%lx, size=%x\n", ram.base, ram.size);
71 	gd->ram_size = ram.size;
72 
73 	return 0;
74 }
75 
76 #ifndef CONFIG_SYS_DCACHE_OFF
77 void enable_caches(void)
78 {
79 	/* Enable D-cache. I-cache is already enabled in start.S */
80 	dcache_enable();
81 }
82 #endif
83 
84 #if defined(CONFIG_USB_GADGET) && defined(CONFIG_USB_GADGET_DWC2_OTG)
85 #include <usb.h>
86 #include <usb/dwc2_udc.h>
87 
88 static struct dwc2_plat_otg_data rk3288_otg_data = {
89 	.rx_fifo_sz	= 512,
90 	.np_tx_fifo_sz	= 16,
91 	.tx_fifo_sz	= 128,
92 };
93 
94 int board_usb_init(int index, enum usb_init_type init)
95 {
96 	int node, phy_node;
97 	const char *mode;
98 	bool matched = false;
99 	const void *blob = gd->fdt_blob;
100 	u32 grf_phy_offset;
101 
102 	/* find the usb_otg node */
103 	node = fdt_node_offset_by_compatible(blob, -1,
104 					"rockchip,rk3288-usb");
105 
106 	while (node > 0) {
107 		mode = fdt_getprop(blob, node, "dr_mode", NULL);
108 		if (mode && strcmp(mode, "otg") == 0) {
109 			matched = true;
110 			break;
111 		}
112 
113 		node = fdt_node_offset_by_compatible(blob, node,
114 					"rockchip,rk3288-usb");
115 	}
116 	if (!matched) {
117 		debug("Not found usb_otg device\n");
118 		return -ENODEV;
119 	}
120 	rk3288_otg_data.regs_otg = fdtdec_get_addr(blob, node, "reg");
121 
122 	node = fdtdec_lookup_phandle(blob, node, "phys");
123 	if (node <= 0) {
124 		debug("Not found usb phy device\n");
125 		return -ENODEV;
126 	}
127 
128 	phy_node = fdt_parent_offset(blob, node);
129 	if (phy_node <= 0) {
130 		debug("Not found usb phy device\n");
131 		return -ENODEV;
132 	}
133 
134 	rk3288_otg_data.phy_of_node = phy_node;
135 	grf_phy_offset = fdtdec_get_addr(blob, node, "reg");
136 
137 	/* find the grf node */
138 	node = fdt_node_offset_by_compatible(blob, -1,
139 					"rockchip,rk3288-grf");
140 	if (node <= 0) {
141 		debug("Not found grf device\n");
142 		return -ENODEV;
143 	}
144 	rk3288_otg_data.regs_phy = grf_phy_offset +
145 				fdtdec_get_addr(blob, node, "reg");
146 
147 	return dwc2_udc_probe(&rk3288_otg_data);
148 }
149 
150 int board_usb_cleanup(int index, enum usb_init_type init)
151 {
152 	return 0;
153 }
154 #endif
155 
156 static int do_clock(cmd_tbl_t *cmdtp, int flag, int argc,
157 		       char * const argv[])
158 {
159 	static const struct {
160 		char *name;
161 		int id;
162 	} clks[] = {
163 		{ "osc", CLK_OSC },
164 		{ "apll", CLK_ARM },
165 		{ "dpll", CLK_DDR },
166 		{ "cpll", CLK_CODEC },
167 		{ "gpll", CLK_GENERAL },
168 #ifdef CONFIG_ROCKCHIP_RK3036
169 		{ "mpll", CLK_NEW },
170 #else
171 		{ "npll", CLK_NEW },
172 #endif
173 	};
174 	int ret, i;
175 	struct udevice *dev;
176 
177 	ret = rockchip_get_clk(&dev);
178 	if (ret) {
179 		printf("clk-uclass not found\n");
180 		return 0;
181 	}
182 
183 	for (i = 0; i < ARRAY_SIZE(clks); i++) {
184 		struct clk clk;
185 		ulong rate;
186 
187 		clk.id = clks[i].id;
188 		ret = clk_request(dev, &clk);
189 		if (ret < 0)
190 			continue;
191 
192 		rate = clk_get_rate(&clk);
193 		printf("%s: %lu\n", clks[i].name, rate);
194 
195 		clk_free(&clk);
196 	}
197 
198 	return 0;
199 }
200 
201 U_BOOT_CMD(
202 	clock, 2, 1, do_clock,
203 	"display information about clocks",
204 	""
205 );
206