Skip to contentSkip to conversation

hw/ppc: SPI model

PATCH Superseded Chalapathi V 2 participants 16 messages May 15, 2024 – Jun 14, 2024 Arm/Amlogic Meson SoC support

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v3 cover hw/ppc: SPI model

Hello,

Thank You so much for reviewing patch v2.
In patch v3, most of Cedric's comments are addressed.
- PnvSPIBus model is removed and added SSIBus to PnvSpiController.
- Added trace-events and removed SPI_DEBUG macro.
- Added Microchip's 25csm04 device on top of m25p80 and create seeprom device from a command line.
- Use qdev_connect_gpio_out_named instead of sysbus_connect_irq to connect cs.

The new qom-tree looks like below.
(qemu) info qom-tree
/machine (powernv10-machine)
/chip[0] (power10_v2.0-pnv-chip)

    /pib_spic[0] (pnv-spi-controller)
      /pnv-spi-bus.0 (SSI)
      /xscom-spi-controller-regs[0] (memory-region)
    /pib_spic[1] (pnv-spi-controller)
      /pnv-spi-bus.1 (SSI)
      /xscom-spi-controller-regs[0] (memory-region)
    /pib_spic[2] (pnv-spi-controller)
      /pnv-spi-bus.2 (SSI)
      /xscom-spi-controller-regs[0] (memory-region)
    /pib_spic[3] (pnv-spi-controller)
      /pnv-spi-bus.3 (SSI)
      /xscom-spi-controller-regs[0] (memory-region)
    /pib_spic[4] (pnv-spi-controller)
      /pnv-spi-bus.4 (SSI)
      /xscom-spi-controller-regs[0] (memory-region)
    /pib_spic[5] (pnv-spi-controller)
      /pnv-spi-bus.5 (SSI)
      /xscom-spi-controller-regs[0] (memory-region)

(qemu) info qom-tree
/machine (powernv10-machine)
/peripheral-anon (container)

    /device[0] (25csm04)
      /WP#[0] (irq)
      /ssi-gpio-cs[0] (irq)

Patches overview in v3.
PATCH1: Create a SPI controller model and implement configuration unit
to model SCOM registers. Move header files to include/hw/ssi/
PATCH2: SPI controller model: implement sequencer FSM and shift engine.
PATCH3: Add Microchip's SEEPROM 25csm04 model on top of m25p80.
PATCH4: Connect SPI controllers to p10 chip and connect cs lines.
PATCH5: Write a qtest pnv-spi-seeprom-test to check the SPI transactions
between spi controller and seeprom device.

Test covered:
Ran make check.

Thank You,
Chalapathi

Chalapathi V (5):
ppc/pnv: Add SPI controller model
ppc/pnv: Extend SPI model
hw/block: Add Microchip's 25CSM04 to m25p80
hw/ppc: SPI controller wiring to P10 chip
tests/qtest: Add pnv-spi-seeprom qtest

 include/hw/ppc/pnv_chip.h          |    3 +
 include/hw/ppc/pnv_xscom.h         |    3 +
 include/hw/ssi/pnv_spi.h           |   72 ++
 include/hw/ssi/pnv_spi_regs.h      |  114 +++
 hw/block/m25p80.c                  |    3 +
 hw/ppc/pnv.c                       |   21 +-
 hw/ppc/pnv_spi_controller.c        | 1310 ++++++++++++++++++++++++++++
 tests/qtest/pnv-spi-seeprom-test.c |  129 +++
 hw/ppc/Kconfig                     |    2 +
 hw/ppc/meson.build                 |    1 +
 hw/ppc/trace-events                |   21 +
 tests/qtest/meson.build            |    1 +
 12 files changed, 1679 insertions(+), 1 deletion(-)
 create mode 100644 include/hw/ssi/pnv_spi.h
 create mode 100644 include/hw/ssi/pnv_spi_regs.h
 create mode 100644 hw/ppc/pnv_spi_controller.c
 create mode 100644 tests/qtest/pnv-spi-seeprom-test.c
Signature · 1 line
2.39.3
#1 lore raw

PATCH v3 1/5 ppc/pnv: Add SPI controller model

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.

An existing QEMU SSI framework is used and SSI_BUS is created.

---
 include/hw/ppc/pnv_xscom.h    |   3 +
 include/hw/ssi/pnv_spi.h      |  44 +++++++
 include/hw/ssi/pnv_spi_regs.h | 114 +++++++++++++++++
 hw/ppc/pnv_spi_controller.c   | 228 ++++++++++++++++++++++++++++++++++
 hw/ppc/Kconfig                |   1 +
 hw/ppc/meson.build            |   1 +
 hw/ppc/trace-events           |   6 +
 7 files changed, 397 insertions(+)
 create mode 100644 include/hw/ssi/pnv_spi.h
 create mode 100644 include/hw/ssi/pnv_spi_regs.h
 create mode 100644 hw/ppc/pnv_spi_controller.c
include/hw/ppc/pnv_xscom.h +3 -0
index 6209e18492..a77b97f9b1 100644
@@ -194,6 +194,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,
include/hw/ssi/pnv_spi.h new file +44 -0

44 lines not on this page · raw message

include/hw/ssi/pnv_spi_regs.h new file +114 -0

114 lines not on this page · raw message

hw/ppc/pnv_spi_controller.c new file +228 -0

228 lines not on this page · raw message

hw/ppc/Kconfig +1 -0
index 78f83e78ce..6f9670b377 100644
@@ -39,6 +39,7 @@ config POWERNV
     select PCI_POWERNV
     select PCA9552
     select PCA9554
+    select SSI
 
 config PPC405
     bool
hw/ppc/meson.build +1 -0
index d096636ee7..68fadbae7b 100644
@@ -56,6 +56,7 @@ ppc_ss.add(when: 'CONFIG_POWERNV', if_true: files(
   'pnv_pnor.c',
   'pnv_nest_pervasive.c',
   'pnv_n1_chiplet.c',
+  'pnv_spi_controller.c',
 ))
 # PowerPC 4xx boards
 ppc_ss.add(when: 'CONFIG_PPC405', if_true: files(
hw/ppc/trace-events +6 -0
index bf29bbfd4b..b8e494ffc5 100644
@@ -110,6 +110,12 @@ pnv_sbe_cmd_timer_stop(void) ""
 pnv_sbe_cmd_timer_expired(void) ""
 pnv_sbe_msg_recv(uint16_t cmd, uint16_t seq, uint16_t ctrl_flags) "cmd 0x%" PRIx16 " seq %"PRIu16 " ctrl_flags 0x%" PRIx16
 
+#pnv_spi_controller.c
+pnv_spi_read(uint64_t addr, uint64_t val) "addr 0x%" PRIx64 " val 0x%" PRIx64
+pnv_spi_write(uint64_t addr, uint64_t val) "addr 0x%" PRIx64 " val 0x%" PRIx64
+pnv_spi_read_RDR(uint64_t val) "data extracted = 0x%" PRIx64
+pnv_spi_write_TDR(uint64_t val) "being written, data written = 0x%" PRIx64
+
 # ppc.c
 ppc_tb_adjust(uint64_t offs1, uint64_t offs2, int64_t diff, int64_t seconds) "adjusted from 0x%"PRIx64" to 0x%"PRIx64", diff %"PRId64" (%"PRId64"s)"
 ppc_tb_load(uint64_t tb) "tb 0x%016" PRIx64
Signature · 1 line
2.39.3
#2 lore raw

Re: [PATCH v3 1/5] ppc/pnv: Add SPI controller model

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 8 lines

An existing QEMU SSI framework is used and SSI_BUS is created.

---
  include/hw/ppc/pnv_xscom.h    |   3 +
  include/hw/ssi/pnv_spi.h      |  44 +++++++
  include/hw/ssi/pnv_spi_regs.h | 114 +++++++++++++++++
  hw/ppc/pnv_spi_controller.c   | 228 ++++++++++++++++++++++++++++++++++

The file names are not consistent.

Please rename hw/ppc/pnv_spi_controller.c to /hw/ssi/pnv_spi.c.

Quoted 37 lines
  hw/ppc/Kconfig                |   1 +
  hw/ppc/meson.build            |   1 +
  hw/ppc/trace-events           |   6 +
  7 files changed, 397 insertions(+)

create mode 100644 include/hw/ssi/pnv_spi.h create mode 100644 include/hw/ssi/pnv_spi_regs.h create mode 100644 hw/ppc/pnv_spi_controller.c

include/hw/ppc/pnv_xscom.h +3 -0
index 6209e18492..a77b97f9b1 100644
@@ -194,6 +194,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,
include/hw/ssi/pnv_spi.h new file +9 -0
new file mode 100644
index 0000000000..244ee1cfc0
@@ -0,0 +1,44 @@
+/*
+ * 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

include/hw/ssi/pnv_spi.h line 19

Quoted 10 lines
include/hw/ssi/pnv_spi.h new file +10 -0
+ * and an ADC controller.
+ */
+#include "hw/ssi/ssi.h"
+
+#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)

OBJECT_DECLARE_SIMPLE_TYPE(PnvSpiController, PNV_SPI_CONTROLLER)

which means PNV_SPICONTROLLER -> PNV_SPI_CONTROLLER

include/hw/ssi/pnv_spi.h line 21

Quoted 2 lines
include/hw/ssi/pnv_spi.h new file +2 -0
+
+#define SPI_CONTROLLER_REG_SIZE 8

please add a PNV_ prefix : PNV_SPI_CONTROLLER_REG_SIZE

Quoted 11 lines
include/hw/ssi/pnv_spi.h new file +11 -0
+
+#define TYPE_PNV_SPI_BUS "pnv-spi-bus"
+typedef struct PnvSpiController {
+    SysBusDevice parent_obj;
+
+    SSIBus *ssi_bus;
+    qemu_irq *cs_line;
+    MemoryRegion    xscom_spic_regs;
+    /* SPI controller object number */
+    uint32_t        spic_num;
+

include/hw/ssi/pnv_spi.h line 42

Quoted 10 lines
include/hw/ssi/pnv_spi.h new file +10 -0
+    /* 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;

I don't think the _reg suffix are needed. This is minor.

Quoted 8 lines
include/hw/ssi/pnv_spi.h new file +2 -0
+} PnvSpiController;
+#endif /* PPC_PNV_SPI_CONTROLLER_H */
include/hw/ssi/pnv_spi_regs.h new file +0 -0
new file mode 100644
index 0000000000..6f613aca5e

include/hw/ssi/pnv_spi_regs.h line 10

Quoted 10 lines
include/hw/ssi/pnv_spi_regs.h new file +10 -0
@@ -0,0 +1,114 @@
+/*
+ * QEMU PowerPC SPI Controller model
+ *
+ * Copyright (c) 2023, IBM Corporation.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef SPI_CONTROLLER_REGS_H
+#define SPI_CONTROLLER_REGS_H

please add a PNV_ prefix

Quoted 118 lines
include/hw/ssi/pnv_spi_regs.h new file +104 -0
+
+/* 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.
+ */
+#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
+
+#endif
hw/ppc/pnv_spi_controller.c new file +8 -0
new file mode 100644
index 0000000000..11b119cf0f
@@ -0,0 +1,228 @@
+/*
+ * QEMU PowerPC SPI Controller model
+ *
+ * Copyright (c) 2024, IBM Corporation.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+

hw/ppc/pnv_spi_controller.c line 18

Quoted 10 lines
hw/ppc/pnv_spi_controller.c new file +10 -0
+#include "qemu/osdep.h"
+#include "qemu/log.h"
+#include "hw/qdev-properties.h"
+#include "hw/ppc/pnv_xscom.h"
+#include "hw/ssi/pnv_spi.h"
+#include "hw/ssi/pnv_spi_regs.h"
+#include "hw/ssi/ssi.h"
+#include "hw/ppc/fdt.h"
+#include <libfdt.h>
+#include <math.h>

why math.h ?

Quoted 36 lines
hw/ppc/pnv_spi_controller.c new file +36 -0
+#include "hw/irq.h"
+#include "trace.h"
+
+static uint64_t pnv_spi_controller_read(void *opaque, hwaddr addr,
+                                 unsigned size)
+{
+    PnvSpiController *s = PNV_SPICONTROLLER(opaque);
+    uint32_t reg = addr >> 3;
+    uint64_t val = ~0ull;
+
+    switch (reg) {
+    case ERROR_REG:
+        val = s->error_reg;
+        break;
+    case COUNTER_CONFIG_REG:
+        val = s->counter_config_reg;
+        break;
+    case CONFIG_REG1:
+        val = s->config_reg1;
+        break;
+    case CLOCK_CONFIG_REG:
+        val = s->clock_config_reset_control;
+        break;
+    case MEMORY_MAPPING_REG:
+        val = s->memory_mapping_reg;
+        break;
+    case TRANSMIT_DATA_REG:
+        val = s->transmit_data_reg;
+        break;
+    case RECEIVE_DATA_REG:
+        val = s->receive_data_reg;
+        trace_pnv_spi_read_RDR(val);
+        s->status_reg = SETFIELD(STATUS_REG_RDR_FULL, s->status_reg, 0);
+        break;
+    case SEQUENCER_OPERATION_REG:
+        val = 0;

hw/ppc/pnv_spi_controller.c line 64

Quoted 10 lines
hw/ppc/pnv_spi_controller.c new file +10 -0
+        for (int i = 0; i < SPI_CONTROLLER_REG_SIZE; i++) {
+            val = (val << 8) | s->sequencer_operation_reg[i];
+        }
+        break;
+    case STATUS_REG:
+        val = s->status_reg;
+        break;
+    default:
+        qemu_log_mask(LOG_GUEST_ERROR, "spi_controller_regs: Invalid xscom "
+                 "read at 0x%08x\n", reg);

Please use PRIx32 format

Quoted 67 lines
hw/ppc/pnv_spi_controller.c new file +67 -0
+    }
+
+    trace_pnv_spi_read(addr, val);
+    return val;
+}
+
+static void pnv_spi_controller_write(void *opaque, hwaddr addr,
+                                 uint64_t val, unsigned size)
+{
+    PnvSpiController *s = PNV_SPICONTROLLER(opaque);
+    uint32_t reg = addr >> 3;
+
+    trace_pnv_spi_write(addr, val);
+
+    switch (reg) {
+    case ERROR_REG:
+        s->error_reg = val;
+        break;
+    case COUNTER_CONFIG_REG:
+        s->counter_config_reg = val;
+        break;
+    case CONFIG_REG1:
+        s->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,
+                                s->clock_config_reset_control) == 0x5) {
+            if (GETFIELD(CLOCK_CONFIG_REG_RESET_CONTROL, val) == 0xA) {
+                /* SPI controller reset sequence completed, resetting */
+                s->clock_config_reset_control =
+                                 CLOCK_CONFIG_RESET_CONTROL_HARD_RESET;
+            } else {
+                s->clock_config_reset_control = val;
+            }
+        } else {
+            s->clock_config_reset_control = val;
+        }
+        break;
+    case MEMORY_MAPPING_REG:
+        s->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.
+         */
+        s->transmit_data_reg = val;
+        trace_pnv_spi_write_TDR(val);
+        s->status_reg = SETFIELD(STATUS_REG_TDR_FULL, s->status_reg, 1);
+        s->status_reg = SETFIELD(STATUS_REG_TDR_UNDERRUN, s->status_reg, 0);
+        break;
+    case RECEIVE_DATA_REG:
+        s->receive_data_reg = val;
+        break;
+    case SEQUENCER_OPERATION_REG:
+        for (int i = 0; i < SPI_CONTROLLER_REG_SIZE; i++) {
+            s->sequencer_operation_reg[i] = (val >> (56 - i * 8)) & 0xFF;
+        }
+        break;

hw/ppc/pnv_spi_controller.c line 141

Quoted 10 lines
hw/ppc/pnv_spi_controller.c new file +10 -0
+    case STATUS_REG:
+        /* other fields are ignore_write */
+        s->status_reg = SETFIELD(STATUS_REG_RDR_OVERRUN, s->status_reg,
+                                  GETFIELD(STATUS_REG_RDR, val));
+        s->status_reg = SETFIELD(STATUS_REG_TDR_OVERRUN, s->status_reg,
+                                  GETFIELD(STATUS_REG_TDR, val));
+        break;
+    default:
+        qemu_log_mask(LOG_GUEST_ERROR, "spi_controller_regs: Invalid xscom "
+                 "write at 0x%08x\n", reg);

Please use PRIx32 format

Thanks,

C.

Quoted 128 lines
hw/ppc/pnv_spi_controller.c new file +87 -0
+    }
+    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 *s = PNV_SPICONTROLLER(dev);
+    g_autofree char *name = g_strdup_printf(TYPE_PNV_SPI_BUS ".%d",
+                    s->spic_num);
+    s->ssi_bus = ssi_create_bus(dev, name);
+    s->cs_line = g_new0(qemu_irq, 1);
+    qdev_init_gpio_out_named(DEVICE(s), s->cs_line, "cs", 1);
+
+    /* spi controller scoms */
+    pnv_xscom_region_init(&s->xscom_spic_regs, OBJECT(s),
+                          &pnv_spi_controller_xscom_ops, s,
+                          "xscom-spi-controller-regs",
+                          PNV10_XSCOM_PIB_SPIC_SIZE);
+}
+
+static int pnv_spi_controller_dt_xscom(PnvXScomInterface *dev, void *fdt,
+                             int offset)
+{
+    PnvSpiController *s = PNV_SPICONTROLLER(dev);
+    g_autofree char *name;
+    int s_offset;
+    const char compat[] = "ibm,power10-spi_controller";
+    uint32_t spic_pcba = PNV10_XSCOM_PIB_SPIC_BASE +
+        s->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);
+    s_offset = fdt_add_subnode(fdt, offset, name);
+    _FDT(s_offset);
+
+    _FDT(fdt_setprop(fdt, s_offset, "reg", reg, sizeof(reg)));
+    _FDT(fdt_setprop(fdt, s_offset, "compatible", compat, sizeof(compat)));
+    _FDT((fdt_setprop_cell(fdt, s_offset, "spic_num#", s->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_SYS_BUS_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/Kconfig +1 -0
index 78f83e78ce..6f9670b377 100644
@@ -39,6 +39,7 @@ config POWERNV
      select PCI_POWERNV
      select PCA9552
      select PCA9554
+    select SSI
 
  config PPC405
      bool
hw/ppc/meson.build +1 -0
index d096636ee7..68fadbae7b 100644
@@ -56,6 +56,7 @@ ppc_ss.add(when: 'CONFIG_POWERNV', if_true: files(
    'pnv_pnor.c',
    'pnv_nest_pervasive.c',
    'pnv_n1_chiplet.c',
+  'pnv_spi_controller.c',
  ))
  # PowerPC 4xx boards
  ppc_ss.add(when: 'CONFIG_PPC405', if_true: files(
hw/ppc/trace-events +6 -0
index bf29bbfd4b..b8e494ffc5 100644
@@ -110,6 +110,12 @@ pnv_sbe_cmd_timer_stop(void) ""
  pnv_sbe_cmd_timer_expired(void) ""
  pnv_sbe_msg_recv(uint16_t cmd, uint16_t seq, uint16_t ctrl_flags) "cmd 0x%" PRIx16 " seq %"PRIu16 " ctrl_flags 0x%" PRIx16
 
+#pnv_spi_controller.c
+pnv_spi_read(uint64_t addr, uint64_t val) "addr 0x%" PRIx64 " val 0x%" PRIx64
+pnv_spi_write(uint64_t addr, uint64_t val) "addr 0x%" PRIx64 " val 0x%" PRIx64
+pnv_spi_read_RDR(uint64_t val) "data extracted = 0x%" PRIx64
+pnv_spi_write_TDR(uint64_t val) "being written, data written = 0x%" PRIx64
+
  # ppc.c
  ppc_tb_adjust(uint64_t offs1, uint64_t offs2, int64_t diff, int64_t seconds) "adjusted from 0x%"PRIx64" to 0x%"PRIx64", diff %"PRId64" (%"PRId64"s)"
  ppc_tb_load(uint64_t tb) "tb 0x%016" PRIx64
#3 lore raw

Re: [PATCH v3 1/5] ppc/pnv: Add SPI controller model

Quoted 538 lines · Cédric Le Goater
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 8 lines

An existing QEMU SSI framework is used and SSI_BUS is created.

---
  include/hw/ppc/pnv_xscom.h    |   3 + include/hw/ssi/pnv_spi.h      |  44 +++++++ include/hw/ssi/pnv_spi_regs.h | 114 +++++++++++++++++ hw/ppc/pnv_spi_controller.c   | 228 ++++++++++++++++++++++++++++++++++

The file names are not consistent.

Please rename hw/ppc/pnv_spi_controller.c to /hw/ssi/pnv_spi.c.

Quoted 39 lines

  hw/ppc/Kconfig                |   1 +
  hw/ppc/meson.build            |   1 +
  hw/ppc/trace-events           |   6 +

  7 files changed, 397 insertions(+)

  create mode 100644 include/hw/ssi/pnv_spi.h create mode 100644 include/hw/ssi/pnv_spi_regs.h create mode 100644 hw/ppc/pnv_spi_controller.c

include/hw/ppc/pnv_xscom.h +0 -0
index 6209e18492..a77b97f9b1 100644
@@ -194,6 +194,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

include/hw/ppc/pnv_xscom.h +2 -0
+#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,

include/hw/ssi/pnv_spi.h new file +9 -0
new file mode 100644
index 0000000000..244ee1cfc0
@@ -0,0 +1,44 @@
+/*
+ * 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.
+ */

include/hw/ssi/pnv_spi.h line 19

Quoted 8 lines
include/hw/ssi/pnv_spi.h new file +8 -0
+#include "hw/ssi/ssi.h"
+
+#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)

OBJECT_DECLARE_SIMPLE_TYPE(PnvSpiController, PNV_SPI_CONTROLLER)

which means PNV_SPICONTROLLER -> PNV_SPI_CONTROLLER

include/hw/ssi/pnv_spi.h

Quoted 2 lines
include/hw/ssi/pnv_spi.h new file +2 -0
+
+#define SPI_CONTROLLER_REG_SIZE 8

please add a PNV_ prefix : PNV_SPI_CONTROLLER_REG_SIZE

Quoted 11 lines
include/hw/ssi/pnv_spi.h new file +11 -0
+
+#define TYPE_PNV_SPI_BUS "pnv-spi-bus"
+typedef struct PnvSpiController {
+    SysBusDevice parent_obj;
+
+    SSIBus *ssi_bus;
+    qemu_irq *cs_line;
+    MemoryRegion    xscom_spic_regs;
+    /* SPI controller object number */
+    uint32_t        spic_num;
+

include/hw/ssi/pnv_spi.h line 42

Quoted 10 lines
include/hw/ssi/pnv_spi.h new file +10 -0
+    /* 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;

I don't think the _reg suffix are needed. This is minor.

Quoted 8 lines
include/hw/ssi/pnv_spi.h new file +2 -0
+} PnvSpiController;
+#endif /* PPC_PNV_SPI_CONTROLLER_H */
include/hw/ssi/pnv_spi_regs.h new file +0 -0
new file mode 100644
index 0000000000..6f613aca5e

include/hw/ssi/pnv_spi_regs.h line 10

Quoted 10 lines
include/hw/ssi/pnv_spi_regs.h new file +10 -0
@@ -0,0 +1,114 @@
+/*
+ * QEMU PowerPC SPI Controller model
+ *
+ * Copyright (c) 2023, IBM Corporation.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef SPI_CONTROLLER_REGS_H
+#define SPI_CONTROLLER_REGS_H

please add a PNV_ prefix

Quoted 119 lines
include/hw/ssi/pnv_spi_regs.h new file +58 -0
+
+/* 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)
+ */

include/hw/ssi/pnv_spi_regs.h new file +37 -0
+
+#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.
+ */
+#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
+
+#endif
hw/ppc/pnv_spi_controller.c new file +8 -0
new file mode 100644
index 0000000000..11b119cf0f
@@ -0,0 +1,228 @@
+/*
+ * QEMU PowerPC SPI Controller model
+ *
+ * Copyright (c) 2024, IBM Corporation.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+

hw/ppc/pnv_spi_controller.c line 18

Quoted 10 lines
hw/ppc/pnv_spi_controller.c new file +10 -0
+#include "qemu/osdep.h"
+#include "qemu/log.h"
+#include "hw/qdev-properties.h"
+#include "hw/ppc/pnv_xscom.h"
+#include "hw/ssi/pnv_spi.h"
+#include "hw/ssi/pnv_spi_regs.h"
+#include "hw/ssi/ssi.h"
+#include "hw/ppc/fdt.h"
+#include <libfdt.h>
+#include <math.h>

why math.h ?

Quoted 40 lines
hw/ppc/pnv_spi_controller.c new file +33 -0
+#include "hw/irq.h"
+#include "trace.h"
+
+static uint64_t pnv_spi_controller_read(void *opaque, hwaddr addr,
+                                 unsigned size)
+{
+    PnvSpiController *s = PNV_SPICONTROLLER(opaque);
+    uint32_t reg = addr >> 3;
+    uint64_t val = ~0ull;
+
+    switch (reg) {
+    case ERROR_REG:
+        val = s->error_reg;
+        break;
+    case COUNTER_CONFIG_REG:
+        val = s->counter_config_reg;
+        break;
+    case CONFIG_REG1:
+        val = s->config_reg1;
+        break;
+    case CLOCK_CONFIG_REG:
+        val = s->clock_config_reset_control;
+        break;
+    case MEMORY_MAPPING_REG:
+        val = s->memory_mapping_reg;
+        break;
+    case TRANSMIT_DATA_REG:
+        val = s->transmit_data_reg;
+        break;
+    case RECEIVE_DATA_REG:
+        val = s->receive_data_reg;
+        trace_pnv_spi_read_RDR(val);
+        s->status_reg = SETFIELD(STATUS_REG_RDR_FULL, s->status_reg, 

0);
+        break;
+    case SEQUENCER_OPERATION_REG:
+        val = 0;
+        for (int i = 0; i < SPI_CONTROLLER_REG_SIZE; i++) { +            val = (val << 8) | s->sequencer_operation_reg[i]; +        }

Quoted 8 lines

+        break;
+    case STATUS_REG:
+        val = s->status_reg;
+        break;
+    default:
+        qemu_log_mask(LOG_GUEST_ERROR, "spi_controller_regs: Invalid xscom "
+                 "read at 0x%08x\n", reg);

Please use PRIx32 format

Quoted 79 lines

+    }

hw/ppc/pnv_spi_controller.c new file +32 -0
+
+    trace_pnv_spi_read(addr, val);
+    return val;
+}
+
+static void pnv_spi_controller_write(void *opaque, hwaddr addr,
+                                 uint64_t val, unsigned size)
+{
+    PnvSpiController *s = PNV_SPICONTROLLER(opaque);
+    uint32_t reg = addr >> 3;
+
+    trace_pnv_spi_write(addr, val);
+
+    switch (reg) {
+    case ERROR_REG:
+        s->error_reg = val;
+        break;
+    case COUNTER_CONFIG_REG:
+        s->counter_config_reg = val;
+        break;
+    case CONFIG_REG1:
+        s->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,
+ s->clock_config_reset_control) == 0x5) {
+            if (GETFIELD(CLOCK_CONFIG_REG_RESET_CONTROL, val) == 0xA) {
+                /* SPI controller reset sequence completed, 

resetting */
+                s->clock_config_reset_control =
+ CLOCK_CONFIG_RESET_CONTROL_HARD_RESET;
+            } else {
+                s->clock_config_reset_control = val;
+            }
+        } else {
+            s->clock_config_reset_control = val;
+        }
+        break;
+    case MEMORY_MAPPING_REG:
+        s->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. +         */
+        s->transmit_data_reg = val;
+        trace_pnv_spi_write_TDR(val);
+        s->status_reg = SETFIELD(STATUS_REG_TDR_FULL, s->status_reg, 1);
+        s->status_reg = SETFIELD(STATUS_REG_TDR_UNDERRUN, s->status_reg, 0);
+        break;
+    case RECEIVE_DATA_REG:
+        s->receive_data_reg = val;
+        break;
+    case SEQUENCER_OPERATION_REG:
+        for (int i = 0; i < SPI_CONTROLLER_REG_SIZE; i++) { +            s->sequencer_operation_reg[i] = (val >> (56 - i * 8)) & 0xFF;
+        }
+        break;
+    case STATUS_REG:
+        /* other fields are ignore_write */
+        s->status_reg = SETFIELD(STATUS_REG_RDR_OVERRUN, s->status_reg,

Quoted 8 lines

+                                  GETFIELD(STATUS_REG_RDR, val)); +        s->status_reg = SETFIELD(STATUS_REG_TDR_OVERRUN, s->status_reg, +                                  GETFIELD(STATUS_REG_TDR, val)); +        break;
+    default:
+        qemu_log_mask(LOG_GUEST_ERROR, "spi_controller_regs: Invalid xscom "
+                 "write at 0x%08x\n", reg);

Please use PRIx32 format

Thanks,

C.

Quoted 132 lines

+    }
+    return;

hw/ppc/pnv_spi_controller.c new file +34 -0
+}
+
+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 *s = PNV_SPICONTROLLER(dev);
+    g_autofree char *name = g_strdup_printf(TYPE_PNV_SPI_BUS ".%d",
+                    s->spic_num);
+    s->ssi_bus = ssi_create_bus(dev, name);
+    s->cs_line = g_new0(qemu_irq, 1);
+    qdev_init_gpio_out_named(DEVICE(s), s->cs_line, "cs", 1);
+
+    /* spi controller scoms */
+    pnv_xscom_region_init(&s->xscom_spic_regs, OBJECT(s),
+                          &pnv_spi_controller_xscom_ops, s,
+                          "xscom-spi-controller-regs",
+                          PNV10_XSCOM_PIB_SPIC_SIZE);
+}
+
+static int pnv_spi_controller_dt_xscom(PnvXScomInterface *dev, void 

*fdt,
+                             int offset)

hw/ppc/pnv_spi_controller.c new file +17 -0
+{
+    PnvSpiController *s = PNV_SPICONTROLLER(dev);
+    g_autofree char *name;
+    int s_offset;
+    const char compat[] = "ibm,power10-spi_controller";
+    uint32_t spic_pcba = PNV10_XSCOM_PIB_SPIC_BASE +
+        s->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);
+    s_offset = fdt_add_subnode(fdt, offset, name);
+    _FDT(s_offset);
+
+    _FDT(fdt_setprop(fdt, s_offset, "reg", reg, sizeof(reg)));
+    _FDT(fdt_setprop(fdt, s_offset, "compatible", compat, 

sizeof(compat)));
+    _FDT((fdt_setprop_cell(fdt, s_offset, "spic_num#", s->spic_num))); +    return 0;

hw/ppc/pnv_spi_controller.c new file +3 -0
+}
+
+static void pnv_spi_controller_class_init(ObjectClass *klass, void 

*data)

hw/ppc/pnv_spi_controller.c new file +28 -0
+{
+    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_SYS_BUS_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/Kconfig +0 -0
index 78f83e78ce..6f9670b377 100644
@@ -39,6 +39,7 @@ config POWERNV

      select PCI_POWERNV
      select PCA9552
      select PCA9554
+    select SSI
    config PPC405
      bool

hw/ppc/meson.build +1 -0
index d096636ee7..68fadbae7b 100644
@@ -56,6 +56,7 @@ ppc_ss.add(when: 'CONFIG_POWERNV', if_true: files(
     'pnv_pnor.c',
     'pnv_nest_pervasive.c',
     'pnv_n1_chiplet.c',
+  'pnv_spi_controller.c',
   ))
   # PowerPC 4xx boards

  ppc_ss.add(when: 'CONFIG_PPC405', if_true: files(

hw/ppc/trace-events +0 -0
index bf29bbfd4b..b8e494ffc5 100644
@@ -110,6 +110,12 @@ pnv_sbe_cmd_timer_stop(void) ""

  pnv_sbe_cmd_timer_expired(void) ""
  pnv_sbe_msg_recv(uint16_t cmd, uint16_t seq, uint16_t ctrl_flags) "cmd 0x%" PRIx16 " seq %"PRIu16 " ctrl_flags 0x%" PRIx16 +#pnv_spi_controller.c
+pnv_spi_read(uint64_t addr, uint64_t val) "addr 0x%" PRIx64 " val
0x%" PRIx64
+pnv_spi_write(uint64_t addr, uint64_t val) "addr 0x%" PRIx64 " val
0x%" PRIx64

hw/ppc/trace-events +2 -0
+pnv_spi_read_RDR(uint64_t val) "data extracted = 0x%" PRIx64
+pnv_spi_write_TDR(uint64_t val) "being written, data written = 0x%" 

PRIx64
+

  # ppc.c
  ppc_tb_adjust(uint64_t offs1, uint64_t offs2, int64_t diff, int64_t seconds) "adjusted from 0x%"PRIx64" to 0x%"PRIx64", diff %"PRId64" (%"PRId64"s)"
  ppc_tb_load(uint64_t tb) "tb 0x%016" PRIx64
#4 lore raw

Re: [PATCH v3 1/5] ppc/pnv: Add SPI controller model

In reply to Cédric Le Goater

Hello Cedric,

Thank You for reviewing the patch v3

Regards,

Chalapathi

Quoted 18 lines · Cédric Le Goater
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 8 lines

An existing QEMU SSI framework is used and SSI_BUS is created.

---
  include/hw/ppc/pnv_xscom.h    |   3 + include/hw/ssi/pnv_spi.h      |  44 +++++++ include/hw/ssi/pnv_spi_regs.h | 114 +++++++++++++++++ hw/ppc/pnv_spi_controller.c   | 228 ++++++++++++++++++++++++++++++++++

Quoted 3 lines

The file names are not consistent.

Please rename hw/ppc/pnv_spi_controller.c to /hw/ssi/pnv_spi.c.

Sure, Will rename pnv_spi_controller to pnv_spi all over. Thank You.

Quoted 52 lines
Quoted 41 lines

  hw/ppc/Kconfig                |   1 +
  hw/ppc/meson.build            |   1 +
  hw/ppc/trace-events           |   6 +

  7 files changed, 397 insertions(+)

  create mode 100644 include/hw/ssi/pnv_spi.h create mode 100644 include/hw/ssi/pnv_spi_regs.h create mode 100644 hw/ppc/pnv_spi_controller.c

include/hw/ppc/pnv_xscom.h +0 -0
index 6209e18492..a77b97f9b1 100644
@@ -194,6 +194,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

include/hw/ppc/pnv_xscom.h +2 -0
+#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,

include/hw/ssi/pnv_spi.h new file +9 -0
new file mode 100644
index 0000000000..244ee1cfc0
@@ -0,0 +1,44 @@
+/*
+ * 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.
+ */

include/hw/ssi/pnv_spi.h line 19

Quoted 8 lines
include/hw/ssi/pnv_spi.h new file +8 -0
+#include "hw/ssi/ssi.h"
+
+#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)

OBJECT_DECLARE_SIMPLE_TYPE(PnvSpiController, PNV_SPI_CONTROLLER)

Quoted 6 lines

which means PNV_SPICONTROLLER -> PNV_SPI_CONTROLLER

include/hw/ssi/pnv_spi.h

Quoted 2 lines
include/hw/ssi/pnv_spi.h new file +2 -0
+
+#define SPI_CONTROLLER_REG_SIZE 8

please add a PNV_ prefix : PNV_SPI_CONTROLLER_REG_SIZE

Sure.

Quoted 16 lines
Quoted 16 lines
include/hw/ssi/pnv_spi.h new file +15 -0
+
+#define TYPE_PNV_SPI_BUS "pnv-spi-bus"
+typedef struct PnvSpiController {
+    SysBusDevice parent_obj;
+
+    SSIBus *ssi_bus;
+    qemu_irq *cs_line;
+    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;
Quoted 8 lines
Quoted 6 lines

+    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;

I don't think the _reg suffix are needed. This is minor.

Will remove. Thank You

Quoted 13 lines
Quoted 13 lines
include/hw/ssi/pnv_spi.h new file +2 -0
+} PnvSpiController;
+#endif /* PPC_PNV_SPI_CONTROLLER_H */
include/hw/ssi/pnv_spi_regs.h new file +4 -0
new file mode 100644
index 0000000000..6f613aca5e
@@ -0,0 +1,114 @@
+/*
+ * QEMU PowerPC SPI Controller model
+ *
+ * Copyright (c) 2023, IBM Corporation.
Quoted 8 lines

include/hw/ssi/pnv_spi_regs.h line 10

Quoted 6 lines
include/hw/ssi/pnv_spi_regs.h new file +6 -0
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef SPI_CONTROLLER_REGS_H
+#define SPI_CONTROLLER_REGS_H

please add a PNV_ prefix

Sure.

Quoted 124 lines
Quoted 124 lines
include/hw/ssi/pnv_spi_regs.h new file +58 -0
+
+/* 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)
+ */

include/hw/ssi/pnv_spi_regs.h new file +37 -0
+
+#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.
+ */
+#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
+
+#endif
hw/ppc/pnv_spi_controller.c new file +12 -0
new file mode 100644
index 0000000000..11b119cf0f
@@ -0,0 +1,228 @@
+/*
+ * 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_xscom.h"
Quoted 8 lines

hw/ppc/pnv_spi_controller.c line 18

Quoted 6 lines
hw/ppc/pnv_spi_controller.c new file +6 -0
+#include "hw/ssi/pnv_spi.h"
+#include "hw/ssi/pnv_spi_regs.h"
+#include "hw/ssi/ssi.h"
+#include "hw/ppc/fdt.h"
+#include <libfdt.h>
+#include <math.h>

why math.h ?

Used for ceil() but that's in patch 2/5. Will remove here.

Quoted 43 lines
Quoted 43 lines
hw/ppc/pnv_spi_controller.c new file +33 -0
+#include "hw/irq.h"
+#include "trace.h"
+
+static uint64_t pnv_spi_controller_read(void *opaque, hwaddr addr,
+                                 unsigned size)
+{
+    PnvSpiController *s = PNV_SPICONTROLLER(opaque);
+    uint32_t reg = addr >> 3;
+    uint64_t val = ~0ull;
+
+    switch (reg) {
+    case ERROR_REG:
+        val = s->error_reg;
+        break;
+    case COUNTER_CONFIG_REG:
+        val = s->counter_config_reg;
+        break;
+    case CONFIG_REG1:
+        val = s->config_reg1;
+        break;
+    case CLOCK_CONFIG_REG:
+        val = s->clock_config_reset_control;
+        break;
+    case MEMORY_MAPPING_REG:
+        val = s->memory_mapping_reg;
+        break;
+    case TRANSMIT_DATA_REG:
+        val = s->transmit_data_reg;
+        break;
+    case RECEIVE_DATA_REG:
+        val = s->receive_data_reg;
+        trace_pnv_spi_read_RDR(val);
+        s->status_reg = SETFIELD(STATUS_REG_RDR_FULL, s->status_reg, 

0);
+        break;
+    case SEQUENCER_OPERATION_REG:
+        val = 0;
+        for (int i = 0; i < SPI_CONTROLLER_REG_SIZE; i++) { +            val = (val << 8) | s->sequencer_operation_reg[i]; +        }
+        break;
+    case STATUS_REG:

Quoted 8 lines
Quoted 6 lines

+        val = s->status_reg;
+        break;
+    default:
+        qemu_log_mask(LOG_GUEST_ERROR, "spi_controller_regs: Invalid xscom "
+                 "read at 0x%08x\n", reg);

Please use PRIx32 format

Sure.

Quoted 93 lines
Quoted 80 lines

+    }

hw/ppc/pnv_spi_controller.c new file +32 -0
+
+    trace_pnv_spi_read(addr, val);
+    return val;
+}
+
+static void pnv_spi_controller_write(void *opaque, hwaddr addr,
+                                 uint64_t val, unsigned size)
+{
+    PnvSpiController *s = PNV_SPICONTROLLER(opaque);
+    uint32_t reg = addr >> 3;
+
+    trace_pnv_spi_write(addr, val);
+
+    switch (reg) {
+    case ERROR_REG:
+        s->error_reg = val;
+        break;
+    case COUNTER_CONFIG_REG:
+        s->counter_config_reg = val;
+        break;
+    case CONFIG_REG1:
+        s->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,
+ s->clock_config_reset_control) == 0x5) {
+            if (GETFIELD(CLOCK_CONFIG_REG_RESET_CONTROL, val) == 0xA) {
+                /* SPI controller reset sequence completed, 

resetting */
+                s->clock_config_reset_control =
+ CLOCK_CONFIG_RESET_CONTROL_HARD_RESET;
+            } else {
+                s->clock_config_reset_control = val;
+            }
+        } else {
+            s->clock_config_reset_control = val;
+        }
+        break;
+    case MEMORY_MAPPING_REG:
+        s->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. +         */
+        s->transmit_data_reg = val;
+        trace_pnv_spi_write_TDR(val);
+        s->status_reg = SETFIELD(STATUS_REG_TDR_FULL, s->status_reg, 1);
+        s->status_reg = SETFIELD(STATUS_REG_TDR_UNDERRUN, s->status_reg, 0);
+        break;
+    case RECEIVE_DATA_REG:
+        s->receive_data_reg = val;
+        break;
+    case SEQUENCER_OPERATION_REG:
+        for (int i = 0; i < SPI_CONTROLLER_REG_SIZE; i++) { +            s->sequencer_operation_reg[i] = (val >> (56 - i * 8)) & 0xFF;
+        }
+        break;
+    case STATUS_REG:
+        /* other fields are ignore_write */
+        s->status_reg = SETFIELD(STATUS_REG_RDR_OVERRUN, s->status_reg,

Quoted 8 lines

+                                  GETFIELD(STATUS_REG_RDR, val)); +        s->status_reg = SETFIELD(STATUS_REG_TDR_OVERRUN, s->status_reg, +                                  GETFIELD(STATUS_REG_TDR, val)); +        break;
+    default:
+        qemu_log_mask(LOG_GUEST_ERROR, "spi_controller_regs: Invalid xscom "
+                 "write at 0x%08x\n", reg);

Please use PRIx32 format

Quoted 3 lines

Thanks,

C.

Sure. Thank You.

Quoted 135 lines
Quoted 134 lines

+    }
+    return;

hw/ppc/pnv_spi_controller.c new file +34 -0
+}
+
+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 *s = PNV_SPICONTROLLER(dev);
+    g_autofree char *name = g_strdup_printf(TYPE_PNV_SPI_BUS ".%d",
+                    s->spic_num);
+    s->ssi_bus = ssi_create_bus(dev, name);
+    s->cs_line = g_new0(qemu_irq, 1);
+    qdev_init_gpio_out_named(DEVICE(s), s->cs_line, "cs", 1);
+
+    /* spi controller scoms */
+    pnv_xscom_region_init(&s->xscom_spic_regs, OBJECT(s),
+                          &pnv_spi_controller_xscom_ops, s,
+                          "xscom-spi-controller-regs",
+                          PNV10_XSCOM_PIB_SPIC_SIZE);
+}
+
+static int pnv_spi_controller_dt_xscom(PnvXScomInterface *dev, void 

*fdt,
+                             int offset)

hw/ppc/pnv_spi_controller.c new file +17 -0
+{
+    PnvSpiController *s = PNV_SPICONTROLLER(dev);
+    g_autofree char *name;
+    int s_offset;
+    const char compat[] = "ibm,power10-spi_controller";
+    uint32_t spic_pcba = PNV10_XSCOM_PIB_SPIC_BASE +
+        s->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);
+    s_offset = fdt_add_subnode(fdt, offset, name);
+    _FDT(s_offset);
+
+    _FDT(fdt_setprop(fdt, s_offset, "reg", reg, sizeof(reg)));
+    _FDT(fdt_setprop(fdt, s_offset, "compatible", compat, 

sizeof(compat)));
+    _FDT((fdt_setprop_cell(fdt, s_offset, "spic_num#", s->spic_num))); +    return 0;

hw/ppc/pnv_spi_controller.c new file +3 -0
+}
+
+static void pnv_spi_controller_class_init(ObjectClass *klass, void 

*data)

hw/ppc/pnv_spi_controller.c new file +28 -0
+{
+    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_SYS_BUS_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/Kconfig +0 -0
index 78f83e78ce..6f9670b377 100644
@@ -39,6 +39,7 @@ config POWERNV

      select PCI_POWERNV
      select PCA9552
      select PCA9554
+    select SSI
    config PPC405
      bool

hw/ppc/meson.build +1 -0
index d096636ee7..68fadbae7b 100644
@@ -56,6 +56,7 @@ ppc_ss.add(when: 'CONFIG_POWERNV', if_true: files(
     'pnv_pnor.c',
     'pnv_nest_pervasive.c',
     'pnv_n1_chiplet.c',
+  'pnv_spi_controller.c',
   ))
   # PowerPC 4xx boards

  ppc_ss.add(when: 'CONFIG_PPC405', if_true: files(

hw/ppc/trace-events +0 -0
index bf29bbfd4b..b8e494ffc5 100644
@@ -110,6 +110,12 @@ pnv_sbe_cmd_timer_stop(void) ""

  pnv_sbe_cmd_timer_expired(void) ""
  pnv_sbe_msg_recv(uint16_t cmd, uint16_t seq, uint16_t ctrl_flags) "cmd 0x%" PRIx16 " seq %"PRIu16 " ctrl_flags 0x%" PRIx16 +#pnv_spi_controller.c
+pnv_spi_read(uint64_t addr, uint64_t val) "addr 0x%" PRIx64 " val
0x%" PRIx64
+pnv_spi_write(uint64_t addr, uint64_t val) "addr 0x%" PRIx64 " val
0x%" PRIx64

hw/ppc/trace-events +2 -0
+pnv_spi_read_RDR(uint64_t val) "data extracted = 0x%" PRIx64
+pnv_spi_write_TDR(uint64_t val) "being written, data written = 0x%" 

PRIx64
+

  # ppc.c
  ppc_tb_adjust(uint64_t offs1, uint64_t offs2, int64_t diff, int64_t seconds) "adjusted from 0x%"PRIx64" to 0x%"PRIx64", diff %"PRId64" (%"PRId64"s)"
  ppc_tb_load(uint64_t tb) "tb 0x%016" PRIx64
#5 lore raw

Re: [PATCH v3 1/5] ppc/pnv: Add SPI controller model

In reply to Cédric Le Goater
Quoted 18 lines · Cédric Le Goater
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 8 lines

An existing QEMU SSI framework is used and SSI_BUS is created.

---
  include/hw/ppc/pnv_xscom.h    |   3 + include/hw/ssi/pnv_spi.h      |  44 +++++++ include/hw/ssi/pnv_spi_regs.h | 114 +++++++++++++++++ hw/ppc/pnv_spi_controller.c   | 228 ++++++++++++++++++++++++++++++++++

Quoted 3 lines

The file names are not consistent.

Please rename hw/ppc/pnv_spi_controller.c to /hw/ssi/pnv_spi.c.

Hello Cedric,

I could not compile hw/ssi/pnv_spi.c because of target specific code.

/FAILED: libcommon.fa.p/hw_ssi_pnv_spi.c.o
cc -m64 -mcx16 -Ilibcommon.fa.p -I/usr/include/p11-kit-1 -I/usr/include/pixman-1 -I/usr/include/glib-2.0 -I/usr/lib64/glib-2.0/include -I/usr/include/gio-unix-2.0/ -I/usr/include/slirp -fdiagnostics-color=auto -Wall -Winvalid-pch -Werror -std=gnu11 -O2 -g -fstack-protector-strong -Wundef -Wwrite-strings -Wmissing-prototypes -Wstrict-prototypes -Wredundant-decls -Wold-style-declaration -Wold-style-definition -Wtype-limits -Wformat-security -Wformat-y2k -Winit-self -Wignored-qualifiers -Wempty-body -Wnested-externs -Wendif-labels -Wexpansion-to-defined -Wimplicit-fallthrough=2 -Wmissing-format-attribute -Wno-missing-include-dirs -Wno-shift-negative-value -Wno-psabi -isystem <qemu_dir>/linux-headers -isystem linux-headers -iquote . -iquote <qemu_dir> -iquote <<qemu_dir>>/include -iquote <qemu_dir>/host/include/x86_64 -iquote <qemu_dir>/host/include/generic -iquote <qemu_dir>/tcg/i386 -pthread -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -fno-strict-aliasing -fno-common -fwrapv -fPIE -MD -MQ libcommon.fa.p/hw_ssi_pnv_spi.c.o -MF libcommon.fa.p/hw_ssi_pnv_spi.c.o.d -o libcommon.fa.p/hw_ssi_pnv_spi.c.o -c ../hw/ssi/pnv_spi.c In file included from <qemu_dir>/target/ppc/cpu.h:25,
from <qemu_dir>/include/hw/ppc/pnv.h:24,
from <qemu_dir>/include/hw/ppc/pnv_xscom.h:24,
                 from ../hw/ssi/pnv_spi.c:12:
<qemu_dir>/include/exec/cpu-defs.h:23:2: error: #error cpu.h included from common code
 #error cpu.h included from common code
  ^~~~~
In file included from /<qemu_dir>/target/ppc/cpu.h:25,
from <qemu_dir>/include/hw/ppc/pnv.h:24,
from <qemu_dir>/include/hw/ppc/pnv_xscom.h:24,
                 from ../hw/ssi/pnv_spi.c:12:
<qemu_dir>/include/exec/cpu-defs.h:34:10: fatal error: cpu-param.h: No such file or directory
 #include "cpu-param.h"
          ^~~~~~~~~~~~~
compilation terminated.
/

Hence shall I keep pnv_spi.c in /hw/ppc/ ?

Thank You,

Chalapathi

Quoted 517 lines
Quoted 41 lines

  hw/ppc/Kconfig                |   1 +
  hw/ppc/meson.build            |   1 +
  hw/ppc/trace-events           |   6 +

  7 files changed, 397 insertions(+)

  create mode 100644 include/hw/ssi/pnv_spi.h create mode 100644 include/hw/ssi/pnv_spi_regs.h create mode 100644 hw/ppc/pnv_spi_controller.c

include/hw/ppc/pnv_xscom.h +0 -0
index 6209e18492..a77b97f9b1 100644
@@ -194,6 +194,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

include/hw/ppc/pnv_xscom.h +2 -0
+#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,

include/hw/ssi/pnv_spi.h new file +9 -0
new file mode 100644
index 0000000000..244ee1cfc0
@@ -0,0 +1,44 @@
+/*
+ * 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.
+ */

include/hw/ssi/pnv_spi.h line 19

Quoted 8 lines
include/hw/ssi/pnv_spi.h new file +8 -0
+#include "hw/ssi/ssi.h"
+
+#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)

OBJECT_DECLARE_SIMPLE_TYPE(PnvSpiController, PNV_SPI_CONTROLLER)

which means PNV_SPICONTROLLER -> PNV_SPI_CONTROLLER

include/hw/ssi/pnv_spi.h

Quoted 2 lines
include/hw/ssi/pnv_spi.h new file +2 -0
+
+#define SPI_CONTROLLER_REG_SIZE 8

please add a PNV_ prefix : PNV_SPI_CONTROLLER_REG_SIZE

Quoted 11 lines
include/hw/ssi/pnv_spi.h new file +11 -0
+
+#define TYPE_PNV_SPI_BUS "pnv-spi-bus"
+typedef struct PnvSpiController {
+    SysBusDevice parent_obj;
+
+    SSIBus *ssi_bus;
+    qemu_irq *cs_line;
+    MemoryRegion    xscom_spic_regs;
+    /* SPI controller object number */
+    uint32_t        spic_num;
+

include/hw/ssi/pnv_spi.h line 42

Quoted 10 lines
include/hw/ssi/pnv_spi.h new file +10 -0
+    /* 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;

I don't think the _reg suffix are needed. This is minor.

Quoted 8 lines
include/hw/ssi/pnv_spi.h new file +2 -0
+} PnvSpiController;
+#endif /* PPC_PNV_SPI_CONTROLLER_H */
include/hw/ssi/pnv_spi_regs.h new file +0 -0
new file mode 100644
index 0000000000..6f613aca5e

include/hw/ssi/pnv_spi_regs.h line 10

Quoted 10 lines
include/hw/ssi/pnv_spi_regs.h new file +10 -0
@@ -0,0 +1,114 @@
+/*
+ * QEMU PowerPC SPI Controller model
+ *
+ * Copyright (c) 2023, IBM Corporation.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef SPI_CONTROLLER_REGS_H
+#define SPI_CONTROLLER_REGS_H

please add a PNV_ prefix

Quoted 119 lines
include/hw/ssi/pnv_spi_regs.h new file +58 -0
+
+/* 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)
+ */

include/hw/ssi/pnv_spi_regs.h new file +37 -0
+
+#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.
+ */
+#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
+
+#endif
hw/ppc/pnv_spi_controller.c new file +8 -0
new file mode 100644
index 0000000000..11b119cf0f
@@ -0,0 +1,228 @@
+/*
+ * QEMU PowerPC SPI Controller model
+ *
+ * Copyright (c) 2024, IBM Corporation.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+

hw/ppc/pnv_spi_controller.c line 18

Quoted 10 lines
hw/ppc/pnv_spi_controller.c new file +10 -0
+#include "qemu/osdep.h"
+#include "qemu/log.h"
+#include "hw/qdev-properties.h"
+#include "hw/ppc/pnv_xscom.h"
+#include "hw/ssi/pnv_spi.h"
+#include "hw/ssi/pnv_spi_regs.h"
+#include "hw/ssi/ssi.h"
+#include "hw/ppc/fdt.h"
+#include <libfdt.h>
+#include <math.h>

why math.h ?

Quoted 40 lines
hw/ppc/pnv_spi_controller.c new file +33 -0
+#include "hw/irq.h"
+#include "trace.h"
+
+static uint64_t pnv_spi_controller_read(void *opaque, hwaddr addr,
+                                 unsigned size)
+{
+    PnvSpiController *s = PNV_SPICONTROLLER(opaque);
+    uint32_t reg = addr >> 3;
+    uint64_t val = ~0ull;
+
+    switch (reg) {
+    case ERROR_REG:
+        val = s->error_reg;
+        break;
+    case COUNTER_CONFIG_REG:
+        val = s->counter_config_reg;
+        break;
+    case CONFIG_REG1:
+        val = s->config_reg1;
+        break;
+    case CLOCK_CONFIG_REG:
+        val = s->clock_config_reset_control;
+        break;
+    case MEMORY_MAPPING_REG:
+        val = s->memory_mapping_reg;
+        break;
+    case TRANSMIT_DATA_REG:
+        val = s->transmit_data_reg;
+        break;
+    case RECEIVE_DATA_REG:
+        val = s->receive_data_reg;
+        trace_pnv_spi_read_RDR(val);
+        s->status_reg = SETFIELD(STATUS_REG_RDR_FULL, s->status_reg, 

0);
+        break;
+    case SEQUENCER_OPERATION_REG:
+        val = 0;
+        for (int i = 0; i < SPI_CONTROLLER_REG_SIZE; i++) { +            val = (val << 8) | s->sequencer_operation_reg[i]; +        }

Quoted 8 lines

+        break;
+    case STATUS_REG:
+        val = s->status_reg;
+        break;
+    default:
+        qemu_log_mask(LOG_GUEST_ERROR, "spi_controller_regs: Invalid xscom "
+                 "read at 0x%08x\n", reg);

Please use PRIx32 format

Quoted 79 lines

+    }

hw/ppc/pnv_spi_controller.c new file +32 -0
+
+    trace_pnv_spi_read(addr, val);
+    return val;
+}
+
+static void pnv_spi_controller_write(void *opaque, hwaddr addr,
+                                 uint64_t val, unsigned size)
+{
+    PnvSpiController *s = PNV_SPICONTROLLER(opaque);
+    uint32_t reg = addr >> 3;
+
+    trace_pnv_spi_write(addr, val);
+
+    switch (reg) {
+    case ERROR_REG:
+        s->error_reg = val;
+        break;
+    case COUNTER_CONFIG_REG:
+        s->counter_config_reg = val;
+        break;
+    case CONFIG_REG1:
+        s->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,
+ s->clock_config_reset_control) == 0x5) {
+            if (GETFIELD(CLOCK_CONFIG_REG_RESET_CONTROL, val) == 0xA) {
+                /* SPI controller reset sequence completed, 

resetting */
+                s->clock_config_reset_control =
+ CLOCK_CONFIG_RESET_CONTROL_HARD_RESET;
+            } else {
+                s->clock_config_reset_control = val;
+            }
+        } else {
+            s->clock_config_reset_control = val;
+        }
+        break;
+    case MEMORY_MAPPING_REG:
+        s->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. +         */
+        s->transmit_data_reg = val;
+        trace_pnv_spi_write_TDR(val);
+        s->status_reg = SETFIELD(STATUS_REG_TDR_FULL, s->status_reg, 1);
+        s->status_reg = SETFIELD(STATUS_REG_TDR_UNDERRUN, s->status_reg, 0);
+        break;
+    case RECEIVE_DATA_REG:
+        s->receive_data_reg = val;
+        break;
+    case SEQUENCER_OPERATION_REG:
+        for (int i = 0; i < SPI_CONTROLLER_REG_SIZE; i++) { +            s->sequencer_operation_reg[i] = (val >> (56 - i * 8)) & 0xFF;
+        }
+        break;
+    case STATUS_REG:
+        /* other fields are ignore_write */
+        s->status_reg = SETFIELD(STATUS_REG_RDR_OVERRUN, s->status_reg,

Quoted 8 lines

+                                  GETFIELD(STATUS_REG_RDR, val)); +        s->status_reg = SETFIELD(STATUS_REG_TDR_OVERRUN, s->status_reg, +                                  GETFIELD(STATUS_REG_TDR, val)); +        break;
+    default:
+        qemu_log_mask(LOG_GUEST_ERROR, "spi_controller_regs: Invalid xscom "
+                 "write at 0x%08x\n", reg);

Please use PRIx32 format

Thanks,

C.

Quoted 132 lines

+    }
+    return;

hw/ppc/pnv_spi_controller.c new file +34 -0
+}
+
+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 *s = PNV_SPICONTROLLER(dev);
+    g_autofree char *name = g_strdup_printf(TYPE_PNV_SPI_BUS ".%d",
+                    s->spic_num);
+    s->ssi_bus = ssi_create_bus(dev, name);
+    s->cs_line = g_new0(qemu_irq, 1);
+    qdev_init_gpio_out_named(DEVICE(s), s->cs_line, "cs", 1);
+
+    /* spi controller scoms */
+    pnv_xscom_region_init(&s->xscom_spic_regs, OBJECT(s),
+                          &pnv_spi_controller_xscom_ops, s,
+                          "xscom-spi-controller-regs",
+                          PNV10_XSCOM_PIB_SPIC_SIZE);
+}
+
+static int pnv_spi_controller_dt_xscom(PnvXScomInterface *dev, void 

*fdt,
+                             int offset)

hw/ppc/pnv_spi_controller.c new file +17 -0
+{
+    PnvSpiController *s = PNV_SPICONTROLLER(dev);
+    g_autofree char *name;
+    int s_offset;
+    const char compat[] = "ibm,power10-spi_controller";
+    uint32_t spic_pcba = PNV10_XSCOM_PIB_SPIC_BASE +
+        s->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);
+    s_offset = fdt_add_subnode(fdt, offset, name);
+    _FDT(s_offset);
+
+    _FDT(fdt_setprop(fdt, s_offset, "reg", reg, sizeof(reg)));
+    _FDT(fdt_setprop(fdt, s_offset, "compatible", compat, 

sizeof(compat)));
+    _FDT((fdt_setprop_cell(fdt, s_offset, "spic_num#", s->spic_num))); +    return 0;

hw/ppc/pnv_spi_controller.c new file +3 -0
+}
+
+static void pnv_spi_controller_class_init(ObjectClass *klass, void 

*data)

hw/ppc/pnv_spi_controller.c new file +28 -0
+{
+    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_SYS_BUS_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/Kconfig +0 -0
index 78f83e78ce..6f9670b377 100644
@@ -39,6 +39,7 @@ config POWERNV

      select PCI_POWERNV
      select PCA9552
      select PCA9554
+    select SSI
    config PPC405
      bool

hw/ppc/meson.build +1 -0
index d096636ee7..68fadbae7b 100644
@@ -56,6 +56,7 @@ ppc_ss.add(when: 'CONFIG_POWERNV', if_true: files(
     'pnv_pnor.c',
     'pnv_nest_pervasive.c',
     'pnv_n1_chiplet.c',
+  'pnv_spi_controller.c',
   ))
   # PowerPC 4xx boards

  ppc_ss.add(when: 'CONFIG_PPC405', if_true: files(

hw/ppc/trace-events +0 -0
index bf29bbfd4b..b8e494ffc5 100644
@@ -110,6 +110,12 @@ pnv_sbe_cmd_timer_stop(void) ""

  pnv_sbe_cmd_timer_expired(void) ""
  pnv_sbe_msg_recv(uint16_t cmd, uint16_t seq, uint16_t ctrl_flags) "cmd 0x%" PRIx16 " seq %"PRIu16 " ctrl_flags 0x%" PRIx16 +#pnv_spi_controller.c
+pnv_spi_read(uint64_t addr, uint64_t val) "addr 0x%" PRIx64 " val
0x%" PRIx64
+pnv_spi_write(uint64_t addr, uint64_t val) "addr 0x%" PRIx64 " val
0x%" PRIx64

hw/ppc/trace-events +2 -0
+pnv_spi_read_RDR(uint64_t val) "data extracted = 0x%" PRIx64
+pnv_spi_write_TDR(uint64_t val) "being written, data written = 0x%" 

PRIx64
+

  # ppc.c
  ppc_tb_adjust(uint64_t offs1, uint64_t offs2, int64_t diff, int64_t seconds) "adjusted from 0x%"PRIx64" to 0x%"PRIx64", diff %"PRId64" (%"PRId64"s)"
  ppc_tb_load(uint64_t tb) "tb 0x%016" PRIx64
#6 lore raw

Re: [PATCH v3 1/5] ppc/pnv: Add SPI controller model

Quoted 48 lines · Chalapathi V
Quoted 18 lines · Cédric Le Goater
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 8 lines

An existing QEMU SSI framework is used and SSI_BUS is created.

---
  include/hw/ppc/pnv_xscom.h    |   3 + include/hw/ssi/pnv_spi.h      |  44 +++++++ include/hw/ssi/pnv_spi_regs.h | 114 +++++++++++++++++ hw/ppc/pnv_spi_controller.c   | 228 ++++++++++++++++++++++++++++++++++

Quoted 3 lines

The file names are not consistent.

Please rename hw/ppc/pnv_spi_controller.c to /hw/ssi/pnv_spi.c.

Hello Cedric,

I could not compile hw/ssi/pnv_spi.c because of target specific code.

/FAILED: libcommon.fa.p/hw_ssi_pnv_spi.c.o
cc -m64 -mcx16 -Ilibcommon.fa.p -I/usr/include/p11-kit-1 -I/usr/include/pixman-1 -I/usr/include/glib-2.0 -I/usr/lib64/glib-2.0/include -I/usr/include/gio-unix-2.0/ -I/usr/include/slirp -fdiagnostics-color=auto -Wall -Winvalid-pch -Werror -std=gnu11 -O2 -g -fstack-protector-strong -Wundef -Wwrite-strings -Wmissing-prototypes -Wstrict-prototypes -Wredundant-decls -Wold-style-declaration -Wold-style-definition -Wtype-limits -Wformat-security -Wformat-y2k -Winit-self -Wignored-qualifiers -Wempty-body -Wnested-externs -Wendif-labels -Wexpansion-to-defined -Wimplicit-fallthrough=2 -Wmissing-format-attribute -Wno-missing-include-dirs -Wno-shift-negative-value -Wno-psabi -isystem <qemu_dir>/linux-headers -isystem linux-headers -iquote . -iquote <qemu_dir> -iquote <<qemu_dir>>/include -iquote <qemu_dir>/host/include/x86_64 -iquote <qemu_dir>/host/include/generic -iquote <qemu_dir>/tcg/i386 -pthread -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -fno-strict-aliasing -fno-common -fwrapv -fPIE -MD -MQ libcommon.fa.p/hw_ssi_pnv_spi.c.o -MF libcommon.fa.p/hw_ssi_pnv_spi.c.o.d -o libcommon.fa.p/hw_ssi_pnv_spi.c.o -c ../hw/ssi/pnv_spi.c
In file included from <qemu_dir>/target/ppc/cpu.h:25,
from <qemu_dir>/include/hw/ppc/pnv.h:24,
from <qemu_dir>/include/hw/ppc/pnv_xscom.h:24,
                 from ../hw/ssi/pnv_spi.c:12:
<qemu_dir>/include/exec/cpu-defs.h:23:2: error: #error cpu.h included from common code
 #error cpu.h included from common code
  ^~~~~
In file included from /<qemu_dir>/target/ppc/cpu.h:25,
from <qemu_dir>/include/hw/ppc/pnv.h:24,
from <qemu_dir>/include/hw/ppc/pnv_xscom.h:24,
                 from ../hw/ssi/pnv_spi.c:12:
<qemu_dir>/include/exec/cpu-defs.h:34:10: fatal error: cpu-param.h: No such file or directory
 #include "cpu-param.h"
          ^~~~~~~~~~~~~
compilation terminated.
/

Quoted 1 line

Hence shall I keep pnv_spi.c in /hw/ppc/ ?

Can't we "fix" pnv_xscom.h to be more friendly ? with forward declarations of the Pnv* types ?

Thanks,

C.

#7 lore raw

Re: [PATCH v3 1/5] ppc/pnv: Add SPI controller model

Quoted 73 lines · Cédric Le Goater
Quoted 65 lines · Chalapathi V
Quoted 19 lines · Cédric Le Goater
Quoted 10 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.

An existing QEMU SSI framework is used and SSI_BUS is created.

Quoted 7 lines

---
  include/hw/ppc/pnv_xscom.h    |   3 + include/hw/ssi/pnv_spi.h      |  44 +++++++ include/hw/ssi/pnv_spi_regs.h | 114 +++++++++++++++++ hw/ppc/pnv_spi_controller.c   | 228
++++++++++++++++++++++++++++++++++

Quoted 3 lines

The file names are not consistent.

Please rename hw/ppc/pnv_spi_controller.c to /hw/ssi/pnv_spi.c.

Hello Cedric,

I could not compile hw/ssi/pnv_spi.c because of target specific code.

/FAILED: libcommon.fa.p/hw_ssi_pnv_spi.c.o
cc -m64 -mcx16 -Ilibcommon.fa.p -I/usr/include/p11-kit-1

(quoted diff) +0 -11
-I/usr/include/pixman-1 -I/usr/include/glib-2.0 
-I/usr/lib64/glib-2.0/include -I/usr/include/gio-unix-2.0/ 
-I/usr/include/slirp -fdiagnostics-color=auto -Wall -Winvalid-pch 
-Werror -std=gnu11 -O2 -g -fstack-protector-strong -Wundef 
-Wwrite-strings -Wmissing-prototypes -Wstrict-prototypes 
-Wredundant-decls -Wold-style-declaration -Wold-style-definition 
-Wtype-limits -Wformat-security -Wformat-y2k -Winit-self 
-Wignored-qualifiers -Wempty-body -Wnested-externs -Wendif-labels 
-Wexpansion-to-defined -Wimplicit-fallthrough=2 
-Wmissing-format-attribute -Wno-missing-include-dirs 
-Wno-shift-negative-value -Wno-psabi -isystem 

<qemu_dir>/linux-headers -isystem linux-headers -iquote . -iquote <qemu_dir> -iquote <<qemu_dir>>/include

(quoted diff) +0 -4
-iquote <qemu_dir>/host/include/x86_64 
-iquote <qemu_dir>/host/include/generic -iquote <qemu_dir>/tcg/i386 
-pthread -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE 
-fno-strict-aliasing -fno-common -fwrapv -fPIE -MD -MQ 

libcommon.fa.p/hw_ssi_pnv_spi.c.o -MF libcommon.fa.p/hw_ssi_pnv_spi.c.o.d -o libcommon.fa.p/hw_ssi_pnv_spi.c.o -c ../hw/ssi/pnv_spi.c In file included from <qemu_dir>/target/ppc/cpu.h:25,
from <qemu_dir>/include/hw/ppc/pnv.h:24,
from <qemu_dir>/include/hw/ppc/pnv_xscom.h:24,
                  from ../hw/ssi/pnv_spi.c:12:
<qemu_dir>/include/exec/cpu-defs.h:23:2: error: #error cpu.h included from common code
  #error cpu.h included from common code
   ^~~~~
In file included from /<qemu_dir>/target/ppc/cpu.h:25,
from <qemu_dir>/include/hw/ppc/pnv.h:24,
from <qemu_dir>/include/hw/ppc/pnv_xscom.h:24,
                  from ../hw/ssi/pnv_spi.c:12:
<qemu_dir>/include/exec/cpu-defs.h:34:10: fatal error: cpu-param.h: No such file or directory
  #include "cpu-param.h"
           ^~~~~~~~~~~~~
compilation terminated.
/

Quoted 1 line

Hence shall I keep pnv_spi.c in /hw/ppc/ ?

Can't we "fix" pnv_xscom.h to be more friendly ? with forward declarations of the Pnv* types ?

Quoted 3 lines

Thanks,

C.

Sure, Thank you for the suggestions, I will test and update the patch ASAP.

Thank You,

Chalapathi

#8 lore raw

PATCH v3 2/5 ppc/pnv: Extend SPI model

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/ssi/pnv_spi.h    |   28 +
 hw/ppc/pnv_spi_controller.c | 1074 +++++++++++++++++++++++++++++++++++
 hw/ppc/trace-events         |   15 +
 3 files changed, 1117 insertions(+)
include/hw/ssi/pnv_spi.h +28 -0
index 244ee1cfc0..6e2bceab3b 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.
  */
 #include "hw/ssi/ssi.h"
 
@@ -29,6 +37,25 @@ typedef struct PnvSpiController {
     MemoryRegion    xscom_spic_regs;
     /* SPI controller object number */
     uint32_t        spic_num;
+    uint8_t         transfer_len;
+    uint8_t         responder_select;
+    /* To verify if shift_n1 happens prior to shift_n2 */
+    bool            shift_n1_done;
+    /* 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;
 
     /* SPI Controller registers */
     uint64_t        error_reg;
@@ -40,5 +67,6 @@ typedef struct PnvSpiController {
     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 */
hw/ppc/pnv_spi_controller.c +1074 -0

1,104 lines not on this page · raw message

hw/ppc/trace-events +15 -0
index b8e494ffc5..4b08311160 100644
@@ -115,6 +115,21 @@ pnv_spi_read(uint64_t addr, uint64_t val) "addr 0x%" PRIx64 " val 0x%" PRIx64
 pnv_spi_write(uint64_t addr, uint64_t val) "addr 0x%" PRIx64 " val 0x%" PRIx64
 pnv_spi_read_RDR(uint64_t val) "data extracted = 0x%" PRIx64
 pnv_spi_write_TDR(uint64_t val) "being written, data written = 0x%" PRIx64
+pnv_spi_start_sequencer(void) ""
+pnv_spi_reset(void) "spic engine sequencer configuration and spi communication"
+pnv_spi_sequencer_op(const char* op, uint8_t index) "%s at index = 0x%x"
+pnv_spi_shifter_stating(void) "pull CS line low"
+pnv_spi_shifter_done(void) "pull the CS line high"
+pnv_spi_log_Ncounts(uint8_t N1_bits, uint8_t N1_bytes, uint8_t N1_tx, uint8_t N1_rx, uint8_t N2_bits, uint8_t N2_bytes, uint8_t N2_tx, uint8_t N2_rx) "N1_bits = %d, N1_bytes = %d, N1_tx = %d, N1_rx = %d, N2_bits = %d, N2_bytes = %d, N2_tx = %d, N2_rx = %d"
+pnv_spi_tx_append(const char* frame, uint8_t byte, uint8_t tdr_index) "%s = 0x%2.2x to payload from TDR at index %d"
+pnv_spi_tx_append_FF(const char* frame) "%s to Payload"
+pnv_spi_tx_request(const char* frame, uint32_t payload_len) "%s, payload len = %d"
+pnv_spi_rx_received(uint32_t payload_len) "payload len = %d"
+pnv_spi_rx_read_N1frame(void) ""
+pnv_spi_rx_read_N2frame(void) ""
+pnv_spi_shift_rx(uint8_t byte, uint32_t index) "byte = 0x%2.2x into RDR from payload index %d"
+pnv_spi_sequencer_stop_requested(const char* reason) "due to %s"
+pnv_spi_RDR_match(const char* result) "%s"
 
 # ppc.c
 ppc_tb_adjust(uint64_t offs1, uint64_t offs2, int64_t diff, int64_t seconds) "adjusted from 0x%"PRIx64" to 0x%"PRIx64", diff %"PRId64" (%"PRId64"s)"
Signature · 1 line
2.39.3
#9 lore raw

PATCH v3 3/5 hw/block: Add Microchip's 25CSM04 to m25p80

In reply to Chalapathi V

Add Microchip's 25CSM04 Serial EEPROM to m25p80. 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.

---
 hw/block/m25p80.c | 3 +++
 hw/ppc/Kconfig    | 1 +
 2 files changed, 4 insertions(+)
hw/block/m25p80.c +3 -0
index 8dec134832..824a6c5c60 100644
@@ -357,6 +357,9 @@ static const FlashPartInfo known_devices[] = {
       .sfdp_read = m25p80_sfdp_w25q512jv },
     { INFO("w25q01jvq",   0xef4021,      0,  64 << 10, 2048, ER_4K),
       .sfdp_read = m25p80_sfdp_w25q01jvq },
+
+    /* Microchip */
+    { INFO("25csm04",      0x29cc00,      0x100,  64 << 10,  8, 0) },
 };
 
 typedef enum {
hw/ppc/Kconfig +1 -0
index 6f9670b377..a93430b734 100644
@@ -40,6 +40,7 @@ config POWERNV
     select PCA9552
     select PCA9554
     select SSI
+    select SSI_M25P80
 
 config PPC405
     bool
Signature · 1 line
2.39.3
Reviewed-by on v3 3/5
#10 lore raw

Re: [PATCH v3 3/5] hw/block: Add Microchip's 25CSM04 to m25p80

Quoted 7 lines · Chalapathi V

Add Microchip's 25CSM04 Serial EEPROM to m25p80. 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.

Thanks,

C.

Quoted 31 lines
---
  hw/block/m25p80.c | 3 +++
  hw/ppc/Kconfig    | 1 +
  2 files changed, 4 insertions(+)
hw/block/m25p80.c +3 -0
index 8dec134832..824a6c5c60 100644
@@ -357,6 +357,9 @@ static const FlashPartInfo known_devices[] = {
        .sfdp_read = m25p80_sfdp_w25q512jv },
      { INFO("w25q01jvq",   0xef4021,      0,  64 << 10, 2048, ER_4K),
        .sfdp_read = m25p80_sfdp_w25q01jvq },
+
+    /* Microchip */
+    { INFO("25csm04",      0x29cc00,      0x100,  64 << 10,  8, 0) },
  };
 
  typedef enum {
hw/ppc/Kconfig +1 -0
index 6f9670b377..a93430b734 100644
@@ -40,6 +40,7 @@ config POWERNV
      select PCA9552
      select PCA9554
      select SSI
+    select SSI_M25P80
 
  config PPC405
      bool
#11 lore raw

PATCH v3 4/5 hw/ppc: SPI controller wiring to P10 chip

In reply to Chalapathi V

In this commit, create SPI controller on p10 chip and connect cs irq.

The QOM tree of spi controller and seeprom are.
/machine (powernv10-machine)
/chip[0] (power10_v2.0-pnv-chip)

    /pib_spic[2] (pnv-spi-controller)
      /pnv-spi-bus.2 (SSI)
      /xscom-spi-controller-regs[0] (memory-region)

/machine (powernv10-machine)
/peripheral-anon (container)

    /device[0] (25csm04)
      /WP#[0] (irq)
      /ssi-gpio-cs[0] (irq)

(qemu) qom-get /machine/peripheral-anon /device[76] "parent_bus" "/machine/chip[0]/pib_spic[2]/pnv-spi-bus.2"

---
 include/hw/ppc/pnv_chip.h   |  3 +++
 hw/ppc/pnv.c                | 21 ++++++++++++++++++++-
 hw/ppc/pnv_spi_controller.c |  8 ++++++++
 3 files changed, 31 insertions(+), 1 deletion(-)
include/hw/ppc/pnv_chip.h +3 -0
index 8589f3291e..d464858f79 100644
@@ -6,6 +6,7 @@
 #include "hw/ppc/pnv_core.h"
 #include "hw/ppc/pnv_homer.h"
 #include "hw/ppc/pnv_n1_chiplet.h"
+#include "hw/ssi/pnv_spi.h"
 #include "hw/ppc/pnv_lpc.h"
 #include "hw/ppc/pnv_occ.h"
 #include "hw/ppc/pnv_psi.h"
@@ -118,6 +119,8 @@ struct Pnv10Chip {
     PnvSBE       sbe;
     PnvHomer     homer;
     PnvN1Chiplet     n1_chiplet;
+#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 +20 -1
index 6e3a5ccdec..6850592a85 100644
@@ -1829,6 +1829,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)
@@ -2043,7 +2048,21 @@ 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);
+        /* pib_spic[2] connected to 25csm04 which implements 1 byte transfer */
+        object_property_set_int(OBJECT(&chip10->pib_spic[i]), "transfer_len",
+                                (i == 2) ? 1 : 4, &error_fatal);
+        if (!sysbus_realize(SYS_BUS_DEVICE(OBJECT
+                                        (&chip10->pib_spic[i])), 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);
+    }
 }
 
 static void pnv_rainier_i2c_init(PnvMachineState *pnv)
hw/ppc/pnv_spi_controller.c +8 -0
index e87f583074..3d47e932de 100644
@@ -1067,9 +1067,17 @@ static void operation_sequencer(PnvSpiController *s)
 static void do_reset(DeviceState *dev)
 {
     PnvSpiController *s = PNV_SPICONTROLLER(dev);
+    DeviceState *ssi_dev;
 
     trace_pnv_spi_reset();
 
+    /* Connect cs irq */
+    ssi_dev = ssi_get_cs(s->ssi_bus, 0);
+    if (ssi_dev) {
+        qemu_irq cs_line = qdev_get_gpio_in_named(ssi_dev, SSI_GPIO_CS, 0);
+        qdev_connect_gpio_out_named(DEVICE(s), "cs", 0, cs_line);
+    }
+
     /* Reset all N1 and N2 counters, and other constants */
     s->N2_bits = 0;
     s->N2_bytes = 0;
Signature · 1 line
2.39.3
Reviewed-by on v3 4/5
#12 lore raw

Re: [PATCH v3 4/5] hw/ppc: SPI controller wiring to P10 chip

+ Phil

Quoted 97 lines · Chalapathi V

In this commit, create SPI controller on p10 chip and connect cs irq.

The QOM tree of spi controller and seeprom are.
/machine (powernv10-machine)
/chip[0] (power10_v2.0-pnv-chip)

     /pib_spic[2] (pnv-spi-controller)
       /pnv-spi-bus.2 (SSI)
       /xscom-spi-controller-regs[0] (memory-region)

/machine (powernv10-machine)
/peripheral-anon (container)

     /device[0] (25csm04)
       /WP#[0] (irq)
       /ssi-gpio-cs[0] (irq)

(qemu) qom-get /machine/peripheral-anon /device[76] "parent_bus" "/machine/chip[0]/pib_spic[2]/pnv-spi-bus.2"

---
  include/hw/ppc/pnv_chip.h   |  3 +++
  hw/ppc/pnv.c                | 21 ++++++++++++++++++++-
  hw/ppc/pnv_spi_controller.c |  8 ++++++++
  3 files changed, 31 insertions(+), 1 deletion(-)
include/hw/ppc/pnv_chip.h +3 -0
index 8589f3291e..d464858f79 100644
@@ -6,6 +6,7 @@
  #include "hw/ppc/pnv_core.h"
  #include "hw/ppc/pnv_homer.h"
  #include "hw/ppc/pnv_n1_chiplet.h"
+#include "hw/ssi/pnv_spi.h"
  #include "hw/ppc/pnv_lpc.h"
  #include "hw/ppc/pnv_occ.h"
  #include "hw/ppc/pnv_psi.h"
@@ -118,6 +119,8 @@ struct Pnv10Chip {
      PnvSBE       sbe;
      PnvHomer     homer;
      PnvN1Chiplet     n1_chiplet;
+#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 +20 -1
index 6e3a5ccdec..6850592a85 100644
@@ -1829,6 +1829,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)
@@ -2043,7 +2048,21 @@ 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);
+        /* pib_spic[2] connected to 25csm04 which implements 1 byte transfer */
+        object_property_set_int(OBJECT(&chip10->pib_spic[i]), "transfer_len",
+                                (i == 2) ? 1 : 4, &error_fatal);
+        if (!sysbus_realize(SYS_BUS_DEVICE(OBJECT
+                                        (&chip10->pib_spic[i])), 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);
+    }
  }
 
  static void pnv_rainier_i2c_init(PnvMachineState *pnv)
hw/ppc/pnv_spi_controller.c +1 -0
index e87f583074..3d47e932de 100644
@@ -1067,9 +1067,17 @@ static void operation_sequencer(PnvSpiController *s)
  static void do_reset(DeviceState *dev)
  {
      PnvSpiController *s = PNV_SPICONTROLLER(dev);
+    DeviceState *ssi_dev;
 
      trace_pnv_spi_reset();
 

hw/ppc/pnv_spi_controller.c static void operation_sequencer(PnvSpiController *s) · line 1083

Quoted 10 lines
hw/ppc/pnv_spi_controller.c +7 -0
+    /* Connect cs irq */
+    ssi_dev = ssi_get_cs(s->ssi_bus, 0);
+    if (ssi_dev) {
+        qemu_irq cs_line = qdev_get_gpio_in_named(ssi_dev, SSI_GPIO_CS, 0);
+        qdev_connect_gpio_out_named(DEVICE(s), "cs", 0, cs_line);
+    }
+
      /* Reset all N1 and N2 counters, and other constants */
      s->N2_bits = 0;
      s->N2_bytes = 0;

Thanks,

C.

#13 lore raw

PATCH v3 5/5 tests/qtest: Add pnv-spi-seeprom qtest

In reply to Chalapathi V

In this commit Write a qtest pnv-spi-seeprom-test to check the SPI transactions between spi controller and seeprom device.

---
 tests/qtest/pnv-spi-seeprom-test.c | 129 +++++++++++++++++++++++++++++
 tests/qtest/meson.build            |   1 +
 2 files changed, 130 insertions(+)
 create mode 100644 tests/qtest/pnv-spi-seeprom-test.c
tests/qtest/pnv-spi-seeprom-test.c new file +129 -0
new file mode 100644
index 0000000000..bfa57f3234
@@ -0,0 +1,129 @@
+/*
+ * QTest testcase for PowerNV 10 Seeprom Communications
+ *
+ * Copyright (c) 2024, IBM Corporation.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#include <unistd.h>
+#include "qemu/osdep.h"
+#include "libqtest.h"
+#include "qemu/bswap.h"
+#include "hw/ssi/pnv_spi_regs.h"
+
+#define P10_XSCOM_BASE          0x000603fc00000000ull
+#define SPIC2_XSCOM_BASE        0xc0040
+
+/* To transmit READ opcode and address */
+#define READ_OP_TDR_DATA        0x0300010000000000
+/*
+ * N1 shift - tx 4 bytes (transmit opcode and address)
+ * N2 shift - tx and rx 8 bytes.
+ */
+#define READ_OP_COUNTER_CONFIG  0x2040000000002b00
+/* SEQ_OP_SELECT_RESPONDER - N1 Shift - N2 Shift * 5 - SEQ_OP_STOP */
+#define READ_OP_SEQUENCER       0x1130404040404010
+
+/* To transmit WREN(Set Write Enable Latch in status0 register) opcode */
+#define WRITE_OP_WREN           0x0600000000000000
+/* To transmit WRITE opcode, address and data */
+#define WRITE_OP_TDR_DATA       0x0300010012345678
+/* N1 shift - tx 8 bytes (transmit opcode, address and data) */
+#define WRITE_OP_COUNTER_CONFIG 0x4000000000002000
+/* SEQ_OP_SELECT_RESPONDER - N1 Shift - SEQ_OP_STOP */
+#define WRITE_OP_SEQUENCER      0x1130100000000000
+
+static uint64_t pnv_xscom_addr(uint32_t pcba)
+{
+    return P10_XSCOM_BASE | ((uint64_t) pcba << 3);
+}
+
+static uint64_t pnv_spi_seeprom_xscom_addr(uint32_t reg)
+{
+    return pnv_xscom_addr(SPIC2_XSCOM_BASE + reg);
+}
+
+static void pnv_spi_controller_xscom_write(QTestState *qts, uint32_t reg,
+                uint64_t val)
+{
+    qtest_writeq(qts, pnv_spi_seeprom_xscom_addr(reg), val);
+}
+
+static uint64_t pnv_spi_controller_xscom_read(QTestState *qts, uint32_t reg)
+{
+    return qtest_readq(qts, pnv_spi_seeprom_xscom_addr(reg));
+}
+
+static void spi_seeprom_transaction(QTestState *qts)
+{
+    /* SPI transactions to SEEPROM to read from SEEPROM image */
+    pnv_spi_controller_xscom_write(qts, COUNTER_CONFIG_REG,
+                                    READ_OP_COUNTER_CONFIG);
+    pnv_spi_controller_xscom_write(qts, SEQUENCER_OPERATION_REG,
+                                    READ_OP_SEQUENCER);
+    pnv_spi_controller_xscom_write(qts, TRANSMIT_DATA_REG, READ_OP_TDR_DATA);
+    pnv_spi_controller_xscom_write(qts, TRANSMIT_DATA_REG, 0);
+    /* Read 5*8 bytes from SEEPROM at 0x100 */
+    uint64_t rdr_val = pnv_spi_controller_xscom_read(qts, RECEIVE_DATA_REG);
+    printf("RDR READ = 0x%lx\n", rdr_val);
+    rdr_val = pnv_spi_controller_xscom_read(qts, RECEIVE_DATA_REG);
+    rdr_val = pnv_spi_controller_xscom_read(qts, RECEIVE_DATA_REG);
+    rdr_val = pnv_spi_controller_xscom_read(qts, RECEIVE_DATA_REG);
+    rdr_val = pnv_spi_controller_xscom_read(qts, RECEIVE_DATA_REG);
+    printf("RDR READ = 0x%lx\n", rdr_val);
+
+    /* SPI transactions to SEEPROM to write to SEEPROM image */
+    pnv_spi_controller_xscom_write(qts, COUNTER_CONFIG_REG,
+                                    WRITE_OP_COUNTER_CONFIG);
+    /* Set Write Enable Latch bit of status0 register */
+    pnv_spi_controller_xscom_write(qts, SEQUENCER_OPERATION_REG,
+                                    WRITE_OP_SEQUENCER);
+    pnv_spi_controller_xscom_write(qts, TRANSMIT_DATA_REG, WRITE_OP_WREN);
+    /* write 8 bytes to SEEPROM at 0x100 */
+    pnv_spi_controller_xscom_write(qts, SEQUENCER_OPERATION_REG,
+                                    WRITE_OP_SEQUENCER);
+    pnv_spi_controller_xscom_write(qts, TRANSMIT_DATA_REG, WRITE_OP_TDR_DATA);
+}
+
+/* Find complete path of in_file in the current working directory */
+static void find_file(const char *in_file, char *in_path)
+{
+    g_autofree char *cwd = g_get_current_dir();
+    char *filepath = g_build_filename(cwd, in_file, NULL);
+    if (!access(filepath, F_OK)) {
+        strcpy(in_path, filepath);
+    } else {
+        strcpy(in_path, "");
+        printf("File %s not found within %s\n", in_file, cwd);
+    }
+}
+
+static void test_spi_seeprom(void)
+{
+    QTestState *qts = NULL;
+    char seepromfile[500];
+    find_file("sbe_measurement_seeprom.bin.ecc", seepromfile);
+    if (strcmp(seepromfile, "")) {
+        printf("Starting QEMU with seeprom file.\n");
+        qts = qtest_initf("-m 2G -machine powernv10 -smp 2,cores=2,"
+                          "threads=1 -accel tcg,thread=single -nographic "
+                          "-blockdev node-name=pib_spic2,driver=file,"
+			  "filename=sbe_measurement_seeprom.bin.ecc "
+			  "-device 25csm04,bus=pnv-spi-bus.2,cs=0,"
+			  "drive=pib_spic2");
+    } else {
+        printf("Starting QEMU without seeprom file.\n");
+        qts = qtest_initf("-m 2G -machine powernv10 -smp 2,cores=2,"
+                          "threads=1 -accel tcg,thread=single -nographic"
+			  " -device 25csm04,bus=pnv-spi-bus.2,cs=0");
+    }
+    spi_seeprom_transaction(qts);
+    qtest_quit(qts);
+}
+
+int main(int argc, char **argv)
+{
+    g_test_init(&argc, &argv, NULL);
+    qtest_add_func("spi_seeprom", test_spi_seeprom);
+    return g_test_run();
+}
tests/qtest/meson.build +1 -0
index 86293051dc..2fa98b2430 100644
@@ -171,6 +171,7 @@ qtests_ppc64 = \
   qtests_ppc + \
   (config_all_devices.has_key('CONFIG_PSERIES') ? ['device-plug-test'] : []) +               \
   (config_all_devices.has_key('CONFIG_POWERNV') ? ['pnv-xscom-test'] : []) +                 \
+  (config_all_devices.has_key('CONFIG_POWERNV') ? ['pnv-spi-seeprom-test'] : []) +           \
   (config_all_devices.has_key('CONFIG_POWERNV') ? ['pnv-host-i2c-test'] : []) +              \
   (config_all_devices.has_key('CONFIG_PSERIES') ? ['rtas-test'] : []) +                      \
   (slirp.found() ? ['pxe-test'] : []) +              \
Signature · 1 line
2.39.3
#14 lore raw

Re: [PATCH v3 5/5] tests/qtest: Add pnv-spi-seeprom qtest

Quoted 45 lines · Chalapathi V

In this commit Write a qtest pnv-spi-seeprom-test to check the SPI transactions between spi controller and seeprom device.

---
  tests/qtest/pnv-spi-seeprom-test.c | 129 +++++++++++++++++++++++++++++
  tests/qtest/meson.build            |   1 +
  2 files changed, 130 insertions(+)

create mode 100644 tests/qtest/pnv-spi-seeprom-test.c

tests/qtest/pnv-spi-seeprom-test.c new file +29 -0
new file mode 100644
index 0000000000..bfa57f3234
@@ -0,0 +1,129 @@
+/*
+ * QTest testcase for PowerNV 10 Seeprom Communications
+ *
+ * Copyright (c) 2024, IBM Corporation.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#include <unistd.h>
+#include "qemu/osdep.h"
+#include "libqtest.h"
+#include "qemu/bswap.h"
+#include "hw/ssi/pnv_spi_regs.h"
+
+#define P10_XSCOM_BASE          0x000603fc00000000ull
+#define SPIC2_XSCOM_BASE        0xc0040
+
+/* To transmit READ opcode and address */
+#define READ_OP_TDR_DATA        0x0300010000000000
+/*
+ * N1 shift - tx 4 bytes (transmit opcode and address)
+ * N2 shift - tx and rx 8 bytes.
+ */
+#define READ_OP_COUNTER_CONFIG  0x2040000000002b00
+/* SEQ_OP_SELECT_RESPONDER - N1 Shift - N2 Shift * 5 - SEQ_OP_STOP */
+#define READ_OP_SEQUENCER       0x1130404040404010
+
+/* To transmit WREN(Set Write Enable Latch in status0 register) opcode */
+#define WRITE_OP_WREN           0x0600000000000000
+/* To transmit WRITE opcode, address and data */

tests/qtest/pnv-spi-seeprom-test.c line 39

Quoted 10 lines
tests/qtest/pnv-spi-seeprom-test.c new file +10 -0
+#define WRITE_OP_TDR_DATA       0x0300010012345678
+/* N1 shift - tx 8 bytes (transmit opcode, address and data) */
+#define WRITE_OP_COUNTER_CONFIG 0x4000000000002000
+/* SEQ_OP_SELECT_RESPONDER - N1 Shift - SEQ_OP_STOP */
+#define WRITE_OP_SEQUENCER      0x1130100000000000
+
+static uint64_t pnv_xscom_addr(uint32_t pcba)
+{
+    return P10_XSCOM_BASE | ((uint64_t) pcba << 3);
+}

I would prefer if the test used the pnv_xscom_addr() definition from tests/qtest/pnv-xscom.h.

Quoted 55 lines
tests/qtest/pnv-spi-seeprom-test.c new file +55 -0
+static uint64_t pnv_spi_seeprom_xscom_addr(uint32_t reg)
+{
+    return pnv_xscom_addr(SPIC2_XSCOM_BASE + reg);
+}
+
+static void pnv_spi_controller_xscom_write(QTestState *qts, uint32_t reg,
+                uint64_t val)
+{
+    qtest_writeq(qts, pnv_spi_seeprom_xscom_addr(reg), val);
+}
+
+static uint64_t pnv_spi_controller_xscom_read(QTestState *qts, uint32_t reg)
+{
+    return qtest_readq(qts, pnv_spi_seeprom_xscom_addr(reg));
+}
+
+static void spi_seeprom_transaction(QTestState *qts)
+{
+    /* SPI transactions to SEEPROM to read from SEEPROM image */
+    pnv_spi_controller_xscom_write(qts, COUNTER_CONFIG_REG,
+                                    READ_OP_COUNTER_CONFIG);
+    pnv_spi_controller_xscom_write(qts, SEQUENCER_OPERATION_REG,
+                                    READ_OP_SEQUENCER);
+    pnv_spi_controller_xscom_write(qts, TRANSMIT_DATA_REG, READ_OP_TDR_DATA);
+    pnv_spi_controller_xscom_write(qts, TRANSMIT_DATA_REG, 0);
+    /* Read 5*8 bytes from SEEPROM at 0x100 */
+    uint64_t rdr_val = pnv_spi_controller_xscom_read(qts, RECEIVE_DATA_REG);
+    printf("RDR READ = 0x%lx\n", rdr_val);
+    rdr_val = pnv_spi_controller_xscom_read(qts, RECEIVE_DATA_REG);
+    rdr_val = pnv_spi_controller_xscom_read(qts, RECEIVE_DATA_REG);
+    rdr_val = pnv_spi_controller_xscom_read(qts, RECEIVE_DATA_REG);
+    rdr_val = pnv_spi_controller_xscom_read(qts, RECEIVE_DATA_REG);
+    printf("RDR READ = 0x%lx\n", rdr_val);
+
+    /* SPI transactions to SEEPROM to write to SEEPROM image */
+    pnv_spi_controller_xscom_write(qts, COUNTER_CONFIG_REG,
+                                    WRITE_OP_COUNTER_CONFIG);
+    /* Set Write Enable Latch bit of status0 register */
+    pnv_spi_controller_xscom_write(qts, SEQUENCER_OPERATION_REG,
+                                    WRITE_OP_SEQUENCER);
+    pnv_spi_controller_xscom_write(qts, TRANSMIT_DATA_REG, WRITE_OP_WREN);
+    /* write 8 bytes to SEEPROM at 0x100 */
+    pnv_spi_controller_xscom_write(qts, SEQUENCER_OPERATION_REG,
+                                    WRITE_OP_SEQUENCER);
+    pnv_spi_controller_xscom_write(qts, TRANSMIT_DATA_REG, WRITE_OP_TDR_DATA);
+}
+
+/* Find complete path of in_file in the current working directory */
+static void find_file(const char *in_file, char *in_path)
+{
+    g_autofree char *cwd = g_get_current_dir();
+    char *filepath = g_build_filename(cwd, in_file, NULL);
+    if (!access(filepath, F_OK)) {
+        strcpy(in_path, filepath);
+    } else {

tests/qtest/pnv-spi-seeprom-test.c line 105

Quoted 10 lines
tests/qtest/pnv-spi-seeprom-test.c new file +10 -0
+        strcpy(in_path, "");
+        printf("File %s not found within %s\n", in_file, cwd);
+    }
+}
+
+static void test_spi_seeprom(void)
+{
+    QTestState *qts = NULL;
+    char seepromfile[500];
+    find_file("sbe_measurement_seeprom.bin.ecc", seepromfile);

hmm, could you generate the contents instead ?

Quoted 36 lines
tests/qtest/pnv-spi-seeprom-test.c new file +24 -0
+    if (strcmp(seepromfile, "")) {
+        printf("Starting QEMU with seeprom file.\n");
+        qts = qtest_initf("-m 2G -machine powernv10 -smp 2,cores=2,"
+                          "threads=1 -accel tcg,thread=single -nographic "
+                          "-blockdev node-name=pib_spic2,driver=file,"
+			  "filename=sbe_measurement_seeprom.bin.ecc "
+			  "-device 25csm04,bus=pnv-spi-bus.2,cs=0,"
+			  "drive=pib_spic2");
+    } else {
+        printf("Starting QEMU without seeprom file.\n");
+        qts = qtest_initf("-m 2G -machine powernv10 -smp 2,cores=2,"
+                          "threads=1 -accel tcg,thread=single -nographic"
+			  " -device 25csm04,bus=pnv-spi-bus.2,cs=0");
+    }
+    spi_seeprom_transaction(qts);
+    qtest_quit(qts);
+}
+
+int main(int argc, char **argv)
+{
+    g_test_init(&argc, &argv, NULL);
+    qtest_add_func("spi_seeprom", test_spi_seeprom);
+    return g_test_run();
+}
tests/qtest/meson.build +1 -0
index 86293051dc..2fa98b2430 100644
@@ -171,6 +171,7 @@ qtests_ppc64 = \
    qtests_ppc + \
    (config_all_devices.has_key('CONFIG_PSERIES') ? ['device-plug-test'] : []) +               \
    (config_all_devices.has_key('CONFIG_POWERNV') ? ['pnv-xscom-test'] : []) +                 \
+  (config_all_devices.has_key('CONFIG_POWERNV') ? ['pnv-spi-seeprom-test'] : []) +           \
    (config_all_devices.has_key('CONFIG_POWERNV') ? ['pnv-host-i2c-test'] : []) +              \
    (config_all_devices.has_key('CONFIG_PSERIES') ? ['rtas-test'] : []) +                      \
    (slirp.found() ? ['pxe-test'] : []) +              \
#15 lore raw

Re: [PATCH v3 5/5] tests/qtest: Add pnv-spi-seeprom qtest

Quoted 53 lines · Cédric Le Goater
Quoted 52 lines · Chalapathi V

In this commit Write a qtest pnv-spi-seeprom-test to check the SPI transactions between spi controller and seeprom device.

---
  tests/qtest/pnv-spi-seeprom-test.c | 129 +++++++++++++++++++++++++++++ tests/qtest/meson.build            |   1 +

  2 files changed, 130 insertions(+)

  create mode 100644 tests/qtest/pnv-spi-seeprom-test.c

tests/qtest/pnv-spi-seeprom-test.c new file +27 -0
new file mode 100644
index 0000000000..bfa57f3234
@@ -0,0 +1,129 @@
+/*
+ * QTest testcase for PowerNV 10 Seeprom Communications
+ *
+ * Copyright (c) 2024, IBM Corporation.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#include <unistd.h>
+#include "qemu/osdep.h"
+#include "libqtest.h"
+#include "qemu/bswap.h"
+#include "hw/ssi/pnv_spi_regs.h"
+
+#define P10_XSCOM_BASE          0x000603fc00000000ull
+#define SPIC2_XSCOM_BASE        0xc0040
+
+/* To transmit READ opcode and address */
+#define READ_OP_TDR_DATA        0x0300010000000000
+/*
+ * N1 shift - tx 4 bytes (transmit opcode and address)
+ * N2 shift - tx and rx 8 bytes.
+ */
+#define READ_OP_COUNTER_CONFIG  0x2040000000002b00
+/* SEQ_OP_SELECT_RESPONDER - N1 Shift - N2 Shift * 5 - SEQ_OP_STOP */
+#define READ_OP_SEQUENCER       0x1130404040404010
+
+/* To transmit WREN(Set Write Enable Latch in status0 register) 

opcode */

tests/qtest/pnv-spi-seeprom-test.c new file +8 -0
+#define WRITE_OP_WREN           0x0600000000000000
+/* To transmit WRITE opcode, address and data */
+#define WRITE_OP_TDR_DATA       0x0300010012345678
+/* N1 shift - tx 8 bytes (transmit opcode, address and data) */
+#define WRITE_OP_COUNTER_CONFIG 0x4000000000002000
+/* SEQ_OP_SELECT_RESPONDER - N1 Shift - SEQ_OP_STOP */
+#define WRITE_OP_SEQUENCER      0x1130100000000000
+
Quoted 8 lines

tests/qtest/pnv-spi-seeprom-test.c line 39

Quoted 4 lines
tests/qtest/pnv-spi-seeprom-test.c new file +4 -0
+static uint64_t pnv_xscom_addr(uint32_t pcba)
+{
+    return P10_XSCOM_BASE | ((uint64_t) pcba << 3);
+}

I would prefer if the test used the pnv_xscom_addr() definition from tests/qtest/pnv-xscom.h.

Sure. Will update. Thank You

Quoted 115 lines
Quoted 62 lines
tests/qtest/pnv-spi-seeprom-test.c new file +6 -0
+static uint64_t pnv_spi_seeprom_xscom_addr(uint32_t reg)
+{
+    return pnv_xscom_addr(SPIC2_XSCOM_BASE + reg);
+}
+
+static void pnv_spi_controller_xscom_write(QTestState *qts, uint32_t 

reg,
+                uint64_t val)

tests/qtest/pnv-spi-seeprom-test.c new file +5 -0
+{
+    qtest_writeq(qts, pnv_spi_seeprom_xscom_addr(reg), val);
+}
+
+static uint64_t pnv_spi_controller_xscom_read(QTestState *qts, 

uint32_t reg)

tests/qtest/pnv-spi-seeprom-test.c new file +12 -0
+{
+    return qtest_readq(qts, pnv_spi_seeprom_xscom_addr(reg));
+}
+
+static void spi_seeprom_transaction(QTestState *qts)
+{
+    /* SPI transactions to SEEPROM to read from SEEPROM image */
+    pnv_spi_controller_xscom_write(qts, COUNTER_CONFIG_REG,
+                                    READ_OP_COUNTER_CONFIG);
+    pnv_spi_controller_xscom_write(qts, SEQUENCER_OPERATION_REG,
+                                    READ_OP_SEQUENCER);
+    pnv_spi_controller_xscom_write(qts, TRANSMIT_DATA_REG, 

READ_OP_TDR_DATA);
+    pnv_spi_controller_xscom_write(qts, TRANSMIT_DATA_REG, 0); +    /* Read 5*8 bytes from SEEPROM at 0x100 */
+    uint64_t rdr_val = pnv_spi_controller_xscom_read(qts, RECEIVE_DATA_REG);
+    printf("RDR READ = 0x%lx\n", rdr_val);
+    rdr_val = pnv_spi_controller_xscom_read(qts, RECEIVE_DATA_REG); +    rdr_val = pnv_spi_controller_xscom_read(qts, RECEIVE_DATA_REG); +    rdr_val = pnv_spi_controller_xscom_read(qts, RECEIVE_DATA_REG); +    rdr_val = pnv_spi_controller_xscom_read(qts, RECEIVE_DATA_REG); +    printf("RDR READ = 0x%lx\n", rdr_val);
+
+    /* SPI transactions to SEEPROM to write to SEEPROM image */ +    pnv_spi_controller_xscom_write(qts, COUNTER_CONFIG_REG, +                                    WRITE_OP_COUNTER_CONFIG); +    /* Set Write Enable Latch bit of status0 register */ +    pnv_spi_controller_xscom_write(qts, SEQUENCER_OPERATION_REG, +                                    WRITE_OP_SEQUENCER); +    pnv_spi_controller_xscom_write(qts, TRANSMIT_DATA_REG, WRITE_OP_WREN);
+    /* write 8 bytes to SEEPROM at 0x100 */
+    pnv_spi_controller_xscom_write(qts, SEQUENCER_OPERATION_REG, +                                    WRITE_OP_SEQUENCER); +    pnv_spi_controller_xscom_write(qts, TRANSMIT_DATA_REG, WRITE_OP_TDR_DATA);

tests/qtest/pnv-spi-seeprom-test.c new file +10 -0
+}
+
+/* Find complete path of in_file in the current working directory */
+static void find_file(const char *in_file, char *in_path)
+{
+    g_autofree char *cwd = g_get_current_dir();
+    char *filepath = g_build_filename(cwd, in_file, NULL);
+    if (!access(filepath, F_OK)) {
+        strcpy(in_path, filepath);
+    } else {

tests/qtest/pnv-spi-seeprom-test.c line 105

Quoted 10 lines
tests/qtest/pnv-spi-seeprom-test.c new file +10 -0
+        strcpy(in_path, "");
+        printf("File %s not found within %s\n", in_file, cwd);
+    }
+}
+
+static void test_spi_seeprom(void)
+{
+    QTestState *qts = NULL;
+    char seepromfile[500];
+    find_file("sbe_measurement_seeprom.bin.ecc", seepromfile);

hmm, could you generate the contents instead ?

Quoted 39 lines

+    if (strcmp(seepromfile, "")) {
+        printf("Starting QEMU with seeprom file.\n");
+        qts = qtest_initf("-m 2G -machine powernv10 -smp 2,cores=2," +                          "threads=1 -accel tcg,thread=single

tests/qtest/pnv-spi-seeprom-test.c new file +20 -2
-nographic "
+                          "-blockdev node-name=pib_spic2,driver=file,"
+              "filename=sbe_measurement_seeprom.bin.ecc "
+              "-device 25csm04,bus=pnv-spi-bus.2,cs=0,"
+              "drive=pib_spic2");
+    } else {
+        printf("Starting QEMU without seeprom file.\n");
+        qts = qtest_initf("-m 2G -machine powernv10 -smp 2,cores=2,"
+                          "threads=1 -accel tcg,thread=single 
-nographic"
+              " -device 25csm04,bus=pnv-spi-bus.2,cs=0");
+    }
+    spi_seeprom_transaction(qts);
+    qtest_quit(qts);
+}
+
+int main(int argc, char **argv)
+{
+    g_test_init(&argc, &argv, NULL);
+    qtest_add_func("spi_seeprom", test_spi_seeprom);
+    return g_test_run();
+}
tests/qtest/meson.build +1 -0
index 86293051dc..2fa98b2430 100644
@@ -171,6 +171,7 @@ qtests_ppc64 = \
     qtests_ppc + \
     (config_all_devices.has_key('CONFIG_PSERIES') ? ['device-plug-test'] : []) +               \
     (config_all_devices.has_key('CONFIG_POWERNV') ? ['pnv-xscom-test'] : []) +                 \
+  (config_all_devices.has_key('CONFIG_POWERNV') ? 
 ['pnv-spi-seeprom-test'] : []) +           \
     (config_all_devices.has_key('CONFIG_POWERNV') ? ['pnv-host-i2c-test'] : []) +              \
     (config_all_devices.has_key('CONFIG_PSERIES') ? ['rtas-test'] : []) +                      \

    (slirp.found() ? ['pxe-test'] : []) +              \