stp4020.c revision 1.54 1 1.54 cegger /* $NetBSD: stp4020.c,v 1.54 2008/04/05 18:35:32 cegger Exp $ */
2 1.1 pk
3 1.1 pk /*-
4 1.1 pk * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 1.1 pk * All rights reserved.
6 1.1 pk *
7 1.1 pk * This code is derived from software contributed to The NetBSD Foundation
8 1.1 pk * by Paul Kranenburg.
9 1.1 pk *
10 1.1 pk * Redistribution and use in source and binary forms, with or without
11 1.1 pk * modification, are permitted provided that the following conditions
12 1.1 pk * are met:
13 1.1 pk * 1. Redistributions of source code must retain the above copyright
14 1.1 pk * notice, this list of conditions and the following disclaimer.
15 1.1 pk * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 pk * notice, this list of conditions and the following disclaimer in the
17 1.1 pk * documentation and/or other materials provided with the distribution.
18 1.1 pk * 3. All advertising materials mentioning features or use of this software
19 1.1 pk * must display the following acknowledgement:
20 1.1 pk * This product includes software developed by the NetBSD
21 1.1 pk * Foundation, Inc. and its contributors.
22 1.1 pk * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 pk * contributors may be used to endorse or promote products derived
24 1.1 pk * from this software without specific prior written permission.
25 1.1 pk *
26 1.1 pk * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 pk * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 pk * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 pk * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 pk * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 pk * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 pk * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 pk * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 pk * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 pk * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 pk * POSSIBILITY OF SUCH DAMAGE.
37 1.1 pk */
38 1.1 pk
39 1.1 pk /*
40 1.1 pk * STP4020: SBus/PCMCIA bridge supporting two Type-3 PCMCIA cards.
41 1.1 pk */
42 1.12 lukem
43 1.12 lukem #include <sys/cdefs.h>
44 1.54 cegger __KERNEL_RCSID(0, "$NetBSD: stp4020.c,v 1.54 2008/04/05 18:35:32 cegger Exp $");
45 1.1 pk
46 1.1 pk #include <sys/param.h>
47 1.1 pk #include <sys/systm.h>
48 1.1 pk #include <sys/errno.h>
49 1.1 pk #include <sys/malloc.h>
50 1.15 martin #include <sys/extent.h>
51 1.1 pk #include <sys/proc.h>
52 1.1 pk #include <sys/kernel.h>
53 1.1 pk #include <sys/kthread.h>
54 1.1 pk #include <sys/device.h>
55 1.51 ad #include <sys/intr.h>
56 1.1 pk
57 1.1 pk #include <dev/pcmcia/pcmciareg.h>
58 1.1 pk #include <dev/pcmcia/pcmciavar.h>
59 1.1 pk #include <dev/pcmcia/pcmciachip.h>
60 1.1 pk
61 1.52 ad #include <sys/bus.h>
62 1.1 pk
63 1.1 pk #include <dev/sbus/sbusvar.h>
64 1.1 pk #include <dev/sbus/stp4020reg.h>
65 1.1 pk
66 1.1 pk #define STP4020_DEBUG 1 /* XXX-temp */
67 1.1 pk
68 1.15 martin /*
69 1.15 martin * We use the three available windows per socket in a simple, fixed
70 1.15 martin * arrangement. Each window maps (at full 1 MB size) one of the pcmcia
71 1.15 martin * spaces into sbus space.
72 1.15 martin */
73 1.15 martin #define STP_WIN_ATTR 0 /* index of the attribute memory space window */
74 1.15 martin #define STP_WIN_MEM 1 /* index of the common memory space window */
75 1.15 martin #define STP_WIN_IO 2 /* index of the io space window */
76 1.15 martin
77 1.15 martin
78 1.1 pk #if defined(STP4020_DEBUG)
79 1.1 pk int stp4020_debug = 0;
80 1.1 pk #define DPRINTF(x) do { if (stp4020_debug) printf x; } while(0)
81 1.1 pk #else
82 1.1 pk #define DPRINTF(x)
83 1.1 pk #endif
84 1.1 pk
85 1.1 pk /*
86 1.1 pk * Event queue; events detected in an interrupt context go here
87 1.1 pk * awaiting attention from our event handling thread.
88 1.1 pk */
89 1.1 pk struct stp4020_event {
90 1.1 pk SIMPLEQ_ENTRY(stp4020_event) se_q;
91 1.1 pk int se_type;
92 1.1 pk int se_sock;
93 1.1 pk };
94 1.1 pk /* Defined event types */
95 1.1 pk #define STP4020_EVENT_INSERTION 0
96 1.1 pk #define STP4020_EVENT_REMOVAL 1
97 1.1 pk
98 1.1 pk /*
99 1.1 pk * Per socket data.
100 1.1 pk */
101 1.1 pk struct stp4020_socket {
102 1.1 pk struct stp4020_softc *sc; /* Back link */
103 1.1 pk int flags;
104 1.1 pk #define STP4020_SOCKET_BUSY 0x0001
105 1.1 pk int sock; /* Socket number (0 or 1) */
106 1.28 martin int sbus_intno; /* Do we use first (0) or second (1)
107 1.28 martin interrupt? */
108 1.53 martin #ifndef SUN4U
109 1.31 martin int int_enable; /* ICR0 value for interrupt enabled */
110 1.31 martin int int_disable; /* ICR0 value for interrupt disabled */
111 1.53 martin #endif
112 1.33 martin bus_space_tag_t tag; /* socket control io */
113 1.33 martin bus_space_handle_t regs; /* space */
114 1.33 martin bus_space_tag_t pcmciat; /* io space for pcmcia */
115 1.1 pk struct device *pcmcia; /* Associated PCMCIA device */
116 1.1 pk int (*intrhandler) /* Card driver interrupt handler */
117 1.42 perry (void *);
118 1.1 pk void *intrarg; /* Card interrupt handler argument */
119 1.53 martin #ifndef SUN4U
120 1.31 martin void *softint; /* cookie for the softintr */
121 1.53 martin #endif
122 1.31 martin
123 1.1 pk struct {
124 1.1 pk bus_space_handle_t winaddr;/* this window's address */
125 1.1 pk } windows[STP4020_NWIN];
126 1.1 pk
127 1.1 pk };
128 1.1 pk
129 1.1 pk struct stp4020_softc {
130 1.1 pk struct device sc_dev; /* Base device */
131 1.1 pk struct sbusdev sc_sd; /* SBus device */
132 1.1 pk pcmcia_chipset_tag_t sc_pct; /* Chipset methods */
133 1.1 pk
134 1.50 ad struct lwp *event_thread; /* event handling thread */
135 1.1 pk SIMPLEQ_HEAD(, stp4020_event) events; /* Pending events for thread */
136 1.1 pk
137 1.1 pk struct stp4020_socket sc_socks[STP4020_NSOCK];
138 1.53 martin #ifndef SUN4U
139 1.53 martin bool sc_use_softint;
140 1.53 martin #endif
141 1.1 pk };
142 1.1 pk
143 1.1 pk
144 1.42 perry static int stp4020print(void *, const char *);
145 1.42 perry static int stp4020match(struct device *, struct cfdata *, void *);
146 1.42 perry static void stp4020attach(struct device *, struct device *, void *);
147 1.42 perry static int stp4020_intr(void *);
148 1.16 martin static void stp4020_map_window(struct stp4020_socket *h, int win, int speed);
149 1.44 jdc static void stp4020_calc_speed(int bus_speed, int ns, int *length, int *cmd_delay);
150 1.53 martin #ifndef SUN4U
151 1.31 martin static void stp4020_intr_dispatch(void *arg);
152 1.53 martin #endif
153 1.1 pk
154 1.26 thorpej CFATTACH_DECL(nell, sizeof(struct stp4020_softc),
155 1.27 thorpej stp4020match, stp4020attach, NULL, NULL);
156 1.1 pk
157 1.6 pk #ifdef STP4020_DEBUG
158 1.42 perry static void stp4020_dump_regs(struct stp4020_socket *);
159 1.6 pk #endif
160 1.1 pk
161 1.42 perry static int stp4020_rd_sockctl(struct stp4020_socket *, int);
162 1.42 perry static void stp4020_wr_sockctl(struct stp4020_socket *, int, int);
163 1.42 perry static int stp4020_rd_winctl(struct stp4020_socket *, int, int);
164 1.42 perry static void stp4020_wr_winctl(struct stp4020_socket *, int, int, int);
165 1.42 perry
166 1.46 martin void stp4020_delay(struct stp4020_softc *sc, unsigned int);
167 1.42 perry void stp4020_attach_socket(struct stp4020_socket *, int);
168 1.42 perry void stp4020_event_thread(void *);
169 1.42 perry void stp4020_queue_event(struct stp4020_softc *, int, int);
170 1.42 perry
171 1.42 perry int stp4020_chip_mem_alloc(pcmcia_chipset_handle_t, bus_size_t,
172 1.42 perry struct pcmcia_mem_handle *);
173 1.42 perry void stp4020_chip_mem_free(pcmcia_chipset_handle_t,
174 1.42 perry struct pcmcia_mem_handle *);
175 1.42 perry int stp4020_chip_mem_map(pcmcia_chipset_handle_t, int, bus_addr_t,
176 1.1 pk bus_size_t, struct pcmcia_mem_handle *,
177 1.42 perry bus_size_t *, int *);
178 1.42 perry void stp4020_chip_mem_unmap(pcmcia_chipset_handle_t, int);
179 1.1 pk
180 1.42 perry int stp4020_chip_io_alloc(pcmcia_chipset_handle_t,
181 1.1 pk bus_addr_t, bus_size_t, bus_size_t,
182 1.42 perry struct pcmcia_io_handle *);
183 1.42 perry void stp4020_chip_io_free(pcmcia_chipset_handle_t,
184 1.42 perry struct pcmcia_io_handle *);
185 1.42 perry int stp4020_chip_io_map(pcmcia_chipset_handle_t, int, bus_addr_t,
186 1.42 perry bus_size_t, struct pcmcia_io_handle *, int *);
187 1.42 perry void stp4020_chip_io_unmap(pcmcia_chipset_handle_t, int);
188 1.42 perry
189 1.42 perry void stp4020_chip_socket_enable(pcmcia_chipset_handle_t);
190 1.42 perry void stp4020_chip_socket_disable(pcmcia_chipset_handle_t);
191 1.42 perry void stp4020_chip_socket_settype(pcmcia_chipset_handle_t, int);
192 1.42 perry void *stp4020_chip_intr_establish(pcmcia_chipset_handle_t,
193 1.1 pk struct pcmcia_function *, int,
194 1.42 perry int (*)(void *), void *);
195 1.42 perry void stp4020_chip_intr_disestablish(pcmcia_chipset_handle_t, void *);
196 1.1 pk
197 1.1 pk /* Our PCMCIA chipset methods */
198 1.1 pk static struct pcmcia_chip_functions stp4020_functions = {
199 1.1 pk stp4020_chip_mem_alloc,
200 1.1 pk stp4020_chip_mem_free,
201 1.1 pk stp4020_chip_mem_map,
202 1.1 pk stp4020_chip_mem_unmap,
203 1.1 pk
204 1.1 pk stp4020_chip_io_alloc,
205 1.1 pk stp4020_chip_io_free,
206 1.1 pk stp4020_chip_io_map,
207 1.1 pk stp4020_chip_io_unmap,
208 1.1 pk
209 1.1 pk stp4020_chip_intr_establish,
210 1.1 pk stp4020_chip_intr_disestablish,
211 1.1 pk
212 1.1 pk stp4020_chip_socket_enable,
213 1.39 mycroft stp4020_chip_socket_disable,
214 1.39 mycroft stp4020_chip_socket_settype,
215 1.49 jdc NULL
216 1.1 pk };
217 1.1 pk
218 1.1 pk
219 1.47 perry static inline int
220 1.1 pk stp4020_rd_sockctl(h, idx)
221 1.1 pk struct stp4020_socket *h;
222 1.1 pk int idx;
223 1.1 pk {
224 1.1 pk int o = ((STP4020_SOCKREGS_SIZE * (h->sock)) + idx);
225 1.1 pk return (bus_space_read_2(h->tag, h->regs, o));
226 1.1 pk }
227 1.1 pk
228 1.47 perry static inline void
229 1.1 pk stp4020_wr_sockctl(h, idx, v)
230 1.1 pk struct stp4020_socket *h;
231 1.1 pk int idx;
232 1.1 pk int v;
233 1.1 pk {
234 1.1 pk int o = (STP4020_SOCKREGS_SIZE * (h->sock)) + idx;
235 1.1 pk bus_space_write_2(h->tag, h->regs, o, v);
236 1.1 pk }
237 1.1 pk
238 1.47 perry static inline int
239 1.1 pk stp4020_rd_winctl(h, win, idx)
240 1.1 pk struct stp4020_socket *h;
241 1.1 pk int win;
242 1.1 pk int idx;
243 1.1 pk {
244 1.1 pk int o = (STP4020_SOCKREGS_SIZE * (h->sock)) +
245 1.1 pk (STP4020_WINREGS_SIZE * win) + idx;
246 1.1 pk return (bus_space_read_2(h->tag, h->regs, o));
247 1.1 pk }
248 1.1 pk
249 1.47 perry static inline void
250 1.1 pk stp4020_wr_winctl(h, win, idx, v)
251 1.1 pk struct stp4020_socket *h;
252 1.1 pk int win;
253 1.1 pk int idx;
254 1.1 pk int v;
255 1.1 pk {
256 1.1 pk int o = (STP4020_SOCKREGS_SIZE * (h->sock)) +
257 1.1 pk (STP4020_WINREGS_SIZE * win) + idx;
258 1.1 pk
259 1.1 pk bus_space_write_2(h->tag, h->regs, o, v);
260 1.1 pk }
261 1.1 pk
262 1.33 martin #ifndef SUN4U /* XXX - move to SBUS machdep function? */
263 1.33 martin
264 1.32 mrg static u_int16_t stp4020_read_2(bus_space_tag_t,
265 1.32 mrg bus_space_handle_t,
266 1.32 mrg bus_size_t);
267 1.32 mrg static u_int32_t stp4020_read_4(bus_space_tag_t,
268 1.32 mrg bus_space_handle_t,
269 1.32 mrg bus_size_t);
270 1.32 mrg static u_int64_t stp4020_read_8(bus_space_tag_t,
271 1.32 mrg bus_space_handle_t,
272 1.32 mrg bus_size_t);
273 1.32 mrg static void stp4020_write_2(bus_space_tag_t,
274 1.32 mrg bus_space_handle_t,
275 1.32 mrg bus_size_t,
276 1.32 mrg u_int16_t);
277 1.32 mrg static void stp4020_write_4(bus_space_tag_t,
278 1.32 mrg bus_space_handle_t,
279 1.32 mrg bus_size_t,
280 1.32 mrg u_int32_t);
281 1.32 mrg static void stp4020_write_8(bus_space_tag_t,
282 1.32 mrg bus_space_handle_t,
283 1.32 mrg bus_size_t,
284 1.32 mrg u_int64_t);
285 1.32 mrg
286 1.32 mrg static u_int16_t
287 1.32 mrg stp4020_read_2(space, handle, offset)
288 1.32 mrg bus_space_tag_t space;
289 1.32 mrg bus_space_handle_t handle;
290 1.32 mrg bus_size_t offset;
291 1.32 mrg {
292 1.32 mrg return (le16toh(*(volatile u_int16_t *)(handle + offset)));
293 1.32 mrg }
294 1.32 mrg
295 1.32 mrg static u_int32_t
296 1.32 mrg stp4020_read_4(space, handle, offset)
297 1.32 mrg bus_space_tag_t space;
298 1.32 mrg bus_space_handle_t handle;
299 1.32 mrg bus_size_t offset;
300 1.32 mrg {
301 1.32 mrg return (le32toh(*(volatile u_int32_t *)(handle + offset)));
302 1.32 mrg }
303 1.32 mrg
304 1.32 mrg static u_int64_t
305 1.32 mrg stp4020_read_8(space, handle, offset)
306 1.32 mrg bus_space_tag_t space;
307 1.32 mrg bus_space_handle_t handle;
308 1.32 mrg bus_size_t offset;
309 1.32 mrg {
310 1.32 mrg return (le64toh(*(volatile u_int64_t *)(handle + offset)));
311 1.32 mrg }
312 1.32 mrg
313 1.32 mrg static void
314 1.32 mrg stp4020_write_2(space, handle, offset, value)
315 1.32 mrg bus_space_tag_t space;
316 1.32 mrg bus_space_handle_t handle;
317 1.32 mrg bus_size_t offset;
318 1.32 mrg u_int16_t value;
319 1.32 mrg {
320 1.32 mrg (*(volatile u_int16_t *)(handle + offset)) = htole16(value);
321 1.32 mrg }
322 1.32 mrg
323 1.32 mrg static void
324 1.32 mrg stp4020_write_4(space, handle, offset, value)
325 1.32 mrg bus_space_tag_t space;
326 1.32 mrg bus_space_handle_t handle;
327 1.32 mrg bus_size_t offset;
328 1.32 mrg u_int32_t value;
329 1.32 mrg {
330 1.32 mrg (*(volatile u_int32_t *)(handle + offset)) = htole32(value);
331 1.32 mrg }
332 1.32 mrg
333 1.32 mrg static void
334 1.32 mrg stp4020_write_8(space, handle, offset, value)
335 1.32 mrg bus_space_tag_t space;
336 1.32 mrg bus_space_handle_t handle;
337 1.32 mrg bus_size_t offset;
338 1.32 mrg u_int64_t value;
339 1.32 mrg {
340 1.32 mrg (*(volatile u_int64_t *)(handle + offset)) = htole64(value);
341 1.32 mrg }
342 1.33 martin #endif /* SUN4U */
343 1.1 pk
344 1.1 pk int
345 1.1 pk stp4020print(aux, busname)
346 1.1 pk void *aux;
347 1.1 pk const char *busname;
348 1.1 pk {
349 1.4 pk struct pcmciabus_attach_args *paa = aux;
350 1.3 pk struct stp4020_socket *h = paa->pch;
351 1.3 pk
352 1.30 thorpej aprint_normal(" socket %d", h->sock);
353 1.1 pk return (UNCONF);
354 1.1 pk }
355 1.1 pk
356 1.1 pk int
357 1.1 pk stp4020match(parent, cf, aux)
358 1.1 pk struct device *parent;
359 1.1 pk struct cfdata *cf;
360 1.1 pk void *aux;
361 1.1 pk {
362 1.1 pk struct sbus_attach_args *sa = aux;
363 1.1 pk
364 1.2 pk return (strcmp("SUNW,pcmcia", sa->sa_name) == 0);
365 1.1 pk }
366 1.1 pk
367 1.1 pk /*
368 1.1 pk * Attach all the sub-devices we can find
369 1.1 pk */
370 1.1 pk void
371 1.1 pk stp4020attach(parent, self, aux)
372 1.1 pk struct device *parent, *self;
373 1.1 pk void *aux;
374 1.1 pk {
375 1.1 pk struct sbus_attach_args *sa = aux;
376 1.1 pk struct stp4020_softc *sc = (void *)self;
377 1.32 mrg bus_space_tag_t tag;
378 1.53 martin int rev, i, sbus_intno, hw_ipl;
379 1.1 pk bus_space_handle_t bh;
380 1.1 pk
381 1.1 pk /* Transfer bus tags */
382 1.37 martin #ifdef SUN4U
383 1.37 martin tag = sa->sa_bustag;
384 1.37 martin #else
385 1.38 pk tag = bus_space_tag_alloc(sa->sa_bustag, sc);
386 1.38 pk if (tag == NULL) {
387 1.54 cegger aprint_error_dev(self, "attach: out of memory\n");
388 1.38 pk return;
389 1.38 pk }
390 1.32 mrg tag->sparc_read_2 = stp4020_read_2;
391 1.32 mrg tag->sparc_read_4 = stp4020_read_4;
392 1.32 mrg tag->sparc_read_8 = stp4020_read_8;
393 1.32 mrg tag->sparc_write_2 = stp4020_write_2;
394 1.32 mrg tag->sparc_write_4 = stp4020_write_4;
395 1.32 mrg tag->sparc_write_8 = stp4020_write_8;
396 1.38 pk #endif /* SUN4U */
397 1.1 pk
398 1.53 martin /* check interrupt options, decide if we need a softint */
399 1.53 martin #ifdef SUN4U
400 1.53 martin /*
401 1.53 martin * On sparc64 the hardware interrupt priority does not restrict
402 1.53 martin * the IPL we run our interrupt handler on, so we can always just
403 1.53 martin * use the first interrupt and reqest the handler to run at
404 1.53 martin * IPL_VM.
405 1.53 martin */
406 1.53 martin sbus_intno = 0;
407 1.53 martin hw_ipl = IPL_VM;
408 1.53 martin #else
409 1.53 martin /*
410 1.53 martin * We need to check if one of the available interrupts has
411 1.53 martin * a priority that allows us to establish a handler at IPL_VM.
412 1.53 martin * If not (hard to imagine), use a soft interrupt.
413 1.53 martin */
414 1.53 martin sbus_intno = -1;
415 1.53 martin for (i = 0; i < sa->sa_nintr; i++) {
416 1.53 martin struct sbus_softc *bus =
417 1.53 martin (struct sbus_softc *) sa->sa_bustag->cookie;
418 1.53 martin int ipl = bus->sc_intr2ipl[sa->sa_intr[i].oi_pri];
419 1.53 martin if (ipl <= IPL_VM) {
420 1.53 martin sbus_intno = i;
421 1.53 martin sc->sc_use_softint = false;
422 1.53 martin hw_ipl = IPL_VM;
423 1.53 martin break;
424 1.53 martin }
425 1.53 martin }
426 1.53 martin if (sbus_intno == -1) {
427 1.53 martin /*
428 1.53 martin * We have not found a usable hardware interrupt - so
429 1.53 martin * use a softint to bounce to the proper IPL.
430 1.53 martin */
431 1.53 martin printf("no usable HW interrupt found, using softint\n");
432 1.53 martin sbus_intno = 0;
433 1.53 martin sc->sc_use_softint = true;
434 1.53 martin hw_ipl = IPL_NONE;
435 1.53 martin }
436 1.53 martin #endif
437 1.53 martin
438 1.1 pk /* Set up per-socket static initialization */
439 1.1 pk sc->sc_socks[0].sc = sc->sc_socks[1].sc = sc;
440 1.33 martin sc->sc_socks[0].tag = sc->sc_socks[1].tag = sa->sa_bustag;
441 1.33 martin /*
442 1.33 martin * XXX we rely on "tag" accepting the same handle-domain
443 1.33 martin * as sa->sa_bustag.
444 1.33 martin */
445 1.33 martin sc->sc_socks[0].pcmciat = sc->sc_socks[1].pcmciat = tag;
446 1.28 martin sc->sc_socks[0].sbus_intno =
447 1.28 martin sc->sc_socks[1].sbus_intno = sbus_intno;
448 1.1 pk
449 1.9 pk if (sa->sa_nreg < 8) {
450 1.1 pk printf("%s: only %d register sets\n",
451 1.54 cegger device_xname(self), sa->sa_nreg);
452 1.1 pk return;
453 1.1 pk }
454 1.1 pk
455 1.1 pk if (sa->sa_nintr != 2) {
456 1.1 pk printf("%s: expect 2 interrupt Sbus levels; got %d\n",
457 1.54 cegger device_xname(self), sa->sa_nintr);
458 1.1 pk return;
459 1.1 pk }
460 1.1 pk
461 1.9 pk #define STP4020_BANK_PROM 0
462 1.1 pk #define STP4020_BANK_CTRL 4
463 1.1 pk for (i = 0; i < 8; i++) {
464 1.10 pk
465 1.1 pk /*
466 1.1 pk * STP4020 Register address map:
467 1.1 pk * bank 0: Forth PROM
468 1.1 pk * banks 1-3: socket 0, windows 0-2
469 1.1 pk * bank 4: control registers
470 1.1 pk * banks 5-7: socket 1, windows 0-2
471 1.1 pk */
472 1.10 pk
473 1.9 pk if (i == STP4020_BANK_PROM)
474 1.9 pk /* Skip the PROM */
475 1.9 pk continue;
476 1.9 pk
477 1.1 pk if (sbus_bus_map(sa->sa_bustag,
478 1.24 martin sa->sa_reg[i].oa_space,
479 1.24 martin sa->sa_reg[i].oa_base,
480 1.24 martin sa->sa_reg[i].oa_size,
481 1.21 eeh 0, &bh) != 0) {
482 1.54 cegger aprint_error_dev(self, "attach: cannot map registers\n");
483 1.1 pk return;
484 1.43 perry }
485 1.10 pk
486 1.10 pk if (i == STP4020_BANK_CTRL) {
487 1.10 pk /*
488 1.10 pk * Copy tag and handle to both socket structures
489 1.10 pk * for easy access in control/status IO functions.
490 1.10 pk */
491 1.10 pk sc->sc_socks[0].regs = sc->sc_socks[1].regs = bh;
492 1.10 pk } else if (i < STP4020_BANK_CTRL) {
493 1.10 pk /* banks 1-3 */
494 1.10 pk sc->sc_socks[0].windows[i-1].winaddr = bh;
495 1.10 pk } else {
496 1.10 pk /* banks 5-7 */
497 1.10 pk sc->sc_socks[1].windows[i-5].winaddr = bh;
498 1.10 pk }
499 1.1 pk }
500 1.1 pk
501 1.1 pk sbus_establish(&sc->sc_sd, &sc->sc_dev);
502 1.1 pk
503 1.28 martin /* We only use one interrupt level. */
504 1.28 martin if (sa->sa_nintr > sbus_intno) {
505 1.28 martin bus_intr_establish(sa->sa_bustag,
506 1.28 martin sa->sa_intr[sbus_intno].oi_pri,
507 1.53 martin hw_ipl, stp4020_intr, sc);
508 1.7 pk }
509 1.1 pk
510 1.1 pk rev = stp4020_rd_sockctl(&sc->sc_socks[0], STP4020_ISR1_IDX) &
511 1.1 pk STP4020_ISR1_REV_M;
512 1.1 pk printf(": rev %x\n", rev);
513 1.1 pk
514 1.1 pk sc->sc_pct = (pcmcia_chipset_tag_t)&stp4020_functions;
515 1.1 pk
516 1.1 pk SIMPLEQ_INIT(&sc->events);
517 1.1 pk
518 1.1 pk for (i = 0; i < STP4020_NSOCK; i++) {
519 1.1 pk struct stp4020_socket *h = &sc->sc_socks[i];
520 1.1 pk h->sock = i;
521 1.1 pk h->sc = sc;
522 1.6 pk #ifdef STP4020_DEBUG
523 1.18 martin if (stp4020_debug)
524 1.18 martin stp4020_dump_regs(h);
525 1.6 pk #endif
526 1.16 martin stp4020_attach_socket(h, sa->sa_frequency);
527 1.1 pk }
528 1.50 ad
529 1.50 ad /*
530 1.50 ad * Arrange that a kernel thread be created to handle
531 1.50 ad * insert/removal events.
532 1.50 ad */
533 1.50 ad if (kthread_create(PRI_NONE, 0, NULL, stp4020_event_thread, sc,
534 1.54 cegger &sc->event_thread, "%s", device_xname(self))) {
535 1.54 cegger panic("%s: unable to create event thread", device_xname(self));
536 1.50 ad }
537 1.1 pk }
538 1.1 pk
539 1.1 pk void
540 1.16 martin stp4020_attach_socket(h, speed)
541 1.1 pk struct stp4020_socket *h;
542 1.16 martin int speed;
543 1.1 pk {
544 1.1 pk struct pcmciabus_attach_args paa;
545 1.1 pk int v;
546 1.1 pk
547 1.31 martin /* no interrupt handlers yet */
548 1.31 martin h->intrhandler = NULL;
549 1.31 martin h->intrarg = NULL;
550 1.53 martin #ifndef SUN4U
551 1.31 martin h->softint = NULL;
552 1.31 martin h->int_enable = 0;
553 1.31 martin h->int_disable = 0;
554 1.53 martin #endif
555 1.31 martin
556 1.15 martin /* Map all three windows */
557 1.16 martin stp4020_map_window(h, STP_WIN_ATTR, speed);
558 1.16 martin stp4020_map_window(h, STP_WIN_MEM, speed);
559 1.16 martin stp4020_map_window(h, STP_WIN_IO, speed);
560 1.1 pk
561 1.1 pk /* Configure one pcmcia device per socket */
562 1.9 pk paa.paa_busname = "pcmcia";
563 1.1 pk paa.pct = (pcmcia_chipset_tag_t)h->sc->sc_pct;
564 1.1 pk paa.pch = (pcmcia_chipset_handle_t)h;
565 1.1 pk paa.iobase = 0;
566 1.15 martin paa.iosize = STP4020_WINDOW_SIZE;
567 1.1 pk
568 1.1 pk h->pcmcia = config_found(&h->sc->sc_dev, &paa, stp4020print);
569 1.1 pk
570 1.1 pk if (h->pcmcia == NULL)
571 1.1 pk return;
572 1.1 pk
573 1.1 pk /*
574 1.1 pk * There's actually a pcmcia bus attached; initialize the slot.
575 1.1 pk */
576 1.1 pk
577 1.1 pk /*
578 1.16 martin * Clear things up before we enable status change interrupts.
579 1.16 martin * This seems to not be fully initialized by the PROM.
580 1.16 martin */
581 1.16 martin stp4020_wr_sockctl(h, STP4020_ICR1_IDX, 0);
582 1.16 martin stp4020_wr_sockctl(h, STP4020_ICR0_IDX, 0);
583 1.16 martin stp4020_wr_sockctl(h, STP4020_ISR1_IDX, 0x3fff);
584 1.16 martin stp4020_wr_sockctl(h, STP4020_ISR0_IDX, 0x3fff);
585 1.16 martin
586 1.16 martin /*
587 1.1 pk * Enable socket status change interrupts.
588 1.28 martin * We only use one common interrupt for status change
589 1.28 martin * and IO, to avoid locking issues.
590 1.1 pk */
591 1.28 martin v = STP4020_ICR0_ALL_STATUS_IE
592 1.28 martin | (h->sbus_intno ? STP4020_ICR0_SCILVL_SB1
593 1.28 martin : STP4020_ICR0_SCILVL_SB0);
594 1.1 pk stp4020_wr_sockctl(h, STP4020_ICR0_IDX, v);
595 1.1 pk
596 1.35 martin /* Get live status bits from ISR0 and clear pending interrupts */
597 1.1 pk v = stp4020_rd_sockctl(h, STP4020_ISR0_IDX);
598 1.35 martin stp4020_wr_sockctl(h, STP4020_ISR0_IDX, v);
599 1.35 martin
600 1.1 pk if ((v & (STP4020_ISR0_CD1ST|STP4020_ISR0_CD2ST)) == 0)
601 1.1 pk return;
602 1.1 pk
603 1.1 pk pcmcia_card_attach(h->pcmcia);
604 1.1 pk h->flags |= STP4020_SOCKET_BUSY;
605 1.1 pk }
606 1.1 pk
607 1.1 pk /*
608 1.1 pk * The actual event handling thread.
609 1.1 pk */
610 1.1 pk void
611 1.1 pk stp4020_event_thread(arg)
612 1.1 pk void *arg;
613 1.1 pk {
614 1.1 pk struct stp4020_softc *sc = arg;
615 1.1 pk struct stp4020_event *e;
616 1.1 pk int s;
617 1.1 pk
618 1.1 pk while (1) {
619 1.1 pk struct stp4020_socket *h;
620 1.1 pk int n;
621 1.1 pk
622 1.1 pk s = splhigh();
623 1.1 pk if ((e = SIMPLEQ_FIRST(&sc->events)) == NULL) {
624 1.1 pk splx(s);
625 1.45 martin (void)tsleep(&sc->events, PWAIT, "nellevt", 0);
626 1.1 pk continue;
627 1.1 pk }
628 1.23 lukem SIMPLEQ_REMOVE_HEAD(&sc->events, se_q);
629 1.1 pk splx(s);
630 1.1 pk
631 1.1 pk n = e->se_sock;
632 1.1 pk if (n < 0 || n >= STP4020_NSOCK)
633 1.1 pk panic("stp4020_event_thread: wayward socket number %d",
634 1.1 pk n);
635 1.1 pk
636 1.1 pk h = &sc->sc_socks[n];
637 1.1 pk switch (e->se_type) {
638 1.1 pk case STP4020_EVENT_INSERTION:
639 1.1 pk pcmcia_card_attach(h->pcmcia);
640 1.1 pk break;
641 1.1 pk case STP4020_EVENT_REMOVAL:
642 1.1 pk pcmcia_card_detach(h->pcmcia, DETACH_FORCE);
643 1.1 pk break;
644 1.1 pk default:
645 1.1 pk panic("stp4020_event_thread: unknown event type %d",
646 1.1 pk e->se_type);
647 1.1 pk }
648 1.1 pk free(e, M_TEMP);
649 1.1 pk }
650 1.1 pk }
651 1.1 pk
652 1.1 pk void
653 1.1 pk stp4020_queue_event(sc, sock, event)
654 1.1 pk struct stp4020_softc *sc;
655 1.1 pk int sock, event;
656 1.1 pk {
657 1.1 pk struct stp4020_event *e;
658 1.1 pk int s;
659 1.1 pk
660 1.1 pk e = malloc(sizeof(*e), M_TEMP, M_NOWAIT);
661 1.1 pk if (e == NULL)
662 1.1 pk panic("stp4020_queue_event: can't allocate event");
663 1.1 pk
664 1.1 pk e->se_type = event;
665 1.1 pk e->se_sock = sock;
666 1.1 pk s = splhigh();
667 1.1 pk SIMPLEQ_INSERT_TAIL(&sc->events, e, se_q);
668 1.1 pk splx(s);
669 1.1 pk wakeup(&sc->events);
670 1.1 pk }
671 1.1 pk
672 1.53 martin #ifndef SUN4U
673 1.31 martin /*
674 1.31 martin * Softinterrupt called to invoke the real driver interrupt handler.
675 1.31 martin */
676 1.31 martin static void
677 1.31 martin stp4020_intr_dispatch(arg)
678 1.31 martin void *arg;
679 1.31 martin {
680 1.31 martin struct stp4020_socket *h = arg;
681 1.31 martin int s;
682 1.31 martin
683 1.31 martin /* invoke driver handler */
684 1.31 martin h->intrhandler(h->intrarg);
685 1.31 martin
686 1.31 martin /* enable SBUS interrupts for pcmcia interrupts again */
687 1.31 martin s = splhigh();
688 1.31 martin stp4020_wr_sockctl(h, STP4020_ICR0_IDX, h->int_enable);
689 1.31 martin splx(s);
690 1.31 martin }
691 1.53 martin #endif
692 1.31 martin
693 1.1 pk int
694 1.28 martin stp4020_intr(arg)
695 1.1 pk void *arg;
696 1.1 pk {
697 1.1 pk struct stp4020_softc *sc = arg;
698 1.53 martin #ifndef SUN4U
699 1.53 martin int s;
700 1.53 martin #endif
701 1.53 martin int i, r = 0, cd_change = 0;
702 1.31 martin
703 1.31 martin
704 1.53 martin #ifndef SUN4U
705 1.31 martin /* protect hardware access by splhigh against softint */
706 1.31 martin s = splhigh();
707 1.53 martin #endif
708 1.1 pk
709 1.1 pk /*
710 1.1 pk * Check each socket for pending requests.
711 1.1 pk */
712 1.1 pk for (i = 0 ; i < STP4020_NSOCK; i++) {
713 1.1 pk struct stp4020_socket *h;
714 1.28 martin int v;
715 1.1 pk
716 1.1 pk h = &sc->sc_socks[i];
717 1.31 martin
718 1.28 martin v = stp4020_rd_sockctl(h, STP4020_ISR0_IDX);
719 1.1 pk
720 1.31 martin /* Ack all interrupts at once. */
721 1.35 martin stp4020_wr_sockctl(h, STP4020_ISR0_IDX, v);
722 1.1 pk
723 1.1 pk #ifdef STP4020_DEBUG
724 1.1 pk if (stp4020_debug != 0) {
725 1.1 pk char bits[64];
726 1.1 pk bitmask_snprintf(v, STP4020_ISR0_IOBITS,
727 1.1 pk bits, sizeof(bits));
728 1.1 pk printf("stp4020_statintr: ISR0=%s\n", bits);
729 1.1 pk }
730 1.1 pk #endif
731 1.1 pk
732 1.1 pk if ((v & STP4020_ISR0_CDCHG) != 0) {
733 1.1 pk /*
734 1.1 pk * Card status change detect
735 1.1 pk */
736 1.18 martin cd_change = 1;
737 1.18 martin r = 1;
738 1.18 martin if ((v & (STP4020_ISR0_CD1ST|STP4020_ISR0_CD2ST)) == (STP4020_ISR0_CD1ST|STP4020_ISR0_CD2ST)){
739 1.1 pk if ((h->flags & STP4020_SOCKET_BUSY) == 0) {
740 1.1 pk stp4020_queue_event(sc, i,
741 1.1 pk STP4020_EVENT_INSERTION);
742 1.1 pk h->flags |= STP4020_SOCKET_BUSY;
743 1.1 pk }
744 1.1 pk }
745 1.1 pk if ((v & (STP4020_ISR0_CD1ST|STP4020_ISR0_CD2ST)) == 0){
746 1.1 pk if ((h->flags & STP4020_SOCKET_BUSY) != 0) {
747 1.1 pk stp4020_queue_event(sc, i,
748 1.1 pk STP4020_EVENT_REMOVAL);
749 1.1 pk h->flags &= ~STP4020_SOCKET_BUSY;
750 1.1 pk }
751 1.1 pk }
752 1.1 pk }
753 1.43 perry
754 1.28 martin if ((v & STP4020_ISR0_IOINT) != 0) {
755 1.28 martin /* we can not deny this is ours, no matter what the
756 1.28 martin card driver says. */
757 1.28 martin r = 1;
758 1.28 martin
759 1.28 martin /* It's a card interrupt */
760 1.28 martin if ((h->flags & STP4020_SOCKET_BUSY) == 0) {
761 1.28 martin printf("stp4020[%d]: spurious interrupt?\n",
762 1.28 martin h->sock);
763 1.28 martin continue;
764 1.28 martin }
765 1.31 martin
766 1.53 martin #ifndef SUN4U
767 1.31 martin /*
768 1.43 perry * Schedule softint to invoke driver interrupt
769 1.31 martin * handler
770 1.31 martin */
771 1.31 martin if (h->softint != NULL)
772 1.53 martin sparc_softintr_schedule(h->softint);
773 1.31 martin /*
774 1.31 martin * Disable this sbus interrupt, until the soft-int
775 1.31 martin * handler had a chance to run
776 1.31 martin */
777 1.31 martin stp4020_wr_sockctl(h, STP4020_ICR0_IDX, h->int_disable);
778 1.53 martin #else
779 1.53 martin (*h->intrhandler)(h->intrarg);
780 1.53 martin #endif
781 1.28 martin }
782 1.1 pk
783 1.18 martin /* informational messages */
784 1.1 pk if ((v & STP4020_ISR0_BVD1CHG) != 0) {
785 1.18 martin /* ignore if this is caused by insert or removal */
786 1.18 martin if (!cd_change)
787 1.18 martin printf("stp4020[%d]: Battery change 1\n", h->sock);
788 1.15 martin r = 1;
789 1.1 pk }
790 1.1 pk
791 1.1 pk if ((v & STP4020_ISR0_BVD2CHG) != 0) {
792 1.18 martin /* ignore if this is caused by insert or removal */
793 1.18 martin if (!cd_change)
794 1.18 martin printf("stp4020[%d]: Battery change 2\n", h->sock);
795 1.15 martin r = 1;
796 1.1 pk }
797 1.1 pk
798 1.36 martin if ((v & STP4020_ISR0_SCINT) != 0) {
799 1.36 martin DPRINTF(("stp4020[%d]: status change\n", h->sock));
800 1.36 martin r = 1;
801 1.36 martin }
802 1.36 martin
803 1.1 pk if ((v & STP4020_ISR0_RDYCHG) != 0) {
804 1.18 martin DPRINTF(("stp4020[%d]: Ready/Busy change\n", h->sock));
805 1.15 martin r = 1;
806 1.1 pk }
807 1.1 pk
808 1.1 pk if ((v & STP4020_ISR0_WPCHG) != 0) {
809 1.18 martin DPRINTF(("stp4020[%d]: Write protect change\n", h->sock));
810 1.15 martin r = 1;
811 1.1 pk }
812 1.1 pk
813 1.1 pk if ((v & STP4020_ISR0_PCTO) != 0) {
814 1.18 martin DPRINTF(("stp4020[%d]: Card access timeout\n", h->sock));
815 1.15 martin r = 1;
816 1.1 pk }
817 1.18 martin
818 1.35 martin if ((v & ~STP4020_ISR0_LIVE) && r == 0)
819 1.35 martin printf("stp4020[%d]: unhandled interrupt: 0x%x\n", h->sock, v);
820 1.35 martin
821 1.1 pk }
822 1.53 martin #ifndef SUN4U
823 1.31 martin splx(s);
824 1.53 martin #endif
825 1.1 pk
826 1.1 pk return (r);
827 1.1 pk }
828 1.1 pk
829 1.16 martin /*
830 1.16 martin * The function gets the sbus speed and a access time and calculates
831 1.16 martin * values for the CMDLNG and CMDDLAY registers.
832 1.16 martin */
833 1.15 martin static void
834 1.44 jdc stp4020_calc_speed(int bus_speed, int ns, int *length, int *cmd_delay)
835 1.1 pk {
836 1.16 martin int result;
837 1.16 martin
838 1.16 martin if (ns < STP4020_MEM_SPEED_MIN)
839 1.16 martin ns = STP4020_MEM_SPEED_MIN;
840 1.16 martin else if (ns > STP4020_MEM_SPEED_MAX)
841 1.16 martin ns = STP4020_MEM_SPEED_MAX;
842 1.16 martin result = ns*(bus_speed/1000);
843 1.16 martin if (result % 1000000)
844 1.16 martin result = result/1000000 + 1;
845 1.16 martin else
846 1.16 martin result /= 1000000;
847 1.16 martin *length = result;
848 1.16 martin
849 1.16 martin /* the sbus frequency range is limited, so we can keep this simple */
850 1.44 jdc *cmd_delay = ns <= STP4020_MEM_SPEED_MIN? 1 : 2;
851 1.16 martin }
852 1.15 martin
853 1.16 martin static void
854 1.16 martin stp4020_map_window(struct stp4020_socket *h, int win, int speed)
855 1.16 martin {
856 1.44 jdc int v, length, cmd_delay;
857 1.15 martin
858 1.15 martin /*
859 1.16 martin * According to the PC Card standard 300ns access timing should be
860 1.16 martin * used for attribute memory access. Our pcmcia framework does not
861 1.16 martin * seem to propagate timing information, so we use that
862 1.16 martin * everywhere.
863 1.15 martin */
864 1.44 jdc stp4020_calc_speed(speed, (win==STP_WIN_ATTR)? 300 : 100, &length, &cmd_delay);
865 1.1 pk
866 1.1 pk /*
867 1.15 martin * Fill in the Address Space Select and Base Address
868 1.15 martin * fields of this windows control register 0.
869 1.1 pk */
870 1.44 jdc v = ((cmd_delay << STP4020_WCR0_CMDDLY_S)&STP4020_WCR0_CMDDLY_M)
871 1.16 martin | ((length << STP4020_WCR0_CMDLNG_S)&STP4020_WCR0_CMDLNG_M);
872 1.15 martin switch (win) {
873 1.15 martin case STP_WIN_ATTR:
874 1.15 martin v |= STP4020_WCR0_ASPSEL_AM;
875 1.15 martin break;
876 1.15 martin case STP_WIN_MEM:
877 1.15 martin v |= STP4020_WCR0_ASPSEL_CM;
878 1.15 martin break;
879 1.15 martin case STP_WIN_IO:
880 1.15 martin v |= STP4020_WCR0_ASPSEL_IO;
881 1.15 martin break;
882 1.15 martin }
883 1.15 martin v |= (STP4020_ADDR2PAGE(0) & STP4020_WCR0_BASE_M);
884 1.15 martin stp4020_wr_winctl(h, win, STP4020_WCR0_IDX, v);
885 1.16 martin stp4020_wr_winctl(h, win, STP4020_WCR1_IDX, 1<<STP4020_WCR1_WAITREQ_S);
886 1.15 martin }
887 1.1 pk
888 1.15 martin int
889 1.15 martin stp4020_chip_mem_alloc(pch, size, pcmhp)
890 1.15 martin pcmcia_chipset_handle_t pch;
891 1.15 martin bus_size_t size;
892 1.15 martin struct pcmcia_mem_handle *pcmhp;
893 1.15 martin {
894 1.15 martin struct stp4020_socket *h = (struct stp4020_socket *)pch;
895 1.1 pk
896 1.15 martin /* we can not do much here, defere work to _mem_map */
897 1.33 martin pcmhp->memt = h->pcmciat;
898 1.1 pk pcmhp->size = size;
899 1.19 martin pcmhp->addr = 0;
900 1.19 martin pcmhp->mhandle = 0;
901 1.19 martin pcmhp->realsize = size;
902 1.1 pk
903 1.1 pk return (0);
904 1.1 pk }
905 1.1 pk
906 1.1 pk void
907 1.1 pk stp4020_chip_mem_free(pch, pcmhp)
908 1.1 pk pcmcia_chipset_handle_t pch;
909 1.1 pk struct pcmcia_mem_handle *pcmhp;
910 1.1 pk {
911 1.1 pk }
912 1.1 pk
913 1.1 pk int
914 1.1 pk stp4020_chip_mem_map(pch, kind, card_addr, size, pcmhp, offsetp, windowp)
915 1.1 pk pcmcia_chipset_handle_t pch;
916 1.1 pk int kind;
917 1.1 pk bus_addr_t card_addr;
918 1.1 pk bus_size_t size;
919 1.1 pk struct pcmcia_mem_handle *pcmhp;
920 1.14 soren bus_size_t *offsetp;
921 1.1 pk int *windowp;
922 1.1 pk {
923 1.1 pk struct stp4020_socket *h = (struct stp4020_socket *)pch;
924 1.15 martin int win = (kind&PCMCIA_MEM_ATTR)? STP_WIN_ATTR : STP_WIN_MEM;
925 1.8 joda
926 1.33 martin pcmhp->memt = h->pcmciat;
927 1.33 martin bus_space_subregion(h->pcmciat, h->windows[win].winaddr, card_addr, size, &pcmhp->memh);
928 1.34 martin #ifdef SUN4U
929 1.34 martin if ((u_int8_t)pcmhp->memh._asi == ASI_PHYS_NON_CACHED)
930 1.34 martin pcmhp->memh._asi = ASI_PHYS_NON_CACHED_LITTLE;
931 1.34 martin else if ((u_int8_t)pcmhp->memh._asi == ASI_PRIMARY)
932 1.34 martin pcmhp->memh._asi = ASI_PRIMARY_LITTLE;
933 1.34 martin #endif
934 1.19 martin pcmhp->size = size;
935 1.19 martin pcmhp->realsize = STP4020_WINDOW_SIZE - card_addr;
936 1.15 martin *offsetp = 0;
937 1.15 martin *windowp = 0;
938 1.1 pk
939 1.1 pk return (0);
940 1.1 pk }
941 1.1 pk
942 1.1 pk void
943 1.1 pk stp4020_chip_mem_unmap(pch, win)
944 1.1 pk pcmcia_chipset_handle_t pch;
945 1.1 pk int win;
946 1.1 pk {
947 1.1 pk }
948 1.1 pk
949 1.1 pk int
950 1.1 pk stp4020_chip_io_alloc(pch, start, size, align, pcihp)
951 1.1 pk pcmcia_chipset_handle_t pch;
952 1.1 pk bus_addr_t start;
953 1.1 pk bus_size_t size;
954 1.1 pk bus_size_t align;
955 1.1 pk struct pcmcia_io_handle *pcihp;
956 1.1 pk {
957 1.1 pk struct stp4020_socket *h = (struct stp4020_socket *)pch;
958 1.1 pk
959 1.33 martin pcihp->iot = h->pcmciat;
960 1.15 martin pcihp->ioh = h->windows[STP_WIN_IO].winaddr;
961 1.15 martin return 0;
962 1.1 pk }
963 1.1 pk
964 1.1 pk void
965 1.1 pk stp4020_chip_io_free(pch, pcihp)
966 1.1 pk pcmcia_chipset_handle_t pch;
967 1.1 pk struct pcmcia_io_handle *pcihp;
968 1.1 pk {
969 1.1 pk }
970 1.1 pk
971 1.1 pk int
972 1.1 pk stp4020_chip_io_map(pch, width, offset, size, pcihp, windowp)
973 1.1 pk pcmcia_chipset_handle_t pch;
974 1.1 pk int width;
975 1.1 pk bus_addr_t offset;
976 1.1 pk bus_size_t size;
977 1.1 pk struct pcmcia_io_handle *pcihp;
978 1.1 pk int *windowp;
979 1.1 pk {
980 1.1 pk struct stp4020_socket *h = (struct stp4020_socket *)pch;
981 1.1 pk
982 1.33 martin pcihp->iot = h->pcmciat;
983 1.33 martin bus_space_subregion(h->pcmciat, h->windows[STP_WIN_IO].winaddr, offset, size, &pcihp->ioh);
984 1.34 martin #ifdef SUN4U
985 1.34 martin if ((u_int8_t)pcihp->ioh._asi == ASI_PHYS_NON_CACHED)
986 1.34 martin pcihp->ioh._asi = ASI_PHYS_NON_CACHED_LITTLE;
987 1.34 martin else if ((u_int8_t)pcihp->ioh._asi == ASI_PRIMARY)
988 1.34 martin pcihp->ioh._asi = ASI_PRIMARY_LITTLE;
989 1.34 martin #endif
990 1.15 martin *windowp = 0;
991 1.15 martin return 0;
992 1.1 pk }
993 1.1 pk
994 1.1 pk void
995 1.1 pk stp4020_chip_io_unmap(pch, win)
996 1.1 pk pcmcia_chipset_handle_t pch;
997 1.1 pk int win;
998 1.1 pk {
999 1.1 pk }
1000 1.1 pk
1001 1.1 pk void
1002 1.1 pk stp4020_chip_socket_enable(pch)
1003 1.1 pk pcmcia_chipset_handle_t pch;
1004 1.1 pk {
1005 1.1 pk struct stp4020_socket *h = (struct stp4020_socket *)pch;
1006 1.18 martin int i, v;
1007 1.1 pk
1008 1.1 pk /* this bit is mostly stolen from pcic_attach_card */
1009 1.1 pk
1010 1.1 pk /* Power down the socket to reset it, clear the card reset pin */
1011 1.18 martin stp4020_wr_sockctl(h, STP4020_ICR1_IDX, 0);
1012 1.1 pk
1013 1.1 pk /*
1014 1.1 pk * wait 300ms until power fails (Tpf). Then, wait 100ms since
1015 1.1 pk * we are changing Vcc (Toff).
1016 1.1 pk */
1017 1.46 martin stp4020_delay(h->sc, 300 + 100);
1018 1.1 pk
1019 1.1 pk /* Power up the socket */
1020 1.18 martin v = STP4020_ICR1_MSTPWR;
1021 1.1 pk stp4020_wr_sockctl(h, STP4020_ICR1_IDX, v);
1022 1.1 pk
1023 1.1 pk /*
1024 1.1 pk * wait 100ms until power raise (Tpr) and 20ms to become
1025 1.1 pk * stable (Tsu(Vcc)).
1026 1.1 pk */
1027 1.46 martin stp4020_delay(h->sc, 100 + 20);
1028 1.1 pk
1029 1.18 martin v |= STP4020_ICR1_PCIFOE|STP4020_ICR1_VPP1_VCC;
1030 1.1 pk stp4020_wr_sockctl(h, STP4020_ICR1_IDX, v);
1031 1.1 pk
1032 1.1 pk /*
1033 1.1 pk * hold RESET at least 10us.
1034 1.1 pk */
1035 1.1 pk delay(10);
1036 1.1 pk
1037 1.40 mycroft /* Clear reset flag, set to memory mode */
1038 1.1 pk v = stp4020_rd_sockctl(h, STP4020_ICR0_IDX);
1039 1.40 mycroft v &= ~(STP4020_ICR0_IOIE | STP4020_ICR0_IOILVL | STP4020_ICR0_IFTYPE |
1040 1.40 mycroft STP4020_ICR0_SPKREN);
1041 1.1 pk v &= ~STP4020_ICR0_RESET;
1042 1.1 pk stp4020_wr_sockctl(h, STP4020_ICR0_IDX, v);
1043 1.1 pk
1044 1.1 pk /* wait 20ms as per pc card standard (r2.01) section 4.3.6 */
1045 1.46 martin stp4020_delay(h->sc, 20);
1046 1.1 pk
1047 1.1 pk /* Wait for the chip to finish initializing (5 seconds max) */
1048 1.1 pk for (i = 10000; i > 0; i--) {
1049 1.1 pk v = stp4020_rd_sockctl(h, STP4020_ISR0_IDX);
1050 1.1 pk if ((v & STP4020_ISR0_RDYST) != 0)
1051 1.1 pk break;
1052 1.1 pk delay(500);
1053 1.1 pk }
1054 1.1 pk if (i <= 0) {
1055 1.1 pk char bits[64];
1056 1.1 pk bitmask_snprintf(stp4020_rd_sockctl(h, STP4020_ISR0_IDX),
1057 1.1 pk STP4020_ISR0_IOBITS, bits, sizeof(bits));
1058 1.1 pk printf("stp4020_chip_socket_enable: not ready: status %s\n",
1059 1.1 pk bits);
1060 1.1 pk return;
1061 1.1 pk }
1062 1.39 mycroft }
1063 1.1 pk
1064 1.39 mycroft void
1065 1.39 mycroft stp4020_chip_socket_settype(pch, type)
1066 1.39 mycroft pcmcia_chipset_handle_t pch;
1067 1.39 mycroft int type;
1068 1.39 mycroft {
1069 1.39 mycroft struct stp4020_socket *h = (struct stp4020_socket *)pch;
1070 1.39 mycroft int v;
1071 1.1 pk
1072 1.1 pk /*
1073 1.18 martin * Check the card type.
1074 1.18 martin * Enable socket I/O interrupts for IO cards.
1075 1.1 pk */
1076 1.39 mycroft v = stp4020_rd_sockctl(h, STP4020_ICR0_IDX);
1077 1.41 mycroft v &= ~(STP4020_ICR0_IOIE | STP4020_ICR0_IOILVL | STP4020_ICR0_IFTYPE |
1078 1.41 mycroft STP4020_ICR0_SPKREN);
1079 1.39 mycroft if (type == PCMCIA_IFTYPE_IO) {
1080 1.18 martin v |= STP4020_ICR0_IFTYPE_IO|STP4020_ICR0_IOIE
1081 1.28 martin |STP4020_ICR0_SPKREN;
1082 1.28 martin v |= h->sbus_intno ? STP4020_ICR0_IOILVL_SB1
1083 1.28 martin : STP4020_ICR0_IOILVL_SB0;
1084 1.53 martin #ifndef SUN4U
1085 1.31 martin h->int_enable = v;
1086 1.31 martin h->int_disable = v & ~STP4020_ICR0_IOIE;
1087 1.53 martin #endif
1088 1.54 cegger DPRINTF(("%s: configuring card for IO useage\n", device_xname(&h->sc->sc_dev)));
1089 1.18 martin } else {
1090 1.18 martin v |= STP4020_ICR0_IFTYPE_MEM;
1091 1.53 martin #ifndef SUN4U
1092 1.35 martin h->int_enable = h->int_disable = v;
1093 1.53 martin #endif
1094 1.54 cegger DPRINTF(("%s: configuring card for IO useage\n", device_xname(&h->sc->sc_dev)));
1095 1.54 cegger DPRINTF(("%s: configuring card for MEM ONLY useage\n", device_xname(&h->sc->sc_dev)));
1096 1.18 martin }
1097 1.1 pk stp4020_wr_sockctl(h, STP4020_ICR0_IDX, v);
1098 1.1 pk }
1099 1.1 pk
1100 1.1 pk void
1101 1.1 pk stp4020_chip_socket_disable(pch)
1102 1.1 pk pcmcia_chipset_handle_t pch;
1103 1.1 pk {
1104 1.1 pk struct stp4020_socket *h = (struct stp4020_socket *)pch;
1105 1.1 pk int v;
1106 1.1 pk
1107 1.1 pk /*
1108 1.1 pk * Disable socket I/O interrupts.
1109 1.1 pk */
1110 1.1 pk v = stp4020_rd_sockctl(h, STP4020_ICR0_IDX);
1111 1.40 mycroft v &= ~(STP4020_ICR0_IOIE | STP4020_ICR0_IOILVL | STP4020_ICR0_IFTYPE |
1112 1.40 mycroft STP4020_ICR0_SPKREN);
1113 1.1 pk stp4020_wr_sockctl(h, STP4020_ICR0_IDX, v);
1114 1.1 pk
1115 1.1 pk /* Power down the socket */
1116 1.18 martin stp4020_wr_sockctl(h, STP4020_ICR1_IDX, 0);
1117 1.1 pk
1118 1.1 pk /*
1119 1.1 pk * wait 300ms until power fails (Tpf).
1120 1.1 pk */
1121 1.46 martin stp4020_delay(h->sc, 300);
1122 1.1 pk }
1123 1.1 pk
1124 1.1 pk void *
1125 1.1 pk stp4020_chip_intr_establish(pch, pf, ipl, handler, arg)
1126 1.1 pk pcmcia_chipset_handle_t pch;
1127 1.1 pk struct pcmcia_function *pf;
1128 1.1 pk int ipl;
1129 1.42 perry int (*handler)(void *);
1130 1.1 pk void *arg;
1131 1.1 pk {
1132 1.1 pk struct stp4020_socket *h = (struct stp4020_socket *)pch;
1133 1.1 pk
1134 1.31 martin /* only one interrupt handler per slot */
1135 1.31 martin if (h->intrhandler != NULL) return NULL;
1136 1.31 martin
1137 1.1 pk h->intrhandler = handler;
1138 1.1 pk h->intrarg = arg;
1139 1.53 martin #ifndef SUN4U
1140 1.53 martin if (h->sc->sc_use_softint) {
1141 1.53 martin h->softint = sparc_softintr_establish(ipl, stp4020_intr_dispatch, h);
1142 1.53 martin return h->softint;
1143 1.53 martin }
1144 1.53 martin #endif
1145 1.53 martin return h;
1146 1.1 pk }
1147 1.1 pk
1148 1.1 pk void
1149 1.1 pk stp4020_chip_intr_disestablish(pch, ih)
1150 1.1 pk pcmcia_chipset_handle_t pch;
1151 1.1 pk void *ih;
1152 1.1 pk {
1153 1.1 pk struct stp4020_socket *h = (struct stp4020_socket *)pch;
1154 1.1 pk
1155 1.1 pk h->intrhandler = NULL;
1156 1.1 pk h->intrarg = NULL;
1157 1.53 martin #ifndef SUN4U
1158 1.31 martin if (h->softint) {
1159 1.53 martin sparc_softintr_disestablish(h->softint);
1160 1.31 martin h->softint = NULL;
1161 1.31 martin }
1162 1.53 martin #endif
1163 1.1 pk }
1164 1.1 pk
1165 1.1 pk /*
1166 1.1 pk * Delay and possibly yield CPU.
1167 1.1 pk * XXX - assumes a context
1168 1.1 pk */
1169 1.1 pk void
1170 1.46 martin stp4020_delay(sc, ms)
1171 1.46 martin struct stp4020_softc *sc;
1172 1.1 pk unsigned int ms;
1173 1.1 pk {
1174 1.46 martin unsigned int ticks = mstohz(ms);
1175 1.1 pk
1176 1.1 pk if (cold || ticks == 0) {
1177 1.1 pk delay(ms);
1178 1.1 pk return;
1179 1.1 pk }
1180 1.1 pk
1181 1.1 pk #ifdef DIAGNOSTIC
1182 1.1 pk if (ticks > 60*hz)
1183 1.1 pk panic("stp4020: preposterous delay: %u", ticks);
1184 1.1 pk #endif
1185 1.46 martin tsleep(sc, 0, "nelldel", ticks);
1186 1.1 pk }
1187 1.6 pk
1188 1.6 pk #ifdef STP4020_DEBUG
1189 1.6 pk void
1190 1.6 pk stp4020_dump_regs(h)
1191 1.6 pk struct stp4020_socket *h;
1192 1.6 pk {
1193 1.6 pk char bits[64];
1194 1.6 pk /*
1195 1.6 pk * Dump control and status registers.
1196 1.6 pk */
1197 1.6 pk printf("socket[%d] registers:\n", h->sock);
1198 1.6 pk bitmask_snprintf(stp4020_rd_sockctl(h, STP4020_ICR0_IDX),
1199 1.6 pk STP4020_ICR0_BITS, bits, sizeof(bits));
1200 1.6 pk printf("\tICR0=%s\n", bits);
1201 1.6 pk
1202 1.6 pk bitmask_snprintf(stp4020_rd_sockctl(h, STP4020_ICR1_IDX),
1203 1.6 pk STP4020_ICR1_BITS, bits, sizeof(bits));
1204 1.6 pk printf("\tICR1=%s\n", bits);
1205 1.6 pk
1206 1.6 pk bitmask_snprintf(stp4020_rd_sockctl(h, STP4020_ISR0_IDX),
1207 1.6 pk STP4020_ISR0_IOBITS, bits, sizeof(bits));
1208 1.6 pk printf("\tISR0=%s\n", bits);
1209 1.6 pk
1210 1.6 pk bitmask_snprintf(stp4020_rd_sockctl(h, STP4020_ISR1_IDX),
1211 1.6 pk STP4020_ISR1_BITS, bits, sizeof(bits));
1212 1.6 pk printf("\tISR1=%s\n", bits);
1213 1.6 pk }
1214 1.6 pk #endif /* STP4020_DEBUG */
1215