xref: /rk3399_rockchip-uboot/board/raspberrypi/rpi/rpi.c (revision 7233fb3143c7329cb072318ce39360213d80ea54)
1 /*
2  * (C) Copyright 2012-2013,2015 Stephen Warren
3  *
4  * SPDX-License-Identifier:	GPL-2.0
5  */
6 
7 #include <common.h>
8 #include <inttypes.h>
9 #include <config.h>
10 #include <dm.h>
11 #include <fdt_support.h>
12 #include <fdt_simplefb.h>
13 #include <lcd.h>
14 #include <memalign.h>
15 #include <mmc.h>
16 #include <asm/gpio.h>
17 #include <asm/arch/mbox.h>
18 #include <asm/arch/sdhci.h>
19 #include <asm/global_data.h>
20 #include <dm/platform_data/serial_pl01x.h>
21 
22 DECLARE_GLOBAL_DATA_PTR;
23 
24 static const struct bcm2835_gpio_platdata gpio_platdata = {
25 	.base = BCM2835_GPIO_BASE,
26 };
27 
28 U_BOOT_DEVICE(bcm2835_gpios) = {
29 	.name = "gpio_bcm2835",
30 	.platdata = &gpio_platdata,
31 };
32 
33 static const struct pl01x_serial_platdata serial_platdata = {
34 #ifndef CONFIG_BCM2835
35 	.base = 0x3f201000,
36 #else
37 	.base = 0x20201000,
38 #endif
39 	.type = TYPE_PL011,
40 	.skip_init = true,
41 };
42 
43 U_BOOT_DEVICE(bcm2835_serials) = {
44 	.name = "serial_pl01x",
45 	.platdata = &serial_platdata,
46 };
47 
48 struct msg_get_arm_mem {
49 	struct bcm2835_mbox_hdr hdr;
50 	struct bcm2835_mbox_tag_get_arm_mem get_arm_mem;
51 	u32 end_tag;
52 };
53 
54 struct msg_get_board_rev {
55 	struct bcm2835_mbox_hdr hdr;
56 	struct bcm2835_mbox_tag_get_board_rev get_board_rev;
57 	u32 end_tag;
58 };
59 
60 struct msg_get_board_serial {
61 	struct bcm2835_mbox_hdr hdr;
62 	struct bcm2835_mbox_tag_get_board_serial get_board_serial;
63 	u32 end_tag;
64 };
65 
66 struct msg_get_mac_address {
67 	struct bcm2835_mbox_hdr hdr;
68 	struct bcm2835_mbox_tag_get_mac_address get_mac_address;
69 	u32 end_tag;
70 };
71 
72 struct msg_set_power_state {
73 	struct bcm2835_mbox_hdr hdr;
74 	struct bcm2835_mbox_tag_set_power_state set_power_state;
75 	u32 end_tag;
76 };
77 
78 struct msg_get_clock_rate {
79 	struct bcm2835_mbox_hdr hdr;
80 	struct bcm2835_mbox_tag_get_clock_rate get_clock_rate;
81 	u32 end_tag;
82 };
83 
84 /*
85  * http://raspberryalphaomega.org.uk/2013/02/06/automatic-raspberry-pi-board-revision-detection-model-a-b1-and-b2/
86  * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=32733
87  * http://git.drogon.net/?p=wiringPi;a=blob;f=wiringPi/wiringPi.c;h=503151f61014418b9c42f4476a6086f75cd4e64b;hb=refs/heads/master#l922
88  *
89  * In http://lists.denx.de/pipermail/u-boot/2016-January/243752.html
90  * ("[U-Boot] [PATCH] rpi: fix up Model B entries") Dom Cobley at the RPi
91  * Foundation stated that the following source was accurate:
92  * https://github.com/AndrewFromMelbourne/raspberry_pi_revision
93  */
94 struct rpi_model {
95 	const char *name;
96 	const char *fdtfile;
97 	bool has_onboard_eth;
98 };
99 
100 static const struct rpi_model rpi_model_unknown = {
101 	"Unknown model",
102 	"bcm283x-rpi-other.dtb",
103 	false,
104 };
105 
106 static const struct rpi_model rpi_models_new_scheme[] = {
107 	[0x4] = {
108 		"2 Model B",
109 		"bcm2836-rpi-2-b.dtb",
110 		true,
111 	},
112 	[0x8] = {
113 		"3 Model B",
114 		"bcm2837-rpi-3-b.dtb",
115 		true,
116 	},
117 	[0x9] = {
118 		"Zero",
119 		"bcm2835-rpi-zero.dtb",
120 		false,
121 	},
122 };
123 
124 static const struct rpi_model rpi_models_old_scheme[] = {
125 	[0x2] = {
126 		"Model B",
127 		"bcm2835-rpi-b.dtb",
128 		true,
129 	},
130 	[0x3] = {
131 		"Model B",
132 		"bcm2835-rpi-b.dtb",
133 		true,
134 	},
135 	[0x4] = {
136 		"Model B rev2",
137 		"bcm2835-rpi-b-rev2.dtb",
138 		true,
139 	},
140 	[0x5] = {
141 		"Model B rev2",
142 		"bcm2835-rpi-b-rev2.dtb",
143 		true,
144 	},
145 	[0x6] = {
146 		"Model B rev2",
147 		"bcm2835-rpi-b-rev2.dtb",
148 		true,
149 	},
150 	[0x7] = {
151 		"Model A",
152 		"bcm2835-rpi-a.dtb",
153 		false,
154 	},
155 	[0x8] = {
156 		"Model A",
157 		"bcm2835-rpi-a.dtb",
158 		false,
159 	},
160 	[0x9] = {
161 		"Model A",
162 		"bcm2835-rpi-a.dtb",
163 		false,
164 	},
165 	[0xd] = {
166 		"Model B rev2",
167 		"bcm2835-rpi-b-rev2.dtb",
168 		true,
169 	},
170 	[0xe] = {
171 		"Model B rev2",
172 		"bcm2835-rpi-b-rev2.dtb",
173 		true,
174 	},
175 	[0xf] = {
176 		"Model B rev2",
177 		"bcm2835-rpi-b-rev2.dtb",
178 		true,
179 	},
180 	[0x10] = {
181 		"Model B+",
182 		"bcm2835-rpi-b-plus.dtb",
183 		true,
184 	},
185 	[0x11] = {
186 		"Compute Module",
187 		"bcm2835-rpi-cm.dtb",
188 		false,
189 	},
190 	[0x12] = {
191 		"Model A+",
192 		"bcm2835-rpi-a-plus.dtb",
193 		false,
194 	},
195 	[0x13] = {
196 		"Model B+",
197 		"bcm2835-rpi-b-plus.dtb",
198 		true,
199 	},
200 	[0x14] = {
201 		"Compute Module",
202 		"bcm2835-rpi-cm.dtb",
203 		false,
204 	},
205 	[0x15] = {
206 		"Model A+",
207 		"bcm2835-rpi-a-plus.dtb",
208 		false,
209 	},
210 };
211 
212 static uint32_t revision;
213 static uint32_t rev_scheme;
214 static uint32_t rev_type;
215 static const struct rpi_model *model;
216 
217 int dram_init(void)
218 {
219 	ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_arm_mem, msg, 1);
220 	int ret;
221 
222 	BCM2835_MBOX_INIT_HDR(msg);
223 	BCM2835_MBOX_INIT_TAG(&msg->get_arm_mem, GET_ARM_MEMORY);
224 
225 	ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
226 	if (ret) {
227 		printf("bcm2835: Could not query ARM memory size\n");
228 		return -1;
229 	}
230 
231 	gd->ram_size = msg->get_arm_mem.body.resp.mem_size;
232 
233 	return 0;
234 }
235 
236 static void set_fdtfile(void)
237 {
238 	const char *fdtfile;
239 
240 	if (getenv("fdtfile"))
241 		return;
242 
243 	fdtfile = model->fdtfile;
244 	setenv("fdtfile", fdtfile);
245 }
246 
247 static void set_usbethaddr(void)
248 {
249 	ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_mac_address, msg, 1);
250 	int ret;
251 
252 	if (!model->has_onboard_eth)
253 		return;
254 
255 	if (getenv("usbethaddr"))
256 		return;
257 
258 	BCM2835_MBOX_INIT_HDR(msg);
259 	BCM2835_MBOX_INIT_TAG(&msg->get_mac_address, GET_MAC_ADDRESS);
260 
261 	ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
262 	if (ret) {
263 		printf("bcm2835: Could not query MAC address\n");
264 		/* Ignore error; not critical */
265 		return;
266 	}
267 
268 	eth_setenv_enetaddr("usbethaddr", msg->get_mac_address.body.resp.mac);
269 
270 	if (!getenv("ethaddr"))
271 		setenv("ethaddr", getenv("usbethaddr"));
272 
273 	return;
274 }
275 
276 #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
277 static void set_board_info(void)
278 {
279 	char s[11];
280 
281 	snprintf(s, sizeof(s), "0x%X", revision);
282 	setenv("board_revision", s);
283 	snprintf(s, sizeof(s), "%d", rev_scheme);
284 	setenv("board_rev_scheme", s);
285 	/* Can't rename this to board_rev_type since it's an ABI for scripts */
286 	snprintf(s, sizeof(s), "0x%X", rev_type);
287 	setenv("board_rev", s);
288 	setenv("board_name", model->name);
289 }
290 #endif /* CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG */
291 
292 static void set_serial_number(void)
293 {
294 	ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_serial, msg, 1);
295 	int ret;
296 	char serial_string[17] = { 0 };
297 
298 	if (getenv("serial#"))
299 		return;
300 
301 	BCM2835_MBOX_INIT_HDR(msg);
302 	BCM2835_MBOX_INIT_TAG_NO_REQ(&msg->get_board_serial, GET_BOARD_SERIAL);
303 
304 	ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
305 	if (ret) {
306 		printf("bcm2835: Could not query board serial\n");
307 		/* Ignore error; not critical */
308 		return;
309 	}
310 
311 	snprintf(serial_string, sizeof(serial_string), "%016" PRIx64,
312 		 msg->get_board_serial.body.resp.serial);
313 	setenv("serial#", serial_string);
314 }
315 
316 int misc_init_r(void)
317 {
318 	set_fdtfile();
319 	set_usbethaddr();
320 #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
321 	set_board_info();
322 #endif
323 	set_serial_number();
324 
325 	return 0;
326 }
327 
328 static int power_on_module(u32 module)
329 {
330 	ALLOC_CACHE_ALIGN_BUFFER(struct msg_set_power_state, msg_pwr, 1);
331 	int ret;
332 
333 	BCM2835_MBOX_INIT_HDR(msg_pwr);
334 	BCM2835_MBOX_INIT_TAG(&msg_pwr->set_power_state,
335 			      SET_POWER_STATE);
336 	msg_pwr->set_power_state.body.req.device_id = module;
337 	msg_pwr->set_power_state.body.req.state =
338 		BCM2835_MBOX_SET_POWER_STATE_REQ_ON |
339 		BCM2835_MBOX_SET_POWER_STATE_REQ_WAIT;
340 
341 	ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN,
342 				     &msg_pwr->hdr);
343 	if (ret) {
344 		printf("bcm2835: Could not set module %u power state\n",
345 		       module);
346 		return -1;
347 	}
348 
349 	return 0;
350 }
351 
352 static void get_board_rev(void)
353 {
354 	ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_rev, msg, 1);
355 	int ret;
356 	const struct rpi_model *models;
357 	uint32_t models_count;
358 
359 	BCM2835_MBOX_INIT_HDR(msg);
360 	BCM2835_MBOX_INIT_TAG(&msg->get_board_rev, GET_BOARD_REV);
361 
362 	ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
363 	if (ret) {
364 		printf("bcm2835: Could not query board revision\n");
365 		/* Ignore error; not critical */
366 		return;
367 	}
368 
369 	/*
370 	 * For details of old-vs-new scheme, see:
371 	 * https://github.com/pimoroni/RPi.version/blob/master/RPi/version.py
372 	 * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=99293&p=690282
373 	 * (a few posts down)
374 	 *
375 	 * For the RPi 1, bit 24 is the "warranty bit", so we mask off just the
376 	 * lower byte to use as the board rev:
377 	 * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=98367&start=250
378 	 * http://www.raspberrypi.org/forums/viewtopic.php?f=31&t=20594
379 	 */
380 	revision = msg->get_board_rev.body.resp.rev;
381 	if (revision & 0x800000) {
382 		rev_scheme = 1;
383 		rev_type = (revision >> 4) & 0xff;
384 		models = rpi_models_new_scheme;
385 		models_count = ARRAY_SIZE(rpi_models_new_scheme);
386 	} else {
387 		rev_scheme = 0;
388 		rev_type = revision & 0xff;
389 		models = rpi_models_old_scheme;
390 		models_count = ARRAY_SIZE(rpi_models_old_scheme);
391 	}
392 	if (rev_type >= models_count) {
393 		printf("RPI: Board rev 0x%x outside known range\n", rev_type);
394 		model = &rpi_model_unknown;
395 	} else if (!models[rev_type].name) {
396 		printf("RPI: Board rev 0x%x unknown\n", rev_type);
397 		model = &rpi_model_unknown;
398 	} else {
399 		model = &models[rev_type];
400 	}
401 
402 	printf("RPI %s (0x%x)\n", model->name, revision);
403 }
404 
405 int board_init(void)
406 {
407 	get_board_rev();
408 
409 	gd->bd->bi_boot_params = 0x100;
410 
411 	return power_on_module(BCM2835_MBOX_POWER_DEVID_USB_HCD);
412 }
413 
414 int board_mmc_init(bd_t *bis)
415 {
416 	ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_clock_rate, msg_clk, 1);
417 	int ret;
418 
419 	power_on_module(BCM2835_MBOX_POWER_DEVID_SDHCI);
420 
421 	BCM2835_MBOX_INIT_HDR(msg_clk);
422 	BCM2835_MBOX_INIT_TAG(&msg_clk->get_clock_rate, GET_CLOCK_RATE);
423 	msg_clk->get_clock_rate.body.req.clock_id = BCM2835_MBOX_CLOCK_ID_EMMC;
424 
425 	ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg_clk->hdr);
426 	if (ret) {
427 		printf("bcm2835: Could not query eMMC clock rate\n");
428 		return -1;
429 	}
430 
431 	return bcm2835_sdhci_init(BCM2835_SDHCI_BASE,
432 				  msg_clk->get_clock_rate.body.resp.rate_hz);
433 }
434 
435 int ft_board_setup(void *blob, bd_t *bd)
436 {
437 	/*
438 	 * For now, we simply always add the simplefb DT node. Later, we
439 	 * should be more intelligent, and e.g. only do this if no enabled DT
440 	 * node exists for the "real" graphics driver.
441 	 */
442 	lcd_dt_simplefb_add_node(blob);
443 
444 	return 0;
445 }
446