qemu/hw/timer/twl92230.c
<<
>>
Prefs
   1/*
   2 * TI TWL92230C energy-management companion device for the OMAP24xx.
   3 * Aka. Menelaus (N4200 MENELAUS1_V2.2)
   4 *
   5 * Copyright (C) 2008 Nokia Corporation
   6 * Written by Andrzej Zaborowski <andrew@openedhand.com>
   7 *
   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.
  12 *
  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.
  17 *
  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/>.
  20 */
  21
  22#include "qemu/osdep.h"
  23#include "hw/hw.h"
  24#include "qemu/timer.h"
  25#include "hw/i2c/i2c.h"
  26#include "sysemu/sysemu.h"
  27#include "ui/console.h"
  28#include "qemu/bcd.h"
  29
  30#define VERBOSE 1
  31
  32#define TYPE_TWL92230 "twl92230"
  33#define TWL92230(obj) OBJECT_CHECK(MenelausState, (obj), TYPE_TWL92230)
  34
  35typedef struct MenelausState {
  36    I2CSlave parent_obj;
  37
  38    int firstbyte;
  39    uint8_t reg;
  40
  41    uint8_t vcore[5];
  42    uint8_t dcdc[3];
  43    uint8_t ldo[8];
  44    uint8_t sleep[2];
  45    uint8_t osc;
  46    uint8_t detect;
  47    uint16_t mask;
  48    uint16_t status;
  49    uint8_t dir;
  50    uint8_t inputs;
  51    uint8_t outputs;
  52    uint8_t bbsms;
  53    uint8_t pull[4];
  54    uint8_t mmc_ctrl[3];
  55    uint8_t mmc_debounce;
  56    struct {
  57        uint8_t ctrl;
  58        uint16_t comp;
  59        QEMUTimer *hz_tm;
  60        int64_t next;
  61        struct tm tm;
  62        struct tm new;
  63        struct tm alm;
  64        int sec_offset;
  65        int alm_sec;
  66        int next_comp;
  67    } rtc;
  68    uint16_t rtc_next_vmstate;
  69    qemu_irq out[4];
  70    uint8_t pwrbtn_state;
  71} MenelausState;
  72
  73static inline void menelaus_update(MenelausState *s)
  74{
  75    qemu_set_irq(s->out[3], s->status & ~s->mask);
  76}
  77
  78static inline void menelaus_rtc_start(MenelausState *s)
  79{
  80    s->rtc.next += qemu_clock_get_ms(rtc_clock);
  81    timer_mod(s->rtc.hz_tm, s->rtc.next);
  82}
  83
  84static inline void menelaus_rtc_stop(MenelausState *s)
  85{
  86    timer_del(s->rtc.hz_tm);
  87    s->rtc.next -= qemu_clock_get_ms(rtc_clock);
  88    if (s->rtc.next < 1)
  89        s->rtc.next = 1;
  90}
  91
  92static void menelaus_rtc_update(MenelausState *s)
  93{
  94    qemu_get_timedate(&s->rtc.tm, s->rtc.sec_offset);
  95}
  96
  97static void menelaus_alm_update(MenelausState *s)
  98{
  99    if ((s->rtc.ctrl & 3) == 3)
 100        s->rtc.alm_sec = qemu_timedate_diff(&s->rtc.alm) - s->rtc.sec_offset;
 101}
 102
 103static void menelaus_rtc_hz(void *opaque)
 104{
 105    MenelausState *s = (MenelausState *) opaque;
 106
 107    s->rtc.next_comp --;
 108    s->rtc.alm_sec --;
 109    s->rtc.next += 1000;
 110    timer_mod(s->rtc.hz_tm, s->rtc.next);
 111    if ((s->rtc.ctrl >> 3) & 3) {                               /* EVERY */
 112        menelaus_rtc_update(s);
 113        if (((s->rtc.ctrl >> 3) & 3) == 1 && !s->rtc.tm.tm_sec)
 114            s->status |= 1 << 8;                                /* RTCTMR */
 115        else if (((s->rtc.ctrl >> 3) & 3) == 2 && !s->rtc.tm.tm_min)
 116            s->status |= 1 << 8;                                /* RTCTMR */
 117        else if (!s->rtc.tm.tm_hour)
 118            s->status |= 1 << 8;                                /* RTCTMR */
 119    } else
 120        s->status |= 1 << 8;                                    /* RTCTMR */
 121    if ((s->rtc.ctrl >> 1) & 1) {                               /* RTC_AL_EN */
 122        if (s->rtc.alm_sec == 0)
 123            s->status |= 1 << 9;                                /* RTCALM */
 124        /* TODO: wake-up */
 125    }
 126    if (s->rtc.next_comp <= 0) {
 127        s->rtc.next -= muldiv64((int16_t) s->rtc.comp, 1000, 0x8000);
 128        s->rtc.next_comp = 3600;
 129    }
 130    menelaus_update(s);
 131}
 132
 133static void menelaus_reset(I2CSlave *i2c)
 134{
 135    MenelausState *s = TWL92230(i2c);
 136
 137    s->reg = 0x00;
 138
 139    s->vcore[0] = 0x0c; /* XXX: X-loader needs 0x8c? check!  */
 140    s->vcore[1] = 0x05;
 141    s->vcore[2] = 0x02;
 142    s->vcore[3] = 0x0c;
 143    s->vcore[4] = 0x03;
 144    s->dcdc[0] = 0x33;  /* Depends on wiring */
 145    s->dcdc[1] = 0x03;
 146    s->dcdc[2] = 0x00;
 147    s->ldo[0] = 0x95;
 148    s->ldo[1] = 0x7e;
 149    s->ldo[2] = 0x00;
 150    s->ldo[3] = 0x00;   /* Depends on wiring */
 151    s->ldo[4] = 0x03;   /* Depends on wiring */
 152    s->ldo[5] = 0x00;
 153    s->ldo[6] = 0x00;
 154    s->ldo[7] = 0x00;
 155    s->sleep[0] = 0x00;
 156    s->sleep[1] = 0x00;
 157    s->osc = 0x01;
 158    s->detect = 0x09;
 159    s->mask = 0x0fff;
 160    s->status = 0;
 161    s->dir = 0x07;
 162    s->outputs = 0x00;
 163    s->bbsms = 0x00;
 164    s->pull[0] = 0x00;
 165    s->pull[1] = 0x00;
 166    s->pull[2] = 0x00;
 167    s->pull[3] = 0x00;
 168    s->mmc_ctrl[0] = 0x03;
 169    s->mmc_ctrl[1] = 0xc0;
 170    s->mmc_ctrl[2] = 0x00;
 171    s->mmc_debounce = 0x05;
 172
 173    if (s->rtc.ctrl & 1)
 174        menelaus_rtc_stop(s);
 175    s->rtc.ctrl = 0x00;
 176    s->rtc.comp = 0x0000;
 177    s->rtc.next = 1000;
 178    s->rtc.sec_offset = 0;
 179    s->rtc.next_comp = 1800;
 180    s->rtc.alm_sec = 1800;
 181    s->rtc.alm.tm_sec = 0x00;
 182    s->rtc.alm.tm_min = 0x00;
 183    s->rtc.alm.tm_hour = 0x00;
 184    s->rtc.alm.tm_mday = 0x01;
 185    s->rtc.alm.tm_mon = 0x00;
 186    s->rtc.alm.tm_year = 2004;
 187    menelaus_update(s);
 188}
 189
 190static void menelaus_gpio_set(void *opaque, int line, int level)
 191{
 192    MenelausState *s = (MenelausState *) opaque;
 193
 194    if (line < 3) {
 195        /* No interrupt generated */
 196        s->inputs &= ~(1 << line);
 197        s->inputs |= level << line;
 198        return;
 199    }
 200
 201    if (!s->pwrbtn_state && level) {
 202        s->status |= 1 << 11;                                   /* PSHBTN */
 203        menelaus_update(s);
 204    }
 205    s->pwrbtn_state = level;
 206}
 207
 208#define MENELAUS_REV            0x01
 209#define MENELAUS_VCORE_CTRL1    0x02
 210#define MENELAUS_VCORE_CTRL2    0x03
 211#define MENELAUS_VCORE_CTRL3    0x04
 212#define MENELAUS_VCORE_CTRL4    0x05
 213#define MENELAUS_VCORE_CTRL5    0x06
 214#define MENELAUS_DCDC_CTRL1     0x07
 215#define MENELAUS_DCDC_CTRL2     0x08
 216#define MENELAUS_DCDC_CTRL3     0x09
 217#define MENELAUS_LDO_CTRL1      0x0a
 218#define MENELAUS_LDO_CTRL2      0x0b
 219#define MENELAUS_LDO_CTRL3      0x0c
 220#define MENELAUS_LDO_CTRL4      0x0d
 221#define MENELAUS_LDO_CTRL5      0x0e
 222#define MENELAUS_LDO_CTRL6      0x0f
 223#define MENELAUS_LDO_CTRL7      0x10
 224#define MENELAUS_LDO_CTRL8      0x11
 225#define MENELAUS_SLEEP_CTRL1    0x12
 226#define MENELAUS_SLEEP_CTRL2    0x13
 227#define MENELAUS_DEVICE_OFF     0x14
 228#define MENELAUS_OSC_CTRL       0x15
 229#define MENELAUS_DETECT_CTRL    0x16
 230#define MENELAUS_INT_MASK1      0x17
 231#define MENELAUS_INT_MASK2      0x18
 232#define MENELAUS_INT_STATUS1    0x19
 233#define MENELAUS_INT_STATUS2    0x1a
 234#define MENELAUS_INT_ACK1       0x1b
 235#define MENELAUS_INT_ACK2       0x1c
 236#define MENELAUS_GPIO_CTRL      0x1d
 237#define MENELAUS_GPIO_IN        0x1e
 238#define MENELAUS_GPIO_OUT       0x1f
 239#define MENELAUS_BBSMS          0x20
 240#define MENELAUS_RTC_CTRL       0x21
 241#define MENELAUS_RTC_UPDATE     0x22
 242#define MENELAUS_RTC_SEC        0x23
 243#define MENELAUS_RTC_MIN        0x24
 244#define MENELAUS_RTC_HR         0x25
 245#define MENELAUS_RTC_DAY        0x26
 246#define MENELAUS_RTC_MON        0x27
 247#define MENELAUS_RTC_YR         0x28
 248#define MENELAUS_RTC_WKDAY      0x29
 249#define MENELAUS_RTC_AL_SEC     0x2a
 250#define MENELAUS_RTC_AL_MIN     0x2b
 251#define MENELAUS_RTC_AL_HR      0x2c
 252#define MENELAUS_RTC_AL_DAY     0x2d
 253#define MENELAUS_RTC_AL_MON     0x2e
 254#define MENELAUS_RTC_AL_YR      0x2f
 255#define MENELAUS_RTC_COMP_MSB   0x30
 256#define MENELAUS_RTC_COMP_LSB   0x31
 257#define MENELAUS_S1_PULL_EN     0x32
 258#define MENELAUS_S1_PULL_DIR    0x33
 259#define MENELAUS_S2_PULL_EN     0x34
 260#define MENELAUS_S2_PULL_DIR    0x35
 261#define MENELAUS_MCT_CTRL1      0x36
 262#define MENELAUS_MCT_CTRL2      0x37
 263#define MENELAUS_MCT_CTRL3      0x38
 264#define MENELAUS_MCT_PIN_ST     0x39
 265#define MENELAUS_DEBOUNCE1      0x3a
 266
 267static uint8_t menelaus_read(void *opaque, uint8_t addr)
 268{
 269    MenelausState *s = (MenelausState *) opaque;
 270    int reg = 0;
 271
 272    switch (addr) {
 273    case MENELAUS_REV:
 274        return 0x22;
 275
 276    case MENELAUS_VCORE_CTRL5: reg ++;
 277    case MENELAUS_VCORE_CTRL4: reg ++;
 278    case MENELAUS_VCORE_CTRL3: reg ++;
 279    case MENELAUS_VCORE_CTRL2: reg ++;
 280    case MENELAUS_VCORE_CTRL1:
 281        return s->vcore[reg];
 282
 283    case MENELAUS_DCDC_CTRL3: reg ++;
 284    case MENELAUS_DCDC_CTRL2: reg ++;
 285    case MENELAUS_DCDC_CTRL1:
 286        return s->dcdc[reg];
 287
 288    case MENELAUS_LDO_CTRL8: reg ++;
 289    case MENELAUS_LDO_CTRL7: reg ++;
 290    case MENELAUS_LDO_CTRL6: reg ++;
 291    case MENELAUS_LDO_CTRL5: reg ++;
 292    case MENELAUS_LDO_CTRL4: reg ++;
 293    case MENELAUS_LDO_CTRL3: reg ++;
 294    case MENELAUS_LDO_CTRL2: reg ++;
 295    case MENELAUS_LDO_CTRL1:
 296        return s->ldo[reg];
 297
 298    case MENELAUS_SLEEP_CTRL2: reg ++;
 299    case MENELAUS_SLEEP_CTRL1:
 300        return s->sleep[reg];
 301
 302    case MENELAUS_DEVICE_OFF:
 303        return 0;
 304
 305    case MENELAUS_OSC_CTRL:
 306        return s->osc | (1 << 7);                       /* CLK32K_GOOD */
 307
 308    case MENELAUS_DETECT_CTRL:
 309        return s->detect;
 310
 311    case MENELAUS_INT_MASK1:
 312        return (s->mask >> 0) & 0xff;
 313    case MENELAUS_INT_MASK2:
 314        return (s->mask >> 8) & 0xff;
 315
 316    case MENELAUS_INT_STATUS1:
 317        return (s->status >> 0) & 0xff;
 318    case MENELAUS_INT_STATUS2:
 319        return (s->status >> 8) & 0xff;
 320
 321    case MENELAUS_INT_ACK1:
 322    case MENELAUS_INT_ACK2:
 323        return 0;
 324
 325    case MENELAUS_GPIO_CTRL:
 326        return s->dir;
 327    case MENELAUS_GPIO_IN:
 328        return s->inputs | (~s->dir & s->outputs);
 329    case MENELAUS_GPIO_OUT:
 330        return s->outputs;
 331
 332    case MENELAUS_BBSMS:
 333        return s->bbsms;
 334
 335    case MENELAUS_RTC_CTRL:
 336        return s->rtc.ctrl;
 337    case MENELAUS_RTC_UPDATE:
 338        return 0x00;
 339    case MENELAUS_RTC_SEC:
 340        menelaus_rtc_update(s);
 341        return to_bcd(s->rtc.tm.tm_sec);
 342    case MENELAUS_RTC_MIN:
 343        menelaus_rtc_update(s);
 344        return to_bcd(s->rtc.tm.tm_min);
 345    case MENELAUS_RTC_HR:
 346        menelaus_rtc_update(s);
 347        if ((s->rtc.ctrl >> 2) & 1)                     /* MODE12_n24 */
 348            return to_bcd((s->rtc.tm.tm_hour % 12) + 1) |
 349                    (!!(s->rtc.tm.tm_hour >= 12) << 7); /* PM_nAM */
 350        else
 351            return to_bcd(s->rtc.tm.tm_hour);
 352    case MENELAUS_RTC_DAY:
 353        menelaus_rtc_update(s);
 354        return to_bcd(s->rtc.tm.tm_mday);
 355    case MENELAUS_RTC_MON:
 356        menelaus_rtc_update(s);
 357        return to_bcd(s->rtc.tm.tm_mon + 1);
 358    case MENELAUS_RTC_YR:
 359        menelaus_rtc_update(s);
 360        return to_bcd(s->rtc.tm.tm_year - 2000);
 361    case MENELAUS_RTC_WKDAY:
 362        menelaus_rtc_update(s);
 363        return to_bcd(s->rtc.tm.tm_wday);
 364    case MENELAUS_RTC_AL_SEC:
 365        return to_bcd(s->rtc.alm.tm_sec);
 366    case MENELAUS_RTC_AL_MIN:
 367        return to_bcd(s->rtc.alm.tm_min);
 368    case MENELAUS_RTC_AL_HR:
 369        if ((s->rtc.ctrl >> 2) & 1)                     /* MODE12_n24 */
 370            return to_bcd((s->rtc.alm.tm_hour % 12) + 1) |
 371                    (!!(s->rtc.alm.tm_hour >= 12) << 7);/* AL_PM_nAM */
 372        else
 373            return to_bcd(s->rtc.alm.tm_hour);
 374    case MENELAUS_RTC_AL_DAY:
 375        return to_bcd(s->rtc.alm.tm_mday);
 376    case MENELAUS_RTC_AL_MON:
 377        return to_bcd(s->rtc.alm.tm_mon + 1);
 378    case MENELAUS_RTC_AL_YR:
 379        return to_bcd(s->rtc.alm.tm_year - 2000);
 380    case MENELAUS_RTC_COMP_MSB:
 381        return (s->rtc.comp >> 8) & 0xff;
 382    case MENELAUS_RTC_COMP_LSB:
 383        return (s->rtc.comp >> 0) & 0xff;
 384
 385    case MENELAUS_S1_PULL_EN:
 386        return s->pull[0];
 387    case MENELAUS_S1_PULL_DIR:
 388        return s->pull[1];
 389    case MENELAUS_S2_PULL_EN:
 390        return s->pull[2];
 391    case MENELAUS_S2_PULL_DIR:
 392        return s->pull[3];
 393
 394    case MENELAUS_MCT_CTRL3: reg ++;
 395    case MENELAUS_MCT_CTRL2: reg ++;
 396    case MENELAUS_MCT_CTRL1:
 397        return s->mmc_ctrl[reg];
 398    case MENELAUS_MCT_PIN_ST:
 399        /* TODO: return the real Card Detect */
 400        return 0;
 401    case MENELAUS_DEBOUNCE1:
 402        return s->mmc_debounce;
 403
 404    default:
 405#ifdef VERBOSE
 406        printf("%s: unknown register %02x\n", __func__, addr);
 407#endif
 408        break;
 409    }
 410    return 0;
 411}
 412
 413static void menelaus_write(void *opaque, uint8_t addr, uint8_t value)
 414{
 415    MenelausState *s = (MenelausState *) opaque;
 416    int line;
 417    int reg = 0;
 418    struct tm tm;
 419
 420    switch (addr) {
 421    case MENELAUS_VCORE_CTRL1:
 422        s->vcore[0] = (value & 0xe) | MIN(value & 0x1f, 0x12);
 423        break;
 424    case MENELAUS_VCORE_CTRL2:
 425        s->vcore[1] = value;
 426        break;
 427    case MENELAUS_VCORE_CTRL3:
 428        s->vcore[2] = MIN(value & 0x1f, 0x12);
 429        break;
 430    case MENELAUS_VCORE_CTRL4:
 431        s->vcore[3] = MIN(value & 0x1f, 0x12);
 432        break;
 433    case MENELAUS_VCORE_CTRL5:
 434        s->vcore[4] = value & 3;
 435        /* XXX
 436         * auto set to 3 on M_Active, nRESWARM
 437         * auto set to 0 on M_WaitOn, M_Backup
 438         */
 439        break;
 440
 441    case MENELAUS_DCDC_CTRL1:
 442        s->dcdc[0] = value & 0x3f;
 443        break;
 444    case MENELAUS_DCDC_CTRL2:
 445        s->dcdc[1] = value & 0x07;
 446        /* XXX
 447         * auto set to 3 on M_Active, nRESWARM
 448         * auto set to 0 on M_WaitOn, M_Backup
 449         */
 450        break;
 451    case MENELAUS_DCDC_CTRL3:
 452        s->dcdc[2] = value & 0x07;
 453        break;
 454
 455    case MENELAUS_LDO_CTRL1:
 456        s->ldo[0] = value;
 457        break;
 458    case MENELAUS_LDO_CTRL2:
 459        s->ldo[1] = value & 0x7f;
 460        /* XXX
 461         * auto set to 0x7e on M_WaitOn, M_Backup
 462         */
 463        break;
 464    case MENELAUS_LDO_CTRL3:
 465        s->ldo[2] = value & 3;
 466        /* XXX
 467         * auto set to 3 on M_Active, nRESWARM
 468         * auto set to 0 on M_WaitOn, M_Backup
 469         */
 470        break;
 471    case MENELAUS_LDO_CTRL4:
 472        s->ldo[3] = value & 3;
 473        /* XXX
 474         * auto set to 3 on M_Active, nRESWARM
 475         * auto set to 0 on M_WaitOn, M_Backup
 476         */
 477        break;
 478    case MENELAUS_LDO_CTRL5:
 479        s->ldo[4] = value & 3;
 480        /* XXX
 481         * auto set to 3 on M_Active, nRESWARM
 482         * auto set to 0 on M_WaitOn, M_Backup
 483         */
 484        break;
 485    case MENELAUS_LDO_CTRL6:
 486        s->ldo[5] = value & 3;
 487        break;
 488    case MENELAUS_LDO_CTRL7:
 489        s->ldo[6] = value & 3;
 490        break;
 491    case MENELAUS_LDO_CTRL8:
 492        s->ldo[7] = value & 3;
 493        break;
 494
 495    case MENELAUS_SLEEP_CTRL2: reg ++;
 496    case MENELAUS_SLEEP_CTRL1:
 497        s->sleep[reg] = value;
 498        break;
 499
 500    case MENELAUS_DEVICE_OFF:
 501        if (value & 1) {
 502            menelaus_reset(I2C_SLAVE(s));
 503        }
 504        break;
 505
 506    case MENELAUS_OSC_CTRL:
 507        s->osc = value & 7;
 508        break;
 509
 510    case MENELAUS_DETECT_CTRL:
 511        s->detect = value & 0x7f;
 512        break;
 513
 514    case MENELAUS_INT_MASK1:
 515        s->mask &= 0xf00;
 516        s->mask |= value << 0;
 517        menelaus_update(s);
 518        break;
 519    case MENELAUS_INT_MASK2:
 520        s->mask &= 0x0ff;
 521        s->mask |= value << 8;
 522        menelaus_update(s);
 523        break;
 524
 525    case MENELAUS_INT_ACK1:
 526        s->status &= ~(((uint16_t) value) << 0);
 527        menelaus_update(s);
 528        break;
 529    case MENELAUS_INT_ACK2:
 530        s->status &= ~(((uint16_t) value) << 8);
 531        menelaus_update(s);
 532        break;
 533
 534    case MENELAUS_GPIO_CTRL:
 535        for (line = 0; line < 3; line ++) {
 536            if (((s->dir ^ value) >> line) & 1) {
 537                qemu_set_irq(s->out[line],
 538                             ((s->outputs & ~s->dir) >> line) & 1);
 539            }
 540        }
 541        s->dir = value & 0x67;
 542        break;
 543    case MENELAUS_GPIO_OUT:
 544        for (line = 0; line < 3; line ++) {
 545            if ((((s->outputs ^ value) & ~s->dir) >> line) & 1) {
 546                qemu_set_irq(s->out[line], (s->outputs >> line) & 1);
 547            }
 548        }
 549        s->outputs = value & 0x07;
 550        break;
 551
 552    case MENELAUS_BBSMS:
 553        s->bbsms = 0x0d;
 554        break;
 555
 556    case MENELAUS_RTC_CTRL:
 557        if ((s->rtc.ctrl ^ value) & 1) {                        /* RTC_EN */
 558            if (value & 1)
 559                menelaus_rtc_start(s);
 560            else
 561                menelaus_rtc_stop(s);
 562        }
 563        s->rtc.ctrl = value & 0x1f;
 564        menelaus_alm_update(s);
 565        break;
 566    case MENELAUS_RTC_UPDATE:
 567        menelaus_rtc_update(s);
 568        memcpy(&tm, &s->rtc.tm, sizeof(tm));
 569        switch (value & 0xf) {
 570        case 0:
 571            break;
 572        case 1:
 573            tm.tm_sec = s->rtc.new.tm_sec;
 574            break;
 575        case 2:
 576            tm.tm_min = s->rtc.new.tm_min;
 577            break;
 578        case 3:
 579            if (s->rtc.new.tm_hour > 23)
 580                goto rtc_badness;
 581            tm.tm_hour = s->rtc.new.tm_hour;
 582            break;
 583        case 4:
 584            if (s->rtc.new.tm_mday < 1)
 585                goto rtc_badness;
 586            /* TODO check range */
 587            tm.tm_mday = s->rtc.new.tm_mday;
 588            break;
 589        case 5:
 590            if (s->rtc.new.tm_mon < 0 || s->rtc.new.tm_mon > 11)
 591                goto rtc_badness;
 592            tm.tm_mon = s->rtc.new.tm_mon;
 593            break;
 594        case 6:
 595            tm.tm_year = s->rtc.new.tm_year;
 596            break;
 597        case 7:
 598            /* TODO set .tm_mday instead */
 599            tm.tm_wday = s->rtc.new.tm_wday;
 600            break;
 601        case 8:
 602            if (s->rtc.new.tm_hour > 23)
 603                goto rtc_badness;
 604            if (s->rtc.new.tm_mday < 1)
 605                goto rtc_badness;
 606            if (s->rtc.new.tm_mon < 0 || s->rtc.new.tm_mon > 11)
 607                goto rtc_badness;
 608            tm.tm_sec = s->rtc.new.tm_sec;
 609            tm.tm_min = s->rtc.new.tm_min;
 610            tm.tm_hour = s->rtc.new.tm_hour;
 611            tm.tm_mday = s->rtc.new.tm_mday;
 612            tm.tm_mon = s->rtc.new.tm_mon;
 613            tm.tm_year = s->rtc.new.tm_year;
 614            break;
 615        rtc_badness:
 616        default:
 617            fprintf(stderr, "%s: bad RTC_UPDATE value %02x\n",
 618                            __func__, value);
 619            s->status |= 1 << 10;                               /* RTCERR */
 620            menelaus_update(s);
 621        }
 622        s->rtc.sec_offset = qemu_timedate_diff(&tm);
 623        break;
 624    case MENELAUS_RTC_SEC:
 625        s->rtc.tm.tm_sec = from_bcd(value & 0x7f);
 626        break;
 627    case MENELAUS_RTC_MIN:
 628        s->rtc.tm.tm_min = from_bcd(value & 0x7f);
 629        break;
 630    case MENELAUS_RTC_HR:
 631        s->rtc.tm.tm_hour = (s->rtc.ctrl & (1 << 2)) ?  /* MODE12_n24 */
 632                MIN(from_bcd(value & 0x3f), 12) + ((value >> 7) ? 11 : -1) :
 633                from_bcd(value & 0x3f);
 634        break;
 635    case MENELAUS_RTC_DAY:
 636        s->rtc.tm.tm_mday = from_bcd(value);
 637        break;
 638    case MENELAUS_RTC_MON:
 639        s->rtc.tm.tm_mon = MAX(1, from_bcd(value)) - 1;
 640        break;
 641    case MENELAUS_RTC_YR:
 642        s->rtc.tm.tm_year = 2000 + from_bcd(value);
 643        break;
 644    case MENELAUS_RTC_WKDAY:
 645        s->rtc.tm.tm_mday = from_bcd(value);
 646        break;
 647    case MENELAUS_RTC_AL_SEC:
 648        s->rtc.alm.tm_sec = from_bcd(value & 0x7f);
 649        menelaus_alm_update(s);
 650        break;
 651    case MENELAUS_RTC_AL_MIN:
 652        s->rtc.alm.tm_min = from_bcd(value & 0x7f);
 653        menelaus_alm_update(s);
 654        break;
 655    case MENELAUS_RTC_AL_HR:
 656        s->rtc.alm.tm_hour = (s->rtc.ctrl & (1 << 2)) ? /* MODE12_n24 */
 657                MIN(from_bcd(value & 0x3f), 12) + ((value >> 7) ? 11 : -1) :
 658                from_bcd(value & 0x3f);
 659        menelaus_alm_update(s);
 660        break;
 661    case MENELAUS_RTC_AL_DAY:
 662        s->rtc.alm.tm_mday = from_bcd(value);
 663        menelaus_alm_update(s);
 664        break;
 665    case MENELAUS_RTC_AL_MON:
 666        s->rtc.alm.tm_mon = MAX(1, from_bcd(value)) - 1;
 667        menelaus_alm_update(s);
 668        break;
 669    case MENELAUS_RTC_AL_YR:
 670        s->rtc.alm.tm_year = 2000 + from_bcd(value);
 671        menelaus_alm_update(s);
 672        break;
 673    case MENELAUS_RTC_COMP_MSB:
 674        s->rtc.comp &= 0xff;
 675        s->rtc.comp |= value << 8;
 676        break;
 677    case MENELAUS_RTC_COMP_LSB:
 678        s->rtc.comp &= 0xff << 8;
 679        s->rtc.comp |= value;
 680        break;
 681
 682    case MENELAUS_S1_PULL_EN:
 683        s->pull[0] = value;
 684        break;
 685    case MENELAUS_S1_PULL_DIR:
 686        s->pull[1] = value & 0x1f;
 687        break;
 688    case MENELAUS_S2_PULL_EN:
 689        s->pull[2] = value;
 690        break;
 691    case MENELAUS_S2_PULL_DIR:
 692        s->pull[3] = value & 0x1f;
 693        break;
 694
 695    case MENELAUS_MCT_CTRL1:
 696        s->mmc_ctrl[0] = value & 0x7f;
 697        break;
 698    case MENELAUS_MCT_CTRL2:
 699        s->mmc_ctrl[1] = value;
 700        /* TODO update Card Detect interrupts */
 701        break;
 702    case MENELAUS_MCT_CTRL3:
 703        s->mmc_ctrl[2] = value & 0xf;
 704        break;
 705    case MENELAUS_DEBOUNCE1:
 706        s->mmc_debounce = value & 0x3f;
 707        break;
 708
 709    default:
 710#ifdef VERBOSE
 711        printf("%s: unknown register %02x\n", __func__, addr);
 712#endif
 713    }
 714}
 715
 716static int menelaus_event(I2CSlave *i2c, enum i2c_event event)
 717{
 718    MenelausState *s = TWL92230(i2c);
 719
 720    if (event == I2C_START_SEND)
 721        s->firstbyte = 1;
 722
 723    return 0;
 724}
 725
 726static int menelaus_tx(I2CSlave *i2c, uint8_t data)
 727{
 728    MenelausState *s = TWL92230(i2c);
 729
 730    /* Interpret register address byte */
 731    if (s->firstbyte) {
 732        s->reg = data;
 733        s->firstbyte = 0;
 734    } else
 735        menelaus_write(s, s->reg ++, data);
 736
 737    return 0;
 738}
 739
 740static int menelaus_rx(I2CSlave *i2c)
 741{
 742    MenelausState *s = TWL92230(i2c);
 743
 744    return menelaus_read(s, s->reg ++);
 745}
 746
 747/* Save restore 32 bit int as uint16_t
 748   This is a Big hack, but it is how the old state did it.
 749   Or we broke compatibility in the state, or we can't use struct tm
 750 */
 751
 752static int get_int32_as_uint16(QEMUFile *f, void *pv, size_t size,
 753                               const VMStateField *field)
 754{
 755    int *v = pv;
 756    *v = qemu_get_be16(f);
 757    return 0;
 758}
 759
 760static int put_int32_as_uint16(QEMUFile *f, void *pv, size_t size,
 761                               const VMStateField *field, QJSON *vmdesc)
 762{
 763    int *v = pv;
 764    qemu_put_be16(f, *v);
 765
 766    return 0;
 767}
 768
 769static const VMStateInfo vmstate_hack_int32_as_uint16 = {
 770    .name = "int32_as_uint16",
 771    .get  = get_int32_as_uint16,
 772    .put  = put_int32_as_uint16,
 773};
 774
 775#define VMSTATE_UINT16_HACK(_f, _s)                                  \
 776    VMSTATE_SINGLE(_f, _s, 0, vmstate_hack_int32_as_uint16, int32_t)
 777
 778
 779static const VMStateDescription vmstate_menelaus_tm = {
 780    .name = "menelaus_tm",
 781    .version_id = 0,
 782    .minimum_version_id = 0,
 783    .fields = (VMStateField[]) {
 784        VMSTATE_UINT16_HACK(tm_sec, struct tm),
 785        VMSTATE_UINT16_HACK(tm_min, struct tm),
 786        VMSTATE_UINT16_HACK(tm_hour, struct tm),
 787        VMSTATE_UINT16_HACK(tm_mday, struct tm),
 788        VMSTATE_UINT16_HACK(tm_min, struct tm),
 789        VMSTATE_UINT16_HACK(tm_year, struct tm),
 790        VMSTATE_END_OF_LIST()
 791    }
 792};
 793
 794static int menelaus_pre_save(void *opaque)
 795{
 796    MenelausState *s = opaque;
 797    /* Should be <= 1000 */
 798    s->rtc_next_vmstate =  s->rtc.next - qemu_clock_get_ms(rtc_clock);
 799
 800    return 0;
 801}
 802
 803static int menelaus_post_load(void *opaque, int version_id)
 804{
 805    MenelausState *s = opaque;
 806
 807    if (s->rtc.ctrl & 1)                                        /* RTC_EN */
 808        menelaus_rtc_stop(s);
 809
 810    s->rtc.next = s->rtc_next_vmstate;
 811
 812    menelaus_alm_update(s);
 813    menelaus_update(s);
 814    if (s->rtc.ctrl & 1)                                        /* RTC_EN */
 815        menelaus_rtc_start(s);
 816    return 0;
 817}
 818
 819static const VMStateDescription vmstate_menelaus = {
 820    .name = "menelaus",
 821    .version_id = 0,
 822    .minimum_version_id = 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(parent_obj, MenelausState),
 852        VMSTATE_END_OF_LIST()
 853    }
 854};
 855
 856static void twl92230_realize(DeviceState *dev, Error **errp)
 857{
 858    MenelausState *s = TWL92230(dev);
 859
 860    s->rtc.hz_tm = timer_new_ms(rtc_clock, menelaus_rtc_hz, s);
 861    /* Three output pins plus one interrupt pin.  */
 862    qdev_init_gpio_out(dev, s->out, 4);
 863
 864    /* Three input pins plus one power-button pin.  */
 865    qdev_init_gpio_in(dev, menelaus_gpio_set, 4);
 866
 867    menelaus_reset(I2C_SLAVE(dev));
 868}
 869
 870static void twl92230_class_init(ObjectClass *klass, void *data)
 871{
 872    DeviceClass *dc = DEVICE_CLASS(klass);
 873    I2CSlaveClass *sc = I2C_SLAVE_CLASS(klass);
 874
 875    dc->realize = twl92230_realize;
 876    sc->event = menelaus_event;
 877    sc->recv = menelaus_rx;
 878    sc->send = menelaus_tx;
 879    dc->vmsd = &vmstate_menelaus;
 880}
 881
 882static const TypeInfo twl92230_info = {
 883    .name          = TYPE_TWL92230,
 884    .parent        = TYPE_I2C_SLAVE,
 885    .instance_size = sizeof(MenelausState),
 886    .class_init    = twl92230_class_init,
 887};
 888
 889static void twl92230_register_types(void)
 890{
 891    type_register_static(&twl92230_info);
 892}
 893
 894type_init(twl92230_register_types)
 895