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 7100eb880dSConrad Meyer #define PCI_CHIP_BRASWELL_I2C_1 0x22c18086 7200eb880dSConrad Meyer #define PCI_CHIP_BRASWELL_I2C_2 0x22c28086 7300eb880dSConrad Meyer #define PCI_CHIP_BRASWELL_I2C_3 0x22c38086 7400eb880dSConrad Meyer #define PCI_CHIP_BRASWELL_I2C_5 0x22c58086 7500eb880dSConrad Meyer #define PCI_CHIP_BRASWELL_I2C_6 0x22c68086 7600eb880dSConrad 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; 8800eb880dSConrad Meyer case PCI_CHIP_BRASWELL_I2C_1: 8900eb880dSConrad Meyer device_set_desc(dev, "Intel Braswell Serial I/O I2C Port 1"); 9000eb880dSConrad Meyer break; 9100eb880dSConrad Meyer case PCI_CHIP_BRASWELL_I2C_2: 9200eb880dSConrad Meyer device_set_desc(dev, "Intel Braswell Serial I/O I2C Port 2"); 9300eb880dSConrad Meyer break; 9400eb880dSConrad Meyer case PCI_CHIP_BRASWELL_I2C_3: 9500eb880dSConrad Meyer device_set_desc(dev, "Intel Braswell Serial I/O I2C Port 3"); 9600eb880dSConrad Meyer break; 9700eb880dSConrad Meyer case PCI_CHIP_BRASWELL_I2C_5: 9800eb880dSConrad Meyer device_set_desc(dev, "Intel Braswell Serial I/O I2C Port 5"); 9900eb880dSConrad Meyer break; 10000eb880dSConrad Meyer case PCI_CHIP_BRASWELL_I2C_6: 10100eb880dSConrad Meyer device_set_desc(dev, "Intel Braswell Serial I/O I2C Port 6"); 10200eb880dSConrad Meyer break; 10300eb880dSConrad Meyer case PCI_CHIP_BRASWELL_I2C_7: 10400eb880dSConrad Meyer device_set_desc(dev, "Intel Braswell Serial I/O I2C Port 7"); 10500eb880dSConrad 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 sc->dev = dev; 11971d51719SMichael Gmelin sc->regs_rid = PCIR_BAR(0); 12071d51719SMichael Gmelin sc->regs_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 12171d51719SMichael Gmelin &sc->regs_rid, RF_ACTIVE); 12271d51719SMichael Gmelin if (sc->regs_res == NULL) { 12371d51719SMichael Gmelin device_printf(dev, "unable to map registers\n"); 12471d51719SMichael Gmelin ig4iic_pci_detach(dev); 12571d51719SMichael Gmelin return (ENXIO); 12671d51719SMichael Gmelin } 12771d51719SMichael Gmelin sc->intr_rid = 0; 12871d51719SMichael Gmelin if (pci_alloc_msi(dev, &sc->intr_rid)) { 12971d51719SMichael Gmelin device_printf(dev, "Using MSI\n"); 13071d51719SMichael Gmelin } 13171d51719SMichael Gmelin sc->intr_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, 13271d51719SMichael Gmelin &sc->intr_rid, RF_SHAREABLE | RF_ACTIVE); 13371d51719SMichael Gmelin if (sc->intr_res == NULL) { 13471d51719SMichael Gmelin device_printf(dev, "unable to map interrupt\n"); 13571d51719SMichael Gmelin ig4iic_pci_detach(dev); 13671d51719SMichael Gmelin return (ENXIO); 13771d51719SMichael Gmelin } 138*5c5bcb1dSOleksandr Tymoshenko sc->platform_attached = 1; 13971d51719SMichael Gmelin 14071d51719SMichael Gmelin error = ig4iic_attach(sc); 14171d51719SMichael Gmelin if (error) 14271d51719SMichael Gmelin ig4iic_pci_detach(dev); 14371d51719SMichael Gmelin 14471d51719SMichael Gmelin return (error); 14571d51719SMichael Gmelin } 14671d51719SMichael Gmelin 14771d51719SMichael Gmelin static int 14871d51719SMichael Gmelin ig4iic_pci_detach(device_t dev) 14971d51719SMichael Gmelin { 15071d51719SMichael Gmelin ig4iic_softc_t *sc = device_get_softc(dev); 15171d51719SMichael Gmelin int error; 15271d51719SMichael Gmelin 153*5c5bcb1dSOleksandr Tymoshenko if (sc->platform_attached) { 15471d51719SMichael Gmelin error = ig4iic_detach(sc); 15571d51719SMichael Gmelin if (error) 15671d51719SMichael Gmelin return (error); 157*5c5bcb1dSOleksandr Tymoshenko sc->platform_attached = 0; 15871d51719SMichael Gmelin } 15971d51719SMichael Gmelin 16071d51719SMichael Gmelin if (sc->intr_res) { 16171d51719SMichael Gmelin bus_release_resource(dev, SYS_RES_IRQ, 16271d51719SMichael Gmelin sc->intr_rid, sc->intr_res); 16371d51719SMichael Gmelin sc->intr_res = NULL; 16471d51719SMichael Gmelin } 16571d51719SMichael Gmelin if (sc->intr_rid != 0) 16671d51719SMichael Gmelin pci_release_msi(dev); 16771d51719SMichael Gmelin if (sc->regs_res) { 16871d51719SMichael Gmelin bus_release_resource(dev, SYS_RES_MEMORY, 16971d51719SMichael Gmelin sc->regs_rid, sc->regs_res); 17071d51719SMichael Gmelin sc->regs_res = NULL; 17171d51719SMichael Gmelin } 17271d51719SMichael Gmelin 17371d51719SMichael Gmelin return (0); 17471d51719SMichael Gmelin } 17571d51719SMichael Gmelin 17671d51719SMichael Gmelin static device_method_t ig4iic_pci_methods[] = { 17771d51719SMichael Gmelin /* Device interface */ 17871d51719SMichael Gmelin DEVMETHOD(device_probe, ig4iic_pci_probe), 17971d51719SMichael Gmelin DEVMETHOD(device_attach, ig4iic_pci_attach), 18071d51719SMichael Gmelin DEVMETHOD(device_detach, ig4iic_pci_detach), 18171d51719SMichael Gmelin 182448897d3SAndriy Gapon DEVMETHOD(iicbus_transfer, ig4iic_transfer), 183448897d3SAndriy Gapon DEVMETHOD(iicbus_reset, ig4iic_reset), 184448897d3SAndriy Gapon DEVMETHOD(iicbus_callback, iicbus_null_callback), 185448897d3SAndriy Gapon 18671d51719SMichael Gmelin DEVMETHOD_END 18771d51719SMichael Gmelin }; 18871d51719SMichael Gmelin 18971d51719SMichael Gmelin static driver_t ig4iic_pci_driver = { 190*5c5bcb1dSOleksandr Tymoshenko "ig4iic_pci", 19171d51719SMichael Gmelin ig4iic_pci_methods, 19271d51719SMichael Gmelin sizeof(struct ig4iic_softc) 19371d51719SMichael Gmelin }; 19471d51719SMichael Gmelin 19571d51719SMichael Gmelin static devclass_t ig4iic_pci_devclass; 19671d51719SMichael Gmelin 197*5c5bcb1dSOleksandr Tymoshenko DRIVER_MODULE_ORDERED(ig4iic_pci, pci, ig4iic_pci_driver, ig4iic_pci_devclass, 0, 0, 198448897d3SAndriy Gapon SI_ORDER_ANY); 199*5c5bcb1dSOleksandr Tymoshenko MODULE_DEPEND(ig4iic_pci, pci, 1, 1, 1); 200*5c5bcb1dSOleksandr Tymoshenko MODULE_DEPEND(ig4iic_pci, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER); 201*5c5bcb1dSOleksandr Tymoshenko MODULE_VERSION(ig4iic_pci, 1); 202