ti_div_clock.c revision 1.1.10.2 1 1.1.10.2 martin /* $NetBSD: ti_div_clock.c,v 1.1.10.2 2020/04/13 08:03:38 martin Exp $ */
2 1.1.10.2 martin
3 1.1.10.2 martin /*-
4 1.1.10.2 martin * Copyright (c) 2019 Jared McNeill <jmcneill (at) invisible.ca>
5 1.1.10.2 martin * All rights reserved.
6 1.1.10.2 martin *
7 1.1.10.2 martin * Redistribution and use in source and binary forms, with or without
8 1.1.10.2 martin * modification, are permitted provided that the following conditions
9 1.1.10.2 martin * are met:
10 1.1.10.2 martin * 1. Redistributions of source code must retain the above copyright
11 1.1.10.2 martin * notice, this list of conditions and the following disclaimer.
12 1.1.10.2 martin * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.10.2 martin * notice, this list of conditions and the following disclaimer in the
14 1.1.10.2 martin * documentation and/or other materials provided with the distribution.
15 1.1.10.2 martin *
16 1.1.10.2 martin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1.10.2 martin * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1.10.2 martin * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1.10.2 martin * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1.10.2 martin * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.1.10.2 martin * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.1.10.2 martin * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.1.10.2 martin * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.1.10.2 martin * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1.10.2 martin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1.10.2 martin * SUCH DAMAGE.
27 1.1.10.2 martin */
28 1.1.10.2 martin
29 1.1.10.2 martin #include <sys/cdefs.h>
30 1.1.10.2 martin __KERNEL_RCSID(0, "$NetBSD: ti_div_clock.c,v 1.1.10.2 2020/04/13 08:03:38 martin Exp $");
31 1.1.10.2 martin
32 1.1.10.2 martin #include <sys/param.h>
33 1.1.10.2 martin #include <sys/systm.h>
34 1.1.10.2 martin #include <sys/device.h>
35 1.1.10.2 martin #include <sys/kmem.h>
36 1.1.10.2 martin #include <sys/bus.h>
37 1.1.10.2 martin
38 1.1.10.2 martin #include <dev/clk/clk_backend.h>
39 1.1.10.2 martin
40 1.1.10.2 martin #include <dev/fdt/fdtvar.h>
41 1.1.10.2 martin
42 1.1.10.2 martin static const char * const compatible[] = {
43 1.1.10.2 martin "ti,divider-clock",
44 1.1.10.2 martin NULL
45 1.1.10.2 martin };
46 1.1.10.2 martin
47 1.1.10.2 martin static int ti_div_clock_match(device_t, cfdata_t, void *);
48 1.1.10.2 martin static void ti_div_clock_attach(device_t, device_t, void *);
49 1.1.10.2 martin
50 1.1.10.2 martin static struct clk *ti_div_clock_decode(device_t, int, const void *, size_t);
51 1.1.10.2 martin
52 1.1.10.2 martin static const struct fdtbus_clock_controller_func ti_div_clock_fdt_funcs = {
53 1.1.10.2 martin .decode = ti_div_clock_decode
54 1.1.10.2 martin };
55 1.1.10.2 martin
56 1.1.10.2 martin static struct clk *ti_div_clock_get(void *, const char *);
57 1.1.10.2 martin static void ti_div_clock_put(void *, struct clk *);
58 1.1.10.2 martin static u_int ti_div_clock_get_rate(void *, struct clk *);
59 1.1.10.2 martin static struct clk *ti_div_clock_get_parent(void *, struct clk *);
60 1.1.10.2 martin
61 1.1.10.2 martin static const struct clk_funcs ti_div_clock_clk_funcs = {
62 1.1.10.2 martin .get = ti_div_clock_get,
63 1.1.10.2 martin .put = ti_div_clock_put,
64 1.1.10.2 martin .get_rate = ti_div_clock_get_rate,
65 1.1.10.2 martin .get_parent = ti_div_clock_get_parent,
66 1.1.10.2 martin };
67 1.1.10.2 martin
68 1.1.10.2 martin struct ti_div_clock_softc {
69 1.1.10.2 martin device_t sc_dev;
70 1.1.10.2 martin int sc_phandle;
71 1.1.10.2 martin bus_space_tag_t sc_bst;
72 1.1.10.2 martin bus_space_handle_t sc_bsh;
73 1.1.10.2 martin
74 1.1.10.2 martin struct clk_domain sc_clkdom;
75 1.1.10.2 martin struct clk sc_clk;
76 1.1.10.2 martin };
77 1.1.10.2 martin
78 1.1.10.2 martin #define RD4(sc) \
79 1.1.10.2 martin bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, 0)
80 1.1.10.2 martin #define WR4(sc, val) \
81 1.1.10.2 martin bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, 0, (val))
82 1.1.10.2 martin
83 1.1.10.2 martin CFATTACH_DECL_NEW(ti_div_clock, sizeof(struct ti_div_clock_softc),
84 1.1.10.2 martin ti_div_clock_match, ti_div_clock_attach, NULL, NULL);
85 1.1.10.2 martin
86 1.1.10.2 martin static int
87 1.1.10.2 martin ti_div_clock_match(device_t parent, cfdata_t cf, void *aux)
88 1.1.10.2 martin {
89 1.1.10.2 martin const struct fdt_attach_args *faa = aux;
90 1.1.10.2 martin
91 1.1.10.2 martin return of_match_compatible(faa->faa_phandle, compatible);
92 1.1.10.2 martin }
93 1.1.10.2 martin
94 1.1.10.2 martin static void
95 1.1.10.2 martin ti_div_clock_attach(device_t parent, device_t self, void *aux)
96 1.1.10.2 martin {
97 1.1.10.2 martin struct ti_div_clock_softc * const sc = device_private(self);
98 1.1.10.2 martin const struct fdt_attach_args *faa = aux;
99 1.1.10.2 martin const int phandle = faa->faa_phandle;
100 1.1.10.2 martin bus_addr_t addr, base_addr;
101 1.1.10.2 martin
102 1.1.10.2 martin const int prcm_phandle = OF_parent(OF_parent(phandle));
103 1.1.10.2 martin if (fdtbus_get_reg(phandle, 0, &addr, NULL) != 0 ||
104 1.1.10.2 martin fdtbus_get_reg(prcm_phandle, 0, &base_addr, NULL) != 0) {
105 1.1.10.2 martin aprint_error(": couldn't get registers\n");
106 1.1.10.2 martin return;
107 1.1.10.2 martin }
108 1.1.10.2 martin
109 1.1.10.2 martin sc->sc_dev = self;
110 1.1.10.2 martin sc->sc_phandle = phandle;
111 1.1.10.2 martin sc->sc_bst = faa->faa_bst;
112 1.1.10.2 martin if (bus_space_map(sc->sc_bst, base_addr + addr, 4, 0, &sc->sc_bsh) != 0) {
113 1.1.10.2 martin aprint_error(": couldn't map registers\n");
114 1.1.10.2 martin return;
115 1.1.10.2 martin }
116 1.1.10.2 martin
117 1.1.10.2 martin sc->sc_clkdom.name = device_xname(self);
118 1.1.10.2 martin sc->sc_clkdom.funcs = &ti_div_clock_clk_funcs;
119 1.1.10.2 martin sc->sc_clkdom.priv = sc;
120 1.1.10.2 martin
121 1.1.10.2 martin sc->sc_clk.domain = &sc->sc_clkdom;
122 1.1.10.2 martin sc->sc_clk.name = kmem_asprintf("%s", faa->faa_name);
123 1.1.10.2 martin clk_attach(&sc->sc_clk);
124 1.1.10.2 martin
125 1.1.10.2 martin aprint_naive("\n");
126 1.1.10.2 martin aprint_normal(": TI divider clock (%s)\n", sc->sc_clk.name);
127 1.1.10.2 martin
128 1.1.10.2 martin fdtbus_register_clock_controller(self, phandle, &ti_div_clock_fdt_funcs);
129 1.1.10.2 martin }
130 1.1.10.2 martin
131 1.1.10.2 martin static struct clk *
132 1.1.10.2 martin ti_div_clock_decode(device_t dev, int cc_phandle, const void *data,
133 1.1.10.2 martin size_t len)
134 1.1.10.2 martin {
135 1.1.10.2 martin struct ti_div_clock_softc * const sc = device_private(dev);
136 1.1.10.2 martin
137 1.1.10.2 martin return &sc->sc_clk;
138 1.1.10.2 martin }
139 1.1.10.2 martin
140 1.1.10.2 martin static struct clk *
141 1.1.10.2 martin ti_div_clock_get(void *priv, const char *name)
142 1.1.10.2 martin {
143 1.1.10.2 martin struct ti_div_clock_softc * const sc = priv;
144 1.1.10.2 martin
145 1.1.10.2 martin return &sc->sc_clk;
146 1.1.10.2 martin }
147 1.1.10.2 martin
148 1.1.10.2 martin static void
149 1.1.10.2 martin ti_div_clock_put(void *priv, struct clk *clk)
150 1.1.10.2 martin {
151 1.1.10.2 martin }
152 1.1.10.2 martin
153 1.1.10.2 martin static u_int
154 1.1.10.2 martin ti_div_clock_get_rate(void *priv, struct clk *clk)
155 1.1.10.2 martin {
156 1.1.10.2 martin struct ti_div_clock_softc * const sc = priv;
157 1.1.10.2 martin struct clk *clk_parent = clk_get_parent(clk);
158 1.1.10.2 martin uint64_t parent_rate;
159 1.1.10.2 martin uint32_t val, max_val, mask;
160 1.1.10.2 martin u_int div, max_div, bit_shift;
161 1.1.10.2 martin
162 1.1.10.2 martin if (clk_parent == NULL)
163 1.1.10.2 martin return 0;
164 1.1.10.2 martin
165 1.1.10.2 martin if (of_hasprop(sc->sc_phandle, "ti,index-power-of-two"))
166 1.1.10.2 martin return EINVAL; /* not supported yet */
167 1.1.10.2 martin
168 1.1.10.2 martin const int start_index = of_hasprop(sc->sc_phandle, "ti,index-starts-at-one") ? 1 : 0;
169 1.1.10.2 martin
170 1.1.10.2 martin if (of_getprop_uint32(sc->sc_phandle, "ti,bit-shift", &bit_shift) != 0)
171 1.1.10.2 martin bit_shift = 0;
172 1.1.10.2 martin
173 1.1.10.2 martin if (of_getprop_uint32(sc->sc_phandle, "ti,max-div", &max_div) == 0) {
174 1.1.10.2 martin max_val = start_index + max_div;
175 1.1.10.2 martin while (!powerof2(max_val))
176 1.1.10.2 martin max_val++;
177 1.1.10.2 martin mask = (max_val - 1) << bit_shift;
178 1.1.10.2 martin } else {
179 1.1.10.2 martin /*
180 1.1.10.2 martin * The bindings allow for this, but it is not yet supported
181 1.1.10.2 martin * by this driver.
182 1.1.10.2 martin */
183 1.1.10.2 martin return EINVAL;
184 1.1.10.2 martin }
185 1.1.10.2 martin
186 1.1.10.2 martin val = RD4(sc);
187 1.1.10.2 martin div = __SHIFTOUT(val, mask) + (start_index ? 0 : 1);
188 1.1.10.2 martin if (div == 0)
189 1.1.10.2 martin return EINVAL;
190 1.1.10.2 martin
191 1.1.10.2 martin parent_rate = clk_get_rate(clk_parent);
192 1.1.10.2 martin
193 1.1.10.2 martin return (u_int)(parent_rate / div);
194 1.1.10.2 martin }
195 1.1.10.2 martin
196 1.1.10.2 martin static struct clk *
197 1.1.10.2 martin ti_div_clock_get_parent(void *priv, struct clk *clk)
198 1.1.10.2 martin {
199 1.1.10.2 martin struct ti_div_clock_softc * const sc = priv;
200 1.1.10.2 martin
201 1.1.10.2 martin return fdtbus_clock_get_index(sc->sc_phandle, 0);
202 1.1.10.2 martin }
203