xref: /linux-6.15/arch/arc/kernel/devtree.c (revision ad81fcb5)
1 /*
2  * Copyright (C) 2012 Synopsys, Inc. (www.synopsys.com)
3  *
4  * Based on reduced version of METAG
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10 
11 
12 #include <linux/init.h>
13 #include <linux/reboot.h>
14 #include <linux/memblock.h>
15 #include <linux/of.h>
16 #include <linux/of_fdt.h>
17 #include <asm/prom.h>
18 #include <asm/clk.h>
19 #include <asm/mach_desc.h>
20 
21 /**
22  * setup_machine_fdt - Machine setup when an dtb was passed to the kernel
23  * @dt:		virtual address pointer to dt blob
24  *
25  * If a dtb was passed to the kernel, then use it to choose the correct
26  * machine_desc and to setup the system.
27  */
28 struct machine_desc * __init setup_machine_fdt(void *dt)
29 {
30 	struct machine_desc *mdesc = NULL, *mdesc_best = NULL;
31 	unsigned int score, mdesc_score = ~1;
32 	unsigned long dt_root;
33 	const char *model, *compat;
34 	void *clk;
35 	char manufacturer[16];
36 	unsigned long len;
37 
38 	if (!early_init_dt_scan(dt))
39 		return NULL;
40 
41 	dt_root = of_get_flat_dt_root();
42 
43 	/*
44 	 * The kernel could be multi-platform enabled, thus could have many
45 	 * "baked-in" machine descriptors. Search thru all for the best
46 	 * "compatible" string match.
47 	 */
48 	for_each_machine_desc(mdesc) {
49 		score = of_flat_dt_match(dt_root, mdesc->dt_compat);
50 		if (score > 0 && score < mdesc_score) {
51 			mdesc_best = mdesc;
52 			mdesc_score = score;
53 		}
54 	}
55 	if (!mdesc_best) {
56 		const char *prop;
57 		long size;
58 
59 		pr_err("\n unrecognized device tree list:\n[ ");
60 
61 		prop = of_get_flat_dt_prop(dt_root, "compatible", &size);
62 		if (prop) {
63 			while (size > 0) {
64 				printk("'%s' ", prop);
65 				size -= strlen(prop) + 1;
66 				prop += strlen(prop) + 1;
67 			}
68 		}
69 		printk("]\n\n");
70 
71 		machine_halt();
72 	}
73 
74 	/* compat = "<manufacturer>,<model>" */
75 	compat =  mdesc_best->dt_compat[0];
76 
77 	model = strchr(compat, ',');
78 	if (model)
79 		model++;
80 
81 	strlcpy(manufacturer, compat, model ? model - compat : strlen(compat));
82 
83 	pr_info("Board \"%s\" from %s (Manufacturer)\n", model, manufacturer);
84 
85 	clk = of_get_flat_dt_prop(dt_root, "clock-frequency", &len);
86 	if (clk)
87 		arc_set_core_freq(of_read_ulong(clk, len/4));
88 
89 	return mdesc_best;
90 }
91