171d51719SMichael Gmelin /* 271d51719SMichael Gmelin * Copyright (c) 2014 The DragonFly Project. All rights reserved. 371d51719SMichael Gmelin * 471d51719SMichael Gmelin * This code is derived from software contributed to The DragonFly Project 571d51719SMichael Gmelin * by Matthew Dillon <dillon@backplane.com> and was subsequently ported 671d51719SMichael Gmelin * to FreeBSD by Michael Gmelin <freebsd@grem.de> 771d51719SMichael Gmelin * 871d51719SMichael Gmelin * Redistribution and use in source and binary forms, with or without 971d51719SMichael Gmelin * modification, are permitted provided that the following conditions 1071d51719SMichael Gmelin * are met: 1171d51719SMichael Gmelin * 1271d51719SMichael Gmelin * 1. Redistributions of source code must retain the above copyright 1371d51719SMichael Gmelin * notice, this list of conditions and the following disclaimer. 1471d51719SMichael Gmelin * 2. Redistributions in binary form must reproduce the above copyright 1571d51719SMichael Gmelin * notice, this list of conditions and the following disclaimer in 1671d51719SMichael Gmelin * the documentation and/or other materials provided with the 1771d51719SMichael Gmelin * distribution. 1871d51719SMichael Gmelin * 3. Neither the name of The DragonFly Project nor the names of its 1971d51719SMichael Gmelin * contributors may be used to endorse or promote products derived 2071d51719SMichael Gmelin * from this software without specific, prior written permission. 2171d51719SMichael Gmelin * 2271d51719SMichael Gmelin * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 2371d51719SMichael Gmelin * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 2471d51719SMichael Gmelin * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 2571d51719SMichael Gmelin * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 2671d51719SMichael Gmelin * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 2771d51719SMichael Gmelin * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 2871d51719SMichael Gmelin * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 2971d51719SMichael Gmelin * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 3071d51719SMichael Gmelin * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 3171d51719SMichael Gmelin * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 3271d51719SMichael Gmelin * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3371d51719SMichael Gmelin * SUCH DAMAGE. 3471d51719SMichael Gmelin */ 3571d51719SMichael Gmelin 3671d51719SMichael Gmelin #include <sys/cdefs.h> 3771d51719SMichael Gmelin __FBSDID("$FreeBSD$"); 3871d51719SMichael Gmelin 3971d51719SMichael Gmelin /* 40e3d25549SAndriy Gapon * Intel fourth generation mobile cpus integrated I2C device. 4171d51719SMichael Gmelin * 4271d51719SMichael Gmelin * See ig4_reg.h for datasheet reference and notes. 4371d51719SMichael Gmelin */ 4471d51719SMichael Gmelin 4571d51719SMichael Gmelin #include <sys/param.h> 4671d51719SMichael Gmelin #include <sys/systm.h> 4771d51719SMichael Gmelin #include <sys/kernel.h> 4871d51719SMichael Gmelin #include <sys/module.h> 4971d51719SMichael Gmelin #include <sys/errno.h> 5071d51719SMichael Gmelin #include <sys/lock.h> 5171d51719SMichael Gmelin #include <sys/mutex.h> 524cd6abddSMichael Gmelin #include <sys/sx.h> 5371d51719SMichael Gmelin #include <sys/syslog.h> 5471d51719SMichael Gmelin #include <sys/bus.h> 5571d51719SMichael Gmelin 5671d51719SMichael Gmelin #include <machine/bus.h> 5771d51719SMichael Gmelin #include <sys/rman.h> 5871d51719SMichael Gmelin #include <machine/resource.h> 5971d51719SMichael Gmelin 6071d51719SMichael Gmelin #include <dev/pci/pcivar.h> 6171d51719SMichael Gmelin #include <dev/pci/pcireg.h> 62448897d3SAndriy Gapon #include <dev/iicbus/iiconf.h> 6371d51719SMichael Gmelin 6471d51719SMichael Gmelin #include <dev/ichiic/ig4_reg.h> 6571d51719SMichael Gmelin #include <dev/ichiic/ig4_var.h> 6671d51719SMichael Gmelin 6771d51719SMichael Gmelin static int ig4iic_pci_detach(device_t dev); 6871d51719SMichael Gmelin 6971d51719SMichael Gmelin #define PCI_CHIP_LYNXPT_LP_I2C_1 0x9c618086 7071d51719SMichael Gmelin #define PCI_CHIP_LYNXPT_LP_I2C_2 0x9c628086 71*00eb880dSConrad Meyer #define PCI_CHIP_BRASWELL_I2C_1 0x22c18086 72*00eb880dSConrad Meyer #define PCI_CHIP_BRASWELL_I2C_2 0x22c28086 73*00eb880dSConrad Meyer #define PCI_CHIP_BRASWELL_I2C_3 0x22c38086 74*00eb880dSConrad Meyer #define PCI_CHIP_BRASWELL_I2C_5 0x22c58086 75*00eb880dSConrad Meyer #define PCI_CHIP_BRASWELL_I2C_6 0x22c68086 76*00eb880dSConrad Meyer #define PCI_CHIP_BRASWELL_I2C_7 0x22c78086 7771d51719SMichael Gmelin 7871d51719SMichael Gmelin static int 7971d51719SMichael Gmelin ig4iic_pci_probe(device_t dev) 8071d51719SMichael Gmelin { 8171d51719SMichael Gmelin switch(pci_get_devid(dev)) { 8271d51719SMichael Gmelin case PCI_CHIP_LYNXPT_LP_I2C_1: 8371d51719SMichael Gmelin device_set_desc(dev, "Intel Lynx Point-LP I2C Controller-1"); 8471d51719SMichael Gmelin break; 8571d51719SMichael Gmelin case PCI_CHIP_LYNXPT_LP_I2C_2: 8671d51719SMichael Gmelin device_set_desc(dev, "Intel Lynx Point-LP I2C Controller-2"); 8771d51719SMichael Gmelin break; 88*00eb880dSConrad Meyer case PCI_CHIP_BRASWELL_I2C_1: 89*00eb880dSConrad Meyer device_set_desc(dev, "Intel Braswell Serial I/O I2C Port 1"); 90*00eb880dSConrad Meyer break; 91*00eb880dSConrad Meyer case PCI_CHIP_BRASWELL_I2C_2: 92*00eb880dSConrad Meyer device_set_desc(dev, "Intel Braswell Serial I/O I2C Port 2"); 93*00eb880dSConrad Meyer break; 94*00eb880dSConrad Meyer case PCI_CHIP_BRASWELL_I2C_3: 95*00eb880dSConrad Meyer device_set_desc(dev, "Intel Braswell Serial I/O I2C Port 3"); 96*00eb880dSConrad Meyer break; 97*00eb880dSConrad Meyer case PCI_CHIP_BRASWELL_I2C_5: 98*00eb880dSConrad Meyer device_set_desc(dev, "Intel Braswell Serial I/O I2C Port 5"); 99*00eb880dSConrad Meyer break; 100*00eb880dSConrad Meyer case PCI_CHIP_BRASWELL_I2C_6: 101*00eb880dSConrad Meyer device_set_desc(dev, "Intel Braswell Serial I/O I2C Port 6"); 102*00eb880dSConrad Meyer break; 103*00eb880dSConrad Meyer case PCI_CHIP_BRASWELL_I2C_7: 104*00eb880dSConrad Meyer device_set_desc(dev, "Intel Braswell Serial I/O I2C Port 7"); 105*00eb880dSConrad Meyer break; 10671d51719SMichael Gmelin default: 10771d51719SMichael Gmelin return (ENXIO); 10871d51719SMichael Gmelin } 10971d51719SMichael Gmelin return (BUS_PROBE_DEFAULT); 11071d51719SMichael Gmelin } 11171d51719SMichael Gmelin 11271d51719SMichael Gmelin static int 11371d51719SMichael Gmelin ig4iic_pci_attach(device_t dev) 11471d51719SMichael Gmelin { 11571d51719SMichael Gmelin ig4iic_softc_t *sc = device_get_softc(dev); 11671d51719SMichael Gmelin int error; 11771d51719SMichael Gmelin 11871d51719SMichael Gmelin bzero(sc, sizeof(*sc)); 11971d51719SMichael Gmelin 1204cd6abddSMichael Gmelin mtx_init(&sc->io_lock, "IG4 I/O lock", NULL, MTX_DEF); 1214cd6abddSMichael Gmelin sx_init(&sc->call_lock, "IG4 call lock"); 12271d51719SMichael Gmelin 12371d51719SMichael Gmelin sc->dev = dev; 12471d51719SMichael Gmelin sc->regs_rid = PCIR_BAR(0); 12571d51719SMichael Gmelin sc->regs_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 12671d51719SMichael Gmelin &sc->regs_rid, RF_ACTIVE); 12771d51719SMichael Gmelin if (sc->regs_res == NULL) { 12871d51719SMichael Gmelin device_printf(dev, "unable to map registers\n"); 12971d51719SMichael Gmelin ig4iic_pci_detach(dev); 13071d51719SMichael Gmelin return (ENXIO); 13171d51719SMichael Gmelin } 13271d51719SMichael Gmelin sc->intr_rid = 0; 13371d51719SMichael Gmelin if (pci_alloc_msi(dev, &sc->intr_rid)) { 13471d51719SMichael Gmelin device_printf(dev, "Using MSI\n"); 13571d51719SMichael Gmelin } 13671d51719SMichael Gmelin sc->intr_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, 13771d51719SMichael Gmelin &sc->intr_rid, RF_SHAREABLE | RF_ACTIVE); 13871d51719SMichael Gmelin if (sc->intr_res == NULL) { 13971d51719SMichael Gmelin device_printf(dev, "unable to map interrupt\n"); 14071d51719SMichael Gmelin ig4iic_pci_detach(dev); 14171d51719SMichael Gmelin return (ENXIO); 14271d51719SMichael Gmelin } 14371d51719SMichael Gmelin sc->pci_attached = 1; 14471d51719SMichael Gmelin 14571d51719SMichael Gmelin error = ig4iic_attach(sc); 14671d51719SMichael Gmelin if (error) 14771d51719SMichael Gmelin ig4iic_pci_detach(dev); 14871d51719SMichael Gmelin 14971d51719SMichael Gmelin return (error); 15071d51719SMichael Gmelin } 15171d51719SMichael Gmelin 15271d51719SMichael Gmelin static int 15371d51719SMichael Gmelin ig4iic_pci_detach(device_t dev) 15471d51719SMichael Gmelin { 15571d51719SMichael Gmelin ig4iic_softc_t *sc = device_get_softc(dev); 15671d51719SMichael Gmelin int error; 15771d51719SMichael Gmelin 15871d51719SMichael Gmelin if (sc->pci_attached) { 15971d51719SMichael Gmelin error = ig4iic_detach(sc); 16071d51719SMichael Gmelin if (error) 16171d51719SMichael Gmelin return (error); 16271d51719SMichael Gmelin sc->pci_attached = 0; 16371d51719SMichael Gmelin } 16471d51719SMichael Gmelin 16571d51719SMichael Gmelin if (sc->intr_res) { 16671d51719SMichael Gmelin bus_release_resource(dev, SYS_RES_IRQ, 16771d51719SMichael Gmelin sc->intr_rid, sc->intr_res); 16871d51719SMichael Gmelin sc->intr_res = NULL; 16971d51719SMichael Gmelin } 17071d51719SMichael Gmelin if (sc->intr_rid != 0) 17171d51719SMichael Gmelin pci_release_msi(dev); 17271d51719SMichael Gmelin if (sc->regs_res) { 17371d51719SMichael Gmelin bus_release_resource(dev, SYS_RES_MEMORY, 17471d51719SMichael Gmelin sc->regs_rid, sc->regs_res); 17571d51719SMichael Gmelin sc->regs_res = NULL; 17671d51719SMichael Gmelin } 1774cd6abddSMichael Gmelin if (mtx_initialized(&sc->io_lock)) { 1784cd6abddSMichael Gmelin mtx_destroy(&sc->io_lock); 1794cd6abddSMichael Gmelin sx_destroy(&sc->call_lock); 1804cd6abddSMichael Gmelin } 18171d51719SMichael Gmelin 18271d51719SMichael Gmelin return (0); 18371d51719SMichael Gmelin } 18471d51719SMichael Gmelin 18571d51719SMichael Gmelin static device_method_t ig4iic_pci_methods[] = { 18671d51719SMichael Gmelin /* Device interface */ 18771d51719SMichael Gmelin DEVMETHOD(device_probe, ig4iic_pci_probe), 18871d51719SMichael Gmelin DEVMETHOD(device_attach, ig4iic_pci_attach), 18971d51719SMichael Gmelin DEVMETHOD(device_detach, ig4iic_pci_detach), 19071d51719SMichael Gmelin 191448897d3SAndriy Gapon DEVMETHOD(iicbus_transfer, ig4iic_transfer), 192448897d3SAndriy Gapon DEVMETHOD(iicbus_reset, ig4iic_reset), 193448897d3SAndriy Gapon DEVMETHOD(iicbus_callback, iicbus_null_callback), 194448897d3SAndriy Gapon 19571d51719SMichael Gmelin DEVMETHOD_END 19671d51719SMichael Gmelin }; 19771d51719SMichael Gmelin 19871d51719SMichael Gmelin static driver_t ig4iic_pci_driver = { 19971d51719SMichael Gmelin "ig4iic", 20071d51719SMichael Gmelin ig4iic_pci_methods, 20171d51719SMichael Gmelin sizeof(struct ig4iic_softc) 20271d51719SMichael Gmelin }; 20371d51719SMichael Gmelin 20471d51719SMichael Gmelin static devclass_t ig4iic_pci_devclass; 20571d51719SMichael Gmelin 206448897d3SAndriy Gapon DRIVER_MODULE_ORDERED(ig4iic, pci, ig4iic_pci_driver, ig4iic_pci_devclass, 0, 0, 207448897d3SAndriy Gapon SI_ORDER_ANY); 20871d51719SMichael Gmelin MODULE_DEPEND(ig4iic, pci, 1, 1, 1); 209448897d3SAndriy Gapon MODULE_DEPEND(ig4iic, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER); 21071d51719SMichael Gmelin MODULE_VERSION(ig4iic, 1); 211