blob: 3b7a54f519d701de8f83543038d696873a415601 [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 },
Dmitry Korunov89930562017-11-26 13:38:53 +0400108 [0xC] = {
109 "Zero W",
110 DTB_DIR "bcm2835-rpi-zero-w.dtb",
111 false,
112 },
Stephen Warrenc4ea1ed2015-12-04 22:07:45 -0700113};
114
115static const struct rpi_model rpi_models_old_scheme[] = {
Stephen Warrendbe6f1e2015-12-04 22:07:44 -0700116 [0x2] = {
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 [0x3] = {
Lubomir Rintel7443a9c2016-01-29 09:35:52 +0100122 "Model B",
Tuomas Tynkkynen5d3c4ba2017-01-23 01:34:39 +0200123 DTB_DIR "bcm2835-rpi-b.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 [0x4] = {
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 [0x5] = {
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 [0x6] = {
Lubomir Rintel7443a9c2016-01-29 09:35:52 +0100137 "Model B rev2",
Tuomas Tynkkynen5d3c4ba2017-01-23 01:34:39 +0200138 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
Stephen Warren3207d8f2014-12-05 20:56:46 -0700139 true,
Stephen Warren6fe78452014-11-18 21:40:21 -0700140 },
Stephen Warrendbe6f1e2015-12-04 22:07:44 -0700141 [0x7] = {
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 [0x8] = {
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 [0x9] = {
Stephen Warren6fe78452014-11-18 21:40:21 -0700152 "Model A",
Tuomas Tynkkynen5d3c4ba2017-01-23 01:34:39 +0200153 DTB_DIR "bcm2835-rpi-a.dtb",
Stephen Warren3207d8f2014-12-05 20:56:46 -0700154 false,
Stephen Warren6fe78452014-11-18 21:40:21 -0700155 },
Stephen Warrendbe6f1e2015-12-04 22:07:44 -0700156 [0xd] = {
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 [0xe] = {
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 [0xf] = {
Stephen Warren6fe78452014-11-18 21:40:21 -0700167 "Model B rev2",
Tuomas Tynkkynen5d3c4ba2017-01-23 01:34:39 +0200168 DTB_DIR "bcm2835-rpi-b-rev2.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 [0x10] = {
Stephen Warren6fe78452014-11-18 21:40:21 -0700172 "Model B+",
Tuomas Tynkkynen5d3c4ba2017-01-23 01:34:39 +0200173 DTB_DIR "bcm2835-rpi-b-plus.dtb",
Stephen Warren3207d8f2014-12-05 20:56:46 -0700174 true,
Stephen Warren6fe78452014-11-18 21:40:21 -0700175 },
Stephen Warrendbe6f1e2015-12-04 22:07:44 -0700176 [0x11] = {
Stephen Warren6fe78452014-11-18 21:40:21 -0700177 "Compute Module",
Tuomas Tynkkynen5d3c4ba2017-01-23 01:34:39 +0200178 DTB_DIR "bcm2835-rpi-cm.dtb",
Stephen Warren3207d8f2014-12-05 20:56:46 -0700179 false,
Stephen Warren6fe78452014-11-18 21:40:21 -0700180 },
Stephen Warrendbe6f1e2015-12-04 22:07:44 -0700181 [0x12] = {
Stephen Warren47705ef2014-12-23 20:01:43 -0700182 "Model A+",
Tuomas Tynkkynen5d3c4ba2017-01-23 01:34:39 +0200183 DTB_DIR "bcm2835-rpi-a-plus.dtb",
Stephen Warren47705ef2014-12-23 20:01:43 -0700184 false,
185 },
Stephen Warrendbe6f1e2015-12-04 22:07:44 -0700186 [0x13] = {
Stephen Warren787affb2015-04-12 21:43:25 -0600187 "Model B+",
Tuomas Tynkkynen5d3c4ba2017-01-23 01:34:39 +0200188 DTB_DIR "bcm2835-rpi-b-plus.dtb",
Stephen Warren787affb2015-04-12 21:43:25 -0600189 true,
190 },
Stephen Warrendbe6f1e2015-12-04 22:07:44 -0700191 [0x14] = {
Stephen Warren787affb2015-04-12 21:43:25 -0600192 "Compute Module",
Tuomas Tynkkynen5d3c4ba2017-01-23 01:34:39 +0200193 DTB_DIR "bcm2835-rpi-cm.dtb",
Stephen Warren787affb2015-04-12 21:43:25 -0600194 false,
195 },
Stephen Warrendbe6f1e2015-12-04 22:07:44 -0700196 [0x15] = {
Lubomir Rintel79ad5ce2015-10-14 17:17:54 +0200197 "Model A+",
Tuomas Tynkkynen5d3c4ba2017-01-23 01:34:39 +0200198 DTB_DIR "bcm2835-rpi-a-plus.dtb",
Lubomir Rintel79ad5ce2015-10-14 17:17:54 +0200199 false,
200 },
Stephen Warren6fe78452014-11-18 21:40:21 -0700201};
202
Stephen Warrenc4ea1ed2015-12-04 22:07:45 -0700203static uint32_t revision;
204static uint32_t rev_scheme;
205static uint32_t rev_type;
206static const struct rpi_model *model;
Stephen Warren6fe78452014-11-18 21:40:21 -0700207
Stephen Warrend22a7652016-04-01 21:14:15 -0600208#ifdef CONFIG_ARM64
209static struct mm_region bcm2837_mem_map[] = {
210 {
York Suncd4b0c52016-06-24 16:46:22 -0700211 .virt = 0x00000000UL,
212 .phys = 0x00000000UL,
Stephen Warrend22a7652016-04-01 21:14:15 -0600213 .size = 0x3f000000UL,
214 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
215 PTE_BLOCK_INNER_SHARE
216 }, {
York Suncd4b0c52016-06-24 16:46:22 -0700217 .virt = 0x3f000000UL,
218 .phys = 0x3f000000UL,
Stephen Warrend22a7652016-04-01 21:14:15 -0600219 .size = 0x01000000UL,
220 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
221 PTE_BLOCK_NON_SHARE |
222 PTE_BLOCK_PXN | PTE_BLOCK_UXN
223 }, {
224 /* List terminator */
225 0,
226 }
227};
228
229struct mm_region *mem_map = bcm2837_mem_map;
230#endif
231
Stephen Warren0d04f342012-08-05 16:07:22 +0000232int dram_init(void)
233{
Alexander Stein927753a2015-07-24 09:22:12 +0200234 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_arm_mem, msg, 1);
Stephen Warren3f397782013-01-29 16:37:37 +0000235 int ret;
236
237 BCM2835_MBOX_INIT_HDR(msg);
238 BCM2835_MBOX_INIT_TAG(&msg->get_arm_mem, GET_ARM_MEMORY);
239
240 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
241 if (ret) {
242 printf("bcm2835: Could not query ARM memory size\n");
243 return -1;
244 }
245
246 gd->ram_size = msg->get_arm_mem.body.resp.mem_size;
Stephen Warren0d04f342012-08-05 16:07:22 +0000247
248 return 0;
249}
250
Stephen Warren6fe78452014-11-18 21:40:21 -0700251static void set_fdtfile(void)
252{
253 const char *fdtfile;
254
Simon Glass00caae62017-08-03 12:22:12 -0600255 if (env_get("fdtfile"))
Stephen Warren6fe78452014-11-18 21:40:21 -0700256 return;
257
Stephen Warrenc4ea1ed2015-12-04 22:07:45 -0700258 fdtfile = model->fdtfile;
Simon Glass382bee52017-08-03 12:22:09 -0600259 env_set("fdtfile", fdtfile);
Stephen Warren6fe78452014-11-18 21:40:21 -0700260}
261
Cédric Schieliade243a2016-11-11 11:59:07 +0100262/*
263 * If the firmware provided a valid FDT at boot time, let's expose it in
264 * ${fdt_addr} so it may be passed unmodified to the kernel.
265 */
266static void set_fdt_addr(void)
267{
Simon Glass00caae62017-08-03 12:22:12 -0600268 if (env_get("fdt_addr"))
Cédric Schieliade243a2016-11-11 11:59:07 +0100269 return;
270
271 if (fdt_magic(fw_dtb_pointer) != FDT_MAGIC)
272 return;
273
Simon Glass018f5302017-08-03 12:22:10 -0600274 env_set_hex("fdt_addr", fw_dtb_pointer);
Cédric Schieliade243a2016-11-11 11:59:07 +0100275}
276
277/*
278 * Prevent relocation from stomping on a firmware provided FDT blob.
279 */
280unsigned long board_get_usable_ram_top(unsigned long total_size)
281{
282 if ((gd->ram_top - fw_dtb_pointer) > SZ_64M)
283 return gd->ram_top;
284 return fw_dtb_pointer & ~0xffff;
285}
286
Stephen Warren6fe78452014-11-18 21:40:21 -0700287static void set_usbethaddr(void)
Stephen Warren4f80a062014-09-26 20:51:39 -0600288{
Alexander Stein927753a2015-07-24 09:22:12 +0200289 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_mac_address, msg, 1);
Stephen Warren4f80a062014-09-26 20:51:39 -0600290 int ret;
291
Stephen Warrenc4ea1ed2015-12-04 22:07:45 -0700292 if (!model->has_onboard_eth)
Stephen Warren3207d8f2014-12-05 20:56:46 -0700293 return;
294
Simon Glass00caae62017-08-03 12:22:12 -0600295 if (env_get("usbethaddr"))
Stephen Warren6fe78452014-11-18 21:40:21 -0700296 return;
Stephen Warren4f80a062014-09-26 20:51:39 -0600297
298 BCM2835_MBOX_INIT_HDR(msg);
299 BCM2835_MBOX_INIT_TAG(&msg->get_mac_address, GET_MAC_ADDRESS);
300
301 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
302 if (ret) {
303 printf("bcm2835: Could not query MAC address\n");
304 /* Ignore error; not critical */
Stephen Warren6fe78452014-11-18 21:40:21 -0700305 return;
Stephen Warren4f80a062014-09-26 20:51:39 -0600306 }
307
Simon Glassfd1e9592017-08-03 12:22:11 -0600308 eth_env_set_enetaddr("usbethaddr", msg->get_mac_address.body.resp.mac);
Stephen Warren4f80a062014-09-26 20:51:39 -0600309
Simon Glass00caae62017-08-03 12:22:12 -0600310 if (!env_get("ethaddr"))
311 env_set("ethaddr", env_get("usbethaddr"));
Lubomir Rintel859f1432016-02-03 16:08:09 +0100312
Stephen Warren6fe78452014-11-18 21:40:21 -0700313 return;
314}
315
Guillaume GARDETbff78562015-08-25 15:10:26 +0200316#ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
317static void set_board_info(void)
318{
Stephen Warrenc4ea1ed2015-12-04 22:07:45 -0700319 char s[11];
320
321 snprintf(s, sizeof(s), "0x%X", revision);
Simon Glass382bee52017-08-03 12:22:09 -0600322 env_set("board_revision", s);
Stephen Warrenc4ea1ed2015-12-04 22:07:45 -0700323 snprintf(s, sizeof(s), "%d", rev_scheme);
Simon Glass382bee52017-08-03 12:22:09 -0600324 env_set("board_rev_scheme", s);
Stephen Warrenc4ea1ed2015-12-04 22:07:45 -0700325 /* Can't rename this to board_rev_type since it's an ABI for scripts */
326 snprintf(s, sizeof(s), "0x%X", rev_type);
Simon Glass382bee52017-08-03 12:22:09 -0600327 env_set("board_rev", s);
328 env_set("board_name", model->name);
Guillaume GARDETbff78562015-08-25 15:10:26 +0200329}
330#endif /* CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG */
331
Lubomir Rintel757cd142016-02-22 22:06:47 +0100332static void set_serial_number(void)
333{
334 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_serial, msg, 1);
335 int ret;
336 char serial_string[17] = { 0 };
337
Simon Glass00caae62017-08-03 12:22:12 -0600338 if (env_get("serial#"))
Lubomir Rintel757cd142016-02-22 22:06:47 +0100339 return;
340
341 BCM2835_MBOX_INIT_HDR(msg);
342 BCM2835_MBOX_INIT_TAG_NO_REQ(&msg->get_board_serial, GET_BOARD_SERIAL);
343
344 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
345 if (ret) {
346 printf("bcm2835: Could not query board serial\n");
347 /* Ignore error; not critical */
348 return;
349 }
350
351 snprintf(serial_string, sizeof(serial_string), "%016" PRIx64,
352 msg->get_board_serial.body.resp.serial);
Simon Glass382bee52017-08-03 12:22:09 -0600353 env_set("serial#", serial_string);
Lubomir Rintel757cd142016-02-22 22:06:47 +0100354}
355
Stephen Warren6fe78452014-11-18 21:40:21 -0700356int misc_init_r(void)
357{
Cédric Schieliade243a2016-11-11 11:59:07 +0100358 set_fdt_addr();
Stephen Warren6fe78452014-11-18 21:40:21 -0700359 set_fdtfile();
360 set_usbethaddr();
Guillaume GARDETbff78562015-08-25 15:10:26 +0200361#ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
362 set_board_info();
363#endif
Lubomir Rintel757cd142016-02-22 22:06:47 +0100364 set_serial_number();
365
Stephen Warren4f80a062014-09-26 20:51:39 -0600366 return 0;
367}
368
Stephen Warren6fe78452014-11-18 21:40:21 -0700369static void get_board_rev(void)
370{
Alexander Stein927753a2015-07-24 09:22:12 +0200371 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_rev, msg, 1);
Stephen Warren6fe78452014-11-18 21:40:21 -0700372 int ret;
Stephen Warrenc4ea1ed2015-12-04 22:07:45 -0700373 const struct rpi_model *models;
374 uint32_t models_count;
Stephen Warren6fe78452014-11-18 21:40:21 -0700375
376 BCM2835_MBOX_INIT_HDR(msg);
377 BCM2835_MBOX_INIT_TAG(&msg->get_board_rev, GET_BOARD_REV);
378
379 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
380 if (ret) {
381 printf("bcm2835: Could not query board revision\n");
382 /* Ignore error; not critical */
383 return;
384 }
385
Stephen Warren46414292015-02-16 12:16:15 -0700386 /*
387 * For details of old-vs-new scheme, see:
388 * https://github.com/pimoroni/RPi.version/blob/master/RPi/version.py
389 * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=99293&p=690282
390 * (a few posts down)
Stephen Warren95b4f112015-03-23 23:00:25 -0600391 *
392 * For the RPi 1, bit 24 is the "warranty bit", so we mask off just the
393 * lower byte to use as the board rev:
394 * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=98367&start=250
395 * http://www.raspberrypi.org/forums/viewtopic.php?f=31&t=20594
Stephen Warren46414292015-02-16 12:16:15 -0700396 */
Stephen Warrenc4ea1ed2015-12-04 22:07:45 -0700397 revision = msg->get_board_rev.body.resp.rev;
398 if (revision & 0x800000) {
399 rev_scheme = 1;
400 rev_type = (revision >> 4) & 0xff;
401 models = rpi_models_new_scheme;
402 models_count = ARRAY_SIZE(rpi_models_new_scheme);
403 } else {
404 rev_scheme = 0;
405 rev_type = revision & 0xff;
406 models = rpi_models_old_scheme;
407 models_count = ARRAY_SIZE(rpi_models_old_scheme);
Stephen Warren47705ef2014-12-23 20:01:43 -0700408 }
Stephen Warrenc4ea1ed2015-12-04 22:07:45 -0700409 if (rev_type >= models_count) {
410 printf("RPI: Board rev 0x%x outside known range\n", rev_type);
411 model = &rpi_model_unknown;
412 } else if (!models[rev_type].name) {
413 printf("RPI: Board rev 0x%x unknown\n", rev_type);
414 model = &rpi_model_unknown;
415 } else {
416 model = &models[rev_type];
Stephen Warren914627f2014-12-23 20:01:44 -0700417 }
Stephen Warren6fe78452014-11-18 21:40:21 -0700418
Stephen Warrenc4ea1ed2015-12-04 22:07:45 -0700419 printf("RPI %s (0x%x)\n", model->name, revision);
Stephen Warren6fe78452014-11-18 21:40:21 -0700420}
421
Alexander Graf601147b2016-08-15 17:48:51 +0200422#ifndef CONFIG_PL01X_SERIAL
423static bool rpi_is_serial_active(void)
424{
425 int serial_gpio = 15;
426 struct udevice *dev;
427
428 /*
429 * The RPi3 disables the mini uart by default. The easiest way to find
430 * out whether it is available is to check if the RX pin is muxed.
431 */
432
433 if (uclass_first_device(UCLASS_GPIO, &dev) || !dev)
434 return true;
435
436 if (bcm2835_gpio_get_func_id(dev, serial_gpio) != BCM2835_GPIO_ALT5)
437 return false;
438
439 return true;
440}
Fabian Vogtd8396a32016-09-26 14:26:50 +0200441
442/* Disable mini-UART I/O if it's not pinmuxed to our pins.
443 * The firmware only enables it if explicitly done in config.txt: enable_uart=1
444 */
445static void rpi_disable_inactive_uart(void)
446{
447 struct udevice *dev;
448 struct bcm283x_mu_serial_platdata *plat;
449
450 if (uclass_get_device_by_driver(UCLASS_SERIAL,
451 DM_GET_DRIVER(serial_bcm283x_mu),
452 &dev) || !dev)
453 return;
454
455 if (!rpi_is_serial_active()) {
456 plat = dev_get_platdata(dev);
457 plat->disabled = true;
458 }
459}
Alexander Graf601147b2016-08-15 17:48:51 +0200460#endif
461
Fabian Vogtd8396a32016-09-26 14:26:50 +0200462int board_init(void)
Alexander Graf601147b2016-08-15 17:48:51 +0200463{
Paolo Pisati45a6d232017-02-10 17:28:05 +0100464#ifdef CONFIG_HW_WATCHDOG
465 hw_watchdog_init();
466#endif
Alexander Graf601147b2016-08-15 17:48:51 +0200467#ifndef CONFIG_PL01X_SERIAL
Fabian Vogtd8396a32016-09-26 14:26:50 +0200468 rpi_disable_inactive_uart();
Alexander Graf601147b2016-08-15 17:48:51 +0200469#endif
470
Fabian Vogtd8396a32016-09-26 14:26:50 +0200471 get_board_rev();
472
473 gd->bd->bi_boot_params = 0x100;
474
Simon Glass70997d82017-04-05 16:23:36 -0600475 return bcm2835_power_on_module(BCM2835_MBOX_POWER_DEVID_USB_HCD);
Alexander Graf601147b2016-08-15 17:48:51 +0200476}
477
Alex Deymo82f766d2017-04-02 01:25:20 -0700478/*
479 * If the firmware passed a device tree use it for U-Boot.
480 */
481void *board_fdt_blob_setup(void)
482{
483 if (fdt_magic(fw_dtb_pointer) != FDT_MAGIC)
484 return NULL;
485 return (void *)fw_dtb_pointer;
486}
487
Simon Glasse895a4b2014-10-23 18:58:47 -0600488int ft_board_setup(void *blob, bd_t *bd)
Stephen Warrenea697ae2013-05-27 18:31:18 +0000489{
490 /*
491 * For now, we simply always add the simplefb DT node. Later, we
492 * should be more intelligent, and e.g. only do this if no enabled DT
493 * node exists for the "real" graphics driver.
494 */
495 lcd_dt_simplefb_add_node(blob);
Simon Glasse895a4b2014-10-23 18:58:47 -0600496
Alexander Graf1bcf7a32016-11-02 10:36:20 +0100497#ifdef CONFIG_EFI_LOADER
498 /* Reserve the spin table */
499 efi_add_memory_map(0, 1, EFI_RESERVED_MEMORY_TYPE, 0);
500#endif
501
Simon Glasse895a4b2014-10-23 18:58:47 -0600502 return 0;
Stephen Warrenea697ae2013-05-27 18:31:18 +0000503}