coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
gspi.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
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>
19 
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)
65 
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)
98 
99 #define GSPI_DATA_BIT_LENGTH (8)
100 #define GSPI_BUS_BASE(bar, bus) ((bar) + (bus) * 4 * KiB)
101 
102 /* Get base address for early init of GSPI controllers. */
103 static uintptr_t gspi_get_early_base(void)
104 {
106 }
107 
108 /* Get gspi_config array from devicetree. Returns NULL in case of error. */
109 static const struct gspi_cfg *gspi_get_cfg(void)
110 {
111  const struct soc_intel_common_config *common_config;
112  common_config = chip_get_common_soc_structure();
113 
114  return &common_config->gspi[0];
115 }
116 
117 #if defined(__SIMPLE_DEVICE__)
118 
119 static uintptr_t gspi_get_base_addr(int devfn,
120  DEVTREE_CONST struct device *dev)
121 {
122  pci_devfn_t pci_dev = PCI_DEV(0, PCI_SLOT(devfn), PCI_FUNC(devfn));
124 }
125 
126 static void gspi_set_base_addr(int devfn, DEVTREE_CONST struct device *dev,
127  uintptr_t base)
128 {
129  pci_devfn_t pci_dev = PCI_DEV(0, PCI_SLOT(devfn), PCI_FUNC(devfn));
133 }
134 
135 void gspi_early_bar_init(void)
136 {
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;
142 
143  assert(gspi_max != 0);
144  if (!cfg) {
145  printk(BIOS_ERR, "%s: No GSPI config provided by SoC!\n",
146  __func__);
147  return;
148  }
149 
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;
155  }
156 
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));
163  }
164 }
165 
166 #else
167 
168 static uintptr_t gspi_get_base_addr(int devfn, struct device *dev)
169 {
171 }
172 
173 static void gspi_set_base_addr(int devfn, struct device *dev, uintptr_t base)
174 {
178 }
179 
180 #endif
181 
182 static int gspi_read_bus_range(unsigned int *start, unsigned int *end)
183 {
184  size_t i;
185  const struct spi_ctrlr_buses *desc;
186 
187  for (i = 0; i < spi_ctrlr_bus_map_count; i++) {
188  desc = &spi_ctrlr_bus_map[i];
189 
190  if (desc->ctrlr != &gspi_ctrlr)
191  continue;
192 
193  *start = desc->bus_start;
194  *end = desc->bus_end;
195 
196  return 0;
197  }
198  return -1;
199 }
200 
201 static int gspi_spi_to_gspi_bus(unsigned int spi_bus, unsigned int *gspi_bus)
202 {
203  unsigned int start;
204  unsigned int end;
205  int ret;
206 
207  ret = gspi_read_bus_range(&start, &end);
208 
209  if (ret != 0 || (spi_bus < start) || (spi_bus > end))
210  return -1;
211 
212  *gspi_bus = spi_bus - start;
213 
214  return 0;
215 }
216 
217 static uintptr_t gspi_calc_base_addr(unsigned int gspi_bus)
218 {
219  uintptr_t bus_base, gspi_base_addr;
220  DEVTREE_CONST struct device *dev;
221  int devfn = gspi_soc_bus_to_devfn(gspi_bus);
222 
223  if (devfn < 0)
224  return 0;
225 
226  dev = pcidev_path_on_root(devfn);
227  if (!dev || !dev->enabled)
228  return 0;
229 
230  bus_base = gspi_get_base_addr(devfn, dev);
231  if (bus_base)
232  return bus_base;
233 
234  gspi_base_addr = gspi_get_early_base();
235  if (!gspi_base_addr)
236  return 0;
237 
238  bus_base = GSPI_BUS_BASE(gspi_base_addr, gspi_bus);
239 
240  gspi_set_base_addr(devfn, dev, bus_base);
241  return bus_base;
242 }
243 
244 static uint32_t gspi_get_bus_clk_mhz(unsigned int gspi_bus)
245 {
246  const struct gspi_cfg *cfg = gspi_get_cfg();
247  if (!cfg)
248  return 0;
249  return cfg[gspi_bus].speed_mhz;
250 }
251 
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)
254 {
255  if (!gspi_base[gspi_bus])
256  gspi_base[gspi_bus] = gspi_calc_base_addr(gspi_bus);
257 
258  return gspi_base[gspi_bus];
259 }
260 
261 /*
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.
265  */
266 static void gspi_clear_cached_base(void *unused)
267 {
268  memset(gspi_base, 0, sizeof(gspi_base));
269 }
271 
272 /* Parameters for GSPI controller operation. */
275  unsigned int gspi_bus;
277  size_t bytesin;
278  const uint8_t *out;
279  size_t bytesout;
280 };
281 
282 static uint32_t gspi_read_mmio_reg(const struct gspi_ctrlr_params *p,
284 {
285  assert(p->mmio_base != 0);
286  return read32((void *)(p->mmio_base + offset));
287 }
288 
289 static void gspi_write_mmio_reg(const struct gspi_ctrlr_params *p,
291 {
292  assert(p->mmio_base != 0);
293  write32((void *)(p->mmio_base + offset), value);
294 }
295 
296 static int gspi_ctrlr_params_init(struct gspi_ctrlr_params *p,
297  unsigned int spi_bus)
298 {
299  memset(p, 0, sizeof(*p));
300 
302  printk(BIOS_ERR, "%s: No GSPI bus available for SPI bus %u.\n",
303  __func__, spi_bus);
304  return -1;
305  }
306 
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;
312  }
313 
314  return 0;
315 }
316 
317 enum cs_assert {
319  CS_DEASSERT,
320 };
321 
322 /*
323  * SPI_CS_CONTROL bit definitions based on GSPI_VERSION_x:
324  *
325  * VERSION_2 (CNL GSPI controller):
326  * Polarity: Indicates inactive polarity of chip-select
327  * State : Indicates assert/de-assert of chip-select
328  *
329  * Default (SKL/KBL GSPI controller):
330  * Polarity: Indicates active polarity of chip-select
331  * State : Indicates low/high output state of chip-select
332  */
334 {
335  return cs_assert;
336 }
337 
339 {
340  uint32_t state;
341 
342  if (pol == CS_POL_HIGH)
345  else
348 
349  return state;
350 }
351 
353 {
354  if (CONFIG(SOC_INTEL_COMMON_BLOCK_GSPI_VERSION_2))
355  return gspi_csctrl_state_v2(pol, cs_assert);
356 
357  return gspi_csctrl_state_v1(pol, cs_assert);
358 }
359 
360 static uint32_t gspi_csctrl_polarity_v2(enum spi_polarity active_pol)
361 {
362  /* Polarity field indicates cs inactive polarity */
363  if (active_pol == SPI_POLARITY_LOW)
364  return CS_POL_HIGH;
365  return CS_POL_LOW;
366 }
367 
368 static uint32_t gspi_csctrl_polarity_v1(enum spi_polarity active_pol)
369 {
370  /* Polarity field indicates cs active polarity */
371  if (active_pol == SPI_POLARITY_LOW)
372  return CS_POL_LOW;
373  return CS_POL_HIGH;
374 }
375 
376 static uint32_t gspi_csctrl_polarity(enum spi_polarity active_pol)
377 {
378  if (CONFIG(SOC_INTEL_COMMON_BLOCK_GSPI_VERSION_2))
379  return gspi_csctrl_polarity_v2(active_pol);
380 
381  return gspi_csctrl_polarity_v1(active_pol);
382 }
383 
384 static void __gspi_cs_change(const struct gspi_ctrlr_params *p,
385  enum cs_assert cs_assert)
386 {
387  uint32_t cs_ctrl, pol;
388  cs_ctrl = gspi_read_mmio_reg(p, SPI_CS_CONTROL);
389 
390  cs_ctrl &= ~CS_STATE_MASK;
391 
392  pol = !!(cs_ctrl & CS_0_POL_MASK);
393  cs_ctrl |= gspi_csctrl_state(pol, cs_assert) << CS_STATE_SHIFT;
394 
395  gspi_write_mmio_reg(p, SPI_CS_CONTROL, cs_ctrl);
396 }
397 
398 static int gspi_cs_change(const struct spi_slave *dev, enum cs_assert cs_assert)
399 {
400  struct gspi_ctrlr_params params, *p = &params;
401 
402  if (gspi_ctrlr_params_init(p, dev->bus))
403  return -1;
404 
406 
407  return 0;
408 }
409 
410 int __weak gspi_get_soc_spi_cfg(unsigned int gspi_bus,
411  struct spi_cfg *cfg)
412 {
416  cfg->wire_mode = SPI_4_WIRE_MODE;
418 
419  return 0;
420 }
421 
422 static int gspi_cs_assert(const struct spi_slave *dev)
423 {
424  return gspi_cs_change(dev, CS_ASSERT);
425 }
426 
427 static void gspi_cs_deassert(const struct spi_slave *dev)
428 {
430 }
431 
432 static uint32_t gspi_get_clk_div(unsigned int gspi_bus)
433 {
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);
437 
438  if (!gspi_clk_mhz)
439  gspi_clk_mhz = 1;
440 
441  assert(ref_clk_mhz != 0);
442  return (DIV_ROUND_UP(ref_clk_mhz, gspi_clk_mhz) - 1) & SSCR0_SCR_MASK;
443 }
444 
445 static int gspi_ctrlr_setup(const struct spi_slave *dev)
446 {
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;
451 
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;
457  }
458 
459  if (gspi_ctrlr_params_init(p, dev->bus))
460  return -1;
461 
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;
467  }
468 
469  devfn = gspi_soc_bus_to_devfn(p->gspi_bus);
470 
471  /* Ensure controller is in D0 state */
473 
474  /* Take controller out of reset, keeping DMA in reset. */
476 
477  /*
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.
483  */
484  cs_ctrl = CS_MODE_SW | CS_0;
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);
489 
490  /* Disable SPI controller. */
492 
493  /*
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
499  */
502  sscr0 |= ((GSPI_DATA_BIT_LENGTH - 1) << SSCR0_DSS_SHIFT) | SSCR0_EDSS_0;
504  gspi_write_mmio_reg(p, SSCR0, sscr0);
505 
506  /*
507  * SSCR1 configuration:
508  * - Chip select polarity
509  * - Clock phase setting
510  * - Clock polarity
511  */
512  sscr1 = (cfg.cs_polarity == SPI_POLARITY_LOW) ? SSCR1_IFS_LOW :
514  sscr1 |= (cfg.clk_phase == SPI_CLOCK_PHASE_FIRST) ? SSCR1_SPH_FIRST :
516  sscr1 |= (cfg.clk_polarity == SPI_POLARITY_LOW) ? SSCR1_SPO_LOW :
518  gspi_write_mmio_reg(p, SSCR1, sscr1);
519 
520  /*
521  * Program m/n divider.
522  * Set m and n to 1, so that this divider acts as a pass-through.
523  */
524  clocks = (1 << CLOCKS_N_SHIFT) | (1 << CLOCKS_M_SHIFT) | CLOCKS_ENABLE |
526  gspi_write_mmio_reg(p, CLOCKS, clocks);
527  udelay(10);
528 
529  /*
530  * Tx FIFO Threshold.
531  * Low watermark threshold = 1
532  * High watermark threshold = 1
533  */
534  sitf = SITF_LWM(1) | SITF_HWM(1);
535  gspi_write_mmio_reg(p, SITF, sitf);
536 
537  /* Rx FIFO Threshold (set to 1). */
538  sirf = SIRF_WM(1);
539  gspi_write_mmio_reg(p, SIRF, sirf);
540 
541  /* Enable GSPI controller. */
542  sscr0 |= SSCR0_SSE_ENABLE;
543  gspi_write_mmio_reg(p, SSCR0, sscr0);
544 
545  return 0;
546 }
547 
548 static uint32_t gspi_read_status(const struct gspi_ctrlr_params *p)
549 {
550  return gspi_read_mmio_reg(p, SSSR);
551 }
552 
553 static void gspi_clear_status(const struct gspi_ctrlr_params *p)
554 {
555  const uint32_t sssr = SSSR_TUR | SSSR_TINT | SSSR_PINT | SSSR_ROR;
556  gspi_write_mmio_reg(p, SSSR, sssr);
557 }
558 
559 static bool gspi_rx_fifo_empty(const struct gspi_ctrlr_params *p)
560 {
561  return !(gspi_read_status(p) & SSSR_RNE);
562 }
563 
564 static bool gspi_tx_fifo_full(const struct gspi_ctrlr_params *p)
565 {
566  return !(gspi_read_status(p) & SSSR_TNF);
567 }
568 
569 static bool gspi_rx_fifo_overrun(const struct gspi_ctrlr_params *p)
570 {
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;
575  }
576 
577  return false;
578 }
579 
580 /* Read SSDR and return lowest byte. */
581 static uint8_t gspi_read_byte(const struct gspi_ctrlr_params *p)
582 {
583  return gspi_read_mmio_reg(p, SSDR) & 0xFF;
584 }
585 
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)
588 {
589  return gspi_write_mmio_reg(p, SSDR, data);
590 }
591 
592 static void gspi_read_data(struct gspi_ctrlr_params *p)
593 {
594  *(p->in) = gspi_read_byte(p);
595  p->in++;
596  p->bytesin--;
597 }
598 
599 static void gspi_write_data(struct gspi_ctrlr_params *p)
600 {
601  gspi_write_byte(p, *(p->out));
602  p->out++;
603  p->bytesout--;
604 }
605 
606 static void gspi_read_dummy(struct gspi_ctrlr_params *p)
607 {
608  gspi_read_byte(p);
609  p->bytesin--;
610 }
611 
612 static void gspi_write_dummy(struct gspi_ctrlr_params *p)
613 {
614  gspi_write_byte(p, 0);
615  p->bytesout--;
616 }
617 
618 static int gspi_ctrlr_flush(const struct gspi_ctrlr_params *p)
619 {
620  const uint32_t timeout_ms = 500;
621  struct stopwatch sw;
622 
623  /* Wait 500ms to allow Rx FIFO to be empty. */
624  stopwatch_init_msecs_expire(&sw, timeout_ms);
625 
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;
631  }
632 
633  gspi_read_byte(p);
634  }
635 
636  return 0;
637 }
638 
639 static int __gspi_xfer(struct gspi_ctrlr_params *p)
640 {
641  /*
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.
645  */
646  void (*fn_read)(struct gspi_ctrlr_params *p) = gspi_read_data;
647 
648  /*
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.
653  */
654  void (*fn_write)(struct gspi_ctrlr_params *p) = gspi_write_data;
655 
656  if (!p->bytesin) {
657  p->bytesin = p->bytesout;
658  fn_read = gspi_read_dummy;
659  }
660 
661  if (!p->bytesout) {
662  p->bytesout = p->bytesin;
663  fn_write = gspi_write_dummy;
664  }
665 
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);
673  }
674  }
675 
676  return 0;
677 }
678 
679 static int gspi_ctrlr_xfer(const struct spi_slave *dev,
680  const void *dout, size_t bytesout,
681  void *din, size_t bytesin)
682 {
683  struct gspi_ctrlr_params params;
684  struct gspi_ctrlr_params *p = &params;
685 
686  /*
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.
694  */
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;
703  }
704  if ((bytesin && !din) || (bytesout && !dout)) {
705  printk(BIOS_ERR, "%s: in/out buffer is NULL!\n", __func__);
706  return -1;
707  }
708 
709  if (gspi_ctrlr_params_init(p, dev->bus))
710  return -1;
711 
712  /* Flush out any stale data in Rx FIFO. */
713  if (gspi_ctrlr_flush(p))
714  return -1;
715 
716  /* Clear status bits. */
718 
719  p->in = din;
720  p->bytesin = bytesin;
721  p->out = dout;
722  p->bytesout = bytesout;
723 
724  return __gspi_xfer(p);
725 }
726 
727 const struct spi_ctrlr gspi_ctrlr = {
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,
733 };
int gspi_soc_bus_to_devfn(unsigned int gspi_bus)
Definition: gspi.c:12
pte_t value
Definition: mmu.c:91
static void write32(void *addr, uint32_t val)
Definition: mmio.h:40
static uint32_t read32(const void *addr)
Definition: mmio.h:22
void * memset(void *dstpp, int c, size_t len)
Definition: memset.c:12
#define pci_dev
#define assert(statement)
Definition: assert.h:74
static struct sdram_info params
Definition: sdram_configs.c:83
@ BS_DEV_RESOURCES
Definition: bootstate.h:81
@ BS_ON_EXIT
Definition: bootstate.h:96
#define ALIGN_DOWN(x, a)
Definition: helpers.h:18
#define DIV_ROUND_UP(x, y)
Definition: helpers.h:60
static uintptr_t gspi_get_early_base(void)
Definition: gspi.c:102
#define SIRF_WM(x)
Definition: gspi.c:63
#define SSCR0_DSS_SHIFT
Definition: gspi.c:30
#define SSCR0_SCR_MASK
Definition: gspi.c:25
static int gspi_cs_assert(const struct spi_slave *dev)
Definition: gspi.c:421
#define SSCR1_SPH_SECOND
Definition: gspi.c:36
static uintptr_t gspi_base[CONFIG_SOC_INTEL_COMMON_BLOCK_GSPI_MAX]
Definition: gspi.c:251
static uint32_t gspi_read_mmio_reg(const struct gspi_ctrlr_params *p, uint32_t offset)
Definition: gspi.c:281
#define SSDR
Definition: gspi.c:47
#define SIRF
Definition: gspi.c:58
#define SSCR1
Definition: gspi.c:32
static void gspi_write_mmio_reg(const struct gspi_ctrlr_params *p, uint32_t offset, uint32_t value)
Definition: gspi.c:288
static uint32_t gspi_csctrl_state(uint32_t pol, enum cs_assert cs_assert)
Definition: gspi.c:351
static uint32_t gspi_get_bus_clk_mhz(unsigned int gspi_bus)
Definition: gspi.c:243
#define SSCR0_SSE_ENABLE
Definition: gspi.c:27
#define CS_POL_LOW
Definition: gspi.c:88
#define SSSR_RNE
Definition: gspi.c:45
static const struct gspi_cfg * gspi_get_cfg(void)
Definition: gspi.c:108
#define CLOCKS_M_SHIFT
Definition: gspi.c:70
static uint32_t gspi_get_clk_div(unsigned int gspi_bus)
Definition: gspi.c:431
#define CTRLR_ACTIVE
Definition: gspi.c:78
static uintptr_t gspi_calc_base_addr(unsigned int gspi_bus)
Definition: gspi.c:216
static int gspi_read_bus_range(unsigned int *start, unsigned int *end)
Definition: gspi.c:181
static uint32_t gspi_csctrl_polarity_v1(enum spi_polarity active_pol)
Definition: gspi.c:367
#define CS_V1_STATE_HIGH
Definition: gspi.c:94
#define CS_0
Definition: gspi.c:90
static void gspi_read_data(struct gspi_ctrlr_params *p)
Definition: gspi.c:591
#define SITF_HWM(x)
Definition: gspi.c:57
#define SSCR1_SPO_LOW
Definition: gspi.c:37
int __weak gspi_get_soc_spi_cfg(unsigned int gspi_bus, struct spi_cfg *cfg)
Definition: gspi.c:409
#define SSSR_PINT
Definition: gspi.c:42
#define GSPI_BUS_BASE(bar, bus)
Definition: gspi.c:99
#define CS_STATE_MASK
Definition: gspi.c:92
#define GSPI_DATA_BIT_LENGTH
Definition: gspi.c:98
#define CS_0_POL_SHIFT
Definition: gspi.c:86
static void gspi_clear_status(const struct gspi_ctrlr_params *p)
Definition: gspi.c:552
#define SSCR1_IFS_LOW
Definition: gspi.c:33
#define SSSR_TINT
Definition: gspi.c:41
#define CLOCKS_N_SHIFT
Definition: gspi.c:68
#define SSCR0
Definition: gspi.c:21
static void gspi_clear_cached_base(void *unused)
Definition: gspi.c:265
#define DMA_RESET
Definition: gspi.c:75
static void gspi_cs_deassert(const struct spi_slave *dev)
Definition: gspi.c:426
#define SITF_LWM(x)
Definition: gspi.c:54
BOOT_STATE_INIT_ENTRY(BS_DEV_RESOURCES, BS_ON_EXIT, gspi_clear_cached_base, NULL)
static bool gspi_tx_fifo_full(const struct gspi_ctrlr_params *p)
Definition: gspi.c:563
#define SSSR
Definition: gspi.c:39
#define CS_STATE_SHIFT
Definition: gspi.c:91
#define CS_V1_STATE_LOW
Definition: gspi.c:93
static bool gspi_rx_fifo_empty(const struct gspi_ctrlr_params *p)
Definition: gspi.c:558
static int gspi_ctrlr_flush(const struct gspi_ctrlr_params *p)
Definition: gspi.c:617
static uint32_t gspi_csctrl_polarity(enum spi_polarity active_pol)
Definition: gspi.c:375
#define SSCR1_SPH_FIRST
Definition: gspi.c:35
static uint32_t gspi_csctrl_state_v1(uint32_t pol, enum cs_assert cs_assert)
Definition: gspi.c:337
static uintptr_t gspi_get_bus_base_addr(unsigned int gspi_bus)
Definition: gspi.c:252
static int gspi_ctrlr_params_init(struct gspi_ctrlr_params *p, unsigned int spi_bus)
Definition: gspi.c:295
#define SSCR1_SPO_HIGH
Definition: gspi.c:38
static void gspi_set_base_addr(int devfn, struct device *dev, uintptr_t base)
Definition: gspi.c:172
#define SSCR1_IFS_HIGH
Definition: gspi.c:34
#define SSCR0_ECS_ON_CHIP
Definition: gspi.c:28
static uint8_t gspi_read_byte(const struct gspi_ctrlr_params *p)
Definition: gspi.c:580
static int gspi_ctrlr_xfer(const struct spi_slave *dev, const void *dout, size_t bytesout, void *din, size_t bytesin)
Definition: gspi.c:678
#define SITF
Definition: gspi.c:49
static int gspi_spi_to_gspi_bus(unsigned int spi_bus, unsigned int *gspi_bus)
Definition: gspi.c:200
#define CS_POL_HIGH
Definition: gspi.c:89
static uintptr_t gspi_get_base_addr(int devfn, struct device *dev)
Definition: gspi.c:167
static int __gspi_xfer(struct gspi_ctrlr_params *p)
Definition: gspi.c:638
static void __gspi_cs_change(const struct gspi_ctrlr_params *p, enum cs_assert cs_assert)
Definition: gspi.c:383
static uint32_t gspi_csctrl_state_v2(uint32_t pol, enum cs_assert cs_assert)
Definition: gspi.c:332
cs_assert
Definition: gspi.c:316
@ CS_DEASSERT
Definition: gspi.c:318
@ CS_ASSERT
Definition: gspi.c:317
static uint32_t gspi_read_status(const struct gspi_ctrlr_params *p)
Definition: gspi.c:547
#define SSSR_TNF
Definition: gspi.c:46
#define CLOCKS_ENABLE
Definition: gspi.c:73
static int gspi_cs_change(const struct spi_slave *dev, enum cs_assert cs_assert)
Definition: gspi.c:397
#define SPI_CS_CONTROL
Definition: gspi.c:85
#define CLOCKS
Definition: gspi.c:66
static void gspi_write_dummy(struct gspi_ctrlr_params *p)
Definition: gspi.c:611
#define SSCR0_SSE_DISABLE
Definition: gspi.c:26
#define CS_0_POL_MASK
Definition: gspi.c:87
#define SSSR_ROR
Definition: gspi.c:43
static void gspi_write_data(struct gspi_ctrlr_params *p)
Definition: gspi.c:598
static bool gspi_rx_fifo_overrun(const struct gspi_ctrlr_params *p)
Definition: gspi.c:568
#define CLOCKS_UPDATE
Definition: gspi.c:67
static void gspi_read_dummy(struct gspi_ctrlr_params *p)
Definition: gspi.c:605
static uint32_t gspi_csctrl_polarity_v2(enum spi_polarity active_pol)
Definition: gspi.c:359
#define RESETS
Definition: gspi.c:74
#define SSCR0_EDSS_0
Definition: gspi.c:22
#define CS_MODE_SW
Definition: gspi.c:96
static void gspi_write_byte(const struct gspi_ctrlr_params *p, uint8_t data)
Definition: gspi.c:586
const struct spi_ctrlr gspi_ctrlr
Definition: gspi.c:726
#define SSCR0_SCR_SHIFT
Definition: gspi.c:24
static int gspi_ctrlr_setup(const struct spi_slave *dev)
Definition: gspi.c:444
#define SSCR0_FRF_MOTOROLA
Definition: gspi.c:29
#define SSSR_TUR
Definition: gspi.c:40
#define printk(level,...)
Definition: stdlib.h:16
DEVTREE_CONST struct device * pcidev_path_on_root(pci_devfn_t devfn)
Definition: device_const.c:255
@ CONFIG
Definition: dsi_common.h:201
static size_t offset
Definition: flashconsole.c:16
void gspi_early_bar_init(void)
static __always_inline void pci_write_config32(const struct device *dev, u16 reg, u32 val)
Definition: pci_ops.h:76
static __always_inline u32 pci_read_config32(const struct device *dev, u16 reg)
Definition: pci_ops.h:58
static int stopwatch_expired(struct stopwatch *sw)
Definition: timer.h:152
static void stopwatch_init_msecs_expire(struct stopwatch *sw, long ms)
Definition: timer.h:133
#define EARLY_GSPI_BASE_ADDRESS
Definition: iomap.h:85
#define BIOS_ERR
BIOS_ERR - System in incomplete state.
Definition: loglevel.h:72
void lpss_set_power_state(pci_devfn_t devfn, enum lpss_pwr_state state)
Definition: lpss.c:68
@ STATE_D0
Definition: lpss.h:11
state
Definition: raminit.c:1787
#define PCI_COMMAND_MASTER
Definition: pci_def.h:13
#define PCI_COMMAND_MEMORY
Definition: pci_def.h:12
#define PCI_FUNC(devfn)
Definition: pci_def.h:550
#define PCI_BASE_ADDRESS_0
Definition: pci_def.h:63
#define PCI_COMMAND
Definition: pci_def.h:10
#define PCI_SLOT(devfn)
Definition: pci_def.h:549
#define PCI_DEV(SEGBUS, DEV, FN)
Definition: pci_type.h:14
u32 pci_devfn_t
Definition: pci_type.h:8
const struct smm_save_state_ops *legacy_ops __weak
Definition: save_state.c:8
uintptr_t base
Definition: uart.c:17
const struct soc_intel_common_config * chip_get_common_soc_structure(void)
Definition: chip.c:5
#define SPI_CTRLR_DEFAULT_MAX_XFER_SIZE
Definition: spi-generic.h:102
spi_polarity
Definition: spi-generic.h:79
@ SPI_POLARITY_LOW
Definition: spi-generic.h:80
const struct spi_ctrlr_buses spi_ctrlr_bus_map[]
Definition: fch_spi_ctrl.c:300
@ SPI_4_WIRE_MODE
Definition: spi-generic.h:75
@ SPI_CLOCK_PHASE_FIRST
Definition: spi-generic.h:70
const size_t spi_ctrlr_bus_map_count
Definition: fch_spi_ctrl.c:308
struct mtk_spi_bus spi_bus[]
Definition: spi.c:11
#define NULL
Definition: stddef.h:19
#define DEVTREE_CONST
Definition: stddef.h:30
unsigned int uint32_t
Definition: stdint.h:14
unsigned long uintptr_t
Definition: stdint.h:21
unsigned char uint8_t
Definition: stdint.h:8
Definition: device.h:107
unsigned int enabled
Definition: device.h:122
Definition: gspi.h:12
uint8_t early_init
Definition: gspi.h:16
uint32_t speed_mhz
Definition: gspi.h:14
const uint8_t * out
Definition: gspi.c:277
size_t bytesin
Definition: gspi.c:276
uint8_t * in
Definition: gspi.c:275
uintptr_t mmio_base
Definition: gspi.c:273
unsigned int gspi_bus
Definition: gspi.c:274
size_t bytesout
Definition: gspi.c:278
struct gspi_cfg gspi[CONFIG_SOC_INTEL_COMMON_BLOCK_GSPI_MAX]
Definition: cfg.h:23
enum spi_wire_mode wire_mode
Definition: spi-generic.h:92
unsigned int data_bit_length
Definition: spi-generic.h:94
enum spi_clock_phase clk_phase
Definition: spi-generic.h:86
enum spi_polarity cs_polarity
Definition: spi-generic.h:90
enum spi_polarity clk_polarity
Definition: spi-generic.h:88
unsigned int bus_end
Definition: spi-generic.h:177
const struct spi_ctrlr * ctrlr
Definition: spi-generic.h:175
unsigned int bus_start
Definition: spi-generic.h:176
int(* claim_bus)(const struct spi_slave *slave)
Definition: spi-generic.h:149
unsigned int bus
Definition: spi-generic.h:41
unsigned int cs
Definition: spi-generic.h:42
void udelay(uint32_t us)
Definition: udelay.c:15
typedef void(X86APIP X86EMU_intrFuncs)(int num)