blob: d3c6ba580f5cadf7e1b387a2d580424c5dcde138 [file] [log] [blame]
Stephen Warren0d04f342012-08-05 16:07:22 +00001/*
Stephen Warrenf031f502016-03-24 22:15:20 -06002 * (C) Copyright 2012-2016 Stephen Warren
Stephen Warren0d04f342012-08-05 16:07:22 +00003 *
Stephen Warrena0331712015-02-16 12:16:13 -07004 * SPDX-License-Identifier: GPL-2.0
Stephen Warren0d04f342012-08-05 16:07:22 +00005 */
6
7#include <common.h>
Lubomir Rintel757cd142016-02-22 22:06:47 +01008#include <inttypes.h>
Stephen Warrenea697ae2013-05-27 18:31:18 +00009#include <config.h>
Simon Glass41e98e02014-09-22 17:30:56 -060010#include <dm.h>
Alexander Graf1bcf7a32016-11-02 10:36:20 +010011#include <efi_loader.h>
Jeroen Hofstee5dfd1622014-07-13 22:01:51 +020012#include <fdt_support.h>
Nikita Kiryanov033167c2015-02-03 13:32:31 +020013#include <fdt_simplefb.h>
Stephen Warrenea697ae2013-05-27 18:31:18 +000014#include <lcd.h>
Simon Glasscf92e052015-09-02 17:24:58 -060015#include <memalign.h>
Jeroen Hofstee5dfd1622014-07-13 22:01:51 +020016#include <mmc.h>
Simon Glass41e98e02014-09-22 17:30:56 -060017#include <asm/gpio.h>
Stephen Warren3f397782013-01-29 16:37:37 +000018#include <asm/arch/mbox.h>
Simon Glass70997d82017-04-05 16:23:36 -060019#include <asm/arch/msg.h>
Stephen Warren131a1e62013-01-29 16:37:42 +000020#include <asm/arch/sdhci.h>
Stephen Warren0d04f342012-08-05 16:07:22 +000021#include <asm/global_data.h>
Stephen Warrenf031f502016-03-24 22:15:20 -060022#include <dm/platform_data/serial_bcm283x_mu.h>
Stephen Warrend22a7652016-04-01 21:14:15 -060023#ifdef CONFIG_ARM64
24#include <asm/armv8/mmu.h>
25#endif
Paolo Pisati45a6d232017-02-10 17:28:05 +010026#include <watchdog.h>
Stephen Warren0d04f342012-08-05 16:07:22 +000027
28DECLARE_GLOBAL_DATA_PTR;
29
Cédric Schieliade243a2016-11-11 11:59:07 +010030/* From lowlevel_init.S */
31extern unsigned long fw_dtb_pointer;
32
Simon Glass3e167052017-04-05 16:23:45 -060033/* TODO(sjg@chromium.org): Move these to the msg.c file */
Stephen Warren3f397782013-01-29 16:37:37 +000034struct msg_get_arm_mem {
35 struct bcm2835_mbox_hdr hdr;
36 struct bcm2835_mbox_tag_get_arm_mem get_arm_mem;
37 u32 end_tag;
38};
39
Stephen Warren6fe78452014-11-18 21:40:21 -070040struct msg_get_board_rev {
41 struct bcm2835_mbox_hdr hdr;
42 struct bcm2835_mbox_tag_get_board_rev get_board_rev;
43 u32 end_tag;
44};
45
Lubomir Rintel757cd142016-02-22 22:06:47 +010046struct msg_get_board_serial {
47 struct bcm2835_mbox_hdr hdr;
48 struct bcm2835_mbox_tag_get_board_serial get_board_serial;
49 u32 end_tag;
50};
51
Stephen Warren4f80a062014-09-26 20:51:39 -060052struct msg_get_mac_address {
53 struct bcm2835_mbox_hdr hdr;
54 struct bcm2835_mbox_tag_get_mac_address get_mac_address;
55 u32 end_tag;
56};
57
Stephen Warren131a1e62013-01-29 16:37:42 +000058struct msg_get_clock_rate {
59 struct bcm2835_mbox_hdr hdr;
60 struct bcm2835_mbox_tag_get_clock_rate get_clock_rate;
61 u32 end_tag;
62};
63
Tuomas Tynkkynen5d3c4ba2017-01-23 01:34:39 +020064#ifdef CONFIG_ARM64
65#define DTB_DIR "broadcom/"
66#else
67#define DTB_DIR ""
68#endif
69
Stephen Warrendbe6f1e2015-12-04 22:07:44 -070070/*
71 * http://raspberryalphaomega.org.uk/2013/02/06/automatic-raspberry-pi-board-revision-detection-model-a-b1-and-b2/
72 * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=32733
Stephen Warrenc4ea1ed2015-12-04 22:07:45 -070073 * http://git.drogon.net/?p=wiringPi;a=blob;f=wiringPi/wiringPi.c;h=503151f61014418b9c42f4476a6086f75cd4e64b;hb=refs/heads/master#l922
Stephen Warrendba060c2016-01-28 22:24:44 -070074 *
75 * In http://lists.denx.de/pipermail/u-boot/2016-January/243752.html
76 * ("[U-Boot] [PATCH] rpi: fix up Model B entries") Dom Cobley at the RPi
77 * Foundation stated that the following source was accurate:
78 * https://github.com/AndrewFromMelbourne/raspberry_pi_revision
Stephen Warrendbe6f1e2015-12-04 22:07:44 -070079 */
Stephen Warrenc4ea1ed2015-12-04 22:07:45 -070080struct rpi_model {
Stephen Warren6fe78452014-11-18 21:40:21 -070081 const char *name;
82 const char *fdtfile;
Stephen Warren3207d8f2014-12-05 20:56:46 -070083 bool has_onboard_eth;
Stephen Warrenc4ea1ed2015-12-04 22:07:45 -070084};
85
86static const struct rpi_model rpi_model_unknown = {
87 "Unknown model",
Tuomas Tynkkynen5d3c4ba2017-01-23 01:34:39 +020088 DTB_DIR "bcm283x-rpi-other.dtb",
Stephen Warrenc4ea1ed2015-12-04 22:07:45 -070089 false,
90};
91
92static const struct rpi_model rpi_models_new_scheme[] = {
Stephen Warrendbe6f1e2015-12-04 22:07:44 -070093 [0x4] = {
Stephen Warren46414292015-02-16 12:16:15 -070094 "2 Model B",
Tuomas Tynkkynen5d3c4ba2017-01-23 01:34:39 +020095 DTB_DIR "bcm2836-rpi-2-b.dtb",
Stephen Warren46414292015-02-16 12:16:15 -070096 true,
97 },
Stephen Warren7233fb32016-03-24 22:15:18 -060098 [0x8] = {
99 "3 Model B",
Tuomas Tynkkynen5d3c4ba2017-01-23 01:34:39 +0200100 DTB_DIR "bcm2837-rpi-3-b.dtb",
Stephen Warren7233fb32016-03-24 22:15:18 -0600101 true,
102 },
Stephen Warrenaf7c03e2015-12-04 22:07:46 -0700103 [0x9] = {
104 "Zero",
Tuomas Tynkkynen5d3c4ba2017-01-23 01:34:39 +0200105 DTB_DIR "bcm2835-rpi-zero.dtb",
Stephen Warrenaf7c03e2015-12-04 22:07:46 -0700106 false,
107 },
Stephen Warrenc4ea1ed2015-12-04 22:07:45 -0700108};
109
110static const struct rpi_model rpi_models_old_scheme[] = {
Stephen Warrendbe6f1e2015-12-04 22:07:44 -0700111 [0x2] = {
Lubomir Rintel7443a9c2016-01-29 09:35:52 +0100112 "Model B",
Tuomas Tynkkynen5d3c4ba2017-01-23 01:34:39 +0200113 DTB_DIR "bcm2835-rpi-b.dtb",
Stephen Warren3207d8f2014-12-05 20:56:46 -0700114 true,
Stephen Warren6fe78452014-11-18 21:40:21 -0700115 },
Stephen Warrendbe6f1e2015-12-04 22:07:44 -0700116 [0x3] = {
Lubomir Rintel7443a9c2016-01-29 09:35:52 +0100117 "Model B",
Tuomas Tynkkynen5d3c4ba2017-01-23 01:34:39 +0200118 DTB_DIR "bcm2835-rpi-b.dtb",
Stephen Warren3207d8f2014-12-05 20:56:46 -0700119 true,
Stephen Warren6fe78452014-11-18 21:40:21 -0700120 },
Stephen Warrendbe6f1e2015-12-04 22:07:44 -0700121 [0x4] = {
Lubomir Rintel7443a9c2016-01-29 09:35:52 +0100122 "Model B rev2",
Tuomas Tynkkynen5d3c4ba2017-01-23 01:34:39 +0200123 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
Stephen Warren3207d8f2014-12-05 20:56:46 -0700124 true,
Stephen Warren6fe78452014-11-18 21:40:21 -0700125 },
Stephen Warrendbe6f1e2015-12-04 22:07:44 -0700126 [0x5] = {
Lubomir Rintel7443a9c2016-01-29 09:35:52 +0100127 "Model B rev2",
Tuomas Tynkkynen5d3c4ba2017-01-23 01:34:39 +0200128 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
Stephen Warren3207d8f2014-12-05 20:56:46 -0700129 true,
Stephen Warren6fe78452014-11-18 21:40:21 -0700130 },
Stephen Warrendbe6f1e2015-12-04 22:07:44 -0700131 [0x6] = {
Lubomir Rintel7443a9c2016-01-29 09:35:52 +0100132 "Model B rev2",
Tuomas Tynkkynen5d3c4ba2017-01-23 01:34:39 +0200133 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
Stephen Warren3207d8f2014-12-05 20:56:46 -0700134 true,
Stephen Warren6fe78452014-11-18 21:40:21 -0700135 },
Stephen Warrendbe6f1e2015-12-04 22:07:44 -0700136 [0x7] = {
Stephen Warren6fe78452014-11-18 21:40:21 -0700137 "Model A",
Tuomas Tynkkynen5d3c4ba2017-01-23 01:34:39 +0200138 DTB_DIR "bcm2835-rpi-a.dtb",
Stephen Warren3207d8f2014-12-05 20:56:46 -0700139 false,
Stephen Warren6fe78452014-11-18 21:40:21 -0700140 },
Stephen Warrendbe6f1e2015-12-04 22:07:44 -0700141 [0x8] = {
Stephen Warren6fe78452014-11-18 21:40:21 -0700142 "Model A",
Tuomas Tynkkynen5d3c4ba2017-01-23 01:34:39 +0200143 DTB_DIR "bcm2835-rpi-a.dtb",
Stephen Warren3207d8f2014-12-05 20:56:46 -0700144 false,
Stephen Warren6fe78452014-11-18 21:40:21 -0700145 },
Stephen Warrendbe6f1e2015-12-04 22:07:44 -0700146 [0x9] = {
Stephen Warren6fe78452014-11-18 21:40:21 -0700147 "Model A",
Tuomas Tynkkynen5d3c4ba2017-01-23 01:34:39 +0200148 DTB_DIR "bcm2835-rpi-a.dtb",
Stephen Warren3207d8f2014-12-05 20:56:46 -0700149 false,
Stephen Warren6fe78452014-11-18 21:40:21 -0700150 },
Stephen Warrendbe6f1e2015-12-04 22:07:44 -0700151 [0xd] = {
Stephen Warren6fe78452014-11-18 21:40:21 -0700152 "Model B rev2",
Tuomas Tynkkynen5d3c4ba2017-01-23 01:34:39 +0200153 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
Stephen Warren3207d8f2014-12-05 20:56:46 -0700154 true,
Stephen Warren6fe78452014-11-18 21:40:21 -0700155 },
Stephen Warrendbe6f1e2015-12-04 22:07:44 -0700156 [0xe] = {
Stephen Warren6fe78452014-11-18 21:40:21 -0700157 "Model B rev2",
Tuomas Tynkkynen5d3c4ba2017-01-23 01:34:39 +0200158 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
Stephen Warren3207d8f2014-12-05 20:56:46 -0700159 true,
Stephen Warren6fe78452014-11-18 21:40:21 -0700160 },
Stephen Warrendbe6f1e2015-12-04 22:07:44 -0700161 [0xf] = {
Stephen Warren6fe78452014-11-18 21:40:21 -0700162 "Model B rev2",
Tuomas Tynkkynen5d3c4ba2017-01-23 01:34:39 +0200163 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
Stephen Warren3207d8f2014-12-05 20:56:46 -0700164 true,
Stephen Warren6fe78452014-11-18 21:40:21 -0700165 },
Stephen Warrendbe6f1e2015-12-04 22:07:44 -0700166 [0x10] = {
Stephen Warren6fe78452014-11-18 21:40:21 -0700167 "Model B+",
Tuomas Tynkkynen5d3c4ba2017-01-23 01:34:39 +0200168 DTB_DIR "bcm2835-rpi-b-plus.dtb",
Stephen Warren3207d8f2014-12-05 20:56:46 -0700169 true,
Stephen Warren6fe78452014-11-18 21:40:21 -0700170 },
Stephen Warrendbe6f1e2015-12-04 22:07:44 -0700171 [0x11] = {
Stephen Warren6fe78452014-11-18 21:40:21 -0700172 "Compute Module",
Tuomas Tynkkynen5d3c4ba2017-01-23 01:34:39 +0200173 DTB_DIR "bcm2835-rpi-cm.dtb",
Stephen Warren3207d8f2014-12-05 20:56:46 -0700174 false,
Stephen Warren6fe78452014-11-18 21:40:21 -0700175 },
Stephen Warrendbe6f1e2015-12-04 22:07:44 -0700176 [0x12] = {
Stephen Warren47705ef2014-12-23 20:01:43 -0700177 "Model A+",
Tuomas Tynkkynen5d3c4ba2017-01-23 01:34:39 +0200178 DTB_DIR "bcm2835-rpi-a-plus.dtb",
Stephen Warren47705ef2014-12-23 20:01:43 -0700179 false,
180 },
Stephen Warrendbe6f1e2015-12-04 22:07:44 -0700181 [0x13] = {
Stephen Warren787affb2015-04-12 21:43:25 -0600182 "Model B+",
Tuomas Tynkkynen5d3c4ba2017-01-23 01:34:39 +0200183 DTB_DIR "bcm2835-rpi-b-plus.dtb",
Stephen Warren787affb2015-04-12 21:43:25 -0600184 true,
185 },
Stephen Warrendbe6f1e2015-12-04 22:07:44 -0700186 [0x14] = {
Stephen Warren787affb2015-04-12 21:43:25 -0600187 "Compute Module",
Tuomas Tynkkynen5d3c4ba2017-01-23 01:34:39 +0200188 DTB_DIR "bcm2835-rpi-cm.dtb",
Stephen Warren787affb2015-04-12 21:43:25 -0600189 false,
190 },
Stephen Warrendbe6f1e2015-12-04 22:07:44 -0700191 [0x15] = {
Lubomir Rintel79ad5ce2015-10-14 17:17:54 +0200192 "Model A+",
Tuomas Tynkkynen5d3c4ba2017-01-23 01:34:39 +0200193 DTB_DIR "bcm2835-rpi-a-plus.dtb",
Lubomir Rintel79ad5ce2015-10-14 17:17:54 +0200194 false,
195 },
Stephen Warren6fe78452014-11-18 21:40:21 -0700196};
197
Stephen Warrenc4ea1ed2015-12-04 22:07:45 -0700198static uint32_t revision;
199static uint32_t rev_scheme;
200static uint32_t rev_type;
201static const struct rpi_model *model;
Stephen Warren6fe78452014-11-18 21:40:21 -0700202
Stephen Warrend22a7652016-04-01 21:14:15 -0600203#ifdef CONFIG_ARM64
204static struct mm_region bcm2837_mem_map[] = {
205 {
York Suncd4b0c52016-06-24 16:46:22 -0700206 .virt = 0x00000000UL,
207 .phys = 0x00000000UL,
Stephen Warrend22a7652016-04-01 21:14:15 -0600208 .size = 0x3f000000UL,
209 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
210 PTE_BLOCK_INNER_SHARE
211 }, {
York Suncd4b0c52016-06-24 16:46:22 -0700212 .virt = 0x3f000000UL,
213 .phys = 0x3f000000UL,
Stephen Warrend22a7652016-04-01 21:14:15 -0600214 .size = 0x01000000UL,
215 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
216 PTE_BLOCK_NON_SHARE |
217 PTE_BLOCK_PXN | PTE_BLOCK_UXN
218 }, {
219 /* List terminator */
220 0,
221 }
222};
223
224struct mm_region *mem_map = bcm2837_mem_map;
225#endif
226
Stephen Warren0d04f342012-08-05 16:07:22 +0000227int dram_init(void)
228{
Alexander Stein927753a2015-07-24 09:22:12 +0200229 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_arm_mem, msg, 1);
Stephen Warren3f397782013-01-29 16:37:37 +0000230 int ret;
231
232 BCM2835_MBOX_INIT_HDR(msg);
233 BCM2835_MBOX_INIT_TAG(&msg->get_arm_mem, GET_ARM_MEMORY);
234
235 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
236 if (ret) {
237 printf("bcm2835: Could not query ARM memory size\n");
238 return -1;
239 }
240
241 gd->ram_size = msg->get_arm_mem.body.resp.mem_size;
Stephen Warren0d04f342012-08-05 16:07:22 +0000242
243 return 0;
244}
245
Stephen Warren6fe78452014-11-18 21:40:21 -0700246static void set_fdtfile(void)
247{
248 const char *fdtfile;
249
250 if (getenv("fdtfile"))
251 return;
252
Stephen Warrenc4ea1ed2015-12-04 22:07:45 -0700253 fdtfile = model->fdtfile;
Stephen Warren6fe78452014-11-18 21:40:21 -0700254 setenv("fdtfile", fdtfile);
255}
256
Cédric Schieliade243a2016-11-11 11:59:07 +0100257/*
258 * If the firmware provided a valid FDT at boot time, let's expose it in
259 * ${fdt_addr} so it may be passed unmodified to the kernel.
260 */
261static void set_fdt_addr(void)
262{
263 if (getenv("fdt_addr"))
264 return;
265
266 if (fdt_magic(fw_dtb_pointer) != FDT_MAGIC)
267 return;
268
269 setenv_hex("fdt_addr", fw_dtb_pointer);
270}
271
272/*
273 * Prevent relocation from stomping on a firmware provided FDT blob.
274 */
275unsigned long board_get_usable_ram_top(unsigned long total_size)
276{
277 if ((gd->ram_top - fw_dtb_pointer) > SZ_64M)
278 return gd->ram_top;
279 return fw_dtb_pointer & ~0xffff;
280}
281
Stephen Warren6fe78452014-11-18 21:40:21 -0700282static void set_usbethaddr(void)
Stephen Warren4f80a062014-09-26 20:51:39 -0600283{
Alexander Stein927753a2015-07-24 09:22:12 +0200284 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_mac_address, msg, 1);
Stephen Warren4f80a062014-09-26 20:51:39 -0600285 int ret;
286
Stephen Warrenc4ea1ed2015-12-04 22:07:45 -0700287 if (!model->has_onboard_eth)
Stephen Warren3207d8f2014-12-05 20:56:46 -0700288 return;
289
Stephen Warren4f80a062014-09-26 20:51:39 -0600290 if (getenv("usbethaddr"))
Stephen Warren6fe78452014-11-18 21:40:21 -0700291 return;
Stephen Warren4f80a062014-09-26 20:51:39 -0600292
293 BCM2835_MBOX_INIT_HDR(msg);
294 BCM2835_MBOX_INIT_TAG(&msg->get_mac_address, GET_MAC_ADDRESS);
295
296 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
297 if (ret) {
298 printf("bcm2835: Could not query MAC address\n");
299 /* Ignore error; not critical */
Stephen Warren6fe78452014-11-18 21:40:21 -0700300 return;
Stephen Warren4f80a062014-09-26 20:51:39 -0600301 }
302
303 eth_setenv_enetaddr("usbethaddr", msg->get_mac_address.body.resp.mac);
304
Lubomir Rintel859f1432016-02-03 16:08:09 +0100305 if (!getenv("ethaddr"))
306 setenv("ethaddr", getenv("usbethaddr"));
307
Stephen Warren6fe78452014-11-18 21:40:21 -0700308 return;
309}
310
Guillaume GARDETbff78562015-08-25 15:10:26 +0200311#ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
312static void set_board_info(void)
313{
Stephen Warrenc4ea1ed2015-12-04 22:07:45 -0700314 char s[11];
315
316 snprintf(s, sizeof(s), "0x%X", revision);
317 setenv("board_revision", s);
318 snprintf(s, sizeof(s), "%d", rev_scheme);
319 setenv("board_rev_scheme", s);
320 /* Can't rename this to board_rev_type since it's an ABI for scripts */
321 snprintf(s, sizeof(s), "0x%X", rev_type);
322 setenv("board_rev", s);
323 setenv("board_name", model->name);
Guillaume GARDETbff78562015-08-25 15:10:26 +0200324}
325#endif /* CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG */
326
Lubomir Rintel757cd142016-02-22 22:06:47 +0100327static void set_serial_number(void)
328{
329 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_serial, msg, 1);
330 int ret;
331 char serial_string[17] = { 0 };
332
333 if (getenv("serial#"))
334 return;
335
336 BCM2835_MBOX_INIT_HDR(msg);
337 BCM2835_MBOX_INIT_TAG_NO_REQ(&msg->get_board_serial, GET_BOARD_SERIAL);
338
339 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
340 if (ret) {
341 printf("bcm2835: Could not query board serial\n");
342 /* Ignore error; not critical */
343 return;
344 }
345
346 snprintf(serial_string, sizeof(serial_string), "%016" PRIx64,
347 msg->get_board_serial.body.resp.serial);
348 setenv("serial#", serial_string);
349}
350
Stephen Warren6fe78452014-11-18 21:40:21 -0700351int misc_init_r(void)
352{
Cédric Schieliade243a2016-11-11 11:59:07 +0100353 set_fdt_addr();
Stephen Warren6fe78452014-11-18 21:40:21 -0700354 set_fdtfile();
355 set_usbethaddr();
Guillaume GARDETbff78562015-08-25 15:10:26 +0200356#ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
357 set_board_info();
358#endif
Lubomir Rintel757cd142016-02-22 22:06:47 +0100359 set_serial_number();
360
Stephen Warren4f80a062014-09-26 20:51:39 -0600361 return 0;
362}
363
Stephen Warren6fe78452014-11-18 21:40:21 -0700364static void get_board_rev(void)
365{
Alexander Stein927753a2015-07-24 09:22:12 +0200366 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_rev, msg, 1);
Stephen Warren6fe78452014-11-18 21:40:21 -0700367 int ret;
Stephen Warrenc4ea1ed2015-12-04 22:07:45 -0700368 const struct rpi_model *models;
369 uint32_t models_count;
Stephen Warren6fe78452014-11-18 21:40:21 -0700370
371 BCM2835_MBOX_INIT_HDR(msg);
372 BCM2835_MBOX_INIT_TAG(&msg->get_board_rev, GET_BOARD_REV);
373
374 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
375 if (ret) {
376 printf("bcm2835: Could not query board revision\n");
377 /* Ignore error; not critical */
378 return;
379 }
380
Stephen Warren46414292015-02-16 12:16:15 -0700381 /*
382 * For details of old-vs-new scheme, see:
383 * https://github.com/pimoroni/RPi.version/blob/master/RPi/version.py
384 * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=99293&p=690282
385 * (a few posts down)
Stephen Warren95b4f112015-03-23 23:00:25 -0600386 *
387 * For the RPi 1, bit 24 is the "warranty bit", so we mask off just the
388 * lower byte to use as the board rev:
389 * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=98367&start=250
390 * http://www.raspberrypi.org/forums/viewtopic.php?f=31&t=20594
Stephen Warren46414292015-02-16 12:16:15 -0700391 */
Stephen Warrenc4ea1ed2015-12-04 22:07:45 -0700392 revision = msg->get_board_rev.body.resp.rev;
393 if (revision & 0x800000) {
394 rev_scheme = 1;
395 rev_type = (revision >> 4) & 0xff;
396 models = rpi_models_new_scheme;
397 models_count = ARRAY_SIZE(rpi_models_new_scheme);
398 } else {
399 rev_scheme = 0;
400 rev_type = revision & 0xff;
401 models = rpi_models_old_scheme;
402 models_count = ARRAY_SIZE(rpi_models_old_scheme);
Stephen Warren47705ef2014-12-23 20:01:43 -0700403 }
Stephen Warrenc4ea1ed2015-12-04 22:07:45 -0700404 if (rev_type >= models_count) {
405 printf("RPI: Board rev 0x%x outside known range\n", rev_type);
406 model = &rpi_model_unknown;
407 } else if (!models[rev_type].name) {
408 printf("RPI: Board rev 0x%x unknown\n", rev_type);
409 model = &rpi_model_unknown;
410 } else {
411 model = &models[rev_type];
Stephen Warren914627f2014-12-23 20:01:44 -0700412 }
Stephen Warren6fe78452014-11-18 21:40:21 -0700413
Stephen Warrenc4ea1ed2015-12-04 22:07:45 -0700414 printf("RPI %s (0x%x)\n", model->name, revision);
Stephen Warren6fe78452014-11-18 21:40:21 -0700415}
416
Alexander Graf601147b2016-08-15 17:48:51 +0200417#ifndef CONFIG_PL01X_SERIAL
418static bool rpi_is_serial_active(void)
419{
420 int serial_gpio = 15;
421 struct udevice *dev;
422
423 /*
424 * The RPi3 disables the mini uart by default. The easiest way to find
425 * out whether it is available is to check if the RX pin is muxed.
426 */
427
428 if (uclass_first_device(UCLASS_GPIO, &dev) || !dev)
429 return true;
430
431 if (bcm2835_gpio_get_func_id(dev, serial_gpio) != BCM2835_GPIO_ALT5)
432 return false;
433
434 return true;
435}
Fabian Vogtd8396a32016-09-26 14:26:50 +0200436
437/* Disable mini-UART I/O if it's not pinmuxed to our pins.
438 * The firmware only enables it if explicitly done in config.txt: enable_uart=1
439 */
440static void rpi_disable_inactive_uart(void)
441{
442 struct udevice *dev;
443 struct bcm283x_mu_serial_platdata *plat;
444
445 if (uclass_get_device_by_driver(UCLASS_SERIAL,
446 DM_GET_DRIVER(serial_bcm283x_mu),
447 &dev) || !dev)
448 return;
449
450 if (!rpi_is_serial_active()) {
451 plat = dev_get_platdata(dev);
452 plat->disabled = true;
453 }
454}
Alexander Graf601147b2016-08-15 17:48:51 +0200455#endif
456
Fabian Vogtd8396a32016-09-26 14:26:50 +0200457int board_init(void)
Alexander Graf601147b2016-08-15 17:48:51 +0200458{
Paolo Pisati45a6d232017-02-10 17:28:05 +0100459#ifdef CONFIG_HW_WATCHDOG
460 hw_watchdog_init();
461#endif
Alexander Graf601147b2016-08-15 17:48:51 +0200462#ifndef CONFIG_PL01X_SERIAL
Fabian Vogtd8396a32016-09-26 14:26:50 +0200463 rpi_disable_inactive_uart();
Alexander Graf601147b2016-08-15 17:48:51 +0200464#endif
465
Fabian Vogtd8396a32016-09-26 14:26:50 +0200466 get_board_rev();
467
468 gd->bd->bi_boot_params = 0x100;
469
Simon Glass70997d82017-04-05 16:23:36 -0600470 return bcm2835_power_on_module(BCM2835_MBOX_POWER_DEVID_USB_HCD);
Alexander Graf601147b2016-08-15 17:48:51 +0200471}
472
Alex Deymo82f766d2017-04-02 01:25:20 -0700473/*
474 * If the firmware passed a device tree use it for U-Boot.
475 */
476void *board_fdt_blob_setup(void)
477{
478 if (fdt_magic(fw_dtb_pointer) != FDT_MAGIC)
479 return NULL;
480 return (void *)fw_dtb_pointer;
481}
482
Simon Glasse895a4b2014-10-23 18:58:47 -0600483int ft_board_setup(void *blob, bd_t *bd)
Stephen Warrenea697ae2013-05-27 18:31:18 +0000484{
485 /*
486 * For now, we simply always add the simplefb DT node. Later, we
487 * should be more intelligent, and e.g. only do this if no enabled DT
488 * node exists for the "real" graphics driver.
489 */
490 lcd_dt_simplefb_add_node(blob);
Simon Glasse895a4b2014-10-23 18:58:47 -0600491
Alexander Graf1bcf7a32016-11-02 10:36:20 +0100492#ifdef CONFIG_EFI_LOADER
493 /* Reserve the spin table */
494 efi_add_memory_map(0, 1, EFI_RESERVED_MEMORY_TYPE, 0);
495#endif
496
Simon Glasse895a4b2014-10-23 18:58:47 -0600497 return 0;
Stephen Warrenea697ae2013-05-27 18:31:18 +0000498}