warp.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. if (!of_flat_dt_is_compatible(root, "pika,warp"))
  41. return 0;
  42. /* For __dma_alloc_coherent */
  43. ISA_DMA_THRESHOLD = ~0L;
  44. return 1;
  45. }
  46. define_machine(warp) {
  47. .name = "Warp",
  48. .probe = warp_probe,
  49. .progress = udbg_progress,
  50. .init_IRQ = uic_init_tree,
  51. .get_irq = uic_get_irq,
  52. .restart = ppc4xx_reset_system,
  53. .calibrate_decr = generic_calibrate_decr,
  54. };
  55. static u32 post_info;
  56. /* I am not sure this is the best place for this... */
  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 i2c_client *client)
  153. {
  154. struct device_node *np;
  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. np = of_find_compatible_node(NULL, NULL, "adi,ad7414");
  164. if (np == NULL) {
  165. printk(KERN_ERR __FILE__ ": Unable to find ad7414\n");
  166. return;
  167. }
  168. irq = irq_of_parse_and_map(np, 0);
  169. of_node_put(np);
  170. if (irq == NO_IRQ) {
  171. printk(KERN_ERR __FILE__ ": Unable to get ad7414 irq\n");
  172. return;
  173. }
  174. rc = request_irq(irq, temp_isr, 0, "ad7414", NULL);
  175. if (rc) {
  176. printk(KERN_ERR __FILE__
  177. ": Unable to request ad7414 irq %d = %d\n", irq, rc);
  178. return;
  179. }
  180. }
  181. static inline void pika_dtm_check_fan(void __iomem *fpga)
  182. {
  183. static int fan_state;
  184. u32 fan = in_be32(fpga + 0x34) & (1 << 14);
  185. if (fan_state != fan) {
  186. fan_state = fan;
  187. if (fan)
  188. printk(KERN_WARNING "Fan rotation error detected."
  189. " Please check hardware.\n");
  190. }
  191. }
  192. static int pika_dtm_thread(void __iomem *fpga)
  193. {
  194. struct i2c_adapter *adap;
  195. struct i2c_client *client;
  196. /* We loop in case either driver was compiled as a module and
  197. * has not been insmoded yet.
  198. */
  199. while (!(adap = i2c_get_adapter(0))) {
  200. set_current_state(TASK_INTERRUPTIBLE);
  201. schedule_timeout(HZ);
  202. }
  203. while (1) {
  204. list_for_each_entry(client, &adap->clients, list)
  205. if (client->addr == 0x4a)
  206. goto found_it;
  207. set_current_state(TASK_INTERRUPTIBLE);
  208. schedule_timeout(HZ);
  209. }
  210. found_it:
  211. pika_setup_critical_temp(client);
  212. i2c_put_adapter(adap);
  213. printk(KERN_INFO "PIKA DTM thread running.\n");
  214. while (!kthread_should_stop()) {
  215. int val;
  216. val = i2c_smbus_read_word_data(client, 0);
  217. if (val < 0)
  218. dev_dbg(&client->dev, "DTM read temp failed.\n");
  219. else {
  220. s16 temp = swab16(val);
  221. out_be32(fpga + 0x20, temp);
  222. }
  223. pika_dtm_check_fan(fpga);
  224. set_current_state(TASK_INTERRUPTIBLE);
  225. schedule_timeout(HZ);
  226. }
  227. return 0;
  228. }
  229. static int __init pika_dtm_start(void)
  230. {
  231. struct task_struct *dtm_thread;
  232. struct device_node *np;
  233. np = of_find_compatible_node(NULL, NULL, "pika,fpga");
  234. if (np == NULL)
  235. return -ENOENT;
  236. dtm_fpga = of_iomap(np, 0);
  237. of_node_put(np);
  238. if (dtm_fpga == NULL)
  239. return -ENOENT;
  240. /* Must get post info before thread starts. */
  241. warp_post_info();
  242. dtm_thread = kthread_run(pika_dtm_thread, dtm_fpga, "pika-dtm");
  243. if (IS_ERR(dtm_thread)) {
  244. iounmap(dtm_fpga);
  245. return PTR_ERR(dtm_thread);
  246. }
  247. return 0;
  248. }
  249. machine_late_initcall(warp, pika_dtm_start);
  250. #else /* !CONFIG_SENSORS_AD7414 */
  251. int pika_dtm_register_shutdown(void (*func)(void *arg), void *arg)
  252. {
  253. return 0;
  254. }
  255. int pika_dtm_unregister_shutdown(void (*func)(void *arg), void *arg)
  256. {
  257. return 0;
  258. }
  259. machine_late_initcall(warp, warp_post_info);
  260. #endif
  261. EXPORT_SYMBOL(pika_dtm_register_shutdown);
  262. EXPORT_SYMBOL(pika_dtm_unregister_shutdown);