beat.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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/export.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 "beat_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. EXPORT_SYMBOL(beat_halt_code);
  45. void beat_halt(void)
  46. {
  47. beat_shutdown_logical_partition(beat_halt_code);
  48. }
  49. int beat_set_rtc_time(struct rtc_time *rtc_time)
  50. {
  51. u64 tim;
  52. tim = mktime(rtc_time->tm_year+1900,
  53. rtc_time->tm_mon+1, rtc_time->tm_mday,
  54. rtc_time->tm_hour, rtc_time->tm_min, rtc_time->tm_sec);
  55. if (beat_rtc_write(tim))
  56. return -1;
  57. return 0;
  58. }
  59. void beat_get_rtc_time(struct rtc_time *rtc_time)
  60. {
  61. u64 tim;
  62. if (beat_rtc_read(&tim))
  63. tim = 0;
  64. to_tm(tim, rtc_time);
  65. rtc_time->tm_year -= 1900;
  66. rtc_time->tm_mon -= 1;
  67. }
  68. #define BEAT_NVRAM_SIZE 4096
  69. ssize_t beat_nvram_read(char *buf, size_t count, loff_t *index)
  70. {
  71. unsigned int i;
  72. unsigned long len;
  73. char *p = buf;
  74. if (*index >= BEAT_NVRAM_SIZE)
  75. return -ENODEV;
  76. i = *index;
  77. if (i + count > BEAT_NVRAM_SIZE)
  78. count = BEAT_NVRAM_SIZE - i;
  79. for (; count != 0; count -= len) {
  80. len = count;
  81. if (len > BEAT_NVRW_CNT)
  82. len = BEAT_NVRW_CNT;
  83. if (beat_eeprom_read(i, len, p))
  84. return -EIO;
  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. p += len;
  108. i += len;
  109. }
  110. *index = i;
  111. return p - buf;
  112. }
  113. ssize_t beat_nvram_get_size(void)
  114. {
  115. return BEAT_NVRAM_SIZE;
  116. }
  117. int beat_set_xdabr(unsigned long dabr)
  118. {
  119. if (beat_set_dabr(dabr, DABRX_KERNEL | DABRX_USER))
  120. return -1;
  121. return 0;
  122. }
  123. int64_t beat_get_term_char(u64 vterm, u64 *len, u64 *t1, u64 *t2)
  124. {
  125. u64 db[2];
  126. s64 ret;
  127. ret = beat_get_characters_from_console(vterm, len, (u8 *)db);
  128. if (ret == 0) {
  129. *t1 = db[0];
  130. *t2 = db[1];
  131. }
  132. return ret;
  133. }
  134. EXPORT_SYMBOL(beat_get_term_char);
  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. EXPORT_SYMBOL(beat_put_term_char);
  143. void beat_power_save(void)
  144. {
  145. beat_pause(0);
  146. }
  147. #ifdef CONFIG_KEXEC
  148. void beat_kexec_cpu_down(int crash, int secondary)
  149. {
  150. beatic_deinit_IRQ();
  151. }
  152. #endif
  153. static irqreturn_t beat_power_event(int virq, void *arg)
  154. {
  155. printk(KERN_DEBUG "Beat: power button pressed\n");
  156. beat_pm_poweroff_flag = 1;
  157. ctrl_alt_del();
  158. return IRQ_HANDLED;
  159. }
  160. static irqreturn_t beat_reset_event(int virq, void *arg)
  161. {
  162. printk(KERN_DEBUG "Beat: reset button pressed\n");
  163. beat_pm_poweroff_flag = 0;
  164. ctrl_alt_del();
  165. return IRQ_HANDLED;
  166. }
  167. static struct beat_event_list {
  168. const char *typecode;
  169. irq_handler_t handler;
  170. unsigned int virq;
  171. } beat_event_list[] = {
  172. { "power", beat_power_event, 0 },
  173. { "reset", beat_reset_event, 0 },
  174. };
  175. static int __init beat_register_event(void)
  176. {
  177. u64 path[4], data[2];
  178. int rc, i;
  179. unsigned int virq;
  180. for (i = 0; i < ARRAY_SIZE(beat_event_list); i++) {
  181. struct beat_event_list *ev = &beat_event_list[i];
  182. if (beat_construct_event_receive_port(data) != 0) {
  183. printk(KERN_ERR "Beat: "
  184. "cannot construct event receive port for %s\n",
  185. ev->typecode);
  186. return -EINVAL;
  187. }
  188. virq = irq_create_mapping(NULL, data[0]);
  189. if (virq == NO_IRQ) {
  190. printk(KERN_ERR "Beat: failed to get virtual IRQ"
  191. " for event receive port for %s\n",
  192. ev->typecode);
  193. beat_destruct_event_receive_port(data[0]);
  194. return -EIO;
  195. }
  196. ev->virq = virq;
  197. rc = request_irq(virq, ev->handler, 0,
  198. ev->typecode, NULL);
  199. if (rc != 0) {
  200. printk(KERN_ERR "Beat: failed to request virtual IRQ"
  201. " for event receive port for %s\n",
  202. ev->typecode);
  203. beat_destruct_event_receive_port(data[0]);
  204. return rc;
  205. }
  206. path[0] = 0x1000000065780000ul; /* 1,ex */
  207. path[1] = 0x627574746f6e0000ul; /* button */
  208. path[2] = 0;
  209. strncpy((char *)&path[2], ev->typecode, 8);
  210. path[3] = 0;
  211. data[1] = 0;
  212. beat_create_repository_node(path, data);
  213. }
  214. return 0;
  215. }
  216. static int __init beat_event_init(void)
  217. {
  218. if (!firmware_has_feature(FW_FEATURE_BEAT))
  219. return -EINVAL;
  220. beat_pm_poweroff_flag = 0;
  221. return beat_register_event();
  222. }
  223. device_initcall(beat_event_init);