qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Peter Maydell <peter.maydell@linaro.org>
To: Anthony Liguori <anthony@codemonkey.ws>
Cc: qemu-devel@nongnu.org, Paul Brook <paul@codesourcery.com>
Subject: [Qemu-devel] [PATCH 4/9] hw/ds1338: Recapture current time when register pointer wraps around
Date: Fri, 12 Oct 2012 12:38:27 +0100	[thread overview]
Message-ID: <1350041912-12595-5-git-send-email-peter.maydell@linaro.org> (raw)
In-Reply-To: <1350041912-12595-1-git-send-email-peter.maydell@linaro.org>

The DS1338 datasheet documents that the current time is captured into
the secondary registers when the register pointer wraps round to zero
as well as at a START condition. Implement this.

Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
---
 hw/ds1338.c |   59 ++++++++++++++++++++++++++++++++++++++++++-----------------
 1 file changed, 42 insertions(+), 17 deletions(-)

diff --git a/hw/ds1338.c b/hw/ds1338.c
index be68140..842d2de 100644
--- a/hw/ds1338.c
+++ b/hw/ds1338.c
@@ -26,27 +26,52 @@ typedef struct {
     int addr_byte;
 } DS1338State;
 
+static void capture_current_time(DS1338State *s)
+{
+    /* Capture the current time into the secondary registers
+     * which will be actually read by the data transfer operation.
+     */
+    qemu_get_timedate(&s->now, s->offset);
+    s->nvram[0] = to_bcd(s->now.tm_sec);
+    s->nvram[1] = to_bcd(s->now.tm_min);
+    if (s->nvram[2] & 0x40) {
+        s->nvram[2] = (to_bcd((s->now.tm_hour % 12)) + 1) | 0x40;
+        if (s->now.tm_hour >= 12) {
+            s->nvram[2] |= 0x20;
+        }
+    } else {
+        s->nvram[2] = to_bcd(s->now.tm_hour);
+    }
+    s->nvram[3] = to_bcd(s->now.tm_wday) + 1;
+    s->nvram[4] = to_bcd(s->now.tm_mday);
+    s->nvram[5] = to_bcd(s->now.tm_mon) + 1;
+    s->nvram[6] = to_bcd(s->now.tm_year - 100);
+}
+
+static void inc_regptr(DS1338State *s)
+{
+    /* The register pointer wraps around after 0x3F; wraparound
+     * causes the current time/date to be retransferred into
+     * the secondary registers.
+     */
+    s->ptr = (s->ptr + 1) & (NVRAM_SIZE - 1);
+    if (!s->ptr) {
+        capture_current_time(s);
+    }
+}
+
 static void ds1338_event(I2CSlave *i2c, enum i2c_event event)
 {
     DS1338State *s = FROM_I2C_SLAVE(DS1338State, i2c);
 
     switch (event) {
     case I2C_START_RECV:
-        qemu_get_timedate(&s->now, s->offset);
-        s->nvram[0] = to_bcd(s->now.tm_sec);
-        s->nvram[1] = to_bcd(s->now.tm_min);
-        if (s->nvram[2] & 0x40) {
-            s->nvram[2] = (to_bcd((s->now.tm_hour % 12)) + 1) | 0x40;
-            if (s->now.tm_hour >= 12) {
-                s->nvram[2] |= 0x20;
-            }
-        } else {
-            s->nvram[2] = to_bcd(s->now.tm_hour);
-        }
-        s->nvram[3] = to_bcd(s->now.tm_wday) + 1;
-        s->nvram[4] = to_bcd(s->now.tm_mday);
-        s->nvram[5] = to_bcd(s->now.tm_mon) + 1;
-        s->nvram[6] = to_bcd(s->now.tm_year - 100);
+        /* In h/w, capture happens on any START condition, not just a
+         * START_RECV, but there is no need to actually capture on
+         * START_SEND, because the guest can't get at that data
+         * without going through a START_RECV which would overwrite it.
+         */
+        capture_current_time(s);
         break;
     case I2C_START_SEND:
         s->addr_byte = 1;
@@ -62,7 +87,7 @@ static int ds1338_recv(I2CSlave *i2c)
     uint8_t res;
 
     res  = s->nvram[s->ptr];
-    s->ptr = (s->ptr + 1) & (NVRAM_SIZE - 1);
+    inc_regptr(s);
     return res;
 }
 
@@ -116,7 +141,7 @@ static int ds1338_send(I2CSlave *i2c, uint8_t data)
     } else {
         s->nvram[s->ptr] = data;
     }
-    s->ptr = (s->ptr + 1) & (NVRAM_SIZE - 1);
+    inc_regptr(s);
     return 0;
 }
 
-- 
1.7.9.5

  parent reply	other threads:[~2012-10-12 11:38 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-10-12 11:38 [Qemu-devel] [PULL 0/9] arm-devs queue Peter Maydell
2012-10-12 11:38 ` [Qemu-devel] [PATCH 1/9] cadence_ttc: Fix 'clear on read' behavior Peter Maydell
2012-10-12 11:38 ` [Qemu-devel] [PATCH 2/9] hw/arm_gic.c: Fix improper DPRINTF output Peter Maydell
2012-10-12 11:38 ` [Qemu-devel] [PATCH 3/9] hw/ds1338: Fix mishandling of register pointer Peter Maydell
2012-10-12 11:38 ` Peter Maydell [this message]
2012-10-12 11:38 ` [Qemu-devel] [PATCH 5/9] hw/ds1338: Remove 'now' field from state struct Peter Maydell
2012-10-12 11:38 ` [Qemu-devel] [PATCH 6/9] hw/ds1338: Implement state save/restore Peter Maydell
2012-10-12 11:38 ` [Qemu-devel] [PATCH 7/9] versatilepb: add gpio pl061 support Peter Maydell
2012-10-12 11:38 ` [Qemu-devel] [PATCH 8/9] zynq_slcr: Fixed ResetValues enum Peter Maydell
2012-10-12 11:38 ` [Qemu-devel] [PATCH 9/9] arm_gic: Rename gic_state to GICState Peter Maydell
2012-10-12 19:16 ` [Qemu-devel] [PULL 0/9] arm-devs queue Anthony Liguori

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1350041912-12595-5-git-send-email-peter.maydell@linaro.org \
    --to=peter.maydell@linaro.org \
    --cc=anthony@codemonkey.ws \
    --cc=paul@codesourcery.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).