#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: sacc_obio.c,v 1.17 2023/07/13 21:29:49 riastradh Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/mbuf.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/errno.h>
#include <sys/syslog.h>
#include <sys/select.h>
#include <sys/device.h>
#include <sys/bitops.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_ether.h>
#include <net/if_media.h>
#include <machine/intr.h>
#include <sys/bus.h>
#include <arm/sa11x0/sa1111_reg.h>
#include <arm/sa11x0/sa1111_var.h>
#include <arm/xscale/pxa2x0cpu.h>
#include <arm/xscale/pxa2x0reg.h>
#include <arm/xscale/pxa2x0var.h>
#include <arm/xscale/pxa2x0_gpio.h>
#include <evbarm/lubbock/lubbock_reg.h>
#include <evbarm/lubbock/lubbock_var.h>
static int sacc_obio_probe(device_t parent, cfdata_t match, void *aux);
static void sacc_obio_attach(device_t, device_t, void *);
static int sacc_obio_intr(void *arg);
CFATTACH_DECL_NEW(sacc_obio, sizeof(struct sacc_softc), sacc_obio_probe,
sacc_obio_attach, NULL, NULL);
#if 0
#define DPRINTF(arg) aprint_normal arg
#else
#define DPRINTF(arg)
#endif
uint16_t cs2_memctl_init = 0x7ff0;
static int
sacc_obio_probe(device_t parent, cfdata_t match, void *aux)
{
struct obio_attach_args *oa = aux;
struct sa11x0_attach_args sa;
printf("%s: addr=%lx\n", __func__, oa->oba_addr);
sa.sa_sc = oa->oba_sc;
sa.sa_iot = oa->oba_iot;
sa.sa_addr = oa->oba_addr;
sa.sa_size = 0x2000;
return sacc_probe(parent, match, &sa);
}
static void
sacc_obio_attach(device_t parent, device_t self, void *aux)
{
int i;
uint32_t skid, tmp;
struct sacc_softc *sc = device_private(self);
struct obio_softc *psc = device_private(parent);
struct obio_attach_args *sa = aux;
bus_space_tag_t iot = sa->oba_iot;
bus_space_handle_t memctl_ioh;
aprint_normal("\n");
for (i=48; i <= 55; ++i)
pxa2x0_gpio_set_function(i, GPIO_ALT_FN_2_OUT);
pxa2x0_gpio_set_function(56, GPIO_ALT_FN_1_IN);
pxa2x0_gpio_set_function(57, GPIO_ALT_FN_1_IN);
if (bus_space_map(iot, PXA2X0_MEMCTL_BASE, PXA2X0_MEMCTL_SIZE, 0,
&memctl_ioh))
goto fail;
tmp = bus_space_read_4(iot, memctl_ioh, MEMCTL_MSC2 );
bus_space_write_4(iot, memctl_ioh, MEMCTL_MSC2,
(tmp & 0xffff0000) | cs2_memctl_init );
bus_space_unmap(iot, memctl_ioh, PXA2X0_MEMCTL_SIZE);
sc->sc_dev = self;
sc->sc_piot = sc->sc_iot = iot;
sc->sc_gpioh = 0;
if (bus_space_map(iot, sa->oba_addr, 0x2000, 0, &sc->sc_ioh))
goto fail;
skid = bus_space_read_4(sc->sc_iot, sc->sc_ioh, SACCSBI_SKID);
aprint_normal_dev(self, "SA1111 rev %d.%d\n",
(skid & 0xf0) >> 4, skid & 0xf);
tmp = bus_space_read_4(sc->sc_iot, sc->sc_ioh, SACCSBI_SKCR);
tmp = (tmp & ~SKCR_VCOOFF) | SKCR_PLLBYPASS;
bus_space_write_4( sc->sc_iot, sc->sc_ioh, SACCSBI_SKCR, tmp );
delay(100);
tmp |= SKCR_RCLKEN;
bus_space_write_4( sc->sc_iot, sc->sc_ioh, SACCSBI_SKCR, tmp );
#if 1
if( tmp != bus_space_read_4( sc->sc_iot, sc->sc_ioh, SACCSBI_SKCR ) )
printf( "!!! FAIL SKCR\n" );
#endif
bus_space_write_4( sc->sc_iot, sc->sc_ioh, SACCGPIOA_DVR, 0 );
bus_space_write_4( sc->sc_iot, sc->sc_ioh, SACCGPIOA_DDR, 0 );
for(i = 0; i < SACCIC_LEN; i++)
sc->sc_intrhand[i] = NULL;
bus_space_write_4(sc->sc_iot, sc->sc_ioh, SACCIC_INTEN0, 0);
bus_space_write_4(sc->sc_iot, sc->sc_ioh, SACCIC_INTEN1, 0);
bus_space_write_4(sc->sc_iot, sc->sc_ioh, SACCIC_INTTSTSEL, 0);
bus_space_write_4(sc->sc_iot, sc->sc_ioh,
SACCIC_INTSTATCLR0, 0xffffffff);
bus_space_write_4(sc->sc_iot, sc->sc_ioh,
SACCIC_INTSTATCLR1, 0xffffffff);
obio_intr_establish(psc, sa->oba_intr,
IPL_HIGH, sacc_obio_intr, sc );
config_search(self, NULL,
CFARGS(.search = sa1111_search));
return;
fail:
aprint_normal_dev(self, "unable to map registers\n");
}
static int
sacc_obio_intr(void *arg)
{
int i;
struct sacc_intrvec intstat;
struct sacc_softc *sc = arg;
struct sacc_intrhand *ih;
intstat.lo =
bus_space_read_4(sc->sc_iot, sc->sc_ioh, SACCIC_INTSTATCLR0);
intstat.hi =
bus_space_read_4(sc->sc_iot, sc->sc_ioh, SACCIC_INTSTATCLR1);
DPRINTF(("sacc_obio_intr_dispatch: %x %x\n", intstat.lo, intstat.hi));
while ((i = fls32(intstat.lo) - 1) >= 0) {
bus_space_write_4(sc->sc_iot, sc->sc_ioh,
SACCIC_INTSTATCLR0, 1U<<i );
for(ih = sc->sc_intrhand[i]; ih; ih = ih->ih_next)
softint_schedule(ih->ih_soft);
intstat.lo &= ~(1U<<i);
}
while ((i = fls32(intstat.hi) - 1) >= 0) {
bus_space_write_4(sc->sc_iot, sc->sc_ioh,
SACCIC_INTSTATCLR1, 1U<<i);
for(ih = sc->sc_intrhand[i + 32]; ih; ih = ih->ih_next)
softint_schedule(ih->ih_soft);
intstat.hi &= ~(1U<<i);
}
return 1;
}