1 /*- 2 * Copyright (c) 2016-2019 Emmanuel Vadot <manu@freebsd.org> 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE 17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 * SUCH DAMAGE. 24 */ 25 26 #include <sys/cdefs.h> 27 #include <sys/param.h> 28 #include <sys/systm.h> 29 #include <sys/bus.h> 30 #include <sys/kernel.h> 31 #include <sys/module.h> 32 #include <sys/resource.h> 33 34 #include <machine/bus.h> 35 #include <machine/resource.h> 36 37 #include <sys/rman.h> 38 39 #include <sys/lock.h> 40 #include <sys/mutex.h> 41 42 #include <dev/iicbus/iiconf.h> 43 #include <dev/iicbus/iicbus.h> 44 #include <dev/iicbus/controller/twsi/twsi.h> 45 #include <dev/ofw/ofw_bus.h> 46 #include <dev/ofw/ofw_bus_subr.h> 47 48 #include <dev/extres/clk/clk.h> 49 #include <dev/extres/hwreset/hwreset.h> 50 51 #include "iicbus_if.h" 52 53 #define TWI_ADDR 0x0 54 #define TWI_XADDR 0x4 55 #define TWI_DATA 0x8 56 #define TWI_CNTR 0xC 57 #define TWI_STAT 0x10 58 #define TWI_CCR 0x14 59 #define TWI_SRST 0x18 60 #define TWI_EFR 0x1C 61 #define TWI_LCR 0x20 62 63 static struct ofw_compat_data compat_data[] = { 64 {"allwinner,sun4i-a10-i2c", 1}, 65 {"allwinner,sun6i-a31-i2c", 1}, 66 {"allwinner,sun8i-a83t-i2c", 1}, 67 {NULL, 0}, 68 }; 69 70 static int 71 a10_twsi_probe(device_t dev) 72 { 73 74 if (!ofw_bus_status_okay(dev)) 75 return (ENXIO); 76 77 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0) 78 return (ENXIO); 79 80 device_set_desc(dev, "Allwinner Integrated I2C Bus Controller"); 81 return (BUS_PROBE_DEFAULT); 82 } 83 84 static int 85 a10_twsi_attach(device_t dev) 86 { 87 struct twsi_softc *sc; 88 hwreset_t rst; 89 int error; 90 91 sc = device_get_softc(dev); 92 93 /* De-assert reset */ 94 if (hwreset_get_by_ofw_idx(dev, 0, 0, &rst) == 0) { 95 error = hwreset_deassert(rst); 96 if (error != 0) { 97 device_printf(dev, "could not de-assert reset\n"); 98 return (error); 99 } 100 } 101 102 /* Activate clock */ 103 error = clk_get_by_ofw_index(dev, 0, 0, &sc->clk_core); 104 if (error != 0) { 105 device_printf(dev, "could not find clock\n"); 106 return (error); 107 } 108 error = clk_enable(sc->clk_core); 109 if (error != 0) { 110 device_printf(dev, "could not enable clock\n"); 111 return (error); 112 } 113 114 sc->reg_data = TWI_DATA; 115 sc->reg_slave_addr = TWI_ADDR; 116 sc->reg_slave_ext_addr = TWI_XADDR; 117 sc->reg_control = TWI_CNTR; 118 sc->reg_status = TWI_STAT; 119 sc->reg_baud_rate = TWI_CCR; 120 sc->reg_soft_reset = TWI_SRST; 121 122 if (ofw_bus_is_compatible(dev, "allwinner,sun6i-a31-i2c") || 123 ofw_bus_is_compatible(dev, "allwinner,sun6i-a83t-i2c")) 124 sc->iflag_w1c = true; 125 return (twsi_attach(dev)); 126 } 127 128 static phandle_t 129 a10_twsi_get_node(device_t bus, device_t dev) 130 { 131 return (ofw_bus_get_node(bus)); 132 } 133 134 static device_method_t a10_twsi_methods[] = { 135 /* device interface */ 136 DEVMETHOD(device_probe, a10_twsi_probe), 137 DEVMETHOD(device_attach, a10_twsi_attach), 138 139 /* OFW methods */ 140 DEVMETHOD(ofw_bus_get_node, a10_twsi_get_node), 141 142 { 0, 0 } 143 }; 144 145 DEFINE_CLASS_1(iichb, a10_twsi_driver, a10_twsi_methods, 146 sizeof(struct twsi_softc), twsi_driver); 147 148 EARLY_DRIVER_MODULE(a10_twsi, simplebus, a10_twsi_driver, 149 0, 0, BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LATE); 150 EARLY_DRIVER_MODULE(iicbus, a10_twsi, iicbus_driver, 151 0, 0, BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LATE); 152 MODULE_DEPEND(a10_twsi, iicbus, 1, 1, 1); 153 SIMPLEBUS_PNP_INFO(compat_data); 154