2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2019 Axiado Corporation.
7 * This software was developed in part by Philip Paeps under contract for
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
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.
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.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/limits.h>
39 #include <sys/module.h>
42 #include <sys/mutex.h>
45 #include <dev/extres/clk/clk.h>
47 #include <dev/iicbus/iicbus.h>
48 #include <dev/iicbus/iiconf.h>
50 #include <dev/ofw/ofw_bus.h>
51 #include <dev/ofw/ofw_bus_subr.h>
53 #include "iicbus_if.h"
56 static struct ofw_compat_data compat_data[] = {
57 { "opencores,i2c-ocores", 1 },
61 static struct resource_spec iicoc_spec[] = {
62 { SYS_RES_MEMORY, 0, RF_ACTIVE },
67 iicoc_get_node(device_t bus, device_t dev)
70 /* Share controller node with iicbus device. */
71 return (ofw_bus_get_node(bus));
75 iicoc_attach(device_t dev)
77 struct iicoc_softc *sc;
83 sc = device_get_softc(dev);
86 mtx_init(&sc->sc_mtx, "iicoc", "iicoc", MTX_DEF);
88 error = bus_alloc_resources(dev, iicoc_spec, &sc->mem_res);
90 device_printf(dev, "Could not allocate bus resource.\n");
94 node = ofw_bus_get_node(dev);
96 OF_getencprop(node, "reg-shift", &sc->reg_shift,
97 sizeof(sc->reg_shift));
99 error = clk_get_by_ofw_index(dev, 0, 0, &clock);
101 device_printf(dev, "Couldn't get clock\n");
104 error = clk_enable(clock);
106 device_printf(dev, "Couldn't enable clock\n");
109 error = clk_get_freq(clock, &clockfreq);
111 device_printf(dev, "Couldn't get clock frequency\n");
114 if (clockfreq > UINT_MAX) {
115 device_printf(dev, "Unsupported clock frequency\n");
118 sc->clockfreq = (u_int)clockfreq;
119 sc->i2cfreq = XLP_I2C_FREQ;
122 sc->iicbus = device_add_child(dev, "iicbus", -1);
123 if (sc->iicbus == NULL) {
124 device_printf(dev, "Could not allocate iicbus instance.\n");
129 /* Probe and attach the iicbus when interrupts are available. */
130 config_intrhook_oneshot((ich_func_t)bus_generic_attach, dev);
138 bus_release_resources(dev, iicoc_spec, &sc->mem_res);
139 mtx_destroy(&sc->sc_mtx);
144 iicoc_probe(device_t dev)
147 if (!ofw_bus_status_okay(dev))
150 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
153 device_set_desc(dev, "OpenCores I2C master controller");
155 return (BUS_PROBE_DEFAULT);
158 static device_method_t iicoc_methods[] = {
159 /* device interface */
160 DEVMETHOD(device_probe, iicoc_probe),
161 DEVMETHOD(device_attach, iicoc_attach),
164 DEVMETHOD(ofw_bus_get_node, iicoc_get_node),
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),
179 static driver_t iicoc_driver = {
182 sizeof(struct iicoc_softc),
185 DRIVER_MODULE(iicoc, simplebus, iicoc_driver, iicoc_devclass, 0, 0);
186 DRIVER_MODULE(ofw_iicbus, iicoc, ofw_iicbus_driver, ofw_iicbus_devclass, 0, 0);
187 MODULE_DEPEND(iicoc, iicbus, 1, 1, 1);
188 MODULE_DEPEND(iicoc, ofw_iicbus, 1, 1, 1);