xref: /openbmc/u-boot/board/raspberrypi/rpi/rpi.c (revision fea7f3aa)
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 <config.h>
9 #include <dm.h>
10 #include <fdt_support.h>
11 #include <fdt_simplefb.h>
12 #include <lcd.h>
13 #include <memalign.h>
14 #include <mmc.h>
15 #include <asm/gpio.h>
16 #include <asm/arch/mbox.h>
17 #include <asm/arch/sdhci.h>
18 #include <asm/global_data.h>
19 #include <dm/platform_data/serial_pl01x.h>
20 
21 DECLARE_GLOBAL_DATA_PTR;
22 
23 static const struct bcm2835_gpio_platdata gpio_platdata = {
24 	.base = BCM2835_GPIO_BASE,
25 };
26 
27 U_BOOT_DEVICE(bcm2835_gpios) = {
28 	.name = "gpio_bcm2835",
29 	.platdata = &gpio_platdata,
30 };
31 
32 static const struct pl01x_serial_platdata serial_platdata = {
33 #ifdef CONFIG_BCM2836
34 	.base = 0x3f201000,
35 #else
36 	.base = 0x20201000,
37 #endif
38 	.type = TYPE_PL011,
39 	.clock = 3000000,
40 };
41 
42 U_BOOT_DEVICE(bcm2835_serials) = {
43 	.name = "serial_pl01x",
44 	.platdata = &serial_platdata,
45 };
46 
47 struct msg_get_arm_mem {
48 	struct bcm2835_mbox_hdr hdr;
49 	struct bcm2835_mbox_tag_get_arm_mem get_arm_mem;
50 	u32 end_tag;
51 };
52 
53 struct msg_get_board_rev {
54 	struct bcm2835_mbox_hdr hdr;
55 	struct bcm2835_mbox_tag_get_board_rev get_board_rev;
56 	u32 end_tag;
57 };
58 
59 struct msg_get_mac_address {
60 	struct bcm2835_mbox_hdr hdr;
61 	struct bcm2835_mbox_tag_get_mac_address get_mac_address;
62 	u32 end_tag;
63 };
64 
65 struct msg_set_power_state {
66 	struct bcm2835_mbox_hdr hdr;
67 	struct bcm2835_mbox_tag_set_power_state set_power_state;
68 	u32 end_tag;
69 };
70 
71 struct msg_get_clock_rate {
72 	struct bcm2835_mbox_hdr hdr;
73 	struct bcm2835_mbox_tag_get_clock_rate get_clock_rate;
74 	u32 end_tag;
75 };
76 
77 /* See comments in mbox.h for data source */
78 static const struct {
79 	const char *name;
80 	const char *fdtfile;
81 	bool has_onboard_eth;
82 } models[] = {
83 	[0] = {
84 		"Unknown model",
85 #ifdef CONFIG_BCM2836
86 		"bcm2836-rpi-other.dtb",
87 #else
88 		"bcm2835-rpi-other.dtb",
89 #endif
90 		false,
91 	},
92 #ifdef CONFIG_BCM2836
93 	[BCM2836_BOARD_REV_2_B] = {
94 		"2 Model B",
95 		"bcm2836-rpi-2-b.dtb",
96 		true,
97 	},
98 #else
99 	[BCM2835_BOARD_REV_B_I2C0_2] = {
100 		"Model B (no P5)",
101 		"bcm2835-rpi-b-i2c0.dtb",
102 		true,
103 	},
104 	[BCM2835_BOARD_REV_B_I2C0_3] = {
105 		"Model B (no P5)",
106 		"bcm2835-rpi-b-i2c0.dtb",
107 		true,
108 	},
109 	[BCM2835_BOARD_REV_B_I2C1_4] = {
110 		"Model B",
111 		"bcm2835-rpi-b.dtb",
112 		true,
113 	},
114 	[BCM2835_BOARD_REV_B_I2C1_5] = {
115 		"Model B",
116 		"bcm2835-rpi-b.dtb",
117 		true,
118 	},
119 	[BCM2835_BOARD_REV_B_I2C1_6] = {
120 		"Model B",
121 		"bcm2835-rpi-b.dtb",
122 		true,
123 	},
124 	[BCM2835_BOARD_REV_A_7] = {
125 		"Model A",
126 		"bcm2835-rpi-a.dtb",
127 		false,
128 	},
129 	[BCM2835_BOARD_REV_A_8] = {
130 		"Model A",
131 		"bcm2835-rpi-a.dtb",
132 		false,
133 	},
134 	[BCM2835_BOARD_REV_A_9] = {
135 		"Model A",
136 		"bcm2835-rpi-a.dtb",
137 		false,
138 	},
139 	[BCM2835_BOARD_REV_B_REV2_d] = {
140 		"Model B rev2",
141 		"bcm2835-rpi-b-rev2.dtb",
142 		true,
143 	},
144 	[BCM2835_BOARD_REV_B_REV2_e] = {
145 		"Model B rev2",
146 		"bcm2835-rpi-b-rev2.dtb",
147 		true,
148 	},
149 	[BCM2835_BOARD_REV_B_REV2_f] = {
150 		"Model B rev2",
151 		"bcm2835-rpi-b-rev2.dtb",
152 		true,
153 	},
154 	[BCM2835_BOARD_REV_B_PLUS] = {
155 		"Model B+",
156 		"bcm2835-rpi-b-plus.dtb",
157 		true,
158 	},
159 	[BCM2835_BOARD_REV_CM] = {
160 		"Compute Module",
161 		"bcm2835-rpi-cm.dtb",
162 		false,
163 	},
164 	[BCM2835_BOARD_REV_A_PLUS] = {
165 		"Model A+",
166 		"bcm2835-rpi-a-plus.dtb",
167 		false,
168 	},
169 	[BCM2835_BOARD_REV_B_PLUS_13] = {
170 		"Model B+",
171 		"bcm2835-rpi-b-plus.dtb",
172 		true,
173 	},
174 	[BCM2835_BOARD_REV_CM_14] = {
175 		"Compute Module",
176 		"bcm2835-rpi-cm.dtb",
177 		false,
178 	},
179 	[BCM2835_BOARD_REV_A_PLUS_15] = {
180 		"Model A+",
181 		"bcm2835-rpi-a-plus.dtb",
182 		false,
183 	},
184 #endif
185 };
186 
187 u32 rpi_board_rev = 0;
188 
189 int dram_init(void)
190 {
191 	ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_arm_mem, msg, 1);
192 	int ret;
193 
194 	BCM2835_MBOX_INIT_HDR(msg);
195 	BCM2835_MBOX_INIT_TAG(&msg->get_arm_mem, GET_ARM_MEMORY);
196 
197 	ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
198 	if (ret) {
199 		printf("bcm2835: Could not query ARM memory size\n");
200 		return -1;
201 	}
202 
203 	gd->ram_size = msg->get_arm_mem.body.resp.mem_size;
204 
205 	return 0;
206 }
207 
208 static void set_fdtfile(void)
209 {
210 	const char *fdtfile;
211 
212 	if (getenv("fdtfile"))
213 		return;
214 
215 	fdtfile = models[rpi_board_rev].fdtfile;
216 	setenv("fdtfile", fdtfile);
217 }
218 
219 static void set_usbethaddr(void)
220 {
221 	ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_mac_address, msg, 1);
222 	int ret;
223 
224 	if (!models[rpi_board_rev].has_onboard_eth)
225 		return;
226 
227 	if (getenv("usbethaddr"))
228 		return;
229 
230 	BCM2835_MBOX_INIT_HDR(msg);
231 	BCM2835_MBOX_INIT_TAG(&msg->get_mac_address, GET_MAC_ADDRESS);
232 
233 	ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
234 	if (ret) {
235 		printf("bcm2835: Could not query MAC address\n");
236 		/* Ignore error; not critical */
237 		return;
238 	}
239 
240 	eth_setenv_enetaddr("usbethaddr", msg->get_mac_address.body.resp.mac);
241 
242 	return;
243 }
244 
245 int misc_init_r(void)
246 {
247 	set_fdtfile();
248 	set_usbethaddr();
249 	return 0;
250 }
251 
252 static int power_on_module(u32 module)
253 {
254 	ALLOC_CACHE_ALIGN_BUFFER(struct msg_set_power_state, msg_pwr, 1);
255 	int ret;
256 
257 	BCM2835_MBOX_INIT_HDR(msg_pwr);
258 	BCM2835_MBOX_INIT_TAG(&msg_pwr->set_power_state,
259 			      SET_POWER_STATE);
260 	msg_pwr->set_power_state.body.req.device_id = module;
261 	msg_pwr->set_power_state.body.req.state =
262 		BCM2835_MBOX_SET_POWER_STATE_REQ_ON |
263 		BCM2835_MBOX_SET_POWER_STATE_REQ_WAIT;
264 
265 	ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN,
266 				     &msg_pwr->hdr);
267 	if (ret) {
268 		printf("bcm2835: Could not set module %u power state\n",
269 		       module);
270 		return -1;
271 	}
272 
273 	return 0;
274 }
275 
276 static void get_board_rev(void)
277 {
278 	ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_rev, msg, 1);
279 	int ret;
280 	const char *name;
281 
282 	BCM2835_MBOX_INIT_HDR(msg);
283 	BCM2835_MBOX_INIT_TAG(&msg->get_board_rev, GET_BOARD_REV);
284 
285 	ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
286 	if (ret) {
287 		printf("bcm2835: Could not query board revision\n");
288 		/* Ignore error; not critical */
289 		return;
290 	}
291 
292 	/*
293 	 * For details of old-vs-new scheme, see:
294 	 * https://github.com/pimoroni/RPi.version/blob/master/RPi/version.py
295 	 * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=99293&p=690282
296 	 * (a few posts down)
297 	 *
298 	 * For the RPi 1, bit 24 is the "warranty bit", so we mask off just the
299 	 * lower byte to use as the board rev:
300 	 * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=98367&start=250
301 	 * http://www.raspberrypi.org/forums/viewtopic.php?f=31&t=20594
302 	 */
303 	rpi_board_rev = msg->get_board_rev.body.resp.rev;
304 	if (rpi_board_rev & 0x800000)
305 		rpi_board_rev = (rpi_board_rev >> 4) & 0xff;
306 	else
307 		rpi_board_rev &= 0xff;
308 	if (rpi_board_rev >= ARRAY_SIZE(models)) {
309 		printf("RPI: Board rev %u outside known range\n",
310 		       rpi_board_rev);
311 		rpi_board_rev = 0;
312 	}
313 	if (!models[rpi_board_rev].name) {
314 		printf("RPI: Board rev %u unknown\n", rpi_board_rev);
315 		rpi_board_rev = 0;
316 	}
317 
318 	name = models[rpi_board_rev].name;
319 	printf("RPI %s\n", name);
320 }
321 
322 int board_init(void)
323 {
324 	get_board_rev();
325 
326 	gd->bd->bi_boot_params = 0x100;
327 
328 	return power_on_module(BCM2835_MBOX_POWER_DEVID_USB_HCD);
329 }
330 
331 int board_mmc_init(bd_t *bis)
332 {
333 	ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_clock_rate, msg_clk, 1);
334 	int ret;
335 
336 	power_on_module(BCM2835_MBOX_POWER_DEVID_SDHCI);
337 
338 	BCM2835_MBOX_INIT_HDR(msg_clk);
339 	BCM2835_MBOX_INIT_TAG(&msg_clk->get_clock_rate, GET_CLOCK_RATE);
340 	msg_clk->get_clock_rate.body.req.clock_id = BCM2835_MBOX_CLOCK_ID_EMMC;
341 
342 	ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg_clk->hdr);
343 	if (ret) {
344 		printf("bcm2835: Could not query eMMC clock rate\n");
345 		return -1;
346 	}
347 
348 	return bcm2835_sdhci_init(BCM2835_SDHCI_BASE,
349 				  msg_clk->get_clock_rate.body.resp.rate_hz);
350 }
351 
352 int ft_board_setup(void *blob, bd_t *bd)
353 {
354 	/*
355 	 * For now, we simply always add the simplefb DT node. Later, we
356 	 * should be more intelligent, and e.g. only do this if no enabled DT
357 	 * node exists for the "real" graphics driver.
358 	 */
359 	lcd_dt_simplefb_add_node(blob);
360 
361 	return 0;
362 }
363