1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2004 Joerg Wunsch 5 * 6 * derived from sys/i386/isa/pcf.c which is: 7 * 8 * Copyright (c) 1998 Nicolas Souchu, Marc Bouget 9 * All rights reserved. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 #include <sys/cdefs.h> 33 __FBSDID("$FreeBSD$"); 34 35 /* 36 * Hardware driver for a Philips PCF8584 I2C bus controller sitting 37 * on a generic ISA bus. 38 */ 39 40 #include <sys/param.h> 41 #include <sys/bus.h> 42 #include <sys/lock.h> 43 #include <sys/kernel.h> 44 #include <sys/module.h> 45 #include <sys/mutex.h> 46 #include <sys/resource.h> 47 #include <sys/systm.h> 48 49 #include <machine/bus.h> 50 #include <machine/resource.h> 51 52 #include <sys/rman.h> 53 54 #include <isa/isareg.h> 55 #include <isa/isavar.h> 56 57 #include <dev/iicbus/iiconf.h> 58 #include <dev/pcf/pcfvar.h> 59 #include "iicbus_if.h" 60 61 #define PCF_NAME "pcf" 62 63 static void pcf_isa_identify(driver_t *, device_t); 64 static int pcf_isa_probe(device_t); 65 static int pcf_isa_attach(device_t); 66 static int pcf_isa_detach(device_t); 67 68 static device_method_t pcf_isa_methods[] = { 69 /* device interface */ 70 DEVMETHOD(device_identify, pcf_isa_identify), 71 DEVMETHOD(device_probe, pcf_isa_probe), 72 DEVMETHOD(device_attach, pcf_isa_attach), 73 DEVMETHOD(device_detach, pcf_isa_detach), 74 75 /* iicbus interface */ 76 DEVMETHOD(iicbus_callback, iicbus_null_callback), 77 DEVMETHOD(iicbus_repeated_start, pcf_repeated_start), 78 DEVMETHOD(iicbus_start, pcf_start), 79 DEVMETHOD(iicbus_stop, pcf_stop), 80 DEVMETHOD(iicbus_write, pcf_write), 81 DEVMETHOD(iicbus_read, pcf_read), 82 DEVMETHOD(iicbus_reset, pcf_rst_card), 83 { 0, 0 } 84 }; 85 86 static devclass_t pcf_isa_devclass; 87 88 static driver_t pcf_isa_driver = { 89 PCF_NAME, 90 pcf_isa_methods, 91 sizeof(struct pcf_softc), 92 }; 93 94 static void 95 pcf_isa_identify(driver_t *driver, device_t parent) 96 { 97 BUS_ADD_CHILD(parent, ISA_ORDER_SPECULATIVE, PCF_NAME, 0); 98 99 return; 100 } 101 102 static int 103 pcf_isa_probe(device_t dev) 104 { 105 rman_res_t start, count; 106 u_int rid = 0, port, error; 107 108 /* skip PnP probes */ 109 if (isa_get_logicalid(dev)) 110 return (ENXIO); 111 112 /* The port address must be explicitly specified */ 113 bus_get_resource(dev, SYS_RES_IOPORT, rid, &start, &count); 114 if ((error = resource_int_value(PCF_NAME, 0, "port", &port) != 0)) 115 return (error); 116 117 /* Probe is only successful for the specified base io */ 118 if (port != (u_int)start) 119 return (ENXIO); 120 121 device_set_desc(dev, "PCF8584 I2C bus controller"); 122 123 return (0); 124 } 125 126 static int 127 pcf_isa_attach(device_t dev) 128 { 129 struct pcf_softc *sc; 130 int rv = ENXIO; 131 132 sc = DEVTOSOFTC(dev); 133 mtx_init(&sc->pcf_lock, device_get_nameunit(dev), "pcf", MTX_DEF); 134 135 /* IO port is mandatory */ 136 sc->res_ioport = bus_alloc_resource_any(dev, SYS_RES_IOPORT, 137 &sc->rid_ioport, RF_ACTIVE); 138 if (sc->res_ioport == 0) { 139 device_printf(dev, "cannot reserve I/O port range\n"); 140 goto error; 141 } 142 143 sc->pcf_flags = device_get_flags(dev); 144 145 if (!(sc->pcf_flags & IIC_POLLED)) { 146 sc->res_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->rid_irq, 147 RF_ACTIVE); 148 if (sc->res_irq == 0) { 149 device_printf(dev, "can't reserve irq, polled mode.\n"); 150 sc->pcf_flags |= IIC_POLLED; 151 } 152 } 153 154 /* reset the chip */ 155 pcf_rst_card(dev, IIC_FASTEST, PCF_DEFAULT_ADDR, NULL); 156 157 if (sc->res_irq) { 158 rv = bus_setup_intr(dev, sc->res_irq, 159 INTR_TYPE_NET /* | INTR_ENTROPY */, 160 NULL, pcf_intr, sc, &sc->intr_cookie); 161 if (rv) { 162 device_printf(dev, "could not setup IRQ\n"); 163 goto error; 164 } 165 } 166 167 if ((sc->iicbus = device_add_child(dev, "iicbus", -1)) == NULL) 168 device_printf(dev, "could not allocate iicbus instance\n"); 169 170 /* probe and attach the iicbus */ 171 bus_generic_attach(dev); 172 173 return (0); 174 175 error: 176 if (sc->res_irq != 0) { 177 bus_release_resource(dev, SYS_RES_IRQ, sc->rid_irq, 178 sc->res_irq); 179 } 180 if (sc->res_ioport != 0) { 181 bus_release_resource(dev, SYS_RES_IOPORT, sc->rid_ioport, 182 sc->res_ioport); 183 } 184 mtx_destroy(&sc->pcf_lock); 185 return (rv); 186 } 187 188 static int 189 pcf_isa_detach(device_t dev) 190 { 191 struct pcf_softc *sc; 192 int rv; 193 194 sc = DEVTOSOFTC(dev); 195 196 if ((rv = bus_generic_detach(dev)) != 0) 197 return (rv); 198 199 if ((rv = device_delete_child(dev, sc->iicbus)) != 0) 200 return (rv); 201 202 if (sc->res_irq != 0) { 203 bus_teardown_intr(dev, sc->res_irq, sc->intr_cookie); 204 bus_release_resource(dev, SYS_RES_IRQ, sc->rid_irq, sc->res_irq); 205 } 206 207 bus_release_resource(dev, SYS_RES_IOPORT, sc->rid_ioport, sc->res_ioport); 208 mtx_destroy(&sc->pcf_lock); 209 210 return (0); 211 } 212 213 DRIVER_MODULE(pcf_isa, isa, pcf_isa_driver, pcf_isa_devclass, 0, 0); 214