1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2019 Axiado Corporation. 5 * All rights reserved. 6 * 7 * This software was developed in part by Philip Paeps under contract for 8 * Axiado Corporation. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright notice, 14 * this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright notice, 16 * this list of conditions and the following disclaimer in the documentation 17 * and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 29 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/kernel.h> 35 #include <sys/limits.h> 36 #include <sys/module.h> 37 #include <sys/bus.h> 38 #include <sys/lock.h> 39 #include <sys/mutex.h> 40 #include <sys/rman.h> 41 42 #include <dev/extres/clk/clk.h> 43 44 #include <dev/iicbus/iicbus.h> 45 #include <dev/iicbus/iiconf.h> 46 47 #include <dev/ofw/ofw_bus.h> 48 #include <dev/ofw/ofw_bus_subr.h> 49 50 #include "iicbus_if.h" 51 #include "iicoc.h" 52 53 static struct ofw_compat_data compat_data[] = { 54 { "opencores,i2c-ocores", 1 }, 55 { "sifive,fu740-c000-i2c", 1 }, 56 { "sifive,fu540-c000-i2c", 1 }, 57 { "sifive,i2c0", 1 }, 58 { NULL, 0 } 59 }; 60 61 static struct resource_spec iicoc_spec[] = { 62 { SYS_RES_MEMORY, 0, RF_ACTIVE }, 63 RESOURCE_SPEC_END 64 }; 65 66 static phandle_t 67 iicoc_get_node(device_t bus, device_t dev) 68 { 69 70 /* Share controller node with iicbus device. */ 71 return (ofw_bus_get_node(bus)); 72 } 73 74 static int 75 iicoc_attach(device_t dev) 76 { 77 struct iicoc_softc *sc; 78 phandle_t node; 79 clk_t clock; 80 uint64_t clockfreq; 81 int error; 82 83 sc = device_get_softc(dev); 84 sc->dev = dev; 85 86 mtx_init(&sc->sc_mtx, "iicoc", "iicoc", MTX_DEF); 87 88 error = bus_alloc_resources(dev, iicoc_spec, &sc->mem_res); 89 if (error) { 90 device_printf(dev, "Could not allocate bus resource.\n"); 91 goto fail; 92 } 93 94 node = ofw_bus_get_node(dev); 95 sc->reg_shift = 0; 96 OF_getencprop(node, "reg-shift", &sc->reg_shift, 97 sizeof(sc->reg_shift)); 98 99 error = clk_get_by_ofw_index(dev, 0, 0, &clock); 100 if (error) { 101 device_printf(dev, "Couldn't get clock\n"); 102 goto fail; 103 } 104 error = clk_enable(clock); 105 if (error) { 106 device_printf(dev, "Couldn't enable clock\n"); 107 goto fail1; 108 } 109 error = clk_get_freq(clock, &clockfreq); 110 if (error) { 111 device_printf(dev, "Couldn't get clock frequency\n"); 112 goto fail1; 113 } 114 if (clockfreq > UINT_MAX) { 115 device_printf(dev, "Unsupported clock frequency\n"); 116 goto fail1; 117 } 118 sc->clockfreq = (u_int)clockfreq; 119 sc->i2cfreq = XLP_I2C_FREQ; 120 iicoc_init(dev); 121 122 sc->iicbus = device_add_child(dev, "iicbus", -1); 123 if (sc->iicbus == NULL) { 124 device_printf(dev, "Could not allocate iicbus instance.\n"); 125 error = ENXIO; 126 goto fail1; 127 } 128 129 /* Probe and attach the iicbus when interrupts are available. */ 130 config_intrhook_oneshot((ich_func_t)bus_generic_attach, dev); 131 132 return (0); 133 134 fail1: 135 clk_disable(clock); 136 137 fail: 138 bus_release_resources(dev, iicoc_spec, &sc->mem_res); 139 mtx_destroy(&sc->sc_mtx); 140 return (error); 141 } 142 143 static int 144 iicoc_probe(device_t dev) 145 { 146 147 if (!ofw_bus_status_okay(dev)) 148 return (ENXIO); 149 150 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0) 151 return (ENXIO); 152 153 device_set_desc(dev, "OpenCores I2C master controller"); 154 155 return (BUS_PROBE_DEFAULT); 156 } 157 158 static device_method_t iicoc_methods[] = { 159 /* device interface */ 160 DEVMETHOD(device_probe, iicoc_probe), 161 DEVMETHOD(device_attach, iicoc_attach), 162 163 /* ofw interface */ 164 DEVMETHOD(ofw_bus_get_node, iicoc_get_node), 165 166 /* iicbus interface */ 167 DEVMETHOD(iicbus_callback, iicbus_null_callback), 168 DEVMETHOD(iicbus_repeated_start, iicoc_iicbus_repeated_start), 169 DEVMETHOD(iicbus_start, iicoc_iicbus_start), 170 DEVMETHOD(iicbus_stop, iicoc_iicbus_stop), 171 DEVMETHOD(iicbus_reset, iicoc_iicbus_reset), 172 DEVMETHOD(iicbus_write, iicoc_iicbus_write), 173 DEVMETHOD(iicbus_read, iicoc_iicbus_read), 174 DEVMETHOD(iicbus_transfer, iicbus_transfer_gen), 175 176 DEVMETHOD_END 177 }; 178 179 static driver_t iicoc_driver = { 180 "iicoc", 181 iicoc_methods, 182 sizeof(struct iicoc_softc), 183 }; 184 185 SIMPLEBUS_PNP_INFO(compat_data); 186 DRIVER_MODULE(iicoc, simplebus, iicoc_driver, 0, 0); 187 DRIVER_MODULE(ofw_iicbus, iicoc, ofw_iicbus_driver, 0, 0); 188 MODULE_DEPEND(iicoc, iicbus, 1, 1, 1); 189 MODULE_DEPEND(iicoc, ofw_iicbus, 1, 1, 1); 190