All of lore.kernel.org
 help / color / mirror / Atom feed
From: David Gibson <david@gibson.dropbear.id.au>
To: Michael Davidsaver <mdavidsaver@gmail.com>
Cc: Alexander Graf <agraf@suse.de>,
	qemu-ppc@nongnu.org, qemu-devel@nongnu.org
Subject: Re: [Qemu-devel] [PATCH 09/12] nvram: add AT24Cx i2c eeprom
Date: Wed, 22 Nov 2017 15:10:02 +1100	[thread overview]
Message-ID: <20171122041002.GN2380@umbus.fritz.box> (raw)
In-Reply-To: <20171120032420.9134-10-mdavidsaver@gmail.com>

[-- Attachment #1: Type: text/plain, Size: 6770 bytes --]

On Sun, Nov 19, 2017 at 09:24:17PM -0600, Michael Davidsaver wrote:
> Signed-off-by: Michael Davidsaver <mdavidsaver@gmail.com>

Applied to ppc-for-2.12.

> ---
>  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) += ds1225y.o
>  common-obj-y += eeprom93xx.o
> +common-obj-y += eeprom_at24c.o
>  common-obj-y += fw_cfg.o
>  common-obj-y += chrp_nvram.o
>  common-obj-$(CONFIG_MAC_NVRAM) += 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 <string.h>
> +
> +#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 = container_of(s, EEPROMState, parent_obj);
> +
> +    switch (event) {
> +    case I2C_START_SEND:
> +    case I2C_START_RECV:
> +    case I2C_FINISH:
> +        ee->haveaddr = 0;
> +        DPRINTK("clear\n");
> +        if (ee->blk && ee->changed) {
> +            int len = blk_pwrite(ee->blk, 0, ee->mem, ee->rsize, 0);
> +            if (len != ee->rsize) {
> +                ERR(TYPE_AT24C_EE
> +                        " : failed to write backing file\n");
> +            }
> +            DPRINTK("Wrote to backing file\n");
> +        }
> +        ee->changed = false;
> +        break;
> +    case I2C_NACK:
> +        break;
> +    }
> +    return 0;
> +}
> +
> +static
> +int at24c_eeprom_recv(I2CSlave *s)
> +{
> +    EEPROMState *ee = AT24C_EE(s);
> +    int ret;
> +
> +    ret = ee->mem[ee->cur];
> +
> +    ee->cur = (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 = AT24C_EE(s);
> +
> +    if (ee->haveaddr < 2) {
> +        ee->cur <<= 8;
> +        ee->cur |= data;
> +        ee->haveaddr++;
> +        if (ee->haveaddr == 2) {
> +            ee->cur %= ee->rsize;
> +            DPRINTK("Set pointer %04x\n", ee->cur);
> +        }
> +
> +    } else {
> +        if (ee->writable) {
> +            DPRINTK("Send %02x\n", data);
> +            ee->mem[ee->cur] = data;
> +            ee->changed = true;
> +        } else {
> +            DPRINTK("Send error %02x read-only\n", data);
> +        }
> +        ee->cur = (ee->cur + 1u) % ee->rsize;
> +
> +    }
> +
> +    return 0;
> +}
> +
> +static
> +int at24c_eeprom_init(I2CSlave *i2c)
> +{
> +    EEPROMState *ee = AT24C_EE(i2c);
> +
> +    ee->mem = g_malloc0(ee->rsize);
> +
> +    if (ee->blk) {
> +        int64_t len = blk_getlength(ee->blk);
> +
> +        if (len != ee->rsize) {
> +            ERR(TYPE_AT24C_EE " : Backing file size %lu != %u\n",
> +                    (unsigned long)len, (unsigned)ee->rsize);
> +            exit(1);
> +        }
> +
> +        if (blk_set_perm(ee->blk, BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE,
> +                         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 = AT24C_EE(state);
> +
> +    ee->changed = false;
> +    ee->cur = 0;
> +    ee->haveaddr = 0;
> +
> +    memset(ee->mem, 0, ee->rsize);
> +
> +    if (ee->blk) {
> +        int len = blk_pread(ee->blk, 0, ee->mem, ee->rsize);
> +
> +        if (len != ee->rsize) {
> +            ERR(TYPE_AT24C_EE
> +                    " : Failed initial sync with backing file\n");
> +        }
> +        DPRINTK("Reset read backing file\n");
> +    }
> +}
> +
> +static Property at24c_eeprom_props[] = {
> +    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 = DEVICE_CLASS(klass);
> +    I2CSlaveClass *k = I2C_SLAVE_CLASS(klass);
> +
> +    k->init = &at24c_eeprom_init;
> +    k->event = &at24c_eeprom_event;
> +    k->recv = &at24c_eeprom_recv;
> +    k->send = &at24c_eeprom_send;
> +
> +    dc->props = at24c_eeprom_props;
> +    dc->reset = at24c_eeprom_reset;
> +}
> +
> +static
> +const TypeInfo at24c_eeprom_type = {
> +    .name = TYPE_AT24C_EE,
> +    .parent = TYPE_I2C_SLAVE,
> +    .instance_size = sizeof(EEPROMState),
> +    .class_size = sizeof(I2CSlaveClass),
> +    .class_init = at24c_eeprom_class_init,
> +};
> +
> +static void at24c_eeprom_register(void)
> +{
> +    type_register_static(&at24c_eeprom_type);
> +}
> +
> +type_init(at24c_eeprom_register)

-- 
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

  reply	other threads:[~2017-11-22  4:12 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-11-20  3:24 [Qemu-devel] [PATCH 00/12] Add MVME3100 PPC SBC Michael Davidsaver
2017-11-20  3:24 ` [Qemu-devel] [PATCH 01/12] e500: add board config options Michael Davidsaver
2017-11-22  3:28   ` David Gibson
2017-11-22 17:55     ` Michael Davidsaver
2017-11-23 16:07       ` [Qemu-devel] [Qemu-ppc] " Cédric Le Goater
2017-11-24  0:21       ` [Qemu-devel] " David Gibson
2017-11-20  3:24 ` [Qemu-devel] [PATCH 03/12] e500: note possible bug with host bridge Michael Davidsaver
2017-11-22  3:46   ` David Gibson
2017-11-22  4:57     ` Michael Davidsaver
2017-11-20  3:24 ` [Qemu-devel] [PATCH 04/12] e500: additional CCSR registers Michael Davidsaver
2017-11-22  3:57   ` David Gibson
2017-11-20  3:24 ` [Qemu-devel] [PATCH 05/12] e500: name openpic and pci host bridge Michael Davidsaver
2017-11-22  3:58   ` David Gibson
2017-11-20  3:24 ` [Qemu-devel] [PATCH 06/12] i2c: add mpc8540 i2c controller Michael Davidsaver
2017-11-22  4:06   ` David Gibson
2017-11-23 15:39   ` [Qemu-devel] [Qemu-ppc] " Cédric Le Goater
2017-11-24  0:13     ` David Gibson
2017-11-20  3:24 ` [Qemu-devel] [PATCH 07/12] qtest: add e500_i2c_create() Michael Davidsaver
2017-11-20  3:24 ` [Qemu-devel] [PATCH 08/12] e500: add mpc8540 i2c controller to ccsr Michael Davidsaver
2017-11-22  4:08   ` David Gibson
2017-11-22 16:46     ` Michael Davidsaver
2017-11-20  3:24 ` [Qemu-devel] [PATCH 09/12] nvram: add AT24Cx i2c eeprom Michael Davidsaver
2017-11-22  4:10   ` David Gibson [this message]
2017-11-20  3:24 ` [Qemu-devel] [PATCH 10/12] timer: add ds1375 RTC Michael Davidsaver
2017-11-22  4:11   ` David Gibson
2017-11-20  3:24 ` [Qemu-devel] [PATCH 11/12] ppc: add mvme3100 machine Michael Davidsaver
2017-11-20  3:24 ` [Qemu-devel] [PATCH 12/12] tests: add mvme3100-test Michael Davidsaver
     [not found] ` <20171120032420.9134-3-mdavidsaver@gmail.com>
2017-11-22  3:36   ` [Qemu-devel] [PATCH 02/12] e500: consolidate mpc8540 guts with e500-ccsr David Gibson
2017-12-06  3:12     ` David Gibson
2017-12-06  3:18       ` Michael Davidsaver
2017-12-06  4:23         ` David Gibson
2017-11-22  4:12 ` [Qemu-devel] [PATCH 00/12] Add MVME3100 PPC SBC David Gibson
2017-11-22  4:58   ` Michael Davidsaver

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=20171122041002.GN2380@umbus.fritz.box \
    --to=david@gibson.dropbear.id.au \
    --cc=agraf@suse.de \
    --cc=mdavidsaver@gmail.com \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-ppc@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.