xref: /freebsd/sys/arm/mv/clk/a37x0_xtal.c (revision 6e563a1b608438504d963c2d7c70e50d2e75af46)
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 	uint32_t reg;
89 	int error;
90 
91 	sc = device_get_softc(dev);
92 
93 	def.clkdef.name = "armada-3700-xtal";
94 	def.clkdef.parent_names = NULL;
95 	def.clkdef.parent_cnt = 0;
96 	def.clkdef.id = 1;
97 	def.mult = 0;
98 	def.div = 0;
99 
100 	if (SYSCON_GET_HANDLE(dev, &syscon) != 0 || syscon == NULL){
101 		device_printf(dev, "Cannot get syscon driver handle\n");
102 		return (ENXIO);
103 	}
104 
105 	reg = SYSCON_READ_4(syscon, NB_GPIO1_PIN_LT_L);
106 	if (reg & NB_GPIO1_MPP1_9)
107 		def.freq = 40000000;
108 	else
109 		def.freq = 25000000;
110 
111 	sc->clkdom = clkdom_create(dev);
112 	error = clknode_fixed_register(sc->clkdom, &def);
113 	if (error){
114 		device_printf(dev, "Cannot register clock node\n");
115 		return (ENXIO);
116 	}
117 
118 	error = clkdom_finit(sc->clkdom);
119 	if (error){
120 		device_printf(dev, "Cannot finalize clock domain initialization\n");
121 		return (ENXIO);
122 	}
123 
124 	if (bootverbose)
125 		clkdom_dump(sc->clkdom);
126 
127 	return (0);
128 }
129 
130 static int
131 a37x0_xtal_probe(device_t dev)
132 {
133 
134 	if (!ofw_bus_status_okay(dev))
135 		return (ENXIO);
136 
137 	if (!ofw_bus_is_compatible(dev, "marvell,armada-3700-xtal-clock"))
138 		return (ENXIO);
139 
140 	device_set_desc(dev, "Marvell Armada 3700 Oscillator");
141 	return (BUS_PROBE_DEFAULT);
142 }
143 
144 static int
145 a37x0_xtal_detach(device_t dev)
146 {
147 
148 	return (EBUSY);
149 }
150