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