No empty .Rs/.Re
[netbsd-mini2440.git] / sys / arch / evbarm / mpcsa / mpcsa_spi.c
blob3212d1ad7a8be6e98e29aa9fbb312a51bb793d9a
1 /* $Id: mpcsa_spi.c,v 1.2 2008/07/03 01:15:39 matt Exp $ */
2 /* $NetBSD$ */
4 /*
5 * Copyright (c) 2007 Embedtronics Oy. All rights reserved.
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.
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.
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD$");
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>
42 #ifdef MPCSA_SPI_DEBUG
43 int 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
49 struct at91pio_softc;
51 struct mpcsa_spi_softc {
52 struct at91spi_softc sc_dev;
53 struct at91pio_softc *sc_pioa, *sc_piod;
56 static int mpcsa_spi_match(struct device *, struct cfdata *, void *);
57 static void mpcsa_spi_attach(struct device *, struct device *, void *);
59 CFATTACH_DECL(mpcsa_spi, sizeof(struct mpcsa_spi_softc),
60 mpcsa_spi_match, mpcsa_spi_attach, NULL, NULL);
62 static int mpcsa_spi_select(void *self, int sel);
64 struct at91spi_machdep mpcsa_spi_tag = {
65 mpcsa_spi_select
68 static int
69 mpcsa_spi_match(struct device *parent, struct cfdata *match, void *aux)
71 if (strcmp(match->cf_name, "at91spi") == 0 && strcmp(match->cf_atname, "mpcsa_spi") == 0)
72 return 2;
73 return 0;
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)
81 static void
82 mpcsa_spi_attach(struct device *parent, struct device *self, void *aux)
84 struct mpcsa_spi_softc *sc = (struct mpcsa_spi_softc *)self;
86 // do some checks
87 if ((sc->sc_pioa = at91pio_sc(AT91_PIOA)) == NULL) {
88 printf("no PIOA!\n");
89 return;
91 if ((sc->sc_piod = at91pio_sc(AT91_PIOD)) == NULL) {
92 printf("no PIOD!\n");
93 return;
96 // initialize softc
97 sc->sc_dev.sc_spi.sct_nslaves = 3; // number of slaves
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);
104 // and call common routine
105 at91spi_attach_common(parent, self, aux, &mpcsa_spi_tag);
108 static int mpcsa_spi_select(void *self, int sel)
110 struct mpcsa_spi_softc *sc = (struct mpcsa_spi_softc *)self;
112 /* first deselect everything */
113 GPIO_SPICS0(at91pio_set);
114 GPIO_SPICS1(at91pio_set);
115 GPIO_SPICS2(at91pio_set);
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;
134 return 0;