aupcmcia.c revision 1.1 1 /* $NetBSD: aupcmcia.c,v 1.1 2006/02/23 03:49:28 gdamore Exp $ */
2
3 /*-
4 * Copyright (c) 2006 Itronix Inc.
5 * All rights reserved.
6 *
7 * Written by Garrett D'Amore for Itronix Inc.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of Itronix Inc. may not be used to endorse
18 * or promote products derived from this software without specific
19 * prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
25 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28 * ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 /* #include "opt_pci.h" */
35 /* #include "pci.h" */
36
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: aupcmcia.c,v 1.1 2006/02/23 03:49:28 gdamore Exp $");
39
40 #include <sys/types.h>
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/errno.h>
44 #include <sys/kernel.h>
45 #include <sys/kthread.h>
46
47 #include <dev/pcmcia/pcmciareg.h>
48 #include <dev/pcmcia/pcmciavar.h>
49 #include <dev/pcmcia/pcmciachip.h>
50
51 #include <mips/alchemy/include/au_himem_space.h>
52 #include <mips/alchemy/include/aubusvar.h>
53 #include <mips/alchemy/include/aureg.h>
54 #include <mips/alchemy/include/auvar.h>
55
56 #include <mips/alchemy/dev/aupcmciareg.h>
57 #include <mips/alchemy/dev/aupcmciavar.h>
58
59 /*
60 * Borrow PCMCIADEBUG for now. Generally aupcmcia is the only PCMCIA
61 * host on these machines anyway.
62 */
63 #ifdef PCMCIADEBUG
64 int aupcm_debug = 1;
65 #define DPRINTF(arg) if (aupcm_debug) printf arg
66 #else
67 #define DPRINTF(arg)
68 #endif
69
70 /*
71 * And for information about mappings, etc. use this one.
72 */
73 #ifdef AUPCMCIANOISY
74 #define NOISY(arg) printf arg
75 #else
76 #define NOISY(arg)
77 #endif
78
79 /*
80 * Note, we use prefix "aupcm" instead of "aupcmcia", even though our
81 * driver is the latter, mostly because my fingers have trouble typing
82 * the former. "aupcm" should be sufficiently unique to avoid
83 * confusion.
84 */
85
86 static int aupcm_mem_alloc(pcmcia_chipset_handle_t, bus_size_t,
87 struct pcmcia_mem_handle *);
88 static void aupcm_mem_free(pcmcia_chipset_handle_t,
89 struct pcmcia_mem_handle *);
90 static int aupcm_mem_map(pcmcia_chipset_handle_t, int, bus_addr_t,
91 bus_size_t, struct pcmcia_mem_handle *, bus_size_t *, int *);
92 static void aupcm_mem_unmap(pcmcia_chipset_handle_t, int);
93
94 static int aupcm_io_alloc(pcmcia_chipset_handle_t, bus_addr_t, bus_size_t,
95 bus_size_t, struct pcmcia_io_handle *);
96 static void aupcm_io_free(pcmcia_chipset_handle_t, struct pcmcia_io_handle *);
97 static int aupcm_io_map(pcmcia_chipset_handle_t, int, bus_addr_t,
98 bus_size_t, struct pcmcia_io_handle *, int *);
99 static void aupcm_io_unmap(pcmcia_chipset_handle_t, int);
100 static void *aupcm_intr_establish(pcmcia_chipset_handle_t,
101 struct pcmcia_function *, int, int (*)(void *), void *);
102 static void aupcm_intr_disestablish(pcmcia_chipset_handle_t, void *);
103
104 static void aupcm_slot_enable(pcmcia_chipset_handle_t);
105 static void aupcm_slot_disable(pcmcia_chipset_handle_t);
106 static void aupcm_slot_settype(pcmcia_chipset_handle_t, int);
107
108 static int aupcm_match(struct device *, struct cfdata *, void *);
109 static void aupcm_attach(struct device *, struct device *, void *);
110
111 static void aupcm_create_thread(void *);
112 static void aupcm_event_thread(void *);
113 static int aupcm_card_intr(void *);
114 static void aupcm_softintr(void *);
115 static int aupcm_print(void *, const char *);
116
117 struct aupcm_slot {
118 struct aupcm_softc *as_softc;
119 int as_slot;
120 int as_status;
121 int as_enabled;
122 int (*as_intr)(void *);
123 int as_card_irq;
124 int as_status_irq;
125 void *as_intrarg;
126 void *as_softint;
127 void *as_hardint;
128 const char *as_name;
129 bus_addr_t as_offset;
130 struct mips_bus_space as_iot;
131 struct mips_bus_space as_attrt;
132 struct mips_bus_space as_memt;
133 void *as_wins[AUPCMCIA_NWINS];
134
135 struct device *as_pcmcia;
136 };
137
138 /* this structure needs to be exposed... */
139 struct aupcm_softc {
140 struct device sc_dev;
141 pcmcia_chipset_tag_t sc_pct;
142
143 void (*sc_slot_enable)(int);
144 void (*sc_slot_disable)(int);
145 int (*sc_slot_status)(int);
146
147 paddr_t sc_base;
148
149 int sc_wake;
150 struct proc *sc_thread;
151
152 int sc_nslots;
153 struct aupcm_slot sc_slots[AUPCMCIA_NSLOTS];
154 };
155
156 static struct pcmcia_chip_functions aupcm_functions = {
157 aupcm_mem_alloc,
158 aupcm_mem_free,
159 aupcm_mem_map,
160 aupcm_mem_unmap,
161
162 aupcm_io_alloc,
163 aupcm_io_free,
164 aupcm_io_map,
165 aupcm_io_unmap,
166
167 aupcm_intr_establish,
168 aupcm_intr_disestablish,
169
170 aupcm_slot_enable,
171 aupcm_slot_disable,
172 aupcm_slot_settype,
173 };
174
175 static struct mips_bus_space aupcm_memt;
176
177 CFATTACH_DECL(aupcmcia, sizeof (struct aupcm_softc),
178 aupcm_match, aupcm_attach, NULL, NULL);
179
180 int
181 aupcm_match(struct device *parent, struct cfdata *cf, void *aux)
182 {
183 struct aubus_attach_args *aa = aux;
184 static int found = 0;
185
186 if (found)
187 return 0;
188
189 if (strcmp(aa->aa_name, "aupcmcia") != 0)
190 return 0;
191
192 found = 1;
193
194 return 1;
195 }
196
197 void
198 aupcm_attach(struct device *parent, struct device *self, void *aux)
199 {
200 /* struct aubus_attach_args *aa = aux; */
201 struct aupcm_softc *sc = (struct aupcm_softc *)self;
202 static int done = 0;
203 int slot;
204 struct aupcmcia_machdep *md;
205
206 /* initialize bus space */
207 if (done) {
208 /* there can be only one. */
209 return;
210 }
211
212 done = 1;
213 /*
214 * PCMCIA memory can live within pretty much the entire 32-bit
215 * space, modulo 64 MB wraps. We don't have to support coexisting
216 * DMA.
217 */
218 au_himem_space_init(&aupcm_memt, "pcmciamem",
219 PCMCIA_BASE, AUPCMCIA_ATTR_OFFSET, 0xffffffff,
220 AU_HIMEM_SPACE_LITTLE_ENDIAN);
221
222 if ((md = aupcmcia_machdep()) == NULL) {
223 printf("\n%s:unable to get machdep structure\n",
224 sc->sc_dev.dv_xname);
225 return;
226 }
227
228 sc->sc_nslots = md->am_nslots;
229 sc->sc_slot_enable = md->am_slot_enable;
230 sc->sc_slot_disable = md->am_slot_disable;
231 sc->sc_slot_status = md->am_slot_status;
232
233 printf(": Alchemy PCMCIA, %d slots\n", sc->sc_nslots);
234
235 sc->sc_pct = (pcmcia_chipset_tag_t)&aupcm_functions;
236
237 for (slot = 0; slot < sc->sc_nslots; slot++) {
238 struct aupcm_slot *sp;
239 struct pcmciabus_attach_args paa;
240
241 sp = &sc->sc_slots[slot];
242 sp->as_softc = sc;
243
244 sp->as_slot = slot;
245 sp->as_name = md->am_slot_name(slot);
246 sp->as_offset = md->am_slot_offset(slot);
247 sp->as_card_irq = md->am_slot_irq(slot, AUPCMCIA_IRQ_CARD);
248 sp->as_status_irq = md->am_slot_irq(slot,
249 AUPCMCIA_IRQ_INSERT);
250
251 au_himem_space_init(&sp->as_attrt, "pcmciaattr",
252 PCMCIA_BASE + sp->as_offset + AUPCMCIA_ATTR_OFFSET,
253 0, AUPCMCIA_MAP_SIZE, AU_HIMEM_SPACE_LITTLE_ENDIAN);
254
255 au_himem_space_init(&sp->as_memt, "pcmciamem",
256 PCMCIA_BASE + sp->as_offset + AUPCMCIA_MEM_OFFSET,
257 0, AUPCMCIA_MAP_SIZE, AU_HIMEM_SPACE_LITTLE_ENDIAN);
258
259 au_himem_space_init(&sp->as_iot, "pcmciaio",
260 PCMCIA_BASE + sp->as_offset + AUPCMCIA_IO_OFFSET,
261 0, AUPCMCIA_MAP_SIZE,
262 AU_HIMEM_SPACE_LITTLE_ENDIAN | AU_HIMEM_SPACE_IO);
263
264 sp->as_status = 0;
265
266 paa.paa_busname = "pcmcia";
267 paa.pct = sc->sc_pct;
268 paa.pch = (pcmcia_chipset_handle_t)sp;
269
270 paa.iobase = 0;
271 paa.iosize = AUPCMCIA_MAP_SIZE;
272
273 sp->as_pcmcia = config_found(&sc->sc_dev, &paa, aupcm_print);
274
275 /* if no pcmcia, make sure slot is powered down */
276 if (sp->as_pcmcia == NULL) {
277 aupcm_slot_disable(sp);
278 continue;
279 }
280
281 /* this makes sure we probe the slot */
282 sc->sc_wake |= (1 << slot);
283 }
284
285 /*
286 * XXX: this would be an excellent time time to establish a handler
287 * for the card insertion interrupt, but that's edge triggered, and
288 * au_icu.c won't support it right now. We poll in the event thread
289 * for now. Start by initializing it now.
290 */
291 kthread_create(aupcm_create_thread, sc);
292 }
293
294 int
295 aupcm_print(void *aux, const char *pnp)
296 {
297 struct pcmciabus_attach_args *paa = aux;
298 struct aupcm_slot *sp = paa->pch;
299
300 printf(" socket %d irq %d, %s", sp->as_slot, sp->as_card_irq,
301 sp->as_name);
302
303 return (UNCONF);
304 }
305
306 void *
307 aupcm_intr_establish(pcmcia_chipset_handle_t pch,
308 struct pcmcia_function *pf, int level, int (*handler)(void *), void *arg)
309 {
310 struct aupcm_slot *sp = (struct aupcm_slot *)pch;
311 int s;
312
313 /*
314 * Hmm. perhaps this intr should be a list. well, PCMCIA
315 * devices generally only have one interrupt, and so should
316 * generally have only one handler. So we leave it for now.
317 * (Other PCMCIA bus drivers do it this way.)
318 */
319 sp->as_intr = handler;
320 sp->as_intrarg = arg;
321
322 /*
323 * XXX: pil must be a software interrupt level. That
324 * automatically implies that it is lower than any other
325 * hardware interrupts. So trying to figure out which level
326 * (IPL_SOFTNET, IPL_SOFTSERIAL, etc.) doesn't really do
327 * anything for us.
328 */
329 sp->as_softint = softintr_establish(IPL_SOFT, aupcm_softintr, sp);
330
331 printf("slot %d interrupting on irq %d\n",
332 sp->as_slot, (int)sp->as_card_irq);
333
334 /* set up hard interrupt handler for the card IRQs */
335 s = splhigh();
336 sp->as_hardint = au_intr_establish(sp->as_card_irq, 0,
337 IPL_NONE, IST_LEVEL_LOW, aupcm_card_intr, sp);
338 /* if card is not powered up, then leave the IRQ masked */
339 if (!sp->as_enabled) {
340 au_intr_disable(sp->as_card_irq);
341 }
342 splx(s);
343
344 return (sp->as_softint);
345 }
346
347 void
348 aupcm_intr_disestablish(pcmcia_chipset_handle_t pch, void *ih)
349 {
350 struct aupcm_slot *sp = (struct aupcm_slot *)pch;
351
352 KASSERT(sp->as_softint == ih);
353 /* KASSERT(sp->as_hardint); */
354 /* set up hard interrupt handler for the card IRQs */
355
356 au_intr_disestablish(sp->as_hardint);
357 sp->as_hardint = 0;
358
359 softintr_disestablish(ih);
360 sp->as_softint = 0;
361 sp->as_intr = NULL;
362 sp->as_intrarg = NULL;
363 }
364
365 void
366 aupcm_create_thread(void *arg)
367 {
368 struct aupcm_softc *sc = arg;
369 const char *name = sc->sc_dev.dv_xname;
370
371 if (kthread_create1(aupcm_event_thread, sc,
372 &sc->sc_thread, "%s", name) != 0)
373 panic("%s: unable to create event kthread", name);
374 }
375
376 /*
377 * FYI: Hot detach of PCMCIA is supposedly safe because H/W doesn't
378 * fault on accesses to missing hardware.
379 */
380 void
381 aupcm_event_thread(void *arg)
382 {
383 struct aupcm_softc *sc = arg;
384 struct aupcm_slot *sp;
385 int s, i, attach, detach;
386
387 for (;;) {
388 s = splhigh();
389 if (sc->sc_wake == 0) {
390 splx(s);
391 /*
392 * XXX: Currently, the au_icu.c lacks support
393 * for edge-triggered interrupts. So we
394 * cannot really use the status change
395 * inerrupts. For now we poll (once per sec).
396 * FYI, Linux does it this way, and they *do*
397 * have support for edge triggered interrupts.
398 * Go figure.
399 */
400 tsleep(&sc->sc_wake, PWAIT, "aupcm_event", hz);
401 }
402 sc->sc_wake = 0;
403
404 attach = detach = 0;
405 for (i = 0; i < sc->sc_nslots; i++) {
406 sp = &sc->sc_slots[i];
407
408 if (sc->sc_slot_status(sp->as_slot) != 0) {
409 if (!sp->as_status) {
410 DPRINTF(("%s: card %d insertion\n",
411 sc->sc_dev.dv_xname, i));
412 attach |= (1 << i);
413 sp->as_status = 1;
414 }
415 } else {
416 if (sp->as_status) {
417 DPRINTF(("%s: card %d removal\n",
418 sc->sc_dev.dv_xname, i));
419 detach |= (1 << i);
420 sp->as_status = 0;
421 }
422 }
423 }
424 splx(s);
425
426 for (i = 0; i < sc->sc_nslots; i++) {
427 sp = &sc->sc_slots[i];
428
429 if (detach & (1 << i)) {
430 aupcm_slot_disable(sp);
431 pcmcia_card_detach(sp->as_pcmcia,
432 DETACH_FORCE);
433 } else if (attach & (1 << i)) {
434 /*
435 * until the function is enabled, don't
436 * honor interrupts
437 */
438 sp->as_enabled = 0;
439 au_intr_disable(sp->as_card_irq);
440 pcmcia_card_attach(sp->as_pcmcia);
441 }
442 }
443 }
444 }
445
446 #if 0
447 void
448 aupcm_status_intr(void *arg)
449 {
450 int s;
451 struct aupcm_softc *sc = arg;
452
453 s = splhigh();
454
455 /* kick the status thread so it does its bit */
456 sc->sc_wake = 1;
457 wakeup(&sc->sc_wake);
458
459 splx(s);
460 }
461 #endif
462
463 int
464 aupcm_card_intr(void *arg)
465 {
466 struct aupcm_slot *sp = arg;
467
468 /* disable the hard interrupt for now */
469 au_intr_disable(sp->as_card_irq);
470
471 if (sp->as_intr != NULL) {
472 softintr_schedule(sp->as_softint);
473 }
474
475 return 1;
476 }
477
478 void
479 aupcm_softintr(void *arg)
480 {
481 struct aupcm_slot *sp = arg;
482 int s;
483
484 sp->as_intr(sp->as_intrarg);
485
486 s = splhigh();
487
488 if (sp->as_intr && sp->as_enabled) {
489 au_intr_enable(sp->as_card_irq);
490 }
491
492 splx(s);
493 }
494
495 void
496 aupcm_slot_enable(pcmcia_chipset_handle_t pch)
497 {
498 struct aupcm_slot *sp = (struct aupcm_slot *)pch;
499 int s;
500
501 /* no interrupts while we reset the card, please */
502 if (sp->as_intr)
503 au_intr_disable(sp->as_card_irq);
504
505 /*
506 * XXX: should probably lock to make sure slot_disable and
507 * enable not called together. However, i believe that the
508 * event thread basically serializes them anyway.
509 */
510
511 sp->as_softc->sc_slot_enable(sp->as_slot);
512 /* card is powered up now, honor device interrupts */
513
514 s = splhigh();
515 sp->as_enabled = 1;
516 if (sp->as_intr)
517 au_intr_enable(sp->as_card_irq);
518 splx(s);
519 }
520
521 void
522 aupcm_slot_disable(pcmcia_chipset_handle_t pch)
523 {
524 struct aupcm_slot *sp = (struct aupcm_slot *)pch;
525 int s;
526
527 s = splhigh();
528 au_intr_disable(sp->as_card_irq);
529 sp->as_enabled = 0;
530 splx(s);
531
532 sp->as_softc->sc_slot_disable(sp->as_slot);
533 }
534
535 void
536 aupcm_slot_settype(pcmcia_chipset_handle_t pch, int type)
537 {
538 /* we do nothing now : type == PCMCIA_IFTYPE_IO */
539 }
540
541 int
542 aupcm_mem_alloc(pcmcia_chipset_handle_t pch, bus_size_t size,
543 struct pcmcia_mem_handle *pcmh)
544 {
545 pcmh->memt = NULL;
546 pcmh->size = pcmh->realsize = size;
547 pcmh->addr = 0;
548 pcmh->mhandle = 0;
549
550 return 0;
551 }
552
553 void
554 aupcm_mem_free(pcmcia_chipset_handle_t pch, struct pcmcia_mem_handle *pcmh)
555 {
556 /* nothing to do */
557 }
558
559 int
560 aupcm_mem_map(pcmcia_chipset_handle_t pch, int kind, bus_addr_t addr,
561 bus_size_t size, struct pcmcia_mem_handle *pcmh, bus_size_t *offsetp,
562 int *windowp)
563 {
564 struct aupcm_slot *sp = (struct aupcm_slot *)pch;
565 int win, err;
566 int s;
567
568 s = splhigh();
569 for (win = 0; win < AUPCMCIA_NWINS; win++) {
570 if (sp->as_wins[win] == NULL) {
571 sp->as_wins[win] = pcmh;
572 break;
573 }
574 }
575 splx(s);
576
577 if (win >= AUPCMCIA_NWINS) {
578 return ENOMEM;
579 }
580
581 if (kind & PCMCIA_MEM_ATTR) {
582 pcmh->memt = &sp->as_attrt;
583 NOISY(("mapping ATTR addr %x size %x\n", (uint32_t)addr,
584 (uint32_t)size));
585 } else {
586 pcmh->memt = &sp->as_memt;
587 NOISY(("mapping MEMORY addr %x size %x\n", (uint32_t)addr,
588 (uint32_t)size));
589 }
590
591 if ((size + addr) > (64 * 1024 * 1024))
592 return EINVAL;
593
594 pcmh->size = size;
595
596 err = bus_space_map(pcmh->memt, addr, size, 0, &pcmh->memh);
597 if (err != 0) {
598 sp->as_wins[win] = NULL;
599 return err;
600 }
601 *offsetp = 0;
602 *windowp = win;
603
604 return 0;
605 }
606
607 void
608 aupcm_mem_unmap(pcmcia_chipset_handle_t pch, int win)
609 {
610 struct aupcm_slot *sp = (struct aupcm_slot *)pch;
611 struct pcmcia_mem_handle *pcmh;
612
613 pcmh = (struct pcmcia_mem_handle *)sp->as_wins[win];
614 sp->as_wins[win] = NULL;
615
616 NOISY(("memory umap virtual %x\n", (uint32_t)pcmh->memh));
617 bus_space_unmap(pcmh->memt, pcmh->memh, pcmh->size);
618 pcmh->memt = NULL;
619 }
620
621 int
622 aupcm_io_alloc(pcmcia_chipset_handle_t pch, bus_addr_t start,
623 bus_size_t size, bus_size_t align, struct pcmcia_io_handle *pih)
624 {
625 struct aupcm_slot *sp = (struct aupcm_slot *)pch;
626 bus_space_handle_t bush;
627 int err;
628
629 pih->iot = &sp->as_iot;
630 pih->size = size;
631 pih->flags = 0;
632
633 /*
634 * start from the initial offset - this gets us a slot
635 * specific address, while still leaving the addresses more or
636 * less zero-based which is required for x86-style device
637 * drivers.
638 */
639 err = bus_space_alloc(pih->iot, start, 0x100000,
640 size, align, 0, 0, &pih->addr, &bush);
641 NOISY(("start = %x, addr = %x, size = %x, bush = %x\n",
642 (uint32_t)start, (uint32_t)pih->addr, (uint32_t)size,
643 (uint32_t)bush));
644
645 /* and we convert it back */
646 if (err == 0) {
647 pih->ihandle = (void *)bush;
648 }
649
650 return (err);
651 }
652
653 void
654 aupcm_io_free(pcmcia_chipset_handle_t pch, struct pcmcia_io_handle *pih)
655 {
656 bus_space_free(pih->iot, (bus_space_handle_t)pih->ihandle,
657 pih->size);
658 }
659
660 int
661 aupcm_io_map(pcmcia_chipset_handle_t pch, int width, bus_addr_t offset,
662 bus_size_t size, struct pcmcia_io_handle *pih, int *windowp)
663 {
664 int err;
665
666 err = bus_space_subregion(pih->iot, (bus_space_handle_t)pih->ihandle,
667 offset, size, &pih->ioh);
668 NOISY(("io map offset = %x, size = %x, ih = %x, hdl=%x\n",
669 (uint32_t)offset, (uint32_t)size,
670 (uint32_t)pih->ihandle, (uint32_t)pih->ioh));
671
672 return err;
673 }
674
675 void
676 aupcm_io_unmap(pcmcia_chipset_handle_t pch, int win)
677 {
678 /* We mustn't unmap/free subregion bus space! */
679 NOISY(("io unmap\n"));
680 }
681