1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
/*-
* SPDX-License-Identifier: BSD-2-Clause
*
* Copyright (c) 2021 Alstom Group.
* Copyright (c) 2021 Semihalf.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bus.h>
#include <sys/kernel.h>
#include <sys/kobj.h>
#include <sys/module.h>
#include <sys/malloc.h>
#include <sys/rman.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <machine/bus.h>
#include <machine/cpu.h>
#include <dev/clk/clk_div.h>
#include <dev/ofw/openfirm.h>
#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_bus_subr.h>
#include "clkdev_if.h"
#include "syscon_if.h"
struct ls1028a_flexspi_clk_softc {
device_t dev;
struct clkdom *clkdom;
uint64_t reg_offset;
struct syscon *syscon;
struct clk_div_def clk_def;
struct mtx mtx;
};
static struct clk_div_table ls1028a_flexspi_div_tbl[] = {
{ .value = 0, .divider = 1, },
{ .value = 1, .divider = 2, },
{ .value = 2, .divider = 3, },
{ .value = 3, .divider = 4, },
{ .value = 4, .divider = 5, },
{ .value = 5, .divider = 6, },
{ .value = 6, .divider = 7, },
{ .value = 7, .divider = 8, },
{ .value = 11, .divider = 12, },
{ .value = 15, .divider = 16, },
{ .value = 16, .divider = 20, },
{ .value = 17, .divider = 24, },
{ .value = 18, .divider = 28, },
{ .value = 19, .divider = 32, },
{ .value = 20, .divider = 80, },
{}
};
static struct clk_div_table lx2160a_flexspi_div_tbl[] = {
{ .value = 1, .divider = 2, },
{ .value = 3, .divider = 4, },
{ .value = 5, .divider = 6, },
{ .value = 7, .divider = 8, },
{ .value = 11, .divider = 12, },
{ .value = 15, .divider = 16, },
{ .value = 16, .divider = 20, },
{ .value = 17, .divider = 24, },
{ .value = 18, .divider = 28, },
{ .value = 19, .divider = 32, },
{ .value = 20, .divider = 80, },
{}
};
static struct ofw_compat_data compat_data[] = {
{ "fsl,ls1028a-flexspi-clk", (uintptr_t)ls1028a_flexspi_div_tbl },
{ "fsl,lx2160a-flexspi-clk", (uintptr_t)lx2160a_flexspi_div_tbl },
{ NULL, 0 }
};
static int
ls1028a_flexspi_clk_probe(device_t dev)
{
if (!ofw_bus_status_okay(dev))
return (ENXIO);
if (ofw_bus_search_compatible(dev, compat_data)->ocd_data != 0) {
device_set_desc(dev, "NXP FlexSPI clock driver");
return (BUS_PROBE_DEFAULT);
}
return (ENXIO);
}
static int
ls1028a_flexspi_clk_attach(device_t dev)
{
struct ls1028a_flexspi_clk_softc *sc;
const char *oclkname = NULL;
const char *pclkname[1];
uint32_t acells;
uint32_t scells;
pcell_t cells[4];
phandle_t node;
uint64_t reg_size;
int ret;
clk_t clk;
sc = device_get_softc(dev);
sc->dev = dev;
node = ofw_bus_get_node(dev);
/* Parse address-cells and size-cells from the parent node as a fallback */
if (OF_getencprop(node, "#address-cells", &acells,
sizeof(acells)) == -1) {
if (OF_getencprop(OF_parent(node), "#address-cells", &acells,
sizeof(acells)) == -1) {
acells = 2;
}
}
if (OF_getencprop(node, "#size-cells", &scells,
sizeof(scells)) == -1) {
if (OF_getencprop(OF_parent(node), "#size-cells", &scells,
sizeof(scells)) == -1) {
scells = 1;
}
}
ret = OF_getencprop(node, "reg", cells, (acells + scells) * sizeof(pcell_t));
if (ret < 0) {
device_printf(dev, "ERROR: failed to read REG property\n");
return (ENOMEM);
}
sc->reg_offset = (uint64_t)cells[0];
if (acells == 2)
sc->reg_offset = (sc->reg_offset << 32) | (uint64_t)cells[1];
reg_size = (uint64_t)cells[acells];
if (scells == 2)
reg_size = (reg_size << 32) | (uint64_t)cells[acells + 1];
if (reg_size != 4) {
device_printf(dev, "ERROR, expected only single register\n");
return (EINVAL);
}
if (sc->reg_offset >> 32UL) {
device_printf(dev, "ERROR, only 32-bit address offset is supported\n");
return (EINVAL);
}
/* Get syscon handle */
ret = SYSCON_GET_HANDLE(dev, &sc->syscon);
if ((ret != 0) || (sc->syscon == NULL)) {
device_printf(dev, "ERROR: failed to get syscon\n");
return (EFAULT);
}
/* Initialize access mutex */
mtx_init(&sc->mtx, "FSL clock mtx", NULL, MTX_DEF);
/* Get clock names */
ret = clk_get_by_ofw_index(dev, node, 0, &clk);
if (ret) {
device_printf(dev, "ERROR: failed to get parent clock\n");
return (EINVAL);
}
pclkname[0] = clk_get_name(clk);
ret = clk_parse_ofw_clk_name(dev, node, &oclkname);
if (ret) {
device_printf(dev, "ERROR: failed to get output clock name\n");
return (EINVAL);
}
#ifdef DEBUG
device_printf(dev, "INFO: pclkname %s, oclkname %s\n", pclkname[0], oclkname);
#endif
/* Fixup CLK structure */
sc->clk_def.clkdef.name = oclkname;
sc->clk_def.clkdef.parent_names = (const char **)pclkname;
sc->clk_def.offset = (uint32_t)sc->reg_offset;
sc->clk_def.clkdef.id = 1;
sc->clk_def.clkdef.parent_cnt = 1;
sc->clk_def.clkdef.flags = 0;
sc->clk_def.div_flags = CLK_DIV_WITH_TABLE;
sc->clk_def.i_shift = 0;
sc->clk_def.i_width = 5;
sc->clk_def.div_table = (struct clk_div_table*)ofw_bus_search_compatible(dev, compat_data)->ocd_data;
/* Create clock */
sc->clkdom = clkdom_create(dev);
if (sc->clkdom == NULL)
panic("clkdom == NULL");
ret = clknode_div_register(sc->clkdom, &sc->clk_def);
if (ret) {
device_printf(dev, "ERROR: unable to register clock\n");
return (EINVAL);
}
clkdom_finit(sc->clkdom);
if (bootverbose)
clkdom_dump(sc->clkdom);
return (0);
}
static int
ls1028a_flexspi_clk_detach(device_t dev)
{
/* Clock detaching is not supported */
return (EACCES);
}
static int
ls1028a_flexspi_clk_read_4(device_t dev, bus_addr_t addr, uint32_t *val)
{
struct ls1028a_flexspi_clk_softc *sc;
sc = device_get_softc(dev);
*val = SYSCON_READ_4(sc->syscon, addr);
return (0);
}
static int
ls1028a_flexspi_clk_write_4(device_t dev, bus_addr_t addr, uint32_t val)
{
struct ls1028a_flexspi_clk_softc *sc;
int ret;
sc = device_get_softc(dev);
ret = SYSCON_WRITE_4(sc->syscon, addr, val);
return (ret);
}
static int
ls1028a_flexspi_clk_modify_4(device_t dev, bus_addr_t addr, uint32_t clr, uint32_t set)
{
struct ls1028a_flexspi_clk_softc *sc;
int ret;
sc = device_get_softc(dev);
ret = SYSCON_MODIFY_4(sc->syscon, addr, clr, set);
return (ret);
}
static void
ls1028a_flexspi_clk_device_lock(device_t dev)
{
struct ls1028a_flexspi_clk_softc *sc;
sc = device_get_softc(dev);
mtx_lock(&sc->mtx);
}
static void
ls1028a_flexspi_clk_device_unlock(device_t dev)
{
struct ls1028a_flexspi_clk_softc *sc;
sc = device_get_softc(dev);
mtx_unlock(&sc->mtx);
}
static device_method_t ls1028a_flexspi_clk_methods[] = {
/* Device interface */
DEVMETHOD(device_probe, ls1028a_flexspi_clk_probe),
DEVMETHOD(device_attach, ls1028a_flexspi_clk_attach),
DEVMETHOD(device_detach, ls1028a_flexspi_clk_detach),
DEVMETHOD(clkdev_read_4, ls1028a_flexspi_clk_read_4),
DEVMETHOD(clkdev_write_4, ls1028a_flexspi_clk_write_4),
DEVMETHOD(clkdev_modify_4, ls1028a_flexspi_clk_modify_4),
DEVMETHOD(clkdev_device_lock, ls1028a_flexspi_clk_device_lock),
DEVMETHOD(clkdev_device_unlock, ls1028a_flexspi_clk_device_unlock),
DEVMETHOD_END
};
static DEFINE_CLASS_0(fspi_clk, ls1028a_flexspi_clk_driver, ls1028a_flexspi_clk_methods,
sizeof(struct ls1028a_flexspi_clk_softc));
EARLY_DRIVER_MODULE(ls1028a_flexspi_clk, simple_mfd, ls1028a_flexspi_clk_driver,
NULL, NULL, BUS_PASS_TIMER);
|