From patchwork Tue Aug 11 19:16:37 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benoit Canet X-Patchwork-Id: 31159 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by bilbo.ozlabs.org (Postfix) with ESMTPS id B5FF9B6EDF for ; Wed, 12 Aug 2009 05:21:28 +1000 (EST) Received: from localhost ([127.0.0.1]:33994 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MawuS-0004yR-LK for incoming@patchwork.ozlabs.org; Tue, 11 Aug 2009 15:21:24 -0400 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1Mawq6-0003cp-8b for qemu-devel@nongnu.org; Tue, 11 Aug 2009 15:16:54 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1Mawq1-0003Zu-HN for qemu-devel@nongnu.org; Tue, 11 Aug 2009 15:16:53 -0400 Received: from [199.232.76.173] (port=59373 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Mawq1-0003Zl-9g for qemu-devel@nongnu.org; Tue, 11 Aug 2009 15:16:49 -0400 Received: from mail-ew0-f210.google.com ([209.85.219.210]:57127) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1Mawq0-0007V5-ND for qemu-devel@nongnu.org; Tue, 11 Aug 2009 15:16:49 -0400 Received: by mail-ew0-f210.google.com with SMTP id 6so2183527ewy.34 for ; Tue, 11 Aug 2009 12:16:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:from:to:cc:subject:date :message-id:x-mailer:in-reply-to:references; bh=S5NW+3LxKYPPdLGU19729yXWHAS9do7SvOJ5XGdx064=; b=TOIMVqIKPoUf8rjAACXZuqs1l1wQdfvqBh8rrsrK5p5O9WqzqIu5xZLB0jvpbd0laq uo/Epqb/QykMJvvpnYkpKTs77MtoHQdTHU7k+t+ohGUsf5uzi21E1i9SR6Oy3Pt5xJWH mjpX8XzuNZRvBUKFedpeAXhwM8NJJxmAczgzc= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; b=v2M5F12Cz5nP6RtLtOTrscB3uj2LWEg/coJl8bJiYiWBRczYLLWtQHOVwPrY6LT2KD YN8kBzxqxEjMCN8r1K+PMsO82AmoD57bKMOmynGu+ZU8kWSvz1RLk3/1lYYSFZUwLbdu qtfKtmpL1zZHG5eiBxpqk2w9eyHiKPbyIA3Pc= Received: by 10.210.39.2 with SMTP id m2mr1287448ebm.85.1250018207832; Tue, 11 Aug 2009 12:16:47 -0700 (PDT) Received: from localhost.localdomain (AMontsouris-157-1-65-87.w90-46.abo.wanadoo.fr [90.46.56.87]) by mx.google.com with ESMTPS id 10sm553068eyz.51.2009.08.11.12.16.45 (version=SSLv3 cipher=RC4-MD5); Tue, 11 Aug 2009 12:16:47 -0700 (PDT) From: Benoit Canet To: qemu-devel@nongnu.org Date: Tue, 11 Aug 2009 21:16:37 +0200 Message-Id: <1250018199-7456-3-git-send-email-benoit.canet@gmail.com> X-Mailer: git-send-email 1.6.0.4 In-Reply-To: <1250018199-7456-2-git-send-email-benoit.canet@gmail.com> References: <1250018199-7456-1-git-send-email-benoit.canet@gmail.com> <1250018199-7456-2-git-send-email-benoit.canet@gmail.com> X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6 (newer, 2) Cc: Benoit Canet Subject: [Qemu-devel] [PATCH 2/4] Extract musicpal.c I2C bitbanging code and make it gpio aware X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Signed-off-by: Benoit Canet --- Makefile.target | 2 +- hw/bitbang_i2c.c | 183 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 184 insertions(+), 1 deletions(-) create mode 100644 hw/bitbang_i2c.c diff --git a/Makefile.target b/Makefile.target index f9cd42a..12fcf21 100644 --- a/Makefile.target +++ b/Makefile.target @@ -592,7 +592,7 @@ obj-arm-y += omap2.o omap_dss.o soc_dma.o obj-arm-y += omap_sx1.o palm.o tsc210x.o obj-arm-y += nseries.o blizzard.o onenand.o vga.o cbus.o tusb6010.o usb-musb.o obj-arm-y += mst_fpga.o mainstone.o -obj-arm-y += musicpal.o pflash_cfi02.o +obj-arm-y += musicpal.o pflash_cfi02.o bitbang_i2c.o obj-arm-y += framebuffer.o obj-arm-y += syborg.o syborg_fb.o syborg_interrupt.o syborg_keyboard.o obj-arm-y += syborg_serial.o syborg_timer.o syborg_pointer.o syborg_rtc.o diff --git a/hw/bitbang_i2c.c b/hw/bitbang_i2c.c new file mode 100644 index 0000000..b1fe014 --- /dev/null +++ b/hw/bitbang_i2c.c @@ -0,0 +1,183 @@ +/* + * Bit-Bang i2c emulation extracted from + * Marvell MV88W8618 / Freecom MusicPal emulation. + * + * Copyright (c) 2008 Jan Kiszka + * + * This code is licenced under the GNU GPL v2. + */ +#include "hw.h" +#include "i2c.h" +#include "sysbus.h" + +typedef enum bitbang_i2c_state { + STOPPED = 0, + INITIALIZING, + SENDING_BIT7, + SENDING_BIT6, + SENDING_BIT5, + SENDING_BIT4, + SENDING_BIT3, + SENDING_BIT2, + SENDING_BIT1, + SENDING_BIT0, + WAITING_FOR_ACK, + RECEIVING_BIT7, + RECEIVING_BIT6, + RECEIVING_BIT5, + RECEIVING_BIT4, + RECEIVING_BIT3, + RECEIVING_BIT2, + RECEIVING_BIT1, + RECEIVING_BIT0, + SENDING_ACK +} bitbang_i2c_state; + +typedef struct bitbang_i2c_interface { + SysBusDevice busdev; + i2c_bus *bus; + bitbang_i2c_state state; + int last_data; + int last_clock; + uint8_t buffer; + int current_addr; + qemu_irq out; +} bitbang_i2c_interface; + +static void bitbang_i2c_enter_stop(bitbang_i2c_interface *i2c) +{ + if (i2c->current_addr >= 0) + i2c_end_transfer(i2c->bus); + i2c->current_addr = -1; + i2c->state = STOPPED; +} + +static void bitbang_i2c_gpio_set(void *opaque, int irq, int level) +{ + bitbang_i2c_interface *i2c = opaque; + int data; + int clock; + int data_goes_up; + int data_goes_down; + int clock_goes_up; + int clock_goes_down; + + /* get pins states */ + data = i2c->last_data; + clock = i2c->last_clock; + + if (irq == 0) + data = level; + if (irq == 1) + clock = level; + + /* compute pins changes */ + data_goes_up = data == 1 && i2c->last_data == 0; + data_goes_down = data == 0 && i2c->last_data == 1; + clock_goes_up = clock == 1 && i2c->last_clock == 0; + clock_goes_down = clock == 0 && i2c->last_clock == 1; + + if (data_goes_up == 0 && data_goes_down == 0 && + clock_goes_up == 0 && clock_goes_down == 0) + return; + + if (!i2c) + return; + + switch (i2c->state) { + case STOPPED: + if (data_goes_down && clock == 1) + i2c->state = INITIALIZING; + break; + + case INITIALIZING: + if (clock_goes_down && data == 0) + i2c->state = SENDING_BIT7; + else + bitbang_i2c_enter_stop(i2c); + break; + + case SENDING_BIT7 ... SENDING_BIT0: + if (clock_goes_down) { + i2c->buffer = (i2c->buffer << 1) | data; + /* will end up in WAITING_FOR_ACK */ + i2c->state++; + } else if (data_goes_up && clock == 1) + bitbang_i2c_enter_stop(i2c); + break; + + case WAITING_FOR_ACK: + if (clock_goes_down) { + if (i2c->current_addr < 0) { + i2c->current_addr = i2c->buffer; + i2c_start_transfer(i2c->bus, (i2c->current_addr & 0xfe) / 2, + i2c->buffer & 1); + } else + i2c_send(i2c->bus, i2c->buffer); + if (i2c->current_addr & 1) { + i2c->state = RECEIVING_BIT7; + i2c->buffer = i2c_recv(i2c->bus); + } else + i2c->state = SENDING_BIT7; + } else if (data_goes_up && clock == 1) + bitbang_i2c_enter_stop(i2c); + break; + + case RECEIVING_BIT7 ... RECEIVING_BIT0: + if (clock_goes_down) { + /* will end up in SENDING_ACK */ + i2c->state++; + i2c->buffer <<= 1; + } else if (data_goes_up && clock == 1) + bitbang_i2c_enter_stop(i2c); + break; + + case SENDING_ACK: + if (clock_goes_down) { + i2c->state = RECEIVING_BIT7; + if (data == 0) + i2c->buffer = i2c_recv(i2c->bus); + else + i2c_nack(i2c->bus); + } else if (data_goes_up && clock == 1) + bitbang_i2c_enter_stop(i2c); + break; + } + + /* update output GPIO */ + switch (i2c->state) { + case RECEIVING_BIT7 ... RECEIVING_BIT0: + qemu_set_irq(i2c->out, i2c->buffer >> 7); + case WAITING_FOR_ACK: + default: + qemu_set_irq(i2c->out, 0); + } + + i2c->last_data = data; + i2c->last_clock = clock; +} + +static void bitbang_i2c_init(SysBusDevice *dev) +{ + bitbang_i2c_interface *s = FROM_SYSBUS(bitbang_i2c_interface, dev); + i2c_bus *bus; + + sysbus_init_mmio(dev, 0x0, 0); + + bus = i2c_init_bus(&dev->qdev, "i2c"); + s->bus = bus; + + s->last_data = 1; + s->last_clock = 1; + + qdev_init_gpio_in(&dev->qdev, bitbang_i2c_gpio_set, 2); + qdev_init_gpio_out(&dev->qdev, &s->out, 1); +} + +static void bitbang_i2c_register(void) +{ + sysbus_register_dev("bitbang_i2c", + sizeof(bitbang_i2c_interface), bitbang_i2c_init); +} + +device_init(bitbang_i2c_register)