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