5062486311e229f3d8e22bc77e4db3483bd7b596
[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 <environment.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 };
142
143 static const struct rpi_model rpi_models_old_scheme[] = {
144         [0x2] = {
145                 "Model B",
146                 DTB_DIR "bcm2835-rpi-b.dtb",
147                 true,
148         },
149         [0x3] = {
150                 "Model B",
151                 DTB_DIR "bcm2835-rpi-b.dtb",
152                 true,
153         },
154         [0x4] = {
155                 "Model B rev2",
156                 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
157                 true,
158         },
159         [0x5] = {
160                 "Model B rev2",
161                 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
162                 true,
163         },
164         [0x6] = {
165                 "Model B rev2",
166                 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
167                 true,
168         },
169         [0x7] = {
170                 "Model A",
171                 DTB_DIR "bcm2835-rpi-a.dtb",
172                 false,
173         },
174         [0x8] = {
175                 "Model A",
176                 DTB_DIR "bcm2835-rpi-a.dtb",
177                 false,
178         },
179         [0x9] = {
180                 "Model A",
181                 DTB_DIR "bcm2835-rpi-a.dtb",
182                 false,
183         },
184         [0xd] = {
185                 "Model B rev2",
186                 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
187                 true,
188         },
189         [0xe] = {
190                 "Model B rev2",
191                 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
192                 true,
193         },
194         [0xf] = {
195                 "Model B rev2",
196                 DTB_DIR "bcm2835-rpi-b-rev2.dtb",
197                 true,
198         },
199         [0x10] = {
200                 "Model B+",
201                 DTB_DIR "bcm2835-rpi-b-plus.dtb",
202                 true,
203         },
204         [0x11] = {
205                 "Compute Module",
206                 DTB_DIR "bcm2835-rpi-cm.dtb",
207                 false,
208         },
209         [0x12] = {
210                 "Model A+",
211                 DTB_DIR "bcm2835-rpi-a-plus.dtb",
212                 false,
213         },
214         [0x13] = {
215                 "Model B+",
216                 DTB_DIR "bcm2835-rpi-b-plus.dtb",
217                 true,
218         },
219         [0x14] = {
220                 "Compute Module",
221                 DTB_DIR "bcm2835-rpi-cm.dtb",
222                 false,
223         },
224         [0x15] = {
225                 "Model A+",
226                 DTB_DIR "bcm2835-rpi-a-plus.dtb",
227                 false,
228         },
229 };
230
231 static uint32_t revision;
232 static uint32_t rev_scheme;
233 static uint32_t rev_type;
234 static const struct rpi_model *model;
235
236 #ifdef CONFIG_ARM64
237 static struct mm_region bcm2837_mem_map[] = {
238         {
239                 .virt = 0x00000000UL,
240                 .phys = 0x00000000UL,
241                 .size = 0x3f000000UL,
242                 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
243                          PTE_BLOCK_INNER_SHARE
244         }, {
245                 .virt = 0x3f000000UL,
246                 .phys = 0x3f000000UL,
247                 .size = 0x01000000UL,
248                 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
249                          PTE_BLOCK_NON_SHARE |
250                          PTE_BLOCK_PXN | PTE_BLOCK_UXN
251         }, {
252                 /* List terminator */
253                 0,
254         }
255 };
256
257 struct mm_region *mem_map = bcm2837_mem_map;
258 #endif
259
260 int dram_init(void)
261 {
262         ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_arm_mem, msg, 1);
263         int ret;
264
265         BCM2835_MBOX_INIT_HDR(msg);
266         BCM2835_MBOX_INIT_TAG(&msg->get_arm_mem, GET_ARM_MEMORY);
267
268         ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
269         if (ret) {
270                 printf("bcm2835: Could not query ARM memory size\n");
271                 return -1;
272         }
273
274         gd->ram_size = msg->get_arm_mem.body.resp.mem_size;
275
276         return 0;
277 }
278
279 static void set_fdtfile(void)
280 {
281         const char *fdtfile;
282
283         if (env_get("fdtfile"))
284                 return;
285
286         fdtfile = model->fdtfile;
287         env_set("fdtfile", fdtfile);
288 }
289
290 /*
291  * If the firmware provided a valid FDT at boot time, let's expose it in
292  * ${fdt_addr} so it may be passed unmodified to the kernel.
293  */
294 static void set_fdt_addr(void)
295 {
296         if (env_get("fdt_addr"))
297                 return;
298
299         if (fdt_magic(fw_dtb_pointer) != FDT_MAGIC)
300                 return;
301
302         env_set_hex("fdt_addr", fw_dtb_pointer);
303 }
304
305 /*
306  * Prevent relocation from stomping on a firmware provided FDT blob.
307  */
308 unsigned long board_get_usable_ram_top(unsigned long total_size)
309 {
310         if ((gd->ram_top - fw_dtb_pointer) > SZ_64M)
311                 return gd->ram_top;
312         return fw_dtb_pointer & ~0xffff;
313 }
314
315 static void set_usbethaddr(void)
316 {
317         ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_mac_address, msg, 1);
318         int ret;
319
320         if (!model->has_onboard_eth)
321                 return;
322
323         if (env_get("usbethaddr"))
324                 return;
325
326         BCM2835_MBOX_INIT_HDR(msg);
327         BCM2835_MBOX_INIT_TAG(&msg->get_mac_address, GET_MAC_ADDRESS);
328
329         ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
330         if (ret) {
331                 printf("bcm2835: Could not query MAC address\n");
332                 /* Ignore error; not critical */
333                 return;
334         }
335
336         eth_env_set_enetaddr("usbethaddr", msg->get_mac_address.body.resp.mac);
337
338         if (!env_get("ethaddr"))
339                 env_set("ethaddr", env_get("usbethaddr"));
340
341         return;
342 }
343
344 #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
345 static void set_board_info(void)
346 {
347         char s[11];
348
349         snprintf(s, sizeof(s), "0x%X", revision);
350         env_set("board_revision", s);
351         snprintf(s, sizeof(s), "%d", rev_scheme);
352         env_set("board_rev_scheme", s);
353         /* Can't rename this to board_rev_type since it's an ABI for scripts */
354         snprintf(s, sizeof(s), "0x%X", rev_type);
355         env_set("board_rev", s);
356         env_set("board_name", model->name);
357 }
358 #endif /* CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG */
359
360 static void set_serial_number(void)
361 {
362         ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_serial, msg, 1);
363         int ret;
364         char serial_string[17] = { 0 };
365
366         if (env_get("serial#"))
367                 return;
368
369         BCM2835_MBOX_INIT_HDR(msg);
370         BCM2835_MBOX_INIT_TAG_NO_REQ(&msg->get_board_serial, GET_BOARD_SERIAL);
371
372         ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
373         if (ret) {
374                 printf("bcm2835: Could not query board serial\n");
375                 /* Ignore error; not critical */
376                 return;
377         }
378
379         snprintf(serial_string, sizeof(serial_string), "%016llx",
380                  msg->get_board_serial.body.resp.serial);
381         env_set("serial#", serial_string);
382 }
383
384 int misc_init_r(void)
385 {
386         set_fdt_addr();
387         set_fdtfile();
388         set_usbethaddr();
389 #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
390         set_board_info();
391 #endif
392         set_serial_number();
393
394         return 0;
395 }
396
397 static void get_board_rev(void)
398 {
399         ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_board_rev, msg, 1);
400         int ret;
401         const struct rpi_model *models;
402         uint32_t models_count;
403
404         BCM2835_MBOX_INIT_HDR(msg);
405         BCM2835_MBOX_INIT_TAG(&msg->get_board_rev, GET_BOARD_REV);
406
407         ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg->hdr);
408         if (ret) {
409                 printf("bcm2835: Could not query board revision\n");
410                 /* Ignore error; not critical */
411                 return;
412         }
413
414         /*
415          * For details of old-vs-new scheme, see:
416          * https://github.com/pimoroni/RPi.version/blob/master/RPi/version.py
417          * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=99293&p=690282
418          * (a few posts down)
419          *
420          * For the RPi 1, bit 24 is the "warranty bit", so we mask off just the
421          * lower byte to use as the board rev:
422          * http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=98367&start=250
423          * http://www.raspberrypi.org/forums/viewtopic.php?f=31&t=20594
424          */
425         revision = msg->get_board_rev.body.resp.rev;
426         if (revision & 0x800000) {
427                 rev_scheme = 1;
428                 rev_type = (revision >> 4) & 0xff;
429                 models = rpi_models_new_scheme;
430                 models_count = ARRAY_SIZE(rpi_models_new_scheme);
431         } else {
432                 rev_scheme = 0;
433                 rev_type = revision & 0xff;
434                 models = rpi_models_old_scheme;
435                 models_count = ARRAY_SIZE(rpi_models_old_scheme);
436         }
437         if (rev_type >= models_count) {
438                 printf("RPI: Board rev 0x%x outside known range\n", rev_type);
439                 model = &rpi_model_unknown;
440         } else if (!models[rev_type].name) {
441                 printf("RPI: Board rev 0x%x unknown\n", rev_type);
442                 model = &rpi_model_unknown;
443         } else {
444                 model = &models[rev_type];
445         }
446
447         printf("RPI %s (0x%x)\n", model->name, revision);
448 }
449
450 int board_init(void)
451 {
452 #ifdef CONFIG_HW_WATCHDOG
453         hw_watchdog_init();
454 #endif
455
456         get_board_rev();
457
458         gd->bd->bi_boot_params = 0x100;
459
460         return bcm2835_power_on_module(BCM2835_MBOX_POWER_DEVID_USB_HCD);
461 }
462
463 /*
464  * If the firmware passed a device tree use it for U-Boot.
465  */
466 void *board_fdt_blob_setup(void)
467 {
468         if (fdt_magic(fw_dtb_pointer) != FDT_MAGIC)
469                 return NULL;
470         return (void *)fw_dtb_pointer;
471 }
472
473 int ft_board_setup(void *blob, bd_t *bd)
474 {
475         /*
476          * For now, we simply always add the simplefb DT node. Later, we
477          * should be more intelligent, and e.g. only do this if no enabled DT
478          * node exists for the "real" graphics driver.
479          */
480         lcd_dt_simplefb_add_node(blob);
481
482 #ifdef CONFIG_EFI_LOADER
483         /* Reserve the spin table */
484         efi_add_memory_map(0, 1, EFI_RESERVED_MEMORY_TYPE, 0);
485 #endif
486
487         return 0;
488 }