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/>.
23 #include "qemu-timer.h"
62 uint16_t rtc_next_vmstate
;
67 static inline void menelaus_update(MenelausState
*s
)
69 qemu_set_irq(s
->out
[3], s
->status
& ~s
->mask
);
72 static inline void menelaus_rtc_start(MenelausState
*s
)
74 s
->rtc
.next
+= qemu_get_clock_ms(rt_clock
);
75 qemu_mod_timer(s
->rtc
.hz_tm
, s
->rtc
.next
);
78 static inline void menelaus_rtc_stop(MenelausState
*s
)
80 qemu_del_timer(s
->rtc
.hz_tm
);
81 s
->rtc
.next
-= qemu_get_clock_ms(rt_clock
);
86 static void menelaus_rtc_update(MenelausState
*s
)
88 qemu_get_timedate(&s
->rtc
.tm
, s
->rtc
.sec_offset
);
91 static void menelaus_alm_update(MenelausState
*s
)
93 if ((s
->rtc
.ctrl
& 3) == 3)
94 s
->rtc
.alm_sec
= qemu_timedate_diff(&s
->rtc
.alm
) - s
->rtc
.sec_offset
;
97 static void menelaus_rtc_hz(void *opaque
)
99 MenelausState
*s
= (MenelausState
*) opaque
;
104 qemu_mod_timer(s
->rtc
.hz_tm
, s
->rtc
.next
);
105 if ((s
->rtc
.ctrl
>> 3) & 3) { /* EVERY */
106 menelaus_rtc_update(s
);
107 if (((s
->rtc
.ctrl
>> 3) & 3) == 1 && !s
->rtc
.tm
.tm_sec
)
108 s
->status
|= 1 << 8; /* RTCTMR */
109 else if (((s
->rtc
.ctrl
>> 3) & 3) == 2 && !s
->rtc
.tm
.tm_min
)
110 s
->status
|= 1 << 8; /* RTCTMR */
111 else if (!s
->rtc
.tm
.tm_hour
)
112 s
->status
|= 1 << 8; /* RTCTMR */
114 s
->status
|= 1 << 8; /* RTCTMR */
115 if ((s
->rtc
.ctrl
>> 1) & 1) { /* RTC_AL_EN */
116 if (s
->rtc
.alm_sec
== 0)
117 s
->status
|= 1 << 9; /* RTCALM */
120 if (s
->rtc
.next_comp
<= 0) {
121 s
->rtc
.next
-= muldiv64((int16_t) s
->rtc
.comp
, 1000, 0x8000);
122 s
->rtc
.next_comp
= 3600;
127 static void menelaus_reset(I2CSlave
*i2c
)
129 MenelausState
*s
= (MenelausState
*) i2c
;
132 s
->vcore
[0] = 0x0c; /* XXX: X-loader needs 0x8c? check! */
137 s
->dcdc
[0] = 0x33; /* Depends on wiring */
143 s
->ldo
[3] = 0x00; /* Depends on wiring */
144 s
->ldo
[4] = 0x03; /* Depends on wiring */
161 s
->mmc_ctrl
[0] = 0x03;
162 s
->mmc_ctrl
[1] = 0xc0;
163 s
->mmc_ctrl
[2] = 0x00;
164 s
->mmc_debounce
= 0x05;
167 menelaus_rtc_stop(s
);
169 s
->rtc
.comp
= 0x0000;
171 s
->rtc
.sec_offset
= 0;
172 s
->rtc
.next_comp
= 1800;
173 s
->rtc
.alm_sec
= 1800;
174 s
->rtc
.alm
.tm_sec
= 0x00;
175 s
->rtc
.alm
.tm_min
= 0x00;
176 s
->rtc
.alm
.tm_hour
= 0x00;
177 s
->rtc
.alm
.tm_mday
= 0x01;
178 s
->rtc
.alm
.tm_mon
= 0x00;
179 s
->rtc
.alm
.tm_year
= 2004;
183 static void menelaus_gpio_set(void *opaque
, int line
, int level
)
185 MenelausState
*s
= (MenelausState
*) opaque
;
188 /* No interrupt generated */
189 s
->inputs
&= ~(1 << line
);
190 s
->inputs
|= level
<< line
;
194 if (!s
->pwrbtn_state
&& level
) {
195 s
->status
|= 1 << 11; /* PSHBTN */
198 s
->pwrbtn_state
= level
;
201 #define MENELAUS_REV 0x01
202 #define MENELAUS_VCORE_CTRL1 0x02
203 #define MENELAUS_VCORE_CTRL2 0x03
204 #define MENELAUS_VCORE_CTRL3 0x04
205 #define MENELAUS_VCORE_CTRL4 0x05
206 #define MENELAUS_VCORE_CTRL5 0x06
207 #define MENELAUS_DCDC_CTRL1 0x07
208 #define MENELAUS_DCDC_CTRL2 0x08
209 #define MENELAUS_DCDC_CTRL3 0x09
210 #define MENELAUS_LDO_CTRL1 0x0a
211 #define MENELAUS_LDO_CTRL2 0x0b
212 #define MENELAUS_LDO_CTRL3 0x0c
213 #define MENELAUS_LDO_CTRL4 0x0d
214 #define MENELAUS_LDO_CTRL5 0x0e
215 #define MENELAUS_LDO_CTRL6 0x0f
216 #define MENELAUS_LDO_CTRL7 0x10
217 #define MENELAUS_LDO_CTRL8 0x11
218 #define MENELAUS_SLEEP_CTRL1 0x12
219 #define MENELAUS_SLEEP_CTRL2 0x13
220 #define MENELAUS_DEVICE_OFF 0x14
221 #define MENELAUS_OSC_CTRL 0x15
222 #define MENELAUS_DETECT_CTRL 0x16
223 #define MENELAUS_INT_MASK1 0x17
224 #define MENELAUS_INT_MASK2 0x18
225 #define MENELAUS_INT_STATUS1 0x19
226 #define MENELAUS_INT_STATUS2 0x1a
227 #define MENELAUS_INT_ACK1 0x1b
228 #define MENELAUS_INT_ACK2 0x1c
229 #define MENELAUS_GPIO_CTRL 0x1d
230 #define MENELAUS_GPIO_IN 0x1e
231 #define MENELAUS_GPIO_OUT 0x1f
232 #define MENELAUS_BBSMS 0x20
233 #define MENELAUS_RTC_CTRL 0x21
234 #define MENELAUS_RTC_UPDATE 0x22
235 #define MENELAUS_RTC_SEC 0x23
236 #define MENELAUS_RTC_MIN 0x24
237 #define MENELAUS_RTC_HR 0x25
238 #define MENELAUS_RTC_DAY 0x26
239 #define MENELAUS_RTC_MON 0x27
240 #define MENELAUS_RTC_YR 0x28
241 #define MENELAUS_RTC_WKDAY 0x29
242 #define MENELAUS_RTC_AL_SEC 0x2a
243 #define MENELAUS_RTC_AL_MIN 0x2b
244 #define MENELAUS_RTC_AL_HR 0x2c
245 #define MENELAUS_RTC_AL_DAY 0x2d
246 #define MENELAUS_RTC_AL_MON 0x2e
247 #define MENELAUS_RTC_AL_YR 0x2f
248 #define MENELAUS_RTC_COMP_MSB 0x30
249 #define MENELAUS_RTC_COMP_LSB 0x31
250 #define MENELAUS_S1_PULL_EN 0x32
251 #define MENELAUS_S1_PULL_DIR 0x33
252 #define MENELAUS_S2_PULL_EN 0x34
253 #define MENELAUS_S2_PULL_DIR 0x35
254 #define MENELAUS_MCT_CTRL1 0x36
255 #define MENELAUS_MCT_CTRL2 0x37
256 #define MENELAUS_MCT_CTRL3 0x38
257 #define MENELAUS_MCT_PIN_ST 0x39
258 #define MENELAUS_DEBOUNCE1 0x3a
260 static uint8_t menelaus_read(void *opaque
, uint8_t addr
)
262 MenelausState
*s
= (MenelausState
*) opaque
;
269 case MENELAUS_VCORE_CTRL5
: reg
++;
270 case MENELAUS_VCORE_CTRL4
: reg
++;
271 case MENELAUS_VCORE_CTRL3
: reg
++;
272 case MENELAUS_VCORE_CTRL2
: reg
++;
273 case MENELAUS_VCORE_CTRL1
:
274 return s
->vcore
[reg
];
276 case MENELAUS_DCDC_CTRL3
: reg
++;
277 case MENELAUS_DCDC_CTRL2
: reg
++;
278 case MENELAUS_DCDC_CTRL1
:
281 case MENELAUS_LDO_CTRL8
: reg
++;
282 case MENELAUS_LDO_CTRL7
: reg
++;
283 case MENELAUS_LDO_CTRL6
: reg
++;
284 case MENELAUS_LDO_CTRL5
: reg
++;
285 case MENELAUS_LDO_CTRL4
: reg
++;
286 case MENELAUS_LDO_CTRL3
: reg
++;
287 case MENELAUS_LDO_CTRL2
: reg
++;
288 case MENELAUS_LDO_CTRL1
:
291 case MENELAUS_SLEEP_CTRL2
: reg
++;
292 case MENELAUS_SLEEP_CTRL1
:
293 return s
->sleep
[reg
];
295 case MENELAUS_DEVICE_OFF
:
298 case MENELAUS_OSC_CTRL
:
299 return s
->osc
| (1 << 7); /* CLK32K_GOOD */
301 case MENELAUS_DETECT_CTRL
:
304 case MENELAUS_INT_MASK1
:
305 return (s
->mask
>> 0) & 0xff;
306 case MENELAUS_INT_MASK2
:
307 return (s
->mask
>> 8) & 0xff;
309 case MENELAUS_INT_STATUS1
:
310 return (s
->status
>> 0) & 0xff;
311 case MENELAUS_INT_STATUS2
:
312 return (s
->status
>> 8) & 0xff;
314 case MENELAUS_INT_ACK1
:
315 case MENELAUS_INT_ACK2
:
318 case MENELAUS_GPIO_CTRL
:
320 case MENELAUS_GPIO_IN
:
321 return s
->inputs
| (~s
->dir
& s
->outputs
);
322 case MENELAUS_GPIO_OUT
:
328 case MENELAUS_RTC_CTRL
:
330 case MENELAUS_RTC_UPDATE
:
332 case MENELAUS_RTC_SEC
:
333 menelaus_rtc_update(s
);
334 return to_bcd(s
->rtc
.tm
.tm_sec
);
335 case MENELAUS_RTC_MIN
:
336 menelaus_rtc_update(s
);
337 return to_bcd(s
->rtc
.tm
.tm_min
);
338 case MENELAUS_RTC_HR
:
339 menelaus_rtc_update(s
);
340 if ((s
->rtc
.ctrl
>> 2) & 1) /* MODE12_n24 */
341 return to_bcd((s
->rtc
.tm
.tm_hour
% 12) + 1) |
342 (!!(s
->rtc
.tm
.tm_hour
>= 12) << 7); /* PM_nAM */
344 return to_bcd(s
->rtc
.tm
.tm_hour
);
345 case MENELAUS_RTC_DAY
:
346 menelaus_rtc_update(s
);
347 return to_bcd(s
->rtc
.tm
.tm_mday
);
348 case MENELAUS_RTC_MON
:
349 menelaus_rtc_update(s
);
350 return to_bcd(s
->rtc
.tm
.tm_mon
+ 1);
351 case MENELAUS_RTC_YR
:
352 menelaus_rtc_update(s
);
353 return to_bcd(s
->rtc
.tm
.tm_year
- 2000);
354 case MENELAUS_RTC_WKDAY
:
355 menelaus_rtc_update(s
);
356 return to_bcd(s
->rtc
.tm
.tm_wday
);
357 case MENELAUS_RTC_AL_SEC
:
358 return to_bcd(s
->rtc
.alm
.tm_sec
);
359 case MENELAUS_RTC_AL_MIN
:
360 return to_bcd(s
->rtc
.alm
.tm_min
);
361 case MENELAUS_RTC_AL_HR
:
362 if ((s
->rtc
.ctrl
>> 2) & 1) /* MODE12_n24 */
363 return to_bcd((s
->rtc
.alm
.tm_hour
% 12) + 1) |
364 (!!(s
->rtc
.alm
.tm_hour
>= 12) << 7);/* AL_PM_nAM */
366 return to_bcd(s
->rtc
.alm
.tm_hour
);
367 case MENELAUS_RTC_AL_DAY
:
368 return to_bcd(s
->rtc
.alm
.tm_mday
);
369 case MENELAUS_RTC_AL_MON
:
370 return to_bcd(s
->rtc
.alm
.tm_mon
+ 1);
371 case MENELAUS_RTC_AL_YR
:
372 return to_bcd(s
->rtc
.alm
.tm_year
- 2000);
373 case MENELAUS_RTC_COMP_MSB
:
374 return (s
->rtc
.comp
>> 8) & 0xff;
375 case MENELAUS_RTC_COMP_LSB
:
376 return (s
->rtc
.comp
>> 0) & 0xff;
378 case MENELAUS_S1_PULL_EN
:
380 case MENELAUS_S1_PULL_DIR
:
382 case MENELAUS_S2_PULL_EN
:
384 case MENELAUS_S2_PULL_DIR
:
387 case MENELAUS_MCT_CTRL3
: reg
++;
388 case MENELAUS_MCT_CTRL2
: reg
++;
389 case MENELAUS_MCT_CTRL1
:
390 return s
->mmc_ctrl
[reg
];
391 case MENELAUS_MCT_PIN_ST
:
392 /* TODO: return the real Card Detect */
394 case MENELAUS_DEBOUNCE1
:
395 return s
->mmc_debounce
;
399 printf("%s: unknown register %02x\n", __FUNCTION__
, addr
);
406 static void menelaus_write(void *opaque
, uint8_t addr
, uint8_t value
)
408 MenelausState
*s
= (MenelausState
*) opaque
;
414 case MENELAUS_VCORE_CTRL1
:
415 s
->vcore
[0] = (value
& 0xe) | MIN(value
& 0x1f, 0x12);
417 case MENELAUS_VCORE_CTRL2
:
420 case MENELAUS_VCORE_CTRL3
:
421 s
->vcore
[2] = MIN(value
& 0x1f, 0x12);
423 case MENELAUS_VCORE_CTRL4
:
424 s
->vcore
[3] = MIN(value
& 0x1f, 0x12);
426 case MENELAUS_VCORE_CTRL5
:
427 s
->vcore
[4] = value
& 3;
429 * auto set to 3 on M_Active, nRESWARM
430 * auto set to 0 on M_WaitOn, M_Backup
434 case MENELAUS_DCDC_CTRL1
:
435 s
->dcdc
[0] = value
& 0x3f;
437 case MENELAUS_DCDC_CTRL2
:
438 s
->dcdc
[1] = value
& 0x07;
440 * auto set to 3 on M_Active, nRESWARM
441 * auto set to 0 on M_WaitOn, M_Backup
444 case MENELAUS_DCDC_CTRL3
:
445 s
->dcdc
[2] = value
& 0x07;
448 case MENELAUS_LDO_CTRL1
:
451 case MENELAUS_LDO_CTRL2
:
452 s
->ldo
[1] = value
& 0x7f;
454 * auto set to 0x7e on M_WaitOn, M_Backup
457 case MENELAUS_LDO_CTRL3
:
458 s
->ldo
[2] = value
& 3;
460 * auto set to 3 on M_Active, nRESWARM
461 * auto set to 0 on M_WaitOn, M_Backup
464 case MENELAUS_LDO_CTRL4
:
465 s
->ldo
[3] = value
& 3;
467 * auto set to 3 on M_Active, nRESWARM
468 * auto set to 0 on M_WaitOn, M_Backup
471 case MENELAUS_LDO_CTRL5
:
472 s
->ldo
[4] = value
& 3;
474 * auto set to 3 on M_Active, nRESWARM
475 * auto set to 0 on M_WaitOn, M_Backup
478 case MENELAUS_LDO_CTRL6
:
479 s
->ldo
[5] = value
& 3;
481 case MENELAUS_LDO_CTRL7
:
482 s
->ldo
[6] = value
& 3;
484 case MENELAUS_LDO_CTRL8
:
485 s
->ldo
[7] = value
& 3;
488 case MENELAUS_SLEEP_CTRL2
: reg
++;
489 case MENELAUS_SLEEP_CTRL1
:
490 s
->sleep
[reg
] = value
;
493 case MENELAUS_DEVICE_OFF
:
495 menelaus_reset(&s
->i2c
);
498 case MENELAUS_OSC_CTRL
:
502 case MENELAUS_DETECT_CTRL
:
503 s
->detect
= value
& 0x7f;
506 case MENELAUS_INT_MASK1
:
508 s
->mask
|= value
<< 0;
511 case MENELAUS_INT_MASK2
:
513 s
->mask
|= value
<< 8;
517 case MENELAUS_INT_ACK1
:
518 s
->status
&= ~(((uint16_t) value
) << 0);
521 case MENELAUS_INT_ACK2
:
522 s
->status
&= ~(((uint16_t) value
) << 8);
526 case MENELAUS_GPIO_CTRL
:
527 for (line
= 0; line
< 3; line
++) {
528 if (((s
->dir
^ value
) >> line
) & 1) {
529 qemu_set_irq(s
->out
[line
],
530 ((s
->outputs
& ~s
->dir
) >> line
) & 1);
533 s
->dir
= value
& 0x67;
535 case MENELAUS_GPIO_OUT
:
536 for (line
= 0; line
< 3; line
++) {
537 if ((((s
->outputs
^ value
) & ~s
->dir
) >> line
) & 1) {
538 qemu_set_irq(s
->out
[line
], (s
->outputs
>> line
) & 1);
541 s
->outputs
= value
& 0x07;
548 case MENELAUS_RTC_CTRL
:
549 if ((s
->rtc
.ctrl
^ value
) & 1) { /* RTC_EN */
551 menelaus_rtc_start(s
);
553 menelaus_rtc_stop(s
);
555 s
->rtc
.ctrl
= value
& 0x1f;
556 menelaus_alm_update(s
);
558 case MENELAUS_RTC_UPDATE
:
559 menelaus_rtc_update(s
);
560 memcpy(&tm
, &s
->rtc
.tm
, sizeof(tm
));
561 switch (value
& 0xf) {
565 tm
.tm_sec
= s
->rtc
.new.tm_sec
;
568 tm
.tm_min
= s
->rtc
.new.tm_min
;
571 if (s
->rtc
.new.tm_hour
> 23)
573 tm
.tm_hour
= s
->rtc
.new.tm_hour
;
576 if (s
->rtc
.new.tm_mday
< 1)
578 /* TODO check range */
579 tm
.tm_mday
= s
->rtc
.new.tm_mday
;
582 if (s
->rtc
.new.tm_mon
< 0 || s
->rtc
.new.tm_mon
> 11)
584 tm
.tm_mon
= s
->rtc
.new.tm_mon
;
587 tm
.tm_year
= s
->rtc
.new.tm_year
;
590 /* TODO set .tm_mday instead */
591 tm
.tm_wday
= s
->rtc
.new.tm_wday
;
594 if (s
->rtc
.new.tm_hour
> 23)
596 if (s
->rtc
.new.tm_mday
< 1)
598 if (s
->rtc
.new.tm_mon
< 0 || s
->rtc
.new.tm_mon
> 11)
600 tm
.tm_sec
= s
->rtc
.new.tm_sec
;
601 tm
.tm_min
= s
->rtc
.new.tm_min
;
602 tm
.tm_hour
= s
->rtc
.new.tm_hour
;
603 tm
.tm_mday
= s
->rtc
.new.tm_mday
;
604 tm
.tm_mon
= s
->rtc
.new.tm_mon
;
605 tm
.tm_year
= s
->rtc
.new.tm_year
;
609 fprintf(stderr
, "%s: bad RTC_UPDATE value %02x\n",
610 __FUNCTION__
, value
);
611 s
->status
|= 1 << 10; /* RTCERR */
614 s
->rtc
.sec_offset
= qemu_timedate_diff(&tm
);
616 case MENELAUS_RTC_SEC
:
617 s
->rtc
.tm
.tm_sec
= from_bcd(value
& 0x7f);
619 case MENELAUS_RTC_MIN
:
620 s
->rtc
.tm
.tm_min
= from_bcd(value
& 0x7f);
622 case MENELAUS_RTC_HR
:
623 s
->rtc
.tm
.tm_hour
= (s
->rtc
.ctrl
& (1 << 2)) ? /* MODE12_n24 */
624 MIN(from_bcd(value
& 0x3f), 12) + ((value
>> 7) ? 11 : -1) :
625 from_bcd(value
& 0x3f);
627 case MENELAUS_RTC_DAY
:
628 s
->rtc
.tm
.tm_mday
= from_bcd(value
);
630 case MENELAUS_RTC_MON
:
631 s
->rtc
.tm
.tm_mon
= MAX(1, from_bcd(value
)) - 1;
633 case MENELAUS_RTC_YR
:
634 s
->rtc
.tm
.tm_year
= 2000 + from_bcd(value
);
636 case MENELAUS_RTC_WKDAY
:
637 s
->rtc
.tm
.tm_mday
= from_bcd(value
);
639 case MENELAUS_RTC_AL_SEC
:
640 s
->rtc
.alm
.tm_sec
= from_bcd(value
& 0x7f);
641 menelaus_alm_update(s
);
643 case MENELAUS_RTC_AL_MIN
:
644 s
->rtc
.alm
.tm_min
= from_bcd(value
& 0x7f);
645 menelaus_alm_update(s
);
647 case MENELAUS_RTC_AL_HR
:
648 s
->rtc
.alm
.tm_hour
= (s
->rtc
.ctrl
& (1 << 2)) ? /* MODE12_n24 */
649 MIN(from_bcd(value
& 0x3f), 12) + ((value
>> 7) ? 11 : -1) :
650 from_bcd(value
& 0x3f);
651 menelaus_alm_update(s
);
653 case MENELAUS_RTC_AL_DAY
:
654 s
->rtc
.alm
.tm_mday
= from_bcd(value
);
655 menelaus_alm_update(s
);
657 case MENELAUS_RTC_AL_MON
:
658 s
->rtc
.alm
.tm_mon
= MAX(1, from_bcd(value
)) - 1;
659 menelaus_alm_update(s
);
661 case MENELAUS_RTC_AL_YR
:
662 s
->rtc
.alm
.tm_year
= 2000 + from_bcd(value
);
663 menelaus_alm_update(s
);
665 case MENELAUS_RTC_COMP_MSB
:
667 s
->rtc
.comp
|= value
<< 8;
669 case MENELAUS_RTC_COMP_LSB
:
670 s
->rtc
.comp
&= 0xff << 8;
671 s
->rtc
.comp
|= value
;
674 case MENELAUS_S1_PULL_EN
:
677 case MENELAUS_S1_PULL_DIR
:
678 s
->pull
[1] = value
& 0x1f;
680 case MENELAUS_S2_PULL_EN
:
683 case MENELAUS_S2_PULL_DIR
:
684 s
->pull
[3] = value
& 0x1f;
687 case MENELAUS_MCT_CTRL1
:
688 s
->mmc_ctrl
[0] = value
& 0x7f;
690 case MENELAUS_MCT_CTRL2
:
691 s
->mmc_ctrl
[1] = value
;
692 /* TODO update Card Detect interrupts */
694 case MENELAUS_MCT_CTRL3
:
695 s
->mmc_ctrl
[2] = value
& 0xf;
697 case MENELAUS_DEBOUNCE1
:
698 s
->mmc_debounce
= value
& 0x3f;
703 printf("%s: unknown register %02x\n", __FUNCTION__
, addr
);
708 static void menelaus_event(I2CSlave
*i2c
, enum i2c_event event
)
710 MenelausState
*s
= (MenelausState
*) i2c
;
712 if (event
== I2C_START_SEND
)
716 static int menelaus_tx(I2CSlave
*i2c
, uint8_t data
)
718 MenelausState
*s
= (MenelausState
*) i2c
;
719 /* Interpret register address byte */
724 menelaus_write(s
, s
->reg
++, data
);
729 static int menelaus_rx(I2CSlave
*i2c
)
731 MenelausState
*s
= (MenelausState
*) i2c
;
733 return menelaus_read(s
, s
->reg
++);
736 /* Save restore 32 bit int as uint16_t
737 This is a Big hack, but it is how the old state did it.
738 Or we broke compatibility in the state, or we can't use struct tm
741 static int get_int32_as_uint16(QEMUFile
*f
, void *pv
, size_t size
)
744 *v
= qemu_get_be16(f
);
748 static void put_int32_as_uint16(QEMUFile
*f
, void *pv
, size_t size
)
751 qemu_put_be16(f
, *v
);
754 static const VMStateInfo vmstate_hack_int32_as_uint16
= {
755 .name
= "int32_as_uint16",
756 .get
= get_int32_as_uint16
,
757 .put
= put_int32_as_uint16
,
760 #define VMSTATE_UINT16_HACK(_f, _s) \
761 VMSTATE_SINGLE(_f, _s, 0, vmstate_hack_int32_as_uint16, int32_t)
764 static const VMStateDescription vmstate_menelaus_tm
= {
765 .name
= "menelaus_tm",
767 .minimum_version_id
= 0,
768 .minimum_version_id_old
= 0,
769 .fields
= (VMStateField
[]) {
770 VMSTATE_UINT16_HACK(tm_sec
, struct tm
),
771 VMSTATE_UINT16_HACK(tm_min
, struct tm
),
772 VMSTATE_UINT16_HACK(tm_hour
, struct tm
),
773 VMSTATE_UINT16_HACK(tm_mday
, struct tm
),
774 VMSTATE_UINT16_HACK(tm_min
, struct tm
),
775 VMSTATE_UINT16_HACK(tm_year
, struct tm
),
776 VMSTATE_END_OF_LIST()
780 static void menelaus_pre_save(void *opaque
)
782 MenelausState
*s
= opaque
;
783 /* Should be <= 1000 */
784 s
->rtc_next_vmstate
= s
->rtc
.next
- qemu_get_clock_ms(rt_clock
);
787 static int menelaus_post_load(void *opaque
, int version_id
)
789 MenelausState
*s
= opaque
;
791 if (s
->rtc
.ctrl
& 1) /* RTC_EN */
792 menelaus_rtc_stop(s
);
794 s
->rtc
.next
= s
->rtc_next_vmstate
;
796 menelaus_alm_update(s
);
798 if (s
->rtc
.ctrl
& 1) /* RTC_EN */
799 menelaus_rtc_start(s
);
803 static const VMStateDescription vmstate_menelaus
= {
806 .minimum_version_id
= 0,
807 .minimum_version_id_old
= 0,
808 .pre_save
= menelaus_pre_save
,
809 .post_load
= menelaus_post_load
,
810 .fields
= (VMStateField
[]) {
811 VMSTATE_INT32(firstbyte
, MenelausState
),
812 VMSTATE_UINT8(reg
, MenelausState
),
813 VMSTATE_UINT8_ARRAY(vcore
, MenelausState
, 5),
814 VMSTATE_UINT8_ARRAY(dcdc
, MenelausState
, 3),
815 VMSTATE_UINT8_ARRAY(ldo
, MenelausState
, 8),
816 VMSTATE_UINT8_ARRAY(sleep
, MenelausState
, 2),
817 VMSTATE_UINT8(osc
, MenelausState
),
818 VMSTATE_UINT8(detect
, MenelausState
),
819 VMSTATE_UINT16(mask
, MenelausState
),
820 VMSTATE_UINT16(status
, MenelausState
),
821 VMSTATE_UINT8(dir
, MenelausState
),
822 VMSTATE_UINT8(inputs
, MenelausState
),
823 VMSTATE_UINT8(outputs
, MenelausState
),
824 VMSTATE_UINT8(bbsms
, MenelausState
),
825 VMSTATE_UINT8_ARRAY(pull
, MenelausState
, 4),
826 VMSTATE_UINT8_ARRAY(mmc_ctrl
, MenelausState
, 3),
827 VMSTATE_UINT8(mmc_debounce
, MenelausState
),
828 VMSTATE_UINT8(rtc
.ctrl
, MenelausState
),
829 VMSTATE_UINT16(rtc
.comp
, MenelausState
),
830 VMSTATE_UINT16(rtc_next_vmstate
, MenelausState
),
831 VMSTATE_STRUCT(rtc
.new, MenelausState
, 0, vmstate_menelaus_tm
,
833 VMSTATE_STRUCT(rtc
.alm
, MenelausState
, 0, vmstate_menelaus_tm
,
835 VMSTATE_UINT8(pwrbtn_state
, MenelausState
),
836 VMSTATE_I2C_SLAVE(i2c
, MenelausState
),
837 VMSTATE_END_OF_LIST()
841 static int twl92230_init(I2CSlave
*i2c
)
843 MenelausState
*s
= FROM_I2C_SLAVE(MenelausState
, i2c
);
845 s
->rtc
.hz_tm
= qemu_new_timer_ms(rt_clock
, menelaus_rtc_hz
, s
);
846 /* Three output pins plus one interrupt pin. */
847 qdev_init_gpio_out(&i2c
->qdev
, s
->out
, 4);
849 /* Three input pins plus one power-button pin. */
850 qdev_init_gpio_in(&i2c
->qdev
, menelaus_gpio_set
, 4);
852 menelaus_reset(&s
->i2c
);
857 static void twl92230_class_init(ObjectClass
*klass
, void *data
)
859 DeviceClass
*dc
= DEVICE_CLASS(klass
);
860 I2CSlaveClass
*sc
= I2C_SLAVE_CLASS(klass
);
862 sc
->init
= twl92230_init
;
863 sc
->event
= menelaus_event
;
864 sc
->recv
= menelaus_rx
;
865 sc
->send
= menelaus_tx
;
866 dc
->vmsd
= &vmstate_menelaus
;
869 static TypeInfo twl92230_info
= {
871 .parent
= TYPE_I2C_SLAVE
,
872 .instance_size
= sizeof(MenelausState
),
873 .class_init
= twl92230_class_init
,
876 static void twl92230_register_types(void)
878 type_register_static(&twl92230_info
);
881 type_init(twl92230_register_types
)