xref: /freebsd-src/sys/dev/iicbus/controller/opencores/iicoc_fdt.c (revision e716630d4cf89e69ec3f675ebfceee09f1a85e05)
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/cdefs.h>
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/limits.h>
37 #include <sys/module.h>
38 #include <sys/bus.h>
39 #include <sys/lock.h>
40 #include <sys/mutex.h>
41 #include <sys/rman.h>
42 
43 #include <dev/extres/clk/clk.h>
44 
45 #include <dev/iicbus/iicbus.h>
46 #include <dev/iicbus/iiconf.h>
47 
48 #include <dev/ofw/ofw_bus.h>
49 #include <dev/ofw/ofw_bus_subr.h>
50 
51 #include "iicbus_if.h"
52 #include "iicoc.h"
53 
54 static struct ofw_compat_data compat_data[] = {
55 	{ "opencores,i2c-ocores",	1 },
56 	{ "sifive,fu740-c000-i2c",	1 },
57 	{ "sifive,fu540-c000-i2c",	1 },
58 	{ "sifive,i2c0",		1 },
59 	{ NULL,				0 }
60 };
61 
62 static struct resource_spec iicoc_spec[] = {
63 	{ SYS_RES_MEMORY, 0, RF_ACTIVE },
64 	RESOURCE_SPEC_END
65 };
66 
67 static phandle_t
68 iicoc_get_node(device_t bus, device_t dev)
69 {
70 
71 	/* Share controller node with iicbus device. */
72 	return (ofw_bus_get_node(bus));
73 }
74 
75 static int
76 iicoc_attach(device_t dev)
77 {
78 	struct iicoc_softc *sc;
79 	phandle_t node;
80 	clk_t clock;
81 	uint64_t clockfreq;
82 	int error;
83 
84 	sc = device_get_softc(dev);
85 	sc->dev = dev;
86 
87 	mtx_init(&sc->sc_mtx, "iicoc", "iicoc", MTX_DEF);
88 
89 	error = bus_alloc_resources(dev, iicoc_spec, &sc->mem_res);
90 	if (error) {
91 		device_printf(dev, "Could not allocate bus resource.\n");
92 		goto fail;
93 	}
94 
95 	node = ofw_bus_get_node(dev);
96 	sc->reg_shift = 0;
97 	OF_getencprop(node, "reg-shift", &sc->reg_shift,
98 	    sizeof(sc->reg_shift));
99 
100 	error = clk_get_by_ofw_index(dev, 0, 0, &clock);
101 	if (error) {
102 		device_printf(dev, "Couldn't get clock\n");
103 		goto fail;
104 	}
105 	error = clk_enable(clock);
106 	if (error) {
107 		device_printf(dev, "Couldn't enable clock\n");
108 		goto fail1;
109 	}
110 	error = clk_get_freq(clock, &clockfreq);
111 	if (error) {
112 		device_printf(dev, "Couldn't get clock frequency\n");
113 		goto fail1;
114 	}
115 	if (clockfreq > UINT_MAX) {
116 		device_printf(dev, "Unsupported clock frequency\n");
117 		goto fail1;
118 	}
119 	sc->clockfreq = (u_int)clockfreq;
120 	sc->i2cfreq = XLP_I2C_FREQ;
121 	iicoc_init(dev);
122 
123 	sc->iicbus = device_add_child(dev, "iicbus", -1);
124 	if (sc->iicbus == NULL) {
125 		device_printf(dev, "Could not allocate iicbus instance.\n");
126 		error = ENXIO;
127 		goto fail1;
128 	}
129 
130 	/* Probe and attach the iicbus when interrupts are available. */
131 	config_intrhook_oneshot((ich_func_t)bus_generic_attach, dev);
132 
133 	return (0);
134 
135 fail1:
136 	clk_disable(clock);
137 
138 fail:
139 	bus_release_resources(dev, iicoc_spec, &sc->mem_res);
140 	mtx_destroy(&sc->sc_mtx);
141 	return (error);
142 }
143 
144 static int
145 iicoc_probe(device_t dev)
146 {
147 
148 	if (!ofw_bus_status_okay(dev))
149 		return (ENXIO);
150 
151 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
152 		return (ENXIO);
153 
154 	device_set_desc(dev, "OpenCores I2C master controller");
155 
156 	return (BUS_PROBE_DEFAULT);
157 }
158 
159 static device_method_t iicoc_methods[] = {
160 	/* device interface */
161 	DEVMETHOD(device_probe, iicoc_probe),
162 	DEVMETHOD(device_attach, iicoc_attach),
163 
164 	/* ofw interface */
165 	DEVMETHOD(ofw_bus_get_node, iicoc_get_node),
166 
167 	/* iicbus interface */
168 	DEVMETHOD(iicbus_callback, iicbus_null_callback),
169 	DEVMETHOD(iicbus_repeated_start, iicoc_iicbus_repeated_start),
170 	DEVMETHOD(iicbus_start, iicoc_iicbus_start),
171 	DEVMETHOD(iicbus_stop, iicoc_iicbus_stop),
172 	DEVMETHOD(iicbus_reset, iicoc_iicbus_reset),
173 	DEVMETHOD(iicbus_write, iicoc_iicbus_write),
174 	DEVMETHOD(iicbus_read, iicoc_iicbus_read),
175 	DEVMETHOD(iicbus_transfer, iicbus_transfer_gen),
176 
177 	DEVMETHOD_END
178 };
179 
180 static driver_t iicoc_driver = {
181 	"iicoc",
182 	iicoc_methods,
183 	sizeof(struct iicoc_softc),
184 };
185 
186 SIMPLEBUS_PNP_INFO(compat_data);
187 DRIVER_MODULE(iicoc, simplebus, iicoc_driver, 0, 0);
188 DRIVER_MODULE(ofw_iicbus, iicoc, ofw_iicbus_driver, 0, 0);
189 MODULE_DEPEND(iicoc, iicbus, 1, 1, 1);
190 MODULE_DEPEND(iicoc, ofw_iicbus, 1, 1, 1);
191