#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bus.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/malloc.h>
#include <sys/rman.h>
#include <sys/timeet.h>
#include <sys/timetc.h>
#include <dev/ofw/openfirm.h>
#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_bus_subr.h>
#include <machine/bus.h>
#include <machine/cpu.h>
#include <machine/intr.h>
#include <arm/freescale/vybrid/vf_common.h>
#include <arm/freescale/vybrid/vf_adc.h>
#define ADC_HC0 0x00
#define ADC_HC1 0x04
#define HC_AIEN (1 << 7)
#define HC_ADCH_M 0x1f
#define HC_ADCH_S 0
#define ADC_HS 0x08
#define HS_COCO0 (1 << 0)
#define HS_COCO1 (1 << 1)
#define ADC_R0 0x0C
#define ADC_R1 0x10
#define ADC_CFG 0x14
#define CFG_OVWREN (1 << 16)
#define CFG_AVGS_M 0x3
#define CFG_AVGS_S 14
#define CFG_ADTRG (1 << 13)
#define CFG_REFSEL_M 0x3
#define CFG_REFSEL_S 11
#define CFG_ADHSC (1 << 10)
#define CFG_ADSTS_M 0x3
#define CFG_ADSTS_S 8
#define CFG_ADLPC (1 << 7)
#define CFG_ADIV_M 0x3
#define CFG_ADIV_S 5
#define CFG_ADLSMP (1 << 4)
#define CFG_MODE_M 0x3
#define CFG_MODE_S 2
#define CFG_MODE_12 0x2
#define CFG_ADICLK_M 0x3
#define CFG_ADICLK_S 0
#define ADC_GC 0x18
#define GC_CAL (1 << 7)
#define GC_ADCO (1 << 6)
#define GC_AVGE (1 << 5)
#define GC_ACFE (1 << 4)
#define GC_ACFGT (1 << 3)
#define GC_ACREN (1 << 2)
#define GC_DMAEN (1 << 1)
#define GC_ADACKEN (1 << 0)
#define ADC_GS 0x1C
#define GS_AWKST (1 << 2)
#define GS_CALF (1 << 1)
#define GS_ADACT (1 << 0)
#define ADC_CV 0x20
#define CV_CV2_M 0xfff
#define CV_CV2_S 16
#define CV_CV1_M 0xfff
#define CV_CV1_S 0
#define ADC_OFS 0x24
#define OFS_SIGN 12
#define OFS_M 0xfff
#define OFS_S 0
#define ADC_CAL 0x28
#define CAL_CODE_M 0xf
#define CAL_CODE_S 0
#define ADC_PCTL 0x30
struct adc_softc {
struct resource *res[2];
bus_space_tag_t bst;
bus_space_handle_t bsh;
void *ih;
};
struct adc_softc *adc_sc;
static struct resource_spec adc_spec[] = {
{ SYS_RES_MEMORY, 0, RF_ACTIVE },
{ SYS_RES_IRQ, 0, RF_ACTIVE },
{ -1, 0 }
};
static int
adc_probe(device_t dev)
{
if (!ofw_bus_status_okay(dev))
return (ENXIO);
if (!ofw_bus_is_compatible(dev, "fsl,mvf600-adc"))
return (ENXIO);
device_set_desc(dev, "Vybrid Family "
"12-bit Analog to Digital Converter");
return (BUS_PROBE_DEFAULT);
}
static void
adc_intr(void *arg)
{
}
uint32_t
adc_read(void)
{
struct adc_softc *sc;
sc = adc_sc;
if (sc == NULL)
return (0);
return (READ4(sc, ADC_R0));
}
uint32_t
adc_enable(int channel)
{
struct adc_softc *sc;
int reg;
sc = adc_sc;
if (sc == NULL)
return (1);
reg = READ4(sc, ADC_HC0);
reg &= ~(HC_ADCH_M << HC_ADCH_S);
reg |= (channel << HC_ADCH_S);
WRITE4(sc, ADC_HC0, reg);
return (0);
}
static int
adc_attach(device_t dev)
{
struct adc_softc *sc;
int err;
int reg;
sc = device_get_softc(dev);
if (bus_alloc_resources(dev, adc_spec, sc->res)) {
device_printf(dev, "could not allocate resources\n");
return (ENXIO);
}
sc->bst = rman_get_bustag(sc->res[0]);
sc->bsh = rman_get_bushandle(sc->res[0]);
adc_sc = sc;
err = bus_setup_intr(dev, sc->res[1], INTR_TYPE_BIO | INTR_MPSAFE,
NULL, adc_intr, sc, &sc->ih);
if (err) {
device_printf(dev, "Unable to alloc interrupt resource.\n");
return (ENXIO);
}
reg = READ4(sc, ADC_CFG);
reg &= ~(CFG_MODE_M << CFG_MODE_S);
reg |= (CFG_MODE_12 << CFG_MODE_S);
WRITE4(sc, ADC_CFG, reg);
reg = READ4(sc, ADC_GC);
reg |= (GC_ADCO | GC_AVGE);
WRITE4(sc, ADC_GC, reg);
reg = READ4(sc, ADC_HC0);
reg &= HC_AIEN;
WRITE4(sc, ADC_HC0, reg);
return (0);
}
static device_method_t adc_methods[] = {
DEVMETHOD(device_probe, adc_probe),
DEVMETHOD(device_attach, adc_attach),
{ 0, 0 }
};
static driver_t adc_driver = {
"adc",
adc_methods,
sizeof(struct adc_softc),
};
DRIVER_MODULE(adc, simplebus, adc_driver, 0, 0);