1*6e54367aSthorpej /* $NetBSD: sun9i_a80_cpusclk.c,v 1.2 2021/01/27 03:10:20 thorpej Exp $ */
2545669b7Sjmcneill
3545669b7Sjmcneill /*-
4545669b7Sjmcneill * Copyright (c) 2017 Jared McNeill <jmcneill@invisible.ca>
5545669b7Sjmcneill * All rights reserved.
6545669b7Sjmcneill *
7545669b7Sjmcneill * Redistribution and use in source and binary forms, with or without
8545669b7Sjmcneill * modification, are permitted provided that the following conditions
9545669b7Sjmcneill * are met:
10545669b7Sjmcneill * 1. Redistributions of source code must retain the above copyright
11545669b7Sjmcneill * notice, this list of conditions and the following disclaimer.
12545669b7Sjmcneill * 2. Redistributions in binary form must reproduce the above copyright
13545669b7Sjmcneill * notice, this list of conditions and the following disclaimer in the
14545669b7Sjmcneill * documentation and/or other materials provided with the distribution.
15545669b7Sjmcneill *
16545669b7Sjmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17545669b7Sjmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18545669b7Sjmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19545669b7Sjmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20545669b7Sjmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21545669b7Sjmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22545669b7Sjmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23545669b7Sjmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24545669b7Sjmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25545669b7Sjmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26545669b7Sjmcneill * SUCH DAMAGE.
27545669b7Sjmcneill */
28545669b7Sjmcneill
29545669b7Sjmcneill #include <sys/cdefs.h>
30*6e54367aSthorpej __KERNEL_RCSID(0, "$NetBSD: sun9i_a80_cpusclk.c,v 1.2 2021/01/27 03:10:20 thorpej Exp $");
31545669b7Sjmcneill
32545669b7Sjmcneill #include <sys/param.h>
33545669b7Sjmcneill #include <sys/systm.h>
34545669b7Sjmcneill #include <sys/device.h>
35545669b7Sjmcneill #include <sys/kmem.h>
36545669b7Sjmcneill #include <sys/bus.h>
37545669b7Sjmcneill
38545669b7Sjmcneill #include <dev/clk/clk_backend.h>
39545669b7Sjmcneill
40545669b7Sjmcneill #include <dev/fdt/fdtvar.h>
41545669b7Sjmcneill
42545669b7Sjmcneill #define CPUS_CLK_SRC_SEL __BITS(17,16)
43545669b7Sjmcneill #define CPUS_CLK_SRC_SEL_PLL_PERIPH0 2
44545669b7Sjmcneill #define CPUS_POST_DIV __BITS(12,8)
45545669b7Sjmcneill #define CPUS_CLK_RATIO __BITS(5,4)
46545669b7Sjmcneill
47545669b7Sjmcneill static int sun9i_a80_cpusclk_match(device_t, cfdata_t, void *);
48545669b7Sjmcneill static void sun9i_a80_cpusclk_attach(device_t, device_t, void *);
49545669b7Sjmcneill
50545669b7Sjmcneill static struct clk *sun9i_a80_cpusclk_decode(device_t, int, const void *, size_t);
51545669b7Sjmcneill
52545669b7Sjmcneill static const struct fdtbus_clock_controller_func sun9i_a80_cpusclk_fdt_funcs = {
53545669b7Sjmcneill .decode = sun9i_a80_cpusclk_decode
54545669b7Sjmcneill };
55545669b7Sjmcneill
56545669b7Sjmcneill static struct clk *sun9i_a80_cpusclk_get(void *, const char *);
57545669b7Sjmcneill static void sun9i_a80_cpusclk_put(void *, struct clk *);
58545669b7Sjmcneill static int sun9i_a80_cpusclk_set_rate(void *, struct clk *, u_int);
59545669b7Sjmcneill static u_int sun9i_a80_cpusclk_get_rate(void *, struct clk *);
60545669b7Sjmcneill static struct clk *sun9i_a80_cpusclk_get_parent(void *, struct clk *);
61545669b7Sjmcneill
62545669b7Sjmcneill static const struct clk_funcs sun9i_a80_cpusclk_clk_funcs = {
63545669b7Sjmcneill .get = sun9i_a80_cpusclk_get,
64545669b7Sjmcneill .put = sun9i_a80_cpusclk_put,
65545669b7Sjmcneill .set_rate = sun9i_a80_cpusclk_set_rate,
66545669b7Sjmcneill .get_rate = sun9i_a80_cpusclk_get_rate,
67545669b7Sjmcneill .get_parent = sun9i_a80_cpusclk_get_parent,
68545669b7Sjmcneill };
69545669b7Sjmcneill
70545669b7Sjmcneill struct sun9i_a80_cpusclk_softc {
71545669b7Sjmcneill device_t sc_dev;
72545669b7Sjmcneill int sc_phandle;
73545669b7Sjmcneill bus_space_tag_t sc_bst;
74545669b7Sjmcneill bus_space_handle_t sc_bsh;
75545669b7Sjmcneill
76545669b7Sjmcneill struct clk_domain sc_clkdom;
77545669b7Sjmcneill struct clk sc_clk;
78545669b7Sjmcneill };
79545669b7Sjmcneill
80545669b7Sjmcneill #define RD4(sc, reg) \
81545669b7Sjmcneill bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
82545669b7Sjmcneill #define WR4(sc, reg, val) \
83545669b7Sjmcneill bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
84545669b7Sjmcneill
85545669b7Sjmcneill CFATTACH_DECL_NEW(sunxi_a80_cpusclk, sizeof(struct sun9i_a80_cpusclk_softc),
86545669b7Sjmcneill sun9i_a80_cpusclk_match, sun9i_a80_cpusclk_attach, NULL, NULL);
87545669b7Sjmcneill
88*6e54367aSthorpej static const struct device_compatible_entry compat_data[] = {
89*6e54367aSthorpej { .compat = "allwinner,sun9i-a80-cpus-clk" },
90*6e54367aSthorpej DEVICE_COMPAT_EOL
91*6e54367aSthorpej };
92*6e54367aSthorpej
93545669b7Sjmcneill static int
sun9i_a80_cpusclk_match(device_t parent,cfdata_t cf,void * aux)94545669b7Sjmcneill sun9i_a80_cpusclk_match(device_t parent, cfdata_t cf, void *aux)
95545669b7Sjmcneill {
96545669b7Sjmcneill const struct fdt_attach_args *faa = aux;
97545669b7Sjmcneill
98*6e54367aSthorpej return of_compatible_match(faa->faa_phandle, compat_data);
99545669b7Sjmcneill }
100545669b7Sjmcneill
101545669b7Sjmcneill static void
sun9i_a80_cpusclk_attach(device_t parent,device_t self,void * aux)102545669b7Sjmcneill sun9i_a80_cpusclk_attach(device_t parent, device_t self, void *aux)
103545669b7Sjmcneill {
104545669b7Sjmcneill struct sun9i_a80_cpusclk_softc * const sc = device_private(self);
105545669b7Sjmcneill const struct fdt_attach_args *faa = aux;
106545669b7Sjmcneill const int phandle = faa->faa_phandle;
107545669b7Sjmcneill bus_addr_t addr;
108545669b7Sjmcneill bus_size_t size;
109545669b7Sjmcneill
110545669b7Sjmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
111545669b7Sjmcneill aprint_error(": couldn't get registers\n");
112545669b7Sjmcneill return;
113545669b7Sjmcneill }
114545669b7Sjmcneill
115545669b7Sjmcneill sc->sc_dev = self;
116545669b7Sjmcneill sc->sc_phandle = phandle;
117545669b7Sjmcneill sc->sc_bst = faa->faa_bst;
118545669b7Sjmcneill if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
119545669b7Sjmcneill aprint_error(": couldn't map registers\n");
120545669b7Sjmcneill return;
121545669b7Sjmcneill }
122545669b7Sjmcneill
123545669b7Sjmcneill sc->sc_clkdom.funcs = &sun9i_a80_cpusclk_clk_funcs;
124545669b7Sjmcneill sc->sc_clkdom.priv = sc;
125545669b7Sjmcneill
126545669b7Sjmcneill sc->sc_clk.domain = &sc->sc_clkdom;
127545669b7Sjmcneill sc->sc_clk.name = kmem_asprintf("%s", faa->faa_name);
128545669b7Sjmcneill
129545669b7Sjmcneill aprint_naive("\n");
130545669b7Sjmcneill aprint_normal(": A80 CPUS clock\n");
131545669b7Sjmcneill
132545669b7Sjmcneill fdtbus_register_clock_controller(self, phandle, &sun9i_a80_cpusclk_fdt_funcs);
133545669b7Sjmcneill }
134545669b7Sjmcneill
135545669b7Sjmcneill static struct clk *
sun9i_a80_cpusclk_decode(device_t dev,int cc_phandle,const void * data,size_t len)136545669b7Sjmcneill sun9i_a80_cpusclk_decode(device_t dev, int cc_phandle, const void *data,
137545669b7Sjmcneill size_t len)
138545669b7Sjmcneill {
139545669b7Sjmcneill struct sun9i_a80_cpusclk_softc * const sc = device_private(dev);
140545669b7Sjmcneill
141545669b7Sjmcneill if (len != 0)
142545669b7Sjmcneill return NULL;
143545669b7Sjmcneill
144545669b7Sjmcneill return &sc->sc_clk;
145545669b7Sjmcneill }
146545669b7Sjmcneill
147545669b7Sjmcneill static struct clk *
sun9i_a80_cpusclk_get(void * priv,const char * name)148545669b7Sjmcneill sun9i_a80_cpusclk_get(void *priv, const char *name)
149545669b7Sjmcneill {
150545669b7Sjmcneill struct sun9i_a80_cpusclk_softc * const sc = priv;
151545669b7Sjmcneill
152545669b7Sjmcneill if (strcmp(name, sc->sc_clk.name) != 0)
153545669b7Sjmcneill return NULL;
154545669b7Sjmcneill
155545669b7Sjmcneill return &sc->sc_clk;
156545669b7Sjmcneill }
157545669b7Sjmcneill
158545669b7Sjmcneill static void
sun9i_a80_cpusclk_put(void * priv,struct clk * clk)159545669b7Sjmcneill sun9i_a80_cpusclk_put(void *priv, struct clk *clk)
160545669b7Sjmcneill {
161545669b7Sjmcneill }
162545669b7Sjmcneill
163545669b7Sjmcneill static int
sun9i_a80_cpusclk_set_rate(void * priv,struct clk * clk,u_int rate)164545669b7Sjmcneill sun9i_a80_cpusclk_set_rate(void *priv, struct clk *clk, u_int rate)
165545669b7Sjmcneill {
166545669b7Sjmcneill return ENXIO;
167545669b7Sjmcneill }
168545669b7Sjmcneill
169545669b7Sjmcneill static u_int
sun9i_a80_cpusclk_get_rate(void * priv,struct clk * clk)170545669b7Sjmcneill sun9i_a80_cpusclk_get_rate(void *priv, struct clk *clk)
171545669b7Sjmcneill {
172545669b7Sjmcneill struct sun9i_a80_cpusclk_softc * const sc = priv;
173545669b7Sjmcneill struct clk *clk_parent = clk_get_parent(clk);
174545669b7Sjmcneill u_int rate;
175545669b7Sjmcneill
176545669b7Sjmcneill const uint32_t val = RD4(sc, 0);
177545669b7Sjmcneill const u_int sel = __SHIFTOUT(val, CPUS_CLK_SRC_SEL);
178545669b7Sjmcneill const u_int post_div = __SHIFTOUT(val, CPUS_POST_DIV);
179545669b7Sjmcneill const u_int clk_ratio = __SHIFTOUT(val, CPUS_CLK_RATIO);
180545669b7Sjmcneill
181545669b7Sjmcneill rate = clk_get_rate(clk_parent) / (clk_ratio + 1);
182545669b7Sjmcneill if (sel == CPUS_CLK_SRC_SEL_PLL_PERIPH0)
183545669b7Sjmcneill rate /= (post_div + 1);
184545669b7Sjmcneill
185545669b7Sjmcneill return rate;
186545669b7Sjmcneill }
187545669b7Sjmcneill
188545669b7Sjmcneill static struct clk *
sun9i_a80_cpusclk_get_parent(void * priv,struct clk * clk)189545669b7Sjmcneill sun9i_a80_cpusclk_get_parent(void *priv, struct clk *clk)
190545669b7Sjmcneill {
191545669b7Sjmcneill struct sun9i_a80_cpusclk_softc * const sc = priv;
192545669b7Sjmcneill
193545669b7Sjmcneill const uint32_t val = RD4(sc, 0);
194545669b7Sjmcneill const u_int sel = __SHIFTOUT(val, CPUS_CLK_SRC_SEL);
195545669b7Sjmcneill
196545669b7Sjmcneill return fdtbus_clock_get_index(sc->sc_phandle, sel);
197545669b7Sjmcneill }
198