dwiic_pci.c revision 1.13 1 1.13 thorpej /* $NetBSD: dwiic_pci.c,v 1.13 2025/09/15 15:31:44 thorpej Exp $ */
2 1.1 bouyer
3 1.1 bouyer /*-
4 1.1 bouyer * Copyright (c) 2017 The NetBSD Foundation, Inc.
5 1.1 bouyer * All rights reserved.
6 1.1 bouyer *
7 1.1 bouyer * This code is derived from software contributed to The NetBSD Foundation
8 1.1 bouyer * by Manuel Bouyer.
9 1.1 bouyer *
10 1.1 bouyer * Redistribution and use in source and binary forms, with or without
11 1.1 bouyer * modification, are permitted provided that the following conditions
12 1.1 bouyer * are met:
13 1.1 bouyer * 1. Redistributions of source code must retain the above copyright
14 1.1 bouyer * notice, this list of conditions and the following disclaimer.
15 1.1 bouyer * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 bouyer * notice, this list of conditions and the following disclaimer in the
17 1.1 bouyer * documentation and/or other materials provided with the distribution.
18 1.1 bouyer *
19 1.1 bouyer * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 bouyer * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 bouyer * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 bouyer * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 bouyer * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 bouyer * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 bouyer * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 bouyer * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 bouyer * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 bouyer * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 bouyer * POSSIBILITY OF SUCH DAMAGE.
30 1.1 bouyer */
31 1.1 bouyer /*
32 1.1 bouyer * Synopsys DesignWare I2C controller, PCI front-end
33 1.1 bouyer */
34 1.1 bouyer
35 1.1 bouyer #include <sys/cdefs.h>
36 1.13 thorpej __KERNEL_RCSID(0, "$NetBSD: dwiic_pci.c,v 1.13 2025/09/15 15:31:44 thorpej Exp $");
37 1.1 bouyer
38 1.1 bouyer #include <sys/param.h>
39 1.1 bouyer #include <sys/systm.h>
40 1.1 bouyer
41 1.1 bouyer #include <dev/pci/pcireg.h>
42 1.1 bouyer #include <dev/pci/pcivar.h>
43 1.1 bouyer #include <dev/pci/pcidevs.h>
44 1.1 bouyer
45 1.1 bouyer #include <dev/acpi/acpivar.h>
46 1.1 bouyer #include <dev/acpi/acpi_pci.h>
47 1.1 bouyer #include <dev/acpi/acpi_util.h>
48 1.1 bouyer
49 1.1 bouyer #include <dev/ic/dwiic_var.h>
50 1.1 bouyer #include <arch/x86/pci/lpssreg.h>
51 1.1 bouyer
52 1.11 martin #include "acpica.h"
53 1.11 martin
54 1.1 bouyer //#define DWIIC_DEBUG
55 1.1 bouyer
56 1.1 bouyer #ifdef DWIIC_DEBUG
57 1.1 bouyer #define DPRINTF(x) printf x
58 1.1 bouyer #else
59 1.1 bouyer #define DPRINTF(x)
60 1.1 bouyer #endif
61 1.1 bouyer
62 1.10 andvar #if NACPICA > 0
63 1.10 andvar #define I2C_USE_ACPI
64 1.10 andvar #endif /* NACPICA > 0 */
65 1.10 andvar
66 1.1 bouyer struct pci_dwiic_softc {
67 1.1 bouyer struct dwiic_softc sc_dwiic;
68 1.1 bouyer pci_chipset_tag_t sc_pc;
69 1.1 bouyer pcitag_t sc_ptag;
70 1.1 bouyer };
71 1.1 bouyer
72 1.7 msaitoh #define VIDDID(a, b) PCI_ID_CODE(PCI_VENDOR_ ## a, PCI_PRODUCT_ ## a ## _ ## b)
73 1.7 msaitoh
74 1.7 msaitoh static const struct device_compatible_entry compat_data[] = {
75 1.7 msaitoh { .id = VIDDID(INTEL, CORE4G_M_S_I2C_0) },
76 1.7 msaitoh { .id = VIDDID(INTEL, CORE4G_M_S_I2C_1) },
77 1.7 msaitoh { .id = VIDDID(INTEL, 100SERIES_I2C_0) },
78 1.7 msaitoh { .id = VIDDID(INTEL, 100SERIES_I2C_1) },
79 1.7 msaitoh { .id = VIDDID(INTEL, 100SERIES_I2C_2) },
80 1.7 msaitoh { .id = VIDDID(INTEL, 100SERIES_I2C_3) },
81 1.7 msaitoh { .id = VIDDID(INTEL, 100SERIES_LP_I2C_0) },
82 1.7 msaitoh { .id = VIDDID(INTEL, 100SERIES_LP_I2C_1) },
83 1.7 msaitoh { .id = VIDDID(INTEL, 100SERIES_LP_I2C_2) },
84 1.7 msaitoh { .id = VIDDID(INTEL, 100SERIES_LP_I2C_3) },
85 1.7 msaitoh { .id = VIDDID(INTEL, 100SERIES_LP_I2C_4) },
86 1.7 msaitoh { .id = VIDDID(INTEL, 100SERIES_LP_I2C_5) },
87 1.7 msaitoh { .id = VIDDID(INTEL, 2HS_I2C_0) },
88 1.7 msaitoh { .id = VIDDID(INTEL, 2HS_I2C_1) },
89 1.7 msaitoh { .id = VIDDID(INTEL, 2HS_I2C_2) },
90 1.7 msaitoh { .id = VIDDID(INTEL, 2HS_I2C_3) },
91 1.7 msaitoh { .id = VIDDID(INTEL, 3HS_I2C_0) },
92 1.7 msaitoh { .id = VIDDID(INTEL, 3HS_I2C_1) },
93 1.7 msaitoh { .id = VIDDID(INTEL, 3HS_I2C_2) },
94 1.7 msaitoh { .id = VIDDID(INTEL, 3HS_I2C_3) },
95 1.7 msaitoh { .id = VIDDID(INTEL, 3HS_U_I2C_0) },
96 1.7 msaitoh { .id = VIDDID(INTEL, 3HS_U_I2C_1) },
97 1.7 msaitoh { .id = VIDDID(INTEL, 3HS_U_I2C_2) },
98 1.7 msaitoh { .id = VIDDID(INTEL, 3HS_U_I2C_3) },
99 1.7 msaitoh { .id = VIDDID(INTEL, 3HS_U_I2C_4) },
100 1.7 msaitoh { .id = VIDDID(INTEL, 3HS_U_I2C_5) },
101 1.7 msaitoh { .id = VIDDID(INTEL, 4HS_H_I2C_0) },
102 1.7 msaitoh { .id = VIDDID(INTEL, 4HS_H_I2C_1) },
103 1.7 msaitoh { .id = VIDDID(INTEL, 4HS_H_I2C_2) },
104 1.7 msaitoh { .id = VIDDID(INTEL, 4HS_H_I2C_3) },
105 1.7 msaitoh { .id = VIDDID(INTEL, 4HS_V_I2C_0) },
106 1.7 msaitoh { .id = VIDDID(INTEL, 4HS_V_I2C_1) },
107 1.7 msaitoh { .id = VIDDID(INTEL, 4HS_V_I2C_2) },
108 1.7 msaitoh { .id = VIDDID(INTEL, 4HS_V_I2C_3) },
109 1.7 msaitoh { .id = VIDDID(INTEL, CMTLK_I2C_0) }, /* 4HS LP */
110 1.7 msaitoh { .id = VIDDID(INTEL, CMTLK_I2C_1) },
111 1.7 msaitoh { .id = VIDDID(INTEL, CMTLK_I2C_2) },
112 1.7 msaitoh { .id = VIDDID(INTEL, CMTLK_I2C_3) },
113 1.7 msaitoh { .id = VIDDID(INTEL, CMTLK_I2C_4) },
114 1.7 msaitoh { .id = VIDDID(INTEL, CMTLK_I2C_5) },
115 1.7 msaitoh { .id = VIDDID(INTEL, 495_YU_I2C_0) },
116 1.7 msaitoh { .id = VIDDID(INTEL, 495_YU_I2C_1) },
117 1.7 msaitoh { .id = VIDDID(INTEL, 495_YU_I2C_2) },
118 1.7 msaitoh { .id = VIDDID(INTEL, 495_YU_I2C_3) },
119 1.7 msaitoh { .id = VIDDID(INTEL, 495_YU_I2C_4) },
120 1.7 msaitoh { .id = VIDDID(INTEL, 495_YU_I2C_5) },
121 1.7 msaitoh { .id = VIDDID(INTEL, 5HS_H_I2C_0) },
122 1.7 msaitoh { .id = VIDDID(INTEL, 5HS_H_I2C_1) },
123 1.7 msaitoh { .id = VIDDID(INTEL, 5HS_H_I2C_2) },
124 1.7 msaitoh { .id = VIDDID(INTEL, 5HS_H_I2C_3) },
125 1.7 msaitoh { .id = VIDDID(INTEL, 5HS_H_I2C_4) },
126 1.7 msaitoh { .id = VIDDID(INTEL, 5HS_H_I2C_5) },
127 1.7 msaitoh { .id = VIDDID(INTEL, 5HS_H_I2C_6) },
128 1.7 msaitoh { .id = VIDDID(INTEL, 5HS_LP_I2C_0) },
129 1.7 msaitoh { .id = VIDDID(INTEL, 5HS_LP_I2C_1) },
130 1.7 msaitoh { .id = VIDDID(INTEL, 5HS_LP_I2C_2) },
131 1.7 msaitoh { .id = VIDDID(INTEL, 5HS_LP_I2C_3) },
132 1.7 msaitoh { .id = VIDDID(INTEL, 5HS_LP_I2C_4) },
133 1.7 msaitoh { .id = VIDDID(INTEL, 5HS_LP_I2C_5) },
134 1.7 msaitoh { .id = VIDDID(INTEL, BAYTRAIL_SIO_I2C1) },
135 1.7 msaitoh { .id = VIDDID(INTEL, BAYTRAIL_SIO_I2C2) },
136 1.7 msaitoh { .id = VIDDID(INTEL, BAYTRAIL_SIO_I2C3) },
137 1.7 msaitoh { .id = VIDDID(INTEL, BAYTRAIL_SIO_I2C4) },
138 1.7 msaitoh { .id = VIDDID(INTEL, BAYTRAIL_SIO_I2C5) },
139 1.7 msaitoh { .id = VIDDID(INTEL, BAYTRAIL_SIO_I2C6) },
140 1.7 msaitoh { .id = VIDDID(INTEL, BAYTRAIL_SIO_I2C7) },
141 1.7 msaitoh { .id = VIDDID(INTEL, BSW_SIO_I2C_1) },
142 1.7 msaitoh { .id = VIDDID(INTEL, BSW_SIO_I2C_2) },
143 1.7 msaitoh { .id = VIDDID(INTEL, BSW_SIO_I2C_3) },
144 1.7 msaitoh { .id = VIDDID(INTEL, BSW_SIO_I2C_4) },
145 1.7 msaitoh { .id = VIDDID(INTEL, BSW_SIO_I2C_5) },
146 1.7 msaitoh { .id = VIDDID(INTEL, BSW_SIO_I2C_6) },
147 1.7 msaitoh { .id = VIDDID(INTEL, BSW_SIO_I2C_7) },
148 1.7 msaitoh { .id = VIDDID(INTEL, APL_I2C_0) },
149 1.7 msaitoh { .id = VIDDID(INTEL, APL_I2C_1) },
150 1.7 msaitoh { .id = VIDDID(INTEL, APL_I2C_2) },
151 1.7 msaitoh { .id = VIDDID(INTEL, APL_I2C_3) },
152 1.7 msaitoh { .id = VIDDID(INTEL, APL_I2C_4) },
153 1.7 msaitoh { .id = VIDDID(INTEL, APL_I2C_5) },
154 1.7 msaitoh { .id = VIDDID(INTEL, APL_I2C_6) },
155 1.7 msaitoh { .id = VIDDID(INTEL, APL_I2C_7) },
156 1.7 msaitoh { .id = VIDDID(INTEL, GLK_I2C_0) },
157 1.7 msaitoh { .id = VIDDID(INTEL, GLK_I2C_1) },
158 1.7 msaitoh { .id = VIDDID(INTEL, GLK_I2C_2) },
159 1.7 msaitoh { .id = VIDDID(INTEL, GLK_I2C_3) },
160 1.7 msaitoh { .id = VIDDID(INTEL, GLK_I2C_4) },
161 1.7 msaitoh { .id = VIDDID(INTEL, GLK_I2C_5) },
162 1.7 msaitoh { .id = VIDDID(INTEL, GLK_I2C_6) },
163 1.7 msaitoh { .id = VIDDID(INTEL, GLK_I2C_7) },
164 1.8 msaitoh { .id = VIDDID(INTEL, EHL_SIO_I2C_0) },
165 1.8 msaitoh { .id = VIDDID(INTEL, EHL_SIO_I2C_1) },
166 1.8 msaitoh { .id = VIDDID(INTEL, EHL_SIO_I2C_2) },
167 1.8 msaitoh { .id = VIDDID(INTEL, EHL_SIO_I2C_3) },
168 1.8 msaitoh { .id = VIDDID(INTEL, EHL_SIO_I2C_4) },
169 1.8 msaitoh { .id = VIDDID(INTEL, EHL_SIO_I2C_5) },
170 1.8 msaitoh { .id = VIDDID(INTEL, EHL_SIO_I2C_6) },
171 1.8 msaitoh { .id = VIDDID(INTEL, EHL_SIO_I2C_7) },
172 1.7 msaitoh { .id = VIDDID(INTEL, JSL_LPSS_I2C_0) },
173 1.7 msaitoh { .id = VIDDID(INTEL, JSL_LPSS_I2C_1) },
174 1.7 msaitoh { .id = VIDDID(INTEL, JSL_LPSS_I2C_2) },
175 1.7 msaitoh { .id = VIDDID(INTEL, JSL_LPSS_I2C_3) },
176 1.8 msaitoh { .id = VIDDID(INTEL, JSL_LPSS_I2C_4) },
177 1.8 msaitoh { .id = VIDDID(INTEL, JSL_LPSS_I2C_5) },
178 1.7 msaitoh
179 1.7 msaitoh PCI_COMPAT_EOL
180 1.7 msaitoh };
181 1.7 msaitoh
182 1.1 bouyer static uint32_t
183 1.1 bouyer lpss_read(struct pci_dwiic_softc *sc, int offset)
184 1.1 bouyer {
185 1.5 riastrad return bus_space_read_4(sc->sc_dwiic.sc_iot, sc->sc_dwiic.sc_ioh,
186 1.5 riastrad offset);
187 1.1 bouyer }
188 1.1 bouyer
189 1.1 bouyer static void
190 1.1 bouyer lpss_write(struct pci_dwiic_softc *sc, int offset, uint32_t val)
191 1.1 bouyer {
192 1.1 bouyer bus_space_write_4(sc->sc_dwiic.sc_iot, sc->sc_dwiic.sc_ioh,
193 1.1 bouyer offset, val);
194 1.1 bouyer }
195 1.1 bouyer
196 1.1 bouyer static int pci_dwiic_match(device_t, cfdata_t, void *);
197 1.1 bouyer static void pci_dwiic_attach(device_t, device_t, void *);
198 1.1 bouyer static bool dwiic_pci_power(struct dwiic_softc *, bool);
199 1.1 bouyer
200 1.1 bouyer CFATTACH_DECL_NEW(pcidwiic, sizeof(struct pci_dwiic_softc),
201 1.1 bouyer pci_dwiic_match, pci_dwiic_attach, dwiic_detach, NULL);
202 1.1 bouyer
203 1.1 bouyer
204 1.1 bouyer int
205 1.1 bouyer pci_dwiic_match(device_t parent, cfdata_t match, void *aux)
206 1.1 bouyer {
207 1.1 bouyer struct pci_attach_args *pa = aux;
208 1.1 bouyer
209 1.7 msaitoh return pci_compatible_match(pa, compat_data);
210 1.1 bouyer }
211 1.1 bouyer
212 1.1 bouyer void
213 1.1 bouyer pci_dwiic_attach(device_t parent, device_t self, void *aux)
214 1.1 bouyer {
215 1.1 bouyer struct pci_dwiic_softc *sc = device_private(self);
216 1.1 bouyer struct pci_attach_args *pa = aux;
217 1.1 bouyer const char *intrstr;
218 1.1 bouyer pci_intr_handle_t intrhandle;
219 1.1 bouyer char intrbuf[PCI_INTRSTR_LEN];
220 1.1 bouyer pcireg_t memtype;
221 1.1 bouyer pcireg_t csr;
222 1.1 bouyer uint32_t caps;
223 1.1 bouyer
224 1.1 bouyer sc->sc_dwiic.sc_dev = self;
225 1.1 bouyer sc->sc_dwiic.sc_power = dwiic_pci_power;
226 1.1 bouyer sc->sc_dwiic.sc_type = dwiic_type_sunrisepoint;
227 1.1 bouyer
228 1.1 bouyer sc->sc_pc = pa->pa_pc;
229 1.1 bouyer sc->sc_ptag = pa->pa_tag;
230 1.1 bouyer
231 1.1 bouyer /* register access not enabled by BIOS */
232 1.1 bouyer csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
233 1.1 bouyer pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
234 1.1 bouyer csr | PCI_COMMAND_MEM_ENABLE);
235 1.1 bouyer
236 1.1 bouyer memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, PCI_BAR0);
237 1.1 bouyer if (pci_mapreg_map(pa, PCI_BAR0, memtype, 0, &sc->sc_dwiic.sc_iot,
238 1.1 bouyer &sc->sc_dwiic.sc_ioh, NULL, NULL) != 0) {
239 1.1 bouyer aprint_error(": can't map register space\n");
240 1.1 bouyer goto out;
241 1.1 bouyer }
242 1.1 bouyer dwiic_pci_power(&sc->sc_dwiic, 1);
243 1.1 bouyer
244 1.1 bouyer caps = lpss_read(sc, LPSS_CAP);
245 1.1 bouyer
246 1.1 bouyer aprint_naive(": I2C controller\n");
247 1.1 bouyer aprint_normal(": I2C controller instance %d\n",
248 1.1 bouyer (int)(caps & LPSS_CAP_INSTANCE));
249 1.1 bouyer
250 1.1 bouyer if (pci_intr_map(pa, &intrhandle)) {
251 1.1 bouyer aprint_error_dev(self, "can't map interrupt\n");
252 1.1 bouyer goto out;
253 1.1 bouyer }
254 1.1 bouyer intrstr = pci_intr_string(pa->pa_pc, intrhandle,
255 1.1 bouyer intrbuf, sizeof(intrbuf));
256 1.1 bouyer
257 1.1 bouyer sc->sc_dwiic.sc_ih = pci_intr_establish(pa->pa_pc, intrhandle,
258 1.1 bouyer IPL_VM, dwiic_intr, sc);
259 1.1 bouyer if (sc->sc_dwiic.sc_ih == NULL) {
260 1.1 bouyer aprint_error_dev(self, "couldn't establish interrupt");
261 1.1 bouyer if (intrstr != NULL)
262 1.1 bouyer aprint_error(" at %s", intrstr);
263 1.1 bouyer aprint_error("\n");
264 1.1 bouyer goto out;
265 1.1 bouyer }
266 1.1 bouyer aprint_normal_dev(self, "interrupting at %s\n", intrstr);
267 1.1 bouyer
268 1.1 bouyer lpss_write(sc, LPSS_RESET, LPSS_RESET_CTRL_REL);
269 1.1 bouyer lpss_write(sc, LPSS_REMAP_LO,
270 1.1 bouyer pci_conf_read(sc->sc_pc, sc->sc_ptag, PCI_BAR0));
271 1.1 bouyer lpss_write(sc, LPSS_REMAP_HI,
272 1.1 bouyer pci_conf_read(sc->sc_pc, sc->sc_ptag, PCI_BAR0 + 0x4));
273 1.1 bouyer
274 1.9 riastrad if (!dwiic_attach(&sc->sc_dwiic))
275 1.9 riastrad goto out;
276 1.1 bouyer
277 1.12 thorpej iicbus_attach(self, &sc->sc_dwiic.sc_i2c_tag);
278 1.2 jakllsch
279 1.1 bouyer pmf_device_register(self, dwiic_suspend, dwiic_resume);
280 1.1 bouyer
281 1.1 bouyer out:
282 1.1 bouyer return;
283 1.1 bouyer }
284 1.1 bouyer
285 1.1 bouyer static bool
286 1.1 bouyer dwiic_pci_power(struct dwiic_softc *dwsc, bool power)
287 1.1 bouyer {
288 1.5 riastrad struct pci_dwiic_softc *sc = container_of(dwsc, struct pci_dwiic_softc,
289 1.5 riastrad sc_dwiic);
290 1.5 riastrad pcireg_t pmreg, csr;
291 1.5 riastrad uint32_t reset, rlo, rhi;
292 1.5 riastrad
293 1.5 riastrad csr = pci_conf_read(sc->sc_pc, sc->sc_ptag, PCI_COMMAND_STATUS_REG);
294 1.5 riastrad reset = lpss_read(sc, LPSS_RESET);
295 1.5 riastrad rlo = lpss_read(sc, LPSS_REMAP_LO);
296 1.5 riastrad rhi = lpss_read(sc, LPSS_REMAP_HI);
297 1.5 riastrad aprint_debug_dev(dwsc->sc_dev,
298 1.5 riastrad "status 0x%x reset 0x%x rlo 0x%x rhi 0x%x\n",
299 1.5 riastrad csr, reset, rlo, rhi);
300 1.1 bouyer
301 1.1 bouyer if (!power)
302 1.1 bouyer lpss_write(sc, LPSS_CLKGATE, LPSS_CLKGATE_CTRL_OFF);
303 1.1 bouyer if (pci_get_capability(sc->sc_pc, sc->sc_ptag, PCI_CAP_PWRMGMT,
304 1.1 bouyer &pmreg, NULL)) {
305 1.1 bouyer DPRINTF(("%s: power status 0x%x", device_xname(dwsc->sc_dev),
306 1.1 bouyer pci_conf_read(sc->sc_pc, sc->sc_ptag, pmreg + PCI_PMCSR)));
307 1.1 bouyer pci_conf_write(sc->sc_pc, sc->sc_ptag, pmreg + PCI_PMCSR,
308 1.1 bouyer power ? PCI_PMCSR_STATE_D0 : PCI_PMCSR_STATE_D3);
309 1.1 bouyer DELAY(10000); /* 10 milliseconds */
310 1.5 riastrad DPRINTF((" -> 0x%x\n",
311 1.1 bouyer pci_conf_read(sc->sc_pc, sc->sc_ptag, pmreg + PCI_PMCSR)));
312 1.1 bouyer }
313 1.1 bouyer if (power) {
314 1.1 bouyer lpss_write(sc, LPSS_CLKGATE, LPSS_CLKGATE_CTRL_ON);
315 1.1 bouyer }
316 1.1 bouyer return true;
317 1.1 bouyer }
318