Merge branch '2020-05-18-reduce-size-of-common.h'
[oweals/u-boot.git] / arch / arm / cpu / armv8 / fsl-layerscape / fdt.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2014-2015 Freescale Semiconductor, Inc.
4  * Copyright 2020 NXP
5  */
6
7 #include <common.h>
8 #include <clock_legacy.h>
9 #include <efi_loader.h>
10 #include <log.h>
11 #include <asm/cache.h>
12 #include <linux/libfdt.h>
13 #include <fdt_support.h>
14 #include <phy.h>
15 #ifdef CONFIG_FSL_LSCH3
16 #include <asm/arch/fdt.h>
17 #endif
18 #ifdef CONFIG_FSL_ESDHC
19 #include <fsl_esdhc.h>
20 #endif
21 #ifdef CONFIG_SYS_DPAA_FMAN
22 #include <fsl_fman.h>
23 #endif
24 #ifdef CONFIG_MP
25 #include <asm/arch/mp.h>
26 #endif
27 #include <fsl_sec.h>
28 #include <asm/arch-fsl-layerscape/soc.h>
29 #ifdef CONFIG_ARMV8_SEC_FIRMWARE_SUPPORT
30 #include <asm/armv8/sec_firmware.h>
31 #endif
32 #include <asm/arch/speed.h>
33 #include <fsl_qbman.h>
34
35 int fdt_fixup_phy_connection(void *blob, int offset, phy_interface_t phyc)
36 {
37         const char *conn;
38
39         /* Do NOT apply fixup for backplane modes specified in DT */
40         if (phyc == PHY_INTERFACE_MODE_XGMII) {
41                 conn = fdt_getprop(blob, offset, "phy-connection-type", NULL);
42                 if (is_backplane_mode(conn))
43                         return 0;
44         }
45         return fdt_setprop_string(blob, offset, "phy-connection-type",
46                                          phy_string_for_interface(phyc));
47 }
48
49 #ifdef CONFIG_MP
50 void ft_fixup_cpu(void *blob)
51 {
52         int off;
53         __maybe_unused u64 spin_tbl_addr = (u64)get_spin_tbl_addr();
54         fdt32_t *reg;
55         int addr_cells;
56         u64 val, core_id;
57         size_t *boot_code_size = &(__secondary_boot_code_size);
58         u32 mask = cpu_pos_mask();
59         int off_prev = -1;
60
61         off = fdt_path_offset(blob, "/cpus");
62         if (off < 0) {
63                 puts("couldn't find /cpus node\n");
64                 return;
65         }
66
67         fdt_support_default_count_cells(blob, off, &addr_cells, NULL);
68
69         off = fdt_node_offset_by_prop_value(blob, off_prev, "device_type",
70                                             "cpu", 4);
71         while (off != -FDT_ERR_NOTFOUND) {
72                 reg = (fdt32_t *)fdt_getprop(blob, off, "reg", 0);
73                 if (reg) {
74                         core_id = fdt_read_number(reg, addr_cells);
75                         if (!test_bit(id_to_core(core_id), &mask)) {
76                                 fdt_del_node(blob, off);
77                                 off = off_prev;
78                         }
79                 }
80                 off_prev = off;
81                 off = fdt_node_offset_by_prop_value(blob, off_prev,
82                                                     "device_type", "cpu", 4);
83         }
84
85 #if defined(CONFIG_ARMV8_SEC_FIRMWARE_SUPPORT) && \
86         defined(CONFIG_SEC_FIRMWARE_ARMV8_PSCI)
87         int node;
88         u32 psci_ver;
89
90         /* Check the psci version to determine if the psci is supported */
91         psci_ver = sec_firmware_support_psci_version();
92         if (psci_ver == 0xffffffff) {
93                 /* remove psci DT node */
94                 node = fdt_path_offset(blob, "/psci");
95                 if (node >= 0)
96                         goto remove_psci_node;
97
98                 node = fdt_node_offset_by_compatible(blob, -1, "arm,psci");
99                 if (node >= 0)
100                         goto remove_psci_node;
101
102                 node = fdt_node_offset_by_compatible(blob, -1, "arm,psci-0.2");
103                 if (node >= 0)
104                         goto remove_psci_node;
105
106                 node = fdt_node_offset_by_compatible(blob, -1, "arm,psci-1.0");
107                 if (node >= 0)
108                         goto remove_psci_node;
109
110 remove_psci_node:
111                 if (node >= 0)
112                         fdt_del_node(blob, node);
113         } else {
114                 return;
115         }
116 #endif
117         off = fdt_path_offset(blob, "/cpus");
118         if (off < 0) {
119                 puts("couldn't find /cpus node\n");
120                 return;
121         }
122         fdt_support_default_count_cells(blob, off, &addr_cells, NULL);
123
124         off = fdt_node_offset_by_prop_value(blob, -1, "device_type", "cpu", 4);
125         while (off != -FDT_ERR_NOTFOUND) {
126                 reg = (fdt32_t *)fdt_getprop(blob, off, "reg", 0);
127                 if (reg) {
128                         core_id = fdt_read_number(reg, addr_cells);
129                         if (core_id  == 0 || (is_core_online(core_id))) {
130                                 val = spin_tbl_addr;
131                                 val += id_to_core(core_id) *
132                                        SPIN_TABLE_ELEM_SIZE;
133                                 val = cpu_to_fdt64(val);
134                                 fdt_setprop_string(blob, off, "enable-method",
135                                                    "spin-table");
136                                 fdt_setprop(blob, off, "cpu-release-addr",
137                                             &val, sizeof(val));
138                         } else {
139                                 debug("skipping offline core\n");
140                         }
141                 } else {
142                         puts("Warning: found cpu node without reg property\n");
143                 }
144                 off = fdt_node_offset_by_prop_value(blob, off, "device_type",
145                                                     "cpu", 4);
146         }
147
148         fdt_add_mem_rsv(blob, (uintptr_t)&secondary_boot_code,
149                         *boot_code_size);
150 #if CONFIG_IS_ENABLED(EFI_LOADER)
151         efi_add_memory_map((uintptr_t)&secondary_boot_code, *boot_code_size,
152                            EFI_RESERVED_MEMORY_TYPE);
153 #endif
154 }
155 #endif
156
157 void fsl_fdt_disable_usb(void *blob)
158 {
159         int off;
160         /*
161          * SYSCLK is used as a reference clock for USB. When the USB
162          * controller is used, SYSCLK must meet the additional requirement
163          * of 100 MHz.
164          */
165         if (CONFIG_SYS_CLK_FREQ != 100000000) {
166                 off = fdt_node_offset_by_compatible(blob, -1, "snps,dwc3");
167                 while (off != -FDT_ERR_NOTFOUND) {
168                         fdt_status_disabled(blob, off);
169                         off = fdt_node_offset_by_compatible(blob, off,
170                                                             "snps,dwc3");
171                 }
172         }
173 }
174
175 #ifdef CONFIG_HAS_FEATURE_GIC64K_ALIGN
176 static void fdt_fixup_gic(void *blob)
177 {
178         int offset, err;
179         u64 reg[8];
180         struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
181         unsigned int val;
182         struct ccsr_scfg __iomem *scfg = (void *)CONFIG_SYS_FSL_SCFG_ADDR;
183         int align_64k = 0;
184
185         val = gur_in32(&gur->svr);
186
187         if (!IS_SVR_DEV(val, SVR_DEV(SVR_LS1043A))) {
188                 align_64k = 1;
189         } else if (SVR_REV(val) != REV1_0) {
190                 val = scfg_in32(&scfg->gic_align) & (0x01 << GIC_ADDR_BIT);
191                 if (!val)
192                         align_64k = 1;
193         }
194
195         offset = fdt_subnode_offset(blob, 0, "interrupt-controller@1400000");
196         if (offset < 0) {
197                 printf("WARNING: fdt_subnode_offset can't find node %s: %s\n",
198                        "interrupt-controller@1400000", fdt_strerror(offset));
199                 return;
200         }
201
202         /* Fixup gic node align with 64K */
203         if (align_64k) {
204                 reg[0] = cpu_to_fdt64(GICD_BASE_64K);
205                 reg[1] = cpu_to_fdt64(GICD_SIZE_64K);
206                 reg[2] = cpu_to_fdt64(GICC_BASE_64K);
207                 reg[3] = cpu_to_fdt64(GICC_SIZE_64K);
208                 reg[4] = cpu_to_fdt64(GICH_BASE_64K);
209                 reg[5] = cpu_to_fdt64(GICH_SIZE_64K);
210                 reg[6] = cpu_to_fdt64(GICV_BASE_64K);
211                 reg[7] = cpu_to_fdt64(GICV_SIZE_64K);
212         } else {
213         /* Fixup gic node align with default */
214                 reg[0] = cpu_to_fdt64(GICD_BASE);
215                 reg[1] = cpu_to_fdt64(GICD_SIZE);
216                 reg[2] = cpu_to_fdt64(GICC_BASE);
217                 reg[3] = cpu_to_fdt64(GICC_SIZE);
218                 reg[4] = cpu_to_fdt64(GICH_BASE);
219                 reg[5] = cpu_to_fdt64(GICH_SIZE);
220                 reg[6] = cpu_to_fdt64(GICV_BASE);
221                 reg[7] = cpu_to_fdt64(GICV_SIZE);
222         }
223
224         err = fdt_setprop(blob, offset, "reg", reg, sizeof(reg));
225         if (err < 0) {
226                 printf("WARNING: fdt_setprop can't set %s from node %s: %s\n",
227                        "reg", "interrupt-controller@1400000",
228                        fdt_strerror(err));
229                 return;
230         }
231
232         return;
233 }
234 #endif
235
236 #ifdef CONFIG_HAS_FEATURE_ENHANCED_MSI
237 static int _fdt_fixup_msi_node(void *blob, const char *name,
238                                   int irq_0, int irq_1, int rev)
239 {
240         int err, offset, len;
241         u32 tmp[4][3];
242         void *p;
243
244         offset = fdt_path_offset(blob, name);
245         if (offset < 0) {
246                 printf("WARNING: fdt_path_offset can't find path %s: %s\n",
247                        name, fdt_strerror(offset));
248                 return 0;
249         }
250
251         /*fixup the property of interrupts*/
252
253         tmp[0][0] = cpu_to_fdt32(0x0);
254         tmp[0][1] = cpu_to_fdt32(irq_0);
255         tmp[0][2] = cpu_to_fdt32(0x4);
256
257         if (rev > REV1_0) {
258                 tmp[1][0] = cpu_to_fdt32(0x0);
259                 tmp[1][1] = cpu_to_fdt32(irq_1);
260                 tmp[1][2] = cpu_to_fdt32(0x4);
261                 tmp[2][0] = cpu_to_fdt32(0x0);
262                 tmp[2][1] = cpu_to_fdt32(irq_1 + 1);
263                 tmp[2][2] = cpu_to_fdt32(0x4);
264                 tmp[3][0] = cpu_to_fdt32(0x0);
265                 tmp[3][1] = cpu_to_fdt32(irq_1 + 2);
266                 tmp[3][2] = cpu_to_fdt32(0x4);
267                 len = sizeof(tmp);
268         } else {
269                 len = sizeof(tmp[0]);
270         }
271
272         err = fdt_setprop(blob, offset, "interrupts", tmp, len);
273         if (err < 0) {
274                 printf("WARNING: fdt_setprop can't set %s from node %s: %s\n",
275                        "interrupts", name, fdt_strerror(err));
276                 return 0;
277         }
278
279         /*fixup the property of reg*/
280         p = (char *)fdt_getprop(blob, offset, "reg", &len);
281         if (!p) {
282                 printf("WARNING: fdt_getprop can't get %s from node %s\n",
283                        "reg", name);
284                 return 0;
285         }
286
287         memcpy((char *)tmp, p, len);
288
289         if (rev > REV1_0)
290                 *((u32 *)tmp + 3) = cpu_to_fdt32(0x1000);
291         else
292                 *((u32 *)tmp + 3) = cpu_to_fdt32(0x8);
293
294         err = fdt_setprop(blob, offset, "reg", tmp, len);
295         if (err < 0) {
296                 printf("WARNING: fdt_setprop can't set %s from node %s: %s\n",
297                        "reg", name, fdt_strerror(err));
298                 return 0;
299         }
300
301         /*fixup the property of compatible*/
302         if (rev > REV1_0)
303                 err = fdt_setprop_string(blob, offset, "compatible",
304                                          "fsl,ls1043a-v1.1-msi");
305         else
306                 err = fdt_setprop_string(blob, offset, "compatible",
307                                          "fsl,ls1043a-msi");
308         if (err < 0) {
309                 printf("WARNING: fdt_setprop can't set %s from node %s: %s\n",
310                        "compatible", name, fdt_strerror(err));
311                 return 0;
312         }
313
314         return 1;
315 }
316
317 static int _fdt_fixup_pci_msi(void *blob, const char *name, int rev)
318 {
319         int offset, len, err;
320         void *p;
321         int val;
322         u32 tmp[4][8];
323
324         offset = fdt_path_offset(blob, name);
325         if (offset < 0) {
326                 printf("WARNING: fdt_path_offset can't find path %s: %s\n",
327                        name, fdt_strerror(offset));
328                 return 0;
329         }
330
331         p = (char *)fdt_getprop(blob, offset, "interrupt-map", &len);
332         if (!p || len != sizeof(tmp)) {
333                 printf("WARNING: fdt_getprop can't get %s from node %s\n",
334                        "interrupt-map", name);
335                 return 0;
336         }
337
338         memcpy((char *)tmp, p, len);
339
340         val = fdt32_to_cpu(tmp[0][6]);
341         if (rev == REV1_0) {
342                 tmp[1][6] = cpu_to_fdt32(val + 1);
343                 tmp[2][6] = cpu_to_fdt32(val + 2);
344                 tmp[3][6] = cpu_to_fdt32(val + 3);
345         } else {
346                 tmp[1][6] = cpu_to_fdt32(val);
347                 tmp[2][6] = cpu_to_fdt32(val);
348                 tmp[3][6] = cpu_to_fdt32(val);
349         }
350
351         err = fdt_setprop(blob, offset, "interrupt-map", tmp, sizeof(tmp));
352         if (err < 0) {
353                 printf("WARNING: fdt_setprop can't set %s from node %s: %s.\n",
354                        "interrupt-map", name, fdt_strerror(err));
355                 return 0;
356         }
357         return 1;
358 }
359
360 /* Fixup msi node for ls1043a rev1.1*/
361
362 static void fdt_fixup_msi(void *blob)
363 {
364         struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
365         unsigned int rev;
366
367         rev = gur_in32(&gur->svr);
368
369         if (!IS_SVR_DEV(rev, SVR_DEV(SVR_LS1043A)))
370                 return;
371
372         rev = SVR_REV(rev);
373
374         _fdt_fixup_msi_node(blob, "/soc/msi-controller1@1571000",
375                             116, 111, rev);
376         _fdt_fixup_msi_node(blob, "/soc/msi-controller2@1572000",
377                             126, 121, rev);
378         _fdt_fixup_msi_node(blob, "/soc/msi-controller3@1573000",
379                             160, 155, rev);
380
381         _fdt_fixup_pci_msi(blob, "/soc/pcie@3400000", rev);
382         _fdt_fixup_pci_msi(blob, "/soc/pcie@3500000", rev);
383         _fdt_fixup_pci_msi(blob, "/soc/pcie@3600000", rev);
384 }
385 #endif
386
387 #ifdef CONFIG_ARMV8_SEC_FIRMWARE_SUPPORT
388 /* Remove JR node used by SEC firmware */
389 void fdt_fixup_remove_jr(void *blob)
390 {
391         int jr_node, addr_cells, len;
392         int crypto_node = fdt_path_offset(blob, "crypto");
393         u64 jr_offset, used_jr;
394         fdt32_t *reg;
395
396         used_jr = sec_firmware_used_jobring_offset();
397         fdt_support_default_count_cells(blob, crypto_node, &addr_cells, NULL);
398
399         jr_node = fdt_node_offset_by_compatible(blob, crypto_node,
400                                                 "fsl,sec-v4.0-job-ring");
401
402         while (jr_node != -FDT_ERR_NOTFOUND) {
403                 reg = (fdt32_t *)fdt_getprop(blob, jr_node, "reg", &len);
404                 jr_offset = fdt_read_number(reg, addr_cells);
405                 if (jr_offset == used_jr) {
406                         fdt_del_node(blob, jr_node);
407                         break;
408                 }
409                 jr_node = fdt_node_offset_by_compatible(blob, jr_node,
410                                                         "fsl,sec-v4.0-job-ring");
411         }
412 }
413 #endif
414
415 #ifdef CONFIG_ARCH_LS1028A
416 static void fdt_disable_multimedia(void *blob, unsigned int svr)
417 {
418         int off;
419
420         if (IS_MULTIMEDIA_EN(svr))
421                 return;
422
423         /* Disable eDP/LCD node */
424         off = fdt_node_offset_by_compatible(blob, -1, "arm,mali-dp500");
425         if (off != -FDT_ERR_NOTFOUND)
426                 fdt_status_disabled(blob, off);
427
428         /* Disable GPU node */
429         off = fdt_node_offset_by_compatible(blob, -1, "fsl,ls1028a-gpu");
430         if (off != -FDT_ERR_NOTFOUND)
431                 fdt_status_disabled(blob, off);
432 }
433 #endif
434
435 #ifdef CONFIG_PCIE_ECAM_GENERIC
436 __weak void fdt_fixup_ecam(void *blob)
437 {
438 }
439 #endif
440
441 void ft_cpu_setup(void *blob, bd_t *bd)
442 {
443         struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
444         unsigned int svr = gur_in32(&gur->svr);
445
446         /* delete crypto node if not on an E-processor */
447         if (!IS_E_PROCESSOR(svr))
448                 fdt_fixup_crypto_node(blob, 0);
449 #if CONFIG_SYS_FSL_SEC_COMPAT >= 4
450         else {
451                 ccsr_sec_t __iomem *sec;
452
453 #ifdef CONFIG_ARMV8_SEC_FIRMWARE_SUPPORT
454                 fdt_fixup_remove_jr(blob);
455                 fdt_fixup_kaslr(blob);
456 #endif
457
458                 sec = (void __iomem *)CONFIG_SYS_FSL_SEC_ADDR;
459                 fdt_fixup_crypto_node(blob, sec_in32(&sec->secvid_ms));
460         }
461 #endif
462
463 #ifdef CONFIG_MP
464         ft_fixup_cpu(blob);
465 #endif
466
467 #ifdef CONFIG_SYS_NS16550
468         do_fixup_by_compat_u32(blob, "fsl,ns16550",
469                                "clock-frequency", CONFIG_SYS_NS16550_CLK, 1);
470 #endif
471
472         do_fixup_by_path_u32(blob, "/sysclk", "clock-frequency",
473                              CONFIG_SYS_CLK_FREQ, 1);
474
475 #ifdef CONFIG_GIC_V3_ITS
476         ls_gic_rd_tables_init(blob);
477 #endif
478
479 #if defined(CONFIG_PCIE_LAYERSCAPE) || defined(CONFIG_PCIE_LAYERSCAPE_GEN4)
480         ft_pci_setup(blob, bd);
481 #endif
482
483 #ifdef CONFIG_FSL_ESDHC
484         fdt_fixup_esdhc(blob, bd);
485 #endif
486
487 #ifdef CONFIG_SYS_DPAA_QBMAN
488         fdt_fixup_bportals(blob);
489         fdt_fixup_qportals(blob);
490         do_fixup_by_compat_u32(blob, "fsl,qman",
491                                "clock-frequency", get_qman_freq(), 1);
492 #endif
493
494 #ifdef CONFIG_SYS_DPAA_FMAN
495         fdt_fixup_fman_firmware(blob);
496 #endif
497 #ifndef CONFIG_ARCH_LS1012A
498         fsl_fdt_disable_usb(blob);
499 #endif
500 #ifdef CONFIG_HAS_FEATURE_GIC64K_ALIGN
501         fdt_fixup_gic(blob);
502 #endif
503 #ifdef CONFIG_HAS_FEATURE_ENHANCED_MSI
504         fdt_fixup_msi(blob);
505 #endif
506 #ifdef CONFIG_ARCH_LS1028A
507         fdt_disable_multimedia(blob, svr);
508 #endif
509 #ifdef CONFIG_PCIE_ECAM_GENERIC
510         fdt_fixup_ecam(blob);
511 #endif
512 }