xref: /openbmc/linux/arch/arc/kernel/devtree.c (revision f35e839a)
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 /* called from unflatten_device_tree() to bootstrap devicetree itself */
22 void * __init early_init_dt_alloc_memory_arch(u64 size, u64 align)
23 {
24 	return __va(memblock_alloc(size, align));
25 }
26 
27 /**
28  * setup_machine_fdt - Machine setup when an dtb was passed to the kernel
29  * @dt:		virtual address pointer to dt blob
30  *
31  * If a dtb was passed to the kernel, then use it to choose the correct
32  * machine_desc and to setup the system.
33  */
34 struct machine_desc * __init setup_machine_fdt(void *dt)
35 {
36 	struct boot_param_header *devtree = dt;
37 	struct machine_desc *mdesc = NULL, *mdesc_best = NULL;
38 	unsigned int score, mdesc_score = ~1;
39 	unsigned long dt_root;
40 	const char *model, *compat;
41 	void *clk;
42 	char manufacturer[16];
43 	unsigned long len;
44 
45 	/* check device tree validity */
46 	if (be32_to_cpu(devtree->magic) != OF_DT_HEADER)
47 		return NULL;
48 
49 	initial_boot_params = devtree;
50 	dt_root = of_get_flat_dt_root();
51 
52 	/*
53 	 * The kernel could be multi-platform enabled, thus could have many
54 	 * "baked-in" machine descriptors. Search thru all for the best
55 	 * "compatible" string match.
56 	 */
57 	for_each_machine_desc(mdesc) {
58 		score = of_flat_dt_match(dt_root, mdesc->dt_compat);
59 		if (score > 0 && score < mdesc_score) {
60 			mdesc_best = mdesc;
61 			mdesc_score = score;
62 		}
63 	}
64 	if (!mdesc_best) {
65 		const char *prop;
66 		long size;
67 
68 		pr_err("\n unrecognized device tree list:\n[ ");
69 
70 		prop = of_get_flat_dt_prop(dt_root, "compatible", &size);
71 		if (prop) {
72 			while (size > 0) {
73 				printk("'%s' ", prop);
74 				size -= strlen(prop) + 1;
75 				prop += strlen(prop) + 1;
76 			}
77 		}
78 		printk("]\n\n");
79 
80 		machine_halt();
81 	}
82 
83 	/* compat = "<manufacturer>,<model>" */
84 	compat =  mdesc_best->dt_compat[0];
85 
86 	model = strchr(compat, ',');
87 	if (model)
88 		model++;
89 
90 	strlcpy(manufacturer, compat, model ? model - compat : strlen(compat));
91 
92 	pr_info("Board \"%s\" from %s (Manufacturer)\n", model, manufacturer);
93 
94 	/* Retrieve various information from the /chosen node */
95 	of_scan_flat_dt(early_init_dt_scan_chosen, boot_command_line);
96 
97 	/* Initialize {size,address}-cells info */
98 	of_scan_flat_dt(early_init_dt_scan_root, NULL);
99 
100 	/* Setup memory, calling early_init_dt_add_memory_arch */
101 	of_scan_flat_dt(early_init_dt_scan_memory, NULL);
102 
103 	clk = of_get_flat_dt_prop(dt_root, "clock-frequency", &len);
104 	if (clk)
105 		arc_set_core_freq(of_read_ulong(clk, len/4));
106 
107 	return mdesc_best;
108 }
109 
110 /*
111  * Copy the flattened DT out of .init since unflattening doesn't copy strings
112  * and the normal DT APIs refs them from orig flat DT
113  */
114 void __init copy_devtree(void)
115 {
116 	void *alloc = early_init_dt_alloc_memory_arch(
117 			be32_to_cpu(initial_boot_params->totalsize), 64);
118 	if (alloc) {
119 		memcpy(alloc, initial_boot_params,
120 				be32_to_cpu(initial_boot_params->totalsize));
121 		initial_boot_params = alloc;
122 	}
123 }
124