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