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