s3c2410_spi.c revision 1.6 1 1.6 dyoung /* $NetBSD: s3c2410_spi.c,v 1.6 2011/07/01 20:31:39 dyoung Exp $ */
2 1.1 bsh
3 1.1 bsh /*
4 1.1 bsh * Copyright (c) 2004 Genetec Corporation. All rights reserved.
5 1.1 bsh * Written by Hiroyuki Bessho for Genetec Corporation.
6 1.1 bsh *
7 1.1 bsh * Redistribution and use in source and binary forms, with or without
8 1.1 bsh * modification, are permitted provided that the following conditions
9 1.1 bsh * are met:
10 1.1 bsh * 1. Redistributions of source code must retain the above copyright
11 1.1 bsh * notice, this list of conditions and the following disclaimer.
12 1.1 bsh * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 bsh * notice, this list of conditions and the following disclaimer in the
14 1.1 bsh * documentation and/or other materials provided with the distribution.
15 1.1 bsh * 3. The name of Genetec Corporation may not be used to endorse or
16 1.1 bsh * promote products derived from this software without specific prior
17 1.1 bsh * written permission.
18 1.1 bsh *
19 1.1 bsh * THIS SOFTWARE IS PROVIDED BY GENETEC CORPORATION ``AS IS'' AND
20 1.1 bsh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 bsh * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 bsh * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GENETEC CORPORATION
23 1.1 bsh * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 bsh * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 bsh * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 bsh * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 bsh * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 bsh * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 bsh * POSSIBILITY OF SUCH DAMAGE.
30 1.1 bsh */
31 1.1 bsh
32 1.1 bsh /*
33 1.1 bsh * Support S3C2410's SPI dirver.
34 1.1 bsh * Real works are done by drivers attached to SPI ports.
35 1.1 bsh */
36 1.1 bsh
37 1.1 bsh #include <sys/cdefs.h>
38 1.6 dyoung __KERNEL_RCSID(0, "$NetBSD: s3c2410_spi.c,v 1.6 2011/07/01 20:31:39 dyoung Exp $");
39 1.1 bsh
40 1.1 bsh #include <sys/param.h>
41 1.1 bsh #include <sys/systm.h>
42 1.1 bsh #include <sys/conf.h>
43 1.1 bsh
44 1.6 dyoung #include <sys/bus.h>
45 1.1 bsh #include <machine/cpu.h>
46 1.1 bsh
47 1.1 bsh #include <arm/s3c2xx0/s3c24x0var.h>
48 1.1 bsh #include <arm/s3c2xx0/s3c24x0reg.h>
49 1.1 bsh #include <arm/s3c2xx0/s3c2410reg.h>
50 1.1 bsh
51 1.1 bsh #include <arm/s3c2xx0/s3c24x0_spi.h>
52 1.1 bsh
53 1.1 bsh #include "locators.h"
54 1.1 bsh
55 1.1 bsh struct ssspi_softc {
56 1.1 bsh struct device dev;
57 1.1 bsh
58 1.1 bsh bus_space_tag_t iot;
59 1.1 bsh bus_space_handle_t ioh;
60 1.1 bsh short index;
61 1.1 bsh };
62 1.1 bsh
63 1.1 bsh
64 1.1 bsh /* prototypes */
65 1.1 bsh static int ssspi_match(struct device *, struct cfdata *, void *);
66 1.1 bsh static void ssspi_attach(struct device *, struct device *, void *);
67 1.2 drochner static int ssspi_search(struct device *, struct cfdata *,
68 1.3 drochner const int *, void *);
69 1.1 bsh static int ssspi_print(void *, const char *);
70 1.1 bsh
71 1.1 bsh /* attach structures */
72 1.1 bsh CFATTACH_DECL(ssspi, sizeof(struct ssspi_softc), ssspi_match, ssspi_attach,
73 1.1 bsh NULL, NULL);
74 1.1 bsh
75 1.1 bsh
76 1.1 bsh static int
77 1.1 bsh ssspi_print(void *aux, const char *name)
78 1.1 bsh {
79 1.1 bsh struct ssspi_attach_args *spia = aux;
80 1.1 bsh
81 1.1 bsh if (spia->spia_aux_intr != SSSPICF_INTR_DEFAULT)
82 1.1 bsh printf(" intr %d", spia->spia_aux_intr);
83 1.1 bsh return (UNCONF);
84 1.1 bsh }
85 1.1 bsh
86 1.1 bsh int
87 1.1 bsh ssspi_match(struct device *parent, struct cfdata *match, void *aux)
88 1.1 bsh {
89 1.1 bsh struct s3c2xx0_attach_args *sa = aux;
90 1.1 bsh
91 1.1 bsh /* S3C2410 have only two SPIs */
92 1.1 bsh switch (sa->sa_index) {
93 1.1 bsh case 0:
94 1.1 bsh case 1:
95 1.1 bsh break;
96 1.1 bsh default:
97 1.1 bsh return 0;
98 1.1 bsh }
99 1.1 bsh
100 1.1 bsh return 1;
101 1.1 bsh }
102 1.1 bsh
103 1.1 bsh void
104 1.1 bsh ssspi_attach(struct device *parent, struct device *self, void *aux)
105 1.1 bsh {
106 1.1 bsh struct ssspi_softc *sc = (struct ssspi_softc*)self;
107 1.1 bsh struct s3c2xx0_attach_args *sa = (struct s3c2xx0_attach_args *)aux;
108 1.1 bsh bus_space_tag_t iot = sa->sa_iot;
109 1.1 bsh
110 1.1 bsh static bus_space_handle_t spi_ioh = 0;
111 1.1 bsh
112 1.1 bsh /* we map all registers for SPI0 and SPI1 at once, then
113 1.1 bsh use subregions */
114 1.1 bsh if (spi_ioh == 0) {
115 1.1 bsh if (bus_space_map(iot, S3C2410_SPI0_BASE,
116 1.1 bsh 2 * S3C24X0_SPI_SIZE,
117 1.1 bsh 0, &spi_ioh)) {
118 1.1 bsh aprint_error(": can't map registers\n");
119 1.1 bsh return;
120 1.1 bsh }
121 1.1 bsh }
122 1.1 bsh
123 1.1 bsh aprint_normal("\n");
124 1.1 bsh
125 1.1 bsh sc->index = sa->sa_index;
126 1.1 bsh sc->iot = iot;
127 1.1 bsh
128 1.1 bsh bus_space_subregion(iot, spi_ioh, sc->index == 0 ? 0 : S3C24X0_SPI_SIZE,
129 1.1 bsh S3C24X0_SPI_SIZE, &sc->ioh);
130 1.1 bsh
131 1.1 bsh /*
132 1.1 bsh * Attach child devices
133 1.1 bsh */
134 1.2 drochner config_search_ia(ssspi_search, self, "ssspi", NULL);
135 1.1 bsh }
136 1.1 bsh
137 1.1 bsh int
138 1.5 dsl ssspi_search(struct device *parent, struct cfdata *cf, const int *ldesc, void *aux)
139 1.1 bsh {
140 1.1 bsh struct ssspi_softc *sc = (struct ssspi_softc *)parent;
141 1.1 bsh struct ssspi_attach_args spia;
142 1.1 bsh static const unsigned char intr[] = { S3C24X0_INT_SPI0,
143 1.1 bsh S3C2410_INT_SPI1 };
144 1.1 bsh
145 1.1 bsh KASSERT(sc->index == 0 || sc->index == 1);
146 1.1 bsh
147 1.1 bsh spia.spia_iot = sc->iot;
148 1.1 bsh spia.spia_ioh = sc->ioh;
149 1.1 bsh spia.spia_gpioh = s3c2xx0_softc->sc_gpio_ioh;
150 1.1 bsh spia.spia_index = sc->index;
151 1.1 bsh spia.spia_intr = intr[sc->index];
152 1.1 bsh spia.spia_aux_intr = cf->cf_loc[SSSPICF_INTR];
153 1.1 bsh spia.spia_dmat = s3c2xx0_softc->sc_dmat;
154 1.1 bsh
155 1.1 bsh if (config_match(parent, cf, &spia))
156 1.1 bsh config_attach(parent, cf, &spia, ssspi_print);
157 1.1 bsh
158 1.1 bsh return 0;
159 1.1 bsh }
160 1.1 bsh
161 1.1 bsh /*
162 1.1 bsh * Intiialze SPI port. called by child devices.
163 1.1 bsh */
164 1.1 bsh int
165 1.1 bsh s3c24x0_spi_setup(struct ssspi_softc *sc, uint32_t mode, int bps, int use_ss)
166 1.1 bsh {
167 1.1 bsh int pclk = s3c2xx0_softc->sc_pclk;
168 1.1 bsh int prescaler;
169 1.1 bsh uint32_t pgcon, pecon;
170 1.1 bsh bus_space_handle_t gpioh = s3c2xx0_softc->sc_gpio_ioh;
171 1.1 bsh bus_space_tag_t iot = sc->iot;
172 1.1 bsh
173 1.1 bsh if (bps > 1) {
174 1.1 bsh prescaler = pclk / 2 / bps - 1;
175 1.1 bsh
176 1.1 bsh if (prescaler <= 0 || 0xff < prescaler)
177 1.1 bsh return -1;
178 1.1 bsh bus_space_write_1(sc->iot, sc->ioh, SPI_SPPRE, prescaler);
179 1.1 bsh }
180 1.1 bsh
181 1.1 bsh
182 1.1 bsh if (sc->index == 0) {
183 1.1 bsh pecon = bus_space_read_4(iot, gpioh, GPIO_PECON);
184 1.1 bsh
185 1.1 bsh if (use_ss) {
186 1.1 bsh pgcon = bus_space_read_4(iot, gpioh, GPIO_PGCON);
187 1.1 bsh pgcon = GPIO_SET_FUNC(pgcon, 2, PCON_ALTFUN2);
188 1.1 bsh bus_space_write_4(iot, gpioh, GPIO_PGCON, pgcon);
189 1.1 bsh }
190 1.1 bsh
191 1.1 bsh pecon = GPIO_SET_FUNC(pecon, 11, PCON_ALTFUN2); /* SPIMISO0 */
192 1.1 bsh pecon = GPIO_SET_FUNC(pecon, 12, PCON_ALTFUN2); /* SPIMOSI0 */
193 1.1 bsh pecon = GPIO_SET_FUNC(pecon, 13, PCON_ALTFUN2); /* SPICL0 */
194 1.1 bsh
195 1.1 bsh bus_space_write_4(iot, gpioh, GPIO_PECON, pecon);
196 1.1 bsh }
197 1.1 bsh else {
198 1.1 bsh pgcon = bus_space_read_4(iot, gpioh, GPIO_PGCON);
199 1.1 bsh
200 1.1 bsh if (use_ss)
201 1.1 bsh pgcon = GPIO_SET_FUNC(pgcon, 3, PCON_ALTFUN2);
202 1.1 bsh
203 1.1 bsh pgcon = GPIO_SET_FUNC(pgcon, 5, PCON_ALTFUN2); /* SPIMISO1 */
204 1.1 bsh pgcon = GPIO_SET_FUNC(pgcon, 6, PCON_ALTFUN2); /* SPIMOSI1 */
205 1.1 bsh pgcon = GPIO_SET_FUNC(pgcon, 7, PCON_ALTFUN2); /* SPICLK1 */
206 1.1 bsh
207 1.1 bsh bus_space_write_4(iot, gpioh, GPIO_PGCON, pgcon);
208 1.1 bsh }
209 1.1 bsh
210 1.1 bsh bus_space_write_4(iot, sc->ioh, SPI_SPCON, mode);
211 1.1 bsh
212 1.1 bsh return 0;
213 1.1 bsh }
214