1*6e54367aSthorpej /* $NetBSD: sun8i_a23_apbclk.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: sun8i_a23_apbclk.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 APB0_DIV __BITS(1,0)
43545669b7Sjmcneill
44545669b7Sjmcneill static int sun8i_a23_apbclk_match(device_t, cfdata_t, void *);
45545669b7Sjmcneill static void sun8i_a23_apbclk_attach(device_t, device_t, void *);
46545669b7Sjmcneill
47545669b7Sjmcneill static struct clk *sun8i_a23_apbclk_decode(device_t, int, const void *, size_t);
48545669b7Sjmcneill
49545669b7Sjmcneill static const struct fdtbus_clock_controller_func sun8i_a23_apbclk_fdt_funcs = {
50545669b7Sjmcneill .decode = sun8i_a23_apbclk_decode
51545669b7Sjmcneill };
52545669b7Sjmcneill
53545669b7Sjmcneill static struct clk *sun8i_a23_apbclk_get(void *, const char *);
54545669b7Sjmcneill static void sun8i_a23_apbclk_put(void *, struct clk *);
55545669b7Sjmcneill static int sun8i_a23_apbclk_set_rate(void *, struct clk *, u_int);
56545669b7Sjmcneill static u_int sun8i_a23_apbclk_get_rate(void *, struct clk *);
57545669b7Sjmcneill static struct clk *sun8i_a23_apbclk_get_parent(void *, struct clk *);
58545669b7Sjmcneill
59545669b7Sjmcneill static const struct clk_funcs sun8i_a23_apbclk_clk_funcs = {
60545669b7Sjmcneill .get = sun8i_a23_apbclk_get,
61545669b7Sjmcneill .put = sun8i_a23_apbclk_put,
62545669b7Sjmcneill .set_rate = sun8i_a23_apbclk_set_rate,
63545669b7Sjmcneill .get_rate = sun8i_a23_apbclk_get_rate,
64545669b7Sjmcneill .get_parent = sun8i_a23_apbclk_get_parent,
65545669b7Sjmcneill };
66545669b7Sjmcneill
67545669b7Sjmcneill struct sun8i_a23_apbclk_softc {
68545669b7Sjmcneill device_t sc_dev;
69545669b7Sjmcneill int sc_phandle;
70545669b7Sjmcneill bus_space_tag_t sc_bst;
71545669b7Sjmcneill bus_space_handle_t sc_bsh;
72545669b7Sjmcneill
73545669b7Sjmcneill struct clk_domain sc_clkdom;
74545669b7Sjmcneill struct clk sc_clk;
75545669b7Sjmcneill struct clk *sc_parent;
76545669b7Sjmcneill };
77545669b7Sjmcneill
78545669b7Sjmcneill #define RD4(sc, reg) \
79545669b7Sjmcneill bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
80545669b7Sjmcneill #define WR4(sc, reg, val) \
81545669b7Sjmcneill bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
82545669b7Sjmcneill
83545669b7Sjmcneill CFATTACH_DECL_NEW(sunxi_a23_apbclk, sizeof(struct sun8i_a23_apbclk_softc),
84545669b7Sjmcneill sun8i_a23_apbclk_match, sun8i_a23_apbclk_attach, NULL, NULL);
85545669b7Sjmcneill
86*6e54367aSthorpej static const struct device_compatible_entry compat_data[] = {
87*6e54367aSthorpej { .compat = "allwinner,sun8i-a23-apb0-clk" },
88*6e54367aSthorpej DEVICE_COMPAT_EOL
89*6e54367aSthorpej };
90*6e54367aSthorpej
91545669b7Sjmcneill static int
sun8i_a23_apbclk_match(device_t parent,cfdata_t cf,void * aux)92545669b7Sjmcneill sun8i_a23_apbclk_match(device_t parent, cfdata_t cf, void *aux)
93545669b7Sjmcneill {
94545669b7Sjmcneill const struct fdt_attach_args *faa = aux;
95545669b7Sjmcneill
96*6e54367aSthorpej return of_compatible_match(faa->faa_phandle, compat_data);
97545669b7Sjmcneill }
98545669b7Sjmcneill
99545669b7Sjmcneill static void
sun8i_a23_apbclk_attach(device_t parent,device_t self,void * aux)100545669b7Sjmcneill sun8i_a23_apbclk_attach(device_t parent, device_t self, void *aux)
101545669b7Sjmcneill {
102545669b7Sjmcneill struct sun8i_a23_apbclk_softc * const sc = device_private(self);
103545669b7Sjmcneill const struct fdt_attach_args *faa = aux;
104545669b7Sjmcneill const int phandle = faa->faa_phandle;
105545669b7Sjmcneill bus_addr_t addr;
106545669b7Sjmcneill bus_size_t size;
107545669b7Sjmcneill
108545669b7Sjmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
109545669b7Sjmcneill aprint_error(": couldn't get registers\n");
110545669b7Sjmcneill return;
111545669b7Sjmcneill }
112545669b7Sjmcneill
113545669b7Sjmcneill sc->sc_dev = self;
114545669b7Sjmcneill sc->sc_phandle = phandle;
115545669b7Sjmcneill sc->sc_bst = faa->faa_bst;
116545669b7Sjmcneill if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
117545669b7Sjmcneill aprint_error(": couldn't map registers\n");
118545669b7Sjmcneill return;
119545669b7Sjmcneill }
120545669b7Sjmcneill sc->sc_parent = fdtbus_clock_get_index(phandle, 0);
121545669b7Sjmcneill
122545669b7Sjmcneill sc->sc_clkdom.funcs = &sun8i_a23_apbclk_clk_funcs;
123545669b7Sjmcneill sc->sc_clkdom.priv = sc;
124545669b7Sjmcneill
125545669b7Sjmcneill sc->sc_clk.domain = &sc->sc_clkdom;
126545669b7Sjmcneill sc->sc_clk.name = kmem_asprintf("%s", faa->faa_name);
127545669b7Sjmcneill
128545669b7Sjmcneill aprint_naive("\n");
129545669b7Sjmcneill aprint_normal(": A23 APB0 clock\n");
130545669b7Sjmcneill
131545669b7Sjmcneill fdtbus_register_clock_controller(self, phandle, &sun8i_a23_apbclk_fdt_funcs);
132545669b7Sjmcneill }
133545669b7Sjmcneill
134545669b7Sjmcneill static struct clk *
sun8i_a23_apbclk_decode(device_t dev,int cc_phandle,const void * data,size_t len)135545669b7Sjmcneill sun8i_a23_apbclk_decode(device_t dev, int cc_phandle, const void *data,
136545669b7Sjmcneill size_t len)
137545669b7Sjmcneill {
138545669b7Sjmcneill struct sun8i_a23_apbclk_softc * const sc = device_private(dev);
139545669b7Sjmcneill
140545669b7Sjmcneill if (len != 0)
141545669b7Sjmcneill return NULL;
142545669b7Sjmcneill
143545669b7Sjmcneill return &sc->sc_clk;
144545669b7Sjmcneill }
145545669b7Sjmcneill
146545669b7Sjmcneill static struct clk *
sun8i_a23_apbclk_get(void * priv,const char * name)147545669b7Sjmcneill sun8i_a23_apbclk_get(void *priv, const char *name)
148545669b7Sjmcneill {
149545669b7Sjmcneill struct sun8i_a23_apbclk_softc * const sc = priv;
150545669b7Sjmcneill
151545669b7Sjmcneill if (strcmp(name, sc->sc_clk.name) != 0)
152545669b7Sjmcneill return NULL;
153545669b7Sjmcneill
154545669b7Sjmcneill return &sc->sc_clk;
155545669b7Sjmcneill }
156545669b7Sjmcneill
157545669b7Sjmcneill static void
sun8i_a23_apbclk_put(void * priv,struct clk * clk)158545669b7Sjmcneill sun8i_a23_apbclk_put(void *priv, struct clk *clk)
159545669b7Sjmcneill {
160545669b7Sjmcneill }
161545669b7Sjmcneill
162545669b7Sjmcneill static int
sun8i_a23_apbclk_set_rate(void * priv,struct clk * clk,u_int rate)163545669b7Sjmcneill sun8i_a23_apbclk_set_rate(void *priv, struct clk *clk, u_int rate)
164545669b7Sjmcneill {
165545669b7Sjmcneill return ENXIO;
166545669b7Sjmcneill }
167545669b7Sjmcneill
168545669b7Sjmcneill static u_int
sun8i_a23_apbclk_get_rate(void * priv,struct clk * clk)169545669b7Sjmcneill sun8i_a23_apbclk_get_rate(void *priv, struct clk *clk)
170545669b7Sjmcneill {
171545669b7Sjmcneill struct sun8i_a23_apbclk_softc * const sc = priv;
172545669b7Sjmcneill struct clk *clk_parent = clk_get_parent(clk);
173545669b7Sjmcneill
174545669b7Sjmcneill const uint32_t val = RD4(sc, 0);
175545669b7Sjmcneill const u_int div = __SHIFTOUT(val, APB0_DIV);
176545669b7Sjmcneill
177545669b7Sjmcneill return clk_get_rate(clk_parent) / (div + 1);
178545669b7Sjmcneill }
179545669b7Sjmcneill
180545669b7Sjmcneill static struct clk *
sun8i_a23_apbclk_get_parent(void * priv,struct clk * clk)181545669b7Sjmcneill sun8i_a23_apbclk_get_parent(void *priv, struct clk *clk)
182545669b7Sjmcneill {
183545669b7Sjmcneill struct sun8i_a23_apbclk_softc * const sc = priv;
184545669b7Sjmcneill
185545669b7Sjmcneill return sc->sc_parent;
186545669b7Sjmcneill }
187