Debian Codegraph @

src/soc/intel/common/block/i2c/i2c.c

raw symbols SCIP
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_ids.h>
9 #include <drivers/i2c/designware/dw_i2c.h>
10 #include <intelblocks/cfg.h>
11 #include <intelblocks/lpss.h>
12 #include <soc/iomap.h>
13 #include <soc/pci_devs.h>
14
15 int dw_i2c_soc_dev_to_bus(const struct device *dev)
16 {
17 pci_devfn_t devfn = dev->path.pci.devfn;
18 return dw_i2c_soc_devfn_to_bus(devfn);
19 }
20
21 /* Getting I2C bus configuration from devicetree config */
22 const struct dw_i2c_bus_config *dw_i2c_get_soc_cfg(unsigned int bus)
23 {
24 const struct soc_intel_common_config *common_config;
25 common_config = chip_get_common_soc_structure();
26
27 return &common_config->i2c[bus];
28 }
29
30 /* Get base address for early init of I2C controllers. */
31 uintptr_t dw_i2c_get_soc_early_base(unsigned int bus)
32 {
33 return EARLY_I2C_BASE(bus);
34 }
35
36 #if !ENV_PAYLOAD_LOADER
37 static int lpss_i2c_early_init_bus(unsigned int bus)
38 {
39 const struct dw_i2c_bus_config *config;
40 pci_devfn_t dev;
41 int devfn;
42 uintptr_t base;
43
44 /* Find the PCI device for this bus controller */
45 devfn = dw_i2c_soc_bus_to_devfn(bus);
46 if (devfn < 0) {
47 printk(BIOS_ERR, "I2C%u device not found\n", bus);
48 return -1;
49 }
50
51 /* Look up the controller device in the devicetree */
52 dev = PCI_DEV(0, PCI_SLOT(devfn), PCI_FUNC(devfn));
53
54 /* Skip if not enabled for early init */
55 config = dw_i2c_get_soc_cfg(bus);
56 if (!config || !config->early_init) {
57 printk(BIOS_DEBUG, "I2C%u not enabled for early init\n", bus);
58 return -1;
59 }
60
61 /* Prepare early base address for access before memory */
62 base = dw_i2c_get_soc_early_base(bus);
63 pci_write_config32(dev, PCI_BASE_ADDRESS_0, base);
64 pci_write_config16(dev, PCI_COMMAND,
65 PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
66
67 /* Take device out of reset */
68 lpss_reset_release(base);
69
70 /* Ensure controller is in D0 state */
71 lpss_set_power_state(dev, STATE_D0);
72
73 /* Initialize the controller */
74 if (dw_i2c_init(bus, config) != CB_SUCCESS) {
75 printk(BIOS_ERR, "I2C%u failed to initialize\n", bus);
76 return -1;
77 }
78
79 return 0;
80 }
81
82 uintptr_t dw_i2c_base_address(unsigned int bus)
83 {
84 int devfn;
85 pci_devfn_t dev;
86 uintptr_t base;
87
88 /* Find device+function for this controller */
89 devfn = dw_i2c_soc_bus_to_devfn(bus);
90 if (devfn < 0)
91 return 0;
92
93 /* Form a PCI address for this device */
94 dev = PCI_DEV(0, PCI_SLOT(devfn), PCI_FUNC(devfn));
95
96 /* Read the first base address for this device */
97 base = ALIGN_DOWN(pci_read_config32(dev, PCI_BASE_ADDRESS_0), 16);
98
99 /* Attempt to initialize bus if base is not set yet */
100 if (!base && !lpss_i2c_early_init_bus(bus))
101 base = ALIGN_DOWN(pci_read_config32(dev, PCI_BASE_ADDRESS_0),
102 16);
103 return base;
104 }
105 #else
106
107 uintptr_t dw_i2c_base_address(unsigned int bus)
108 {
109 int devfn;
110 struct device *dev;
111 struct resource *res;
112
113 /* bus -> devfn */
114 devfn = dw_i2c_soc_bus_to_devfn(bus);
115
116 if (devfn < 0)
117 return 0;
118
119 /* devfn -> dev */
120 dev = pcidev_path_on_root(devfn);
121 if (!dev || !dev->enabled)
122 return 0;
123
124 /* dev -> bar0 */
125 res = probe_resource(dev, PCI_BASE_ADDRESS_0);
126 if (res)
127 return res->base;
128
129 /* No resource found yet, it's possible this is running in the
130 * PAYLOAD_LOADER stage before resources have been assigned yet,
131 * therefore, any early init BAR should still be valid. */
132
133 /* Read the first base address for this device */
134 return (uintptr_t)ALIGN_DOWN(pci_read_config32(dev, PCI_BASE_ADDRESS_0), 16);
135 }
136
137 /*
138 * This function ensures that the device is actually out of reset and
139 * its ready for initialization sequence.
140 */
141 static void dw_i2c_device_init(struct device *dev)
142 {
143 uintptr_t base_address;
144 int bus = dw_i2c_soc_dev_to_bus(dev);
145
146 if (bus < 0)
147 return;
148
149 base_address = dw_i2c_base_address(bus);
150 if (!base_address)
151 return;
152
153 /* Ensure controller is in D0 state */
154 lpss_set_power_state(PCI_BDF(dev), STATE_D0);
155
156 /* Take device out of reset if its not done before */
157 if (lpss_is_controller_in_reset(base_address))
158 lpss_reset_release(base_address);
159
160 dw_i2c_dev_init(dev);
161 }
162
163 struct device_operations i2c_dev_ops = {
164 .read_resources = pci_dev_read_resources,
165 .set_resources = pci_dev_set_resources,
166 .enable_resources = pci_dev_enable_resources,
167 .scan_bus = scan_static_bus,
168 .ops_i2c_bus = &dw_i2c_bus_ops,
169 .ops_pci = &pci_dev_ops_pci,
170 .init = dw_i2c_device_init,
171 #if CONFIG(HAVE_ACPI_TABLES)
172 .acpi_fill_ssdt = dw_i2c_acpi_fill_ssdt,
173 #endif
174 };
175
176 static const unsigned short pci_device_ids[] = {
177 PCI_DID_INTEL_WCL_I2C0,
178 PCI_DID_INTEL_WCL_I2C1,
179 PCI_DID_INTEL_WCL_I2C2,
180 PCI_DID_INTEL_WCL_I2C3,
181 PCI_DID_INTEL_WCL_I2C4,
182 PCI_DID_INTEL_WCL_I2C5,
183 PCI_DID_INTEL_PTL_H_I2C0,
184 PCI_DID_INTEL_PTL_H_I2C1,
185 PCI_DID_INTEL_PTL_H_I2C2,
186 PCI_DID_INTEL_PTL_H_I2C3,
187 PCI_DID_INTEL_PTL_H_I2C4,
188 PCI_DID_INTEL_PTL_H_I2C5,
189 PCI_DID_INTEL_PTL_U_H_I2C0,
190 PCI_DID_INTEL_PTL_U_H_I2C1,
191 PCI_DID_INTEL_PTL_U_H_I2C2,
192 PCI_DID_INTEL_PTL_U_H_I2C3,
193 PCI_DID_INTEL_PTL_U_H_I2C4,
194 PCI_DID_INTEL_PTL_U_H_I2C5,
195 PCI_DID_INTEL_LNL_I2C0,
196 PCI_DID_INTEL_LNL_I2C1,
197 PCI_DID_INTEL_LNL_I2C2,
198 PCI_DID_INTEL_LNL_I2C3,
199 PCI_DID_INTEL_LNL_I2C4,
200 PCI_DID_INTEL_LNL_I2C5,
201 PCI_DID_INTEL_MTL_I2C0,
202 PCI_DID_INTEL_MTL_I2C1,
203 PCI_DID_INTEL_MTL_I2C2,
204 PCI_DID_INTEL_MTL_I2C3,
205 PCI_DID_INTEL_MTL_I2C4,
206 PCI_DID_INTEL_MTL_I2C5,
207 PCI_DID_INTEL_ARL_I2C0,
208 PCI_DID_INTEL_ARL_I2C1,
209 PCI_DID_INTEL_ARL_I2C2,
210 PCI_DID_INTEL_ARL_I2C3,
211 PCI_DID_INTEL_ARL_I2C4,
212 PCI_DID_INTEL_ARL_I2C5,
213 PCI_DID_INTEL_ARP_S_I2C0,
214 PCI_DID_INTEL_ARP_S_I2C1,
215 PCI_DID_INTEL_ARP_S_I2C2,
216 PCI_DID_INTEL_ARP_S_I2C3,
217 PCI_DID_INTEL_ARP_S_I2C4,
218 PCI_DID_INTEL_ARP_S_I2C5,
219 PCI_DID_INTEL_APL_I2C0,
220 PCI_DID_INTEL_APL_I2C1,
221 PCI_DID_INTEL_APL_I2C2,
222 PCI_DID_INTEL_APL_I2C3,
223 PCI_DID_INTEL_APL_I2C4,
224 PCI_DID_INTEL_APL_I2C5,
225 PCI_DID_INTEL_APL_I2C6,
226 PCI_DID_INTEL_APL_I2C7,
227 PCI_DID_INTEL_CNL_I2C0,
228 PCI_DID_INTEL_CNL_I2C1,
229 PCI_DID_INTEL_CNL_I2C2,
230 PCI_DID_INTEL_CNL_I2C3,
231 PCI_DID_INTEL_CNL_I2C4,
232 PCI_DID_INTEL_CNL_I2C5,
233 PCI_DID_INTEL_GLK_I2C0,
234 PCI_DID_INTEL_GLK_I2C1,
235 PCI_DID_INTEL_GLK_I2C2,
236 PCI_DID_INTEL_GLK_I2C3,
237 PCI_DID_INTEL_GLK_I2C4,
238 PCI_DID_INTEL_GLK_I2C5,
239 PCI_DID_INTEL_GLK_I2C6,
240 PCI_DID_INTEL_GLK_I2C7,
241 PCI_DID_INTEL_CNP_H_I2C0,
242 PCI_DID_INTEL_CNP_H_I2C1,
243 PCI_DID_INTEL_CNP_H_I2C2,
244 PCI_DID_INTEL_CNP_H_I2C3,
245 PCI_DID_INTEL_ICP_I2C0,
246 PCI_DID_INTEL_ICP_I2C1,
247 PCI_DID_INTEL_ICP_I2C2,
248 PCI_DID_INTEL_ICP_I2C3,
249 PCI_DID_INTEL_ICP_I2C4,
250 PCI_DID_INTEL_ICP_I2C5,
251 PCI_DID_INTEL_CMP_I2C0,
252 PCI_DID_INTEL_CMP_I2C1,
253 PCI_DID_INTEL_CMP_I2C2,
254 PCI_DID_INTEL_CMP_I2C3,
255 PCI_DID_INTEL_CMP_I2C4,
256 PCI_DID_INTEL_CMP_I2C5,
257 PCI_DID_INTEL_CMP_H_I2C0,
258 PCI_DID_INTEL_CMP_H_I2C1,
259 PCI_DID_INTEL_CMP_H_I2C2,
260 PCI_DID_INTEL_CMP_H_I2C3,
261 PCI_DID_INTEL_TGP_I2C0,
262 PCI_DID_INTEL_TGP_I2C1,
263 PCI_DID_INTEL_TGP_I2C2,
264 PCI_DID_INTEL_TGP_I2C3,
265 PCI_DID_INTEL_TGP_I2C4,
266 PCI_DID_INTEL_TGP_I2C5,
267 PCI_DID_INTEL_TGP_I2C6,
268 PCI_DID_INTEL_TGP_I2C7,
269 PCI_DID_INTEL_TGP_H_I2C0,
270 PCI_DID_INTEL_TGP_H_I2C1,
271 PCI_DID_INTEL_TGP_H_I2C2,
272 PCI_DID_INTEL_TGP_H_I2C3,
273 PCI_DID_INTEL_TGP_H_I2C4,
274 PCI_DID_INTEL_TGP_H_I2C5,
275 PCI_DID_INTEL_TGP_H_I2C6,
276 PCI_DID_INTEL_MCC_I2C0,
277 PCI_DID_INTEL_MCC_I2C1,
278 PCI_DID_INTEL_MCC_I2C2,
279 PCI_DID_INTEL_MCC_I2C3,
280 PCI_DID_INTEL_MCC_I2C4,
281 PCI_DID_INTEL_MCC_I2C5,
282 PCI_DID_INTEL_MCC_I2C6,
283 PCI_DID_INTEL_MCC_I2C7,
284 PCI_DID_INTEL_JSP_I2C0,
285 PCI_DID_INTEL_JSP_I2C1,
286 PCI_DID_INTEL_JSP_I2C2,
287 PCI_DID_INTEL_JSP_I2C3,
288 PCI_DID_INTEL_JSP_I2C4,
289 PCI_DID_INTEL_JSP_I2C5,
290 PCI_DID_INTEL_ADP_P_I2C0,
291 PCI_DID_INTEL_ADP_P_I2C1,
292 PCI_DID_INTEL_ADP_P_I2C2,
293 PCI_DID_INTEL_ADP_P_I2C3,
294 PCI_DID_INTEL_ADP_P_I2C4,
295 PCI_DID_INTEL_ADP_P_I2C5,
296 PCI_DID_INTEL_ADP_P_I2C6,
297 PCI_DID_INTEL_ADP_P_I2C7,
298 PCI_DID_INTEL_ADP_S_I2C0,
299 PCI_DID_INTEL_ADP_S_I2C1,
300 PCI_DID_INTEL_ADP_S_I2C2,
301 PCI_DID_INTEL_ADP_S_I2C3,
302 PCI_DID_INTEL_ADP_S_I2C4,
303 PCI_DID_INTEL_ADP_S_I2C5,
304 PCI_DID_INTEL_ADP_M_N_I2C0,
305 PCI_DID_INTEL_ADP_M_N_I2C1,
306 PCI_DID_INTEL_ADP_M_N_I2C2,
307 PCI_DID_INTEL_ADP_M_N_I2C3,
308 PCI_DID_INTEL_ADP_M_N_I2C4,
309 PCI_DID_INTEL_ADP_M_N_I2C5,
310 PCI_DID_INTEL_RPP_S_I2C0,
311 PCI_DID_INTEL_RPP_S_I2C1,
312 PCI_DID_INTEL_RPP_S_I2C2,
313 PCI_DID_INTEL_RPP_S_I2C3,
314 PCI_DID_INTEL_RPP_S_I2C4,
315 PCI_DID_INTEL_RPP_S_I2C5,
316 0,
317 };
318
319 static const struct pci_driver pch_i2c __pci_driver = {
320 .ops = &i2c_dev_ops,
321 .vendor = PCI_VID_INTEL,
322 .devices = pci_device_ids,
323 };
324 #endif