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