xref: /freebsd/sys/arm/mv/mv_ap806_clock.c (revision 62e8ccc3a489434af379c7f47da71545bc1e14ee)
1e1453c9eSEmmanuel Vadot /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3e1453c9eSEmmanuel Vadot  *
4e1453c9eSEmmanuel Vadot  * Copyright (c) 2018 Rubicon Communications, LLC (Netgate)
5e1453c9eSEmmanuel Vadot  *
6e1453c9eSEmmanuel Vadot  * Redistribution and use in source and binary forms, with or without
7e1453c9eSEmmanuel Vadot  * modification, are permitted provided that the following conditions
8e1453c9eSEmmanuel Vadot  * are met:
9e1453c9eSEmmanuel Vadot  * 1. Redistributions of source code must retain the above copyright
10e1453c9eSEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer.
11e1453c9eSEmmanuel Vadot  * 2. Redistributions in binary form must reproduce the above copyright
12e1453c9eSEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer in the
13e1453c9eSEmmanuel Vadot  *    documentation and/or other materials provided with the distribution.
14e1453c9eSEmmanuel Vadot  *
15e1453c9eSEmmanuel Vadot  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16e1453c9eSEmmanuel Vadot  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17e1453c9eSEmmanuel Vadot  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18e1453c9eSEmmanuel Vadot  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19e1453c9eSEmmanuel Vadot  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20e1453c9eSEmmanuel Vadot  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21e1453c9eSEmmanuel Vadot  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22e1453c9eSEmmanuel Vadot  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23e1453c9eSEmmanuel Vadot  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24e1453c9eSEmmanuel Vadot  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25e1453c9eSEmmanuel Vadot  * SUCH DAMAGE.
26e1453c9eSEmmanuel Vadot  */
27e1453c9eSEmmanuel Vadot 
28e1453c9eSEmmanuel Vadot #include <sys/param.h>
29e1453c9eSEmmanuel Vadot #include <sys/systm.h>
30e1453c9eSEmmanuel Vadot #include <sys/bus.h>
31e1453c9eSEmmanuel Vadot 
32e1453c9eSEmmanuel Vadot #include <sys/kernel.h>
33e1453c9eSEmmanuel Vadot #include <sys/module.h>
34e1453c9eSEmmanuel Vadot #include <sys/rman.h>
35e1453c9eSEmmanuel Vadot #include <sys/lock.h>
36e1453c9eSEmmanuel Vadot #include <sys/mutex.h>
37e1453c9eSEmmanuel Vadot 
38e1453c9eSEmmanuel Vadot #include <machine/bus.h>
39e1453c9eSEmmanuel Vadot #include <machine/resource.h>
40e1453c9eSEmmanuel Vadot #include <machine/intr.h>
41e1453c9eSEmmanuel Vadot 
42be82b3a0SEmmanuel Vadot #include <dev/clk/clk_fixed.h>
43*62e8ccc3SEmmanuel Vadot #include <dev/syscon/syscon.h>
44e1453c9eSEmmanuel Vadot 
45e1453c9eSEmmanuel Vadot #include <dev/ofw/ofw_bus.h>
46e1453c9eSEmmanuel Vadot #include <dev/ofw/ofw_bus_subr.h>
47e1453c9eSEmmanuel Vadot 
484b84206bSMichal Meloun #include "syscon_if.h"
49e1453c9eSEmmanuel Vadot 
50e1453c9eSEmmanuel Vadot static struct clk_fixed_def ap806_clk_cluster_0 = {
51e1453c9eSEmmanuel Vadot 	.clkdef.id = 0,
52e1453c9eSEmmanuel Vadot 	.clkdef.name = "ap806-cpu-cluster-0",
53e1453c9eSEmmanuel Vadot 	.freq = 0,
54e1453c9eSEmmanuel Vadot };
55e1453c9eSEmmanuel Vadot 
56e1453c9eSEmmanuel Vadot static struct clk_fixed_def ap806_clk_cluster_1 = {
57e1453c9eSEmmanuel Vadot 	.clkdef.id = 1,
58e1453c9eSEmmanuel Vadot 	.clkdef.name = "ap806-cpu-cluster-1",
59e1453c9eSEmmanuel Vadot 	.freq = 0,
60e1453c9eSEmmanuel Vadot };
61e1453c9eSEmmanuel Vadot 
62e1453c9eSEmmanuel Vadot static struct clk_fixed_def ap806_clk_fixed = {
63e1453c9eSEmmanuel Vadot 	.clkdef.id = 2,
64e1453c9eSEmmanuel Vadot 	.clkdef.name = "ap806-fixed",
65e1453c9eSEmmanuel Vadot 	.freq = 1200000000,
66e1453c9eSEmmanuel Vadot };
67e1453c9eSEmmanuel Vadot 
68e1453c9eSEmmanuel Vadot /* Thoses are the only exported clocks AFAICT */
69e1453c9eSEmmanuel Vadot 
70e1453c9eSEmmanuel Vadot static const char *mss_parents[] = {"ap806-fixed"};
71e1453c9eSEmmanuel Vadot static struct clk_fixed_def ap806_clk_mss = {
72e1453c9eSEmmanuel Vadot 	.clkdef.id = 3,
73e1453c9eSEmmanuel Vadot 	.clkdef.name = "ap806-mss",
74e1453c9eSEmmanuel Vadot 	.clkdef.parent_names = mss_parents,
75e1453c9eSEmmanuel Vadot 	.clkdef.parent_cnt = 1,
76e1453c9eSEmmanuel Vadot 	.mult = 1,
77e1453c9eSEmmanuel Vadot 	.div = 6,
78e1453c9eSEmmanuel Vadot };
79e1453c9eSEmmanuel Vadot 
80e1453c9eSEmmanuel Vadot static const char *sdio_parents[] = {"ap806-fixed"};
81e1453c9eSEmmanuel Vadot static struct clk_fixed_def ap806_clk_sdio = {
82e1453c9eSEmmanuel Vadot 	.clkdef.id = 4,
83e1453c9eSEmmanuel Vadot 	.clkdef.name = "ap806-sdio",
84e1453c9eSEmmanuel Vadot 	.clkdef.parent_names = sdio_parents,
85e1453c9eSEmmanuel Vadot 	.clkdef.parent_cnt = 1,
86e1453c9eSEmmanuel Vadot 	.mult = 1,
87e1453c9eSEmmanuel Vadot 	.div = 3,
88e1453c9eSEmmanuel Vadot };
89e1453c9eSEmmanuel Vadot 
90e1453c9eSEmmanuel Vadot struct mv_ap806_clock_softc {
91e1453c9eSEmmanuel Vadot 	device_t		dev;
924b84206bSMichal Meloun 	struct syscon		*syscon;
93e1453c9eSEmmanuel Vadot };
94e1453c9eSEmmanuel Vadot 
95e1453c9eSEmmanuel Vadot static struct ofw_compat_data compat_data[] = {
96e1453c9eSEmmanuel Vadot 	{"marvell,ap806-clock",	1},
97e1453c9eSEmmanuel Vadot 	{NULL,			0}
98e1453c9eSEmmanuel Vadot };
99e1453c9eSEmmanuel Vadot 
1004b84206bSMichal Meloun #define	RD4(sc, reg)		SYSCON_READ_4((sc)->syscon, (reg))
1014b84206bSMichal Meloun #define	WR4(sc, reg, val)	SYSCON_WRITE_4((sc)->syscon, (reg), (val))
102e1453c9eSEmmanuel Vadot 
103e1453c9eSEmmanuel Vadot static int
mv_ap806_clock_probe(device_t dev)104e1453c9eSEmmanuel Vadot mv_ap806_clock_probe(device_t dev)
105e1453c9eSEmmanuel Vadot {
106e1453c9eSEmmanuel Vadot 
107e1453c9eSEmmanuel Vadot 	if (!ofw_bus_status_okay(dev))
108e1453c9eSEmmanuel Vadot 		return (ENXIO);
109e1453c9eSEmmanuel Vadot 
110e1453c9eSEmmanuel Vadot 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
111e1453c9eSEmmanuel Vadot 		return (ENXIO);
112e1453c9eSEmmanuel Vadot 
113e1453c9eSEmmanuel Vadot 	device_set_desc(dev, "Marvell AP806 Clock Controller");
114e1453c9eSEmmanuel Vadot 	return (BUS_PROBE_DEFAULT);
115e1453c9eSEmmanuel Vadot }
116e1453c9eSEmmanuel Vadot 
117e1453c9eSEmmanuel Vadot static int
mv_ap806_clock_attach(device_t dev)118e1453c9eSEmmanuel Vadot mv_ap806_clock_attach(device_t dev)
119e1453c9eSEmmanuel Vadot {
120e1453c9eSEmmanuel Vadot 	struct mv_ap806_clock_softc *sc;
121e1453c9eSEmmanuel Vadot 	struct clkdom *clkdom;
122e1453c9eSEmmanuel Vadot 	uint64_t clock_freq;
123e1453c9eSEmmanuel Vadot 	uint32_t reg;
124e1453c9eSEmmanuel Vadot 
125e1453c9eSEmmanuel Vadot 	sc = device_get_softc(dev);
126e1453c9eSEmmanuel Vadot 	sc->dev = dev;
127e1453c9eSEmmanuel Vadot 
1284b84206bSMichal Meloun 	if (SYSCON_GET_HANDLE(sc->dev, &sc->syscon) != 0 ||
1294b84206bSMichal Meloun 	    sc->syscon == NULL) {
1304b84206bSMichal Meloun 		device_printf(dev, "cannot get syscon for device\n");
131e1453c9eSEmmanuel Vadot 		return (ENXIO);
132e1453c9eSEmmanuel Vadot 	}
133e1453c9eSEmmanuel Vadot 
134e1453c9eSEmmanuel Vadot 	reg = RD4(sc, 0x400);
135e1453c9eSEmmanuel Vadot 	switch (reg & 0x1f) {
136e1453c9eSEmmanuel Vadot 	case 0x0:
137e1453c9eSEmmanuel Vadot 	case 0x1:
138e1453c9eSEmmanuel Vadot 		clock_freq = 2000000000;
139e1453c9eSEmmanuel Vadot 		break;
140a86b0839SMarcin Wojtas 	case 0x4:
141a86b0839SMarcin Wojtas 		clock_freq = 1600000000;
142a86b0839SMarcin Wojtas 		break;
143e1453c9eSEmmanuel Vadot 	case 0x6:
144e1453c9eSEmmanuel Vadot 		clock_freq = 1800000000;
145e1453c9eSEmmanuel Vadot 		break;
146a86b0839SMarcin Wojtas 	case 0x7:
147a86b0839SMarcin Wojtas 		clock_freq = 1800000000;
148a86b0839SMarcin Wojtas 		break;
149a86b0839SMarcin Wojtas 	case 0xb:
150a86b0839SMarcin Wojtas 		clock_freq = 1600000000;
151a86b0839SMarcin Wojtas 		break;
152e1453c9eSEmmanuel Vadot 	case 0xd:
153e1453c9eSEmmanuel Vadot 		clock_freq = 1600000000;
154e1453c9eSEmmanuel Vadot 		break;
155a86b0839SMarcin Wojtas 	case 0x13:
156a86b0839SMarcin Wojtas 		clock_freq = 1000000000;
157a86b0839SMarcin Wojtas 		break;
158e1453c9eSEmmanuel Vadot 	case 0x14:
159e1453c9eSEmmanuel Vadot 		clock_freq = 1333000000;
160e1453c9eSEmmanuel Vadot 		break;
161a86b0839SMarcin Wojtas 	case 0x17:
162a86b0839SMarcin Wojtas 		clock_freq = 1333000000;
163a86b0839SMarcin Wojtas 		break;
164a86b0839SMarcin Wojtas 	case 0x19:
165a86b0839SMarcin Wojtas 		clock_freq = 1200000000;
166a86b0839SMarcin Wojtas 		break;
167a86b0839SMarcin Wojtas 	case 0x1a:
168a86b0839SMarcin Wojtas 		clock_freq = 1400000000;
169a86b0839SMarcin Wojtas 		break;
170a86b0839SMarcin Wojtas 	case 0x1b:
171a86b0839SMarcin Wojtas 		clock_freq = 600000000;
172a86b0839SMarcin Wojtas 		break;
173a86b0839SMarcin Wojtas 	case 0x1c:
174a86b0839SMarcin Wojtas 		clock_freq = 800000000;
175a86b0839SMarcin Wojtas 		break;
176a86b0839SMarcin Wojtas 	case 0x1d:
177a86b0839SMarcin Wojtas 		clock_freq = 1000000000;
178a86b0839SMarcin Wojtas 		break;
179e1453c9eSEmmanuel Vadot 	default:
1804b84206bSMichal Meloun 		device_printf(dev, "Cannot guess clock freq with reg %x\n",
1814b84206bSMichal Meloun 		     reg & 0x1f);
182e1453c9eSEmmanuel Vadot 		return (ENXIO);
183e1453c9eSEmmanuel Vadot 		break;
184e1453c9eSEmmanuel Vadot 	};
185e1453c9eSEmmanuel Vadot 
186e1453c9eSEmmanuel Vadot 	ap806_clk_cluster_0.freq = clock_freq;
187e1453c9eSEmmanuel Vadot 	ap806_clk_cluster_1.freq = clock_freq;
188e1453c9eSEmmanuel Vadot 	clkdom = clkdom_create(dev);
189e1453c9eSEmmanuel Vadot 
190e1453c9eSEmmanuel Vadot 	clknode_fixed_register(clkdom, &ap806_clk_cluster_0);
191e1453c9eSEmmanuel Vadot 	clknode_fixed_register(clkdom, &ap806_clk_cluster_1);
192e1453c9eSEmmanuel Vadot 	clknode_fixed_register(clkdom, &ap806_clk_fixed);
193e1453c9eSEmmanuel Vadot 	clknode_fixed_register(clkdom, &ap806_clk_mss);
194e1453c9eSEmmanuel Vadot 	clknode_fixed_register(clkdom, &ap806_clk_sdio);
195e1453c9eSEmmanuel Vadot 
196e1453c9eSEmmanuel Vadot 	clkdom_finit(clkdom);
197e1453c9eSEmmanuel Vadot 
198e1453c9eSEmmanuel Vadot 	if (bootverbose)
199e1453c9eSEmmanuel Vadot 		clkdom_dump(clkdom);
200e1453c9eSEmmanuel Vadot 	return (0);
201e1453c9eSEmmanuel Vadot }
202e1453c9eSEmmanuel Vadot 
203e1453c9eSEmmanuel Vadot static int
mv_ap806_clock_detach(device_t dev)204e1453c9eSEmmanuel Vadot mv_ap806_clock_detach(device_t dev)
205e1453c9eSEmmanuel Vadot {
206e1453c9eSEmmanuel Vadot 
207e1453c9eSEmmanuel Vadot 	return (EBUSY);
208e1453c9eSEmmanuel Vadot }
209e1453c9eSEmmanuel Vadot 
210e1453c9eSEmmanuel Vadot static device_method_t mv_ap806_clock_methods[] = {
211e1453c9eSEmmanuel Vadot 	/* Device interface */
212e1453c9eSEmmanuel Vadot 	DEVMETHOD(device_probe,		mv_ap806_clock_probe),
213e1453c9eSEmmanuel Vadot 	DEVMETHOD(device_attach,	mv_ap806_clock_attach),
214e1453c9eSEmmanuel Vadot 	DEVMETHOD(device_detach,	mv_ap806_clock_detach),
215e1453c9eSEmmanuel Vadot 
216e1453c9eSEmmanuel Vadot 	DEVMETHOD_END
217e1453c9eSEmmanuel Vadot };
218e1453c9eSEmmanuel Vadot 
219e1453c9eSEmmanuel Vadot static driver_t mv_ap806_clock_driver = {
220e1453c9eSEmmanuel Vadot 	"mv_ap806_clock",
221e1453c9eSEmmanuel Vadot 	mv_ap806_clock_methods,
222e1453c9eSEmmanuel Vadot 	sizeof(struct mv_ap806_clock_softc),
223e1453c9eSEmmanuel Vadot };
224e1453c9eSEmmanuel Vadot 
225a3b866cbSJohn Baldwin EARLY_DRIVER_MODULE(mv_ap806_clock, simplebus, mv_ap806_clock_driver, 0, 0,
226a3b866cbSJohn Baldwin     BUS_PASS_RESOURCE + BUS_PASS_ORDER_LATE);
227