coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
spi.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 #define __SIMPLE_DEVICE__
4 
5 #include <device/device.h>
6 #include <device/mmio.h>
7 #include <device/pci.h>
8 #include <device/pci_ids.h>
9 #include <device/pci_ops.h>
10 #include <device/spi.h>
11 #include <intelblocks/fast_spi.h>
12 #include <intelblocks/gspi.h>
13 #include <intelblocks/spi.h>
14 #include <soc/pci_devs.h>
15 #include <spi-generic.h>
16 
17 const struct spi_ctrlr_buses spi_ctrlr_bus_map[] = {
18  { .ctrlr = &fast_spi_flash_ctrlr, .bus_start = 0, .bus_end = 0 },
19 #if !ENV_SMM && CONFIG(SOC_INTEL_COMMON_BLOCK_GSPI)
20  { .ctrlr = &gspi_ctrlr, .bus_start = 1,
21  .bus_end = 1 + (CONFIG_SOC_INTEL_COMMON_BLOCK_GSPI_MAX - 1)},
22 #endif
23 };
24 
26 
27 #define MMIO_BIOS_GPR0 0x98
28 
30  struct {
31  /* Specified write protection is enabled */
32  /*
33  * This field corresponds to flash address bits 26:12
34  * and specifies the lower limit of protected range.
35  */
37 
38  /* Specifies read protection is enabled */
40 
41  /*
42  * This field corresponds to flash address bits 26:12
43  * and specifies the upper limit of the protected range
44  */
46 
49 
51 };
52 
53 /* Read SPI BAR 0 from PCI configuration space */
55 {
56  uintptr_t bar;
57 
59  assert(bar != 0);
60  /*
61  * Bits 31-12 are the base address as per EDS for SPI,
62  * Don't care about 0-11 bit
63  */
64  return bar & ~PCI_BASE_ADDRESS_MEM_ATTR_MASK;
65 }
66 
68 {
69  return read32p(get_spi_bar(dev) + offset);
70 }
71 
73 {
75 }
76 
78 {
79  return bios_gpr0.fields.protect_range_base << 12;
80 }
81 
83 {
84  return bios_gpr0.fields.protect_range_limit << 12 | 0xfff;
85 }
86 
88 {
89  union spi_bios_gpr0 bios_gpr0;
90 
91  bios_gpr0.data = spi_read_bios_gpr0();
92  return !!bios_gpr0.fields.write_protect_en;
93 }
94 
96 {
97  union spi_bios_gpr0 bios_gpr0;
98 
99  bios_gpr0.data = spi_read_bios_gpr0();
100  *base = spi_get_wp_cse_ro_start_offset(bios_gpr0);
101  *limit = spi_get_wp_cse_ro_limit(bios_gpr0);
102 }
103 
104 static int spi_dev_to_bus(struct device *dev)
105 {
106  return spi_soc_devfn_to_bus(dev->path.pci.devfn);
107 }
108 
109 static struct spi_bus_operations spi_bus_ops = {
111 };
112 
113 static struct device_operations spi_dev_ops = {
115  .set_resources = pci_dev_set_resources,
116  .enable_resources = pci_dev_enable_resources,
117  .scan_bus = scan_generic_bus,
118  .ops_spi_bus = &spi_bus_ops,
119  .ops_pci = &pci_dev_ops_pci,
120 };
121 
122 static const unsigned short pci_device_ids[] = {
201  0
202 };
203 
204 static const struct pci_driver pch_spi __pci_driver = {
205  .ops = &spi_dev_ops,
206  .vendor = PCI_VID_INTEL,
207  .devices = pci_device_ids,
208 };
#define assert(statement)
Definition: assert.h:74
#define ARRAY_SIZE(a)
Definition: helpers.h:12
const struct spi_ctrlr gspi_ctrlr
Definition: gspi.c:726
const struct spi_ctrlr fast_spi_flash_ctrlr
static size_t offset
Definition: flashconsole.c:16
static __always_inline uint32_t read32p(const uintptr_t addr)
Definition: mmio.h:220
static __always_inline u32 pci_read_config32(const struct device *dev, u16 reg)
Definition: pci_ops.h:58
#define PCI_BASE_ADDRESS_MEM_ATTR_MASK
Definition: pci_def.h:77
#define PCI_BASE_ADDRESS_0
Definition: pci_def.h:63
void pci_dev_enable_resources(struct device *dev)
Definition: pci_device.c:721
void pci_dev_read_resources(struct device *dev)
Definition: pci_device.c:534
struct pci_operations pci_dev_ops_pci
Default device operation for PCI devices.
Definition: pci_device.c:911
void pci_dev_set_resources(struct device *dev)
Definition: pci_device.c:691
#define PCI_DID_INTEL_ICP_SPI1
Definition: pci_ids.h:3748
#define PCI_DID_INTEL_GLK_SPI0
Definition: pci_ids.h:3734
#define PCI_DID_INTEL_ADP_P_HWSEQ_SPI
Definition: pci_ids.h:3781
#define PCI_DID_INTEL_ADP_P_SPI5
Definition: pci_ids.h:3787
#define PCI_DID_INTEL_SPR_HWSEQ_SPI
Definition: pci_ids.h:3804
#define PCI_DID_INTEL_CNP_H_SPI1
Definition: pci_ids.h:3744
#define PCI_DID_INTEL_TGP_H_GSPI3
Definition: pci_ids.h:3771
#define PCI_DID_INTEL_CNL_SPI0
Definition: pci_ids.h:3737
#define PCI_DID_INTEL_CMP_H_SPI2
Definition: pci_ids.h:3757
#define PCI_DID_INTEL_ADP_S_SPI5
Definition: pci_ids.h:3796
#define PCI_DID_INTEL_APL_HWSEQ_SPI
Definition: pci_ids.h:3733
#define PCI_DID_INTEL_ADP_P_SPI2
Definition: pci_ids.h:3784
#define PCI_DID_INTEL_CNL_HWSEQ_SPI
Definition: pci_ids.h:3740
#define PCI_DID_INTEL_ADP_P_SPI1
Definition: pci_ids.h:3783
#define PCI_DID_INTEL_ICP_SPI0
Definition: pci_ids.h:3747
#define PCI_DID_INTEL_ADP_P_SPI3
Definition: pci_ids.h:3785
#define PCI_DID_INTEL_LWB_SPI
Definition: pci_ids.h:3741
#define PCI_DID_INTEL_CNL_SPI1
Definition: pci_ids.h:3738
#define PCI_DID_INTEL_JSP_SPI2
Definition: pci_ids.h:3778
#define PCI_DID_INTEL_MTL_GSPI0
Definition: pci_ids.h:3807
#define PCI_DID_INTEL_MCC_GSPI1
Definition: pci_ids.h:3774
#define PCI_DID_INTEL_ADP_M_N_SPI0
Definition: pci_ids.h:3800
#define PCI_DID_INTEL_TGP_SPI0
Definition: pci_ids.h:3759
#define PCI_DID_INTEL_MTL_GSPI1
Definition: pci_ids.h:3808
#define PCI_DID_INTEL_JSP_HWSEQ_SPI
Definition: pci_ids.h:3779
#define PCI_DID_INTEL_ADP_M_N_SPI1
Definition: pci_ids.h:3801
#define PCI_DID_INTEL_CMP_HWSEQ_SPI
Definition: pci_ids.h:3754
#define PCI_DID_INTEL_ADP_S_SPI6
Definition: pci_ids.h:3797
#define PCI_DID_INTEL_CNP_H_SPI0
Definition: pci_ids.h:3743
#define PCI_DID_INTEL_ADP_S_SPI3
Definition: pci_ids.h:3794
#define PCI_DID_INTEL_ADP_P_SPI6
Definition: pci_ids.h:3788
#define PCI_DID_INTEL_APL_SPI1
Definition: pci_ids.h:3731
#define PCI_DID_INTEL_TGP_GSPI2
Definition: pci_ids.h:3762
#define PCI_DID_INTEL_TGP_GSPI5
Definition: pci_ids.h:3765
#define PCI_DID_INTEL_ADP_S_HWSEQ_SPI
Definition: pci_ids.h:3790
#define PCI_DID_INTEL_TGP_H_GSPI2
Definition: pci_ids.h:3770
#define PCI_DID_INTEL_APL_SPI2
Definition: pci_ids.h:3732
#define PCI_DID_INTEL_MCC_SPI0
Definition: pci_ids.h:3772
#define PCI_DID_INTEL_JSP_SPI0
Definition: pci_ids.h:3776
#define PCI_DID_INTEL_TGP_H_GSPI1
Definition: pci_ids.h:3769
#define PCI_DID_INTEL_CMP_SPI0
Definition: pci_ids.h:3751
#define PCI_DID_INTEL_SPT_SPI2
Definition: pci_ids.h:3728
#define PCI_DID_INTEL_TGP_GSPI6
Definition: pci_ids.h:3766
#define PCI_DID_INTEL_ADP_P_SPI0
Definition: pci_ids.h:3782
#define PCI_DID_INTEL_CNL_SPI2
Definition: pci_ids.h:3739
#define PCI_DID_INTEL_ADP_M_N_HWSEQ_SPI
Definition: pci_ids.h:3799
#define PCI_DID_INTEL_LWB_SPI_SUPER
Definition: pci_ids.h:3742
#define PCI_DID_INTEL_ADP_M_SPI2
Definition: pci_ids.h:3802
#define PCI_DID_INTEL_ADP_P_SPI4
Definition: pci_ids.h:3786
#define PCI_DID_INTEL_TGP_H_SPI0
Definition: pci_ids.h:3767
#define PCI_DID_INTEL_ICP_SPI2
Definition: pci_ids.h:3749
#define PCI_DID_INTEL_CMP_SPI2
Definition: pci_ids.h:3753
#define PCI_DID_INTEL_MCC_GSPI0
Definition: pci_ids.h:3773
#define PCI_DID_INTEL_TGP_GSPI4
Definition: pci_ids.h:3764
#define PCI_DID_INTEL_CMP_SPI1
Definition: pci_ids.h:3752
#define PCI_DID_INTEL_ADP_S_SPI0
Definition: pci_ids.h:3791
#define PCI_DID_INTEL_TGP_GSPI0
Definition: pci_ids.h:3760
#define PCI_DID_INTEL_JSP_SPI1
Definition: pci_ids.h:3777
#define PCI_DID_INTEL_ICP_HWSEQ_SPI
Definition: pci_ids.h:3750
#define PCI_DID_INTEL_CNP_H_HWSEQ_SPI
Definition: pci_ids.h:3746
#define PCI_DID_INTEL_ADP_S_SPI1
Definition: pci_ids.h:3792
#define PCI_DID_INTEL_GLK_SPI1
Definition: pci_ids.h:3735
#define PCI_DID_INTEL_SPT_SPI1
Definition: pci_ids.h:3727
#define PCI_VID_INTEL
Definition: pci_ids.h:2157
#define PCI_DID_INTEL_MCC_GSPI2
Definition: pci_ids.h:3775
#define PCI_DID_INTEL_TGP_H_GSPI0
Definition: pci_ids.h:3768
#define PCI_DID_INTEL_GLK_SPI2
Definition: pci_ids.h:3736
#define PCI_DID_INTEL_TGP_GSPI3
Definition: pci_ids.h:3763
#define PCI_DID_INTEL_MTL_GSPI2
Definition: pci_ids.h:3809
#define PCI_DID_INTEL_MTL_HWSEQ_SPI
Definition: pci_ids.h:3806
#define PCI_DID_INTEL_TGP_GSPI1
Definition: pci_ids.h:3761
#define PCI_DID_INTEL_CMP_H_SPI1
Definition: pci_ids.h:3756
#define PCI_DID_INTEL_CMP_H_SPI0
Definition: pci_ids.h:3755
#define PCI_DID_INTEL_CMP_H_HWSEQ_SPI
Definition: pci_ids.h:3758
#define PCI_DID_INTEL_CNP_H_SPI2
Definition: pci_ids.h:3745
#define PCI_DID_INTEL_ADP_S_SPI2
Definition: pci_ids.h:3793
#define PCI_DID_INTEL_SPT_SPI3
Definition: pci_ids.h:3729
#define PCI_DID_INTEL_APL_SPI0
Definition: pci_ids.h:3730
#define PCI_DID_INTEL_ADP_S_SPI4
Definition: pci_ids.h:3795
u32 pci_devfn_t
Definition: pci_type.h:8
void scan_generic_bus(struct device *bus)
Definition: root_device.c:52
uintptr_t base
Definition: uart.c:17
const struct spi_ctrlr_buses spi_ctrlr_bus_map[]
Definition: spi.c:401
const size_t spi_ctrlr_bus_map_count
Definition: spi.c:408
#define PCH_DEV_SPI
Definition: pci_devs.h:241
int spi_soc_devfn_to_bus(unsigned int devfn)
Definition: spi.c:15
static const struct pci_driver pch_spi __pci_driver
Definition: spi.c:204
static struct device_operations spi_dev_ops
Definition: spi.c:113
static uint32_t spi_get_wp_cse_ro_limit(union spi_bios_gpr0 bios_gpr0)
Definition: spi.c:82
static int spi_dev_to_bus(struct device *dev)
Definition: spi.c:104
static struct spi_bus_operations spi_bus_ops
Definition: spi.c:109
#define MMIO_BIOS_GPR0
Definition: spi.c:27
static uint32_t spi_get_wp_cse_ro_start_offset(union spi_bios_gpr0 bios_gpr0)
Definition: spi.c:77
static const unsigned short pci_device_ids[]
Definition: spi.c:122
static uint32_t spi_read_bios_gpr0(void)
Definition: spi.c:72
void spi_get_wp_cse_ro_range(uint32_t *base, uint32_t *limit)
Definition: spi.c:95
static uintptr_t get_spi_bar(pci_devfn_t dev)
Definition: spi.c:54
static uint32_t spi_read_bar(pci_devfn_t dev, uint32_t offset)
Definition: spi.c:67
bool is_spi_wp_cse_ro_en(void)
Definition: spi.c:87
unsigned int uint32_t
Definition: stdint.h:14
unsigned long uintptr_t
Definition: stdint.h:21
Definition: x86.c:23
void(* read_resources)(struct device *dev)
Definition: device.h:39
struct pci_path pci
Definition: path.h:116
Definition: device.h:107
struct device_path path
Definition: device.h:115
unsigned int devfn
Definition: path.h:54
int(* dev_to_bus)(struct device *dev)
Definition: spi.h:14
const struct spi_ctrlr * ctrlr
Definition: spi-generic.h:175
uint32_t protect_range_base
Definition: spi.c:36
uint32_t protect_range_limit
Definition: spi.c:45
uint32_t data
Definition: spi.c:50
uint32_t read_protect_en
Definition: spi.c:39
struct spi_bios_gpr0::@549 fields
uint32_t write_protect_en
Definition: spi.c:47