xref: /freebsd/sys/powerpc/powermac/tbgpio.c (revision c583b02587285535d1af8691be2d83ac3916f083)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2021 Brandon Bergren <bdragon@FreeBSD.org>
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 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
34 #include <sys/bus.h>
35 
36 #include <dev/ofw/ofw_bus.h>
37 #include <dev/ofw/openfirm.h>
38 
39 #include <powerpc/powermac/macgpiovar.h>
40 #include <powerpc/powermac/platform_powermac.h>
41 
42 static int	tbgpio_probe(device_t);
43 static int	tbgpio_attach(device_t);
44 static void	tbgpio_freeze_timebase(device_t, bool);
45 
46 static device_method_t	tbgpio_methods[] = {
47 	/* Device interface */
48 	DEVMETHOD(device_probe,		tbgpio_probe),
49 	DEVMETHOD(device_attach,	tbgpio_attach),
50 	DEVMETHOD_END
51 };
52 
53 struct tbgpio_softc {
54 	uint32_t	sc_value;
55 	uint32_t	sc_mask;
56 };
57 
58 static driver_t tbgpio_driver = {
59 	"tbgpio",
60 	tbgpio_methods,
61 	sizeof(struct tbgpio_softc)
62 };
63 
64 static devclass_t tbgpio_devclass;
65 
66 EARLY_DRIVER_MODULE(tbgpio, macgpio, tbgpio_driver, tbgpio_devclass, 0, 0,
67     BUS_PASS_CPU);
68 
69 static int
70 tbgpio_probe(device_t dev)
71 {
72 	phandle_t node;
73 	const char *name;
74 	pcell_t pfunc[32];
75 	int res;
76 
77 	name = ofw_bus_get_name(dev);
78 	node = ofw_bus_get_node(dev);
79 
80 	if (strcmp(name, "timebase-enable") != 0)
81 		return (ENXIO);
82 
83 	res = OF_getencprop(node, "platform-do-cpu-timebase", pfunc,
84 	    sizeof(pfunc));
85 	if (res == -1)
86 		return (ENXIO);
87 
88 	/*
89 	 * If this doesn't look like a simple gpio_write pfunc,
90 	 * complain about it so we can collect the pfunc.
91 	 */
92 	if (res != 20 || pfunc[2] != 0x01) {
93 		printf("\nUnknown platform function detected!\n");
94 		printf("Please send a PR including the following data:\n");
95 		printf("===================\n");
96 		printf("Func: platform-do-cpu-timebase\n");
97 		hexdump(pfunc, res, NULL, HD_OMIT_CHARS);
98 		printf("===================\n");
99 		return (ENXIO);
100 	}
101 
102 	device_set_desc(dev, "CPU Timebase Control");
103 	return (BUS_PROBE_SPECIFIC);
104 }
105 
106 static int
107 tbgpio_attach(device_t dev)
108 {
109 	phandle_t node;
110 	struct tbgpio_softc *sc;
111 
112 	/*
113 	 * Structure of pfunc:
114 	 * pfunc[0]: phandle to /cpus
115 	 * pfunc[1]: flags
116 	 * pfunc[2]: 0x1 == CMD_WRITE_GPIO
117 	 * pfunc[3]: value
118 	 * pfunc[4]: mask
119 	 */
120 	pcell_t pfunc[5];
121 
122 	sc = device_get_softc(dev);
123 	node = ofw_bus_get_node(dev);
124 
125 	OF_getencprop(node, "platform-do-cpu-timebase", pfunc, sizeof(pfunc));
126 
127 	sc->sc_value = pfunc[3];
128 	sc->sc_mask = pfunc[4];
129 
130 	powermac_register_timebase(dev, tbgpio_freeze_timebase);
131 	return (0);
132 }
133 
134 static void
135 tbgpio_freeze_timebase(device_t dev, bool freeze)
136 {
137 	struct tbgpio_softc *sc;
138 	uint32_t val;
139 
140 	sc = device_get_softc(dev);
141 
142 	val = sc->sc_value;
143 	if (freeze)
144 		val = ~val;
145 	val &= sc->sc_mask;
146 
147 	macgpio_write(dev, val);
148 }
149