Skip to contentSkip to conversation

hw/ppc: SPI model

PATCH Superseded Chalapathi V 3 participants 15 messages Feb 7, 2024 – Mar 8, 2024 Arm/Amlogic Meson SoC support

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

cover hw/ppc: SPI model

Hello,

In this series of patchset, SPI controller and responder models for Power10 processor are modelled.

Serial peripheral interface provides full-duplex synchronous serial communication between single controller and multiple responder devices.

The current configuration supports a single SPI controller reside on the SPI bus. In p10, SPI controller device model supports a connection to a single SPI responder such as SPI seeproms, TPM, flash device and an ADC controller.

SPI controller model is divided into configuration unit, sequencer FSM and shifter engine. All SPI function control is mapped into the SPI register space to enable full control by firmware.

SPI configuration component is modelled which contains all SPI configuration and status registers as well as the hold registers for data to be sent or having been received.
Shift engine performs serialization and de-serialization according to the control by the sequencer and according to the setup defined in the configuration registers.
Sequencer implements the main control logic and
FSM to handle data transmit and data receive control of the shift engine.

Microchip's 25CSM04 SEEPROM device is modelled and connected to SPI bus "spi_bus2" of SPI controller "PIB_SPIC[2]".

Patches overview in V1.
PATCH1: Create a SPI responder model which includes responder methods
and SPI bus implementation.
PATCH2: Create a SPI controller model and implement configuration unit
to model SCOM registers.
PATCH3: SPI controller model: implement sequencer FSM and shift engine.
PATCH4: create SPI SEEPROM model.
PATCH5: Connect SPI controllers to p10 chip and create keystore seeprom
device on spi_bus2 of PIB_SPIC[2].

Thank You,
Chalapathi

Chalapathi V (5):
hw/ppc: SPI responder model
hw/ppc: SPI controller model - registers implementation hw/ppc: SPI controller model - sequencer and shifter hw/ppc: SPI SEEPROM model
hw/ppc: SPI controller wiring to P10 chip and create seeprom device

 include/hw/ppc/pnv_chip.h           |    4 +
 include/hw/ppc/pnv_spi_controller.h |  101 ++
 include/hw/ppc/pnv_spi_responder.h  |  109 ++
 include/hw/ppc/pnv_spi_seeprom.h    |   70 ++
 include/hw/ppc/pnv_xscom.h          |    3 +
 hw/ppc/pnv.c                        |   32 +
 hw/ppc/pnv_spi_controller.c         | 1609 +++++++++++++++++++++++++++
 hw/ppc/pnv_spi_responder.c          |  166 +++
 hw/ppc/pnv_spi_seeprom.c            |  989 ++++++++++++++++
 hw/ppc/meson.build                  |    3 +
 10 files changed, 3086 insertions(+)
 create mode 100644 include/hw/ppc/pnv_spi_controller.h
 create mode 100644 include/hw/ppc/pnv_spi_responder.h
 create mode 100644 include/hw/ppc/pnv_spi_seeprom.h
 create mode 100644 hw/ppc/pnv_spi_controller.c
 create mode 100644 hw/ppc/pnv_spi_responder.c
 create mode 100644 hw/ppc/pnv_spi_seeprom.c
Signature · 1 line
2.31.1
#1 lore raw

PATCH 1/5 hw/ppc: SPI responder model

Serial pheripheral interface provides full-duplex synchronous serial communication between single controller and multiple responder devices. One SPI Controller is implemented and supported on a SPI Bus, there is no support for multiple controllers on the SPI bus.

The current implemetation assumes that single responder is connected to bus, hence chip_select is not modelled.

---
 include/hw/ppc/pnv_spi_responder.h | 109 +++++++++++++++++++
 hw/ppc/pnv_spi_responder.c         | 166 +++++++++++++++++++++++++++++
 hw/ppc/meson.build                 |   1 +
 3 files changed, 276 insertions(+)
 create mode 100644 include/hw/ppc/pnv_spi_responder.h
 create mode 100644 hw/ppc/pnv_spi_responder.c
include/hw/ppc/pnv_spi_responder.h new file +109 -0
new file mode 100644
index 0000000000..1cf7279aad
@@ -0,0 +1,109 @@
+/*
+ * QEMU SPI Responder.
+ *
+ * Copyright (c) 2024, IBM Corporation.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ *
+ * SPI provides full-duplex synchronous serial communication between single
+ * controller and multiple responder devices. One SPI Controller is
+ * implemented and supported on a SPI Bus, there is no support for multiple
+ * controllers on the SPI bus.
+ *
+ * The current implementation assumes that single responder is connected to
+ * bus, hence chip_select is not modelled.
+ */
+
+#ifndef PPC_PNV_SPI_RESPONDER_H
+#define PPC_PNV_SPI_RESPONDER_H
+
+#include "hw/qdev-core.h"
+#include "qom/object.h"
+#include "qemu/log.h"
+
+#define TYPE_PNV_SPI_RESPONDER "spi-responder"
+OBJECT_DECLARE_TYPE(PnvSpiResponder, PnvSpiResponderClass,
+                    PNV_SPI_RESPONDER)
+
+typedef struct xfer_buffer xfer_buffer;
+
+struct PnvSpiResponderClass {
+    DeviceClass parent_class;
+
+    /*
+     * These methods are from controller to responder and implemented
+     * by all responders.
+     * Connect_controller/disconnect_controller methods are called by
+     * controller to initiate/stop the SPI transfer.
+     */
+    void (*connect_controller)(PnvSpiResponder *responder, const char *port);
+    void (*disconnect_controller)(PnvSpiResponder *responder);
+    /*
+     * SPI transfer consists of a number of consecutive calls to the request
+     * method.
+     * The parameter first is true on first call of the transfer and last is on
+     * the final call of the transfer. Parameter bits and payload defines the
+     * number of bits and data payload sent by controller.
+     * Responder returns the response payload.
+     */
+    xfer_buffer *(*request)(PnvSpiResponder *responder, int first, int last,
+                    int bits, xfer_buffer *payload);
+};
+
+struct PnvSpiResponder {
+    DeviceState parent_obj;
+};
+
+#define TYPE_SPI_BUS "spi-bus"
+OBJECT_DECLARE_SIMPLE_TYPE(SpiBus, SPI_BUS)
+
+struct SpiBus {
+    BusState parent_obj;
+};
+
+/*
+ * spi_realize_and_unref: realize and unref an SPI responder
+ * @dev: SPI responder to realize
+ * @bus: SPI bus to put it on
+ * @errp: error pointer
+ */
+bool spi_realize_and_unref(DeviceState *dev, SpiBus *bus, Error **errp);
+
+/*
+ * spi_create_responder: create a SPI responder.
+ * @bus: SPI bus to put it on
+ * @name: name of the responder object.
+ * call spi_realize_and_unref() after creating the responder.
+ */
+
+PnvSpiResponder *spi_create_responder(SpiBus *bus, const char *name);
+
+/* xfer_buffer */
+typedef struct xfer_buffer {
+
+    uint32_t    len;
+    uint8_t    *data;
+
+} xfer_buffer;
+
+/*
+ * xfer_buffer_read_ptr: Increment the payload length and return the pointer
+ * to the data at offset
+ */
+uint8_t *xfer_buffer_write_ptr(xfer_buffer *payload, uint32_t offset,
+                uint32_t length);
+/* xfer_buffer_read_ptr: Return the pointer to the data at offset */
+void xfer_buffer_read_ptr(xfer_buffer *payload, uint8_t **read_buf,
+                uint32_t offset, uint32_t length);
+/* xfer_buffer_new: Allocate memory and return the pointer */
+xfer_buffer *xfer_buffer_new(void);
+/* xfer_buffer_free: free the payload */
+void xfer_buffer_free(xfer_buffer *payload);
+
+/* Controller interface */
+SpiBus *spi_create_bus(DeviceState *parent, const char *name);
+xfer_buffer *spi_request(SpiBus *bus, int first, int last, int bits,
+                xfer_buffer *payload);
+bool spi_connect_controller(SpiBus *bus, const char *port);
+bool spi_disconnect_controller(SpiBus *bus);
+#endif /* PPC_PNV_SPI_SEEPROM_H */
hw/ppc/pnv_spi_responder.c new file +166 -0

166 lines not on this page · raw message

hw/ppc/meson.build +1 -0
index eba3406e7f..9bfd5a5613 100644
@@ -53,6 +53,7 @@ ppc_ss.add(when: 'CONFIG_POWERNV', if_true: files(
   'pnv_bmc.c',
   'pnv_homer.c',
   'pnv_pnor.c',
+  'pnv_spi_responder.c',
 ))
 # PowerPC 4xx boards
 ppc_ss.add(when: 'CONFIG_PPC405', if_true: files(
Signature · 1 line
2.31.1
#2 lore raw

Re: [PATCH v1 1/5] hw/ppc: SPI responder model

Quoted 23 lines · Chalapathi V

Serial pheripheral interface provides full-duplex synchronous serial communication between single controller and multiple responder devices. One SPI Controller is implemented and supported on a SPI Bus, there is no support for multiple controllers on the SPI bus.

The current implemetation assumes that single responder is connected to bus, hence chip_select is not modelled.

---
  include/hw/ppc/pnv_spi_responder.h | 109 +++++++++++++++++++
  hw/ppc/pnv_spi_responder.c         | 166 +++++++++++++++++++++++++++++
  hw/ppc/meson.build                 |   1 +
  3 files changed, 276 insertions(+)

create mode 100644 include/hw/ppc/pnv_spi_responder.h create mode 100644 hw/ppc/pnv_spi_responder.c

include/hw/ppc/pnv_spi_responder.h new file +0 -0
new file mode 100644
index 0000000000..1cf7279aad

include/hw/ppc/pnv_spi_responder.h line 10

Quoted 10 lines
include/hw/ppc/pnv_spi_responder.h new file +10 -0
@@ -0,0 +1,109 @@
+/*
+ * QEMU SPI Responder.
+ *
+ * Copyright (c) 2024, IBM Corporation.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ *
+ * SPI provides full-duplex synchronous serial communication between single
+ * controller and multiple responder devices. One SPI Controller is
+ * implemented and supported on a SPI Bus, there is no support for multiple

an SPI Bus

include/hw/ppc/pnv_spi_responder.h

Quoted 3 lines
include/hw/ppc/pnv_spi_responder.h new file +3 -0
+ * controllers on the SPI bus.
+ *
+ * The current implementation assumes that single responder is connected to > + * bus, hence chip_select is not modelled.

to the bus, hence chip_select is not modeled.

Quoted 49 lines
include/hw/ppc/pnv_spi_responder.h new file +49 -0
+ */
+
+#ifndef PPC_PNV_SPI_RESPONDER_H
+#define PPC_PNV_SPI_RESPONDER_H
+
+#include "hw/qdev-core.h"
+#include "qom/object.h"
+#include "qemu/log.h"
+
+#define TYPE_PNV_SPI_RESPONDER "spi-responder"
+OBJECT_DECLARE_TYPE(PnvSpiResponder, PnvSpiResponderClass,
+                    PNV_SPI_RESPONDER)
+
+typedef struct xfer_buffer xfer_buffer;
+
+struct PnvSpiResponderClass {
+    DeviceClass parent_class;
+
+    /*
+     * These methods are from controller to responder and implemented
+     * by all responders.
+     * Connect_controller/disconnect_controller methods are called by
+     * controller to initiate/stop the SPI transfer.
+     */
+    void (*connect_controller)(PnvSpiResponder *responder, const char *port);
+    void (*disconnect_controller)(PnvSpiResponder *responder);
+    /*
+     * SPI transfer consists of a number of consecutive calls to the request
+     * method.
+     * The parameter first is true on first call of the transfer and last is on
+     * the final call of the transfer. Parameter bits and payload defines the
+     * number of bits and data payload sent by controller.
+     * Responder returns the response payload.
+     */
+    xfer_buffer *(*request)(PnvSpiResponder *responder, int first, int last,
+                    int bits, xfer_buffer *payload);
+};
+
+struct PnvSpiResponder {
+    DeviceState parent_obj;
+};
+
+#define TYPE_SPI_BUS "spi-bus"
+OBJECT_DECLARE_SIMPLE_TYPE(SpiBus, SPI_BUS)
+
+struct SpiBus {
+    BusState parent_obj;
+};
+

include/hw/ppc/pnv_spi_responder.h line 73

Quoted 10 lines
include/hw/ppc/pnv_spi_responder.h new file +10 -0
+/*
+ * spi_realize_and_unref: realize and unref an SPI responder
+ * @dev: SPI responder to realize
+ * @bus: SPI bus to put it on
+ * @errp: error pointer
+ */
+bool spi_realize_and_unref(DeviceState *dev, SpiBus *bus, Error **errp);
+
+/*
+ * spi_create_responder: create a SPI responder.

a -> an

Quoted 53 lines
include/hw/ppc/pnv_spi_responder.h new file +36 -0
+ * @bus: SPI bus to put it on
+ * @name: name of the responder object.
+ * call spi_realize_and_unref() after creating the responder.
+ */
+
+PnvSpiResponder *spi_create_responder(SpiBus *bus, const char *name);
+
+/* xfer_buffer */
+typedef struct xfer_buffer {
+
+    uint32_t    len;
+    uint8_t    *data;
+
+} xfer_buffer;
+
+/*
+ * xfer_buffer_read_ptr: Increment the payload length and return the pointer
+ * to the data at offset
+ */
+uint8_t *xfer_buffer_write_ptr(xfer_buffer *payload, uint32_t offset,
+                uint32_t length);
+/* xfer_buffer_read_ptr: Return the pointer to the data at offset */
+void xfer_buffer_read_ptr(xfer_buffer *payload, uint8_t **read_buf,
+                uint32_t offset, uint32_t length);
+/* xfer_buffer_new: Allocate memory and return the pointer */
+xfer_buffer *xfer_buffer_new(void);
+/* xfer_buffer_free: free the payload */
+void xfer_buffer_free(xfer_buffer *payload);
+
+/* Controller interface */
+SpiBus *spi_create_bus(DeviceState *parent, const char *name);
+xfer_buffer *spi_request(SpiBus *bus, int first, int last, int bits,
+                xfer_buffer *payload);
+bool spi_connect_controller(SpiBus *bus, const char *port);
+bool spi_disconnect_controller(SpiBus *bus);
+#endif /* PPC_PNV_SPI_SEEPROM_H */
hw/ppc/pnv_spi_responder.c new file +11 -0
new file mode 100644
index 0000000000..c3bc659b1b
@@ -0,0 +1,166 @@
+/*
+ * QEMU PowerPC SPI Responder
+ *
+ * Copyright (c) 2024, IBM Corporation.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "qemu/osdep.h"
+#include "hw/ppc/pnv_spi_responder.h"
+#include "qapi/error.h"

hw/ppc/pnv_spi_responder.c line 21

Quoted 10 lines
hw/ppc/pnv_spi_responder.c new file +10 -0
+
+static const TypeInfo spi_bus_info = {
+    .name = TYPE_SPI_BUS,
+    .parent = TYPE_BUS,
+    .instance_size = sizeof(SpiBus),
+};
+
+SpiBus *spi_create_bus(DeviceState *parent, const char *name)
+{
+    BusState *bus;

empty line after var decl

hw/ppc/pnv_spi_responder.c line 29

Quoted 8 lines
hw/ppc/pnv_spi_responder.c new file +8 -0
+    bus = qbus_new(TYPE_SPI_BUS, parent, name);
+    return SPI_BUS(bus);
+}
+
+/* xfer_buffer_methods */
+xfer_buffer *xfer_buffer_new(void)
+{
+    xfer_buffer *payload = g_malloc0(sizeof(*payload));

empty line after var decl

hw/ppc/pnv_spi_responder.c line 30

Quoted 1 line
hw/ppc/pnv_spi_responder.c new file +1 -0
+    payload->data = g_malloc0(payload->len * sizeof(uint8_t));

sizeof(uint8_t) doesn't seem necessary

hw/ppc/pnv_spi_responder.c line 37

Quoted 7 lines
hw/ppc/pnv_spi_responder.c new file +7 -0
+    return payload;
+}
+
+void xfer_buffer_free(xfer_buffer *payload)
+{
+    free(payload->data);
+    payload->data = NULL;

not necessary to do this

hw/ppc/pnv_spi_responder.c line 47

Quoted 10 lines
hw/ppc/pnv_spi_responder.c new file +10 -0
+    free(payload);
+}
+
+uint8_t *xfer_buffer_write_ptr(xfer_buffer *payload, uint32_t offset,
+                            uint32_t length)
+{
+    if (payload->len < (offset + length)) {
+        payload->len = offset + length;
+        payload->data = g_realloc(payload->data,
+                        payload->len * sizeof(uint8_t));

sizeof(uint8_t) does not seem necessary

hw/ppc/pnv_spi_responder.c line 55

Quoted 8 lines
hw/ppc/pnv_spi_responder.c new file +8 -0
+    }
+    return &payload->data[offset];
+}
+
+void xfer_buffer_read_ptr(xfer_buffer *payload, uint8_t **read_buf,
+                uint32_t offset, uint32_t length)
+{
+    static uint32_t prev_len;

Why do you keep prev_len around?

hw/ppc/pnv_spi_responder.c line 56

Quoted 1 line
hw/ppc/pnv_spi_responder.c new file +1 -0
+    uint32_t offset_org = offset;

empty line after var decl

hw/ppc/pnv_spi_responder.c line 57

Quoted 1 line
hw/ppc/pnv_spi_responder.c new file +1 -0
+    if (offset > payload->len) {

if (offset + length > payload->len) ?

hw/ppc/pnv_spi_responder.c line 63

Quoted 6 lines
hw/ppc/pnv_spi_responder.c new file +6 -0
+        if (length < payload->len) {
+            offset = payload->len - length;
+        } else {
+            offset = 0;
+            length = payload->len;
+        }

If the user passes in length (1000) and now you artificially lower it (10) he may assume he got 1000 bytes to read and end up reading over the end of the buffer in the end becasue there are only 10 bytes. I don't think you should neither adjust offset nor length but realloc (if at all necessary to do this here) to accomodate these values.

Quoted 13 lines
hw/ppc/pnv_spi_responder.c new file +13 -0
+        qemu_log_mask(LOG_GUEST_ERROR, "Read offset(%d) exceeds buffer "
+                        "length(%d), altered offset to %d and length to %d to "
+                        "read within buffer\n", offset_org, payload->len,
+                        offset, length);
+    } else if ((offset + length) > payload->len) {
+        qemu_log_mask(LOG_GUEST_ERROR, "Read length(%d) bytes from offset (%d)"
+                        ", exceeds buffer length(%d)\n", length, offset,
+                        payload->len);
+        length = payload->len - offset;
+    }
+
+    if ((prev_len != length) || (*read_buf == NULL)) {
+        *read_buf = g_realloc(*read_buf, length * sizeof(uint8_t));

hw/ppc/pnv_spi_responder.c line 86

Quoted 10 lines
hw/ppc/pnv_spi_responder.c new file +10 -0
+        prev_len = length;
+    }
+    *read_buf = &payload->data[offset];
+}
+
+/* Controller interface methods */
+bool spi_connect_controller(SpiBus *bus, const char *port)
+{
+    BusState *b = BUS(bus);
+    BusChild *kid;

empty line

hw/ppc/pnv_spi_responder.c line 89

Quoted 3 lines
hw/ppc/pnv_spi_responder.c new file +3 -0
+    QTAILQ_FOREACH(kid, &b->children, sibling) {
+        PnvSpiResponder *r = PNV_SPI_RESPONDER(kid->child);
+        PnvSpiResponderClass *rc = PNV_SPI_RESPONDER_GET_CLASS(r);

empty line

hw/ppc/pnv_spi_responder.c line 91

Quoted 2 lines
hw/ppc/pnv_spi_responder.c new file +2 -0
+        rc->connect_controller(r, port);
+        return true;

you didn't process the whole list but returned on the first element?

hw/ppc/pnv_spi_responder.c line 98

Quoted 7 lines
hw/ppc/pnv_spi_responder.c new file +7 -0
+    }
+    return false;
+}
+bool spi_disconnect_controller(SpiBus *bus)
+{
+    BusState *b = BUS(bus);
+    BusChild *kid;

empty line

hw/ppc/pnv_spi_responder.c line 103

Quoted 5 lines
hw/ppc/pnv_spi_responder.c new file +5 -0
+    QTAILQ_FOREACH(kid, &b->children, sibling) {
+        PnvSpiResponder *r = PNV_SPI_RESPONDER(kid->child);
+        PnvSpiResponderClass *rc = PNV_SPI_RESPONDER_GET_CLASS(r);
+        rc->disconnect_controller(r);
+        return true;

same comments here

Quoted 7 lines
hw/ppc/pnv_spi_responder.c new file +7 -0
+    }
+    return false;
+}
+
+xfer_buffer *spi_request(SpiBus *bus,
+                int first, int last, int bits, xfer_buffer *payload)
+{

hw/ppc/pnv_spi_responder.c line 120

Quoted 10 lines
hw/ppc/pnv_spi_responder.c new file +10 -0
+    BusState *b = BUS(bus);
+    BusChild *kid;
+    xfer_buffer *rsp_payload = NULL;
+    uint8_t *buf = NULL;
+
+    QTAILQ_FOREACH(kid, &b->children, sibling) {
+        PnvSpiResponder *r = PNV_SPI_RESPONDER(kid->child);
+        PnvSpiResponderClass *rc = PNV_SPI_RESPONDER_GET_CLASS(r);
+        rsp_payload = rc->request(r, first, last, bits, payload);
+        return rsp_payload;

Also here you seem to stop processing in the first element.

Quoted 58 lines
hw/ppc/pnv_spi_responder.c new file +46 -0
+    }
+    if (rsp_payload == NULL) {
+        rsp_payload = xfer_buffer_new();
+    }
+    buf = xfer_buffer_write_ptr(rsp_payload, 0, payload->len);
+    memset(buf, 0, payload->len);
+    return rsp_payload;
+}
+
+/* create and realise spi responder device */
+bool spi_realize_and_unref(DeviceState *dev, SpiBus *bus, Error **errp)
+{
+    return qdev_realize_and_unref(dev, &bus->parent_obj, errp);
+}
+
+PnvSpiResponder *spi_create_responder(SpiBus *bus, const char *name)
+{
+    DeviceState *dev = qdev_new(name);
+
+    spi_realize_and_unref(dev, bus, &error_fatal);
+    return PNV_SPI_RESPONDER(dev);
+}
+
+static void pnv_spi_responder_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+
+    dc->desc = "PowerNV SPI RESPONDER";
+}
+
+static const TypeInfo pnv_spi_responder_info = {
+    .name          = TYPE_PNV_SPI_RESPONDER,
+    .parent        = TYPE_DEVICE,
+    .instance_size = sizeof(PnvSpiResponder),
+    .class_init    = pnv_spi_responder_class_init,
+    .abstract      = true,
+    .class_size    = sizeof(PnvSpiResponderClass),
+};
+
+static void pnv_spi_responder_register_types(void)
+{
+    type_register_static(&pnv_spi_responder_info);
+    type_register_static(&spi_bus_info);
+}
+
+type_init(pnv_spi_responder_register_types);
hw/ppc/meson.build +1 -0
index eba3406e7f..9bfd5a5613 100644
@@ -53,6 +53,7 @@ ppc_ss.add(when: 'CONFIG_POWERNV', if_true: files(
    'pnv_bmc.c',
    'pnv_homer.c',
    'pnv_pnor.c',
+  'pnv_spi_responder.c',
  ))
  # PowerPC 4xx boards
  ppc_ss.add(when: 'CONFIG_PPC405', if_true: files(
#3 lore raw

PATCH 2/5 hw/ppc: SPI controller model - registers implementation

In reply to Chalapathi V

SPI controller device model supports a connection to a single SPI responder. This provide access to SPI seeproms, TPM, flash device and an ADC controller.

All SPI function control is mapped into the SPI register space to enable full control by firmware. In this commit SPI configuration component is modelled which contains all SPI configuration and status registers as well as the hold registers for data to be sent or having been received.

---
 include/hw/ppc/pnv_spi_controller.h |  43 ++++
 include/hw/ppc/pnv_xscom.h          |   3 +
 hw/ppc/pnv_spi_controller.c         | 337 ++++++++++++++++++++++++++++
 hw/ppc/meson.build                  |   1 +
 4 files changed, 384 insertions(+)
 create mode 100644 include/hw/ppc/pnv_spi_controller.h
 create mode 100644 hw/ppc/pnv_spi_controller.c
include/hw/ppc/pnv_spi_controller.h new file +43 -0
new file mode 100644
index 0000000000..8afaabdd1b
@@ -0,0 +1,43 @@
+/*
+ * QEMU PowerPC SPI Controller model
+ *
+ * Copyright (c) 2024, IBM Corporation.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ *
+ * This model Supports a connection to a single SPI responder.
+ * Introduced for P10 to provide access to SPI seeproms, TPM, flash device
+ * and an ADC controller.
+ */
+
+#ifndef PPC_PNV_SPI_CONTROLLER_H
+#define PPC_PNV_SPI_CONTROLLER_H
+
+#define TYPE_PNV_SPI_CONTROLLER "pnv-spi-controller"
+#define PNV_SPICONTROLLER(obj) \
+        OBJECT_CHECK(PnvSpiController, (obj), TYPE_PNV_SPI_CONTROLLER)
+
+#define SPI_CONTROLLER_REG_SIZE 8
+
+typedef struct SpiBus SpiBus;
+
+typedef struct PnvSpiController {
+    DeviceState parent;
+
+    SpiBus            *spi_bus;
+    MemoryRegion    xscom_spic_regs;
+    /* SPI controller object number */
+    uint32_t        spic_num;
+
+    /* SPI Controller registers */
+    uint64_t        error_reg;
+    uint64_t        counter_config_reg;
+    uint64_t        config_reg1;
+    uint64_t        clock_config_reset_control;
+    uint64_t        memory_mapping_reg;
+    uint64_t        transmit_data_reg;
+    uint64_t        receive_data_reg;
+    uint8_t         sequencer_operation_reg[SPI_CONTROLLER_REG_SIZE];
+    uint64_t        status_reg;
+} PnvSpiController;
+#endif /* PPC_PNV_SPI_CONTROLLER_H */
include/hw/ppc/pnv_xscom.h +3 -0
index f5becbab41..0575bf0985 100644
@@ -176,6 +176,9 @@ struct PnvXScomInterfaceClass {
 #define PNV10_XSCOM_PEC_PCI_BASE   0x8010800 /* index goes upwards ... */
 #define PNV10_XSCOM_PEC_PCI_SIZE   0x200
 
+#define PNV10_XSCOM_PIB_SPIC_BASE 0xc0000
+#define PNV10_XSCOM_PIB_SPIC_SIZE 0x20
+
 void pnv_xscom_init(PnvChip *chip, uint64_t size, hwaddr addr);
 int pnv_dt_xscom(PnvChip *chip, void *fdt, int root_offset,
                  uint64_t xscom_base, uint64_t xscom_size,
hw/ppc/pnv_spi_controller.c new file +337 -0

337 lines not on this page · raw message

hw/ppc/meson.build +1 -0
index 9bfd5a5613..de25cac763 100644
@@ -54,6 +54,7 @@ ppc_ss.add(when: 'CONFIG_POWERNV', if_true: files(
   'pnv_homer.c',
   'pnv_pnor.c',
   'pnv_spi_responder.c',
+  'pnv_spi_controller.c',
 ))
 # PowerPC 4xx boards
 ppc_ss.add(when: 'CONFIG_PPC405', if_true: files(
Signature · 1 line
2.31.1
#4 lore raw

Re: [PATCH v1 2/5] hw/ppc: SPI controller model - registers implementation

Quoted 200 lines · Chalapathi V

SPI controller device model supports a connection to a single SPI responder. This provide access to SPI seeproms, TPM, flash device and an ADC controller.

All SPI function control is mapped into the SPI register space to enable full control by firmware. In this commit SPI configuration component is modelled which contains all SPI configuration and status registers as well as the hold registers for data to be sent or having been received.

---
  include/hw/ppc/pnv_spi_controller.h |  43 ++++
  include/hw/ppc/pnv_xscom.h          |   3 +
  hw/ppc/pnv_spi_controller.c         | 337 ++++++++++++++++++++++++++++
  hw/ppc/meson.build                  |   1 +
  4 files changed, 384 insertions(+)

create mode 100644 include/hw/ppc/pnv_spi_controller.h create mode 100644 hw/ppc/pnv_spi_controller.c

include/hw/ppc/pnv_spi_controller.h new file +43 -0
new file mode 100644
index 0000000000..8afaabdd1b
@@ -0,0 +1,43 @@
+/*
+ * QEMU PowerPC SPI Controller model
+ *
+ * Copyright (c) 2024, IBM Corporation.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ *
+ * This model Supports a connection to a single SPI responder.
+ * Introduced for P10 to provide access to SPI seeproms, TPM, flash device
+ * and an ADC controller.
+ */
+
+#ifndef PPC_PNV_SPI_CONTROLLER_H
+#define PPC_PNV_SPI_CONTROLLER_H
+
+#define TYPE_PNV_SPI_CONTROLLER "pnv-spi-controller"
+#define PNV_SPICONTROLLER(obj) \
+        OBJECT_CHECK(PnvSpiController, (obj), TYPE_PNV_SPI_CONTROLLER)
+
+#define SPI_CONTROLLER_REG_SIZE 8
+
+typedef struct SpiBus SpiBus;
+
+typedef struct PnvSpiController {
+    DeviceState parent;
+
+    SpiBus            *spi_bus;
+    MemoryRegion    xscom_spic_regs;
+    /* SPI controller object number */
+    uint32_t        spic_num;
+
+    /* SPI Controller registers */
+    uint64_t        error_reg;
+    uint64_t        counter_config_reg;
+    uint64_t        config_reg1;
+    uint64_t        clock_config_reset_control;
+    uint64_t        memory_mapping_reg;
+    uint64_t        transmit_data_reg;
+    uint64_t        receive_data_reg;
+    uint8_t         sequencer_operation_reg[SPI_CONTROLLER_REG_SIZE];
+    uint64_t        status_reg;
+} PnvSpiController;
+#endif /* PPC_PNV_SPI_CONTROLLER_H */
include/hw/ppc/pnv_xscom.h +3 -0
index f5becbab41..0575bf0985 100644
@@ -176,6 +176,9 @@ struct PnvXScomInterfaceClass {
  #define PNV10_XSCOM_PEC_PCI_BASE   0x8010800 /* index goes upwards ... */
  #define PNV10_XSCOM_PEC_PCI_SIZE   0x200
 
+#define PNV10_XSCOM_PIB_SPIC_BASE 0xc0000
+#define PNV10_XSCOM_PIB_SPIC_SIZE 0x20
+
  void pnv_xscom_init(PnvChip *chip, uint64_t size, hwaddr addr);
  int pnv_dt_xscom(PnvChip *chip, void *fdt, int root_offset,
                   uint64_t xscom_base, uint64_t xscom_size,
hw/ppc/pnv_spi_controller.c new file +113 -0
new file mode 100644
index 0000000000..0f2bc25e82
@@ -0,0 +1,337 @@
+/*
+ * QEMU PowerPC SPI Controller model
+ *
+ * Copyright (c) 2024, IBM Corporation.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "qemu/osdep.h"
+#include "qemu/log.h"
+#include "hw/qdev-properties.h"
+#include "hw/ppc/pnv.h"
+#include "hw/ppc/pnv_xscom.h"
+#include "hw/ppc/pnv_spi_controller.h"
+#include "hw/ppc/pnv_spi_responder.h"
+#include "hw/ppc/fdt.h"
+#include <libfdt.h>
+#include <math.h>
+
+#define SPI_DEBUG(x)
+
+/* Error Register */
+#define ERROR_REG                               0x00
+
+/* counter_config_reg */
+#define COUNTER_CONFIG_REG                      0x01
+#define COUNTER_CONFIG_REG_SHIFT_COUNT_N1       PPC_BITMASK(0 , 7)
+#define COUNTER_CONFIG_REG_SHIFT_COUNT_N2       PPC_BITMASK(8 , 15)
+#define COUNTER_CONFIG_REG_COUNT_COMPARE1       PPC_BITMASK(24 , 31)
+#define COUNTER_CONFIG_REG_COUNT_COMPARE2       PPC_BITMASK(32 , 39)
+#define COUNTER_CONFIG_REG_N1_COUNT_CONTROL     PPC_BITMASK(48 , 51)
+#define COUNTER_CONFIG_REG_N2_COUNT_CONTROL     PPC_BITMASK(52 , 55)
+
+/* config_reg */
+#define CONFIG_REG1                             0x02
+
+/* clock_config_reset_control_ecc_enable_reg */
+#define CLOCK_CONFIG_REG                        0x03
+#define CLOCK_CONFIG_RESET_CONTROL_HARD_RESET   0x0084000000000000;
+#define CLOCK_CONFIG_REG_RESET_CONTROL          PPC_BITMASK(24 , 27)
+#define CLOCK_CONFIG_REG_ECC_CONTROL            PPC_BITMASK(28 , 30)
+
+/* memory_mapping_reg */
+#define MEMORY_MAPPING_REG                      0x04
+#define MEMORY_MAPPING_REG_MMSPISM_BASE_ADDR    PPC_BITMASK(0 , 15)
+#define MEMORY_MAPPING_REG_MMSPISM_ADDR_MASK    PPC_BITMASK(16 , 31)
+#define MEMORY_MAPPING_REG_RDR_MATCH_VAL        PPC_BITMASK(32 , 47)
+#define MEMORY_MAPPING_REG_RDR_MATCH_MASK       PPC_BITMASK(48 , 63)
+
+/* transmit_data_reg */
+#define TRANSMIT_DATA_REG                       0x05
+
+/* receive_data_reg */
+#define RECEIVE_DATA_REG                        0x06
+
+/* sequencer_operation_reg */
+#define SEQUENCER_OPERATION_REG                 0x07
+
+/* status_reg */
+#define STATUS_REG                              0x08
+#define STATUS_REG_RDR_FULL                     PPC_BIT(0)
+#define STATUS_REG_RDR_OVERRUN                  PPC_BIT(1)
+#define STATUS_REG_RDR_UNDERRUN                 PPC_BIT(2)
+#define STATUS_REG_TDR_FULL                     PPC_BIT(4)
+#define STATUS_REG_TDR_OVERRUN                  PPC_BIT(5)
+#define STATUS_REG_TDR_UNDERRUN                 PPC_BIT(6)
+#define STATUS_REG_SEQUENCER_FSM                PPC_BITMASK(8 , 15)
+#define STATUS_REG_SHIFTER_FSM                  PPC_BITMASK(16 , 27)
+#define STATUS_REG_SEQUENCER_INDEX              PPC_BITMASK(28 , 31)
+#define STATUS_REG_GENERAL_SPI_STATUS           PPC_BITMASK(32 , 63)
+#define STATUS_REG_RDR                          PPC_BITMASK(1 , 3)
+#define STATUS_REG_TDR                          PPC_BITMASK(5 , 7)
+
+/*
+ * Shifter states
+ *
+ * These are the same values defined for the Shifter FSM field of the
+ * status register.  It's a 12 bit field so we will represent it as three
+ * nibbles in the constants.
+ *
+ * These are shifter_fsm values
+ *
+ * Status reg bits 16-27 -> field bits 0-11
+ * bits 0,1,2,5 unused/reserved
+ * bit 4 crc shift in (unused)
+ * bit 8 crc shift out (unused)
+ */
+
+#define FSM_DONE                        0x100   /* bit 3 */
+#define FSM_SHIFT_N2                    0x020   /* bit 6 */
+#define FSM_WAIT                        0x010   /* bit 7 */
+#define FSM_SHIFT_N1                    0x004   /* bit 9 */
+#define FSM_START                       0x002   /* bit 10 */
+#define FSM_IDLE                        0x001   /* bit 11 */
+
+/*
+ * Sequencer states
+ *
+ * These are sequencer_fsm values
+ *
+ * Status reg bits 8-15 -> field bits 0-7
+ * bits 0-3 unused/reserved
+ *
+ */
+#define SEQ_STATE_INDEX_INCREMENT       0x08    /* bit 4 */
+#define SEQ_STATE_EXECUTE               0x04    /* bit 5 */
+#define SEQ_STATE_DECODE                0x02    /* bit 6 */
+#define SEQ_STATE_IDLE                  0x01    /* bit 7 */
+
+/*
+ * These are the supported sequencer operations.
+ * Only the upper nibble is significant because for many operations
+ * the lower nibble is a variable specific to the operation.

hw/ppc/pnv_spi_controller.c line 123

Quoted 10 lines
hw/ppc/pnv_spi_controller.c new file +10 -0
+ */
+#define SEQ_OP_STOP                     0x00
+#define SEQ_OP_SELECT_SLAVE             0x10
+#define SEQ_OP_SHIFT_N1                 0x30
+#define SEQ_OP_SHIFT_N2                 0x40
+#define SEQ_OP_BRANCH_IFNEQ_RDR         0x60
+#define SEQ_OP_TRANSFER_TDR             0xC0
+#define SEQ_OP_BRANCH_IFNEQ_INC_1       0xE0
+#define SEQ_OP_BRANCH_IFNEQ_INC_2       0xF0
+

I heared there's a TPM SPI model coming up, so I looked at this now under the aspect of modifying tests/qtest/tpm-tis-i2c-test.c for it.
The I2C test relies on tests/qtest/qtest_aspeed.c which in turn gets register constants from include/hw/i2c/aspeed_i2c.h. So it may be good to make registers available in a header file for others to use in this case as well.

Quoted 29 lines
hw/ppc/pnv_spi_controller.c new file +29 -0
+
+static uint64_t pnv_spi_controller_read(void *opaque, hwaddr addr,
+                                 unsigned size)
+{
+    PnvSpiController *sc = PNV_SPICONTROLLER(opaque);
+    uint32_t reg = addr >> 3;
+
+    uint64_t val = ~0ull;
+
+    switch (reg) {
+    case ERROR_REG:
+        val = sc->error_reg;
+        break;
+    case COUNTER_CONFIG_REG:
+        val = sc->counter_config_reg;
+        break;
+    case CONFIG_REG1:
+        val = sc->config_reg1;
+        break;
+    case CLOCK_CONFIG_REG:
+        val = sc->clock_config_reset_control;
+        break;
+    case MEMORY_MAPPING_REG:
+        val = sc->memory_mapping_reg;
+        break;
+    case TRANSMIT_DATA_REG:
+        val = sc->transmit_data_reg;
+        break;
+    case RECEIVE_DATA_REG:

hw/ppc/pnv_spi_controller.c line 162

Quoted 10 lines
hw/ppc/pnv_spi_controller.c new file +10 -0
+        val = sc->receive_data_reg;
+        SPI_DEBUG(qemu_log("RDR being read, data extracted = 0x%16.16lx\n",
+                           val));
+        sc->status_reg = SETFIELD(STATUS_REG_RDR_FULL, sc->status_reg, 0);
+        SPI_DEBUG(qemu_log("RDR being read, RDR_full set to 0\n"));
+        break;
+    case SEQUENCER_OPERATION_REG:
+        for (int i = 0; i < SPI_CONTROLLER_REG_SIZE; i++) {
+            val |= ((uint64_t)sc->sequencer_operation_reg[i] <<
+                                                    (64 - ((i + 1) * 8)));

Probably there's a missing val = 0 initialization here otherwise one gets only ~0ull back from the initial initialization.

Quoted 65 lines
hw/ppc/pnv_spi_controller.c new file +65 -0
+        }
+        break;
+    case STATUS_REG:
+        val = sc->status_reg;
+        break;
+    default:
+        qemu_log_mask(LOG_GUEST_ERROR, "spi_controller_regs: Invalid xscom "
+                 "read at 0x%08x\n", reg);
+    }
+    return val;
+}
+
+static void pnv_spi_controller_write(void *opaque, hwaddr addr,
+                                 uint64_t val, unsigned size)
+{
+    PnvSpiController *sc = PNV_SPICONTROLLER(opaque);
+    uint32_t reg = addr >> 3;
+
+    switch (reg) {
+    case ERROR_REG:
+        sc->error_reg = val;
+        break;
+    case COUNTER_CONFIG_REG:
+        sc->counter_config_reg = val;
+        break;
+    case CONFIG_REG1:
+        sc->config_reg1 = val;
+        break;
+    case CLOCK_CONFIG_REG:
+        /*
+         * To reset the SPI controller write the sequence 0x5 0xA to
+         * reset_control field
+         */
+        if (GETFIELD(CLOCK_CONFIG_REG_RESET_CONTROL,
+                                sc->clock_config_reset_control) == 0x5) {
+            if (GETFIELD(CLOCK_CONFIG_REG_RESET_CONTROL, val) == 0xA) {
+                SPI_DEBUG(qemu_log("SPI controller reset sequence completed, "
+                               "resetting..."));
+                sc->clock_config_reset_control =
+                                 CLOCK_CONFIG_RESET_CONTROL_HARD_RESET;
+            } else {
+                sc->clock_config_reset_control = val;
+            }
+        } else {
+            sc->clock_config_reset_control = val;
+        }
+        break;
+    case MEMORY_MAPPING_REG:
+        sc->memory_mapping_reg = val;
+        break;
+    case TRANSMIT_DATA_REG:
+        /*
+         * Writing to the transmit data register causes the transmit data
+         * register full status bit in the status register to be set.  Writing
+         * when the transmit data register full status bit is already set
+         * causes a "Resource Not Available" condition.  This is not possible
+         * in the model since writes to this register are not asynchronous to
+         * the operation sequence like it would be in hardware.
+         */
+        sc->transmit_data_reg = val;
+        SPI_DEBUG(qemu_log("TDR being written, data written = 0x%16.16lx\n",
+                            val));
+        sc->status_reg = SETFIELD(STATUS_REG_TDR_FULL, sc->status_reg, 1);
+        SPI_DEBUG(qemu_log("TDRsequencer_operation_reg[i] = being written, TDR_full set to 1\n"));
+        sc->status_reg = SETFIELD(STATUS_REG_TDR_UNDERRUN, sc->status_reg, 0);

hw/ppc/pnv_spi_controller.c line 237

Quoted 10 lines
hw/ppc/pnv_spi_controller.c new file +10 -0
+        SPI_DEBUG(qemu_log("TDR being written, TDR_underrun set to 0\n"));
+        SPI_DEBUG(qemu_log("TDR being written, starting sequencer\n"));
+        break;
+    case RECEIVE_DATA_REG:
+        sc->receive_data_reg = val;
+        break;
+    case SEQUENCER_OPERATION_REG:
+        for (int i = 0; i < SPI_CONTROLLER_REG_SIZE; i++) {
+            sc->sequencer_operation_reg[i] =
+                 (val & PPC_BITMASK(i * 8 , i * 8 + 7)) >> (63 - (i * 8 + 7));

To me it would be more obvious if you used a mask here like this:

mask = PPC_BIT_MASK(0, 7);
mask = (0xff << 56);

for (...) {
    sc->sequencer_operation_reg[i] = (val & mask) >> (56 - i * 8);
    mask >>= 8;
}

hw/ppc/pnv_spi_controller.c line 241

Quoted 4 lines
hw/ppc/pnv_spi_controller.c new file +4 -0
+        }
+        break;
+    case STATUS_REG:
+        ;

stray ';'?

Quoted 108 lines
hw/ppc/pnv_spi_controller.c new file +96 -0
+        uint8_t rdr_val = GETFIELD(STATUS_REG_RDR, val);
+        uint8_t tdr_val = GETFIELD(STATUS_REG_TDR, val);
+        /* other fields are ignore_write */
+        sc->status_reg = SETFIELD(STATUS_REG_RDR_OVERRUN,
+                                         sc->status_reg, rdr_val);
+        sc->status_reg = SETFIELD(STATUS_REG_TDR_OVERRUN,
+                                         sc->status_reg, tdr_val);
+        break;
+    default:
+        qemu_log_mask(LOG_GUEST_ERROR, "spi_controller_regs: Invalid xscom "
+                 "write at 0x%08x\n", reg);
+    }
+    return;
+}
+
+static const MemoryRegionOps pnv_spi_controller_xscom_ops = {
+    .read = pnv_spi_controller_read,
+    .write = pnv_spi_controller_write,
+    .valid.min_access_size = 8,
+    .valid.max_access_size = 8,
+    .impl.min_access_size = 8,
+    .impl.max_access_size = 8,
+    .endianness = DEVICE_BIG_ENDIAN,
+};
+
+static Property pnv_spi_controller_properties[] = {
+    DEFINE_PROP_UINT32("spic_num", PnvSpiController, spic_num, 0),
+    DEFINE_PROP_END_OF_LIST(),
+};
+
+static void pnv_spi_controller_realize(DeviceState *dev, Error **errp)
+{
+    PnvSpiController *sc = PNV_SPICONTROLLER(dev);
+    g_autofree char *bus_name;
+    bus_name = g_strdup_printf("spi_bus%x", sc->spic_num);
+    sc->spi_bus = spi_create_bus(dev, bus_name);
+
+    /* spi controller scoms */
+    pnv_xscom_region_init(&sc->xscom_spic_regs, OBJECT(sc),
+                          &pnv_spi_controller_xscom_ops, sc,
+                          "xscom-spi-controller-regs",
+                          PNV10_XSCOM_PIB_SPIC_SIZE);
+}
+
+static int pnv_spi_controller_dt_xscom(PnvXScomInterface *dev, void *fdt,
+                             int offset)
+{
+    PnvSpiController *sc = PNV_SPICONTROLLER(dev);
+    g_autofree char *name;
+    int sc_offset;
+    const char compat[] = "ibm,power10-spi_controller";
+    uint32_t spic_pcba = PNV10_XSCOM_PIB_SPIC_BASE +
+        sc->spic_num * PNV10_XSCOM_PIB_SPIC_SIZE;
+    uint32_t reg[] = {
+        cpu_to_be32(spic_pcba),
+        cpu_to_be32(PNV10_XSCOM_PIB_SPIC_SIZE)
+    };
+    name = g_strdup_printf("spi_controller@%x", spic_pcba);
+    sc_offset = fdt_add_subnode(fdt, offset, name);
+    _FDT(sc_offset);
+
+    _FDT(fdt_setprop(fdt, sc_offset, "reg", reg, sizeof(reg)));
+    _FDT(fdt_setprop(fdt, sc_offset, "compatible", compat, sizeof(compat)));
+    _FDT((fdt_setprop_cell(fdt, sc_offset, "spic_num#", sc->spic_num)));
+    return 0;
+}
+
+static void pnv_spi_controller_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+    PnvXScomInterfaceClass *xscomc = PNV_XSCOM_INTERFACE_CLASS(klass);
+
+    xscomc->dt_xscom = pnv_spi_controller_dt_xscom;
+
+    dc->desc = "PowerNV SPI Controller";
+    dc->realize = pnv_spi_controller_realize;
+    device_class_set_props(dc, pnv_spi_controller_properties);
+}
+
+static const TypeInfo pnv_spi_controller_info = {
+    .name          = TYPE_PNV_SPI_CONTROLLER,
+    .parent        = TYPE_DEVICE,
+    .instance_size = sizeof(PnvSpiController),
+    .class_init    = pnv_spi_controller_class_init,
+    .interfaces    = (InterfaceInfo[]) {
+        { TYPE_PNV_XSCOM_INTERFACE },
+        { }
+    }
+};
+
+static void pnv_spi_controller_register_types(void)
+{
+    type_register_static(&pnv_spi_controller_info);
+}
+
+type_init(pnv_spi_controller_register_types);
hw/ppc/meson.build +1 -0
index 9bfd5a5613..de25cac763 100644
@@ -54,6 +54,7 @@ ppc_ss.add(when: 'CONFIG_POWERNV', if_true: files(
    'pnv_homer.c',
    'pnv_pnor.c',
    'pnv_spi_responder.c',
+  'pnv_spi_controller.c',
  ))
  # PowerPC 4xx boards
  ppc_ss.add(when: 'CONFIG_PPC405', if_true: files(
#5 lore raw

Re: [PATCH v1 2/5] hw/ppc: SPI controller model - registers implementation

Quoted 5 lines · Stefan Berger
Quoted 1 line · Chalapathi V

+#define COUNTER_CONFIG_REG_SHIFT_COUNT_N1       PPC_BITMASK(0 , 7)

No space before the ',' ==> PPC_BITMASK(0, 7)

#6 lore raw

Re: [PATCH v1 2/5] hw/ppc: SPI controller model - registers implementation

In reply to Stefan Berger
Quoted 21 lines · Stefan Berger
Quoted 8 lines · Chalapathi V

SPI controller device model supports a connection to a single SPI responder. This provide access to SPI seeproms, TPM, flash device and an ADC controller.

All SPI function control is mapped into the SPI register space to enable full control by firmware. In this commit SPI configuration component is modelled which contains all SPI configuration and status registers as well as the hold registers for data to be sent or having been received.

Quoted 7 lines

+    case SEQUENCER_OPERATION_REG:
+        for (int i = 0; i < SPI_CONTROLLER_REG_SIZE; i++) { +            sc->sequencer_operation_reg[i] =
+                 (val & PPC_BITMASK(i * 8 , i * 8 + 7)) >> (63 - (i * 8 + 7));

To me it would be more obvious if you used a mask here like this:

Quoted 8 lines

mask = PPC_BIT_MASK(0, 7);
mask = (0xff << 56);

for (...) {
    sc->sequencer_operation_reg[i] = (val & mask) >> (56 - i * 8); mask >>= 8;
}

Actually simpler and even this masking is not necessary:

for (...) {
    sc->sequencer_operation_reg[i] = (val >> (56 - i * 8)) & 0xff;
}
#7 lore raw

PATCH 3/5 hw/ppc: SPI controller model - sequencer and shifter

In reply to Chalapathi V

In this commit SPI shift engine and sequencer logic is implemented. Shift engine performs serialization and de-serialization according to the control by the sequencer and according to the setup defined in the configuration registers. Sequencer implements the main control logic and FSM to handle data transmit and data receive control of the shift engine.

---
 include/hw/ppc/pnv_spi_controller.h |   58 ++
 hw/ppc/pnv_spi_controller.c         | 1274 ++++++++++++++++++++++++++-
 2 files changed, 1331 insertions(+), 1 deletion(-)
include/hw/ppc/pnv_spi_controller.h +58 -0
index 8afaabdd1b..8160c35f5c 100644
@@ -8,6 +8,14 @@
  * This model Supports a connection to a single SPI responder.
  * Introduced for P10 to provide access to SPI seeproms, TPM, flash device
  * and an ADC controller.
+ *
+ * All SPI function control is mapped into the SPI register space to enable
+ * full control by firmware.
+ *
+ * SPI Controller has sequencer and shift engine. The SPI shift engine
+ * performs serialization and de-serialization according to the control by
+ * the sequencer and according to the setup defined in the configuration
+ * registers and the SPI sequencer implements the main control logic.
  */
 
 #ifndef PPC_PNV_SPI_CONTROLLER_H
@@ -20,6 +28,7 @@
 #define SPI_CONTROLLER_REG_SIZE 8
 
 typedef struct SpiBus SpiBus;
+typedef struct xfer_buffer xfer_buffer;
 
 typedef struct PnvSpiController {
     DeviceState parent;
@@ -28,6 +37,39 @@ typedef struct PnvSpiController {
     MemoryRegion    xscom_spic_regs;
     /* SPI controller object number */
     uint32_t        spic_num;
+    uint8_t         responder_select;
+    /* To verify if shift_n1 happens prior to shift_n2 */
+    bool            shift_n1_done;
+    /*
+     * Internal flags for the first and last indicators for the SPI
+     * interface methods
+     */
+    uint8_t         first;
+    uint8_t         last;
+    /* Loop counter for branch operation opcode Ex/Fx */
+    uint8_t         loop_counter_1;
+    uint8_t         loop_counter_2;
+    /* N1/N2_bits specifies the size of the N1/N2 segment of a frame in bits.*/
+    uint8_t         N1_bits;
+    uint8_t         N2_bits;
+    /* Number of bytes in a payload for the N1/N2 frame segment.*/
+    uint8_t         N1_bytes;
+    uint8_t         N2_bytes;
+    /* Number of N1/N2 bytes marked for transmit */
+    uint8_t         N1_tx;
+    uint8_t         N2_tx;
+    /* Number of N1/N2 bytes marked for receive */
+    uint8_t         N1_rx;
+    uint8_t         N2_rx;
+    /*
+     * Setting this attribute to true will cause the engine to reverse the
+     * bit order of each byte it appends to a payload before sending the
+     * payload to a device. There may be cases where an end device expects
+     * a reversed order, like in the case of the Nuvoton TPM device. The
+     * order of bytes in the payload is not reversed, only the order of the
+     * 8 bits in each payload byte.
+     */
+    bool            reverse_bits;
 
     /* SPI Controller registers */
     uint64_t        error_reg;
@@ -40,4 +82,20 @@ typedef struct PnvSpiController {
     uint8_t         sequencer_operation_reg[SPI_CONTROLLER_REG_SIZE];
     uint64_t        status_reg;
 } PnvSpiController;
+
+void log_all_N_counts(PnvSpiController *spi_controller);
+void spi_response(PnvSpiController *spi_controller, int bits,
+                xfer_buffer *rsp_payload);
+void operation_sequencer(PnvSpiController *spi_controller);
+bool operation_shiftn1(PnvSpiController *spi_controller, uint8_t opcode,
+                       xfer_buffer **payload, bool send_n1_alone);
+bool operation_shiftn2(PnvSpiController *spi_controller, uint8_t opcode,
+                       xfer_buffer **payload);
+bool does_rdr_match(PnvSpiController *spi_controller);
+uint8_t get_from_offset(PnvSpiController *spi_controller, uint8_t offset);
+void shift_byte_in(PnvSpiController *spi_controller, uint8_t byte);
+void calculate_N1(PnvSpiController *spi_controller, uint8_t opcode);
+void calculate_N2(PnvSpiController *spi_controller, uint8_t opcode);
+void do_reset(PnvSpiController *spi_controller);
+uint8_t reverse_bits8(uint8_t x);
 #endif /* PPC_PNV_SPI_CONTROLLER_H */
hw/ppc/pnv_spi_controller.c +1273 -1

1,300 lines not on this page · raw message

Signature · 1 line
2.31.1
#8 lore raw

Re: [PATCH v1 3/5] hw/ppc: SPI controller model - sequencer and shifter

Quoted 42 lines · Chalapathi V

In this commit SPI shift engine and sequencer logic is implemented. Shift engine performs serialization and de-serialization according to the control by the sequencer and according to the setup defined in the configuration registers. Sequencer implements the main control logic and FSM to handle data transmit and data receive control of the shift engine.

---
  include/hw/ppc/pnv_spi_controller.h |   58 ++
  hw/ppc/pnv_spi_controller.c         | 1274 ++++++++++++++++++++++++++-
  2 files changed, 1331 insertions(+), 1 deletion(-)
include/hw/ppc/pnv_spi_controller.h +9 -0
index 8afaabdd1b..8160c35f5c 100644
@@ -8,6 +8,14 @@
   * This model Supports a connection to a single SPI responder.
   * Introduced for P10 to provide access to SPI seeproms, TPM, flash device
   * and an ADC controller.
+ *
+ * All SPI function control is mapped into the SPI register space to enable
+ * full control by firmware.
+ *
+ * SPI Controller has sequencer and shift engine. The SPI shift engine
+ * performs serialization and de-serialization according to the control by
+ * the sequencer and according to the setup defined in the configuration
+ * registers and the SPI sequencer implements the main control logic.
   */
 
  #ifndef PPC_PNV_SPI_CONTROLLER_H
@@ -20,6 +28,7 @@
  #define SPI_CONTROLLER_REG_SIZE 8
 
  typedef struct SpiBus SpiBus;
+typedef struct xfer_buffer xfer_buffer;
 
  typedef struct PnvSpiController {
      DeviceState parent;
@@ -28,6 +37,39 @@ typedef struct PnvSpiController {
      MemoryRegion    xscom_spic_regs;
      /* SPI controller object number */

include/hw/ppc/pnv_spi_controller.h typedef struct PnvSpiController { · line 48

Quoted 10 lines
include/hw/ppc/pnv_spi_controller.h +9 -0
      uint32_t        spic_num;
+    uint8_t         responder_select;
+    /* To verify if shift_n1 happens prior to shift_n2 */
+    bool            shift_n1_done;
+    /*
+     * Internal flags for the first and last indicators for the SPI
+     * interface methods
+     */
+    uint8_t         first;
+    uint8_t         last;

Do these two correspond to the first and liast here?

xfer_buffer *seeprom_spi_request(PnvSpiResponder *resp, int first, int last, int bits, xfer_buffer *payload);

If so I think the data types in the prototype should be set to uint8_t as well and also bits should probably be an unsigned int or uint8_t?

Quoted 61 lines
include/hw/ppc/pnv_spi_controller.h +40 -0
+    /* Loop counter for branch operation opcode Ex/Fx */
+    uint8_t         loop_counter_1;
+    uint8_t         loop_counter_2;
+    /* N1/N2_bits specifies the size of the N1/N2 segment of a frame in bits.*/
+    uint8_t         N1_bits;
+    uint8_t         N2_bits;
+    /* Number of bytes in a payload for the N1/N2 frame segment.*/
+    uint8_t         N1_bytes;
+    uint8_t         N2_bytes;
+    /* Number of N1/N2 bytes marked for transmit */
+    uint8_t         N1_tx;
+    uint8_t         N2_tx;
+    /* Number of N1/N2 bytes marked for receive */
+    uint8_t         N1_rx;
+    uint8_t         N2_rx;
+    /*
+     * Setting this attribute to true will cause the engine to reverse the
+     * bit order of each byte it appends to a payload before sending the
+     * payload to a device. There may be cases where an end device expects
+     * a reversed order, like in the case of the Nuvoton TPM device. The
+     * order of bytes in the payload is not reversed, only the order of the
+     * 8 bits in each payload byte.
+     */
+    bool            reverse_bits;
 
      /* SPI Controller registers */
      uint64_t        error_reg;
@@ -40,4 +82,20 @@ typedef struct PnvSpiController {
      uint8_t         sequencer_operation_reg[SPI_CONTROLLER_REG_SIZE];
      uint64_t        status_reg;
  } PnvSpiController;
+
+void log_all_N_counts(PnvSpiController *spi_controller);
+void spi_response(PnvSpiController *spi_controller, int bits,
+                xfer_buffer *rsp_payload);
+void operation_sequencer(PnvSpiController *spi_controller);
+bool operation_shiftn1(PnvSpiController *spi_controller, uint8_t opcode,
+                       xfer_buffer **payload, bool send_n1_alone);
+bool operation_shiftn2(PnvSpiController *spi_controller, uint8_t opcode,
+                       xfer_buffer **payload);
+bool does_rdr_match(PnvSpiController *spi_controller);
+uint8_t get_from_offset(PnvSpiController *spi_controller, uint8_t offset);
+void shift_byte_in(PnvSpiController *spi_controller, uint8_t byte);
+void calculate_N1(PnvSpiController *spi_controller, uint8_t opcode);
+void calculate_N2(PnvSpiController *spi_controller, uint8_t opcode);
+void do_reset(PnvSpiController *spi_controller);
+uint8_t reverse_bits8(uint8_t x);
  #endif /* PPC_PNV_SPI_CONTROLLER_H */
hw/ppc/pnv_spi_controller.c +0 -1
index 0f2bc25e82..ef48af5d03 100644
@@ -9,7 +9,6 @@
  #include "qemu/osdep.h"
  #include "qemu/log.h"
  #include "hw/qdev-properties.h"
-#include "hw/ppc/pnv.h"
  #include "hw/ppc/pnv_xscom.h"
  #include "hw/ppc/pnv_spi_controller.h"
  #include "hw/ppc/pnv_spi_responder.h"

hw/ppc/pnv_spi_controller.c static uint64_t pnv_spi_controller_read(void *opaque, hwaddr addr, · line 157

Quoted 4 lines
hw/ppc/pnv_spi_controller.c +1 -0
@@ -155,6 +154,12 @@ static uint64_t pnv_spi_controller_read(void *opaque, hwaddr addr,
                             val));
          sc->status_reg = SETFIELD(STATUS_REG_RDR_FULL, sc->status_reg, 0);
          SPI_DEBUG(qemu_log("RDR being read, RDR_full set to 0\n"));
+        if (GETFIELD(STATUS_REG_SHIFTER_FSM, sc->status_reg) == FSM_WAIT) {

In the other patch you seemed to have used extract() and deposit(). Now you are switching to GETFIELD() and SETFIELD(). Any reason for this?

hw/ppc/pnv_spi_controller.c static uint64_t pnv_spi_controller_read(void *opaque, hwaddr addr, · line 158

Quoted 1 line
hw/ppc/pnv_spi_controller.c +1 -0
+                /* call $operation_sequencer(); */

You can remove this comment -- it doesn't add much

Quoted 29 lines
hw/ppc/pnv_spi_controller.c +15 -0
+            SPI_DEBUG(qemu_log("RDR being read while shifter is waiting, "
+                           "starting sequencer\n"));
+            operation_sequencer(sc);
+        }
          break;
      case SEQUENCER_OPERATION_REG:
          for (int i = 0; i < SPI_CONTROLLER_REG_SIZE; i++) {
@@ -227,6 +232,9 @@ static void pnv_spi_controller_write(void *opaque, hwaddr addr,
          sc->status_reg = SETFIELD(STATUS_REG_TDR_UNDERRUN, sc->status_reg, 0);
          SPI_DEBUG(qemu_log("TDR being written, TDR_underrun set to 0\n"));
          SPI_DEBUG(qemu_log("TDR being written, starting sequencer\n"));
+        /* call $operation_sequencer(); */
+        operation_sequencer(sc);
+
          break;
      case RECEIVE_DATA_REG:
          sc->receive_data_reg = val;
@@ -264,8 +272,1272 @@ static const MemoryRegionOps pnv_spi_controller_xscom_ops = {
      .endianness = DEVICE_BIG_ENDIAN,
  };
 
+uint8_t reverse_bits8(uint8_t x)
+{
+    x = (x << 4) | (x >> 4);
+    x = ((x & 0x33) << 2) | ((x & 0xcc) >> 2);
+    x = ((x & 0x55) << 1) | ((x & 0xaa) >> 1);
+    return x;
+}
+

hw/ppc/pnv_spi_controller.c static const MemoryRegionOps pnv_spi_controller_xscom_ops = { · line 292

Quoted 10 lines
hw/ppc/pnv_spi_controller.c +10 -0
+bool does_rdr_match(PnvSpiController *sc)
+{
+    /*
+     * The mask bits that are 0 are compared and the
+     * bits that are 1 are ignored.
+     */
+    uint16_t rdr_match_mask = GETFIELD(MEMORY_MAPPING_REG_RDR_MATCH_MASK,
+                                        sc->memory_mapping_reg);
+    uint16_t rdr_match_val = GETFIELD(MEMORY_MAPPING_REG_RDR_MATCH_VAL,
+                                        sc->memory_mapping_reg);

empty line after var decl -- applies to rest of file / all patches

hw/ppc/pnv_spi_controller.c static const MemoryRegionOps pnv_spi_controller_xscom_ops = { · line 301

Quoted 9 lines
hw/ppc/pnv_spi_controller.c +9 -0
+    if ((~rdr_match_mask & rdr_match_val) == ((~rdr_match_mask) &
+            GETFIELD(PPC_BITMASK(48, 63), sc->receive_data_reg))) {
+        SPI_DEBUG(qemu_log("RDR match successful, match=0x%4.4x, "
+                                 "mask=0x%4.4x, RDR[48:63]=0x%4.4llx\n",
+                           rdr_match_val, rdr_match_mask,
+                                  GETFIELD(PPC_BITMASK(48, 63),
+                                   sc->receive_data_reg)));
+        return true;
+    } else {

No else branch necessary.

Quoted 7 lines
hw/ppc/pnv_spi_controller.c +7 -0
+        SPI_DEBUG(qemu_log("RDR match failed, match=0x%4.4x, mask=0x%4.4x, "
+              "RDR[48:63]=0x%4.4llx\n", rdr_match_val, rdr_match_mask,
+               GETFIELD(PPC_BITMASK(48, 63), sc->receive_data_reg)));
+        return false;
+    }
+}
+

hw/ppc/pnv_spi_controller.c static const MemoryRegionOps pnv_spi_controller_xscom_ops = { · line 318

Quoted 10 lines
hw/ppc/pnv_spi_controller.c +10 -0
+uint8_t get_from_offset(PnvSpiController *sc, uint8_t offset)
+{
+    uint8_t byte;
+    /*
+     * Offset is an index between 0 and SPI_CONTROLLER_REG_SIZE - 1
+     * Check the offset before using it.
+     */
+    if (offset < SPI_CONTROLLER_REG_SIZE) {
+        byte = GETFIELD(PPC_BITMASK(offset * 8 , (offset * 8) + 7),
+                        sc->transmit_data_reg);

Would find this easier to read:

byte = sc->transmit_data_reg >> (56 - (offset * 8))

Quoted 5 lines
hw/ppc/pnv_spi_controller.c +5 -0
+    } else {
+        /*
+         * Log an error and return a 0xFF since we have to assign something
+         * to byte before returning.
+         */

hw/ppc/pnv_spi_controller.c static const MemoryRegionOps pnv_spi_controller_xscom_ops = { · line 333

Quoted 10 lines
hw/ppc/pnv_spi_controller.c +10 -0
+        qemu_log_mask(LOG_GUEST_ERROR, "Invalid offset = %d used to get byte "
+                      "from TDR\n", offset);
+        byte = 0xff;
+    }
+    return byte;
+}
+
+void shift_byte_in(PnvSpiController *sc, uint8_t byte)
+{
+    sc->receive_data_reg = (sc->receive_data_reg << 8) | byte;

No extract/deposit or SETFIELD & GETFIELD. :-) thanks

Quoted 19 lines
hw/ppc/pnv_spi_controller.c +19 -0
+    SPI_DEBUG(qemu_log("0x%2.2x shifted in, RDR now = 0x%16.16lx\n", byte,
+                    sc->receive_data_reg));
+}
+
+void spi_response(PnvSpiController *sc, int bits, xfer_buffer *rsp_payload)
+{
+    uint8_t *read_buf = NULL;
+    /*
+     * Processing here must handle:
+     * - Which bytes in the payload we should move to the RDR
+     * - Explicit mode counter configuration settings
+     * - RDR full and RDR overrun status
+     */
+
+    /*
+     * First check that the response payload is the exact same
+     * number of bytes as the request payload was
+     */
+    if (rsp_payload->len != (sc->N1_bytes + sc->N2_bytes)) {

hw/ppc/pnv_spi_controller.c static const MemoryRegionOps pnv_spi_controller_xscom_ops = { · line 362

Quoted 10 lines
hw/ppc/pnv_spi_controller.c +10 -0
+        qemu_log_mask(LOG_GUEST_ERROR, "Invalid response payload size in "
+                       "bytes, expected %d, got %d\n",
+                       (sc->N1_bytes + sc->N2_bytes), rsp_payload->len);
+    } else {
+        SPI_DEBUG(qemu_log("SPI response received, payload len = %d\n",
+                       rsp_payload->len));
+        log_all_N_counts(sc);
+        /*
+         * Adding an ECC count let's us know when we have found a payload byte
+         * that was shifted in but cannot be loaded into RDR.  Bits 29-30

Bits 29-30 of 'what' ? clock_config_reset_control?

hw/ppc/pnv_spi_controller.c static const MemoryRegionOps pnv_spi_controller_xscom_ops = { · line 369

Quoted 7 lines
hw/ppc/pnv_spi_controller.c +7 -0
+         * equal to either 0b00 or 0b10 indicate that we are taking in data
+         * with ECC and either applying the ECC or discarding it.
+         */
+        uint8_t ecc_count = 0;
+        uint8_t ecc_control = GETFIELD(CLOCK_CONFIG_REG_ECC_CONTROL,
+                                    sc->clock_config_reset_control);
+        uint8_t ecc_control_1_2 = GETFIELD(PPC_BITMASK8(1, 2), ecc_control);

Var devcls to top of block (vdttob).

Hm, I wished you were using extract/extract8 again...

hw/ppc/pnv_spi_controller.c static const MemoryRegionOps pnv_spi_controller_xscom_ops = { · line 379

Quoted 10 lines
hw/ppc/pnv_spi_controller.c +10 -0
+        if (ecc_control_1_2 == 0 || ecc_control_1_2 == 2) {
+            ecc_count = 1;
+        }
+        /*
+         * Use the N1_rx and N2_rx counts to control shifting data from the
+         * payload into the RDR.  Keep an overall count of the number of bytes
+         * shifted into RDR so we can discard every 9th byte when ECC is
+         * enabled.
+         */
+        uint8_t shift_in_count = 0;

vdttob

hw/ppc/pnv_spi_controller.c static const MemoryRegionOps pnv_spi_controller_xscom_ops = {

Quoted 12 lines
hw/ppc/pnv_spi_controller.c +12 -0
+        /* Handle the N1 portion of the frame first */
+        if (sc->N1_rx != 0) {
+            uint8_t n1_count = 0;
+            while (n1_count < sc->N1_bytes) {
+                shift_in_count++;
+                xfer_buffer_read_ptr(rsp_payload, &read_buf, n1_count, 1);
+                if ((ecc_count != 0) &&
+                    (shift_in_count == (SPI_CONTROLLER_REG_SIZE + ecc_count))) {
+                    SPI_DEBUG(qemu_log("Discarding rx N1 ECC byte = 0x%2.2x at "
+                           "payload index = %d\n", read_buf[0], n1_count));
+                    shift_in_count = 0;
+                } else { > +                    uint8_t n1_byte = 0x00;

no need to initialize it since you assign it immediately read_buf[0].

Quoted 12 lines
hw/ppc/pnv_spi_controller.c +12 -0
+                    n1_byte = read_buf[0];
+                    SPI_DEBUG(qemu_log("Extracting rx n1_byte = 0x%2.2x from "
+                            "payload at index = %d\n", n1_byte, n1_count));
+                    if (sc->reverse_bits) {
+                        SPI_DEBUG(qemu_log("Reversing bit order of rx "
+                                            "n1_byte\n"));
+                        n1_byte = reverse_bits8(n1_byte);
+                    }
+                    SPI_DEBUG(qemu_log("Shifting rx N1 byte = 0x%2.2x into "
+                                   "RDR\n", n1_byte));
+                    shift_byte_in(sc, n1_byte);
+                }

hw/ppc/pnv_spi_controller.c static const MemoryRegionOps pnv_spi_controller_xscom_ops = { · line 414

Quoted 10 lines
hw/ppc/pnv_spi_controller.c +10 -0
+                n1_count++;
+            } /* end of while */
+        }
+        /* Handle the N2 portion of the frame */
+        if (sc->N2_rx != 0) {
+            uint8_t n2_count = 0;
+            while (n2_count < sc->N2_bytes) {
+                shift_in_count++;
+                xfer_buffer_read_ptr(rsp_payload, &read_buf,
+                                (sc->N1_bytes + n2_count), 1);

Could you not move this outside the loop :

xfer_buffer_read_ptr(rsp_payload, &read_buf, sc->N1_bytes, sc->N2_bytes);

Same in the n1 loop above. And then you could think of creating a function for what seems to be almost duplicate code for the n1 and n2 loops.

Quoted 10 lines
hw/ppc/pnv_spi_controller.c +10 -0
+                if ((ecc_count != 0) &&
+                    (shift_in_count == (SPI_CONTROLLER_REG_SIZE + ecc_count))) {
+                    SPI_DEBUG(qemu_log("Discarding rx N1 ECC byte = 0x%2.2x at "
+                           "payload index = %d\n", read_buf[0],
+                           (sc->N1_bytes + n2_count)));
+                    shift_in_count = 0;
+                } else {
+                    /*
+                     * The code handles shifting data from the payload received
+                     * from the responder into the responder's RDR.  Since this

hw/ppc/pnv_spi_controller.c static const MemoryRegionOps pnv_spi_controller_xscom_ops = { · line 434

Quoted 10 lines
hw/ppc/pnv_spi_controller.c +10 -0
+                     * is an N2 frame segment it is safe to assume that there
+                     * was a preceding N1 segment which was combined with an N2
+                     * segment to create a single frame. The response data will
+                     * then have N1_bytes of data in the payload representing a
+                     * responder response to the N1 section of the frame. If N2
+                     * is set to receive the shifting for N2 data begins after
+                     * the N1 bytes regardless of whether or not N1 was marked
+                     * for transmit or receive.
+                     */
+                    uint8_t n2_byte = 0x00;

same comment as above

Quoted 43 lines
hw/ppc/pnv_spi_controller.c +43 -0
+                    n2_byte = read_buf[0];
+                    SPI_DEBUG(qemu_log("Extracting rx n2_byte = 0x%2.2x from "
+                            "payload at index = %d\n", n2_byte,
+                            (sc->N1_bytes + n2_count)));
+                    if (sc->reverse_bits) {
+                        SPI_DEBUG(qemu_log("Reversing bit order of rx "
+                                            "n2_byte\n"));
+                        n2_byte = reverse_bits8(n2_byte);
+                    }
+                    SPI_DEBUG(qemu_log("Shifting rx N2 byte = 0x%2.2x into "
+                              "RDR\n", n2_byte));
+                    shift_byte_in(sc, n2_byte);
+                }
+                n2_count++;
+            }
+        }
+        if ((sc->N1_rx + sc->N2_rx) > 0) {
+            /*
+             * Data was received so handle RDR status.
+             * It is easier to handle RDR_full and RDR_overrun status here
+             * since the RDR register's shift_byte_in method is called
+             * multiple times in a row. Controlling RDR status is done here
+             * instead of in the RDR scoped methods for that reason.
+             */
+            if (GETFIELD(STATUS_REG_RDR_FULL, sc->status_reg) == 1) {
+                /*
+                 * Data was shifted into the RDR before having been read
+                 * causing previous data to have been overrun.
+                 */
+                sc->status_reg = SETFIELD(STATUS_REG_RDR_OVERRUN,
+                                sc->status_reg, 1);
+            } else {
+                /*
+                 * Set status to indicate that the received data register is
+                 * full. This flag is only cleared once the RDR is unloaded.
+                 */
+                sc->status_reg = SETFIELD(STATUS_REG_RDR_FULL,
+                                sc->status_reg, 1);
+                SPI_DEBUG(qemu_log("RDR_full set to 1\n"));
+            }
+        }
+    } /* end of else */
+} /* end of spi_response() */

hw/ppc/pnv_spi_controller.c static const MemoryRegionOps pnv_spi_controller_xscom_ops = { · line 487

Quoted 10 lines
hw/ppc/pnv_spi_controller.c +10 -0
+
+void log_all_N_counts(PnvSpiController *sc)
+{
+    SPI_DEBUG(qemu_log("N1_bits = %d, N1_bytes = %d, N1_tx = %d, N1_rx = %d, "
+          "N2_bits = %d, N2_bytes = %d, N2_tx = %d, N2_rx = %d\n",
+           sc->N1_bits, sc->N1_bytes, sc->N1_tx, sc->N1_rx, sc->N2_bits,
+           sc->N2_bytes, sc->N2_tx, sc->N2_rx));
+}
+
+void operation_sequencer(PnvSpiController *sc)

All function namees in this file should have a prefix I would say.

Quoted 33 lines
hw/ppc/pnv_spi_controller.c +33 -0
+{
+    /*
+     * Loop through each sequencer operation ID and perform the requested
+     *  operations.
+     * Flag for indicating if we should send the N1 frame or wait to combine
+     * it with a preceding N2 frame.
+     */
+    bool send_n1_alone = true;
+    /* Flag to stop the sequencer */
+    bool stop = false;
+
+    /*
+     * xfer_buffer for containing the payload of the SPI frame.
+     * This is a static because there are cases where a sequence has to stop
+     * and wait for the target application to unload the RDR.  If this occurs
+     * during a sequence where N1 is not sent alone and instead combined with
+     * N2 since the N1 tx length + the N2 tx length is less than the size of
+     * the TDR.
+     */
+    static xfer_buffer *payload;
+    if (payload == NULL) {
+        payload = xfer_buffer_new();
+    }
+    /*
+     * Clear the sequencer FSM error bit - general_SPI_status[3]
+     * before starting a sequence.
+     */
+    sc->status_reg = SETFIELD(PPC_BIT(35), sc->status_reg, 0);
+    /*
+     * If the FSM is idle set the sequencer index to 0
+     * (new/restarted sequence)
+     */
+    if (GETFIELD(STATUS_REG_SEQUENCER_FSM, sc->status_reg) ==

hw/ppc/pnv_spi_controller.c static const MemoryRegionOps pnv_spi_controller_xscom_ops = { · line 530

Quoted 10 lines
hw/ppc/pnv_spi_controller.c +10 -0
+                    SEQ_STATE_IDLE) {
+        sc->status_reg = SETFIELD(STATUS_REG_SEQUENCER_INDEX,
+                                  sc->status_reg, 0);
+    }
+    /*
+     * There are only 8 possible operation IDs to iterate through though
+     * some operations may cause more than one frame to be sequenced.
+     */
+    while (GETFIELD(STATUS_REG_SEQUENCER_INDEX, sc->status_reg) < 8) {
+        uint8_t opcode = 0;

no need to assign a value.

Quoted 56 lines
hw/ppc/pnv_spi_controller.c +56 -0
+        uint8_t masked_opcode = 0;
+
+        opcode = sc->sequencer_operation_reg[GETFIELD(
+                        STATUS_REG_SEQUENCER_INDEX, sc->status_reg)];
+        /* Set sequencer state to decode */
+        sc->status_reg = SETFIELD(STATUS_REG_SEQUENCER_FSM, sc->status_reg,
+                                  SEQ_STATE_DECODE);
+        /*
+         * Only the upper nibble of the operation ID is needed to know what
+         * kind of operation is requested.
+         */
+        masked_opcode = opcode & 0xF0;
+        switch (masked_opcode) {
+        /*
+         * Increment the operation index in each case instead of just
+         * once at the end in case an operation like the branch
+         * operation needs to change the index.
+         */
+        case SEQ_OP_STOP:
+            sc->status_reg = SETFIELD(STATUS_REG_SEQUENCER_FSM,
+                                      sc->status_reg, SEQ_STATE_EXECUTE);
+            /* A stop operation in any position stops the sequencer */
+            SPI_DEBUG(qemu_log("Sequencer STOP at index = 0x%llx, sequencer "
+                               "idling\n", GETFIELD(
+                               STATUS_REG_SEQUENCER_INDEX,
+                               sc->status_reg)));
+            stop = true;
+            sc->status_reg = SETFIELD(STATUS_REG_SHIFTER_FSM, sc->status_reg,
+                                          FSM_IDLE);
+            sc->loop_counter_1 = 0;
+            sc->loop_counter_2 = 0;
+            sc->status_reg = SETFIELD(STATUS_REG_SEQUENCER_FSM,
+                                      sc->status_reg, SEQ_STATE_IDLE);
+            break;
+
+        case SEQ_OP_SELECT_SLAVE:
+            sc->status_reg = SETFIELD(STATUS_REG_SEQUENCER_FSM,
+                                      sc->status_reg, SEQ_STATE_EXECUTE);
+            SPI_DEBUG(qemu_log("Sequencer SELECT_SLAVE at index = 0x%llx\n",
+                GETFIELD(STATUS_REG_SEQUENCER_INDEX, sc->status_reg)));
+            /*
+             * This device currently only supports a single responder
+             * connection at position 0.  De-selecting a responder is fine
+             * and expected at the end of a sequence but selecting any
+             * responder other than 0 should cause an error.
+             */
+            sc->responder_select = opcode & 0x0F;
+            if (sc->responder_select == 0) {
+                if (spi_disconnect_controller(sc->spi_bus)) {
+                    SPI_DEBUG(qemu_log("Slave (present) de-selected, "
+                                       "shifter done\n"));
+                } else {
+                   SPI_DEBUG(qemu_log("Slave (not-present) de-selected "
+                                      "(no-op), shifter done\n"));
+                }
+                sc->status_reg = SETFIELD(STATUS_REG_SEQUENCER_INDEX,

hw/ppc/pnv_spi_controller.c static const MemoryRegionOps pnv_spi_controller_xscom_ops = { · line 596

Quoted 10 lines
hw/ppc/pnv_spi_controller.c +10 -0
+                                          sc->status_reg,
+                                          (GETFIELD(
+                                          STATUS_REG_SEQUENCER_INDEX,
+                                          sc->status_reg) + 1));
+                sc->status_reg = SETFIELD(STATUS_REG_SHIFTER_FSM,
+                                          sc->status_reg, FSM_DONE);
+            } else if (sc->responder_select != 1) {
+                qemu_log_mask(LOG_GUEST_ERROR, "Slave selection other than 1 "
+                              "not supported, select = 0x%x\n",
+                               sc->responder_select);

Does this mean that the SPI controller can only support 1 slave? what is responed_select = 1 -- some kind of slave id?

Quoted 6 lines
hw/ppc/pnv_spi_controller.c +6 -0
+                SPI_DEBUG(qemu_log("Sequencer stop requested due to invalid "
+                                        "responder select at index = 0x%llx, "
+                                   "shifter idling\n", GETFIELD(
+                                   STATUS_REG_SEQUENCER_INDEX,
+                                   sc->status_reg)));
+                sc->status_reg = SETFIELD(STATUS_REG_SHIFTER_FSM,

hw/ppc/pnv_spi_controller.c static const MemoryRegionOps pnv_spi_controller_xscom_ops = { · line 612

Quoted 10 lines
hw/ppc/pnv_spi_controller.c +10 -0
+                                          sc->status_reg, FSM_IDLE);
+                stop = true;
+            } else {
+                /*
+                 * Only allow an FSM_START state when a responder is
+                 * selected
+                 */
+                sc->status_reg = SETFIELD(STATUS_REG_SHIFTER_FSM,
+                                          sc->status_reg, FSM_START);
+                if (spi_connect_controller(sc->spi_bus, NULL)) {

My guess is sc->responder_select should be passed to the spi_connect_controller(). I am not much familiar with SPI but do you need an explicit connection or should it not rather be a lookup whether a slave with the given id is registered/available?

Quoted 6 lines
hw/ppc/pnv_spi_controller.c +6 -0
+                    SPI_DEBUG(qemu_log("Slave 0x%x (present) selected, "
+                                       "shifter starting\n",
+                                       sc->responder_select));
+                } else {
+                    SPI_DEBUG(qemu_log("Slave 0x%x (not-present) selected "
+                                        "(no-op), shifter starting\n",

hw/ppc/pnv_spi_controller.c static const MemoryRegionOps pnv_spi_controller_xscom_ops = { · line 628

Quoted 10 lines
hw/ppc/pnv_spi_controller.c +10 -0
+                                       sc->responder_select));
+                }
+                sc->first = 1;
+                sc->last = 0;
+                /*
+                 * A Shift_N2 operation is only valid after a Shift_N1. We
+		 * will track the occurrence of a Shift_N1 to enforce this
+		 * requirement in the most generic way possible by assuming
+		 * that the rule applies once a valid responder select has
+		 * occurred.

Indentation.

Quoted 135 lines
hw/ppc/pnv_spi_controller.c +135 -0
+                 */
+                sc->shift_n1_done = false;
+                sc->status_reg = SETFIELD(STATUS_REG_SEQUENCER_FSM,
+                                          sc->status_reg,
+                                          SEQ_STATE_INDEX_INCREMENT);
+                sc->status_reg = SETFIELD(STATUS_REG_SEQUENCER_INDEX,
+                                          sc->status_reg,
+                                (GETFIELD(STATUS_REG_SEQUENCER_INDEX,
+                                          sc->status_reg) + 1));
+            }
+            break;
+
+        case SEQ_OP_SHIFT_N1:
+            sc->status_reg = SETFIELD(STATUS_REG_SEQUENCER_FSM,
+                                      sc->status_reg, SEQ_STATE_EXECUTE);
+            SPI_DEBUG(qemu_log("Sequencer SHIFT_N1 at index = 0x%llx\n",
+                GETFIELD(STATUS_REG_SEQUENCER_INDEX, sc->status_reg)));
+            /*
+             * Only allow a shift_n1 when the state is not IDLE or DONE.
+             * In either of those two cases the sequencer is not in a proper
+             * state to perform shift operations because the sequencer has:
+             * - processed a responder deselect (DONE)
+             * - processed a stop opcode (IDLE)
+             * - encountered an error (IDLE)
+             */
+            if ((GETFIELD(STATUS_REG_SHIFTER_FSM,
+                               sc->status_reg) == FSM_IDLE) ||
+                (GETFIELD(STATUS_REG_SHIFTER_FSM,
+                               sc->status_reg) == FSM_DONE)) {
+                qemu_log_mask(LOG_GUEST_ERROR, "Shift_N1 not allowed in "
+                              "shifter state = 0x%llx", GETFIELD(
+                        STATUS_REG_SHIFTER_FSM, sc->status_reg));
+                /*
+                 * Set sequencer FSM error bit 3 (general_SPI_status[3])
+                 * in status reg.
+                 */
+                sc->status_reg = SETFIELD(PPC_BIT(35), sc->status_reg, 1);
+                SPI_DEBUG(qemu_log("Sequencer stop requested due to invalid "
+                      "shifter state at index = 0x%llx\n", GETFIELD(
+                         STATUS_REG_SEQUENCER_INDEX, sc->status_reg)));
+                stop = true;
+            } else {
+                /*
+                 * Look for the special case where there is a shift_n1 set for
+                 * transmit and it is followed by a shift_n2 set for transmit
+                 * AND the combined transmit length of the two operations is
+                 * less than or equal to the size of the TDR register. In this
+                 * case we want to use both this current shift_n1 opcode and the
+                 * following shift_n2 opcode to assemble the frame for
+                 * transmission to the responder without requiring a refill of
+                 * the TDR between the two operations.
+                 */
+                if ((sc->sequencer_operation_reg[GETFIELD(
+                     STATUS_REG_SEQUENCER_INDEX, sc->status_reg) + 1] & 0xF0)
+                                == SEQ_OP_SHIFT_N2) {
+                    SPI_DEBUG(qemu_log("Not sending N1 alone\n"));
+                    send_n1_alone = false;
+                }
+                /*
+                 * If the next opcode is 0x10, which deselects the SPI device
+                 * then this is the last shift
+                 */
+                if (sc->sequencer_operation_reg[GETFIELD(
+                    STATUS_REG_SEQUENCER_INDEX, sc->status_reg) + 1] ==
+                                SEQ_OP_SELECT_SLAVE) {
+                    sc->last = 1;
+                }
+                sc->status_reg = SETFIELD(STATUS_REG_SHIFTER_FSM,
+                                sc->status_reg, FSM_SHIFT_N1);
+                stop = operation_shiftn1(sc, opcode, &payload, send_n1_alone);
+                if (stop) {
+                    /*
+                     *  The operation code says to stop, this can occur if:
+                     * (1) RDR is full and the N1 shift is set for receive
+                     * (2) TDR was empty at the time of the N1 shift so we need
+                     * to wait for data.
+                     * (3) Neither 1 nor 2 are occurring and we aren't sending
+                     * N1 alone and N2 counter reload is set (bit 0 of the N2
+                     * counter reload field).  In this case TDR_underrun will
+                     * will be set and the Payload has been loaded so it is
+                     * ok to advance the sequencer.
+                     */
+                    if (GETFIELD(STATUS_REG_TDR_UNDERRUN, sc->status_reg)) {
+                        SPI_DEBUG(qemu_log("Sequencer stop requested due to N2 "
+                              "counter reload active.\n"));
+                        sc->shift_n1_done = true;
+                        sc->status_reg = SETFIELD(STATUS_REG_SHIFTER_FSM,
+                                                  sc->status_reg,
+                                                  FSM_SHIFT_N2);
+                        sc->status_reg = SETFIELD(STATUS_REG_SEQUENCER_INDEX,
+                                                  sc->status_reg,
+                                                  (GETFIELD(
+                                                  STATUS_REG_SEQUENCER_INDEX,
+                                                  sc->status_reg) + 1));
+                        SPI_DEBUG(qemu_log("Set new sequencer index to = "
+                                           "0x%llx\n", GETFIELD(
+                                           STATUS_REG_SEQUENCER_INDEX,
+                                           sc->status_reg)));
+                    } else {
+                        /*
+                         * This is case (1) or (2) so the sequencer needs to
+                         * wait and NOT go to the next sequence yet.
+                         */
+                        sc->status_reg = SETFIELD(STATUS_REG_SHIFTER_FSM,
+                                                  sc->status_reg, FSM_WAIT);
+                    }
+                } else {
+                    /* Ok to move on to the next index */
+                    sc->shift_n1_done = true;
+                    sc->status_reg = SETFIELD(STATUS_REG_SEQUENCER_FSM,
+                                sc->status_reg, SEQ_STATE_INDEX_INCREMENT);
+                    sc->status_reg = SETFIELD(STATUS_REG_SEQUENCER_INDEX,
+                                              sc->status_reg,
+                                              (GETFIELD(
+                                              STATUS_REG_SEQUENCER_INDEX,
+                                              sc->status_reg) + 1));
+                }
+            }
+            break;
+
+        case SEQ_OP_SHIFT_N2:
+            sc->status_reg = SETFIELD(STATUS_REG_SEQUENCER_FSM,
+                                      sc->status_reg, SEQ_STATE_EXECUTE);
+            SPI_DEBUG(qemu_log("Sequencer SHIFT_N2 at index = %lld\n",
+                                GETFIELD(STATUS_REG_SEQUENCER_INDEX,
+                                         sc->status_reg)));
+            if (!sc->shift_n1_done) {
+                qemu_log_mask(LOG_GUEST_ERROR, "Shift_N2 is not allowed if a "
+                              "Shift_N1 is not done, shifter state = 0x%llx",
+                              GETFIELD(STATUS_REG_SHIFTER_FSM,
+                                       sc->status_reg));
+                /*
+                 * In case the sequencer actually stops if an N2 shift is
+                 * requested before any N1 shift is done. Set sequencer FSM
+                 * error bit 3 (general_SPI_status[3]) in status reg.

hw/ppc/pnv_spi_controller.c static const MemoryRegionOps pnv_spi_controller_xscom_ops = { · line 773

Quoted 10 lines
hw/ppc/pnv_spi_controller.c +10 -0
+                 */
+                sc->status_reg = SETFIELD(PPC_BIT(35), sc->status_reg, 1);
+                SPI_DEBUG(qemu_log("Sequencer stop requested due to shift_n2 "
+                                    "w/no shift_n1 done at index = 0x%llx\n",
+                                   GETFIELD(STATUS_REG_SEQUENCER_INDEX,
+                                            sc->status_reg)));
+                stop = true;
+            } else {
+                /*
+                 * If the next opcode is 0x10, which deselects the SPI device

... is SEQ_OP_SELECT_SLAVE (0x10), which ...

Quoted 18 lines
hw/ppc/pnv_spi_controller.c +18 -0
+                 * then this is the last shift
+                 */
+                if (sc->sequencer_operation_reg[GETFIELD(
+                    STATUS_REG_SEQUENCER_INDEX,
+                    sc->status_reg) + 1] == SEQ_OP_SELECT_SLAVE) {
+                    sc->last = 1;
+                }
+                /* Ok to do a Shift_N2 */
+                sc->status_reg = SETFIELD(STATUS_REG_SHIFTER_FSM,
+                                          sc->status_reg, FSM_SHIFT_N2);
+                stop = operation_shiftn2(sc, opcode, &payload);
+                /*
+                 * If the operation code says to stop set the shifter state to
+                 * wait and stop
+                 */
+                if (stop) {
+                    sc->status_reg = SETFIELD(STATUS_REG_SHIFTER_FSM,
+                                              sc->status_reg, FSM_WAIT);

hw/ppc/pnv_spi_controller.c static const MemoryRegionOps pnv_spi_controller_xscom_ops = { · line 801

Quoted 10 lines
hw/ppc/pnv_spi_controller.c +10 -0
+                } else {
+                    /* Ok to move on to the next index */
+                    sc->status_reg = SETFIELD(STATUS_REG_SEQUENCER_FSM,
+                                              sc->status_reg,
+                                              SEQ_STATE_INDEX_INCREMENT);
+                    sc->status_reg = SETFIELD(STATUS_REG_SEQUENCER_INDEX,
+                                              sc->status_reg,
+                                              (GETFIELD(
+                                              STATUS_REG_SEQUENCER_INDEX,
+                                              sc->status_reg) + 1));

This sequence seems to appear multiple times. Put into 'a function'.

Quoted 24 lines
hw/ppc/pnv_spi_controller.c +24 -0
+                }
+            }
+            break;
+
+        case SEQ_OP_BRANCH_IFNEQ_RDR:
+            sc->status_reg = SETFIELD(STATUS_REG_SEQUENCER_FSM,
+                                      sc->status_reg, SEQ_STATE_EXECUTE);
+            SPI_DEBUG(qemu_log("Sequencer BRANCH_IFNEQ_RDR at "
+                               "index = 0x%llx\n", GETFIELD(
+                               STATUS_REG_SEQUENCER_INDEX, sc->status_reg)));
+            /*
+             * The memory mapping register RDR match value is compared against
+             * the 16 rightmost bytes of the RDR (potentially with masking).
+             * Since this comparison is performed against the contents of the
+             * RDR then a receive must have previously occurred otherwise
+             * there is no data to compare and the operation cannot be
+             * completed and will stop the sequencer until RDR full is set to
+             * 1.
+             */
+            if (GETFIELD(STATUS_REG_RDR_FULL, sc->status_reg) == 1) {
+                bool rdr_matched = false;
+                rdr_matched = does_rdr_match(sc);
+                if (rdr_matched) {
+                    SPI_DEBUG(qemu_log("Proceed to next sequencer index "

hw/ppc/pnv_spi_controller.c static const MemoryRegionOps pnv_spi_controller_xscom_ops = { · line 835

Quoted 10 lines
hw/ppc/pnv_spi_controller.c +10 -0
+                                   "(increment on RDR match)\n"));
+                    /* A match occurred, increment the sequencer index. */
+                    sc->status_reg = SETFIELD(STATUS_REG_SEQUENCER_FSM,
+                                              sc->status_reg,
+                                              SEQ_STATE_INDEX_INCREMENT);
+                    sc->status_reg = SETFIELD(STATUS_REG_SEQUENCER_INDEX,
+                                              sc->status_reg,
+                                              (GETFIELD(
+                                              STATUS_REG_SEQUENCER_INDEX,
+                                              sc->status_reg) + 1));

... call 'the function'

Quoted 28 lines
hw/ppc/pnv_spi_controller.c +28 -0
+                } else {
+                    SPI_DEBUG(qemu_log("Proceed to sequencer index=0x%x "
+                          "(branch on RDR match fail)\n", (opcode & 0x7)));
+                    /*
+                     * Branch the sequencer to the index coded into the op
+                     * code.
+                     */
+                    sc->status_reg = SETFIELD(STATUS_REG_SEQUENCER_INDEX,
+                                              sc->status_reg, (opcode & 0x7));
+                }
+                /*
+                 * Regardless of where the branch ended up we want the
+                 * sequencer to continue shifting so we have to clear
+                 * RDR_full.
+                 */
+                sc->status_reg = SETFIELD(STATUS_REG_RDR_FULL,
+                                sc->status_reg, 0);
+            } else {
+                SPI_DEBUG(qemu_log("RDR not full for 0x6x opcode! Stopping "
+                      "sequencer.\n"));
+                stop = true;
+                sc->status_reg = SETFIELD(STATUS_REG_SHIFTER_FSM,
+                                         sc->status_reg, FSM_WAIT);
+            }
+            break;
+
+        case SEQ_OP_TRANSFER_TDR:
+            sc->status_reg = SETFIELD(STATUS_REG_SEQUENCER_FSM,

hw/ppc/pnv_spi_controller.c static const MemoryRegionOps pnv_spi_controller_xscom_ops = {

Quoted 10 lines
hw/ppc/pnv_spi_controller.c +10 -0
+                                      sc->status_reg, SEQ_STATE_EXECUTE);
+            qemu_log_mask(LOG_GUEST_ERROR, "Transfer TDR is not supported\n");
+            sc->status_reg = SETFIELD(STATUS_REG_SEQUENCER_FSM,
+                                      sc->status_reg,
+                                      SEQ_STATE_INDEX_INCREMENT);
+            /* status_reg.sequencer_index++ */
+            sc->status_reg = SETFIELD(STATUS_REG_SEQUENCER_INDEX,
+                                      sc->status_reg,
+                                      (GETFIELD(
+                                      STATUS_REG_SEQUENCER_INDEX, > +                                      sc->status_reg) + 1));

... again

Quoted 41 lines
hw/ppc/pnv_spi_controller.c +41 -0
+            break;
+
+        case SEQ_OP_BRANCH_IFNEQ_INC_1:
+            sc->status_reg = SETFIELD(STATUS_REG_SEQUENCER_FSM,
+                                      sc->status_reg, SEQ_STATE_EXECUTE);
+            SPI_DEBUG(qemu_log("Sequencer BRANCH_IFNEQ_INC_1 at index = "
+                               "0x%llx, next index = %d, count_compare_1 = "
+                               "0x%llx, loop_counter_1 = %d\n", GETFIELD(
+                               STATUS_REG_SEQUENCER_INDEX, sc->status_reg),
+                               (opcode & 0x07),
+                  GETFIELD(COUNTER_CONFIG_REG_COUNT_COMPARE1,
+                  sc->status_reg), sc->loop_counter_1));
+            if (sc->loop_counter_1 !=
+                GETFIELD(COUNTER_CONFIG_REG_COUNT_COMPARE1,
+                                sc->counter_config_reg)) {
+                /*
+                 * If the next opcode is 0x10, which deselects the SPI device
+                 * and we know that the next opcode is the last one in the
+                 * loop then the next shift is the last shift
+                 */
+                uint8_t condition1 = sc->sequencer_operation_reg[
+                                     GETFIELD(STATUS_REG_SEQUENCER_INDEX,
+                                     sc->status_reg) + 1];
+                uint8_t condition2 =
+                        GETFIELD(COUNTER_CONFIG_REG_COUNT_COMPARE1,
+                                 sc->counter_config_reg);
+                if ((condition1 == SEQ_OP_SELECT_SLAVE) &&
+                    ((sc->loop_counter_1 + 1) == condition2)) {
+                    sc->last = 1;
+                }
+                /*
+                 * Next index is the lower nibble of the branch operation ID,
+                 * mask off all but the first three bits so we don't try to
+                 * access beyond the sequencer_operation_reg boundary.
+                 */
+                sc->status_reg = SETFIELD(STATUS_REG_SEQUENCER_INDEX,
+                                          sc->status_reg, (opcode & 0x7));
+                sc->loop_counter_1++;
+                SPI_DEBUG(qemu_log("Branching to index = %d, loop_counter_1 = "
+                      "%d\n", (opcode & 0x7), sc->loop_counter_1));
+            } else {

hw/ppc/pnv_spi_controller.c static const MemoryRegionOps pnv_spi_controller_xscom_ops = { · line 925

Quoted 10 lines
hw/ppc/pnv_spi_controller.c +10 -0
+                /* Continue to next index if loop counter is reached */
+                sc->status_reg = SETFIELD(STATUS_REG_SEQUENCER_FSM,
+                                          sc->status_reg,
+                                          SEQ_STATE_INDEX_INCREMENT);
+                /* status_reg.sequencer_index++ */
+                sc->status_reg = SETFIELD(STATUS_REG_SEQUENCER_INDEX,
+                                          sc->status_reg,
+                                          (GETFIELD(
+                                          STATUS_REG_SEQUENCER_INDEX,
+                                          sc->status_reg) + 1));

.. and again.

That's how far I got for now. I think it will take multiple passes and others will have yet other comments on the code.

Stefan

#9 lore raw

PATCH 4/5 hw/ppc: SPI SEEPROM model

In reply to Chalapathi V

This commit implements a Serial EEPROM utilizing the Serial Peripheral Interface (SPI) compatible bus.
Currently implemented SEEPROM is Microchip's 25CSM04 which provides 4 Mbits of Serial EEPROM utilizing the Serial Peripheral Interface (SPI) compatible bus. The device is organized as 524288 bytes of 8 bits each (512Kbyte) and is optimized for use in consumer and industrial applications where reliable and dependable nonvolatile memory storage is essential.

---
 include/hw/ppc/pnv_spi_seeprom.h |  70 +++
 hw/ppc/pnv_spi_seeprom.c         | 989 +++++++++++++++++++++++++++++++
 hw/ppc/meson.build               |   1 +
 3 files changed, 1060 insertions(+)
 create mode 100644 include/hw/ppc/pnv_spi_seeprom.h
 create mode 100644 hw/ppc/pnv_spi_seeprom.c
include/hw/ppc/pnv_spi_seeprom.h new file +70 -0
new file mode 100644
index 0000000000..9739e411b5
@@ -0,0 +1,70 @@
+/*
+ * QEMU PowerPC SPI SEEPROM model
+ *
+ * Copyright (c) 2024, IBM Corporation.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ *
+ * This model implements a Serial EEPROM utilizing the Serial Peripheral
+ * Interface (SPI) compatible bus.
+ * Currently supported variants: 25CSM04.
+ * The Microchip Technology Inc. 25CSM04 provides 4 Mbits of Serial EEPROM
+ * utilizing the Serial Peripheral Interface (SPI) compatible bus. The device
+ * is organized as 524288 bytes of 8 bits each (512Kbyte) and is optimized
+ * for use in consumer and industrial applications where reliable and
+ * dependable nonvolatile memory storage is essential
+ */
+
+#ifndef PPC_PNV_SPI_SEEPROM_H
+#define PPC_PNV_SPI_SEEPROM_H
+
+#include "hw/ppc/pnv_spi_responder.h"
+#include "qom/object.h"
+
+#define TYPE_PNV_SPI_SEEPROM "pnv-spi-seeprom"
+
+OBJECT_DECLARE_SIMPLE_TYPE(PnvSpiSeeprom, PNV_SPI_SEEPROM)
+
+typedef struct xfer_buffer xfer_buffer;
+
+typedef struct PnvSpiSeeprom {
+    PnvSpiResponder resp;
+
+    char            *file; /* SEEPROM image file */
+    uint8_t         opcode; /* SEEPROM Opcode */
+    uint32_t        addr; /* SEEPROM Command Address */
+    uint8_t         rd_state; /* READ State Machine state variable */
+    bool            locked; /* Security Register Locked */
+    bool            controller_connected; /* Flag for master connection */
+    /*
+     * Device registers
+     * The 25CSM04 contains four types of registers that modulate device
+     * operation and/or report on the current status of the device. These
+     * registers are:
+     * STATUS register
+     * Security register
+     * Memory Partition registers (eight total)
+     * Identification register
+     */
+    uint8_t         status0;
+    uint8_t         status1;
+    /*
+     * The Security register is split into
+     * 1. user-programmable lockable ID page section.
+     * 2. The read-only section contains a preprogrammed, globally unique,
+     *    128-bit serial number.
+     */
+    uint8_t         uplid[256];
+    uint8_t         dsn[16];
+    uint8_t         mpr[8];
+    uint8_t         idr[5];
+} PnvSpiSeeprom;
+
+xfer_buffer *seeprom_spi_request(PnvSpiResponder *resp, int first, int last,
+                int bits, xfer_buffer *payload);
+void seeprom_connect_controller(PnvSpiResponder *resp, const char *port);
+void seeprom_disconnect_controller(PnvSpiResponder *resp);
+bool compute_addr(PnvSpiSeeprom *spi_resp, xfer_buffer *req_payload,
+                   xfer_buffer *rsp_payload, int bits, uint32_t *data_offset);
+bool validate_addr(PnvSpiSeeprom *spi_resp);
+#endif /* PPC_PNV_SPI_SEEPROM_H */
hw/ppc/pnv_spi_seeprom.c new file +989 -0

989 lines not on this page · raw message

hw/ppc/meson.build +1 -0
index de25cac763..2c1ef0c937 100644
@@ -55,6 +55,7 @@ ppc_ss.add(when: 'CONFIG_POWERNV', if_true: files(
   'pnv_pnor.c',
   'pnv_spi_responder.c',
   'pnv_spi_controller.c',
+  'pnv_spi_seeprom.c',
 ))
 # PowerPC 4xx boards
 ppc_ss.add(when: 'CONFIG_PPC405', if_true: files(
Signature · 1 line
2.31.1
#10 lore raw

Re: [PATCH v1 4/5] hw/ppc: SPI SEEPROM model

Quoted 228 lines · Chalapathi V

This commit implements a Serial EEPROM utilizing the Serial Peripheral Interface (SPI) compatible bus.
Currently implemented SEEPROM is Microchip's 25CSM04 which provides 4 Mbits of Serial EEPROM utilizing the Serial Peripheral Interface (SPI) compatible bus. The device is organized as 524288 bytes of 8 bits each (512Kbyte) and is optimized for use in consumer and industrial applications where reliable and dependable nonvolatile memory storage is essential.

---
  include/hw/ppc/pnv_spi_seeprom.h |  70 +++
  hw/ppc/pnv_spi_seeprom.c         | 989 +++++++++++++++++++++++++++++++
  hw/ppc/meson.build               |   1 +
  3 files changed, 1060 insertions(+)

create mode 100644 include/hw/ppc/pnv_spi_seeprom.h create mode 100644 hw/ppc/pnv_spi_seeprom.c

include/hw/ppc/pnv_spi_seeprom.h new file +70 -0
new file mode 100644
index 0000000000..9739e411b5
@@ -0,0 +1,70 @@
+/*
+ * QEMU PowerPC SPI SEEPROM model
+ *
+ * Copyright (c) 2024, IBM Corporation.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ *
+ * This model implements a Serial EEPROM utilizing the Serial Peripheral
+ * Interface (SPI) compatible bus.
+ * Currently supported variants: 25CSM04.
+ * The Microchip Technology Inc. 25CSM04 provides 4 Mbits of Serial EEPROM
+ * utilizing the Serial Peripheral Interface (SPI) compatible bus. The device
+ * is organized as 524288 bytes of 8 bits each (512Kbyte) and is optimized
+ * for use in consumer and industrial applications where reliable and
+ * dependable nonvolatile memory storage is essential
+ */
+
+#ifndef PPC_PNV_SPI_SEEPROM_H
+#define PPC_PNV_SPI_SEEPROM_H
+
+#include "hw/ppc/pnv_spi_responder.h"
+#include "qom/object.h"
+
+#define TYPE_PNV_SPI_SEEPROM "pnv-spi-seeprom"
+
+OBJECT_DECLARE_SIMPLE_TYPE(PnvSpiSeeprom, PNV_SPI_SEEPROM)
+
+typedef struct xfer_buffer xfer_buffer;
+
+typedef struct PnvSpiSeeprom {
+    PnvSpiResponder resp;
+
+    char            *file; /* SEEPROM image file */
+    uint8_t         opcode; /* SEEPROM Opcode */
+    uint32_t        addr; /* SEEPROM Command Address */
+    uint8_t         rd_state; /* READ State Machine state variable */
+    bool            locked; /* Security Register Locked */
+    bool            controller_connected; /* Flag for master connection */
+    /*
+     * Device registers
+     * The 25CSM04 contains four types of registers that modulate device
+     * operation and/or report on the current status of the device. These
+     * registers are:
+     * STATUS register
+     * Security register
+     * Memory Partition registers (eight total)
+     * Identification register
+     */
+    uint8_t         status0;
+    uint8_t         status1;
+    /*
+     * The Security register is split into
+     * 1. user-programmable lockable ID page section.
+     * 2. The read-only section contains a preprogrammed, globally unique,
+     *    128-bit serial number.
+     */
+    uint8_t         uplid[256];
+    uint8_t         dsn[16];
+    uint8_t         mpr[8];
+    uint8_t         idr[5];
+} PnvSpiSeeprom;
+
+xfer_buffer *seeprom_spi_request(PnvSpiResponder *resp, int first, int last,
+                int bits, xfer_buffer *payload);
+void seeprom_connect_controller(PnvSpiResponder *resp, const char *port);
+void seeprom_disconnect_controller(PnvSpiResponder *resp);
+bool compute_addr(PnvSpiSeeprom *spi_resp, xfer_buffer *req_payload,
+                   xfer_buffer *rsp_payload, int bits, uint32_t *data_offset);
+bool validate_addr(PnvSpiSeeprom *spi_resp);
+#endif /* PPC_PNV_SPI_SEEPROM_H */
hw/ppc/pnv_spi_seeprom.c new file +129 -0
new file mode 100644
index 0000000000..ae46610045
@@ -0,0 +1,989 @@
+/*
+ * QEMU PowerPC SPI SEEPROM model
+ *
+ * Copyright (c) 2024, IBM Corporation.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "qemu/osdep.h"
+#include "qemu/log.h"
+#include "hw/ppc/pnv_spi_seeprom.h"
+#include <math.h>
+
+#define SPI_DEBUG(x)
+
+/*
+ * 2-byte STATUS register which is a combination of six nonvolatile bits of
+ * EEPROM and five volatile latches.
+ *
+ * status 0:
+ * bit 7 WPEN: Write-Protect Enable bit
+ * 1 = Write-Protect pin is enabled, 0 = Write-Protect pin is ignored
+ *
+ * bit 3-2 BP<1:0>: Block Protection bits
+ * 00 = No array write protection
+ * 01 = Upper quarter memory array protection
+ * 10 = Upper half memory array protection
+ * 11 = Entire memory array protection
+ *
+ * bit 1 WEL: Write Enable Latch bit
+ * 1 = WREN has been executed and device is enabled for writing
+ * 0 = Device is not write-enabled
+ *
+ * bit 0 RDY/BSY: Ready/Busy Status Latch bit
+ * 1 = Device is busy with an internal write cycle
+ * 0 = Device is ready for a new sequence
+ */
+#define STATUS0_WPEN        0x7
+#define STATUS0_BP          0x2
+#define STATUS0_WEL         0x1
+#define STATUS0_BUSY        0x0
+
+/*
+ * status 1:
+ * bit 7 WPM: Write Protection Mode bit(1)
+ * 1 = Enhanced Write Protection mode selected (factory default)
+ * 0 = Legacy Write Protection mode selected
+ *
+ * bit 6 ECS: Error Correction State Latch bit
+ * 1 = The previously executed read sequence did require the ECC
+ * 0 = The previous executed read sequence did not require the ECC
+ *
+ * bit 5 FMPC: Freeze Memory Protection Configuration bit(2)
+ * 1 = Memory Partition registers and write protection mode are permanently
+ *     frozen and cannot be modified
+ * 0 = Memory Partition registers and write protection mode are not frozen
+ *     and are modifiable
+ *
+ * bit 4 PREL: Partition Register Write Enable Latch bit
+ * 1 = PRWE has been executed and WMPR, FRZR and PPAB instructions are enabled
+ * 0 = WMPR, FRZR and PPAB instructions are disabled
+ *
+ * bit 3 PABP: Partition Address Boundary Protection bit
+ * 1 = Partition Address Endpoints set in Memory Partition registers
+ *     cannot be modified
+ * 0 = Partition Address Endpoints set in Memory Partition registers
+ *     are modifiable
+ *
+ * bit 0 RDY/BSY: Ready/Busy Status Latch bit
+ * 1 = Device is busy with an internal write cycle
+ * 0 = Device is ready for a new sequence
+ */
+#define STATUS1_WPM             0x7
+#define STATUS1_ECS             0x6
+#define STATUS1_FMPC            0x5
+#define STATUS1_PREL            0x4
+#define STATUS1_PABP            0x3
+#define STATUS1_BUSY            0x0
+
+/*
+ * MEMORY PARTITION REGISTERS
+ * Note 1: The MPR cannot be written if the FMPC bit has been set.
+ *      2: The Partition Endpoint Address bits cannot be written if the PABP
+ *         bit has been set.
+ *
+ * bits 7-6 PB<1:0>: Partition Behavior bits(1)
+ * 00 = Partition is open and writing is permitted
+ *      factory default is unprotected.
+ * 01 = Partition is always write-protected but can be reversed at a later
+ *      time (software write-protected).
+ * 10 = Partition is write-protected only when WP pin is asserted
+ *      (hardware write-protected).
+ * 11 = Partition is software write-protected and MPR is permanently locked
+ *
+ * bit 5-0 A<18:13>: Partition Endpoint Address bits(1, 2)
+ * 000000 = Endpoint address of partition is set to 01FFFh.
+ * 000001 = Endpoint address of partition is set to 03FFFh.
+ * ----
+ * 111110 = Endpoint address of partition is set to 7DFFFh.
+ * 111111 = Endpoint address of partition is set to 7FFFFh.
+ */
+#define MPR_PB                  0x6
+#define MPR_PEA                 0x5
+
+/* INSTRUCTION SET FOR 25CSM04 */
+#define RDSR        0x05
+#define WRBP        0x08
+#define WREN        0x06
+#define WRDI        0x04
+#define WRSR        0x01
+#define READ        0x03
+#define WRITE       0x0
+#define RDEX_CHLK   0x83
+#define WREX_LOCK   0x82
+#define RMPR        0x31
+#define PRWE        0x07
+#define PRWD        0x0A
+#define WMPR        0x32
+#define PPAB        0x34
+#define FRZR        0x37
+#define SPID        0x9F
+#define SRST        0x7C
+
+/* READ FSM state */
+#define ST_IDLE     0
+#define ST_READ     1
+#define ST_SEC_READ 2
+
+xfer_buffer *seeprom_spi_request(PnvSpiResponder *resp,

hw/ppc/pnv_spi_seeprom.c line 139

Quoted 10 lines
hw/ppc/pnv_spi_seeprom.c new file +10 -0
+        int first, int last, int bits, xfer_buffer *payload)
+{
+    PnvSpiSeeprom *seeprom = PNV_SPI_SEEPROM(resp);
+    uint32_t data_offset = 0;
+    int data_len = 0;
+    xfer_buffer *rsp_payload = NULL;
+    uint8_t *read_buf = NULL;
+    uint8_t *buf = NULL;
+    uint16_t idx;
+    bool failed = false;

Add empty line after variables declaration.

Quoted 11 lines
hw/ppc/pnv_spi_seeprom.c new file +11 -0
+    SPI_DEBUG(qemu_log("Received SPI request, first=%d, last=%d, bits=%d, "
+              "payload length=%d\n", first, last, bits, payload->len));
+    if (seeprom->controller_connected == false) {
+        qemu_log_mask(LOG_GUEST_ERROR, "Controller is disconnected, invoke "
+                      "connect method of spi_responder interface\n");
+        return rsp_payload;
+    }
+    if (rsp_payload == NULL) {
+        rsp_payload = xfer_buffer_new();
+    }
+    buf = xfer_buffer_write_ptr(rsp_payload, 0, payload->len);

hw/ppc/pnv_spi_seeprom.c line 160

Quoted 10 lines
hw/ppc/pnv_spi_seeprom.c new file +10 -0
+    memset(buf, 0xFF, payload->len);
+    /*
+     * SPI communication is always full-duplex, so the controller receives as
+     * many bits as it sends, although often both the responder and controller
+     * device ignores some incoming bits. To simulate half-duplex the controller
+     * sends zeros to the responder when controller is receiving and ignores
+     * incoming data when the controller transmitting. So, a SPI response should
+     * always have the same length in bits as the corresponding request.
+     */
+    if ((payload->len != ceil(bits / 8)) || (payload->len <= 0)) {

len is a uint32 and can never be < 0.

Quoted 38 lines
hw/ppc/pnv_spi_seeprom.c new file +38 -0
+        qemu_log_mask(LOG_GUEST_ERROR, "Incorrect Payload size bits(%d) "
+                      "Payload_len(%d bytes)\n", bits, payload->len);
+        return rsp_payload;
+    }
+    if ((bits % 8) != 0) {
+        qemu_log_mask(LOG_GUEST_ERROR, "non-8bit aligned SPI transfer is "
+                      "unimplemented\n");
+        return rsp_payload;
+    }
+    /*
+     * Different scenarios for first and last SPI interface method parameters
+     *
+     * first(1) and last(1)
+     * SPI Controller can invoke spi_request with parameters first(1) and
+     * last(1), which indicates this is first and last spi_request in this
+     * transaction. This can be used when the valid data (excluding fake bytes)
+     * transmitted or received over SPI is less than or equal to 8 Bytes
+     *
+     * first(1) and last(0), # (required) first request
+     * first(0) and last(0), # (optional) in-between requests
+     * first(0) and last(0), # (optional) in-between requests
+     * ..
+     * ..
+     * first(0) and last(1), # (required) last request in the transaction
+     * SPI Controller can invoke spi_request multiple times with parameters
+     * first and last as shown in the sequence above for a transaction. This
+     * can be used when the valid data(excluding fake bytes) transmitted or
+     * received over SPI is more than 8 Bytes, SPI controller splits the
+     * transaction into multiple requests, this is due to TDR and RDR size(8B)
+     * restriction in SPI Controller.
+     */
+
+    /*
+     * check if first is "1", indicates a new incoming command sequence fetch
+     * the opcode and address from payload.
+     */
+    if (first == 1) {
+        /* Fetch opcode from offset 0 of payload */

hw/ppc/pnv_spi_seeprom.c line 208

Quoted 10 lines
hw/ppc/pnv_spi_seeprom.c new file +10 -0
+        xfer_buffer_read_ptr(payload, &read_buf, 0, 1);
+        seeprom->opcode = read_buf[0];
+        SPI_DEBUG(qemu_log("Command Opcode (0x%x)\n", seeprom->opcode));
+        /*
+         * Check if device is busy with internal write cycle, During this
+         * time, only the Read STATUS Register (RDSR) and the Write Ready/Busy
+         * Poll (WRBP) instructions will be executed by the device.
+         */
+        bool status0_busy = extract8(seeprom->status0, STATUS0_BUSY, 1);
+        bool status1_busy = extract8(seeprom->status1, STATUS1_BUSY, 1);

Variables should be declared at the top of the function or block.

Quoted 25 lines
hw/ppc/pnv_spi_seeprom.c new file +25 -0
+        if (((status0_busy == 1) || (status1_busy == 1)) &&
+            ((seeprom->opcode != RDSR) || (seeprom->opcode != WRBP))) {
+            qemu_log_mask(LOG_GUEST_ERROR, "Busy with Internal Write Cycle, "
+                           "opcode(0x%x) not executed\n", seeprom->opcode);
+            return rsp_payload;
+        }
+        /*
+         * Implement a state machine for READ sequence, to catch an error
+         * scenario when controller generates a new command sequence, with out
+         * properly terminating the READ sequence, as shown below
+         * first(1) and last(0), # READ command
+         * first(0) and last(0), # READ command continues
+         * ...
+         * first(1) and last(0,1), # New command sequence
+         * Not required to implement a state machine for write sequence as
+         * we can leverage status register for it
+         */
+        if (seeprom->rd_state != ST_IDLE) {
+            qemu_log_mask(LOG_GUEST_ERROR, "New Command Sequence with "
+                           "opcode(0x%x)is ignored Previous READ sequence is "
+                           "not terminated properly!!! Continuing the previous "
+                           "READ sequence\n", seeprom->opcode);
+            seeprom->opcode = (seeprom->rd_state == ST_READ) ? READ :
+                                                                 RDEX_CHLK;
+        } else {

hw/ppc/pnv_spi_seeprom.c

Quoted 10 lines
hw/ppc/pnv_spi_seeprom.c new file +10 -0
+            /*
+             * For a new command sequence compute Address and data offset in
+             * xfer_buffer.
+             */
+            failed = compute_addr(seeprom, payload, rsp_payload, bits,
+                            &data_offset);
+            /*
+             * Address computation failed, nothing to do further, just send
+             * response and return from here.
+             */ > +            if (failed == true) {

if (failed) {

hw/ppc/pnv_spi_seeprom.c line 248

Quoted 4 lines
hw/ppc/pnv_spi_seeprom.c new file +4 -0
+                return rsp_payload;
+            }
+        }
+    } /* end of branch if (first == 1) */

add empty line here

Quoted 5 lines
hw/ppc/pnv_spi_seeprom.c new file +5 -0
+    switch (seeprom->opcode) {
+    case READ:
+        SPI_DEBUG(qemu_log("READ(0x%x), addr(0x%x)\n",
+                  seeprom->opcode, seeprom->addr));
+        seeprom->rd_state = ST_READ;

hw/ppc/pnv_spi_seeprom.c line 263

Quoted 10 lines
hw/ppc/pnv_spi_seeprom.c new file +10 -0
+        data_len = payload->len - data_offset;
+        /* Make sure data is at least 1 Byte */
+        if (data_len <= 0) {
+            qemu_log_mask(LOG_GUEST_ERROR, "Insufficient Data Bytes(%dB), "
+                           "should be at least 1 Byte\n", data_len);
+            break;
+        }
+        /* Fill the buffer with the data read from image */
+        buf = xfer_buffer_write_ptr(rsp_payload, data_offset, data_len);
+        FILE *f;

Move variable decl to top of the block.

hw/ppc/pnv_spi_seeprom.c line 267

Quoted 4 lines
hw/ppc/pnv_spi_seeprom.c new file +4 -0
+        if (seeprom->file) {
+            f = fopen(seeprom->file, "rb+");
+            if (f) {
+                fseek(f, seeprom->addr, SEEK_SET);

Check return code.

hw/ppc/pnv_spi_seeprom.c line 268

Quoted 1 line
hw/ppc/pnv_spi_seeprom.c new file +1 -0
+                int read_len = fread(buf, sizeof(uint8_t), data_len, f);

move variable declaration to top of block

Quoted 39 lines
hw/ppc/pnv_spi_seeprom.c new file +39 -0
+                if (read_len == data_len) {
+                    SPI_DEBUG(qemu_log("Read %d bytes from seeprom\n",
+                                            read_len));
+                } else {
+                    if (ferror(f)) {
+                        SPI_DEBUG(qemu_log("Error reading seeprom\n"));
+                    }
+                }
+            }
+            fclose(f);
+        }
+        /* Check if last is 0 and increase address by data length */
+        if (last == 0) {
+            seeprom->addr = (seeprom->addr & 0x7FFFF) + data_len;
+        } else {
+            seeprom->rd_state = ST_IDLE;
+        }
+        break;
+
+    case RDSR:
+        SPI_DEBUG(qemu_log("READ Status Register RDSR(0x%x)\n",
+                    seeprom->opcode));
+        data_len = payload->len - data_offset;
+        /* Make sure data is at least 1 Byte */
+        if (data_len <= 0) {
+            qemu_log_mask(LOG_GUEST_ERROR, "Insufficient Data Bytes(%dB), "
+                           "should be at least 1 Byte\n", data_len);
+            break;
+        }
+        buf = xfer_buffer_write_ptr(rsp_payload, data_offset, data_len);
+        buf[0] = seeprom->status0;
+        if (data_len == 2) {
+            buf[1] = seeprom->status1;
+        }
+        break;
+
+    case WRBP:
+        SPI_DEBUG(qemu_log("Write Ready/Busy Poll WRBP(0x%x)\n",
+                    seeprom->opcode));

hw/ppc/pnv_spi_seeprom.c line 317

Quoted 10 lines
hw/ppc/pnv_spi_seeprom.c new file +10 -0
+        data_len = payload->len - data_offset;
+        /* Make sure data is at least 1 Byte */
+        if (data_len <= 0) {
+            qemu_log_mask(LOG_GUEST_ERROR, "Insufficient Data Bytes(%dB), "
+                           "should be at least 1 Byte\n", data_len);
+            break;
+        }
+        buf = xfer_buffer_write_ptr(rsp_payload, data_offset, 0x1);
+        bool status0_busy = extract8(seeprom->status0, STATUS0_BUSY, 1);
+        bool status1_busy = extract8(seeprom->status1, STATUS1_BUSY, 1);

variable declaration

Quoted 37 lines
hw/ppc/pnv_spi_seeprom.c new file +37 -0
+        if ((status0_busy == 1) || (status1_busy == 1)) {
+            buf[0] = 0xFF;
+        } else {
+            buf[0] = 0x00;
+        }
+        break;
+
+    case WREN:
+        SPI_DEBUG(qemu_log("Set Write Enable Latch (WEL) WREN(0x%x)\n",
+                    seeprom->opcode));
+        seeprom->status0 = deposit32(seeprom->status0, STATUS0_WEL, 1, 1);
+        break;
+
+    case WRDI:
+        SPI_DEBUG(qemu_log("Set Write Enable Latch (WEL) WRDI(0x%x)\n",
+                    seeprom->opcode));
+        seeprom->status0 = deposit32(seeprom->status0, STATUS0_WEL, 1, 0);
+        break;
+
+    case WRSR:
+        SPI_DEBUG(qemu_log("Write STATUS Register WRSR(0x%x)\n",
+                    seeprom->opcode));
+        if (extract8(seeprom->status0, STATUS0_WEL, 1) == 1) {
+            data_len = payload->len - data_offset;
+            /* Make sure data is at least 1 Byte */
+            if (data_len <= 0) {
+                qemu_log_mask(LOG_GUEST_ERROR, "Insufficient Data Bytes(%dB),"
+                                 " should be at least 1 Byte\n", data_len);
+                break;
+            }
+            /* Mask and update status0/1 bytes */
+            xfer_buffer_read_ptr(payload, &read_buf, 1, 2);
+            seeprom->status0 = read_buf[0] & 0x8C;
+            /* 2nd Status Byte is optional */
+            if (data_len == 2) {
+                seeprom->status1 = read_buf[1] & 0x80;
+            }

hw/ppc/pnv_spi_seeprom.c line 364

Quoted 10 lines
hw/ppc/pnv_spi_seeprom.c new file +10 -0
+        } else {
+            qemu_log_mask(LOG_GUEST_ERROR, "Set Write Enable Latch (WEL) "
+                            "before doing WRSR\n");
+        }
+        break;
+
+    case SPID:
+        SPI_DEBUG(qemu_log("READ IDENTIFICATION REGISTER, SPID(0x%x)\n",
+                    seeprom->opcode));
+        data_len = payload->len - data_offset;

Missing check here for data_len <= 0?

hw/ppc/pnv_spi_seeprom.c line 368

Quoted 4 lines
hw/ppc/pnv_spi_seeprom.c new file +4 -0
+        buf = xfer_buffer_write_ptr(rsp_payload, data_offset, data_len);
+        for (idx = 0; idx < data_len; idx++) {
+            buf[idx] = seeprom->idr[idx];
+        }

Use memcpy().

hw/ppc/pnv_spi_seeprom.c line 377

Quoted 9 lines
hw/ppc/pnv_spi_seeprom.c new file +9 -0
+        break;
+
+    case SRST:
+        SPI_DEBUG(qemu_log("Software Device Reset, SRST(0x%x)\n",
+                    seeprom->opcode));
+        /*
+         * Note: The SRST instruction cannot interrupt the device while it is
+         * in a Busy state (Section 6.1.4 Ready/Busy Status Latch).
+         * This is already taken care when the command opcode is fetched

... taken care of ...

hw/ppc/pnv_spi_seeprom.c line 385

Quoted 8 lines
hw/ppc/pnv_spi_seeprom.c new file +8 -0
+         *
+         * 1.2 Device Default State
+         * 1.2.1 POWER-UP DEFAULT STATE
+         * The 25CSM04 default state upon power-up consists of:
+         * - Standby Power mode (CS = HIGH)
+         * - A high-to-low level transition on CS is required to enter the
+         *   active state
+         `* - WEL bit in the STATUS register = 0

stray '`'

Quoted 11 lines
hw/ppc/pnv_spi_seeprom.c new file +11 -0
+         * - ECS bit in the STATUS register = 0
+         * - PREL bit in the STATUS register = 0
+         * - Ready/Busy (RDY/BUSY) bit in the STATUS register = 0, indicating
+         *   the device is ready to accept a new instruction.
+         */
+        seeprom->status0 = deposit32(seeprom->status0, STATUS0_WEL, 1, 0);
+        seeprom->status1 = deposit32(seeprom->status1, STATUS1_ECS, 1, 0);
+        seeprom->status1 = deposit32(seeprom->status1, STATUS1_PREL, 1, 0);
+        seeprom->status0 = deposit32(seeprom->status0, STATUS0_BUSY, 1, 0);
+        seeprom->status1 = deposit32(seeprom->status1, STATUS1_BUSY, 1, 0);
+        break;

hw/ppc/pnv_spi_seeprom.c line 406

Quoted 10 lines
hw/ppc/pnv_spi_seeprom.c new file +10 -0
+
+    case WRITE:
+        SPI_DEBUG(qemu_log("WRITE(0x%x), addr(0x%x)\n",
+                    seeprom->opcode, seeprom->addr));
+        if (extract8(seeprom->status0, STATUS0_WEL, 1) != 1) {
+            qemu_log_mask(LOG_GUEST_ERROR, "Device is not Write Enabled, "
+                            "ignoring WRITE instruction\n");
+            break;
+        }
+        /* Make sure data is at least 1 Byte */

You forgot to calculate data_len here it seems.

hw/ppc/pnv_spi_seeprom.c line 415

Quoted 9 lines
hw/ppc/pnv_spi_seeprom.c new file +9 -0
+        if (data_len <= 0) {
+            /*
+             * first   last    comment
+             * 0       0       data length cannot be 0
+             * 0       1       data length cannot be 0
+             * 1       0       data length can be 0, don't log error
+             * 1       1       data length cannot be 0
+             */
+            if (!(first == 1 && (last == 0))) {

(first == 1) so it looks like (last == 0)

hw/ppc/pnv_spi_seeprom.c line 428

Quoted 13 lines
hw/ppc/pnv_spi_seeprom.c new file +13 -0
+                qemu_log_mask(LOG_GUEST_ERROR, "Insufficient Data Bytes(%dB),"
+                                    " should be at least 1 Byte\n", data_len);
+                break;
+            }
+        }
+        /* Write into SEEPROM Array */
+        SPI_DEBUG(qemu_log("(%d)%s Write sequence\n", data_len,
+                            (data_len == 1) ? "Byte" : "Bytes Page"));
+        xfer_buffer_read_ptr(payload, &read_buf, data_offset, data_len);
+        if (seeprom->file) {
+            f = fopen(seeprom->file, "rb+");
+            if (f) {
+                fseek(f, seeprom->addr, SEEK_SET);

check return code

hw/ppc/pnv_spi_seeprom.c line 429

Quoted 1 line
hw/ppc/pnv_spi_seeprom.c new file +1 -0
+                int write_len = fwrite(read_buf, sizeof(uint8_t), data_len, f);

variable declaration

Quoted 11 lines
hw/ppc/pnv_spi_seeprom.c new file +11 -0
+                if (write_len == data_len) {
+                    SPI_DEBUG(qemu_log("Write %d bytes to seeprom\n",
+                                            write_len));
+                } else {
+                    SPI_DEBUG(qemu_log("Failed to write seeprom\n"));
+                }
+            }
+            fclose(f);
+        }
+        /* Increase offset in the page */
+        seeprom->addr += data_len;

hw/ppc/pnv_spi_seeprom.c

Quoted 10 lines
hw/ppc/pnv_spi_seeprom.c new file +10 -0
+        /* Check if last is 1 and end the sequence */
+        if (last == 1) {
+            seeprom->status0 = deposit32(seeprom->status0, STATUS0_WEL, 1, 0);
+        }
+        break;
+
+    case RMPR:
+        SPI_DEBUG(qemu_log("RMPR(0x%x) for MPR[%d]\n", seeprom->opcode,
+                  extract8(seeprom->addr, 16, 2)));
+        data_len = payload->len - data_offset

It may be worth considering another switch statement before this one where you do this repetitive data_len handling just once:

switch (seeprom->opcode) {
case READ:
...
case RMPR:
case WMPR:
case PPAB:

    data_len = payload->len - data_offset;
    if (data_len <= 0) {
        qem_log_mask(LOG_GUEST_ERROR, "%s: Insufficient number of 
bytes: %d", __func__, data_len););
        return NULL;
    }
    break;

default:
/* does not access payload */
}

hw/ppc/pnv_spi_seeprom.c line 458

Quoted 8 lines
hw/ppc/pnv_spi_seeprom.c new file +8 -0
+        /* Make sure data is at least 1 Byte */
+        if (data_len <= 0) {
+            qemu_log_mask(LOG_GUEST_ERROR, "Insufficient Data Bytes(%dB),"
+                             " should be at least 1 Byte\n", data_len);
+            break;
+        }
+        buf = xfer_buffer_write_ptr(rsp_payload, data_offset, 0x1);
+        buf[0] = seeprom->mpr[extract8(seeprom->addr, 16, 2)];

What is '16' in this case (used agaoin below also)? Add comment or define a constant?

Quoted 101 lines
hw/ppc/pnv_spi_seeprom.c new file +101 -0
+        break;
+
+    case PRWE:
+        SPI_DEBUG(qemu_log("Set Memory Partition Write Enable Latch "
+                    "PRWE(0x%x)\n", seeprom->opcode));
+        seeprom->status1 = deposit32(seeprom->status1, STATUS1_PREL, 1, 1);
+        break;
+
+    case PRWD:
+        SPI_DEBUG(qemu_log("Reset Memory Partition Write Enable Latch "
+                    "PRWD(0x%x)\n", seeprom->opcode));
+        seeprom->status1 = deposit32(seeprom->status1, STATUS1_PREL, 1, 0);
+        break;
+
+    case WMPR:
+        SPI_DEBUG(qemu_log("Write Memory Partition Register[%d] WMPR(0x%x)\n",
+                    extract8(seeprom->addr, 16, 2), seeprom->opcode));
+        /*
+         * Once the WEL and PREL bits in the STATUS register have been set to
+         * 1, the Memory Partition registers can be programmed provided that
+         * the FMPC bit in the STATUS register has not already been set to a
+         * logic 1.
+         */
+        if ((extract8(seeprom->status0, STATUS0_WEL, 1) != 1) ||
+            (extract8(seeprom->status1, STATUS1_PREL, 1) != 1) ||
+            (extract8(seeprom->status1, STATUS1_FMPC, 1) == 1)) {
+            qemu_log_mask(LOG_GUEST_ERROR, "ignoring Write to MPR\n");
+            break;
+        }
+        data_len = payload->len - data_offset;
+        /* Make sure data is at least 1 Byte */
+        if (data_len <= 0) {
+            qemu_log_mask(LOG_GUEST_ERROR, "Insufficient Data Bytes(%dB),"
+                             " should be at least 1 Byte\n", data_len);
+            break;
+        }
+        xfer_buffer_read_ptr(payload, &read_buf, data_offset, 0x1);
+        if (extract8(seeprom->status1, STATUS1_PABP, 1) == 1) {
+            /* Partition Address Boundaries Protected */
+            seeprom->mpr[extract8(seeprom->addr, 16, 2)] =
+                ((read_buf[0] >> 6) & 0x3);
+        } else {
+            seeprom->mpr[extract8(seeprom->addr, 16, 2)] = read_buf[0];
+        }
+        seeprom->status0 = deposit32(seeprom->status0, STATUS0_WEL, 1, 0);
+        seeprom->status1 = deposit32(seeprom->status1, STATUS1_PREL, 1, 0);
+        break;
+
+    case PPAB:
+        SPI_DEBUG(qemu_log("Protect Partition Address Boundaries PPAB(0x%x)\n",
+                            seeprom->opcode));
+        if ((extract8(seeprom->status0, STATUS0_WEL, 1) != 1) ||
+            (extract8(seeprom->status1, STATUS1_PREL, 1) != 1) ||
+            (extract8(seeprom->status1, STATUS1_FMPC, 1) == 1)) {
+            qemu_log_mask(LOG_GUEST_ERROR, "Ignoring PPAB command\n");
+            break;
+        }
+        data_len = payload->len - data_offset;
+        /* Make sure data is at least 1 Byte */
+        if (data_len <= 0) {
+            qemu_log_mask(LOG_GUEST_ERROR, "Insufficient Data Bytes(%dB),"
+                             " should be at least 1 Byte\n", data_len);
+            break;
+        }
+        xfer_buffer_read_ptr(payload, &read_buf, data_offset, 0x1);
+        if (read_buf[0] == 0xFF) {
+            seeprom->status1 = deposit32(seeprom->status1,
+                                            STATUS1_PABP, 1, 1);
+        } else if (read_buf[0] == 0x0) {
+            seeprom->status1 = deposit32(seeprom->status1,
+                                            STATUS1_PABP, 1, 0);
+        } else {
+            qemu_log_mask(LOG_GUEST_ERROR, "Incorrect Data Byte(0x%x), "
+                            "should be 0x0 or 0xFF\n", read_buf[0]);
+        }
+        seeprom->status0 = deposit32(seeprom->status0, STATUS0_WEL, 1, 0);
+        seeprom->status1 = deposit32(seeprom->status1, STATUS1_PREL, 1, 0);
+        break;
+
+    case FRZR:
+        SPI_DEBUG(qemu_log("Freeze Memory Protection Configuration "
+                                "FRZR(0x%x)\n", seeprom->opcode));
+        if ((extract8(seeprom->status0, STATUS0_WEL, 1) != 1) ||
+            (extract8(seeprom->status1, STATUS1_PREL, 1) != 1) ||
+            (extract8(seeprom->status1, STATUS1_FMPC, 1) == 1)) {
+            qemu_log_mask(LOG_GUEST_ERROR, "ignoring FRZR command\n");
+            break;
+        }
+        data_len = payload->len - data_offset;
+        /* Make sure data is at least 1 Byte */
+        if (data_len <= 0) {
+            qemu_log_mask(LOG_GUEST_ERROR, "Insufficient Data Bytes(%dB),"
+                             " should be at least 1 Byte\n", data_len);
+            break;
+        }
+        xfer_buffer_read_ptr(payload, &read_buf, data_offset, 0x1);
+        if (read_buf[0] == 0xD2) {
+            seeprom->status1 = deposit32(seeprom->status1,
+                                            STATUS1_FMPC, 1, 1);
+        } else {
+            qemu_log_mask(LOG_GUEST_ERROR, "Invalid Confirmation Data "

hw/ppc/pnv_spi_seeprom.c line 569

Quoted 10 lines
hw/ppc/pnv_spi_seeprom.c new file +10 -0
+                            "byte(0x%x), expecting 0xD2", read_buf[0]);
+        }
+        seeprom->status0 = deposit32(seeprom->status0, STATUS0_WEL, 1, 0);
+        seeprom->status1 = deposit32(seeprom->status1, STATUS1_PREL, 1, 0);
+        break;
+
+    case RDEX_CHLK:
+        SPI_DEBUG(qemu_log("OPCODE = (0x%x)\n", seeprom->opcode));
+        data_len = payload->len - data_offset;
+        /* Make sure data is at least 1 Byte */

byte not Byte -- everywhere

hw/ppc/pnv_spi_seeprom.c line 579

Quoted 10 lines
hw/ppc/pnv_spi_seeprom.c new file +10 -0
+        if (data_len <= 0) {
+            qemu_log_mask(LOG_GUEST_ERROR, "Insufficient Data Bytes(%dB),"
+                             " should be at least 1 Byte\n", data_len);
+            break;
+        }
+        buf = xfer_buffer_write_ptr(rsp_payload, data_offset, data_len);
+        if (extract8(seeprom->addr, 10, 1) == 0) {
+            /* RDEX */
+            seeprom->rd_state = ST_SEC_READ;
+            for (idx = 0; idx < data_len; idx++) {

Probably the same and IMO easier to follow:

sidx = seeprom->addr & 0x1ff;
if (sidx <= 0xff)
buf[idx] = seeprom->dsn[sidx]
else
buf[idx] = seeprom->uplid[sidx & 0xff];

hw/ppc/pnv_spi_seeprom.c line 586

Quoted 7 lines
hw/ppc/pnv_spi_seeprom.c new file +7 -0
+                if (extract32(seeprom->addr, 0, 9) <= 0xFF) {
+                    buf[idx] = seeprom->dsn[extract8(seeprom->addr, 0, 8)];
+                } else {
+                    buf[idx] = seeprom->uplid[extract8(seeprom->addr, 0, 8)];
+                }
+                seeprom->addr = deposit32(seeprom->addr, 0, 9,
+                                 ((extract32(seeprom->addr, 0, 9)) + 1));

I wonder what this is doing ...

seeprom->addr = (seeprom->addr & ~0x1ff) | ((seeprom->addr + 1) & 0x1ff)

hw/ppc/pnv_spi_seeprom.c line 592

Quoted 6 lines
hw/ppc/pnv_spi_seeprom.c new file +6 -0
+            }
+            if (last == 1) {
+                seeprom->rd_state = ST_IDLE;
+            } else {
+                /* CHLK */
+                if (seeprom->locked == true) {

if (seeprom->locked)

hw/ppc/pnv_spi_seeprom.c line 603

Quoted 11 lines
hw/ppc/pnv_spi_seeprom.c new file +11 -0
+                    buf[0] = 0x01;
+                } else {
+                    buf[0] = 0x00;
+                }
+            }
+        }
+        break;
+
+    case WREX_LOCK:
+        SPI_DEBUG(qemu_log("OPCODE = (0x%x)\n", seeprom->opcode));
+        if (seeprom->locked == true) {

if (seeprom->locked)

Quoted 8 lines
hw/ppc/pnv_spi_seeprom.c new file +8 -0
+            qemu_log_mask(LOG_GUEST_ERROR, "Device is already Locked, "
+                            "command is ignored\n");
+            break;
+        }
+        if (extract8(seeprom->status0, STATUS0_WEL, 1) != 1) {
+            qemu_log_mask(LOG_GUEST_ERROR, "Device is not Write Enabled, "
+                            "command is ignored\n");
+            break;

hw/ppc/pnv_spi_seeprom.c line 621

Quoted 10 lines
hw/ppc/pnv_spi_seeprom.c new file +10 -0
+        }
+        data_len = payload->len - data_offset;
+        /* Make sure data is at least 1 Byte */
+        if (data_len <= 0) {
+            qemu_log_mask(LOG_GUEST_ERROR, "Insufficient Data Bytes(%dB),"
+                             " should be at least 1 Byte\n", data_len);
+            break;
+        }
+        xfer_buffer_read_ptr(payload, &read_buf, data_offset, data_len);
+        if (extract8(seeprom->addr, 10, 1) == 0) {

Can you add a comment why bit 10 must be 0?

hw/ppc/pnv_spi_seeprom.c line 625

Quoted 4 lines
hw/ppc/pnv_spi_seeprom.c new file +4 -0
+            /* WREX */
+            for (idx = 0; idx < data_len; idx++) {
+                seeprom->uplid[extract8(seeprom->addr, 0, 8)] = read_buf[idx];
+                /* Increase address with the page, and let it rool over */

you mean 'roll' over ? not sure what this means, though.

hw/ppc/pnv_spi_seeprom.c line 627

Quoted 2 lines
hw/ppc/pnv_spi_seeprom.c new file +2 -0
+                seeprom->addr = deposit32(seeprom->addr, 0, 8,
+                                 ((extract32(seeprom->addr, 0, 8)) + 1));

Are you just advancing the addr by '1' but then only modifying the lowest 9 bit and never touching bit 9?

Quoted 21 lines
hw/ppc/pnv_spi_seeprom.c new file +21 -0
+            }
+        } else {
+            /*
+             * LOCK (82h) instruction is clocked in on the  SI line, followed
+             * by a fake address where  bits A[23:0] are don't care bits with
+             * the exception that bit A10 must be set to 1. Finally, a
+             * confirmation data byte of xxxx_xx1xb  is  sent
+             */
+            if ((buf[0] & 0x02) == 0x2) {
+                seeprom->locked = true;
+            }
+        }
+        break;
+
+    default:
+        qemu_log_mask(LOG_GUEST_ERROR, "Invalid instruction(0x%x)\n",
+                        seeprom->opcode);
+    } /* end of switch */
+    return rsp_payload;
+} /* end of seeprom_spi_request */
+

hw/ppc/pnv_spi_seeprom.c line 658

Quoted 10 lines
hw/ppc/pnv_spi_seeprom.c new file +10 -0
+void seeprom_connect_controller(PnvSpiResponder *resp, const char *port)
+{
+    PnvSpiSeeprom *seeprom = PNV_SPI_SEEPROM(resp);
+    seeprom->controller_connected = true;
+}
+
+void seeprom_disconnect_controller(PnvSpiResponder *resp)
+{
+    PnvSpiSeeprom *seeprom = PNV_SPI_SEEPROM(resp);
+    /* This method is invoked when Controller wants to deslect responder */

This method is called when the controller wants to deselect the responder

Quoted 5 lines
hw/ppc/pnv_spi_seeprom.c new file +5 -0
+    seeprom->controller_connected = false;
+    seeprom->rd_state = ST_IDLE; /* Reset Read state */
+    if (seeprom->opcode == WRITE) { /* Reset Write enable */
+        seeprom->status0 = deposit32(seeprom->status0, STATUS0_WEL, 1, 0);
+    }

hw/ppc/pnv_spi_seeprom.c line 673

Quoted 10 lines
hw/ppc/pnv_spi_seeprom.c new file +10 -0
+}
+
+/*
+ * Method : compute_addr
+ * This method is used to compute address and data offset for supported
+ * opcodes and only invoked when a valid new command sequence starts aka
+ * first is 1.
+ */
+bool compute_addr(PnvSpiSeeprom *seeprom, xfer_buffer *req_payload,
+                  xfer_buffer *rsp_payload, int bits, uint32_t  *data_offset)

remove additional space: uint32_t *data_offset

hw/ppc/pnv_spi_seeprom.c line 677

Quoted 4 lines
hw/ppc/pnv_spi_seeprom.c new file +4 -0
+{
+    bool addr_wr_protected = false;
+    uint8_t *read_buf = NULL;
+    *data_offset = 0;

move below variable declarations

Quoted 11 lines
hw/ppc/pnv_spi_seeprom.c new file +11 -0
+    bool failed = false;
+
+    xfer_buffer_read_ptr(req_payload, &read_buf, 1, 3);
+    switch (seeprom->opcode) {
+    case READ:
+    case WRITE:
+        SPI_DEBUG(qemu_log("Compute address and payload buffer data offset "
+                  "for %s\n", (seeprom->opcode == READ) ? "READ" : "WRITE"));
+        /* command size is 4 bytes for READ/WRITE, data_offset is 4 */
+        *data_offset = 4;
+

hw/ppc/pnv_spi_seeprom.c line 698

Quoted 10 lines
hw/ppc/pnv_spi_seeprom.c new file +10 -0
+        /* Make sure buffer length is at least 4 Bytes */
+        if (req_payload->len >= 4) {
+            /*
+             * Fetch address from size 24 bit from offset 1,2,3 of payload
+             * and mask of higher 5 bits as valid memory array size is 512KB
+             */
+            seeprom->addr = deposit32(seeprom->addr, 0, 8, read_buf[2]);
+            seeprom->addr = deposit32(seeprom->addr, 8, 8, read_buf[1]);
+            seeprom->addr = deposit32(seeprom->addr, 16, 8,
+                            (read_buf[0] & 0x7));

seeprom->addr = ((read_buf[0] & 7) << 16) | (read_buf[1] << 8) | read_buf[2];

Quoted 33 lines
hw/ppc/pnv_spi_seeprom.c new file +33 -0
+        } else {
+            qemu_log_mask(LOG_GUEST_ERROR, "Payload_len(0x%x) should be at "
+                          "least 4Bytes to fetch Address\n", req_payload->len);
+            failed = true;
+        }
+        if (seeprom->opcode == WRITE) {
+            addr_wr_protected = validate_addr(seeprom);
+            if (addr_wr_protected) {
+                qemu_log_mask(LOG_GUEST_ERROR, "SEEPROM Address(0x%x) is Write "
+                              "protected\n", seeprom->addr);
+                failed = true;
+            }
+        }
+        break;
+
+    case RDSR:
+    case WRBP:
+    case WRSR:
+    case SPID:
+        /*
+         * command size is 1 bytes for RDSR, WRBP, WRSR, SPID. So data_offset
+         * is 1
+         */
+        *data_offset = 1;
+        break;
+
+    case RMPR:
+    case WMPR:
+        SPI_DEBUG(qemu_log("Compute MPR address for %s MPR\n",
+                  (seeprom->opcode == RMPR) ? "READ" : "WRITE"));
+        /* command size is 4 bytes for WMPR/RMPR, data_offset is 4 */
+        *data_offset = 4;
+

hw/ppc/pnv_spi_seeprom.c line 741

Quoted 10 lines
hw/ppc/pnv_spi_seeprom.c new file +10 -0
+        /* Make sure buffer length is at least 4 Bytes */
+        if (req_payload->len >= 4) {
+            /*
+             * The address for each Memory Partition register is embedded into
+             * the first address byte sent to the device,in bit positions A18
+             * through A16.
+             */
+            seeprom->addr = deposit32(seeprom->addr, 0, 15, 0);
+            seeprom->addr = deposit32(seeprom->addr, 16, 8,
+                                    (read_buf[0] & 0x7));

Imo the following would be better to read and understand:

seeprom->addr = (read_buf[0] & 7) << 16;

Quoted 10 lines
hw/ppc/pnv_spi_seeprom.c new file +10 -0
+        } else {
+            qemu_log_mask(LOG_GUEST_ERROR, "Payload_len(0x%x) should be at "
+                          "least 4Bytes to fetch Address\n", req_payload->len);
+            failed = true;
+        }
+        break;
+
+    case PPAB:
+    case FRZR:
+        SPI_DEBUG(qemu_log("Validate if addr[15:0] is %s\n",

hw/ppc/pnv_spi_seeprom.c line 761

Quoted 10 lines
hw/ppc/pnv_spi_seeprom.c new file +10 -0
+                  (seeprom->opcode == PPAB) ? "0xCCFF for PPAB" :
+                                               "0xAA40 for FRZR"));
+        /* command size is 4 bytes for PPAB/FRZR, data_offset is 4 */
+        *data_offset = 4;
+        /* Make sure buffer length is at least 4 Bytes */
+        if (req_payload->len >= 4) {
+            /* Address bits A23-A16 are ignored. */
+            seeprom->addr = deposit32(seeprom->addr, 0, 8, read_buf[2]);
+            seeprom->addr = deposit32(seeprom->addr, 8, 8, read_buf[1]);
+            seeprom->addr = deposit32(seeprom->addr, 16, 8, 0);

seeprom->addr = (read_buf[1] << 8) | read_buf[2];

Quoted 35 lines
hw/ppc/pnv_spi_seeprom.c new file +35 -0
+        } else {
+            qemu_log_mask(LOG_GUEST_ERROR, "Payload_len(0x%x) should be at "
+                          "least 4Bytes to fetch Address\n", req_payload->len);
+            failed = true;
+            break;
+        }
+        /* Address bits A15-A0 must be set to CC55h. */
+        if ((seeprom->opcode == PPAB) &&
+            (extract32(seeprom->addr, 0, 15) != 0xCC55)) {
+            qemu_log_mask(LOG_GUEST_ERROR, "Invalid addr[15:0] = 0x%x sent for "
+                          "PPAB\n", extract32(seeprom->addr, 0, 15));
+            failed = true;
+        }
+        /* Address bits A15-A0 must be set to AA40h. */
+        if ((seeprom->opcode == FRZR) &&
+            (extract32(seeprom->addr, 0, 15) != 0xAA40)) {
+            qemu_log_mask(LOG_GUEST_ERROR, "Invalid addr[15:0] = 0x%x sent for "
+                          "FRZR\n", extract32(seeprom->addr, 0, 15));
+            failed = true;
+        }
+        break;
+
+    case RDEX_CHLK:
+    case WREX_LOCK:
+        SPI_DEBUG(qemu_log("Compute Address for Security reg command\n"));
+        /* command size is 4 bytes for PPAB/FRZR, data_offset is 4 */
+        *data_offset = 4;
+
+        /* Make sure buffer length is at least 4 Bytes */
+        if (req_payload->len >= 4) {
+            /*
+             * RDEX : A[23:9] are don't care bits, except A10 which must be a
+             *        logic 0.
+             * WREX : A[23:9] are don't care bits, except A10 which must be a
+             *        logic 0 and A8 which must be a logic 1 to address the

hw/ppc/pnv_spi_seeprom.c line 806

Quoted 10 lines
hw/ppc/pnv_spi_seeprom.c new file +10 -0
+             *        second Security register byte that is user programmable.
+             * CHLK : A[23:0] are don't care bits, except A10 which must be a
+             *        logic 1.
+             * LOCK : A[23:0] are don't care bits, except A10 which must be a
+             *        logic 1.
+             */
+            seeprom->addr = deposit32(seeprom->addr, 0, 8, read_buf[2]);
+            seeprom->addr = deposit32(seeprom->addr, 8, 8,
+                                    (read_buf[1] & 0x05));
+            seeprom->addr = deposit32(seeprom->addr, 16, 8, 0);

seeprom->addr = ((read_buf[1] & 0x5) << 8) | read_buf[2];

Quoted 60 lines
hw/ppc/pnv_spi_seeprom.c new file +60 -0
+            SPI_DEBUG(qemu_log("Received Command %s\n",
+                      (seeprom->opcode == RDEX_CHLK)
+                       ? (extract32(seeprom->addr, 10, 1) ?
+                          "CHLK : Check Lock Status of Security Register" :
+                          "RDEX : Read from the Security Register")
+                       : (extract32(seeprom->addr, 10, 1) ?
+                          "LOCK : Lock the Security Register (permanent)" :
+                          "WREX : Write to the Security Register")));
+        } else {
+            qemu_log_mask(LOG_GUEST_ERROR, "Payload_len(0x%x) should be at "
+                          "least 4Bytes to fetch Address\n", req_payload->len);
+            failed = true;
+        }
+
+        if ((seeprom->opcode == WREX_LOCK) &&
+            (extract32(seeprom->addr, 10, 1) == 0)) {
+            /*
+             * WREX
+             * In Legacy Write Protection mode, the Security register is
+             * write-protected when the BP <1:0> bits (bits 3-2 byte0) of
+             * the STATUS register = 11.
+             */
+            if (extract8(seeprom->status1, STATUS1_WPM, 1) == 0) {
+                addr_wr_protected = validate_addr(seeprom);
+            } else {
+                if (extract32(seeprom->addr, 0, 9) <= 0xFF) {
+                    addr_wr_protected = true;
+                }
+            }
+            if (addr_wr_protected) {
+                qemu_log_mask(LOG_GUEST_ERROR, "SEEPROM Address(0x%x) is "
+                              "Write protected\n", seeprom->addr);
+                failed = true;
+            }
+        }
+        break;
+    } /* end of switch */
+    return failed;
+} /* end of method compute_addr */
+
+/*
+ * Method : validate_addr
+ * This method validates whether SEEPROM address is write protected or not
+ */
+
+bool validate_addr(PnvSpiSeeprom *seeprom)
+{
+    bool addr_wr_protected = false;
+    uint8_t mpr_idx = 0;
+
+    if (extract8(seeprom->status1, STATUS1_WPM, 1) == 1) {
+        /*
+         * enhanced write protection
+         * Memory partition register Bit5  through  bit0 contain the Partition
+         * Endpoint Address of A18:A13, where A12:A0 are a logic "1". For
+         * example, if the first partition of the memory array is desired to
+         * stop after 128-Kbit of memory, that end point address is 03FFFh. The
+         * corresponding A18:A13 address bits to be loaded into MPR0 are
+         * therefore 000001b. The eight MPRs are each decoded sequentially by
+         * the device, starting with MPR0. Each MPR should be set to a

hw/ppc/pnv_spi_seeprom.c line 876

Quoted 10 lines
hw/ppc/pnv_spi_seeprom.c new file +10 -0
+         * Partition Endpoint Address greater than the ending address of the
+         * previous MPR. If a higher order MPR sets a Partition Endpoint Address
+         * less than or equal to the ending address of a lower order MPR, that
+         * higher order MPR is ignored and no protection is set by it's
+         * contents.
+         */
+        for (mpr_idx = 0; mpr_idx < 8; mpr_idx++) {
+            if ((extract32(seeprom->addr, 13, 6)) <=
+                (extract8(seeprom->mpr[mpr_idx], MPR_PEA, 1))) {
+                switch (extract8(seeprom->mpr[mpr_idx], MPR_PB, 1)) {

Don't you have to extract 2 bits for the case values 0..3 below?

Quoted 25 lines
hw/ppc/pnv_spi_seeprom.c new file +25 -0
+                case 0:
+                    /*
+                     * 0b00 = Partition is open and writing is permitted
+                     * (factory default is unprotected).
+                     */
+                    addr_wr_protected = false;
+                    break;
+                case 1:
+                    /*
+                     * 0b01 = Partition is always write-protected but can be
+                     * reversed at a later time (software write-protected).
+                     */
+                    addr_wr_protected = true;
+                    break;
+                case 2:
+                    /*
+                     * 0b10 = Partition is write-protected only when WP pin is
+                     * asserted (hardware write-protected).
+                     */
+                    addr_wr_protected = false;
+                    break;
+                case 3:
+                    /*
+                     * 0b11 = Partition is software write-protected and Memory
+                     * Partition register is permanently locked.

hw/ppc/pnv_spi_seeprom.c line 911

Quoted 10 lines
hw/ppc/pnv_spi_seeprom.c new file +10 -0
+                     */
+                    addr_wr_protected = true;
+                    break;
+                } /* end of switch */
+                break; /* break from for loop. */
+            }
+        } /* end of for loop */
+    } else {
+        /* Legacy write protection mode */
+        switch (extract8(seeprom->status0, STATUS0_BP, 2)) {

2 bits extracted -- good

Quoted 90 lines
hw/ppc/pnv_spi_seeprom.c new file +78 -0
+        case 0:
+            /*
+             * 0b00 = No array write protection
+             * EEPROM None
+             * Security Register 00000h - 000FFh
+             */
+            if ((seeprom->opcode == WREX_LOCK) &&
+                (extract32(seeprom->addr, 0, 9)  <= 0xFF)) {
+                addr_wr_protected = true;
+            }
+            break;
+        case 1:
+            /*
+             * 0b01 = Upper quarter memory array protection
+             * EEPROM 60000h - 7FFFFh
+             * Security Register 00000h - 000FFh
+             */
+            if ((seeprom->opcode == WREX_LOCK) &&
+                (extract32(seeprom->addr, 0, 9)  <= 0xFF)) {
+                addr_wr_protected = true;
+            } else if ((seeprom->opcode == WRITE) &&
+                       (extract32(seeprom->addr, 0, 19)  <= 0x60000)) {
+                addr_wr_protected = true;
+            }
+            break;
+        case 2:
+            /*
+             * 0b10 = Upper half memory array protection
+             * EEPROM 40000h - 7FFFFh
+             * Security Register 00000h - 000FFh
+             */
+            if ((seeprom->opcode == WREX_LOCK) &&
+                (extract32(seeprom->addr, 0, 9)  <= 0xFF)) {
+                addr_wr_protected = true;
+            } else if ((seeprom->opcode == WRITE) &&
+                       (extract32(seeprom->addr, 0, 19)  <= 0x40000)) {
+                addr_wr_protected = true;
+            }
+            break;
+        case 3:
+            /*
+             * 0b11 = Entire memory array protection
+             * EEPROM 00000h - 7FFFFh
+             * Security Register 00000h - 001FFh
+             */
+            addr_wr_protected = true;
+            break;
+        } /* end of switch */
+    }
+    return addr_wr_protected;
+} /* end of validate_addr */
+
+static void pnv_spi_seeprom_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+    PnvSpiResponderClass *resp_class = PNV_SPI_RESPONDER_CLASS(klass);
+
+    resp_class->connect_controller = seeprom_connect_controller;
+    resp_class->disconnect_controller = seeprom_disconnect_controller;
+    resp_class->request = seeprom_spi_request;
+
+    dc->desc = "PowerNV SPI SEEPROM";
+    dc->bus_type = TYPE_SPI_BUS;
+}
+
+static const TypeInfo pnv_spi_seeprom_info = {
+    .name          = TYPE_PNV_SPI_SEEPROM,
+    .parent        = TYPE_PNV_SPI_RESPONDER,
+    .instance_size = sizeof(PnvSpiSeeprom),
+    .class_init    = pnv_spi_seeprom_class_init,
+};
+
+static void pnv_spi_seeprom_register_types(void)
+{
+    type_register_static(&pnv_spi_seeprom_info);
+}
+
+type_init(pnv_spi_seeprom_register_types);
hw/ppc/meson.build +1 -0
index de25cac763..2c1ef0c937 100644
@@ -55,6 +55,7 @@ ppc_ss.add(when: 'CONFIG_POWERNV', if_true: files(
    'pnv_pnor.c',
    'pnv_spi_responder.c',
    'pnv_spi_controller.c',
+  'pnv_spi_seeprom.c',
  ))
  # PowerPC 4xx boards
  ppc_ss.add(when: 'CONFIG_PPC405', if_true: files(
#11 lore raw

PATCH 5/5 hw/ppc: SPI controller wiring to P10 chip and create seeprom device

In reply to Chalapathi V

This commit creates SPI controller to p10 chip and create the keystore seeprom device on spi_bus2 of PIB_SPIC[2].

---
 include/hw/ppc/pnv_chip.h |  4 ++++
 hw/ppc/pnv.c              | 32 ++++++++++++++++++++++++++++++++
 2 files changed, 36 insertions(+)
include/hw/ppc/pnv_chip.h +4 -0
index 0ab5c42308..0d67516ede 100644
@@ -4,6 +4,8 @@
 #include "hw/pci-host/pnv_phb4.h"
 #include "hw/ppc/pnv_core.h"
 #include "hw/ppc/pnv_homer.h"
+#include "hw/ppc/pnv_spi_controller.h"
+#include "hw/ppc/pnv_spi_seeprom.h"
 #include "hw/ppc/pnv_lpc.h"
 #include "hw/ppc/pnv_occ.h"
 #include "hw/ppc/pnv_psi.h"
@@ -113,6 +115,8 @@ struct Pnv10Chip {
     PnvOCC       occ;
     PnvSBE       sbe;
     PnvHomer     homer;
+#define PNV10_CHIP_MAX_PIB_SPIC 6
+    PnvSpiController pib_spic[PNV10_CHIP_MAX_PIB_SPIC];
 
     uint32_t     nr_quads;
     PnvQuad      *quads;
hw/ppc/pnv.c +32 -0
index 0297871bdd..8fffd999f6 100644
@@ -1691,6 +1691,11 @@ static void pnv_chip_power10_instance_init(Object *obj)
     for (i = 0; i < pcc->i2c_num_engines; i++) {
         object_initialize_child(obj, "i2c[*]", &chip10->i2c[i], TYPE_PNV_I2C);
     }
+
+    for (i = 0; i < PNV10_CHIP_MAX_PIB_SPIC ; i++) {
+        object_initialize_child(obj, "pib_spic[*]", &chip10->pib_spic[i],
+                                TYPE_PNV_SPI_CONTROLLER);
+    }
 }
 
 static void pnv_chip_power10_quad_realize(Pnv10Chip *chip10, Error **errp)
@@ -1879,6 +1884,33 @@ static void pnv_chip_power10_realize(DeviceState *dev, Error **errp)
                               qdev_get_gpio_in(DEVICE(&chip10->psi),
                                                PSIHB9_IRQ_SBE_I2C));
     }
+
+    /* PIB SPI Controller */
+    for (i = 0; i < PNV10_CHIP_MAX_PIB_SPIC; i++) {
+        object_property_set_int(OBJECT(&chip10->pib_spic[i]), "spic_num",
+                                i , &error_fatal);
+        /*
+         * The TPM attached SPIC needs to reverse the bit order in each byte
+         * it sends to the TPM.
+         */
+        if (i == 4) {
+            object_property_set_bool(OBJECT(&chip10->pib_spic[i]),
+                    "reverse_bits", true, &error_fatal);
+        }
+        if (!qdev_realize(DEVICE(&chip10->pib_spic[i]), NULL, errp)) {
+            return;
+        }
+        pnv_xscom_add_subregion(chip, PNV10_XSCOM_PIB_SPIC_BASE +
+                                i * PNV10_XSCOM_PIB_SPIC_SIZE,
+                                &chip10->pib_spic[i].xscom_spic_regs);
+    }
+
+    /* Primary MEAS/MVPD/Keystore SEEPROM connected to pib_spic[2]*/
+    PnvSpiSeeprom *meas_mvpd_ks_p = PNV_SPI_SEEPROM(spi_create_responder(
+                            (&chip10->pib_spic[2])->spi_bus,
+                            TYPE_PNV_SPI_SEEPROM));
+    meas_mvpd_ks_p->file = qemu_find_file(QEMU_FILE_TYPE_BIOS,
+                    "sbe_measurement_seeprom.bin.ecc");
 }
 
 static uint32_t pnv_chip_power10_xscom_pcba(PnvChip *chip, uint64_t addr)
Signature · 1 line
2.31.1
#12 lore raw
In reply to Chalapathi V

Hello,

I would greatly appreciate the review comments/suggestions on PATCH V1.

Thank You and Regards,

Chalapathi

Quoted 67 lines · Chalapathi V

Hello,

In this series of patchset, SPI controller and responder models for Power10 processor are modelled.

Serial peripheral interface provides full-duplex synchronous serial communication between single controller and multiple responder devices.

The current configuration supports a single SPI controller reside on the SPI bus. In p10, SPI controller device model supports a connection to a single SPI responder such as SPI seeproms, TPM, flash device and an ADC controller.

SPI controller model is divided into configuration unit, sequencer FSM and shifter engine. All SPI function control is mapped into the SPI register space to enable full control by firmware.

SPI configuration component is modelled which contains all SPI configuration and status registers as well as the hold registers for data to be sent or having been received.
Shift engine performs serialization and de-serialization according to the control by the sequencer and according to the setup defined in the configuration registers.
Sequencer implements the main control logic and
FSM to handle data transmit and data receive control of the shift engine.

Microchip's 25CSM04 SEEPROM device is modelled and connected to SPI bus "spi_bus2" of SPI controller "PIB_SPIC[2]".

Patches overview in V1.
PATCH1: Create a SPI responder model which includes responder methods
and SPI bus implementation.
PATCH2: Create a SPI controller model and implement configuration unit
to model SCOM registers.
PATCH3: SPI controller model: implement sequencer FSM and shift engine.
PATCH4: create SPI SEEPROM model.
PATCH5: Connect SPI controllers to p10 chip and create keystore seeprom
device on spi_bus2 of PIB_SPIC[2].

Thank You,
Chalapathi

Chalapathi V (5):
hw/ppc: SPI responder model
hw/ppc: SPI controller model - registers implementation hw/ppc: SPI controller model - sequencer and shifter hw/ppc: SPI SEEPROM model
hw/ppc: SPI controller wiring to P10 chip and create seeprom device

  include/hw/ppc/pnv_chip.h           |    4 +
  include/hw/ppc/pnv_spi_controller.h |  101 ++
  include/hw/ppc/pnv_spi_responder.h  |  109 ++
  include/hw/ppc/pnv_spi_seeprom.h    |   70 ++
  include/hw/ppc/pnv_xscom.h          |    3 +
  hw/ppc/pnv.c                        |   32 +
  hw/ppc/pnv_spi_controller.c         | 1609 +++++++++++++++++++++++++++
  hw/ppc/pnv_spi_responder.c          |  166 +++
  hw/ppc/pnv_spi_seeprom.c            |  989 ++++++++++++++++
  hw/ppc/meson.build                  |    3 +
  10 files changed, 3086 insertions(+)

create mode 100644 include/hw/ppc/pnv_spi_controller.h create mode 100644 include/hw/ppc/pnv_spi_responder.h create mode 100644 include/hw/ppc/pnv_spi_seeprom.h create mode 100644 hw/ppc/pnv_spi_controller.c create mode 100644 hw/ppc/pnv_spi_responder.c create mode 100644 hw/ppc/pnv_spi_seeprom.c

#13 lore raw

Chalapathi,

Quoted 5 lines · Chalapathi V

Hello,

I would greatly appreciate the review comments/suggestions on PATCH V1.

Thank You and Regards,

I didn't forget but I lacked the time in this release cycle. Sorry about that.

I have one quick comment though. There are already a few models implementing SPI controllers in QEMU. When I skimmed through the patches, I was surprised to see no use of the available SSI framework. Doesn't the current framework fit your needs ?

Please take a look at files in : include/hw/ssi/* hw/ssi/*

Same comment for the Serial EEPROM model. This device is generic and not POWER specific. It should be possible to attach the device model on other machines and different SPI bus provided by QEMU. This doesn't mean rewriting everything, but the "HW" interface probably needs to be reworked. It would make it easier to write unit test (see ests/qtest/) and ease the review also.

Thanks,

C.

Quoted 71 lines

Chalapathi

Quoted 67 lines · Chalapathi V

Hello,

In this series of patchset, SPI controller and responder models for Power10 processor are modelled.

Serial peripheral interface provides full-duplex synchronous serial communication between single controller and multiple responder devices.

The current configuration supports a single SPI controller reside on the SPI bus. In p10, SPI controller device model supports a connection to a single SPI responder such as SPI seeproms, TPM, flash device and an ADC controller.

SPI controller model is divided into configuration unit, sequencer FSM and shifter engine. All SPI function control is mapped into the SPI register space to enable full control by firmware.

SPI configuration component is modelled which contains all SPI configuration and status registers as well as the hold registers for data to be sent or having been received.
Shift engine performs serialization and de-serialization according to the control by the sequencer and according to the setup defined in the configuration registers.
Sequencer implements the main control logic and
FSM to handle data transmit and data receive control of the shift engine.

Microchip's 25CSM04 SEEPROM device is modelled and connected to SPI bus "spi_bus2" of SPI controller "PIB_SPIC[2]".

Patches overview in V1.
PATCH1: Create a SPI responder model which includes responder methods and SPI bus implementation.
PATCH2: Create a SPI controller model and implement configuration unit to model SCOM registers.
PATCH3: SPI controller model: implement sequencer FSM and shift engine.
PATCH4: create SPI SEEPROM model.
PATCH5: Connect SPI controllers to p10 chip and create keystore seeprom device on spi_bus2 of PIB_SPIC[2].

Thank You,
Chalapathi

Chalapathi V (5):
   hw/ppc: SPI responder model
   hw/ppc: SPI controller model - registers implementation hw/ppc: SPI controller model - sequencer and shifter hw/ppc: SPI SEEPROM model
   hw/ppc: SPI controller wiring to P10 chip and create seeprom device

  include/hw/ppc/pnv_chip.h           |    4 + include/hw/ppc/pnv_spi_controller.h |  101 ++ include/hw/ppc/pnv_spi_responder.h  |  109 ++ include/hw/ppc/pnv_spi_seeprom.h    |   70 ++ include/hw/ppc/pnv_xscom.h          |    3 +
  hw/ppc/pnv.c                        |   32 + hw/ppc/pnv_spi_controller.c         | 1609 +++++++++++++++++++++++++++ hw/ppc/pnv_spi_responder.c          |  166 +++ hw/ppc/pnv_spi_seeprom.c            |  989 ++++++++++++++++ hw/ppc/meson.build                  |    3 +

  10 files changed, 3086 insertions(+)

  create mode 100644 include/hw/ppc/pnv_spi_controller.h create mode 100644 include/hw/ppc/pnv_spi_responder.h create mode 100644 include/hw/ppc/pnv_spi_seeprom.h create mode 100644 hw/ppc/pnv_spi_controller.c create mode 100644 hw/ppc/pnv_spi_responder.c create mode 100644 hw/ppc/pnv_spi_seeprom.c

#14 lore raw
Quoted 26 lines · Cédric Le Goater

Chalapathi,

Quoted 5 lines · Chalapathi V

Hello,

I would greatly appreciate the review comments/suggestions on PATCH V1.

Thank You and Regards,

I didn't forget but I lacked the time in this release cycle. Sorry about that.

I have one quick comment though. There are already a few models implementing SPI controllers in QEMU. When I skimmed through the patches, I was surprised to see no use of the available SSI framework. Doesn't the current framework fit your needs ?

Please take a look at files in : include/hw/ssi/* hw/ssi/*

Same comment for the Serial EEPROM model. This device is generic and not POWER specific. It should be possible to attach the device model on other machines and different SPI bus provided by QEMU. This doesn't mean rewriting everything, but the "HW" interface probably needs to be reworked. It would make it easier to write unit test (see ests/qtest/) and ease the review also.

Quoted 4 lines

Thanks,

C.

Hello Cedric,

Thank You so much for the suggestions. Current implementation of SPI_controller and Serial EEPROM device using xfer_buffer payload allows me to use the *SSI_BUS* and *ssi_create_peripheral(). *I will modify the spi_controller and Serial EEPROM models to use *ssi_transfer()* and transfer control using CS line and update in PATCH V2.

Thank You,

Chalapathi

Quoted 75 lines
Quoted 74 lines

Chalapathi

Quoted 68 lines · Chalapathi V

Hello,

In this series of patchset, SPI controller and responder models for Power10 processor are modelled.

Serial peripheral interface provides full-duplex synchronous serial communication between single controller and multiple responder devices.

The current configuration supports a single SPI controller reside on the SPI bus. In p10, SPI controller device model supports a connection to a single SPI responder such as SPI seeproms, TPM, flash device and an ADC controller.

SPI controller model is divided into configuration unit, sequencer FSM and shifter engine. All SPI function control is mapped into the SPI register space to enable full control by firmware.

SPI configuration component is modelled which contains all SPI configuration and status registers as well as the hold registers for data to be sent or having been received.
Shift engine performs serialization and de-serialization according to the control by the sequencer and according to the setup defined in the configuration registers.
Sequencer implements the main control logic and
FSM to handle data transmit and data receive control of the shift engine.

Microchip's 25CSM04 SEEPROM device is modelled and connected to SPI bus "spi_bus2" of SPI controller "PIB_SPIC[2]".

Patches overview in V1.
PATCH1: Create a SPI responder model which includes responder methods and SPI bus implementation.
PATCH2: Create a SPI controller model and implement configuration unit to model SCOM registers.
PATCH3: SPI controller model: implement sequencer FSM and shift engine.
PATCH4: create SPI SEEPROM model.
PATCH5: Connect SPI controllers to p10 chip and create keystore seeprom device on spi_bus2 of PIB_SPIC[2].

Thank You,
Chalapathi

Chalapathi V (5):
   hw/ppc: SPI responder model
   hw/ppc: SPI controller model - registers implementation hw/ppc: SPI controller model - sequencer and shifter hw/ppc: SPI SEEPROM model
   hw/ppc: SPI controller wiring to P10 chip and create seeprom device

  include/hw/ppc/pnv_chip.h           |    4 + include/hw/ppc/pnv_spi_controller.h |  101 ++ include/hw/ppc/pnv_spi_responder.h  |  109 ++ include/hw/ppc/pnv_spi_seeprom.h    |   70 ++ include/hw/ppc/pnv_xscom.h          |    3 + hw/ppc/pnv.c                        |   32 + hw/ppc/pnv_spi_controller.c         | 1609
+++++++++++++++++++++++++++
  hw/ppc/pnv_spi_responder.c          |  166 +++ hw/ppc/pnv_spi_seeprom.c            |  989 ++++++++++++++++ hw/ppc/meson.build                  |    3 +

  10 files changed, 3086 insertions(+)

  create mode 100644 include/hw/ppc/pnv_spi_controller.h create mode 100644 include/hw/ppc/pnv_spi_responder.h create mode 100644 include/hw/ppc/pnv_spi_seeprom.h create mode 100644 hw/ppc/pnv_spi_controller.c create mode 100644 hw/ppc/pnv_spi_responder.c create mode 100644 hw/ppc/pnv_spi_seeprom.c