1/* $Id: mpcsa_spi.c,v 1.2 2008/07/03 01:15:39 matt Exp $ */ 2/* $NetBSD$ */ 3 4/* 5 * Copyright (c) 2007 Embedtronics Oy. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29#include <sys/cdefs.h> 30__KERNEL_RCSID(0, "$NetBSD$"); 31 32#include <sys/param.h> 33#include <sys/systm.h> 34#include <sys/kernel.h> 35#include <sys/device.h> 36#include <sys/lock.h> 37#include <arm/at91/at91reg.h> 38#include <arm/at91/at91var.h> 39#include <arm/at91/at91spivar.h> 40#include <arm/at91/at91piovar.h> 41 42#ifdef MPCSA_SPI_DEBUG 43int mpcsa_spi_debug = MPCSA_SPI_DEBUG; 44#define DPRINTFN(n,x) if (mpcsa_spi_debug>(n)) printf x; 45#else 46#define DPRINTFN(n,x) 47#endif 48 49struct at91pio_softc; 50 51struct mpcsa_spi_softc { 52 struct at91spi_softc sc_dev; 53 struct at91pio_softc *sc_pioa, *sc_piod; 54}; 55 56static int mpcsa_spi_match(struct device *, struct cfdata *, void *); 57static void mpcsa_spi_attach(struct device *, struct device *, void *); 58 59CFATTACH_DECL(mpcsa_spi, sizeof(struct mpcsa_spi_softc), 60 mpcsa_spi_match, mpcsa_spi_attach, NULL, NULL); 61 62static int mpcsa_spi_select(void *self, int sel); 63 64struct at91spi_machdep mpcsa_spi_tag = { 65 mpcsa_spi_select 66}; 67 68static int 69mpcsa_spi_match(struct device *parent, struct cfdata *match, void *aux) 70{ 71 if (strcmp(match->cf_name, "at91spi") == 0 && strcmp(match->cf_atname, "mpcsa_spi") == 0) 72 return 2; 73 return 0; 74} 75 76#define GPIO_SPICS0(func) (func)(sc->sc_pioa, 3) 77#define GPIO_SPICS1(func) (func)(sc->sc_pioa, 4) 78#define GPIO_SPICS2(func) (func)(sc->sc_piod, 19) 79 80 81static void 82mpcsa_spi_attach(struct device *parent, struct device *self, void *aux) 83{ 84 struct mpcsa_spi_softc *sc = (struct mpcsa_spi_softc *)self; 85 86 // do some checks 87 if ((sc->sc_pioa = at91pio_sc(AT91_PIOA)) == NULL) { 88 printf("no PIOA!\n"); 89 return; 90 } 91 if ((sc->sc_piod = at91pio_sc(AT91_PIOD)) == NULL) { 92 printf("no PIOD!\n"); 93 return; 94 } 95 96 // initialize softc 97 sc->sc_dev.sc_spi.sct_nslaves = 3; // number of slaves 98 99 // configure GPIO 100 GPIO_SPICS0(at91pio_out); GPIO_SPICS0(at91pio_set); 101 GPIO_SPICS1(at91pio_out); GPIO_SPICS1(at91pio_set); 102 GPIO_SPICS2(at91pio_out); GPIO_SPICS2(at91pio_set); 103 104 // and call common routine 105 at91spi_attach_common(parent, self, aux, &mpcsa_spi_tag); 106} 107 108static int mpcsa_spi_select(void *self, int sel) 109{ 110 struct mpcsa_spi_softc *sc = (struct mpcsa_spi_softc *)self; 111 112 /* first deselect everything */ 113 GPIO_SPICS0(at91pio_set); 114 GPIO_SPICS1(at91pio_set); 115 GPIO_SPICS2(at91pio_set); 116 117 /* then select wanted target */ 118 switch (sel) { 119 case -1: 120 break; 121 case 0: 122 GPIO_SPICS0(at91pio_clear); 123 break; 124 case 1: 125 GPIO_SPICS2(at91pio_clear); 126 break; 127 case 2: 128 GPIO_SPICS2(at91pio_clear); 129 break; 130 default: 131 return EINVAL; 132 } 133 134 return 0; 135} 136