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