37eab70708a2d271fd3b0f92acc7868710971e92
[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 <init.h>
14 #include <lcd.h>
15 #include <memalign.h>
16 #include <mmc.h>
17 #include <usb/xhci.h>
18 #include <asm/gpio.h>
19 #include <asm/arch/mbox.h>
20 #include <asm/arch/msg.h>
21 #include <asm/arch/sdhci.h>
22 #include <asm/global_data.h>
23 #include <dm/platform_data/serial_bcm283x_mu.h>
24 #ifdef CONFIG_ARM64
25 #include <asm/armv8/mmu.h>
26 #endif
27 #include <watchdog.h>
28 #include <dm/pinctrl.h>
29
30 DECLARE_GLOBAL_DATA_PTR;
31
32 /* Assigned in lowlevel_init.S
33  * Push the variable into the .data section so that it
34  * does not get cleared later.
35  */
36 unsigned long __section(".data") fw_dtb_pointer;
37
38 /* TODO(sjg@chromium.org): Move these to the msg.c file */
39 struct msg_get_arm_mem {
40         struct bcm2835_mbox_hdr hdr;
41         struct bcm2835_mbox_tag_get_arm_mem get_arm_mem;
42         u32 end_tag;
43 };
44
45 struct msg_get_board_rev {
46         struct bcm2835_mbox_hdr hdr;
47         struct bcm2835_mbox_tag_get_board_rev get_board_rev;
48         u32 end_tag;
49 };
50
51 struct msg_get_board_serial {
52         struct bcm2835_mbox_hdr hdr;
53         struct bcm2835_mbox_tag_get_board_serial get_board_serial;
54         u32 end_tag;
55 };
56
57 struct msg_get_mac_address {
58         struct bcm2835_mbox_hdr hdr;
59         struct bcm2835_mbox_tag_get_mac_address get_mac_address;
60         u32 end_tag;
61 };
62
63 struct msg_get_clock_rate {
64         struct bcm2835_mbox_hdr hdr;
65         struct bcm2835_mbox_tag_get_clock_rate get_clock_rate;
66         u32 end_tag;
67 };
68
69 struct msg_get_rsts {
70         struct bcm2835_mbox_hdr hdr;
71         struct bcm2835_mbox_tag_get_rsts get_rsts;
72         u32 end_tag;
73 };
74
75 /* Dump a message. */
76 #ifdef BCM2835_MBOX_DUMP_MESSAGES
77 #define BCM2835_MBOX_DUMP_MSG(msg) {                                    \
78                 u32 *p = msg;                                           \
79                 int i;                                                  \
80                 printf("%s:%d", __func__, __LINE__);                    \
81                 if (msg->hdr.code == 0)                                 \
82                         printf("  req");                                \
83                 else if (msg->hdr.code == 0x80000000)                   \
84                         printf(" resp");                                \
85                 else if (msg->hdr.code == 0x80000000)                   \
86                         printf("  err");                                \
87                 else                                                    \
88                         printf(" WAT‽");                              \
89                 for (i = 0; i < sizeof(*(msg)) / sizeof(*p); i++)       \
90                         printf(" 0x%08X", p[i]);                        \
91                 printf("\n");                                           \
92 }
93 #else
94 #define BCM2835_MBOX_DUMP_MSG(msg)
95 #endif
96
97 #ifdef CONFIG_ARM64
98 #define DTB_DIR "broadcom/"
99 #else
100 #define DTB_DIR ""
101 #endif
102
103 /*
104  * https://www.raspberrypi.org/documentation/hardware/raspberrypi/revision-codes/README.md
105  */
106 struct rpi_model {
107         const char *name;
108         const char *fdtfile;
109         bool has_onboard_eth;
110 };
111
112 static const struct rpi_model rpi_model_unknown = {
113         "Unknown model",
114         DTB_DIR "bcm283x-rpi-other.dtb",
115         false,
116 };
117
118 static const struct rpi_model rpi_models_new_scheme[] = {
119         [0x0] = {
120                 "Model A",
121                 DTB_DIR "bcm2835-rpi-a.dtb",
122                 false,
123         },
124         [0x1] = {
125                 "Model B",
126                 DTB_DIR "bcm2835-rpi-b.dtb",
127                 true,
128         },
129         [0x2] = {
130                 "Model A+",
131                 DTB_DIR "bcm2835-rpi-a-plus.dtb",
132                 false,
133         },
134         [0x3] = {
135                 "Model B+",
136                 DTB_DIR "bcm2835-rpi-b-plus.dtb",
137                 true,
138         },
139         [0x4] = {
140                 "2 Model B",
141                 DTB_DIR "bcm2836-rpi-2-b.dtb",
142                 true,
143         },
144         [0x6] = {
145                 "Compute Module",
146                 DTB_DIR "bcm2835-rpi-cm.dtb",
147                 false,
148         },
149         [0x8] = {
150                 "3 Model B",
151                 DTB_DIR "bcm2837-rpi-3-b.dtb",
152                 true,
153         },
154         [0x9] = {
155                 "Zero",
156                 DTB_DIR "bcm2835-rpi-zero.dtb",
157                 false,
158         },
159         [0xA] = {
160                 "Compute Module 3",
161                 DTB_DIR "bcm2837-rpi-cm3.dtb",
162                 false,
163         },
164         [0xC] = {
165                 "Zero W",
166                 DTB_DIR "bcm2835-rpi-zero-w.dtb",
167                 false,
168         },
169         [0xD] = {
170                 "3 Model B+",
171                 DTB_DIR "bcm2837-rpi-3-b-plus.dtb",
172                 true,
173         },
174         [0xE] = {
175                 "3 Model A+",
176                 DTB_DIR "bcm2837-rpi-3-a-plus.dtb",
177                 false,
178         },
179         [0x10] = {
180                 "Compute Module 3+",
181                 DTB_DIR "bcm2837-rpi-cm3.dtb",
182                 false,
183         },
184         [0x11] = {
185                 "4 Model B",
186                 DTB_DIR "bcm2711-rpi-4-b.dtb",
187                 true,
188         },
189         [0x13] = {
190                 "400",
191                 DTB_DIR "bcm2711-rpi-400.dtb",
192                 true,
193         },
194 };
195
196 static const struct rpi_model rpi_models_old_scheme[] = {
197         [0x2] = {
198                 "Model B",
199                 DTB_DIR "bcm2835-rpi-b.dtb",
200                 true,
201         },
202         [0x3] = {
203                 "Model B",
204                 DTB_DIR "bcm2835-rpi-b.dtb",
205                 true,
206         },
207         [0x4] = {
208                 "Model B rev2",
209                 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
210                 true,
211         },
212         [0x5] = {
213                 "Model B rev2",
214                 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
215                 true,
216         },
217         [0x6] = {
218                 "Model B rev2",
219                 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
220                 true,
221         },
222         [0x7] = {
223                 "Model A",
224                 DTB_DIR "bcm2835-rpi-a.dtb",
225                 false,
226         },
227         [0x8] = {
228                 "Model A",
229                 DTB_DIR "bcm2835-rpi-a.dtb",
230                 false,
231         },
232         [0x9] = {
233                 "Model A",
234                 DTB_DIR "bcm2835-rpi-a.dtb",
235                 false,
236         },
237         [0xd] = {
238                 "Model B rev2",
239                 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
240                 true,
241         },
242         [0xe] = {
243                 "Model B rev2",
244                 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
245                 true,
246         },
247         [0xf] = {
248                 "Model B rev2",
249                 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
250                 true,
251         },
252         [0x10] = {
253                 "Model B+",
254                 DTB_DIR "bcm2835-rpi-b-plus.dtb",
255                 true,
256         },
257         [0x11] = {
258                 "Compute Module",
259                 DTB_DIR "bcm2835-rpi-cm.dtb",
260                 false,
261         },
262         [0x12] = {
263                 "Model A+",
264                 DTB_DIR "bcm2835-rpi-a-plus.dtb",
265                 false,
266         },
267         [0x13] = {
268                 "Model B+",
269                 DTB_DIR "bcm2835-rpi-b-plus.dtb",
270                 true,
271         },
272         [0x14] = {
273                 "Compute Module",
274                 DTB_DIR "bcm2835-rpi-cm.dtb",
275                 false,
276         },
277         [0x15] = {
278                 "Model A+",
279                 DTB_DIR "bcm2835-rpi-a-plus.dtb",
280                 false,
281         },
282 };
283
284 static uint32_t revision;
285 static uint32_t rev_scheme;
286 static uint32_t rev_type;
287 static const struct rpi_model *model;
288
289 int dram_init(void)
290 {
291         ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_arm_mem, msg, 1);
292         int ret;
293
294         BCM2835_MBOX_INIT_HDR(msg);
295         BCM2835_MBOX_INIT_TAG(&msg->get_arm_mem, GET_ARM_MEMORY);
296
297         ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
298         if (ret) {
299                 printf("bcm2835: Could not query ARM memory size\n");
300                 return -1;
301         }
302
303         gd->ram_size = msg->get_arm_mem.body.resp.mem_size;
304
305         /*
306          * In some configurations the memory size returned by VideoCore
307          * is not aligned to the section size, what is mandatory for
308          * the u-boot's memory setup.
309          */
310         gd->ram_size &= ~MMU_SECTION_SIZE;
311
312         return 0;
313 }
314
315 #ifdef CONFIG_OF_BOARD
316 int dram_init_banksize(void)
317 {
318         int ret;
319
320         ret = fdtdec_setup_memory_banksize();
321         if (ret)
322                 return ret;
323
324         return fdtdec_setup_mem_size_base();
325 }
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 get_rsts(void)
365 {
366         ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_rsts, msg, 1);
367         int ret;
368         char rsts_string[11] = { 0 };
369
370         BCM2835_MBOX_INIT_HDR(msg);
371         BCM2835_MBOX_INIT_TAG(&msg->get_rsts, GET_RSTS);
372
373         BCM2835_MBOX_DUMP_MSG(msg);
374
375         ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
376         if (ret) {
377                 printf("bcm2835: Could not query RSTS value\n");
378                 /* Ignore the error, let's carry on. */
379                 return;
380         }
381
382         BCM2835_MBOX_DUMP_MSG(msg);
383
384         snprintf(rsts_string, sizeof(rsts_string), "0x%08X",
385                  msg->get_rsts.body.resp.rsts);
386         env_set("reg_rsts", rsts_string);
387
388         return;
389 }
390
391 static void set_usbethaddr(void)
392 {
393         ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_mac_address, msg, 1);
394         int ret;
395
396         if (!model->has_onboard_eth)
397                 return;
398
399         if (env_get("usbethaddr"))
400                 return;
401
402         BCM2835_MBOX_INIT_HDR(msg);
403         BCM2835_MBOX_INIT_TAG(&msg->get_mac_address, GET_MAC_ADDRESS);
404
405         ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
406         if (ret) {
407                 printf("bcm2835: Could not query MAC address\n");
408                 /* Ignore error; not critical */
409                 return;
410         }
411
412         eth_env_set_enetaddr("usbethaddr", msg->get_mac_address.body.resp.mac);
413
414         if (!env_get("ethaddr"))
415                 env_set("ethaddr", env_get("usbethaddr"));
416
417         return;
418 }
419
420 #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
421 static void set_board_info(void)
422 {
423         char s[11];
424
425         snprintf(s, sizeof(s), "0x%X", revision);
426         env_set("board_revision", s);
427         snprintf(s, sizeof(s), "%d", rev_scheme);
428         env_set("board_rev_scheme", s);
429         /* Can't rename this to board_rev_type since it's an ABI for scripts */
430         snprintf(s, sizeof(s), "0x%X", rev_type);
431         env_set("board_rev", s);
432         env_set("board_name", model->name);
433 }
434 #endif /* CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG */
435
436 static void set_serial_number(void)
437 {
438         ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_serial, msg, 1);
439         int ret;
440         char serial_string[17] = { 0 };
441
442         if (env_get("serial#"))
443                 return;
444
445         BCM2835_MBOX_INIT_HDR(msg);
446         BCM2835_MBOX_INIT_TAG_NO_REQ(&msg->get_board_serial, GET_BOARD_SERIAL);
447
448         ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
449         if (ret) {
450                 printf("bcm2835: Could not query board serial\n");
451                 /* Ignore error; not critical */
452                 return;
453         }
454
455         snprintf(serial_string, sizeof(serial_string), "%016llx",
456                  msg->get_board_serial.body.resp.serial);
457         env_set("serial#", serial_string);
458 }
459
460 #if defined(CONFIG_PCI) && defined(CONFIG_BOARD_EARLY_INIT_R)
461 static char *boot_interface;
462 int board_early_init_r(void)
463 {
464         /*
465          * Currently there is not way to detect which device (SD card or
466          * USB Mass Storage) has been used by VideoCore to load the uboot,
467          * so just try MMC0 first, if not then use USB.
468          */
469         mmc_init_device(0);
470         boot_interface = (blk_get_dev("mmc", 0)) ? "mmc" : "usb";
471
472         pci_init();
473         usb_init();
474
475         return 0;
476 }
477
478 char *env_fat_get_interface(void)
479 {
480         return boot_interface;
481 }
482 #endif
483
484 int misc_init_r(void)
485 {
486         set_fdt_addr();
487         set_fdtfile();
488         set_usbethaddr();
489         get_rsts();
490 #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
491         set_board_info();
492 #endif
493         set_serial_number();
494
495         return 0;
496 }
497
498 static void get_board_rev(void)
499 {
500         ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_rev, msg, 1);
501         int ret;
502         const struct rpi_model *models;
503         uint32_t models_count;
504
505         BCM2835_MBOX_INIT_HDR(msg);
506         BCM2835_MBOX_INIT_TAG(&msg->get_board_rev, GET_BOARD_REV);
507
508         ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
509         if (ret) {
510                 printf("bcm2835: Could not query board revision\n");
511                 /* Ignore error; not critical */
512                 return;
513         }
514
515         /*
516          * For details of old-vs-new scheme, see:
517          * https://github.com/pimoroni/RPi.version/blob/master/RPi/version.py
518          * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=99293&p=690282
519          * (a few posts down)
520          *
521          * For the RPi 1, bit 24 is the "warranty bit", so we mask off just the
522          * lower byte to use as the board rev:
523          * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=98367&start=250
524          * http://www.raspberrypi.org/forums/viewtopic.php?f=31&t=20594
525          */
526         revision = msg->get_board_rev.body.resp.rev;
527         if (revision & 0x800000) {
528                 rev_scheme = 1;
529                 rev_type = (revision >> 4) & 0xff;
530                 models = rpi_models_new_scheme;
531                 models_count = ARRAY_SIZE(rpi_models_new_scheme);
532         } else {
533                 rev_scheme = 0;
534                 rev_type = revision & 0xff;
535                 models = rpi_models_old_scheme;
536                 models_count = ARRAY_SIZE(rpi_models_old_scheme);
537         }
538         if (rev_type >= models_count) {
539                 printf("RPI: Board rev 0x%x outside known range\n", rev_type);
540                 model = &rpi_model_unknown;
541         } else if (!models[rev_type].name) {
542                 printf("RPI: Board rev 0x%x unknown\n", rev_type);
543                 model = &rpi_model_unknown;
544         } else {
545                 model = &models[rev_type];
546         }
547
548         printf("RPI %s (0x%x)\n", model->name, revision);
549
550         printf("CPU: %s\n", CONFIG_SYS_CPU);
551 }
552
553 int board_init(void)
554 {
555 #ifdef CONFIG_HW_WATCHDOG
556         hw_watchdog_init();
557 #endif
558
559         get_board_rev();
560
561         gd->bd->bi_boot_params = 0x100;
562
563         return bcm2835_power_on_module(BCM2835_MBOX_POWER_DEVID_USB_HCD);
564 }
565
566 /*
567  * If the firmware passed a device tree use it for U-Boot.
568  */
569 void *board_fdt_blob_setup(void)
570 {
571         if (fdt_magic(fw_dtb_pointer) != FDT_MAGIC)
572                 return NULL;
573         return (void *)fw_dtb_pointer;
574 }
575
576 int ft_board_setup(void *blob, struct bd_info *bd)
577 {
578         /*
579          * For now, we simply always add the simplefb DT node. Later, we
580          * should be more intelligent, and e.g. only do this if no enabled DT
581          * node exists for the "real" graphics driver.
582          */
583         lcd_dt_simplefb_add_node(blob);
584
585 #ifdef CONFIG_EFI_LOADER
586         /* Reserve the spin table */
587         efi_add_memory_map(0, CONFIG_RPI_EFI_NR_SPIN_PAGES << EFI_PAGE_SHIFT,
588                            EFI_RESERVED_MEMORY_TYPE);
589 #endif
590
591         return 0;
592 }
593
594 void xhci_pci_fixup(struct udevice *dev)
595 {
596         static int done = false;
597
598         if (!done) {
599                 bcm2711_notify_vl805_reset();
600                 done = true;
601         }
602 }
603
604 int checkboard(void)
605 {
606         return 0;
607 }