#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: mpcsa_spi.c,v 1.3 2012/10/27 17:17:48 chs Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/device.h>
#include <sys/lock.h>
#include <arm/at91/at91reg.h>
#include <arm/at91/at91var.h>
#include <arm/at91/at91spivar.h>
#include <arm/at91/at91piovar.h>
#ifdef MPCSA_SPI_DEBUG
int mpcsa_spi_debug = MPCSA_SPI_DEBUG;
#define DPRINTFN(n,x) if (mpcsa_spi_debug>(n)) printf x;
#else
#define DPRINTFN(n,x)
#endif
struct at91pio_softc;
struct mpcsa_spi_softc {
struct at91spi_softc sc_at91spi;
struct at91pio_softc *sc_pioa, *sc_piod;
};
static int mpcsa_spi_match(device_t, cfdata_t, void *);
static void mpcsa_spi_attach(device_t, device_t, void *);
CFATTACH_DECL_NEW(mpcsa_spi, sizeof(struct mpcsa_spi_softc),
mpcsa_spi_match, mpcsa_spi_attach, NULL, NULL);
static int mpcsa_spi_select(void *self, int sel);
struct at91spi_machdep mpcsa_spi_tag = {
mpcsa_spi_select
};
static int
mpcsa_spi_match(device_t parent, cfdata_t match, void *aux)
{
if (strcmp(match->cf_name, "at91spi") == 0 && strcmp(match->cf_atname, "mpcsa_spi") == 0)
return 2;
return 0;
}
#define GPIO_SPICS0(func) (func)(sc->sc_pioa, 3)
#define GPIO_SPICS1(func) (func)(sc->sc_pioa, 4)
#define GPIO_SPICS2(func) (func)(sc->sc_piod, 19)
static void
mpcsa_spi_attach(device_t parent, device_t self, void *aux)
{
struct mpcsa_spi_softc *sc = device_private(self);
if ((sc->sc_pioa = at91pio_sc(AT91_PIOA)) == NULL) {
printf("no PIOA!\n");
return;
}
if ((sc->sc_piod = at91pio_sc(AT91_PIOD)) == NULL) {
printf("no PIOD!\n");
return;
}
sc->sc_at91spi.sc_spi.sct_nslaves = 3;
GPIO_SPICS0(at91pio_out); GPIO_SPICS0(at91pio_set);
GPIO_SPICS1(at91pio_out); GPIO_SPICS1(at91pio_set);
GPIO_SPICS2(at91pio_out); GPIO_SPICS2(at91pio_set);
at91spi_attach_common(parent, self, aux, &mpcsa_spi_tag);
}
static int mpcsa_spi_select(void *self, int sel)
{
struct mpcsa_spi_softc *sc = device_private(self);
GPIO_SPICS0(at91pio_set);
GPIO_SPICS1(at91pio_set);
GPIO_SPICS2(at91pio_set);
switch (sel) {
case -1:
break;
case 0:
GPIO_SPICS0(at91pio_clear);
break;
case 1:
GPIO_SPICS2(at91pio_clear);
break;
case 2:
GPIO_SPICS2(at91pio_clear);
break;
default:
return EINVAL;
}
return 0;
}