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