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