1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2021 Semihalf.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
14 *
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
25 * SUCH DAMAGE.
26 */
27
28 #include <sys/param.h>
29 #include <sys/bus.h>
30 #include <sys/kernel.h>
31 #include <sys/module.h>
32 #include <sys/mutex.h>
33 #include <sys/rman.h>
34 #include <machine/bus.h>
35
36 #include <dev/fdt/simplebus.h>
37
38 #include <dev/clk/clk.h>
39 #include <dev/clk/clk_fixed.h>
40 #include <dev/syscon/syscon.h>
41
42 #include <dev/ofw/ofw_bus.h>
43 #include <dev/ofw/ofw_bus_subr.h>
44
45 #include "syscon_if.h"
46
47 #define BIT(x) (1 << (x))
48
49 #define NB_GPIO1_PIN_LT_L 0x8
50 #define NB_GPIO1_MPP1_9 BIT(9)
51
52 struct a37x0_xtal_softc {
53 device_t dev;
54 struct clkdom *clkdom;
55 };
56
57 static int a37x0_xtal_attach(device_t dev);
58 static int a37x0_xtal_detach(device_t dev);
59 static int a37x0_xtal_probe(device_t dev);
60
61 static device_method_t a37x0_xtal_methods [] = {
62 DEVMETHOD(device_probe, a37x0_xtal_probe),
63 DEVMETHOD(device_attach, a37x0_xtal_attach),
64 DEVMETHOD(device_detach, a37x0_xtal_detach),
65 DEVMETHOD_END
66 };
67
68 static driver_t a37x0_xtal_driver = {
69 "a37x0-xtal",
70 a37x0_xtal_methods,
71 sizeof(struct a37x0_xtal_softc)
72 };
73
74 EARLY_DRIVER_MODULE(a37x0_xtal, simplebus, a37x0_xtal_driver, 0, 0,
75 BUS_PASS_TIMER + BUS_PASS_ORDER_EARLY);
76
77 static int
a37x0_xtal_attach(device_t dev)78 a37x0_xtal_attach(device_t dev)
79 {
80 struct a37x0_xtal_softc *sc;
81 struct clk_fixed_def def;
82 struct syscon *syscon;
83 uint32_t reg;
84 int error;
85
86 sc = device_get_softc(dev);
87
88 def.clkdef.name = "armada-3700-xtal";
89 def.clkdef.parent_names = NULL;
90 def.clkdef.parent_cnt = 0;
91 def.clkdef.id = 1;
92 def.mult = 0;
93 def.div = 0;
94
95 if (SYSCON_GET_HANDLE(dev, &syscon) != 0 || syscon == NULL){
96 device_printf(dev, "Cannot get syscon driver handle\n");
97 return (ENXIO);
98 }
99
100 reg = SYSCON_READ_4(syscon, NB_GPIO1_PIN_LT_L);
101 if (reg & NB_GPIO1_MPP1_9)
102 def.freq = 40000000;
103 else
104 def.freq = 25000000;
105
106 sc->clkdom = clkdom_create(dev);
107 error = clknode_fixed_register(sc->clkdom, &def);
108 if (error){
109 device_printf(dev, "Cannot register clock node\n");
110 return (ENXIO);
111 }
112
113 error = clkdom_finit(sc->clkdom);
114 if (error){
115 device_printf(dev, "Cannot finalize clock domain initialization\n");
116 return (ENXIO);
117 }
118
119 if (bootverbose)
120 clkdom_dump(sc->clkdom);
121
122 return (0);
123 }
124
125 static int
a37x0_xtal_probe(device_t dev)126 a37x0_xtal_probe(device_t dev)
127 {
128
129 if (!ofw_bus_status_okay(dev))
130 return (ENXIO);
131
132 if (!ofw_bus_is_compatible(dev, "marvell,armada-3700-xtal-clock"))
133 return (ENXIO);
134
135 device_set_desc(dev, "Marvell Armada 3700 Oscillator");
136 return (BUS_PROBE_DEFAULT);
137 }
138
139 static int
a37x0_xtal_detach(device_t dev)140 a37x0_xtal_detach(device_t dev)
141 {
142
143 return (EBUSY);
144 }
145