Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/sys/dev/clk/allwinner/aw_ccu.c
39537 views
1
/*-
2
* Copyright (c) 2016 Jared McNeill <[email protected]>
3
*
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions
6
* are met:
7
* 1. Redistributions of source code must retain the above copyright
8
* notice, this list of conditions and the following disclaimer.
9
* 2. Redistributions in binary form must reproduce the above copyright
10
* notice, this list of conditions and the following disclaimer in the
11
* documentation and/or other materials provided with the distribution.
12
*
13
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
18
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
20
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
21
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23
* SUCH DAMAGE.
24
*/
25
26
/*
27
* Allwinner oscillator clock
28
*/
29
30
#include <sys/param.h>
31
#include <sys/systm.h>
32
#include <sys/bus.h>
33
#include <sys/rman.h>
34
#include <sys/kernel.h>
35
#include <sys/lock.h>
36
#include <sys/module.h>
37
#include <sys/mutex.h>
38
#include <machine/bus.h>
39
40
#include <dev/fdt/simplebus.h>
41
42
#include <dev/ofw/ofw_bus.h>
43
#include <dev/ofw/ofw_bus_subr.h>
44
45
#include <dev/clk/clk.h>
46
47
#include "clkdev_if.h"
48
49
#define CCU_BASE 0x01c20000
50
#define CCU_SIZE 0x400
51
52
struct aw_ccu_softc {
53
struct simplebus_softc sc;
54
bus_space_tag_t bst;
55
bus_space_handle_t bsh;
56
struct mtx mtx;
57
int flags;
58
};
59
60
static struct ofw_compat_data compat_data[] = {
61
{ "allwinner,sun7i-a20", 1 },
62
{ "allwinner,sun6i-a31", 1 },
63
{ "allwinner,sun6i-a31s", 1 },
64
{ NULL, 0 }
65
};
66
67
static int
68
aw_ccu_check_addr(struct aw_ccu_softc *sc, bus_addr_t addr,
69
bus_space_handle_t *pbsh, bus_size_t *poff)
70
{
71
if (addr >= CCU_BASE && addr < (CCU_BASE + CCU_SIZE)) {
72
*poff = addr - CCU_BASE;
73
*pbsh = sc->bsh;
74
return (0);
75
}
76
return (EINVAL);
77
}
78
79
static int
80
aw_ccu_write_4(device_t dev, bus_addr_t addr, uint32_t val)
81
{
82
struct aw_ccu_softc *sc;
83
bus_space_handle_t bsh;
84
bus_size_t reg;
85
86
sc = device_get_softc(dev);
87
88
if (aw_ccu_check_addr(sc, addr, &bsh, &reg) != 0)
89
return (EINVAL);
90
91
mtx_assert(&sc->mtx, MA_OWNED);
92
bus_space_write_4(sc->bst, bsh, reg, val);
93
94
return (0);
95
}
96
97
static int
98
aw_ccu_read_4(device_t dev, bus_addr_t addr, uint32_t *val)
99
{
100
struct aw_ccu_softc *sc;
101
bus_space_handle_t bsh;
102
bus_size_t reg;
103
104
sc = device_get_softc(dev);
105
106
if (aw_ccu_check_addr(sc, addr, &bsh, &reg) != 0)
107
return (EINVAL);
108
109
mtx_assert(&sc->mtx, MA_OWNED);
110
*val = bus_space_read_4(sc->bst, bsh, reg);
111
112
return (0);
113
}
114
115
static int
116
aw_ccu_modify_4(device_t dev, bus_addr_t addr, uint32_t clr, uint32_t set)
117
{
118
struct aw_ccu_softc *sc;
119
bus_space_handle_t bsh;
120
bus_size_t reg;
121
uint32_t val;
122
123
sc = device_get_softc(dev);
124
125
if (aw_ccu_check_addr(sc, addr, &bsh, &reg) != 0)
126
return (EINVAL);
127
128
mtx_assert(&sc->mtx, MA_OWNED);
129
val = bus_space_read_4(sc->bst, bsh, reg);
130
val &= ~clr;
131
val |= set;
132
bus_space_write_4(sc->bst, bsh, reg, val);
133
134
return (0);
135
}
136
137
static void
138
aw_ccu_device_lock(device_t dev)
139
{
140
struct aw_ccu_softc *sc;
141
142
sc = device_get_softc(dev);
143
mtx_lock(&sc->mtx);
144
}
145
146
static void
147
aw_ccu_device_unlock(device_t dev)
148
{
149
struct aw_ccu_softc *sc;
150
151
sc = device_get_softc(dev);
152
mtx_unlock(&sc->mtx);
153
}
154
155
static const struct ofw_compat_data *
156
aw_ccu_search_compatible(void)
157
{
158
const struct ofw_compat_data *compat;
159
phandle_t root;
160
161
root = OF_finddevice("/");
162
for (compat = compat_data; compat->ocd_str != NULL; compat++)
163
if (ofw_bus_node_is_compatible(root, compat->ocd_str))
164
break;
165
166
return (compat);
167
}
168
169
static int
170
aw_ccu_probe(device_t dev)
171
{
172
const char *name;
173
174
name = ofw_bus_get_name(dev);
175
176
if (name == NULL || strcmp(name, "clocks") != 0)
177
return (ENXIO);
178
179
if (aw_ccu_search_compatible()->ocd_data == 0)
180
return (ENXIO);
181
182
device_set_desc(dev, "Allwinner Clock Control Unit");
183
return (BUS_PROBE_SPECIFIC);
184
}
185
186
static int
187
aw_ccu_attach(device_t dev)
188
{
189
struct aw_ccu_softc *sc;
190
phandle_t node, child;
191
device_t cdev;
192
int error;
193
194
sc = device_get_softc(dev);
195
node = ofw_bus_get_node(dev);
196
197
simplebus_init(dev, node);
198
199
sc->flags = aw_ccu_search_compatible()->ocd_data;
200
201
/*
202
* Map registers. The DT doesn't have a "reg" property
203
* for the /clocks node and child nodes have conflicting "reg"
204
* properties.
205
*/
206
sc->bst = bus_get_bus_tag(dev);
207
error = bus_space_map(sc->bst, CCU_BASE, CCU_SIZE, 0,
208
&sc->bsh);
209
if (error != 0) {
210
device_printf(dev, "couldn't map CCU: %d\n", error);
211
return (error);
212
}
213
214
mtx_init(&sc->mtx, device_get_nameunit(dev), NULL, MTX_DEF);
215
216
/* Attach child devices */
217
for (child = OF_child(node); child > 0; child = OF_peer(child)) {
218
cdev = simplebus_add_device(dev, child, 0, NULL, -1, NULL);
219
if (cdev != NULL)
220
device_probe_and_attach(cdev);
221
}
222
223
bus_attach_children(dev);
224
return (0);
225
}
226
227
static device_method_t aw_ccu_methods[] = {
228
/* Device interface */
229
DEVMETHOD(device_probe, aw_ccu_probe),
230
DEVMETHOD(device_attach, aw_ccu_attach),
231
232
/* clkdev interface */
233
DEVMETHOD(clkdev_write_4, aw_ccu_write_4),
234
DEVMETHOD(clkdev_read_4, aw_ccu_read_4),
235
DEVMETHOD(clkdev_modify_4, aw_ccu_modify_4),
236
DEVMETHOD(clkdev_device_lock, aw_ccu_device_lock),
237
DEVMETHOD(clkdev_device_unlock, aw_ccu_device_unlock),
238
239
DEVMETHOD_END
240
};
241
242
DEFINE_CLASS_1(aw_ccu, aw_ccu_driver, aw_ccu_methods,
243
sizeof(struct aw_ccu_softc), simplebus_driver);
244
245
EARLY_DRIVER_MODULE(aw_ccu, simplebus, aw_ccu_driver, 0, 0,
246
BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
247
248
MODULE_VERSION(aw_ccu, 1);
249
250