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