warp.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. unsigned long root = of_get_flat_dt_root();
  43. if (!of_flat_dt_is_compatible(root, "pika,warp"))
  44. return 0;
  45. /* For __dma_alloc_coherent */
  46. ISA_DMA_THRESHOLD = ~0L;
  47. return 1;
  48. }
  49. define_machine(warp) {
  50. .name = "Warp",
  51. .probe = warp_probe,
  52. .progress = udbg_progress,
  53. .init_IRQ = uic_init_tree,
  54. .get_irq = uic_get_irq,
  55. .restart = ppc4xx_reset_system,
  56. .calibrate_decr = generic_calibrate_decr,
  57. };
  58. static int __init warp_post_info(void)
  59. {
  60. struct device_node *np;
  61. void __iomem *fpga;
  62. u32 post1, post2;
  63. /* Sighhhh... POST information is in the sd area. */
  64. np = of_find_compatible_node(NULL, NULL, "pika,fpga-sd");
  65. if (np == NULL)
  66. return -ENOENT;
  67. fpga = of_iomap(np, 0);
  68. of_node_put(np);
  69. if (fpga == NULL)
  70. return -ENOENT;
  71. post1 = in_be32(fpga + 0x40);
  72. post2 = in_be32(fpga + 0x44);
  73. iounmap(fpga);
  74. if (post1 || post2)
  75. printk(KERN_INFO "Warp POST %08x %08x\n", post1, post2);
  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. /* Not reached */
  131. return IRQ_HANDLED;
  132. }
  133. static int pika_setup_leds(void)
  134. {
  135. struct device_node *np, *child;
  136. np = of_find_compatible_node(NULL, NULL, "gpio-leds");
  137. if (!np) {
  138. printk(KERN_ERR __FILE__ ": Unable to find leds\n");
  139. return -ENOENT;
  140. }
  141. for_each_child_of_node(np, child)
  142. if (strcmp(child->name, "green") == 0)
  143. green_led = of_get_gpio(child, 0);
  144. else if (strcmp(child->name, "red") == 0)
  145. red_led = of_get_gpio(child, 0);
  146. of_node_put(np);
  147. return 0;
  148. }
  149. static void pika_setup_critical_temp(struct device_node *np,
  150. struct i2c_client *client)
  151. {
  152. int irq, rc;
  153. /* Do this before enabling critical temp interrupt since we
  154. * may immediately interrupt.
  155. */
  156. pika_setup_leds();
  157. /* These registers are in 1 degree increments. */
  158. i2c_smbus_write_byte_data(client, 2, 65); /* Thigh */
  159. i2c_smbus_write_byte_data(client, 3, 0); /* Tlow */
  160. irq = irq_of_parse_and_map(np, 0);
  161. if (irq == NO_IRQ) {
  162. printk(KERN_ERR __FILE__ ": Unable to get ad7414 irq\n");
  163. return;
  164. }
  165. rc = request_irq(irq, temp_isr, 0, "ad7414", NULL);
  166. if (rc) {
  167. printk(KERN_ERR __FILE__
  168. ": Unable to request ad7414 irq %d = %d\n", irq, rc);
  169. return;
  170. }
  171. }
  172. static inline void pika_dtm_check_fan(void __iomem *fpga)
  173. {
  174. static int fan_state;
  175. u32 fan = in_be32(fpga + 0x34) & (1 << 14);
  176. if (fan_state != fan) {
  177. fan_state = fan;
  178. if (fan)
  179. printk(KERN_WARNING "Fan rotation error detected."
  180. " Please check hardware.\n");
  181. }
  182. }
  183. static int pika_dtm_thread(void __iomem *fpga)
  184. {
  185. struct device_node *np;
  186. struct i2c_client *client;
  187. np = of_find_compatible_node(NULL, NULL, "adi,ad7414");
  188. if (np == NULL)
  189. return -ENOENT;
  190. client = of_find_i2c_device_by_node(np);
  191. if (client == NULL) {
  192. of_node_put(np);
  193. return -ENOENT;
  194. }
  195. pika_setup_critical_temp(np, client);
  196. of_node_put(np);
  197. printk(KERN_INFO "Warp DTM thread running.\n");
  198. while (!kthread_should_stop()) {
  199. int val;
  200. val = i2c_smbus_read_word_data(client, 0);
  201. if (val < 0)
  202. dev_dbg(&client->dev, "DTM read temp failed.\n");
  203. else {
  204. s16 temp = swab16(val);
  205. out_be32(fpga + 0x20, temp);
  206. }
  207. pika_dtm_check_fan(fpga);
  208. set_current_state(TASK_INTERRUPTIBLE);
  209. schedule_timeout(HZ);
  210. }
  211. return 0;
  212. }
  213. static int __init pika_dtm_start(void)
  214. {
  215. struct task_struct *dtm_thread;
  216. struct device_node *np;
  217. np = of_find_compatible_node(NULL, NULL, "pika,fpga");
  218. if (np == NULL)
  219. return -ENOENT;
  220. dtm_fpga = of_iomap(np, 0);
  221. of_node_put(np);
  222. if (dtm_fpga == NULL)
  223. return -ENOENT;
  224. /* Must get post info before thread starts. */
  225. warp_post_info();
  226. dtm_thread = kthread_run(pika_dtm_thread, dtm_fpga, "pika-dtm");
  227. if (IS_ERR(dtm_thread)) {
  228. iounmap(dtm_fpga);
  229. return PTR_ERR(dtm_thread);
  230. }
  231. return 0;
  232. }
  233. machine_late_initcall(warp, pika_dtm_start);
  234. #else /* !CONFIG_SENSORS_AD7414 */
  235. int pika_dtm_register_shutdown(void (*func)(void *arg), void *arg)
  236. {
  237. return 0;
  238. }
  239. int pika_dtm_unregister_shutdown(void (*func)(void *arg), void *arg)
  240. {
  241. return 0;
  242. }
  243. machine_late_initcall(warp, warp_post_info);
  244. #endif
  245. EXPORT_SYMBOL(pika_dtm_register_shutdown);
  246. EXPORT_SYMBOL(pika_dtm_unregister_shutdown);