blob: e82022ef12fdc19381453e335a0d7762eefc8447 [file] [log] [blame]
Stephen Warren0e012c32012-08-05 16:07:22 +00001/*
Stephen Warrend6f47c82016-03-24 22:15:20 -06002 * (C) Copyright 2012-2016 Stephen Warren
Stephen Warren0e012c32012-08-05 16:07:22 +00003 *
Stephen Warren29e494c2015-02-16 12:16:13 -07004 * SPDX-License-Identifier: GPL-2.0
Stephen Warren0e012c32012-08-05 16:07:22 +00005 */
6
7#include <common.h>
Lubomir Rintel7d33bb62016-02-22 22:06:47 +01008#include <inttypes.h>
Stephen Warrenaa44d532013-05-27 18:31:18 +00009#include <config.h>
Simon Glass74807622014-09-22 17:30:56 -060010#include <dm.h>
Jeroen Hofsteed5b2fed2014-07-13 22:01:51 +020011#include <fdt_support.h>
Nikita Kiryanovb071e4c2015-02-03 13:32:31 +020012#include <fdt_simplefb.h>
Stephen Warrenaa44d532013-05-27 18:31:18 +000013#include <lcd.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060014#include <memalign.h>
Jeroen Hofsteed5b2fed2014-07-13 22:01:51 +020015#include <mmc.h>
Simon Glass74807622014-09-22 17:30:56 -060016#include <asm/gpio.h>
Stephen Warrenacc8bf42013-01-29 16:37:37 +000017#include <asm/arch/mbox.h>
Stephen Warrenc4ab9712013-01-29 16:37:42 +000018#include <asm/arch/sdhci.h>
Stephen Warren0e012c32012-08-05 16:07:22 +000019#include <asm/global_data.h>
Simon Glassf52cada2014-11-24 21:36:34 -070020#include <dm/platform_data/serial_pl01x.h>
Stephen Warrend6f47c82016-03-24 22:15:20 -060021#include <dm/platform_data/serial_bcm283x_mu.h>
Stephen Warren93ea5262016-04-01 21:14:15 -060022#ifdef CONFIG_ARM64
23#include <asm/armv8/mmu.h>
24#endif
Stephen Warren0e012c32012-08-05 16:07:22 +000025
26DECLARE_GLOBAL_DATA_PTR;
27
Cédric Schieli9473f532016-11-11 11:59:07 +010028/* From lowlevel_init.S */
29extern unsigned long fw_dtb_pointer;
30
Simon Glass74807622014-09-22 17:30:56 -060031static const struct bcm2835_gpio_platdata gpio_platdata = {
32 .base = BCM2835_GPIO_BASE,
33};
34
35U_BOOT_DEVICE(bcm2835_gpios) = {
36 .name = "gpio_bcm2835",
37 .platdata = &gpio_platdata,
38};
39
Stephen Warrend6f47c82016-03-24 22:15:20 -060040#ifdef CONFIG_PL01X_SERIAL
Simon Glassf52cada2014-11-24 21:36:34 -070041static const struct pl01x_serial_platdata serial_platdata = {
Stephen Warren9d5990b2016-03-16 21:40:56 -060042#ifndef CONFIG_BCM2835
Stephen Warrendc7ea682015-02-16 12:16:15 -070043 .base = 0x3f201000,
44#else
Simon Glassf52cada2014-11-24 21:36:34 -070045 .base = 0x20201000,
Stephen Warrendc7ea682015-02-16 12:16:15 -070046#endif
Simon Glassf52cada2014-11-24 21:36:34 -070047 .type = TYPE_PL011,
Eric Anholtbe5a7dd2016-03-13 18:16:54 -070048 .skip_init = true,
Simon Glassf52cada2014-11-24 21:36:34 -070049};
50
51U_BOOT_DEVICE(bcm2835_serials) = {
52 .name = "serial_pl01x",
53 .platdata = &serial_platdata,
54};
Stephen Warrend6f47c82016-03-24 22:15:20 -060055#else
Alexander Grafafb99072016-08-15 17:48:51 +020056static struct bcm283x_mu_serial_platdata serial_platdata = {
Stephen Warrend6f47c82016-03-24 22:15:20 -060057 .base = 0x3f215040,
58 .clock = 250000000,
59 .skip_init = true,
60};
61
62U_BOOT_DEVICE(bcm2837_serials) = {
63 .name = "serial_bcm283x_mu",
64 .platdata = &serial_platdata,
65};
66#endif
Simon Glassf52cada2014-11-24 21:36:34 -070067
Stephen Warrenacc8bf42013-01-29 16:37:37 +000068struct msg_get_arm_mem {
69 struct bcm2835_mbox_hdr hdr;
70 struct bcm2835_mbox_tag_get_arm_mem get_arm_mem;
71 u32 end_tag;
72};
73
Stephen Warrencd210c12014-11-18 21:40:21 -070074struct msg_get_board_rev {
75 struct bcm2835_mbox_hdr hdr;
76 struct bcm2835_mbox_tag_get_board_rev get_board_rev;
77 u32 end_tag;
78};
79
Lubomir Rintel7d33bb62016-02-22 22:06:47 +010080struct msg_get_board_serial {
81 struct bcm2835_mbox_hdr hdr;
82 struct bcm2835_mbox_tag_get_board_serial get_board_serial;
83 u32 end_tag;
84};
85
Stephen Warrenaf6e20d2014-09-26 20:51:39 -060086struct msg_get_mac_address {
87 struct bcm2835_mbox_hdr hdr;
88 struct bcm2835_mbox_tag_get_mac_address get_mac_address;
89 u32 end_tag;
90};
91
Stephen Warren8672d202014-01-13 19:50:11 -070092struct msg_set_power_state {
93 struct bcm2835_mbox_hdr hdr;
94 struct bcm2835_mbox_tag_set_power_state set_power_state;
95 u32 end_tag;
96};
97
Stephen Warrenc4ab9712013-01-29 16:37:42 +000098struct msg_get_clock_rate {
99 struct bcm2835_mbox_hdr hdr;
100 struct bcm2835_mbox_tag_get_clock_rate get_clock_rate;
101 u32 end_tag;
102};
103
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700104/*
105 * http://raspberryalphaomega.org.uk/2013/02/06/automatic-raspberry-pi-board-revision-detection-model-a-b1-and-b2/
106 * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=32733
Stephen Warren26683852015-12-04 22:07:45 -0700107 * http://git.drogon.net/?p=wiringPi;a=blob;f=wiringPi/wiringPi.c;h=503151f61014418b9c42f4476a6086f75cd4e64b;hb=refs/heads/master#l922
Stephen Warrena33dc8b2016-01-28 22:24:44 -0700108 *
109 * In http://lists.denx.de/pipermail/u-boot/2016-January/243752.html
110 * ("[U-Boot] [PATCH] rpi: fix up Model B entries") Dom Cobley at the RPi
111 * Foundation stated that the following source was accurate:
112 * https://github.com/AndrewFromMelbourne/raspberry_pi_revision
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700113 */
Stephen Warren26683852015-12-04 22:07:45 -0700114struct rpi_model {
Stephen Warrencd210c12014-11-18 21:40:21 -0700115 const char *name;
116 const char *fdtfile;
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700117 bool has_onboard_eth;
Stephen Warren26683852015-12-04 22:07:45 -0700118};
119
120static const struct rpi_model rpi_model_unknown = {
121 "Unknown model",
Stephen Warren93970ce2016-03-24 22:15:17 -0600122 "bcm283x-rpi-other.dtb",
Stephen Warren26683852015-12-04 22:07:45 -0700123 false,
124};
125
126static const struct rpi_model rpi_models_new_scheme[] = {
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700127 [0x4] = {
Stephen Warrendc7ea682015-02-16 12:16:15 -0700128 "2 Model B",
129 "bcm2836-rpi-2-b.dtb",
130 true,
131 },
Stephen Warren303244a2016-03-24 22:15:18 -0600132 [0x8] = {
133 "3 Model B",
134 "bcm2837-rpi-3-b.dtb",
135 true,
136 },
Stephen Warren2f1f20b2015-12-04 22:07:46 -0700137 [0x9] = {
138 "Zero",
139 "bcm2835-rpi-zero.dtb",
140 false,
141 },
Stephen Warren26683852015-12-04 22:07:45 -0700142};
143
144static const struct rpi_model rpi_models_old_scheme[] = {
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700145 [0x2] = {
Lubomir Rintelc44f76e2016-01-29 09:35:52 +0100146 "Model B",
147 "bcm2835-rpi-b.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700148 true,
Stephen Warrencd210c12014-11-18 21:40:21 -0700149 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700150 [0x3] = {
Lubomir Rintelc44f76e2016-01-29 09:35:52 +0100151 "Model B",
152 "bcm2835-rpi-b.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700153 true,
Stephen Warrencd210c12014-11-18 21:40:21 -0700154 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700155 [0x4] = {
Lubomir Rintelc44f76e2016-01-29 09:35:52 +0100156 "Model B rev2",
157 "bcm2835-rpi-b-rev2.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700158 true,
Stephen Warrencd210c12014-11-18 21:40:21 -0700159 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700160 [0x5] = {
Lubomir Rintelc44f76e2016-01-29 09:35:52 +0100161 "Model B rev2",
162 "bcm2835-rpi-b-rev2.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700163 true,
Stephen Warrencd210c12014-11-18 21:40:21 -0700164 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700165 [0x6] = {
Lubomir Rintelc44f76e2016-01-29 09:35:52 +0100166 "Model B rev2",
167 "bcm2835-rpi-b-rev2.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700168 true,
Stephen Warrencd210c12014-11-18 21:40:21 -0700169 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700170 [0x7] = {
Stephen Warrencd210c12014-11-18 21:40:21 -0700171 "Model A",
172 "bcm2835-rpi-a.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700173 false,
Stephen Warrencd210c12014-11-18 21:40:21 -0700174 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700175 [0x8] = {
Stephen Warrencd210c12014-11-18 21:40:21 -0700176 "Model A",
177 "bcm2835-rpi-a.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700178 false,
Stephen Warrencd210c12014-11-18 21:40:21 -0700179 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700180 [0x9] = {
Stephen Warrencd210c12014-11-18 21:40:21 -0700181 "Model A",
182 "bcm2835-rpi-a.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700183 false,
Stephen Warrencd210c12014-11-18 21:40:21 -0700184 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700185 [0xd] = {
Stephen Warrencd210c12014-11-18 21:40:21 -0700186 "Model B rev2",
187 "bcm2835-rpi-b-rev2.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700188 true,
Stephen Warrencd210c12014-11-18 21:40:21 -0700189 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700190 [0xe] = {
Stephen Warrencd210c12014-11-18 21:40:21 -0700191 "Model B rev2",
192 "bcm2835-rpi-b-rev2.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700193 true,
Stephen Warrencd210c12014-11-18 21:40:21 -0700194 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700195 [0xf] = {
Stephen Warrencd210c12014-11-18 21:40:21 -0700196 "Model B rev2",
197 "bcm2835-rpi-b-rev2.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700198 true,
Stephen Warrencd210c12014-11-18 21:40:21 -0700199 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700200 [0x10] = {
Stephen Warrencd210c12014-11-18 21:40:21 -0700201 "Model B+",
202 "bcm2835-rpi-b-plus.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700203 true,
Stephen Warrencd210c12014-11-18 21:40:21 -0700204 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700205 [0x11] = {
Stephen Warrencd210c12014-11-18 21:40:21 -0700206 "Compute Module",
207 "bcm2835-rpi-cm.dtb",
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700208 false,
Stephen Warrencd210c12014-11-18 21:40:21 -0700209 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700210 [0x12] = {
Stephen Warren18a5fc62014-12-23 20:01:43 -0700211 "Model A+",
212 "bcm2835-rpi-a-plus.dtb",
213 false,
214 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700215 [0x13] = {
Stephen Warren3648a852015-04-12 21:43:25 -0600216 "Model B+",
217 "bcm2835-rpi-b-plus.dtb",
218 true,
219 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700220 [0x14] = {
Stephen Warren3648a852015-04-12 21:43:25 -0600221 "Compute Module",
222 "bcm2835-rpi-cm.dtb",
223 false,
224 },
Stephen Warrenbe8efec2015-12-04 22:07:44 -0700225 [0x15] = {
Lubomir Rintelc87458c2015-10-14 17:17:54 +0200226 "Model A+",
227 "bcm2835-rpi-a-plus.dtb",
228 false,
229 },
Stephen Warrencd210c12014-11-18 21:40:21 -0700230};
231
Stephen Warren26683852015-12-04 22:07:45 -0700232static uint32_t revision;
233static uint32_t rev_scheme;
234static uint32_t rev_type;
235static const struct rpi_model *model;
Stephen Warrencd210c12014-11-18 21:40:21 -0700236
Stephen Warren93ea5262016-04-01 21:14:15 -0600237#ifdef CONFIG_ARM64
238static struct mm_region bcm2837_mem_map[] = {
239 {
York Sunc7104e52016-06-24 16:46:22 -0700240 .virt = 0x00000000UL,
241 .phys = 0x00000000UL,
Stephen Warren93ea5262016-04-01 21:14:15 -0600242 .size = 0x3f000000UL,
243 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
244 PTE_BLOCK_INNER_SHARE
245 }, {
York Sunc7104e52016-06-24 16:46:22 -0700246 .virt = 0x3f000000UL,
247 .phys = 0x3f000000UL,
Stephen Warren93ea5262016-04-01 21:14:15 -0600248 .size = 0x01000000UL,
249 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
250 PTE_BLOCK_NON_SHARE |
251 PTE_BLOCK_PXN | PTE_BLOCK_UXN
252 }, {
253 /* List terminator */
254 0,
255 }
256};
257
258struct mm_region *mem_map = bcm2837_mem_map;
259#endif
260
Stephen Warren0e012c32012-08-05 16:07:22 +0000261int dram_init(void)
262{
Alexander Steinc56c9472015-07-24 09:22:12 +0200263 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_arm_mem, msg, 1);
Stephen Warrenacc8bf42013-01-29 16:37:37 +0000264 int ret;
265
266 BCM2835_MBOX_INIT_HDR(msg);
267 BCM2835_MBOX_INIT_TAG(&msg->get_arm_mem, GET_ARM_MEMORY);
268
269 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
270 if (ret) {
271 printf("bcm2835: Could not query ARM memory size\n");
272 return -1;
273 }
274
275 gd->ram_size = msg->get_arm_mem.body.resp.mem_size;
Stephen Warren0e012c32012-08-05 16:07:22 +0000276
277 return 0;
278}
279
Stephen Warrencd210c12014-11-18 21:40:21 -0700280static void set_fdtfile(void)
281{
282 const char *fdtfile;
283
284 if (getenv("fdtfile"))
285 return;
286
Stephen Warren26683852015-12-04 22:07:45 -0700287 fdtfile = model->fdtfile;
Stephen Warrencd210c12014-11-18 21:40:21 -0700288 setenv("fdtfile", fdtfile);
289}
290
Cédric Schieli9473f532016-11-11 11:59:07 +0100291/*
292 * If the firmware provided a valid FDT at boot time, let's expose it in
293 * ${fdt_addr} so it may be passed unmodified to the kernel.
294 */
295static void set_fdt_addr(void)
296{
297 if (getenv("fdt_addr"))
298 return;
299
300 if (fdt_magic(fw_dtb_pointer) != FDT_MAGIC)
301 return;
302
303 setenv_hex("fdt_addr", fw_dtb_pointer);
304}
305
306/*
307 * Prevent relocation from stomping on a firmware provided FDT blob.
308 */
309unsigned long board_get_usable_ram_top(unsigned long total_size)
310{
311 if ((gd->ram_top - fw_dtb_pointer) > SZ_64M)
312 return gd->ram_top;
313 return fw_dtb_pointer & ~0xffff;
314}
315
Stephen Warrencd210c12014-11-18 21:40:21 -0700316static void set_usbethaddr(void)
Stephen Warrenaf6e20d2014-09-26 20:51:39 -0600317{
Alexander Steinc56c9472015-07-24 09:22:12 +0200318 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_mac_address, msg, 1);
Stephen Warrenaf6e20d2014-09-26 20:51:39 -0600319 int ret;
320
Stephen Warren26683852015-12-04 22:07:45 -0700321 if (!model->has_onboard_eth)
Stephen Warrenb2fa38362014-12-05 20:56:46 -0700322 return;
323
Stephen Warrenaf6e20d2014-09-26 20:51:39 -0600324 if (getenv("usbethaddr"))
Stephen Warrencd210c12014-11-18 21:40:21 -0700325 return;
Stephen Warrenaf6e20d2014-09-26 20:51:39 -0600326
327 BCM2835_MBOX_INIT_HDR(msg);
328 BCM2835_MBOX_INIT_TAG(&msg->get_mac_address, GET_MAC_ADDRESS);
329
330 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
331 if (ret) {
332 printf("bcm2835: Could not query MAC address\n");
333 /* Ignore error; not critical */
Stephen Warrencd210c12014-11-18 21:40:21 -0700334 return;
Stephen Warrenaf6e20d2014-09-26 20:51:39 -0600335 }
336
337 eth_setenv_enetaddr("usbethaddr", msg->get_mac_address.body.resp.mac);
338
Lubomir Rintel3ac09f82016-02-03 16:08:09 +0100339 if (!getenv("ethaddr"))
340 setenv("ethaddr", getenv("usbethaddr"));
341
Stephen Warrencd210c12014-11-18 21:40:21 -0700342 return;
343}
344
Guillaume GARDET25fa8602015-08-25 15:10:26 +0200345#ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
346static void set_board_info(void)
347{
Stephen Warren26683852015-12-04 22:07:45 -0700348 char s[11];
349
350 snprintf(s, sizeof(s), "0x%X", revision);
351 setenv("board_revision", s);
352 snprintf(s, sizeof(s), "%d", rev_scheme);
353 setenv("board_rev_scheme", s);
354 /* Can't rename this to board_rev_type since it's an ABI for scripts */
355 snprintf(s, sizeof(s), "0x%X", rev_type);
356 setenv("board_rev", s);
357 setenv("board_name", model->name);
Guillaume GARDET25fa8602015-08-25 15:10:26 +0200358}
359#endif /* CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG */
360
Lubomir Rintel7d33bb62016-02-22 22:06:47 +0100361static void set_serial_number(void)
362{
363 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_serial, msg, 1);
364 int ret;
365 char serial_string[17] = { 0 };
366
367 if (getenv("serial#"))
368 return;
369
370 BCM2835_MBOX_INIT_HDR(msg);
371 BCM2835_MBOX_INIT_TAG_NO_REQ(&msg->get_board_serial, GET_BOARD_SERIAL);
372
373 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
374 if (ret) {
375 printf("bcm2835: Could not query board serial\n");
376 /* Ignore error; not critical */
377 return;
378 }
379
380 snprintf(serial_string, sizeof(serial_string), "%016" PRIx64,
381 msg->get_board_serial.body.resp.serial);
382 setenv("serial#", serial_string);
383}
384
Stephen Warrencd210c12014-11-18 21:40:21 -0700385int misc_init_r(void)
386{
Cédric Schieli9473f532016-11-11 11:59:07 +0100387 set_fdt_addr();
Stephen Warrencd210c12014-11-18 21:40:21 -0700388 set_fdtfile();
389 set_usbethaddr();
Guillaume GARDET25fa8602015-08-25 15:10:26 +0200390#ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
391 set_board_info();
392#endif
Lubomir Rintel7d33bb62016-02-22 22:06:47 +0100393 set_serial_number();
394
Stephen Warrenaf6e20d2014-09-26 20:51:39 -0600395 return 0;
396}
397
Stephen Warren8672d202014-01-13 19:50:11 -0700398static int power_on_module(u32 module)
399{
Alexander Steinc56c9472015-07-24 09:22:12 +0200400 ALLOC_CACHE_ALIGN_BUFFER(struct msg_set_power_state, msg_pwr, 1);
Stephen Warren8672d202014-01-13 19:50:11 -0700401 int ret;
402
403 BCM2835_MBOX_INIT_HDR(msg_pwr);
404 BCM2835_MBOX_INIT_TAG(&msg_pwr->set_power_state,
405 SET_POWER_STATE);
406 msg_pwr->set_power_state.body.req.device_id = module;
407 msg_pwr->set_power_state.body.req.state =
408 BCM2835_MBOX_SET_POWER_STATE_REQ_ON |
409 BCM2835_MBOX_SET_POWER_STATE_REQ_WAIT;
410
411 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN,
412 &msg_pwr->hdr);
413 if (ret) {
414 printf("bcm2835: Could not set module %u power state\n",
415 module);
416 return -1;
417 }
418
419 return 0;
420}
421
Stephen Warrencd210c12014-11-18 21:40:21 -0700422static void get_board_rev(void)
423{
Alexander Steinc56c9472015-07-24 09:22:12 +0200424 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_rev, msg, 1);
Stephen Warrencd210c12014-11-18 21:40:21 -0700425 int ret;
Stephen Warren26683852015-12-04 22:07:45 -0700426 const struct rpi_model *models;
427 uint32_t models_count;
Stephen Warrencd210c12014-11-18 21:40:21 -0700428
429 BCM2835_MBOX_INIT_HDR(msg);
430 BCM2835_MBOX_INIT_TAG(&msg->get_board_rev, GET_BOARD_REV);
431
432 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
433 if (ret) {
434 printf("bcm2835: Could not query board revision\n");
435 /* Ignore error; not critical */
436 return;
437 }
438
Stephen Warrendc7ea682015-02-16 12:16:15 -0700439 /*
440 * For details of old-vs-new scheme, see:
441 * https://github.com/pimoroni/RPi.version/blob/master/RPi/version.py
442 * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=99293&p=690282
443 * (a few posts down)
Stephen Warrenb834af82015-03-23 23:00:25 -0600444 *
445 * For the RPi 1, bit 24 is the "warranty bit", so we mask off just the
446 * lower byte to use as the board rev:
447 * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=98367&start=250
448 * http://www.raspberrypi.org/forums/viewtopic.php?f=31&t=20594
Stephen Warrendc7ea682015-02-16 12:16:15 -0700449 */
Stephen Warren26683852015-12-04 22:07:45 -0700450 revision = msg->get_board_rev.body.resp.rev;
451 if (revision & 0x800000) {
452 rev_scheme = 1;
453 rev_type = (revision >> 4) & 0xff;
454 models = rpi_models_new_scheme;
455 models_count = ARRAY_SIZE(rpi_models_new_scheme);
456 } else {
457 rev_scheme = 0;
458 rev_type = revision & 0xff;
459 models = rpi_models_old_scheme;
460 models_count = ARRAY_SIZE(rpi_models_old_scheme);
Stephen Warren18a5fc62014-12-23 20:01:43 -0700461 }
Stephen Warren26683852015-12-04 22:07:45 -0700462 if (rev_type >= models_count) {
463 printf("RPI: Board rev 0x%x outside known range\n", rev_type);
464 model = &rpi_model_unknown;
465 } else if (!models[rev_type].name) {
466 printf("RPI: Board rev 0x%x unknown\n", rev_type);
467 model = &rpi_model_unknown;
468 } else {
469 model = &models[rev_type];
Stephen Warren18a5fc62014-12-23 20:01:43 -0700470 }
Stephen Warrenc61dfd12014-12-23 20:01:44 -0700471
Stephen Warren26683852015-12-04 22:07:45 -0700472 printf("RPI %s (0x%x)\n", model->name, revision);
Stephen Warrencd210c12014-11-18 21:40:21 -0700473}
474
Alexander Grafafb99072016-08-15 17:48:51 +0200475#ifndef CONFIG_PL01X_SERIAL
476static bool rpi_is_serial_active(void)
477{
478 int serial_gpio = 15;
479 struct udevice *dev;
480
481 /*
482 * The RPi3 disables the mini uart by default. The easiest way to find
483 * out whether it is available is to check if the RX pin is muxed.
484 */
485
486 if (uclass_first_device(UCLASS_GPIO, &dev) || !dev)
487 return true;
488
489 if (bcm2835_gpio_get_func_id(dev, serial_gpio) != BCM2835_GPIO_ALT5)
490 return false;
491
492 return true;
493}
Fabian Vogt1d317b72016-09-26 14:26:50 +0200494
495/* Disable mini-UART I/O if it's not pinmuxed to our pins.
496 * The firmware only enables it if explicitly done in config.txt: enable_uart=1
497 */
498static void rpi_disable_inactive_uart(void)
499{
500 struct udevice *dev;
501 struct bcm283x_mu_serial_platdata *plat;
502
503 if (uclass_get_device_by_driver(UCLASS_SERIAL,
504 DM_GET_DRIVER(serial_bcm283x_mu),
505 &dev) || !dev)
506 return;
507
508 if (!rpi_is_serial_active()) {
509 plat = dev_get_platdata(dev);
510 plat->disabled = true;
511 }
512}
Alexander Grafafb99072016-08-15 17:48:51 +0200513#endif
514
Fabian Vogt1d317b72016-09-26 14:26:50 +0200515int board_init(void)
Alexander Grafafb99072016-08-15 17:48:51 +0200516{
517#ifndef CONFIG_PL01X_SERIAL
Fabian Vogt1d317b72016-09-26 14:26:50 +0200518 rpi_disable_inactive_uart();
Alexander Grafafb99072016-08-15 17:48:51 +0200519#endif
520
Fabian Vogt1d317b72016-09-26 14:26:50 +0200521 get_board_rev();
522
523 gd->bd->bi_boot_params = 0x100;
524
525 return power_on_module(BCM2835_MBOX_POWER_DEVID_USB_HCD);
Alexander Grafafb99072016-08-15 17:48:51 +0200526}
527
Jeroen Hofsteed5b2fed2014-07-13 22:01:51 +0200528int board_mmc_init(bd_t *bis)
Stephen Warrenc4ab9712013-01-29 16:37:42 +0000529{
Alexander Steinc56c9472015-07-24 09:22:12 +0200530 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_clock_rate, msg_clk, 1);
Stephen Warrenc4ab9712013-01-29 16:37:42 +0000531 int ret;
532
Stephen Warren8672d202014-01-13 19:50:11 -0700533 power_on_module(BCM2835_MBOX_POWER_DEVID_SDHCI);
534
Stephen Warrenc4ab9712013-01-29 16:37:42 +0000535 BCM2835_MBOX_INIT_HDR(msg_clk);
536 BCM2835_MBOX_INIT_TAG(&msg_clk->get_clock_rate, GET_CLOCK_RATE);
537 msg_clk->get_clock_rate.body.req.clock_id = BCM2835_MBOX_CLOCK_ID_EMMC;
538
539 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg_clk->hdr);
540 if (ret) {
541 printf("bcm2835: Could not query eMMC clock rate\n");
542 return -1;
543 }
544
545 return bcm2835_sdhci_init(BCM2835_SDHCI_BASE,
546 msg_clk->get_clock_rate.body.resp.rate_hz);
547}
Stephen Warrenaa44d532013-05-27 18:31:18 +0000548
Simon Glass2aec3cc2014-10-23 18:58:47 -0600549int ft_board_setup(void *blob, bd_t *bd)
Stephen Warrenaa44d532013-05-27 18:31:18 +0000550{
551 /*
552 * For now, we simply always add the simplefb DT node. Later, we
553 * should be more intelligent, and e.g. only do this if no enabled DT
554 * node exists for the "real" graphics driver.
555 */
556 lcd_dt_simplefb_add_node(blob);
Simon Glass2aec3cc2014-10-23 18:58:47 -0600557
558 return 0;
Stephen Warrenaa44d532013-05-27 18:31:18 +0000559}