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/param.h>
33125f5c5bSEmmanuel Vadot #include <sys/systm.h>
34125f5c5bSEmmanuel Vadot #include <sys/kernel.h>
35125f5c5bSEmmanuel Vadot #include <sys/limits.h>
36125f5c5bSEmmanuel Vadot #include <sys/module.h>
37125f5c5bSEmmanuel Vadot #include <sys/bus.h>
38125f5c5bSEmmanuel Vadot #include <sys/lock.h>
39125f5c5bSEmmanuel Vadot #include <sys/mutex.h>
40125f5c5bSEmmanuel Vadot #include <sys/rman.h>
41125f5c5bSEmmanuel Vadot
42be82b3a0SEmmanuel Vadot #include <dev/clk/clk.h>
43125f5c5bSEmmanuel Vadot
44125f5c5bSEmmanuel Vadot #include <dev/iicbus/iicbus.h>
45125f5c5bSEmmanuel Vadot #include <dev/iicbus/iiconf.h>
46125f5c5bSEmmanuel Vadot
47125f5c5bSEmmanuel Vadot #include <dev/ofw/ofw_bus.h>
48125f5c5bSEmmanuel Vadot #include <dev/ofw/ofw_bus_subr.h>
49125f5c5bSEmmanuel Vadot
50125f5c5bSEmmanuel Vadot #include "iicbus_if.h"
51125f5c5bSEmmanuel Vadot #include "iicoc.h"
52125f5c5bSEmmanuel Vadot
53125f5c5bSEmmanuel Vadot static struct ofw_compat_data compat_data[] = {
54125f5c5bSEmmanuel Vadot { "opencores,i2c-ocores", 1 },
55125f5c5bSEmmanuel Vadot { "sifive,fu740-c000-i2c", 1 },
56125f5c5bSEmmanuel Vadot { "sifive,fu540-c000-i2c", 1 },
57125f5c5bSEmmanuel Vadot { "sifive,i2c0", 1 },
58125f5c5bSEmmanuel Vadot { NULL, 0 }
59125f5c5bSEmmanuel Vadot };
60125f5c5bSEmmanuel Vadot
61125f5c5bSEmmanuel Vadot static struct resource_spec iicoc_spec[] = {
62125f5c5bSEmmanuel Vadot { SYS_RES_MEMORY, 0, RF_ACTIVE },
63125f5c5bSEmmanuel Vadot RESOURCE_SPEC_END
64125f5c5bSEmmanuel Vadot };
65125f5c5bSEmmanuel Vadot
66125f5c5bSEmmanuel Vadot static phandle_t
iicoc_get_node(device_t bus,device_t dev)67125f5c5bSEmmanuel Vadot iicoc_get_node(device_t bus, device_t dev)
68125f5c5bSEmmanuel Vadot {
69125f5c5bSEmmanuel Vadot
70125f5c5bSEmmanuel Vadot /* Share controller node with iicbus device. */
71125f5c5bSEmmanuel Vadot return (ofw_bus_get_node(bus));
72125f5c5bSEmmanuel Vadot }
73125f5c5bSEmmanuel Vadot
74125f5c5bSEmmanuel Vadot static int
iicoc_attach(device_t dev)75125f5c5bSEmmanuel Vadot iicoc_attach(device_t dev)
76125f5c5bSEmmanuel Vadot {
77125f5c5bSEmmanuel Vadot struct iicoc_softc *sc;
78125f5c5bSEmmanuel Vadot phandle_t node;
79125f5c5bSEmmanuel Vadot clk_t clock;
80125f5c5bSEmmanuel Vadot uint64_t clockfreq;
81125f5c5bSEmmanuel Vadot int error;
82125f5c5bSEmmanuel Vadot
83125f5c5bSEmmanuel Vadot sc = device_get_softc(dev);
84125f5c5bSEmmanuel Vadot sc->dev = dev;
85125f5c5bSEmmanuel Vadot
86125f5c5bSEmmanuel Vadot mtx_init(&sc->sc_mtx, "iicoc", "iicoc", MTX_DEF);
87125f5c5bSEmmanuel Vadot
88125f5c5bSEmmanuel Vadot error = bus_alloc_resources(dev, iicoc_spec, &sc->mem_res);
89125f5c5bSEmmanuel Vadot if (error) {
90125f5c5bSEmmanuel Vadot device_printf(dev, "Could not allocate bus resource.\n");
91125f5c5bSEmmanuel Vadot goto fail;
92125f5c5bSEmmanuel Vadot }
93125f5c5bSEmmanuel Vadot
94125f5c5bSEmmanuel Vadot node = ofw_bus_get_node(dev);
95125f5c5bSEmmanuel Vadot sc->reg_shift = 0;
96125f5c5bSEmmanuel Vadot OF_getencprop(node, "reg-shift", &sc->reg_shift,
97125f5c5bSEmmanuel Vadot sizeof(sc->reg_shift));
98125f5c5bSEmmanuel Vadot
99125f5c5bSEmmanuel Vadot error = clk_get_by_ofw_index(dev, 0, 0, &clock);
100125f5c5bSEmmanuel Vadot if (error) {
101125f5c5bSEmmanuel Vadot device_printf(dev, "Couldn't get clock\n");
102125f5c5bSEmmanuel Vadot goto fail;
103125f5c5bSEmmanuel Vadot }
104125f5c5bSEmmanuel Vadot error = clk_enable(clock);
105125f5c5bSEmmanuel Vadot if (error) {
106125f5c5bSEmmanuel Vadot device_printf(dev, "Couldn't enable clock\n");
107125f5c5bSEmmanuel Vadot goto fail1;
108125f5c5bSEmmanuel Vadot }
109125f5c5bSEmmanuel Vadot error = clk_get_freq(clock, &clockfreq);
110125f5c5bSEmmanuel Vadot if (error) {
111125f5c5bSEmmanuel Vadot device_printf(dev, "Couldn't get clock frequency\n");
112125f5c5bSEmmanuel Vadot goto fail1;
113125f5c5bSEmmanuel Vadot }
114125f5c5bSEmmanuel Vadot if (clockfreq > UINT_MAX) {
115125f5c5bSEmmanuel Vadot device_printf(dev, "Unsupported clock frequency\n");
116125f5c5bSEmmanuel Vadot goto fail1;
117125f5c5bSEmmanuel Vadot }
118125f5c5bSEmmanuel Vadot sc->clockfreq = (u_int)clockfreq;
119125f5c5bSEmmanuel Vadot sc->i2cfreq = XLP_I2C_FREQ;
120125f5c5bSEmmanuel Vadot iicoc_init(dev);
121125f5c5bSEmmanuel Vadot
122125f5c5bSEmmanuel Vadot sc->iicbus = device_add_child(dev, "iicbus", -1);
123125f5c5bSEmmanuel Vadot if (sc->iicbus == NULL) {
124125f5c5bSEmmanuel Vadot device_printf(dev, "Could not allocate iicbus instance.\n");
125125f5c5bSEmmanuel Vadot error = ENXIO;
126125f5c5bSEmmanuel Vadot goto fail1;
127125f5c5bSEmmanuel Vadot }
128125f5c5bSEmmanuel Vadot
129125f5c5bSEmmanuel Vadot /* Probe and attach the iicbus when interrupts are available. */
130125f5c5bSEmmanuel Vadot config_intrhook_oneshot((ich_func_t)bus_generic_attach, dev);
131125f5c5bSEmmanuel Vadot
132125f5c5bSEmmanuel Vadot return (0);
133125f5c5bSEmmanuel Vadot
134125f5c5bSEmmanuel Vadot fail1:
135125f5c5bSEmmanuel Vadot clk_disable(clock);
136125f5c5bSEmmanuel Vadot
137125f5c5bSEmmanuel Vadot fail:
138125f5c5bSEmmanuel Vadot bus_release_resources(dev, iicoc_spec, &sc->mem_res);
139125f5c5bSEmmanuel Vadot mtx_destroy(&sc->sc_mtx);
140125f5c5bSEmmanuel Vadot return (error);
141125f5c5bSEmmanuel Vadot }
142125f5c5bSEmmanuel Vadot
143125f5c5bSEmmanuel Vadot static int
iicoc_probe(device_t dev)144125f5c5bSEmmanuel Vadot iicoc_probe(device_t dev)
145125f5c5bSEmmanuel Vadot {
146125f5c5bSEmmanuel Vadot
147125f5c5bSEmmanuel Vadot if (!ofw_bus_status_okay(dev))
148125f5c5bSEmmanuel Vadot return (ENXIO);
149125f5c5bSEmmanuel Vadot
150125f5c5bSEmmanuel Vadot if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
151125f5c5bSEmmanuel Vadot return (ENXIO);
152125f5c5bSEmmanuel Vadot
153125f5c5bSEmmanuel Vadot device_set_desc(dev, "OpenCores I2C master controller");
154125f5c5bSEmmanuel Vadot
155125f5c5bSEmmanuel Vadot return (BUS_PROBE_DEFAULT);
156125f5c5bSEmmanuel Vadot }
157125f5c5bSEmmanuel Vadot
158125f5c5bSEmmanuel Vadot static device_method_t iicoc_methods[] = {
159125f5c5bSEmmanuel Vadot /* device interface */
160125f5c5bSEmmanuel Vadot DEVMETHOD(device_probe, iicoc_probe),
161125f5c5bSEmmanuel Vadot DEVMETHOD(device_attach, iicoc_attach),
162125f5c5bSEmmanuel Vadot
163125f5c5bSEmmanuel Vadot /* ofw interface */
164125f5c5bSEmmanuel Vadot DEVMETHOD(ofw_bus_get_node, iicoc_get_node),
165125f5c5bSEmmanuel Vadot
166125f5c5bSEmmanuel Vadot /* iicbus interface */
167125f5c5bSEmmanuel Vadot DEVMETHOD(iicbus_callback, iicbus_null_callback),
168125f5c5bSEmmanuel Vadot DEVMETHOD(iicbus_repeated_start, iicoc_iicbus_repeated_start),
169125f5c5bSEmmanuel Vadot DEVMETHOD(iicbus_start, iicoc_iicbus_start),
170125f5c5bSEmmanuel Vadot DEVMETHOD(iicbus_stop, iicoc_iicbus_stop),
171125f5c5bSEmmanuel Vadot DEVMETHOD(iicbus_reset, iicoc_iicbus_reset),
172125f5c5bSEmmanuel Vadot DEVMETHOD(iicbus_write, iicoc_iicbus_write),
173125f5c5bSEmmanuel Vadot DEVMETHOD(iicbus_read, iicoc_iicbus_read),
174125f5c5bSEmmanuel Vadot DEVMETHOD(iicbus_transfer, iicbus_transfer_gen),
175125f5c5bSEmmanuel Vadot
176125f5c5bSEmmanuel Vadot DEVMETHOD_END
177125f5c5bSEmmanuel Vadot };
178125f5c5bSEmmanuel Vadot
179125f5c5bSEmmanuel Vadot static driver_t iicoc_driver = {
180125f5c5bSEmmanuel Vadot "iicoc",
181125f5c5bSEmmanuel Vadot iicoc_methods,
182125f5c5bSEmmanuel Vadot sizeof(struct iicoc_softc),
183125f5c5bSEmmanuel Vadot };
184125f5c5bSEmmanuel Vadot
185125f5c5bSEmmanuel Vadot SIMPLEBUS_PNP_INFO(compat_data);
186125f5c5bSEmmanuel Vadot DRIVER_MODULE(iicoc, simplebus, iicoc_driver, 0, 0);
187125f5c5bSEmmanuel Vadot DRIVER_MODULE(ofw_iicbus, iicoc, ofw_iicbus_driver, 0, 0);
188125f5c5bSEmmanuel Vadot MODULE_DEPEND(iicoc, iicbus, 1, 1, 1);
189125f5c5bSEmmanuel Vadot MODULE_DEPEND(iicoc, ofw_iicbus, 1, 1, 1);
190