236 lines
5.5 KiB
C
236 lines
5.5 KiB
C
#include "hw/arm/ipod_touch_pcf50633_pmu.h"
|
|
#include "hw/arm/ipod_touch_lcd.h"
|
|
#include "qemu/cutils.h"
|
|
#include "sysemu/rtc.h"
|
|
#include "sysemu/sysemu.h"
|
|
|
|
static int pcf50633_event(I2CSlave *i2c, enum i2c_event event)
|
|
{
|
|
Pcf50633State *s = PCF50633(i2c);
|
|
// printf("%s Event %d\n", __func__, s->cmd);
|
|
|
|
if (event == I2C_START_SEND)
|
|
{
|
|
// printf("%s start send %d\n", __func__, s->cmd);
|
|
}
|
|
else if (event == I2C_FINISH)
|
|
{
|
|
s->ready = 1;
|
|
// printf("%s end send %d\n", __func__, s->cmd);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int int_to_bcd(int value) {
|
|
int shift = 0;
|
|
int res = 0;
|
|
while (value > 0) {
|
|
res |= (value % 10) << (shift++ << 2);
|
|
value /= 10;
|
|
}
|
|
return res;
|
|
}
|
|
|
|
static uint8_t pcf50633_recv(I2CSlave *i2c)
|
|
{
|
|
Pcf50633State *s = PCF50633(i2c);
|
|
printf("Reading PMU register %d\n", s->cmd);
|
|
|
|
time_t t = time(NULL);
|
|
struct tm tm = *localtime(&t);
|
|
|
|
int res = 0;
|
|
|
|
switch(s->cmd) {
|
|
case PMU_MBCS1:
|
|
res = 1; // battery power source
|
|
break;
|
|
case PMU_ADCC1:
|
|
res = 3; // battery charge voltage
|
|
break;
|
|
case PMU_RTCSC: // seconds
|
|
res = int_to_bcd(tm.tm_sec);
|
|
break;
|
|
case PMU_RTCMN: // minutes
|
|
res = int_to_bcd(tm.tm_min);
|
|
break;
|
|
case PMU_RTCHR: // hours
|
|
res = int_to_bcd(tm.tm_hour);
|
|
break;
|
|
case PMU_RTCDT: // days
|
|
res = int_to_bcd(tm.tm_mday);
|
|
break;
|
|
case PMU_RTCMT: // month
|
|
res = int_to_bcd(tm.tm_mon + 1);
|
|
break;
|
|
case PMU_RTCYR: // year
|
|
res = int_to_bcd(tm.tm_year - 100); // the year counts from 1900
|
|
break;
|
|
case 0x67:
|
|
res = 1; // whether we should enable debug UARTS
|
|
break;
|
|
case 0x69:
|
|
res = 0; // boot count error/panic
|
|
break;
|
|
case 0x76:
|
|
res = 0; // unknown register
|
|
break;
|
|
default:
|
|
res = 0;
|
|
}
|
|
|
|
s->cmd += 1;
|
|
return res;
|
|
}
|
|
|
|
static int pcf50633_send(I2CSlave *i2c, uint8_t data)
|
|
{
|
|
Pcf50633State *s = PCF50633(i2c);
|
|
if (s->ready)
|
|
{
|
|
s->curreg = data;
|
|
s->ready = 0;
|
|
}
|
|
else
|
|
{
|
|
s->cmd = data;
|
|
}
|
|
|
|
printf("Writing PMU register cmd %d reg %d\n", s->cmd, s->curreg);
|
|
switch(s->curreg) {
|
|
case PMU_DSBL1:
|
|
lcd_changebrightness(s->cmd);
|
|
break;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static void pcf50633_init(Object *obj)
|
|
{
|
|
|
|
}
|
|
|
|
static void pcf50633_class_init(ObjectClass *klass, void *data)
|
|
{
|
|
I2CSlaveClass *k = I2C_SLAVE_CLASS(klass);
|
|
|
|
k->event = pcf50633_event;
|
|
k->recv = pcf50633_recv;
|
|
k->send = pcf50633_send;
|
|
}
|
|
|
|
static const TypeInfo pcf50633_info = {
|
|
.name = TYPE_PCF50633,
|
|
.parent = TYPE_I2C_SLAVE,
|
|
.instance_init = pcf50633_init,
|
|
.instance_size = sizeof(Pcf50633State),
|
|
.class_init = pcf50633_class_init,
|
|
};
|
|
|
|
/* Le pilote AppleD1759PMURTC lit un compteur LE32 en 0x5c et un
|
|
* décalage LE32 en 0x64. settimeofday ne réécrit que ce décalage.
|
|
* Les registres 0x61/0x62 concernent les erreurs de démarrage, pas le RTC.
|
|
*/
|
|
static uint32_t d1759_counter(Pcf50633State *s)
|
|
{
|
|
return s->rtc_tick_offset +
|
|
qemu_clock_get_ns(rtc_clock) / NANOSECONDS_PER_SECOND;
|
|
}
|
|
|
|
static int d1759_event(I2CSlave *i2c, enum i2c_event event)
|
|
{
|
|
Pcf50633State *s = PCF50633(i2c);
|
|
|
|
if (event == I2C_START_SEND || event == I2C_FINISH) {
|
|
s->ready = 1;
|
|
} else if (event == I2C_START_RECV) {
|
|
/* Une lecture multioctet reste cohérente au passage d'une seconde. */
|
|
s->rtc_snapshot = d1759_counter(s);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static uint8_t d1759_recv(I2CSlave *i2c)
|
|
{
|
|
Pcf50633State *s = PCF50633(i2c);
|
|
uint8_t reg = s->curreg++;
|
|
|
|
if (reg >= 0x5c && reg <= 0x5f) {
|
|
return s->rtc_snapshot >> ((reg - 0x5c) * 8);
|
|
}
|
|
if (reg >= 0x64 && reg <= 0x67) {
|
|
return s->rtc_unix_offset[reg - 0x64];
|
|
}
|
|
if (reg >= 0x50 && reg <= 0x5a) {
|
|
return s->rtc_alarm[reg - 0x50];
|
|
}
|
|
switch (reg) {
|
|
case 0x02: /* Conversion ADC terminée. */
|
|
return 2;
|
|
case PMU_MBCS1:
|
|
return 1;
|
|
case 0x69:
|
|
case 0x76:
|
|
default:
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
static int d1759_send(I2CSlave *i2c, uint8_t data)
|
|
{
|
|
Pcf50633State *s = PCF50633(i2c);
|
|
uint8_t reg;
|
|
|
|
if (s->ready) {
|
|
s->curreg = data;
|
|
s->ready = 0;
|
|
return 0;
|
|
}
|
|
reg = s->curreg++;
|
|
if (reg >= 0x64 && reg <= 0x67) {
|
|
s->rtc_unix_offset[reg - 0x64] = data;
|
|
} else if (reg >= 0x50 && reg <= 0x5a) {
|
|
/* Conservation des réglages d'alarme ; IRQ RTC non émulée. */
|
|
s->rtc_alarm[reg - 0x50] = data;
|
|
} else if (reg == PMU_DSBL1) {
|
|
lcd_changebrightness(data);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static void d1759_init(Object *obj)
|
|
{
|
|
Pcf50633State *s = PCF50633(obj);
|
|
struct tm tm;
|
|
|
|
qemu_get_timedate(&tm, 0);
|
|
s->rtc_tick_offset = mktimegm(&tm) -
|
|
qemu_clock_get_ns(rtc_clock) / NANOSECONDS_PER_SECOND;
|
|
s->ready = 1;
|
|
}
|
|
|
|
static void d1759_class_init(ObjectClass *klass, void *data)
|
|
{
|
|
I2CSlaveClass *k = I2C_SLAVE_CLASS(klass);
|
|
|
|
k->event = d1759_event;
|
|
k->recv = d1759_recv;
|
|
k->send = d1759_send;
|
|
}
|
|
|
|
static const TypeInfo d1759_info = {
|
|
.name = TYPE_D1759,
|
|
.parent = TYPE_PCF50633,
|
|
.instance_init = d1759_init,
|
|
.class_init = d1759_class_init,
|
|
};
|
|
|
|
static void pcf50633_register_types(void)
|
|
{
|
|
type_register_static(&pcf50633_info);
|
|
type_register_static(&d1759_info);
|
|
}
|
|
|
|
type_init(pcf50633_register_types)
|