Home | History | Annotate | Line # | Download | only in dev
plumpcmcia.c revision 1.4
      1 /*	$NetBSD: plumpcmcia.c,v 1.4 2000/09/27 17:32:34 uch Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1999, 2000 UCHIYAMA Yasushi. All rights reserved.
      5  * Copyright (c) 1997 Marc Horowitz. All rights reserved.
      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  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by Marc Horowitz.
     18  * 4. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 #include "opt_tx39_debug.h"
     34 
     35 #include <sys/param.h>
     36 #include <sys/systm.h>
     37 #include <sys/device.h>
     38 #include <sys/kthread.h>
     39 
     40 #include <machine/bus.h>
     41 
     42 #include <dev/pcmcia/pcmciareg.h>
     43 #include <dev/pcmcia/pcmciavar.h>
     44 #include <dev/pcmcia/pcmciachip.h>
     45 
     46 #include <hpcmips/tx/tx39var.h>
     47 #include <hpcmips/dev/plumvar.h>
     48 #include <hpcmips/dev/plumicuvar.h>
     49 #include <hpcmips/dev/plumpowervar.h>
     50 #include <hpcmips/dev/plumpcmciareg.h>
     51 
     52 #ifdef PLUMPCMCIADEBUG
     53 #define	DPRINTF(arg) printf arg
     54 #else
     55 #define	DPRINTF(arg)
     56 #endif
     57 
     58 int	plumpcmcia_match(struct device *, struct cfdata *, void *);
     59 void	plumpcmcia_attach(struct device *, struct device *, void *);
     60 int	plumpcmcia_print(void *, const char *);
     61 int	plumpcmcia_submatch(struct device *, struct cfdata *, void *);
     62 
     63 struct plumpcmcia_softc;
     64 
     65 struct plumpcmcia_handle {
     66 	/* parent */
     67 	struct device	*ph_parent;
     68 	/* child */
     69 	struct device	*ph_pcmcia;
     70 
     71 	/* PCMCIA controller register space */
     72 	bus_space_tag_t ph_regt;
     73 	bus_space_handle_t ph_regh;
     74 
     75 	/* I/O port space */
     76 	int ph_ioarea;	/* not PCMCIA window */
     77 	struct {
     78 		bus_addr_t	pi_addr;
     79 		bus_size_t	pi_size;
     80 		int		pi_width;
     81 	} ph_io[PLUM_PCMCIA_IO_WINS];
     82 	int ph_ioalloc;
     83 	bus_space_tag_t ph_iot;
     84 	bus_space_handle_t ph_ioh;
     85 	bus_addr_t ph_iobase;
     86 	bus_size_t ph_iosize;
     87 
     88 	/* I/O Memory space */
     89 	int ph_memarea;	/* not PCMCIA window */
     90 	struct {
     91 		bus_addr_t	pm_addr;
     92 		bus_size_t	pm_size;
     93 		int32_t		pm_offset;
     94 		int		pm_kind;
     95 	} ph_mem[PLUM_PCMCIA_MEM_WINS];
     96 	int ph_memalloc;
     97 	bus_space_tag_t ph_memt;
     98 	bus_space_handle_t ph_memh;
     99 	bus_addr_t ph_membase;
    100 	bus_size_t ph_memsize;
    101 
    102 	/* Card interrupt handler */
    103 	int ph_plum_irq;
    104 	void *ph_card_ih;
    105 };
    106 
    107 enum plumpcmcia_event_type {
    108 	PLUM_PCMCIA_EVENT_INSERT,
    109 	PLUM_PCMCIA_EVENT_REMOVE,
    110 };
    111 
    112 struct plumpcmcia_event {
    113 	int __queued;
    114 	enum plumpcmcia_event_type pe_type;
    115 	struct plumpcmcia_handle *pe_ph;
    116 	SIMPLEQ_ENTRY(plumpcmcia_event) pe_link;
    117 };
    118 
    119 struct plumpcmcia_softc {
    120 	struct device	sc_dev;
    121 	plum_chipset_tag_t sc_pc;
    122 
    123 	/* Register space */
    124 	bus_space_tag_t sc_regt;
    125 	bus_space_handle_t sc_regh;
    126 
    127 	/* CSC event */
    128 	struct proc *sc_event_thread;
    129 	SIMPLEQ_HEAD (, plumpcmcia_event) sc_event_head;
    130 
    131 	/* for each slot */
    132 	struct plumpcmcia_handle sc_ph[PLUMPCMCIA_NSLOTS];
    133 };
    134 
    135 static void plumpcmcia_attach_socket(struct plumpcmcia_handle *);
    136 static int plumpcmcia_chip_mem_alloc(pcmcia_chipset_handle_t, bus_size_t,
    137 				     struct pcmcia_mem_handle *);
    138 static void plumpcmcia_chip_mem_free(pcmcia_chipset_handle_t,
    139 				     struct pcmcia_mem_handle *);
    140 static int plumpcmcia_chip_mem_map(pcmcia_chipset_handle_t, int, bus_addr_t,
    141 				   bus_size_t, struct pcmcia_mem_handle *,
    142 				   bus_addr_t *, int *);
    143 static void plumpcmcia_chip_mem_unmap(pcmcia_chipset_handle_t, int);
    144 static int plumpcmcia_chip_io_alloc(pcmcia_chipset_handle_t, bus_addr_t,
    145 				    bus_size_t, bus_size_t,
    146 				    struct pcmcia_io_handle *);
    147 static void plumpcmcia_chip_io_free(pcmcia_chipset_handle_t,
    148 				    struct pcmcia_io_handle *);
    149 static int plumpcmcia_chip_io_map(pcmcia_chipset_handle_t, int, bus_addr_t,
    150 				  bus_size_t, struct pcmcia_io_handle *,
    151 				  int *);
    152 static void plumpcmcia_chip_io_unmap(pcmcia_chipset_handle_t, int);
    153 static void plumpcmcia_chip_socket_enable(pcmcia_chipset_handle_t);
    154 static void plumpcmcia_chip_socket_disable(pcmcia_chipset_handle_t);
    155 static void *plumpcmcia_chip_intr_establish(pcmcia_chipset_handle_t,
    156 					    struct pcmcia_function *, int,
    157 					    int (*)(void *), void *);
    158 static void plumpcmcia_chip_intr_disestablish(pcmcia_chipset_handle_t, void *);
    159 static void plumpcmcia_wait_ready(	struct plumpcmcia_handle *);
    160 static void plumpcmcia_chip_do_mem_map(struct plumpcmcia_handle *, int);
    161 static void plumpcmcia_chip_do_io_map(struct plumpcmcia_handle *, int);
    162 
    163 static struct pcmcia_chip_functions plumpcmcia_functions = {
    164 	plumpcmcia_chip_mem_alloc,
    165 	plumpcmcia_chip_mem_free,
    166 	plumpcmcia_chip_mem_map,
    167 	plumpcmcia_chip_mem_unmap,
    168 	plumpcmcia_chip_io_alloc,
    169 	plumpcmcia_chip_io_free,
    170 	plumpcmcia_chip_io_map,
    171 	plumpcmcia_chip_io_unmap,
    172 	plumpcmcia_chip_intr_establish,
    173 	plumpcmcia_chip_intr_disestablish,
    174 	plumpcmcia_chip_socket_enable,
    175 	plumpcmcia_chip_socket_disable
    176 };
    177 
    178 /* CSC */
    179 #define PLUM_PCMCIA_EVENT_QUEUE_MAX		5
    180 static struct plumpcmcia_event __event_queue_pool[PLUM_PCMCIA_EVENT_QUEUE_MAX];
    181 static struct plumpcmcia_event *plumpcmcia_event_alloc(void);
    182 static void plumpcmcia_event_free(struct plumpcmcia_event *);
    183 static void plum_csc_intr_setup(struct plumpcmcia_softc *,
    184 				struct plumpcmcia_handle *, int);
    185 static int plum_csc_intr(void *);
    186 static void plumpcmcia_create_event_thread(void *);
    187 static void plumpcmcia_event_thread(void *);
    188 
    189 /* debug */
    190 #define __DEBUG_FUNC	__attribute__((__unused__))
    191 static void __ioareadump(plumreg_t) __DEBUG_FUNC;
    192 static void __memareadump(plumreg_t) __DEBUG_FUNC;
    193 static void plumpcmcia_dump(struct plumpcmcia_softc *) __DEBUG_FUNC;
    194 
    195 struct cfattach plumpcmcia_ca = {
    196 	sizeof(struct plumpcmcia_softc), plumpcmcia_match, plumpcmcia_attach
    197 };
    198 
    199 int
    200 plumpcmcia_match(struct device *parent, struct cfdata *cf, void *aux)
    201 {
    202 	return 1;
    203 }
    204 
    205 void
    206 plumpcmcia_attach(struct device *parent, struct device *self, void *aux)
    207 {
    208 	struct plum_attach_args *pa = aux;
    209 	struct plumpcmcia_softc *sc = (void*)self;
    210 	struct plumpcmcia_handle *ph;
    211 
    212 	sc->sc_pc	= pa->pa_pc;
    213 	sc->sc_regt	= pa->pa_regt;
    214 	if (bus_space_map(sc->sc_regt, PLUM_PCMCIA_REGBASE,
    215 			  PLUM_PCMCIA_REGSIZE, 0, &sc->sc_regh)) {
    216 		printf(": register map failed\n");
    217 	}
    218 
    219 	printf("\n");
    220 
    221 	/* Slot0/1 CSC event queue */
    222 	SIMPLEQ_INIT (&sc->sc_event_head);
    223 	kthread_create(plumpcmcia_create_event_thread, sc);
    224 
    225 	/* Slot 0 */
    226 	ph = &sc->sc_ph[0];
    227 	ph->ph_plum_irq	= PLUM_INT_C1IO;
    228 	ph->ph_memarea	= PLUM_PCMCIA_MEMWINCTRL_MAP_AREA1;
    229 	ph->ph_membase	= PLUM_PCMCIA_MEMBASE1;
    230 	ph->ph_memsize	= PLUM_PCMCIA_MEMSIZE1;
    231 	ph->ph_ioarea	= PLUM_PCMCIA_IOWINADDRCTRL_AREA1;
    232 	ph->ph_iobase	= PLUM_PCMCIA_IOBASE1;
    233 	ph->ph_iosize	= PLUM_PCMCIA_IOSIZE1;
    234 	ph->ph_regt = sc->sc_regt;
    235 	bus_space_subregion(sc->sc_regt, sc->sc_regh,
    236 			    PLUM_PCMCIA_REGSPACE_SLOT0,
    237 			    PLUM_PCMCIA_REGSPACE_SIZE,
    238 			    &ph->ph_regh);
    239 	ph->ph_iot	= pa->pa_iot;
    240 	ph->ph_memt	= pa->pa_iot;
    241 	ph->ph_parent = (void*)sc;
    242 
    243 	plum_csc_intr_setup(sc, ph, PLUM_INT_C1SC);
    244 	plum_power_establish(sc->sc_pc, PLUM_PWR_PCC1);
    245 	plumpcmcia_attach_socket(ph);
    246 
    247 	/* Slot 1 */
    248 	ph = &sc->sc_ph[1];
    249 	ph->ph_plum_irq	= PLUM_INT_C2IO;
    250 	ph->ph_memarea	= PLUM_PCMCIA_MEMWINCTRL_MAP_AREA2;
    251 	ph->ph_membase	= PLUM_PCMCIA_MEMBASE2;
    252 	ph->ph_memsize	= PLUM_PCMCIA_MEMSIZE2;
    253 	ph->ph_ioarea	= PLUM_PCMCIA_IOWINADDRCTRL_AREA2;
    254 	ph->ph_iobase	= PLUM_PCMCIA_IOBASE2;
    255 	ph->ph_iosize	= PLUM_PCMCIA_IOSIZE2;
    256 	ph->ph_regt = sc->sc_regt;
    257 	bus_space_subregion(sc->sc_regt, sc->sc_regh,
    258 			    PLUM_PCMCIA_REGSPACE_SLOT1,
    259 			    PLUM_PCMCIA_REGSPACE_SIZE,
    260 			    &ph->ph_regh);
    261 	ph->ph_iot	= pa->pa_iot;
    262 	ph->ph_memt	= pa->pa_iot;
    263 	ph->ph_parent = (void*)sc;
    264 
    265 	plum_csc_intr_setup(sc, ph, PLUM_INT_C2SC);
    266 	plum_power_establish(sc->sc_pc, PLUM_PWR_PCC2);
    267 	plumpcmcia_attach_socket(ph);
    268 }
    269 
    270 int
    271 plumpcmcia_print(void *arg, const char *pnp)
    272 {
    273 	if (pnp) {
    274 		printf("pcmcia at %s", pnp);
    275 	}
    276 
    277 	return UNCONF;
    278 }
    279 
    280 int
    281 plumpcmcia_submatch(struct device *parent, struct cfdata *cf, void *aux)
    282 {
    283 	return ((*cf->cf_attach->ca_match)(parent, cf, aux));
    284 }
    285 
    286 static void
    287 plumpcmcia_attach_socket(struct plumpcmcia_handle *ph)
    288 {
    289 	struct pcmciabus_attach_args paa;
    290 	struct plumpcmcia_softc *sc = (void*)ph->ph_parent;
    291 
    292 	paa.paa_busname = "pcmcia";
    293 	paa.pct = (pcmcia_chipset_tag_t)&plumpcmcia_functions;
    294 	paa.pch = (pcmcia_chipset_handle_t)ph;
    295 	paa.iobase = 0;		/* I don't use them */
    296 	paa.iosize = 0;
    297 
    298 
    299 	if ((ph->ph_pcmcia = config_found_sm((void*)sc, &paa,
    300 					     plumpcmcia_print,
    301  					     plumpcmcia_submatch))) {
    302 		/* Enable slot */
    303 		plum_conf_write(ph->ph_regt, ph->ph_regh,
    304 				PLUM_PCMCIA_SLOTCTRL,
    305 				PLUM_PCMCIA_SLOTCTRL_ENABLE);
    306 		/* Support 3.3V card & enable Voltage Sense Status */
    307 		plum_conf_write(ph->ph_regt, ph->ph_regh,
    308 				PLUM_PCMCIA_FUNCCTRL,
    309 				PLUM_PCMCIA_FUNCCTRL_VSSEN |
    310 				PLUM_PCMCIA_FUNCCTRL_3VSUPPORT);
    311 		pcmcia_card_attach(ph->ph_pcmcia);
    312 	}
    313 }
    314 
    315 static void *
    316 plumpcmcia_chip_intr_establish(pcmcia_chipset_handle_t pch,
    317 			       struct pcmcia_function *pf, int ipl,
    318 			       int (*ih_fun)(void *), void *ih_arg)
    319 {
    320 	struct plumpcmcia_handle *ph = (void*)pch;
    321 	struct plumpcmcia_softc *sc = (void*)ph->ph_parent;
    322 
    323 	if (!(ph->ph_card_ih =
    324 	      plum_intr_establish(sc->sc_pc, ph->ph_plum_irq,
    325 				  IST_EDGE, IPL_BIO, ih_fun, ih_arg))) {
    326 		printf("plumpcmcia_chip_intr_establish: can't establish\n");
    327 		return 0;
    328 	}
    329 
    330 	return ph->ph_card_ih;
    331 }
    332 
    333 static void
    334 plumpcmcia_chip_intr_disestablish(pcmcia_chipset_handle_t pch, void *ih)
    335 {
    336 	struct plumpcmcia_handle *ph = (void*)pch;
    337 	struct plumpcmcia_softc *sc = (void*)ph->ph_parent;
    338 
    339 	plum_intr_disestablish(sc->sc_pc, ih);
    340 }
    341 
    342 static int
    343 plumpcmcia_chip_mem_alloc(pcmcia_chipset_handle_t pch, bus_size_t size,
    344 			  struct pcmcia_mem_handle *pcmhp)
    345 {
    346 	struct plumpcmcia_handle *ph = (void*)pch;
    347 	bus_size_t realsize;
    348 
    349 	/* convert size to PCIC pages */
    350 	realsize = ((size + (PLUM_PCMCIA_MEM_PAGESIZE - 1)) /
    351 		    PLUM_PCMCIA_MEM_PAGESIZE) * PLUM_PCMCIA_MEM_PAGESIZE;
    352 
    353 	if (bus_space_alloc(ph->ph_memt, ph->ph_membase,
    354 			    ph->ph_membase + ph->ph_memsize,
    355 			    realsize, PLUM_PCMCIA_MEM_PAGESIZE,
    356 			    0, 0, 0, &pcmhp->memh)) {
    357 		return 1;
    358 	}
    359 
    360 	pcmhp->memt = ph->ph_memt;
    361 	/* Address offset from MEM area base */
    362 	pcmhp->addr = pcmhp->memh - ph->ph_membase - ph->ph_memt->t_base;
    363 	pcmhp->size = size;
    364 	pcmhp->realsize = realsize;
    365 
    366 	DPRINTF(("plumpcmcia_chip_mem_alloc: size %#x->%#x addr %#x->%#x\n",
    367 		 (unsigned)size, (unsigned)realsize, (unsigned)pcmhp->addr,
    368 		 (unsigned)pcmhp->memh));
    369 
    370 	return 0;
    371 }
    372 
    373 static void
    374 plumpcmcia_chip_mem_free(pcmcia_chipset_handle_t pch,
    375 			 struct pcmcia_mem_handle *pcmhp)
    376 {
    377 	bus_space_free(pcmhp->memt, pcmhp->memh, pcmhp->size);
    378 }
    379 
    380 static int
    381 plumpcmcia_chip_mem_map(pcmcia_chipset_handle_t pch, int kind,
    382 			bus_addr_t card_addr, bus_size_t size,
    383 			struct pcmcia_mem_handle *pcmhp,
    384 			bus_addr_t *offsetp, int *windowp)
    385 {
    386 	struct plumpcmcia_handle *ph = (void*)pch;
    387 	bus_addr_t busaddr;
    388 	int32_t card_offset;
    389 	int i, win;
    390 
    391 	for (win = -1, i = 0; i < PLUM_PCMCIA_MEM_WINS; i++) {
    392 		if ((ph->ph_memalloc & (1 << i)) == 0) {
    393 			win = i;
    394 			ph->ph_memalloc |= (1 << i);
    395 			break;
    396 		}
    397 	}
    398 	if (win == -1) {
    399 		DPRINTF(("plumpcmcia_chip_mem_map: no window\n"));
    400 		return 1;
    401 	}
    402 
    403 	busaddr = pcmhp->addr;
    404 
    405 	*offsetp = card_addr % PLUM_PCMCIA_MEM_PAGESIZE;
    406 	card_addr -= *offsetp;
    407 	size += *offsetp - 1;
    408 	*windowp = win;
    409 	card_offset = (((int32_t)card_addr) - ((int32_t)busaddr));
    410 
    411 	DPRINTF(("plumpcmcia_chip_mem_map window %d bus %#x(kv:%#x)+%#x"
    412 		 " size %#x at card addr %#x offset %#x\n", win,
    413 		 (unsigned)busaddr, (unsigned)pcmhp->memh, (unsigned)*offsetp,
    414 		 (unsigned)size, (unsigned)card_addr, (unsigned)card_offset));
    415 
    416 	ph->ph_mem[win].pm_addr = busaddr;
    417 	ph->ph_mem[win].pm_size = size;
    418 	ph->ph_mem[win].pm_offset = card_offset;
    419 	ph->ph_mem[win].pm_kind = kind;
    420 	ph->ph_memalloc |= (1 << win);
    421 
    422 	plumpcmcia_chip_do_mem_map(ph, win);
    423 
    424 	return 0;
    425 }
    426 
    427 static void
    428 plumpcmcia_chip_do_mem_map(struct plumpcmcia_handle *ph, int win)
    429 {
    430 	bus_space_tag_t regt = ph->ph_regt;
    431 	bus_space_handle_t regh = ph->ph_regh;
    432 	plumreg_t reg, addr, offset, size;
    433 
    434 	if (win < 0 || win > 4) {
    435 		panic("plumpcmcia_chip_do_mem_map: bogus window %d", win);
    436 	}
    437 
    438 	addr = (ph->ph_mem[win].pm_addr) >> PLUM_PCMCIA_MEM_SHIFT;
    439 	size = (ph->ph_mem[win].pm_size) >> PLUM_PCMCIA_MEM_SHIFT;
    440 	offset = (ph->ph_mem[win].pm_offset) >> PLUM_PCMCIA_MEM_SHIFT;
    441 
    442 	/* Attribute memory or not */
    443 	reg = ph->ph_mem[win].pm_kind == PCMCIA_MEM_ATTR ?
    444 		PLUM_PCMCIA_MEMWINCTRL_REGACTIVE : 0;
    445 
    446 	/* Notify I/O area to select for PCMCIA controller */
    447 	reg = PLUM_PCMCIA_MEMWINCTRL_MAP_SET(reg, ph->ph_memarea);
    448 
    449 	/* Zero wait & 16bit access */
    450 	reg |= (PLUM_PCMCIA_MEMWINCTRL_ZERO_WS |
    451 		PLUM_PCMCIA_MEMWINCTRL_DATASIZE16);
    452 	plum_conf_write(regt, regh, PLUM_PCMCIA_MEMWINCTRL(win), reg);
    453 
    454 	/* Map Host <-> PC-Card address */
    455 
    456 	/* host-side */
    457 	plum_conf_write(regt, regh, PLUM_PCMCIA_MEMWINSTARTADDR(win),
    458 			addr);
    459 	plum_conf_write(regt, regh, PLUM_PCMCIA_MEMWINSTOPADDR(win),
    460 			addr + size);
    461 
    462 	/* card-side */
    463 	plum_conf_write(regt, regh, PLUM_PCMCIA_MEMWINOFSADDR(win), offset);
    464 
    465 	/* Enable memory window */
    466 	reg = plum_conf_read(regt, regh, PLUM_PCMCIA_WINEN);
    467 	reg |= PLUM_PCMCIA_WINEN_MEM(win);
    468 	plum_conf_write(regt, regh, PLUM_PCMCIA_WINEN, reg);
    469 
    470 	DPRINTF(("plumpcmcia_chip_do_mem_map: window:%d %#x(%#x)+%#x\n",
    471 		 win, offset, addr, size));
    472 
    473 	delay(100);
    474 }
    475 
    476 static void
    477 plumpcmcia_chip_mem_unmap(pcmcia_chipset_handle_t pch, int window)
    478 {
    479 	struct plumpcmcia_handle *ph = (void*)pch;
    480 	bus_space_tag_t regt = ph->ph_regt;
    481 	bus_space_handle_t regh = ph->ph_regh;
    482 	plumreg_t reg;
    483 
    484 	reg = plum_conf_read(regt, regh, PLUM_PCMCIA_WINEN);
    485 	reg &= ~PLUM_PCMCIA_WINEN_MEM(window);
    486 	plum_conf_write(regt, regh, PLUM_PCMCIA_WINEN, reg);
    487 
    488 	ph->ph_memalloc &= ~(1 << window);
    489 }
    490 
    491 static int
    492 plumpcmcia_chip_io_alloc(pcmcia_chipset_handle_t pch, bus_addr_t start,
    493 			 bus_size_t size, bus_size_t align,
    494 			 struct pcmcia_io_handle *pcihp)
    495 {
    496 	struct plumpcmcia_handle *ph = (void*)pch;
    497 
    498 	DPRINTF(("plumpcmcia_chip_io_alloc: start=%#x size=%#x ",
    499 		 (unsigned)start, (unsigned)size));
    500 	if (start) {
    501 		if (bus_space_map(ph->ph_iot, ph->ph_iobase + start,
    502 				  size, 0, &pcihp->ioh)) {
    503 			DPRINTF(("bus_space_map failed\n"));
    504 			return 1;
    505 		}
    506 		pcihp->flags = 0;
    507 		pcihp->addr = start;
    508 		DPRINTF(("(mapped) %#x+%#x\n", (unsigned)start,
    509 			 (unsigned)size));
    510 	} else {
    511 		if (bus_space_alloc(ph->ph_iot, ph->ph_iobase,
    512 				    ph->ph_iobase + ph->ph_iosize, size,
    513 				    align, 0, 0, 0, &pcihp->ioh)) {
    514 			DPRINTF(("bus_space_alloc failed\n"));
    515 			return 1;
    516 		}
    517 		/* Address offset from IO area base */
    518 		pcihp->addr = pcihp->ioh - ph->ph_iobase -
    519 			ph->ph_iot->t_base;
    520 		pcihp->flags = PCMCIA_IO_ALLOCATED;
    521 		DPRINTF(("(allocated) %#x+%#x\n", (unsigned)pcihp->addr,
    522 			 (unsigned)size));
    523 	}
    524 
    525 	pcihp->iot = ph->ph_iot;
    526 	pcihp->size = size;
    527 
    528 	return 0;
    529 }
    530 
    531 static int
    532 plumpcmcia_chip_io_map(pcmcia_chipset_handle_t pch, int width,
    533 		       bus_addr_t offset, bus_size_t size,
    534 		       struct pcmcia_io_handle *pcihp, int *windowp)
    535 {
    536 #ifdef PLUMPCMCIADEBUG
    537 	static char *width_names[] = { "auto", "io8", "io16" };
    538 #endif /* PLUMPCMCIADEBUG */
    539 	struct plumpcmcia_handle *ph = (void*)pch;
    540 	bus_addr_t winofs;
    541 	int i, win;
    542 
    543 	winofs = pcihp->addr + offset;
    544 
    545 	if (winofs > 0x3ff) {
    546 		printf("plumpcmcia_chip_io_map: WARNING port %#lx > 0x3ff\n",
    547 		       winofs);
    548 	}
    549 
    550 	for (win = -1, i = 0; i < PLUM_PCMCIA_IO_WINS; i++) {
    551 		if ((ph->ph_ioalloc & (1 << i)) == 0) {
    552 			win = i;
    553 			ph->ph_ioalloc |= (1 << i);
    554 			break;
    555 		}
    556 	}
    557 	if (win == -1) {
    558 		DPRINTF(("plumpcmcia_chip_io_map: no window\n"));
    559 		return 1;
    560 	}
    561 	*windowp = win;
    562 
    563 	ph->ph_io[win].pi_addr = winofs;
    564 	ph->ph_io[win].pi_size = size;
    565 	ph->ph_io[win].pi_width = width;
    566 
    567 	plumpcmcia_chip_do_io_map(ph, win);
    568 
    569 	DPRINTF(("plumpcmcia_chip_io_map: %#x(kv:%#x)+%#x %s\n",
    570 		 (unsigned)offset, (unsigned)pcihp->ioh, (unsigned)size,
    571 		 width_names[width]));
    572 
    573 	return 0;
    574 }
    575 
    576 static void
    577 plumpcmcia_chip_do_io_map(struct plumpcmcia_handle *ph, int win)
    578 {
    579 	bus_space_tag_t regt = ph->ph_regt;
    580 	bus_space_handle_t regh = ph->ph_regh;
    581 	plumreg_t reg;
    582 	bus_addr_t addr;
    583 	bus_size_t size;
    584 	int shift;
    585 	plumreg_t ioctlbits[3] = {
    586 		PLUM_PCMCIA_IOWINCTRL_IOCS16SRC,
    587 		0,
    588 		PLUM_PCMCIA_IOWINCTRL_DATASIZE16
    589 	};
    590 
    591 	if (win < 0 || win > 1) {
    592 		panic("plumpcmcia_chip_do_io_map: bogus window %d", win);
    593 	}
    594 
    595 	addr = ph->ph_io[win].pi_addr;
    596 	size = ph->ph_io[win].pi_size;
    597 
    598 	/* Notify I/O area to select for PCMCIA controller */
    599 	plum_conf_write(regt, regh, PLUM_PCMCIA_IOWINADDRCTRL(win),
    600 			ph->ph_ioarea);
    601 
    602 	/* Start/Stop addr */
    603 	plum_conf_write(regt, regh, PLUM_PCMCIA_IOWINSTARTADDR(win), addr);
    604 	plum_conf_write(regt, regh, PLUM_PCMCIA_IOWINSTOPADDR(win),
    605 			addr + size - 1);
    606 
    607 	/* Set bus width */
    608 	reg = plum_conf_read(regt, regh, PLUM_PCMCIA_IOWINCTRL);
    609 	shift = win == 0 ? PLUM_PCMCIA_IOWINCTRL_WIN0SHIFT :
    610 		PLUM_PCMCIA_IOWINCTRL_WIN1SHIFT;
    611 
    612 	reg &= ~(PLUM_PCMCIA_IOWINCTRL_WINMASK << shift);
    613 	reg |= ((ioctlbits[ph->ph_io[win].pi_width] |
    614 		 PLUM_PCMCIA_IOWINCTRL_ZEROWAIT) << shift);
    615 	plum_conf_write(regt, regh, PLUM_PCMCIA_IOWINCTRL, reg);
    616 
    617 	/* Enable window */
    618 	reg = plum_conf_read(regt, regh, PLUM_PCMCIA_WINEN);
    619 	reg |= (win == 0 ? PLUM_PCMCIA_WINEN_IO0 :
    620 		PLUM_PCMCIA_WINEN_IO1);
    621 	plum_conf_write(regt, regh, PLUM_PCMCIA_WINEN, reg);
    622 
    623 	delay(100);
    624 }
    625 
    626 static void
    627 plumpcmcia_chip_io_free(pcmcia_chipset_handle_t pch,
    628 			struct pcmcia_io_handle *pcihp)
    629 {
    630 	if (pcihp->flags & PCMCIA_IO_ALLOCATED) {
    631 		bus_space_free(pcihp->iot, pcihp->ioh, pcihp->size);
    632 	} else {
    633 		bus_space_unmap(pcihp->iot, pcihp->ioh, pcihp->size);
    634 	}
    635 
    636 	DPRINTF(("plumpcmcia_chip_io_free %#x+%#x\n", pcihp->ioh,
    637 		 (unsigned)pcihp->size));
    638 }
    639 
    640 static void
    641 plumpcmcia_chip_io_unmap(pcmcia_chipset_handle_t pch, int window)
    642 {
    643 	struct plumpcmcia_handle *ph = (void*)pch;
    644 	bus_space_tag_t regt = ph->ph_regt;
    645 	bus_space_handle_t regh = ph->ph_regh;
    646 	plumreg_t reg;
    647 
    648 	reg = plum_conf_read(regt, regh, PLUM_PCMCIA_WINEN);
    649 	switch (window) {
    650 	default:
    651 		panic("plumpcmcia_chip_io_unmap: bogus window");
    652 	case 0:
    653 		reg &= ~PLUM_PCMCIA_WINEN_IO0;
    654 		break;
    655 	case 1:
    656 		reg &= ~PLUM_PCMCIA_WINEN_IO1;
    657 		break;
    658 	}
    659 	plum_conf_write(regt, regh, PLUM_PCMCIA_WINEN, reg);
    660 	ph->ph_ioalloc &= ~(1 << window);
    661 }
    662 
    663 static void
    664 plumpcmcia_wait_ready(struct plumpcmcia_handle *ph)
    665 {
    666 	bus_space_tag_t regt = ph->ph_regt;
    667 	bus_space_handle_t regh = ph->ph_regh;
    668 	int i;
    669 
    670 	for (i = 0; i < 10000; i++) {
    671 		if ((plum_conf_read(regt, regh, PLUM_PCMCIA_STATUS) &
    672 		    PLUM_PCMCIA_STATUS_READY) &&
    673 		    (plum_conf_read(regt, regh, PLUM_PCMCIA_STATUS) &
    674 		     PLUM_PCMCIA_STATUS_PWROK)) {
    675 			return;
    676 		}
    677 		delay(500);
    678 
    679 		if ((i > 5000) && (i % 100 == 99)) {
    680 			printf(".");
    681 		}
    682 	}
    683 	printf("plumpcmcia_wait_ready: failed\n");
    684 }
    685 
    686 static void
    687 plumpcmcia_chip_socket_enable(pcmcia_chipset_handle_t pch)
    688 {
    689 	struct plumpcmcia_handle *ph = (void*)pch;
    690 	bus_space_tag_t regt = ph->ph_regt;
    691 	bus_space_handle_t regh = ph->ph_regh;
    692 	plumreg_t reg, power;
    693 	int win, cardtype;
    694 
    695 	/* this bit is mostly stolen from pcic_attach_card */
    696 
    697 	/* power down the socket to reset it, clear the card reset pin */
    698 
    699 	plum_conf_write(regt, regh, PLUM_PCMCIA_PWRCTRL, 0);
    700 
    701 	/*
    702 	 * wait 300ms until power fails (Tpf).  Then, wait 100ms since
    703 	 * we are changing Vcc (Toff).
    704 	 */
    705 	delay((300 + 100) * 1000);
    706 
    707 	/*
    708 	 *  power up the socket
    709 	 */
    710 	/* detect voltage */
    711 	reg = plum_conf_read(regt, regh, PLUM_PCMCIA_GENCTRL2);
    712 	if ((reg & PLUM_PCMCIA_GENCTRL2_VCC5V) ==
    713 	    PLUM_PCMCIA_GENCTRL2_VCC5V) {
    714 		power = PLUM_PCMCIA_PWRCTRL_VCC_CTRLBIT1; /* 5V */
    715 	} else {
    716 		power = PLUM_PCMCIA_PWRCTRL_VCC_CTRLBIT0; /* 3.3V */
    717 	}
    718 
    719 	plum_conf_write(regt, regh, PLUM_PCMCIA_PWRCTRL,
    720 			PLUM_PCMCIA_PWRCTRL_DISABLE_RESETDRV |
    721 			power |
    722 			PLUM_PCMCIA_PWRCTRL_PWR_ENABLE);
    723 
    724 	/*
    725 	 * wait 100ms until power raise (Tpr) and 20ms to become
    726 	 * stable (Tsu(Vcc)).
    727 	 *
    728 	 * some machines require some more time to be settled
    729 	 * (300ms is added here).
    730 	 */
    731 	delay((100 + 20 + 300) * 1000);
    732 
    733 	plum_conf_write(regt, regh, PLUM_PCMCIA_PWRCTRL,
    734 			PLUM_PCMCIA_PWRCTRL_DISABLE_RESETDRV |
    735 			power |
    736 			PLUM_PCMCIA_PWRCTRL_OE |
    737 			PLUM_PCMCIA_PWRCTRL_PWR_ENABLE);
    738 	plum_conf_write(regt, regh, PLUM_PCMCIA_GENCTRL, 0);
    739 
    740 	/*
    741 	 * hold RESET at least 10us.
    742 	 */
    743 	delay(10);
    744 
    745 	/* clear the reset flag */
    746 	plum_conf_write(regt, regh, PLUM_PCMCIA_GENCTRL,
    747 			PLUM_PCMCIA_GENCTRL_RESET);
    748 
    749 	/* wait 20ms as per pc card standard (r2.01) section 4.3.6 */
    750 
    751 	delay(20000);
    752 
    753 	/* wait for the chip to finish initializing */
    754 	plumpcmcia_wait_ready(ph);
    755 
    756 	/* zero out the address windows */
    757 
    758 	plum_conf_write(regt, regh, PLUM_PCMCIA_WINEN, 0);
    759 
    760 	/* set the card type */
    761 
    762 	cardtype = pcmcia_card_gettype(ph->ph_pcmcia);
    763 
    764 	reg = (cardtype == PCMCIA_IFTYPE_IO) ?
    765 		PLUM_PCMCIA_GENCTRL_CARDTYPE_IO :
    766 		PLUM_PCMCIA_GENCTRL_CARDTYPE_MEM;
    767 	reg |= plum_conf_read(regt, regh, PLUM_PCMCIA_GENCTRL);
    768 	DPRINTF(("%s: plumpcmcia_chip_socket_enable cardtype %s\n",
    769 		 ph->ph_parent->dv_xname,
    770 		 ((cardtype == PCMCIA_IFTYPE_IO) ? "io" : "mem")));
    771 
    772 	plum_conf_write(regt, regh, PLUM_PCMCIA_GENCTRL, reg);
    773 
    774 	/* reinstall all the memory and io mappings */
    775 
    776 	for (win = 0; win < PLUM_PCMCIA_MEM_WINS; win++) {
    777 		if (ph->ph_memalloc & (1 << win)) {
    778 			plumpcmcia_chip_do_mem_map(ph, win);
    779 		}
    780 	}
    781 
    782 	for (win = 0; win < PLUM_PCMCIA_IO_WINS; win++) {
    783 		if (ph->ph_ioalloc & (1 << win)) {
    784 			plumpcmcia_chip_do_io_map(ph, win);
    785 		}
    786 	}
    787 
    788 }
    789 
    790 static void
    791 plumpcmcia_chip_socket_disable(pcmcia_chipset_handle_t pch)
    792 {
    793 	struct plumpcmcia_handle *ph = (void*)pch;
    794 	bus_space_tag_t regt = ph->ph_regt;
    795 	bus_space_handle_t regh = ph->ph_regh;
    796 
    797 	/* power down the socket */
    798 	plum_conf_write(regt, regh, PLUM_PCMCIA_PWRCTRL, 0);
    799 
    800 	/*
    801 	 * wait 300ms until power fails (Tpf).
    802 	 */
    803 	delay(300 * 1000);
    804 }
    805 
    806 static void
    807 plum_csc_intr_setup(struct plumpcmcia_softc *sc, struct plumpcmcia_handle *ph,
    808 		    int irq)
    809 {
    810 	bus_space_tag_t regt = ph->ph_regt;
    811 	bus_space_handle_t regh = ph->ph_regh;
    812 	plumreg_t reg;
    813 	void *ih;
    814 
    815 	/* enable CARD DETECT ENABLE only */
    816 	plum_conf_write(regt, regh, PLUM_PCMCIA_CSCINT,
    817 			PLUM_PCMCIA_CSCINT_CARD_DETECT);
    818 
    819 	/* don't use explicit writeback csc interrupt status */
    820 	reg = plum_conf_read(regt, regh, PLUM_PCMCIA_GLOBALCTRL);
    821 	reg &= ~PLUM_PCMCIA_GLOBALCTRL_EXPLICIT_WB_CSC_INT;
    822 	plum_conf_write(regt, regh, PLUM_PCMCIA_GLOBALCTRL, reg);
    823 
    824 	/* install interrupt handler (don't fail) */
    825 	ih = plum_intr_establish(sc->sc_pc, irq, IST_EDGE, IPL_TTY,
    826 				 plum_csc_intr, ph);
    827 	KASSERT(ih != 0);
    828 }
    829 
    830 static int
    831 plum_csc_intr(void *arg)
    832 {
    833 	struct plumpcmcia_handle *ph = arg;
    834 	struct plumpcmcia_softc *sc = (void *)ph->ph_parent;
    835 	struct plumpcmcia_event *pe;
    836 	bus_space_tag_t regt = ph->ph_regt;
    837 	bus_space_handle_t regh = ph->ph_regh;
    838 	plumreg_t reg;
    839 	int flag;
    840 
    841 	/* read and clear interrupt status */
    842 	reg = plum_conf_read(regt, regh, PLUM_PCMCIA_CSCINT_STAT);
    843 	if (reg & PLUM_PCMCIA_CSCINT_CARD_DETECT) {
    844 		DPRINTF(("%s: card status change.\n", __FUNCTION__));
    845 	} else {
    846 		DPRINTF(("%s: unhandled csc event. 0x%02x\n",
    847 			 __FUNCTION__, reg));
    848 		return 0;
    849 	}
    850 
    851 	/* inquire card status (insert or remove) */
    852 	reg = plum_conf_read(regt, regh, PLUM_PCMCIA_STATUS);
    853 	reg &= (PLUM_PCMCIA_STATUS_CD1 | PLUM_PCMCIA_STATUS_CD2);
    854 	if (reg == (PLUM_PCMCIA_STATUS_CD1 | PLUM_PCMCIA_STATUS_CD2)) {
    855 		/* insert */
    856 		flag = PLUM_PCMCIA_EVENT_INSERT;
    857 	} else {
    858 		/* remove */
    859 		flag = PLUM_PCMCIA_EVENT_REMOVE;
    860 	}
    861 
    862 	/* queue event to event thread and wakeup. */
    863 	pe = plumpcmcia_event_alloc();
    864 	if (pe == 0) {
    865 		printf("%s: event FIFO overflow (%d).\n", __FUNCTION__,
    866 		       PLUM_PCMCIA_EVENT_QUEUE_MAX);
    867 		return 0;
    868 	}
    869 	pe->pe_type = flag;
    870 	pe->pe_ph = ph;
    871 	SIMPLEQ_INSERT_TAIL(&sc->sc_event_head, pe, pe_link);
    872 	wakeup(sc);
    873 
    874 	return 0;
    875 }
    876 
    877 static void
    878 __memareadump(plumreg_t reg)
    879 {
    880 	int maparea;
    881 
    882 	maparea = PLUM_PCMCIA_MEMWINCTRL_MAP(reg);
    883 	switch (maparea) {
    884 	case PLUM_PCMCIA_MEMWINCTRL_MAP_AREA1:
    885 		printf("MEM Area1\n");
    886 		break;
    887 	case PLUM_PCMCIA_MEMWINCTRL_MAP_AREA2:
    888 		printf("MEM Area2\n");
    889 		break;
    890 	case PLUM_PCMCIA_MEMWINCTRL_MAP_AREA3:
    891 		printf("MEM Area3\n");
    892 		break;
    893 	case PLUM_PCMCIA_MEMWINCTRL_MAP_AREA4:
    894 		printf("MEM Area4\n");
    895 		break;
    896 	}
    897 }
    898 
    899 static struct plumpcmcia_event *
    900 plumpcmcia_event_alloc()
    901 {
    902 	int i;
    903 	/* I assume called from interrupt context only. so don't lock */
    904 	for (i = 0; i < PLUM_PCMCIA_EVENT_QUEUE_MAX; i++) {
    905 		if (!__event_queue_pool[i].__queued) {
    906 			__event_queue_pool[i].__queued = 1;
    907 			return &__event_queue_pool[i];
    908 		}
    909 	}
    910 
    911 	return 0;
    912 }
    913 
    914 static void
    915 plumpcmcia_event_free(struct plumpcmcia_event *pe)
    916 {
    917 	/* I assume context is already locked */
    918 	pe->__queued = 0;
    919 }
    920 
    921 static void
    922 plumpcmcia_create_event_thread(void *arg)
    923 {
    924 	struct plumpcmcia_softc *sc = arg;
    925 	int error;
    926 
    927 	error = kthread_create1(plumpcmcia_event_thread, sc,
    928 				&sc->sc_event_thread, "%s",
    929 				sc->sc_dev.dv_xname);
    930 	KASSERT(error == 0);
    931 }
    932 
    933 static void
    934 plumpcmcia_event_thread(void *arg)
    935 {
    936 	struct plumpcmcia_softc *sc = arg;
    937 	struct plumpcmcia_event *pe;
    938 	int s;
    939 
    940 	while (/*CONSTCOND*/1) { /* XXX shutdown. -uch */
    941 		tsleep(sc, PWAIT, "CSC wait", 0);
    942 		s = spltty();
    943 		while ((pe = SIMPLEQ_FIRST(&sc->sc_event_head))) {
    944 			splx(s);
    945 			switch (pe->pe_type) {
    946 			default:
    947 				printf("%s: unknown event.\n", __FUNCTION__);
    948 				break;
    949 			case PLUM_PCMCIA_EVENT_INSERT:
    950 				DPRINTF(("%s: insert event.\n", __FUNCTION__));
    951 				pcmcia_card_attach(pe->pe_ph->ph_pcmcia);
    952 				break;
    953 			case PLUM_PCMCIA_EVENT_REMOVE:
    954 				DPRINTF(("%s: remove event.\n", __FUNCTION__));
    955 				pcmcia_card_detach(pe->pe_ph->ph_pcmcia,
    956 						   DETACH_FORCE);
    957 				break;
    958 			}
    959 			s = spltty();
    960 			SIMPLEQ_REMOVE_HEAD(&sc->sc_event_head, pe, pe_link);
    961 			plumpcmcia_event_free(pe);
    962 		}
    963 		splx(s);
    964 	}
    965 	/* NOTREACHED */
    966 }
    967 
    968 static void
    969 __ioareadump(plumreg_t reg)
    970 {
    971 	if (reg & PLUM_PCMCIA_IOWINADDRCTRL_AREA2) {
    972 		printf("I/O Area 2\n");
    973 	} else {
    974 		printf("I/O Area 1\n");
    975 	}
    976 }
    977 
    978 static void
    979 plumpcmcia_dump(struct plumpcmcia_softc *sc)
    980 {
    981 	bus_space_tag_t regt = sc->sc_regt;
    982 	bus_space_handle_t regh = sc->sc_regh;
    983 	plumreg_t reg;
    984 
    985 	int i, j;
    986 
    987 	__memareadump(plum_conf_read(regt, regh, PLUM_PCMCIA_MEMWIN0CTRL));
    988 	__memareadump(plum_conf_read(regt, regh, PLUM_PCMCIA_MEMWIN1CTRL));
    989 	__memareadump(plum_conf_read(regt, regh, PLUM_PCMCIA_MEMWIN2CTRL));
    990 	__memareadump(plum_conf_read(regt, regh, PLUM_PCMCIA_MEMWIN3CTRL));
    991 	__memareadump(plum_conf_read(regt, regh, PLUM_PCMCIA_MEMWIN4CTRL));
    992 
    993 	__ioareadump(plum_conf_read(regt, regh, PLUM_PCMCIA_IOWIN0ADDRCTRL));
    994 	__ioareadump(plum_conf_read(regt, regh, PLUM_PCMCIA_IOWIN1ADDRCTRL));
    995 
    996 	for (j = 0; j < 2; j++) {
    997 		printf("[slot %d]\n", j);
    998 		for (i = 0; i < 0x120; i += 4) {
    999 			reg = plum_conf_read(sc->sc_regt, sc->sc_regh, i + 0x800 * j);
   1000 			printf("%03x %08x", i, reg);
   1001 			bitdisp(reg);
   1002 		}
   1003 	}
   1004 	printf("\n");
   1005 }
   1006