[POWERPC] Celleb: Support for Power/Reset buttons
[linux-2.6] / arch / powerpc / platforms / celleb / beat.c
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
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/err.h>
24 #include <linux/rtc.h>
25 #include <linux/interrupt.h>
26 #include <linux/irqreturn.h>
27 #include <linux/reboot.h>
28
29 #include <asm/hvconsole.h>
30 #include <asm/time.h>
31 #include <asm/machdep.h>
32 #include <asm/firmware.h>
33
34 #include "beat_wrapper.h"
35 #include "beat.h"
36 #include "interrupt.h"
37
38 static int beat_pm_poweroff_flag;
39
40 void beat_restart(char *cmd)
41 {
42         beat_shutdown_logical_partition(!beat_pm_poweroff_flag);
43 }
44
45 void beat_power_off(void)
46 {
47         beat_shutdown_logical_partition(0);
48 }
49
50 u64 beat_halt_code = 0x1000000000000000UL;
51
52 void beat_halt(void)
53 {
54         beat_shutdown_logical_partition(beat_halt_code);
55 }
56
57 int beat_set_rtc_time(struct rtc_time *rtc_time)
58 {
59         u64 tim;
60         tim = mktime(rtc_time->tm_year+1900,
61                      rtc_time->tm_mon+1, rtc_time->tm_mday,
62                      rtc_time->tm_hour, rtc_time->tm_min, rtc_time->tm_sec);
63         if (beat_rtc_write(tim))
64                 return -1;
65         return 0;
66 }
67
68 void beat_get_rtc_time(struct rtc_time *rtc_time)
69 {
70         u64 tim;
71
72         if (beat_rtc_read(&tim))
73                 tim = 0;
74         to_tm(tim, rtc_time);
75         rtc_time->tm_year -= 1900;
76         rtc_time->tm_mon -= 1;
77 }
78
79 #define BEAT_NVRAM_SIZE 4096
80
81 ssize_t beat_nvram_read(char *buf, size_t count, loff_t *index)
82 {
83         unsigned int i;
84         unsigned long len;
85         char *p = buf;
86
87         if (*index >= BEAT_NVRAM_SIZE)
88                 return -ENODEV;
89         i = *index;
90         if (i + count > BEAT_NVRAM_SIZE)
91                 count = BEAT_NVRAM_SIZE - i;
92
93         for (; count != 0; count -= len) {
94                 len = count;
95                 if (len > BEAT_NVRW_CNT)
96                         len = BEAT_NVRW_CNT;
97                 if (beat_eeprom_read(i, len, p)) {
98                         return -EIO;
99                 }
100
101                 p += len;
102                 i += len;
103         }
104         *index = i;
105         return p - buf;
106 }
107
108 ssize_t beat_nvram_write(char *buf, size_t count, loff_t *index)
109 {
110         unsigned int i;
111         unsigned long len;
112         char *p = buf;
113
114         if (*index >= BEAT_NVRAM_SIZE)
115                 return -ENODEV;
116         i = *index;
117         if (i + count > BEAT_NVRAM_SIZE)
118                 count = BEAT_NVRAM_SIZE - i;
119
120         for (; count != 0; count -= len) {
121                 len = count;
122                 if (len > BEAT_NVRW_CNT)
123                         len = BEAT_NVRW_CNT;
124                 if (beat_eeprom_write(i, len, p)) {
125                         return -EIO;
126                 }
127
128                 p += len;
129                 i += len;
130         }
131         *index = i;
132         return p - buf;
133 }
134
135 ssize_t beat_nvram_get_size(void)
136 {
137         return BEAT_NVRAM_SIZE;
138 }
139
140 int beat_set_xdabr(unsigned long dabr)
141 {
142         if (beat_set_dabr(dabr, DABRX_KERNEL | DABRX_USER))
143                 return -1;
144         return 0;
145 }
146
147 int64_t beat_get_term_char(u64 vterm, u64 *len, u64 *t1, u64 *t2)
148 {
149         u64 db[2];
150         s64 ret;
151
152         ret = beat_get_characters_from_console(vterm, len, (u8*)db);
153         if (ret == 0) {
154                 *t1 = db[0];
155                 *t2 = db[1];
156         }
157         return ret;
158 }
159
160 int64_t beat_put_term_char(u64 vterm, u64 len, u64 t1, u64 t2)
161 {
162         u64 db[2];
163
164         db[0] = t1;
165         db[1] = t2;
166         return beat_put_characters_to_console(vterm, len, (u8*)db);
167 }
168
169 void beat_power_save(void)
170 {
171         beat_pause(0);
172 }
173
174 #ifdef CONFIG_KEXEC
175 void beat_kexec_cpu_down(int crash, int secondary)
176 {
177         beatic_deinit_IRQ();
178 }
179 #endif
180
181 static irqreturn_t beat_power_event(int virq, void *arg)
182 {
183         printk(KERN_DEBUG "Beat: power button pressed\n");
184         beat_pm_poweroff_flag = 1;
185         ctrl_alt_del();
186         return IRQ_HANDLED;
187 }
188
189 static irqreturn_t beat_reset_event(int virq, void *arg)
190 {
191         printk(KERN_DEBUG "Beat: reset button pressed\n");
192         beat_pm_poweroff_flag = 0;
193         ctrl_alt_del();
194         return IRQ_HANDLED;
195 }
196
197 static struct beat_event_list {
198         const char *typecode;
199         irq_handler_t handler;
200         unsigned int virq;
201 } beat_event_list[] = {
202         { "power", beat_power_event, 0 },
203         { "reset", beat_reset_event, 0 },
204 };
205
206 static int __init beat_register_event(void)
207 {
208         u64 path[4], data[2];
209         int rc, i;
210         unsigned int virq;
211
212         for (i = 0; i < ARRAY_SIZE(beat_event_list); i++) {
213                 struct beat_event_list *ev = &beat_event_list[i];
214
215                 if (beat_construct_event_receive_port(data) != 0) {
216                         printk(KERN_ERR "Beat: "
217                                "cannot construct event receive port for %s\n",
218                                ev->typecode);
219                         return -EINVAL;
220                 }
221
222                 virq = irq_create_mapping(NULL, data[0]);
223                 if (virq == NO_IRQ) {
224                         printk(KERN_ERR "Beat: failed to get virtual IRQ"
225                                " for event receive port for %s\n",
226                                ev->typecode);
227                         beat_destruct_event_receive_port(data[0]);
228                         return -EIO;
229                 }
230                 ev->virq = virq;
231
232                 rc = request_irq(virq, ev->handler, IRQF_DISABLED,
233                                       ev->typecode, NULL);
234                 if (rc != 0) {
235                         printk(KERN_ERR "Beat: failed to request virtual IRQ"
236                                " for event receive port for %s\n",
237                                ev->typecode);
238                         beat_destruct_event_receive_port(data[0]);
239                         return rc;
240                 }
241
242                 path[0] = 0x1000000065780000ul; /* 1,ex */
243                 path[1] = 0x627574746f6e0000ul; /* button */
244                 path[2] = 0;
245                 strncpy((char *)&path[2], ev->typecode, 8);
246                 path[3] = 0;
247                 data[1] = 0;
248
249                 beat_create_repository_node(path, data);
250         }
251         return 0;
252 }
253
254 static int __init beat_event_init(void)
255 {
256         if (!firmware_has_feature(FW_FEATURE_BEAT))
257                 return -EINVAL;
258
259         beat_pm_poweroff_flag = 0;
260         return beat_register_event();
261 }
262
263 device_initcall(beat_event_init);
264
265 EXPORT_SYMBOL(beat_get_term_char);
266 EXPORT_SYMBOL(beat_put_term_char);
267 EXPORT_SYMBOL(beat_halt_code);