fixedfactorclock.c revision 1.2.2.1 1 1.2.2.1 christos /* $NetBSD: fixedfactorclock.c,v 1.2.2.1 2019/06/10 22:07:08 christos Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2017 Jared McNeill <jmcneill (at) invisible.ca>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
14 1.1 jmcneill * documentation and/or other materials provided with the distribution.
15 1.1 jmcneill *
16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.1 jmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.1 jmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.1 jmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.1 jmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 jmcneill * SUCH DAMAGE.
27 1.1 jmcneill */
28 1.1 jmcneill
29 1.1 jmcneill #include <sys/cdefs.h>
30 1.2.2.1 christos __KERNEL_RCSID(0, "$NetBSD: fixedfactorclock.c,v 1.2.2.1 2019/06/10 22:07:08 christos Exp $");
31 1.1 jmcneill
32 1.1 jmcneill #include <sys/param.h>
33 1.1 jmcneill #include <sys/systm.h>
34 1.1 jmcneill #include <sys/device.h>
35 1.1 jmcneill #include <sys/kmem.h>
36 1.1 jmcneill #include <sys/bus.h>
37 1.1 jmcneill
38 1.1 jmcneill #include <dev/clk/clk_backend.h>
39 1.1 jmcneill
40 1.1 jmcneill #include <dev/fdt/fdtvar.h>
41 1.1 jmcneill
42 1.1 jmcneill static int fixedfactorclock_match(device_t, cfdata_t, void *);
43 1.1 jmcneill static void fixedfactorclock_attach(device_t, device_t, void *);
44 1.1 jmcneill
45 1.2.2.1 christos static struct clk *fixedfactorclock_decode(device_t, int, const void *, size_t);
46 1.1 jmcneill
47 1.1 jmcneill static const struct fdtbus_clock_controller_func fixedfactorclock_fdt_funcs = {
48 1.1 jmcneill .decode = fixedfactorclock_decode
49 1.1 jmcneill };
50 1.1 jmcneill
51 1.1 jmcneill static struct clk *fixedfactorclock_get(void *, const char *);
52 1.1 jmcneill static void fixedfactorclock_put(void *, struct clk *);
53 1.1 jmcneill static u_int fixedfactorclock_get_rate(void *, struct clk *);
54 1.1 jmcneill
55 1.1 jmcneill static const struct clk_funcs fixedfactorclock_clk_funcs = {
56 1.1 jmcneill .get = fixedfactorclock_get,
57 1.1 jmcneill .put = fixedfactorclock_put,
58 1.1 jmcneill .get_rate = fixedfactorclock_get_rate,
59 1.1 jmcneill };
60 1.1 jmcneill
61 1.1 jmcneill struct fixedfactorclock_clk {
62 1.1 jmcneill struct clk base;
63 1.1 jmcneill
64 1.1 jmcneill u_int div;
65 1.1 jmcneill u_int mult;
66 1.1 jmcneill };
67 1.1 jmcneill
68 1.1 jmcneill struct fixedfactorclock_softc {
69 1.1 jmcneill device_t sc_dev;
70 1.1 jmcneill int sc_phandle;
71 1.1 jmcneill
72 1.1 jmcneill struct clk_domain sc_clkdom;
73 1.1 jmcneill struct fixedfactorclock_clk sc_clk;
74 1.1 jmcneill };
75 1.1 jmcneill
76 1.1 jmcneill CFATTACH_DECL_NEW(ffclock, sizeof(struct fixedfactorclock_softc),
77 1.1 jmcneill fixedfactorclock_match, fixedfactorclock_attach, NULL, NULL);
78 1.1 jmcneill
79 1.1 jmcneill static int
80 1.1 jmcneill fixedfactorclock_match(device_t parent, cfdata_t cf, void *aux)
81 1.1 jmcneill {
82 1.1 jmcneill const char * const compatible[] = { "fixed-factor-clock", NULL };
83 1.1 jmcneill const struct fdt_attach_args *faa = aux;
84 1.1 jmcneill
85 1.1 jmcneill return of_match_compatible(faa->faa_phandle, compatible);
86 1.1 jmcneill }
87 1.1 jmcneill
88 1.1 jmcneill static void
89 1.1 jmcneill fixedfactorclock_attach(device_t parent, device_t self, void *aux)
90 1.1 jmcneill {
91 1.1 jmcneill struct fixedfactorclock_softc * const sc = device_private(self);
92 1.1 jmcneill const struct fdt_attach_args *faa = aux;
93 1.1 jmcneill const int phandle = faa->faa_phandle;
94 1.1 jmcneill const char *name;
95 1.1 jmcneill
96 1.1 jmcneill sc->sc_dev = self;
97 1.1 jmcneill sc->sc_phandle = phandle;
98 1.2 jmcneill sc->sc_clkdom.name = device_xname(self);
99 1.1 jmcneill sc->sc_clkdom.funcs = &fixedfactorclock_clk_funcs;
100 1.1 jmcneill sc->sc_clkdom.priv = sc;
101 1.1 jmcneill
102 1.1 jmcneill of_getprop_uint32(phandle, "clock-div", &sc->sc_clk.div);
103 1.1 jmcneill of_getprop_uint32(phandle, "clock-mult", &sc->sc_clk.mult);
104 1.1 jmcneill
105 1.1 jmcneill if (sc->sc_clk.div == 0 || sc->sc_clk.mult == 0) {
106 1.1 jmcneill aprint_error(": invalid properties\n");
107 1.1 jmcneill return;
108 1.1 jmcneill }
109 1.1 jmcneill
110 1.1 jmcneill name = fdtbus_get_string(phandle, "clock-output-names");
111 1.1 jmcneill if (name == NULL)
112 1.1 jmcneill name = faa->faa_name;
113 1.1 jmcneill
114 1.1 jmcneill sc->sc_clk.base.domain = &sc->sc_clkdom;
115 1.1 jmcneill sc->sc_clk.base.name = name;
116 1.2 jmcneill clk_attach(&sc->sc_clk.base);
117 1.1 jmcneill
118 1.1 jmcneill aprint_naive("\n");
119 1.1 jmcneill aprint_normal(": x%u /%u fixed-factor clock\n",
120 1.1 jmcneill sc->sc_clk.mult, sc->sc_clk.div);
121 1.1 jmcneill
122 1.1 jmcneill fdtbus_register_clock_controller(self, phandle,
123 1.1 jmcneill &fixedfactorclock_fdt_funcs);
124 1.1 jmcneill }
125 1.1 jmcneill
126 1.1 jmcneill static struct clk *
127 1.2.2.1 christos fixedfactorclock_decode(device_t dev, int cc_phandle, const void *data,
128 1.2.2.1 christos size_t len)
129 1.1 jmcneill {
130 1.1 jmcneill struct fixedfactorclock_softc * const sc = device_private(dev);
131 1.1 jmcneill
132 1.1 jmcneill /* #clock-cells for a fixed factor clock is always 0 */
133 1.1 jmcneill if (len != 0)
134 1.1 jmcneill return NULL;
135 1.1 jmcneill
136 1.1 jmcneill return &sc->sc_clk.base;
137 1.1 jmcneill }
138 1.1 jmcneill
139 1.1 jmcneill static struct clk *
140 1.1 jmcneill fixedfactorclock_get(void *priv, const char *name)
141 1.1 jmcneill {
142 1.1 jmcneill struct fixedfactorclock_softc * const sc = priv;
143 1.1 jmcneill
144 1.1 jmcneill if (strcmp(name, sc->sc_clk.base.name) != 0)
145 1.1 jmcneill return NULL;
146 1.1 jmcneill
147 1.1 jmcneill return &sc->sc_clk.base;
148 1.1 jmcneill }
149 1.1 jmcneill
150 1.1 jmcneill static void
151 1.1 jmcneill fixedfactorclock_put(void *priv, struct clk *clk)
152 1.1 jmcneill {
153 1.1 jmcneill }
154 1.1 jmcneill
155 1.1 jmcneill static u_int
156 1.1 jmcneill fixedfactorclock_get_rate(void *priv, struct clk *clk)
157 1.1 jmcneill {
158 1.1 jmcneill struct fixedfactorclock_softc * const sc = priv;
159 1.1 jmcneill struct fixedfactorclock_clk *fclk = (struct fixedfactorclock_clk *)clk;
160 1.1 jmcneill struct clk *clkp_parent;
161 1.1 jmcneill
162 1.1 jmcneill clkp_parent = fdtbus_clock_get_index(sc->sc_phandle, 0);
163 1.1 jmcneill if (clkp_parent == NULL)
164 1.1 jmcneill return 0;
165 1.1 jmcneill
166 1.1 jmcneill return (clk_get_rate(clkp_parent) * fclk->mult) / fclk->div;
167 1.1 jmcneill }
168