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>
41 #include <machine/bus.h>
42 #include <dev/fdt/simplebus.h>
44 #include <dev/extres/clk/clk_div.h>
45 #include <dev/ofw/ofw_bus.h>
46 #include <dev/ofw/ofw_bus_subr.h>
48 #include "clock_common.h"
51 #define DPRINTF(dev, msg...) device_printf(dev, msg)
53 #define DPRINTF(dev, msg...)
57 * Devicetree description
58 * Documentation/devicetree/bindings/clock/ti/divider.txt
61 struct ti_divider_softc {
64 struct clk_div_def div_def;
66 struct clock_cell_info clock_cell;
67 struct clkdom *clkdom;
70 static int ti_divider_probe(device_t dev);
71 static int ti_divider_attach(device_t dev);
72 static int ti_divider_detach(device_t dev);
74 #define TI_DIVIDER_CLOCK 2
75 #define TI_COMPOSITE_DIVIDER_CLOCK 1
76 #define TI_DIVIDER_END 0
78 static struct ofw_compat_data compat_data[] = {
79 { "ti,divider-clock", TI_DIVIDER_CLOCK },
80 { "ti,composite-divider-clock", TI_COMPOSITE_DIVIDER_CLOCK },
81 { NULL, TI_DIVIDER_END }
85 register_clk(struct ti_divider_softc *sc) {
88 sc->clkdom = clkdom_create(sc->sc_dev);
89 if (sc->clkdom == NULL) {
90 DPRINTF(sc->sc_dev, "Failed to create clkdom\n");
94 err = clknode_div_register(sc->clkdom, &sc->div_def);
96 DPRINTF(sc->sc_dev, "clknode_div_register failed %x\n", err);
100 err = clkdom_finit(sc->clkdom);
102 DPRINTF(sc->sc_dev, "Clk domain finit fails %x.\n", err);
110 ti_divider_probe(device_t dev)
112 if (!ofw_bus_status_okay(dev))
115 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
118 device_set_desc(dev, "TI Divider Clock");
120 return (BUS_PROBE_DEFAULT);
124 ti_divider_attach(device_t dev)
126 struct ti_divider_softc *sc;
132 sc = device_get_softc(dev);
134 node = ofw_bus_get_node(dev);
136 /* Grab the content of reg properties */
137 OF_getencprop(node, "reg", &value, sizeof(value));
138 sc->div_def.offset = value;
140 if (OF_hasprop(node, "ti,bit-shift")) {
141 OF_getencprop(node, "ti,bit-shift", &value, sizeof(value));
142 sc->div_def.i_shift = value;
145 if (OF_hasprop(node, "ti,index-starts-at-one")) {
146 sc->div_def.div_flags = CLK_DIV_ZERO_BASED;
149 if (OF_hasprop(node, "ti,index-power-of-two")) {
151 device_printf(sc->sc_dev, "ti,index-power-of-two - Not implemented\n");
152 /* remember to update i_width a few lines below */
154 if (OF_hasprop(node, "ti,max-div")) {
155 OF_getencprop(node, "ti,max-div", &value, sizeof(value));
159 if (OF_hasprop(node, "clock-output-names"))
160 device_printf(sc->sc_dev, "clock-output-names\n");
161 if (OF_hasprop(node, "ti,dividers"))
162 device_printf(sc->sc_dev, "ti,dividers\n");
163 if (OF_hasprop(node, "ti,min-div"))
164 device_printf(sc->sc_dev, "ti,min-div - Not implemented\n");
166 if (OF_hasprop(node, "ti,autoidle-shift"))
167 device_printf(sc->sc_dev, "ti,autoidle-shift - Not implemented\n");
168 if (OF_hasprop(node, "ti,set-rate-parent"))
169 device_printf(sc->sc_dev, "ti,set-rate-parent - Not implemented\n");
170 if (OF_hasprop(node, "ti,latch-bit"))
171 device_printf(sc->sc_dev, "ti,latch-bit - Not implemented\n");
173 /* Figure out the width from ti_max_div */
174 if (sc->div_def.div_flags)
175 sc->div_def.i_width = fls(ti_max_div-1);
177 sc->div_def.i_width = fls(ti_max_div);
179 DPRINTF(sc->sc_dev, "div_def.i_width %x\n", sc->div_def.i_width);
181 read_clock_cells(sc->sc_dev, &sc->clock_cell);
183 create_clkdef(sc->sc_dev, &sc->clock_cell, &sc->div_def.clkdef);
185 err = find_parent_clock_names(sc->sc_dev, &sc->clock_cell, &sc->div_def.clkdef);
188 /* free_clkdef will be called in ti_divider_new_pass */
189 DPRINTF(sc->sc_dev, "find_parent_clock_names failed\n");
190 return (bus_generic_attach(sc->sc_dev));
193 err = register_clk(sc);
196 /* free_clkdef will be called in ti_divider_new_pass */
197 DPRINTF(sc->sc_dev, "register_clk failed\n");
198 return (bus_generic_attach(sc->sc_dev));
201 sc->attach_done = true;
203 free_clkdef(&sc->div_def.clkdef);
205 return (bus_generic_attach(sc->sc_dev));
209 ti_divider_detach(device_t dev)
215 ti_divider_new_pass(device_t dev)
217 struct ti_divider_softc *sc;
220 sc = device_get_softc(dev);
222 if (sc->attach_done) {
226 err = find_parent_clock_names(sc->sc_dev, &sc->clock_cell, &sc->div_def.clkdef);
228 /* free_clkdef will be called in a later call to ti_divider_new_pass */
229 DPRINTF(sc->sc_dev, "new_pass find_parent_clock_names failed\n");
233 err = register_clk(sc);
235 /* free_clkdef will be called in a later call to ti_divider_new_pass */
236 DPRINTF(sc->sc_dev, "new_pass register_clk failed\n");
240 sc->attach_done = true;
242 free_clkdef(&sc->div_def.clkdef);
245 static device_method_t ti_divider_methods[] = {
246 /* Device interface */
247 DEVMETHOD(device_probe, ti_divider_probe),
248 DEVMETHOD(device_attach, ti_divider_attach),
249 DEVMETHOD(device_detach, ti_divider_detach),
252 DEVMETHOD(bus_new_pass, ti_divider_new_pass),
257 DEFINE_CLASS_0(ti_divider, ti_divider_driver, ti_divider_methods,
258 sizeof(struct ti_divider_softc));
260 static devclass_t ti_divider_devclass;
262 EARLY_DRIVER_MODULE(ti_divider, simplebus, ti_divider_driver,
263 ti_divider_devclass, 0, 0, BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
264 MODULE_VERSION(ti_divider, 1);