2 * NEC MECIA controller.
3 *-------------------------------------------------------------------------
5 * Copyright (c) 2001 M. Warner Losh. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 * $FreeBSD: src/sys/pccard/mecia.c,v 1.2.2.4 2001/08/14 23:36:18 imp Exp $
28 * $DragonFly: src/sys/bus/pccard/Attic/mecia.c,v 1.3 2003/08/07 21:16:46 dillon Exp $
30 * Based heavily on the FreeBSD pcic driver's pcic98 support, derived
31 * from PAO3 tree. This copyright notice likely needs modification for
32 * such a linage. The only authorship I could find was:
34 * PC9801 original PCMCIA controller code for NS/A,Ne,NX/C,NR/L.
35 * by Noriyuki Hosobuchi <hoso@ce.mbn.or.jp>
38 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/module.h>
48 #define MECIA_IOBASE 0x80d0
52 #include <bus/isa/isavar.h>
54 #include <dev/misc/pccard/pccardvar.h>
57 #define MECIA_DEVICE2SOFTC(dev) ((struct mecia_slot *) device_get_softc(dev))
60 * Prototypes for interrupt handler.
62 static driver_intr_t meciaintr;
63 static int mecia_ioctl(struct slot *, int, caddr_t);
64 static int mecia_power(struct slot *);
65 static void mecia_mapirq(struct slot *, int);
66 static timeout_t mecia_reset;
67 static void mecia_resume(struct slot *);
68 static void mecia_disable(struct slot *);
69 static timeout_t meciatimeout;
70 static struct callout_handle meciatimeout_ch
71 = CALLOUT_HANDLE_INITIALIZER(&meciatimeout_ch);
72 static int mecia_memory(struct slot *, int);
73 static int mecia_io(struct slot *, int);
76 * Per-slot data table.
79 int unit; /* Unit number */
80 int slotnum; /* My slot number */
81 struct slot *slt; /* Back ptr to slot */
82 device_t dev; /* My device */
83 u_char last_reg1; /* Last value of change reg */
86 static struct slot_ctrl mecia_cinfo = {
99 2 /* Fake for UE2212 LAN card */
103 static int validunits = 0;
106 * Look for an NEC MECIA.
107 * For each available slot, allocate a PC-CARD slot.
111 mecia_probe(device_t dev)
115 /* Check isapnp ids */
116 if (isa_get_logicalid(dev)) /* skip PnP probes */
119 if (inb(MECIA_REG0) != 0xff) {
121 /* XXX need to allocated the port resources */
122 device_set_desc(dev, "MECIA PC98 Original PCMCIA Controller");
124 return (validslots ? 0 : ENXIO);
128 mecia_attach(device_t dev)
137 struct mecia_slot *sp;
139 sp = MECIA_DEVICE2SOFTC(dev);
140 sp->unit = validunits++;
141 kid = device_add_child(dev, NULL, -1);
143 device_printf(dev, "Can't add pccard bus slot 0\n");
146 device_probe_and_attach(kid);
147 slt = pccard_init_slot(kid, &mecia_cinfo);
149 device_printf(dev, "Can't get pccard info slot 0\n");
157 r = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE);
161 irq = bus_get_resource_start(dev, SYS_RES_IRQ, 0);
163 /* See if the user has requested a specific IRQ */
164 if (!getenv_int("machdep.pccard.mecia_irq", &irq))
170 r = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, irq,
173 if (r && ((1 << (rman_get_start(r))) & MECIA_INT_MASK_ALLOWED) == 0) {
175 "Hardware does not support irq %d, trying polling.\n",
177 bus_release_resource(dev, SYS_RES_IRQ, rid, r);
182 error = bus_setup_intr(dev, r, INTR_TYPE_MISC,
183 meciaintr, (void *) sp, &ih);
185 bus_release_resource(dev, SYS_RES_IRQ, rid, r);
188 irq = rman_get_start(r);
189 device_printf(dev, "management irq %d\n", irq);
194 meciatimeout_ch = timeout(meciatimeout, (void *) sp, hz/2);
195 device_printf(dev, "Polling mode\n");
198 sp->last_reg1 = inb(MECIA_REG1);
199 if (sp->last_reg1 & MECIA_CARDEXIST) {
200 /* PCMCIA card exist */
201 sp->slt->laststate = sp->slt->state = filled;
202 pccard_event(sp->slt, card_inserted);
204 sp->slt->laststate = sp->slt->state = empty;
208 return (bus_generic_attach(dev));
212 mecia_sresource(struct slot *slt, caddr_t data)
214 struct pccard_resource *pr;
218 device_t pccarddev = slt->dev;
220 pr = (struct pccard_resource *)data;
221 pr->resource_addr = ~0ul;
230 flags = rman_make_alignment_flags(pr->size);
231 r = bus_alloc_resource(pccarddev, pr->type, &rid, pr->min, pr->max,
234 pr->resource_addr = (u_long)rman_get_start(r);
235 bus_release_resource(bridgedev, pr->type, rid, r);
241 * ioctl calls - Controller specific ioctls
244 mecia_ioctl(struct slot *slt, int cmd, caddr_t data)
249 case PIOCSRESOURCE: /* Can I use this resource? */
250 mecia_sresource(slt, data);
257 * MECIA timer. If the controller doesn't have a free IRQ to use
258 * or if interrupt steering doesn't work, poll the controller for
259 * insertion/removal events.
262 meciatimeout(void *chan)
265 meciatimeout_ch = timeout(meciatimeout, chan, hz/2);
269 * MECIA Interrupt handler.
270 * Check the slot and report any changes.
277 struct mecia_slot *sp = (struct mecia_slot *) arg;
280 /* Check for a card in this slot */
281 reg1 = inb(MECIA_REG1);
282 if ((sp->last_reg1 ^ reg1) & MECIA_CARDEXIST) {
283 sp->last_reg1 = reg1;
284 if (reg1 & MECIA_CARDEXIST)
285 pccard_event(sp->slt, card_inserted);
287 pccard_event(sp->slt, card_removed);
293 * local functions for PC-98 Original PC-Card controller
295 #define MECIA_ALWAYS_128MAPPING 1 /* trick for using UE2212 */
297 int mecia_mode = 0; /* almost the same as the value in MECIA_REG2 */
299 static unsigned char reg_winsel = MECIA_UNMAPWIN;
300 static unsigned short reg_pagofs = 0;
303 mecia_memory(struct slot *slt, int win)
305 struct mem_desc *mp = &slt->mem[win];
308 if (mp->flags & MDF_ACTIVE) {
309 /* slot = 0, window = 0, sys_addr = 0xda000, length = 8KB */
310 if ((unsigned long)mp->start != 0xda000) {
312 "sys_addr must be 0xda000. requested address = %p\n",
323 reg_winsel = inb(MECIA_REG_WINSEL);
324 reg_pagofs = inw(MECIA_REG_PAGOFS);
325 outb(MECIA_REG_WINSEL, MECIA_MAPWIN);
326 outw(MECIA_REG_PAGOFS, (mp->card >> 13)); /* 8KB */
328 if (mp->flags & MDF_ATTR)
329 outb(MECIA_REG7, inb(MECIA_REG7) | MECIA_ATTRMEM);
331 outb(MECIA_REG7, inb(MECIA_REG7) & (~MECIA_ATTRMEM));
333 outb(MECIA_REG_WINSEL, MECIA_MAPWIN);
335 if ((mp->flags & MDF_16BITS) == 1) /* 16bit */
336 outb(MECIA_REG2, inb(MECIA_REG2) & (~MECIA_8BIT));
338 outb(MECIA_REG2, inb(MECIA_REG2) | MECIA_8BIT);
340 } else { /* !(mp->flags & MDF_ACTIVE) */
347 outb(MECIA_REG_WINSEL, MECIA_UNMAPWIN);
348 outw(MECIA_REG_PAGOFS, 0);
350 outb(MECIA_REG_WINSEL, reg_winsel);
351 outw(MECIA_REG_PAGOFS, reg_pagofs);
358 mecia_io(struct slot *slt, int win)
360 struct io_desc *ip = &slt->io[win];
362 unsigned short cardbase;
366 /* ignore for UE2212 */
368 "mecia:Illegal MECIA I/O window(%d) request! Ignored.\n", win);
369 /* return (EINVAL);*/
373 if (ip->flags & IODF_ACTIVE) {
374 x = inb(MECIA_REG2) & 0x0f;
376 if (! (ip->flags & IODF_CS16))
379 if (! (ip->flags & IODF_16BIT)) {
381 mecia_mode |= MECIA_8BIT;
385 ofst = ip->start & 0xf;
386 cardbase = ip->start & ~0xf;
387 #ifndef MECIA_ALWAYS_128MAPPING
388 if (ip->size + ofst > 16)
390 { /* 128bytes mapping */
392 mecia_mode |= MECIA_MAP128;
393 ofst |= ((cardbase & 0x70) << 4);
400 outw(MECIA_REG4, MECIA_IOBASE); /* 98side I/O base */
401 outw(MECIA_REG5, cardbase); /* card side I/O base */
404 printf("mecia: I/O mapped 0x%04x(98) -> "
405 "0x%04x(Card) and width %d bytes\n",
406 MECIA_IOBASE+ofst, ip->start, ip->size);
407 printf("mecia: reg2=0x%02x reg3=0x%02x reg7=0x%02x\n",
408 inb(MECIA_REG2), inb(MECIA_REG3),
410 printf("mecia: mode=%d\n", mecia_mode);
413 ip->start = MECIA_IOBASE + ofst;
415 outb(MECIA_REG2, inb(MECIA_REG2) & (~MECIA_MAPIO));
422 mecia_power(struct slot *slt)
426 reg = inb(MECIA_REG7) & (~MECIA_VPP12V);
427 switch(slt->pwr.vpp) {
436 outb(MECIA_REG7, reg);
439 reg = inb(MECIA_REG2) & (~MECIA_VCC3P3V);
440 switch(slt->pwr.vcc) {
444 reg |= MECIA_VCC3P3V;
449 outb(MECIA_REG2, reg);
455 mecia_mapirq(struct slot *slt, int irq)
475 case 0: /* disable */
476 x = MECIA_INTDISABLE;
479 printf("mecia: illegal irq %d\n", irq);
483 printf("mecia: irq=%d mapped.\n", irq);
489 mecia_reset(void *chan)
491 struct slot *slt = chan;
494 outb(MECIA_REG2, inb(MECIA_REG2) & (~MECIA_MAPIO));
495 outb(MECIA_REG3, MECIA_INTDISABLE);
497 /* mecia_reset() is called after mecia_power() */
498 outb(MECIA_REG2, inb(MECIA_REG2) & (~MECIA_VCC3P3V));
499 outb(MECIA_REG7, inb(MECIA_REG7) & (~MECIA_VPP12V));
503 selwakeup(&slt->selp);
507 mecia_disable(struct slot *slt)
513 mecia_resume(struct slot *slt)
515 /* XXX MECIA How ? */
519 mecia_activate_resource(device_t dev, device_t child, int type, int rid,
522 struct pccard_devinfo *devi = device_get_ivars(child);
525 if (dev != device_get_parent(device_get_parent(child)) || devi == NULL)
526 return (bus_generic_activate_resource(dev, child, type,
530 case SYS_RES_IOPORT: {
532 ip = &devi->slt->io[rid];
533 if (ip->flags == 0) {
535 ip->flags = IODF_WS | IODF_16BIT | IODF_CS16;
537 ip->flags = devi->slt->io[0].flags;
539 ip->flags |= IODF_ACTIVE;
540 ip->start = rman_get_start(r);
541 ip->size = rman_get_end(r) - rman_get_start(r) + 1;
542 err = mecia_cinfo.mapio(devi->slt, rid);
549 * We actually defer the activation of the IRQ resource
550 * until the interrupt is registered to avoid stray
551 * interrupt messages.
554 case SYS_RES_MEMORY: {
556 if (rid >= NUM_MEM_WINDOWS)
558 mp = &devi->slt->mem[rid];
559 mp->flags |= MDF_ACTIVE;
560 mp->start = (caddr_t) rman_get_start(r);
561 mp->size = rman_get_end(r) - rman_get_start(r) + 1;
562 err = mecia_cinfo.mapmem(devi->slt, rid);
570 err = bus_generic_activate_resource(dev, child, type, rid, r);
575 mecia_deactivate_resource(device_t dev, device_t child, int type, int rid,
578 struct pccard_devinfo *devi = device_get_ivars(child);
581 if (dev != device_get_parent(device_get_parent(child)) || devi == NULL)
582 return (bus_generic_deactivate_resource(dev, child, type,
586 case SYS_RES_IOPORT: {
587 struct io_desc *ip = &devi->slt->io[rid];
588 ip->flags &= ~IODF_ACTIVE;
589 err = mecia_cinfo.mapio(devi->slt, rid);
596 case SYS_RES_MEMORY: {
597 struct mem_desc *mp = &devi->slt->mem[rid];
598 mp->flags &= ~(MDF_ACTIVE | MDF_ATTR);
599 err = mecia_cinfo.mapmem(devi->slt, rid);
607 err = bus_generic_deactivate_resource(dev, child, type, rid, r);
612 mecia_setup_intr(device_t dev, device_t child, struct resource *irq,
613 int flags, driver_intr_t *intr, void *arg, void **cookiep)
615 struct pccard_devinfo *devi = device_get_ivars(child);
618 if (((1 << rman_get_start(irq)) & MECIA_INT_MASK_ALLOWED) == 0) {
619 device_printf(dev, "Hardware does not support irq %ld.\n",
620 rman_get_start(irq));
624 err = bus_generic_setup_intr(dev, child, irq, flags, intr, arg,
627 mecia_cinfo.mapirq(devi->slt, rman_get_start(irq));
629 device_printf(dev, "Error %d irq %ld\n", err,
630 rman_get_start(irq));
635 mecia_teardown_intr(device_t dev, device_t child, struct resource *irq,
638 struct pccard_devinfo *devi = device_get_ivars(child);
640 mecia_cinfo.mapirq(devi->slt, 0);
641 return (bus_generic_teardown_intr(dev, child, irq, cookie));
645 mecia_set_res_flags(device_t bus, device_t child, int restype, int rid,
648 struct pccard_devinfo *devi = device_get_ivars(child);
652 case SYS_RES_MEMORY: {
653 struct mem_desc *mp = &devi->slt->mem[rid];
655 case PCCARD_A_MEM_COM:
656 mp->flags &= ~MDF_ATTR;
658 case PCCARD_A_MEM_ATTR:
659 mp->flags |= MDF_ATTR;
661 case PCCARD_A_MEM_8BIT:
662 mp->flags &= ~MDF_16BITS;
664 case PCCARD_A_MEM_16BIT:
665 mp->flags |= MDF_16BITS;
668 err = mecia_cinfo.mapmem(devi->slt, rid);
678 mecia_get_res_flags(device_t bus, device_t child, int restype, int rid,
681 struct pccard_devinfo *devi = device_get_ivars(child);
688 case SYS_RES_IOPORT: {
689 struct io_desc *ip = &devi->slt->io[rid];
693 case SYS_RES_MEMORY: {
694 struct mem_desc *mp = &devi->slt->mem[rid];
705 mecia_set_memory_offset(device_t bus, device_t child, int rid,
706 u_int32_t offset, u_int32_t *deltap)
708 struct pccard_devinfo *devi = device_get_ivars(child);
709 struct mem_desc *mp = &devi->slt->mem[rid];
713 *deltap = 0; /* XXX BAD XXX */
714 return (mecia_cinfo.mapmem(devi->slt, rid));
718 mecia_get_memory_offset(device_t bus, device_t child, int rid,
721 struct pccard_devinfo *devi = device_get_ivars(child);
722 struct mem_desc *mp = &devi->slt->mem[rid];
732 static device_method_t mecia_methods[] = {
733 /* Device interface */
734 DEVMETHOD(device_probe, mecia_probe),
735 DEVMETHOD(device_attach, mecia_attach),
736 DEVMETHOD(device_detach, bus_generic_detach),
737 DEVMETHOD(device_shutdown, bus_generic_shutdown),
738 DEVMETHOD(device_suspend, bus_generic_suspend),
739 DEVMETHOD(device_resume, bus_generic_resume),
742 DEVMETHOD(bus_print_child, bus_generic_print_child),
743 DEVMETHOD(bus_alloc_resource, bus_generic_alloc_resource),
744 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
745 DEVMETHOD(bus_activate_resource, mecia_activate_resource),
746 DEVMETHOD(bus_deactivate_resource, mecia_deactivate_resource),
747 DEVMETHOD(bus_setup_intr, mecia_setup_intr),
748 DEVMETHOD(bus_teardown_intr, mecia_teardown_intr),
751 DEVMETHOD(card_set_res_flags, mecia_set_res_flags),
752 DEVMETHOD(card_get_res_flags, mecia_get_res_flags),
753 DEVMETHOD(card_set_memory_offset, mecia_set_memory_offset),
754 DEVMETHOD(card_get_memory_offset, mecia_get_memory_offset),
759 devclass_t mecia_devclass;
761 static driver_t mecia_driver = {
764 sizeof(struct mecia_slot)
767 DRIVER_MODULE(mecia, isa, mecia_driver, mecia_devclass, 0, 0);