warp.c 7.2 KB

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