From nobody Mon Feb 9 18:19:05 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1513317594177256.269813963939; Thu, 14 Dec 2017 21:59:54 -0800 (PST) Received: from localhost ([::1]:44598 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ePj2D-0008CJ-9o for importer@patchew.org; Fri, 15 Dec 2017 00:59:49 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:53171) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ePixL-0003c3-IB for qemu-devel@nongnu.org; Fri, 15 Dec 2017 00:54:49 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ePixK-0004dc-3k for qemu-devel@nongnu.org; Fri, 15 Dec 2017 00:54:47 -0500 Received: from ozlabs.org ([2401:3900:2:1::2]:38189) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ePixJ-0004ZO-PD; Fri, 15 Dec 2017 00:54:46 -0500 Received: by ozlabs.org (Postfix, from userid 1007) id 3yyflN3d2Yz9t4V; Fri, 15 Dec 2017 16:54:39 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gibson.dropbear.id.au; s=201602; t=1513317280; bh=vns/9Wj2IYeZU6UoTj8I9vdJy6zvmvzPiuFETuzy8hs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JNY/+UH606OT6OIXpp2pBW5RZHMnoUDzVDwypKha6/6hqpStj2m9JT/VXy8Qnpv+D CPnjAJv2V88XIAnvN4V5jORfHIq0RErmfDzGP+aCHh9NVJbGVkn0rcE+6NVQMopMSS HVnL2vYvY9M/j4/QPFRaIvioEM0eMLTLyPbG+tn8= From: David Gibson To: peter.maydell@linaro.org Date: Fri, 15 Dec 2017 16:54:17 +1100 Message-Id: <20171215055435.24204-7-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20171215055435.24204-1-david@gibson.dropbear.id.au> References: <20171215055435.24204-1-david@gibson.dropbear.id.au> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2401:3900:2:1::2 Subject: [Qemu-devel] [PULL 06/24] nvram: add AT24Cx i2c eeprom X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: lvivier@redhat.com, agraf@suse.de, Michael Davidsaver , mdroth@linux.vnet.ibm.com, qemu-devel@nongnu.org, groug@kaod.org, qemu-ppc@nongnu.org, David Gibson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Michael Davidsaver Signed-off-by: Michael Davidsaver Signed-off-by: David Gibson --- hw/nvram/Makefile.objs | 1 + hw/nvram/eeprom_at24c.c | 205 ++++++++++++++++++++++++++++++++++++++++++++= ++++ 2 files changed, 206 insertions(+) create mode 100644 hw/nvram/eeprom_at24c.c diff --git a/hw/nvram/Makefile.objs b/hw/nvram/Makefile.objs index c018f6b2ff..0f4ee71dcb 100644 --- a/hw/nvram/Makefile.objs +++ b/hw/nvram/Makefile.objs @@ -1,5 +1,6 @@ common-obj-$(CONFIG_DS1225Y) +=3D ds1225y.o common-obj-y +=3D eeprom93xx.o +common-obj-y +=3D eeprom_at24c.o common-obj-y +=3D fw_cfg.o common-obj-y +=3D chrp_nvram.o common-obj-$(CONFIG_MAC_NVRAM) +=3D mac_nvram.o diff --git a/hw/nvram/eeprom_at24c.c b/hw/nvram/eeprom_at24c.c new file mode 100644 index 0000000000..efa3621ac6 --- /dev/null +++ b/hw/nvram/eeprom_at24c.c @@ -0,0 +1,205 @@ +/* + * *AT24C* series I2C EEPROM + * + * Copyright (c) 2015 Michael Davidsaver + * + * This work is licensed under the terms of the GNU GPL, version 2. See + * the LICENSE file in the top-level directory. + */ + +#include + +#include "qemu/osdep.h" +#include "qapi/error.h" +#include "hw/hw.h" +#include "hw/i2c/i2c.h" +#include "sysemu/block-backend.h" + +/* #define DEBUG_AT24C */ + +#ifdef DEBUG_AT24C +#define DPRINTK(FMT, ...) printf(TYPE_AT24C_EE " : " FMT, ## __VA_ARGS__) +#else +#define DPRINTK(FMT, ...) do {} while (0) +#endif + +#define ERR(FMT, ...) fprintf(stderr, TYPE_AT24C_EE " : " FMT, \ + ## __VA_ARGS__) + +#define TYPE_AT24C_EE "at24c-eeprom" +#define AT24C_EE(obj) OBJECT_CHECK(EEPROMState, (obj), TYPE_AT24C_EE) + +typedef struct EEPROMState { + I2CSlave parent_obj; + + /* address counter */ + uint16_t cur; + /* total size in bytes */ + uint32_t rsize; + bool writable; + /* cells changed since last START? */ + bool changed; + /* during WRITE, # of address bytes transfered */ + uint8_t haveaddr; + + uint8_t *mem; + + BlockBackend *blk; +} EEPROMState; + +static +int at24c_eeprom_event(I2CSlave *s, enum i2c_event event) +{ + EEPROMState *ee =3D container_of(s, EEPROMState, parent_obj); + + switch (event) { + case I2C_START_SEND: + case I2C_START_RECV: + case I2C_FINISH: + ee->haveaddr =3D 0; + DPRINTK("clear\n"); + if (ee->blk && ee->changed) { + int len =3D blk_pwrite(ee->blk, 0, ee->mem, ee->rsize, 0); + if (len !=3D ee->rsize) { + ERR(TYPE_AT24C_EE + " : failed to write backing file\n"); + } + DPRINTK("Wrote to backing file\n"); + } + ee->changed =3D false; + break; + case I2C_NACK: + break; + } + return 0; +} + +static +int at24c_eeprom_recv(I2CSlave *s) +{ + EEPROMState *ee =3D AT24C_EE(s); + int ret; + + ret =3D ee->mem[ee->cur]; + + ee->cur =3D (ee->cur + 1u) % ee->rsize; + DPRINTK("Recv %02x %c\n", ret, ret); + + return ret; +} + +static +int at24c_eeprom_send(I2CSlave *s, uint8_t data) +{ + EEPROMState *ee =3D AT24C_EE(s); + + if (ee->haveaddr < 2) { + ee->cur <<=3D 8; + ee->cur |=3D data; + ee->haveaddr++; + if (ee->haveaddr =3D=3D 2) { + ee->cur %=3D ee->rsize; + DPRINTK("Set pointer %04x\n", ee->cur); + } + + } else { + if (ee->writable) { + DPRINTK("Send %02x\n", data); + ee->mem[ee->cur] =3D data; + ee->changed =3D true; + } else { + DPRINTK("Send error %02x read-only\n", data); + } + ee->cur =3D (ee->cur + 1u) % ee->rsize; + + } + + return 0; +} + +static +int at24c_eeprom_init(I2CSlave *i2c) +{ + EEPROMState *ee =3D AT24C_EE(i2c); + + ee->mem =3D g_malloc0(ee->rsize); + + if (ee->blk) { + int64_t len =3D blk_getlength(ee->blk); + + if (len !=3D ee->rsize) { + ERR(TYPE_AT24C_EE " : Backing file size %lu !=3D %u\n", + (unsigned long)len, (unsigned)ee->rsize); + exit(1); + } + + if (blk_set_perm(ee->blk, BLK_PERM_CONSISTENT_READ | BLK_PERM_WRIT= E, + BLK_PERM_ALL, &error_fatal) < 0) + { + ERR(TYPE_AT24C_EE + " : Backing file incorrect permission\n"); + exit(1); + } + } + return 0; +} + +static +void at24c_eeprom_reset(DeviceState *state) +{ + EEPROMState *ee =3D AT24C_EE(state); + + ee->changed =3D false; + ee->cur =3D 0; + ee->haveaddr =3D 0; + + memset(ee->mem, 0, ee->rsize); + + if (ee->blk) { + int len =3D blk_pread(ee->blk, 0, ee->mem, ee->rsize); + + if (len !=3D ee->rsize) { + ERR(TYPE_AT24C_EE + " : Failed initial sync with backing file\n"); + } + DPRINTK("Reset read backing file\n"); + } +} + +static Property at24c_eeprom_props[] =3D { + DEFINE_PROP_UINT32("rom-size", EEPROMState, rsize, 0), + DEFINE_PROP_BOOL("writable", EEPROMState, writable, true), + DEFINE_PROP_DRIVE("drive", EEPROMState, blk), + DEFINE_PROP_END_OF_LIST() +}; + +static +void at24c_eeprom_class_init(ObjectClass *klass, void *data) +{ + DeviceClass *dc =3D DEVICE_CLASS(klass); + I2CSlaveClass *k =3D I2C_SLAVE_CLASS(klass); + + k->init =3D &at24c_eeprom_init; + k->event =3D &at24c_eeprom_event; + k->recv =3D &at24c_eeprom_recv; + k->send =3D &at24c_eeprom_send; + + dc->props =3D at24c_eeprom_props; + dc->reset =3D at24c_eeprom_reset; +} + +static +const TypeInfo at24c_eeprom_type =3D { + .name =3D TYPE_AT24C_EE, + .parent =3D TYPE_I2C_SLAVE, + .instance_size =3D sizeof(EEPROMState), + .class_size =3D sizeof(I2CSlaveClass), + .class_init =3D at24c_eeprom_class_init, +}; + +static void at24c_eeprom_register(void) +{ + type_register_static(&at24c_eeprom_type); +} + +type_init(at24c_eeprom_register) --=20 2.14.3