Port PCI NIC hotplug to QemuOpts
[qemu/aliguori-queue.git] / hw / twl92230.c
bloba6c60ba2b817e7d1c41bd8d3de91fc5896654a71
1 /*
2 * TI TWL92230C energy-management companion device for the OMAP24xx.
3 * Aka. Menelaus (N4200 MENELAUS1_V2.2)
5 * Copyright (C) 2008 Nokia Corporation
6 * Written by Andrzej Zaborowski <andrew@openedhand.com>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 or
11 * (at your option) version 3 of the License.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include "hw.h"
23 #include "qemu-timer.h"
24 #include "i2c.h"
25 #include "sysemu.h"
26 #include "console.h"
28 #define VERBOSE 1
30 typedef struct {
31 i2c_slave i2c;
33 int firstbyte;
34 uint8_t reg;
36 uint8_t vcore[5];
37 uint8_t dcdc[3];
38 uint8_t ldo[8];
39 uint8_t sleep[2];
40 uint8_t osc;
41 uint8_t detect;
42 uint16_t mask;
43 uint16_t status;
44 uint8_t dir;
45 uint8_t inputs;
46 uint8_t outputs;
47 uint8_t bbsms;
48 uint8_t pull[4];
49 uint8_t mmc_ctrl[3];
50 uint8_t mmc_debounce;
51 struct {
52 uint8_t ctrl;
53 uint16_t comp;
54 QEMUTimer *hz_tm;
55 int64_t next;
56 struct tm tm;
57 struct tm new;
58 struct tm alm;
59 int sec_offset;
60 int alm_sec;
61 int next_comp;
62 } rtc;
63 uint16_t rtc_next_vmstate;
64 qemu_irq out[4];
65 qemu_irq *in;
66 uint8_t pwrbtn_state;
67 qemu_irq pwrbtn;
68 } MenelausState;
70 static inline void menelaus_update(MenelausState *s)
72 qemu_set_irq(s->out[3], s->status & ~s->mask);
75 static inline void menelaus_rtc_start(MenelausState *s)
77 s->rtc.next += qemu_get_clock(rt_clock);
78 qemu_mod_timer(s->rtc.hz_tm, s->rtc.next);
81 static inline void menelaus_rtc_stop(MenelausState *s)
83 qemu_del_timer(s->rtc.hz_tm);
84 s->rtc.next -= qemu_get_clock(rt_clock);
85 if (s->rtc.next < 1)
86 s->rtc.next = 1;
89 static void menelaus_rtc_update(MenelausState *s)
91 qemu_get_timedate(&s->rtc.tm, s->rtc.sec_offset);
94 static void menelaus_alm_update(MenelausState *s)
96 if ((s->rtc.ctrl & 3) == 3)
97 s->rtc.alm_sec = qemu_timedate_diff(&s->rtc.alm) - s->rtc.sec_offset;
100 static void menelaus_rtc_hz(void *opaque)
102 MenelausState *s = (MenelausState *) opaque;
104 s->rtc.next_comp --;
105 s->rtc.alm_sec --;
106 s->rtc.next += 1000;
107 qemu_mod_timer(s->rtc.hz_tm, s->rtc.next);
108 if ((s->rtc.ctrl >> 3) & 3) { /* EVERY */
109 menelaus_rtc_update(s);
110 if (((s->rtc.ctrl >> 3) & 3) == 1 && !s->rtc.tm.tm_sec)
111 s->status |= 1 << 8; /* RTCTMR */
112 else if (((s->rtc.ctrl >> 3) & 3) == 2 && !s->rtc.tm.tm_min)
113 s->status |= 1 << 8; /* RTCTMR */
114 else if (!s->rtc.tm.tm_hour)
115 s->status |= 1 << 8; /* RTCTMR */
116 } else
117 s->status |= 1 << 8; /* RTCTMR */
118 if ((s->rtc.ctrl >> 1) & 1) { /* RTC_AL_EN */
119 if (s->rtc.alm_sec == 0)
120 s->status |= 1 << 9; /* RTCALM */
121 /* TODO: wake-up */
123 if (s->rtc.next_comp <= 0) {
124 s->rtc.next -= muldiv64((int16_t) s->rtc.comp, 1000, 0x8000);
125 s->rtc.next_comp = 3600;
127 menelaus_update(s);
130 static void menelaus_reset(i2c_slave *i2c)
132 MenelausState *s = (MenelausState *) i2c;
133 s->reg = 0x00;
135 s->vcore[0] = 0x0c; /* XXX: X-loader needs 0x8c? check! */
136 s->vcore[1] = 0x05;
137 s->vcore[2] = 0x02;
138 s->vcore[3] = 0x0c;
139 s->vcore[4] = 0x03;
140 s->dcdc[0] = 0x33; /* Depends on wiring */
141 s->dcdc[1] = 0x03;
142 s->dcdc[2] = 0x00;
143 s->ldo[0] = 0x95;
144 s->ldo[1] = 0x7e;
145 s->ldo[2] = 0x00;
146 s->ldo[3] = 0x00; /* Depends on wiring */
147 s->ldo[4] = 0x03; /* Depends on wiring */
148 s->ldo[5] = 0x00;
149 s->ldo[6] = 0x00;
150 s->ldo[7] = 0x00;
151 s->sleep[0] = 0x00;
152 s->sleep[1] = 0x00;
153 s->osc = 0x01;
154 s->detect = 0x09;
155 s->mask = 0x0fff;
156 s->status = 0;
157 s->dir = 0x07;
158 s->outputs = 0x00;
159 s->bbsms = 0x00;
160 s->pull[0] = 0x00;
161 s->pull[1] = 0x00;
162 s->pull[2] = 0x00;
163 s->pull[3] = 0x00;
164 s->mmc_ctrl[0] = 0x03;
165 s->mmc_ctrl[1] = 0xc0;
166 s->mmc_ctrl[2] = 0x00;
167 s->mmc_debounce = 0x05;
169 if (s->rtc.ctrl & 1)
170 menelaus_rtc_stop(s);
171 s->rtc.ctrl = 0x00;
172 s->rtc.comp = 0x0000;
173 s->rtc.next = 1000;
174 s->rtc.sec_offset = 0;
175 s->rtc.next_comp = 1800;
176 s->rtc.alm_sec = 1800;
177 s->rtc.alm.tm_sec = 0x00;
178 s->rtc.alm.tm_min = 0x00;
179 s->rtc.alm.tm_hour = 0x00;
180 s->rtc.alm.tm_mday = 0x01;
181 s->rtc.alm.tm_mon = 0x00;
182 s->rtc.alm.tm_year = 2004;
183 menelaus_update(s);
186 static inline uint8_t to_bcd(int val)
188 return ((val / 10) << 4) | (val % 10);
191 static inline int from_bcd(uint8_t val)
193 return ((val >> 4) * 10) + (val & 0x0f);
196 static void menelaus_gpio_set(void *opaque, int line, int level)
198 MenelausState *s = (MenelausState *) opaque;
200 /* No interrupt generated */
201 s->inputs &= ~(1 << line);
202 s->inputs |= level << line;
205 static void menelaus_pwrbtn_set(void *opaque, int line, int level)
207 MenelausState *s = (MenelausState *) opaque;
209 if (!s->pwrbtn_state && level) {
210 s->status |= 1 << 11; /* PSHBTN */
211 menelaus_update(s);
213 s->pwrbtn_state = level;
216 #define MENELAUS_REV 0x01
217 #define MENELAUS_VCORE_CTRL1 0x02
218 #define MENELAUS_VCORE_CTRL2 0x03
219 #define MENELAUS_VCORE_CTRL3 0x04
220 #define MENELAUS_VCORE_CTRL4 0x05
221 #define MENELAUS_VCORE_CTRL5 0x06
222 #define MENELAUS_DCDC_CTRL1 0x07
223 #define MENELAUS_DCDC_CTRL2 0x08
224 #define MENELAUS_DCDC_CTRL3 0x09
225 #define MENELAUS_LDO_CTRL1 0x0a
226 #define MENELAUS_LDO_CTRL2 0x0b
227 #define MENELAUS_LDO_CTRL3 0x0c
228 #define MENELAUS_LDO_CTRL4 0x0d
229 #define MENELAUS_LDO_CTRL5 0x0e
230 #define MENELAUS_LDO_CTRL6 0x0f
231 #define MENELAUS_LDO_CTRL7 0x10
232 #define MENELAUS_LDO_CTRL8 0x11
233 #define MENELAUS_SLEEP_CTRL1 0x12
234 #define MENELAUS_SLEEP_CTRL2 0x13
235 #define MENELAUS_DEVICE_OFF 0x14
236 #define MENELAUS_OSC_CTRL 0x15
237 #define MENELAUS_DETECT_CTRL 0x16
238 #define MENELAUS_INT_MASK1 0x17
239 #define MENELAUS_INT_MASK2 0x18
240 #define MENELAUS_INT_STATUS1 0x19
241 #define MENELAUS_INT_STATUS2 0x1a
242 #define MENELAUS_INT_ACK1 0x1b
243 #define MENELAUS_INT_ACK2 0x1c
244 #define MENELAUS_GPIO_CTRL 0x1d
245 #define MENELAUS_GPIO_IN 0x1e
246 #define MENELAUS_GPIO_OUT 0x1f
247 #define MENELAUS_BBSMS 0x20
248 #define MENELAUS_RTC_CTRL 0x21
249 #define MENELAUS_RTC_UPDATE 0x22
250 #define MENELAUS_RTC_SEC 0x23
251 #define MENELAUS_RTC_MIN 0x24
252 #define MENELAUS_RTC_HR 0x25
253 #define MENELAUS_RTC_DAY 0x26
254 #define MENELAUS_RTC_MON 0x27
255 #define MENELAUS_RTC_YR 0x28
256 #define MENELAUS_RTC_WKDAY 0x29
257 #define MENELAUS_RTC_AL_SEC 0x2a
258 #define MENELAUS_RTC_AL_MIN 0x2b
259 #define MENELAUS_RTC_AL_HR 0x2c
260 #define MENELAUS_RTC_AL_DAY 0x2d
261 #define MENELAUS_RTC_AL_MON 0x2e
262 #define MENELAUS_RTC_AL_YR 0x2f
263 #define MENELAUS_RTC_COMP_MSB 0x30
264 #define MENELAUS_RTC_COMP_LSB 0x31
265 #define MENELAUS_S1_PULL_EN 0x32
266 #define MENELAUS_S1_PULL_DIR 0x33
267 #define MENELAUS_S2_PULL_EN 0x34
268 #define MENELAUS_S2_PULL_DIR 0x35
269 #define MENELAUS_MCT_CTRL1 0x36
270 #define MENELAUS_MCT_CTRL2 0x37
271 #define MENELAUS_MCT_CTRL3 0x38
272 #define MENELAUS_MCT_PIN_ST 0x39
273 #define MENELAUS_DEBOUNCE1 0x3a
275 static uint8_t menelaus_read(void *opaque, uint8_t addr)
277 MenelausState *s = (MenelausState *) opaque;
278 int reg = 0;
280 switch (addr) {
281 case MENELAUS_REV:
282 return 0x22;
284 case MENELAUS_VCORE_CTRL5: reg ++;
285 case MENELAUS_VCORE_CTRL4: reg ++;
286 case MENELAUS_VCORE_CTRL3: reg ++;
287 case MENELAUS_VCORE_CTRL2: reg ++;
288 case MENELAUS_VCORE_CTRL1:
289 return s->vcore[reg];
291 case MENELAUS_DCDC_CTRL3: reg ++;
292 case MENELAUS_DCDC_CTRL2: reg ++;
293 case MENELAUS_DCDC_CTRL1:
294 return s->dcdc[reg];
296 case MENELAUS_LDO_CTRL8: reg ++;
297 case MENELAUS_LDO_CTRL7: reg ++;
298 case MENELAUS_LDO_CTRL6: reg ++;
299 case MENELAUS_LDO_CTRL5: reg ++;
300 case MENELAUS_LDO_CTRL4: reg ++;
301 case MENELAUS_LDO_CTRL3: reg ++;
302 case MENELAUS_LDO_CTRL2: reg ++;
303 case MENELAUS_LDO_CTRL1:
304 return s->ldo[reg];
306 case MENELAUS_SLEEP_CTRL2: reg ++;
307 case MENELAUS_SLEEP_CTRL1:
308 return s->sleep[reg];
310 case MENELAUS_DEVICE_OFF:
311 return 0;
313 case MENELAUS_OSC_CTRL:
314 return s->osc | (1 << 7); /* CLK32K_GOOD */
316 case MENELAUS_DETECT_CTRL:
317 return s->detect;
319 case MENELAUS_INT_MASK1:
320 return (s->mask >> 0) & 0xff;
321 case MENELAUS_INT_MASK2:
322 return (s->mask >> 8) & 0xff;
324 case MENELAUS_INT_STATUS1:
325 return (s->status >> 0) & 0xff;
326 case MENELAUS_INT_STATUS2:
327 return (s->status >> 8) & 0xff;
329 case MENELAUS_INT_ACK1:
330 case MENELAUS_INT_ACK2:
331 return 0;
333 case MENELAUS_GPIO_CTRL:
334 return s->dir;
335 case MENELAUS_GPIO_IN:
336 return s->inputs | (~s->dir & s->outputs);
337 case MENELAUS_GPIO_OUT:
338 return s->outputs;
340 case MENELAUS_BBSMS:
341 return s->bbsms;
343 case MENELAUS_RTC_CTRL:
344 return s->rtc.ctrl;
345 case MENELAUS_RTC_UPDATE:
346 return 0x00;
347 case MENELAUS_RTC_SEC:
348 menelaus_rtc_update(s);
349 return to_bcd(s->rtc.tm.tm_sec);
350 case MENELAUS_RTC_MIN:
351 menelaus_rtc_update(s);
352 return to_bcd(s->rtc.tm.tm_min);
353 case MENELAUS_RTC_HR:
354 menelaus_rtc_update(s);
355 if ((s->rtc.ctrl >> 2) & 1) /* MODE12_n24 */
356 return to_bcd((s->rtc.tm.tm_hour % 12) + 1) |
357 (!!(s->rtc.tm.tm_hour >= 12) << 7); /* PM_nAM */
358 else
359 return to_bcd(s->rtc.tm.tm_hour);
360 case MENELAUS_RTC_DAY:
361 menelaus_rtc_update(s);
362 return to_bcd(s->rtc.tm.tm_mday);
363 case MENELAUS_RTC_MON:
364 menelaus_rtc_update(s);
365 return to_bcd(s->rtc.tm.tm_mon + 1);
366 case MENELAUS_RTC_YR:
367 menelaus_rtc_update(s);
368 return to_bcd(s->rtc.tm.tm_year - 2000);
369 case MENELAUS_RTC_WKDAY:
370 menelaus_rtc_update(s);
371 return to_bcd(s->rtc.tm.tm_wday);
372 case MENELAUS_RTC_AL_SEC:
373 return to_bcd(s->rtc.alm.tm_sec);
374 case MENELAUS_RTC_AL_MIN:
375 return to_bcd(s->rtc.alm.tm_min);
376 case MENELAUS_RTC_AL_HR:
377 if ((s->rtc.ctrl >> 2) & 1) /* MODE12_n24 */
378 return to_bcd((s->rtc.alm.tm_hour % 12) + 1) |
379 (!!(s->rtc.alm.tm_hour >= 12) << 7);/* AL_PM_nAM */
380 else
381 return to_bcd(s->rtc.alm.tm_hour);
382 case MENELAUS_RTC_AL_DAY:
383 return to_bcd(s->rtc.alm.tm_mday);
384 case MENELAUS_RTC_AL_MON:
385 return to_bcd(s->rtc.alm.tm_mon + 1);
386 case MENELAUS_RTC_AL_YR:
387 return to_bcd(s->rtc.alm.tm_year - 2000);
388 case MENELAUS_RTC_COMP_MSB:
389 return (s->rtc.comp >> 8) & 0xff;
390 case MENELAUS_RTC_COMP_LSB:
391 return (s->rtc.comp >> 0) & 0xff;
393 case MENELAUS_S1_PULL_EN:
394 return s->pull[0];
395 case MENELAUS_S1_PULL_DIR:
396 return s->pull[1];
397 case MENELAUS_S2_PULL_EN:
398 return s->pull[2];
399 case MENELAUS_S2_PULL_DIR:
400 return s->pull[3];
402 case MENELAUS_MCT_CTRL3: reg ++;
403 case MENELAUS_MCT_CTRL2: reg ++;
404 case MENELAUS_MCT_CTRL1:
405 return s->mmc_ctrl[reg];
406 case MENELAUS_MCT_PIN_ST:
407 /* TODO: return the real Card Detect */
408 return 0;
409 case MENELAUS_DEBOUNCE1:
410 return s->mmc_debounce;
412 default:
413 #ifdef VERBOSE
414 printf("%s: unknown register %02x\n", __FUNCTION__, addr);
415 #endif
416 break;
418 return 0;
421 static void menelaus_write(void *opaque, uint8_t addr, uint8_t value)
423 MenelausState *s = (MenelausState *) opaque;
424 int line;
425 int reg = 0;
426 struct tm tm;
428 switch (addr) {
429 case MENELAUS_VCORE_CTRL1:
430 s->vcore[0] = (value & 0xe) | MIN(value & 0x1f, 0x12);
431 break;
432 case MENELAUS_VCORE_CTRL2:
433 s->vcore[1] = value;
434 break;
435 case MENELAUS_VCORE_CTRL3:
436 s->vcore[2] = MIN(value & 0x1f, 0x12);
437 break;
438 case MENELAUS_VCORE_CTRL4:
439 s->vcore[3] = MIN(value & 0x1f, 0x12);
440 break;
441 case MENELAUS_VCORE_CTRL5:
442 s->vcore[4] = value & 3;
443 /* XXX
444 * auto set to 3 on M_Active, nRESWARM
445 * auto set to 0 on M_WaitOn, M_Backup
447 break;
449 case MENELAUS_DCDC_CTRL1:
450 s->dcdc[0] = value & 0x3f;
451 break;
452 case MENELAUS_DCDC_CTRL2:
453 s->dcdc[1] = value & 0x07;
454 /* XXX
455 * auto set to 3 on M_Active, nRESWARM
456 * auto set to 0 on M_WaitOn, M_Backup
458 break;
459 case MENELAUS_DCDC_CTRL3:
460 s->dcdc[2] = value & 0x07;
461 break;
463 case MENELAUS_LDO_CTRL1:
464 s->ldo[0] = value;
465 break;
466 case MENELAUS_LDO_CTRL2:
467 s->ldo[1] = value & 0x7f;
468 /* XXX
469 * auto set to 0x7e on M_WaitOn, M_Backup
471 break;
472 case MENELAUS_LDO_CTRL3:
473 s->ldo[2] = value & 3;
474 /* XXX
475 * auto set to 3 on M_Active, nRESWARM
476 * auto set to 0 on M_WaitOn, M_Backup
478 break;
479 case MENELAUS_LDO_CTRL4:
480 s->ldo[3] = value & 3;
481 /* XXX
482 * auto set to 3 on M_Active, nRESWARM
483 * auto set to 0 on M_WaitOn, M_Backup
485 break;
486 case MENELAUS_LDO_CTRL5:
487 s->ldo[4] = value & 3;
488 /* XXX
489 * auto set to 3 on M_Active, nRESWARM
490 * auto set to 0 on M_WaitOn, M_Backup
492 break;
493 case MENELAUS_LDO_CTRL6:
494 s->ldo[5] = value & 3;
495 break;
496 case MENELAUS_LDO_CTRL7:
497 s->ldo[6] = value & 3;
498 break;
499 case MENELAUS_LDO_CTRL8:
500 s->ldo[7] = value & 3;
501 break;
503 case MENELAUS_SLEEP_CTRL2: reg ++;
504 case MENELAUS_SLEEP_CTRL1:
505 s->sleep[reg] = value;
506 break;
508 case MENELAUS_DEVICE_OFF:
509 if (value & 1)
510 menelaus_reset(&s->i2c);
511 break;
513 case MENELAUS_OSC_CTRL:
514 s->osc = value & 7;
515 break;
517 case MENELAUS_DETECT_CTRL:
518 s->detect = value & 0x7f;
519 break;
521 case MENELAUS_INT_MASK1:
522 s->mask &= 0xf00;
523 s->mask |= value << 0;
524 menelaus_update(s);
525 break;
526 case MENELAUS_INT_MASK2:
527 s->mask &= 0x0ff;
528 s->mask |= value << 8;
529 menelaus_update(s);
530 break;
532 case MENELAUS_INT_ACK1:
533 s->status &= ~(((uint16_t) value) << 0);
534 menelaus_update(s);
535 break;
536 case MENELAUS_INT_ACK2:
537 s->status &= ~(((uint16_t) value) << 8);
538 menelaus_update(s);
539 break;
541 case MENELAUS_GPIO_CTRL:
542 for (line = 0; line < 3; line ++) {
543 if (((s->dir ^ value) >> line) & 1) {
544 qemu_set_irq(s->out[line],
545 ((s->outputs & ~s->dir) >> line) & 1);
548 s->dir = value & 0x67;
549 break;
550 case MENELAUS_GPIO_OUT:
551 for (line = 0; line < 3; line ++) {
552 if ((((s->outputs ^ value) & ~s->dir) >> line) & 1) {
553 qemu_set_irq(s->out[line], (s->outputs >> line) & 1);
556 s->outputs = value & 0x07;
557 break;
559 case MENELAUS_BBSMS:
560 s->bbsms = 0x0d;
561 break;
563 case MENELAUS_RTC_CTRL:
564 if ((s->rtc.ctrl ^ value) & 1) { /* RTC_EN */
565 if (value & 1)
566 menelaus_rtc_start(s);
567 else
568 menelaus_rtc_stop(s);
570 s->rtc.ctrl = value & 0x1f;
571 menelaus_alm_update(s);
572 break;
573 case MENELAUS_RTC_UPDATE:
574 menelaus_rtc_update(s);
575 memcpy(&tm, &s->rtc.tm, sizeof(tm));
576 switch (value & 0xf) {
577 case 0:
578 break;
579 case 1:
580 tm.tm_sec = s->rtc.new.tm_sec;
581 break;
582 case 2:
583 tm.tm_min = s->rtc.new.tm_min;
584 break;
585 case 3:
586 if (s->rtc.new.tm_hour > 23)
587 goto rtc_badness;
588 tm.tm_hour = s->rtc.new.tm_hour;
589 break;
590 case 4:
591 if (s->rtc.new.tm_mday < 1)
592 goto rtc_badness;
593 /* TODO check range */
594 tm.tm_mday = s->rtc.new.tm_mday;
595 break;
596 case 5:
597 if (s->rtc.new.tm_mon < 0 || s->rtc.new.tm_mon > 11)
598 goto rtc_badness;
599 tm.tm_mon = s->rtc.new.tm_mon;
600 break;
601 case 6:
602 tm.tm_year = s->rtc.new.tm_year;
603 break;
604 case 7:
605 /* TODO set .tm_mday instead */
606 tm.tm_wday = s->rtc.new.tm_wday;
607 break;
608 case 8:
609 if (s->rtc.new.tm_hour > 23)
610 goto rtc_badness;
611 if (s->rtc.new.tm_mday < 1)
612 goto rtc_badness;
613 if (s->rtc.new.tm_mon < 0 || s->rtc.new.tm_mon > 11)
614 goto rtc_badness;
615 tm.tm_sec = s->rtc.new.tm_sec;
616 tm.tm_min = s->rtc.new.tm_min;
617 tm.tm_hour = s->rtc.new.tm_hour;
618 tm.tm_mday = s->rtc.new.tm_mday;
619 tm.tm_mon = s->rtc.new.tm_mon;
620 tm.tm_year = s->rtc.new.tm_year;
621 break;
622 rtc_badness:
623 default:
624 fprintf(stderr, "%s: bad RTC_UPDATE value %02x\n",
625 __FUNCTION__, value);
626 s->status |= 1 << 10; /* RTCERR */
627 menelaus_update(s);
629 s->rtc.sec_offset = qemu_timedate_diff(&tm);
630 break;
631 case MENELAUS_RTC_SEC:
632 s->rtc.tm.tm_sec = from_bcd(value & 0x7f);
633 break;
634 case MENELAUS_RTC_MIN:
635 s->rtc.tm.tm_min = from_bcd(value & 0x7f);
636 break;
637 case MENELAUS_RTC_HR:
638 s->rtc.tm.tm_hour = (s->rtc.ctrl & (1 << 2)) ? /* MODE12_n24 */
639 MIN(from_bcd(value & 0x3f), 12) + ((value >> 7) ? 11 : -1) :
640 from_bcd(value & 0x3f);
641 break;
642 case MENELAUS_RTC_DAY:
643 s->rtc.tm.tm_mday = from_bcd(value);
644 break;
645 case MENELAUS_RTC_MON:
646 s->rtc.tm.tm_mon = MAX(1, from_bcd(value)) - 1;
647 break;
648 case MENELAUS_RTC_YR:
649 s->rtc.tm.tm_year = 2000 + from_bcd(value);
650 break;
651 case MENELAUS_RTC_WKDAY:
652 s->rtc.tm.tm_mday = from_bcd(value);
653 break;
654 case MENELAUS_RTC_AL_SEC:
655 s->rtc.alm.tm_sec = from_bcd(value & 0x7f);
656 menelaus_alm_update(s);
657 break;
658 case MENELAUS_RTC_AL_MIN:
659 s->rtc.alm.tm_min = from_bcd(value & 0x7f);
660 menelaus_alm_update(s);
661 break;
662 case MENELAUS_RTC_AL_HR:
663 s->rtc.alm.tm_hour = (s->rtc.ctrl & (1 << 2)) ? /* MODE12_n24 */
664 MIN(from_bcd(value & 0x3f), 12) + ((value >> 7) ? 11 : -1) :
665 from_bcd(value & 0x3f);
666 menelaus_alm_update(s);
667 break;
668 case MENELAUS_RTC_AL_DAY:
669 s->rtc.alm.tm_mday = from_bcd(value);
670 menelaus_alm_update(s);
671 break;
672 case MENELAUS_RTC_AL_MON:
673 s->rtc.alm.tm_mon = MAX(1, from_bcd(value)) - 1;
674 menelaus_alm_update(s);
675 break;
676 case MENELAUS_RTC_AL_YR:
677 s->rtc.alm.tm_year = 2000 + from_bcd(value);
678 menelaus_alm_update(s);
679 break;
680 case MENELAUS_RTC_COMP_MSB:
681 s->rtc.comp &= 0xff;
682 s->rtc.comp |= value << 8;
683 break;
684 case MENELAUS_RTC_COMP_LSB:
685 s->rtc.comp &= 0xff << 8;
686 s->rtc.comp |= value;
687 break;
689 case MENELAUS_S1_PULL_EN:
690 s->pull[0] = value;
691 break;
692 case MENELAUS_S1_PULL_DIR:
693 s->pull[1] = value & 0x1f;
694 break;
695 case MENELAUS_S2_PULL_EN:
696 s->pull[2] = value;
697 break;
698 case MENELAUS_S2_PULL_DIR:
699 s->pull[3] = value & 0x1f;
700 break;
702 case MENELAUS_MCT_CTRL1:
703 s->mmc_ctrl[0] = value & 0x7f;
704 break;
705 case MENELAUS_MCT_CTRL2:
706 s->mmc_ctrl[1] = value;
707 /* TODO update Card Detect interrupts */
708 break;
709 case MENELAUS_MCT_CTRL3:
710 s->mmc_ctrl[2] = value & 0xf;
711 break;
712 case MENELAUS_DEBOUNCE1:
713 s->mmc_debounce = value & 0x3f;
714 break;
716 default:
717 #ifdef VERBOSE
718 printf("%s: unknown register %02x\n", __FUNCTION__, addr);
719 #endif
723 static void menelaus_event(i2c_slave *i2c, enum i2c_event event)
725 MenelausState *s = (MenelausState *) i2c;
727 if (event == I2C_START_SEND)
728 s->firstbyte = 1;
731 static int menelaus_tx(i2c_slave *i2c, uint8_t data)
733 MenelausState *s = (MenelausState *) i2c;
734 /* Interpret register address byte */
735 if (s->firstbyte) {
736 s->reg = data;
737 s->firstbyte = 0;
738 } else
739 menelaus_write(s, s->reg ++, data);
741 return 0;
744 static int menelaus_rx(i2c_slave *i2c)
746 MenelausState *s = (MenelausState *) i2c;
748 return menelaus_read(s, s->reg ++);
751 /* Save restore 32 bit int as uint16_t
752 This is a Big hack, but it is how the old state did it.
753 Or we broke compatibility in the state, or we can't use struct tm
756 static int get_int32_as_uint16(QEMUFile *f, void *pv, size_t size)
758 int *v = pv;
759 *v = qemu_get_be16(f);
760 return 0;
763 static void put_int32_as_uint16(QEMUFile *f, void *pv, size_t size)
765 int *v = pv;
766 qemu_put_be16(f, *v);
769 const VMStateInfo vmstate_hack_int32_as_uint16 = {
770 .name = "int32_as_uint16",
771 .get = get_int32_as_uint16,
772 .put = put_int32_as_uint16,
775 #define VMSTATE_UINT16_HACK(_f, _s) \
776 VMSTATE_SINGLE(_f, _s, 0, vmstate_hack_int32_as_uint16, int32_t)
779 static const VMStateDescription vmstate_menelaus_tm = {
780 .name = "menelaus_tm",
781 .version_id = 0,
782 .minimum_version_id = 0,
783 .minimum_version_id_old = 0,
784 .fields = (VMStateField []) {
785 VMSTATE_UINT16_HACK(tm_sec, struct tm),
786 VMSTATE_UINT16_HACK(tm_min, struct tm),
787 VMSTATE_UINT16_HACK(tm_hour, struct tm),
788 VMSTATE_UINT16_HACK(tm_mday, struct tm),
789 VMSTATE_UINT16_HACK(tm_min, struct tm),
790 VMSTATE_UINT16_HACK(tm_year, struct tm),
791 VMSTATE_END_OF_LIST()
795 static void menelaus_pre_save(void *opaque)
797 MenelausState *s = opaque;
798 /* Should be <= 1000 */
799 s->rtc_next_vmstate = s->rtc.next - qemu_get_clock(rt_clock);
802 static int menelaus_post_load(void *opaque, int version_id)
804 MenelausState *s = opaque;
806 if (s->rtc.ctrl & 1) /* RTC_EN */
807 menelaus_rtc_stop(s);
809 s->rtc.next = s->rtc_next_vmstate;
811 menelaus_alm_update(s);
812 menelaus_update(s);
813 if (s->rtc.ctrl & 1) /* RTC_EN */
814 menelaus_rtc_start(s);
815 return 0;
818 static const VMStateDescription vmstate_menelaus = {
819 .name = "menelaus",
820 .version_id = 0,
821 .minimum_version_id = 0,
822 .minimum_version_id_old = 0,
823 .pre_save = menelaus_pre_save,
824 .post_load = menelaus_post_load,
825 .fields = (VMStateField []) {
826 VMSTATE_INT32(firstbyte, MenelausState),
827 VMSTATE_UINT8(reg, MenelausState),
828 VMSTATE_UINT8_ARRAY(vcore, MenelausState, 5),
829 VMSTATE_UINT8_ARRAY(dcdc, MenelausState, 3),
830 VMSTATE_UINT8_ARRAY(ldo, MenelausState, 8),
831 VMSTATE_UINT8_ARRAY(sleep, MenelausState, 2),
832 VMSTATE_UINT8(osc, MenelausState),
833 VMSTATE_UINT8(detect, MenelausState),
834 VMSTATE_UINT16(mask, MenelausState),
835 VMSTATE_UINT16(status, MenelausState),
836 VMSTATE_UINT8(dir, MenelausState),
837 VMSTATE_UINT8(inputs, MenelausState),
838 VMSTATE_UINT8(outputs, MenelausState),
839 VMSTATE_UINT8(bbsms, MenelausState),
840 VMSTATE_UINT8_ARRAY(pull, MenelausState, 4),
841 VMSTATE_UINT8_ARRAY(mmc_ctrl, MenelausState, 3),
842 VMSTATE_UINT8(mmc_debounce, MenelausState),
843 VMSTATE_UINT8(rtc.ctrl, MenelausState),
844 VMSTATE_UINT16(rtc.comp, MenelausState),
845 VMSTATE_UINT16(rtc_next_vmstate, MenelausState),
846 VMSTATE_STRUCT(rtc.new, MenelausState, 0, vmstate_menelaus_tm,
847 struct tm),
848 VMSTATE_STRUCT(rtc.alm, MenelausState, 0, vmstate_menelaus_tm,
849 struct tm),
850 VMSTATE_UINT8(pwrbtn_state, MenelausState),
851 VMSTATE_I2C_SLAVE(i2c, MenelausState),
852 VMSTATE_END_OF_LIST()
856 static int twl92230_init(i2c_slave *i2c)
858 MenelausState *s = FROM_I2C_SLAVE(MenelausState, i2c);
860 s->rtc.hz_tm = qemu_new_timer(rt_clock, menelaus_rtc_hz, s);
861 /* Three output pins plus one interrupt pin. */
862 qdev_init_gpio_out(&i2c->qdev, s->out, 4);
863 qdev_init_gpio_in(&i2c->qdev, menelaus_gpio_set, 3);
864 s->pwrbtn = qemu_allocate_irqs(menelaus_pwrbtn_set, s, 1)[0];
866 menelaus_reset(&s->i2c);
868 vmstate_register(-1, &vmstate_menelaus, s);
869 return 0;
872 static I2CSlaveInfo twl92230_info = {
873 .qdev.name ="twl92230",
874 .qdev.size = sizeof(MenelausState),
875 .init = twl92230_init,
876 .event = menelaus_event,
877 .recv = menelaus_rx,
878 .send = menelaus_tx
881 static void twl92230_register_devices(void)
883 i2c_register_slave(&twl92230_info);
886 device_init(twl92230_register_devices)