[CPUFREQ] Powernow-k8: support family 0xf with 2 low p-states
[pandora-kernel.git] / arch / powerpc / platforms / 44x / warp.c
1 /*
2  * PIKA Warp(tm) board specific routines
3  *
4  * Copyright (c) 2008-2009 PIKA Technologies
5  *   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>
15 #include <linux/i2c.h>
16 #include <linux/interrupt.h>
17 #include <linux/delay.h>
18 #include <linux/of_gpio.h>
19 #include <linux/of_i2c.h>
20
21 #include <asm/machdep.h>
22 #include <asm/prom.h>
23 #include <asm/udbg.h>
24 #include <asm/time.h>
25 #include <asm/uic.h>
26 #include <asm/ppc4xx.h>
27
28
29 static __initdata struct of_device_id warp_of_bus[] = {
30         { .compatible = "ibm,plb4", },
31         { .compatible = "ibm,opb", },
32         { .compatible = "ibm,ebc", },
33         {},
34 };
35
36 static int __init warp_device_probe(void)
37 {
38         of_platform_bus_probe(NULL, warp_of_bus, NULL);
39         return 0;
40 }
41 machine_device_initcall(warp, warp_device_probe);
42
43 static int __init warp_probe(void)
44 {
45         unsigned long root = of_get_flat_dt_root();
46
47         if (!of_flat_dt_is_compatible(root, "pika,warp"))
48                 return 0;
49
50         /* For __dma_alloc_coherent */
51         ISA_DMA_THRESHOLD = ~0L;
52
53         return 1;
54 }
55
56 define_machine(warp) {
57         .name           = "Warp",
58         .probe          = warp_probe,
59         .progress       = udbg_progress,
60         .init_IRQ       = uic_init_tree,
61         .get_irq        = uic_get_irq,
62         .restart        = ppc4xx_reset_system,
63         .calibrate_decr = generic_calibrate_decr,
64 };
65
66
67 static u32 post_info;
68
69 static int __init warp_post_info(void)
70 {
71         struct device_node *np;
72         void __iomem *fpga;
73         u32 post1, post2;
74
75         /* Sighhhh... POST information is in the sd area. */
76         np = of_find_compatible_node(NULL, NULL, "pika,fpga-sd");
77         if (np == NULL)
78                 return -ENOENT;
79
80         fpga = of_iomap(np, 0);
81         of_node_put(np);
82         if (fpga == NULL)
83                 return -ENOENT;
84
85         post1 = in_be32(fpga + 0x40);
86         post2 = in_be32(fpga + 0x44);
87
88         iounmap(fpga);
89
90         if (post1 || post2) {
91                 printk(KERN_INFO "Warp POST %08x %08x\n", post1, post2);
92                 post_info = 1;
93         } else
94                 printk(KERN_INFO "Warp POST OK\n");
95
96         return 0;
97 }
98
99
100 #ifdef CONFIG_SENSORS_AD7414
101
102 static LIST_HEAD(dtm_shutdown_list);
103 static void __iomem *dtm_fpga;
104 static unsigned green_led, red_led;
105
106
107 struct dtm_shutdown {
108         struct list_head list;
109         void (*func)(void *arg);
110         void *arg;
111 };
112
113
114 int pika_dtm_register_shutdown(void (*func)(void *arg), void *arg)
115 {
116         struct dtm_shutdown *shutdown;
117
118         shutdown = kmalloc(sizeof(struct dtm_shutdown), GFP_KERNEL);
119         if (shutdown == NULL)
120                 return -ENOMEM;
121
122         shutdown->func = func;
123         shutdown->arg = arg;
124
125         list_add(&shutdown->list, &dtm_shutdown_list);
126
127         return 0;
128 }
129
130 int pika_dtm_unregister_shutdown(void (*func)(void *arg), void *arg)
131 {
132         struct dtm_shutdown *shutdown;
133
134         list_for_each_entry(shutdown, &dtm_shutdown_list, list)
135                 if (shutdown->func == func && shutdown->arg == arg) {
136                         list_del(&shutdown->list);
137                         kfree(shutdown);
138                         return 0;
139                 }
140
141         return -EINVAL;
142 }
143
144 static irqreturn_t temp_isr(int irq, void *context)
145 {
146         struct dtm_shutdown *shutdown;
147         int value = 1;
148
149         local_irq_disable();
150
151         gpio_set_value(green_led, 0);
152
153         /* Run through the shutdown list. */
154         list_for_each_entry(shutdown, &dtm_shutdown_list, list)
155                 shutdown->func(shutdown->arg);
156
157         printk(KERN_EMERG "\n\nCritical Temperature Shutdown\n\n");
158
159         while (1) {
160                 if (dtm_fpga) {
161                         unsigned reset = in_be32(dtm_fpga + 0x14);
162                         out_be32(dtm_fpga + 0x14, reset);
163                 }
164
165                 gpio_set_value(red_led, value);
166                 value ^= 1;
167                 mdelay(500);
168         }
169 }
170
171 static int pika_setup_leds(void)
172 {
173         struct device_node *np, *child;
174
175         np = of_find_compatible_node(NULL, NULL, "gpio-leds");
176         if (!np) {
177                 printk(KERN_ERR __FILE__ ": Unable to find leds\n");
178                 return -ENOENT;
179         }
180
181         for_each_child_of_node(np, child)
182                 if (strcmp(child->name, "green") == 0) {
183                         green_led = of_get_gpio(child, 0);
184                         /* Turn back on the green LED */
185                         gpio_set_value(green_led, 1);
186                 } else if (strcmp(child->name, "red") == 0) {
187                         red_led = of_get_gpio(child, 0);
188                         /* Set based on post */
189                         gpio_set_value(red_led, post_info);
190                 }
191
192         of_node_put(np);
193
194         return 0;
195 }
196
197 static void pika_setup_critical_temp(struct device_node *np,
198                                      struct i2c_client *client)
199 {
200         int irq, rc;
201
202         /* Do this before enabling critical temp interrupt since we
203          * may immediately interrupt.
204          */
205         pika_setup_leds();
206
207         /* These registers are in 1 degree increments. */
208         i2c_smbus_write_byte_data(client, 2, 65); /* Thigh */
209         i2c_smbus_write_byte_data(client, 3,  0); /* Tlow */
210
211         irq = irq_of_parse_and_map(np, 0);
212         if (irq  == NO_IRQ) {
213                 printk(KERN_ERR __FILE__ ": Unable to get ad7414 irq\n");
214                 return;
215         }
216
217         rc = request_irq(irq, temp_isr, 0, "ad7414", NULL);
218         if (rc) {
219                 printk(KERN_ERR __FILE__
220                        ": Unable to request ad7414 irq %d = %d\n", irq, rc);
221                 return;
222         }
223 }
224
225 static inline void pika_dtm_check_fan(void __iomem *fpga)
226 {
227         static int fan_state;
228         u32 fan = in_be32(fpga + 0x34) & (1 << 14);
229
230         if (fan_state != fan) {
231                 fan_state = fan;
232                 if (fan)
233                         printk(KERN_WARNING "Fan rotation error detected."
234                                    " Please check hardware.\n");
235         }
236 }
237
238 static int pika_dtm_thread(void __iomem *fpga)
239 {
240         struct device_node *np;
241         struct i2c_client *client;
242
243         np = of_find_compatible_node(NULL, NULL, "adi,ad7414");
244         if (np == NULL)
245                 return -ENOENT;
246
247         client = of_find_i2c_device_by_node(np);
248         if (client == NULL) {
249                 of_node_put(np);
250                 return -ENOENT;
251         }
252
253         pika_setup_critical_temp(np, client);
254
255         of_node_put(np);
256
257         printk(KERN_INFO "Warp DTM thread running.\n");
258
259         while (!kthread_should_stop()) {
260                 int val;
261
262                 val = i2c_smbus_read_word_data(client, 0);
263                 if (val < 0)
264                         dev_dbg(&client->dev, "DTM read temp failed.\n");
265                 else {
266                         s16 temp = swab16(val);
267                         out_be32(fpga + 0x20, temp);
268                 }
269
270                 pika_dtm_check_fan(fpga);
271
272                 set_current_state(TASK_INTERRUPTIBLE);
273                 schedule_timeout(HZ);
274         }
275
276         return 0;
277 }
278
279 static int __init pika_dtm_start(void)
280 {
281         struct task_struct *dtm_thread;
282         struct device_node *np;
283
284         np = of_find_compatible_node(NULL, NULL, "pika,fpga");
285         if (np == NULL)
286                 return -ENOENT;
287
288         dtm_fpga = of_iomap(np, 0);
289         of_node_put(np);
290         if (dtm_fpga == NULL)
291                 return -ENOENT;
292
293         /* Must get post info before thread starts. */
294         warp_post_info();
295
296         dtm_thread = kthread_run(pika_dtm_thread, dtm_fpga, "pika-dtm");
297         if (IS_ERR(dtm_thread)) {
298                 iounmap(dtm_fpga);
299                 return PTR_ERR(dtm_thread);
300         }
301
302         return 0;
303 }
304 machine_late_initcall(warp, pika_dtm_start);
305
306 #else /* !CONFIG_SENSORS_AD7414 */
307
308 int pika_dtm_register_shutdown(void (*func)(void *arg), void *arg)
309 {
310         return 0;
311 }
312
313 int pika_dtm_unregister_shutdown(void (*func)(void *arg), void *arg)
314 {
315         return 0;
316 }
317
318 machine_late_initcall(warp, warp_post_info);
319
320 #endif
321
322 EXPORT_SYMBOL(pika_dtm_register_shutdown);
323 EXPORT_SYMBOL(pika_dtm_unregister_shutdown);