blob: c5118802a281a776b7a3e23f9dddcbf8e36ea75a [file] [log] [blame]
Sean MacLennanf9bdedb2008-01-22 04:55:29 +11001/*
2 * PIKA Warp(tm) board specific routines
3 *
Sean MacLennan805e3242009-04-06 11:58:25 +00004 * Copyright (c) 2008-2009 PIKA Technologies
Sean MacLennanf9bdedb2008-01-22 04:55:29 +11005 * Sean MacLennan <smaclennan@pikatech.com>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 */
12#include <linux/init.h>
13#include <linux/of_platform.h>
14#include <linux/kthread.h>
Sean MacLennan4ebef312008-05-20 08:28:52 -050015#include <linux/i2c.h>
16#include <linux/interrupt.h>
17#include <linux/delay.h>
Sean MacLennan805e3242009-04-06 11:58:25 +000018#include <linux/of_gpio.h>
Sean MacLennanf9bdedb2008-01-22 04:55:29 +110019
20#include <asm/machdep.h>
21#include <asm/prom.h>
22#include <asm/udbg.h>
23#include <asm/time.h>
24#include <asm/uic.h>
Stefan Roese93173ce2008-03-28 01:43:31 +110025#include <asm/ppc4xx.h>
Sean MacLennanf9bdedb2008-01-22 04:55:29 +110026
Sean MacLennan805e3242009-04-06 11:58:25 +000027
Sean MacLennanf9bdedb2008-01-22 04:55:29 +110028static __initdata struct of_device_id warp_of_bus[] = {
29 { .compatible = "ibm,plb4", },
30 { .compatible = "ibm,opb", },
31 { .compatible = "ibm,ebc", },
32 {},
33};
34
35static int __init warp_device_probe(void)
36{
37 of_platform_bus_probe(NULL, warp_of_bus, NULL);
38 return 0;
39}
40machine_device_initcall(warp, warp_device_probe);
41
42static int __init warp_probe(void)
43{
44 unsigned long root = of_get_flat_dt_root();
45
46 return of_flat_dt_is_compatible(root, "pika,warp");
47}
48
49define_machine(warp) {
50 .name = "Warp",
51 .probe = warp_probe,
52 .progress = udbg_progress,
53 .init_IRQ = uic_init_tree,
54 .get_irq = uic_get_irq,
Stefan Roese93173ce2008-03-28 01:43:31 +110055 .restart = ppc4xx_reset_system,
Sean MacLennanf9bdedb2008-01-22 04:55:29 +110056 .calibrate_decr = generic_calibrate_decr,
57};
58
59
Sean MacLennan805e3242009-04-06 11:58:25 +000060static u32 post_info;
61
Sean MacLennan4ebef312008-05-20 08:28:52 -050062/* I am not sure this is the best place for this... */
63static int __init warp_post_info(void)
Sean MacLennanf9bdedb2008-01-22 04:55:29 +110064{
Sean MacLennan4ebef312008-05-20 08:28:52 -050065 struct device_node *np;
66 void __iomem *fpga;
67 u32 post1, post2;
Sean MacLennanf9bdedb2008-01-22 04:55:29 +110068
Sean MacLennan4ebef312008-05-20 08:28:52 -050069 /* Sighhhh... POST information is in the sd area. */
70 np = of_find_compatible_node(NULL, NULL, "pika,fpga-sd");
71 if (np == NULL)
72 return -ENOENT;
Sean MacLennanf9bdedb2008-01-22 04:55:29 +110073
Sean MacLennan4ebef312008-05-20 08:28:52 -050074 fpga = of_iomap(np, 0);
75 of_node_put(np);
76 if (fpga == NULL)
77 return -ENOENT;
Sean MacLennanf9bdedb2008-01-22 04:55:29 +110078
Sean MacLennan4ebef312008-05-20 08:28:52 -050079 post1 = in_be32(fpga + 0x40);
80 post2 = in_be32(fpga + 0x44);
Sean MacLennanf9bdedb2008-01-22 04:55:29 +110081
Sean MacLennan4ebef312008-05-20 08:28:52 -050082 iounmap(fpga);
Sean MacLennanf9bdedb2008-01-22 04:55:29 +110083
Sean MacLennan805e3242009-04-06 11:58:25 +000084 if (post1 || post2) {
Sean MacLennan4ebef312008-05-20 08:28:52 -050085 printk(KERN_INFO "Warp POST %08x %08x\n", post1, post2);
Sean MacLennan805e3242009-04-06 11:58:25 +000086 post_info = 1;
87 } else
Sean MacLennan4ebef312008-05-20 08:28:52 -050088 printk(KERN_INFO "Warp POST OK\n");
Sean MacLennanf9bdedb2008-01-22 04:55:29 +110089
Sean MacLennan4ebef312008-05-20 08:28:52 -050090 return 0;
Sean MacLennanf9bdedb2008-01-22 04:55:29 +110091}
Sean MacLennanf9bdedb2008-01-22 04:55:29 +110092
93
94#ifdef CONFIG_SENSORS_AD7414
Sean MacLennan4ebef312008-05-20 08:28:52 -050095
96static LIST_HEAD(dtm_shutdown_list);
97static void __iomem *dtm_fpga;
Sean MacLennan805e3242009-04-06 11:58:25 +000098static unsigned green_led, red_led;
Sean MacLennan4ebef312008-05-20 08:28:52 -050099
100
101struct dtm_shutdown {
102 struct list_head list;
103 void (*func)(void *arg);
104 void *arg;
105};
106
107
108int pika_dtm_register_shutdown(void (*func)(void *arg), void *arg)
109{
110 struct dtm_shutdown *shutdown;
111
112 shutdown = kmalloc(sizeof(struct dtm_shutdown), GFP_KERNEL);
113 if (shutdown == NULL)
114 return -ENOMEM;
115
116 shutdown->func = func;
117 shutdown->arg = arg;
118
119 list_add(&shutdown->list, &dtm_shutdown_list);
120
121 return 0;
122}
123
124int pika_dtm_unregister_shutdown(void (*func)(void *arg), void *arg)
125{
126 struct dtm_shutdown *shutdown;
127
128 list_for_each_entry(shutdown, &dtm_shutdown_list, list)
129 if (shutdown->func == func && shutdown->arg == arg) {
130 list_del(&shutdown->list);
131 kfree(shutdown);
132 return 0;
133 }
134
135 return -EINVAL;
136}
137
138static irqreturn_t temp_isr(int irq, void *context)
139{
140 struct dtm_shutdown *shutdown;
Sean MacLennan805e3242009-04-06 11:58:25 +0000141 int value = 1;
Sean MacLennan4ebef312008-05-20 08:28:52 -0500142
143 local_irq_disable();
144
Sean MacLennan805e3242009-04-06 11:58:25 +0000145 gpio_set_value(green_led, 0);
146
Sean MacLennan4ebef312008-05-20 08:28:52 -0500147 /* Run through the shutdown list. */
148 list_for_each_entry(shutdown, &dtm_shutdown_list, list)
149 shutdown->func(shutdown->arg);
150
Sean MacLennan805e3242009-04-06 11:58:25 +0000151 printk(KERN_EMERG "\n\nCritical Temperature Shutdown\n\n");
Sean MacLennan4ebef312008-05-20 08:28:52 -0500152
153 while (1) {
154 if (dtm_fpga) {
155 unsigned reset = in_be32(dtm_fpga + 0x14);
156 out_be32(dtm_fpga + 0x14, reset);
157 }
158
Sean MacLennan805e3242009-04-06 11:58:25 +0000159 gpio_set_value(red_led, value);
160 value ^= 1;
Sean MacLennan4ebef312008-05-20 08:28:52 -0500161 mdelay(500);
162 }
163}
164
165static int pika_setup_leds(void)
166{
Sean MacLennan805e3242009-04-06 11:58:25 +0000167 struct device_node *np, *child;
Sean MacLennan4ebef312008-05-20 08:28:52 -0500168
Sean MacLennan805e3242009-04-06 11:58:25 +0000169 np = of_find_compatible_node(NULL, NULL, "gpio-leds");
Sean MacLennan4ebef312008-05-20 08:28:52 -0500170 if (!np) {
Sean MacLennan805e3242009-04-06 11:58:25 +0000171 printk(KERN_ERR __FILE__ ": Unable to find leds\n");
Sean MacLennan4ebef312008-05-20 08:28:52 -0500172 return -ENOENT;
173 }
174
Sean MacLennan805e3242009-04-06 11:58:25 +0000175 for_each_child_of_node(np, child)
176 if (strcmp(child->name, "green") == 0) {
177 green_led = of_get_gpio(child, 0);
178 /* Turn back on the green LED */
179 gpio_set_value(green_led, 1);
180 } else if (strcmp(child->name, "red") == 0) {
181 red_led = of_get_gpio(child, 0);
182 /* Set based on post */
183 gpio_set_value(red_led, post_info);
184 }
185
Sean MacLennan4ebef312008-05-20 08:28:52 -0500186 of_node_put(np);
Sean MacLennan4ebef312008-05-20 08:28:52 -0500187
188 return 0;
189}
190
191static void pika_setup_critical_temp(struct i2c_client *client)
192{
193 struct device_node *np;
194 int irq, rc;
195
196 /* Do this before enabling critical temp interrupt since we
197 * may immediately interrupt.
198 */
199 pika_setup_leds();
200
201 /* These registers are in 1 degree increments. */
202 i2c_smbus_write_byte_data(client, 2, 65); /* Thigh */
Sean MacLennan048040a2008-07-26 11:45:10 +1000203 i2c_smbus_write_byte_data(client, 3, 0); /* Tlow */
Sean MacLennan4ebef312008-05-20 08:28:52 -0500204
205 np = of_find_compatible_node(NULL, NULL, "adi,ad7414");
206 if (np == NULL) {
207 printk(KERN_ERR __FILE__ ": Unable to find ad7414\n");
208 return;
209 }
210
211 irq = irq_of_parse_and_map(np, 0);
212 of_node_put(np);
213 if (irq == NO_IRQ) {
214 printk(KERN_ERR __FILE__ ": Unable to get ad7414 irq\n");
215 return;
216 }
217
218 rc = request_irq(irq, temp_isr, 0, "ad7414", NULL);
219 if (rc) {
220 printk(KERN_ERR __FILE__
221 ": Unable to request ad7414 irq %d = %d\n", irq, rc);
222 return;
223 }
224}
225
226static inline void pika_dtm_check_fan(void __iomem *fpga)
227{
228 static int fan_state;
229 u32 fan = in_be32(fpga + 0x34) & (1 << 14);
230
231 if (fan_state != fan) {
232 fan_state = fan;
233 if (fan)
234 printk(KERN_WARNING "Fan rotation error detected."
235 " Please check hardware.\n");
236 }
237}
238
Sean MacLennanf9bdedb2008-01-22 04:55:29 +1100239static int pika_dtm_thread(void __iomem *fpga)
240{
Sean MacLennan4ebef312008-05-20 08:28:52 -0500241 struct i2c_adapter *adap;
242 struct i2c_client *client;
243
244 /* We loop in case either driver was compiled as a module and
245 * has not been insmoded yet.
246 */
247 while (!(adap = i2c_get_adapter(0))) {
248 set_current_state(TASK_INTERRUPTIBLE);
249 schedule_timeout(HZ);
250 }
251
252 while (1) {
253 list_for_each_entry(client, &adap->clients, list)
254 if (client->addr == 0x4a)
255 goto found_it;
256
257 set_current_state(TASK_INTERRUPTIBLE);
258 schedule_timeout(HZ);
259 }
260
261found_it:
Sean MacLennan4ebef312008-05-20 08:28:52 -0500262 pika_setup_critical_temp(client);
263
Sean MacLennan805e3242009-04-06 11:58:25 +0000264 i2c_put_adapter(adap);
265
Sean MacLennan4ebef312008-05-20 08:28:52 -0500266 printk(KERN_INFO "PIKA DTM thread running.\n");
Sean MacLennanf9bdedb2008-01-22 04:55:29 +1100267
268 while (!kthread_should_stop()) {
Sean MacLennan048040a2008-07-26 11:45:10 +1000269 int val;
270
271 val = i2c_smbus_read_word_data(client, 0);
272 if (val < 0)
273 dev_dbg(&client->dev, "DTM read temp failed.\n");
274 else {
275 s16 temp = swab16(val);
276 out_be32(fpga + 0x20, temp);
277 }
Sean MacLennanf9bdedb2008-01-22 04:55:29 +1100278
Sean MacLennan4ebef312008-05-20 08:28:52 -0500279 pika_dtm_check_fan(fpga);
Sean MacLennanf9bdedb2008-01-22 04:55:29 +1100280
281 set_current_state(TASK_INTERRUPTIBLE);
282 schedule_timeout(HZ);
283 }
284
285 return 0;
286}
287
Sean MacLennan4ebef312008-05-20 08:28:52 -0500288
Sean MacLennanf9bdedb2008-01-22 04:55:29 +1100289static int __init pika_dtm_start(void)
290{
291 struct task_struct *dtm_thread;
292 struct device_node *np;
Sean MacLennanf9bdedb2008-01-22 04:55:29 +1100293
294 np = of_find_compatible_node(NULL, NULL, "pika,fpga");
295 if (np == NULL)
296 return -ENOENT;
297
Sean MacLennan4ebef312008-05-20 08:28:52 -0500298 dtm_fpga = of_iomap(np, 0);
Sean MacLennanf9bdedb2008-01-22 04:55:29 +1100299 of_node_put(np);
Sean MacLennan4ebef312008-05-20 08:28:52 -0500300 if (dtm_fpga == NULL)
Sean MacLennanf9bdedb2008-01-22 04:55:29 +1100301 return -ENOENT;
302
Sean MacLennan805e3242009-04-06 11:58:25 +0000303 /* Must get post info before thread starts. */
304 warp_post_info();
305
Sean MacLennan4ebef312008-05-20 08:28:52 -0500306 dtm_thread = kthread_run(pika_dtm_thread, dtm_fpga, "pika-dtm");
Sean MacLennanf9bdedb2008-01-22 04:55:29 +1100307 if (IS_ERR(dtm_thread)) {
Sean MacLennan4ebef312008-05-20 08:28:52 -0500308 iounmap(dtm_fpga);
Sean MacLennanf9bdedb2008-01-22 04:55:29 +1100309 return PTR_ERR(dtm_thread);
310 }
311
312 return 0;
313}
Sean MacLennan4ebef312008-05-20 08:28:52 -0500314machine_late_initcall(warp, pika_dtm_start);
315
316#else /* !CONFIG_SENSORS_AD7414 */
317
318int pika_dtm_register_shutdown(void (*func)(void *arg), void *arg)
319{
320 return 0;
321}
322
323int pika_dtm_unregister_shutdown(void (*func)(void *arg), void *arg)
324{
325 return 0;
326}
327
Sean MacLennan805e3242009-04-06 11:58:25 +0000328machine_late_initcall(warp, warp_post_info);
329
Sean MacLennanf9bdedb2008-01-22 04:55:29 +1100330#endif
Sean MacLennan4ebef312008-05-20 08:28:52 -0500331
332EXPORT_SYMBOL(pika_dtm_register_shutdown);
333EXPORT_SYMBOL(pika_dtm_unregister_shutdown);