#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: bcm2835_cm.c,v 1.3 2021/01/27 03:10:19 thorpej Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/kernel.h>
#include <sys/bus.h>
#include <arm/broadcom/bcm2835reg.h>
#include <arm/broadcom/bcm2835_cm.h>
#include <dev/fdt/fdtvar.h>
#include <arm/fdt/arm_fdtvar.h>
struct bcm2835cm_softc {
device_t sc_dev;
bus_space_tag_t sc_iot;
bus_space_handle_t sc_ioh;
};
#define CM_WRITE(sc, reg, val) \
bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (reg), (val))
#define CM_READ(sc, reg) \
bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg))
static int bcmcm_match(device_t, cfdata_t, void *);
static void bcmcm_attach(device_t, device_t, void *);
static int bcmcm_wait(struct bcm2835cm_softc *, int, int);
CFATTACH_DECL_NEW(bcmcm_fdt, sizeof(struct bcm2835cm_softc),
bcmcm_match, bcmcm_attach, NULL, NULL);
static const struct device_compatible_entry compat_data[] = {
{ .compat = "brcm,bcm2835-cprman" },
DEVICE_COMPAT_EOL
};
static int
bcmcm_match(device_t parent, cfdata_t match, void *aux)
{
struct fdt_attach_args * const faa = aux;
return of_compatible_match(faa->faa_phandle, compat_data);
}
static void
bcmcm_attach(device_t parent, device_t self, void *aux)
{
struct bcm2835cm_softc *sc = device_private(self);
struct fdt_attach_args * const faa = aux;
aprint_naive("\n");
aprint_normal(": CM\n");
sc->sc_dev = self;
sc->sc_iot = faa->faa_bst;
const int phandle = faa->faa_phandle;
bus_addr_t addr;
bus_size_t size;
if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
aprint_error(": missing 'reg' property\n");
return;
}
if (bus_space_map(faa->faa_bst, addr, size, 0, &sc->sc_ioh)) {
aprint_error_dev(sc->sc_dev, "unable to map device\n");
return;
}
return;
}
static int
bcmcm_wait(struct bcm2835cm_softc *sc, int ctlreg, int onoff)
{
int i;
uint32_t r;
for (i=0; i<100; ++i) {
r = CM_READ(sc, ctlreg);
if (((r & CM_CTL_BUSY) != 0) == onoff)
break;
delay(10);
}
if (i >= 100) {
device_printf(sc->sc_dev, "busy (addr=%#x)\n", ctlreg);
return EIO;
}
return 0;
}
int
bcm_cm_set(enum bcm_cm_clock clk, uint32_t ctl, uint32_t div)
{
struct bcm2835cm_softc *sc;
device_t dev;
int ctlreg, divreg;
uint32_t r;
dev = device_find_by_driver_unit("bcmcm", 0);
if (dev == NULL)
return ENXIO;
sc = device_private(dev);
switch (clk) {
case BCM_CM_GP0:
ctlreg = CM_GP0CTL;
divreg = CM_GP0DIV;
break;
case BCM_CM_GP1:
ctlreg = CM_GP1CTL;
divreg = CM_GP1DIV;
break;
case BCM_CM_GP2:
ctlreg = CM_GP2CTL;
divreg = CM_GP2DIV;
break;
case BCM_CM_PCM:
ctlreg = CM_PCMCTL;
divreg = CM_PCMDIV;
break;
case BCM_CM_PWM:
ctlreg = CM_PWMCTL;
divreg = CM_PWMDIV;
break;
default:
return EINVAL;
}
ctl &= ~CM_CTL_PASSWD;
ctl |= __SHIFTIN(CM_PASSWD, CM_CTL_PASSWD);
div &= ~CM_DIV_PASSWD;
div |= __SHIFTIN(CM_PASSWD, CM_DIV_PASSWD);
r = CM_READ(sc, ctlreg);
if (r & CM_CTL_ENAB) {
r &= ~CM_CTL_PASSWD;
r |= __SHIFTIN(CM_PASSWD, CM_CTL_PASSWD);
r &= ~CM_CTL_ENAB;
CM_WRITE(sc, ctlreg, r);
}
bcmcm_wait(sc, ctlreg, 0);
CM_WRITE(sc, divreg, div);
CM_WRITE(sc, ctlreg, ctl & ~CM_CTL_ENAB);
if (ctl & CM_CTL_ENAB) {
CM_WRITE(sc, ctlreg, ctl);
return bcmcm_wait(sc, ctlreg, 1);
}
return 0;
}
int
bcm_cm_get(enum bcm_cm_clock clk, uint32_t *ctlp, uint32_t *divp)
{
struct bcm2835cm_softc *sc;
device_t dev;
int ctlreg, divreg;
dev = device_find_by_driver_unit("bcmcm", 0);
if (dev == NULL)
return ENXIO;
sc = device_private(dev);
switch (clk) {
case BCM_CM_GP0:
ctlreg = CM_GP0CTL;
divreg = CM_GP0DIV;
break;
case BCM_CM_GP1:
ctlreg = CM_GP1CTL;
divreg = CM_GP1DIV;
break;
case BCM_CM_GP2:
ctlreg = CM_GP2CTL;
divreg = CM_GP2DIV;
break;
case BCM_CM_PCM:
ctlreg = CM_PCMCTL;
divreg = CM_PCMDIV;
break;
case BCM_CM_PWM:
ctlreg = CM_PWMCTL;
divreg = CM_PWMDIV;
break;
default:
return EINVAL;
}
if (ctlp != NULL)
*ctlp = CM_READ(sc, ctlreg);
if (divp != NULL)
*divp = CM_READ(sc, divreg);
return 0;
}