riscv: Fix builtin DTB handling
authorDamien Le Moal <damien.lemoal@wdc.com>
Sun, 13 Dec 2020 13:50:37 +0000 (22:50 +0900)
committerPalmer Dabbelt <palmerdabbelt@google.com>
Fri, 8 Jan 2021 03:00:50 +0000 (19:00 -0800)
All SiPeed K210 MAIX boards have the exact same vendor, arch and
implementation IDs, preventing differentiation to select the correct
device tree to use through the SOC_BUILTIN_DTB_DECLARE() macro. This
result in this macro to be useless and mandates changing the code of
the sysctl driver to change the builtin device tree suitable for the
target board.

Fix this problem by removing the SOC_BUILTIN_DTB_DECLARE() macro since
it is used only for the K210 support. The code searching the builtin
DTBs using the vendor, arch an implementation IDs is also removed.
Support for builtin DTB falls back to the simpler and more traditional
handling of builtin DTB using the CONFIG_BUILTIN_DTB option, similarly
to other architectures.

Signed-off-by: Damien Le Moal <damien.lemoal@wdc.com>
Signed-off-by: Palmer Dabbelt <palmerdabbelt@google.com>
arch/riscv/Kconfig.socs
arch/riscv/boot/dts/Makefile
arch/riscv/boot/dts/kendryte/Makefile
arch/riscv/include/asm/soc.h
arch/riscv/kernel/soc.c
arch/riscv/mm/init.c
drivers/soc/kendryte/k210-sysctl.c

index 3284d5c291be541f0cc7d7316b9938b66e4c4ec3..59000675cb9fbe7323b9838a23e4f2c7b1335a79 100644 (file)
@@ -32,9 +32,7 @@ config SOC_KENDRYTE
        help
          This enables support for Kendryte K210 SoC platform hardware.
 
-config SOC_KENDRYTE_K210_DTB
-       def_bool y
-       depends on SOC_KENDRYTE_K210_DTB_BUILTIN
+if SOC_KENDRYTE
 
 config SOC_KENDRYTE_K210_DTB_BUILTIN
        bool "Builtin device tree for the Kendryte K210"
@@ -42,10 +40,21 @@ config SOC_KENDRYTE_K210_DTB_BUILTIN
        default y
        select OF
        select BUILTIN_DTB
-       select SOC_KENDRYTE_K210_DTB
        help
-         Builds a device tree for the Kendryte K210 into the Linux image.
+         Build a device tree for the Kendryte K210 into the Linux image.
          This option should be selected if no bootloader is being used.
          If unsure, say Y.
 
+config SOC_KENDRYTE_K210_DTB_SOURCE
+       string "Source file for the Kendryte K210 builtin DTB"
+       depends on SOC_KENDRYTE
+       depends on SOC_KENDRYTE_K210_DTB_BUILTIN
+       default "k210"
+       help
+         Base name (without suffix, relative to arch/riscv/boot/dts/kendryte)
+         for the DTS file that will be used to produce the DTB linked into the
+         kernel.
+
+endif
+
 endmenu
index ca1f8cbd78c0e3a44bf17753237d7eb517256148..21e3905f1c4468d7a267e03ea2020761182fe3c2 100644 (file)
@@ -1,5 +1,5 @@
 # SPDX-License-Identifier: GPL-2.0
 subdir-y += sifive
-subdir-y += kendryte
+subdir-$(CONFIG_SOC_KENDRYTE) += kendryte
 
 obj-$(CONFIG_BUILTIN_DTB) := $(addsuffix /, $(subdir-y))
index 1a88e616f18e0dafa7eb54842018fde2de6aaa29..83636693166dae3dbf3231eb3f6ceaaa35d5b594 100644 (file)
@@ -1,4 +1,5 @@
 # SPDX-License-Identifier: GPL-2.0
-dtb-$(CONFIG_SOC_KENDRYTE_K210_DTB) += k210.dtb
-
+ifneq ($(CONFIG_SOC_KENDRYTE_K210_DTB_SOURCE),"")
+dtb-y += $(strip $(shell echo $(CONFIG_SOC_KENDRYTE_K210_DTB_SOURCE))).dtb
 obj-$(CONFIG_SOC_KENDRYTE_K210_DTB_BUILTIN) += $(addsuffix .o, $(dtb-y))
+endif
index 6c8363b1f3272ea669ce9761783dc30436e6656f..f494066051a2c10a122c17ecf855f230571c6f98 100644 (file)
@@ -21,42 +21,4 @@ void soc_early_init(void);
 extern unsigned long __soc_early_init_table_start;
 extern unsigned long __soc_early_init_table_end;
 
-/*
- * Allows Linux to provide a device tree, which is necessary for SOCs that
- * don't provide a useful one on their own.
- */
-struct soc_builtin_dtb {
-       unsigned long vendor_id;
-       unsigned long arch_id;
-       unsigned long imp_id;
-       void *(*dtb_func)(void);
-};
-
-/*
- * The argument name must specify a valid DTS file name without the dts
- * extension.
- */
-#define SOC_BUILTIN_DTB_DECLARE(name, vendor, arch, impl)              \
-       extern void *__dtb_##name##_begin;                              \
-                                                                       \
-       static __init __used                                            \
-       void *__soc_builtin_dtb_f__##name(void)                         \
-       {                                                               \
-               return (void *)&__dtb_##name##_begin;                   \
-       }                                                               \
-                                                                       \
-       static const struct soc_builtin_dtb __soc_builtin_dtb__##name   \
-               __used __section("__soc_builtin_dtb_table") =           \
-       {                                                               \
-               .vendor_id = vendor,                                    \
-               .arch_id   = arch,                                      \
-               .imp_id    = impl,                                      \
-               .dtb_func  = __soc_builtin_dtb_f__##name,               \
-       }
-
-extern unsigned long __soc_builtin_dtb_table_start;
-extern unsigned long __soc_builtin_dtb_table_end;
-
-void *soc_lookup_builtin_dtb(void);
-
 #endif
index c7b0a73e382e920bbfb1a1d6b6488cf906bf0cfa..a0516172a33c84f0f0ce3b58afd6f984aaa5e38d 100644 (file)
@@ -26,30 +26,3 @@ void __init soc_early_init(void)
                }
        }
 }
-
-static bool soc_builtin_dtb_match(unsigned long vendor_id,
-                               unsigned long arch_id, unsigned long imp_id,
-                               const struct soc_builtin_dtb *entry)
-{
-       return entry->vendor_id == vendor_id &&
-              entry->arch_id == arch_id &&
-              entry->imp_id == imp_id;
-}
-
-void * __init soc_lookup_builtin_dtb(void)
-{
-       unsigned long vendor_id, arch_id, imp_id;
-       const struct soc_builtin_dtb *s;
-
-       __asm__ ("csrr %0, mvendorid" : "=r"(vendor_id));
-       __asm__ ("csrr %0, marchid" : "=r"(arch_id));
-       __asm__ ("csrr %0, mimpid" : "=r"(imp_id));
-
-       for (s = (void *)&__soc_builtin_dtb_table_start;
-            (void *)s < (void *)&__soc_builtin_dtb_table_end; s++) {
-               if (soc_builtin_dtb_match(vendor_id, arch_id, imp_id, s))
-                       return s->dtb_func();
-       }
-
-       return NULL;
-}
index bf5379135e39b628619de135632876d5338abe9d..77bd23f47a7247a888e3d403389288c0bdcf11a6 100644 (file)
@@ -605,11 +605,7 @@ static void __init setup_vm_final(void)
 asmlinkage void __init setup_vm(uintptr_t dtb_pa)
 {
 #ifdef CONFIG_BUILTIN_DTB
-       dtb_early_va = soc_lookup_builtin_dtb();
-       if (!dtb_early_va) {
-               /* Fallback to first available DTS */
-               dtb_early_va = (void *) __dtb_start;
-       }
+       dtb_early_va = (void *) __dtb_start;
 #else
        dtb_early_va = (void *)dtb_pa;
 #endif
index 707019223dd81bfa484f2734c724a8783faf0c44..4608fbca20e1bad035cf397aa6817842b58098d6 100644 (file)
@@ -246,15 +246,3 @@ static void __init k210_soc_early_init(const void *fdt)
        iounmap(regs);
 }
 SOC_EARLY_INIT_DECLARE(generic_k210, "kendryte,k210", k210_soc_early_init);
-
-#ifdef CONFIG_SOC_KENDRYTE_K210_DTB_BUILTIN
-/*
- * Generic entry for the default k210.dtb embedded DTB for boards with:
- *   - Vendor ID: 0x4B5
- *   - Arch ID: 0xE59889E6A5A04149 (= "Canaan AI" in UTF-8 encoded Chinese)
- *   - Impl ID:        0x4D41495832303030 (= "MAIX2000")
- * These values are reported by the SiPEED MAXDUINO, SiPEED MAIX GO and
- * SiPEED Dan dock boards.
- */
-SOC_BUILTIN_DTB_DECLARE(k210, 0x4B5, 0xE59889E6A5A04149, 0x4D41495832303030);
-#endif