#include <sys/cdefs.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/bus.h>
#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/openfirm.h>
#include <powerpc/powermac/macgpiovar.h>
#include <powerpc/powermac/platform_powermac.h>
static int tbgpio_probe(device_t);
static int tbgpio_attach(device_t);
static void tbgpio_freeze_timebase(device_t, bool);
static device_method_t tbgpio_methods[] = {
DEVMETHOD(device_probe, tbgpio_probe),
DEVMETHOD(device_attach, tbgpio_attach),
DEVMETHOD_END
};
struct tbgpio_softc {
uint32_t sc_value;
uint32_t sc_mask;
};
static driver_t tbgpio_driver = {
"tbgpio",
tbgpio_methods,
sizeof(struct tbgpio_softc)
};
EARLY_DRIVER_MODULE(tbgpio, macgpio, tbgpio_driver, 0, 0, BUS_PASS_CPU);
static int
tbgpio_probe(device_t dev)
{
phandle_t node;
const char *name;
pcell_t pfunc[32];
int res;
name = ofw_bus_get_name(dev);
node = ofw_bus_get_node(dev);
if (strcmp(name, "timebase-enable") != 0)
return (ENXIO);
res = OF_getencprop(node, "platform-do-cpu-timebase", pfunc,
sizeof(pfunc));
if (res == -1)
return (ENXIO);
if (res != 20 || pfunc[2] != 0x01) {
printf("\nUnknown platform function detected!\n");
printf("Please send a PR including the following data:\n");
printf("===================\n");
printf("Func: platform-do-cpu-timebase\n");
hexdump(pfunc, res, NULL, HD_OMIT_CHARS);
printf("===================\n");
return (ENXIO);
}
device_set_desc(dev, "CPU Timebase Control");
return (BUS_PROBE_SPECIFIC);
}
static int
tbgpio_attach(device_t dev)
{
phandle_t node;
struct tbgpio_softc *sc;
pcell_t pfunc[5];
sc = device_get_softc(dev);
node = ofw_bus_get_node(dev);
OF_getencprop(node, "platform-do-cpu-timebase", pfunc, sizeof(pfunc));
sc->sc_value = pfunc[3];
sc->sc_mask = pfunc[4];
powermac_register_timebase(dev, tbgpio_freeze_timebase);
return (0);
}
static void
tbgpio_freeze_timebase(device_t dev, bool freeze)
{
struct tbgpio_softc *sc;
uint32_t val;
sc = device_get_softc(dev);
val = sc->sc_value;
if (freeze)
val = ~val;
val &= sc->sc_mask;
macgpio_write(dev, val);
}