#include <sys/param.h>
#include <sys/bus.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/mutex.h>
#include <sys/rman.h>
#include <machine/bus.h>
#include <dev/fdt/simplebus.h>
#include <dev/clk/clk.h>
#include <dev/clk/clk_fixed.h>
#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_bus_subr.h>
#include "clkdev_if.h"
#include "periph.h"
#define TBG_COUNT 4
#define XTAL_OFW_INDEX 4
static struct resource_spec a37x0_periph_clk_spec[] = {
{ SYS_RES_MEMORY, 0, RF_ACTIVE },
{ -1, 0 }
};
int
a37x0_periph_clk_attach(device_t dev)
{
struct a37x0_periph_clknode_def *dev_defs;
struct a37x0_periph_clk_softc *sc;
const char *tbg_clocks[5];
const char *xtal_clock;
phandle_t node;
int error, i;
clk_t clock;
sc = device_get_softc(dev);
node = ofw_bus_get_node(dev);
sc->dev = dev;
mtx_init(&sc->mtx, device_get_nameunit(dev), NULL, MTX_DEF);
if (bus_alloc_resources(dev, a37x0_periph_clk_spec, &sc->res) != 0) {
device_printf(dev, "Cannot allocate resources\n");
return (ENXIO);
}
sc->clkdom = clkdom_create(dev);
if (sc->clkdom == NULL) {
device_printf(dev, "Cannot create clock domain\n");
return (ENXIO);
}
for (i = 0; i < TBG_COUNT; i++){
error = clk_get_by_ofw_index(dev, node, i, &clock);
if (error)
goto fail;
tbg_clocks[i] = clk_get_name(clock);
}
error = clk_get_by_ofw_index(dev, node, XTAL_OFW_INDEX, &clock);
if (error)
goto fail;
xtal_clock = clk_get_name(clock);
dev_defs = sc->devices;
for (i = 0; i< sc->device_count; i++) {
dev_defs[i].common_def.tbgs = tbg_clocks;
dev_defs[i].common_def.xtal = xtal_clock;
dev_defs[i].common_def.tbg_cnt = TBG_COUNT;
switch (dev_defs[i].type) {
case CLK_FULL_DD:
error = a37x0_periph_d_register_full_clk_dd(
sc->clkdom, &dev_defs[i]);
if (error)
goto fail;
break;
case CLK_FULL:
error = a37x0_periph_d_register_full_clk(
sc->clkdom, &dev_defs[i]);
if (error)
goto fail;
break;
case CLK_GATE:
error = a37x0_periph_gate_register_gate(
sc->clkdom, &dev_defs[i]);
if (error)
goto fail;
break;
case CLK_MUX_GATE:
error = a37x0_periph_register_mux_gate(
sc->clkdom, &dev_defs[i]);
if (error)
goto fail;
break;
case CLK_FIXED:
error = a37x0_periph_fixed_register_fixed(
sc->clkdom, &dev_defs[i]);
if (error)
goto fail;
break;
case CLK_CPU:
error = a37x0_periph_d_register_periph_cpu(
sc->clkdom, &dev_defs[i]);
if (error)
goto fail;
break;
case CLK_MDD:
error = a37x0_periph_d_register_mdd(
sc->clkdom, &dev_defs[i]);
if (error)
goto fail;
break;
case CLK_MUX_GATE_FIXED:
error = a37x0_periph_register_mux_gate_fixed(
sc->clkdom, &dev_defs[i]);
if (error)
goto fail;
break;
default:
return (ENXIO);
}
}
error = clkdom_finit(sc->clkdom);
if (error)
goto fail;
if (bootverbose)
clkdom_dump(sc->clkdom);
return (0);
fail:
bus_release_resources(dev, a37x0_periph_clk_spec, &sc->res);
return (error);
}
int
a37x0_periph_clk_read_4(device_t dev, bus_addr_t addr, uint32_t *val)
{
struct a37x0_periph_clk_softc *sc;
sc = device_get_softc(dev);
*val = bus_read_4(sc->res, addr);
return (0);
}
void
a37x0_periph_clk_device_lock(device_t dev)
{
struct a37x0_periph_clk_softc *sc;
sc = device_get_softc(dev);
mtx_lock(&sc->mtx);
}
void
a37x0_periph_clk_device_unlock(device_t dev)
{
struct a37x0_periph_clk_softc *sc;
sc = device_get_softc(dev);
mtx_unlock(&sc->mtx);
}
int
a37x0_periph_clk_detach(device_t dev)
{
return (EBUSY);
}