Skip to contentSkip to conversation

SPI bus support + Xilinx SPI controller

RFC PATCH Superseded Peter A. G. Crosthwaite 5 participants 23 messages Mar 30, 2012 – Jun 5, 2012

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover SPI bus support + Xilinx SPI controller

Add support for Serial Peripheral interface (SPI) as a proper bus standard. Includes an example device (m25p80 SPI flash), an example controller (Xilinx XPS SPI) and adds it to all to a machine model (petalogix_ml605_mmu.c).

Patch 1 adds the Serial Peripheral Interface (SPI) protocol as a bus and defines a QOM type for slave devices. The approach to doing this is based looseley on the existing I2C QOMification.

Patch 2 is a device model for the m25p80 style SPI flash chip.

Patch 3 is the Xilinx XPS SPI contoller. Its a sysbus device that instantiates a spi bus, and interfaces the two (as per the controllers functionality)

Patch 4 instantiates the XPS SPI controller in the petalogix ML605 reference platform and connects one m25p80 to it.

Peter A. G. Crosthwaite (4):
SPI: initial support
m25p80: initial verion
xilinx_spi: initial version
petalogix-ml605: added spi controller with m25p80

 Makefile.target          |    3 +
 hw/m25p80.c              |  495 ++++++++++++++++++++++++++++++++++++++++++++++
 hw/petalogix_ml605_mmu.c |   19 ++
 hw/spi.c                 |  175 ++++++++++++++++
 hw/spi.h                 |   86 ++++++++
 hw/xilinx_spi.c          |  477 ++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 1255 insertions(+), 0 deletions(-)
 create mode 100644 hw/m25p80.c
 create mode 100644 hw/spi.c
 create mode 100644 hw/spi.h
 create mode 100644 hw/xilinx_spi.c
Signature · 1 line
1.7.3.2
#1 lore raw

RFC PATCH 1/4 SPI: initial support

defined spi bus and spi slave QOM interfaces. Inspired by and loosley based on existing I2C framework.

---
 Makefile.target |    1 +
 hw/spi.c        |  175 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 hw/spi.h        |   86 +++++++++++++++++++++++++++
 3 files changed, 262 insertions(+), 0 deletions(-)
 create mode 100644 hw/spi.c
 create mode 100644 hw/spi.h
Makefile.target +1 -0
index 44b2e83..8fd3718 100644
@@ -320,6 +320,7 @@ obj-mips-$(CONFIG_FULONG) += bonito.o vt82c686.o mips_fulong2e.o
 obj-microblaze-y = petalogix_s3adsp1800_mmu.o
 obj-microblaze-y += petalogix_ml605_mmu.o
 obj-microblaze-y += microblaze_boot.o
+obj-microblaze-y += spi.o
 
 obj-microblaze-y += microblaze_pic_cpu.o
 obj-microblaze-y += xilinx_intc.o
hw/spi.c new file +175 -0

175 lines not on this page · raw message

hw/spi.h new file +86 -0

86 lines not on this page · raw message

Signature · 1 line
1.7.3.2
#2 lore raw

Re: [Qemu-devel] [RFC PATCH v1 1/4] SPI: initial support

Quoted 2 lines · Peter A. G. Crosthwaite

defined spi bus and spi slave QOM interfaces. Inspired by and loosley based on existing I2C framework.

There's also SPI support in qemu-linaro thanks to the folks at Nokia:
http://git.linaro.org/gitweb?p=qemu/qemu-linaro.git;a=blob;f=hw/spi.h

I guess we need to look at both and figure out which is the better implementation...

-- PMM

#3 lore raw

Re: [Qemu-devel] [RFC PATCH v1 1/4] SPI: initial support

Hi Peter,

One major flaw in that interface (which is similar to another that we originally used) is it doesnt have an API for wiggling the CS lines. Some spi devices (e.g. the m25p80 in this series) have side effects from cs strobing whether or not a txrx occurs during cs assertion, which is not possible in that api. Thats the main difference between the two implementations.

Regards,
Peter

Quoted 12 lines · Peter Maydell
Quoted 2 lines · Peter A. G. Crosthwaite

defined spi bus and spi slave QOM interfaces. Inspired by and loosley based on existing I2C framework.

There's also SPI support in qemu-linaro thanks to the folks at Nokia:
http://git.linaro.org/gitweb?p=qemu/qemu-linaro.git;a=blob;f=hw/spi.h

I guess we need to look at both and figure out which is the better implementation...

-- PMM

#4 lore raw

Re: [Qemu-devel] [RFC PATCH v1 1/4] SPI: initial support

In reply to Peter A. G. Crosthwaite
Quoted 1 line · Peter A. G. Crosthwaite

defined spi bus and spi slave QOM interfaces. Inspired by and loosley based on

"Define"; "loosely".

Quoted 16 lines

existing I2C framework.

---
 Makefile.target |    1 +
 hw/spi.c        |  175 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ hw/spi.h        |   86 +++++++++++++++++++++++++++

 3 files changed, 262 insertions(+), 0 deletions(-)

 create mode 100644 hw/spi.c
 create mode 100644 hw/spi.h

Makefile.target +0 -0
index 44b2e83..8fd3718 100644
@@ -320,6 +320,7 @@ obj-mips-$(CONFIG_FULONG) += bonito.o vt82c686.o mips_fulong2e.o
Quoted 4 lines

 obj-microblaze-y = petalogix_s3adsp1800_mmu.o
 obj-microblaze-y += petalogix_ml605_mmu.o
 obj-microblaze-y += microblaze_boot.o
+obj-microblaze-y += spi.o

This should be in common-obj-y with i2c.o, I think -- microblaze isn't the only target with SPI (eg hw/omap_spi.c).

Quoted 189 lines

 obj-microblaze-y += microblaze_pic_cpu.o
 obj-microblaze-y += xilinx_intc.o

hw/spi.c new file +175 -0
new file mode 100644
index 0000000..3afef07
@@ -0,0 +1,175 @@
+/*
+ * QEMU SPI bus interface.
+ *
+ * Copyright (C) 2012 Peter A. G. Crosthwaite <[email protected]>
+ * Copyright (C) 2012 PetaLogix
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version
+ * 2 of the License, or (at your option) any later version.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "spi.h"
+
+static struct BusInfo spi_bus_info = {
+    .name = "SPI",
+    .size = sizeof(spi_bus)
+};
+
+static const VMStateDescription vmstate_spi_bus = {
+    .name = "spi_bus",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .minimum_version_id_old = 1,
+    .fields      = (VMStateField[]) {
+        VMSTATE_UINT8(cur_slave, spi_bus),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
+spi_bus *spi_init_bus(DeviceState *parent, int num_slaves, const char *name)
+{
+    spi_bus *bus;
+
+    bus = FROM_QBUS(spi_bus, qbus_create(&spi_bus_info, parent, name));
+    if (num_slaves >= SPI_BUS_NO_CS) {
+        hw_error("too many slaves on spi bus: %d\n", num_slaves);
+    }
+    bus->num_slaves = num_slaves;
+    bus->slaves = g_malloc0(sizeof(*bus->slaves) * num_slaves);
+    vmstate_register(NULL, -1, &vmstate_spi_bus, bus);
+    return bus;
+}
+
+int spi_attach_slave(spi_bus *bus, SPISlave *slave, int cs)
+{
+    if (bus->slaves[cs]) {
+        return 1;
+    }
+    bus->slaves[cs] = slave;
+    return 0;
+}
+
+int spi_set_cs(spi_bus *bus, int cs)
+{
+    SPISlave *dev;
+    SPISlaveClass *klass;
+
+    if (bus->cur_slave == cs) {
+        return 0;
+    }
+
+    if (bus->cur_slave != SPI_BUS_NO_CS) {
+        dev = bus->slaves[bus->cur_slave];
+        dev->cs = 0;
+        klass = SPI_SLAVE_GET_CLASS(dev);
+        klass->cs(dev, 0);
+    }
+
+    if (cs >= bus->num_slaves && cs != SPI_BUS_NO_CS) {
+        hw_error("attempted to assert non existent spi CS line: %d\n", cs);
+    }
+
+    bus->cur_slave = (uint8_t)cs;
+
+    if (cs != SPI_BUS_NO_CS) {
+        dev = bus->slaves[cs];
+        dev->cs = 1;
+        klass = SPI_SLAVE_GET_CLASS(dev);
+        klass->cs(dev, 1);
+    }
+    return 0;
+};
+
+int spi_get_cs(spi_bus *bus)
+{
+    return bus->cur_slave;
+}
+
+SpiSlaveState spi_get_state(spi_bus *bus)
+{
+    SPISlave *dev;
+    SPISlaveClass *klass;
+
+    if (bus->cur_slave == SPI_BUS_NO_CS) {
+        return SPI_NO_CS;
+    }
+    dev = bus->slaves[bus->cur_slave];
+    klass = SPI_SLAVE_GET_CLASS(dev);
+
+    return klass->get_state(dev);
+}
+
+SpiSlaveState spi_send(spi_bus *bus, uint32_t data, int len)
+{
+    SPISlave *dev;
+    SPISlaveClass *klass;
+
+    if (bus->cur_slave == SPI_BUS_NO_CS) {
+        return SPI_NO_CS;
+    }
+    dev = bus->slaves[bus->cur_slave];
+    klass = SPI_SLAVE_GET_CLASS(dev);
+
+    return klass->send(dev, data, len);
+}
+
+SpiSlaveState spi_recv(spi_bus *bus, uint32_t *data)
+{
+    SPISlave *dev;
+    SPISlaveClass *klass;
+
+    if (bus->cur_slave == SPI_BUS_NO_CS) {
+        return SPI_NO_CS;
+    }
+    dev = bus->slaves[bus->cur_slave];
+    klass = SPI_SLAVE_GET_CLASS(dev);
+
+    return klass->recv(dev, data);
+}
+
+const VMStateDescription vmstate_spi_slave = {
+    .name = "SPISlave",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .minimum_version_id_old = 1,
+    .fields      = (VMStateField[]) {
+        VMSTATE_UINT8(cs, SPISlave),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
+static int spi_slave_qdev_init(DeviceState *dev)
+{
+    SPISlave *s = SPI_SLAVE_FROM_QDEV(dev);
+    SPISlaveClass *sc = SPI_SLAVE_GET_CLASS(s);
+
+    return sc->init(s);
+}
+
+static void spi_slave_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *k = DEVICE_CLASS(klass);
+    k->init = spi_slave_qdev_init;
+    k->bus_info = &spi_bus_info;
+}
+
+static TypeInfo spi_slave_type_info = {
+    .name = TYPE_SPI_SLAVE,
+    .parent = TYPE_DEVICE,
+    .instance_size = sizeof(SPISlave),
+    .abstract = true,
+    .class_size = sizeof(SPISlaveClass),
+    .class_init = spi_slave_class_init,
+};
+
+static void spi_slave_register_types(void)
+{
+    type_register_static(&spi_slave_type_info);
+}
+
+type_init(spi_slave_register_types)
hw/spi.h new file +0 -0
new file mode 100644
index 0000000..668e9b0

hw/spi.h line 6

Quoted 6 lines
hw/spi.h new file +6 -0
@@ -0,0 +1,86 @@
+#ifndef QEMU_SPI_H
+#define QEMU_SPI_H
+
+#include "qdev.h"
+
+/* pass to spi_set_cs to deslect all devices on bus */

"deselect"

hw/spi.h line 11

Quoted 5 lines
hw/spi.h new file +5 -0
+
+#define SPI_BUS_NO_CS 0xFF
+
+/* state of a SPI device,
+ * SPI_NO_CS -> the CS pin in de-asserted -> device is tristated

"is"

hw/spi.h line 20

Quoted 9 lines
hw/spi.h new file +9 -0
+ * SPI_IDLE -> CS is asserted and device ready to recv
+ * SPI_DATA_PENDING -> CS is asserted and the device has pushed data to master
+ */
+
+typedef enum {
+    SPI_NO_CS,
+    SPI_IDLE,
+    SPI_DATA_PENDING
+} SpiSlaveState;

Why not SPISlaveState ?

Quoted 42 lines
hw/spi.h new file +42 -0
+
+typedef struct SPISlave {
+    DeviceState qdev;
+    uint8_t cs;
+} SPISlave;
+
+#define TYPE_SPI_SLAVE "spi-slave"
+#define SPI_SLAVE(obj) \
+     OBJECT_CHECK(SPISlave, (obj), TYPE_SPI_SLAVE)
+#define SPI_SLAVE_CLASS(klass) \
+     OBJECT_CLASS_CHECK(SPISlaveClass, (klass), TYPE_SPI_SLAVE)
+#define SPI_SLAVE_GET_CLASS(obj) \
+     OBJECT_GET_CLASS(SPISlaveClass, (obj), TYPE_SPI_SLAVE)
+
+typedef struct SPISlaveClass {
+    DeviceClass parent_class;
+
+    /* Callbacks provided by the device.  */
+    int (*init)(SPISlave *s);
+
+    /* change the cs pin state */
+    void (*cs)(SPISlave *s, uint8_t select);
+
+    /* Master to slave.  */
+    SpiSlaveState (*send)(SPISlave *s, uint32_t data, int len);
+
+    /* Slave to master.  */
+    SpiSlaveState (*recv)(SPISlave *s, uint32_t *data);
+
+    /* poll the spi device state */
+    SpiSlaveState (*get_state)(SPISlave *s);
+} SPISlaveClass;
+
+#define SPI_SLAVE_FROM_QDEV(dev) DO_UPCAST(SPISlave, qdev, dev)
+#define FROM_SPI_SLAVE(type, dev) DO_UPCAST(type, spi, dev)
+
+extern const VMStateDescription vmstate_spi_slave;
+
+#define VMSTATE_SPI_SLAVE(_field, _state) {                          \
+    .name       = (stringify(_field)),                               \
+    .size       = sizeof(SPISlave),                                  \
+    .vmsd       = &vmstate_spi_slave,                                \

hw/spi.h line 72

Quoted 10 lines
hw/spi.h new file +10 -0
+    .flags      = VMS_STRUCT,                                        \
+    .offset     = vmstate_offset_value(_state, _field, SPISlave),    \
+}
+
+typedef struct spi_bus {
+    BusState qbus;
+    SPISlave **slaves;
+    uint8_t num_slaves;
+    uint8_t cur_slave;
+} spi_bus;

CODING_STYLE demands camelcase for type names, so SPIBus.

hw/spi.h line 84

Quoted 12 lines
hw/spi.h new file +12 -0
+
+/* create a new spi bus */
+spi_bus *spi_init_bus(DeviceState *parent, int num_slaves, const char *name);
+int spi_attach_slave(spi_bus *bus, SPISlave *s, int cs);
+
+/* change the chip select. Return 1 on failure. */
+int spi_set_cs(spi_bus *bus, int cs);
+int spi_get_cs(spi_bus *bus);
+SpiSlaveState spi_get_state(spi_bus *bus);
+
+SpiSlaveState spi_send(spi_bus *bus, uint32_t data, int len);
+SpiSlaveState spi_recv(spi_bus *bus, uint32_t *data);

I'm no SPI expert, but a bit of googling suggests that it's a synchronous duplex bus, so you always send a byte of data to the slave and get one back in return (even if for some slaves it might be random garbage). The current OMAP SPI devices in QEMU master have an API that reflects this: eg tsc210x_txrx() which takes an input value and returns an output value. This API seems to have separate send and recv methods -- can you explain how this works?

(Incidentally if we're going to qdevify the SPI interface we should also convert the existing omap SPI devices...)

thanks
-- PMM

#5 lore raw

Re: [Qemu-devel] [RFC PATCH v1 1/4] SPI: initial support

Quoted 9 lines
Quoted 9 lines
hw/spi.h new file +9 -0
+
+/* create a new spi bus */
+spi_bus *spi_init_bus(DeviceState *parent, int num_slaves, const char *name);
+int spi_attach_slave(spi_bus *bus, SPISlave *s, int cs);
+
+/* change the chip select. Return 1 on failure. */
+int spi_set_cs(spi_bus *bus, int cs);
+int spi_get_cs(spi_bus *bus);
+SpiSlaveState spi_get_state(spi_bus *bus);
Quoted 8 lines

hw/spi.h line 84

Quoted 3 lines
hw/spi.h new file +3 -0
+
+SpiSlaveState spi_send(spi_bus *bus, uint32_t data, int len);
+SpiSlaveState spi_recv(spi_bus *bus, uint32_t *data);

I'm no SPI expert, but a bit of googling suggests that it's a synchronous duplex bus, so you always send a byte of data to the slave and get one back in return (even if for some slaves it might be random garbage).

Hi Peter,

Thats not correct, SPI txs can trigger rxs of arbitrary length. Heres the data sheet for the m25p80 spi flash device:
www.datasheetcatalog.org/datasheet/stmicroelectronics/8495.pdf. Figure 12, page 15 is a good example of this, the master transmits 4 bytes back to back for a opcode and an addresses, and the slave bursts the entire playload back. To hack around this, Edgar had to put escape sequences in that txrx api to flag all the txs and rxs for devices and controllers respecitvely, ive attached a file (xilinx_spi.h) which is Eds original hack of the txrx interface that im trying to fix here by changing the API.

This api allows controllers to manage that without dummy txs or invalid rxs.

The current OMAP SPI devices in QEMU

Quoted 5 lines

master have an API that reflects this: eg tsc210x_txrx() which takes an input value and returns an output value. This API seems to have separate send and recv methods -- can you explain how this works?

Each send or receive command will return a enum SPISlaveState. If SPI_DATA_PENDING the master can receive a byte back from the slave with recv. If that recv returns SPI_DATA_PENDING then the master should recieve again i.e. recv should be looped. Heres how its handled in the xilinx_spi controller:

    uint32_t value;
    SpiSlaveState st;

    while (!txfifo_empty(s)) {
        value = txfifo_get(s);
        DB_PRINT("data transfer:%x\n", value);
        st = spi_send(s->spi, value, 8);
        while (st == SPI_DATA_PENDING) {
            uint32_t d;
            st = spi_recv(s->spi, &d);
            rxfifo_put(s, d);
        }
    }

The inner loop is basically a poll, waiting for the recv to return != SPI_DATA_PENDING.

Quoted 2 lines

(Incidentally if we're going to qdevify the SPI interface we should also convert the existing omap SPI devices...)

Yes I havent looked into the details, but shouldnt be too much work. I can provide the diff for the m25p80 to change it over from txrx style to this, to give an idea of the change pattern. Little bit of this that and the other in the machine models too.

If we really want to minimise work, the old txrx function can be added (to spi.c) as something like:

uint32_t spi_txrx(spi_bus *bus, uint32_t data) {
      uint32_t ret;

      assert(spi_send(bus, data) == SPI_DATA_PENDING);
      assert(spi_recv(bus, &ret) == SPI_DATA_IDLE);
      return ret;
}
Quoted 3 lines

thanks
-- PMM

Regards,
Peter

/* SPI connection. */

#define SPI_LEN_UNSELECT -1 /* Used to simulate CS. */
#define SPI_LEN_NODATA 0 /* Used to read data without writing. */

#define SPI_DATA_EMPTY (1 << 31)
#define SPI_DATA_PENDING (1 << 30)

static inline int xlx_spi_valid_data(uint32_t data)

{
    return !(data & SPI_DATA_EMPTY);
}

static inline int xlx_spi_data_pending(uint32_t data)

{
    return data & SPI_DATA_PENDING;
}

typedef uint32_t (*spi_push_fn)(void *opaque, uint32_t data, int len);

struct xlx_spi_ch

{
    void *slave[32];
    spi_push_fn txrx[32];
};

struct xlx_spi_device

{
    void * (*init) (void);
    spi_push_fn txrx;
    const char * device_name;
};

static inline void xlx_spi_connect_slave(struct xlx_spi_ch *spich, int nr,
void *slave, spi_push_fn f)

{
    spich->slave[nr] = slave;
    spich->txrx[nr] = f;
}

static inline
uint32_t xlx_spi_txrx(struct xlx_spi_ch *spich, int nr, uint32_t data, int len)

{
    return spich->txrx[nr](spich->slave[nr], data, len);
}
#6 lore raw

Re: [Qemu-devel] [RFC PATCH v1 1/4] SPI: initial support

Hi Peter,

So ive looked into this further, and I think I better see the confusion between us. My api attempt is attempting to abstract away the clock wheras the txrx approach is true'er to the physical interface. This sense of one-sided communication that im trying to model is comming from the fact that device can drive their SDO line to tristate, which effiectly means "no-data". To that end, a compromise between the two API would be to modify the txrx API to allow capturing of this information:

int spi_txrx(uint8_t *tx, uint8_t *txz, uint8_t *rx, uint8_t *rxz, int len);

tx and rx and arbitary length bit-buffers for tx and rx data. txz and rxz and parrell to their two respective buffers, and indicate whether the line is in tristate, i.e. between the two arrays you can infer whether the line is 0, 1 or Z. Len then exactly corresponds to the number of clock edges in the transaction. This would allow encapsulation of all the desired behaviour with minimal modification to the txrx interface and no hacky escape return behaviours.

What your think?

Regards
Peter

Quoted 88 lines · Peter Crosthwaite
Quoted 9 lines
Quoted 9 lines
hw/spi.h new file +9 -0
+
+/* create a new spi bus */
+spi_bus *spi_init_bus(DeviceState *parent, int num_slaves, const char *name);
+int spi_attach_slave(spi_bus *bus, SPISlave *s, int cs);
+
+/* change the chip select. Return 1 on failure. */
+int spi_set_cs(spi_bus *bus, int cs);
+int spi_get_cs(spi_bus *bus);
+SpiSlaveState spi_get_state(spi_bus *bus);
Quoted 8 lines

hw/spi.h line 84

Quoted 3 lines
hw/spi.h new file +3 -0
+
+SpiSlaveState spi_send(spi_bus *bus, uint32_t data, int len);
+SpiSlaveState spi_recv(spi_bus *bus, uint32_t *data);

I'm no SPI expert, but a bit of googling suggests that it's a synchronous duplex bus, so you always send a byte of data to the slave and get one back in return (even if for some slaves it might be random garbage).

Hi Peter,

Thats not correct, SPI txs can trigger rxs of arbitrary length. Heres the data sheet for the m25p80 spi flash device:
www.datasheetcatalog.org/datasheet/stmicroelectronics/8495.pdf. Figure 12, page 15 is a good example of this, the master transmits 4 bytes back to back for a opcode and an addresses, and the slave bursts the entire playload back. To hack around this, Edgar had to put escape sequences in that txrx api to flag all the txs and rxs for devices and controllers respecitvely, ive attached a file (xilinx_spi.h) which is Eds original hack of the txrx interface that im trying to fix here by changing the API.

This api allows controllers to manage that without dummy txs or invalid rxs.

The current OMAP SPI devices in QEMU

Quoted 5 lines

master have an API that reflects this: eg tsc210x_txrx() which takes an input value and returns an output value. This API seems to have separate send and recv methods -- can you explain how this works?

Each send or receive command will return a enum SPISlaveState. If SPI_DATA_PENDING the master can receive a byte back from the slave with recv. If that recv returns SPI_DATA_PENDING then the master should recieve again i.e. recv should be looped. Heres how its handled in the xilinx_spi controller:

   uint32_t value;
   SpiSlaveState st;

   while (!txfifo_empty(s)) {
       value = txfifo_get(s);
       DB_PRINT("data transfer:%x\n", value);
       st = spi_send(s->spi, value, 8);
       while (st == SPI_DATA_PENDING) {
           uint32_t d;
           st = spi_recv(s->spi, &d);
           rxfifo_put(s, d);
       }
   }

The inner loop is basically a poll, waiting for the recv to return != SPI_DATA_PENDING.

Quoted 2 lines

(Incidentally if we're going to qdevify the SPI interface we should also convert the existing omap SPI devices...)

Yes I havent looked into the details, but shouldnt be too much work. I can provide the diff for the m25p80 to change it over from txrx style to this, to give an idea of the change pattern. Little bit of this that and the other in the machine models too.

If we really want to minimise work, the old txrx function can be added (to spi.c) as something like:

uint32_t spi_txrx(spi_bus *bus, uint32_t data) {
     uint32_t ret;

     assert(spi_send(bus, data) == SPI_DATA_PENDING); assert(spi_recv(bus, &ret) == SPI_DATA_IDLE); return ret;
}

Quoted 3 lines

thanks
-- PMM

Regards,
Peter

#7 lore raw

Re: [Qemu-devel] [RFC PATCH v1 1/4] SPI: initial support

In reply to Peter Crosthwaite
Quoted 4 lines
Quoted 4 lines

I'm no SPI expert, but a bit of googling suggests that it's a synchronous duplex bus, so you always send a byte of data to the slave and get one back in return (even if for some slaves it might be random garbage).

Waitaminute. So this is just basic synchronous serial?

We already have an API for this. hw/spi.c.

If you're building higer level protocols they should be layered on top of the SPI, same as smbus is a protocol layer on top of I2C.

Paul

#8 lore raw

Re: [Qemu-devel] [RFC PATCH v1 1/4] SPI: initial support

2012/4/3 Paul Brook <[email protected]>:

Quoted 8 lines
Quoted 4 lines
Quoted 4 lines

I'm no SPI expert, but a bit of googling suggests that it's a synchronous duplex bus, so you always send a byte of data to the slave and get one back in return (even if for some slaves it might be random garbage).

Waitaminute. So this is just basic synchronous serial?

We already have an API for this. hw/spi.c.

Er, no we don't, not in upstream QEMU. You'll notice that the patch we're discussing creates hw/spi.c as a new file...

-- PMM

#9 lore raw

Re: [Qemu-devel] [RFC PATCH v1 1/4] SPI: initial support

Quoted 6 lines

2012/4/3 Paul Brook <[email protected]>:

Quoted 5 lines
Quoted 4 lines
Quoted 4 lines

I'm no SPI expert, but a bit of googling suggests that it's a synchronous duplex bus, so you always send a byte of data to the slave and get one back in return (even if for some slaves it might be random garbage).

Quoted 6 lines
Quoted 3 lines

Waitaminute. So this is just basic synchronous serial?

We already have an API for this. hw/spi.c.

Er, no we don't, not in upstream QEMU. You'll notice that the patch we're discussing creates hw/spi.c as a new file...

Sorry, I mean hw/ssi.c

Synchronous serial is something that pretty much every vendor has their own name for, but in practice they're actually all the same.

Paul

#10 lore raw

Re: [Qemu-devel] [RFC PATCH v1 1/4] SPI: initial support

Hi Paul,

Regarding using ssi, theres a few things that come to mind:

Theres no sense of it being a multi-slave bus, its just a point to point link. SPI devices universally have the notion of the CS pin that tristates the device of the bus. Masters connect to a number of slaves and one-hot-decode the active slave. It would be tedious if all SPI controllers (of which i have two (only pushed one in this series), omap have theirs, Nokia may have something out of tree too), had to implement the\is common CS decoding behaviour. To that end, my SPI bus implementation has some infra-structure for managing this - spi_set_cs(). The spi_bus in this series is single master, multi-slave, where as ssi is point-to-point.

Also as mentioned in earlier discussions with Peter, that api has no way of emulating the CS (sometimes called SS) pin. The m25p80 in this series (and potenitally other devices) has side effect associated with wiggling the cs pin, which can not be encapsulated by that ssi slave interface.

It may be a case though that ssi is a superclass of spi - all SPI devices are SSI devices but not all SSI devices are SPI? To that end can we make SPI a child object of SSI with the desired extra behaviors mentioned in this thread? This kind of stuff is the whole reason for QOM.

Regards,
Peter

Quoted 19 lines · Paul Brook
Quoted 6 lines

2012/4/3 Paul Brook <[email protected]>:

Quoted 5 lines
Quoted 4 lines
Quoted 4 lines

I'm no SPI expert, but a bit of googling suggests that it's a synchronous duplex bus, so you always send a byte of data to the slave and get one back in return (even if for some slaves it might be random garbage).

Quoted 6 lines
Quoted 3 lines

Waitaminute. So this is just basic synchronous serial?

We already have an API for this. hw/spi.c.

Er, no we don't, not in upstream QEMU. You'll notice that the patch we're discussing creates hw/spi.c as a new file...

Sorry, I mean hw/ssi.c

Synchronous serial is something that pretty much every vendor has their own name for, but in practice they're actually all the same.

Paul

#11 lore raw

Re: [Qemu-devel] [RFC PATCH v1 1/4] SPI: initial support

Quoted 21 lines

Hi Paul,

Regarding using ssi, theres a few things that come to mind:

Theres no sense of it being a multi-slave bus, its just a point to point link. SPI devices universally have the notion of the CS pin that tristates the device of the bus. Masters connect to a number of slaves and one-hot-decode the active slave. It would be tedious if all SPI controllers (of which i have two (only pushed one in this series), omap have theirs, Nokia may have something out of tree too), had to implement the\is common CS decoding behaviour. To that end, my SPI bus implementation has some infra-structure for managing this - spi_set_cs(). The spi_bus in this series is single master, multi-slave, where as ssi is point-to-point.

Also as mentioned in earlier discussions with Peter, that api has no way of emulating the CS (sometimes called SS) pin. The m25p80 in this series (and potenitally other devices) has side effect associated with wiggling the cs pin, which can not be encapsulated by that ssi slave interface.

Quoted 5 lines

It may be a case though that ssi is a superclass of spi - all SPI devices are SSI devices but not all SSI devices are SPI? To that end can we make SPI a child object of SSI with the desired extra behaviors mentioned in this thread? This kind of stuff is the whole reason for QOM.

I don't believe there is any difference between SSI and SPI. It's the exact same thing - the same way that many devices support a "two-wire interface" that is actually just I2C with a different name.

The behavior of the CS pin varies between devices. It sounds like you need a bit of extra logic not present in the current ssi code. You should fix that, not invent a whole new bus.

Paul

#12 lore raw

Re: [Qemu-devel] [RFC PATCH v1 1/4] SPI: initial support

Quoted 7 lines
Quoted 6 lines

It may be a case though that ssi is a superclass of spi - all SPI devices are SSI devices but not all SSI devices are SPI? To that end can we make SPI a child object of SSI with the desired extra behaviors mentioned in this thread? This kind of stuff is the whole reason for QOM.

Quoted 7 lines

I don't believe there is any difference between SSI and SPI.  It's the exact same thing - the same way that many devices support a "two-wire interface" that is actually just I2C with a different name.

The behavior of the CS pin varies between devices.  It sounds like you need a bit of extra logic not present in the current ssi code.  You should fix that, not invent a whole new bus.

Ok then in that case we are extending SSI to have these as optional API features. Before withing any implementation, can I get some consensus on the following approach:

1: extend SSISlave to have the set_cs fuction:

hw/ssi.h +1 -0
@@ -29,6 +29,7 @@ typedef struct SSISlaveClass {
 
     int (*init)(SSISlave *dev);
     uint32_t (*transfer)(SSISlave *dev, uint32_t val);
+    int (*set_cs)(SSISlave *dev, int state);
 } SSISlaveClass;
 
 struct SSISlave {

2: set up SSI bus as a multi-slave bus:

hw/ssi.h +3 -2
@@ -38,12 +38,13 @@ struct SSISlave {
 #define SSI_SLAVE_FROM_QDEV(dev) DO_UPCAST(SSISlave, qdev, dev)
 #define FROM_SSI_SLAVE(type, dev) DO_UPCAST(type, ssidev, dev)
 
-DeviceState *ssi_create_slave(SSIBus *bus, const char *name);
+DeviceState *ssi_create_slave(SSIBus *bus, const char *name, int slave);
 
 /* Master interface.  */
-SSIBus *ssi_create_bus(DeviceState *parent, const char *name);
+SSIBus *ssi_create_bus(DeviceState *parent, const char *name, int num_slaves);
 
 uint32_t ssi_transfer(SSIBus *bus, uint32_t val);
+uint32_t ssi_select_slave(SSIBus *bus, int slave);
 
 /* max111x.c */
 

3: add support for flagging sdi/sdo bits as tristated:

hw/ssi.h +5 -1
@@ -28,7 +28,11 @@ typedef struct SSISlaveClass {
     DeviceClass parent_class;
 
     int (*init)(SSISlave *dev);
-    uint32_t (*transfer)(SSISlave *dev, uint32_t val);
+    /* transfer data. If z if provided, *z is the tristate mask
+     * It is initially populated with tristate value for tx, and
+     * on return should be populated with tristate for rx
+     */
+    uint32_t (*transfer)(SSISlave *dev, uint32_t val, uint32_t *z);
 } SSISlaveClass;
 
Quoted 2 lines

Paul

Peter

#13 lore raw

Re: [Qemu-devel] [RFC PATCH v1 1/4] SPI: initial support

Quoted 5 lines · Peter Crosthwaite

3: add support for flagging sdi/sdo bits as tristated:

hw/ssi.h typedef struct SSISlaveClass { · line 36

Quoted 10 lines
hw/ssi.h +5 -1
@@ -28,7 +28,11 @@ typedef struct SSISlaveClass {
     DeviceClass parent_class;
 
     int (*init)(SSISlave *dev);
-    uint32_t (*transfer)(SSISlave *dev, uint32_t val);
+    /* transfer data. If z if provided, *z is the tristate mask
+     * It is initially populated with tristate value for tx, and
+     * on return should be populated with tristate for rx
+     */
+    uint32_t (*transfer)(SSISlave *dev, uint32_t val, uint32_t *z);
  } SSISlaveClass;

I'm not sure what you intend the tristate masks to be used for? If the sending device puts the line into high-impedance presumably the receiving device is just going to read something random which it's supposed to ignore, so the sender could just send whatever it likes. We're not modelling multiple devices sending at once, which is the only case I can think of where you'd need to care which bits each device was tristating for.

-- PMM

#14 lore raw

Re: [Qemu-devel] [RFC PATCH v1 1/4] SPI: initial support

In reply to Peter Maydell
Quoted 6 lines · Peter Maydell
Quoted 1 line · Peter A. G. Crosthwaite

defined spi bus and spi slave QOM interfaces. Inspired by and loosley based on

"Define"; "loosely".

ack

Quoted 11 lines
Quoted 11 lines

existing I2C framework.

---
 Makefile.target |    1 +
 hw/spi.c        |  175 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ hw/spi.h        |   86 +++++++++++++++++++++++++++

 3 files changed, 262 insertions(+), 0 deletions(-)

 create mode 100644 hw/spi.c
 create mode 100644 hw/spi.h

Quoted 13 lines
Quoted 9 lines
Makefile.target +0 -0
index 44b2e83..8fd3718 100644
@@ -320,6 +320,7 @@ obj-mips-$(CONFIG_FULONG) += bonito.o vt82c686.o mips_fulong2e.o

 obj-microblaze-y = petalogix_s3adsp1800_mmu.o
 obj-microblaze-y += petalogix_ml605_mmu.o
 obj-microblaze-y += microblaze_boot.o
+obj-microblaze-y += spi.o

This should be in common-obj-y with i2c.o, I think -- microblaze isn't the only target with SPI (eg hw/omap_spi.c).

ok

Quoted 198 lines
Quoted 189 lines

 obj-microblaze-y += microblaze_pic_cpu.o
 obj-microblaze-y += xilinx_intc.o

hw/spi.c new file +175 -0
new file mode 100644
index 0000000..3afef07
@@ -0,0 +1,175 @@
+/*
+ * QEMU SPI bus interface.
+ *
+ * Copyright (C) 2012 Peter A. G. Crosthwaite <[email protected]>
+ * Copyright (C) 2012 PetaLogix
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version
+ * 2 of the License, or (at your option) any later version.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "spi.h"
+
+static struct BusInfo spi_bus_info = {
+    .name = "SPI",
+    .size = sizeof(spi_bus)
+};
+
+static const VMStateDescription vmstate_spi_bus = {
+    .name = "spi_bus",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .minimum_version_id_old = 1,
+    .fields      = (VMStateField[]) {
+        VMSTATE_UINT8(cur_slave, spi_bus),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
+spi_bus *spi_init_bus(DeviceState *parent, int num_slaves, const char *name)
+{
+    spi_bus *bus;
+
+    bus = FROM_QBUS(spi_bus, qbus_create(&spi_bus_info, parent, name));
+    if (num_slaves >= SPI_BUS_NO_CS) {
+        hw_error("too many slaves on spi bus: %d\n", num_slaves);
+    }
+    bus->num_slaves = num_slaves;
+    bus->slaves = g_malloc0(sizeof(*bus->slaves) * num_slaves);
+    vmstate_register(NULL, -1, &vmstate_spi_bus, bus);
+    return bus;
+}
+
+int spi_attach_slave(spi_bus *bus, SPISlave *slave, int cs)
+{
+    if (bus->slaves[cs]) {
+        return 1;
+    }
+    bus->slaves[cs] = slave;
+    return 0;
+}
+
+int spi_set_cs(spi_bus *bus, int cs)
+{
+    SPISlave *dev;
+    SPISlaveClass *klass;
+
+    if (bus->cur_slave == cs) {
+        return 0;
+    }
+
+    if (bus->cur_slave != SPI_BUS_NO_CS) {
+        dev = bus->slaves[bus->cur_slave];
+        dev->cs = 0;
+        klass = SPI_SLAVE_GET_CLASS(dev);
+        klass->cs(dev, 0);
+    }
+
+    if (cs >= bus->num_slaves && cs != SPI_BUS_NO_CS) {
+        hw_error("attempted to assert non existent spi CS line: %d\n", cs);
+    }
+
+    bus->cur_slave = (uint8_t)cs;
+
+    if (cs != SPI_BUS_NO_CS) {
+        dev = bus->slaves[cs];
+        dev->cs = 1;
+        klass = SPI_SLAVE_GET_CLASS(dev);
+        klass->cs(dev, 1);
+    }
+    return 0;
+};
+
+int spi_get_cs(spi_bus *bus)
+{
+    return bus->cur_slave;
+}
+
+SpiSlaveState spi_get_state(spi_bus *bus)
+{
+    SPISlave *dev;
+    SPISlaveClass *klass;
+
+    if (bus->cur_slave == SPI_BUS_NO_CS) {
+        return SPI_NO_CS;
+    }
+    dev = bus->slaves[bus->cur_slave];
+    klass = SPI_SLAVE_GET_CLASS(dev);
+
+    return klass->get_state(dev);
+}
+
+SpiSlaveState spi_send(spi_bus *bus, uint32_t data, int len)
+{
+    SPISlave *dev;
+    SPISlaveClass *klass;
+
+    if (bus->cur_slave == SPI_BUS_NO_CS) {
+        return SPI_NO_CS;
+    }
+    dev = bus->slaves[bus->cur_slave];
+    klass = SPI_SLAVE_GET_CLASS(dev);
+
+    return klass->send(dev, data, len);
+}
+
+SpiSlaveState spi_recv(spi_bus *bus, uint32_t *data)
+{
+    SPISlave *dev;
+    SPISlaveClass *klass;
+
+    if (bus->cur_slave == SPI_BUS_NO_CS) {
+        return SPI_NO_CS;
+    }
+    dev = bus->slaves[bus->cur_slave];
+    klass = SPI_SLAVE_GET_CLASS(dev);
+
+    return klass->recv(dev, data);
+}
+
+const VMStateDescription vmstate_spi_slave = {
+    .name = "SPISlave",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .minimum_version_id_old = 1,
+    .fields      = (VMStateField[]) {
+        VMSTATE_UINT8(cs, SPISlave),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
+static int spi_slave_qdev_init(DeviceState *dev)
+{
+    SPISlave *s = SPI_SLAVE_FROM_QDEV(dev);
+    SPISlaveClass *sc = SPI_SLAVE_GET_CLASS(s);
+
+    return sc->init(s);
+}
+
+static void spi_slave_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *k = DEVICE_CLASS(klass);
+    k->init = spi_slave_qdev_init;
+    k->bus_info = &spi_bus_info;
+}
+
+static TypeInfo spi_slave_type_info = {
+    .name = TYPE_SPI_SLAVE,
+    .parent = TYPE_DEVICE,
+    .instance_size = sizeof(SPISlave),
+    .abstract = true,
+    .class_size = sizeof(SPISlaveClass),
+    .class_init = spi_slave_class_init,
+};
+
+static void spi_slave_register_types(void)
+{
+    type_register_static(&spi_slave_type_info);
+}
+
+type_init(spi_slave_register_types)
hw/spi.h new file +0 -0
new file mode 100644
index 0000000..668e9b0

hw/spi.h line 6

Quoted 6 lines
hw/spi.h new file +6 -0
@@ -0,0 +1,86 @@
+#ifndef QEMU_SPI_H
+#define QEMU_SPI_H
+
+#include "qdev.h"
+
+/* pass to spi_set_cs to deslect all devices on bus */

"deselect"

Quoted 8 lines

hw/spi.h line 11

Quoted 5 lines
hw/spi.h new file +5 -0
+
+#define SPI_BUS_NO_CS 0xFF
+
+/* state of a SPI device,
+ * SPI_NO_CS -> the CS pin in de-asserted -> device is tristated

"is"

ack

Quoted 12 lines

hw/spi.h line 20

Quoted 9 lines
hw/spi.h new file +9 -0
+ * SPI_IDLE -> CS is asserted and device ready to recv
+ * SPI_DATA_PENDING -> CS is asserted and the device has pushed data to master
+ */
+
+typedef enum {
+    SPI_NO_CS,
+    SPI_IDLE,
+    SPI_DATA_PENDING
+} SpiSlaveState;

Why not SPISlaveState ?

ack

Quoted 46 lines
Quoted 46 lines
hw/spi.h new file +46 -0
+
+typedef struct SPISlave {
+    DeviceState qdev;
+    uint8_t cs;
+} SPISlave;
+
+#define TYPE_SPI_SLAVE "spi-slave"
+#define SPI_SLAVE(obj) \
+     OBJECT_CHECK(SPISlave, (obj), TYPE_SPI_SLAVE)
+#define SPI_SLAVE_CLASS(klass) \
+     OBJECT_CLASS_CHECK(SPISlaveClass, (klass), TYPE_SPI_SLAVE)
+#define SPI_SLAVE_GET_CLASS(obj) \
+     OBJECT_GET_CLASS(SPISlaveClass, (obj), TYPE_SPI_SLAVE)
+
+typedef struct SPISlaveClass {
+    DeviceClass parent_class;
+
+    /* Callbacks provided by the device.  */
+    int (*init)(SPISlave *s);
+
+    /* change the cs pin state */
+    void (*cs)(SPISlave *s, uint8_t select);
+
+    /* Master to slave.  */
+    SpiSlaveState (*send)(SPISlave *s, uint32_t data, int len);
+
+    /* Slave to master.  */
+    SpiSlaveState (*recv)(SPISlave *s, uint32_t *data);
+
+    /* poll the spi device state */
+    SpiSlaveState (*get_state)(SPISlave *s);
+} SPISlaveClass;
+
+#define SPI_SLAVE_FROM_QDEV(dev) DO_UPCAST(SPISlave, qdev, dev)
+#define FROM_SPI_SLAVE(type, dev) DO_UPCAST(type, spi, dev)
+
+extern const VMStateDescription vmstate_spi_slave;
+
+#define VMSTATE_SPI_SLAVE(_field, _state) {                          \
+    .name       = (stringify(_field)),                               \
+    .size       = sizeof(SPISlave),                                  \
+    .vmsd       = &vmstate_spi_slave,                                \
+    .flags      = VMS_STRUCT,                                        \
+    .offset     = vmstate_offset_value(_state, _field, SPISlave),    \
+}
+
Quoted 9 lines

hw/spi.h line 72

Quoted 6 lines
hw/spi.h new file +6 -0
+typedef struct spi_bus {
+    BusState qbus;
+    SPISlave **slaves;
+    uint8_t num_slaves;
+    uint8_t cur_slave;
+} spi_bus;

CODING_STYLE demands camelcase for type names, so SPIBus.

Ok, I have a related question tho, if camel casing with acronyms, should a space perhaps be inserted after for readability? As in SPI_Bus rather than SPIBus.

Quoted 27 lines

hw/spi.h line 84

Quoted 12 lines
hw/spi.h new file +12 -0
+
+/* create a new spi bus */
+spi_bus *spi_init_bus(DeviceState *parent, int num_slaves, const char *name);
+int spi_attach_slave(spi_bus *bus, SPISlave *s, int cs);
+
+/* change the chip select. Return 1 on failure. */
+int spi_set_cs(spi_bus *bus, int cs);
+int spi_get_cs(spi_bus *bus);
+SpiSlaveState spi_get_state(spi_bus *bus);
+
+SpiSlaveState spi_send(spi_bus *bus, uint32_t data, int len);
+SpiSlaveState spi_recv(spi_bus *bus, uint32_t *data);

I'm no SPI expert, but a bit of googling suggests that it's a synchronous duplex bus, so you always send a byte of data to the slave and get one back in return (even if for some slaves it might be random garbage). The current OMAP SPI devices in QEMU master have an API that reflects this: eg tsc210x_txrx() which takes an input value and returns an output value. This API seems to have separate send and recv methods -- can you explain how this works?

(Incidentally if we're going to qdevify the SPI interface we should also convert the existing omap SPI devices...)

thanks
-- PMM

#15 lore raw

Re: [Qemu-devel] [RFC PATCH v1 1/4] SPI: initial support

Am 03.04.2012 01:57, schrieb Peter Crosthwaite:

Quoted 13 lines · Peter Maydell
Quoted 11 lines

hw/spi.h line 72

Quoted 6 lines · Peter A. G. Crosthwaite
hw/spi.h new file +6 -0
+typedef struct spi_bus {
+    BusState qbus;
+    SPISlave **slaves;
+    uint8_t num_slaves;
+    uint8_t cur_slave;
+} spi_bus;

CODING_STYLE demands camelcase for type names, so SPIBus.

Quoted 3 lines

Ok, I have a related question tho, if camel casing with acronyms, should a space perhaps be inserted after for readability? As in SPI_Bus rather than SPIBus.

Negative, we have PCIDevice, I2CSlave, etc.

The underscore is used in macro names, such as X86_CPU() vs. X86CPU. If we allow underscores in type names as word separator then sooner or later we'll run into the same ugly uppercase name conflicts that caused the huge'ish CPUState refactoring.

.NET avoids four consecutive uppercase letter by writing, e.g., XmlDocument. So far we have rather adopted the older (e.g., Java) model of not lower-casing acronyms. My preference would be for consistency.

Andreas

Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
#16 lore raw

RFC PATCH 2/4 m25p80: initial verion

In reply to Peter A. G. Crosthwaite

Added device model for m25p80 SPI flash

---
 Makefile.target |    1 +
 hw/m25p80.c     |  495 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 496 insertions(+), 0 deletions(-)
 create mode 100644 hw/m25p80.c
Makefile.target +1 -0
index 8fd3718..fcccf1b 100644
@@ -321,6 +321,7 @@ obj-microblaze-y = petalogix_s3adsp1800_mmu.o
 obj-microblaze-y += petalogix_ml605_mmu.o
 obj-microblaze-y += microblaze_boot.o
 obj-microblaze-y += spi.o
+obj-microblaze-y += m25p80.o
 
 obj-microblaze-y += microblaze_pic_cpu.o
 obj-microblaze-y += xilinx_intc.o
hw/m25p80.c new file +495 -0

495 lines not on this page · raw message

Signature · 1 line
1.7.3.2
#17 lore raw

Re: [Qemu-devel] [RFC PATCH v1 2/4] m25p80: initial verion

hw/m25p80.c line 97

Quoted 10 lines · Peter A. G. Crosthwaite
hw/m25p80.c new file +10 -0
+static void flash_sync_page(struct flash *s, int page)
+{
+    if (s->bdrv) {
+        int bdrv_sector;
+        int offset;
+
+        bdrv_sector = (page * s->pagesize) / 512;
+        offset = bdrv_sector * 512;
+        bdrv_write(s->bdrv, bdrv_sector,
+                   s->storage + offset, (s->pagesize + 511) / 512);

Devices should not use synchronous block I/O interfaces. sd, flash, and a couple others still do for historical reasons but new devices should not.

The vcpu, QEMU monitor, and VNC are all blocked while I/O takes place. This can be avoided by using bdrv_aio_writev() instead.

Can you change this code to use bdrv_aio_writev() or is this flash device specified to complete operations within certain time constraints? (The problem is that the image file could be on a slow harddisk or other media that don't meet those timing requirements.)

Quoted 5 lines
hw/m25p80.c new file +5 -0
+static int m25p80_init(SPISlave *ss)
+{
+    DriveInfo *dinfo;
+    struct flash *s = FROM_SPI_SLAVE(struct flash, ss);
+    /* FIXME: This should be handled centrally!  */

hw/m25p80.c line 452

Quoted 10 lines
hw/m25p80.c new file +10 -0
+    static int mtdblock_idx;
+    dinfo = drive_get(IF_MTD, 0, mtdblock_idx++);
+
+    DB_PRINT("inited m25p80 device model - dinfo = %p\n", dinfo);
+    /* TODO: parameterize */
+    s->size = 8 * 1024 * 1024;
+    s->pagesize = 256;
+    s->sectorsize = 4 * 1024;
+    s->dirty_page = -1;
+    s->storage = g_malloc0(s->size);

Please use qemu_blockalign(s->bdrv, s->size) to allocate I/O buffers. It honors memory alignment requirements (necessary for O_DIRECT files).

Stefan

#18 lore raw

Re: [Qemu-devel] [RFC PATCH v1 2/4] m25p80: initial verion

In reply to Peter A. G. Crosthwaite

Am 30.03.2012 08:37, schrieb Peter A. G. Crosthwaite:

Quoted 27 lines

Added device model for m25p80 SPI flash

---
 Makefile.target |    1 +
 hw/m25p80.c     |  495 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 496 insertions(+), 0 deletions(-)
 create mode 100644 hw/m25p80.c
Makefile.target +1 -0
index 8fd3718..fcccf1b 100644
@@ -321,6 +321,7 @@ obj-microblaze-y = petalogix_s3adsp1800_mmu.o
 obj-microblaze-y += petalogix_ml605_mmu.o
 obj-microblaze-y += microblaze_boot.o
 obj-microblaze-y += spi.o
+obj-microblaze-y += m25p80.o
 
 obj-microblaze-y += microblaze_pic_cpu.o
 obj-microblaze-y += xilinx_intc.o
hw/m25p80.c new file +0 -0
new file mode 100644
index 0000000..2b67375

hw/m25p80.c line 2

Quoted 2 lines
hw/m25p80.c new file +2 -0
@@ -0,0 +1,495 @@
+/*
+ * ST M25P80 emulator.

[snip]

A device by ST does not sound microblaze-specific and in that case, similar to the recent Atmel maxtouch device, should go into hw-obj-$(CONFIG_M25P80) instead so that it's compiled only once for microblaze-softmmu and microblazeel-softmmu.

Same for spi.o since it implements a generic bus AFAIU.

Andreas

Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
#19 lore raw

Re: [Qemu-devel] [RFC PATCH v1 2/4] m25p80: initial verion

Quoted 32 lines · Andreas Färber

Am 30.03.2012 08:37, schrieb Peter A. G. Crosthwaite:

Quoted 29 lines

Added device model for m25p80 SPI flash

---
 Makefile.target |    1 +
 hw/m25p80.c     |  495 +++++++++++++++++++++++++++++++++++++++++++++++++++++++

 2 files changed, 496 insertions(+), 0 deletions(-)

 create mode 100644 hw/m25p80.c

Makefile.target +0 -0
index 8fd3718..fcccf1b 100644
@@ -321,6 +321,7 @@ obj-microblaze-y = petalogix_s3adsp1800_mmu.o

 obj-microblaze-y += petalogix_ml605_mmu.o
 obj-microblaze-y += microblaze_boot.o
 obj-microblaze-y += spi.o
+obj-microblaze-y += m25p80.o

 obj-microblaze-y += microblaze_pic_cpu.o
 obj-microblaze-y += xilinx_intc.o

hw/m25p80.c new file +2 -0
new file mode 100644
index 0000000..2b67375
@@ -0,0 +1,495 @@
+/*
+ * ST M25P80 emulator.

[snip]

Quoted 5 lines

A device by ST does not sound microblaze-specific and in that case, similar to the recent Atmel maxtouch device, should go into hw-obj-$(CONFIG_M25P80) instead so that it's compiled only once for microblaze-softmmu and microblazeel-softmmu.

Hi Andreas,

I have regenerated this series, but have not actioned this just yet. I think adding a config switch is probably not the right solution, as its just a device model. If every device model has a config switch then isnt creating everyone config process going to be excessively tedious? perhaps it can just live next to obj-y=ssi.o? I.E. if you have SSI, then you have m25p80. I cant think of a case where you would want SSI but need to exclude m25p80 from the build.

Regards,
Peter

Quoted 7 lines

Same for spi.o since it implements a generic bus AFAIU.

Andreas

--
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg

#20 lore raw

Re: [Qemu-devel] [RFC PATCH v1 2/4] m25p80: initial verion

Am 05.06.2012 02:47, schrieb Peter Crosthwaite:

Quoted 41 lines · Andreas Färber
Quoted 32 lines

Am 30.03.2012 08:37, schrieb Peter A. G. Crosthwaite:

Quoted 29 lines

Added device model for m25p80 SPI flash

---
 Makefile.target |    1 +
 hw/m25p80.c     |  495 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 496 insertions(+), 0 deletions(-)
 create mode 100644 hw/m25p80.c
Makefile.target +1 -0
index 8fd3718..fcccf1b 100644
@@ -321,6 +321,7 @@ obj-microblaze-y = petalogix_s3adsp1800_mmu.o
 obj-microblaze-y += petalogix_ml605_mmu.o
 obj-microblaze-y += microblaze_boot.o
 obj-microblaze-y += spi.o
+obj-microblaze-y += m25p80.o
 
 obj-microblaze-y += microblaze_pic_cpu.o
 obj-microblaze-y += xilinx_intc.o
hw/m25p80.c new file +2 -0
new file mode 100644
index 0000000..2b67375
@@ -0,0 +1,495 @@
+/*
+ * ST M25P80 emulator.

[snip]

Quoted 5 lines

A device by ST does not sound microblaze-specific and in that case, similar to the recent Atmel maxtouch device, should go into hw-obj-$(CONFIG_M25P80) instead so that it's compiled only once for microblaze-softmmu and microblazeel-softmmu.

Hi Andreas,

Quoted 7 lines

I have regenerated this series, but have not actioned this just yet. I think adding a config switch is probably not the right solution, as its just a device model. If every device model has a config switch then isnt creating everyone config process going to be excessively tedious? perhaps it can just live next to obj-y=ssi.o? I.E. if you have SSI, then you have m25p80. I cant think of a case where you would want SSI but need to exclude m25p80 from the build.

I think you're missing the point here. Whenever I touch, e.g., include/qemu/object.h, it takes an awfully long time to rebuild all targets because, among others, you are unneccessarily duplicating device model objects between microblaze and microblazeel. Devices by definition do not depend on target endianness (they specify their endianness in code) and should be built in libhw32/64. The mechanism to do so is defining flags in default-configs/microblaze[el]-softmmu.mak. For example, I had locally started a CONFIG_XILINX iirc, to share devices between microblaze and ppc4xx, then there might be CONFIG_XILINX_PPC and CONFIG_XILINX_MICROBLAZE for target-specific ones. My suggestion here was to use CONFIG_M25P80 but if you have a better grouping that would be fine, too. CONFIG_SSI maybe? Or if the amount of such devices is large and to be shared across targets you might consider your own Makefile like default-configs/pci.mak that can be included from multiple *-softmmu.mak files.

Note however that Paolo has posted a series on the list refactoring the whole Makefile system with which any such changes / additions clash. Cc'ing Paolo so he can comment on the intended timing.

Andreas

Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
#21 lore raw

RFC PATCH 3/4 xilinx_spi: initial version

In reply to Peter A. G. Crosthwaite

device model for xilinx XPS SPI controller (v2.0)

---
 Makefile.target |    1 +
 hw/xilinx_spi.c |  477 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 478 insertions(+), 0 deletions(-)
 create mode 100644 hw/xilinx_spi.c
Makefile.target +1 -0
index fcccf1b..15edfa4 100644
@@ -322,6 +322,7 @@ obj-microblaze-y += petalogix_ml605_mmu.o
 obj-microblaze-y += microblaze_boot.o
 obj-microblaze-y += spi.o
 obj-microblaze-y += m25p80.o
+obj-microblaze-y += xilinx_spi.o
 
 obj-microblaze-y += microblaze_pic_cpu.o
 obj-microblaze-y += xilinx_intc.o
hw/xilinx_spi.c new file +477 -0

477 lines not on this page · raw message

Signature · 1 line
1.7.3.2
#22 lore raw

RFC PATCH 4/4 petalogix-ml605: added spi controller with m25p80

In reply to Peter A. G. Crosthwaite

Added spi controller to the reference design, with a single cs line and a m25p80 style spi-flash connected

---
 hw/petalogix_ml605_mmu.c |   19 +++++++++++++++++++
 1 files changed, 19 insertions(+), 0 deletions(-)
hw/petalogix_ml605_mmu.c +19 -0
index 31a4348..b2359a8 100644
@@ -36,6 +36,7 @@
 #include "blockdev.h"
 #include "pc.h"
 #include "exec-memory.h"
+#include "spi.h"
 
 #include "microblaze_boot.h"
 #include "microblaze_pic_cpu.h"
@@ -75,6 +76,7 @@ petalogix_ml605_init(ram_addr_t ram_size,
 {
     MemoryRegion *address_space_mem = get_system_memory();
     DeviceState *dev;
+    SysBusDevice *busdev;
     CPUMBState *env;
     DriveInfo *dinfo;
     int i;
@@ -131,6 +133,23 @@ petalogix_ml605_init(ram_addr_t ram_size,
                                      irq[1], irq[0], 100 * 1000000);
     }
 
+    {
+        spi_bus *spi;
+
+        dev = qdev_create(NULL, "xilinx,spi");
+        qdev_prop_set_uint8(dev, "num-cs", 1);
+        qdev_init_nofail(dev);
+        busdev = sysbus_from_qdev(dev);
+        sysbus_mmio_map(busdev, 0, 0x40a00000);
+        sysbus_connect_irq(busdev, 0, irq[6]);
+
+        spi = FROM_QBUS(spi_bus, qdev_get_child_bus(dev, "spi"));
+
+        dev = qdev_create(NULL, "m25p80");
+        qdev_init_nofail(dev);
+        spi_attach_slave(spi, SPI_SLAVE(dev), 0);
+    }
+
     microblaze_load_kernel(env, ddr_base, ram_size, BINARY_DEVICE_TREE_FILE,
                                                             machine_cpu_reset);
 
Signature · 1 line
1.7.3.2