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