xref: /openbmc/linux/arch/powerpc/platforms/44x/warp.c (revision f4356947)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * PIKA Warp(tm) board specific routines
4  *
5  * Copyright (c) 2008-2009 PIKA Technologies
6  *   Sean MacLennan <smaclennan@pikatech.com>
7  */
8 #include <linux/err.h>
9 #include <linux/init.h>
10 #include <linux/of_platform.h>
11 #include <linux/kthread.h>
12 #include <linux/leds.h>
13 #include <linux/i2c.h>
14 #include <linux/interrupt.h>
15 #include <linux/delay.h>
16 #include <linux/of_address.h>
17 #include <linux/of_irq.h>
18 #include <linux/gpio/consumer.h>
19 #include <linux/slab.h>
20 #include <linux/export.h>
21 
22 #include <asm/machdep.h>
23 #include <asm/udbg.h>
24 #include <asm/time.h>
25 #include <asm/uic.h>
26 #include <asm/ppc4xx.h>
27 #include <asm/dma.h>
28 
29 
30 static const struct of_device_id warp_of_bus[] __initconst = {
31 	{ .compatible = "ibm,plb4", },
32 	{ .compatible = "ibm,opb", },
33 	{ .compatible = "ibm,ebc", },
34 	{},
35 };
36 
37 static int __init warp_device_probe(void)
38 {
39 	of_platform_bus_probe(NULL, warp_of_bus, NULL);
40 	return 0;
41 }
42 machine_device_initcall(warp, warp_device_probe);
43 
44 define_machine(warp) {
45 	.name		= "Warp",
46 	.compatible	= "pika,warp",
47 	.progress 	= udbg_progress,
48 	.init_IRQ 	= uic_init_tree,
49 	.get_irq 	= uic_get_irq,
50 	.restart	= ppc4xx_reset_system,
51 };
52 
53 
54 static int __init warp_post_info(void)
55 {
56 	struct device_node *np;
57 	void __iomem *fpga;
58 	u32 post1, post2;
59 
60 	/* Sighhhh... POST information is in the sd area. */
61 	np = of_find_compatible_node(NULL, NULL, "pika,fpga-sd");
62 	if (np == NULL)
63 		return -ENOENT;
64 
65 	fpga = of_iomap(np, 0);
66 	of_node_put(np);
67 	if (fpga == NULL)
68 		return -ENOENT;
69 
70 	post1 = in_be32(fpga + 0x40);
71 	post2 = in_be32(fpga + 0x44);
72 
73 	iounmap(fpga);
74 
75 	if (post1 || post2)
76 		printk(KERN_INFO "Warp POST %08x %08x\n", post1, post2);
77 	else
78 		printk(KERN_INFO "Warp POST OK\n");
79 
80 	return 0;
81 }
82 
83 
84 #ifdef CONFIG_SENSORS_AD7414
85 
86 static LIST_HEAD(dtm_shutdown_list);
87 static void __iomem *dtm_fpga;
88 
89 struct dtm_shutdown {
90 	struct list_head list;
91 	void (*func)(void *arg);
92 	void *arg;
93 };
94 
95 int pika_dtm_register_shutdown(void (*func)(void *arg), void *arg)
96 {
97 	struct dtm_shutdown *shutdown;
98 
99 	shutdown = kmalloc(sizeof(struct dtm_shutdown), GFP_KERNEL);
100 	if (shutdown == NULL)
101 		return -ENOMEM;
102 
103 	shutdown->func = func;
104 	shutdown->arg = arg;
105 
106 	list_add(&shutdown->list, &dtm_shutdown_list);
107 
108 	return 0;
109 }
110 
111 int pika_dtm_unregister_shutdown(void (*func)(void *arg), void *arg)
112 {
113 	struct dtm_shutdown *shutdown;
114 
115 	list_for_each_entry(shutdown, &dtm_shutdown_list, list)
116 		if (shutdown->func == func && shutdown->arg == arg) {
117 			list_del(&shutdown->list);
118 			kfree(shutdown);
119 			return 0;
120 		}
121 
122 	return -EINVAL;
123 }
124 
125 #define WARP_GREEN_LED	0
126 #define WARP_RED_LED	1
127 
128 static struct gpio_led warp_gpio_led_pins[] = {
129 	[WARP_GREEN_LED] = {
130 		.name		= "green",
131 		.default_state	= LEDS_DEFSTATE_KEEP,
132 		.gpiod		= NULL, /* to be filled by pika_setup_leds() */
133 	},
134 	[WARP_RED_LED] = {
135 		.name		= "red",
136 		.default_state	= LEDS_DEFSTATE_KEEP,
137 		.gpiod		= NULL, /* to be filled by pika_setup_leds() */
138 	},
139 };
140 
141 static struct gpio_led_platform_data warp_gpio_led_data = {
142 	.leds		= warp_gpio_led_pins,
143 	.num_leds	= ARRAY_SIZE(warp_gpio_led_pins),
144 };
145 
146 static struct platform_device warp_gpio_leds = {
147 	.name	= "leds-gpio",
148 	.id	= -1,
149 	.dev	= {
150 		.platform_data = &warp_gpio_led_data,
151 	},
152 };
153 
154 static irqreturn_t temp_isr(int irq, void *context)
155 {
156 	struct dtm_shutdown *shutdown;
157 	int value = 1;
158 
159 	local_irq_disable();
160 
161 	gpiod_set_value(warp_gpio_led_pins[WARP_GREEN_LED].gpiod, 0);
162 
163 	/* Run through the shutdown list. */
164 	list_for_each_entry(shutdown, &dtm_shutdown_list, list)
165 		shutdown->func(shutdown->arg);
166 
167 	printk(KERN_EMERG "\n\nCritical Temperature Shutdown\n\n");
168 
169 	while (1) {
170 		if (dtm_fpga) {
171 			unsigned reset = in_be32(dtm_fpga + 0x14);
172 			out_be32(dtm_fpga + 0x14, reset);
173 		}
174 
175 		gpiod_set_value(warp_gpio_led_pins[WARP_RED_LED].gpiod, value);
176 		value ^= 1;
177 		mdelay(500);
178 	}
179 
180 	/* Not reached */
181 	return IRQ_HANDLED;
182 }
183 
184 /*
185  * Because green and red power LEDs are normally driven by leds-gpio driver,
186  * but in case of critical temperature shutdown we want to drive them
187  * ourselves, we acquire both and then create leds-gpio platform device
188  * ourselves, instead of doing it through device tree. This way we can still
189  * keep access to the gpios and use them when needed.
190  */
191 static int pika_setup_leds(void)
192 {
193 	struct device_node *np, *child;
194 	struct gpio_desc *gpio;
195 	struct gpio_led *led;
196 	int led_count = 0;
197 	int error;
198 	int i;
199 
200 	np = of_find_compatible_node(NULL, NULL, "warp-power-leds");
201 	if (!np) {
202 		printk(KERN_ERR __FILE__ ": Unable to find leds\n");
203 		return -ENOENT;
204 	}
205 
206 	for_each_child_of_node(np, child) {
207 		for (i = 0; i < ARRAY_SIZE(warp_gpio_led_pins); i++) {
208 			led = &warp_gpio_led_pins[i];
209 
210 			if (!of_node_name_eq(child, led->name))
211 				continue;
212 
213 			if (led->gpiod) {
214 				printk(KERN_ERR __FILE__ ": %s led has already been defined\n",
215 				       led->name);
216 				continue;
217 			}
218 
219 			gpio = fwnode_gpiod_get_index(of_fwnode_handle(child),
220 						      NULL, 0, GPIOD_ASIS,
221 						      led->name);
222 			error = PTR_ERR_OR_ZERO(gpio);
223 			if (error) {
224 				printk(KERN_ERR __FILE__ ": Failed to get %s led gpio: %d\n",
225 				       led->name, error);
226 				of_node_put(child);
227 				goto err_cleanup_pins;
228 			}
229 
230 			led->gpiod = gpio;
231 			led_count++;
232 		}
233 	}
234 
235 	of_node_put(np);
236 
237 	/* Skip device registration if no leds have been defined */
238 	if (led_count) {
239 		error = platform_device_register(&warp_gpio_leds);
240 		if (error) {
241 			printk(KERN_ERR __FILE__ ": Unable to add leds-gpio: %d\n",
242 			       error);
243 			goto err_cleanup_pins;
244 		}
245 	}
246 
247 	return 0;
248 
249 err_cleanup_pins:
250 	for (i = 0; i < ARRAY_SIZE(warp_gpio_led_pins); i++) {
251 		led = &warp_gpio_led_pins[i];
252 		gpiod_put(led->gpiod);
253 		led->gpiod = NULL;
254 	}
255 	return error;
256 }
257 
258 static void pika_setup_critical_temp(struct device_node *np,
259 				     struct i2c_client *client)
260 {
261 	int irq, rc;
262 
263 	/* Do this before enabling critical temp interrupt since we
264 	 * may immediately interrupt.
265 	 */
266 	pika_setup_leds();
267 
268 	/* These registers are in 1 degree increments. */
269 	i2c_smbus_write_byte_data(client, 2, 65); /* Thigh */
270 	i2c_smbus_write_byte_data(client, 3,  0); /* Tlow */
271 
272 	irq = irq_of_parse_and_map(np, 0);
273 	if (!irq) {
274 		printk(KERN_ERR __FILE__ ": Unable to get ad7414 irq\n");
275 		return;
276 	}
277 
278 	rc = request_irq(irq, temp_isr, 0, "ad7414", NULL);
279 	if (rc) {
280 		printk(KERN_ERR __FILE__
281 		       ": Unable to request ad7414 irq %d = %d\n", irq, rc);
282 		return;
283 	}
284 }
285 
286 static inline void pika_dtm_check_fan(void __iomem *fpga)
287 {
288 	static int fan_state;
289 	u32 fan = in_be32(fpga + 0x34) & (1 << 14);
290 
291 	if (fan_state != fan) {
292 		fan_state = fan;
293 		if (fan)
294 			printk(KERN_WARNING "Fan rotation error detected."
295 				   " Please check hardware.\n");
296 	}
297 }
298 
299 static int pika_dtm_thread(void __iomem *fpga)
300 {
301 	struct device_node *np;
302 	struct i2c_client *client;
303 
304 	np = of_find_compatible_node(NULL, NULL, "adi,ad7414");
305 	if (np == NULL)
306 		return -ENOENT;
307 
308 	client = of_find_i2c_device_by_node(np);
309 	if (client == NULL) {
310 		of_node_put(np);
311 		return -ENOENT;
312 	}
313 
314 	pika_setup_critical_temp(np, client);
315 
316 	of_node_put(np);
317 
318 	printk(KERN_INFO "Warp DTM thread running.\n");
319 
320 	while (!kthread_should_stop()) {
321 		int val;
322 
323 		val = i2c_smbus_read_word_data(client, 0);
324 		if (val < 0)
325 			dev_dbg(&client->dev, "DTM read temp failed.\n");
326 		else {
327 			s16 temp = swab16(val);
328 			out_be32(fpga + 0x20, temp);
329 		}
330 
331 		pika_dtm_check_fan(fpga);
332 
333 		set_current_state(TASK_INTERRUPTIBLE);
334 		schedule_timeout(HZ);
335 	}
336 
337 	return 0;
338 }
339 
340 static int __init pika_dtm_start(void)
341 {
342 	struct task_struct *dtm_thread;
343 	struct device_node *np;
344 
345 	np = of_find_compatible_node(NULL, NULL, "pika,fpga");
346 	if (np == NULL)
347 		return -ENOENT;
348 
349 	dtm_fpga = of_iomap(np, 0);
350 	of_node_put(np);
351 	if (dtm_fpga == NULL)
352 		return -ENOENT;
353 
354 	/* Must get post info before thread starts. */
355 	warp_post_info();
356 
357 	dtm_thread = kthread_run(pika_dtm_thread, dtm_fpga, "pika-dtm");
358 	if (IS_ERR(dtm_thread)) {
359 		iounmap(dtm_fpga);
360 		return PTR_ERR(dtm_thread);
361 	}
362 
363 	return 0;
364 }
365 machine_late_initcall(warp, pika_dtm_start);
366 
367 #else /* !CONFIG_SENSORS_AD7414 */
368 
369 int pika_dtm_register_shutdown(void (*func)(void *arg), void *arg)
370 {
371 	return 0;
372 }
373 
374 int pika_dtm_unregister_shutdown(void (*func)(void *arg), void *arg)
375 {
376 	return 0;
377 }
378 
379 machine_late_initcall(warp, warp_post_info);
380 
381 #endif
382 
383 EXPORT_SYMBOL(pika_dtm_register_shutdown);
384 EXPORT_SYMBOL(pika_dtm_unregister_shutdown);
385