soc/intel/pantherlake: Remove soc_info.[hc] interface
[coreboot2.git] / src / soc / intel / common / block / gspi / gspi.c
blobd37b33062286be39df1ffa4516fb275dd2702b59
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 #include <device/mmio.h>
4 #include <assert.h>
5 #include <bootstate.h>
6 #include <console/console.h>
7 #include <delay.h>
8 #include <device/device.h>
9 #include <device/pci_def.h>
10 #include <device/pci_ops.h>
11 #include <intelblocks/cfg.h>
12 #include <intelblocks/gspi.h>
13 #include <intelblocks/lpss.h>
14 #include <intelblocks/spi.h>
15 #include <soc/iomap.h>
16 #include <soc/pci_devs.h>
17 #include <string.h>
18 #include <timer.h>
20 /* GSPI Memory Mapped Registers */
21 #define SSCR0 0x0 /* SSP Control Register 0 */
22 #define SSCR0_EDSS_0 (0 << 20)
23 #define SSCR0_EDSS_1 (1 << 20)
24 #define SSCR0_SCR_SHIFT (8)
25 #define SSCR0_SCR_MASK (0xFFF)
26 #define SSCR0_SSE_DISABLE (0 << 7)
27 #define SSCR0_SSE_ENABLE (1 << 7)
28 #define SSCR0_ECS_ON_CHIP (0 << 6)
29 #define SSCR0_FRF_MOTOROLA (0 << 4)
30 #define SSCR0_DSS_SHIFT (0)
31 #define SSCR0_DSS_MASK (0xF)
32 #define SSCR1 0x4 /* SSP Control Register 1 */
33 #define SSCR1_IFS_LOW (0 << 16)
34 #define SSCR1_IFS_HIGH (1 << 16)
35 #define SSCR1_SPH_FIRST (0 << 4)
36 #define SSCR1_SPH_SECOND (1 << 4)
37 #define SSCR1_SPO_LOW (0 << 3)
38 #define SSCR1_SPO_HIGH (1 << 3)
39 #define SSSR 0x8 /* SSP Status Register */
40 #define SSSR_TUR (1 << 21) /* Tx FIFO underrun */
41 #define SSSR_TINT (1 << 19) /* Rx Time-out interrupt */
42 #define SSSR_PINT (1 << 18) /* Peripheral trailing byte
43 interrupt */
44 #define SSSR_ROR (1 << 7) /* Rx FIFO Overrun */
45 #define SSSR_BSY (1 << 4) /* SSP Busy */
46 #define SSSR_RNE (1 << 3) /* Receive FIFO not empty */
47 #define SSSR_TNF (1 << 2) /* Transmit FIFO not full */
48 #define SSDR 0x10 /* SSP Data Register */
49 #define SSTO 0x28 /* SSP Time out */
50 #define SITF 0x44 /* SPI Transmit FIFO */
51 #define SITF_LEVEL_SHIFT (16)
52 #define SITF_LEVEL_MASK (0x3f)
53 #define SITF_LWM_SHIFT (8)
54 #define SITF_LWM_MASK (0x3f)
55 #define SITF_LWM(x) ((((x) - 1) & SITF_LWM_MASK) << SITF_LWM_SHIFT)
56 #define SITF_HWM_SHIFT (0)
57 #define SITF_HWM_MASK (0x3f)
58 #define SITF_HWM(x) ((((x) - 1) & SITF_HWM_MASK) << SITF_HWM_SHIFT)
59 #define SIRF 0x48 /* SPI Receive FIFO */
60 #define SIRF_LEVEL_SHIFT (8)
61 #define SIRF_LEVEL_MASK (0x3f)
62 #define SIRF_WM_SHIFT (0)
63 #define SIRF_WM_MASK (0x3f)
64 #define SIRF_WM(x) ((((x) - 1) & SIRF_WM_MASK) << SIRF_WM_SHIFT)
66 /* GSPI Additional Registers */
67 #define CLOCKS 0x200 /* Clocks */
68 #define CLOCKS_UPDATE (1 << 31)
69 #define CLOCKS_N_SHIFT (16)
70 #define CLOCKS_N_MASK (0x7fff)
71 #define CLOCKS_M_SHIFT (1)
72 #define CLOCKS_M_MASK (0x7fff)
73 #define CLOCKS_DISABLE (0 << 0)
74 #define CLOCKS_ENABLE (1 << 0)
75 #define RESETS 0x204 /* Resets */
76 #define DMA_RESET (0 << 2)
77 #define DMA_ACTIVE (1 << 2)
78 #define CTRLR_RESET (0 << 0)
79 #define CTRLR_ACTIVE (3 << 0)
80 #define ACTIVELTR_VALUE 0x210 /* Active LTR */
81 #define IDLELTR_VALUE 0x214 /* Idle LTR Value */
82 #define TX_BIT_COUNT 0x218 /* Tx Bit Count */
83 #define RX_BIT_COUNT 0x21c /* Rx Bit Count */
84 #define SSP_REG 0x220 /* SSP Reg */
85 #define DMA_FINISH_DISABLE (1 << 0)
86 #define SPI_CS_CONTROL 0x224 /* SPI CS Control */
87 #define CS_0_POL_SHIFT (12)
88 #define CS_0_POL_MASK (1 << CS_0_POL_SHIFT)
89 #define CS_POL_LOW (0)
90 #define CS_POL_HIGH (1)
91 #define CS_0 (0 << 8)
92 #define CS_STATE_SHIFT (1)
93 #define CS_STATE_MASK (1 << CS_STATE_SHIFT)
94 #define CS_V1_STATE_LOW (0)
95 #define CS_V1_STATE_HIGH (1)
96 #define CS_MODE_HW (0 << 0)
97 #define CS_MODE_SW (1 << 0)
99 #define GSPI_DATA_BIT_LENGTH (8)
100 #define GSPI_BUS_BASE(bar, bus) ((bar) + (bus) * 4 * KiB)
102 /* Get base address for early init of GSPI controllers. */
103 static uintptr_t gspi_get_early_base(void)
105 return EARLY_GSPI_BASE_ADDRESS;
108 /* Get gspi_config array from devicetree. Returns NULL in case of error. */
109 static const struct gspi_cfg *gspi_get_cfg(void)
111 const struct soc_intel_common_config *common_config;
112 common_config = chip_get_common_soc_structure();
114 return &common_config->gspi[0];
117 #if defined(__SIMPLE_DEVICE__)
119 static uintptr_t gspi_get_base_addr(int devfn,
120 DEVTREE_CONST struct device *dev)
122 pci_devfn_t pci_dev = PCI_DEV(0, PCI_SLOT(devfn), PCI_FUNC(devfn));
123 return ALIGN_DOWN(pci_read_config32(pci_dev, PCI_BASE_ADDRESS_0), 16);
126 static void gspi_set_base_addr(int devfn, DEVTREE_CONST struct device *dev,
127 uintptr_t base)
129 pci_devfn_t pci_dev = PCI_DEV(0, PCI_SLOT(devfn), PCI_FUNC(devfn));
130 pci_write_config32(pci_dev, PCI_BASE_ADDRESS_0, base);
131 pci_write_config32(pci_dev, PCI_COMMAND, PCI_COMMAND_MEMORY |
132 PCI_COMMAND_MASTER);
135 void gspi_early_bar_init(void)
137 unsigned int gspi_bus;
138 const unsigned int gspi_max = CONFIG_SOC_INTEL_COMMON_BLOCK_GSPI_MAX;
139 const struct gspi_cfg *cfg = gspi_get_cfg();
140 int devfn;
141 uintptr_t gspi_base_addr;
143 assert(gspi_max != 0);
144 if (!cfg) {
145 printk(BIOS_ERR, "%s: No GSPI config provided by SoC!\n",
146 __func__);
147 return;
150 gspi_base_addr = gspi_get_early_base();
151 if (!gspi_base_addr) {
152 printk(BIOS_ERR, "%s: GSPI base address provided is NULL!\n",
153 __func__);
154 return;
157 for (gspi_bus = 0; gspi_bus < gspi_max; gspi_bus++) {
158 if (!cfg[gspi_bus].early_init)
159 continue;
160 devfn = gspi_soc_bus_to_devfn(gspi_bus);
161 gspi_set_base_addr(devfn, NULL,
162 GSPI_BUS_BASE(gspi_base_addr, gspi_bus));
166 #else
168 static uintptr_t gspi_get_base_addr(int devfn, struct device *dev)
170 return ALIGN_DOWN(pci_read_config32(dev, PCI_BASE_ADDRESS_0), 16);
173 static void gspi_set_base_addr(int devfn, struct device *dev, uintptr_t base)
175 pci_write_config32(dev, PCI_BASE_ADDRESS_0, base);
176 pci_write_config32(dev, PCI_COMMAND, PCI_COMMAND_MEMORY |
177 PCI_COMMAND_MASTER);
180 #endif
182 static int gspi_read_bus_range(unsigned int *start, unsigned int *end)
184 size_t i;
185 const struct spi_ctrlr_buses *desc;
187 for (i = 0; i < spi_ctrlr_bus_map_count; i++) {
188 desc = &spi_ctrlr_bus_map[i];
190 if (desc->ctrlr != &gspi_ctrlr)
191 continue;
193 *start = desc->bus_start;
194 *end = desc->bus_end;
196 return 0;
198 return -1;
201 static int gspi_spi_to_gspi_bus(unsigned int spi_bus, unsigned int *gspi_bus)
203 unsigned int start;
204 unsigned int end;
205 int ret;
207 ret = gspi_read_bus_range(&start, &end);
209 if (ret != 0 || (spi_bus < start) || (spi_bus > end))
210 return -1;
212 *gspi_bus = spi_bus - start;
214 return 0;
217 static uintptr_t gspi_calc_base_addr(unsigned int gspi_bus)
219 uintptr_t bus_base, gspi_base_addr;
220 DEVTREE_CONST struct device *dev;
221 int devfn = gspi_soc_bus_to_devfn(gspi_bus);
223 if (devfn < 0)
224 return 0;
226 dev = pcidev_path_on_root(devfn);
227 if (!dev || !dev->enabled)
228 return 0;
230 bus_base = gspi_get_base_addr(devfn, dev);
231 if (bus_base)
232 return bus_base;
234 gspi_base_addr = gspi_get_early_base();
235 if (!gspi_base_addr)
236 return 0;
238 bus_base = GSPI_BUS_BASE(gspi_base_addr, gspi_bus);
240 gspi_set_base_addr(devfn, dev, bus_base);
241 return bus_base;
244 static uint32_t gspi_get_bus_clk_mhz(unsigned int gspi_bus)
246 const struct gspi_cfg *cfg = gspi_get_cfg();
247 if (!cfg)
248 return 0;
249 return cfg[gspi_bus].speed_mhz;
252 static uintptr_t gspi_base[CONFIG_SOC_INTEL_COMMON_BLOCK_GSPI_MAX];
253 static uintptr_t gspi_get_bus_base_addr(unsigned int gspi_bus)
255 if (!gspi_base[gspi_bus])
256 gspi_base[gspi_bus] = gspi_calc_base_addr(gspi_bus);
258 return gspi_base[gspi_bus];
262 * PCI resource allocation will likely change the base address of the mapped
263 * I/O registers. Clearing the cached value after the allocation step will
264 * cause it to be recomputed by gspi_calc_base_addr() on next access.
266 static void gspi_clear_cached_base(void *unused)
268 memset(gspi_base, 0, sizeof(gspi_base));
270 BOOT_STATE_INIT_ENTRY(BS_DEV_RESOURCES, BS_ON_EXIT, gspi_clear_cached_base, NULL);
272 /* Parameters for GSPI controller operation. */
273 struct gspi_ctrlr_params {
274 uintptr_t mmio_base;
275 unsigned int gspi_bus;
276 uint8_t *in;
277 size_t bytesin;
278 const uint8_t *out;
279 size_t bytesout;
282 static uint32_t gspi_read_mmio_reg(const struct gspi_ctrlr_params *p,
283 uint32_t offset)
285 assert(p->mmio_base != 0);
286 return read32p(p->mmio_base + offset);
289 static void gspi_write_mmio_reg(const struct gspi_ctrlr_params *p,
290 uint32_t offset, uint32_t value)
292 assert(p->mmio_base != 0);
293 write32p(p->mmio_base + offset, value);
296 static int gspi_ctrlr_params_init(struct gspi_ctrlr_params *p,
297 unsigned int spi_bus)
299 memset(p, 0, sizeof(*p));
301 if (gspi_spi_to_gspi_bus(spi_bus, &p->gspi_bus)) {
302 printk(BIOS_ERR, "%s: No GSPI bus available for SPI bus %u.\n",
303 __func__, spi_bus);
304 return -1;
307 p->mmio_base = gspi_get_bus_base_addr(p->gspi_bus);
308 if (!p->mmio_base) {
309 printk(BIOS_ERR, "%s: Base addr is 0 for GSPI bus=%u.\n",
310 __func__, p->gspi_bus);
311 return -1;
314 return 0;
317 enum cs_assert {
318 CS_ASSERT,
319 CS_DEASSERT,
323 * SPI_CS_CONTROL bit definitions based on GSPI_VERSION_x:
325 * VERSION_2 (CNL GSPI controller):
326 * Polarity: Indicates inactive polarity of chip-select
327 * State : Indicates assert/de-assert of chip-select
329 * Default (SKL/KBL GSPI controller):
330 * Polarity: Indicates active polarity of chip-select
331 * State : Indicates low/high output state of chip-select
333 static uint32_t gspi_csctrl_state_v2(uint32_t pol, enum cs_assert cs_assert)
335 return cs_assert;
338 static uint32_t gspi_csctrl_state_v1(uint32_t pol, enum cs_assert cs_assert)
340 uint32_t state;
342 if (pol == CS_POL_HIGH)
343 state = (cs_assert == CS_ASSERT) ? CS_V1_STATE_HIGH :
344 CS_V1_STATE_LOW;
345 else
346 state = (cs_assert == CS_ASSERT) ? CS_V1_STATE_LOW :
347 CS_V1_STATE_HIGH;
349 return state;
352 static uint32_t gspi_csctrl_state(uint32_t pol, enum cs_assert cs_assert)
354 if (CONFIG(SOC_INTEL_COMMON_BLOCK_GSPI_VERSION_2))
355 return gspi_csctrl_state_v2(pol, cs_assert);
357 return gspi_csctrl_state_v1(pol, cs_assert);
360 static uint32_t gspi_csctrl_polarity_v2(enum spi_polarity active_pol)
362 /* Polarity field indicates cs inactive polarity */
363 if (active_pol == SPI_POLARITY_LOW)
364 return CS_POL_HIGH;
365 return CS_POL_LOW;
368 static uint32_t gspi_csctrl_polarity_v1(enum spi_polarity active_pol)
370 /* Polarity field indicates cs active polarity */
371 if (active_pol == SPI_POLARITY_LOW)
372 return CS_POL_LOW;
373 return CS_POL_HIGH;
376 static uint32_t gspi_csctrl_polarity(enum spi_polarity active_pol)
378 if (CONFIG(SOC_INTEL_COMMON_BLOCK_GSPI_VERSION_2))
379 return gspi_csctrl_polarity_v2(active_pol);
381 return gspi_csctrl_polarity_v1(active_pol);
384 static void __gspi_cs_change(const struct gspi_ctrlr_params *p,
385 enum cs_assert cs_assert)
387 uint32_t cs_ctrl, pol;
388 cs_ctrl = gspi_read_mmio_reg(p, SPI_CS_CONTROL);
390 cs_ctrl &= ~CS_STATE_MASK;
392 pol = !!(cs_ctrl & CS_0_POL_MASK);
393 cs_ctrl |= gspi_csctrl_state(pol, cs_assert) << CS_STATE_SHIFT;
395 gspi_write_mmio_reg(p, SPI_CS_CONTROL, cs_ctrl);
398 static int gspi_cs_change(const struct spi_slave *dev, enum cs_assert cs_assert)
400 struct gspi_ctrlr_params params, *p = &params;
402 if (gspi_ctrlr_params_init(p, dev->bus))
403 return -1;
405 __gspi_cs_change(p, cs_assert);
407 return 0;
410 int __weak gspi_get_soc_spi_cfg(unsigned int gspi_bus,
411 struct spi_cfg *cfg)
413 cfg->clk_phase = SPI_CLOCK_PHASE_FIRST;
414 cfg->clk_polarity = SPI_POLARITY_LOW;
415 cfg->cs_polarity = SPI_POLARITY_LOW;
416 cfg->wire_mode = SPI_4_WIRE_MODE;
417 cfg->data_bit_length = GSPI_DATA_BIT_LENGTH;
419 return 0;
422 static int gspi_cs_assert(const struct spi_slave *dev)
424 return gspi_cs_change(dev, CS_ASSERT);
427 static void gspi_cs_deassert(const struct spi_slave *dev)
429 gspi_cs_change(dev, CS_DEASSERT);
432 static uint32_t gspi_get_clk_div(unsigned int gspi_bus)
434 const uint32_t ref_clk_mhz =
435 CONFIG_SOC_INTEL_COMMON_BLOCK_GSPI_CLOCK_MHZ;
436 uint32_t gspi_clk_mhz = gspi_get_bus_clk_mhz(gspi_bus);
438 if (!gspi_clk_mhz)
439 gspi_clk_mhz = 1;
441 assert(ref_clk_mhz != 0);
442 return (DIV_ROUND_UP(ref_clk_mhz, gspi_clk_mhz) - 1) & SSCR0_SCR_MASK;
445 static int gspi_ctrlr_setup(const struct spi_slave *dev)
447 struct spi_cfg cfg;
448 int devfn;
449 uint32_t cs_ctrl, sscr0, sscr1, clocks, sitf, sirf, pol;
450 struct gspi_ctrlr_params params, *p = &params;
452 /* Only chip select 0 is supported. */
453 if (dev->cs != 0) {
454 printk(BIOS_ERR, "%s: Invalid CS value: cs=%u.\n", __func__,
455 dev->cs);
456 return -1;
459 if (gspi_ctrlr_params_init(p, dev->bus))
460 return -1;
462 /* Obtain SPI bus configuration for the device. */
463 if (gspi_get_soc_spi_cfg(p->gspi_bus, &cfg)) {
464 printk(BIOS_ERR, "%s: Failed to get config for bus=%u.\n",
465 __func__, p->gspi_bus);
466 return -1;
469 devfn = gspi_soc_bus_to_devfn(p->gspi_bus);
471 /* Ensure controller is in D0 state */
472 lpss_set_power_state(PCI_DEV(0, PCI_SLOT(devfn), PCI_FUNC(devfn)), STATE_D0);
474 /* Take controller out of reset, keeping DMA in reset. */
475 gspi_write_mmio_reg(p, RESETS, CTRLR_ACTIVE | DMA_RESET);
478 * CS control:
479 * - Set SW mode.
480 * - Set chip select to 0.
481 * - Set polarity based on device configuration.
482 * - Do not assert CS.
484 cs_ctrl = CS_MODE_SW | CS_0;
485 pol = gspi_csctrl_polarity(cfg.cs_polarity);
486 cs_ctrl |= pol << CS_0_POL_SHIFT;
487 cs_ctrl |= gspi_csctrl_state(pol, CS_DEASSERT) << CS_STATE_SHIFT;
488 gspi_write_mmio_reg(p, SPI_CS_CONTROL, cs_ctrl);
490 /* Disable SPI controller. */
491 gspi_write_mmio_reg(p, SSCR0, SSCR0_SSE_DISABLE);
494 * SSCR0 configuration:
495 * clk_div - Based on reference clock and expected clock frequency.
496 * data bit length - assumed to be 8, hence EDSS = 0.
497 * ECS - Use on-chip clock
498 * FRF - Frame format set to Motorola SPI
500 sscr0 = gspi_get_clk_div(p->gspi_bus) << SSCR0_SCR_SHIFT;
501 assert(GSPI_DATA_BIT_LENGTH == 8);
502 sscr0 |= ((GSPI_DATA_BIT_LENGTH - 1) << SSCR0_DSS_SHIFT) | SSCR0_EDSS_0;
503 sscr0 |= SSCR0_ECS_ON_CHIP | SSCR0_FRF_MOTOROLA;
504 gspi_write_mmio_reg(p, SSCR0, sscr0);
507 * SSCR1 configuration:
508 * - Chip select polarity
509 * - Clock phase setting
510 * - Clock polarity
512 sscr1 = (cfg.cs_polarity == SPI_POLARITY_LOW) ? SSCR1_IFS_LOW :
513 SSCR1_IFS_HIGH;
514 sscr1 |= (cfg.clk_phase == SPI_CLOCK_PHASE_FIRST) ? SSCR1_SPH_FIRST :
515 SSCR1_SPH_SECOND;
516 sscr1 |= (cfg.clk_polarity == SPI_POLARITY_LOW) ? SSCR1_SPO_LOW :
517 SSCR1_SPO_HIGH;
518 gspi_write_mmio_reg(p, SSCR1, sscr1);
521 * Program m/n divider.
522 * Set m and n to 1, so that this divider acts as a pass-through.
524 clocks = (1 << CLOCKS_N_SHIFT) | (1 << CLOCKS_M_SHIFT) | CLOCKS_ENABLE |
525 CLOCKS_UPDATE;
526 gspi_write_mmio_reg(p, CLOCKS, clocks);
527 udelay(10);
530 * Tx FIFO Threshold.
531 * Low watermark threshold = 1
532 * High watermark threshold = 1
534 sitf = SITF_LWM(1) | SITF_HWM(1);
535 gspi_write_mmio_reg(p, SITF, sitf);
537 /* Rx FIFO Threshold (set to 1). */
538 sirf = SIRF_WM(1);
539 gspi_write_mmio_reg(p, SIRF, sirf);
541 /* Enable GSPI controller. */
542 sscr0 |= SSCR0_SSE_ENABLE;
543 gspi_write_mmio_reg(p, SSCR0, sscr0);
545 return 0;
548 static uint32_t gspi_read_status(const struct gspi_ctrlr_params *p)
550 return gspi_read_mmio_reg(p, SSSR);
553 static void gspi_clear_status(const struct gspi_ctrlr_params *p)
555 const uint32_t sssr = SSSR_TUR | SSSR_TINT | SSSR_PINT | SSSR_ROR;
556 gspi_write_mmio_reg(p, SSSR, sssr);
559 static bool gspi_rx_fifo_empty(const struct gspi_ctrlr_params *p)
561 return !(gspi_read_status(p) & SSSR_RNE);
564 static bool gspi_tx_fifo_full(const struct gspi_ctrlr_params *p)
566 return !(gspi_read_status(p) & SSSR_TNF);
569 static bool gspi_rx_fifo_overrun(const struct gspi_ctrlr_params *p)
571 if (gspi_read_status(p) & SSSR_ROR) {
572 printk(BIOS_ERR, "%s:GSPI receive FIFO overrun!"
573 " (bus=%u).\n", __func__, p->gspi_bus);
574 return true;
577 return false;
580 /* Read SSDR and return lowest byte. */
581 static uint8_t gspi_read_byte(const struct gspi_ctrlr_params *p)
583 return gspi_read_mmio_reg(p, SSDR) & 0xFF;
586 /* Write 32-bit word with "data" in lowest byte to SSDR. */
587 static void gspi_write_byte(const struct gspi_ctrlr_params *p, uint8_t data)
589 return gspi_write_mmio_reg(p, SSDR, data);
592 static void gspi_read_data(struct gspi_ctrlr_params *p)
594 *(p->in) = gspi_read_byte(p);
595 p->in++;
596 p->bytesin--;
599 static void gspi_write_data(struct gspi_ctrlr_params *p)
601 gspi_write_byte(p, *(p->out));
602 p->out++;
603 p->bytesout--;
606 static void gspi_read_dummy(struct gspi_ctrlr_params *p)
608 gspi_read_byte(p);
609 p->bytesin--;
612 static void gspi_write_dummy(struct gspi_ctrlr_params *p)
614 gspi_write_byte(p, 0);
615 p->bytesout--;
618 static int gspi_ctrlr_flush(const struct gspi_ctrlr_params *p)
620 const uint32_t timeout_ms = 500;
621 struct stopwatch sw;
623 /* Wait 500ms to allow Rx FIFO to be empty. */
624 stopwatch_init_msecs_expire(&sw, timeout_ms);
626 while (!gspi_rx_fifo_empty(p)) {
627 if (stopwatch_expired(&sw)) {
628 printk(BIOS_ERR, "%s: Rx FIFO not empty after 500ms! "
629 "(bus=%u)\n", __func__, p->gspi_bus);
630 return -1;
633 gspi_read_byte(p);
636 return 0;
639 static int __gspi_xfer(struct gspi_ctrlr_params *p)
642 * If bytesin is non-zero, then use gspi_read_data to perform
643 * byte-by-byte read of data from SSDR and save it to "in" buffer. Else
644 * discard the read data using gspi_read_dummy.
646 void (*fn_read)(struct gspi_ctrlr_params *p) = gspi_read_data;
649 * If bytesout is non-zero, then use gspi_write_data to perform
650 * byte-by-byte write of data from "out" buffer to SSDR. Else, use
651 * gspi_write_dummy to write dummy "0" data to SSDR in order to trigger
652 * read from slave.
654 void (*fn_write)(struct gspi_ctrlr_params *p) = gspi_write_data;
656 if (!p->bytesin) {
657 p->bytesin = p->bytesout;
658 fn_read = gspi_read_dummy;
661 if (!p->bytesout) {
662 p->bytesout = p->bytesin;
663 fn_write = gspi_write_dummy;
666 while (p->bytesout || p->bytesin) {
667 if (p->bytesout && !gspi_tx_fifo_full(p))
668 fn_write(p);
669 if (p->bytesin && !gspi_rx_fifo_empty(p)) {
670 if (gspi_rx_fifo_overrun(p))
671 return -1;
672 fn_read(p);
676 return 0;
679 static int gspi_ctrlr_xfer(const struct spi_slave *dev,
680 const void *dout, size_t bytesout,
681 void *din, size_t bytesin)
683 struct gspi_ctrlr_params params;
684 struct gspi_ctrlr_params *p = &params;
687 * Assumptions about in and out transfers:
688 * 1. Both bytesin and bytesout cannot be 0.
689 * 2. If both bytesin and bytesout are non-zero, then they should be
690 * equal i.e. if both in and out transfers are to be done in same
691 * transaction, then they should be equal in length.
692 * 3. Buffer corresponding to non-zero bytes (bytesin/bytesout) cannot
693 * be NULL.
695 if (!bytesin && !bytesout) {
696 printk(BIOS_ERR, "%s: Both in and out bytes cannot be zero!\n",
697 __func__);
698 return -1;
699 } else if (bytesin && bytesout && (bytesin != bytesout)) {
700 printk(BIOS_ERR, "%s: bytesin(%zd) != bytesout(%zd)\n",
701 __func__, bytesin, bytesout);
702 return -1;
704 if ((bytesin && !din) || (bytesout && !dout)) {
705 printk(BIOS_ERR, "%s: in/out buffer is NULL!\n", __func__);
706 return -1;
709 if (gspi_ctrlr_params_init(p, dev->bus))
710 return -1;
712 /* Flush out any stale data in Rx FIFO. */
713 if (gspi_ctrlr_flush(p))
714 return -1;
716 /* Clear status bits. */
717 gspi_clear_status(p);
719 p->in = din;
720 p->bytesin = bytesin;
721 p->out = dout;
722 p->bytesout = bytesout;
724 return __gspi_xfer(p);
727 const struct spi_ctrlr gspi_ctrlr = {
728 .claim_bus = gspi_cs_assert,
729 .release_bus = gspi_cs_deassert,
730 .setup = gspi_ctrlr_setup,
731 .xfer = gspi_ctrlr_xfer,
732 .max_xfer_size = SPI_CTRLR_DEFAULT_MAX_XFER_SIZE,