1 /*
2  * PowerPC 476FPE board specific routines
3  *
4  * Copyright © 2013 Tony Breeds IBM Corporation
5  * Copyright © 2013 Alistair Popple IBM Corporation
6  *
7  * Based on earlier code:
8  *    Matt Porter <mporter@kernel.crashing.org>
9  *    Copyright 2002-2005 MontaVista Software Inc.
10  *
11  *    Eugene Surovegin <eugene.surovegin@zultys.com> or <ebs@ebshome.net>
12  *    Copyright (c) 2003-2005 Zultys Technologies
13  *
14  *    Rewritten and ported to the merged powerpc tree:
15  *    Copyright 2007 David Gibson <dwg@au1.ibm.com>, IBM Corporation.
16  *    Copyright © 2011 David Kliekamp IBM Corporation
17  *
18  * This program is free software; you can redistribute  it and/or modify it
19  * under  the terms of  the GNU General  Public License as published by the
20  * Free Software Foundation;  either version 2 of the  License, or (at your
21  * option) any later version.
22  */
23 
24 #include <linux/init.h>
25 #include <linux/of.h>
26 #include <linux/of_platform.h>
27 #include <linux/rtc.h>
28 
29 #include <asm/machdep.h>
30 #include <asm/prom.h>
31 #include <asm/udbg.h>
32 #include <asm/time.h>
33 #include <asm/uic.h>
34 #include <asm/ppc4xx.h>
35 #include <asm/mpic.h>
36 #include <asm/mmu.h>
37 #include <asm/swiotlb.h>
38 
39 #include <linux/pci.h>
40 #include <linux/i2c.h>
41 
42 static const struct of_device_id ppc47x_of_bus[] __initconst = {
43 	{ .compatible = "ibm,plb4", },
44 	{ .compatible = "ibm,plb6", },
45 	{ .compatible = "ibm,opb", },
46 	{ .compatible = "ibm,ebc", },
47 	{},
48 };
49 
50 /* The EEPROM is missing and the default values are bogus.  This forces USB in
51  * to EHCI mode */
52 static void quirk_ppc_currituck_usb_fixup(struct pci_dev *dev)
53 {
54 	if (of_machine_is_compatible("ibm,currituck")) {
55 		pci_write_config_dword(dev, 0xe0, 0x0114231f);
56 		pci_write_config_dword(dev, 0xe4, 0x00006c40);
57 	}
58 }
59 DECLARE_PCI_FIXUP_HEADER(0x1033, 0x0035, quirk_ppc_currituck_usb_fixup);
60 
61 /* Akebono has an AVR microcontroller attached to the I2C bus
62  * which is used to power off/reset the system. */
63 
64 /* AVR I2C Commands */
65 #define AVR_PWRCTL_CMD (0x26)
66 
67 /* Flags for the power control I2C commands */
68 #define AVR_PWRCTL_PWROFF (0x01)
69 #define AVR_PWRCTL_RESET (0x02)
70 
71 static struct i2c_client *avr_i2c_client;
72 static void __noreturn avr_halt_system(int pwrctl_flags)
73 {
74 	/* Request the AVR to reset the system */
75 	i2c_smbus_write_byte_data(avr_i2c_client,
76 				  AVR_PWRCTL_CMD, pwrctl_flags);
77 
78 	/* Wait for system to be reset */
79 	while (1)
80 		;
81 }
82 
83 static void avr_power_off_system(void)
84 {
85 	avr_halt_system(AVR_PWRCTL_PWROFF);
86 }
87 
88 static void __noreturn avr_reset_system(char *cmd)
89 {
90 	avr_halt_system(AVR_PWRCTL_RESET);
91 }
92 
93 static int avr_probe(struct i2c_client *client,
94 			    const struct i2c_device_id *id)
95 {
96 	avr_i2c_client = client;
97 	ppc_md.restart = avr_reset_system;
98 	pm_power_off = avr_power_off_system;
99 	return 0;
100 }
101 
102 static const struct i2c_device_id avr_id[] = {
103 	{ "akebono-avr", 0 },
104 	{ }
105 };
106 
107 static struct i2c_driver avr_driver = {
108 	.driver = {
109 		.name = "akebono-avr",
110 	},
111 	.probe = avr_probe,
112 	.id_table = avr_id,
113 };
114 
115 static int __init ppc47x_device_probe(void)
116 {
117 	i2c_add_driver(&avr_driver);
118 	of_platform_bus_probe(NULL, ppc47x_of_bus, NULL);
119 
120 	return 0;
121 }
122 machine_device_initcall(ppc47x, ppc47x_device_probe);
123 
124 static void __init ppc47x_init_irq(void)
125 {
126 	struct device_node *np;
127 
128 	/* Find top level interrupt controller */
129 	for_each_node_with_property(np, "interrupt-controller") {
130 		if (of_get_property(np, "interrupts", NULL) == NULL)
131 			break;
132 	}
133 	if (np == NULL)
134 		panic("Can't find top level interrupt controller");
135 
136 	/* Check type and do appropriate initialization */
137 	if (of_device_is_compatible(np, "chrp,open-pic")) {
138 		/* The MPIC driver will get everything it needs from the
139 		 * device-tree, just pass 0 to all arguments
140 		 */
141 		struct mpic *mpic =
142 			mpic_alloc(np, 0, MPIC_NO_RESET, 0, 0, " MPIC     ");
143 		BUG_ON(mpic == NULL);
144 		mpic_init(mpic);
145 		ppc_md.get_irq = mpic_get_irq;
146 	} else
147 		panic("Unrecognized top level interrupt controller");
148 }
149 
150 #ifdef CONFIG_SMP
151 static void smp_ppc47x_setup_cpu(int cpu)
152 {
153 	mpic_setup_this_cpu();
154 }
155 
156 static int smp_ppc47x_kick_cpu(int cpu)
157 {
158 	struct device_node *cpunode = of_get_cpu_node(cpu, NULL);
159 	const u64 *spin_table_addr_prop;
160 	u32 *spin_table;
161 	extern void start_secondary_47x(void);
162 
163 	BUG_ON(cpunode == NULL);
164 
165 	/* Assume spin table. We could test for the enable-method in
166 	 * the device-tree but currently there's little point as it's
167 	 * our only supported method
168 	 */
169 	spin_table_addr_prop =
170 		of_get_property(cpunode, "cpu-release-addr", NULL);
171 
172 	if (spin_table_addr_prop == NULL) {
173 		pr_err("CPU%d: Can't start, missing cpu-release-addr !\n",
174 		       cpu);
175 		return 1;
176 	}
177 
178 	/* Assume it's mapped as part of the linear mapping. This is a bit
179 	 * fishy but will work fine for now
180 	 *
181 	 * XXX: Is there any reason to assume differently?
182 	 */
183 	spin_table = (u32 *)__va(*spin_table_addr_prop);
184 	pr_debug("CPU%d: Spin table mapped at %p\n", cpu, spin_table);
185 
186 	spin_table[3] = cpu;
187 	smp_wmb();
188 	spin_table[1] = __pa(start_secondary_47x);
189 	mb();
190 
191 	return 0;
192 }
193 
194 static struct smp_ops_t ppc47x_smp_ops = {
195 	.probe		= smp_mpic_probe,
196 	.message_pass	= smp_mpic_message_pass,
197 	.setup_cpu	= smp_ppc47x_setup_cpu,
198 	.kick_cpu	= smp_ppc47x_kick_cpu,
199 	.give_timebase	= smp_generic_give_timebase,
200 	.take_timebase	= smp_generic_take_timebase,
201 };
202 
203 static void __init ppc47x_smp_init(void)
204 {
205 	if (mmu_has_feature(MMU_FTR_TYPE_47x))
206 		smp_ops = &ppc47x_smp_ops;
207 }
208 
209 #else /* CONFIG_SMP */
210 static void __init ppc47x_smp_init(void) { }
211 #endif /* CONFIG_SMP */
212 
213 static void __init ppc47x_setup_arch(void)
214 {
215 
216 	/* No need to check the DMA config as we /know/ our windows are all of
217 	 * RAM.  Lets hope that doesn't change */
218 	swiotlb_detect_4g();
219 
220 	ppc47x_smp_init();
221 }
222 
223 static int board_rev = -1;
224 static int __init ppc47x_get_board_rev(void)
225 {
226 	int reg;
227 	u8 *fpga;
228 	struct device_node *np = NULL;
229 
230 	if (of_machine_is_compatible("ibm,currituck")) {
231 		np = of_find_compatible_node(NULL, NULL, "ibm,currituck-fpga");
232 		reg = 0;
233 	} else if (of_machine_is_compatible("ibm,akebono")) {
234 		np = of_find_compatible_node(NULL, NULL, "ibm,akebono-fpga");
235 		reg = 2;
236 	}
237 
238 	if (!np)
239 		goto fail;
240 
241 	fpga = (u8 *) of_iomap(np, 0);
242 	of_node_put(np);
243 	if (!fpga)
244 		goto fail;
245 
246 	board_rev = ioread8(fpga + reg) & 0x03;
247 	pr_info("%s: Found board revision %d\n", __func__, board_rev);
248 	iounmap(fpga);
249 	return 0;
250 
251 fail:
252 	pr_info("%s: Unable to find board revision\n", __func__);
253 	return 0;
254 }
255 machine_arch_initcall(ppc47x, ppc47x_get_board_rev);
256 
257 /* Use USB controller should have been hardware swizzled but it wasn't :( */
258 static void ppc47x_pci_irq_fixup(struct pci_dev *dev)
259 {
260 	if (dev->vendor == 0x1033 && (dev->device == 0x0035 ||
261 				      dev->device == 0x00e0)) {
262 		if (board_rev == 0) {
263 			dev->irq = irq_create_mapping(NULL, 47);
264 			pr_info("%s: Mapping irq %d\n", __func__, dev->irq);
265 		} else if (board_rev == 2) {
266 			dev->irq = irq_create_mapping(NULL, 49);
267 			pr_info("%s: Mapping irq %d\n", __func__, dev->irq);
268 		} else {
269 			pr_alert("%s: Unknown board revision\n", __func__);
270 		}
271 	}
272 }
273 
274 /*
275  * Called very early, MMU is off, device-tree isn't unflattened
276  */
277 static int __init ppc47x_probe(void)
278 {
279 	if (of_machine_is_compatible("ibm,akebono"))
280 		return 1;
281 
282 	if (of_machine_is_compatible("ibm,currituck")) {
283 		ppc_md.pci_irq_fixup = ppc47x_pci_irq_fixup;
284 		return 1;
285 	}
286 
287 	return 0;
288 }
289 
290 define_machine(ppc47x) {
291 	.name			= "PowerPC 47x",
292 	.probe			= ppc47x_probe,
293 	.progress		= udbg_progress,
294 	.init_IRQ		= ppc47x_init_irq,
295 	.setup_arch		= ppc47x_setup_arch,
296 	.restart		= ppc4xx_reset_system,
297 	.calibrate_decr		= generic_calibrate_decr,
298 };
299