soc: Remove copyright notices
[coreboot.git] / src / soc / intel / common / block / gspi / gspi.c
blob148b815ea3e2b96e843a2186eb23e4a6c47e8fc2
1 /*
2 * This file is part of the coreboot project.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #include <device/mmio.h>
17 #include <assert.h>
18 #include <console/console.h>
19 #include <delay.h>
20 #include <device/device.h>
21 #include <device/pci_def.h>
22 #include <device/pci_ops.h>
23 #include <intelblocks/cfg.h>
24 #include <intelblocks/gspi.h>
25 #include <intelblocks/lpss.h>
26 #include <intelblocks/spi.h>
27 #include <soc/iomap.h>
28 #include <soc/pci_devs.h>
29 #include <string.h>
30 #include <timer.h>
32 /* GSPI Memory Mapped Registers */
33 #define SSCR0 0x0 /* SSP Control Register 0 */
34 #define SSCR0_EDSS_0 (0 << 20)
35 #define SSCR0_EDSS_1 (1 << 20)
36 #define SSCR0_SCR_SHIFT (8)
37 #define SSCR0_SCR_MASK (0xFFF)
38 #define SSCR0_SSE_DISABLE (0 << 7)
39 #define SSCR0_SSE_ENABLE (1 << 7)
40 #define SSCR0_ECS_ON_CHIP (0 << 6)
41 #define SSCR0_FRF_MOTOROLA (0 << 4)
42 #define SSCR0_DSS_SHIFT (0)
43 #define SSCR0_DSS_MASK (0xF)
44 #define SSCR1 0x4 /* SSP Control Register 1 */
45 #define SSCR1_IFS_LOW (0 << 16)
46 #define SSCR1_IFS_HIGH (1 << 16)
47 #define SSCR1_SPH_FIRST (0 << 4)
48 #define SSCR1_SPH_SECOND (1 << 4)
49 #define SSCR1_SPO_LOW (0 << 3)
50 #define SSCR1_SPO_HIGH (1 << 3)
51 #define SSSR 0x8 /* SSP Status Register */
52 #define SSSR_TUR (1 << 21) /* Tx FIFO underrun */
53 #define SSSR_TINT (1 << 19) /* Rx Time-out interrupt */
54 #define SSSR_PINT (1 << 18) /* Peripheral trailing byte
55 interrupt */
56 #define SSSR_ROR (1 << 7) /* Rx FIFO Overrun */
57 #define SSSR_BSY (1 << 4) /* SSP Busy */
58 #define SSSR_RNE (1 << 3) /* Receive FIFO not empty */
59 #define SSSR_TNF (1 << 2) /* Transmit FIFO not full */
60 #define SSDR 0x10 /* SSP Data Register */
61 #define SSTO 0x28 /* SSP Time out */
62 #define SITF 0x44 /* SPI Transmit FIFO */
63 #define SITF_LEVEL_SHIFT (16)
64 #define SITF_LEVEL_MASK (0x3f)
65 #define SITF_LWM_SHIFT (8)
66 #define SITF_LWM_MASK (0x3f)
67 #define SITF_LWM(x) ((((x) - 1) & SITF_LWM_MASK) << SITF_LWM_SHIFT)
68 #define SITF_HWM_SHIFT (0)
69 #define SITF_HWM_MASK (0x3f)
70 #define SITF_HWM(x) ((((x) - 1) & SITF_HWM_MASK) << SITF_HWM_SHIFT)
71 #define SIRF 0x48 /* SPI Receive FIFO */
72 #define SIRF_LEVEL_SHIFT (8)
73 #define SIRF_LEVEL_MASK (0x3f)
74 #define SIRF_WM_SHIFT (0)
75 #define SIRF_WM_MASK (0x3f)
76 #define SIRF_WM(x) ((((x) - 1) & SIRF_WM_MASK) << SIRF_WM_SHIFT)
78 /* GSPI Additional Registers */
79 #define CLOCKS 0x200 /* Clocks */
80 #define CLOCKS_UPDATE (1 << 31)
81 #define CLOCKS_N_SHIFT (16)
82 #define CLOCKS_N_MASK (0x7fff)
83 #define CLOCKS_M_SHIFT (1)
84 #define CLOCKS_M_MASK (0x7fff)
85 #define CLOCKS_DISABLE (0 << 0)
86 #define CLOCKS_ENABLE (1 << 0)
87 #define RESETS 0x204 /* Resets */
88 #define DMA_RESET (0 << 2)
89 #define DMA_ACTIVE (1 << 2)
90 #define CTRLR_RESET (0 << 0)
91 #define CTRLR_ACTIVE (3 << 0)
92 #define ACTIVELTR_VALUE 0x210 /* Active LTR */
93 #define IDLELTR_VALUE 0x214 /* Idle LTR Value */
94 #define TX_BIT_COUNT 0x218 /* Tx Bit Count */
95 #define RX_BIT_COUNT 0x21c /* Rx Bit Count */
96 #define SSP_REG 0x220 /* SSP Reg */
97 #define DMA_FINISH_DISABLE (1 << 0)
98 #define SPI_CS_CONTROL 0x224 /* SPI CS Control */
99 #define CS_0_POL_SHIFT (12)
100 #define CS_0_POL_MASK (1 << CS_0_POL_SHIFT)
101 #define CS_POL_LOW (0)
102 #define CS_POL_HIGH (1)
103 #define CS_0 (0 << 8)
104 #define CS_STATE_SHIFT (1)
105 #define CS_STATE_MASK (1 << CS_STATE_SHIFT)
106 #define CS_V1_STATE_LOW (0)
107 #define CS_V1_STATE_HIGH (1)
108 #define CS_MODE_HW (0 << 0)
109 #define CS_MODE_SW (1 << 0)
111 #define GSPI_DATA_BIT_LENGTH (8)
112 #define GSPI_BUS_BASE(bar, bus) ((bar) + (bus) * 4 * KiB)
114 /* Get base address for early init of GSPI controllers. */
115 static uintptr_t gspi_get_early_base(void)
117 return EARLY_GSPI_BASE_ADDRESS;
120 /* Get gspi_config array from devicetree. Returns NULL in case of error. */
121 static const struct gspi_cfg *gspi_get_cfg(void)
123 const struct soc_intel_common_config *common_config;
124 common_config = chip_get_common_soc_structure();
126 return &common_config->gspi[0];
129 #if defined(__SIMPLE_DEVICE__)
131 static uintptr_t gspi_get_base_addr(int devfn,
132 DEVTREE_CONST struct device *dev)
134 pci_devfn_t pci_dev = PCI_DEV(0, PCI_SLOT(devfn), PCI_FUNC(devfn));
135 return ALIGN_DOWN(pci_read_config32(pci_dev, PCI_BASE_ADDRESS_0), 16);
138 static void gspi_set_base_addr(int devfn, DEVTREE_CONST struct device *dev,
139 uintptr_t base)
141 pci_devfn_t pci_dev = PCI_DEV(0, PCI_SLOT(devfn), PCI_FUNC(devfn));
142 pci_write_config32(pci_dev, PCI_BASE_ADDRESS_0, base);
143 pci_write_config32(pci_dev, PCI_COMMAND, PCI_COMMAND_MEMORY |
144 PCI_COMMAND_MASTER);
147 void gspi_early_bar_init(void)
149 unsigned int gspi_bus;
150 const unsigned int gspi_max = CONFIG_SOC_INTEL_COMMON_BLOCK_GSPI_MAX;
151 const struct gspi_cfg *cfg = gspi_get_cfg();
152 int devfn;
153 uintptr_t gspi_base_addr;
155 assert(gspi_max != 0);
156 if (!cfg) {
157 printk(BIOS_ERR, "%s: No GSPI config provided by SoC!\n",
158 __func__);
159 return;
162 gspi_base_addr = gspi_get_early_base();
163 if (!gspi_base_addr) {
164 printk(BIOS_ERR, "%s: GSPI base address provided is NULL!\n",
165 __func__);
166 return;
169 for (gspi_bus = 0; gspi_bus < gspi_max; gspi_bus++) {
170 if (!cfg[gspi_bus].early_init)
171 continue;
172 devfn = gspi_soc_bus_to_devfn(gspi_bus);
173 gspi_set_base_addr(devfn, NULL,
174 GSPI_BUS_BASE(gspi_base_addr, gspi_bus));
178 #else
180 static uintptr_t gspi_get_base_addr(int devfn, struct device *dev)
182 return ALIGN_DOWN(pci_read_config32(dev, PCI_BASE_ADDRESS_0), 16);
185 static void gspi_set_base_addr(int devfn, struct device *dev, uintptr_t base)
187 pci_write_config32(dev, PCI_BASE_ADDRESS_0, base);
188 pci_write_config32(dev, PCI_COMMAND, PCI_COMMAND_MEMORY |
189 PCI_COMMAND_MASTER);
192 #endif
194 static int gspi_read_bus_range(unsigned int *start, unsigned int *end)
196 size_t i;
197 const struct spi_ctrlr_buses *desc;
199 for (i = 0; i < spi_ctrlr_bus_map_count; i++) {
200 desc = &spi_ctrlr_bus_map[i];
202 if (desc->ctrlr != &gspi_ctrlr)
203 continue;
205 *start = desc->bus_start;
206 *end = desc->bus_end;
208 return 0;
210 return -1;
213 static int gspi_spi_to_gspi_bus(unsigned int spi_bus, unsigned int *gspi_bus)
215 unsigned int start;
216 unsigned int end;
217 int ret;
219 ret = gspi_read_bus_range(&start, &end);
221 if (ret != 0 || (spi_bus < start) || (spi_bus > end))
222 return -1;
224 *gspi_bus = spi_bus - start;
226 return 0;
229 static uintptr_t gspi_calc_base_addr(unsigned int gspi_bus)
231 uintptr_t bus_base, gspi_base_addr;
232 DEVTREE_CONST struct device *dev;
233 int devfn = gspi_soc_bus_to_devfn(gspi_bus);
235 if (devfn < 0)
236 return 0;
238 dev = pcidev_path_on_root(devfn);
239 if (!dev || !dev->enabled)
240 return 0;
242 bus_base = gspi_get_base_addr(devfn, dev);
243 if (bus_base)
244 return bus_base;
246 gspi_base_addr = gspi_get_early_base();
247 if (!gspi_base_addr)
248 return 0;
250 bus_base = GSPI_BUS_BASE(gspi_base_addr, gspi_bus);
252 gspi_set_base_addr(devfn, dev, bus_base);
253 return bus_base;
256 static uint32_t gspi_get_bus_clk_mhz(unsigned int gspi_bus)
258 const struct gspi_cfg *cfg = gspi_get_cfg();
259 if (!cfg)
260 return 0;
261 return cfg[gspi_bus].speed_mhz;
264 static uintptr_t gspi_base[CONFIG_SOC_INTEL_COMMON_BLOCK_GSPI_MAX];
265 static uintptr_t gspi_get_bus_base_addr(unsigned int gspi_bus)
267 if (!gspi_base[gspi_bus])
268 gspi_base[gspi_bus] = gspi_calc_base_addr(gspi_bus);
270 return gspi_base[gspi_bus];
273 /* Parameters for GSPI controller operation. */
274 struct gspi_ctrlr_params {
275 uintptr_t mmio_base;
276 unsigned int gspi_bus;
277 uint8_t *in;
278 size_t bytesin;
279 const uint8_t *out;
280 size_t bytesout;
283 static uint32_t gspi_read_mmio_reg(const struct gspi_ctrlr_params *p,
284 uint32_t offset)
286 assert(p->mmio_base != 0);
287 return read32((void *)(p->mmio_base + offset));
290 static void gspi_write_mmio_reg(const struct gspi_ctrlr_params *p,
291 uint32_t offset, uint32_t value)
293 assert(p->mmio_base != 0);
294 write32((void *)(p->mmio_base + offset), value);
297 static int gspi_ctrlr_params_init(struct gspi_ctrlr_params *p,
298 unsigned int spi_bus)
300 memset(p, 0, sizeof(*p));
302 if (gspi_spi_to_gspi_bus(spi_bus, &p->gspi_bus)) {
303 printk(BIOS_ERR, "%s: No GSPI bus available for SPI bus %u.\n",
304 __func__, spi_bus);
305 return -1;
308 p->mmio_base = gspi_get_bus_base_addr(p->gspi_bus);
309 if (!p->mmio_base) {
310 printk(BIOS_ERR, "%s: Base addr is 0 for GSPI bus=%u.\n",
311 __func__, p->gspi_bus);
312 return -1;
315 return 0;
318 enum cs_assert {
319 CS_ASSERT,
320 CS_DEASSERT,
324 * SPI_CS_CONTROL bit definitions based on GSPI_VERSION_x:
326 * VERSION_2 (CNL GSPI controller):
327 * Polarity: Indicates inactive polarity of chip-select
328 * State : Indicates assert/de-assert of chip-select
330 * Default (SKL/KBL GSPI controller):
331 * Polarity: Indicates active polarity of chip-select
332 * State : Indicates low/high output state of chip-select
334 static uint32_t gspi_csctrl_state_v2(uint32_t pol, enum cs_assert cs_assert)
336 return cs_assert;
339 static uint32_t gspi_csctrl_state_v1(uint32_t pol, enum cs_assert cs_assert)
341 uint32_t state;
343 if (pol == CS_POL_HIGH)
344 state = (cs_assert == CS_ASSERT) ? CS_V1_STATE_HIGH :
345 CS_V1_STATE_LOW;
346 else
347 state = (cs_assert == CS_ASSERT) ? CS_V1_STATE_LOW :
348 CS_V1_STATE_HIGH;
350 return state;
353 static uint32_t gspi_csctrl_state(uint32_t pol, enum cs_assert cs_assert)
355 if (CONFIG(SOC_INTEL_COMMON_BLOCK_GSPI_VERSION_2))
356 return gspi_csctrl_state_v2(pol, cs_assert);
358 return gspi_csctrl_state_v1(pol, cs_assert);
361 static uint32_t gspi_csctrl_polarity_v2(enum spi_polarity active_pol)
363 /* Polarity field indicates cs inactive polarity */
364 if (active_pol == SPI_POLARITY_LOW)
365 return CS_POL_HIGH;
366 return CS_POL_LOW;
369 static uint32_t gspi_csctrl_polarity_v1(enum spi_polarity active_pol)
371 /* Polarity field indicates cs active polarity */
372 if (active_pol == SPI_POLARITY_LOW)
373 return CS_POL_LOW;
374 return CS_POL_HIGH;
377 static uint32_t gspi_csctrl_polarity(enum spi_polarity active_pol)
379 if (CONFIG(SOC_INTEL_COMMON_BLOCK_GSPI_VERSION_2))
380 return gspi_csctrl_polarity_v2(active_pol);
382 return gspi_csctrl_polarity_v1(active_pol);
385 static void __gspi_cs_change(const struct gspi_ctrlr_params *p,
386 enum cs_assert cs_assert)
388 uint32_t cs_ctrl, pol;
389 cs_ctrl = gspi_read_mmio_reg(p, SPI_CS_CONTROL);
391 cs_ctrl &= ~CS_STATE_MASK;
393 pol = !!(cs_ctrl & CS_0_POL_MASK);
394 cs_ctrl |= gspi_csctrl_state(pol, cs_assert) << CS_STATE_SHIFT;
396 gspi_write_mmio_reg(p, SPI_CS_CONTROL, cs_ctrl);
399 static int gspi_cs_change(const struct spi_slave *dev, enum cs_assert cs_assert)
401 struct gspi_ctrlr_params params, *p = &params;
403 if (gspi_ctrlr_params_init(p, dev->bus))
404 return -1;
406 __gspi_cs_change(p, cs_assert);
408 return 0;
411 int __weak gspi_get_soc_spi_cfg(unsigned int gspi_bus,
412 struct spi_cfg *cfg)
414 cfg->clk_phase = SPI_CLOCK_PHASE_FIRST;
415 cfg->clk_polarity = SPI_POLARITY_LOW;
416 cfg->cs_polarity = SPI_POLARITY_LOW;
417 cfg->wire_mode = SPI_4_WIRE_MODE;
418 cfg->data_bit_length = GSPI_DATA_BIT_LENGTH;
420 return 0;
423 static int gspi_cs_assert(const struct spi_slave *dev)
425 return gspi_cs_change(dev, CS_ASSERT);
428 static void gspi_cs_deassert(const struct spi_slave *dev)
430 gspi_cs_change(dev, CS_DEASSERT);
433 static uint32_t gspi_get_clk_div(unsigned int gspi_bus)
435 const uint32_t ref_clk_mhz =
436 CONFIG_SOC_INTEL_COMMON_BLOCK_GSPI_CLOCK_MHZ;
437 uint32_t gspi_clk_mhz = gspi_get_bus_clk_mhz(gspi_bus);
439 if (!gspi_clk_mhz)
440 gspi_clk_mhz = 1;
442 assert(ref_clk_mhz != 0);
443 return (DIV_ROUND_UP(ref_clk_mhz, gspi_clk_mhz) - 1) & SSCR0_SCR_MASK;
446 static int gspi_ctrlr_setup(const struct spi_slave *dev)
448 struct spi_cfg cfg;
449 int devfn;
450 uint32_t cs_ctrl, sscr0, sscr1, clocks, sitf, sirf, pol;
451 struct gspi_ctrlr_params params, *p = &params;
452 const struct device *device;
454 /* Only chip select 0 is supported. */
455 if (dev->cs != 0) {
456 printk(BIOS_ERR, "%s: Invalid CS value: cs=%u.\n", __func__,
457 dev->cs);
458 return -1;
461 if (gspi_ctrlr_params_init(p, dev->bus))
462 return -1;
464 /* Obtain SPI bus configuration for the device. */
465 if (gspi_get_soc_spi_cfg(p->gspi_bus, &cfg)) {
466 printk(BIOS_ERR, "%s: Failed to get config for bus=%u.\n",
467 __func__, p->gspi_bus);
468 return -1;
471 devfn = gspi_soc_bus_to_devfn(p->gspi_bus);
473 * devfn is already validated as part of gspi_ctrlr_params_init.
474 * No need to revalidate it again.
476 device = pcidev_path_on_root(devfn);
478 /* Ensure controller is in D0 state */
479 lpss_set_power_state(device, STATE_D0);
481 /* Take controller out of reset, keeping DMA in reset. */
482 gspi_write_mmio_reg(p, RESETS, CTRLR_ACTIVE | DMA_RESET);
485 * CS control:
486 * - Set SW mode.
487 * - Set chip select to 0.
488 * - Set polarity based on device configuration.
489 * - Do not assert CS.
491 cs_ctrl = CS_MODE_SW | CS_0;
492 pol = gspi_csctrl_polarity(cfg.cs_polarity);
493 cs_ctrl |= pol << CS_0_POL_SHIFT;
494 cs_ctrl |= gspi_csctrl_state(pol, CS_DEASSERT) << CS_STATE_SHIFT;
495 gspi_write_mmio_reg(p, SPI_CS_CONTROL, cs_ctrl);
497 /* Disable SPI controller. */
498 gspi_write_mmio_reg(p, SSCR0, SSCR0_SSE_DISABLE);
501 * SSCR0 configuration:
502 * clk_div - Based on reference clock and expected clock frequency.
503 * data bit length - assumed to be 8, hence EDSS = 0.
504 * ECS - Use on-chip clock
505 * FRF - Frame format set to Motorola SPI
507 sscr0 = gspi_get_clk_div(p->gspi_bus) << SSCR0_SCR_SHIFT;
508 assert(GSPI_DATA_BIT_LENGTH == 8);
509 sscr0 |= ((GSPI_DATA_BIT_LENGTH - 1) << SSCR0_DSS_SHIFT) | SSCR0_EDSS_0;
510 sscr0 |= SSCR0_ECS_ON_CHIP | SSCR0_FRF_MOTOROLA;
511 gspi_write_mmio_reg(p, SSCR0, sscr0);
514 * SSCR1 configuration:
515 * - Chip select polarity
516 * - Clock phase setting
517 * - Clock polarity
519 sscr1 = (cfg.cs_polarity == SPI_POLARITY_LOW) ? SSCR1_IFS_LOW :
520 SSCR1_IFS_HIGH;
521 sscr1 |= (cfg.clk_phase == SPI_CLOCK_PHASE_FIRST) ? SSCR1_SPH_FIRST :
522 SSCR1_SPH_SECOND;
523 sscr1 |= (cfg.clk_polarity == SPI_POLARITY_LOW) ? SSCR1_SPO_LOW :
524 SSCR1_SPO_HIGH;
525 gspi_write_mmio_reg(p, SSCR1, sscr1);
528 * Program m/n divider.
529 * Set m and n to 1, so that this divider acts as a pass-through.
531 clocks = (1 << CLOCKS_N_SHIFT) | (1 << CLOCKS_M_SHIFT) | CLOCKS_ENABLE |
532 CLOCKS_UPDATE;
533 gspi_write_mmio_reg(p, CLOCKS, clocks);
534 udelay(10);
537 * Tx FIFO Threshold.
538 * Low watermark threshold = 1
539 * High watermark threshold = 1
541 sitf = SITF_LWM(1) | SITF_HWM(1);
542 gspi_write_mmio_reg(p, SITF, sitf);
544 /* Rx FIFO Threshold (set to 1). */
545 sirf = SIRF_WM(1);
546 gspi_write_mmio_reg(p, SIRF, sirf);
548 /* Enable GSPI controller. */
549 sscr0 |= SSCR0_SSE_ENABLE;
550 gspi_write_mmio_reg(p, SSCR0, sscr0);
552 return 0;
555 static uint32_t gspi_read_status(const struct gspi_ctrlr_params *p)
557 return gspi_read_mmio_reg(p, SSSR);
560 static void gspi_clear_status(const struct gspi_ctrlr_params *p)
562 const uint32_t sssr = SSSR_TUR | SSSR_TINT | SSSR_PINT | SSSR_ROR;
563 gspi_write_mmio_reg(p, SSSR, sssr);
566 static bool gspi_rx_fifo_empty(const struct gspi_ctrlr_params *p)
568 return !(gspi_read_status(p) & SSSR_RNE);
571 static bool gspi_tx_fifo_full(const struct gspi_ctrlr_params *p)
573 return !(gspi_read_status(p) & SSSR_TNF);
576 static bool gspi_rx_fifo_overrun(const struct gspi_ctrlr_params *p)
578 if (gspi_read_status(p) & SSSR_ROR) {
579 printk(BIOS_ERR, "%s:GSPI receive FIFO overrun!"
580 " (bus=%u).\n", __func__, p->gspi_bus);
581 return true;
584 return false;
587 /* Read SSDR and return lowest byte. */
588 static uint8_t gspi_read_byte(const struct gspi_ctrlr_params *p)
590 return gspi_read_mmio_reg(p, SSDR) & 0xFF;
593 /* Write 32-bit word with "data" in lowest byte to SSDR. */
594 static void gspi_write_byte(const struct gspi_ctrlr_params *p, uint8_t data)
596 return gspi_write_mmio_reg(p, SSDR, data);
599 static void gspi_read_data(struct gspi_ctrlr_params *p)
601 *(p->in) = gspi_read_byte(p);
602 p->in++;
603 p->bytesin--;
606 static void gspi_write_data(struct gspi_ctrlr_params *p)
608 gspi_write_byte(p, *(p->out));
609 p->out++;
610 p->bytesout--;
613 static void gspi_read_dummy(struct gspi_ctrlr_params *p)
615 gspi_read_byte(p);
616 p->bytesin--;
619 static void gspi_write_dummy(struct gspi_ctrlr_params *p)
621 gspi_write_byte(p, 0);
622 p->bytesout--;
625 static int gspi_ctrlr_flush(const struct gspi_ctrlr_params *p)
627 const uint32_t timeout_ms = 500;
628 struct stopwatch sw;
630 /* Wait 500ms to allow Rx FIFO to be empty. */
631 stopwatch_init_msecs_expire(&sw, timeout_ms);
633 while (!gspi_rx_fifo_empty(p)) {
634 if (stopwatch_expired(&sw)) {
635 printk(BIOS_ERR, "%s: Rx FIFO not empty after 500ms! "
636 "(bus=%u)\n", __func__, p->gspi_bus);
637 return -1;
640 gspi_read_byte(p);
643 return 0;
646 static int __gspi_xfer(struct gspi_ctrlr_params *p)
649 * If bytesin is non-zero, then use gspi_read_data to perform
650 * byte-by-byte read of data from SSDR and save it to "in" buffer. Else
651 * discard the read data using gspi_read_dummy.
653 void (*fn_read)(struct gspi_ctrlr_params *p) = gspi_read_data;
656 * If bytesout is non-zero, then use gspi_write_data to perform
657 * byte-by-byte write of data from "out" buffer to SSDR. Else, use
658 * gspi_write_dummy to write dummy "0" data to SSDR in order to trigger
659 * read from slave.
661 void (*fn_write)(struct gspi_ctrlr_params *p) = gspi_write_data;
663 if (!p->bytesin) {
664 p->bytesin = p->bytesout;
665 fn_read = gspi_read_dummy;
668 if (!p->bytesout) {
669 p->bytesout = p->bytesin;
670 fn_write = gspi_write_dummy;
673 while (p->bytesout || p->bytesin) {
674 if (p->bytesout && !gspi_tx_fifo_full(p))
675 fn_write(p);
676 if (p->bytesin && !gspi_rx_fifo_empty(p)) {
677 if (gspi_rx_fifo_overrun(p))
678 return -1;
679 fn_read(p);
683 return 0;
686 static int gspi_ctrlr_xfer(const struct spi_slave *dev,
687 const void *dout, size_t bytesout,
688 void *din, size_t bytesin)
690 struct gspi_ctrlr_params params;
691 struct gspi_ctrlr_params *p = &params;
694 * Assumptions about in and out transfers:
695 * 1. Both bytesin and bytesout cannot be 0.
696 * 2. If both bytesin and bytesout are non-zero, then they should be
697 * equal i.e. if both in and out transfers are to be done in same
698 * transaction, then they should be equal in length.
699 * 3. Buffer corresponding to non-zero bytes (bytesin/bytesout) cannot
700 * be NULL.
702 if (!bytesin && !bytesout) {
703 printk(BIOS_ERR, "%s: Both in and out bytes cannot be zero!\n",
704 __func__);
705 return -1;
706 } else if (bytesin && bytesout && (bytesin != bytesout)) {
707 printk(BIOS_ERR, "%s: bytesin(%zd) != bytesout(%zd)\n",
708 __func__, bytesin, bytesout);
709 return -1;
711 if ((bytesin && !din) || (bytesout && !dout)) {
712 printk(BIOS_ERR, "%s: in/out buffer is NULL!\n", __func__);
713 return -1;
716 if (gspi_ctrlr_params_init(p, dev->bus))
717 return -1;
719 /* Flush out any stale data in Rx FIFO. */
720 if (gspi_ctrlr_flush(p))
721 return -1;
723 /* Clear status bits. */
724 gspi_clear_status(p);
726 p->in = din;
727 p->bytesin = bytesin;
728 p->out = dout;
729 p->bytesout = bytesout;
731 return __gspi_xfer(p);
734 const struct spi_ctrlr gspi_ctrlr = {
735 .claim_bus = gspi_cs_assert,
736 .release_bus = gspi_cs_deassert,
737 .setup = gspi_ctrlr_setup,
738 .xfer = gspi_ctrlr_xfer,
739 .max_xfer_size = SPI_CTRLR_DEFAULT_MAX_XFER_SIZE,