beat.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. * Simple routines for Celleb/Beat
  3. *
  4. * (C) Copyright 2006-2007 TOSHIBA CORPORATION
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. #include <linux/module.h>
  21. #include <linux/init.h>
  22. #include <linux/err.h>
  23. #include <linux/rtc.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/irqreturn.h>
  26. #include <linux/reboot.h>
  27. #include <asm/hvconsole.h>
  28. #include <asm/time.h>
  29. #include <asm/machdep.h>
  30. #include <asm/firmware.h>
  31. #include "beat_wrapper.h"
  32. #include "beat.h"
  33. #include "interrupt.h"
  34. static int beat_pm_poweroff_flag;
  35. void beat_restart(char *cmd)
  36. {
  37. beat_shutdown_logical_partition(!beat_pm_poweroff_flag);
  38. }
  39. void beat_power_off(void)
  40. {
  41. beat_shutdown_logical_partition(0);
  42. }
  43. u64 beat_halt_code = 0x1000000000000000UL;
  44. void beat_halt(void)
  45. {
  46. beat_shutdown_logical_partition(beat_halt_code);
  47. }
  48. int beat_set_rtc_time(struct rtc_time *rtc_time)
  49. {
  50. u64 tim;
  51. tim = mktime(rtc_time->tm_year+1900,
  52. rtc_time->tm_mon+1, rtc_time->tm_mday,
  53. rtc_time->tm_hour, rtc_time->tm_min, rtc_time->tm_sec);
  54. if (beat_rtc_write(tim))
  55. return -1;
  56. return 0;
  57. }
  58. void beat_get_rtc_time(struct rtc_time *rtc_time)
  59. {
  60. u64 tim;
  61. if (beat_rtc_read(&tim))
  62. tim = 0;
  63. to_tm(tim, rtc_time);
  64. rtc_time->tm_year -= 1900;
  65. rtc_time->tm_mon -= 1;
  66. }
  67. #define BEAT_NVRAM_SIZE 4096
  68. ssize_t beat_nvram_read(char *buf, size_t count, loff_t *index)
  69. {
  70. unsigned int i;
  71. unsigned long len;
  72. char *p = buf;
  73. if (*index >= BEAT_NVRAM_SIZE)
  74. return -ENODEV;
  75. i = *index;
  76. if (i + count > BEAT_NVRAM_SIZE)
  77. count = BEAT_NVRAM_SIZE - i;
  78. for (; count != 0; count -= len) {
  79. len = count;
  80. if (len > BEAT_NVRW_CNT)
  81. len = BEAT_NVRW_CNT;
  82. if (beat_eeprom_read(i, len, p)) {
  83. return -EIO;
  84. }
  85. p += len;
  86. i += len;
  87. }
  88. *index = i;
  89. return p - buf;
  90. }
  91. ssize_t beat_nvram_write(char *buf, size_t count, loff_t *index)
  92. {
  93. unsigned int i;
  94. unsigned long len;
  95. char *p = buf;
  96. if (*index >= BEAT_NVRAM_SIZE)
  97. return -ENODEV;
  98. i = *index;
  99. if (i + count > BEAT_NVRAM_SIZE)
  100. count = BEAT_NVRAM_SIZE - i;
  101. for (; count != 0; count -= len) {
  102. len = count;
  103. if (len > BEAT_NVRW_CNT)
  104. len = BEAT_NVRW_CNT;
  105. if (beat_eeprom_write(i, len, p)) {
  106. return -EIO;
  107. }
  108. p += len;
  109. i += len;
  110. }
  111. *index = i;
  112. return p - buf;
  113. }
  114. ssize_t beat_nvram_get_size(void)
  115. {
  116. return BEAT_NVRAM_SIZE;
  117. }
  118. int beat_set_xdabr(unsigned long dabr)
  119. {
  120. if (beat_set_dabr(dabr, DABRX_KERNEL | DABRX_USER))
  121. return -1;
  122. return 0;
  123. }
  124. int64_t beat_get_term_char(u64 vterm, u64 *len, u64 *t1, u64 *t2)
  125. {
  126. u64 db[2];
  127. s64 ret;
  128. ret = beat_get_characters_from_console(vterm, len, (u8*)db);
  129. if (ret == 0) {
  130. *t1 = db[0];
  131. *t2 = db[1];
  132. }
  133. return ret;
  134. }
  135. int64_t beat_put_term_char(u64 vterm, u64 len, u64 t1, u64 t2)
  136. {
  137. u64 db[2];
  138. db[0] = t1;
  139. db[1] = t2;
  140. return beat_put_characters_to_console(vterm, len, (u8*)db);
  141. }
  142. void beat_power_save(void)
  143. {
  144. beat_pause(0);
  145. }
  146. #ifdef CONFIG_KEXEC
  147. void beat_kexec_cpu_down(int crash, int secondary)
  148. {
  149. beatic_deinit_IRQ();
  150. }
  151. #endif
  152. static irqreturn_t beat_power_event(int virq, void *arg)
  153. {
  154. printk(KERN_DEBUG "Beat: power button pressed\n");
  155. beat_pm_poweroff_flag = 1;
  156. ctrl_alt_del();
  157. return IRQ_HANDLED;
  158. }
  159. static irqreturn_t beat_reset_event(int virq, void *arg)
  160. {
  161. printk(KERN_DEBUG "Beat: reset button pressed\n");
  162. beat_pm_poweroff_flag = 0;
  163. ctrl_alt_del();
  164. return IRQ_HANDLED;
  165. }
  166. static struct beat_event_list {
  167. const char *typecode;
  168. irq_handler_t handler;
  169. unsigned int virq;
  170. } beat_event_list[] = {
  171. { "power", beat_power_event, 0 },
  172. { "reset", beat_reset_event, 0 },
  173. };
  174. static int __init beat_register_event(void)
  175. {
  176. u64 path[4], data[2];
  177. int rc, i;
  178. unsigned int virq;
  179. for (i = 0; i < ARRAY_SIZE(beat_event_list); i++) {
  180. struct beat_event_list *ev = &beat_event_list[i];
  181. if (beat_construct_event_receive_port(data) != 0) {
  182. printk(KERN_ERR "Beat: "
  183. "cannot construct event receive port for %s\n",
  184. ev->typecode);
  185. return -EINVAL;
  186. }
  187. virq = irq_create_mapping(NULL, data[0]);
  188. if (virq == NO_IRQ) {
  189. printk(KERN_ERR "Beat: failed to get virtual IRQ"
  190. " for event receive port for %s\n",
  191. ev->typecode);
  192. beat_destruct_event_receive_port(data[0]);
  193. return -EIO;
  194. }
  195. ev->virq = virq;
  196. rc = request_irq(virq, ev->handler, IRQF_DISABLED,
  197. ev->typecode, NULL);
  198. if (rc != 0) {
  199. printk(KERN_ERR "Beat: failed to request virtual IRQ"
  200. " for event receive port for %s\n",
  201. ev->typecode);
  202. beat_destruct_event_receive_port(data[0]);
  203. return rc;
  204. }
  205. path[0] = 0x1000000065780000ul; /* 1,ex */
  206. path[1] = 0x627574746f6e0000ul; /* button */
  207. path[2] = 0;
  208. strncpy((char *)&path[2], ev->typecode, 8);
  209. path[3] = 0;
  210. data[1] = 0;
  211. beat_create_repository_node(path, data);
  212. }
  213. return 0;
  214. }
  215. static int __init beat_event_init(void)
  216. {
  217. if (!firmware_has_feature(FW_FEATURE_BEAT))
  218. return -EINVAL;
  219. beat_pm_poweroff_flag = 0;
  220. return beat_register_event();
  221. }
  222. device_initcall(beat_event_init);
  223. EXPORT_SYMBOL(beat_get_term_char);
  224. EXPORT_SYMBOL(beat_put_term_char);
  225. EXPORT_SYMBOL(beat_halt_code);