xref: /netbsd-src/sys/arch/x86/pci/dwiic_pci.c (revision 5c40ffaf93547964ada04ba7799a489d1784b267)
1 /* $NetBSD: dwiic_pci.c,v 1.11 2024/11/11 17:28:38 martin 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.11 2024/11/11 17:28:38 martin 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 	struct acpi_devnode	*sc_acpinode;
72 };
73 
74 #define VIDDID(a, b) PCI_ID_CODE(PCI_VENDOR_ ## a, PCI_PRODUCT_ ## a ## _ ## b)
75 
76 static const struct device_compatible_entry compat_data[] = {
77 	{ .id = VIDDID(INTEL, CORE4G_M_S_I2C_0) },
78 	{ .id = VIDDID(INTEL, CORE4G_M_S_I2C_1) },
79 	{ .id = VIDDID(INTEL, 100SERIES_I2C_0) },
80 	{ .id = VIDDID(INTEL, 100SERIES_I2C_1) },
81 	{ .id = VIDDID(INTEL, 100SERIES_I2C_2) },
82 	{ .id = VIDDID(INTEL, 100SERIES_I2C_3) },
83 	{ .id = VIDDID(INTEL, 100SERIES_LP_I2C_0) },
84 	{ .id = VIDDID(INTEL, 100SERIES_LP_I2C_1) },
85 	{ .id = VIDDID(INTEL, 100SERIES_LP_I2C_2) },
86 	{ .id = VIDDID(INTEL, 100SERIES_LP_I2C_3) },
87 	{ .id = VIDDID(INTEL, 100SERIES_LP_I2C_4) },
88 	{ .id = VIDDID(INTEL, 100SERIES_LP_I2C_5) },
89 	{ .id = VIDDID(INTEL, 2HS_I2C_0) },
90 	{ .id = VIDDID(INTEL, 2HS_I2C_1) },
91 	{ .id = VIDDID(INTEL, 2HS_I2C_2) },
92 	{ .id = VIDDID(INTEL, 2HS_I2C_3) },
93 	{ .id = VIDDID(INTEL, 3HS_I2C_0) },
94 	{ .id = VIDDID(INTEL, 3HS_I2C_1) },
95 	{ .id = VIDDID(INTEL, 3HS_I2C_2) },
96 	{ .id = VIDDID(INTEL, 3HS_I2C_3) },
97 	{ .id = VIDDID(INTEL, 3HS_U_I2C_0) },
98 	{ .id = VIDDID(INTEL, 3HS_U_I2C_1) },
99 	{ .id = VIDDID(INTEL, 3HS_U_I2C_2) },
100 	{ .id = VIDDID(INTEL, 3HS_U_I2C_3) },
101 	{ .id = VIDDID(INTEL, 3HS_U_I2C_4) },
102 	{ .id = VIDDID(INTEL, 3HS_U_I2C_5) },
103 	{ .id = VIDDID(INTEL, 4HS_H_I2C_0) },
104 	{ .id = VIDDID(INTEL, 4HS_H_I2C_1) },
105 	{ .id = VIDDID(INTEL, 4HS_H_I2C_2) },
106 	{ .id = VIDDID(INTEL, 4HS_H_I2C_3) },
107 	{ .id = VIDDID(INTEL, 4HS_V_I2C_0) },
108 	{ .id = VIDDID(INTEL, 4HS_V_I2C_1) },
109 	{ .id = VIDDID(INTEL, 4HS_V_I2C_2) },
110 	{ .id = VIDDID(INTEL, 4HS_V_I2C_3) },
111 	{ .id = VIDDID(INTEL, CMTLK_I2C_0) }, /* 4HS LP */
112 	{ .id = VIDDID(INTEL, CMTLK_I2C_1) },
113 	{ .id = VIDDID(INTEL, CMTLK_I2C_2) },
114 	{ .id = VIDDID(INTEL, CMTLK_I2C_3) },
115 	{ .id = VIDDID(INTEL, CMTLK_I2C_4) },
116 	{ .id = VIDDID(INTEL, CMTLK_I2C_5) },
117 	{ .id = VIDDID(INTEL, 495_YU_I2C_0) },
118 	{ .id = VIDDID(INTEL, 495_YU_I2C_1) },
119 	{ .id = VIDDID(INTEL, 495_YU_I2C_2) },
120 	{ .id = VIDDID(INTEL, 495_YU_I2C_3) },
121 	{ .id = VIDDID(INTEL, 495_YU_I2C_4) },
122 	{ .id = VIDDID(INTEL, 495_YU_I2C_5) },
123 	{ .id = VIDDID(INTEL, 5HS_H_I2C_0) },
124 	{ .id = VIDDID(INTEL, 5HS_H_I2C_1) },
125 	{ .id = VIDDID(INTEL, 5HS_H_I2C_2) },
126 	{ .id = VIDDID(INTEL, 5HS_H_I2C_3) },
127 	{ .id = VIDDID(INTEL, 5HS_H_I2C_4) },
128 	{ .id = VIDDID(INTEL, 5HS_H_I2C_5) },
129 	{ .id = VIDDID(INTEL, 5HS_H_I2C_6) },
130 	{ .id = VIDDID(INTEL, 5HS_LP_I2C_0) },
131 	{ .id = VIDDID(INTEL, 5HS_LP_I2C_1) },
132 	{ .id = VIDDID(INTEL, 5HS_LP_I2C_2) },
133 	{ .id = VIDDID(INTEL, 5HS_LP_I2C_3) },
134 	{ .id = VIDDID(INTEL, 5HS_LP_I2C_4) },
135 	{ .id = VIDDID(INTEL, 5HS_LP_I2C_5) },
136 	{ .id = VIDDID(INTEL, BAYTRAIL_SIO_I2C1) },
137 	{ .id = VIDDID(INTEL, BAYTRAIL_SIO_I2C2) },
138 	{ .id = VIDDID(INTEL, BAYTRAIL_SIO_I2C3) },
139 	{ .id = VIDDID(INTEL, BAYTRAIL_SIO_I2C4) },
140 	{ .id = VIDDID(INTEL, BAYTRAIL_SIO_I2C5) },
141 	{ .id = VIDDID(INTEL, BAYTRAIL_SIO_I2C6) },
142 	{ .id = VIDDID(INTEL, BAYTRAIL_SIO_I2C7) },
143 	{ .id = VIDDID(INTEL, BSW_SIO_I2C_1) },
144 	{ .id = VIDDID(INTEL, BSW_SIO_I2C_2) },
145 	{ .id = VIDDID(INTEL, BSW_SIO_I2C_3) },
146 	{ .id = VIDDID(INTEL, BSW_SIO_I2C_4) },
147 	{ .id = VIDDID(INTEL, BSW_SIO_I2C_5) },
148 	{ .id = VIDDID(INTEL, BSW_SIO_I2C_6) },
149 	{ .id = VIDDID(INTEL, BSW_SIO_I2C_7) },
150 	{ .id = VIDDID(INTEL, APL_I2C_0) },
151 	{ .id = VIDDID(INTEL, APL_I2C_1) },
152 	{ .id = VIDDID(INTEL, APL_I2C_2) },
153 	{ .id = VIDDID(INTEL, APL_I2C_3) },
154 	{ .id = VIDDID(INTEL, APL_I2C_4) },
155 	{ .id = VIDDID(INTEL, APL_I2C_5) },
156 	{ .id = VIDDID(INTEL, APL_I2C_6) },
157 	{ .id = VIDDID(INTEL, APL_I2C_7) },
158 	{ .id = VIDDID(INTEL, GLK_I2C_0) },
159 	{ .id = VIDDID(INTEL, GLK_I2C_1) },
160 	{ .id = VIDDID(INTEL, GLK_I2C_2) },
161 	{ .id = VIDDID(INTEL, GLK_I2C_3) },
162 	{ .id = VIDDID(INTEL, GLK_I2C_4) },
163 	{ .id = VIDDID(INTEL, GLK_I2C_5) },
164 	{ .id = VIDDID(INTEL, GLK_I2C_6) },
165 	{ .id = VIDDID(INTEL, GLK_I2C_7) },
166 	{ .id = VIDDID(INTEL, EHL_SIO_I2C_0) },
167 	{ .id = VIDDID(INTEL, EHL_SIO_I2C_1) },
168 	{ .id = VIDDID(INTEL, EHL_SIO_I2C_2) },
169 	{ .id = VIDDID(INTEL, EHL_SIO_I2C_3) },
170 	{ .id = VIDDID(INTEL, EHL_SIO_I2C_4) },
171 	{ .id = VIDDID(INTEL, EHL_SIO_I2C_5) },
172 	{ .id = VIDDID(INTEL, EHL_SIO_I2C_6) },
173 	{ .id = VIDDID(INTEL, EHL_SIO_I2C_7) },
174 	{ .id = VIDDID(INTEL, JSL_LPSS_I2C_0) },
175 	{ .id = VIDDID(INTEL, JSL_LPSS_I2C_1) },
176 	{ .id = VIDDID(INTEL, JSL_LPSS_I2C_2) },
177 	{ .id = VIDDID(INTEL, JSL_LPSS_I2C_3) },
178 	{ .id = VIDDID(INTEL, JSL_LPSS_I2C_4) },
179 	{ .id = VIDDID(INTEL, JSL_LPSS_I2C_5) },
180 
181 	PCI_COMPAT_EOL
182 };
183 
184 static uint32_t
185 lpss_read(struct pci_dwiic_softc *sc, int offset)
186 {
187 	return bus_space_read_4(sc->sc_dwiic.sc_iot, sc->sc_dwiic.sc_ioh,
188 	    offset);
189 }
190 
191 static void
192 lpss_write(struct pci_dwiic_softc *sc, int offset, uint32_t val)
193 {
194 	bus_space_write_4(sc->sc_dwiic.sc_iot, sc->sc_dwiic.sc_ioh,
195 	    offset, val);
196 }
197 
198 static int	pci_dwiic_match(device_t, cfdata_t, void *);
199 static void	pci_dwiic_attach(device_t, device_t, void *);
200 static bool	dwiic_pci_power(struct dwiic_softc *, bool);
201 
202 CFATTACH_DECL_NEW(pcidwiic, sizeof(struct pci_dwiic_softc),
203     pci_dwiic_match, pci_dwiic_attach, dwiic_detach, NULL);
204 
205 
206 int
207 pci_dwiic_match(device_t parent, cfdata_t match, void *aux)
208 {
209 	struct pci_attach_args *pa = aux;
210 
211 	return pci_compatible_match(pa, compat_data);
212 }
213 
214 void
215 pci_dwiic_attach(device_t parent, device_t self, void *aux)
216 {
217 	struct pci_dwiic_softc *sc = device_private(self);
218 	struct pci_attach_args *pa = aux;
219 	const char *intrstr;
220 	pci_intr_handle_t intrhandle;
221 	char intrbuf[PCI_INTRSTR_LEN];
222 	pcireg_t memtype;
223 	pcireg_t csr;
224 	uint32_t caps;
225 
226 	sc->sc_dwiic.sc_dev = self;
227 	sc->sc_dwiic.sc_power = dwiic_pci_power;
228 	sc->sc_dwiic.sc_type = dwiic_type_sunrisepoint;
229 
230 	sc->sc_pc = pa->pa_pc;
231 	sc->sc_ptag = pa->pa_tag;
232 
233 	/* register access not enabled by BIOS */
234 	csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
235 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
236 	    csr | PCI_COMMAND_MEM_ENABLE);
237 
238 	memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, PCI_BAR0);
239 	if (pci_mapreg_map(pa, PCI_BAR0, memtype, 0, &sc->sc_dwiic.sc_iot,
240 	    &sc->sc_dwiic.sc_ioh, NULL, NULL) != 0) {
241 		aprint_error(": can't map register space\n");
242 		goto out;
243 	}
244 	dwiic_pci_power(&sc->sc_dwiic, 1);
245 
246 	caps = lpss_read(sc, LPSS_CAP);
247 
248 	aprint_naive(": I2C controller\n");
249 	aprint_normal(": I2C controller instance %d\n",
250 	    (int)(caps & LPSS_CAP_INSTANCE));
251 
252 	if (pci_intr_map(pa, &intrhandle)) {
253 		aprint_error_dev(self, "can't map interrupt\n");
254 		goto out;
255 	}
256 	intrstr = pci_intr_string(pa->pa_pc, intrhandle,
257 	    intrbuf, sizeof(intrbuf));
258 
259 	sc->sc_dwiic.sc_ih = pci_intr_establish(pa->pa_pc, intrhandle,
260 	    IPL_VM, dwiic_intr, sc);
261 	if (sc->sc_dwiic.sc_ih == NULL) {
262 		aprint_error_dev(self, "couldn't establish interrupt");
263 		if (intrstr != NULL)
264 			aprint_error(" at %s", intrstr);
265 		aprint_error("\n");
266 		goto out;
267 	}
268 	aprint_normal_dev(self, "interrupting at %s\n", intrstr);
269 
270 	lpss_write(sc, LPSS_RESET, LPSS_RESET_CTRL_REL);
271 	lpss_write(sc, LPSS_REMAP_LO,
272 	    pci_conf_read(sc->sc_pc, sc->sc_ptag, PCI_BAR0));
273 	lpss_write(sc, LPSS_REMAP_HI,
274 	    pci_conf_read(sc->sc_pc, sc->sc_ptag, PCI_BAR0 + 0x4));
275 
276 #ifdef I2C_USE_ACPI
277 	sc->sc_acpinode = acpi_pcidev_find(0 /*XXX segment*/,
278 	    pa->pa_bus, pa->pa_device, pa->pa_function);
279 #else
280 	sc->sc_acpinode = NULL;
281 #endif
282 
283 	if (sc->sc_acpinode) {
284 #ifdef I2C_USE_ACPI
285 		sc->sc_dwiic.sc_iba.iba_child_devices =
286 		    acpi_enter_i2c_devs(NULL, sc->sc_acpinode);
287 #endif
288 	} else {
289 		aprint_verbose_dev(self, "no matching ACPI node\n");
290 	}
291 
292 	if (!dwiic_attach(&sc->sc_dwiic))
293 		goto out;
294 
295 	config_found(self, &sc->sc_dwiic.sc_iba, iicbus_print, CFARGS_NONE);
296 
297 	pmf_device_register(self, dwiic_suspend, dwiic_resume);
298 
299 out:
300 	return;
301 }
302 
303 static bool
304 dwiic_pci_power(struct dwiic_softc *dwsc, bool power)
305 {
306 	struct pci_dwiic_softc *sc = container_of(dwsc, struct pci_dwiic_softc,
307 	    sc_dwiic);
308 	pcireg_t pmreg, csr;
309 	uint32_t reset, rlo, rhi;
310 
311 	csr = pci_conf_read(sc->sc_pc, sc->sc_ptag, PCI_COMMAND_STATUS_REG);
312 	reset = lpss_read(sc, LPSS_RESET);
313 	rlo = lpss_read(sc, LPSS_REMAP_LO);
314 	rhi = lpss_read(sc, LPSS_REMAP_HI);
315 	aprint_debug_dev(dwsc->sc_dev,
316 	    "status 0x%x reset 0x%x rlo 0x%x rhi 0x%x\n",
317 	    csr, reset, rlo, rhi);
318 
319 	if (!power)
320 		lpss_write(sc, LPSS_CLKGATE, LPSS_CLKGATE_CTRL_OFF);
321 	if (pci_get_capability(sc->sc_pc, sc->sc_ptag, PCI_CAP_PWRMGMT,
322 	    &pmreg, NULL)) {
323 		DPRINTF(("%s: power status 0x%x", device_xname(dwsc->sc_dev),
324 		    pci_conf_read(sc->sc_pc, sc->sc_ptag, pmreg + PCI_PMCSR)));
325 		pci_conf_write(sc->sc_pc, sc->sc_ptag, pmreg + PCI_PMCSR,
326 		    power ? PCI_PMCSR_STATE_D0 : PCI_PMCSR_STATE_D3);
327 		DELAY(10000); /* 10 milliseconds */
328 		DPRINTF((" -> 0x%x\n",
329 		    pci_conf_read(sc->sc_pc, sc->sc_ptag, pmreg + PCI_PMCSR)));
330 	}
331 	if (power) {
332 		lpss_write(sc, LPSS_CLKGATE, LPSS_CLKGATE_CTRL_ON);
333 	}
334 	return true;
335 }
336