aboutsummaryrefslogtreecommitdiff
path: root/sys/arm/mv/clk/a37x0_tbg.c
blob: 32ec1c83b102bcaa681acfdc4c75f472ec6363ab (plain) (blame)
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
/*-
 * SPDX-License-Identifier: BSD-2-Clause
 *
 * 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/bus.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/rman.h>
#include <machine/bus.h>

#include <dev/clk/clk.h>
#include <dev/ofw/ofw_bus_subr.h>

#include "clkdev_if.h"

#include "a37x0_tbg_pll.h"

#define NUM_TBG			4

#define TBG_CTRL0		0x4
#define TBG_CTRL1		0x8
#define TBG_CTRL7		0x20
#define TBG_CTRL8		0x30

#define TBG_MASK		0x1FF

#define TBG_A_REFDIV		0
#define TBG_B_REFDIV		16

#define TBG_A_FBDIV		2
#define TBG_B_FBDIV		18

#define TBG_A_VCODIV_SEL	0
#define TBG_B_VCODIV_SEL	16

#define TBG_A_VCODIV_DIFF	1
#define TBG_B_VCODIV_DIFF	17

struct a37x0_tbg_softc {
	device_t 		dev;
	struct clkdom		*clkdom;
	struct resource		*res;
};

static struct resource_spec a37x0_tbg_clk_spec[] = {
	{ SYS_RES_MEMORY,	0,	RF_ACTIVE },
	{ -1, 0 }
};

struct a37x0_tbg_def {
	char 			*name;
	uint32_t		refdiv_shift;
	uint32_t		fbdiv_shift;
	uint32_t		vcodiv_offset;
	uint32_t		vcodiv_shift;
	uint32_t		tbg_bypass_en;
};

static const struct a37x0_tbg_def tbg[NUM_TBG] = {
	{"TBG-A-P", TBG_A_REFDIV, TBG_A_FBDIV, TBG_CTRL8, TBG_A_VCODIV_DIFF, 9},
	{"TBG-B-P", TBG_B_REFDIV, TBG_B_FBDIV, TBG_CTRL8,
	    TBG_B_VCODIV_DIFF, 25},
	{"TBG-A-S", TBG_A_REFDIV, TBG_A_FBDIV, TBG_CTRL1, TBG_A_VCODIV_SEL, 9},
	{"TBG-B-S", TBG_B_REFDIV, TBG_B_FBDIV, TBG_CTRL1, TBG_B_VCODIV_SEL, 25}
};

static int a37x0_tbg_read_4(device_t, bus_addr_t, uint32_t *);
static int a37x0_tbg_attach(device_t);
static int a37x0_tbg_detach(device_t);
static int a37x0_tbg_probe(device_t);

static device_method_t a37x0_tbg_methods [] = {
	DEVMETHOD(device_attach,	a37x0_tbg_attach),
	DEVMETHOD(device_detach,	a37x0_tbg_detach),
	DEVMETHOD(device_probe,		a37x0_tbg_probe),

	DEVMETHOD(clkdev_read_4,	a37x0_tbg_read_4),

	DEVMETHOD_END
};

static driver_t a37x0_tbg_driver = {
	"a37x0_tbg",
	a37x0_tbg_methods,
	sizeof(struct a37x0_tbg_softc)
};

EARLY_DRIVER_MODULE(a37x0_tbg, simplebus, a37x0_tbg_driver, 0, 0,
    BUS_PASS_TIMER + BUS_PASS_ORDER_MIDDLE);

static int
a37x0_tbg_read_4(device_t dev, bus_addr_t offset, uint32_t *val)
{
	struct a37x0_tbg_softc *sc;

	sc = device_get_softc(dev);

	*val = bus_read_4(sc->res, offset);

	return (0);
}

static int
a37x0_tbg_attach(device_t dev)
{
	struct a37x0_tbg_pll_clk_def def;
	struct a37x0_tbg_softc *sc;
	const char *clkname;
	int error, i;
	phandle_t node;
	clk_t clock;

	sc = device_get_softc(dev);
	node = ofw_bus_get_node(dev);
	sc->dev = dev;

	if (bus_alloc_resources(dev, a37x0_tbg_clk_spec, &sc->res) != 0) {
		device_printf(dev, "Cannot allocate resources\n");
		return (ENXIO);
	}

	sc->clkdom = clkdom_create(dev);
	if (sc->clkdom == NULL) {
		device_printf(dev, "Cannot create clock domain.\n");
		return (ENXIO);
	}

	error = clk_get_by_ofw_index(dev, node, 0, &clock);
	if (error != 0) {
		device_printf(dev, "Cannot find clock parent\n");
		bus_release_resources(dev, a37x0_tbg_clk_spec, &sc->res);
		return (error);
	}

	clkname = clk_get_name(clock);

	for (i = 0; i < NUM_TBG; i++) {
		def.clkdef.parent_names = &clkname;
		def.clkdef.parent_cnt = 1;
		def.clkdef.id = i;
		def.clkdef.name = tbg[i].name;

		def.vcodiv.offset = tbg[i].vcodiv_offset;
		def.vcodiv.shift = tbg[i].vcodiv_shift;
		def.refdiv.offset = TBG_CTRL7;
		def.refdiv.shift = tbg[i].refdiv_shift;
		def.fbdiv.offset = TBG_CTRL0;
		def.fbdiv.shift = tbg[i].fbdiv_shift;
		def.vcodiv.mask = def.refdiv.mask = def.fbdiv.mask = TBG_MASK;
		def.tbg_bypass.offset = TBG_CTRL1;
		def.tbg_bypass.shift = tbg[i].tbg_bypass_en;
		def.tbg_bypass.mask = 0x1;

		error = a37x0_tbg_pll_clk_register(sc->clkdom, &def);

		if (error) {
			device_printf(dev, "Cannot register clock node\n");
			bus_release_resources(dev, a37x0_tbg_clk_spec,
			    &sc->res);
			return (ENXIO);
		}
	}

	error = clkdom_finit(sc->clkdom);
	if (error) {
		device_printf(dev,
		    "Cannot finalize clock domain intialization\n");
		bus_release_resources(dev, a37x0_tbg_clk_spec, &sc->res);
		return (ENXIO);
	}

	if (bootverbose)
		clkdom_dump(sc->clkdom);

	return (0);
}

static int
a37x0_tbg_probe(device_t dev)
{

	if (!ofw_bus_status_okay(dev))
		return (ENXIO);

	if (!ofw_bus_is_compatible(dev, "marvell,armada-3700-tbg-clock"))
		return (ENXIO);

	device_set_desc(dev, "Marvell Armada 3700 time base generators");
	return (BUS_PROBE_DEFAULT);
}

static int
a37x0_tbg_detach(device_t dev)
{

	return (EBUSY);
}