1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * FSL SoC setup code
4  *
5  * Maintained by Kumar Gala (see MAINTAINERS for contact information)
6  *
7  * 2006 (c) MontaVista Software, Inc.
8  * Vitaly Bordug <vbordug@ru.mvista.com>
9  */
10 
11 #include <linux/stddef.h>
12 #include <linux/kernel.h>
13 #include <linux/init.h>
14 #include <linux/errno.h>
15 #include <linux/major.h>
16 #include <linux/delay.h>
17 #include <linux/irq.h>
18 #include <linux/export.h>
19 #include <linux/device.h>
20 #include <linux/platform_device.h>
21 #include <linux/of.h>
22 #include <linux/of_platform.h>
23 #include <linux/phy.h>
24 #include <linux/spi/spi.h>
25 #include <linux/fsl_devices.h>
26 #include <linux/fs_enet_pd.h>
27 #include <linux/fs_uart_pd.h>
28 #include <linux/reboot.h>
29 
30 #include <linux/atomic.h>
31 #include <asm/io.h>
32 #include <asm/irq.h>
33 #include <asm/time.h>
34 #include <asm/prom.h>
35 #include <asm/machdep.h>
36 #include <sysdev/fsl_soc.h>
37 #include <mm/mmu_decl.h>
38 #include <asm/cpm2.h>
39 #include <asm/fsl_hcalls.h>	/* For the Freescale hypervisor */
40 
41 extern void init_fcc_ioports(struct fs_platform_info*);
42 extern void init_fec_ioports(struct fs_platform_info*);
43 extern void init_smc_ioports(struct fs_uart_platform_info*);
44 static phys_addr_t immrbase = -1;
45 
get_immrbase(void)46 phys_addr_t get_immrbase(void)
47 {
48 	struct device_node *soc;
49 
50 	if (immrbase != -1)
51 		return immrbase;
52 
53 	soc = of_find_node_by_type(NULL, "soc");
54 	if (soc) {
55 		int size;
56 		u32 naddr;
57 		const __be32 *prop = of_get_property(soc, "#address-cells", &size);
58 
59 		if (prop && size == 4)
60 			naddr = be32_to_cpup(prop);
61 		else
62 			naddr = 2;
63 
64 		prop = of_get_property(soc, "ranges", &size);
65 		if (prop)
66 			immrbase = of_translate_address(soc, prop + naddr);
67 
68 		of_node_put(soc);
69 	}
70 
71 	return immrbase;
72 }
73 
74 EXPORT_SYMBOL(get_immrbase);
75 
fsl_get_sys_freq(void)76 u32 fsl_get_sys_freq(void)
77 {
78 	static u32 sysfreq = -1;
79 	struct device_node *soc;
80 
81 	if (sysfreq != -1)
82 		return sysfreq;
83 
84 	soc = of_find_node_by_type(NULL, "soc");
85 	if (!soc)
86 		return -1;
87 
88 	of_property_read_u32(soc, "clock-frequency", &sysfreq);
89 	if (sysfreq == -1 || !sysfreq)
90 		of_property_read_u32(soc, "bus-frequency", &sysfreq);
91 
92 	of_node_put(soc);
93 	return sysfreq;
94 }
95 EXPORT_SYMBOL(fsl_get_sys_freq);
96 
97 #if defined(CONFIG_CPM) || defined(CONFIG_QUICC_ENGINE)
98 
get_brgfreq(void)99 u32 get_brgfreq(void)
100 {
101 	static u32 brgfreq = -1;
102 	struct device_node *node;
103 
104 	if (brgfreq != -1)
105 		return brgfreq;
106 
107 	node = of_find_compatible_node(NULL, NULL, "fsl,cpm-brg");
108 	if (node) {
109 		of_property_read_u32(node, "clock-frequency", &brgfreq);
110 		of_node_put(node);
111 		return brgfreq;
112 	}
113 
114 	/* Legacy device binding -- will go away when no users are left. */
115 	node = of_find_node_by_type(NULL, "cpm");
116 	if (!node)
117 		node = of_find_compatible_node(NULL, NULL, "fsl,qe");
118 	if (!node)
119 		node = of_find_node_by_type(NULL, "qe");
120 
121 	if (node) {
122 		of_property_read_u32(node, "brg-frequency", &brgfreq);
123 		if (brgfreq == -1 || !brgfreq)
124 			if (!of_property_read_u32(node, "bus-frequency",
125 						  &brgfreq))
126 				brgfreq /= 2;
127 		of_node_put(node);
128 	}
129 
130 	return brgfreq;
131 }
132 
133 EXPORT_SYMBOL(get_brgfreq);
134 
get_baudrate(void)135 u32 get_baudrate(void)
136 {
137 	static u32 fs_baudrate = -1;
138 	struct device_node *node;
139 
140 	if (fs_baudrate != -1)
141 		return fs_baudrate;
142 
143 	node = of_find_node_by_type(NULL, "serial");
144 	if (node) {
145 		of_property_read_u32(node, "current-speed", &fs_baudrate);
146 		of_node_put(node);
147 	}
148 
149 	return fs_baudrate;
150 }
151 
152 EXPORT_SYMBOL(get_baudrate);
153 #endif /* CONFIG_CPM2 */
154 
155 #if defined(CONFIG_FSL_SOC_BOOKE) || defined(CONFIG_PPC_86xx)
156 static __be32 __iomem *rstcr;
157 
fsl_rstcr_restart(struct notifier_block * this,unsigned long mode,void * cmd)158 static int fsl_rstcr_restart(struct notifier_block *this,
159 			     unsigned long mode, void *cmd)
160 {
161 	local_irq_disable();
162 	/* set reset control register */
163 	out_be32(rstcr, 0x2);	/* HRESET_REQ */
164 
165 	return NOTIFY_DONE;
166 }
167 
setup_rstcr(void)168 static int __init setup_rstcr(void)
169 {
170 	struct device_node *np;
171 
172 	static struct notifier_block restart_handler = {
173 		.notifier_call = fsl_rstcr_restart,
174 		.priority = 128,
175 	};
176 
177 	for_each_node_by_name(np, "global-utilities") {
178 		if ((of_get_property(np, "fsl,has-rstcr", NULL))) {
179 			rstcr = of_iomap(np, 0) + 0xb0;
180 			if (!rstcr) {
181 				printk (KERN_ERR "Error: reset control "
182 						"register not mapped!\n");
183 			} else {
184 				register_restart_handler(&restart_handler);
185 			}
186 			break;
187 		}
188 	}
189 
190 	of_node_put(np);
191 
192 	return 0;
193 }
194 
195 arch_initcall(setup_rstcr);
196 
197 #endif
198 
199 #if defined(CONFIG_FB_FSL_DIU) || defined(CONFIG_FB_FSL_DIU_MODULE)
200 struct platform_diu_data_ops diu_ops;
201 EXPORT_SYMBOL(diu_ops);
202 #endif
203 
204 #ifdef CONFIG_EPAPR_PARAVIRT
205 /*
206  * Restart the current partition
207  *
208  * This function should be assigned to the ppc_md.restart function pointer,
209  * to initiate a partition restart when we're running under the Freescale
210  * hypervisor.
211  */
fsl_hv_restart(char * cmd)212 void __noreturn fsl_hv_restart(char *cmd)
213 {
214 	pr_info("hv restart\n");
215 	fh_partition_restart(-1);
216 	while (1) ;
217 }
218 
219 /*
220  * Halt the current partition
221  *
222  * This function should be assigned to the pm_power_off and ppc_md.halt
223  * function pointers, to shut down the partition when we're running under
224  * the Freescale hypervisor.
225  */
fsl_hv_halt(void)226 void __noreturn fsl_hv_halt(void)
227 {
228 	pr_info("hv exit\n");
229 	fh_partition_stop(-1);
230 	while (1) ;
231 }
232 #endif
233