warp.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 <asm/machdep.h>
  20. #include <asm/prom.h>
  21. #include <asm/udbg.h>
  22. #include <asm/time.h>
  23. #include <asm/uic.h>
  24. #include <asm/ppc4xx.h>
  25. static __initdata struct of_device_id warp_of_bus[] = {
  26. { .compatible = "ibm,plb4", },
  27. { .compatible = "ibm,opb", },
  28. { .compatible = "ibm,ebc", },
  29. {},
  30. };
  31. static int __init warp_device_probe(void)
  32. {
  33. of_platform_bus_probe(NULL, warp_of_bus, NULL);
  34. return 0;
  35. }
  36. machine_device_initcall(warp, warp_device_probe);
  37. static int __init warp_probe(void)
  38. {
  39. unsigned long root = of_get_flat_dt_root();
  40. return of_flat_dt_is_compatible(root, "pika,warp");
  41. }
  42. define_machine(warp) {
  43. .name = "Warp",
  44. .probe = warp_probe,
  45. .progress = udbg_progress,
  46. .init_IRQ = uic_init_tree,
  47. .get_irq = uic_get_irq,
  48. .restart = ppc4xx_reset_system,
  49. .calibrate_decr = generic_calibrate_decr,
  50. };
  51. static u32 post_info;
  52. /* I am not sure this is the best place for this... */
  53. static int __init warp_post_info(void)
  54. {
  55. struct device_node *np;
  56. void __iomem *fpga;
  57. u32 post1, post2;
  58. /* Sighhhh... POST information is in the sd area. */
  59. np = of_find_compatible_node(NULL, NULL, "pika,fpga-sd");
  60. if (np == NULL)
  61. return -ENOENT;
  62. fpga = of_iomap(np, 0);
  63. of_node_put(np);
  64. if (fpga == NULL)
  65. return -ENOENT;
  66. post1 = in_be32(fpga + 0x40);
  67. post2 = in_be32(fpga + 0x44);
  68. iounmap(fpga);
  69. if (post1 || post2) {
  70. printk(KERN_INFO "Warp POST %08x %08x\n", post1, post2);
  71. post_info = 1;
  72. } else
  73. printk(KERN_INFO "Warp POST OK\n");
  74. return 0;
  75. }
  76. #ifdef CONFIG_SENSORS_AD7414
  77. static LIST_HEAD(dtm_shutdown_list);
  78. static void __iomem *dtm_fpga;
  79. static unsigned green_led, red_led;
  80. struct dtm_shutdown {
  81. struct list_head list;
  82. void (*func)(void *arg);
  83. void *arg;
  84. };
  85. int pika_dtm_register_shutdown(void (*func)(void *arg), void *arg)
  86. {
  87. struct dtm_shutdown *shutdown;
  88. shutdown = kmalloc(sizeof(struct dtm_shutdown), GFP_KERNEL);
  89. if (shutdown == NULL)
  90. return -ENOMEM;
  91. shutdown->func = func;
  92. shutdown->arg = arg;
  93. list_add(&shutdown->list, &dtm_shutdown_list);
  94. return 0;
  95. }
  96. int pika_dtm_unregister_shutdown(void (*func)(void *arg), void *arg)
  97. {
  98. struct dtm_shutdown *shutdown;
  99. list_for_each_entry(shutdown, &dtm_shutdown_list, list)
  100. if (shutdown->func == func && shutdown->arg == arg) {
  101. list_del(&shutdown->list);
  102. kfree(shutdown);
  103. return 0;
  104. }
  105. return -EINVAL;
  106. }
  107. static irqreturn_t temp_isr(int irq, void *context)
  108. {
  109. struct dtm_shutdown *shutdown;
  110. int value = 1;
  111. local_irq_disable();
  112. gpio_set_value(green_led, 0);
  113. /* Run through the shutdown list. */
  114. list_for_each_entry(shutdown, &dtm_shutdown_list, list)
  115. shutdown->func(shutdown->arg);
  116. printk(KERN_EMERG "\n\nCritical Temperature Shutdown\n\n");
  117. while (1) {
  118. if (dtm_fpga) {
  119. unsigned reset = in_be32(dtm_fpga + 0x14);
  120. out_be32(dtm_fpga + 0x14, reset);
  121. }
  122. gpio_set_value(red_led, value);
  123. value ^= 1;
  124. mdelay(500);
  125. }
  126. }
  127. static int pika_setup_leds(void)
  128. {
  129. struct device_node *np, *child;
  130. np = of_find_compatible_node(NULL, NULL, "gpio-leds");
  131. if (!np) {
  132. printk(KERN_ERR __FILE__ ": Unable to find leds\n");
  133. return -ENOENT;
  134. }
  135. for_each_child_of_node(np, child)
  136. if (strcmp(child->name, "green") == 0) {
  137. green_led = of_get_gpio(child, 0);
  138. /* Turn back on the green LED */
  139. gpio_set_value(green_led, 1);
  140. } else if (strcmp(child->name, "red") == 0) {
  141. red_led = of_get_gpio(child, 0);
  142. /* Set based on post */
  143. gpio_set_value(red_led, post_info);
  144. }
  145. of_node_put(np);
  146. return 0;
  147. }
  148. static void pika_setup_critical_temp(struct i2c_client *client)
  149. {
  150. struct device_node *np;
  151. int irq, rc;
  152. /* Do this before enabling critical temp interrupt since we
  153. * may immediately interrupt.
  154. */
  155. pika_setup_leds();
  156. /* These registers are in 1 degree increments. */
  157. i2c_smbus_write_byte_data(client, 2, 65); /* Thigh */
  158. i2c_smbus_write_byte_data(client, 3, 0); /* Tlow */
  159. np = of_find_compatible_node(NULL, NULL, "adi,ad7414");
  160. if (np == NULL) {
  161. printk(KERN_ERR __FILE__ ": Unable to find ad7414\n");
  162. return;
  163. }
  164. irq = irq_of_parse_and_map(np, 0);
  165. of_node_put(np);
  166. if (irq == NO_IRQ) {
  167. printk(KERN_ERR __FILE__ ": Unable to get ad7414 irq\n");
  168. return;
  169. }
  170. rc = request_irq(irq, temp_isr, 0, "ad7414", NULL);
  171. if (rc) {
  172. printk(KERN_ERR __FILE__
  173. ": Unable to request ad7414 irq %d = %d\n", irq, rc);
  174. return;
  175. }
  176. }
  177. static inline void pika_dtm_check_fan(void __iomem *fpga)
  178. {
  179. static int fan_state;
  180. u32 fan = in_be32(fpga + 0x34) & (1 << 14);
  181. if (fan_state != fan) {
  182. fan_state = fan;
  183. if (fan)
  184. printk(KERN_WARNING "Fan rotation error detected."
  185. " Please check hardware.\n");
  186. }
  187. }
  188. static int pika_dtm_thread(void __iomem *fpga)
  189. {
  190. struct i2c_adapter *adap;
  191. struct i2c_client *client;
  192. /* We loop in case either driver was compiled as a module and
  193. * has not been insmoded yet.
  194. */
  195. while (!(adap = i2c_get_adapter(0))) {
  196. set_current_state(TASK_INTERRUPTIBLE);
  197. schedule_timeout(HZ);
  198. }
  199. while (1) {
  200. list_for_each_entry(client, &adap->clients, list)
  201. if (client->addr == 0x4a)
  202. goto found_it;
  203. set_current_state(TASK_INTERRUPTIBLE);
  204. schedule_timeout(HZ);
  205. }
  206. found_it:
  207. pika_setup_critical_temp(client);
  208. i2c_put_adapter(adap);
  209. printk(KERN_INFO "PIKA DTM thread running.\n");
  210. while (!kthread_should_stop()) {
  211. int val;
  212. val = i2c_smbus_read_word_data(client, 0);
  213. if (val < 0)
  214. dev_dbg(&client->dev, "DTM read temp failed.\n");
  215. else {
  216. s16 temp = swab16(val);
  217. out_be32(fpga + 0x20, temp);
  218. }
  219. pika_dtm_check_fan(fpga);
  220. set_current_state(TASK_INTERRUPTIBLE);
  221. schedule_timeout(HZ);
  222. }
  223. return 0;
  224. }
  225. static int __init pika_dtm_start(void)
  226. {
  227. struct task_struct *dtm_thread;
  228. struct device_node *np;
  229. np = of_find_compatible_node(NULL, NULL, "pika,fpga");
  230. if (np == NULL)
  231. return -ENOENT;
  232. dtm_fpga = of_iomap(np, 0);
  233. of_node_put(np);
  234. if (dtm_fpga == NULL)
  235. return -ENOENT;
  236. /* Must get post info before thread starts. */
  237. warp_post_info();
  238. dtm_thread = kthread_run(pika_dtm_thread, dtm_fpga, "pika-dtm");
  239. if (IS_ERR(dtm_thread)) {
  240. iounmap(dtm_fpga);
  241. return PTR_ERR(dtm_thread);
  242. }
  243. return 0;
  244. }
  245. machine_late_initcall(warp, pika_dtm_start);
  246. #else /* !CONFIG_SENSORS_AD7414 */
  247. int pika_dtm_register_shutdown(void (*func)(void *arg), void *arg)
  248. {
  249. return 0;
  250. }
  251. int pika_dtm_unregister_shutdown(void (*func)(void *arg), void *arg)
  252. {
  253. return 0;
  254. }
  255. machine_late_initcall(warp, warp_post_info);
  256. #endif
  257. EXPORT_SYMBOL(pika_dtm_register_shutdown);
  258. EXPORT_SYMBOL(pika_dtm_unregister_shutdown);