sa11x1_pcic.c revision 1.2 1 1.2 rjs /* $NetBSD: sa11x1_pcic.c,v 1.2 2001/07/30 10:46:08 rjs Exp $ */
2 1.1 rjs
3 1.1 rjs /*-
4 1.1 rjs * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 1.1 rjs * All rights reserved.
6 1.1 rjs *
7 1.1 rjs * This code is derived from software contributed to The NetBSD Foundation
8 1.1 rjs * by IWAMOTO Toshihiro.
9 1.1 rjs *
10 1.1 rjs * Redistribution and use in source and binary forms, with or without
11 1.1 rjs * modification, are permitted provided that the following conditions
12 1.1 rjs * are met:
13 1.1 rjs * 1. Redistributions of source code must retain the above copyright
14 1.1 rjs * notice, this list of conditions and the following disclaimer.
15 1.1 rjs * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 rjs * notice, this list of conditions and the following disclaimer in the
17 1.1 rjs * documentation and/or other materials provided with the distribution.
18 1.1 rjs * 3. All advertising materials mentioning features or use of this software
19 1.1 rjs * must display the following acknowledgement:
20 1.1 rjs * This product includes software developed by the NetBSD
21 1.1 rjs * Foundation, Inc. and its contributors.
22 1.1 rjs * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 rjs * contributors may be used to endorse or promote products derived
24 1.1 rjs * from this software without specific prior written permission.
25 1.1 rjs *
26 1.1 rjs * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 rjs * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 rjs * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 rjs * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 rjs * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 rjs * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 rjs * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 rjs * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 rjs * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 rjs * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 rjs * POSSIBILITY OF SUCH DAMAGE.
37 1.1 rjs */
38 1.1 rjs
39 1.1 rjs #include <sys/param.h>
40 1.1 rjs #include <sys/systm.h>
41 1.1 rjs #include <sys/types.h>
42 1.1 rjs #include <sys/conf.h>
43 1.1 rjs #include <sys/file.h>
44 1.1 rjs #include <sys/device.h>
45 1.1 rjs #include <sys/kernel.h>
46 1.1 rjs #include <sys/kthread.h>
47 1.1 rjs #include <sys/malloc.h>
48 1.1 rjs
49 1.1 rjs #include <machine/bus.h>
50 1.1 rjs #ifdef hpcarm
51 1.1 rjs #include <machine/platid.h>
52 1.1 rjs #include <machine/platid_mask.h>
53 1.1 rjs #endif
54 1.1 rjs
55 1.1 rjs #include <dev/pcmcia/pcmciachip.h>
56 1.1 rjs #include <dev/pcmcia/pcmciavar.h>
57 1.1 rjs #include <arm/sa11x0/sa11x0_reg.h>
58 1.1 rjs #include <arm/sa11x0/sa11x0_var.h>
59 1.1 rjs #include <arm/sa11x0/sa1111_reg.h>
60 1.1 rjs #include <arm/sa11x0/sa1111_var.h>
61 1.1 rjs #include <arm/sa11x0/sa11x1_pcicreg.h>
62 1.1 rjs #include <arm/sa11x0/sa11xx_pcicvar.h>
63 1.1 rjs
64 1.1 rjs #include "sacpcic.h"
65 1.1 rjs
66 1.1 rjs static int sacpcic_match(struct device *, struct cfdata *, void *);
67 1.1 rjs static void sacpcic_attach(struct device *, struct device *, void *);
68 1.1 rjs static int sacpcic_print(void *, const char *);
69 1.1 rjs static int sacpcic_submatch(struct device *, struct cfdata *, void *);
70 1.1 rjs
71 1.1 rjs static int sacpcic_read(struct sapcic_socket *, int);
72 1.1 rjs static void sacpcic_write(struct sapcic_socket *, int, int);
73 1.1 rjs static void sacpcic_set_power(struct sapcic_socket *, int);
74 1.1 rjs static void sacpcic_clear_intr(int);
75 1.1 rjs static void *sacpcic_intr_establish(struct sapcic_socket *, int,
76 1.1 rjs int (*)(void *), void *);
77 1.1 rjs static void sacpcic_intr_disestablish(struct sapcic_socket *, void *);
78 1.1 rjs
79 1.1 rjs struct sacpcic_softc {
80 1.1 rjs struct sapcic_softc sc_pc;
81 1.1 rjs bus_space_handle_t sc_ioh;
82 1.1 rjs
83 1.1 rjs struct sapcic_socket sc_socket[2];
84 1.1 rjs };
85 1.1 rjs
86 1.1 rjs static struct sapcic_tag sacpcic_functions = {
87 1.1 rjs sacpcic_read,
88 1.1 rjs sacpcic_write,
89 1.1 rjs sacpcic_set_power,
90 1.1 rjs sacpcic_clear_intr,
91 1.1 rjs sacpcic_intr_establish,
92 1.1 rjs sacpcic_intr_disestablish
93 1.1 rjs };
94 1.1 rjs
95 1.1 rjs #ifdef hpcarm
96 1.1 rjs static int j720_power_capability[] = {
97 1.1 rjs SAPCIC_POWER_5V | SAPCIC_POWER_3V, SAPCIC_POWER_3V
98 1.1 rjs };
99 1.1 rjs
100 1.1 rjs static struct platid_data sacpcic_platid_table[] = {
101 1.1 rjs { &platid_mask_MACH_HP_JORNADA_720, j720_power_capability },
102 1.1 rjs { &platid_mask_MACH_HP_JORNADA_720JP, j720_power_capability },
103 1.1 rjs { NULL, NULL }
104 1.1 rjs };
105 1.1 rjs #endif
106 1.1 rjs
107 1.1 rjs struct cfattach sacpcic_ca = {
108 1.1 rjs sizeof(struct sacpcic_softc), sacpcic_match, sacpcic_attach
109 1.1 rjs };
110 1.1 rjs
111 1.1 rjs static int
112 1.1 rjs sacpcic_match(parent, cf, aux)
113 1.1 rjs struct device *parent;
114 1.1 rjs struct cfdata *cf;
115 1.1 rjs void *aux;
116 1.1 rjs {
117 1.1 rjs return (1);
118 1.1 rjs }
119 1.1 rjs
120 1.1 rjs static void
121 1.1 rjs sacpcic_attach(parent, self, aux)
122 1.1 rjs struct device *parent;
123 1.1 rjs struct device *self;
124 1.1 rjs void *aux;
125 1.1 rjs {
126 1.1 rjs int i;
127 1.1 rjs #ifdef hpcarm
128 1.1 rjs int *ip;
129 1.1 rjs #endif
130 1.1 rjs struct pcmciabus_attach_args paa;
131 1.1 rjs struct sacpcic_softc *sc = (struct sacpcic_softc *)self;
132 1.1 rjs struct sacc_softc *psc = (struct sacc_softc *)parent;
133 1.1 rjs #ifdef hpcarm
134 1.1 rjs struct platid_data *p;
135 1.1 rjs #endif
136 1.1 rjs
137 1.1 rjs printf("\n");
138 1.1 rjs
139 1.1 rjs sc->sc_pc.sc_iot = psc->sc_iot;
140 1.1 rjs sc->sc_ioh = psc->sc_ioh;
141 1.1 rjs #ifdef hpcarm
142 1.1 rjs p = platid_search(&platid, sacpcic_platid_table);
143 1.1 rjs #endif
144 1.1 rjs
145 1.1 rjs for(i = 0; i < 2; i++) {
146 1.1 rjs sc->sc_socket[i].sc = (struct sapcic_softc *)sc;
147 1.1 rjs sc->sc_socket[i].socket = i;
148 1.2 rjs sc->sc_socket[i].pcictag_cookie = psc;
149 1.1 rjs sc->sc_socket[i].pcictag = &sacpcic_functions;
150 1.1 rjs sc->sc_socket[i].event_thread = NULL;
151 1.1 rjs sc->sc_socket[i].event = 0;
152 1.1 rjs sc->sc_socket[i].laststatus = SAPCIC_CARD_INVALID;
153 1.1 rjs sc->sc_socket[i].shutdown = 0;
154 1.1 rjs
155 1.1 rjs #ifdef hpcarm
156 1.1 rjs if (p == NULL) {
157 1.1 rjs sc->sc_socket[i].power_capability = SAPCIC_POWER_5V;
158 1.1 rjs } else {
159 1.1 rjs ip = (int *)p->data;
160 1.1 rjs sc->sc_socket[i].power_capability = ip[i];
161 1.1 rjs }
162 1.1 rjs #else
163 1.1 rjs /* XXX */
164 1.1 rjs sc->sc_socket[i].power_capability = SAPCIC_POWER_5V;
165 1.1 rjs #endif
166 1.1 rjs
167 1.1 rjs paa.paa_busname = "pcmcia";
168 1.1 rjs paa.pct = (pcmcia_chipset_tag_t)&sa11x0_pcmcia_functions;
169 1.1 rjs paa.pch = (pcmcia_chipset_handle_t)&sc->sc_socket[i];
170 1.1 rjs paa.iobase = 0;
171 1.1 rjs paa.iosize = 0x4000000;
172 1.1 rjs
173 1.1 rjs sc->sc_socket[i].pcmcia =
174 1.1 rjs (struct device *)config_found_sm(&sc->sc_pc.sc_dev,
175 1.1 rjs &paa, sacpcic_print, sacpcic_submatch);
176 1.1 rjs
177 1.1 rjs sacc_intr_establish((sacc_chipset_tag_t)psc,
178 1.1 rjs i ? IRQ_S1_CDVALID : IRQ_S0_CDVALID,
179 1.1 rjs IST_EDGE_RAISE, IPL_BIO, sapcic_intr,
180 1.1 rjs &sc->sc_socket[i]);
181 1.1 rjs
182 1.1 rjs /* schedule kthread creation */
183 1.1 rjs kthread_create(sapcic_kthread_create, &sc->sc_socket[i]);
184 1.1 rjs
185 1.1 rjs #if 0 /* XXX */
186 1.1 rjs /* establish_intr should be after creating the kthread */
187 1.1 rjs config_interrupt(&sc->sc_socket[i], sacpcic_config_intr);
188 1.1 rjs #endif
189 1.1 rjs }
190 1.1 rjs }
191 1.1 rjs
192 1.1 rjs static int
193 1.1 rjs sacpcic_print(aux, name)
194 1.1 rjs void *aux;
195 1.1 rjs const char *name;
196 1.1 rjs {
197 1.1 rjs return (UNCONF);
198 1.1 rjs }
199 1.1 rjs
200 1.1 rjs static int
201 1.1 rjs sacpcic_submatch(parent, cf, aux)
202 1.1 rjs struct device *parent;
203 1.1 rjs struct cfdata *cf;
204 1.1 rjs void *aux;
205 1.1 rjs {
206 1.1 rjs return (*cf->cf_attach->ca_match)(parent, cf, aux);
207 1.1 rjs }
208 1.1 rjs
209 1.1 rjs
210 1.1 rjs static int
211 1.1 rjs sacpcic_read(so, reg)
212 1.1 rjs struct sapcic_socket *so;
213 1.1 rjs int reg;
214 1.1 rjs {
215 1.1 rjs int cr, bit;
216 1.1 rjs struct sacpcic_softc *sc = (struct sacpcic_softc *)so->sc;
217 1.1 rjs
218 1.1 rjs cr = bus_space_read_4(sc->sc_pc.sc_iot, sc->sc_ioh, SACPCIC_SR);
219 1.1 rjs
220 1.1 rjs switch (reg) {
221 1.1 rjs case SAPCIC_STATUS_CARD:
222 1.1 rjs bit = (so->socket ? SR_S1_CARDDETECT : SR_S0_CARDDETECT) & cr;
223 1.1 rjs if (bit)
224 1.1 rjs return SAPCIC_CARD_INVALID;
225 1.1 rjs else
226 1.1 rjs return SAPCIC_CARD_VALID;
227 1.1 rjs
228 1.1 rjs case SAPCIC_STATUS_VS1:
229 1.1 rjs bit = (so->socket ? SR_S1_VS1 : SR_S0_VS1);
230 1.1 rjs return (bit & cr);
231 1.1 rjs
232 1.1 rjs case SAPCIC_STATUS_VS2:
233 1.1 rjs bit = (so->socket ? SR_S1_VS2 : SR_S0_VS2);
234 1.1 rjs return (bit & cr);
235 1.1 rjs
236 1.1 rjs case SAPCIC_STATUS_READY:
237 1.1 rjs bit = (so->socket ? SR_S1_READY : SR_S0_READY);
238 1.1 rjs return (bit & cr);
239 1.1 rjs
240 1.1 rjs default:
241 1.1 rjs panic("sacpcic_read: bogus register\n");
242 1.1 rjs }
243 1.1 rjs }
244 1.1 rjs
245 1.1 rjs static void
246 1.1 rjs sacpcic_write(so, reg, arg)
247 1.1 rjs struct sapcic_socket *so;
248 1.1 rjs int reg;
249 1.1 rjs int arg;
250 1.1 rjs {
251 1.1 rjs int s, oldvalue, newvalue, mask;
252 1.1 rjs struct sacpcic_softc *sc = (struct sacpcic_softc *)so->sc;
253 1.1 rjs
254 1.1 rjs s = splhigh();
255 1.1 rjs oldvalue = bus_space_read_4(sc->sc_pc.sc_iot, sc->sc_ioh, SACPCIC_CR);
256 1.1 rjs
257 1.1 rjs switch (reg) {
258 1.1 rjs case SAPCIC_CONTROL_RESET:
259 1.1 rjs mask = so->socket ? CR_S1_RST : CR_S0_RST;
260 1.1 rjs
261 1.1 rjs newvalue = (oldvalue & ~mask) | (arg ? mask : 0);
262 1.1 rjs break;
263 1.1 rjs
264 1.1 rjs case SAPCIC_CONTROL_LINEENABLE:
265 1.1 rjs mask = so->socket ? CR_S1_FLT : CR_S0_FLT;
266 1.1 rjs
267 1.1 rjs newvalue = (oldvalue & ~mask) | (arg ? mask : 0);
268 1.1 rjs break;
269 1.1 rjs
270 1.1 rjs case SAPCIC_CONTROL_WAITENABLE:
271 1.1 rjs mask = so->socket ? CR_S1_PWAITEN : CR_S0_PWAITEN;
272 1.1 rjs
273 1.1 rjs newvalue = (oldvalue & ~mask) | (arg ? mask : 0);
274 1.1 rjs break;
275 1.1 rjs
276 1.1 rjs case SAPCIC_CONTROL_POWERSELECT:
277 1.1 rjs mask = so->socket ? CR_S1_PSE : CR_S0_PSE;
278 1.1 rjs newvalue = oldvalue & ~mask;
279 1.1 rjs
280 1.1 rjs switch (arg) {
281 1.1 rjs case SAPCIC_POWER_3V:
282 1.1 rjs break;
283 1.1 rjs case SAPCIC_POWER_5V:
284 1.1 rjs newvalue |= mask;
285 1.1 rjs break;
286 1.1 rjs default:
287 1.1 rjs splx(s);
288 1.1 rjs panic("sacpcic_write: bogus arg");
289 1.1 rjs }
290 1.1 rjs break;
291 1.1 rjs
292 1.1 rjs default:
293 1.1 rjs splx(s);
294 1.1 rjs panic("sacpcic_write: bogus register");
295 1.1 rjs }
296 1.1 rjs bus_space_write_4(sc->sc_pc.sc_iot, sc->sc_ioh, SACPCIC_CR, newvalue);
297 1.1 rjs splx(s);
298 1.1 rjs }
299 1.1 rjs
300 1.1 rjs static void
301 1.1 rjs sacpcic_set_power(so, arg)
302 1.1 rjs struct sapcic_socket *so;
303 1.1 rjs int arg;
304 1.1 rjs {
305 1.1 rjs /* XXX this should go to dev/jornada720.c */
306 1.1 rjs int newval, oldval, s;
307 1.2 rjs struct sacc_softc *sc = so->pcictag_cookie;
308 1.1 rjs
309 1.1 rjs /* XXX this isn't well confirmed. DANGER DANGER */
310 1.1 rjs switch (arg) {
311 1.1 rjs case SAPCIC_POWER_OFF:
312 1.1 rjs newval = 0;
313 1.1 rjs break;
314 1.1 rjs case SAPCIC_POWER_3V:
315 1.1 rjs newval = 2;
316 1.1 rjs break;
317 1.1 rjs case SAPCIC_POWER_5V:
318 1.1 rjs newval = 1;
319 1.1 rjs break;
320 1.1 rjs default:
321 1.1 rjs panic("sacpcic_set_power: bogus arg\n");
322 1.1 rjs }
323 1.1 rjs
324 1.1 rjs s = splbio();
325 1.2 rjs oldval = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
326 1.1 rjs SACCGPIOA_DVR);
327 1.1 rjs switch (so->socket) {
328 1.1 rjs case 0:
329 1.1 rjs newval = newval | (oldval & 0xc);
330 1.1 rjs break;
331 1.1 rjs case 1:
332 1.1 rjs newval = (newval << 2) | (oldval & 3);
333 1.1 rjs break;
334 1.1 rjs default:
335 1.1 rjs splx(s);
336 1.1 rjs panic("sacpcic_set_power\n");
337 1.1 rjs }
338 1.2 rjs bus_space_write_4(sc->sc_iot, sc->sc_ioh, SACCGPIOA_DVR, newval);
339 1.1 rjs splx(s);
340 1.1 rjs }
341 1.1 rjs
342 1.1 rjs static void
343 1.1 rjs sacpcic_clear_intr(arg)
344 1.1 rjs {
345 1.1 rjs /* sacc_intr_dispatch takes care of intr status */
346 1.1 rjs }
347 1.1 rjs
348 1.1 rjs static void *
349 1.1 rjs sacpcic_intr_establish(so, level, ih_fun, ih_arg)
350 1.1 rjs struct sapcic_socket *so;
351 1.1 rjs int level;
352 1.1 rjs int (*ih_fun)(void *);
353 1.1 rjs void *ih_arg;
354 1.1 rjs {
355 1.1 rjs int irq;
356 1.1 rjs
357 1.1 rjs irq = so->socket ? IRQ_S1_READY : IRQ_S0_READY;
358 1.2 rjs return (sacc_intr_establish((sacc_chipset_tag_t)so->pcictag_cookie, irq,
359 1.1 rjs IST_EDGE_FALL, level, ih_fun, ih_arg));
360 1.1 rjs }
361 1.1 rjs
362 1.1 rjs static void
363 1.1 rjs sacpcic_intr_disestablish(so, ih)
364 1.1 rjs struct sapcic_socket *so;
365 1.1 rjs void *ih;
366 1.1 rjs {
367 1.2 rjs sacc_intr_disestablish((sacc_chipset_tag_t)so->pcictag_cookie, ih);
368 1.1 rjs }
369