warp.c 6.4 KB

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