1125f5c5bSEmmanuel Vadot /*- 2125f5c5bSEmmanuel Vadot * SPDX-License-Identifier: BSD-2-Clause 3125f5c5bSEmmanuel Vadot * 4125f5c5bSEmmanuel Vadot * Copyright (c) 2019 Axiado Corporation. 5125f5c5bSEmmanuel Vadot * All rights reserved. 6125f5c5bSEmmanuel Vadot * 7125f5c5bSEmmanuel Vadot * This software was developed in part by Philip Paeps under contract for 8125f5c5bSEmmanuel Vadot * Axiado Corporation. 9125f5c5bSEmmanuel Vadot * 10125f5c5bSEmmanuel Vadot * Redistribution and use in source and binary forms, with or without 11125f5c5bSEmmanuel Vadot * modification, are permitted provided that the following conditions are met: 12125f5c5bSEmmanuel Vadot * 13125f5c5bSEmmanuel Vadot * 1. Redistributions of source code must retain the above copyright notice, 14125f5c5bSEmmanuel Vadot * this list of conditions and the following disclaimer. 15125f5c5bSEmmanuel Vadot * 2. Redistributions in binary form must reproduce the above copyright notice, 16125f5c5bSEmmanuel Vadot * this list of conditions and the following disclaimer in the documentation 17125f5c5bSEmmanuel Vadot * and/or other materials provided with the distribution. 18125f5c5bSEmmanuel Vadot * 19125f5c5bSEmmanuel Vadot * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20125f5c5bSEmmanuel Vadot * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 21125f5c5bSEmmanuel Vadot * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22125f5c5bSEmmanuel Vadot * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 23125f5c5bSEmmanuel Vadot * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 24125f5c5bSEmmanuel Vadot * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 25125f5c5bSEmmanuel Vadot * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 26125f5c5bSEmmanuel Vadot * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 27125f5c5bSEmmanuel Vadot * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 28125f5c5bSEmmanuel Vadot * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 29125f5c5bSEmmanuel Vadot * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30125f5c5bSEmmanuel Vadot */ 31125f5c5bSEmmanuel Vadot 32125f5c5bSEmmanuel Vadot #include <sys/cdefs.h> 33125f5c5bSEmmanuel Vadot #include <sys/param.h> 34125f5c5bSEmmanuel Vadot #include <sys/systm.h> 35125f5c5bSEmmanuel Vadot #include <sys/kernel.h> 36125f5c5bSEmmanuel Vadot #include <sys/limits.h> 37125f5c5bSEmmanuel Vadot #include <sys/module.h> 38125f5c5bSEmmanuel Vadot #include <sys/bus.h> 39125f5c5bSEmmanuel Vadot #include <sys/lock.h> 40125f5c5bSEmmanuel Vadot #include <sys/mutex.h> 41125f5c5bSEmmanuel Vadot #include <sys/rman.h> 42125f5c5bSEmmanuel Vadot 43125f5c5bSEmmanuel Vadot #include <dev/extres/clk/clk.h> 44125f5c5bSEmmanuel Vadot 45125f5c5bSEmmanuel Vadot #include <dev/iicbus/iicbus.h> 46125f5c5bSEmmanuel Vadot #include <dev/iicbus/iiconf.h> 47125f5c5bSEmmanuel Vadot 48125f5c5bSEmmanuel Vadot #include <dev/ofw/ofw_bus.h> 49125f5c5bSEmmanuel Vadot #include <dev/ofw/ofw_bus_subr.h> 50125f5c5bSEmmanuel Vadot 51125f5c5bSEmmanuel Vadot #include "iicbus_if.h" 52125f5c5bSEmmanuel Vadot #include "iicoc.h" 53125f5c5bSEmmanuel Vadot 54125f5c5bSEmmanuel Vadot static struct ofw_compat_data compat_data[] = { 55125f5c5bSEmmanuel Vadot { "opencores,i2c-ocores", 1 }, 56125f5c5bSEmmanuel Vadot { "sifive,fu740-c000-i2c", 1 }, 57125f5c5bSEmmanuel Vadot { "sifive,fu540-c000-i2c", 1 }, 58125f5c5bSEmmanuel Vadot { "sifive,i2c0", 1 }, 59125f5c5bSEmmanuel Vadot { NULL, 0 } 60125f5c5bSEmmanuel Vadot }; 61125f5c5bSEmmanuel Vadot 62125f5c5bSEmmanuel Vadot static struct resource_spec iicoc_spec[] = { 63125f5c5bSEmmanuel Vadot { SYS_RES_MEMORY, 0, RF_ACTIVE }, 64125f5c5bSEmmanuel Vadot RESOURCE_SPEC_END 65125f5c5bSEmmanuel Vadot }; 66125f5c5bSEmmanuel Vadot 67125f5c5bSEmmanuel Vadot static phandle_t 68125f5c5bSEmmanuel Vadot iicoc_get_node(device_t bus, device_t dev) 69125f5c5bSEmmanuel Vadot { 70125f5c5bSEmmanuel Vadot 71125f5c5bSEmmanuel Vadot /* Share controller node with iicbus device. */ 72125f5c5bSEmmanuel Vadot return (ofw_bus_get_node(bus)); 73125f5c5bSEmmanuel Vadot } 74125f5c5bSEmmanuel Vadot 75125f5c5bSEmmanuel Vadot static int 76125f5c5bSEmmanuel Vadot iicoc_attach(device_t dev) 77125f5c5bSEmmanuel Vadot { 78125f5c5bSEmmanuel Vadot struct iicoc_softc *sc; 79125f5c5bSEmmanuel Vadot phandle_t node; 80125f5c5bSEmmanuel Vadot clk_t clock; 81125f5c5bSEmmanuel Vadot uint64_t clockfreq; 82125f5c5bSEmmanuel Vadot int error; 83125f5c5bSEmmanuel Vadot 84125f5c5bSEmmanuel Vadot sc = device_get_softc(dev); 85125f5c5bSEmmanuel Vadot sc->dev = dev; 86125f5c5bSEmmanuel Vadot 87125f5c5bSEmmanuel Vadot mtx_init(&sc->sc_mtx, "iicoc", "iicoc", MTX_DEF); 88125f5c5bSEmmanuel Vadot 89125f5c5bSEmmanuel Vadot error = bus_alloc_resources(dev, iicoc_spec, &sc->mem_res); 90125f5c5bSEmmanuel Vadot if (error) { 91125f5c5bSEmmanuel Vadot device_printf(dev, "Could not allocate bus resource.\n"); 92125f5c5bSEmmanuel Vadot goto fail; 93125f5c5bSEmmanuel Vadot } 94125f5c5bSEmmanuel Vadot 95125f5c5bSEmmanuel Vadot node = ofw_bus_get_node(dev); 96125f5c5bSEmmanuel Vadot sc->reg_shift = 0; 97125f5c5bSEmmanuel Vadot OF_getencprop(node, "reg-shift", &sc->reg_shift, 98125f5c5bSEmmanuel Vadot sizeof(sc->reg_shift)); 99125f5c5bSEmmanuel Vadot 100125f5c5bSEmmanuel Vadot error = clk_get_by_ofw_index(dev, 0, 0, &clock); 101125f5c5bSEmmanuel Vadot if (error) { 102125f5c5bSEmmanuel Vadot device_printf(dev, "Couldn't get clock\n"); 103125f5c5bSEmmanuel Vadot goto fail; 104125f5c5bSEmmanuel Vadot } 105125f5c5bSEmmanuel Vadot error = clk_enable(clock); 106125f5c5bSEmmanuel Vadot if (error) { 107125f5c5bSEmmanuel Vadot device_printf(dev, "Couldn't enable clock\n"); 108125f5c5bSEmmanuel Vadot goto fail1; 109125f5c5bSEmmanuel Vadot } 110125f5c5bSEmmanuel Vadot error = clk_get_freq(clock, &clockfreq); 111125f5c5bSEmmanuel Vadot if (error) { 112125f5c5bSEmmanuel Vadot device_printf(dev, "Couldn't get clock frequency\n"); 113125f5c5bSEmmanuel Vadot goto fail1; 114125f5c5bSEmmanuel Vadot } 115125f5c5bSEmmanuel Vadot if (clockfreq > UINT_MAX) { 116125f5c5bSEmmanuel Vadot device_printf(dev, "Unsupported clock frequency\n"); 117125f5c5bSEmmanuel Vadot goto fail1; 118125f5c5bSEmmanuel Vadot } 119125f5c5bSEmmanuel Vadot sc->clockfreq = (u_int)clockfreq; 120125f5c5bSEmmanuel Vadot sc->i2cfreq = XLP_I2C_FREQ; 121125f5c5bSEmmanuel Vadot iicoc_init(dev); 122125f5c5bSEmmanuel Vadot 123125f5c5bSEmmanuel Vadot sc->iicbus = device_add_child(dev, "iicbus", -1); 124125f5c5bSEmmanuel Vadot if (sc->iicbus == NULL) { 125125f5c5bSEmmanuel Vadot device_printf(dev, "Could not allocate iicbus instance.\n"); 126125f5c5bSEmmanuel Vadot error = ENXIO; 127125f5c5bSEmmanuel Vadot goto fail1; 128125f5c5bSEmmanuel Vadot } 129125f5c5bSEmmanuel Vadot 130125f5c5bSEmmanuel Vadot /* Probe and attach the iicbus when interrupts are available. */ 131125f5c5bSEmmanuel Vadot config_intrhook_oneshot((ich_func_t)bus_generic_attach, dev); 132125f5c5bSEmmanuel Vadot 133125f5c5bSEmmanuel Vadot return (0); 134125f5c5bSEmmanuel Vadot 135125f5c5bSEmmanuel Vadot fail1: 136125f5c5bSEmmanuel Vadot clk_disable(clock); 137125f5c5bSEmmanuel Vadot 138125f5c5bSEmmanuel Vadot fail: 139125f5c5bSEmmanuel Vadot bus_release_resources(dev, iicoc_spec, &sc->mem_res); 140125f5c5bSEmmanuel Vadot mtx_destroy(&sc->sc_mtx); 141125f5c5bSEmmanuel Vadot return (error); 142125f5c5bSEmmanuel Vadot } 143125f5c5bSEmmanuel Vadot 144125f5c5bSEmmanuel Vadot static int 145125f5c5bSEmmanuel Vadot iicoc_probe(device_t dev) 146125f5c5bSEmmanuel Vadot { 147125f5c5bSEmmanuel Vadot 148125f5c5bSEmmanuel Vadot if (!ofw_bus_status_okay(dev)) 149125f5c5bSEmmanuel Vadot return (ENXIO); 150125f5c5bSEmmanuel Vadot 151125f5c5bSEmmanuel Vadot if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0) 152125f5c5bSEmmanuel Vadot return (ENXIO); 153125f5c5bSEmmanuel Vadot 154125f5c5bSEmmanuel Vadot device_set_desc(dev, "OpenCores I2C master controller"); 155125f5c5bSEmmanuel Vadot 156125f5c5bSEmmanuel Vadot return (BUS_PROBE_DEFAULT); 157125f5c5bSEmmanuel Vadot } 158125f5c5bSEmmanuel Vadot 159125f5c5bSEmmanuel Vadot static device_method_t iicoc_methods[] = { 160125f5c5bSEmmanuel Vadot /* device interface */ 161125f5c5bSEmmanuel Vadot DEVMETHOD(device_probe, iicoc_probe), 162125f5c5bSEmmanuel Vadot DEVMETHOD(device_attach, iicoc_attach), 163125f5c5bSEmmanuel Vadot 164125f5c5bSEmmanuel Vadot /* ofw interface */ 165125f5c5bSEmmanuel Vadot DEVMETHOD(ofw_bus_get_node, iicoc_get_node), 166125f5c5bSEmmanuel Vadot 167125f5c5bSEmmanuel Vadot /* iicbus interface */ 168125f5c5bSEmmanuel Vadot DEVMETHOD(iicbus_callback, iicbus_null_callback), 169125f5c5bSEmmanuel Vadot DEVMETHOD(iicbus_repeated_start, iicoc_iicbus_repeated_start), 170125f5c5bSEmmanuel Vadot DEVMETHOD(iicbus_start, iicoc_iicbus_start), 171125f5c5bSEmmanuel Vadot DEVMETHOD(iicbus_stop, iicoc_iicbus_stop), 172125f5c5bSEmmanuel Vadot DEVMETHOD(iicbus_reset, iicoc_iicbus_reset), 173125f5c5bSEmmanuel Vadot DEVMETHOD(iicbus_write, iicoc_iicbus_write), 174125f5c5bSEmmanuel Vadot DEVMETHOD(iicbus_read, iicoc_iicbus_read), 175125f5c5bSEmmanuel Vadot DEVMETHOD(iicbus_transfer, iicbus_transfer_gen), 176125f5c5bSEmmanuel Vadot 177125f5c5bSEmmanuel Vadot DEVMETHOD_END 178125f5c5bSEmmanuel Vadot }; 179125f5c5bSEmmanuel Vadot 180125f5c5bSEmmanuel Vadot static driver_t iicoc_driver = { 181125f5c5bSEmmanuel Vadot "iicoc", 182125f5c5bSEmmanuel Vadot iicoc_methods, 183125f5c5bSEmmanuel Vadot sizeof(struct iicoc_softc), 184125f5c5bSEmmanuel Vadot }; 185125f5c5bSEmmanuel Vadot 186125f5c5bSEmmanuel Vadot SIMPLEBUS_PNP_INFO(compat_data); 187125f5c5bSEmmanuel Vadot DRIVER_MODULE(iicoc, simplebus, iicoc_driver, 0, 0); 188125f5c5bSEmmanuel Vadot DRIVER_MODULE(ofw_iicbus, iicoc, ofw_iicbus_driver, 0, 0); 189125f5c5bSEmmanuel Vadot MODULE_DEPEND(iicoc, iicbus, 1, 1, 1); 190125f5c5bSEmmanuel Vadot MODULE_DEPEND(iicoc, ofw_iicbus, 1, 1, 1); 191