Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1M1ckW-0006j7-A4 for qemu-devel@nongnu.org; Wed, 06 May 2009 04:45:08 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1M1ckT-0006hP-9Q for qemu-devel@nongnu.org; Wed, 06 May 2009 04:45:06 -0400 Received: from [199.232.76.173] (port=33364 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1M1ckS-0006h4-Fk for qemu-devel@nongnu.org; Wed, 06 May 2009 04:45:04 -0400 Received: from flounder.pepperfish.net ([87.237.62.181]:58501) by monty-python.gnu.org with esmtps (TLS-1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1M1ckR-00016f-L0 for qemu-devel@nongnu.org; Wed, 06 May 2009 04:45:04 -0400 From: Vincent Sanders Date: Wed, 6 May 2009 09:44:47 +0100 Message-Id: <1241599494-28654-9-git-send-email-vince@simtec.co.uk> In-Reply-To: <1241599494-28654-1-git-send-email-vince@simtec.co.uk> References: <1241599494-28654-1-git-send-email-vince@simtec.co.uk> Subject: [Qemu-devel] [PATCH 08/15] Peripheral driver for S3C SOC real time clock. List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Vincent Sanders Signed-off-by: Vincent Sanders --- Makefile.target | 2 +- hw/s3c24xx_rtc.c | 123 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 124 insertions(+), 1 deletions(-) create mode 100644 hw/s3c24xx_rtc.c diff --git a/Makefile.target b/Makefile.target index 679c9e7..02bb9f1 100644 --- a/Makefile.target +++ b/Makefile.target @@ -675,7 +675,7 @@ OBJS+= nseries.o blizzard.o onenand.o vga.o cbus.o tusb6010.o usb-musb.o OBJS+= mst_fpga.o mainstone.o OBJS+= musicpal.o pflash_cfi02.o OBJS+= s3c24xx_memc.o s3c24xx_irq.o s3c24xx_clkcon.o s3c24xx_timers.o -OBJS+= s3c24xx_serial.o +OBJS+= s3c24xx_serial.o s3c24xx_rtc.o OBJS+= framebuffer.o CPPFLAGS += -DHAS_AUDIO endif diff --git a/hw/s3c24xx_rtc.c b/hw/s3c24xx_rtc.c new file mode 100644 index 0000000..2c799fd --- /dev/null +++ b/hw/s3c24xx_rtc.c @@ -0,0 +1,123 @@ +/* hw/s3c24xx_rtc.c + * + * Samsung S3C24XX RTC emulation + * + * Copyright 2006, 2007, 2008 Daniel Silverstone and Vincent Sanders + * + * This file is under the terms of the GNU General Public + * License Version 2 + */ + +#include "hw.h" + +#include "s3c24xx.h" + + +/* RTC Control RW Byte */ +#define S3C_REG_RTCCON 0 +/* Tick time count RW Byte */ +#define S3C_REG_TICNT 1 +/* RTC Alarm Control RW Byte */ +#define S3C_REG_RTCALM 4 +/* Alarm second */ +#define S3C_REG_ALMSEC 5 +/* Alarm minute */ +#define S3C_REG_ALMMIN 6 +/* Alarm hour */ +#define S3C_REG_ALMHOUR 7 +/* Alarm day */ +#define S3C_REG_ALMDATE 8 +/* Alarm month */ +#define S3C_REG_ALMMON 9 +/* Alarm year */ +#define S3C_REG_ALMYEAR 10 +/* RTC Round Reset */ +#define S3C_REG_RTCRST 11 +/* BCD Second */ +#define S3C_REG_BCDSEC 12 +/* BCD Minute */ +#define S3C_REG_BCDMIN 13 +/* BCD Hour */ +#define S3C_REG_BCDHOUR 14 +/* BCD Day */ +#define S3C_REG_BCDDATE 15 +/* BCD Day of week */ +#define S3C_REG_BCDDAY 16 +/* BCD Month */ +#define S3C_REG_BCDMON 17 +/* BCD Year */ +#define S3C_REG_BCDYEAR 18 + +static inline int to_bcd(int a) +{ + return ((a/10)<<4) | (a%10); +} + +static void update_time(S3CState *soc) +{ + time_t ti; + struct tm *tm; + /* update the RTC registers from system time */ + time(&ti); + tm = gmtime(&ti); + soc->rtc_reg[S3C_REG_BCDSEC] = to_bcd(tm->tm_sec); + soc->rtc_reg[S3C_REG_BCDMIN] = to_bcd(tm->tm_min); + soc->rtc_reg[S3C_REG_BCDHOUR] = to_bcd(tm->tm_hour); + soc->rtc_reg[S3C_REG_BCDDATE] = to_bcd(tm->tm_mday); + soc->rtc_reg[S3C_REG_BCDDAY] = to_bcd(tm->tm_wday + 1); + soc->rtc_reg[S3C_REG_BCDMON] = to_bcd(tm->tm_mon + 1); + soc->rtc_reg[S3C_REG_BCDYEAR] = to_bcd(tm->tm_year - 100); +} + +static void +s3c24xx_rtc_write_f(void *opaque, target_phys_addr_t addr_, uint32_t value) +{ + S3CState *soc = (S3CState *)opaque; + int addr = (addr_ - 0x40) >> 2; + if (addr < 0 || addr > 18) + addr = 18; + + soc->rtc_reg[addr] = value; +} + +static uint32_t +s3c24xx_rtc_read_f(void *opaque, target_phys_addr_t addr_) +{ + S3CState *soc = (S3CState *)opaque; + int addr = (addr_ - 0x40) >> 2; + + if (addr < 0 || addr > 18) + addr = 18; + + update_time(soc); + + return soc->rtc_reg[addr]; +} + +static CPUReadMemoryFunc *s3c24xx_rtc_read[] = { + &s3c24xx_rtc_read_f, + &s3c24xx_rtc_read_f, + &s3c24xx_rtc_read_f, +}; + +static CPUWriteMemoryFunc *s3c24xx_rtc_write[] = { + &s3c24xx_rtc_write_f, + &s3c24xx_rtc_write_f, + &s3c24xx_rtc_write_f, +}; + + +void +s3c24xx_rtc_init(S3CState *soc, target_phys_addr_t base_addr) +{ + int tag; + tag = cpu_register_io_memory(0, s3c24xx_rtc_read, s3c24xx_rtc_write, soc); + + /* there are only 19 real registers but they start at offset 0x40 into the + * range so we have 35 registers mapped + */ + cpu_register_physical_memory(base_addr, 35 * 4, tag); + + /* set the RTC so it appears active */ + soc->rtc_reg[S3C_REG_RTCCON] = 1; +} -- 1.5.4.3