warp.c 6.4 KB

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