2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2018 Rubicon Communications, LLC (Netgate)
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
41 #include <sys/mutex.h>
43 #include <machine/bus.h>
44 #include <machine/resource.h>
45 #include <machine/intr.h>
47 #include <dev/extres/clk/clk_fixed.h>
48 #include <dev/extres/syscon/syscon.h>
50 #include <dev/ofw/ofw_bus.h>
51 #include <dev/ofw/ofw_bus_subr.h>
53 #include "syscon_if.h"
55 static struct clk_fixed_def ap806_clk_cluster_0 = {
57 .clkdef.name = "ap806-cpu-cluster-0",
61 static struct clk_fixed_def ap806_clk_cluster_1 = {
63 .clkdef.name = "ap806-cpu-cluster-1",
67 static struct clk_fixed_def ap806_clk_fixed = {
69 .clkdef.name = "ap806-fixed",
73 /* Thoses are the only exported clocks AFAICT */
75 static const char *mss_parents[] = {"ap806-fixed"};
76 static struct clk_fixed_def ap806_clk_mss = {
78 .clkdef.name = "ap806-mss",
79 .clkdef.parent_names = mss_parents,
80 .clkdef.parent_cnt = 1,
85 static const char *sdio_parents[] = {"ap806-fixed"};
86 static struct clk_fixed_def ap806_clk_sdio = {
88 .clkdef.name = "ap806-sdio",
89 .clkdef.parent_names = sdio_parents,
90 .clkdef.parent_cnt = 1,
95 struct mv_ap806_clock_softc {
97 struct syscon *syscon;
100 static struct ofw_compat_data compat_data[] = {
101 {"marvell,ap806-clock", 1},
105 #define RD4(sc, reg) SYSCON_READ_4((sc)->syscon, (reg))
106 #define WR4(sc, reg, val) SYSCON_WRITE_4((sc)->syscon, (reg), (val))
109 mv_ap806_clock_probe(device_t dev)
112 if (!ofw_bus_status_okay(dev))
115 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
118 device_set_desc(dev, "Marvell AP806 Clock Controller");
119 return (BUS_PROBE_DEFAULT);
123 mv_ap806_clock_attach(device_t dev)
125 struct mv_ap806_clock_softc *sc;
126 struct clkdom *clkdom;
130 sc = device_get_softc(dev);
133 if (SYSCON_GET_HANDLE(sc->dev, &sc->syscon) != 0 ||
134 sc->syscon == NULL) {
135 device_printf(dev, "cannot get syscon for device\n");
140 * We might miss some combinations
141 * Those are the only possible ones on the mcbin
143 reg = RD4(sc, 0x400);
144 switch (reg & 0x1f) {
147 clock_freq = 2000000000;
150 clock_freq = 1800000000;
153 clock_freq = 1600000000;
156 clock_freq = 1333000000;
159 device_printf(dev, "Cannot guess clock freq with reg %x\n",
165 ap806_clk_cluster_0.freq = clock_freq;
166 ap806_clk_cluster_1.freq = clock_freq;
167 clkdom = clkdom_create(dev);
169 clknode_fixed_register(clkdom, &ap806_clk_cluster_0);
170 clknode_fixed_register(clkdom, &ap806_clk_cluster_1);
171 clknode_fixed_register(clkdom, &ap806_clk_fixed);
172 clknode_fixed_register(clkdom, &ap806_clk_mss);
173 clknode_fixed_register(clkdom, &ap806_clk_sdio);
175 clkdom_finit(clkdom);
183 mv_ap806_clock_detach(device_t dev)
189 static device_method_t mv_ap806_clock_methods[] = {
190 /* Device interface */
191 DEVMETHOD(device_probe, mv_ap806_clock_probe),
192 DEVMETHOD(device_attach, mv_ap806_clock_attach),
193 DEVMETHOD(device_detach, mv_ap806_clock_detach),
198 static devclass_t mv_ap806_clock_devclass;
200 static driver_t mv_ap806_clock_driver = {
202 mv_ap806_clock_methods,
203 sizeof(struct mv_ap806_clock_softc),
206 EARLY_DRIVER_MODULE(mv_ap806_clock, simplebus, mv_ap806_clock_driver,
207 mv_ap806_clock_devclass, 0, 0, BUS_PASS_RESOURCE + BUS_PASS_ORDER_LATE);