rpi: push fw_dtb_pointer in the .data section
[platform/kernel/u-boot.git] / board / raspberrypi / rpi / rpi.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * (C) Copyright 2012-2016 Stephen Warren
4  */
5
6 #include <common.h>
7 #include <config.h>
8 #include <dm.h>
9 #include <env.h>
10 #include <efi_loader.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/msg.h>
19 #include <asm/arch/sdhci.h>
20 #include <asm/global_data.h>
21 #include <dm/platform_data/serial_bcm283x_mu.h>
22 #ifdef CONFIG_ARM64
23 #include <asm/armv8/mmu.h>
24 #endif
25 #include <watchdog.h>
26 #include <dm/pinctrl.h>
27
28 DECLARE_GLOBAL_DATA_PTR;
29
30 /* Assigned in lowlevel_init.S
31  * Push the variable into the .data section so that it
32  * does not get cleared later.
33  */
34 unsigned long __section(".data") fw_dtb_pointer;
35
36 /* TODO(sjg@chromium.org): Move these to the msg.c file */
37 struct msg_get_arm_mem {
38         struct bcm2835_mbox_hdr hdr;
39         struct bcm2835_mbox_tag_get_arm_mem get_arm_mem;
40         u32 end_tag;
41 };
42
43 struct msg_get_board_rev {
44         struct bcm2835_mbox_hdr hdr;
45         struct bcm2835_mbox_tag_get_board_rev get_board_rev;
46         u32 end_tag;
47 };
48
49 struct msg_get_board_serial {
50         struct bcm2835_mbox_hdr hdr;
51         struct bcm2835_mbox_tag_get_board_serial get_board_serial;
52         u32 end_tag;
53 };
54
55 struct msg_get_mac_address {
56         struct bcm2835_mbox_hdr hdr;
57         struct bcm2835_mbox_tag_get_mac_address get_mac_address;
58         u32 end_tag;
59 };
60
61 struct msg_get_clock_rate {
62         struct bcm2835_mbox_hdr hdr;
63         struct bcm2835_mbox_tag_get_clock_rate get_clock_rate;
64         u32 end_tag;
65 };
66
67 #ifdef CONFIG_ARM64
68 #define DTB_DIR "broadcom/"
69 #else
70 #define DTB_DIR ""
71 #endif
72
73 /*
74  * https://www.raspberrypi.org/documentation/hardware/raspberrypi/revision-codes/README.md
75  */
76 struct rpi_model {
77         const char *name;
78         const char *fdtfile;
79         bool has_onboard_eth;
80 };
81
82 static const struct rpi_model rpi_model_unknown = {
83         "Unknown model",
84         DTB_DIR "bcm283x-rpi-other.dtb",
85         false,
86 };
87
88 static const struct rpi_model rpi_models_new_scheme[] = {
89         [0x0] = {
90                 "Model A",
91                 DTB_DIR "bcm2835-rpi-a.dtb",
92                 false,
93         },
94         [0x1] = {
95                 "Model B",
96                 DTB_DIR "bcm2835-rpi-b.dtb",
97                 true,
98         },
99         [0x2] = {
100                 "Model A+",
101                 DTB_DIR "bcm2835-rpi-a-plus.dtb",
102                 false,
103         },
104         [0x3] = {
105                 "Model B+",
106                 DTB_DIR "bcm2835-rpi-b-plus.dtb",
107                 true,
108         },
109         [0x4] = {
110                 "2 Model B",
111                 DTB_DIR "bcm2836-rpi-2-b.dtb",
112                 true,
113         },
114         [0x6] = {
115                 "Compute Module",
116                 DTB_DIR "bcm2835-rpi-cm.dtb",
117                 false,
118         },
119         [0x8] = {
120                 "3 Model B",
121                 DTB_DIR "bcm2837-rpi-3-b.dtb",
122                 true,
123         },
124         [0x9] = {
125                 "Zero",
126                 DTB_DIR "bcm2835-rpi-zero.dtb",
127                 false,
128         },
129         [0xA] = {
130                 "Compute Module 3",
131                 DTB_DIR "bcm2837-rpi-cm3.dtb",
132                 false,
133         },
134         [0xC] = {
135                 "Zero W",
136                 DTB_DIR "bcm2835-rpi-zero-w.dtb",
137                 false,
138         },
139         [0xD] = {
140                 "3 Model B+",
141                 DTB_DIR "bcm2837-rpi-3-b-plus.dtb",
142                 true,
143         },
144         [0xE] = {
145                 "3 Model A+",
146                 DTB_DIR "bcm2837-rpi-3-a-plus.dtb",
147                 false,
148         },
149         [0x10] = {
150                 "Compute Module 3+",
151                 DTB_DIR "bcm2837-rpi-cm3.dtb",
152                 false,
153         },
154         [0x11] = {
155                 "4 Model B",
156                 DTB_DIR "bcm2711-rpi-4-b.dtb",
157                 true,
158         },
159 };
160
161 static const struct rpi_model rpi_models_old_scheme[] = {
162         [0x2] = {
163                 "Model B",
164                 DTB_DIR "bcm2835-rpi-b.dtb",
165                 true,
166         },
167         [0x3] = {
168                 "Model B",
169                 DTB_DIR "bcm2835-rpi-b.dtb",
170                 true,
171         },
172         [0x4] = {
173                 "Model B rev2",
174                 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
175                 true,
176         },
177         [0x5] = {
178                 "Model B rev2",
179                 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
180                 true,
181         },
182         [0x6] = {
183                 "Model B rev2",
184                 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
185                 true,
186         },
187         [0x7] = {
188                 "Model A",
189                 DTB_DIR "bcm2835-rpi-a.dtb",
190                 false,
191         },
192         [0x8] = {
193                 "Model A",
194                 DTB_DIR "bcm2835-rpi-a.dtb",
195                 false,
196         },
197         [0x9] = {
198                 "Model A",
199                 DTB_DIR "bcm2835-rpi-a.dtb",
200                 false,
201         },
202         [0xd] = {
203                 "Model B rev2",
204                 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
205                 true,
206         },
207         [0xe] = {
208                 "Model B rev2",
209                 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
210                 true,
211         },
212         [0xf] = {
213                 "Model B rev2",
214                 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
215                 true,
216         },
217         [0x10] = {
218                 "Model B+",
219                 DTB_DIR "bcm2835-rpi-b-plus.dtb",
220                 true,
221         },
222         [0x11] = {
223                 "Compute Module",
224                 DTB_DIR "bcm2835-rpi-cm.dtb",
225                 false,
226         },
227         [0x12] = {
228                 "Model A+",
229                 DTB_DIR "bcm2835-rpi-a-plus.dtb",
230                 false,
231         },
232         [0x13] = {
233                 "Model B+",
234                 DTB_DIR "bcm2835-rpi-b-plus.dtb",
235                 true,
236         },
237         [0x14] = {
238                 "Compute Module",
239                 DTB_DIR "bcm2835-rpi-cm.dtb",
240                 false,
241         },
242         [0x15] = {
243                 "Model A+",
244                 DTB_DIR "bcm2835-rpi-a-plus.dtb",
245                 false,
246         },
247 };
248
249 static uint32_t revision;
250 static uint32_t rev_scheme;
251 static uint32_t rev_type;
252 static const struct rpi_model *model;
253
254 #ifdef CONFIG_ARM64
255 #ifndef CONFIG_BCM2711
256 static struct mm_region bcm283x_mem_map[] = {
257         {
258                 .virt = 0x00000000UL,
259                 .phys = 0x00000000UL,
260                 .size = 0x3f000000UL,
261                 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
262                          PTE_BLOCK_INNER_SHARE
263         }, {
264                 .virt = 0x3f000000UL,
265                 .phys = 0x3f000000UL,
266                 .size = 0x01000000UL,
267                 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
268                          PTE_BLOCK_NON_SHARE |
269                          PTE_BLOCK_PXN | PTE_BLOCK_UXN
270         }, {
271                 /* List terminator */
272                 0,
273         }
274 };
275 #else
276 static struct mm_region bcm283x_mem_map[] = {
277         {
278                 .virt = 0x00000000UL,
279                 .phys = 0x00000000UL,
280                 .size = 0xfe000000UL,
281                 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
282                          PTE_BLOCK_INNER_SHARE
283         }, {
284                 .virt = 0xfe000000UL,
285                 .phys = 0xfe000000UL,
286                 .size = 0x01800000UL,
287                 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
288                          PTE_BLOCK_NON_SHARE |
289                          PTE_BLOCK_PXN | PTE_BLOCK_UXN
290         }, {
291                 /* List terminator */
292                 0,
293         }
294 };
295 #endif
296 struct mm_region *mem_map = bcm283x_mem_map;
297 #endif
298
299 int dram_init(void)
300 {
301         ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_arm_mem, msg, 1);
302         int ret;
303
304         BCM2835_MBOX_INIT_HDR(msg);
305         BCM2835_MBOX_INIT_TAG(&msg->get_arm_mem, GET_ARM_MEMORY);
306
307         ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
308         if (ret) {
309                 printf("bcm2835: Could not query ARM memory size\n");
310                 return -1;
311         }
312
313         gd->ram_size = msg->get_arm_mem.body.resp.mem_size;
314
315         return 0;
316 }
317
318 #ifdef CONFIG_OF_BOARD
319 #ifdef CONFIG_BCM2711
320 int dram_init_banksize(void)
321 {
322         return fdtdec_decode_ram_size(gd->fdt_blob, NULL, 0, NULL,
323                                      (phys_size_t *)&gd->ram_size, gd->bd);
324 }
325 #endif
326 #endif
327
328 static void set_fdtfile(void)
329 {
330         const char *fdtfile;
331
332         if (env_get("fdtfile"))
333                 return;
334
335         fdtfile = model->fdtfile;
336         env_set("fdtfile", fdtfile);
337 }
338
339 /*
340  * If the firmware provided a valid FDT at boot time, let's expose it in
341  * ${fdt_addr} so it may be passed unmodified to the kernel.
342  */
343 static void set_fdt_addr(void)
344 {
345         if (env_get("fdt_addr"))
346                 return;
347
348         if (fdt_magic(fw_dtb_pointer) != FDT_MAGIC)
349                 return;
350
351         env_set_hex("fdt_addr", fw_dtb_pointer);
352 }
353
354 /*
355  * Prevent relocation from stomping on a firmware provided FDT blob.
356  */
357 unsigned long board_get_usable_ram_top(unsigned long total_size)
358 {
359         if ((gd->ram_top - fw_dtb_pointer) > SZ_64M)
360                 return gd->ram_top;
361         return fw_dtb_pointer & ~0xffff;
362 }
363
364 static void set_usbethaddr(void)
365 {
366         ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_mac_address, msg, 1);
367         int ret;
368
369         if (!model->has_onboard_eth)
370                 return;
371
372         if (env_get("usbethaddr"))
373                 return;
374
375         BCM2835_MBOX_INIT_HDR(msg);
376         BCM2835_MBOX_INIT_TAG(&msg->get_mac_address, GET_MAC_ADDRESS);
377
378         ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
379         if (ret) {
380                 printf("bcm2835: Could not query MAC address\n");
381                 /* Ignore error; not critical */
382                 return;
383         }
384
385         eth_env_set_enetaddr("usbethaddr", msg->get_mac_address.body.resp.mac);
386
387         if (!env_get("ethaddr"))
388                 env_set("ethaddr", env_get("usbethaddr"));
389
390         return;
391 }
392
393 #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
394 static void set_board_info(void)
395 {
396         char s[11];
397
398         snprintf(s, sizeof(s), "0x%X", revision);
399         env_set("board_revision", s);
400         snprintf(s, sizeof(s), "%d", rev_scheme);
401         env_set("board_rev_scheme", s);
402         /* Can't rename this to board_rev_type since it's an ABI for scripts */
403         snprintf(s, sizeof(s), "0x%X", rev_type);
404         env_set("board_rev", s);
405         env_set("board_name", model->name);
406 }
407 #endif /* CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG */
408
409 static void set_serial_number(void)
410 {
411         ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_serial, msg, 1);
412         int ret;
413         char serial_string[17] = { 0 };
414
415         if (env_get("serial#"))
416                 return;
417
418         BCM2835_MBOX_INIT_HDR(msg);
419         BCM2835_MBOX_INIT_TAG_NO_REQ(&msg->get_board_serial, GET_BOARD_SERIAL);
420
421         ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
422         if (ret) {
423                 printf("bcm2835: Could not query board serial\n");
424                 /* Ignore error; not critical */
425                 return;
426         }
427
428         snprintf(serial_string, sizeof(serial_string), "%016llx",
429                  msg->get_board_serial.body.resp.serial);
430         env_set("serial#", serial_string);
431 }
432
433 int misc_init_r(void)
434 {
435         set_fdt_addr();
436         set_fdtfile();
437         set_usbethaddr();
438 #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
439         set_board_info();
440 #endif
441         set_serial_number();
442
443         return 0;
444 }
445
446 static void get_board_rev(void)
447 {
448         ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_rev, msg, 1);
449         int ret;
450         const struct rpi_model *models;
451         uint32_t models_count;
452
453         BCM2835_MBOX_INIT_HDR(msg);
454         BCM2835_MBOX_INIT_TAG(&msg->get_board_rev, GET_BOARD_REV);
455
456         ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
457         if (ret) {
458                 printf("bcm2835: Could not query board revision\n");
459                 /* Ignore error; not critical */
460                 return;
461         }
462
463         /*
464          * For details of old-vs-new scheme, see:
465          * https://github.com/pimoroni/RPi.version/blob/master/RPi/version.py
466          * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=99293&p=690282
467          * (a few posts down)
468          *
469          * For the RPi 1, bit 24 is the "warranty bit", so we mask off just the
470          * lower byte to use as the board rev:
471          * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=98367&start=250
472          * http://www.raspberrypi.org/forums/viewtopic.php?f=31&t=20594
473          */
474         revision = msg->get_board_rev.body.resp.rev;
475         if (revision & 0x800000) {
476                 rev_scheme = 1;
477                 rev_type = (revision >> 4) & 0xff;
478                 models = rpi_models_new_scheme;
479                 models_count = ARRAY_SIZE(rpi_models_new_scheme);
480         } else {
481                 rev_scheme = 0;
482                 rev_type = revision & 0xff;
483                 models = rpi_models_old_scheme;
484                 models_count = ARRAY_SIZE(rpi_models_old_scheme);
485         }
486         if (rev_type >= models_count) {
487                 printf("RPI: Board rev 0x%x outside known range\n", rev_type);
488                 model = &rpi_model_unknown;
489         } else if (!models[rev_type].name) {
490                 printf("RPI: Board rev 0x%x unknown\n", rev_type);
491                 model = &rpi_model_unknown;
492         } else {
493                 model = &models[rev_type];
494         }
495
496         printf("RPI %s (0x%x)\n", model->name, revision);
497 }
498
499 int board_init(void)
500 {
501 #ifdef CONFIG_HW_WATCHDOG
502         hw_watchdog_init();
503 #endif
504
505         get_board_rev();
506
507         gd->bd->bi_boot_params = 0x100;
508
509         return bcm2835_power_on_module(BCM2835_MBOX_POWER_DEVID_USB_HCD);
510 }
511
512 /*
513  * If the firmware passed a device tree use it for U-Boot.
514  */
515 void *board_fdt_blob_setup(void)
516 {
517         if (fdt_magic(fw_dtb_pointer) != FDT_MAGIC)
518                 return NULL;
519         return (void *)fw_dtb_pointer;
520 }
521
522 int ft_board_setup(void *blob, bd_t *bd)
523 {
524         /*
525          * For now, we simply always add the simplefb DT node. Later, we
526          * should be more intelligent, and e.g. only do this if no enabled DT
527          * node exists for the "real" graphics driver.
528          */
529         lcd_dt_simplefb_add_node(blob);
530
531 #ifdef CONFIG_EFI_LOADER
532         /* Reserve the spin table */
533         efi_add_memory_map(0, 1, EFI_RESERVED_MEMORY_TYPE, 0);
534 #endif
535
536         return 0;
537 }