2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2020 Oskar Holmlund <oskar.holmlund@ohdata.se>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <sys/systm.h>
39 #include <sys/libkern.h>
40 #include <sys/types.h>
41 #include <sys/malloc.h>
43 #include <machine/bus.h>
44 #include <dev/fdt/simplebus.h>
46 #include <dev/extres/clk/clk_mux.h>
47 #include <dev/ofw/ofw_bus.h>
48 #include <dev/ofw/ofw_bus_subr.h>
50 #include "clock_common.h"
53 #define DPRINTF(dev, msg...) device_printf(dev, msg)
55 #define DPRINTF(dev, msg...)
59 * Devicetree description
60 * Documentation/devicetree/bindings/clock/ti/mux.txt
67 struct clk_mux_def mux_def;
68 struct clock_cell_info clock_cell;
69 struct clkdom *clkdom;
72 static int ti_mux_probe(device_t dev);
73 static int ti_mux_attach(device_t dev);
74 static int ti_mux_detach(device_t dev);
76 #define TI_MUX_CLOCK 2
77 #define TI_COMPOSITE_MUX_CLOCK 1
80 static struct ofw_compat_data compat_data[] = {
81 { "ti,mux-clock", TI_MUX_CLOCK },
82 { "ti,composite-mux-clock", TI_COMPOSITE_MUX_CLOCK },
87 ti_mux_probe(device_t dev)
89 if (!ofw_bus_status_okay(dev))
92 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
95 device_set_desc(dev, "TI Mux Clock");
97 return (BUS_PROBE_DEFAULT);
101 register_clk(struct ti_mux_softc *sc) {
104 sc->clkdom = clkdom_create(sc->sc_dev);
105 if (sc->clkdom == NULL) {
106 DPRINTF(sc->sc_dev, "Failed to create clkdom\n");
110 err = clknode_mux_register(sc->clkdom, &sc->mux_def);
112 DPRINTF(sc->sc_dev, "clknode_mux_register failed %x\n", err);
116 err = clkdom_finit(sc->clkdom);
118 DPRINTF(sc->sc_dev, "Clk domain finit fails %x.\n", err);
126 ti_mux_attach(device_t dev)
128 struct ti_mux_softc *sc;
133 sc = device_get_softc(dev);
135 node = ofw_bus_get_node(dev);
137 /* Grab the content of reg properties */
138 OF_getencprop(node, "reg", &value, sizeof(value));
139 sc->mux_def.offset = value;
141 if (OF_hasprop(node, "ti,bit-shift")) {
142 OF_getencprop(node, "ti,bit-shift", &value, sizeof(value));
143 sc->mux_def.shift = value;
144 DPRINTF(sc->sc_dev, "ti,bit-shift => shift %x\n", sc->mux_def.shift);
146 if (OF_hasprop(node, "ti,index-starts-at-one")) {
147 /* FIXME: Add support in dev/extres/clk */
148 /*sc->mux_def.mux_flags = ... */
149 device_printf(sc->sc_dev, "ti,index-starts-at-one - Not implemented\n");
152 if (OF_hasprop(node, "ti,set-rate-parent"))
153 device_printf(sc->sc_dev, "ti,set-rate-parent - Not implemented\n");
154 if (OF_hasprop(node, "ti,latch-bit"))
155 device_printf(sc->sc_dev, "ti,latch-bit - Not implemented\n");
157 read_clock_cells(sc->sc_dev, &sc->clock_cell);
159 create_clkdef(sc->sc_dev, &sc->clock_cell, &sc->mux_def.clkdef);
161 /* Figure out the width from ti_max_div */
162 if (sc->mux_def.mux_flags)
163 sc->mux_def.width = fls(sc->clock_cell.num_real_clocks-1);
165 sc->mux_def.width = fls(sc->clock_cell.num_real_clocks);
167 DPRINTF(sc->sc_dev, "sc->clock_cell.num_real_clocks %x def.width %x\n",
168 sc->clock_cell.num_real_clocks, sc->mux_def.width);
170 err = find_parent_clock_names(sc->sc_dev, &sc->clock_cell, &sc->mux_def.clkdef);
173 /* free_clkdef will be called in ti_mux_new_pass */
174 DPRINTF(sc->sc_dev, "find_parent_clock_names failed\n");
175 return (bus_generic_attach(sc->sc_dev));
178 err = register_clk(sc);
181 /* free_clkdef will be called in ti_mux_new_pass */
182 DPRINTF(sc->sc_dev, "register_clk failed\n");
183 return (bus_generic_attach(sc->sc_dev));
186 sc->attach_done = true;
188 free_clkdef(&sc->mux_def.clkdef);
190 return (bus_generic_attach(sc->sc_dev));
194 ti_mux_new_pass(device_t dev)
196 struct ti_mux_softc *sc;
199 sc = device_get_softc(dev);
201 if (sc->attach_done) {
205 err = find_parent_clock_names(sc->sc_dev, &sc->clock_cell, &sc->mux_def.clkdef);
207 /* free_clkdef will be called in later call to ti_mux_new_pass */
208 DPRINTF(sc->sc_dev, "ti_mux_new_pass find_parent_clock_names failed\n");
212 err = register_clk(sc);
214 /* free_clkdef will be called in later call to ti_mux_new_pass */
215 DPRINTF(sc->sc_dev, "ti_mux_new_pass register_clk failed\n");
219 sc->attach_done = true;
221 free_clkdef(&sc->mux_def.clkdef);
225 ti_mux_detach(device_t dev)
230 static device_method_t ti_mux_methods[] = {
231 /* Device interface */
232 DEVMETHOD(device_probe, ti_mux_probe),
233 DEVMETHOD(device_attach, ti_mux_attach),
234 DEVMETHOD(device_detach, ti_mux_detach),
237 DEVMETHOD(bus_new_pass, ti_mux_new_pass),
242 DEFINE_CLASS_0(ti_mux, ti_mux_driver, ti_mux_methods,
243 sizeof(struct ti_mux_softc));
245 static devclass_t ti_mux_devclass;
247 EARLY_DRIVER_MODULE(ti_mux, simplebus, ti_mux_driver,
248 ti_mux_devclass, 0, 0, BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
249 MODULE_VERSION(ti_mux, 1);