ipaq_pcic.c revision 1.12 1 /* $NetBSD: ipaq_pcic.c,v 1.12 2003/07/15 00:25:07 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 Ichiro FUKUHARA (ichiro (at) ichiro.org).
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: ipaq_pcic.c,v 1.12 2003/07/15 00:25:07 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 #include <dev/pcmcia/pcmciachip.h>
54 #include <dev/pcmcia/pcmciavar.h>
55
56 #include <hpcarm/dev/ipaq_saipvar.h>
57 #include <hpcarm/dev/ipaq_pcicreg.h>
58 #include <hpcarm/dev/ipaq_gpioreg.h>
59
60 #include <arm/sa11x0/sa11x0_gpioreg.h>
61 #include <arm/sa11x0/sa11x0_var.h>
62 #include <arm/sa11x0/sa11xx_pcicvar.h>
63
64 #include "ipaqpcic.h"
65
66 static int ipaqpcic_match(struct device *, struct cfdata *, void *);
67 static void ipaqpcic_attach(struct device *, struct device *, void *);
68 static int ipaqpcic_print(void *, const char *);
69 static int ipaqpcic_submatch(struct device *, struct cfdata *, void *);
70
71 static int ipaqpcic_read(struct sapcic_socket *, int);
72 static void ipaqpcic_write(struct sapcic_socket *, int, int);
73 static void ipaqpcic_set_power(struct sapcic_socket *, int);
74 static void ipaqpcic_clear_intr(int);
75 static void *ipaqpcic_intr_establish(struct sapcic_socket *, int,
76 int (*)(void *), void *);
77 static void ipaqpcic_intr_disestablish(struct sapcic_socket *, void *);
78
79 struct ipaqpcic_softc {
80 struct sapcic_softc sc_pc;
81 bus_space_handle_t sc_ioh;
82 struct ipaq_softc *sc_parent;
83 struct sapcic_socket sc_socket[2];
84 };
85
86 static void ipaqpcic_init(struct ipaqpcic_softc *);
87
88 static struct sapcic_tag ipaqpcic_functions = {
89 ipaqpcic_read,
90 ipaqpcic_write,
91 ipaqpcic_set_power,
92 ipaqpcic_clear_intr,
93 ipaqpcic_intr_establish,
94 ipaqpcic_intr_disestablish
95 };
96
97 CFATTACH_DECL(ipaqpcic, sizeof(struct ipaqpcic_softc),
98 ipaqpcic_match, ipaqpcic_attach, NULL, NULL);
99
100 static int
101 ipaqpcic_match(parent, cf, aux)
102 struct device *parent;
103 struct cfdata *cf;
104 void *aux;
105 {
106 return (1);
107 }
108
109 static void
110 ipaqpcic_attach(parent, self, aux)
111 struct device *parent;
112 struct device *self;
113 void *aux;
114 {
115 int i;
116 struct pcmciabus_attach_args paa;
117 struct ipaqpcic_softc *sc = (struct ipaqpcic_softc *)self;
118 struct ipaq_softc *psc = (struct ipaq_softc *)parent;
119
120 printf("\n");
121
122 sc->sc_pc.sc_iot = psc->sc_iot;
123 sc->sc_ioh = psc->sc_ioh;
124 sc->sc_parent = (struct ipaq_softc *)parent;
125
126 ipaqpcic_init(sc);
127
128 for(i = 0; i < 2; i++) {
129 sc->sc_socket[i].sc = (struct sapcic_softc *)sc;
130 sc->sc_socket[i].socket = i;
131 sc->sc_socket[i].pcictag_cookie = psc;
132 sc->sc_socket[i].pcictag = &ipaqpcic_functions;
133 sc->sc_socket[i].event_thread = NULL;
134 sc->sc_socket[i].event = 0;
135 sc->sc_socket[i].laststatus = SAPCIC_CARD_INVALID;
136 sc->sc_socket[i].shutdown = 0;
137
138 paa.paa_busname = "pcmcia";
139 paa.pct = (pcmcia_chipset_tag_t)&sa11x0_pcmcia_functions;
140 paa.pch = (pcmcia_chipset_handle_t)&sc->sc_socket[i];
141 paa.iobase = 0;
142 paa.iosize = 0x4000000;
143
144 sc->sc_socket[i].pcmcia =
145 (struct device *)config_found_sm(&sc->sc_pc.sc_dev,
146 &paa, ipaqpcic_print, ipaqpcic_submatch);
147
148 sa11x0_intr_establish((sa11x0_chipset_tag_t)psc,
149 i ? IRQ_CD1 : IRQ_CD0,
150 1, IPL_BIO, sapcic_intr,
151 &sc->sc_socket[i]);
152
153 /* schedule kthread creation */
154 kthread_create(sapcic_kthread_create, &sc->sc_socket[i]);
155
156 #if 0 /* XXX */
157 /* establish_intr should be after creating the kthread */
158 config_interrupt(&sc->sc_socket[i], ipaqpcic_config_intr);
159 #endif
160 }
161 }
162
163 static int
164 ipaqpcic_print(aux, name)
165 void *aux;
166 const char *name;
167 {
168 return (UNCONF);
169 }
170
171 static int
172 ipaqpcic_submatch(parent, cf, aux)
173 struct device *parent;
174 struct cfdata *cf;
175 void *aux;
176 {
177 return config_match(parent, cf, aux);
178 }
179
180 static void
181 ipaqpcic_init(sc)
182 struct ipaqpcic_softc *sc;
183 {
184 int cr;
185
186 /* All those are inputs */
187 cr = bus_space_read_4(sc->sc_pc.sc_iot, sc->sc_parent->sc_gpioh, SAGPIO_PDR);
188 cr &= ~(GPIO_H3600_PCMCIA_CD0 | GPIO_H3600_PCMCIA_CD1 | GPIO_H3600_PCMCIA_IRQ0 |
189 GPIO_H3600_PCMCIA_IRQ1);
190 bus_space_write_4(sc->sc_pc.sc_iot, sc->sc_parent->sc_gpioh, SAGPIO_PDR, cr);
191
192 sc->sc_parent->ipaq_egpio |=
193 EGPIO_H3600_OPT_NVRAM_ON | EGPIO_H3600_OPT_ON ;
194 sc->sc_parent->ipaq_egpio &=
195 ~(EGPIO_H3600_CARD_RESET | EGPIO_H3600_OPT_RESET);
196 bus_space_write_2(sc->sc_pc.sc_iot, sc->sc_parent->sc_egpioh,
197 0, sc->sc_parent->ipaq_egpio);
198 }
199
200 static int
201 ipaqpcic_read(so, reg)
202 struct sapcic_socket *so;
203 int reg;
204 {
205 int cr, bit;
206 struct ipaqpcic_softc *sc = (struct ipaqpcic_softc *)so->sc;
207
208 cr = bus_space_read_4(sc->sc_pc.sc_iot, sc->sc_parent->sc_gpioh, SAGPIO_PLR);
209
210 switch (reg) {
211 case SAPCIC_STATUS_CARD:
212 bit = (so->socket ? GPIO_H3600_PCMCIA_CD0 :
213 GPIO_H3600_PCMCIA_CD1) & cr;
214 if (!bit)
215 return SAPCIC_CARD_INVALID;
216 else
217 return SAPCIC_CARD_VALID;
218 case SAPCIC_STATUS_VS1:
219 case SAPCIC_STATUS_VS2:
220 case SAPCIC_STATUS_READY:
221 bit = (so->socket ? GPIO_H3600_PCMCIA_IRQ0:
222 GPIO_H3600_PCMCIA_IRQ1);
223 return (bit & cr);
224 default:
225 panic("ipaqpcic_read: bogus register");
226 }
227 }
228
229 static void
230 ipaqpcic_write(so, reg, arg)
231 struct sapcic_socket *so;
232 int reg;
233 int arg;
234 {
235 int s;
236 struct ipaqpcic_softc *sc = (struct ipaqpcic_softc *)so->sc;
237
238 s = splhigh();
239 switch (reg) {
240 case SAPCIC_CONTROL_RESET:
241 sc->sc_parent->ipaq_egpio |= EGPIO_H3600_CARD_RESET;
242 break;
243 case SAPCIC_CONTROL_LINEENABLE:
244 case SAPCIC_CONTROL_WAITENABLE:
245 case SAPCIC_CONTROL_POWERSELECT:
246 break;
247
248 default:
249 splx(s);
250 panic("ipaqpcic_write: bogus register");
251 }
252 bus_space_write_2(sc->sc_pc.sc_iot, sc->sc_parent->sc_egpioh, 0,
253 sc->sc_parent->ipaq_egpio);
254 splx(s);
255 }
256
257 static void
258 ipaqpcic_set_power(so, arg)
259 struct sapcic_socket *so;
260 int arg;
261 {
262 int s;
263 struct ipaqpcic_softc *sc = (struct ipaqpcic_softc *)so->sc;
264
265 s = splbio();
266 switch (arg) {
267 case SAPCIC_POWER_OFF:
268 sc->sc_parent->ipaq_egpio &=
269 ~(EGPIO_H3600_OPT_NVRAM_ON | EGPIO_H3600_OPT_ON);
270 break;
271 case SAPCIC_POWER_3V:
272 case SAPCIC_POWER_5V:
273 sc->sc_parent->ipaq_egpio |=
274 EGPIO_H3600_OPT_NVRAM_ON | EGPIO_H3600_OPT_ON;
275 break;
276 default:
277 panic("ipaqpcic_set_power: bogus arg");
278 }
279 bus_space_write_2(sc->sc_pc.sc_iot, sc->sc_parent->sc_egpioh,
280 0, sc->sc_parent->ipaq_egpio);
281 splx(s);
282 }
283
284 static void
285 ipaqpcic_clear_intr(arg)
286 {
287 }
288
289 static void *
290 ipaqpcic_intr_establish(so, level, ih_fun, ih_arg)
291 struct sapcic_socket *so;
292 int level;
293 int (*ih_fun)(void *);
294 void *ih_arg;
295 {
296 int irq;
297
298 irq = so->socket ? IRQ_IRQ0 : IRQ_IRQ1;
299 return (sa11x0_intr_establish((sa11x0_chipset_tag_t)so->pcictag_cookie,
300 irq-16, 1, level, ih_fun, ih_arg));
301 }
302
303 static void
304 ipaqpcic_intr_disestablish(so, ih)
305 struct sapcic_socket *so;
306 void *ih;
307 {
308 sa11x0_intr_disestablish((sa11x0_chipset_tag_t)so->pcictag_cookie, ih);
309 }
310