warp.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 u32 post_info;
  57. static int __init warp_post_info(void)
  58. {
  59. struct device_node *np;
  60. void __iomem *fpga;
  61. u32 post1, post2;
  62. /* Sighhhh... POST information is in the sd area. */
  63. np = of_find_compatible_node(NULL, NULL, "pika,fpga-sd");
  64. if (np == NULL)
  65. return -ENOENT;
  66. fpga = of_iomap(np, 0);
  67. of_node_put(np);
  68. if (fpga == NULL)
  69. return -ENOENT;
  70. post1 = in_be32(fpga + 0x40);
  71. post2 = in_be32(fpga + 0x44);
  72. iounmap(fpga);
  73. if (post1 || post2) {
  74. printk(KERN_INFO "Warp POST %08x %08x\n", post1, post2);
  75. post_info = 1;
  76. } else
  77. printk(KERN_INFO "Warp POST OK\n");
  78. return 0;
  79. }
  80. #ifdef CONFIG_SENSORS_AD7414
  81. static LIST_HEAD(dtm_shutdown_list);
  82. static void __iomem *dtm_fpga;
  83. static unsigned green_led, red_led;
  84. struct dtm_shutdown {
  85. struct list_head list;
  86. void (*func)(void *arg);
  87. void *arg;
  88. };
  89. int pika_dtm_register_shutdown(void (*func)(void *arg), void *arg)
  90. {
  91. struct dtm_shutdown *shutdown;
  92. shutdown = kmalloc(sizeof(struct dtm_shutdown), GFP_KERNEL);
  93. if (shutdown == NULL)
  94. return -ENOMEM;
  95. shutdown->func = func;
  96. shutdown->arg = arg;
  97. list_add(&shutdown->list, &dtm_shutdown_list);
  98. return 0;
  99. }
  100. int pika_dtm_unregister_shutdown(void (*func)(void *arg), void *arg)
  101. {
  102. struct dtm_shutdown *shutdown;
  103. list_for_each_entry(shutdown, &dtm_shutdown_list, list)
  104. if (shutdown->func == func && shutdown->arg == arg) {
  105. list_del(&shutdown->list);
  106. kfree(shutdown);
  107. return 0;
  108. }
  109. return -EINVAL;
  110. }
  111. static irqreturn_t temp_isr(int irq, void *context)
  112. {
  113. struct dtm_shutdown *shutdown;
  114. int value = 1;
  115. local_irq_disable();
  116. gpio_set_value(green_led, 0);
  117. /* Run through the shutdown list. */
  118. list_for_each_entry(shutdown, &dtm_shutdown_list, list)
  119. shutdown->func(shutdown->arg);
  120. printk(KERN_EMERG "\n\nCritical Temperature Shutdown\n\n");
  121. while (1) {
  122. if (dtm_fpga) {
  123. unsigned reset = in_be32(dtm_fpga + 0x14);
  124. out_be32(dtm_fpga + 0x14, reset);
  125. }
  126. gpio_set_value(red_led, value);
  127. value ^= 1;
  128. mdelay(500);
  129. }
  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. /* Turn back on the green LED */
  143. gpio_set_value(green_led, 1);
  144. } else if (strcmp(child->name, "red") == 0) {
  145. red_led = of_get_gpio(child, 0);
  146. /* Set based on post */
  147. gpio_set_value(red_led, post_info);
  148. }
  149. of_node_put(np);
  150. return 0;
  151. }
  152. static void pika_setup_critical_temp(struct device_node *np,
  153. struct i2c_client *client)
  154. {
  155. int irq, rc;
  156. /* Do this before enabling critical temp interrupt since we
  157. * may immediately interrupt.
  158. */
  159. pika_setup_leds();
  160. /* These registers are in 1 degree increments. */
  161. i2c_smbus_write_byte_data(client, 2, 65); /* Thigh */
  162. i2c_smbus_write_byte_data(client, 3, 0); /* Tlow */
  163. irq = irq_of_parse_and_map(np, 0);
  164. if (irq == NO_IRQ) {
  165. printk(KERN_ERR __FILE__ ": Unable to get ad7414 irq\n");
  166. return;
  167. }
  168. rc = request_irq(irq, temp_isr, 0, "ad7414", NULL);
  169. if (rc) {
  170. printk(KERN_ERR __FILE__
  171. ": Unable to request ad7414 irq %d = %d\n", irq, rc);
  172. return;
  173. }
  174. }
  175. static inline void pika_dtm_check_fan(void __iomem *fpga)
  176. {
  177. static int fan_state;
  178. u32 fan = in_be32(fpga + 0x34) & (1 << 14);
  179. if (fan_state != fan) {
  180. fan_state = fan;
  181. if (fan)
  182. printk(KERN_WARNING "Fan rotation error detected."
  183. " Please check hardware.\n");
  184. }
  185. }
  186. static int pika_dtm_thread(void __iomem *fpga)
  187. {
  188. struct device_node *np;
  189. struct i2c_client *client;
  190. np = of_find_compatible_node(NULL, NULL, "adi,ad7414");
  191. if (np == NULL)
  192. return -ENOENT;
  193. client = of_find_i2c_device_by_node(np);
  194. if (client == NULL) {
  195. of_node_put(np);
  196. return -ENOENT;
  197. }
  198. pika_setup_critical_temp(np, client);
  199. of_node_put(np);
  200. printk(KERN_INFO "Warp DTM thread running.\n");
  201. while (!kthread_should_stop()) {
  202. int val;
  203. val = i2c_smbus_read_word_data(client, 0);
  204. if (val < 0)
  205. dev_dbg(&client->dev, "DTM read temp failed.\n");
  206. else {
  207. s16 temp = swab16(val);
  208. out_be32(fpga + 0x20, temp);
  209. }
  210. pika_dtm_check_fan(fpga);
  211. set_current_state(TASK_INTERRUPTIBLE);
  212. schedule_timeout(HZ);
  213. }
  214. return 0;
  215. }
  216. static int __init pika_dtm_start(void)
  217. {
  218. struct task_struct *dtm_thread;
  219. struct device_node *np;
  220. np = of_find_compatible_node(NULL, NULL, "pika,fpga");
  221. if (np == NULL)
  222. return -ENOENT;
  223. dtm_fpga = of_iomap(np, 0);
  224. of_node_put(np);
  225. if (dtm_fpga == NULL)
  226. return -ENOENT;
  227. /* Must get post info before thread starts. */
  228. warp_post_info();
  229. dtm_thread = kthread_run(pika_dtm_thread, dtm_fpga, "pika-dtm");
  230. if (IS_ERR(dtm_thread)) {
  231. iounmap(dtm_fpga);
  232. return PTR_ERR(dtm_thread);
  233. }
  234. return 0;
  235. }
  236. machine_late_initcall(warp, pika_dtm_start);
  237. #else /* !CONFIG_SENSORS_AD7414 */
  238. int pika_dtm_register_shutdown(void (*func)(void *arg), void *arg)
  239. {
  240. return 0;
  241. }
  242. int pika_dtm_unregister_shutdown(void (*func)(void *arg), void *arg)
  243. {
  244. return 0;
  245. }
  246. machine_late_initcall(warp, warp_post_info);
  247. #endif
  248. EXPORT_SYMBOL(pika_dtm_register_shutdown);
  249. EXPORT_SYMBOL(pika_dtm_unregister_shutdown);