ts102.c revision 1.17 1 /* $OpenBSD: ts102.c,v 1.14 2005/01/27 17:03:23 millert Exp $ */
2 /* $NetBSD: ts102.c,v 1.17 2012/10/27 17:18:11 chs Exp $ */
3 /*
4 * Copyright (c) 2003, 2004, Miodrag Vallat.
5 * Copyright (c) 2005, Michael Lorenz.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /*
30 * Driver for the PCMCIA controller found in Tadpole SPARCbook 3 series
31 * notebooks.
32 *
33 * Based on the information provided in the SPARCbook 3 Technical Reference
34 * Manual (s3gxtrmb.pdf), chapter 7. A few ramblings against this document
35 * and/or the chip itself are scattered across this file.
36 *
37 * Implementation notes:
38 *
39 * - The TS102 exports its PCMCIA windows as SBus memory ranges: 64MB for
40 * the common memory window, and 16MB for the attribute and I/O windows.
41 *
42 * Mapping the whole windows would consume 192MB of address space, which
43 * is much more that what the iospace can offer.
44 *
45 * A best-effort solution would be to map the windows on demand. However,
46 * due to the wap mapdev() works, the va used for the mappings would be
47 * lost after unmapping (although using an extent to register iospace memory
48 * usage would fix this). So, instead, we will do a fixed mapping of a subset
49 * of each window upon attach - this is similar to what the stp4020 driver
50 * does.
51 *
52 * Endianness farce:
53 *
54 * - The documentation pretends that the endianness settings only affect the
55 * common memory window. Gee, thanks a lot. What about other windows, then?
56 * As a result, this driver runs with endianness conversions turned off.
57 *
58 * - One of the little-endian SBus and big-endian PCMCIA flags has the reverse
59 * meaning, actually. To achieve a ``no endianness conversion'' status,
60 * one has to be set and the other unset. It does not matter which one,
61 * though.
62 */
63
64 #include <sys/cdefs.h>
65
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/errno.h>
69 #include <sys/malloc.h>
70 #include <sys/extent.h>
71 #include <sys/proc.h>
72 #include <sys/kernel.h>
73 #include <sys/kthread.h>
74 #include <sys/device.h>
75
76 #include <dev/pcmcia/pcmciareg.h>
77 #include <dev/pcmcia/pcmciavar.h>
78 #include <dev/pcmcia/pcmciachip.h>
79
80 #include <sys/bus.h>
81 #include <machine/intr.h>
82 #include <machine/autoconf.h>
83
84 #include <dev/sbus/sbusvar.h>
85 #include <sparc/dev/ts102reg.h>
86
87 #include "tctrl.h"
88
89 #if NTCTRL > 0
90 #include <machine/tctrl.h>
91 #include <sparc/dev/tctrlvar.h>
92 #endif
93
94 #define TS102_NUM_SLOTS 2
95
96 /*
97 * Memory ranges
98 */
99 #define TS102_RANGE_COMMON 0
100 #define TS102_RANGE_ATTR 1
101 #define TS102_RANGE_IO 2
102
103 #define TS102_RANGE_CNT 3
104 #define TS102_NUM_RANGES (TS102_RANGE_CNT * TS102_NUM_SLOTS)
105
106 #define TS102_ARBITRARY_MAP_SIZE (1 * 1024 * 1024)
107
108 struct tslot_softc;
109
110 #ifdef TSLOT_DEBUG
111 #define TSPRINTF printf
112 #else
113 #define TSPRINTF while (0) printf
114 #endif
115
116 /*
117 * Per-slot data
118 */
119 struct tslot_data {
120 struct tslot_softc *td_parent;
121 device_t td_pcmcia;
122
123 volatile uint8_t *td_regs;
124 bus_addr_t td_space[TS102_RANGE_CNT];
125 bus_space_tag_t td_pcmciat; /* for accessing cards */
126
127 /* Interrupt handler */
128 int (*td_intr)(void *);
129 void *td_intrarg;
130 void *td_softint;
131
132 /* Socket status */
133 int td_slot;
134 int td_status;
135 #define TS_CARD 0x0001
136 };
137
138 struct tslot_softc {
139 device_t sc_dev;
140
141 bus_space_tag_t sc_bustag; /* socket control io */
142 bus_space_handle_t sc_regh; /* space */
143
144 pcmcia_chipset_tag_t sc_pct;
145
146 lwp_t *sc_thread; /* event thread */
147 uint32_t sc_events; /* sockets with pending events */
148
149 /* bits 0 and 1 are set according to card presence in slot 0 and 1 */
150 uint32_t sc_active;
151
152 struct tslot_data sc_slot[TS102_NUM_SLOTS];
153 };
154
155 static void tslot_attach(device_t, device_t, void *);
156 static void tslot_event_thread(void *);
157 static int tslot_intr(void *);
158 static void tslot_intr_disestablish(pcmcia_chipset_handle_t, void *);
159 static void *tslot_intr_establish(pcmcia_chipset_handle_t,
160 struct pcmcia_function *, int, int (*)(void *), void *);
161
162 const char *tslot_intr_string(pcmcia_chipset_handle_t, void *);
163 static int tslot_io_alloc(pcmcia_chipset_handle_t, bus_addr_t, bus_size_t,
164 bus_size_t, struct pcmcia_io_handle *);
165 static void tslot_io_free(pcmcia_chipset_handle_t, struct pcmcia_io_handle *);
166 static int tslot_io_map(pcmcia_chipset_handle_t, int, bus_addr_t, bus_size_t,
167 struct pcmcia_io_handle *, int *);
168 static void tslot_io_unmap(pcmcia_chipset_handle_t, int);
169 static int tslot_match(device_t, struct cfdata *, void *);
170 static int tslot_mem_alloc(pcmcia_chipset_handle_t, bus_size_t,
171 struct pcmcia_mem_handle *);
172 static void tslot_mem_free(pcmcia_chipset_handle_t, struct pcmcia_mem_handle *);
173 static int tslot_mem_map(pcmcia_chipset_handle_t, int, bus_addr_t, bus_size_t,
174 struct pcmcia_mem_handle *, bus_size_t *, int *);
175 static void tslot_mem_unmap(pcmcia_chipset_handle_t, int);
176 static int tslot_print(void *, const char *);
177 static void tslot_queue_event(struct tslot_softc *, int);
178 static void tslot_reset(struct tslot_data *, uint32_t);
179 static void tslot_slot_disable(pcmcia_chipset_handle_t);
180 static void tslot_slot_enable(pcmcia_chipset_handle_t);
181 static void tslot_slot_intr(struct tslot_data *, int);
182 static void tslot_slot_settype(pcmcia_chipset_handle_t, int);
183 static void tslot_update_lcd(struct tslot_softc *, int, int);
184 static void tslot_intr_dispatch(void *arg);
185
186 CFATTACH_DECL_NEW(tslot, sizeof(struct tslot_softc),
187 tslot_match, tslot_attach, NULL, NULL);
188
189 extern struct cfdriver tslot_cd;
190
191 /*
192 * PCMCIA chipset methods
193 */
194 struct pcmcia_chip_functions tslot_functions = {
195 tslot_mem_alloc,
196 tslot_mem_free,
197 tslot_mem_map,
198 tslot_mem_unmap,
199
200 tslot_io_alloc,
201 tslot_io_free,
202 tslot_io_map,
203 tslot_io_unmap,
204
205 tslot_intr_establish,
206 tslot_intr_disestablish,
207
208 tslot_slot_enable,
209 tslot_slot_disable,
210 tslot_slot_settype
211 };
212
213 static uint16_t ts102_read_2(bus_space_tag_t,
214 bus_space_handle_t,
215 bus_size_t);
216 static uint32_t ts102_read_4(bus_space_tag_t,
217 bus_space_handle_t,
218 bus_size_t);
219 static uint64_t ts102_read_8(bus_space_tag_t,
220 bus_space_handle_t,
221 bus_size_t);
222 static void ts102_write_2(bus_space_tag_t,
223 bus_space_handle_t,
224 bus_size_t,
225 uint16_t);
226 static void ts102_write_4(bus_space_tag_t,
227 bus_space_handle_t,
228 bus_size_t,
229 uint32_t);
230 static void ts102_write_8(bus_space_tag_t,
231 bus_space_handle_t,
232 bus_size_t,
233 uint64_t);
234
235 static uint16_t
236 ts102_read_2(bus_space_tag_t space, bus_space_handle_t handle,
237 bus_size_t offset)
238 {
239 return (le16toh(*(volatile uint16_t *)(handle +
240 offset)));
241 }
242
243 static uint32_t
244 ts102_read_4(bus_space_tag_t space, bus_space_handle_t handle,
245 bus_size_t offset)
246 {
247 return (le32toh(*(volatile uint32_t *)(handle +
248 offset)));
249 }
250
251 static uint64_t
252 ts102_read_8(bus_space_tag_t space, bus_space_handle_t handle,
253 bus_size_t offset)
254 {
255 return (le64toh(*(volatile uint64_t *)(handle +
256 offset)));
257 }
258
259 static void
260 ts102_write_2(bus_space_tag_t space, bus_space_handle_t handle,
261 bus_size_t offset, uint16_t value)
262 {
263 (*(volatile uint16_t *)(handle + offset)) =
264 htole16(value);
265 }
266
267 static void
268 ts102_write_4(bus_space_tag_t space, bus_space_handle_t handle,
269 bus_size_t offset, uint32_t value)
270 {
271 (*(volatile uint32_t *)(handle + offset)) =
272 htole32(value);
273 }
274
275 static void
276 ts102_write_8(bus_space_tag_t space, bus_space_handle_t handle,
277 bus_size_t offset, uint64_t value)
278 {
279 (*(volatile uint64_t *)(handle + offset)) =
280 htole64(value);
281 }
282
283
284 #define TSLOT_READ(slot, offset) \
285 *(volatile uint16_t *)((slot)->td_regs + (offset))
286 #define TSLOT_WRITE(slot, offset, value) \
287 *(volatile uint16_t *)((slot)->td_regs + (offset)) = (value)
288
289 /*
290 * Attachment and initialization
291 */
292
293 static int
294 tslot_match(device_t parent, struct cfdata *vcf, void *aux)
295 {
296 struct sbus_attach_args *sa = aux;
297
298 return (strcmp("ts102", sa->sa_name) == 0);
299 }
300
301 static void
302 tslot_attach(device_t parent, device_t self, void *args)
303 {
304 struct sbus_attach_args *sa = args;
305 struct tslot_softc *sc = device_private(self);
306 struct tslot_data *td;
307 volatile uint8_t *regs;
308 int node, slot, rnum, base, size;
309 uint32_t ranges[30];
310 void *rptr = ranges;
311 bus_space_handle_t hrang = 0;
312 bus_space_tag_t tag;
313
314 sc->sc_dev = self;
315 node = sa->sa_node;
316 sc->sc_bustag=sa->sa_bustag;
317 if (sbus_bus_map(sa->sa_bustag,
318 sa->sa_slot,
319 sa->sa_offset,
320 sa->sa_size,
321 0, &sc->sc_regh) != 0) {
322 printf("%s: cannot map registers\n", device_xname(self));
323 return;
324 }
325 regs = (uint8_t *)bus_space_vaddr(sa->sa_bustag, sc->sc_regh);
326
327 tag = bus_space_tag_alloc(sa->sa_bustag, sc);
328 if (tag == NULL) {
329 printf("%s: attach: out of memory\n", device_xname(self));
330 return;
331 }
332 tag->sparc_read_2 = ts102_read_2;
333 tag->sparc_read_4 = ts102_read_4;
334 tag->sparc_read_8 = ts102_read_8;
335 tag->sparc_write_2 = ts102_write_2;
336 tag->sparc_write_4 = ts102_write_4;
337 tag->sparc_write_8 = ts102_write_8;
338
339 bus_intr_establish(sa->sa_bustag, sa->sa_intr[0].oi_pri,
340 IPL_NONE, tslot_intr, sc);
341
342 printf(": %d slots\n", TS102_NUM_SLOTS);
343
344 size = sizeof(ranges);
345 if (prom_getprop(node, "ranges", 4, &size, &rptr) != 0) {
346 printf("couldn't read ranges\n");
347 return;
348 }
349
350 /*
351 * Setup asynchronous event handler
352 */
353 sc->sc_events = 0;
354
355 TSPRINTF("starting event thread...\n");
356 if (kthread_create(PRI_NONE, 0, NULL, tslot_event_thread, sc,
357 &sc->sc_thread, "%s", device_xname(self)) != 0) {
358 panic("%s: unable to create event kthread",
359 device_xname(self));
360 }
361
362 sc->sc_pct = (pcmcia_chipset_tag_t)&tslot_functions;
363 sc->sc_active = 0;
364
365 /*
366 * Setup slots
367 */
368 TSPRINTF("mapping resources...\n");
369 for (slot = 0; slot < TS102_NUM_SLOTS; slot++) {
370 td = &sc->sc_slot[slot];
371 TSPRINTF("slot %d, ",slot);
372 for (rnum = 0; rnum < TS102_RANGE_CNT; rnum++) {
373 base = (slot * TS102_RANGE_CNT + rnum) * 5;
374 TSPRINTF("%d: %08x %08x ",rnum,ranges[base + 3],
375 ranges[base + 4]);
376 if(sbus_bus_map(sc->sc_bustag,
377 sa->sa_slot,
378 ranges[base+3],
379 TS102_ARBITRARY_MAP_SIZE,
380 0, &hrang) != 0) {
381 printf("%s: cannot map registers\n",
382 device_xname(self));
383 return;
384 }
385 TSPRINTF("%08x: %08x ",(uint32_t)ranges[base + 3],
386 (uint32_t)hrang);
387 td->td_space[rnum] = hrang;
388 }
389 td->td_parent = sc;
390 td->td_pcmciat = tag;
391 td->td_softint = NULL;
392 td->td_regs = regs + slot * (TS102_REG_CARD_B_INT -
393 TS102_REG_CARD_A_INT);
394 td->td_slot = slot;
395
396 TSPRINTF("resetting slot %d %d\n", slot, (int)td->td_regs);
397 tslot_reset(td, TS102_ARBITRARY_MAP_SIZE);
398 }
399 }
400
401 static void
402 tslot_reset(struct tslot_data *td, uint32_t iosize)
403 {
404 struct pcmciabus_attach_args paa;
405 int ctl, status;
406
407 paa.paa_busname = "pcmcia";
408 paa.pct = (pcmcia_chipset_tag_t)td->td_parent->sc_pct;
409 paa.pch = (pcmcia_chipset_handle_t)td;
410
411 td->td_pcmcia = config_found(td->td_parent->sc_dev, &paa, tslot_print);
412
413 if (td->td_pcmcia == NULL) {
414 /*
415 * If no pcmcia attachment, power down the slot.
416 */
417 tslot_slot_disable((pcmcia_chipset_handle_t)td);
418 return;
419 }
420
421 /*
422 * Initialize the slot
423 */
424
425 ctl = TSLOT_READ(td, TS102_REG_CARD_A_CTL);
426
427 /* force low addresses */
428 ctl &= ~(TS102_CARD_CTL_AA_MASK | TS102_CARD_CTL_IA_MASK);
429
430 /* Put SBus and PCMCIA in their respective endian mode */
431 ctl |= TS102_CARD_CTL_SBLE; /* this is not what it looks like! */
432 ctl &= ~TS102_CARD_CTL_PCMBE; /* default */
433
434 /* disable read ahead and address increment */
435 ctl &= ~TS102_CARD_CTL_RAHD;
436 ctl |= TS102_CARD_CTL_INCDIS;
437
438 /* power on */
439 ctl &= ~TS102_CARD_CTL_PWRD;
440 TSLOT_WRITE(td, TS102_REG_CARD_A_CTL, ctl);
441 TSPRINTF("ctl: %x\n", ctl);
442
443 /*
444 * Enable interrupt upon insertion/removal
445 */
446
447 TSLOT_WRITE(td, TS102_REG_CARD_A_INT,
448 TS102_CARD_INT_MASK_CARDDETECT_STATUS);
449
450 status = TSLOT_READ(td, TS102_REG_CARD_A_STS);
451 if (status & TS102_CARD_STS_PRES) {
452 td->td_status = TS_CARD;
453 pcmcia_card_attach(td->td_pcmcia);
454 } else
455 td->td_status = 0;
456 }
457
458 /* XXX there ought to be a common function for this... */
459 static int
460 tslot_print(void *aux, const char *description)
461 {
462 struct pcmciabus_attach_args *paa = aux;
463 struct tslot_data *td = (struct tslot_data *)paa->pch;
464
465 printf(" socket %d", td->td_slot);
466 return (UNCONF);
467 }
468
469 /*
470 * PCMCIA Helpers
471 */
472
473 static int
474 tslot_io_alloc(pcmcia_chipset_handle_t pch, bus_addr_t start, bus_size_t size,
475 bus_size_t align, struct pcmcia_io_handle *pih)
476 {
477 struct tslot_data *td = (struct tslot_data *)pch;
478
479 #ifdef TSLOT_DEBUG
480 printf("[io alloc %x]", (uint32_t)size);
481 #endif
482
483 pih->iot = td->td_pcmciat;
484 pih->ioh = td->td_space[TS102_RANGE_IO];
485 pih->addr = start;
486 pih->size = size;
487 pih->flags = 0;
488
489 return (0);
490 }
491
492 static void
493 tslot_io_free(pcmcia_chipset_handle_t pch, struct pcmcia_io_handle *pih)
494 {
495 #ifdef TSLOT_DEBUG
496 printf("[io free]");
497 #endif
498 }
499
500 static int
501 tslot_io_map(pcmcia_chipset_handle_t pch, int width, bus_addr_t offset,
502 bus_size_t size, struct pcmcia_io_handle *pih, int *windowp)
503 {
504 struct tslot_data *td = (struct tslot_data *)pch;
505
506 #ifdef TSLOT_DEBUG
507 printf("[io map %x/%x", (uint32_t)offset, (uint32_t)size);
508 #endif
509
510 pih->iot = td->td_pcmciat;
511 if (bus_space_subregion(pih->iot, td->td_space[TS102_RANGE_IO],
512 offset, size, &pih->ioh) != 0)
513 printf("io_map failed, offset %x\n", (uint32_t)offset);
514 *windowp = 0; /* TS102_RANGE_IO */
515
516 #ifdef TSLOT_DEBUG
517 printf("->%x/%x]", (uint32_t)pih->ioh, (uint32_t)size);
518 {
519 int addr, line;
520 for( addr = offset; addr < (offset + size); addr += 16) {
521 printf("%04x:", addr);
522 for(line = addr; line < (addr + 16); line += 2) {
523 printf(" %04x", bus_space_read_2(pih->iot,
524 pih->ioh, line));
525 }
526 printf("\n");
527 }
528 }
529 #endif
530
531 return (0);
532 }
533
534 static void
535 tslot_io_unmap(pcmcia_chipset_handle_t pch, int win)
536 {
537 #ifdef TSLOT_DEBUG
538 struct tslot_data *td = (struct tslot_data *)pch;
539
540 printf("[io unmap]");
541 {
542 int addr, line, offset = 0, size = 0x80;
543 for (addr = offset; addr < (offset + size); addr += 16) {
544 printf("%04x:", addr);
545 for (line = addr; line < (addr + 16); line += 2){
546 printf(" %04x", bus_space_read_2(td->td_pcmciat,
547 td->td_space[2], line));
548 }
549 printf("\n");
550 }
551 }
552 #endif
553 }
554
555 static int
556 tslot_mem_alloc(pcmcia_chipset_handle_t pch, bus_size_t size,
557 struct pcmcia_mem_handle *pmh)
558 {
559 struct tslot_data *td = (struct tslot_data *)pch;
560
561 #ifdef TSLOT_DEBUG
562 printf("[mem alloc %x]", (uint32_t)size);
563 #endif
564 pmh->memt = td->td_pcmciat;
565 pmh->size = size;
566 pmh->addr = 0;
567 pmh->mhandle = 0;
568 pmh->realsize = size; /* nothing so far! */
569
570 return (0);
571 }
572
573 static void
574 tslot_mem_free(pcmcia_chipset_handle_t pch, struct pcmcia_mem_handle *pmh)
575 {
576 #ifdef TSLOT_DEBUG
577 printf("[mem free]");
578 #endif
579 }
580
581 static int
582 tslot_mem_map(pcmcia_chipset_handle_t pch, int kind, bus_addr_t addr,
583 bus_size_t size, struct pcmcia_mem_handle *pmh, bus_size_t *offsetp,
584 int *windowp)
585 {
586 struct tslot_data *td = (struct tslot_data *)pch;
587 int slot;
588
589 slot = kind & PCMCIA_MEM_ATTR ? TS102_RANGE_ATTR : TS102_RANGE_COMMON;
590 #ifdef TSLOT_DEBUG
591 printf("[mem map %d %x/%x", slot, (uint32_t)addr, (uint32_t)size);
592 #endif
593
594 pmh->memt = td->td_parent->sc_bustag;
595 if (bus_space_subregion(pmh->memt, td->td_space[slot],
596 addr, size, &pmh->memh) != 0)
597 printf("mem_map failed, offset %x\n", (uint32_t)addr);
598 pmh->realsize = TS102_ARBITRARY_MAP_SIZE - addr;
599 pmh->size = size;
600 *offsetp = 0;
601 *windowp = 0;
602
603 #ifdef TSLOT_DEBUG
604 printf("->%x/%x]", (uint32_t)pmh->memh, (uint32_t)size);
605 #endif
606
607 return (0);
608 }
609
610 static void
611 tslot_mem_unmap(pcmcia_chipset_handle_t pch, int win)
612 {
613 #ifdef TSLOT_DEBUG
614 printf("[mem unmap %d]", win);
615 #endif
616 }
617
618 static void
619 tslot_slot_disable(pcmcia_chipset_handle_t pch)
620 {
621 struct tslot_data *td = (struct tslot_data *)pch;
622 #ifdef TSLOT_DEBUG
623 printf("%s: disable slot %d\n",
624 device_xname(td->td_parent->sc_dev), td->td_slot);
625 #endif
626
627 /*
628 * Disable card access.
629 */
630 TSLOT_WRITE(td, TS102_REG_CARD_A_STS,
631 TSLOT_READ(td, TS102_REG_CARD_A_STS) & ~TS102_CARD_STS_ACEN);
632
633 /*
634 * Disable interrupts, except for insertion.
635 */
636 TSLOT_WRITE(td, TS102_REG_CARD_A_INT,
637 TS102_CARD_INT_MASK_CARDDETECT_STATUS);
638 }
639
640 static void
641 tslot_slot_enable(pcmcia_chipset_handle_t pch)
642 {
643 struct tslot_data *td = (struct tslot_data *)pch;
644 int status, intr, i;
645
646 #ifdef TSLOT_DEBUG
647 printf("%s: enable slot %d\n",
648 device_xname(td->td_parent->sc_dev), td->td_slot);
649 #endif
650
651 /* Power down the socket to reset it */
652 status = TSLOT_READ(td, TS102_REG_CARD_A_STS);
653 TSPRINTF("status: %x\n", status);
654 TSLOT_WRITE(td, TS102_REG_CARD_A_STS, status | TS102_CARD_STS_VCCEN);
655
656 /*
657 * wait 300ms until power fails (Tpf). Then, wait 100ms since we
658 * are changing Vcc (Toff).
659 */
660 DELAY((300 + 100) * 1000);
661
662 /*
663 * Power on the card if not already done, and enable card access
664 */
665 status |= TS102_CARD_STS_ACEN;
666 status &= ~TS102_CARD_STS_VCCEN;
667 TSLOT_WRITE(td, TS102_REG_CARD_A_STS, status);
668
669 /*
670 * wait 100ms until power raise (Tpr) and 20ms to become
671 * stable (Tsu(Vcc)).
672 */
673 DELAY((100 + 20) * 1000);
674
675 status &= ~TS102_CARD_STS_VPP1_MASK;
676 status |= TS102_CARD_STS_VPP1_VCC;
677 TSLOT_WRITE(td, TS102_REG_CARD_A_STS, status);
678
679 /*
680 * hold RESET at least 20us.
681 */
682 intr = TSLOT_READ(td, TS102_REG_CARD_A_INT);
683 TSLOT_WRITE(td, TS102_REG_CARD_A_INT, TS102_CARD_INT_SOFT_RESET);
684 DELAY(20);
685 TSLOT_WRITE(td, TS102_REG_CARD_A_INT, intr);
686
687 /* wait 20ms as per pc card standard (r2.01) section 4.3.6 */
688 DELAY(20 * 1000);
689
690 /* We need level-triggered interrupts for PC Card hardware */
691 TSLOT_WRITE(td, TS102_REG_CARD_A_STS,
692 TSLOT_READ(td, TS102_REG_CARD_A_STS) | TS102_CARD_STS_LVL);
693
694 /*
695 * Wait until the card is unbusy. If it is still busy after 3 seconds,
696 * give up. We could enable card interrupts and wait for the interrupt
697 * to happen when BUSY is released, but the interrupt could also be
698 * triggered by the card itself if it's an I/O card, so better poll
699 * here.
700 */
701 for (i = 30000; i != 0; i--) {
702 status = TSLOT_READ(td, TS102_REG_CARD_A_STS);
703 /* If the card has been removed, abort */
704 if ((status & TS102_CARD_STS_PRES) == 0) {
705 tslot_slot_disable(pch);
706 return;
707 }
708 if (status & TS102_CARD_STS_RDY)
709 break;
710 else
711 DELAY(100);
712 }
713
714 if (i == 0) {
715 printf("%s: slot %d still busy after 3 seconds, status 0x%x\n",
716 device_xname(td->td_parent->sc_dev), td->td_slot,
717 TSLOT_READ(td, TS102_REG_CARD_A_STS));
718 return;
719 }
720 }
721 static void
722 tslot_event_thread(void *v)
723 {
724 struct tslot_softc *sc = v;
725 struct tslot_data *td;
726 int s, status;
727 unsigned int socket;
728
729 #if NTCTRL > 0
730 int i;
731
732 /*
733 * First-time setup of our LCD symbol. When a card is present at boot
734 * time we won't detect a change here and therefore the LCD symbol won't
735 * light up.
736 */
737 for (i = 0; i < TS102_NUM_SLOTS; i++) {
738 td = &sc->sc_slot[i];
739 status = TSLOT_READ(td, TS102_REG_CARD_A_STS);
740 tslot_update_lcd(sc, i, status & TS102_CARD_STS_PRES);
741 }
742 #endif
743
744 for (;;) {
745 s = splhigh();
746
747 if ((socket = ffs(sc->sc_events)) == 0) {
748 splx(s);
749 tsleep(&sc->sc_events, PWAIT, "tslot_event", hz * 30);
750 continue;
751 }
752 socket--;
753 sc->sc_events &= ~(1 << socket);
754 splx(s);
755
756 if (socket >= TS102_NUM_SLOTS) {
757 #ifdef DEBUG
758 printf("%s: invalid slot number %d\n",
759 device_xname(sc->sc_dev), socket);
760 #endif
761 continue;
762 }
763
764 td = &sc->sc_slot[socket];
765 status = TSLOT_READ(td, TS102_REG_CARD_A_STS);
766
767 if (status & TS102_CARD_STS_PRES) {
768 /* Card insertion */
769 if ((td->td_status & TS_CARD) == 0) {
770 td->td_status |= TS_CARD;
771 tslot_update_lcd(sc, socket, 1);
772 pcmcia_card_attach(td->td_pcmcia);
773 }
774 } else {
775 /* Card removal */
776 if ((td->td_status & TS_CARD) != 0) {
777 tslot_update_lcd(sc, socket, 0);
778 td->td_status &= ~TS_CARD;
779 pcmcia_card_detach(td->td_pcmcia,
780 DETACH_FORCE);
781 }
782 }
783 }
784 }
785
786 /*
787 * Interrupt handling
788 */
789
790 static int
791 tslot_intr(void *v)
792 {
793 struct tslot_softc *sc = v;
794 struct tslot_data *td;
795 int intregs[TS102_NUM_SLOTS], *intreg;
796 int i, s, rc = 0;
797
798 s = splhigh();
799
800 /*
801 * Scan slots, and acknowledge the interrupt if necessary first
802 */
803 for (i = 0; i < TS102_NUM_SLOTS; i++) {
804 td = &sc->sc_slot[i];
805 intreg = &intregs[i];
806 *intreg = TSLOT_READ(td, TS102_REG_CARD_A_INT);
807
808 /*
809 * Acknowledge all interrupt situations at once, even if they
810 * did not occur.
811 */
812 if ((*intreg & (TS102_CARD_INT_STATUS_IRQ |
813 TS102_CARD_INT_STATUS_WP_STATUS_CHANGED |
814 TS102_CARD_INT_STATUS_BATTERY_STATUS_CHANGED |
815 TS102_CARD_INT_STATUS_CARDDETECT_STATUS_CHANGED)) != 0) {
816 rc = 1;
817 TSLOT_WRITE(td, TS102_REG_CARD_A_INT, *intreg |
818 TS102_CARD_INT_RQST_IRQ |
819 TS102_CARD_INT_RQST_WP_STATUS_CHANGED |
820 TS102_CARD_INT_RQST_BATTERY_STATUS_CHANGED |
821 TS102_CARD_INT_RQST_CARDDETECT_STATUS_CHANGED);
822 }
823 }
824
825 #ifdef TSLOT_DEBUG
826 printf("tslot_intr: %x %x\n", intregs[0], intregs[1]);
827 #endif
828
829 /*
830 * Invoke the interrupt handler for each slot
831 */
832 for (i = 0; i < TS102_NUM_SLOTS; i++) {
833 td = &sc->sc_slot[i];
834 intreg = &intregs[i];
835
836 if ((*intreg & (TS102_CARD_INT_STATUS_IRQ |
837 TS102_CARD_INT_STATUS_WP_STATUS_CHANGED |
838 TS102_CARD_INT_STATUS_BATTERY_STATUS_CHANGED |
839 TS102_CARD_INT_STATUS_CARDDETECT_STATUS_CHANGED)) != 0)
840 tslot_slot_intr(td, *intreg);
841 }
842 splx(s);
843
844 return (rc);
845 }
846
847 static void
848 tslot_queue_event(struct tslot_softc *sc, int slot)
849 {
850 int s;
851
852 s = splhigh();
853 sc->sc_events |= (1 << slot);
854 splx(s);
855 wakeup(&sc->sc_events);
856 }
857
858 static void
859 tslot_slot_intr(struct tslot_data *td, int intreg)
860 {
861 struct tslot_softc *sc = td->td_parent;
862 int status, sockstat;
863 uint32_t ireg;
864
865 status = TSLOT_READ(td, TS102_REG_CARD_A_STS);
866 #ifdef TSLOT_DEBUG
867 printf("%s: interrupt on socket %d ir %x sts %x\n",
868 device_xname(sc->sc_dev), td->td_slot, intreg, status);
869 #endif
870
871 sockstat = td->td_status;
872
873 /*
874 * The TS102 queues interrupt request, and may trigger an interrupt
875 * for a condition the driver does not want to receive anymore (for
876 * example, after a card gets removed).
877 * Thus, only proceed if the driver is currently allowing a particular
878 * condition.
879 */
880
881 if ((intreg & TS102_CARD_INT_STATUS_CARDDETECT_STATUS_CHANGED) != 0 &&
882 (intreg & TS102_CARD_INT_MASK_CARDDETECT_STATUS) != 0) {
883 tslot_queue_event(sc, td->td_slot);
884 #ifdef TSLOT_DEBUG
885 printf("%s: slot %d status changed from %d to %d\n",
886 device_xname(sc->sc_dev), td->td_slot, sockstat,
887 td->td_status);
888 #endif
889 /*
890 * Ignore extra interrupt bits, they are part of the change.
891 */
892 return;
893 }
894
895 if ((intreg & TS102_CARD_INT_STATUS_IRQ) != 0 &&
896 (intreg & TS102_CARD_INT_MASK_IRQ) != 0) {
897 /* ignore interrupts if we have a pending state change */
898 if (sc->sc_events & (1 << td->td_slot))
899 {
900 TSPRINTF("ev: %d\n", sc->sc_events);
901 return;
902 }
903 if ((sockstat & TS_CARD) == 0) {
904 printf("%s: spurious interrupt on slot %d isr %x\n",
905 device_xname(sc->sc_dev), td->td_slot, intreg);
906 return;
907 }
908
909 if (td->td_intr != NULL) {
910
911 if (td->td_softint != NULL)
912 sparc_softintr_schedule(td->td_softint);
913 /*
914 * Disable this sbus interrupt, until the soft-int
915 * handler had a chance to run
916 */
917 ireg = TSLOT_READ(td, TS102_REG_CARD_A_INT);
918 TSLOT_WRITE(td, TS102_REG_CARD_A_INT, ireg &
919 ~TS102_CARD_INT_MASK_IRQ);
920 }
921 }
922 }
923
924 static void
925 tslot_intr_disestablish(pcmcia_chipset_handle_t pch, void *ih)
926 {
927 struct tslot_data *td = (struct tslot_data *)pch;
928
929 td->td_intr = NULL;
930 td->td_intrarg = NULL;
931 if (td->td_softint) {
932 sparc_softintr_disestablish(td->td_softint);
933 td->td_softint = NULL;
934 }
935 }
936
937 const char *
938 tslot_intr_string(pcmcia_chipset_handle_t pch, void *ih)
939 {
940 if (ih == NULL)
941 return ("couldn't establish interrupt");
942 else
943 return (""); /* nothing for now */
944 }
945
946 static void *
947 tslot_intr_establish(pcmcia_chipset_handle_t pch, struct pcmcia_function *pf,
948 int ipl, int (*handler)(void *), void *arg)
949 {
950 struct tslot_data *td = (struct tslot_data *)pch;
951
952 td->td_intr = handler;
953 td->td_intrarg = arg;
954 td->td_softint = sparc_softintr_establish(ipl, tslot_intr_dispatch, td);
955
956 return (td);
957 }
958
959 /*
960 * Softinterrupt called to invoke the real driver interrupt handler.
961 */
962 static void
963 tslot_intr_dispatch(void *arg)
964 {
965 struct tslot_data *td = arg;
966 int s;
967 uint32_t ireg;
968
969 /* invoke driver handler */
970 td->td_intr(td->td_intrarg);
971
972 /* enable SBUS interrupts for pcmcia interrupts again */
973 s = splhigh();
974 ireg = TSLOT_READ(td, TS102_REG_CARD_A_INT);
975 TSLOT_WRITE(td, TS102_REG_CARD_A_INT, ireg | TS102_CARD_INT_MASK_IRQ);
976 splx(s);
977 }
978
979 static void
980 tslot_slot_settype(pcmcia_chipset_handle_t pch, int type)
981 {
982 struct tslot_data *td = (struct tslot_data *)pch;
983 uint32_t reg;
984
985 /*
986 * Enable the card interrupts if this is an I/O card.
987 * Note that the TS102_CARD_STS_IO bit in the status register will
988 * never get set, despite what the documentation says!
989 */
990 TSPRINTF("tslot_slot_settype(%d)\n",type);
991 if (type == PCMCIA_IFTYPE_IO) {
992 TSLOT_WRITE(td, TS102_REG_CARD_A_STS,
993 TSLOT_READ(td, TS102_REG_CARD_A_STS) | TS102_CARD_STS_IO);
994 TSLOT_WRITE(td, TS102_REG_CARD_A_INT,
995 TS102_CARD_INT_MASK_CARDDETECT_STATUS |
996 TS102_CARD_INT_MASK_IRQ);
997 reg=TSLOT_READ(td, TS102_REG_CARD_A_STS);
998 TSPRINTF("status: %x\n", reg);
999 }
1000 }
1001
1002 static void
1003 tslot_update_lcd(struct tslot_softc *sc, int socket, int status)
1004 {
1005 #if NTCTRL > 0
1006 int was = (sc->sc_active != 0), is;
1007 int mask = 1 << socket;
1008
1009 if (status > 0) {
1010 sc->sc_active |= mask;
1011 } else {
1012 sc->sc_active &= (mask ^ 3);
1013 }
1014 is = (sc->sc_active != 0);
1015 if (was != is) {
1016 tadpole_set_lcd(is, 0x40);
1017 }
1018 #endif
1019 }
1020