coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
i2c.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #include <device/pci_ops.h>
4 #include <console/console.h>
5 #include <device/device.h>
6 #include <device/i2c_simple.h>
7 #include <device/pci.h>
8 #include <device/pci_def.h>
9 #include <device/pci_ids.h>
11 #include <intelblocks/cfg.h>
12 #include <intelblocks/lpss.h>
13 #include <soc/iomap.h>
14 #include <soc/pci_devs.h>
15 
16 int dw_i2c_soc_dev_to_bus(const struct device *dev)
17 {
18  pci_devfn_t devfn = dev->path.pci.devfn;
19  return dw_i2c_soc_devfn_to_bus(devfn);
20 }
21 
22 /* Getting I2C bus configuration from devicetree config */
23 const struct dw_i2c_bus_config *dw_i2c_get_soc_cfg(unsigned int bus)
24 {
25  const struct soc_intel_common_config *common_config;
26  common_config = chip_get_common_soc_structure();
27 
28  return &common_config->i2c[bus];
29 }
30 
31 /* Get base address for early init of I2C controllers. */
33 {
34  return EARLY_I2C_BASE(bus);
35 }
36 
37 #if !ENV_PAYLOAD_LOADER
38 static int lpss_i2c_early_init_bus(unsigned int bus)
39 {
40  const struct dw_i2c_bus_config *config;
41  pci_devfn_t dev;
42  int devfn;
44 
45  /* Find the PCI device for this bus controller */
47  if (devfn < 0) {
48  printk(BIOS_ERR, "I2C%u device not found\n", bus);
49  return -1;
50  }
51 
52  /* Look up the controller device in the devicetree */
53  dev = PCI_DEV(0, PCI_SLOT(devfn), PCI_FUNC(devfn));
54 
55  /* Skip if not enabled for early init */
57  if (!config || !config->early_init) {
58  printk(BIOS_DEBUG, "I2C%u not enabled for early init\n", bus);
59  return -1;
60  }
61 
62  /* Prepare early base address for access before memory */
67 
68  /* Take device out of reset */
70 
71  /* Ensure controller is in D0 state */
73 
74  /* Initialize the controller */
75  if (dw_i2c_init(bus, config) != CB_SUCCESS) {
76  printk(BIOS_ERR, "I2C%u failed to initialize\n", bus);
77  return -1;
78  }
79 
80  return 0;
81 }
82 
84 {
85  int devfn;
86  pci_devfn_t dev;
88 
89  /* Find device+function for this controller */
91  if (devfn < 0)
92  return (uintptr_t)NULL;
93 
94  /* Form a PCI address for this device */
95  dev = PCI_DEV(0, PCI_SLOT(devfn), PCI_FUNC(devfn));
96 
97  /* Read the first base address for this device */
99 
100  /* Attempt to initialize bus if base is not set yet */
101  if (!base && !lpss_i2c_early_init_bus(bus))
103  16);
104  return base;
105 }
106 #else
107 
108 uintptr_t dw_i2c_base_address(unsigned int bus)
109 {
110  int devfn;
111  struct device *dev;
112  struct resource *res;
113 
114  /* bus -> devfn */
115  devfn = dw_i2c_soc_bus_to_devfn(bus);
116 
117  if (devfn < 0)
118  return (uintptr_t)NULL;
119 
120  /* devfn -> dev */
121  dev = pcidev_path_on_root(devfn);
122  if (!dev || !dev->enabled)
123  return (uintptr_t)NULL;
124 
125  /* dev -> bar0 */
127  if (res)
128  return res->base;
129 
130  /* No resource found yet, it's possible this is running in the
131  * PAYLOAD_LOADER stage before resources have been assigned yet,
132  * therefore, any early init BAR should still be valid. */
133 
134  /* Read the first base address for this device */
136 }
137 
138 /*
139  * This function ensures that the device is actually out of reset and
140  * its ready for initialization sequence.
141  */
142 static void dw_i2c_device_init(struct device *dev)
143 {
144  uintptr_t base_address;
145  int bus = dw_i2c_soc_dev_to_bus(dev);
146 
147  if (bus < 0)
148  return;
149 
150  base_address = dw_i2c_base_address(bus);
151  if (!base_address)
152  return;
153 
154  /* Ensure controller is in D0 state */
156 
157  /* Take device out of reset if its not done before */
158  if (lpss_is_controller_in_reset(base_address))
159  lpss_reset_release(base_address);
160 
161  dw_i2c_dev_init(dev);
162 }
163 
164 static struct device_operations i2c_dev_ops = {
166  .set_resources = pci_dev_set_resources,
167  .enable_resources = pci_dev_enable_resources,
168  .scan_bus = scan_static_bus,
169  .ops_i2c_bus = &dw_i2c_bus_ops,
170  .ops_pci = &pci_dev_ops_pci,
171  .init = dw_i2c_device_init,
172 #if CONFIG(HAVE_ACPI_TABLES)
173  .acpi_fill_ssdt = dw_i2c_acpi_fill_ssdt,
174 #endif
175 };
176 
177 static const unsigned short pci_device_ids[] = {
285  0,
286 };
287 
288 static const struct pci_driver pch_i2c __pci_driver = {
289  .ops = &i2c_dev_ops,
290  .vendor = PCI_VID_INTEL,
291  .devices = pci_device_ids,
292 };
293 #endif
static const struct pci_driver asmedia_noaspm __pci_driver
static const unsigned short pci_device_ids[]
#define ALIGN_DOWN(x, a)
Definition: helpers.h:18
@ CB_SUCCESS
Call completed successfully.
Definition: cb_err.h:16
#define printk(level,...)
Definition: stdlib.h:16
DEVTREE_CONST struct device * pcidev_path_on_root(pci_devfn_t devfn)
Definition: device_const.c:255
struct resource * probe_resource(const struct device *dev, unsigned int index)
See if a resource structure already exists for a given index.
Definition: device_util.c:323
void dw_i2c_dev_init(struct device *dev)
Definition: dw_i2c.c:782
void dw_i2c_acpi_fill_ssdt(const struct device *dev)
Definition: dw_i2c.c:802
enum cb_err dw_i2c_init(unsigned int bus, const struct dw_i2c_bus_config *bcfg)
Definition: dw_i2c.c:691
const struct i2c_bus_operations dw_i2c_bus_ops
Definition: dw_i2c.c:851
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 __always_inline void pci_write_config16(const struct device *dev, u16 reg, u16 val)
Definition: pci_ops.h:70
#define EARLY_I2C_BASE(x)
Definition: iomap.h:88
#define BIOS_DEBUG
BIOS_DEBUG - Verbose output.
Definition: loglevel.h:128
#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
void lpss_reset_release(uintptr_t base)
Definition: lpss.c:47
bool lpss_is_controller_in_reset(uintptr_t base)
Definition: lpss.c:36
@ STATE_D0
Definition: lpss.h:11
enum board_config config
Definition: memory.c:448
#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
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_ADP_P_I2C2
Definition: pci_ids.h:3622
#define PCI_DID_INTEL_GLK_I2C5
Definition: pci_ids.h:3560
#define PCI_DID_INTEL_CMP_I2C1
Definition: pci_ids.h:3580
#define PCI_DID_INTEL_TGP_I2C5
Definition: pci_ids.h:3594
#define PCI_DID_INTEL_JSP_I2C5
Definition: pci_ids.h:3618
#define PCI_DID_INTEL_APL_I2C7
Definition: pci_ids.h:3554
#define PCI_DID_INTEL_MTL_I2C2
Definition: pci_ids.h:3645
#define PCI_DID_INTEL_ADP_S_I2C3
Definition: pci_ids.h:3632
#define PCI_DID_INTEL_ICP_I2C1
Definition: pci_ids.h:3574
#define PCI_DID_INTEL_ADP_M_N_I2C0
Definition: pci_ids.h:3636
#define PCI_DID_INTEL_TGP_H_I2C2
Definition: pci_ids.h:3599
#define PCI_DID_INTEL_CMP_I2C5
Definition: pci_ids.h:3584
#define PCI_DID_INTEL_APL_I2C1
Definition: pci_ids.h:3548
#define PCI_DID_INTEL_APL_I2C5
Definition: pci_ids.h:3552
#define PCI_DID_INTEL_TGP_H_I2C5
Definition: pci_ids.h:3602
#define PCI_DID_INTEL_TGP_H_I2C1
Definition: pci_ids.h:3598
#define PCI_DID_INTEL_MTL_I2C5
Definition: pci_ids.h:3648
#define PCI_DID_INTEL_TGP_H_I2C3
Definition: pci_ids.h:3600
#define PCI_DID_INTEL_TGP_I2C3
Definition: pci_ids.h:3592
#define PCI_DID_INTEL_ICP_I2C4
Definition: pci_ids.h:3577
#define PCI_DID_INTEL_CNL_I2C2
Definition: pci_ids.h:3565
#define PCI_DID_INTEL_ADP_P_I2C7
Definition: pci_ids.h:3627
#define PCI_DID_INTEL_CMP_I2C0
Definition: pci_ids.h:3579
#define PCI_DID_INTEL_GLK_I2C6
Definition: pci_ids.h:3561
#define PCI_DID_INTEL_MCC_I2C2
Definition: pci_ids.h:3606
#define PCI_DID_INTEL_GLK_I2C4
Definition: pci_ids.h:3559
#define PCI_DID_INTEL_SPT_I2C3
Definition: pci_ids.h:3540
#define PCI_DID_INTEL_MTL_I2C3
Definition: pci_ids.h:3646
#define PCI_DID_INTEL_UPT_H_I2C0
Definition: pci_ids.h:3543
#define PCI_DID_INTEL_ADP_S_I2C0
Definition: pci_ids.h:3629
#define PCI_DID_INTEL_CMP_I2C3
Definition: pci_ids.h:3582
#define PCI_DID_INTEL_MTL_I2C1
Definition: pci_ids.h:3644
#define PCI_DID_INTEL_MTL_I2C0
Definition: pci_ids.h:3643
#define PCI_DID_INTEL_ADP_P_I2C5
Definition: pci_ids.h:3625
#define PCI_DID_INTEL_MCC_I2C4
Definition: pci_ids.h:3608
#define PCI_DID_INTEL_TGP_H_I2C4
Definition: pci_ids.h:3601
#define PCI_DID_INTEL_TGP_H_I2C6
Definition: pci_ids.h:3603
#define PCI_DID_INTEL_CNP_H_I2C2
Definition: pci_ids.h:3571
#define PCI_DID_INTEL_JSP_I2C1
Definition: pci_ids.h:3614
#define PCI_DID_INTEL_ADP_P_I2C0
Definition: pci_ids.h:3620
#define PCI_DID_INTEL_ICP_I2C2
Definition: pci_ids.h:3575
#define PCI_DID_INTEL_UPT_H_I2C1
Definition: pci_ids.h:3544
#define PCI_DID_INTEL_ADP_M_N_I2C1
Definition: pci_ids.h:3637
#define PCI_DID_INTEL_CNP_H_I2C3
Definition: pci_ids.h:3572
#define PCI_DID_INTEL_JSP_I2C3
Definition: pci_ids.h:3616
#define PCI_DID_INTEL_ADP_P_I2C3
Definition: pci_ids.h:3623
#define PCI_DID_INTEL_ADP_M_N_I2C3
Definition: pci_ids.h:3639
#define PCI_DID_INTEL_MCC_I2C6
Definition: pci_ids.h:3610
#define PCI_DID_INTEL_TGP_I2C1
Definition: pci_ids.h:3590
#define PCI_DID_INTEL_MCC_I2C5
Definition: pci_ids.h:3609
#define PCI_DID_INTEL_CMP_H_I2C1
Definition: pci_ids.h:3586
#define PCI_DID_INTEL_APL_I2C6
Definition: pci_ids.h:3553
#define PCI_DID_INTEL_CNL_I2C4
Definition: pci_ids.h:3567
#define PCI_DID_INTEL_UPT_H_I2C2
Definition: pci_ids.h:3545
#define PCI_DID_INTEL_SPT_I2C0
Definition: pci_ids.h:3537
#define PCI_DID_INTEL_MCC_I2C0
Definition: pci_ids.h:3604
#define PCI_DID_INTEL_ADP_M_N_I2C4
Definition: pci_ids.h:3640
#define PCI_DID_INTEL_MCC_I2C7
Definition: pci_ids.h:3611
#define PCI_DID_INTEL_TGP_I2C6
Definition: pci_ids.h:3595
#define PCI_DID_INTEL_ADP_P_I2C1
Definition: pci_ids.h:3621
#define PCI_DID_INTEL_TGP_I2C7
Definition: pci_ids.h:3596
#define PCI_DID_INTEL_ICP_I2C0
Definition: pci_ids.h:3573
#define PCI_DID_INTEL_ADP_M_N_I2C2
Definition: pci_ids.h:3638
#define PCI_DID_INTEL_CNL_I2C1
Definition: pci_ids.h:3564
#define PCI_DID_INTEL_UPT_H_I2C3
Definition: pci_ids.h:3546
#define PCI_DID_INTEL_GLK_I2C3
Definition: pci_ids.h:3558
#define PCI_DID_INTEL_APL_I2C3
Definition: pci_ids.h:3550
#define PCI_DID_INTEL_CNP_H_I2C1
Definition: pci_ids.h:3570
#define PCI_DID_INTEL_ICP_I2C5
Definition: pci_ids.h:3578
#define PCI_DID_INTEL_CNL_I2C0
Definition: pci_ids.h:3563
#define PCI_DID_INTEL_ADP_S_I2C1
Definition: pci_ids.h:3630
#define PCI_DID_INTEL_CMP_I2C4
Definition: pci_ids.h:3583
#define PCI_DID_INTEL_SPT_I2C5
Definition: pci_ids.h:3542
#define PCI_DID_INTEL_APL_I2C2
Definition: pci_ids.h:3549
#define PCI_DID_INTEL_CMP_H_I2C2
Definition: pci_ids.h:3587
#define PCI_DID_INTEL_SPT_I2C1
Definition: pci_ids.h:3538
#define PCI_DID_INTEL_CNL_I2C3
Definition: pci_ids.h:3566
#define PCI_DID_INTEL_SPT_I2C2
Definition: pci_ids.h:3539
#define PCI_DID_INTEL_JSP_I2C0
Definition: pci_ids.h:3613
#define PCI_DID_INTEL_ADP_S_I2C2
Definition: pci_ids.h:3631
#define PCI_DID_INTEL_CMP_H_I2C0
Definition: pci_ids.h:3585
#define PCI_DID_INTEL_GLK_I2C7
Definition: pci_ids.h:3562
#define PCI_DID_INTEL_TGP_I2C0
Definition: pci_ids.h:3589
#define PCI_DID_INTEL_ICP_I2C3
Definition: pci_ids.h:3576
#define PCI_DID_INTEL_CNL_I2C5
Definition: pci_ids.h:3568
#define PCI_DID_INTEL_GLK_I2C1
Definition: pci_ids.h:3556
#define PCI_DID_INTEL_JSP_I2C2
Definition: pci_ids.h:3615
#define PCI_DID_INTEL_MTL_I2C4
Definition: pci_ids.h:3647
#define PCI_DID_INTEL_SPT_I2C4
Definition: pci_ids.h:3541
#define PCI_DID_INTEL_GLK_I2C2
Definition: pci_ids.h:3557
#define PCI_DID_INTEL_MCC_I2C3
Definition: pci_ids.h:3607
#define PCI_DID_INTEL_ADP_P_I2C4
Definition: pci_ids.h:3624
#define PCI_VID_INTEL
Definition: pci_ids.h:2157
#define PCI_DID_INTEL_MCC_I2C1
Definition: pci_ids.h:3605
#define PCI_DID_INTEL_CMP_H_I2C3
Definition: pci_ids.h:3588
#define PCI_DID_INTEL_JSP_I2C4
Definition: pci_ids.h:3617
#define PCI_DID_INTEL_TGP_I2C4
Definition: pci_ids.h:3593
#define PCI_DID_INTEL_GLK_I2C0
Definition: pci_ids.h:3555
#define PCI_DID_INTEL_ADP_M_N_I2C5
Definition: pci_ids.h:3641
#define PCI_DID_INTEL_CNP_H_I2C0
Definition: pci_ids.h:3569
#define PCI_DID_INTEL_ADP_S_I2C5
Definition: pci_ids.h:3634
#define PCI_DID_INTEL_CMP_I2C2
Definition: pci_ids.h:3581
#define PCI_DID_INTEL_TGP_I2C2
Definition: pci_ids.h:3591
#define PCI_DID_INTEL_TGP_H_I2C0
Definition: pci_ids.h:3597
#define PCI_DID_INTEL_ADP_P_I2C6
Definition: pci_ids.h:3626
#define PCI_DID_INTEL_ADP_S_I2C4
Definition: pci_ids.h:3633
#define PCI_DID_INTEL_APL_I2C4
Definition: pci_ids.h:3551
#define PCI_DID_INTEL_APL_I2C0
Definition: pci_ids.h:3547
#define PCI_BDF(dev)
Definition: pci_type.h:28
#define PCI_DEV(SEGBUS, DEV, FN)
Definition: pci_type.h:14
u32 pci_devfn_t
Definition: pci_type.h:8
void scan_static_bus(struct device *bus)
Definition: root_device.c:89
uintptr_t base
Definition: uart.c:17
uintptr_t dw_i2c_base_address(unsigned int bus)
Definition: i2c.c:19
int dw_i2c_soc_dev_to_bus(const struct device *dev)
Definition: i2c.c:63
const struct dw_i2c_bus_config * dw_i2c_get_soc_cfg(unsigned int bus)
Definition: i2c.c:33
int dw_i2c_soc_bus_to_devfn(unsigned int bus)
Definition: i2c.c:36
int dw_i2c_soc_devfn_to_bus(unsigned int devfn)
Definition: i2c.c:13
const struct soc_intel_common_config * chip_get_common_soc_structure(void)
Definition: chip.c:5
static int lpss_i2c_early_init_bus(unsigned int bus)
Definition: i2c.c:38
uintptr_t dw_i2c_get_soc_early_base(unsigned int bus)
Definition: i2c.c:32
#define NULL
Definition: stddef.h:19
unsigned long uintptr_t
Definition: stdint.h:21
Definition: device.h:76
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 enabled
Definition: device.h:122
unsigned int devfn
Definition: path.h:54
resource_t base
Definition: resource.h:45
struct dw_i2c_bus_config i2c[CONFIG_SOC_INTEL_I2C_DEV_MAX]
Definition: cfg.h:24