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 /* 40*e3d25549SAndriy 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 7171d51719SMichael Gmelin 7271d51719SMichael Gmelin static int 7371d51719SMichael Gmelin ig4iic_pci_probe(device_t dev) 7471d51719SMichael Gmelin { 7571d51719SMichael Gmelin switch(pci_get_devid(dev)) { 7671d51719SMichael Gmelin case PCI_CHIP_LYNXPT_LP_I2C_1: 7771d51719SMichael Gmelin device_set_desc(dev, "Intel Lynx Point-LP I2C Controller-1"); 7871d51719SMichael Gmelin break; 7971d51719SMichael Gmelin case PCI_CHIP_LYNXPT_LP_I2C_2: 8071d51719SMichael Gmelin device_set_desc(dev, "Intel Lynx Point-LP I2C Controller-2"); 8171d51719SMichael Gmelin break; 8271d51719SMichael Gmelin default: 8371d51719SMichael Gmelin return (ENXIO); 8471d51719SMichael Gmelin } 8571d51719SMichael Gmelin return (BUS_PROBE_DEFAULT); 8671d51719SMichael Gmelin } 8771d51719SMichael Gmelin 8871d51719SMichael Gmelin static int 8971d51719SMichael Gmelin ig4iic_pci_attach(device_t dev) 9071d51719SMichael Gmelin { 9171d51719SMichael Gmelin ig4iic_softc_t *sc = device_get_softc(dev); 9271d51719SMichael Gmelin int error; 9371d51719SMichael Gmelin 9471d51719SMichael Gmelin bzero(sc, sizeof(*sc)); 9571d51719SMichael Gmelin 964cd6abddSMichael Gmelin mtx_init(&sc->io_lock, "IG4 I/O lock", NULL, MTX_DEF); 974cd6abddSMichael Gmelin sx_init(&sc->call_lock, "IG4 call lock"); 9871d51719SMichael Gmelin 9971d51719SMichael Gmelin sc->dev = dev; 10071d51719SMichael Gmelin sc->regs_rid = PCIR_BAR(0); 10171d51719SMichael Gmelin sc->regs_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 10271d51719SMichael Gmelin &sc->regs_rid, RF_ACTIVE); 10371d51719SMichael Gmelin if (sc->regs_res == NULL) { 10471d51719SMichael Gmelin device_printf(dev, "unable to map registers\n"); 10571d51719SMichael Gmelin ig4iic_pci_detach(dev); 10671d51719SMichael Gmelin return (ENXIO); 10771d51719SMichael Gmelin } 10871d51719SMichael Gmelin sc->intr_rid = 0; 10971d51719SMichael Gmelin if (pci_alloc_msi(dev, &sc->intr_rid)) { 11071d51719SMichael Gmelin device_printf(dev, "Using MSI\n"); 11171d51719SMichael Gmelin } 11271d51719SMichael Gmelin sc->intr_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, 11371d51719SMichael Gmelin &sc->intr_rid, RF_SHAREABLE | RF_ACTIVE); 11471d51719SMichael Gmelin if (sc->intr_res == NULL) { 11571d51719SMichael Gmelin device_printf(dev, "unable to map interrupt\n"); 11671d51719SMichael Gmelin ig4iic_pci_detach(dev); 11771d51719SMichael Gmelin return (ENXIO); 11871d51719SMichael Gmelin } 11971d51719SMichael Gmelin sc->pci_attached = 1; 12071d51719SMichael Gmelin 12171d51719SMichael Gmelin error = ig4iic_attach(sc); 12271d51719SMichael Gmelin if (error) 12371d51719SMichael Gmelin ig4iic_pci_detach(dev); 12471d51719SMichael Gmelin 12571d51719SMichael Gmelin return (error); 12671d51719SMichael Gmelin } 12771d51719SMichael Gmelin 12871d51719SMichael Gmelin static int 12971d51719SMichael Gmelin ig4iic_pci_detach(device_t dev) 13071d51719SMichael Gmelin { 13171d51719SMichael Gmelin ig4iic_softc_t *sc = device_get_softc(dev); 13271d51719SMichael Gmelin int error; 13371d51719SMichael Gmelin 13471d51719SMichael Gmelin if (sc->pci_attached) { 13571d51719SMichael Gmelin error = ig4iic_detach(sc); 13671d51719SMichael Gmelin if (error) 13771d51719SMichael Gmelin return (error); 13871d51719SMichael Gmelin sc->pci_attached = 0; 13971d51719SMichael Gmelin } 14071d51719SMichael Gmelin 14171d51719SMichael Gmelin if (sc->intr_res) { 14271d51719SMichael Gmelin bus_release_resource(dev, SYS_RES_IRQ, 14371d51719SMichael Gmelin sc->intr_rid, sc->intr_res); 14471d51719SMichael Gmelin sc->intr_res = NULL; 14571d51719SMichael Gmelin } 14671d51719SMichael Gmelin if (sc->intr_rid != 0) 14771d51719SMichael Gmelin pci_release_msi(dev); 14871d51719SMichael Gmelin if (sc->regs_res) { 14971d51719SMichael Gmelin bus_release_resource(dev, SYS_RES_MEMORY, 15071d51719SMichael Gmelin sc->regs_rid, sc->regs_res); 15171d51719SMichael Gmelin sc->regs_res = NULL; 15271d51719SMichael Gmelin } 1534cd6abddSMichael Gmelin if (mtx_initialized(&sc->io_lock)) { 1544cd6abddSMichael Gmelin mtx_destroy(&sc->io_lock); 1554cd6abddSMichael Gmelin sx_destroy(&sc->call_lock); 1564cd6abddSMichael Gmelin } 15771d51719SMichael Gmelin 15871d51719SMichael Gmelin return (0); 15971d51719SMichael Gmelin } 16071d51719SMichael Gmelin 16171d51719SMichael Gmelin static device_method_t ig4iic_pci_methods[] = { 16271d51719SMichael Gmelin /* Device interface */ 16371d51719SMichael Gmelin DEVMETHOD(device_probe, ig4iic_pci_probe), 16471d51719SMichael Gmelin DEVMETHOD(device_attach, ig4iic_pci_attach), 16571d51719SMichael Gmelin DEVMETHOD(device_detach, ig4iic_pci_detach), 16671d51719SMichael Gmelin 167448897d3SAndriy Gapon DEVMETHOD(iicbus_transfer, ig4iic_transfer), 168448897d3SAndriy Gapon DEVMETHOD(iicbus_reset, ig4iic_reset), 169448897d3SAndriy Gapon DEVMETHOD(iicbus_callback, iicbus_null_callback), 170448897d3SAndriy Gapon 17171d51719SMichael Gmelin DEVMETHOD_END 17271d51719SMichael Gmelin }; 17371d51719SMichael Gmelin 17471d51719SMichael Gmelin static driver_t ig4iic_pci_driver = { 17571d51719SMichael Gmelin "ig4iic", 17671d51719SMichael Gmelin ig4iic_pci_methods, 17771d51719SMichael Gmelin sizeof(struct ig4iic_softc) 17871d51719SMichael Gmelin }; 17971d51719SMichael Gmelin 18071d51719SMichael Gmelin static devclass_t ig4iic_pci_devclass; 18171d51719SMichael Gmelin 182448897d3SAndriy Gapon DRIVER_MODULE_ORDERED(ig4iic, pci, ig4iic_pci_driver, ig4iic_pci_devclass, 0, 0, 183448897d3SAndriy Gapon SI_ORDER_ANY); 18471d51719SMichael Gmelin MODULE_DEPEND(ig4iic, pci, 1, 1, 1); 185448897d3SAndriy Gapon MODULE_DEPEND(ig4iic, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER); 18671d51719SMichael Gmelin MODULE_VERSION(ig4iic, 1); 187