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/fdt/simplebus.h>
49 #include <dev/ofw/ofw_bus.h>
50 #include <dev/ofw/ofw_bus_subr.h>
52 #include <dev/extres/clk/clk_fixed.h>
54 static struct clk_fixed_def ap806_clk_cluster_0 = {
56 .clkdef.name = "ap806-cpu-cluster-0",
60 static struct clk_fixed_def ap806_clk_cluster_1 = {
62 .clkdef.name = "ap806-cpu-cluster-1",
66 static struct clk_fixed_def ap806_clk_fixed = {
68 .clkdef.name = "ap806-fixed",
72 /* Thoses are the only exported clocks AFAICT */
74 static const char *mss_parents[] = {"ap806-fixed"};
75 static struct clk_fixed_def ap806_clk_mss = {
77 .clkdef.name = "ap806-mss",
78 .clkdef.parent_names = mss_parents,
79 .clkdef.parent_cnt = 1,
84 static const char *sdio_parents[] = {"ap806-fixed"};
85 static struct clk_fixed_def ap806_clk_sdio = {
87 .clkdef.name = "ap806-sdio",
88 .clkdef.parent_names = sdio_parents,
89 .clkdef.parent_cnt = 1,
94 struct mv_ap806_clock_softc {
95 struct simplebus_softc simplebus_sc;
100 static struct resource_spec mv_ap806_clock_res_spec[] = {
101 { SYS_RES_MEMORY, 0, RF_ACTIVE | RF_SHAREABLE },
105 static struct ofw_compat_data compat_data[] = {
106 {"marvell,ap806-clock", 1},
110 #define RD4(sc, reg) bus_read_4((sc)->res, (reg))
111 #define WR4(sc, reg, val) bus_write_4((sc)->res, (reg), (val))
114 mv_ap806_clock_probe(device_t dev)
117 if (!ofw_bus_status_okay(dev))
120 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
123 device_set_desc(dev, "Marvell AP806 Clock Controller");
124 return (BUS_PROBE_DEFAULT);
128 mv_ap806_clock_attach(device_t dev)
130 struct mv_ap806_clock_softc *sc;
131 struct clkdom *clkdom;
135 sc = device_get_softc(dev);
138 if (bus_alloc_resources(dev, mv_ap806_clock_res_spec, &sc->res) != 0) {
139 device_printf(dev, "cannot allocate resources for device\n");
144 * We might miss some combinations
145 * Those are the only possible ones on the mcbin
147 reg = RD4(sc, 0x400);
148 switch (reg & 0x1f) {
151 clock_freq = 2000000000;
154 clock_freq = 1800000000;
157 clock_freq = 1600000000;
160 clock_freq = 1333000000;
163 device_printf(dev, "Cannot guess clock freq with reg %x\n", reg & 0x1f);
168 ap806_clk_cluster_0.freq = clock_freq;
169 ap806_clk_cluster_1.freq = clock_freq;
170 clkdom = clkdom_create(dev);
172 clknode_fixed_register(clkdom, &ap806_clk_cluster_0);
173 clknode_fixed_register(clkdom, &ap806_clk_cluster_1);
174 clknode_fixed_register(clkdom, &ap806_clk_fixed);
175 clknode_fixed_register(clkdom, &ap806_clk_mss);
176 clknode_fixed_register(clkdom, &ap806_clk_sdio);
178 clkdom_finit(clkdom);
186 mv_ap806_clock_detach(device_t dev)
192 static device_method_t mv_ap806_clock_methods[] = {
193 /* Device interface */
194 DEVMETHOD(device_probe, mv_ap806_clock_probe),
195 DEVMETHOD(device_attach, mv_ap806_clock_attach),
196 DEVMETHOD(device_detach, mv_ap806_clock_detach),
201 static devclass_t mv_ap806_clock_devclass;
203 static driver_t mv_ap806_clock_driver = {
205 mv_ap806_clock_methods,
206 sizeof(struct mv_ap806_clock_softc),
209 EARLY_DRIVER_MODULE(mv_ap806_clock, simplebus, mv_ap806_clock_driver,
210 mv_ap806_clock_devclass, 0, 0, BUS_PASS_RESOURCE + BUS_PASS_ORDER_LATE);