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