bc3cc597adbf10cbdcc13aee86851d8d8ff14951
[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 <asm/gpio.h>
18 #include <asm/arch/mbox.h>
19 #include <asm/arch/msg.h>
20 #include <asm/arch/sdhci.h>
21 #include <asm/global_data.h>
22 #include <dm/platform_data/serial_bcm283x_mu.h>
23 #ifdef CONFIG_ARM64
24 #include <asm/armv8/mmu.h>
25 #endif
26 #include <usb.h>
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         [0x14] = {
195                 "Compute Module 4",
196                 DTB_DIR "bcm2711-rpi-cm4.dtb",
197                 true,
198         },
199 };
200
201 static const struct rpi_model rpi_models_old_scheme[] = {
202         [0x2] = {
203                 "Model B",
204                 DTB_DIR "bcm2835-rpi-b.dtb",
205                 true,
206         },
207         [0x3] = {
208                 "Model B",
209                 DTB_DIR "bcm2835-rpi-b.dtb",
210                 true,
211         },
212         [0x4] = {
213                 "Model B rev2",
214                 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
215                 true,
216         },
217         [0x5] = {
218                 "Model B rev2",
219                 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
220                 true,
221         },
222         [0x6] = {
223                 "Model B rev2",
224                 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
225                 true,
226         },
227         [0x7] = {
228                 "Model A",
229                 DTB_DIR "bcm2835-rpi-a.dtb",
230                 false,
231         },
232         [0x8] = {
233                 "Model A",
234                 DTB_DIR "bcm2835-rpi-a.dtb",
235                 false,
236         },
237         [0x9] = {
238                 "Model A",
239                 DTB_DIR "bcm2835-rpi-a.dtb",
240                 false,
241         },
242         [0xd] = {
243                 "Model B rev2",
244                 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
245                 true,
246         },
247         [0xe] = {
248                 "Model B rev2",
249                 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
250                 true,
251         },
252         [0xf] = {
253                 "Model B rev2",
254                 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
255                 true,
256         },
257         [0x10] = {
258                 "Model B+",
259                 DTB_DIR "bcm2835-rpi-b-plus.dtb",
260                 true,
261         },
262         [0x11] = {
263                 "Compute Module",
264                 DTB_DIR "bcm2835-rpi-cm.dtb",
265                 false,
266         },
267         [0x12] = {
268                 "Model A+",
269                 DTB_DIR "bcm2835-rpi-a-plus.dtb",
270                 false,
271         },
272         [0x13] = {
273                 "Model B+",
274                 DTB_DIR "bcm2835-rpi-b-plus.dtb",
275                 true,
276         },
277         [0x14] = {
278                 "Compute Module",
279                 DTB_DIR "bcm2835-rpi-cm.dtb",
280                 false,
281         },
282         [0x15] = {
283                 "Model A+",
284                 DTB_DIR "bcm2835-rpi-a-plus.dtb",
285                 false,
286         },
287 };
288
289 static uint32_t revision;
290 static uint32_t rev_scheme;
291 static uint32_t rev_type;
292 static const struct rpi_model *model;
293
294 int dram_init(void)
295 {
296         ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_arm_mem, msg, 1);
297         int ret;
298
299         BCM2835_MBOX_INIT_HDR(msg);
300         BCM2835_MBOX_INIT_TAG(&msg->get_arm_mem, GET_ARM_MEMORY);
301
302         ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
303         if (ret) {
304                 printf("bcm2835: Could not query ARM memory size\n");
305                 return -1;
306         }
307
308         gd->ram_size = msg->get_arm_mem.body.resp.mem_size;
309
310         /*
311          * In some configurations the memory size returned by VideoCore
312          * is not aligned to the section size, what is mandatory for
313          * the u-boot's memory setup.
314          */
315         gd->ram_size &= ~MMU_SECTION_SIZE;
316
317         return 0;
318 }
319
320 #ifdef CONFIG_OF_BOARD
321 int dram_init_banksize(void)
322 {
323         int ret;
324
325         ret = fdtdec_setup_memory_banksize();
326         if (ret)
327                 return ret;
328
329         return fdtdec_setup_mem_size_base();
330 }
331 #endif
332
333 static void set_fdtfile(void)
334 {
335         const char *fdtfile;
336
337         if (env_get("fdtfile"))
338                 return;
339
340         fdtfile = model->fdtfile;
341         env_set("fdtfile", fdtfile);
342 }
343
344 /*
345  * If the firmware provided a valid FDT at boot time, let's expose it in
346  * ${fdt_addr} so it may be passed unmodified to the kernel.
347  */
348 static void set_fdt_addr(void)
349 {
350         if (env_get("fdt_addr"))
351                 return;
352
353         if (fdt_magic(fw_dtb_pointer) != FDT_MAGIC)
354                 return;
355
356         env_set_hex("fdt_addr", fw_dtb_pointer);
357 }
358
359 /*
360  * Prevent relocation from stomping on a firmware provided FDT blob.
361  */
362 unsigned long board_get_usable_ram_top(unsigned long total_size)
363 {
364         if ((gd->ram_top - fw_dtb_pointer) > SZ_64M)
365                 return gd->ram_top;
366         return fw_dtb_pointer & ~0xffff;
367 }
368
369 static void get_rsts(void)
370 {
371         ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_rsts, msg, 1);
372         int ret;
373         char rsts_string[11] = { 0 };
374
375         BCM2835_MBOX_INIT_HDR(msg);
376         BCM2835_MBOX_INIT_TAG(&msg->get_rsts, GET_RSTS);
377
378         BCM2835_MBOX_DUMP_MSG(msg);
379
380         ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
381         if (ret) {
382                 printf("bcm2835: Could not query RSTS value\n");
383                 /* Ignore the error, let's carry on. */
384                 return;
385         }
386
387         BCM2835_MBOX_DUMP_MSG(msg);
388
389         snprintf(rsts_string, sizeof(rsts_string), "0x%08X",
390                  msg->get_rsts.body.resp.rsts);
391         env_set("reg_rsts", rsts_string);
392
393         return;
394 }
395
396 static void set_usbethaddr(void)
397 {
398         ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_mac_address, msg, 1);
399         int ret;
400
401         if (!model->has_onboard_eth)
402                 return;
403
404         if (env_get("usbethaddr"))
405                 return;
406
407         BCM2835_MBOX_INIT_HDR(msg);
408         BCM2835_MBOX_INIT_TAG(&msg->get_mac_address, GET_MAC_ADDRESS);
409
410         ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
411         if (ret) {
412                 printf("bcm2835: Could not query MAC address\n");
413                 /* Ignore error; not critical */
414                 return;
415         }
416
417         eth_env_set_enetaddr("usbethaddr", msg->get_mac_address.body.resp.mac);
418
419         if (!env_get("ethaddr"))
420                 env_set("ethaddr", env_get("usbethaddr"));
421
422         return;
423 }
424
425 #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
426 static void set_board_info(void)
427 {
428         char s[11];
429
430         snprintf(s, sizeof(s), "0x%X", revision);
431         env_set("board_revision", s);
432         snprintf(s, sizeof(s), "%d", rev_scheme);
433         env_set("board_rev_scheme", s);
434         /* Can't rename this to board_rev_type since it's an ABI for scripts */
435         snprintf(s, sizeof(s), "0x%X", rev_type);
436         env_set("board_rev", s);
437         env_set("board_name", model->name);
438 }
439 #endif /* CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG */
440
441 static void set_serial_number(void)
442 {
443         ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_serial, msg, 1);
444         int ret;
445         char serial_string[17] = { 0 };
446
447         if (env_get("serial#"))
448                 return;
449
450         BCM2835_MBOX_INIT_HDR(msg);
451         BCM2835_MBOX_INIT_TAG_NO_REQ(&msg->get_board_serial, GET_BOARD_SERIAL);
452
453         ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
454         if (ret) {
455                 printf("bcm2835: Could not query board serial\n");
456                 /* Ignore error; not critical */
457                 return;
458         }
459
460         snprintf(serial_string, sizeof(serial_string), "%016llx",
461                  msg->get_board_serial.body.resp.serial);
462         env_set("serial#", serial_string);
463 }
464
465 #if defined(CONFIG_PCI) && defined(CONFIG_BOARD_EARLY_INIT_R)
466 static char *boot_interface;
467 int board_early_init_r(void)
468 {
469         /*
470          * Currently there is not way to detect which device (SD card or
471          * USB Mass Storage) has been used by VideoCore to load the uboot,
472          * so just try MMC0 first, if not then use USB.
473          */
474         mmc_init_device(0);
475         boot_interface = (blk_get_dev("mmc", 0)) ? "mmc" : "usb";
476
477         pci_init();
478         usb_init();
479
480         return 0;
481 }
482
483 char *env_fat_get_interface(void)
484 {
485         return boot_interface;
486 }
487 #endif
488
489 int misc_init_r(void)
490 {
491         set_fdt_addr();
492         set_fdtfile();
493         set_usbethaddr();
494         get_rsts();
495 #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
496         set_board_info();
497 #endif
498         set_serial_number();
499
500         return 0;
501 }
502
503 static void get_board_rev(void)
504 {
505         ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_rev, msg, 1);
506         int ret;
507         const struct rpi_model *models;
508         uint32_t models_count;
509
510         BCM2835_MBOX_INIT_HDR(msg);
511         BCM2835_MBOX_INIT_TAG(&msg->get_board_rev, GET_BOARD_REV);
512
513         ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
514         if (ret) {
515                 printf("bcm2835: Could not query board revision\n");
516                 /* Ignore error; not critical */
517                 return;
518         }
519
520         /*
521          * For details of old-vs-new scheme, see:
522          * https://github.com/pimoroni/RPi.version/blob/master/RPi/version.py
523          * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=99293&p=690282
524          * (a few posts down)
525          *
526          * For the RPi 1, bit 24 is the "warranty bit", so we mask off just the
527          * lower byte to use as the board rev:
528          * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=98367&start=250
529          * http://www.raspberrypi.org/forums/viewtopic.php?f=31&t=20594
530          */
531         revision = msg->get_board_rev.body.resp.rev;
532         if (revision & 0x800000) {
533                 rev_scheme = 1;
534                 rev_type = (revision >> 4) & 0xff;
535                 models = rpi_models_new_scheme;
536                 models_count = ARRAY_SIZE(rpi_models_new_scheme);
537         } else {
538                 rev_scheme = 0;
539                 rev_type = revision & 0xff;
540                 models = rpi_models_old_scheme;
541                 models_count = ARRAY_SIZE(rpi_models_old_scheme);
542         }
543         if (rev_type >= models_count) {
544                 printf("RPI: Board rev 0x%x outside known range\n", rev_type);
545                 model = &rpi_model_unknown;
546         } else if (!models[rev_type].name) {
547                 printf("RPI: Board rev 0x%x unknown\n", rev_type);
548                 model = &rpi_model_unknown;
549         } else {
550                 model = &models[rev_type];
551         }
552
553         printf("RPI %s (0x%x)\n", model->name, revision);
554
555         printf("CPU: %s\n", CONFIG_SYS_CPU);
556 }
557
558 int board_init(void)
559 {
560 #ifdef CONFIG_HW_WATCHDOG
561         hw_watchdog_init();
562 #endif
563
564         get_board_rev();
565
566         gd->bd->bi_boot_params = 0x100;
567
568         return bcm2835_power_on_module(BCM2835_MBOX_POWER_DEVID_USB_HCD);
569 }
570
571 /*
572  * If the firmware passed a device tree use it for U-Boot.
573  */
574 void *board_fdt_blob_setup(void)
575 {
576         if (fdt_magic(fw_dtb_pointer) != FDT_MAGIC)
577                 return NULL;
578         return (void *)fw_dtb_pointer;
579 }
580
581 int ft_board_setup(void *blob, struct bd_info *bd)
582 {
583         int node;
584
585         node = fdt_node_offset_by_compatible(blob, -1, "simple-framebuffer");
586         if (node < 0)
587                 lcd_dt_simplefb_add_node(blob);
588
589 #ifdef CONFIG_EFI_LOADER
590         /* Reserve the spin table */
591         efi_add_memory_map(0, CONFIG_RPI_EFI_NR_SPIN_PAGES << EFI_PAGE_SHIFT,
592                            EFI_RESERVED_MEMORY_TYPE);
593 #endif
594
595         return 0;
596 }
597
598 int checkboard(void)
599 {
600         return 0;
601 }