129318db1SChalapathi V /* 229318db1SChalapathi V * QEMU PowerPC SPI model 329318db1SChalapathi V * 429318db1SChalapathi V * Copyright (c) 2024, IBM Corporation. 529318db1SChalapathi V * 629318db1SChalapathi V * SPDX-License-Identifier: GPL-2.0-or-later 729318db1SChalapathi V */ 829318db1SChalapathi V 929318db1SChalapathi V #include "qemu/osdep.h" 1029318db1SChalapathi V #include "qemu/log.h" 1129318db1SChalapathi V #include "hw/qdev-properties.h" 1229318db1SChalapathi V #include "hw/ppc/pnv_xscom.h" 1329318db1SChalapathi V #include "hw/ssi/pnv_spi.h" 1429318db1SChalapathi V #include "hw/ssi/pnv_spi_regs.h" 1529318db1SChalapathi V #include "hw/ssi/ssi.h" 1629318db1SChalapathi V #include <libfdt.h> 1729318db1SChalapathi V #include "hw/irq.h" 1829318db1SChalapathi V #include "trace.h" 1929318db1SChalapathi V 20*b4cb930eSChalapathi V #define PNV_SPI_OPCODE_LO_NIBBLE(x) (x & 0x0F) 21*b4cb930eSChalapathi V #define PNV_SPI_MASKED_OPCODE(x) (x & 0xF0) 22*b4cb930eSChalapathi V 2329318db1SChalapathi V /* 2429318db1SChalapathi V * Macro from include/hw/ppc/fdt.h 2529318db1SChalapathi V * fdt.h cannot be included here as it contain ppc target specific dependency. 2629318db1SChalapathi V */ 2729318db1SChalapathi V #define _FDT(exp) \ 2829318db1SChalapathi V do { \ 2929318db1SChalapathi V int _ret = (exp); \ 3029318db1SChalapathi V if (_ret < 0) { \ 3129318db1SChalapathi V qemu_log_mask(LOG_GUEST_ERROR, \ 3229318db1SChalapathi V "error creating device tree: %s: %s", \ 3329318db1SChalapathi V #exp, fdt_strerror(_ret)); \ 3429318db1SChalapathi V exit(1); \ 3529318db1SChalapathi V } \ 3629318db1SChalapathi V } while (0) 3729318db1SChalapathi V 38*b4cb930eSChalapathi V /* PnvXferBuffer */ 39*b4cb930eSChalapathi V typedef struct PnvXferBuffer { 40*b4cb930eSChalapathi V 41*b4cb930eSChalapathi V uint32_t len; 42*b4cb930eSChalapathi V uint8_t *data; 43*b4cb930eSChalapathi V 44*b4cb930eSChalapathi V } PnvXferBuffer; 45*b4cb930eSChalapathi V 46*b4cb930eSChalapathi V /* pnv_spi_xfer_buffer_methods */ 47*b4cb930eSChalapathi V static PnvXferBuffer *pnv_spi_xfer_buffer_new(void) 48*b4cb930eSChalapathi V { 49*b4cb930eSChalapathi V PnvXferBuffer *payload = g_malloc0(sizeof(*payload)); 50*b4cb930eSChalapathi V 51*b4cb930eSChalapathi V return payload; 52*b4cb930eSChalapathi V } 53*b4cb930eSChalapathi V 54*b4cb930eSChalapathi V static void pnv_spi_xfer_buffer_free(PnvXferBuffer *payload) 55*b4cb930eSChalapathi V { 56*b4cb930eSChalapathi V free(payload->data); 57*b4cb930eSChalapathi V free(payload); 58*b4cb930eSChalapathi V } 59*b4cb930eSChalapathi V 60*b4cb930eSChalapathi V static uint8_t *pnv_spi_xfer_buffer_write_ptr(PnvXferBuffer *payload, 61*b4cb930eSChalapathi V uint32_t offset, uint32_t length) 62*b4cb930eSChalapathi V { 63*b4cb930eSChalapathi V if (payload->len < (offset + length)) { 64*b4cb930eSChalapathi V payload->len = offset + length; 65*b4cb930eSChalapathi V payload->data = g_realloc(payload->data, payload->len); 66*b4cb930eSChalapathi V } 67*b4cb930eSChalapathi V return &payload->data[offset]; 68*b4cb930eSChalapathi V } 69*b4cb930eSChalapathi V 70*b4cb930eSChalapathi V static bool does_rdr_match(PnvSpi *s) 71*b4cb930eSChalapathi V { 72*b4cb930eSChalapathi V /* 73*b4cb930eSChalapathi V * According to spec, the mask bits that are 0 are compared and the 74*b4cb930eSChalapathi V * bits that are 1 are ignored. 75*b4cb930eSChalapathi V */ 76*b4cb930eSChalapathi V uint16_t rdr_match_mask = GETFIELD(SPI_MM_RDR_MATCH_MASK, 77*b4cb930eSChalapathi V s->regs[SPI_MM_REG]); 78*b4cb930eSChalapathi V uint16_t rdr_match_val = GETFIELD(SPI_MM_RDR_MATCH_VAL, 79*b4cb930eSChalapathi V s->regs[SPI_MM_REG]); 80*b4cb930eSChalapathi V 81*b4cb930eSChalapathi V if ((~rdr_match_mask & rdr_match_val) == ((~rdr_match_mask) & 82*b4cb930eSChalapathi V GETFIELD(PPC_BITMASK(48, 63), s->regs[SPI_RCV_DATA_REG]))) { 83*b4cb930eSChalapathi V return true; 84*b4cb930eSChalapathi V } 85*b4cb930eSChalapathi V return false; 86*b4cb930eSChalapathi V } 87*b4cb930eSChalapathi V 88*b4cb930eSChalapathi V static uint8_t get_from_offset(PnvSpi *s, uint8_t offset) 89*b4cb930eSChalapathi V { 90*b4cb930eSChalapathi V uint8_t byte; 91*b4cb930eSChalapathi V 92*b4cb930eSChalapathi V /* 93*b4cb930eSChalapathi V * Offset is an index between 0 and PNV_SPI_REG_SIZE - 1 94*b4cb930eSChalapathi V * Check the offset before using it. 95*b4cb930eSChalapathi V */ 96*b4cb930eSChalapathi V if (offset < PNV_SPI_REG_SIZE) { 97*b4cb930eSChalapathi V byte = (s->regs[SPI_XMIT_DATA_REG] >> (56 - offset * 8)) & 0xFF; 98*b4cb930eSChalapathi V } else { 99*b4cb930eSChalapathi V /* 100*b4cb930eSChalapathi V * Log an error and return a 0xFF since we have to assign something 101*b4cb930eSChalapathi V * to byte before returning. 102*b4cb930eSChalapathi V */ 103*b4cb930eSChalapathi V qemu_log_mask(LOG_GUEST_ERROR, "Invalid offset = %d used to get byte " 104*b4cb930eSChalapathi V "from TDR\n", offset); 105*b4cb930eSChalapathi V byte = 0xff; 106*b4cb930eSChalapathi V } 107*b4cb930eSChalapathi V return byte; 108*b4cb930eSChalapathi V } 109*b4cb930eSChalapathi V 110*b4cb930eSChalapathi V static uint8_t read_from_frame(PnvSpi *s, uint8_t *read_buf, uint8_t nr_bytes, 111*b4cb930eSChalapathi V uint8_t ecc_count, uint8_t shift_in_count) 112*b4cb930eSChalapathi V { 113*b4cb930eSChalapathi V uint8_t byte; 114*b4cb930eSChalapathi V int count = 0; 115*b4cb930eSChalapathi V 116*b4cb930eSChalapathi V while (count < nr_bytes) { 117*b4cb930eSChalapathi V shift_in_count++; 118*b4cb930eSChalapathi V if ((ecc_count != 0) && 119*b4cb930eSChalapathi V (shift_in_count == (PNV_SPI_REG_SIZE + ecc_count))) { 120*b4cb930eSChalapathi V shift_in_count = 0; 121*b4cb930eSChalapathi V } else { 122*b4cb930eSChalapathi V byte = read_buf[count]; 123*b4cb930eSChalapathi V trace_pnv_spi_shift_rx(byte, count); 124*b4cb930eSChalapathi V s->regs[SPI_RCV_DATA_REG] = (s->regs[SPI_RCV_DATA_REG] << 8) | byte; 125*b4cb930eSChalapathi V } 126*b4cb930eSChalapathi V count++; 127*b4cb930eSChalapathi V } /* end of while */ 128*b4cb930eSChalapathi V return shift_in_count; 129*b4cb930eSChalapathi V } 130*b4cb930eSChalapathi V 131*b4cb930eSChalapathi V static void spi_response(PnvSpi *s, int bits, PnvXferBuffer *rsp_payload) 132*b4cb930eSChalapathi V { 133*b4cb930eSChalapathi V uint8_t ecc_count; 134*b4cb930eSChalapathi V uint8_t shift_in_count; 135*b4cb930eSChalapathi V 136*b4cb930eSChalapathi V /* 137*b4cb930eSChalapathi V * Processing here must handle: 138*b4cb930eSChalapathi V * - Which bytes in the payload we should move to the RDR 139*b4cb930eSChalapathi V * - Explicit mode counter configuration settings 140*b4cb930eSChalapathi V * - RDR full and RDR overrun status 141*b4cb930eSChalapathi V */ 142*b4cb930eSChalapathi V 143*b4cb930eSChalapathi V /* 144*b4cb930eSChalapathi V * First check that the response payload is the exact same 145*b4cb930eSChalapathi V * number of bytes as the request payload was 146*b4cb930eSChalapathi V */ 147*b4cb930eSChalapathi V if (rsp_payload->len != (s->N1_bytes + s->N2_bytes)) { 148*b4cb930eSChalapathi V qemu_log_mask(LOG_GUEST_ERROR, "Invalid response payload size in " 149*b4cb930eSChalapathi V "bytes, expected %d, got %d\n", 150*b4cb930eSChalapathi V (s->N1_bytes + s->N2_bytes), rsp_payload->len); 151*b4cb930eSChalapathi V } else { 152*b4cb930eSChalapathi V uint8_t ecc_control; 153*b4cb930eSChalapathi V trace_pnv_spi_rx_received(rsp_payload->len); 154*b4cb930eSChalapathi V trace_pnv_spi_log_Ncounts(s->N1_bits, s->N1_bytes, s->N1_tx, 155*b4cb930eSChalapathi V s->N1_rx, s->N2_bits, s->N2_bytes, s->N2_tx, s->N2_rx); 156*b4cb930eSChalapathi V /* 157*b4cb930eSChalapathi V * Adding an ECC count let's us know when we have found a payload byte 158*b4cb930eSChalapathi V * that was shifted in but cannot be loaded into RDR. Bits 29-30 of 159*b4cb930eSChalapathi V * clock_config_reset_control register equal to either 0b00 or 0b10 160*b4cb930eSChalapathi V * indicate that we are taking in data with ECC and either applying 161*b4cb930eSChalapathi V * the ECC or discarding it. 162*b4cb930eSChalapathi V */ 163*b4cb930eSChalapathi V ecc_count = 0; 164*b4cb930eSChalapathi V ecc_control = GETFIELD(SPI_CLK_CFG_ECC_CTRL, s->regs[SPI_CLK_CFG_REG]); 165*b4cb930eSChalapathi V if (ecc_control == 0 || ecc_control == 2) { 166*b4cb930eSChalapathi V ecc_count = 1; 167*b4cb930eSChalapathi V } 168*b4cb930eSChalapathi V /* 169*b4cb930eSChalapathi V * Use the N1_rx and N2_rx counts to control shifting data from the 170*b4cb930eSChalapathi V * payload into the RDR. Keep an overall count of the number of bytes 171*b4cb930eSChalapathi V * shifted into RDR so we can discard every 9th byte when ECC is 172*b4cb930eSChalapathi V * enabled. 173*b4cb930eSChalapathi V */ 174*b4cb930eSChalapathi V shift_in_count = 0; 175*b4cb930eSChalapathi V /* Handle the N1 portion of the frame first */ 176*b4cb930eSChalapathi V if (s->N1_rx != 0) { 177*b4cb930eSChalapathi V trace_pnv_spi_rx_read_N1frame(); 178*b4cb930eSChalapathi V shift_in_count = read_from_frame(s, &rsp_payload->data[0], 179*b4cb930eSChalapathi V s->N1_bytes, ecc_count, shift_in_count); 180*b4cb930eSChalapathi V } 181*b4cb930eSChalapathi V /* Handle the N2 portion of the frame */ 182*b4cb930eSChalapathi V if (s->N2_rx != 0) { 183*b4cb930eSChalapathi V trace_pnv_spi_rx_read_N2frame(); 184*b4cb930eSChalapathi V shift_in_count = read_from_frame(s, 185*b4cb930eSChalapathi V &rsp_payload->data[s->N1_bytes], s->N2_bytes, 186*b4cb930eSChalapathi V ecc_count, shift_in_count); 187*b4cb930eSChalapathi V } 188*b4cb930eSChalapathi V if ((s->N1_rx + s->N2_rx) > 0) { 189*b4cb930eSChalapathi V /* 190*b4cb930eSChalapathi V * Data was received so handle RDR status. 191*b4cb930eSChalapathi V * It is easier to handle RDR_full and RDR_overrun status here 192*b4cb930eSChalapathi V * since the RDR register's shift_byte_in method is called 193*b4cb930eSChalapathi V * multiple times in a row. Controlling RDR status is done here 194*b4cb930eSChalapathi V * instead of in the RDR scoped methods for that reason. 195*b4cb930eSChalapathi V */ 196*b4cb930eSChalapathi V if (GETFIELD(SPI_STS_RDR_FULL, s->status) == 1) { 197*b4cb930eSChalapathi V /* 198*b4cb930eSChalapathi V * Data was shifted into the RDR before having been read 199*b4cb930eSChalapathi V * causing previous data to have been overrun. 200*b4cb930eSChalapathi V */ 201*b4cb930eSChalapathi V s->status = SETFIELD(SPI_STS_RDR_OVERRUN, s->status, 1); 202*b4cb930eSChalapathi V } else { 203*b4cb930eSChalapathi V /* 204*b4cb930eSChalapathi V * Set status to indicate that the received data register is 205*b4cb930eSChalapathi V * full. This flag is only cleared once the RDR is unloaded. 206*b4cb930eSChalapathi V */ 207*b4cb930eSChalapathi V s->status = SETFIELD(SPI_STS_RDR_FULL, s->status, 1); 208*b4cb930eSChalapathi V } 209*b4cb930eSChalapathi V } 210*b4cb930eSChalapathi V } /* end of else */ 211*b4cb930eSChalapathi V } /* end of spi_response() */ 212*b4cb930eSChalapathi V 213*b4cb930eSChalapathi V static void transfer(PnvSpi *s, PnvXferBuffer *payload) 214*b4cb930eSChalapathi V { 215*b4cb930eSChalapathi V uint32_t tx; 216*b4cb930eSChalapathi V uint32_t rx; 217*b4cb930eSChalapathi V PnvXferBuffer *rsp_payload = NULL; 218*b4cb930eSChalapathi V 219*b4cb930eSChalapathi V rsp_payload = pnv_spi_xfer_buffer_new(); 220*b4cb930eSChalapathi V for (int offset = 0; offset < payload->len; offset += s->transfer_len) { 221*b4cb930eSChalapathi V tx = 0; 222*b4cb930eSChalapathi V for (int i = 0; i < s->transfer_len; i++) { 223*b4cb930eSChalapathi V if ((offset + i) >= payload->len) { 224*b4cb930eSChalapathi V tx <<= 8; 225*b4cb930eSChalapathi V } else { 226*b4cb930eSChalapathi V tx = (tx << 8) | payload->data[offset + i]; 227*b4cb930eSChalapathi V } 228*b4cb930eSChalapathi V } 229*b4cb930eSChalapathi V rx = ssi_transfer(s->ssi_bus, tx); 230*b4cb930eSChalapathi V for (int i = 0; i < s->transfer_len; i++) { 231*b4cb930eSChalapathi V if ((offset + i) >= payload->len) { 232*b4cb930eSChalapathi V break; 233*b4cb930eSChalapathi V } 234*b4cb930eSChalapathi V *(pnv_spi_xfer_buffer_write_ptr(rsp_payload, rsp_payload->len, 1)) = 235*b4cb930eSChalapathi V (rx >> (8 * (s->transfer_len - 1) - i * 8)) & 0xFF; 236*b4cb930eSChalapathi V } 237*b4cb930eSChalapathi V } 238*b4cb930eSChalapathi V if (rsp_payload != NULL) { 239*b4cb930eSChalapathi V spi_response(s, s->N1_bits, rsp_payload); 240*b4cb930eSChalapathi V } 241*b4cb930eSChalapathi V } 242*b4cb930eSChalapathi V 243*b4cb930eSChalapathi V static inline uint8_t get_seq_index(PnvSpi *s) 244*b4cb930eSChalapathi V { 245*b4cb930eSChalapathi V return GETFIELD(SPI_STS_SEQ_INDEX, s->status); 246*b4cb930eSChalapathi V } 247*b4cb930eSChalapathi V 248*b4cb930eSChalapathi V static inline void next_sequencer_fsm(PnvSpi *s) 249*b4cb930eSChalapathi V { 250*b4cb930eSChalapathi V uint8_t seq_index = get_seq_index(s); 251*b4cb930eSChalapathi V s->status = SETFIELD(SPI_STS_SEQ_INDEX, s->status, (seq_index + 1)); 252*b4cb930eSChalapathi V s->status = SETFIELD(SPI_STS_SEQ_FSM, s->status, SEQ_STATE_INDEX_INCREMENT); 253*b4cb930eSChalapathi V } 254*b4cb930eSChalapathi V 255*b4cb930eSChalapathi V /* 256*b4cb930eSChalapathi V * Calculate the N1 counters based on passed in opcode and 257*b4cb930eSChalapathi V * internal register values. 258*b4cb930eSChalapathi V * The method assumes that the opcode is a Shift_N1 opcode 259*b4cb930eSChalapathi V * and doesn't test it. 260*b4cb930eSChalapathi V * The counters returned are: 261*b4cb930eSChalapathi V * N1 bits: Number of bits in the payload data that are significant 262*b4cb930eSChalapathi V * to the responder. 263*b4cb930eSChalapathi V * N1_bytes: Total count of payload bytes for the N1 (portion of the) frame. 264*b4cb930eSChalapathi V * N1_tx: Total number of bytes taken from TDR for N1 265*b4cb930eSChalapathi V * N1_rx: Total number of bytes taken from the payload for N1 266*b4cb930eSChalapathi V */ 267*b4cb930eSChalapathi V static void calculate_N1(PnvSpi *s, uint8_t opcode) 268*b4cb930eSChalapathi V { 269*b4cb930eSChalapathi V /* 270*b4cb930eSChalapathi V * Shift_N1 opcode form: 0x3M 271*b4cb930eSChalapathi V * Implicit mode: 272*b4cb930eSChalapathi V * If M != 0 the shift count is M bytes and M is the number of tx bytes. 273*b4cb930eSChalapathi V * Forced Implicit mode: 274*b4cb930eSChalapathi V * M is the shift count but tx and rx is determined by the count control 275*b4cb930eSChalapathi V * register fields. Note that we only check for forced Implicit mode when 276*b4cb930eSChalapathi V * M != 0 since the mode doesn't make sense when M = 0. 277*b4cb930eSChalapathi V * Explicit mode: 278*b4cb930eSChalapathi V * If M == 0 then shift count is number of bits defined in the 279*b4cb930eSChalapathi V * Counter Configuration Register's shift_count_N1 field. 280*b4cb930eSChalapathi V */ 281*b4cb930eSChalapathi V if (PNV_SPI_OPCODE_LO_NIBBLE(opcode) == 0) { 282*b4cb930eSChalapathi V /* Explicit mode */ 283*b4cb930eSChalapathi V s->N1_bits = GETFIELD(SPI_CTR_CFG_N1, s->regs[SPI_CTR_CFG_REG]); 284*b4cb930eSChalapathi V s->N1_bytes = (s->N1_bits + 7) / 8; 285*b4cb930eSChalapathi V s->N1_tx = 0; 286*b4cb930eSChalapathi V s->N1_rx = 0; 287*b4cb930eSChalapathi V /* If tx count control for N1 is set, load the tx value */ 288*b4cb930eSChalapathi V if (GETFIELD(SPI_CTR_CFG_N1_CTRL_B2, s->regs[SPI_CTR_CFG_REG]) == 1) { 289*b4cb930eSChalapathi V s->N1_tx = s->N1_bytes; 290*b4cb930eSChalapathi V } 291*b4cb930eSChalapathi V /* If rx count control for N1 is set, load the rx value */ 292*b4cb930eSChalapathi V if (GETFIELD(SPI_CTR_CFG_N1_CTRL_B3, s->regs[SPI_CTR_CFG_REG]) == 1) { 293*b4cb930eSChalapathi V s->N1_rx = s->N1_bytes; 294*b4cb930eSChalapathi V } 295*b4cb930eSChalapathi V } else { 296*b4cb930eSChalapathi V /* Implicit mode/Forced Implicit mode, use M field from opcode */ 297*b4cb930eSChalapathi V s->N1_bytes = PNV_SPI_OPCODE_LO_NIBBLE(opcode); 298*b4cb930eSChalapathi V s->N1_bits = s->N1_bytes * 8; 299*b4cb930eSChalapathi V /* 300*b4cb930eSChalapathi V * Assume that we are going to transmit the count 301*b4cb930eSChalapathi V * (pure Implicit only) 302*b4cb930eSChalapathi V */ 303*b4cb930eSChalapathi V s->N1_tx = s->N1_bytes; 304*b4cb930eSChalapathi V s->N1_rx = 0; 305*b4cb930eSChalapathi V /* Let Forced Implicit mode have an effect on the counts */ 306*b4cb930eSChalapathi V if (GETFIELD(SPI_CTR_CFG_N1_CTRL_B1, s->regs[SPI_CTR_CFG_REG]) == 1) { 307*b4cb930eSChalapathi V /* 308*b4cb930eSChalapathi V * If Forced Implicit mode and count control doesn't 309*b4cb930eSChalapathi V * indicate transmit then reset the tx count to 0 310*b4cb930eSChalapathi V */ 311*b4cb930eSChalapathi V if (GETFIELD(SPI_CTR_CFG_N1_CTRL_B2, 312*b4cb930eSChalapathi V s->regs[SPI_CTR_CFG_REG]) == 0) { 313*b4cb930eSChalapathi V s->N1_tx = 0; 314*b4cb930eSChalapathi V } 315*b4cb930eSChalapathi V /* If rx count control for N1 is set, load the rx value */ 316*b4cb930eSChalapathi V if (GETFIELD(SPI_CTR_CFG_N1_CTRL_B3, 317*b4cb930eSChalapathi V s->regs[SPI_CTR_CFG_REG]) == 1) { 318*b4cb930eSChalapathi V s->N1_rx = s->N1_bytes; 319*b4cb930eSChalapathi V } 320*b4cb930eSChalapathi V } 321*b4cb930eSChalapathi V } 322*b4cb930eSChalapathi V /* 323*b4cb930eSChalapathi V * Enforce an upper limit on the size of N1 that is equal to the known size 324*b4cb930eSChalapathi V * of the shift register, 64 bits or 72 bits if ECC is enabled. 325*b4cb930eSChalapathi V * If the size exceeds 72 bits it is a user error so log an error, 326*b4cb930eSChalapathi V * cap the size at a max of 64 bits or 72 bits and set the sequencer FSM 327*b4cb930eSChalapathi V * error bit. 328*b4cb930eSChalapathi V */ 329*b4cb930eSChalapathi V uint8_t ecc_control = GETFIELD(SPI_CLK_CFG_ECC_CTRL, 330*b4cb930eSChalapathi V s->regs[SPI_CLK_CFG_REG]); 331*b4cb930eSChalapathi V if (ecc_control == 0 || ecc_control == 2) { 332*b4cb930eSChalapathi V if (s->N1_bytes > (PNV_SPI_REG_SIZE + 1)) { 333*b4cb930eSChalapathi V qemu_log_mask(LOG_GUEST_ERROR, "Unsupported N1 shift size when " 334*b4cb930eSChalapathi V "ECC enabled, bytes = 0x%x, bits = 0x%x\n", 335*b4cb930eSChalapathi V s->N1_bytes, s->N1_bits); 336*b4cb930eSChalapathi V s->N1_bytes = PNV_SPI_REG_SIZE + 1; 337*b4cb930eSChalapathi V s->N1_bits = s->N1_bytes * 8; 338*b4cb930eSChalapathi V } 339*b4cb930eSChalapathi V } else if (s->N1_bytes > PNV_SPI_REG_SIZE) { 340*b4cb930eSChalapathi V qemu_log_mask(LOG_GUEST_ERROR, "Unsupported N1 shift size, " 341*b4cb930eSChalapathi V "bytes = 0x%x, bits = 0x%x\n", 342*b4cb930eSChalapathi V s->N1_bytes, s->N1_bits); 343*b4cb930eSChalapathi V s->N1_bytes = PNV_SPI_REG_SIZE; 344*b4cb930eSChalapathi V s->N1_bits = s->N1_bytes * 8; 345*b4cb930eSChalapathi V } 346*b4cb930eSChalapathi V } /* end of calculate_N1 */ 347*b4cb930eSChalapathi V 348*b4cb930eSChalapathi V /* 349*b4cb930eSChalapathi V * Shift_N1 operation handler method 350*b4cb930eSChalapathi V */ 351*b4cb930eSChalapathi V static bool operation_shiftn1(PnvSpi *s, uint8_t opcode, 352*b4cb930eSChalapathi V PnvXferBuffer **payload, bool send_n1_alone) 353*b4cb930eSChalapathi V { 354*b4cb930eSChalapathi V uint8_t n1_count; 355*b4cb930eSChalapathi V bool stop = false; 356*b4cb930eSChalapathi V 357*b4cb930eSChalapathi V /* 358*b4cb930eSChalapathi V * If there isn't a current payload left over from a stopped sequence 359*b4cb930eSChalapathi V * create a new one. 360*b4cb930eSChalapathi V */ 361*b4cb930eSChalapathi V if (*payload == NULL) { 362*b4cb930eSChalapathi V *payload = pnv_spi_xfer_buffer_new(); 363*b4cb930eSChalapathi V } 364*b4cb930eSChalapathi V /* 365*b4cb930eSChalapathi V * Use a combination of N1 counters to build the N1 portion of the 366*b4cb930eSChalapathi V * transmit payload. 367*b4cb930eSChalapathi V * We only care about transmit at this time since the request payload 368*b4cb930eSChalapathi V * only represents data going out on the controller output line. 369*b4cb930eSChalapathi V * Leave mode specific considerations in the calculate function since 370*b4cb930eSChalapathi V * all we really care about are counters that tell use exactly how 371*b4cb930eSChalapathi V * many bytes are in the payload and how many of those bytes to 372*b4cb930eSChalapathi V * include from the TDR into the payload. 373*b4cb930eSChalapathi V */ 374*b4cb930eSChalapathi V calculate_N1(s, opcode); 375*b4cb930eSChalapathi V trace_pnv_spi_log_Ncounts(s->N1_bits, s->N1_bytes, s->N1_tx, 376*b4cb930eSChalapathi V s->N1_rx, s->N2_bits, s->N2_bytes, s->N2_tx, s->N2_rx); 377*b4cb930eSChalapathi V /* 378*b4cb930eSChalapathi V * Zero out the N2 counters here in case there is no N2 operation following 379*b4cb930eSChalapathi V * the N1 operation in the sequencer. This keeps leftover N2 information 380*b4cb930eSChalapathi V * from interfering with spi_response logic. 381*b4cb930eSChalapathi V */ 382*b4cb930eSChalapathi V s->N2_bits = 0; 383*b4cb930eSChalapathi V s->N2_bytes = 0; 384*b4cb930eSChalapathi V s->N2_tx = 0; 385*b4cb930eSChalapathi V s->N2_rx = 0; 386*b4cb930eSChalapathi V /* 387*b4cb930eSChalapathi V * N1_bytes is the overall size of the N1 portion of the frame regardless of 388*b4cb930eSChalapathi V * whether N1 is used for tx, rx or both. Loop over the size to build a 389*b4cb930eSChalapathi V * payload that is N1_bytes long. 390*b4cb930eSChalapathi V * N1_tx is the count of bytes to take from the TDR and "shift" into the 391*b4cb930eSChalapathi V * frame which means append those bytes to the payload for the N1 portion 392*b4cb930eSChalapathi V * of the frame. 393*b4cb930eSChalapathi V * If N1_tx is 0 or if the count exceeds the size of the TDR append 0xFF to 394*b4cb930eSChalapathi V * the frame until the overall N1 count is reached. 395*b4cb930eSChalapathi V */ 396*b4cb930eSChalapathi V n1_count = 0; 397*b4cb930eSChalapathi V while (n1_count < s->N1_bytes) { 398*b4cb930eSChalapathi V /* 399*b4cb930eSChalapathi V * Assuming that if N1_tx is not equal to 0 then it is the same as 400*b4cb930eSChalapathi V * N1_bytes. 401*b4cb930eSChalapathi V */ 402*b4cb930eSChalapathi V if ((s->N1_tx != 0) && (n1_count < PNV_SPI_REG_SIZE)) { 403*b4cb930eSChalapathi V 404*b4cb930eSChalapathi V if (GETFIELD(SPI_STS_TDR_FULL, s->status) == 1) { 405*b4cb930eSChalapathi V /* 406*b4cb930eSChalapathi V * Note that we are only appending to the payload IF the TDR 407*b4cb930eSChalapathi V * is full otherwise we don't touch the payload because we are 408*b4cb930eSChalapathi V * going to NOT send the payload and instead tell the sequencer 409*b4cb930eSChalapathi V * that called us to stop and wait for a TDR write so we have 410*b4cb930eSChalapathi V * data to load into the payload. 411*b4cb930eSChalapathi V */ 412*b4cb930eSChalapathi V uint8_t n1_byte = 0x00; 413*b4cb930eSChalapathi V n1_byte = get_from_offset(s, n1_count); 414*b4cb930eSChalapathi V trace_pnv_spi_tx_append("n1_byte", n1_byte, n1_count); 415*b4cb930eSChalapathi V *(pnv_spi_xfer_buffer_write_ptr(*payload, (*payload)->len, 1)) = 416*b4cb930eSChalapathi V n1_byte; 417*b4cb930eSChalapathi V } else { 418*b4cb930eSChalapathi V /* 419*b4cb930eSChalapathi V * We hit a shift_n1 opcode TX but the TDR is empty, tell the 420*b4cb930eSChalapathi V * sequencer to stop and break this loop. 421*b4cb930eSChalapathi V */ 422*b4cb930eSChalapathi V trace_pnv_spi_sequencer_stop_requested("Shift N1" 423*b4cb930eSChalapathi V "set for transmit but TDR is empty"); 424*b4cb930eSChalapathi V stop = true; 425*b4cb930eSChalapathi V break; 426*b4cb930eSChalapathi V } 427*b4cb930eSChalapathi V } else { 428*b4cb930eSChalapathi V /* 429*b4cb930eSChalapathi V * Cases here: 430*b4cb930eSChalapathi V * - we are receiving during the N1 frame segment and the RDR 431*b4cb930eSChalapathi V * is full so we need to stop until the RDR is read 432*b4cb930eSChalapathi V * - we are transmitting and we don't care about RDR status 433*b4cb930eSChalapathi V * since we won't be loading RDR during the frame segment. 434*b4cb930eSChalapathi V * - we are receiving and the RDR is empty so we allow the operation 435*b4cb930eSChalapathi V * to proceed. 436*b4cb930eSChalapathi V */ 437*b4cb930eSChalapathi V if ((s->N1_rx != 0) && (GETFIELD(SPI_STS_RDR_FULL, 438*b4cb930eSChalapathi V s->status) == 1)) { 439*b4cb930eSChalapathi V trace_pnv_spi_sequencer_stop_requested("shift N1" 440*b4cb930eSChalapathi V "set for receive but RDR is full"); 441*b4cb930eSChalapathi V stop = true; 442*b4cb930eSChalapathi V break; 443*b4cb930eSChalapathi V } else { 444*b4cb930eSChalapathi V trace_pnv_spi_tx_append_FF("n1_byte"); 445*b4cb930eSChalapathi V *(pnv_spi_xfer_buffer_write_ptr(*payload, (*payload)->len, 1)) 446*b4cb930eSChalapathi V = 0xff; 447*b4cb930eSChalapathi V } 448*b4cb930eSChalapathi V } 449*b4cb930eSChalapathi V n1_count++; 450*b4cb930eSChalapathi V } /* end of while */ 451*b4cb930eSChalapathi V /* 452*b4cb930eSChalapathi V * If we are not stopping due to an empty TDR and we are doing an N1 TX 453*b4cb930eSChalapathi V * and the TDR is full we need to clear the TDR_full status. 454*b4cb930eSChalapathi V * Do this here instead of up in the loop above so we don't log the message 455*b4cb930eSChalapathi V * in every loop iteration. 456*b4cb930eSChalapathi V * Ignore the send_n1_alone flag, all that does is defer the TX until the N2 457*b4cb930eSChalapathi V * operation, which was found immediately after the current opcode. The TDR 458*b4cb930eSChalapathi V * was unloaded and will be shifted so we have to clear the TDR_full status. 459*b4cb930eSChalapathi V */ 460*b4cb930eSChalapathi V if (!stop && (s->N1_tx != 0) && 461*b4cb930eSChalapathi V (GETFIELD(SPI_STS_TDR_FULL, s->status) == 1)) { 462*b4cb930eSChalapathi V s->status = SETFIELD(SPI_STS_TDR_FULL, s->status, 0); 463*b4cb930eSChalapathi V } 464*b4cb930eSChalapathi V /* 465*b4cb930eSChalapathi V * There are other reasons why the shifter would stop, such as a TDR empty 466*b4cb930eSChalapathi V * or RDR full condition with N1 set to receive. If we haven't stopped due 467*b4cb930eSChalapathi V * to either one of those conditions then check if the send_n1_alone flag is 468*b4cb930eSChalapathi V * equal to False, indicating the next opcode is an N2 operation, AND if 469*b4cb930eSChalapathi V * the N2 counter reload switch (bit 0 of the N2 count control field) is 470*b4cb930eSChalapathi V * set. This condition requires a pacing write to "kick" off the N2 471*b4cb930eSChalapathi V * shift which includes the N1 shift as well when send_n1_alone is False. 472*b4cb930eSChalapathi V */ 473*b4cb930eSChalapathi V if (!stop && !send_n1_alone && 474*b4cb930eSChalapathi V (GETFIELD(SPI_CTR_CFG_N2_CTRL_B0, s->regs[SPI_CTR_CFG_REG]) == 1)) { 475*b4cb930eSChalapathi V trace_pnv_spi_sequencer_stop_requested("N2 counter reload " 476*b4cb930eSChalapathi V "active, stop N1 shift, TDR_underrun set to 1"); 477*b4cb930eSChalapathi V stop = true; 478*b4cb930eSChalapathi V s->status = SETFIELD(SPI_STS_TDR_UNDERRUN, s->status, 1); 479*b4cb930eSChalapathi V } 480*b4cb930eSChalapathi V /* 481*b4cb930eSChalapathi V * If send_n1_alone is set AND we have a full TDR then this is the first and 482*b4cb930eSChalapathi V * last payload to send and we don't have an N2 frame segment to add to the 483*b4cb930eSChalapathi V * payload. 484*b4cb930eSChalapathi V */ 485*b4cb930eSChalapathi V if (send_n1_alone && !stop) { 486*b4cb930eSChalapathi V /* We have a TX and a full TDR or an RX and an empty RDR */ 487*b4cb930eSChalapathi V trace_pnv_spi_tx_request("Shifting N1 frame", (*payload)->len); 488*b4cb930eSChalapathi V transfer(s, *payload); 489*b4cb930eSChalapathi V /* The N1 frame shift is complete so reset the N1 counters */ 490*b4cb930eSChalapathi V s->N2_bits = 0; 491*b4cb930eSChalapathi V s->N2_bytes = 0; 492*b4cb930eSChalapathi V s->N2_tx = 0; 493*b4cb930eSChalapathi V s->N2_rx = 0; 494*b4cb930eSChalapathi V pnv_spi_xfer_buffer_free(*payload); 495*b4cb930eSChalapathi V *payload = NULL; 496*b4cb930eSChalapathi V } 497*b4cb930eSChalapathi V return stop; 498*b4cb930eSChalapathi V } /* end of operation_shiftn1() */ 499*b4cb930eSChalapathi V 500*b4cb930eSChalapathi V /* 501*b4cb930eSChalapathi V * Calculate the N2 counters based on passed in opcode and 502*b4cb930eSChalapathi V * internal register values. 503*b4cb930eSChalapathi V * The method assumes that the opcode is a Shift_N2 opcode 504*b4cb930eSChalapathi V * and doesn't test it. 505*b4cb930eSChalapathi V * The counters returned are: 506*b4cb930eSChalapathi V * N2 bits: Number of bits in the payload data that are significant 507*b4cb930eSChalapathi V * to the responder. 508*b4cb930eSChalapathi V * N2_bytes: Total count of payload bytes for the N2 frame. 509*b4cb930eSChalapathi V * N2_tx: Total number of bytes taken from TDR for N2 510*b4cb930eSChalapathi V * N2_rx: Total number of bytes taken from the payload for N2 511*b4cb930eSChalapathi V */ 512*b4cb930eSChalapathi V static void calculate_N2(PnvSpi *s, uint8_t opcode) 513*b4cb930eSChalapathi V { 514*b4cb930eSChalapathi V /* 515*b4cb930eSChalapathi V * Shift_N2 opcode form: 0x4M 516*b4cb930eSChalapathi V * Implicit mode: 517*b4cb930eSChalapathi V * If M!=0 the shift count is M bytes and M is the number of rx bytes. 518*b4cb930eSChalapathi V * Forced Implicit mode: 519*b4cb930eSChalapathi V * M is the shift count but tx and rx is determined by the count control 520*b4cb930eSChalapathi V * register fields. Note that we only check for Forced Implicit mode when 521*b4cb930eSChalapathi V * M != 0 since the mode doesn't make sense when M = 0. 522*b4cb930eSChalapathi V * Explicit mode: 523*b4cb930eSChalapathi V * If M==0 then shift count is number of bits defined in the 524*b4cb930eSChalapathi V * Counter Configuration Register's shift_count_N1 field. 525*b4cb930eSChalapathi V */ 526*b4cb930eSChalapathi V if (PNV_SPI_OPCODE_LO_NIBBLE(opcode) == 0) { 527*b4cb930eSChalapathi V /* Explicit mode */ 528*b4cb930eSChalapathi V s->N2_bits = GETFIELD(SPI_CTR_CFG_N2, s->regs[SPI_CTR_CFG_REG]); 529*b4cb930eSChalapathi V s->N2_bytes = (s->N2_bits + 7) / 8; 530*b4cb930eSChalapathi V s->N2_tx = 0; 531*b4cb930eSChalapathi V s->N2_rx = 0; 532*b4cb930eSChalapathi V /* If tx count control for N2 is set, load the tx value */ 533*b4cb930eSChalapathi V if (GETFIELD(SPI_CTR_CFG_N2_CTRL_B2, s->regs[SPI_CTR_CFG_REG]) == 1) { 534*b4cb930eSChalapathi V s->N2_tx = s->N2_bytes; 535*b4cb930eSChalapathi V } 536*b4cb930eSChalapathi V /* If rx count control for N2 is set, load the rx value */ 537*b4cb930eSChalapathi V if (GETFIELD(SPI_CTR_CFG_N2_CTRL_B3, s->regs[SPI_CTR_CFG_REG]) == 1) { 538*b4cb930eSChalapathi V s->N2_rx = s->N2_bytes; 539*b4cb930eSChalapathi V } 540*b4cb930eSChalapathi V } else { 541*b4cb930eSChalapathi V /* Implicit mode/Forced Implicit mode, use M field from opcode */ 542*b4cb930eSChalapathi V s->N2_bytes = PNV_SPI_OPCODE_LO_NIBBLE(opcode); 543*b4cb930eSChalapathi V s->N2_bits = s->N2_bytes * 8; 544*b4cb930eSChalapathi V /* Assume that we are going to receive the count */ 545*b4cb930eSChalapathi V s->N2_rx = s->N2_bytes; 546*b4cb930eSChalapathi V s->N2_tx = 0; 547*b4cb930eSChalapathi V /* Let Forced Implicit mode have an effect on the counts */ 548*b4cb930eSChalapathi V if (GETFIELD(SPI_CTR_CFG_N2_CTRL_B1, s->regs[SPI_CTR_CFG_REG]) == 1) { 549*b4cb930eSChalapathi V /* 550*b4cb930eSChalapathi V * If Forced Implicit mode and count control doesn't 551*b4cb930eSChalapathi V * indicate a receive then reset the rx count to 0 552*b4cb930eSChalapathi V */ 553*b4cb930eSChalapathi V if (GETFIELD(SPI_CTR_CFG_N2_CTRL_B3, 554*b4cb930eSChalapathi V s->regs[SPI_CTR_CFG_REG]) == 0) { 555*b4cb930eSChalapathi V s->N2_rx = 0; 556*b4cb930eSChalapathi V } 557*b4cb930eSChalapathi V /* If tx count control for N2 is set, load the tx value */ 558*b4cb930eSChalapathi V if (GETFIELD(SPI_CTR_CFG_N2_CTRL_B2, 559*b4cb930eSChalapathi V s->regs[SPI_CTR_CFG_REG]) == 1) { 560*b4cb930eSChalapathi V s->N2_tx = s->N2_bytes; 561*b4cb930eSChalapathi V } 562*b4cb930eSChalapathi V } 563*b4cb930eSChalapathi V } 564*b4cb930eSChalapathi V /* 565*b4cb930eSChalapathi V * Enforce an upper limit on the size of N1 that is equal to the 566*b4cb930eSChalapathi V * known size of the shift register, 64 bits or 72 bits if ECC 567*b4cb930eSChalapathi V * is enabled. 568*b4cb930eSChalapathi V * If the size exceeds 72 bits it is a user error so log an error, 569*b4cb930eSChalapathi V * cap the size at a max of 64 bits or 72 bits and set the sequencer FSM 570*b4cb930eSChalapathi V * error bit. 571*b4cb930eSChalapathi V */ 572*b4cb930eSChalapathi V uint8_t ecc_control = GETFIELD(SPI_CLK_CFG_ECC_CTRL, 573*b4cb930eSChalapathi V s->regs[SPI_CLK_CFG_REG]); 574*b4cb930eSChalapathi V if (ecc_control == 0 || ecc_control == 2) { 575*b4cb930eSChalapathi V if (s->N2_bytes > (PNV_SPI_REG_SIZE + 1)) { 576*b4cb930eSChalapathi V /* Unsupported N2 shift size when ECC enabled */ 577*b4cb930eSChalapathi V s->N2_bytes = PNV_SPI_REG_SIZE + 1; 578*b4cb930eSChalapathi V s->N2_bits = s->N2_bytes * 8; 579*b4cb930eSChalapathi V } 580*b4cb930eSChalapathi V } else if (s->N2_bytes > PNV_SPI_REG_SIZE) { 581*b4cb930eSChalapathi V /* Unsupported N2 shift size */ 582*b4cb930eSChalapathi V s->N2_bytes = PNV_SPI_REG_SIZE; 583*b4cb930eSChalapathi V s->N2_bits = s->N2_bytes * 8; 584*b4cb930eSChalapathi V } 585*b4cb930eSChalapathi V } /* end of calculate_N2 */ 586*b4cb930eSChalapathi V 587*b4cb930eSChalapathi V /* 588*b4cb930eSChalapathi V * Shift_N2 operation handler method 589*b4cb930eSChalapathi V */ 590*b4cb930eSChalapathi V 591*b4cb930eSChalapathi V static bool operation_shiftn2(PnvSpi *s, uint8_t opcode, 592*b4cb930eSChalapathi V PnvXferBuffer **payload) 593*b4cb930eSChalapathi V { 594*b4cb930eSChalapathi V uint8_t n2_count; 595*b4cb930eSChalapathi V bool stop = false; 596*b4cb930eSChalapathi V 597*b4cb930eSChalapathi V /* 598*b4cb930eSChalapathi V * If there isn't a current payload left over from a stopped sequence 599*b4cb930eSChalapathi V * create a new one. 600*b4cb930eSChalapathi V */ 601*b4cb930eSChalapathi V if (*payload == NULL) { 602*b4cb930eSChalapathi V *payload = pnv_spi_xfer_buffer_new(); 603*b4cb930eSChalapathi V } 604*b4cb930eSChalapathi V /* 605*b4cb930eSChalapathi V * Use a combination of N2 counters to build the N2 portion of the 606*b4cb930eSChalapathi V * transmit payload. 607*b4cb930eSChalapathi V */ 608*b4cb930eSChalapathi V calculate_N2(s, opcode); 609*b4cb930eSChalapathi V trace_pnv_spi_log_Ncounts(s->N1_bits, s->N1_bytes, s->N1_tx, 610*b4cb930eSChalapathi V s->N1_rx, s->N2_bits, s->N2_bytes, s->N2_tx, s->N2_rx); 611*b4cb930eSChalapathi V /* 612*b4cb930eSChalapathi V * The only difference between this code and the code for shift N1 is 613*b4cb930eSChalapathi V * that this code has to account for the possible presence of N1 transmit 614*b4cb930eSChalapathi V * bytes already taken from the TDR. 615*b4cb930eSChalapathi V * If there are bytes to be transmitted for the N2 portion of the frame 616*b4cb930eSChalapathi V * and there are still bytes in TDR that have not been copied into the 617*b4cb930eSChalapathi V * TX data of the payload, this code will handle transmitting those 618*b4cb930eSChalapathi V * remaining bytes. 619*b4cb930eSChalapathi V * If for some reason the transmit count(s) add up to more than the size 620*b4cb930eSChalapathi V * of the TDR we will just append 0xFF to the transmit payload data until 621*b4cb930eSChalapathi V * the payload is N1 + N2 bytes long. 622*b4cb930eSChalapathi V */ 623*b4cb930eSChalapathi V n2_count = 0; 624*b4cb930eSChalapathi V while (n2_count < s->N2_bytes) { 625*b4cb930eSChalapathi V /* 626*b4cb930eSChalapathi V * If the RDR is full and we need to RX just bail out, letting the 627*b4cb930eSChalapathi V * code continue will end up building the payload twice in the same 628*b4cb930eSChalapathi V * buffer since RDR full causes a sequence stop and restart. 629*b4cb930eSChalapathi V */ 630*b4cb930eSChalapathi V if ((s->N2_rx != 0) && 631*b4cb930eSChalapathi V (GETFIELD(SPI_STS_RDR_FULL, s->status) == 1)) { 632*b4cb930eSChalapathi V trace_pnv_spi_sequencer_stop_requested("shift N2 set" 633*b4cb930eSChalapathi V "for receive but RDR is full"); 634*b4cb930eSChalapathi V stop = true; 635*b4cb930eSChalapathi V break; 636*b4cb930eSChalapathi V } 637*b4cb930eSChalapathi V if ((s->N2_tx != 0) && ((s->N1_tx + n2_count) < 638*b4cb930eSChalapathi V PNV_SPI_REG_SIZE)) { 639*b4cb930eSChalapathi V /* Always append data for the N2 segment if it is set for TX */ 640*b4cb930eSChalapathi V uint8_t n2_byte = 0x00; 641*b4cb930eSChalapathi V n2_byte = get_from_offset(s, (s->N1_tx + n2_count)); 642*b4cb930eSChalapathi V trace_pnv_spi_tx_append("n2_byte", n2_byte, (s->N1_tx + n2_count)); 643*b4cb930eSChalapathi V *(pnv_spi_xfer_buffer_write_ptr(*payload, (*payload)->len, 1)) 644*b4cb930eSChalapathi V = n2_byte; 645*b4cb930eSChalapathi V } else { 646*b4cb930eSChalapathi V /* 647*b4cb930eSChalapathi V * Regardless of whether or not N2 is set for TX or RX, we need 648*b4cb930eSChalapathi V * the number of bytes in the payload to match the overall length 649*b4cb930eSChalapathi V * of the operation. 650*b4cb930eSChalapathi V */ 651*b4cb930eSChalapathi V trace_pnv_spi_tx_append_FF("n2_byte"); 652*b4cb930eSChalapathi V *(pnv_spi_xfer_buffer_write_ptr(*payload, (*payload)->len, 1)) 653*b4cb930eSChalapathi V = 0xff; 654*b4cb930eSChalapathi V } 655*b4cb930eSChalapathi V n2_count++; 656*b4cb930eSChalapathi V } /* end of while */ 657*b4cb930eSChalapathi V if (!stop) { 658*b4cb930eSChalapathi V /* We have a TX and a full TDR or an RX and an empty RDR */ 659*b4cb930eSChalapathi V trace_pnv_spi_tx_request("Shifting N2 frame", (*payload)->len); 660*b4cb930eSChalapathi V transfer(s, *payload); 661*b4cb930eSChalapathi V /* 662*b4cb930eSChalapathi V * If we are doing an N2 TX and the TDR is full we need to clear the 663*b4cb930eSChalapathi V * TDR_full status. Do this here instead of up in the loop above so we 664*b4cb930eSChalapathi V * don't log the message in every loop iteration. 665*b4cb930eSChalapathi V */ 666*b4cb930eSChalapathi V if ((s->N2_tx != 0) && 667*b4cb930eSChalapathi V (GETFIELD(SPI_STS_TDR_FULL, s->status) == 1)) { 668*b4cb930eSChalapathi V s->status = SETFIELD(SPI_STS_TDR_FULL, s->status, 0); 669*b4cb930eSChalapathi V } 670*b4cb930eSChalapathi V /* 671*b4cb930eSChalapathi V * The N2 frame shift is complete so reset the N2 counters. 672*b4cb930eSChalapathi V * Reset the N1 counters also in case the frame was a combination of 673*b4cb930eSChalapathi V * N1 and N2 segments. 674*b4cb930eSChalapathi V */ 675*b4cb930eSChalapathi V s->N2_bits = 0; 676*b4cb930eSChalapathi V s->N2_bytes = 0; 677*b4cb930eSChalapathi V s->N2_tx = 0; 678*b4cb930eSChalapathi V s->N2_rx = 0; 679*b4cb930eSChalapathi V s->N1_bits = 0; 680*b4cb930eSChalapathi V s->N1_bytes = 0; 681*b4cb930eSChalapathi V s->N1_tx = 0; 682*b4cb930eSChalapathi V s->N1_rx = 0; 683*b4cb930eSChalapathi V pnv_spi_xfer_buffer_free(*payload); 684*b4cb930eSChalapathi V *payload = NULL; 685*b4cb930eSChalapathi V } 686*b4cb930eSChalapathi V return stop; 687*b4cb930eSChalapathi V } /* end of operation_shiftn2()*/ 688*b4cb930eSChalapathi V 689*b4cb930eSChalapathi V static void operation_sequencer(PnvSpi *s) 690*b4cb930eSChalapathi V { 691*b4cb930eSChalapathi V /* 692*b4cb930eSChalapathi V * Loop through each sequencer operation ID and perform the requested 693*b4cb930eSChalapathi V * operations. 694*b4cb930eSChalapathi V * Flag for indicating if we should send the N1 frame or wait to combine 695*b4cb930eSChalapathi V * it with a preceding N2 frame. 696*b4cb930eSChalapathi V */ 697*b4cb930eSChalapathi V bool send_n1_alone = true; 698*b4cb930eSChalapathi V bool stop = false; /* Flag to stop the sequencer */ 699*b4cb930eSChalapathi V uint8_t opcode = 0; 700*b4cb930eSChalapathi V uint8_t masked_opcode = 0; 701*b4cb930eSChalapathi V 702*b4cb930eSChalapathi V /* 703*b4cb930eSChalapathi V * PnvXferBuffer for containing the payload of the SPI frame. 704*b4cb930eSChalapathi V * This is a static because there are cases where a sequence has to stop 705*b4cb930eSChalapathi V * and wait for the target application to unload the RDR. If this occurs 706*b4cb930eSChalapathi V * during a sequence where N1 is not sent alone and instead combined with 707*b4cb930eSChalapathi V * N2 since the N1 tx length + the N2 tx length is less than the size of 708*b4cb930eSChalapathi V * the TDR. 709*b4cb930eSChalapathi V */ 710*b4cb930eSChalapathi V static PnvXferBuffer *payload; 711*b4cb930eSChalapathi V 712*b4cb930eSChalapathi V if (payload == NULL) { 713*b4cb930eSChalapathi V payload = pnv_spi_xfer_buffer_new(); 714*b4cb930eSChalapathi V } 715*b4cb930eSChalapathi V /* 716*b4cb930eSChalapathi V * Clear the sequencer FSM error bit - general_SPI_status[3] 717*b4cb930eSChalapathi V * before starting a sequence. 718*b4cb930eSChalapathi V */ 719*b4cb930eSChalapathi V s->status = SETFIELD(SPI_STS_GEN_STATUS_B3, s->status, 0); 720*b4cb930eSChalapathi V /* 721*b4cb930eSChalapathi V * If the FSM is idle set the sequencer index to 0 722*b4cb930eSChalapathi V * (new/restarted sequence) 723*b4cb930eSChalapathi V */ 724*b4cb930eSChalapathi V if (GETFIELD(SPI_STS_SEQ_FSM, s->status) == SEQ_STATE_IDLE) { 725*b4cb930eSChalapathi V s->status = SETFIELD(SPI_STS_SEQ_INDEX, s->status, 0); 726*b4cb930eSChalapathi V } 727*b4cb930eSChalapathi V /* 728*b4cb930eSChalapathi V * There are only 8 possible operation IDs to iterate through though 729*b4cb930eSChalapathi V * some operations may cause more than one frame to be sequenced. 730*b4cb930eSChalapathi V */ 731*b4cb930eSChalapathi V while (get_seq_index(s) < NUM_SEQ_OPS) { 732*b4cb930eSChalapathi V opcode = s->seq_op[get_seq_index(s)]; 733*b4cb930eSChalapathi V /* Set sequencer state to decode */ 734*b4cb930eSChalapathi V s->status = SETFIELD(SPI_STS_SEQ_FSM, s->status, SEQ_STATE_DECODE); 735*b4cb930eSChalapathi V /* 736*b4cb930eSChalapathi V * Only the upper nibble of the operation ID is needed to know what 737*b4cb930eSChalapathi V * kind of operation is requested. 738*b4cb930eSChalapathi V */ 739*b4cb930eSChalapathi V masked_opcode = PNV_SPI_MASKED_OPCODE(opcode); 740*b4cb930eSChalapathi V switch (masked_opcode) { 741*b4cb930eSChalapathi V /* 742*b4cb930eSChalapathi V * Increment the operation index in each case instead of just 743*b4cb930eSChalapathi V * once at the end in case an operation like the branch 744*b4cb930eSChalapathi V * operation needs to change the index. 745*b4cb930eSChalapathi V */ 746*b4cb930eSChalapathi V case SEQ_OP_STOP: 747*b4cb930eSChalapathi V s->status = SETFIELD(SPI_STS_SEQ_FSM, s->status, SEQ_STATE_EXECUTE); 748*b4cb930eSChalapathi V /* A stop operation in any position stops the sequencer */ 749*b4cb930eSChalapathi V trace_pnv_spi_sequencer_op("STOP", get_seq_index(s)); 750*b4cb930eSChalapathi V 751*b4cb930eSChalapathi V stop = true; 752*b4cb930eSChalapathi V s->status = SETFIELD(SPI_STS_SHIFTER_FSM, s->status, FSM_IDLE); 753*b4cb930eSChalapathi V s->loop_counter_1 = 0; 754*b4cb930eSChalapathi V s->loop_counter_2 = 0; 755*b4cb930eSChalapathi V s->status = SETFIELD(SPI_STS_SEQ_FSM, s->status, SEQ_STATE_IDLE); 756*b4cb930eSChalapathi V break; 757*b4cb930eSChalapathi V 758*b4cb930eSChalapathi V case SEQ_OP_SELECT_SLAVE: 759*b4cb930eSChalapathi V s->status = SETFIELD(SPI_STS_SEQ_FSM, s->status, SEQ_STATE_EXECUTE); 760*b4cb930eSChalapathi V trace_pnv_spi_sequencer_op("SELECT_SLAVE", get_seq_index(s)); 761*b4cb930eSChalapathi V /* 762*b4cb930eSChalapathi V * This device currently only supports a single responder 763*b4cb930eSChalapathi V * connection at position 0. De-selecting a responder is fine 764*b4cb930eSChalapathi V * and expected at the end of a sequence but selecting any 765*b4cb930eSChalapathi V * responder other than 0 should cause an error. 766*b4cb930eSChalapathi V */ 767*b4cb930eSChalapathi V s->responder_select = PNV_SPI_OPCODE_LO_NIBBLE(opcode); 768*b4cb930eSChalapathi V if (s->responder_select == 0) { 769*b4cb930eSChalapathi V trace_pnv_spi_shifter_done(); 770*b4cb930eSChalapathi V qemu_set_irq(s->cs_line[0], 1); 771*b4cb930eSChalapathi V s->status = SETFIELD(SPI_STS_SEQ_INDEX, s->status, 772*b4cb930eSChalapathi V (get_seq_index(s) + 1)); 773*b4cb930eSChalapathi V s->status = SETFIELD(SPI_STS_SHIFTER_FSM, s->status, FSM_DONE); 774*b4cb930eSChalapathi V } else if (s->responder_select != 1) { 775*b4cb930eSChalapathi V qemu_log_mask(LOG_GUEST_ERROR, "Slave selection other than 1 " 776*b4cb930eSChalapathi V "not supported, select = 0x%x\n", 777*b4cb930eSChalapathi V s->responder_select); 778*b4cb930eSChalapathi V trace_pnv_spi_sequencer_stop_requested("invalid " 779*b4cb930eSChalapathi V "responder select"); 780*b4cb930eSChalapathi V s->status = SETFIELD(SPI_STS_SHIFTER_FSM, s->status, FSM_IDLE); 781*b4cb930eSChalapathi V stop = true; 782*b4cb930eSChalapathi V } else { 783*b4cb930eSChalapathi V /* 784*b4cb930eSChalapathi V * Only allow an FSM_START state when a responder is 785*b4cb930eSChalapathi V * selected 786*b4cb930eSChalapathi V */ 787*b4cb930eSChalapathi V s->status = SETFIELD(SPI_STS_SHIFTER_FSM, s->status, FSM_START); 788*b4cb930eSChalapathi V trace_pnv_spi_shifter_stating(); 789*b4cb930eSChalapathi V qemu_set_irq(s->cs_line[0], 0); 790*b4cb930eSChalapathi V /* 791*b4cb930eSChalapathi V * A Shift_N2 operation is only valid after a Shift_N1 792*b4cb930eSChalapathi V * according to the spec. The spec doesn't say if that means 793*b4cb930eSChalapathi V * immediately after or just after at any point. We will track 794*b4cb930eSChalapathi V * the occurrence of a Shift_N1 to enforce this requirement in 795*b4cb930eSChalapathi V * the most generic way possible by assuming that the rule 796*b4cb930eSChalapathi V * applies once a valid responder select has occurred. 797*b4cb930eSChalapathi V */ 798*b4cb930eSChalapathi V s->shift_n1_done = false; 799*b4cb930eSChalapathi V next_sequencer_fsm(s); 800*b4cb930eSChalapathi V } 801*b4cb930eSChalapathi V break; 802*b4cb930eSChalapathi V 803*b4cb930eSChalapathi V case SEQ_OP_SHIFT_N1: 804*b4cb930eSChalapathi V s->status = SETFIELD(SPI_STS_SEQ_FSM, s->status, SEQ_STATE_EXECUTE); 805*b4cb930eSChalapathi V trace_pnv_spi_sequencer_op("SHIFT_N1", get_seq_index(s)); 806*b4cb930eSChalapathi V /* 807*b4cb930eSChalapathi V * Only allow a shift_n1 when the state is not IDLE or DONE. 808*b4cb930eSChalapathi V * In either of those two cases the sequencer is not in a proper 809*b4cb930eSChalapathi V * state to perform shift operations because the sequencer has: 810*b4cb930eSChalapathi V * - processed a responder deselect (DONE) 811*b4cb930eSChalapathi V * - processed a stop opcode (IDLE) 812*b4cb930eSChalapathi V * - encountered an error (IDLE) 813*b4cb930eSChalapathi V */ 814*b4cb930eSChalapathi V if ((GETFIELD(SPI_STS_SHIFTER_FSM, s->status) == FSM_IDLE) || 815*b4cb930eSChalapathi V (GETFIELD(SPI_STS_SHIFTER_FSM, s->status) == FSM_DONE)) { 816*b4cb930eSChalapathi V qemu_log_mask(LOG_GUEST_ERROR, "Shift_N1 not allowed in " 817*b4cb930eSChalapathi V "shifter state = 0x%llx", GETFIELD( 818*b4cb930eSChalapathi V SPI_STS_SHIFTER_FSM, s->status)); 819*b4cb930eSChalapathi V /* 820*b4cb930eSChalapathi V * Set sequencer FSM error bit 3 (general_SPI_status[3]) 821*b4cb930eSChalapathi V * in status reg. 822*b4cb930eSChalapathi V */ 823*b4cb930eSChalapathi V s->status = SETFIELD(SPI_STS_GEN_STATUS_B3, s->status, 1); 824*b4cb930eSChalapathi V trace_pnv_spi_sequencer_stop_requested("invalid shifter state"); 825*b4cb930eSChalapathi V stop = true; 826*b4cb930eSChalapathi V } else { 827*b4cb930eSChalapathi V /* 828*b4cb930eSChalapathi V * Look for the special case where there is a shift_n1 set for 829*b4cb930eSChalapathi V * transmit and it is followed by a shift_n2 set for transmit 830*b4cb930eSChalapathi V * AND the combined transmit length of the two operations is 831*b4cb930eSChalapathi V * less than or equal to the size of the TDR register. In this 832*b4cb930eSChalapathi V * case we want to use both this current shift_n1 opcode and the 833*b4cb930eSChalapathi V * following shift_n2 opcode to assemble the frame for 834*b4cb930eSChalapathi V * transmission to the responder without requiring a refill of 835*b4cb930eSChalapathi V * the TDR between the two operations. 836*b4cb930eSChalapathi V */ 837*b4cb930eSChalapathi V if (PNV_SPI_MASKED_OPCODE(s->seq_op[get_seq_index(s) + 1]) 838*b4cb930eSChalapathi V == SEQ_OP_SHIFT_N2) { 839*b4cb930eSChalapathi V send_n1_alone = false; 840*b4cb930eSChalapathi V } 841*b4cb930eSChalapathi V s->status = SETFIELD(SPI_STS_SHIFTER_FSM, s->status, 842*b4cb930eSChalapathi V FSM_SHIFT_N1); 843*b4cb930eSChalapathi V stop = operation_shiftn1(s, opcode, &payload, send_n1_alone); 844*b4cb930eSChalapathi V if (stop) { 845*b4cb930eSChalapathi V /* 846*b4cb930eSChalapathi V * The operation code says to stop, this can occur if: 847*b4cb930eSChalapathi V * (1) RDR is full and the N1 shift is set for receive 848*b4cb930eSChalapathi V * (2) TDR was empty at the time of the N1 shift so we need 849*b4cb930eSChalapathi V * to wait for data. 850*b4cb930eSChalapathi V * (3) Neither 1 nor 2 are occurring and we aren't sending 851*b4cb930eSChalapathi V * N1 alone and N2 counter reload is set (bit 0 of the N2 852*b4cb930eSChalapathi V * counter reload field). In this case TDR_underrun will 853*b4cb930eSChalapathi V * will be set and the Payload has been loaded so it is 854*b4cb930eSChalapathi V * ok to advance the sequencer. 855*b4cb930eSChalapathi V */ 856*b4cb930eSChalapathi V if (GETFIELD(SPI_STS_TDR_UNDERRUN, s->status)) { 857*b4cb930eSChalapathi V s->shift_n1_done = true; 858*b4cb930eSChalapathi V s->status = SETFIELD(SPI_STS_SHIFTER_FSM, s->status, 859*b4cb930eSChalapathi V FSM_SHIFT_N2); 860*b4cb930eSChalapathi V s->status = SETFIELD(SPI_STS_SEQ_INDEX, s->status, 861*b4cb930eSChalapathi V (get_seq_index(s) + 1)); 862*b4cb930eSChalapathi V } else { 863*b4cb930eSChalapathi V /* 864*b4cb930eSChalapathi V * This is case (1) or (2) so the sequencer needs to 865*b4cb930eSChalapathi V * wait and NOT go to the next sequence yet. 866*b4cb930eSChalapathi V */ 867*b4cb930eSChalapathi V s->status = SETFIELD(SPI_STS_SHIFTER_FSM, s->status, 868*b4cb930eSChalapathi V FSM_WAIT); 869*b4cb930eSChalapathi V } 870*b4cb930eSChalapathi V } else { 871*b4cb930eSChalapathi V /* Ok to move on to the next index */ 872*b4cb930eSChalapathi V s->shift_n1_done = true; 873*b4cb930eSChalapathi V next_sequencer_fsm(s); 874*b4cb930eSChalapathi V } 875*b4cb930eSChalapathi V } 876*b4cb930eSChalapathi V break; 877*b4cb930eSChalapathi V 878*b4cb930eSChalapathi V case SEQ_OP_SHIFT_N2: 879*b4cb930eSChalapathi V s->status = SETFIELD(SPI_STS_SEQ_FSM, s->status, SEQ_STATE_EXECUTE); 880*b4cb930eSChalapathi V trace_pnv_spi_sequencer_op("SHIFT_N2", get_seq_index(s)); 881*b4cb930eSChalapathi V if (!s->shift_n1_done) { 882*b4cb930eSChalapathi V qemu_log_mask(LOG_GUEST_ERROR, "Shift_N2 is not allowed if a " 883*b4cb930eSChalapathi V "Shift_N1 is not done, shifter state = 0x%llx", 884*b4cb930eSChalapathi V GETFIELD(SPI_STS_SHIFTER_FSM, s->status)); 885*b4cb930eSChalapathi V /* 886*b4cb930eSChalapathi V * In case the sequencer actually stops if an N2 shift is 887*b4cb930eSChalapathi V * requested before any N1 shift is done. Set sequencer FSM 888*b4cb930eSChalapathi V * error bit 3 (general_SPI_status[3]) in status reg. 889*b4cb930eSChalapathi V */ 890*b4cb930eSChalapathi V s->status = SETFIELD(SPI_STS_GEN_STATUS_B3, s->status, 1); 891*b4cb930eSChalapathi V trace_pnv_spi_sequencer_stop_requested("shift_n2 " 892*b4cb930eSChalapathi V "w/no shift_n1 done"); 893*b4cb930eSChalapathi V stop = true; 894*b4cb930eSChalapathi V } else { 895*b4cb930eSChalapathi V /* Ok to do a Shift_N2 */ 896*b4cb930eSChalapathi V s->status = SETFIELD(SPI_STS_SHIFTER_FSM, s->status, 897*b4cb930eSChalapathi V FSM_SHIFT_N2); 898*b4cb930eSChalapathi V stop = operation_shiftn2(s, opcode, &payload); 899*b4cb930eSChalapathi V /* 900*b4cb930eSChalapathi V * If the operation code says to stop set the shifter state to 901*b4cb930eSChalapathi V * wait and stop 902*b4cb930eSChalapathi V */ 903*b4cb930eSChalapathi V if (stop) { 904*b4cb930eSChalapathi V s->status = SETFIELD(SPI_STS_SHIFTER_FSM, s->status, 905*b4cb930eSChalapathi V FSM_WAIT); 906*b4cb930eSChalapathi V } else { 907*b4cb930eSChalapathi V /* Ok to move on to the next index */ 908*b4cb930eSChalapathi V next_sequencer_fsm(s); 909*b4cb930eSChalapathi V } 910*b4cb930eSChalapathi V } 911*b4cb930eSChalapathi V break; 912*b4cb930eSChalapathi V 913*b4cb930eSChalapathi V case SEQ_OP_BRANCH_IFNEQ_RDR: 914*b4cb930eSChalapathi V s->status = SETFIELD(SPI_STS_SEQ_FSM, s->status, SEQ_STATE_EXECUTE); 915*b4cb930eSChalapathi V trace_pnv_spi_sequencer_op("BRANCH_IFNEQ_RDR", get_seq_index(s)); 916*b4cb930eSChalapathi V /* 917*b4cb930eSChalapathi V * The memory mapping register RDR match value is compared against 918*b4cb930eSChalapathi V * the 16 rightmost bytes of the RDR (potentially with masking). 919*b4cb930eSChalapathi V * Since this comparison is performed against the contents of the 920*b4cb930eSChalapathi V * RDR then a receive must have previously occurred otherwise 921*b4cb930eSChalapathi V * there is no data to compare and the operation cannot be 922*b4cb930eSChalapathi V * completed and will stop the sequencer until RDR full is set to 923*b4cb930eSChalapathi V * 1. 924*b4cb930eSChalapathi V */ 925*b4cb930eSChalapathi V if (GETFIELD(SPI_STS_RDR_FULL, s->status) == 1) { 926*b4cb930eSChalapathi V bool rdr_matched = false; 927*b4cb930eSChalapathi V rdr_matched = does_rdr_match(s); 928*b4cb930eSChalapathi V if (rdr_matched) { 929*b4cb930eSChalapathi V trace_pnv_spi_RDR_match("success"); 930*b4cb930eSChalapathi V /* A match occurred, increment the sequencer index. */ 931*b4cb930eSChalapathi V next_sequencer_fsm(s); 932*b4cb930eSChalapathi V } else { 933*b4cb930eSChalapathi V trace_pnv_spi_RDR_match("failed"); 934*b4cb930eSChalapathi V /* 935*b4cb930eSChalapathi V * Branch the sequencer to the index coded into the op 936*b4cb930eSChalapathi V * code. 937*b4cb930eSChalapathi V */ 938*b4cb930eSChalapathi V s->status = SETFIELD(SPI_STS_SEQ_INDEX, s->status, 939*b4cb930eSChalapathi V PNV_SPI_OPCODE_LO_NIBBLE(opcode)); 940*b4cb930eSChalapathi V } 941*b4cb930eSChalapathi V /* 942*b4cb930eSChalapathi V * Regardless of where the branch ended up we want the 943*b4cb930eSChalapathi V * sequencer to continue shifting so we have to clear 944*b4cb930eSChalapathi V * RDR_full. 945*b4cb930eSChalapathi V */ 946*b4cb930eSChalapathi V s->status = SETFIELD(SPI_STS_RDR_FULL, s->status, 0); 947*b4cb930eSChalapathi V } else { 948*b4cb930eSChalapathi V trace_pnv_spi_sequencer_stop_requested("RDR not" 949*b4cb930eSChalapathi V "full for 0x6x opcode"); 950*b4cb930eSChalapathi V stop = true; 951*b4cb930eSChalapathi V s->status = SETFIELD(SPI_STS_SHIFTER_FSM, s->status, FSM_WAIT); 952*b4cb930eSChalapathi V } 953*b4cb930eSChalapathi V break; 954*b4cb930eSChalapathi V 955*b4cb930eSChalapathi V case SEQ_OP_TRANSFER_TDR: 956*b4cb930eSChalapathi V s->status = SETFIELD(SPI_STS_SEQ_FSM, s->status, SEQ_STATE_EXECUTE); 957*b4cb930eSChalapathi V qemu_log_mask(LOG_GUEST_ERROR, "Transfer TDR is not supported\n"); 958*b4cb930eSChalapathi V next_sequencer_fsm(s); 959*b4cb930eSChalapathi V break; 960*b4cb930eSChalapathi V 961*b4cb930eSChalapathi V case SEQ_OP_BRANCH_IFNEQ_INC_1: 962*b4cb930eSChalapathi V s->status = SETFIELD(SPI_STS_SEQ_FSM, s->status, SEQ_STATE_EXECUTE); 963*b4cb930eSChalapathi V trace_pnv_spi_sequencer_op("BRANCH_IFNEQ_INC_1", get_seq_index(s)); 964*b4cb930eSChalapathi V /* 965*b4cb930eSChalapathi V * The spec says the loop should execute count compare + 1 times. 966*b4cb930eSChalapathi V * However we learned from engineering that we really only loop 967*b4cb930eSChalapathi V * count_compare times, count compare = 0 makes this op code a 968*b4cb930eSChalapathi V * no-op 969*b4cb930eSChalapathi V */ 970*b4cb930eSChalapathi V if (s->loop_counter_1 != 971*b4cb930eSChalapathi V GETFIELD(SPI_CTR_CFG_CMP1, s->regs[SPI_CTR_CFG_REG])) { 972*b4cb930eSChalapathi V /* 973*b4cb930eSChalapathi V * Next index is the lower nibble of the branch operation ID, 974*b4cb930eSChalapathi V * mask off all but the first three bits so we don't try to 975*b4cb930eSChalapathi V * access beyond the sequencer_operation_reg boundary. 976*b4cb930eSChalapathi V */ 977*b4cb930eSChalapathi V s->status = SETFIELD(SPI_STS_SEQ_INDEX, s->status, 978*b4cb930eSChalapathi V PNV_SPI_OPCODE_LO_NIBBLE(opcode)); 979*b4cb930eSChalapathi V s->loop_counter_1++; 980*b4cb930eSChalapathi V } else { 981*b4cb930eSChalapathi V /* Continue to next index if loop counter is reached */ 982*b4cb930eSChalapathi V next_sequencer_fsm(s); 983*b4cb930eSChalapathi V } 984*b4cb930eSChalapathi V break; 985*b4cb930eSChalapathi V 986*b4cb930eSChalapathi V case SEQ_OP_BRANCH_IFNEQ_INC_2: 987*b4cb930eSChalapathi V s->status = SETFIELD(SPI_STS_SEQ_FSM, s->status, SEQ_STATE_EXECUTE); 988*b4cb930eSChalapathi V trace_pnv_spi_sequencer_op("BRANCH_IFNEQ_INC_2", get_seq_index(s)); 989*b4cb930eSChalapathi V uint8_t condition2 = GETFIELD(SPI_CTR_CFG_CMP2, 990*b4cb930eSChalapathi V s->regs[SPI_CTR_CFG_REG]); 991*b4cb930eSChalapathi V /* 992*b4cb930eSChalapathi V * The spec says the loop should execute count compare + 1 times. 993*b4cb930eSChalapathi V * However we learned from engineering that we really only loop 994*b4cb930eSChalapathi V * count_compare times, count compare = 0 makes this op code a 995*b4cb930eSChalapathi V * no-op 996*b4cb930eSChalapathi V */ 997*b4cb930eSChalapathi V if (s->loop_counter_2 != condition2) { 998*b4cb930eSChalapathi V /* 999*b4cb930eSChalapathi V * Next index is the lower nibble of the branch operation ID, 1000*b4cb930eSChalapathi V * mask off all but the first three bits so we don't try to 1001*b4cb930eSChalapathi V * access beyond the sequencer_operation_reg boundary. 1002*b4cb930eSChalapathi V */ 1003*b4cb930eSChalapathi V s->status = SETFIELD(SPI_STS_SEQ_INDEX, 1004*b4cb930eSChalapathi V s->status, PNV_SPI_OPCODE_LO_NIBBLE(opcode)); 1005*b4cb930eSChalapathi V s->loop_counter_2++; 1006*b4cb930eSChalapathi V } else { 1007*b4cb930eSChalapathi V /* Continue to next index if loop counter is reached */ 1008*b4cb930eSChalapathi V next_sequencer_fsm(s); 1009*b4cb930eSChalapathi V } 1010*b4cb930eSChalapathi V break; 1011*b4cb930eSChalapathi V 1012*b4cb930eSChalapathi V default: 1013*b4cb930eSChalapathi V s->status = SETFIELD(SPI_STS_SEQ_FSM, s->status, SEQ_STATE_EXECUTE); 1014*b4cb930eSChalapathi V /* Ignore unsupported operations. */ 1015*b4cb930eSChalapathi V next_sequencer_fsm(s); 1016*b4cb930eSChalapathi V break; 1017*b4cb930eSChalapathi V } /* end of switch */ 1018*b4cb930eSChalapathi V /* 1019*b4cb930eSChalapathi V * If we used all 8 opcodes without seeing a 00 - STOP in the sequence 1020*b4cb930eSChalapathi V * we need to go ahead and end things as if there was a STOP at the 1021*b4cb930eSChalapathi V * end. 1022*b4cb930eSChalapathi V */ 1023*b4cb930eSChalapathi V if (get_seq_index(s) == NUM_SEQ_OPS) { 1024*b4cb930eSChalapathi V /* All 8 opcodes completed, sequencer idling */ 1025*b4cb930eSChalapathi V s->status = SETFIELD(SPI_STS_SHIFTER_FSM, s->status, FSM_IDLE); 1026*b4cb930eSChalapathi V s->status = SETFIELD(SPI_STS_SEQ_INDEX, s->status, 0); 1027*b4cb930eSChalapathi V s->loop_counter_1 = 0; 1028*b4cb930eSChalapathi V s->loop_counter_2 = 0; 1029*b4cb930eSChalapathi V s->status = SETFIELD(SPI_STS_SEQ_FSM, s->status, SEQ_STATE_IDLE); 1030*b4cb930eSChalapathi V break; 1031*b4cb930eSChalapathi V } 1032*b4cb930eSChalapathi V /* Break the loop if a stop was requested */ 1033*b4cb930eSChalapathi V if (stop) { 1034*b4cb930eSChalapathi V break; 1035*b4cb930eSChalapathi V } 1036*b4cb930eSChalapathi V } /* end of while */ 1037*b4cb930eSChalapathi V return; 1038*b4cb930eSChalapathi V } /* end of operation_sequencer() */ 1039*b4cb930eSChalapathi V 1040*b4cb930eSChalapathi V /* 1041*b4cb930eSChalapathi V * The SPIC engine and its internal sequencer can be interrupted and reset by 1042*b4cb930eSChalapathi V * a hardware signal, the sbe_spicst_hard_reset bits from Pervasive 1043*b4cb930eSChalapathi V * Miscellaneous Register of sbe_register_bo device. 1044*b4cb930eSChalapathi V * Reset immediately aborts any SPI transaction in progress and returns the 1045*b4cb930eSChalapathi V * sequencer and state machines to idle state. 1046*b4cb930eSChalapathi V * The configuration register values are not changed. The status register is 1047*b4cb930eSChalapathi V * not reset. The engine registers are not reset. 1048*b4cb930eSChalapathi V * The SPIC engine reset does not have any affect on the attached devices. 1049*b4cb930eSChalapathi V * Reset handling of any attached devices is beyond the scope of the engine. 1050*b4cb930eSChalapathi V */ 1051*b4cb930eSChalapathi V static void do_reset(DeviceState *dev) 1052*b4cb930eSChalapathi V { 1053*b4cb930eSChalapathi V PnvSpi *s = PNV_SPI(dev); 1054*b4cb930eSChalapathi V 1055*b4cb930eSChalapathi V trace_pnv_spi_reset(); 1056*b4cb930eSChalapathi V 1057*b4cb930eSChalapathi V /* Reset all N1 and N2 counters, and other constants */ 1058*b4cb930eSChalapathi V s->N2_bits = 0; 1059*b4cb930eSChalapathi V s->N2_bytes = 0; 1060*b4cb930eSChalapathi V s->N2_tx = 0; 1061*b4cb930eSChalapathi V s->N2_rx = 0; 1062*b4cb930eSChalapathi V s->N1_bits = 0; 1063*b4cb930eSChalapathi V s->N1_bytes = 0; 1064*b4cb930eSChalapathi V s->N1_tx = 0; 1065*b4cb930eSChalapathi V s->N1_rx = 0; 1066*b4cb930eSChalapathi V s->loop_counter_1 = 0; 1067*b4cb930eSChalapathi V s->loop_counter_2 = 0; 1068*b4cb930eSChalapathi V /* Disconnected from responder */ 1069*b4cb930eSChalapathi V qemu_set_irq(s->cs_line[0], 1); 1070*b4cb930eSChalapathi V } 1071*b4cb930eSChalapathi V 107229318db1SChalapathi V static uint64_t pnv_spi_xscom_read(void *opaque, hwaddr addr, unsigned size) 107329318db1SChalapathi V { 107429318db1SChalapathi V PnvSpi *s = PNV_SPI(opaque); 107529318db1SChalapathi V uint32_t reg = addr >> 3; 107629318db1SChalapathi V uint64_t val = ~0ull; 107729318db1SChalapathi V 107829318db1SChalapathi V switch (reg) { 107929318db1SChalapathi V case ERROR_REG: 108029318db1SChalapathi V case SPI_CTR_CFG_REG: 108129318db1SChalapathi V case CONFIG_REG1: 108229318db1SChalapathi V case SPI_CLK_CFG_REG: 108329318db1SChalapathi V case SPI_MM_REG: 108429318db1SChalapathi V case SPI_XMIT_DATA_REG: 108529318db1SChalapathi V val = s->regs[reg]; 108629318db1SChalapathi V break; 108729318db1SChalapathi V case SPI_RCV_DATA_REG: 108829318db1SChalapathi V val = s->regs[reg]; 108929318db1SChalapathi V trace_pnv_spi_read_RDR(val); 109029318db1SChalapathi V s->status = SETFIELD(SPI_STS_RDR_FULL, s->status, 0); 1091*b4cb930eSChalapathi V if (GETFIELD(SPI_STS_SHIFTER_FSM, s->status) == FSM_WAIT) { 1092*b4cb930eSChalapathi V trace_pnv_spi_start_sequencer(); 1093*b4cb930eSChalapathi V operation_sequencer(s); 1094*b4cb930eSChalapathi V } 109529318db1SChalapathi V break; 109629318db1SChalapathi V case SPI_SEQ_OP_REG: 109729318db1SChalapathi V val = 0; 109829318db1SChalapathi V for (int i = 0; i < PNV_SPI_REG_SIZE; i++) { 109929318db1SChalapathi V val = (val << 8) | s->seq_op[i]; 110029318db1SChalapathi V } 110129318db1SChalapathi V break; 110229318db1SChalapathi V case SPI_STS_REG: 110329318db1SChalapathi V val = s->status; 110429318db1SChalapathi V break; 110529318db1SChalapathi V default: 110629318db1SChalapathi V qemu_log_mask(LOG_GUEST_ERROR, "pnv_spi_regs: Invalid xscom " 110729318db1SChalapathi V "read at 0x%" PRIx32 "\n", reg); 110829318db1SChalapathi V } 110929318db1SChalapathi V 111029318db1SChalapathi V trace_pnv_spi_read(addr, val); 111129318db1SChalapathi V return val; 111229318db1SChalapathi V } 111329318db1SChalapathi V 111429318db1SChalapathi V static void pnv_spi_xscom_write(void *opaque, hwaddr addr, 111529318db1SChalapathi V uint64_t val, unsigned size) 111629318db1SChalapathi V { 111729318db1SChalapathi V PnvSpi *s = PNV_SPI(opaque); 111829318db1SChalapathi V uint32_t reg = addr >> 3; 111929318db1SChalapathi V 112029318db1SChalapathi V trace_pnv_spi_write(addr, val); 112129318db1SChalapathi V 112229318db1SChalapathi V switch (reg) { 112329318db1SChalapathi V case ERROR_REG: 112429318db1SChalapathi V case SPI_CTR_CFG_REG: 112529318db1SChalapathi V case CONFIG_REG1: 112629318db1SChalapathi V case SPI_MM_REG: 112729318db1SChalapathi V case SPI_RCV_DATA_REG: 112829318db1SChalapathi V s->regs[reg] = val; 112929318db1SChalapathi V break; 113029318db1SChalapathi V case SPI_CLK_CFG_REG: 113129318db1SChalapathi V /* 113229318db1SChalapathi V * To reset the SPI controller write the sequence 0x5 0xA to 113329318db1SChalapathi V * reset_control field 113429318db1SChalapathi V */ 113529318db1SChalapathi V if ((GETFIELD(SPI_CLK_CFG_RST_CTRL, s->regs[SPI_CLK_CFG_REG]) == 0x5) 113629318db1SChalapathi V && (GETFIELD(SPI_CLK_CFG_RST_CTRL, val) == 0xA)) { 113729318db1SChalapathi V /* SPI controller reset sequence completed, resetting */ 113829318db1SChalapathi V s->regs[reg] = SPI_CLK_CFG_HARD_RST; 113929318db1SChalapathi V } else { 114029318db1SChalapathi V s->regs[reg] = val; 114129318db1SChalapathi V } 114229318db1SChalapathi V break; 114329318db1SChalapathi V case SPI_XMIT_DATA_REG: 114429318db1SChalapathi V /* 114529318db1SChalapathi V * Writing to the transmit data register causes the transmit data 114629318db1SChalapathi V * register full status bit in the status register to be set. Writing 114729318db1SChalapathi V * when the transmit data register full status bit is already set 114829318db1SChalapathi V * causes a "Resource Not Available" condition. This is not possible 114929318db1SChalapathi V * in the model since writes to this register are not asynchronous to 115029318db1SChalapathi V * the operation sequence like it would be in hardware. 115129318db1SChalapathi V */ 115229318db1SChalapathi V s->regs[reg] = val; 115329318db1SChalapathi V trace_pnv_spi_write_TDR(val); 115429318db1SChalapathi V s->status = SETFIELD(SPI_STS_TDR_FULL, s->status, 1); 115529318db1SChalapathi V s->status = SETFIELD(SPI_STS_TDR_UNDERRUN, s->status, 0); 1156*b4cb930eSChalapathi V trace_pnv_spi_start_sequencer(); 1157*b4cb930eSChalapathi V operation_sequencer(s); 115829318db1SChalapathi V break; 115929318db1SChalapathi V case SPI_SEQ_OP_REG: 116029318db1SChalapathi V for (int i = 0; i < PNV_SPI_REG_SIZE; i++) { 116129318db1SChalapathi V s->seq_op[i] = (val >> (56 - i * 8)) & 0xFF; 116229318db1SChalapathi V } 116329318db1SChalapathi V break; 116429318db1SChalapathi V case SPI_STS_REG: 116529318db1SChalapathi V /* other fields are ignore_write */ 116629318db1SChalapathi V s->status = SETFIELD(SPI_STS_RDR_OVERRUN, s->status, 116729318db1SChalapathi V GETFIELD(SPI_STS_RDR, val)); 116829318db1SChalapathi V s->status = SETFIELD(SPI_STS_TDR_OVERRUN, s->status, 116929318db1SChalapathi V GETFIELD(SPI_STS_TDR, val)); 117029318db1SChalapathi V break; 117129318db1SChalapathi V default: 117229318db1SChalapathi V qemu_log_mask(LOG_GUEST_ERROR, "pnv_spi_regs: Invalid xscom " 117329318db1SChalapathi V "write at 0x%" PRIx32 "\n", reg); 117429318db1SChalapathi V } 117529318db1SChalapathi V return; 117629318db1SChalapathi V } 117729318db1SChalapathi V 117829318db1SChalapathi V static const MemoryRegionOps pnv_spi_xscom_ops = { 117929318db1SChalapathi V .read = pnv_spi_xscom_read, 118029318db1SChalapathi V .write = pnv_spi_xscom_write, 118129318db1SChalapathi V .valid.min_access_size = 8, 118229318db1SChalapathi V .valid.max_access_size = 8, 118329318db1SChalapathi V .impl.min_access_size = 8, 118429318db1SChalapathi V .impl.max_access_size = 8, 118529318db1SChalapathi V .endianness = DEVICE_BIG_ENDIAN, 118629318db1SChalapathi V }; 118729318db1SChalapathi V 118829318db1SChalapathi V static Property pnv_spi_properties[] = { 118929318db1SChalapathi V DEFINE_PROP_UINT32("spic_num", PnvSpi, spic_num, 0), 1190*b4cb930eSChalapathi V DEFINE_PROP_UINT8("transfer_len", PnvSpi, transfer_len, 4), 119129318db1SChalapathi V DEFINE_PROP_END_OF_LIST(), 119229318db1SChalapathi V }; 119329318db1SChalapathi V 119429318db1SChalapathi V static void pnv_spi_realize(DeviceState *dev, Error **errp) 119529318db1SChalapathi V { 119629318db1SChalapathi V PnvSpi *s = PNV_SPI(dev); 119729318db1SChalapathi V g_autofree char *name = g_strdup_printf(TYPE_PNV_SPI_BUS ".%d", 119829318db1SChalapathi V s->spic_num); 119929318db1SChalapathi V s->ssi_bus = ssi_create_bus(dev, name); 120029318db1SChalapathi V s->cs_line = g_new0(qemu_irq, 1); 120129318db1SChalapathi V qdev_init_gpio_out_named(DEVICE(s), s->cs_line, "cs", 1); 120229318db1SChalapathi V 120329318db1SChalapathi V /* spi scoms */ 120429318db1SChalapathi V pnv_xscom_region_init(&s->xscom_spic_regs, OBJECT(s), &pnv_spi_xscom_ops, 120529318db1SChalapathi V s, "xscom-spi", PNV10_XSCOM_PIB_SPIC_SIZE); 120629318db1SChalapathi V } 120729318db1SChalapathi V 120829318db1SChalapathi V static int pnv_spi_dt_xscom(PnvXScomInterface *dev, void *fdt, 120929318db1SChalapathi V int offset) 121029318db1SChalapathi V { 121129318db1SChalapathi V PnvSpi *s = PNV_SPI(dev); 121229318db1SChalapathi V g_autofree char *name; 121329318db1SChalapathi V int s_offset; 121429318db1SChalapathi V const char compat[] = "ibm,power10-spi"; 121529318db1SChalapathi V uint32_t spic_pcba = PNV10_XSCOM_PIB_SPIC_BASE + 121629318db1SChalapathi V s->spic_num * PNV10_XSCOM_PIB_SPIC_SIZE; 121729318db1SChalapathi V uint32_t reg[] = { 121829318db1SChalapathi V cpu_to_be32(spic_pcba), 121929318db1SChalapathi V cpu_to_be32(PNV10_XSCOM_PIB_SPIC_SIZE) 122029318db1SChalapathi V }; 122129318db1SChalapathi V name = g_strdup_printf("pnv_spi@%x", spic_pcba); 122229318db1SChalapathi V s_offset = fdt_add_subnode(fdt, offset, name); 122329318db1SChalapathi V _FDT(s_offset); 122429318db1SChalapathi V 122529318db1SChalapathi V _FDT(fdt_setprop(fdt, s_offset, "reg", reg, sizeof(reg))); 122629318db1SChalapathi V _FDT(fdt_setprop(fdt, s_offset, "compatible", compat, sizeof(compat))); 122729318db1SChalapathi V _FDT((fdt_setprop_cell(fdt, s_offset, "spic_num#", s->spic_num))); 122829318db1SChalapathi V return 0; 122929318db1SChalapathi V } 123029318db1SChalapathi V 123129318db1SChalapathi V static void pnv_spi_class_init(ObjectClass *klass, void *data) 123229318db1SChalapathi V { 123329318db1SChalapathi V DeviceClass *dc = DEVICE_CLASS(klass); 123429318db1SChalapathi V PnvXScomInterfaceClass *xscomc = PNV_XSCOM_INTERFACE_CLASS(klass); 123529318db1SChalapathi V 123629318db1SChalapathi V xscomc->dt_xscom = pnv_spi_dt_xscom; 123729318db1SChalapathi V 123829318db1SChalapathi V dc->desc = "PowerNV SPI"; 123929318db1SChalapathi V dc->realize = pnv_spi_realize; 1240*b4cb930eSChalapathi V dc->reset = do_reset; 124129318db1SChalapathi V device_class_set_props(dc, pnv_spi_properties); 124229318db1SChalapathi V } 124329318db1SChalapathi V 124429318db1SChalapathi V static const TypeInfo pnv_spi_info = { 124529318db1SChalapathi V .name = TYPE_PNV_SPI, 124629318db1SChalapathi V .parent = TYPE_SYS_BUS_DEVICE, 124729318db1SChalapathi V .instance_size = sizeof(PnvSpi), 124829318db1SChalapathi V .class_init = pnv_spi_class_init, 124929318db1SChalapathi V .interfaces = (InterfaceInfo[]) { 125029318db1SChalapathi V { TYPE_PNV_XSCOM_INTERFACE }, 125129318db1SChalapathi V { } 125229318db1SChalapathi V } 125329318db1SChalapathi V }; 125429318db1SChalapathi V 125529318db1SChalapathi V static void pnv_spi_register_types(void) 125629318db1SChalapathi V { 125729318db1SChalapathi V type_register_static(&pnv_spi_info); 125829318db1SChalapathi V } 125929318db1SChalapathi V 126029318db1SChalapathi V type_init(pnv_spi_register_types); 1261