dwiic_pci.c revision 1.2 1 1.2 jakllsch /* $NetBSD: dwiic_pci.c,v 1.2 2018/09/26 19:06:33 jakllsch 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.2 jakllsch __KERNEL_RCSID(0, "$NetBSD: dwiic_pci.c,v 1.2 2018/09/26 19:06:33 jakllsch 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 #include <dev/acpi/acpi_i2c.h>
49 1.1 bouyer
50 1.1 bouyer #include <dev/ic/dwiic_var.h>
51 1.1 bouyer #include <arch/x86/pci/lpssreg.h>
52 1.1 bouyer
53 1.1 bouyer //#define DWIIC_DEBUG
54 1.1 bouyer
55 1.1 bouyer #ifdef DWIIC_DEBUG
56 1.1 bouyer #define DPRINTF(x) printf x
57 1.1 bouyer #else
58 1.1 bouyer #define DPRINTF(x)
59 1.1 bouyer #endif
60 1.1 bouyer
61 1.1 bouyer struct pci_dwiic_softc {
62 1.1 bouyer struct dwiic_softc sc_dwiic;
63 1.1 bouyer pci_chipset_tag_t sc_pc;
64 1.1 bouyer pcitag_t sc_ptag;
65 1.1 bouyer struct acpi_devnode *sc_acpinode;
66 1.1 bouyer };
67 1.1 bouyer
68 1.1 bouyer static uint32_t
69 1.1 bouyer lpss_read(struct pci_dwiic_softc *sc, int offset)
70 1.1 bouyer {
71 1.1 bouyer u_int32_t b = bus_space_read_4(sc->sc_dwiic.sc_iot, sc->sc_dwiic.sc_ioh,
72 1.1 bouyer offset);
73 1.1 bouyer return b;
74 1.1 bouyer }
75 1.1 bouyer
76 1.1 bouyer static void
77 1.1 bouyer lpss_write(struct pci_dwiic_softc *sc, int offset, uint32_t val)
78 1.1 bouyer {
79 1.1 bouyer bus_space_write_4(sc->sc_dwiic.sc_iot, sc->sc_dwiic.sc_ioh,
80 1.1 bouyer offset, val);
81 1.1 bouyer }
82 1.1 bouyer
83 1.1 bouyer static int pci_dwiic_match(device_t, cfdata_t, void *);
84 1.1 bouyer static void pci_dwiic_attach(device_t, device_t, void *);
85 1.1 bouyer static bool dwiic_pci_power(struct dwiic_softc *, bool);
86 1.1 bouyer
87 1.1 bouyer CFATTACH_DECL_NEW(pcidwiic, sizeof(struct pci_dwiic_softc),
88 1.1 bouyer pci_dwiic_match, pci_dwiic_attach, dwiic_detach, NULL);
89 1.1 bouyer
90 1.1 bouyer
91 1.1 bouyer int
92 1.1 bouyer pci_dwiic_match(device_t parent, cfdata_t match, void *aux)
93 1.1 bouyer {
94 1.1 bouyer struct pci_attach_args *pa = aux;
95 1.1 bouyer
96 1.1 bouyer if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_INTEL)
97 1.1 bouyer return 0;
98 1.1 bouyer
99 1.1 bouyer if (PCI_PRODUCT(pa->pa_id) < PCI_PRODUCT_INTEL_100SERIES_LP_I2C_0 ||
100 1.1 bouyer PCI_PRODUCT(pa->pa_id) > PCI_PRODUCT_INTEL_100SERIES_LP_I2C_3)
101 1.1 bouyer return 0;
102 1.1 bouyer
103 1.1 bouyer return 1;
104 1.1 bouyer }
105 1.1 bouyer
106 1.1 bouyer void
107 1.1 bouyer pci_dwiic_attach(device_t parent, device_t self, void *aux)
108 1.1 bouyer {
109 1.1 bouyer struct pci_dwiic_softc *sc = device_private(self);
110 1.1 bouyer struct pci_attach_args *pa = aux;
111 1.1 bouyer const char *intrstr;
112 1.1 bouyer pci_intr_handle_t intrhandle;
113 1.1 bouyer char intrbuf[PCI_INTRSTR_LEN];
114 1.1 bouyer pcireg_t memtype;
115 1.1 bouyer pcireg_t csr;
116 1.1 bouyer uint32_t caps;
117 1.1 bouyer
118 1.1 bouyer sc->sc_dwiic.sc_dev = self;
119 1.1 bouyer sc->sc_dwiic.sc_power = dwiic_pci_power;
120 1.1 bouyer sc->sc_dwiic.sc_type = dwiic_type_sunrisepoint;
121 1.1 bouyer
122 1.1 bouyer sc->sc_pc = pa->pa_pc;
123 1.1 bouyer sc->sc_ptag = pa->pa_tag;
124 1.1 bouyer
125 1.1 bouyer /* register access not enabled by BIOS */
126 1.1 bouyer csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
127 1.1 bouyer pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
128 1.1 bouyer csr | PCI_COMMAND_MEM_ENABLE);
129 1.1 bouyer
130 1.1 bouyer memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, PCI_BAR0);
131 1.1 bouyer if (pci_mapreg_map(pa, PCI_BAR0, memtype, 0, &sc->sc_dwiic.sc_iot,
132 1.1 bouyer &sc->sc_dwiic.sc_ioh, NULL, NULL) != 0) {
133 1.1 bouyer aprint_error(": can't map register space\n");
134 1.1 bouyer goto out;
135 1.1 bouyer }
136 1.1 bouyer dwiic_pci_power(&sc->sc_dwiic, 1);
137 1.1 bouyer
138 1.1 bouyer caps = lpss_read(sc, LPSS_CAP);
139 1.1 bouyer
140 1.1 bouyer aprint_naive(": I2C controller\n");
141 1.1 bouyer aprint_normal(": I2C controller instance %d\n",
142 1.1 bouyer (int)(caps & LPSS_CAP_INSTANCE));
143 1.1 bouyer
144 1.1 bouyer if (pci_intr_map(pa, &intrhandle)) {
145 1.1 bouyer aprint_error_dev(self, "can't map interrupt\n");
146 1.1 bouyer goto out;
147 1.1 bouyer }
148 1.1 bouyer intrstr = pci_intr_string(pa->pa_pc, intrhandle,
149 1.1 bouyer intrbuf, sizeof(intrbuf));
150 1.1 bouyer
151 1.1 bouyer sc->sc_dwiic.sc_ih = pci_intr_establish(pa->pa_pc, intrhandle,
152 1.1 bouyer IPL_VM, dwiic_intr, sc);
153 1.1 bouyer if (sc->sc_dwiic.sc_ih == NULL) {
154 1.1 bouyer aprint_error_dev(self, "couldn't establish interrupt");
155 1.1 bouyer if (intrstr != NULL)
156 1.1 bouyer aprint_error(" at %s", intrstr);
157 1.1 bouyer aprint_error("\n");
158 1.1 bouyer goto out;
159 1.1 bouyer }
160 1.1 bouyer aprint_normal_dev(self, "interrupting at %s\n", intrstr);
161 1.1 bouyer
162 1.1 bouyer lpss_write(sc, LPSS_RESET, LPSS_RESET_CTRL_REL);
163 1.1 bouyer lpss_write(sc, LPSS_REMAP_LO,
164 1.1 bouyer pci_conf_read(sc->sc_pc, sc->sc_ptag, PCI_BAR0));
165 1.1 bouyer lpss_write(sc, LPSS_REMAP_HI,
166 1.1 bouyer pci_conf_read(sc->sc_pc, sc->sc_ptag, PCI_BAR0 + 0x4));
167 1.1 bouyer
168 1.1 bouyer sc->sc_acpinode = acpi_pcidev_find(0 /*XXX segment*/,
169 1.1 bouyer pa->pa_bus, pa->pa_device, pa->pa_function);
170 1.1 bouyer
171 1.1 bouyer if (sc->sc_acpinode) {
172 1.1 bouyer sc->sc_dwiic.sc_iba.iba_child_devices =
173 1.1 bouyer acpi_enter_i2c_devs(sc->sc_acpinode);
174 1.1 bouyer } else {
175 1.1 bouyer aprint_verbose_dev(self, "no matching ACPI node\n");
176 1.1 bouyer }
177 1.1 bouyer
178 1.1 bouyer dwiic_attach(&sc->sc_dwiic);
179 1.1 bouyer
180 1.2 jakllsch config_found_ia(self, "i2cbus", &sc->sc_dwiic.sc_iba, iicbus_print);
181 1.2 jakllsch
182 1.1 bouyer pmf_device_register(self, dwiic_suspend, dwiic_resume);
183 1.1 bouyer
184 1.1 bouyer out:
185 1.1 bouyer return;
186 1.1 bouyer }
187 1.1 bouyer
188 1.1 bouyer static bool
189 1.1 bouyer dwiic_pci_power(struct dwiic_softc *dwsc, bool power)
190 1.1 bouyer {
191 1.1 bouyer struct pci_dwiic_softc *sc = (void *)dwsc;
192 1.1 bouyer pcireg_t pmreg;
193 1.1 bouyer
194 1.1 bouyer printf("status 0x%x\n", pci_conf_read(sc->sc_pc, sc->sc_ptag, PCI_COMMAND_STATUS_REG));
195 1.1 bouyer printf("reset 0x%x\n", lpss_read(sc, LPSS_RESET));
196 1.1 bouyer printf("rlo 0x%x\n", lpss_read(sc, LPSS_REMAP_LO));
197 1.1 bouyer printf("rho 0x%x\n", lpss_read(sc, LPSS_REMAP_HI));
198 1.1 bouyer
199 1.1 bouyer if (!power)
200 1.1 bouyer lpss_write(sc, LPSS_CLKGATE, LPSS_CLKGATE_CTRL_OFF);
201 1.1 bouyer if (pci_get_capability(sc->sc_pc, sc->sc_ptag, PCI_CAP_PWRMGMT,
202 1.1 bouyer &pmreg, NULL)) {
203 1.1 bouyer DPRINTF(("%s: power status 0x%x", device_xname(dwsc->sc_dev),
204 1.1 bouyer pci_conf_read(sc->sc_pc, sc->sc_ptag, pmreg + PCI_PMCSR)));
205 1.1 bouyer pci_conf_write(sc->sc_pc, sc->sc_ptag, pmreg + PCI_PMCSR,
206 1.1 bouyer power ? PCI_PMCSR_STATE_D0 : PCI_PMCSR_STATE_D3);
207 1.1 bouyer DELAY(10000); /* 10 milliseconds */
208 1.1 bouyer DPRINTF((" -> 0x%x\n",
209 1.1 bouyer pci_conf_read(sc->sc_pc, sc->sc_ptag, pmreg + PCI_PMCSR)));
210 1.1 bouyer }
211 1.1 bouyer if (power) {
212 1.1 bouyer lpss_write(sc, LPSS_CLKGATE, LPSS_CLKGATE_CTRL_ON);
213 1.1 bouyer }
214 1.1 bouyer return true;
215 1.1 bouyer }
216