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