warp.c 6.2 KB

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