qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Benoit Canet <benoit.canet@gmail.com>
To: qemu-devel@nongnu.org
Cc: Benoit Canet <benoit.canet@gmail.com>
Subject: [Qemu-devel] [PATCH 5/9] Create a Versatile I2C device using the I2C bitbanging module
Date: Fri, 14 Aug 2009 22:23:16 +0200	[thread overview]
Message-ID: <1250281397-7660-6-git-send-email-benoit.canet@gmail.com> (raw)
In-Reply-To: <1250281397-7660-5-git-send-email-benoit.canet@gmail.com>

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 4036 bytes --]


Signed-off-by: Benoit Canet <benoit.canet@gmail.com>
---
 Makefile.target    |    2 +-
 hw/versatile_i2c.c |  119 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 120 insertions(+), 1 deletions(-)
 create mode 100644 hw/versatile_i2c.c

diff --git a/Makefile.target b/Makefile.target
index 6b28931..6a78ace 100644
--- a/Makefile.target
+++ b/Makefile.target
@@ -578,7 +578,7 @@ endif
 
 obj-arm-y = integratorcp.o versatilepb.o smc91c111.o arm_pic.o arm_timer.o
 obj-arm-y += arm_boot.o pl011.o pl031.o pl050.o pl080.o pl110.o pl181.o pl190.o
-obj-arm-y += versatile_pci.o
+obj-arm-y += versatile_pci.o versatile_i2c.o
 obj-arm-y += realview_gic.o realview.o arm_sysctl.o mpcore.o
 obj-arm-y += armv7m.o armv7m_nvic.o stellaris.o pl022.o stellaris_enet.o
 obj-arm-y += pl061.o
diff --git a/hw/versatile_i2c.c b/hw/versatile_i2c.c
new file mode 100644
index 0000000..c98cef2
--- /dev/null
+++ b/hw/versatile_i2c.c
@@ -0,0 +1,119 @@
+/*
+ * Versatile i2c interface
+ *
+ * Copyright (c) 2009 Benoît Canet <benoit.canet@gmail.com>
+ *
+ * This code is licenced under the GNU GPL v2.
+ */
+#include "hw.h"
+#include "sysbus.h"
+
+#define I2C_CONTROL   0x0
+#define I2C_CONTROLS  0x0
+#define I2C_CONTROLC  0x4
+
+#define DATA    1<<1
+#define CLOCK   1
+
+typedef struct versatile_i2c_state {
+    SysBusDevice busdev;
+    int         last_clock;
+    int         last_data;
+    int         read_data;  /* data from decoder   */
+    qemu_irq    out[2];     /* 0 = data, 1 = clock */
+} versatile_i2c_state;
+
+static void versatile_i2c_gpio_set(void *opaque, int irq, int level)
+{
+    versatile_i2c_state *s = (versatile_i2c_state *) opaque;
+
+    if (irq == 0)
+        s->read_data = level;
+}
+
+static uint32_t versatile_i2c_read(void *opaque, target_phys_addr_t offset)
+{
+    versatile_i2c_state *s = (versatile_i2c_state *) opaque;
+
+    switch (offset) {
+    case I2C_CONTROL:
+        return (s->read_data << 1) | s->last_clock;
+    default:
+        hw_error("versatile_i2c_read: Bad offset %x\n", (int)offset);
+        return 0;
+    }
+}
+
+static void versatile_i2c_write(void *opaque, target_phys_addr_t offset,
+                        uint32_t value)
+{
+    versatile_i2c_state *s = (versatile_i2c_state *) opaque;
+    int data  = s->last_data;
+    int clock = s->last_clock;
+
+    switch (offset) {
+    case I2C_CONTROLS:
+        if (value & DATA)
+            data = 1;
+        if (value & CLOCK)
+            clock = 1;
+        break;
+    case I2C_CONTROLC:
+        if (value & DATA)
+            data = 0;
+        if (value & CLOCK)
+            clock = 0;
+        break;
+    default:
+        hw_error("versatile_i2c_write: Bad offset %x\n", (int)offset);
+    }
+
+    s->last_clock = clock;
+    s->last_data  = data;
+
+    qemu_set_irq(s->out[0], data);
+    qemu_set_irq(s->out[1], clock);
+}
+
+static CPUReadMemoryFunc *versatile_i2c_readfn[] = {
+   versatile_i2c_read,
+   versatile_i2c_read,
+   versatile_i2c_read
+};
+
+static CPUWriteMemoryFunc *versatile_i2c_writefn[] = {
+   versatile_i2c_write,
+   versatile_i2c_write,
+   versatile_i2c_write
+};
+
+static void versatile_i2c_reset(versatile_i2c_state *s)
+{
+    s->last_clock = 1;
+    s->last_data  = 1;
+    qemu_set_irq(s->out[0], s->last_data);
+    qemu_set_irq(s->out[1], s->last_clock);
+}
+
+static void versatile_i2c_init(SysBusDevice *dev)
+{
+    versatile_i2c_state *s = FROM_SYSBUS(versatile_i2c_state, dev);
+    int iomemtype;
+
+    versatile_i2c_reset(s);
+
+    iomemtype = cpu_register_io_memory(versatile_i2c_readfn,
+                                       versatile_i2c_writefn, s);
+    sysbus_init_mmio(dev, 0x1000, iomemtype);
+
+    qdev_init_gpio_in(&dev->qdev, versatile_i2c_gpio_set, 1);
+    qdev_init_gpio_out(&dev->qdev, s->out, 2);
+}
+
+static void versatile_i2c_register_devices(void)
+{
+    sysbus_register_dev("versatile,i2c", sizeof(versatile_i2c_state),
+                        versatile_i2c_init);
+}
+
+device_init(versatile_i2c_register_devices)
-- 
1.6.0.4

  reply	other threads:[~2009-08-14 20:23 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-08-14 20:23 [Qemu-devel] [Fix] Musicpal I2C bitbanging extraction and Versatile I2C device creation Benoit Canet
2009-08-14 20:23 ` [Qemu-devel] [PATCH 1/9] Musicpal qdev conversion : gpio (except I2C part), keyboard and lcd Benoit Canet
2009-08-14 20:23   ` [Qemu-devel] [PATCH 2/9] Extract musicpal.c I2C bitbanging code and make it gpio aware Benoit Canet
2009-08-14 20:23     ` [Qemu-devel] [PATCH 3/9] Extract the Marvell 88w8618 audio device from musicpal.c Benoit Canet
2009-08-14 20:23       ` [Qemu-devel] [PATCH 4/9] Make musicpal.c use the I2C device and the Marvell 88w8618 audio device Benoit Canet
2009-08-14 20:23         ` Benoit Canet [this message]
2009-08-14 20:23           ` [Qemu-devel] [PATCH 6/9] Add the Versatile I2C device to versatilepb.c Benoit Canet

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=1250281397-7660-6-git-send-email-benoit.canet@gmail.com \
    --to=benoit.canet@gmail.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).