warp.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * PIKA Warp(tm) board specific routines
  3. *
  4. * Copyright (c) 2008 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 <asm/machdep.h>
  19. #include <asm/prom.h>
  20. #include <asm/udbg.h>
  21. #include <asm/time.h>
  22. #include <asm/uic.h>
  23. #include <asm/ppc4xx.h>
  24. static __initdata struct of_device_id warp_of_bus[] = {
  25. { .compatible = "ibm,plb4", },
  26. { .compatible = "ibm,opb", },
  27. { .compatible = "ibm,ebc", },
  28. {},
  29. };
  30. static int __init warp_device_probe(void)
  31. {
  32. of_platform_bus_probe(NULL, warp_of_bus, NULL);
  33. return 0;
  34. }
  35. machine_device_initcall(warp, warp_device_probe);
  36. static int __init warp_probe(void)
  37. {
  38. unsigned long root = of_get_flat_dt_root();
  39. return of_flat_dt_is_compatible(root, "pika,warp");
  40. }
  41. define_machine(warp) {
  42. .name = "Warp",
  43. .probe = warp_probe,
  44. .progress = udbg_progress,
  45. .init_IRQ = uic_init_tree,
  46. .get_irq = uic_get_irq,
  47. .restart = ppc4xx_reset_system,
  48. .calibrate_decr = generic_calibrate_decr,
  49. };
  50. /* I am not sure this is the best place for this... */
  51. static int __init warp_post_info(void)
  52. {
  53. struct device_node *np;
  54. void __iomem *fpga;
  55. u32 post1, post2;
  56. /* Sighhhh... POST information is in the sd area. */
  57. np = of_find_compatible_node(NULL, NULL, "pika,fpga-sd");
  58. if (np == NULL)
  59. return -ENOENT;
  60. fpga = of_iomap(np, 0);
  61. of_node_put(np);
  62. if (fpga == NULL)
  63. return -ENOENT;
  64. post1 = in_be32(fpga + 0x40);
  65. post2 = in_be32(fpga + 0x44);
  66. iounmap(fpga);
  67. if (post1 || post2)
  68. printk(KERN_INFO "Warp POST %08x %08x\n", post1, post2);
  69. else
  70. printk(KERN_INFO "Warp POST OK\n");
  71. return 0;
  72. }
  73. machine_late_initcall(warp, warp_post_info);
  74. #ifdef CONFIG_SENSORS_AD7414
  75. static LIST_HEAD(dtm_shutdown_list);
  76. static void __iomem *dtm_fpga;
  77. static void __iomem *gpio_base;
  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. local_irq_disable();
  109. /* Run through the shutdown list. */
  110. list_for_each_entry(shutdown, &dtm_shutdown_list, list)
  111. shutdown->func(shutdown->arg);
  112. printk(KERN_EMERG "\n\nCritical Temperature Shutdown\n");
  113. while (1) {
  114. if (dtm_fpga) {
  115. unsigned reset = in_be32(dtm_fpga + 0x14);
  116. out_be32(dtm_fpga + 0x14, reset);
  117. }
  118. if (gpio_base) {
  119. unsigned leds = in_be32(gpio_base);
  120. /* green off, red toggle */
  121. leds &= ~0x80000000;
  122. leds ^= 0x40000000;
  123. out_be32(gpio_base, leds);
  124. }
  125. mdelay(500);
  126. }
  127. }
  128. static int pika_setup_leds(void)
  129. {
  130. struct device_node *np;
  131. const u32 *gpios;
  132. int len;
  133. np = of_find_compatible_node(NULL, NULL, "linux,gpio-led");
  134. if (!np) {
  135. printk(KERN_ERR __FILE__ ": Unable to find gpio-led\n");
  136. return -ENOENT;
  137. }
  138. gpios = of_get_property(np, "gpios", &len);
  139. of_node_put(np);
  140. if (!gpios || len < 4) {
  141. printk(KERN_ERR __FILE__
  142. ": Unable to get gpios property (%d)\n", len);
  143. return -ENOENT;
  144. }
  145. np = of_find_node_by_phandle(gpios[0]);
  146. if (!np) {
  147. printk(KERN_ERR __FILE__ ": Unable to find gpio\n");
  148. return -ENOENT;
  149. }
  150. gpio_base = of_iomap(np, 0);
  151. of_node_put(np);
  152. if (!gpio_base) {
  153. printk(KERN_ERR __FILE__ ": Unable to map gpio");
  154. return -ENOMEM;
  155. }
  156. return 0;
  157. }
  158. static void pika_setup_critical_temp(struct i2c_client *client)
  159. {
  160. struct device_node *np;
  161. int irq, rc;
  162. /* Do this before enabling critical temp interrupt since we
  163. * may immediately interrupt.
  164. */
  165. pika_setup_leds();
  166. /* These registers are in 1 degree increments. */
  167. i2c_smbus_write_byte_data(client, 2, 65); /* Thigh */
  168. i2c_smbus_write_byte_data(client, 3, 0); /* Tlow */
  169. np = of_find_compatible_node(NULL, NULL, "adi,ad7414");
  170. if (np == NULL) {
  171. printk(KERN_ERR __FILE__ ": Unable to find ad7414\n");
  172. return;
  173. }
  174. irq = irq_of_parse_and_map(np, 0);
  175. of_node_put(np);
  176. if (irq == NO_IRQ) {
  177. printk(KERN_ERR __FILE__ ": Unable to get ad7414 irq\n");
  178. return;
  179. }
  180. rc = request_irq(irq, temp_isr, 0, "ad7414", NULL);
  181. if (rc) {
  182. printk(KERN_ERR __FILE__
  183. ": Unable to request ad7414 irq %d = %d\n", irq, rc);
  184. return;
  185. }
  186. }
  187. static inline void pika_dtm_check_fan(void __iomem *fpga)
  188. {
  189. static int fan_state;
  190. u32 fan = in_be32(fpga + 0x34) & (1 << 14);
  191. if (fan_state != fan) {
  192. fan_state = fan;
  193. if (fan)
  194. printk(KERN_WARNING "Fan rotation error detected."
  195. " Please check hardware.\n");
  196. }
  197. }
  198. static int pika_dtm_thread(void __iomem *fpga)
  199. {
  200. struct i2c_adapter *adap;
  201. struct i2c_client *client;
  202. /* We loop in case either driver was compiled as a module and
  203. * has not been insmoded yet.
  204. */
  205. while (!(adap = i2c_get_adapter(0))) {
  206. set_current_state(TASK_INTERRUPTIBLE);
  207. schedule_timeout(HZ);
  208. }
  209. while (1) {
  210. list_for_each_entry(client, &adap->clients, list)
  211. if (client->addr == 0x4a)
  212. goto found_it;
  213. set_current_state(TASK_INTERRUPTIBLE);
  214. schedule_timeout(HZ);
  215. }
  216. found_it:
  217. i2c_put_adapter(adap);
  218. pika_setup_critical_temp(client);
  219. printk(KERN_INFO "PIKA DTM thread running.\n");
  220. while (!kthread_should_stop()) {
  221. int val;
  222. val = i2c_smbus_read_word_data(client, 0);
  223. if (val < 0)
  224. dev_dbg(&client->dev, "DTM read temp failed.\n");
  225. else {
  226. s16 temp = swab16(val);
  227. out_be32(fpga + 0x20, temp);
  228. }
  229. pika_dtm_check_fan(fpga);
  230. set_current_state(TASK_INTERRUPTIBLE);
  231. schedule_timeout(HZ);
  232. }
  233. return 0;
  234. }
  235. static int __init pika_dtm_start(void)
  236. {
  237. struct task_struct *dtm_thread;
  238. struct device_node *np;
  239. np = of_find_compatible_node(NULL, NULL, "pika,fpga");
  240. if (np == NULL)
  241. return -ENOENT;
  242. dtm_fpga = of_iomap(np, 0);
  243. of_node_put(np);
  244. if (dtm_fpga == NULL)
  245. return -ENOENT;
  246. dtm_thread = kthread_run(pika_dtm_thread, dtm_fpga, "pika-dtm");
  247. if (IS_ERR(dtm_thread)) {
  248. iounmap(dtm_fpga);
  249. return PTR_ERR(dtm_thread);
  250. }
  251. return 0;
  252. }
  253. machine_late_initcall(warp, pika_dtm_start);
  254. #else /* !CONFIG_SENSORS_AD7414 */
  255. int pika_dtm_register_shutdown(void (*func)(void *arg), void *arg)
  256. {
  257. return 0;
  258. }
  259. int pika_dtm_unregister_shutdown(void (*func)(void *arg), void *arg)
  260. {
  261. return 0;
  262. }
  263. #endif
  264. EXPORT_SYMBOL(pika_dtm_register_shutdown);
  265. EXPORT_SYMBOL(pika_dtm_unregister_shutdown);