1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Maple (970 eval board) setup code
4  *
5  *  (c) Copyright 2004 Benjamin Herrenschmidt (benh@kernel.crashing.org),
6  *                     IBM Corp.
7  */
8 
9 #undef DEBUG
10 
11 #include <linux/init.h>
12 #include <linux/errno.h>
13 #include <linux/sched.h>
14 #include <linux/kernel.h>
15 #include <linux/export.h>
16 #include <linux/mm.h>
17 #include <linux/stddef.h>
18 #include <linux/unistd.h>
19 #include <linux/ptrace.h>
20 #include <linux/user.h>
21 #include <linux/tty.h>
22 #include <linux/string.h>
23 #include <linux/delay.h>
24 #include <linux/ioport.h>
25 #include <linux/major.h>
26 #include <linux/initrd.h>
27 #include <linux/vt_kern.h>
28 #include <linux/console.h>
29 #include <linux/pci.h>
30 #include <linux/adb.h>
31 #include <linux/cuda.h>
32 #include <linux/pmu.h>
33 #include <linux/irq.h>
34 #include <linux/seq_file.h>
35 #include <linux/root_dev.h>
36 #include <linux/serial.h>
37 #include <linux/smp.h>
38 #include <linux/bitops.h>
39 #include <linux/of_device.h>
40 #include <linux/memblock.h>
41 
42 #include <asm/processor.h>
43 #include <asm/sections.h>
44 #include <asm/prom.h>
45 #include <asm/io.h>
46 #include <asm/pci-bridge.h>
47 #include <asm/iommu.h>
48 #include <asm/machdep.h>
49 #include <asm/dma.h>
50 #include <asm/cputable.h>
51 #include <asm/time.h>
52 #include <asm/mpic.h>
53 #include <asm/rtas.h>
54 #include <asm/udbg.h>
55 #include <asm/nvram.h>
56 
57 #include "maple.h"
58 
59 #ifdef DEBUG
60 #define DBG(fmt...) udbg_printf(fmt)
61 #else
62 #define DBG(fmt...)
63 #endif
64 
maple_find_nvram_base(void)65 static unsigned long maple_find_nvram_base(void)
66 {
67 	struct device_node *rtcs;
68 	unsigned long result = 0;
69 
70 	/* find NVRAM device */
71 	rtcs = of_find_compatible_node(NULL, "nvram", "AMD8111");
72 	if (rtcs) {
73 		struct resource r;
74 		if (of_address_to_resource(rtcs, 0, &r)) {
75 			printk(KERN_EMERG "Maple: Unable to translate NVRAM"
76 			       " address\n");
77 			goto bail;
78 		}
79 		if (!(r.flags & IORESOURCE_IO)) {
80 			printk(KERN_EMERG "Maple: NVRAM address isn't PIO!\n");
81 			goto bail;
82 		}
83 		result = r.start;
84 	} else
85 		printk(KERN_EMERG "Maple: Unable to find NVRAM\n");
86  bail:
87 	of_node_put(rtcs);
88 	return result;
89 }
90 
maple_restart(char * cmd)91 static void __noreturn maple_restart(char *cmd)
92 {
93 	unsigned int maple_nvram_base;
94 	const unsigned int *maple_nvram_offset, *maple_nvram_command;
95 	struct device_node *sp;
96 
97 	maple_nvram_base = maple_find_nvram_base();
98 	if (maple_nvram_base == 0)
99 		goto fail;
100 
101 	/* find service processor device */
102 	sp = of_find_node_by_name(NULL, "service-processor");
103 	if (!sp) {
104 		printk(KERN_EMERG "Maple: Unable to find Service Processor\n");
105 		goto fail;
106 	}
107 	maple_nvram_offset = of_get_property(sp, "restart-addr", NULL);
108 	maple_nvram_command = of_get_property(sp, "restart-value", NULL);
109 	of_node_put(sp);
110 
111 	/* send command */
112 	outb_p(*maple_nvram_command, maple_nvram_base + *maple_nvram_offset);
113 	for (;;) ;
114  fail:
115 	printk(KERN_EMERG "Maple: Manual Restart Required\n");
116 	for (;;) ;
117 }
118 
maple_power_off(void)119 static void __noreturn maple_power_off(void)
120 {
121 	unsigned int maple_nvram_base;
122 	const unsigned int *maple_nvram_offset, *maple_nvram_command;
123 	struct device_node *sp;
124 
125 	maple_nvram_base = maple_find_nvram_base();
126 	if (maple_nvram_base == 0)
127 		goto fail;
128 
129 	/* find service processor device */
130 	sp = of_find_node_by_name(NULL, "service-processor");
131 	if (!sp) {
132 		printk(KERN_EMERG "Maple: Unable to find Service Processor\n");
133 		goto fail;
134 	}
135 	maple_nvram_offset = of_get_property(sp, "power-off-addr", NULL);
136 	maple_nvram_command = of_get_property(sp, "power-off-value", NULL);
137 	of_node_put(sp);
138 
139 	/* send command */
140 	outb_p(*maple_nvram_command, maple_nvram_base + *maple_nvram_offset);
141 	for (;;) ;
142  fail:
143 	printk(KERN_EMERG "Maple: Manual Power-Down Required\n");
144 	for (;;) ;
145 }
146 
maple_halt(void)147 static void __noreturn maple_halt(void)
148 {
149 	maple_power_off();
150 }
151 
152 #ifdef CONFIG_SMP
153 static struct smp_ops_t maple_smp_ops = {
154 	.probe		= smp_mpic_probe,
155 	.message_pass	= smp_mpic_message_pass,
156 	.kick_cpu	= smp_generic_kick_cpu,
157 	.setup_cpu	= smp_mpic_setup_cpu,
158 	.give_timebase	= smp_generic_give_timebase,
159 	.take_timebase	= smp_generic_take_timebase,
160 };
161 #endif /* CONFIG_SMP */
162 
maple_use_rtas_reboot_and_halt_if_present(void)163 static void __init maple_use_rtas_reboot_and_halt_if_present(void)
164 {
165 	if (rtas_service_present("system-reboot") &&
166 	    rtas_service_present("power-off")) {
167 		ppc_md.restart = rtas_restart;
168 		pm_power_off = rtas_power_off;
169 		ppc_md.halt = rtas_halt;
170 	}
171 }
172 
maple_setup_arch(void)173 static void __init maple_setup_arch(void)
174 {
175 	/* init to some ~sane value until calibrate_delay() runs */
176 	loops_per_jiffy = 50000000;
177 
178 	/* Setup SMP callback */
179 #ifdef CONFIG_SMP
180 	smp_ops = &maple_smp_ops;
181 #endif
182 	maple_use_rtas_reboot_and_halt_if_present();
183 
184 	printk(KERN_DEBUG "Using native/NAP idle loop\n");
185 
186 	mmio_nvram_init();
187 }
188 
189 /*
190  * This is almost identical to pSeries and CHRP. We need to make that
191  * code generic at one point, with appropriate bits in the device-tree to
192  * identify the presence of an HT APIC
193  */
maple_init_IRQ(void)194 static void __init maple_init_IRQ(void)
195 {
196 	struct device_node *root, *np, *mpic_node = NULL;
197 	const unsigned int *opprop;
198 	unsigned long openpic_addr = 0;
199 	int naddr, n, i, opplen, has_isus = 0;
200 	struct mpic *mpic;
201 	unsigned int flags = 0;
202 
203 	/* Locate MPIC in the device-tree. Note that there is a bug
204 	 * in Maple device-tree where the type of the controller is
205 	 * open-pic and not interrupt-controller
206 	 */
207 
208 	for_each_node_by_type(np, "interrupt-controller")
209 		if (of_device_is_compatible(np, "open-pic")) {
210 			mpic_node = np;
211 			break;
212 		}
213 	if (mpic_node == NULL)
214 		for_each_node_by_type(np, "open-pic") {
215 			mpic_node = np;
216 			break;
217 		}
218 	if (mpic_node == NULL) {
219 		printk(KERN_ERR
220 		       "Failed to locate the MPIC interrupt controller\n");
221 		return;
222 	}
223 
224 	/* Find address list in /platform-open-pic */
225 	root = of_find_node_by_path("/");
226 	naddr = of_n_addr_cells(root);
227 	opprop = of_get_property(root, "platform-open-pic", &opplen);
228 	if (opprop) {
229 		openpic_addr = of_read_number(opprop, naddr);
230 		has_isus = (opplen > naddr);
231 		printk(KERN_DEBUG "OpenPIC addr: %lx, has ISUs: %d\n",
232 		       openpic_addr, has_isus);
233 	}
234 
235 	BUG_ON(openpic_addr == 0);
236 
237 	/* Check for a big endian MPIC */
238 	if (of_get_property(np, "big-endian", NULL) != NULL)
239 		flags |= MPIC_BIG_ENDIAN;
240 
241 	/* XXX Maple specific bits */
242 	flags |= MPIC_U3_HT_IRQS;
243 	/* All U3/U4 are big-endian, older SLOF firmware doesn't encode this */
244 	flags |= MPIC_BIG_ENDIAN;
245 
246 	/* Setup the openpic driver. More device-tree junks, we hard code no
247 	 * ISUs for now. I'll have to revisit some stuffs with the folks doing
248 	 * the firmware for those
249 	 */
250 	mpic = mpic_alloc(mpic_node, openpic_addr, flags,
251 			  /*has_isus ? 16 :*/ 0, 0, " MPIC     ");
252 	BUG_ON(mpic == NULL);
253 
254 	/* Add ISUs */
255 	opplen /= sizeof(u32);
256 	for (n = 0, i = naddr; i < opplen; i += naddr, n++) {
257 		unsigned long isuaddr = of_read_number(opprop + i, naddr);
258 		mpic_assign_isu(mpic, n, isuaddr);
259 	}
260 
261 	/* All ISUs are setup, complete initialization */
262 	mpic_init(mpic);
263 	ppc_md.get_irq = mpic_get_irq;
264 	of_node_put(mpic_node);
265 	of_node_put(root);
266 }
267 
maple_progress(char * s,unsigned short hex)268 static void __init maple_progress(char *s, unsigned short hex)
269 {
270 	printk("*** %04x : %s\n", hex, s ? s : "");
271 }
272 
273 
274 /*
275  * Called very early, MMU is off, device-tree isn't unflattened
276  */
maple_probe(void)277 static int __init maple_probe(void)
278 {
279 	if (!of_machine_is_compatible("Momentum,Maple") &&
280 	    !of_machine_is_compatible("Momentum,Apache"))
281 		return 0;
282 
283 	pm_power_off = maple_power_off;
284 
285 	iommu_init_early_dart(&maple_pci_controller_ops);
286 
287 	return 1;
288 }
289 
290 #ifdef CONFIG_EDAC
291 /*
292  * Register a platform device for CPC925 memory controller on
293  * all boards with U3H (CPC925) bridge.
294  */
maple_cpc925_edac_setup(void)295 static int __init maple_cpc925_edac_setup(void)
296 {
297 	struct platform_device *pdev;
298 	struct device_node *np = NULL;
299 	struct resource r;
300 	int ret;
301 	volatile void __iomem *mem;
302 	u32 rev;
303 
304 	np = of_find_node_by_type(NULL, "memory-controller");
305 	if (!np) {
306 		printk(KERN_ERR "%s: Unable to find memory-controller node\n",
307 			__func__);
308 		return -ENODEV;
309 	}
310 
311 	ret = of_address_to_resource(np, 0, &r);
312 	of_node_put(np);
313 
314 	if (ret < 0) {
315 		printk(KERN_ERR "%s: Unable to get memory-controller reg\n",
316 			__func__);
317 		return -ENODEV;
318 	}
319 
320 	mem = ioremap(r.start, resource_size(&r));
321 	if (!mem) {
322 		printk(KERN_ERR "%s: Unable to map memory-controller memory\n",
323 				__func__);
324 		return -ENOMEM;
325 	}
326 
327 	rev = __raw_readl(mem);
328 	iounmap(mem);
329 
330 	if (rev < 0x34 || rev > 0x3f) { /* U3H */
331 		printk(KERN_ERR "%s: Non-CPC925(U3H) bridge revision: %02x\n",
332 			__func__, rev);
333 		return 0;
334 	}
335 
336 	pdev = platform_device_register_simple("cpc925_edac", 0, &r, 1);
337 	if (IS_ERR(pdev))
338 		return PTR_ERR(pdev);
339 
340 	printk(KERN_INFO "%s: CPC925 platform device created\n", __func__);
341 
342 	return 0;
343 }
344 machine_device_initcall(maple, maple_cpc925_edac_setup);
345 #endif
346 
define_machine(maple)347 define_machine(maple) {
348 	.name			= "Maple",
349 	.probe			= maple_probe,
350 	.setup_arch		= maple_setup_arch,
351 	.discover_phbs		= maple_pci_init,
352 	.init_IRQ		= maple_init_IRQ,
353 	.pci_irq_fixup		= maple_pci_irq_fixup,
354 	.pci_get_legacy_ide_irq	= maple_pci_get_legacy_ide_irq,
355 	.restart		= maple_restart,
356 	.halt			= maple_halt,
357 	.get_boot_time		= maple_get_boot_time,
358 	.set_rtc_time		= maple_set_rtc_time,
359 	.get_rtc_time		= maple_get_rtc_time,
360 	.calibrate_decr		= generic_calibrate_decr,
361 	.progress		= maple_progress,
362 	.power_save		= power4_idle,
363 };
364