xref: /linux/arch/mips/ralink/of.c (revision c4ee0af3fa0dc65f690fc908f02b8355f9576ea0)
1 /*
2  * This program is free software; you can redistribute it and/or modify it
3  * under the terms of the GNU General Public License version 2 as published
4  * by the Free Software Foundation.
5  *
6  * Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org>
7  * Copyright (C) 2008-2009 Gabor Juhos <juhosg@openwrt.org>
8  * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
9  */
10 
11 #include <linux/io.h>
12 #include <linux/clk.h>
13 #include <linux/init.h>
14 #include <linux/sizes.h>
15 #include <linux/of_fdt.h>
16 #include <linux/kernel.h>
17 #include <linux/bootmem.h>
18 #include <linux/of_platform.h>
19 #include <linux/of_address.h>
20 
21 #include <asm/reboot.h>
22 #include <asm/bootinfo.h>
23 #include <asm/addrspace.h>
24 #include <asm/prom.h>
25 
26 #include "common.h"
27 
28 __iomem void *rt_sysc_membase;
29 __iomem void *rt_memc_membase;
30 
31 extern struct boot_param_header __dtb_start;
32 
33 __iomem void *plat_of_remap_node(const char *node)
34 {
35 	struct resource res;
36 	struct device_node *np;
37 
38 	np = of_find_compatible_node(NULL, NULL, node);
39 	if (!np)
40 		panic("Failed to find %s node", node);
41 
42 	if (of_address_to_resource(np, 0, &res))
43 		panic("Failed to get resource for %s", node);
44 
45 	if ((request_mem_region(res.start,
46 				resource_size(&res),
47 				res.name) < 0))
48 		panic("Failed to request resources for %s", node);
49 
50 	return ioremap_nocache(res.start, resource_size(&res));
51 }
52 
53 void __init device_tree_init(void)
54 {
55 	unsigned long base, size;
56 	void *fdt_copy;
57 
58 	if (!initial_boot_params)
59 		return;
60 
61 	base = virt_to_phys((void *)initial_boot_params);
62 	size = be32_to_cpu(initial_boot_params->totalsize);
63 
64 	/* Before we do anything, lets reserve the dt blob */
65 	reserve_bootmem(base, size, BOOTMEM_DEFAULT);
66 
67 	/* The strings in the flattened tree are referenced directly by the
68 	 * device tree, so copy the flattened device tree from init memory
69 	 * to regular memory.
70 	 */
71 	fdt_copy = alloc_bootmem(size);
72 	memcpy(fdt_copy, initial_boot_params, size);
73 	initial_boot_params = fdt_copy;
74 
75 	unflatten_device_tree();
76 
77 	/* free the space reserved for the dt blob */
78 	free_bootmem(base, size);
79 }
80 
81 void __init plat_mem_setup(void)
82 {
83 	set_io_port_base(KSEG1);
84 
85 	/*
86 	 * Load the builtin devicetree. This causes the chosen node to be
87 	 * parsed resulting in our memory appearing
88 	 */
89 	__dt_setup_arch(&__dtb_start);
90 
91 	if (soc_info.mem_size)
92 		add_memory_region(soc_info.mem_base, soc_info.mem_size * SZ_1M,
93 				  BOOT_MEM_RAM);
94 	else
95 		detect_memory_region(soc_info.mem_base,
96 				     soc_info.mem_size_min * SZ_1M,
97 				     soc_info.mem_size_max * SZ_1M);
98 }
99 
100 static int __init plat_of_setup(void)
101 {
102 	static struct of_device_id of_ids[3];
103 	int len = sizeof(of_ids[0].compatible);
104 
105 	if (!of_have_populated_dt())
106 		panic("device tree not present");
107 
108 	strlcpy(of_ids[0].compatible, soc_info.compatible, len);
109 	strncpy(of_ids[1].compatible, "palmbus", len);
110 
111 	if (of_platform_populate(NULL, of_ids, NULL, NULL))
112 		panic("failed to populate DT");
113 
114 	/* make sure ithat the reset controller is setup early */
115 	ralink_rst_init();
116 
117 	return 0;
118 }
119 
120 arch_initcall(plat_of_setup);
121