]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/allwinner/clk/aw_axiclk.c
Allwinner: Add A33 support
[FreeBSD/FreeBSD.git] / sys / arm / allwinner / clk / aw_axiclk.c
1 /*-
2  * Copyright (c) 2016 Jared McNeill <jmcneill@invisible.ca>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
21  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28
29 /*
30  * Allwinner AXI clock
31  */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/bus.h>
39 #include <sys/rman.h>
40 #include <sys/kernel.h>
41 #include <sys/module.h>
42 #include <machine/bus.h>
43
44 #include <dev/ofw/ofw_bus.h>
45 #include <dev/ofw/ofw_bus_subr.h>
46 #include <dev/ofw/ofw_subr.h>
47
48 #include <dev/extres/clk/clk_mux.h>
49 #include <dev/extres/clk/clk_gate.h>
50
51 #include "clkdev_if.h"
52
53 #define AXI_CLK_DIV_RATIO       (0x3 << 0)
54
55 static struct ofw_compat_data compat_data[] = {
56         { "allwinner,sun4i-a10-axi-clk",        1 },
57         { "allwinner,sun8i-a23-axi-clk",        1 },
58         { NULL, 0 }
59 };
60
61 struct aw_axiclk_sc {
62         device_t        clkdev;
63         bus_addr_t      reg;
64 };
65
66 #define AXICLK_READ(sc, val)    CLKDEV_READ_4((sc)->clkdev, (sc)->reg, (val))
67 #define AXICLK_WRITE(sc, val)   CLKDEV_WRITE_4((sc)->clkdev, (sc)->reg, (val))
68 #define DEVICE_LOCK(sc)         CLKDEV_DEVICE_LOCK((sc)->clkdev)
69 #define DEVICE_UNLOCK(sc)       CLKDEV_DEVICE_UNLOCK((sc)->clkdev)
70
71 static int
72 aw_axiclk_init(struct clknode *clk, device_t dev)
73 {
74         clknode_init_parent_idx(clk, 0);
75         return (0);
76 }
77
78 static int
79 aw_axiclk_recalc_freq(struct clknode *clk, uint64_t *freq)
80 {
81         struct aw_axiclk_sc *sc;
82         uint32_t val;
83
84         sc = clknode_get_softc(clk);
85
86         DEVICE_LOCK(sc);
87         AXICLK_READ(sc, &val);
88         DEVICE_UNLOCK(sc);
89
90         *freq = *freq / ((val & AXI_CLK_DIV_RATIO) + 1);
91
92         return (0);
93 }
94
95 static clknode_method_t aw_axiclk_clknode_methods[] = {
96         /* Device interface */
97         CLKNODEMETHOD(clknode_init,             aw_axiclk_init),
98         CLKNODEMETHOD(clknode_recalc_freq,      aw_axiclk_recalc_freq),
99         CLKNODEMETHOD_END
100 };
101 DEFINE_CLASS_1(aw_axiclk_clknode, aw_axiclk_clknode_class,
102     aw_axiclk_clknode_methods, sizeof(struct aw_axiclk_sc), clknode_class);
103
104 static int
105 aw_axiclk_probe(device_t dev)
106 {
107         if (!ofw_bus_status_okay(dev))
108                 return (ENXIO);
109
110         if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
111                 return (ENXIO);
112
113         device_set_desc(dev, "Allwinner AXI Clock");
114         return (BUS_PROBE_DEFAULT);
115 }
116
117 static int
118 aw_axiclk_attach(device_t dev)
119 {
120         struct clknode_init_def def;
121         struct aw_axiclk_sc *sc;
122         struct clkdom *clkdom;
123         struct clknode *clk;
124         clk_t clk_parent;
125         bus_addr_t paddr;
126         bus_size_t psize;
127         phandle_t node;
128         int error;
129
130         node = ofw_bus_get_node(dev);
131
132         if (ofw_reg_to_paddr(node, 0, &paddr, &psize, NULL) != 0) {
133                 device_printf(dev, "cannot parse 'reg' property\n");
134                 return (ENXIO);
135         }
136
137         clkdom = clkdom_create(dev);
138
139         error = clk_get_by_ofw_index(dev, 0, 0, &clk_parent);
140         if (error != 0) {
141                 device_printf(dev, "cannot parse clock parent\n");
142                 return (ENXIO);
143         }
144
145         memset(&def, 0, sizeof(def));
146         error = clk_parse_ofw_clk_name(dev, node, &def.name);
147         if (error != 0) {
148                 device_printf(dev, "cannot parse clock name\n");
149                 error = ENXIO;
150                 goto fail;
151         }
152         def.id = 1;
153         def.parent_names = malloc(sizeof(char *), M_OFWPROP, M_WAITOK);
154         def.parent_names[0] = clk_get_name(clk_parent);
155         def.parent_cnt = 1;
156
157         clk = clknode_create(clkdom, &aw_axiclk_clknode_class, &def);
158         if (clk == NULL) {
159                 device_printf(dev, "cannot create clknode\n");
160                 error = ENXIO;
161                 goto fail;
162         }
163
164         sc = clknode_get_softc(clk);
165         sc->reg = paddr;
166         sc->clkdev = device_get_parent(dev);
167
168         clknode_register(clkdom, clk);
169
170         if (clkdom_finit(clkdom) != 0) {
171                 device_printf(dev, "cannot finalize clkdom initialization\n");
172                 error = ENXIO;
173                 goto fail;
174         }
175
176         if (bootverbose)
177                 clkdom_dump(clkdom);
178
179         return (0);
180
181 fail:
182         return (error);
183 }
184
185 static device_method_t aw_axiclk_methods[] = {
186         /* Device interface */
187         DEVMETHOD(device_probe,         aw_axiclk_probe),
188         DEVMETHOD(device_attach,        aw_axiclk_attach),
189
190         DEVMETHOD_END
191 };
192
193 static driver_t aw_axiclk_driver = {
194         "aw_axiclk",
195         aw_axiclk_methods,
196         0
197 };
198
199 static devclass_t aw_axiclk_devclass;
200
201 EARLY_DRIVER_MODULE(aw_axiclk, simplebus, aw_axiclk_driver,
202     aw_axiclk_devclass, 0, 0, BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);