xref: /freebsd/sys/arm/mv/clk/a37x0_xtal.c (revision c7a063741720ef81d4caa4613242579d12f1d605)
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 int a37x0_xtal_attach(device_t dev);
61 static int a37x0_xtal_detach(device_t dev);
62 static int a37x0_xtal_probe(device_t dev);
63 
64 static device_method_t a37x0_xtal_methods [] = {
65 	DEVMETHOD(device_probe, 	a37x0_xtal_probe),
66 	DEVMETHOD(device_attach, 	a37x0_xtal_attach),
67 	DEVMETHOD(device_detach, 	a37x0_xtal_detach),
68 	DEVMETHOD_END
69 };
70 
71 static driver_t a37x0_xtal_driver = {
72 	"a37x0-xtal",
73 	a37x0_xtal_methods,
74 	sizeof(struct a37x0_xtal_softc)
75 };
76 
77 EARLY_DRIVER_MODULE(a37x0_xtal, simplebus, a37x0_xtal_driver, 0, 0,
78     BUS_PASS_TIMER + BUS_PASS_ORDER_EARLY);
79 
80 static int
81 a37x0_xtal_attach(device_t dev)
82 {
83 	struct a37x0_xtal_softc *sc;
84 	struct clk_fixed_def def;
85 	struct syscon *syscon;
86 	uint32_t reg;
87 	int error;
88 
89 	sc = device_get_softc(dev);
90 
91 	def.clkdef.name = "armada-3700-xtal";
92 	def.clkdef.parent_names = NULL;
93 	def.clkdef.parent_cnt = 0;
94 	def.clkdef.id = 1;
95 	def.mult = 0;
96 	def.div = 0;
97 
98 	if (SYSCON_GET_HANDLE(dev, &syscon) != 0 || syscon == NULL){
99 		device_printf(dev, "Cannot get syscon driver handle\n");
100 		return (ENXIO);
101 	}
102 
103 	reg = SYSCON_READ_4(syscon, NB_GPIO1_PIN_LT_L);
104 	if (reg & NB_GPIO1_MPP1_9)
105 		def.freq = 40000000;
106 	else
107 		def.freq = 25000000;
108 
109 	sc->clkdom = clkdom_create(dev);
110 	error = clknode_fixed_register(sc->clkdom, &def);
111 	if (error){
112 		device_printf(dev, "Cannot register clock node\n");
113 		return (ENXIO);
114 	}
115 
116 	error = clkdom_finit(sc->clkdom);
117 	if (error){
118 		device_printf(dev, "Cannot finalize clock domain initialization\n");
119 		return (ENXIO);
120 	}
121 
122 	if (bootverbose)
123 		clkdom_dump(sc->clkdom);
124 
125 	return (0);
126 }
127 
128 static int
129 a37x0_xtal_probe(device_t dev)
130 {
131 
132 	if (!ofw_bus_status_okay(dev))
133 		return (ENXIO);
134 
135 	if (!ofw_bus_is_compatible(dev, "marvell,armada-3700-xtal-clock"))
136 		return (ENXIO);
137 
138 	device_set_desc(dev, "Marvell Armada 3700 Oscillator");
139 	return (BUS_PROBE_DEFAULT);
140 }
141 
142 static int
143 a37x0_xtal_detach(device_t dev)
144 {
145 
146 	return (EBUSY);
147 }
148