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