2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2021 Brandon Bergren <bdragon@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
36 #include <dev/ofw/ofw_bus.h>
37 #include <dev/ofw/openfirm.h>
39 #include <powerpc/powermac/macgpiovar.h>
40 #include <powerpc/powermac/platform_powermac.h>
42 static int tbgpio_probe(device_t
);
43 static int tbgpio_attach(device_t
);
44 static void tbgpio_freeze_timebase(device_t
, bool);
46 static device_method_t tbgpio_methods
[] = {
47 /* Device interface */
48 DEVMETHOD(device_probe
, tbgpio_probe
),
49 DEVMETHOD(device_attach
, tbgpio_attach
),
58 static driver_t tbgpio_driver
= {
61 sizeof(struct tbgpio_softc
)
64 EARLY_DRIVER_MODULE(tbgpio
, macgpio
, tbgpio_driver
, 0, 0, BUS_PASS_CPU
);
67 tbgpio_probe(device_t dev
)
74 name
= ofw_bus_get_name(dev
);
75 node
= ofw_bus_get_node(dev
);
77 if (strcmp(name
, "timebase-enable") != 0)
80 res
= OF_getencprop(node
, "platform-do-cpu-timebase", pfunc
,
86 * If this doesn't look like a simple gpio_write pfunc,
87 * complain about it so we can collect the pfunc.
89 if (res
!= 20 || pfunc
[2] != 0x01) {
90 printf("\nUnknown platform function detected!\n");
91 printf("Please send a PR including the following data:\n");
92 printf("===================\n");
93 printf("Func: platform-do-cpu-timebase\n");
94 hexdump(pfunc
, res
, NULL
, HD_OMIT_CHARS
);
95 printf("===================\n");
99 device_set_desc(dev
, "CPU Timebase Control");
100 return (BUS_PROBE_SPECIFIC
);
104 tbgpio_attach(device_t dev
)
107 struct tbgpio_softc
*sc
;
110 * Structure of pfunc:
111 * pfunc[0]: phandle to /cpus
113 * pfunc[2]: 0x1 == CMD_WRITE_GPIO
119 sc
= device_get_softc(dev
);
120 node
= ofw_bus_get_node(dev
);
122 OF_getencprop(node
, "platform-do-cpu-timebase", pfunc
, sizeof(pfunc
));
124 sc
->sc_value
= pfunc
[3];
125 sc
->sc_mask
= pfunc
[4];
127 powermac_register_timebase(dev
, tbgpio_freeze_timebase
);
132 tbgpio_freeze_timebase(device_t dev
, bool freeze
)
134 struct tbgpio_softc
*sc
;
137 sc
= device_get_softc(dev
);
144 macgpio_write(dev
, val
);