Home | History | Annotate | Line # | Download | only in ic
i82365.c revision 1.116.46.2
      1  1.116.46.2   thorpej /*	$NetBSD: i82365.c,v 1.116.46.2 2021/03/22 16:23:45 thorpej Exp $	*/
      2        1.84   mycroft 
      3        1.84   mycroft /*
      4        1.84   mycroft  * Copyright (c) 2004 Charles M. Hannum.  All rights reserved.
      5        1.84   mycroft  *
      6        1.84   mycroft  * Redistribution and use in source and binary forms, with or without
      7        1.84   mycroft  * modification, are permitted provided that the following conditions
      8        1.84   mycroft  * are met:
      9        1.84   mycroft  * 1. Redistributions of source code must retain the above copyright
     10        1.84   mycroft  *    notice, this list of conditions and the following disclaimer.
     11        1.84   mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     12        1.84   mycroft  *    notice, this list of conditions and the following disclaimer in the
     13        1.84   mycroft  *    documentation and/or other materials provided with the distribution.
     14        1.84   mycroft  * 3. All advertising materials mentioning features or use of this software
     15        1.84   mycroft  *    must display the following acknowledgement:
     16        1.84   mycroft  *      This product includes software developed by Charles M. Hannum.
     17        1.84   mycroft  * 4. The name of the author may not be used to endorse or promote products
     18        1.84   mycroft  *    derived from this software without specific prior written permission.
     19        1.84   mycroft  */
     20         1.2   thorpej 
     21         1.2   thorpej /*
     22        1.33    chopps  * Copyright (c) 2000 Christian E. Hopps.  All rights reserved.
     23         1.2   thorpej  * Copyright (c) 1997 Marc Horowitz.  All rights reserved.
     24         1.2   thorpej  *
     25         1.2   thorpej  * Redistribution and use in source and binary forms, with or without
     26         1.2   thorpej  * modification, are permitted provided that the following conditions
     27         1.2   thorpej  * are met:
     28         1.2   thorpej  * 1. Redistributions of source code must retain the above copyright
     29         1.2   thorpej  *    notice, this list of conditions and the following disclaimer.
     30         1.2   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     31         1.2   thorpej  *    notice, this list of conditions and the following disclaimer in the
     32         1.2   thorpej  *    documentation and/or other materials provided with the distribution.
     33         1.2   thorpej  * 3. All advertising materials mentioning features or use of this software
     34         1.2   thorpej  *    must display the following acknowledgement:
     35         1.2   thorpej  *	This product includes software developed by Marc Horowitz.
     36         1.2   thorpej  * 4. The name of the author may not be used to endorse or promote products
     37         1.2   thorpej  *    derived from this software without specific prior written permission.
     38         1.2   thorpej  *
     39         1.2   thorpej  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     40         1.2   thorpej  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     41         1.2   thorpej  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     42         1.2   thorpej  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     43         1.2   thorpej  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     44         1.2   thorpej  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     45         1.2   thorpej  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     46         1.2   thorpej  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     47         1.2   thorpej  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     48         1.2   thorpej  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     49         1.2   thorpej  */
     50        1.63     lukem 
     51        1.63     lukem #include <sys/cdefs.h>
     52  1.116.46.2   thorpej __KERNEL_RCSID(0, "$NetBSD: i82365.c,v 1.116.46.2 2021/03/22 16:23:45 thorpej Exp $");
     53        1.63     lukem 
     54        1.63     lukem #define	PCICDEBUG
     55         1.2   thorpej 
     56         1.2   thorpej #include <sys/param.h>
     57         1.2   thorpej #include <sys/systm.h>
     58         1.2   thorpej #include <sys/device.h>
     59         1.2   thorpej #include <sys/extent.h>
     60        1.20   msaitoh #include <sys/kernel.h>
     61         1.2   thorpej #include <sys/malloc.h>
     62        1.14   thorpej #include <sys/kthread.h>
     63         1.2   thorpej 
     64       1.100        ad #include <sys/bus.h>
     65       1.100        ad #include <sys/intr.h>
     66         1.2   thorpej 
     67         1.2   thorpej #include <dev/pcmcia/pcmciareg.h>
     68         1.2   thorpej #include <dev/pcmcia/pcmciavar.h>
     69         1.2   thorpej 
     70         1.2   thorpej #include <dev/ic/i82365reg.h>
     71         1.2   thorpej #include <dev/ic/i82365var.h>
     72         1.2   thorpej 
     73        1.87  drochner #include "locators.h"
     74        1.87  drochner 
     75         1.2   thorpej #ifdef PCICDEBUG
     76         1.2   thorpej int	pcic_debug = 0;
     77         1.2   thorpej #define	DPRINTF(arg) if (pcic_debug) printf arg;
     78         1.2   thorpej #else
     79         1.2   thorpej #define	DPRINTF(arg)
     80         1.2   thorpej #endif
     81         1.2   thorpej 
     82         1.2   thorpej /*
     83         1.2   thorpej  * Individual drivers will allocate their own memory and io regions. Memory
     84         1.2   thorpej  * regions must be a multiple of 4k, aligned on a 4k boundary.
     85         1.2   thorpej  */
     86         1.2   thorpej 
     87         1.2   thorpej #define	PCIC_MEM_ALIGN	PCIC_MEM_PAGESIZE
     88         1.2   thorpej 
     89        1.88     perry void	pcic_attach_socket(struct pcic_handle *);
     90        1.88     perry void	pcic_attach_socket_finish(struct pcic_handle *);
     91         1.2   thorpej 
     92        1.88     perry int	pcic_print (void *arg, const char *pnp);
     93        1.88     perry int	pcic_intr_socket(struct pcic_handle *);
     94        1.88     perry void	pcic_poll_intr(void *);
     95         1.2   thorpej 
     96        1.88     perry void	pcic_attach_card(struct pcic_handle *);
     97        1.88     perry void	pcic_detach_card(struct pcic_handle *, int);
     98        1.88     perry void	pcic_deactivate_card(struct pcic_handle *);
     99         1.2   thorpej 
    100        1.88     perry void	pcic_chip_do_mem_map(struct pcic_handle *, int);
    101        1.88     perry void	pcic_chip_do_io_map(struct pcic_handle *, int);
    102         1.2   thorpej 
    103        1.88     perry void	pcic_event_thread(void *);
    104        1.14   thorpej 
    105        1.88     perry void	pcic_queue_event(struct pcic_handle *, int);
    106        1.88     perry void	pcic_power(int, void *);
    107        1.14   thorpej 
    108        1.88     perry static int	pcic_wait_ready(struct pcic_handle *);
    109        1.88     perry static void	pcic_delay(struct pcic_handle *, int, const char *);
    110         1.8      marc 
    111       1.109   tsutsui static uint8_t st_pcic_read(struct pcic_handle *, int);
    112       1.109   tsutsui static void st_pcic_write(struct pcic_handle *, int, uint8_t);
    113        1.25      haya 
    114         1.2   thorpej int
    115       1.105       dsl pcic_ident_ok(int ident)
    116         1.2   thorpej {
    117       1.111   tsutsui 
    118         1.2   thorpej 	/* this is very empirical and heuristic */
    119         1.2   thorpej 
    120         1.2   thorpej 	if ((ident == 0) || (ident == 0xff) || (ident & PCIC_IDENT_ZERO))
    121       1.111   tsutsui 		return 0;
    122         1.2   thorpej 
    123        1.75   mycroft 	if ((ident & PCIC_IDENT_REV_MASK) == 0)
    124       1.111   tsutsui 		return 0;
    125        1.75   mycroft 
    126         1.2   thorpej 	if ((ident & PCIC_IDENT_IFTYPE_MASK) != PCIC_IDENT_IFTYPE_MEM_AND_IO) {
    127         1.2   thorpej #ifdef DIAGNOSTIC
    128         1.2   thorpej 		printf("pcic: does not support memory and I/O cards, "
    129         1.2   thorpej 		    "ignored (ident=%0x)\n", ident);
    130         1.2   thorpej #endif
    131       1.111   tsutsui 		return 0;
    132         1.2   thorpej 	}
    133        1.75   mycroft 
    134       1.111   tsutsui 	return 1;
    135         1.2   thorpej }
    136         1.2   thorpej 
    137         1.2   thorpej int
    138       1.105       dsl pcic_vendor(struct pcic_handle *h)
    139         1.2   thorpej {
    140         1.2   thorpej 	int reg;
    141        1.69  takemura 	int vendor;
    142         1.2   thorpej 
    143        1.75   mycroft 	reg = pcic_read(h, PCIC_IDENT);
    144         1.2   thorpej 
    145        1.75   mycroft 	if ((reg & PCIC_IDENT_REV_MASK) == 0)
    146       1.111   tsutsui 		return PCIC_VENDOR_NONE;
    147         1.2   thorpej 
    148        1.69  takemura 	switch (reg) {
    149        1.75   mycroft 	case 0x00:
    150        1.75   mycroft 	case 0xff:
    151       1.111   tsutsui 		return PCIC_VENDOR_NONE;
    152        1.69  takemura 	case PCIC_IDENT_ID_INTEL0:
    153        1.69  takemura 		vendor = PCIC_VENDOR_I82365SLR0;
    154        1.69  takemura 		break;
    155        1.69  takemura 	case PCIC_IDENT_ID_INTEL1:
    156        1.69  takemura 		vendor = PCIC_VENDOR_I82365SLR1;
    157        1.69  takemura 		break;
    158        1.69  takemura 	case PCIC_IDENT_ID_INTEL2:
    159        1.69  takemura 		vendor = PCIC_VENDOR_I82365SL_DF;
    160        1.69  takemura 		break;
    161        1.69  takemura 	case PCIC_IDENT_ID_IBM1:
    162        1.69  takemura 	case PCIC_IDENT_ID_IBM2:
    163        1.69  takemura 		vendor = PCIC_VENDOR_IBM;
    164        1.69  takemura 		break;
    165        1.69  takemura 	case PCIC_IDENT_ID_IBM3:
    166        1.69  takemura 		vendor = PCIC_VENDOR_IBM_KING;
    167        1.69  takemura 		break;
    168        1.69  takemura 	default:
    169        1.69  takemura 		vendor = PCIC_VENDOR_UNKNOWN;
    170        1.69  takemura 		break;
    171        1.69  takemura 	}
    172        1.69  takemura 
    173        1.69  takemura 	if (vendor == PCIC_VENDOR_I82365SLR0 ||
    174        1.69  takemura 	    vendor == PCIC_VENDOR_I82365SLR1) {
    175        1.69  takemura 		/*
    176        1.75   mycroft 		 * Check for Cirrus PD67xx.
    177        1.75   mycroft 		 * the chip_id of the cirrus toggles between 11 and 00 after a
    178        1.75   mycroft 		 * write.  weird.
    179        1.75   mycroft 		 */
    180        1.75   mycroft 		pcic_write(h, PCIC_CIRRUS_CHIP_INFO, 0);
    181        1.75   mycroft 		reg = pcic_read(h, -1);
    182        1.75   mycroft 		if ((reg & PCIC_CIRRUS_CHIP_INFO_CHIP_ID) ==
    183        1.75   mycroft 		    PCIC_CIRRUS_CHIP_INFO_CHIP_ID) {
    184        1.75   mycroft 			reg = pcic_read(h, -1);
    185        1.75   mycroft 			if ((reg & PCIC_CIRRUS_CHIP_INFO_CHIP_ID) == 0)
    186       1.111   tsutsui 				return PCIC_VENDOR_CIRRUS_PD67XX;
    187        1.75   mycroft 		}
    188        1.75   mycroft 
    189        1.75   mycroft 		/*
    190        1.69  takemura 		 * check for Ricoh RF5C[23]96
    191        1.69  takemura 		 */
    192        1.69  takemura 		reg = pcic_read(h, PCIC_RICOH_REG_CHIP_ID);
    193        1.69  takemura 		switch (reg) {
    194        1.69  takemura 		case PCIC_RICOH_CHIP_ID_5C296:
    195       1.111   tsutsui 			return PCIC_VENDOR_RICOH_5C296;
    196        1.69  takemura 		case PCIC_RICOH_CHIP_ID_5C396:
    197       1.111   tsutsui 			return PCIC_VENDOR_RICOH_5C396;
    198        1.69  takemura 		}
    199        1.69  takemura 	}
    200        1.69  takemura 
    201       1.111   tsutsui 	return vendor;
    202         1.2   thorpej }
    203         1.2   thorpej 
    204        1.90  christos const char *
    205       1.105       dsl pcic_vendor_to_string(int vendor)
    206         1.2   thorpej {
    207       1.111   tsutsui 
    208         1.2   thorpej 	switch (vendor) {
    209         1.2   thorpej 	case PCIC_VENDOR_I82365SLR0:
    210       1.111   tsutsui 		return "Intel 82365SL Revision 0";
    211         1.2   thorpej 	case PCIC_VENDOR_I82365SLR1:
    212       1.111   tsutsui 		return "Intel 82365SL Revision 1";
    213        1.75   mycroft 	case PCIC_VENDOR_CIRRUS_PD67XX:
    214       1.111   tsutsui 		return "Cirrus PD6710/2X";
    215        1.69  takemura 	case PCIC_VENDOR_I82365SL_DF:
    216       1.111   tsutsui 		return "Intel 82365SL-DF";
    217        1.69  takemura 	case PCIC_VENDOR_RICOH_5C296:
    218       1.111   tsutsui 		return "Ricoh RF5C296";
    219        1.69  takemura 	case PCIC_VENDOR_RICOH_5C396:
    220       1.111   tsutsui 		return "Ricoh RF5C396";
    221        1.69  takemura 	case PCIC_VENDOR_IBM:
    222       1.111   tsutsui 		return "IBM PCIC";
    223        1.69  takemura 	case PCIC_VENDOR_IBM_KING:
    224       1.111   tsutsui 		return "IBM KING";
    225         1.2   thorpej 	}
    226         1.2   thorpej 
    227       1.111   tsutsui 	return "Unknown controller";
    228         1.2   thorpej }
    229         1.2   thorpej 
    230         1.2   thorpej void
    231       1.105       dsl pcic_attach(struct pcic_softc *sc)
    232         1.2   thorpej {
    233        1.75   mycroft 	int i, reg, chip, socket;
    234        1.54   mycroft 	struct pcic_handle *h;
    235       1.110   tsutsui 	device_t self;
    236         1.2   thorpej 
    237        1.33    chopps 	DPRINTF(("pcic ident regs:"));
    238         1.2   thorpej 
    239       1.115       chs 	self = sc->dev;
    240       1.101        ad 	mutex_init(&sc->sc_pcic_lock, MUTEX_DEFAULT, IPL_NONE);
    241        1.53   thorpej 
    242        1.33    chopps 	/* find and configure for the available sockets */
    243        1.94  christos 	for (i = 0; i < __arraycount(sc->handle); i++) {
    244        1.54   mycroft 		h = &sc->handle[i];
    245        1.33    chopps 		chip = i / 2;
    246        1.33    chopps 		socket = i % 2;
    247        1.54   mycroft 
    248       1.110   tsutsui 		h->ph_parent = self;
    249        1.54   mycroft 		h->chip = chip;
    250        1.87  drochner 		h->socket = socket;
    251        1.54   mycroft 		h->sock = chip * PCIC_CHIP_OFFSET + socket * PCIC_SOCKET_OFFSET;
    252        1.54   mycroft 		h->laststate = PCIC_LASTSTATE_EMPTY;
    253        1.35     enami 		/* initialize pcic_read and pcic_write functions */
    254        1.54   mycroft 		h->ph_read = st_pcic_read;
    255        1.54   mycroft 		h->ph_write = st_pcic_write;
    256        1.54   mycroft 		h->ph_bus_t = sc->iot;
    257        1.54   mycroft 		h->ph_bus_h = sc->ioh;
    258        1.75   mycroft 		h->flags = 0;
    259        1.54   mycroft 
    260        1.33    chopps 		/* need to read vendor -- for cirrus to report no xtra chip */
    261        1.94  christos 		if (socket == 0) {
    262        1.94  christos 			h->vendor = pcic_vendor(h);
    263        1.94  christos 			if (i < __arraycount(sc->handle) - 1)
    264       1.111   tsutsui 				(h + 1)->vendor = h->vendor;
    265        1.94  christos 		}
    266        1.54   mycroft 
    267        1.75   mycroft 		switch (h->vendor) {
    268        1.75   mycroft 		case PCIC_VENDOR_NONE:
    269        1.75   mycroft 			/* no chip */
    270        1.75   mycroft 			continue;
    271        1.75   mycroft 		case PCIC_VENDOR_CIRRUS_PD67XX:
    272        1.75   mycroft 			reg = pcic_read(h, PCIC_CIRRUS_CHIP_INFO);
    273        1.75   mycroft 			if (socket == 0 ||
    274        1.75   mycroft 			    (reg & PCIC_CIRRUS_CHIP_INFO_SLOTS))
    275        1.75   mycroft 				h->flags = PCIC_FLAG_SOCKETP;
    276        1.75   mycroft 			break;
    277        1.89     perry 		default:
    278        1.75   mycroft 			/*
    279        1.75   mycroft 			 * During the socket probe, read the ident register
    280        1.75   mycroft 			 * twice.  I don't understand why, but sometimes the
    281        1.75   mycroft 			 * clone chips in hpcmips boxes read all-0s the first
    282        1.75   mycroft 			 * time. -- mycroft
    283        1.75   mycroft 			 */
    284        1.75   mycroft 			reg = pcic_read(h, PCIC_IDENT);
    285        1.75   mycroft 			DPRINTF(("socket %d ident reg 0x%02x\n", i, reg));
    286        1.75   mycroft 			reg = pcic_read(h, PCIC_IDENT);
    287        1.75   mycroft 			DPRINTF(("socket %d ident reg 0x%02x\n", i, reg));
    288        1.75   mycroft 			if (pcic_ident_ok(reg))
    289        1.75   mycroft 				h->flags = PCIC_FLAG_SOCKETP;
    290        1.75   mycroft 			break;
    291        1.75   mycroft 		}
    292         1.2   thorpej 	}
    293         1.2   thorpej 
    294        1.94  christos 	for (i = 0; i < __arraycount(sc->handle); i++) {
    295        1.54   mycroft 		h = &sc->handle[i];
    296        1.54   mycroft 
    297        1.54   mycroft 		if (h->flags & PCIC_FLAG_SOCKETP) {
    298        1.54   mycroft 			SIMPLEQ_INIT(&h->events);
    299        1.33    chopps 
    300        1.75   mycroft 			/* disable interrupts and leave socket in reset */
    301        1.83   mycroft 			pcic_write(h, PCIC_INTR, 0);
    302        1.83   mycroft 
    303        1.83   mycroft 			/* zero out the address windows */
    304        1.83   mycroft 			pcic_write(h, PCIC_ADDRWIN_ENABLE, 0);
    305        1.83   mycroft 
    306        1.83   mycroft 			/* power down the socket */
    307        1.83   mycroft 			pcic_write(h, PCIC_PWRCTL, 0);
    308        1.83   mycroft 
    309        1.54   mycroft 			pcic_write(h, PCIC_CSC_INTR, 0);
    310        1.54   mycroft 			(void) pcic_read(h, PCIC_CSC);
    311         1.2   thorpej 		}
    312         1.2   thorpej 	}
    313         1.2   thorpej 
    314        1.33    chopps 	/* print detected info */
    315        1.94  christos 	for (i = 0; i < __arraycount(sc->handle) - 1; i += 2) {
    316        1.54   mycroft 		h = &sc->handle[i];
    317        1.33    chopps 		chip = i / 2;
    318         1.2   thorpej 
    319        1.75   mycroft 		if (h->vendor == PCIC_VENDOR_NONE)
    320        1.75   mycroft 			continue;
    321        1.75   mycroft 
    322       1.110   tsutsui 		aprint_normal_dev(self, "controller %d (%s) has ",
    323        1.72   thorpej 		    chip, pcic_vendor_to_string(sc->handle[i].vendor));
    324         1.2   thorpej 
    325        1.54   mycroft 		if ((h->flags & PCIC_FLAG_SOCKETP) &&
    326       1.111   tsutsui 		    ((h + 1)->flags & PCIC_FLAG_SOCKETP))
    327        1.72   thorpej 			aprint_normal("sockets A and B\n");
    328        1.54   mycroft 		else if (h->flags & PCIC_FLAG_SOCKETP)
    329        1.72   thorpej 			aprint_normal("socket A only\n");
    330       1.111   tsutsui 		else if ((h + 1)->flags & PCIC_FLAG_SOCKETP)
    331        1.72   thorpej 			aprint_normal("socket B only\n");
    332         1.2   thorpej 		else
    333        1.72   thorpej 			aprint_normal("no sockets\n");
    334         1.2   thorpej 	}
    335         1.2   thorpej }
    336         1.2   thorpej 
    337        1.33    chopps /*
    338        1.33    chopps  * attach the sockets before we know what interrupts we have
    339        1.33    chopps  */
    340         1.2   thorpej void
    341       1.105       dsl pcic_attach_sockets(struct pcic_softc *sc)
    342         1.2   thorpej {
    343         1.2   thorpej 	int i;
    344         1.2   thorpej 
    345        1.94  christos 	for (i = 0; i < __arraycount(sc->handle); i++)
    346         1.2   thorpej 		if (sc->handle[i].flags & PCIC_FLAG_SOCKETP)
    347         1.2   thorpej 			pcic_attach_socket(&sc->handle[i]);
    348         1.2   thorpej }
    349         1.2   thorpej 
    350         1.2   thorpej void
    351       1.105       dsl pcic_power(int why, void *arg)
    352        1.26  sommerfe {
    353       1.111   tsutsui 	struct pcic_handle *h = arg;
    354       1.110   tsutsui 	struct pcic_softc *sc = device_private(h->ph_parent);
    355        1.33    chopps 	int reg;
    356        1.33    chopps 
    357       1.102    cegger 	DPRINTF(("%s: power: why %d\n", device_xname(h->ph_parent), why));
    358        1.26  sommerfe 
    359        1.26  sommerfe 	if (h->flags & PCIC_FLAG_SOCKETP) {
    360        1.26  sommerfe 		if ((why == PWR_RESUME) &&
    361        1.26  sommerfe 		    (pcic_read(h, PCIC_CSC_INTR) == 0)) {
    362        1.26  sommerfe #ifdef PCICDEBUG
    363        1.26  sommerfe 			char bitbuf[64];
    364        1.26  sommerfe #endif
    365        1.33    chopps 			reg = PCIC_CSC_INTR_CD_ENABLE;
    366        1.33    chopps 			if (sc->irq != -1)
    367        1.33    chopps 			    reg |= sc->irq << PCIC_CSC_INTR_IRQ_SHIFT;
    368        1.33    chopps 			pcic_write(h, PCIC_CSC_INTR, reg);
    369       1.103  christos #ifdef PCICDEBUG
    370       1.103  christos 			snprintb(bitbuf, sizeof(bitbuf), PCIC_CSC_INTR_FORMAT,
    371       1.103  christos 			    pcic_read(h, PCIC_CSC_INTR));
    372       1.103  christos #endif
    373        1.26  sommerfe 			DPRINTF(("%s: CSC_INTR was zero; reset to %s\n",
    374       1.115       chs 			    device_xname(sc->dev), bitbuf));
    375        1.26  sommerfe 		}
    376        1.42    itojun 
    377        1.42    itojun 		/*
    378        1.42    itojun 		 * check for card insertion or removal during suspend period.
    379        1.42    itojun 		 * XXX: the code can't cope with card swap (remove then insert).
    380        1.42    itojun 		 * how can we detect such situation?
    381        1.42    itojun 		 */
    382        1.42    itojun 		if (why == PWR_RESUME)
    383        1.42    itojun 			(void)pcic_intr_socket(h);
    384        1.26  sommerfe 	}
    385        1.26  sommerfe }
    386        1.26  sommerfe 
    387        1.26  sommerfe 
    388        1.33    chopps /*
    389        1.33    chopps  * attach a socket -- we don't know about irqs yet
    390        1.33    chopps  */
    391        1.26  sommerfe void
    392       1.105       dsl pcic_attach_socket(struct pcic_handle *h)
    393         1.2   thorpej {
    394         1.2   thorpej 	struct pcmciabus_attach_args paa;
    395       1.110   tsutsui 	struct pcic_softc *sc = device_private(h->ph_parent);
    396        1.91  drochner 	int locs[PCMCIABUSCF_NLOCS];
    397         1.2   thorpej 
    398         1.2   thorpej 	/* initialize the rest of the handle */
    399         1.2   thorpej 
    400        1.14   thorpej 	h->shutdown = 0;
    401         1.2   thorpej 	h->memalloc = 0;
    402         1.2   thorpej 	h->ioalloc = 0;
    403         1.2   thorpej 	h->ih_irq = 0;
    404         1.2   thorpej 
    405         1.2   thorpej 	/* now, config one pcmcia device per socket */
    406         1.2   thorpej 
    407        1.25      haya 	paa.paa_busname = "pcmcia";
    408        1.25      haya 	paa.pct = (pcmcia_chipset_tag_t) sc->pct;
    409         1.2   thorpej 	paa.pch = (pcmcia_chipset_handle_t) h;
    410         1.2   thorpej 
    411        1.91  drochner 	locs[PCMCIABUSCF_CONTROLLER] = h->chip;
    412        1.91  drochner 	locs[PCMCIABUSCF_SOCKET] = h->socket;
    413        1.87  drochner 
    414  1.116.46.1   thorpej 	h->pcmcia = config_found(sc->dev, &paa, pcic_print,
    415  1.116.46.1   thorpej 	    CFARG_SUBMATCH, config_stdsubmatch,
    416  1.116.46.1   thorpej 	    CFARG_LOCATORS, locs,
    417  1.116.46.1   thorpej 	    CFARG_EOL);
    418        1.50   mycroft 	if (h->pcmcia == NULL) {
    419        1.50   mycroft 		h->flags &= ~PCIC_FLAG_SOCKETP;
    420        1.33    chopps 		return;
    421        1.50   mycroft 	}
    422         1.2   thorpej 
    423        1.33    chopps }
    424         1.2   thorpej 
    425        1.33    chopps /*
    426        1.33    chopps  * now finish attaching the sockets, we are ready to allocate
    427        1.33    chopps  * interrupts
    428        1.33    chopps  */
    429        1.33    chopps void
    430       1.105       dsl pcic_attach_sockets_finish(struct pcic_softc *sc)
    431        1.33    chopps {
    432        1.33    chopps 	int i;
    433        1.33    chopps 
    434        1.94  christos 	for (i = 0; i < __arraycount(sc->handle); i++)
    435        1.51   mycroft 		if (sc->handle[i].flags & PCIC_FLAG_SOCKETP)
    436        1.33    chopps 			pcic_attach_socket_finish(&sc->handle[i]);
    437        1.33    chopps }
    438        1.33    chopps 
    439        1.33    chopps /*
    440        1.33    chopps  * finishing attaching the socket.  Interrupts may now be on
    441        1.33    chopps  * if so expects the pcic interrupt to be blocked
    442        1.33    chopps  */
    443        1.33    chopps void
    444       1.105       dsl pcic_attach_socket_finish(struct pcic_handle *h)
    445        1.33    chopps {
    446       1.110   tsutsui 	struct pcic_softc *sc = device_private(h->ph_parent);
    447        1.83   mycroft 	int reg;
    448       1.108       jun 	char cs[4];
    449        1.33    chopps 
    450       1.102    cegger 	DPRINTF(("%s: attach finish socket %ld\n", device_xname(h->ph_parent),
    451        1.46   nathanw 	    (long) (h - &sc->handle[0])));
    452        1.51   mycroft 
    453        1.33    chopps 	/*
    454        1.33    chopps 	 * Set up a powerhook to ensure it continues to interrupt on
    455        1.33    chopps 	 * card detect even after suspend.
    456        1.33    chopps 	 * (this works around a bug seen in suspend-to-disk on the
    457        1.33    chopps 	 * Sony VAIO Z505; on resume, the CSC_INTR state is not preserved).
    458        1.33    chopps 	 */
    459       1.102    cegger 	powerhook_establish(device_xname(h->ph_parent), pcic_power, h);
    460        1.33    chopps 
    461        1.33    chopps 	/* enable interrupts on card detect, poll for them if no irq avail */
    462        1.33    chopps 	reg = PCIC_CSC_INTR_CD_ENABLE;
    463        1.57   thorpej 	if (sc->irq == -1) {
    464        1.57   thorpej 		if (sc->poll_established == 0) {
    465        1.99        ad 			callout_init(&sc->poll_ch, 0);
    466        1.57   thorpej 			callout_reset(&sc->poll_ch, hz / 2, pcic_poll_intr, sc);
    467        1.57   thorpej 			sc->poll_established = 1;
    468        1.57   thorpej 		}
    469        1.57   thorpej 	} else
    470        1.33    chopps 		reg |= sc->irq << PCIC_CSC_INTR_IRQ_SHIFT;
    471        1.33    chopps 	pcic_write(h, PCIC_CSC_INTR, reg);
    472        1.33    chopps 
    473        1.33    chopps 	/* steer above mgmt interrupt to configured place */
    474        1.73   mycroft 	if (sc->irq == 0)
    475        1.83   mycroft 		pcic_write(h, PCIC_INTR, PCIC_INTR_ENABLE);
    476        1.33    chopps 
    477        1.33    chopps 	/* clear possible card detect interrupt */
    478        1.83   mycroft 	(void) pcic_read(h, PCIC_CSC);
    479        1.33    chopps 
    480       1.111   tsutsui 	DPRINTF(("%s: attach finish vendor 0x%02x\n",
    481       1.111   tsutsui 	    device_xname(h->ph_parent), h->vendor));
    482        1.33    chopps 
    483        1.33    chopps 	/* unsleep the cirrus controller */
    484        1.75   mycroft 	if (h->vendor == PCIC_VENDOR_CIRRUS_PD67XX) {
    485        1.33    chopps 		reg = pcic_read(h, PCIC_CIRRUS_MISC_CTL_2);
    486        1.33    chopps 		if (reg & PCIC_CIRRUS_MISC_CTL_2_SUSPEND) {
    487        1.33    chopps 			DPRINTF(("%s: socket %02x was suspended\n",
    488       1.102    cegger 			    device_xname(h->ph_parent), h->sock));
    489        1.33    chopps 			reg &= ~PCIC_CIRRUS_MISC_CTL_2_SUSPEND;
    490        1.33    chopps 			pcic_write(h, PCIC_CIRRUS_MISC_CTL_2, reg);
    491        1.33    chopps 		}
    492        1.33    chopps 	}
    493        1.33    chopps 
    494        1.33    chopps 	/* if there's a card there, then attach it. */
    495        1.33    chopps 	reg = pcic_read(h, PCIC_IF_STATUS);
    496        1.33    chopps 	if ((reg & PCIC_IF_STATUS_CARDDETECT_MASK) ==
    497        1.33    chopps 	    PCIC_IF_STATUS_CARDDETECT_PRESENT) {
    498        1.33    chopps 		pcic_queue_event(h, PCIC_EVENT_INSERTION);
    499        1.33    chopps 		h->laststate = PCIC_LASTSTATE_PRESENT;
    500        1.33    chopps 	} else {
    501        1.33    chopps 		h->laststate = PCIC_LASTSTATE_EMPTY;
    502        1.33    chopps 	}
    503       1.108       jun 
    504       1.108       jun 	/*
    505       1.108       jun 	 * queue creation of a kernel thread to handle insert/removal events.
    506       1.108       jun 	 */
    507       1.108       jun #ifdef DIAGNOSTIC
    508       1.108       jun 	if (h->event_thread != NULL)
    509       1.108       jun 		panic("pcic_attach_socket: event thread");
    510       1.108       jun #endif
    511       1.116       riz 	config_pending_incr(sc->dev);
    512       1.108       jun 	snprintf(cs, sizeof(cs), "%d,%d", h->chip, h->socket);
    513       1.108       jun 
    514       1.108       jun 	if (kthread_create(PRI_NONE, 0, NULL, pcic_event_thread, h,
    515       1.108       jun 	    &h->event_thread, "%s,%s", device_xname(h->ph_parent), cs)) {
    516       1.111   tsutsui 		aprint_error_dev(h->ph_parent,
    517       1.111   tsutsui 		    "unable to create event thread for sock 0x%02x\n", h->sock);
    518       1.108       jun 		panic("pcic_attach_socket");
    519       1.108       jun 	}
    520         1.2   thorpej }
    521         1.2   thorpej 
    522         1.2   thorpej void
    523       1.105       dsl pcic_event_thread(void *arg)
    524        1.14   thorpej {
    525        1.14   thorpej 	struct pcic_handle *h = arg;
    526        1.14   thorpej 	struct pcic_event *pe;
    527        1.29     enami 	int s, first = 1;
    528       1.110   tsutsui 	struct pcic_softc *sc = device_private(h->ph_parent);
    529        1.14   thorpej 
    530        1.14   thorpej 	while (h->shutdown == 0) {
    531        1.53   thorpej 		/*
    532        1.53   thorpej 		 * Serialize event processing on the PCIC.  We may
    533        1.53   thorpej 		 * sleep while we hold this lock.
    534        1.53   thorpej 		 */
    535       1.101        ad 		mutex_enter(&sc->sc_pcic_lock);
    536        1.53   thorpej 
    537        1.14   thorpej 		s = splhigh();
    538        1.14   thorpej 		if ((pe = SIMPLEQ_FIRST(&h->events)) == NULL) {
    539        1.14   thorpej 			splx(s);
    540        1.29     enami 			if (first) {
    541        1.29     enami 				first = 0;
    542       1.116       riz 				config_pending_decr(sc->dev);
    543        1.29     enami 			}
    544        1.53   thorpej 			/*
    545        1.53   thorpej 			 * No events to process; release the PCIC lock.
    546        1.53   thorpej 			 */
    547       1.101        ad 			(void) mutex_exit(&sc->sc_pcic_lock);
    548        1.14   thorpej 			(void) tsleep(&h->events, PWAIT, "pcicev", 0);
    549        1.14   thorpej 			continue;
    550        1.20   msaitoh 		} else {
    551        1.20   msaitoh 			splx(s);
    552        1.20   msaitoh 			/* sleep .25s to be enqueued chatterling interrupts */
    553        1.98  christos 			(void) tsleep((void *)pcic_event_thread, PWAIT,
    554       1.111   tsutsui 			    "pcicss", hz / 4);
    555        1.14   thorpej 		}
    556        1.20   msaitoh 		s = splhigh();
    557        1.66     lukem 		SIMPLEQ_REMOVE_HEAD(&h->events, pe_q);
    558        1.14   thorpej 		splx(s);
    559        1.14   thorpej 
    560        1.14   thorpej 		switch (pe->pe_type) {
    561        1.14   thorpej 		case PCIC_EVENT_INSERTION:
    562        1.20   msaitoh 			s = splhigh();
    563       1.111   tsutsui 			for (;;) {
    564        1.20   msaitoh 				struct pcic_event *pe1, *pe2;
    565        1.20   msaitoh 
    566        1.20   msaitoh 				if ((pe1 = SIMPLEQ_FIRST(&h->events)) == NULL)
    567        1.20   msaitoh 					break;
    568        1.20   msaitoh 				if (pe1->pe_type != PCIC_EVENT_REMOVAL)
    569        1.20   msaitoh 					break;
    570        1.20   msaitoh 				if ((pe2 = SIMPLEQ_NEXT(pe1, pe_q)) == NULL)
    571        1.20   msaitoh 					break;
    572        1.20   msaitoh 				if (pe2->pe_type == PCIC_EVENT_INSERTION) {
    573        1.66     lukem 					SIMPLEQ_REMOVE_HEAD(&h->events, pe_q);
    574        1.20   msaitoh 					free(pe1, M_TEMP);
    575        1.66     lukem 					SIMPLEQ_REMOVE_HEAD(&h->events, pe_q);
    576        1.20   msaitoh 					free(pe2, M_TEMP);
    577        1.20   msaitoh 				}
    578        1.20   msaitoh 			}
    579        1.20   msaitoh 			splx(s);
    580        1.89     perry 
    581        1.35     enami 			DPRINTF(("%s: insertion event\n",
    582       1.102    cegger 			    device_xname(h->ph_parent)));
    583        1.14   thorpej 			pcic_attach_card(h);
    584        1.14   thorpej 			break;
    585        1.14   thorpej 
    586        1.14   thorpej 		case PCIC_EVENT_REMOVAL:
    587        1.20   msaitoh 			s = splhigh();
    588       1.111   tsutsui 			for (;;) {
    589        1.20   msaitoh 				struct pcic_event *pe1, *pe2;
    590        1.20   msaitoh 
    591        1.20   msaitoh 				if ((pe1 = SIMPLEQ_FIRST(&h->events)) == NULL)
    592        1.20   msaitoh 					break;
    593        1.20   msaitoh 				if (pe1->pe_type != PCIC_EVENT_INSERTION)
    594        1.20   msaitoh 					break;
    595        1.20   msaitoh 				if ((pe2 = SIMPLEQ_NEXT(pe1, pe_q)) == NULL)
    596        1.20   msaitoh 					break;
    597        1.20   msaitoh 				if (pe2->pe_type == PCIC_EVENT_REMOVAL) {
    598        1.66     lukem 					SIMPLEQ_REMOVE_HEAD(&h->events, pe_q);
    599        1.20   msaitoh 					free(pe1, M_TEMP);
    600        1.66     lukem 					SIMPLEQ_REMOVE_HEAD(&h->events, pe_q);
    601        1.20   msaitoh 					free(pe2, M_TEMP);
    602        1.20   msaitoh 				}
    603        1.20   msaitoh 			}
    604        1.20   msaitoh 			splx(s);
    605        1.20   msaitoh 
    606        1.35     enami 			DPRINTF(("%s: removal event\n",
    607       1.102    cegger 			    device_xname(h->ph_parent)));
    608        1.15   thorpej 			pcic_detach_card(h, DETACH_FORCE);
    609        1.14   thorpej 			break;
    610        1.14   thorpej 
    611        1.14   thorpej 		default:
    612        1.14   thorpej 			panic("pcic_event_thread: unknown event %d",
    613        1.14   thorpej 			    pe->pe_type);
    614        1.14   thorpej 		}
    615        1.14   thorpej 		free(pe, M_TEMP);
    616        1.53   thorpej 
    617       1.101        ad 		mutex_exit(&sc->sc_pcic_lock);
    618        1.14   thorpej 	}
    619        1.14   thorpej 
    620        1.14   thorpej 	h->event_thread = NULL;
    621        1.14   thorpej 
    622        1.14   thorpej 	/* In case parent is waiting for us to exit. */
    623        1.25      haya 	wakeup(sc);
    624        1.14   thorpej 
    625        1.14   thorpej 	kthread_exit(0);
    626        1.14   thorpej }
    627        1.14   thorpej 
    628         1.2   thorpej int
    629       1.105       dsl pcic_print(void *arg, const char *pnp)
    630         1.2   thorpej {
    631         1.3     enami 	struct pcmciabus_attach_args *paa = arg;
    632         1.2   thorpej 	struct pcic_handle *h = (struct pcic_handle *) paa->pch;
    633         1.2   thorpej 
    634         1.2   thorpej 	/* Only "pcmcia"s can attach to "pcic"s... easy. */
    635         1.2   thorpej 	if (pnp)
    636        1.70   thorpej 		aprint_normal("pcmcia at %s", pnp);
    637         1.2   thorpej 
    638        1.87  drochner 	aprint_normal(" controller %d socket %d", h->chip, h->socket);
    639         1.2   thorpej 
    640       1.111   tsutsui 	return UNCONF;
    641         1.2   thorpej }
    642         1.2   thorpej 
    643        1.33    chopps void
    644       1.105       dsl pcic_poll_intr(void *arg)
    645        1.33    chopps {
    646        1.33    chopps 	struct pcic_softc *sc;
    647        1.33    chopps 	int i, s;
    648        1.33    chopps 
    649        1.33    chopps 	s = spltty();
    650        1.33    chopps 	sc = arg;
    651        1.94  christos 	for (i = 0; i < __arraycount(sc->handle); i++)
    652        1.33    chopps 		if (sc->handle[i].flags & PCIC_FLAG_SOCKETP)
    653        1.33    chopps 			(void)pcic_intr_socket(&sc->handle[i]);
    654        1.57   thorpej 	callout_reset(&sc->poll_ch, hz / 2, pcic_poll_intr, sc);
    655        1.33    chopps 	splx(s);
    656        1.33    chopps }
    657        1.33    chopps 
    658         1.2   thorpej int
    659       1.105       dsl pcic_intr(void *arg)
    660         1.2   thorpej {
    661         1.3     enami 	struct pcic_softc *sc = arg;
    662         1.2   thorpej 	int i, ret = 0;
    663         1.2   thorpej 
    664       1.115       chs 	DPRINTF(("%s: intr\n", device_xname(sc->dev)));
    665         1.2   thorpej 
    666        1.94  christos 	for (i = 0; i < __arraycount(sc->handle); i++)
    667         1.2   thorpej 		if (sc->handle[i].flags & PCIC_FLAG_SOCKETP)
    668         1.2   thorpej 			ret += pcic_intr_socket(&sc->handle[i]);
    669         1.2   thorpej 
    670       1.111   tsutsui 	return ret ? 1 : 0;
    671         1.2   thorpej }
    672         1.2   thorpej 
    673         1.2   thorpej int
    674       1.105       dsl pcic_intr_socket(struct pcic_handle *h)
    675         1.2   thorpej {
    676         1.2   thorpej 	int cscreg;
    677         1.2   thorpej 
    678         1.2   thorpej 	cscreg = pcic_read(h, PCIC_CSC);
    679         1.2   thorpej 
    680         1.2   thorpej 	cscreg &= (PCIC_CSC_GPI |
    681         1.2   thorpej 		   PCIC_CSC_CD |
    682         1.2   thorpej 		   PCIC_CSC_READY |
    683         1.2   thorpej 		   PCIC_CSC_BATTWARN |
    684         1.2   thorpej 		   PCIC_CSC_BATTDEAD);
    685         1.2   thorpej 
    686         1.2   thorpej 	if (cscreg & PCIC_CSC_GPI) {
    687       1.111   tsutsui 		DPRINTF(("%s: %02x GPI\n",
    688       1.111   tsutsui 		    device_xname(h->ph_parent), h->sock));
    689         1.2   thorpej 	}
    690         1.2   thorpej 	if (cscreg & PCIC_CSC_CD) {
    691         1.2   thorpej 		int statreg;
    692         1.2   thorpej 
    693         1.2   thorpej 		statreg = pcic_read(h, PCIC_IF_STATUS);
    694         1.2   thorpej 
    695       1.111   tsutsui 		DPRINTF(("%s: %02x CD %x\n", device_xname(h->ph_parent),
    696       1.111   tsutsui 		    h->sock, statreg));
    697         1.2   thorpej 
    698         1.2   thorpej 		if ((statreg & PCIC_IF_STATUS_CARDDETECT_MASK) ==
    699         1.2   thorpej 		    PCIC_IF_STATUS_CARDDETECT_PRESENT) {
    700        1.20   msaitoh 			if (h->laststate != PCIC_LASTSTATE_PRESENT) {
    701        1.14   thorpej 				DPRINTF(("%s: enqueing INSERTION event\n",
    702       1.111   tsutsui 				    device_xname(h->ph_parent)));
    703        1.14   thorpej 				pcic_queue_event(h, PCIC_EVENT_INSERTION);
    704        1.14   thorpej 			}
    705        1.20   msaitoh 			h->laststate = PCIC_LASTSTATE_PRESENT;
    706         1.2   thorpej 		} else {
    707        1.20   msaitoh 			if (h->laststate == PCIC_LASTSTATE_PRESENT) {
    708        1.15   thorpej 				/* Deactivate the card now. */
    709        1.15   thorpej 				DPRINTF(("%s: deactivating card\n",
    710       1.111   tsutsui 				    device_xname(h->ph_parent)));
    711        1.15   thorpej 				pcic_deactivate_card(h);
    712        1.15   thorpej 
    713        1.14   thorpej 				DPRINTF(("%s: enqueing REMOVAL event\n",
    714       1.111   tsutsui 				    device_xname(h->ph_parent)));
    715        1.14   thorpej 				pcic_queue_event(h, PCIC_EVENT_REMOVAL);
    716        1.14   thorpej 			}
    717        1.83   mycroft 			h->laststate = PCIC_LASTSTATE_EMPTY;
    718         1.2   thorpej 		}
    719         1.2   thorpej 	}
    720         1.2   thorpej 	if (cscreg & PCIC_CSC_READY) {
    721       1.111   tsutsui 		DPRINTF(("%s: %02x READY\n", device_xname(h->ph_parent),
    722       1.111   tsutsui 		     h->sock));
    723         1.2   thorpej 		/* shouldn't happen */
    724         1.2   thorpej 	}
    725         1.2   thorpej 	if (cscreg & PCIC_CSC_BATTWARN) {
    726       1.102    cegger 		DPRINTF(("%s: %02x BATTWARN\n", device_xname(h->ph_parent),
    727        1.35     enami 		    h->sock));
    728         1.2   thorpej 	}
    729         1.2   thorpej 	if (cscreg & PCIC_CSC_BATTDEAD) {
    730       1.102    cegger 		DPRINTF(("%s: %02x BATTDEAD\n", device_xname(h->ph_parent),
    731        1.35     enami 		    h->sock));
    732         1.2   thorpej 	}
    733       1.111   tsutsui 	return cscreg ? 1 : 0;
    734        1.14   thorpej }
    735        1.14   thorpej 
    736        1.14   thorpej void
    737       1.105       dsl pcic_queue_event(struct pcic_handle *h, int event)
    738        1.14   thorpej {
    739        1.14   thorpej 	struct pcic_event *pe;
    740        1.14   thorpej 	int s;
    741        1.14   thorpej 
    742        1.14   thorpej 	pe = malloc(sizeof(*pe), M_TEMP, M_NOWAIT);
    743        1.14   thorpej 	if (pe == NULL)
    744        1.14   thorpej 		panic("pcic_queue_event: can't allocate event");
    745        1.14   thorpej 
    746        1.14   thorpej 	pe->pe_type = event;
    747        1.14   thorpej 	s = splhigh();
    748        1.14   thorpej 	SIMPLEQ_INSERT_TAIL(&h->events, pe, pe_q);
    749        1.14   thorpej 	splx(s);
    750        1.14   thorpej 	wakeup(&h->events);
    751         1.2   thorpej }
    752         1.2   thorpej 
    753         1.2   thorpej void
    754       1.105       dsl pcic_attach_card(struct pcic_handle *h)
    755         1.2   thorpej {
    756        1.15   thorpej 
    757       1.111   tsutsui 	if ((h->flags & PCIC_FLAG_CARDP) == 0) {
    758        1.20   msaitoh 		/* call the MI attach function */
    759        1.20   msaitoh 		pcmcia_card_attach(h->pcmcia);
    760         1.2   thorpej 
    761        1.20   msaitoh 		h->flags |= PCIC_FLAG_CARDP;
    762        1.20   msaitoh 	} else {
    763        1.20   msaitoh 		DPRINTF(("pcic_attach_card: already attached"));
    764        1.20   msaitoh 	}
    765         1.2   thorpej }
    766         1.2   thorpej 
    767         1.2   thorpej void
    768       1.106       dsl pcic_detach_card(struct pcic_handle *h, int flags)
    769       1.106       dsl 	/* flags:		 DETACH_* */
    770         1.2   thorpej {
    771        1.15   thorpej 
    772        1.20   msaitoh 	if (h->flags & PCIC_FLAG_CARDP) {
    773        1.20   msaitoh 		h->flags &= ~PCIC_FLAG_CARDP;
    774         1.2   thorpej 
    775        1.20   msaitoh 		/* call the MI detach function */
    776        1.20   msaitoh 		pcmcia_card_detach(h->pcmcia, flags);
    777        1.20   msaitoh 	} else {
    778        1.20   msaitoh 		DPRINTF(("pcic_detach_card: already detached"));
    779        1.20   msaitoh 	}
    780        1.15   thorpej }
    781        1.15   thorpej 
    782        1.15   thorpej void
    783       1.105       dsl pcic_deactivate_card(struct pcic_handle *h)
    784        1.15   thorpej {
    785        1.74   mycroft 	int intr;
    786         1.2   thorpej 
    787        1.15   thorpej 	/* call the MI deactivate function */
    788        1.15   thorpej 	pcmcia_card_deactivate(h->pcmcia);
    789         1.2   thorpej 
    790        1.15   thorpej 	/* reset the socket */
    791        1.74   mycroft 	intr = pcic_read(h, PCIC_INTR);
    792        1.74   mycroft 	intr &= PCIC_INTR_ENABLE;
    793        1.74   mycroft 	pcic_write(h, PCIC_INTR, intr);
    794        1.86   mycroft 
    795        1.86   mycroft 	/* power down the socket */
    796        1.86   mycroft 	pcic_write(h, PCIC_PWRCTL, 0);
    797         1.2   thorpej }
    798         1.2   thorpej 
    799        1.89     perry int
    800       1.111   tsutsui pcic_chip_mem_alloc(pcmcia_chipset_handle_t pch, bus_size_t size,
    801       1.111   tsutsui     struct pcmcia_mem_handle *pcmhp)
    802         1.2   thorpej {
    803         1.2   thorpej 	struct pcic_handle *h = (struct pcic_handle *) pch;
    804         1.2   thorpej 	bus_space_handle_t memh;
    805         1.2   thorpej 	bus_addr_t addr;
    806         1.2   thorpej 	bus_size_t sizepg;
    807         1.2   thorpej 	int i, mask, mhandle;
    808       1.110   tsutsui 	struct pcic_softc *sc = device_private(h->ph_parent);
    809         1.2   thorpej 
    810         1.2   thorpej 	/* out of sc->memh, allocate as many pages as necessary */
    811         1.2   thorpej 
    812         1.2   thorpej 	/* convert size to PCIC pages */
    813         1.2   thorpej 	sizepg = (size + (PCIC_MEM_ALIGN - 1)) / PCIC_MEM_ALIGN;
    814        1.19  christos 	if (sizepg > PCIC_MAX_MEM_PAGES)
    815       1.111   tsutsui 		return 1;
    816         1.2   thorpej 
    817         1.2   thorpej 	mask = (1 << sizepg) - 1;
    818         1.2   thorpej 
    819         1.2   thorpej 	addr = 0;		/* XXX gcc -Wuninitialized */
    820         1.2   thorpej 	mhandle = 0;		/* XXX gcc -Wuninitialized */
    821         1.2   thorpej 
    822        1.19  christos 	for (i = 0; i <= PCIC_MAX_MEM_PAGES - sizepg; i++) {
    823        1.25      haya 		if ((sc->subregionmask & (mask << i)) == (mask << i)) {
    824        1.25      haya 			if (bus_space_subregion(sc->memt, sc->memh,
    825         1.2   thorpej 			    i * PCIC_MEM_PAGESIZE,
    826         1.2   thorpej 			    sizepg * PCIC_MEM_PAGESIZE, &memh))
    827       1.111   tsutsui 				return 1;
    828         1.2   thorpej 			mhandle = mask << i;
    829        1.25      haya 			addr = sc->membase + (i * PCIC_MEM_PAGESIZE);
    830        1.25      haya 			sc->subregionmask &= ~(mhandle);
    831        1.25      haya 			pcmhp->memt = sc->memt;
    832        1.19  christos 			pcmhp->memh = memh;
    833        1.19  christos 			pcmhp->addr = addr;
    834        1.19  christos 			pcmhp->size = size;
    835        1.19  christos 			pcmhp->mhandle = mhandle;
    836        1.19  christos 			pcmhp->realsize = sizepg * PCIC_MEM_PAGESIZE;
    837       1.111   tsutsui 			return 0;
    838         1.2   thorpej 		}
    839         1.2   thorpej 	}
    840         1.2   thorpej 
    841       1.111   tsutsui 	return 1;
    842         1.2   thorpej }
    843         1.2   thorpej 
    844        1.89     perry void
    845       1.105       dsl pcic_chip_mem_free(pcmcia_chipset_handle_t pch, struct pcmcia_mem_handle *pcmhp)
    846         1.2   thorpej {
    847         1.2   thorpej 	struct pcic_handle *h = (struct pcic_handle *) pch;
    848       1.110   tsutsui 	struct pcic_softc *sc = device_private(h->ph_parent);
    849         1.2   thorpej 
    850        1.25      haya 	sc->subregionmask |= pcmhp->mhandle;
    851         1.2   thorpej }
    852         1.2   thorpej 
    853        1.62  jdolecek static const struct mem_map_index_st {
    854         1.2   thorpej 	int	sysmem_start_lsb;
    855         1.2   thorpej 	int	sysmem_start_msb;
    856         1.2   thorpej 	int	sysmem_stop_lsb;
    857         1.2   thorpej 	int	sysmem_stop_msb;
    858         1.2   thorpej 	int	cardmem_lsb;
    859         1.2   thorpej 	int	cardmem_msb;
    860         1.2   thorpej 	int	memenable;
    861         1.2   thorpej } mem_map_index[] = {
    862         1.2   thorpej 	{
    863         1.2   thorpej 		PCIC_SYSMEM_ADDR0_START_LSB,
    864         1.2   thorpej 		PCIC_SYSMEM_ADDR0_START_MSB,
    865         1.2   thorpej 		PCIC_SYSMEM_ADDR0_STOP_LSB,
    866         1.2   thorpej 		PCIC_SYSMEM_ADDR0_STOP_MSB,
    867         1.2   thorpej 		PCIC_CARDMEM_ADDR0_LSB,
    868         1.2   thorpej 		PCIC_CARDMEM_ADDR0_MSB,
    869         1.2   thorpej 		PCIC_ADDRWIN_ENABLE_MEM0,
    870         1.2   thorpej 	},
    871         1.2   thorpej 	{
    872         1.2   thorpej 		PCIC_SYSMEM_ADDR1_START_LSB,
    873         1.2   thorpej 		PCIC_SYSMEM_ADDR1_START_MSB,
    874         1.2   thorpej 		PCIC_SYSMEM_ADDR1_STOP_LSB,
    875         1.2   thorpej 		PCIC_SYSMEM_ADDR1_STOP_MSB,
    876         1.2   thorpej 		PCIC_CARDMEM_ADDR1_LSB,
    877         1.2   thorpej 		PCIC_CARDMEM_ADDR1_MSB,
    878         1.2   thorpej 		PCIC_ADDRWIN_ENABLE_MEM1,
    879         1.2   thorpej 	},
    880         1.2   thorpej 	{
    881         1.2   thorpej 		PCIC_SYSMEM_ADDR2_START_LSB,
    882         1.2   thorpej 		PCIC_SYSMEM_ADDR2_START_MSB,
    883         1.2   thorpej 		PCIC_SYSMEM_ADDR2_STOP_LSB,
    884         1.2   thorpej 		PCIC_SYSMEM_ADDR2_STOP_MSB,
    885         1.2   thorpej 		PCIC_CARDMEM_ADDR2_LSB,
    886         1.2   thorpej 		PCIC_CARDMEM_ADDR2_MSB,
    887         1.2   thorpej 		PCIC_ADDRWIN_ENABLE_MEM2,
    888         1.2   thorpej 	},
    889         1.2   thorpej 	{
    890         1.2   thorpej 		PCIC_SYSMEM_ADDR3_START_LSB,
    891         1.2   thorpej 		PCIC_SYSMEM_ADDR3_START_MSB,
    892         1.2   thorpej 		PCIC_SYSMEM_ADDR3_STOP_LSB,
    893         1.2   thorpej 		PCIC_SYSMEM_ADDR3_STOP_MSB,
    894         1.2   thorpej 		PCIC_CARDMEM_ADDR3_LSB,
    895         1.2   thorpej 		PCIC_CARDMEM_ADDR3_MSB,
    896         1.2   thorpej 		PCIC_ADDRWIN_ENABLE_MEM3,
    897         1.2   thorpej 	},
    898         1.2   thorpej 	{
    899         1.2   thorpej 		PCIC_SYSMEM_ADDR4_START_LSB,
    900         1.2   thorpej 		PCIC_SYSMEM_ADDR4_START_MSB,
    901         1.2   thorpej 		PCIC_SYSMEM_ADDR4_STOP_LSB,
    902         1.2   thorpej 		PCIC_SYSMEM_ADDR4_STOP_MSB,
    903         1.2   thorpej 		PCIC_CARDMEM_ADDR4_LSB,
    904         1.2   thorpej 		PCIC_CARDMEM_ADDR4_MSB,
    905         1.2   thorpej 		PCIC_ADDRWIN_ENABLE_MEM4,
    906         1.2   thorpej 	},
    907         1.2   thorpej };
    908         1.2   thorpej 
    909        1.89     perry void
    910       1.105       dsl pcic_chip_do_mem_map(struct pcic_handle *h, int win)
    911         1.2   thorpej {
    912         1.2   thorpej 	int reg;
    913        1.28      joda 	int kind = h->mem[win].kind & ~PCMCIA_WIDTH_MEM_MASK;
    914        1.35     enami 	int mem8 =
    915        1.47    chopps 	    (h->mem[win].kind & PCMCIA_WIDTH_MEM_MASK) == PCMCIA_WIDTH_MEM8
    916        1.47    chopps 	    || (kind == PCMCIA_MEM_ATTR);
    917        1.28      joda 
    918        1.33    chopps 	DPRINTF(("mem8 %d\n", mem8));
    919        1.33    chopps 	/* mem8 = 1; */
    920        1.33    chopps 
    921         1.2   thorpej 	pcic_write(h, mem_map_index[win].sysmem_start_lsb,
    922         1.2   thorpej 	    (h->mem[win].addr >> PCIC_SYSMEM_ADDRX_SHIFT) & 0xff);
    923         1.2   thorpej 	pcic_write(h, mem_map_index[win].sysmem_start_msb,
    924         1.2   thorpej 	    ((h->mem[win].addr >> (PCIC_SYSMEM_ADDRX_SHIFT + 8)) &
    925        1.43      joda 	    PCIC_SYSMEM_ADDRX_START_MSB_ADDR_MASK) |
    926        1.44     enami 	    (mem8 ? 0 : PCIC_SYSMEM_ADDRX_START_MSB_DATASIZE_16BIT));
    927         1.2   thorpej 
    928         1.2   thorpej 	pcic_write(h, mem_map_index[win].sysmem_stop_lsb,
    929         1.2   thorpej 	    ((h->mem[win].addr + h->mem[win].size) >>
    930         1.2   thorpej 	    PCIC_SYSMEM_ADDRX_SHIFT) & 0xff);
    931         1.2   thorpej 	pcic_write(h, mem_map_index[win].sysmem_stop_msb,
    932         1.2   thorpej 	    (((h->mem[win].addr + h->mem[win].size) >>
    933         1.2   thorpej 	    (PCIC_SYSMEM_ADDRX_SHIFT + 8)) &
    934         1.2   thorpej 	    PCIC_SYSMEM_ADDRX_STOP_MSB_ADDR_MASK) |
    935         1.2   thorpej 	    PCIC_SYSMEM_ADDRX_STOP_MSB_WAIT2);
    936         1.2   thorpej 
    937         1.2   thorpej 	pcic_write(h, mem_map_index[win].cardmem_lsb,
    938         1.2   thorpej 	    (h->mem[win].offset >> PCIC_CARDMEM_ADDRX_SHIFT) & 0xff);
    939         1.2   thorpej 	pcic_write(h, mem_map_index[win].cardmem_msb,
    940         1.2   thorpej 	    ((h->mem[win].offset >> (PCIC_CARDMEM_ADDRX_SHIFT + 8)) &
    941         1.2   thorpej 	    PCIC_CARDMEM_ADDRX_MSB_ADDR_MASK) |
    942        1.28      joda 	    ((kind == PCMCIA_MEM_ATTR) ?
    943         1.2   thorpej 	    PCIC_CARDMEM_ADDRX_MSB_REGACTIVE_ATTR : 0));
    944         1.2   thorpej 
    945         1.2   thorpej 	reg = pcic_read(h, PCIC_ADDRWIN_ENABLE);
    946        1.43      joda 	reg |= (mem_map_index[win].memenable | PCIC_ADDRWIN_ENABLE_MEMCS16);
    947         1.2   thorpej 	pcic_write(h, PCIC_ADDRWIN_ENABLE, reg);
    948        1.21      marc 
    949        1.21      marc 	delay(100);
    950         1.2   thorpej 
    951         1.2   thorpej #ifdef PCICDEBUG
    952         1.2   thorpej 	{
    953         1.2   thorpej 		int r1, r2, r3, r4, r5, r6;
    954         1.2   thorpej 
    955         1.2   thorpej 		r1 = pcic_read(h, mem_map_index[win].sysmem_start_msb);
    956         1.2   thorpej 		r2 = pcic_read(h, mem_map_index[win].sysmem_start_lsb);
    957         1.2   thorpej 		r3 = pcic_read(h, mem_map_index[win].sysmem_stop_msb);
    958         1.2   thorpej 		r4 = pcic_read(h, mem_map_index[win].sysmem_stop_lsb);
    959         1.2   thorpej 		r5 = pcic_read(h, mem_map_index[win].cardmem_msb);
    960         1.2   thorpej 		r6 = pcic_read(h, mem_map_index[win].cardmem_lsb);
    961         1.2   thorpej 
    962         1.2   thorpej 		DPRINTF(("pcic_chip_do_mem_map window %d: %02x%02x %02x%02x "
    963         1.2   thorpej 		    "%02x%02x\n", win, r1, r2, r3, r4, r5, r6));
    964         1.2   thorpej 	}
    965         1.2   thorpej #endif
    966         1.2   thorpej }
    967         1.2   thorpej 
    968        1.89     perry int
    969       1.111   tsutsui pcic_chip_mem_map(pcmcia_chipset_handle_t pch, int kind, bus_addr_t card_addr,
    970       1.111   tsutsui     bus_size_t size, struct pcmcia_mem_handle *pcmhp, bus_size_t *offsetp,
    971       1.111   tsutsui     int *windowp)
    972         1.2   thorpej {
    973         1.2   thorpej 	struct pcic_handle *h = (struct pcic_handle *) pch;
    974         1.2   thorpej 	bus_addr_t busaddr;
    975         1.2   thorpej 	long card_offset;
    976         1.2   thorpej 	int i, win;
    977         1.2   thorpej 
    978         1.2   thorpej 	win = -1;
    979         1.2   thorpej 	for (i = 0; i < (sizeof(mem_map_index) / sizeof(mem_map_index[0]));
    980         1.2   thorpej 	    i++) {
    981         1.2   thorpej 		if ((h->memalloc & (1 << i)) == 0) {
    982         1.2   thorpej 			win = i;
    983         1.2   thorpej 			h->memalloc |= (1 << i);
    984         1.2   thorpej 			break;
    985         1.2   thorpej 		}
    986         1.2   thorpej 	}
    987         1.2   thorpej 
    988         1.2   thorpej 	if (win == -1)
    989       1.111   tsutsui 		return 1;
    990         1.2   thorpej 
    991         1.2   thorpej 	*windowp = win;
    992         1.2   thorpej 
    993         1.2   thorpej 	/* XXX this is pretty gross */
    994         1.2   thorpej 
    995       1.112    dyoung {
    996       1.112    dyoung 	struct pcic_softc *sc = device_private(h->ph_parent);
    997       1.113    dyoung 	if (!bus_space_is_equal(sc->memt, pcmhp->memt))
    998         1.2   thorpej 		panic("pcic_chip_mem_map memt is bogus");
    999       1.112    dyoung }
   1000         1.2   thorpej 
   1001         1.2   thorpej 	busaddr = pcmhp->addr;
   1002         1.2   thorpej 
   1003         1.2   thorpej 	/*
   1004         1.2   thorpej 	 * compute the address offset to the pcmcia address space for the
   1005         1.2   thorpej 	 * pcic.  this is intentionally signed.  The masks and shifts below
   1006         1.2   thorpej 	 * will cause TRT to happen in the pcic registers.  Deal with making
   1007         1.2   thorpej 	 * sure the address is aligned, and return the alignment offset.
   1008         1.2   thorpej 	 */
   1009         1.2   thorpej 
   1010         1.2   thorpej 	*offsetp = card_addr % PCIC_MEM_ALIGN;
   1011         1.2   thorpej 	card_addr -= *offsetp;
   1012         1.2   thorpej 
   1013         1.2   thorpej 	DPRINTF(("pcic_chip_mem_map window %d bus %lx+%lx+%lx at card addr "
   1014         1.2   thorpej 	    "%lx\n", win, (u_long) busaddr, (u_long) * offsetp, (u_long) size,
   1015         1.2   thorpej 	    (u_long) card_addr));
   1016         1.2   thorpej 
   1017         1.2   thorpej 	/*
   1018         1.2   thorpej 	 * include the offset in the size, and decrement size by one, since
   1019         1.2   thorpej 	 * the hw wants start/stop
   1020         1.2   thorpej 	 */
   1021         1.2   thorpej 	size += *offsetp - 1;
   1022         1.2   thorpej 
   1023         1.2   thorpej 	card_offset = (((long) card_addr) - ((long) busaddr));
   1024         1.2   thorpej 
   1025         1.2   thorpej 	h->mem[win].addr = busaddr;
   1026         1.2   thorpej 	h->mem[win].size = size;
   1027         1.2   thorpej 	h->mem[win].offset = card_offset;
   1028         1.2   thorpej 	h->mem[win].kind = kind;
   1029         1.2   thorpej 
   1030         1.2   thorpej 	pcic_chip_do_mem_map(h, win);
   1031         1.2   thorpej 
   1032       1.111   tsutsui 	return 0;
   1033         1.2   thorpej }
   1034         1.2   thorpej 
   1035        1.89     perry void
   1036       1.105       dsl pcic_chip_mem_unmap(pcmcia_chipset_handle_t pch, int window)
   1037         1.2   thorpej {
   1038         1.2   thorpej 	struct pcic_handle *h = (struct pcic_handle *) pch;
   1039         1.2   thorpej 	int reg;
   1040         1.2   thorpej 
   1041         1.2   thorpej 	if (window >= (sizeof(mem_map_index) / sizeof(mem_map_index[0])))
   1042         1.2   thorpej 		panic("pcic_chip_mem_unmap: window out of range");
   1043         1.2   thorpej 
   1044         1.2   thorpej 	reg = pcic_read(h, PCIC_ADDRWIN_ENABLE);
   1045         1.2   thorpej 	reg &= ~mem_map_index[window].memenable;
   1046         1.2   thorpej 	pcic_write(h, PCIC_ADDRWIN_ENABLE, reg);
   1047         1.2   thorpej 
   1048         1.2   thorpej 	h->memalloc &= ~(1 << window);
   1049         1.2   thorpej }
   1050         1.2   thorpej 
   1051        1.89     perry int
   1052       1.111   tsutsui pcic_chip_io_alloc(pcmcia_chipset_handle_t pch, bus_addr_t start,
   1053       1.111   tsutsui     bus_size_t size, bus_size_t align, struct pcmcia_io_handle *pcihp)
   1054         1.2   thorpej {
   1055         1.2   thorpej 	struct pcic_handle *h = (struct pcic_handle *) pch;
   1056         1.2   thorpej 	bus_space_tag_t iot;
   1057         1.2   thorpej 	bus_space_handle_t ioh;
   1058         1.2   thorpej 	bus_addr_t ioaddr;
   1059         1.2   thorpej 	int flags = 0;
   1060       1.110   tsutsui 	struct pcic_softc *sc = device_private(h->ph_parent);
   1061         1.2   thorpej 
   1062         1.2   thorpej 	/*
   1063         1.2   thorpej 	 * Allocate some arbitrary I/O space.
   1064         1.2   thorpej 	 */
   1065         1.2   thorpej 
   1066        1.25      haya 	iot = sc->iot;
   1067         1.2   thorpej 
   1068         1.2   thorpej 	if (start) {
   1069         1.2   thorpej 		ioaddr = start;
   1070         1.2   thorpej 		if (bus_space_map(iot, start, size, 0, &ioh))
   1071       1.111   tsutsui 			return 1;
   1072         1.2   thorpej 		DPRINTF(("pcic_chip_io_alloc map port %lx+%lx\n",
   1073         1.2   thorpej 		    (u_long) ioaddr, (u_long) size));
   1074         1.2   thorpej 	} else {
   1075         1.2   thorpej 		flags |= PCMCIA_IO_ALLOCATED;
   1076        1.25      haya 		if (bus_space_alloc(iot, sc->iobase,
   1077        1.25      haya 		    sc->iobase + sc->iosize, size, align, 0, 0,
   1078         1.2   thorpej 		    &ioaddr, &ioh))
   1079       1.111   tsutsui 			return 1;
   1080         1.2   thorpej 		DPRINTF(("pcic_chip_io_alloc alloc port %lx+%lx\n",
   1081         1.2   thorpej 		    (u_long) ioaddr, (u_long) size));
   1082         1.2   thorpej 	}
   1083         1.2   thorpej 
   1084         1.2   thorpej 	pcihp->iot = iot;
   1085         1.2   thorpej 	pcihp->ioh = ioh;
   1086         1.2   thorpej 	pcihp->addr = ioaddr;
   1087         1.2   thorpej 	pcihp->size = size;
   1088         1.2   thorpej 	pcihp->flags = flags;
   1089         1.2   thorpej 
   1090       1.111   tsutsui 	return 0;
   1091         1.2   thorpej }
   1092         1.2   thorpej 
   1093        1.89     perry void
   1094       1.111   tsutsui pcic_chip_io_free(pcmcia_chipset_handle_t pch, struct pcmcia_io_handle *pcihp)
   1095         1.2   thorpej {
   1096         1.2   thorpej 	bus_space_tag_t iot = pcihp->iot;
   1097         1.2   thorpej 	bus_space_handle_t ioh = pcihp->ioh;
   1098         1.2   thorpej 	bus_size_t size = pcihp->size;
   1099         1.2   thorpej 
   1100         1.2   thorpej 	if (pcihp->flags & PCMCIA_IO_ALLOCATED)
   1101         1.2   thorpej 		bus_space_free(iot, ioh, size);
   1102         1.2   thorpej 	else
   1103         1.2   thorpej 		bus_space_unmap(iot, ioh, size);
   1104         1.2   thorpej }
   1105         1.2   thorpej 
   1106         1.2   thorpej 
   1107        1.62  jdolecek static const struct io_map_index_st {
   1108         1.2   thorpej 	int	start_lsb;
   1109         1.2   thorpej 	int	start_msb;
   1110         1.2   thorpej 	int	stop_lsb;
   1111         1.2   thorpej 	int	stop_msb;
   1112         1.2   thorpej 	int	ioenable;
   1113         1.2   thorpej 	int	ioctlmask;
   1114         1.2   thorpej 	int	ioctlbits[3];		/* indexed by PCMCIA_WIDTH_* */
   1115         1.2   thorpej }               io_map_index[] = {
   1116         1.2   thorpej 	{
   1117         1.2   thorpej 		PCIC_IOADDR0_START_LSB,
   1118         1.2   thorpej 		PCIC_IOADDR0_START_MSB,
   1119         1.2   thorpej 		PCIC_IOADDR0_STOP_LSB,
   1120         1.2   thorpej 		PCIC_IOADDR0_STOP_MSB,
   1121         1.2   thorpej 		PCIC_ADDRWIN_ENABLE_IO0,
   1122         1.2   thorpej 		PCIC_IOCTL_IO0_WAITSTATE | PCIC_IOCTL_IO0_ZEROWAIT |
   1123         1.2   thorpej 		PCIC_IOCTL_IO0_IOCS16SRC_MASK | PCIC_IOCTL_IO0_DATASIZE_MASK,
   1124         1.2   thorpej 		{
   1125         1.2   thorpej 			PCIC_IOCTL_IO0_IOCS16SRC_CARD,
   1126         1.6     enami 			PCIC_IOCTL_IO0_IOCS16SRC_DATASIZE |
   1127         1.6     enami 			    PCIC_IOCTL_IO0_DATASIZE_8BIT,
   1128         1.6     enami 			PCIC_IOCTL_IO0_IOCS16SRC_DATASIZE |
   1129         1.6     enami 			    PCIC_IOCTL_IO0_DATASIZE_16BIT,
   1130         1.2   thorpej 		},
   1131         1.2   thorpej 	},
   1132         1.2   thorpej 	{
   1133         1.2   thorpej 		PCIC_IOADDR1_START_LSB,
   1134         1.2   thorpej 		PCIC_IOADDR1_START_MSB,
   1135         1.2   thorpej 		PCIC_IOADDR1_STOP_LSB,
   1136         1.2   thorpej 		PCIC_IOADDR1_STOP_MSB,
   1137         1.2   thorpej 		PCIC_ADDRWIN_ENABLE_IO1,
   1138         1.2   thorpej 		PCIC_IOCTL_IO1_WAITSTATE | PCIC_IOCTL_IO1_ZEROWAIT |
   1139         1.2   thorpej 		PCIC_IOCTL_IO1_IOCS16SRC_MASK | PCIC_IOCTL_IO1_DATASIZE_MASK,
   1140         1.2   thorpej 		{
   1141         1.2   thorpej 			PCIC_IOCTL_IO1_IOCS16SRC_CARD,
   1142         1.2   thorpej 			PCIC_IOCTL_IO1_IOCS16SRC_DATASIZE |
   1143         1.2   thorpej 			    PCIC_IOCTL_IO1_DATASIZE_8BIT,
   1144         1.2   thorpej 			PCIC_IOCTL_IO1_IOCS16SRC_DATASIZE |
   1145         1.2   thorpej 			    PCIC_IOCTL_IO1_DATASIZE_16BIT,
   1146         1.2   thorpej 		},
   1147         1.2   thorpej 	},
   1148         1.2   thorpej };
   1149         1.2   thorpej 
   1150        1.89     perry void
   1151       1.105       dsl pcic_chip_do_io_map(struct pcic_handle *h, int win)
   1152         1.2   thorpej {
   1153         1.2   thorpej 	int reg;
   1154         1.2   thorpej 
   1155         1.2   thorpej 	DPRINTF(("pcic_chip_do_io_map win %d addr %lx size %lx width %d\n",
   1156         1.2   thorpej 	    win, (long) h->io[win].addr, (long) h->io[win].size,
   1157         1.2   thorpej 	    h->io[win].width * 8));
   1158         1.2   thorpej 
   1159         1.2   thorpej 	pcic_write(h, io_map_index[win].start_lsb, h->io[win].addr & 0xff);
   1160         1.2   thorpej 	pcic_write(h, io_map_index[win].start_msb,
   1161         1.2   thorpej 	    (h->io[win].addr >> 8) & 0xff);
   1162         1.2   thorpej 
   1163         1.2   thorpej 	pcic_write(h, io_map_index[win].stop_lsb,
   1164         1.2   thorpej 	    (h->io[win].addr + h->io[win].size - 1) & 0xff);
   1165         1.2   thorpej 	pcic_write(h, io_map_index[win].stop_msb,
   1166         1.2   thorpej 	    ((h->io[win].addr + h->io[win].size - 1) >> 8) & 0xff);
   1167         1.2   thorpej 
   1168         1.2   thorpej 	reg = pcic_read(h, PCIC_IOCTL);
   1169         1.2   thorpej 	reg &= ~io_map_index[win].ioctlmask;
   1170         1.2   thorpej 	reg |= io_map_index[win].ioctlbits[h->io[win].width];
   1171         1.2   thorpej 	pcic_write(h, PCIC_IOCTL, reg);
   1172         1.2   thorpej 
   1173         1.2   thorpej 	reg = pcic_read(h, PCIC_ADDRWIN_ENABLE);
   1174         1.2   thorpej 	reg |= io_map_index[win].ioenable;
   1175         1.2   thorpej 	pcic_write(h, PCIC_ADDRWIN_ENABLE, reg);
   1176         1.2   thorpej }
   1177         1.2   thorpej 
   1178        1.89     perry int
   1179       1.111   tsutsui pcic_chip_io_map(pcmcia_chipset_handle_t pch, int width, bus_addr_t offset,
   1180       1.111   tsutsui     bus_size_t size, struct pcmcia_io_handle *pcihp, int *windowp)
   1181         1.2   thorpej {
   1182         1.2   thorpej 	struct pcic_handle *h = (struct pcic_handle *) pch;
   1183         1.2   thorpej 	bus_addr_t ioaddr = pcihp->addr + offset;
   1184         1.4     enami 	int i, win;
   1185         1.4     enami #ifdef PCICDEBUG
   1186        1.90  christos 	static const char *width_names[] = { "auto", "io8", "io16" };
   1187         1.4     enami #endif
   1188       1.110   tsutsui 	struct pcic_softc *sc = device_private(h->ph_parent);
   1189         1.2   thorpej 
   1190         1.2   thorpej 	/* XXX Sanity check offset/size. */
   1191         1.2   thorpej 
   1192         1.2   thorpej 	win = -1;
   1193         1.2   thorpej 	for (i = 0; i < (sizeof(io_map_index) / sizeof(io_map_index[0])); i++) {
   1194         1.2   thorpej 		if ((h->ioalloc & (1 << i)) == 0) {
   1195         1.2   thorpej 			win = i;
   1196         1.2   thorpej 			h->ioalloc |= (1 << i);
   1197         1.2   thorpej 			break;
   1198         1.2   thorpej 		}
   1199         1.2   thorpej 	}
   1200         1.2   thorpej 
   1201         1.2   thorpej 	if (win == -1)
   1202       1.111   tsutsui 		return 1;
   1203         1.2   thorpej 
   1204         1.2   thorpej 	*windowp = win;
   1205         1.2   thorpej 
   1206         1.2   thorpej 	/* XXX this is pretty gross */
   1207         1.2   thorpej 
   1208       1.113    dyoung 	if (!bus_space_is_equal(sc->iot, pcihp->iot))
   1209         1.2   thorpej 		panic("pcic_chip_io_map iot is bogus");
   1210         1.2   thorpej 
   1211         1.2   thorpej 	DPRINTF(("pcic_chip_io_map window %d %s port %lx+%lx\n",
   1212       1.111   tsutsui 	    win, width_names[width], (u_long) ioaddr, (u_long) size));
   1213         1.2   thorpej 
   1214         1.2   thorpej 	/* XXX wtf is this doing here? */
   1215         1.2   thorpej 
   1216       1.115       chs 	printf("%s: port 0x%lx", device_xname(sc->dev), (u_long) ioaddr);
   1217         1.2   thorpej 	if (size > 1)
   1218         1.2   thorpej 		printf("-0x%lx", (u_long) ioaddr + (u_long) size - 1);
   1219        1.77  christos 	printf("\n");
   1220         1.2   thorpej 
   1221         1.2   thorpej 	h->io[win].addr = ioaddr;
   1222         1.2   thorpej 	h->io[win].size = size;
   1223         1.2   thorpej 	h->io[win].width = width;
   1224         1.2   thorpej 
   1225         1.2   thorpej 	pcic_chip_do_io_map(h, win);
   1226         1.2   thorpej 
   1227       1.111   tsutsui 	return 0;
   1228         1.2   thorpej }
   1229         1.2   thorpej 
   1230        1.89     perry void
   1231       1.105       dsl pcic_chip_io_unmap(pcmcia_chipset_handle_t pch, int window)
   1232         1.2   thorpej {
   1233         1.2   thorpej 	struct pcic_handle *h = (struct pcic_handle *) pch;
   1234         1.2   thorpej 	int reg;
   1235         1.2   thorpej 
   1236         1.2   thorpej 	if (window >= (sizeof(io_map_index) / sizeof(io_map_index[0])))
   1237         1.2   thorpej 		panic("pcic_chip_io_unmap: window out of range");
   1238         1.2   thorpej 
   1239         1.2   thorpej 	reg = pcic_read(h, PCIC_ADDRWIN_ENABLE);
   1240         1.2   thorpej 	reg &= ~io_map_index[window].ioenable;
   1241         1.2   thorpej 	pcic_write(h, PCIC_ADDRWIN_ENABLE, reg);
   1242         1.2   thorpej 
   1243         1.2   thorpej 	h->ioalloc &= ~(1 << window);
   1244         1.8      marc }
   1245         1.8      marc 
   1246        1.83   mycroft static int
   1247       1.105       dsl pcic_wait_ready(struct pcic_handle *h)
   1248         1.8      marc {
   1249       1.109   tsutsui 	uint8_t stat;
   1250         1.8      marc 	int i;
   1251         1.8      marc 
   1252        1.31    chopps 	/* wait an initial 10ms for quick cards */
   1253        1.83   mycroft 	stat = pcic_read(h, PCIC_IF_STATUS);
   1254        1.83   mycroft 	if (stat & PCIC_IF_STATUS_READY)
   1255       1.111   tsutsui 		return 0;
   1256        1.36     enami 	pcic_delay(h, 10, "pccwr0");
   1257        1.31    chopps 	for (i = 0; i < 50; i++) {
   1258        1.83   mycroft 		stat = pcic_read(h, PCIC_IF_STATUS);
   1259        1.83   mycroft 		if (stat & PCIC_IF_STATUS_READY)
   1260       1.111   tsutsui 			return 0;
   1261        1.83   mycroft 		if ((stat & PCIC_IF_STATUS_CARDDETECT_MASK) !=
   1262        1.83   mycroft 		    PCIC_IF_STATUS_CARDDETECT_PRESENT)
   1263       1.111   tsutsui 			return ENXIO;
   1264        1.31    chopps 		/* wait .1s (100ms) each iteration now */
   1265        1.36     enami 		pcic_delay(h, 100, "pccwr1");
   1266         1.8      marc 	}
   1267         1.8      marc 
   1268        1.83   mycroft 	printf("pcic_wait_ready: ready never happened, status=%02x\n", stat);
   1269       1.111   tsutsui 	return EWOULDBLOCK;
   1270         1.2   thorpej }
   1271         1.2   thorpej 
   1272        1.30     enami /*
   1273        1.30     enami  * Perform long (msec order) delay.
   1274        1.89     perry  */
   1275        1.30     enami static void
   1276       1.106       dsl pcic_delay(struct pcic_handle *h, int timo, const char *wmesg)
   1277       1.106       dsl 	/* timo:			 in ms.  must not be zero */
   1278        1.30     enami {
   1279        1.30     enami 
   1280        1.30     enami #ifdef DIAGNOSTIC
   1281        1.83   mycroft 	if (timo <= 0)
   1282        1.83   mycroft 		panic("pcic_delay: called with timeout %d", timo);
   1283        1.83   mycroft 	if (!curlwp)
   1284        1.83   mycroft 		panic("pcic_delay: called in interrupt context");
   1285        1.83   mycroft 	if (!h->event_thread)
   1286        1.83   mycroft 		panic("pcic_delay: no event thread");
   1287        1.30     enami #endif
   1288        1.48       dbj 	DPRINTF(("pcic_delay: \"%s\" %p, sleep %d ms\n",
   1289        1.49     enami 	    wmesg, h->event_thread, timo));
   1290       1.104     hauke 	if (doing_shutdown)
   1291       1.104     hauke 		delay(timo * 1000);
   1292       1.104     hauke 	else
   1293       1.104     hauke 		tsleep(pcic_delay, PWAIT, wmesg,
   1294       1.104     hauke 		    roundup(timo * hz, 1000) / 1000);
   1295        1.30     enami }
   1296        1.30     enami 
   1297         1.2   thorpej void
   1298       1.105       dsl pcic_chip_socket_enable(pcmcia_chipset_handle_t pch)
   1299         1.2   thorpej {
   1300         1.2   thorpej 	struct pcic_handle *h = (struct pcic_handle *) pch;
   1301        1.83   mycroft 	int win;
   1302       1.109   tsutsui 	uint8_t power, intr;
   1303        1.83   mycroft #ifdef DIAGNOSTIC
   1304        1.34    chopps 	int reg;
   1305        1.34    chopps #endif
   1306         1.2   thorpej 
   1307        1.41    chopps #ifdef DIAGNOSTIC
   1308        1.41    chopps 	if (h->flags & PCIC_FLAG_ENABLED)
   1309        1.61   mycroft 		printf("pcic_chip_socket_enable: enabling twice\n");
   1310        1.41    chopps #endif
   1311        1.41    chopps 
   1312        1.85   mycroft 	/* disable interrupts; assert RESET */
   1313        1.39     enami 	intr = pcic_read(h, PCIC_INTR);
   1314        1.86   mycroft 	intr &= PCIC_INTR_ENABLE;
   1315        1.34    chopps 	pcic_write(h, PCIC_INTR, intr);
   1316         1.2   thorpej 
   1317        1.82   mycroft 	/* zero out the address windows */
   1318        1.82   mycroft 	pcic_write(h, PCIC_ADDRWIN_ENABLE, 0);
   1319        1.82   mycroft 
   1320        1.85   mycroft 	/* power off; assert output enable bit */
   1321        1.85   mycroft 	power = PCIC_PWRCTL_OE;
   1322        1.83   mycroft 	pcic_write(h, PCIC_PWRCTL, power);
   1323        1.83   mycroft 
   1324        1.69  takemura 	/*
   1325        1.69  takemura 	 * power hack for RICOH RF5C[23]96
   1326        1.69  takemura 	 */
   1327       1.111   tsutsui 	switch (h->vendor) {
   1328        1.69  takemura 	case PCIC_VENDOR_RICOH_5C296:
   1329        1.69  takemura 	case PCIC_VENDOR_RICOH_5C396:
   1330       1.111   tsutsui 	    {
   1331        1.76   mycroft 		int regtmp;
   1332        1.69  takemura 		regtmp = pcic_read(h, PCIC_RICOH_REG_MCR2);
   1333        1.76   mycroft #ifdef RICOH_POWER_HACK
   1334        1.76   mycroft 		regtmp |= PCIC_RICOH_MCR2_VCC_DIRECT;
   1335        1.76   mycroft #else
   1336       1.111   tsutsui 		regtmp &=
   1337       1.111   tsutsui 		    ~(PCIC_RICOH_MCR2_VCC_DIRECT|PCIC_RICOH_MCR2_VCC_SEL_3V);
   1338        1.76   mycroft #endif
   1339        1.69  takemura 		pcic_write(h, PCIC_RICOH_REG_MCR2, regtmp);
   1340       1.111   tsutsui 	    }
   1341        1.69  takemura 		break;
   1342        1.69  takemura 	default:
   1343        1.69  takemura 		break;
   1344        1.69  takemura 	}
   1345         1.9     enami 
   1346        1.22   mycroft #ifdef VADEM_POWER_HACK
   1347        1.25      haya 	bus_space_write_1(sc->iot, sc->ioh, PCIC_REG_INDEX, 0x0e);
   1348        1.25      haya 	bus_space_write_1(sc->iot, sc->ioh, PCIC_REG_INDEX, 0x37);
   1349        1.22   mycroft 	printf("prcr = %02x\n", pcic_read(h, 0x02));
   1350        1.22   mycroft 	printf("cvsr = %02x\n", pcic_read(h, 0x2f));
   1351        1.22   mycroft 	printf("DANGER WILL ROBINSON!  Changing voltage select!\n");
   1352        1.22   mycroft 	pcic_write(h, 0x2f, pcic_read(h, 0x2f) & ~0x03);
   1353        1.22   mycroft 	printf("cvsr = %02x\n", pcic_read(h, 0x2f));
   1354        1.22   mycroft #endif
   1355        1.83   mycroft 
   1356         1.2   thorpej 	/* power up the socket */
   1357        1.83   mycroft 	power |= PCIC_PWRCTL_PWR_ENABLE | PCIC_PWRCTL_VPP1_VCC;
   1358        1.83   mycroft 	pcic_write(h, PCIC_PWRCTL, power);
   1359         1.9     enami 
   1360         1.9     enami 	/*
   1361        1.85   mycroft 	 * Table 4-18 and figure 4-6 of the PC Card specifiction say:
   1362        1.85   mycroft 	 * Vcc Rising Time (Tpr) = 100ms
   1363        1.85   mycroft 	 * RESET Width (Th (Hi-z RESET)) = 1ms
   1364        1.85   mycroft 	 * RESET Width (Tw (RESET)) = 10us
   1365        1.12   msaitoh 	 *
   1366        1.12   msaitoh 	 * some machines require some more time to be settled
   1367        1.85   mycroft 	 * (100ms is added here).
   1368         1.9     enami 	 */
   1369        1.85   mycroft 	pcic_delay(h, 200 + 1, "pccen1");
   1370        1.38    chopps 
   1371        1.85   mycroft 	/* negate RESET */
   1372        1.85   mycroft 	intr |= PCIC_INTR_RESET;
   1373        1.85   mycroft 	pcic_write(h, PCIC_INTR, intr);
   1374         1.9     enami 
   1375         1.9     enami 	/*
   1376        1.85   mycroft 	 * RESET Setup Time (Tsu (RESET)) = 20ms
   1377         1.9     enami 	 */
   1378        1.30     enami 	pcic_delay(h, 20, "pccen2");
   1379         1.2   thorpej 
   1380        1.83   mycroft #ifdef DIAGNOSTIC
   1381        1.68    simonb 	reg = pcic_read(h, PCIC_IF_STATUS);
   1382        1.83   mycroft 	if ((reg & PCIC_IF_STATUS_POWERACTIVE) == 0)
   1383        1.83   mycroft 		printf("pcic_chip_socket_enable: no power, status=%x\n", reg);
   1384        1.68    simonb #endif
   1385        1.83   mycroft 
   1386        1.83   mycroft 	/* wait for the chip to finish initializing */
   1387        1.83   mycroft 	if (pcic_wait_ready(h)) {
   1388        1.83   mycroft 		/* XXX return a failure status?? */
   1389        1.83   mycroft 		pcic_write(h, PCIC_PWRCTL, 0);
   1390        1.83   mycroft 		return;
   1391        1.20   msaitoh 	}
   1392         1.2   thorpej 
   1393         1.2   thorpej 	/* reinstall all the memory and io mappings */
   1394         1.2   thorpej 	for (win = 0; win < PCIC_MEM_WINS; win++)
   1395         1.2   thorpej 		if (h->memalloc & (1 << win))
   1396         1.2   thorpej 			pcic_chip_do_mem_map(h, win);
   1397         1.2   thorpej 	for (win = 0; win < PCIC_IO_WINS; win++)
   1398         1.2   thorpej 		if (h->ioalloc & (1 << win))
   1399         1.2   thorpej 			pcic_chip_do_io_map(h, win);
   1400        1.34    chopps 
   1401        1.41    chopps 	h->flags |= PCIC_FLAG_ENABLED;
   1402         1.2   thorpej }
   1403         1.2   thorpej 
   1404         1.2   thorpej void
   1405       1.105       dsl pcic_chip_socket_disable(pcmcia_chipset_handle_t pch)
   1406         1.2   thorpej {
   1407         1.2   thorpej 	struct pcic_handle *h = (struct pcic_handle *) pch;
   1408       1.109   tsutsui 	uint8_t intr;
   1409         1.2   thorpej 
   1410         1.2   thorpej 	DPRINTF(("pcic_chip_socket_disable\n"));
   1411        1.38    chopps 
   1412        1.85   mycroft 	/* disable interrupts; assert RESET */
   1413        1.39     enami 	intr = pcic_read(h, PCIC_INTR);
   1414        1.86   mycroft 	intr &= PCIC_INTR_ENABLE;
   1415        1.38    chopps 	pcic_write(h, PCIC_INTR, intr);
   1416         1.2   thorpej 
   1417        1.81   mycroft 	/* zero out the address windows */
   1418        1.81   mycroft 	pcic_write(h, PCIC_ADDRWIN_ENABLE, 0);
   1419        1.81   mycroft 
   1420        1.83   mycroft 	/* disable socket: negate output enable bit and power off */
   1421         1.2   thorpej 	pcic_write(h, PCIC_PWRCTL, 0);
   1422        1.52   mycroft 
   1423        1.85   mycroft 	/*
   1424        1.85   mycroft 	 * Vcc Falling Time (Tpf) = 300ms
   1425        1.85   mycroft 	 */
   1426        1.83   mycroft 	pcic_delay(h, 300, "pccwr1");
   1427        1.83   mycroft 
   1428        1.41    chopps 	h->flags &= ~PCIC_FLAG_ENABLED;
   1429        1.25      haya }
   1430        1.25      haya 
   1431        1.80   mycroft void
   1432       1.105       dsl pcic_chip_socket_settype(pcmcia_chipset_handle_t pch, int type)
   1433        1.80   mycroft {
   1434        1.80   mycroft 	struct pcic_handle *h = (struct pcic_handle *) pch;
   1435        1.80   mycroft 	int intr;
   1436        1.80   mycroft 
   1437        1.80   mycroft 	intr = pcic_read(h, PCIC_INTR);
   1438        1.81   mycroft 	intr &= ~(PCIC_INTR_IRQ_MASK | PCIC_INTR_CARDTYPE_MASK);
   1439        1.80   mycroft 	if (type == PCMCIA_IFTYPE_IO) {
   1440        1.80   mycroft 		intr |= PCIC_INTR_CARDTYPE_IO;
   1441        1.80   mycroft 		intr |= h->ih_irq << PCIC_INTR_IRQ_SHIFT;
   1442        1.80   mycroft 	} else
   1443        1.80   mycroft 		intr |= PCIC_INTR_CARDTYPE_MEM;
   1444        1.80   mycroft 	pcic_write(h, PCIC_INTR, intr);
   1445        1.80   mycroft 
   1446        1.80   mycroft 	DPRINTF(("%s: pcic_chip_socket_settype %02x type %s %02x\n",
   1447       1.102    cegger 	    device_xname(h->ph_parent), h->sock,
   1448        1.80   mycroft 	    ((type == PCMCIA_IFTYPE_IO) ? "io" : "mem"), intr));
   1449        1.80   mycroft }
   1450        1.80   mycroft 
   1451       1.109   tsutsui static uint8_t
   1452       1.105       dsl st_pcic_read(struct pcic_handle *h, int idx)
   1453        1.25      haya {
   1454        1.35     enami 
   1455        1.27  sommerfe 	if (idx != -1)
   1456        1.27  sommerfe 		bus_space_write_1(h->ph_bus_t, h->ph_bus_h, PCIC_REG_INDEX,
   1457        1.27  sommerfe 		    h->sock + idx);
   1458       1.111   tsutsui 	return bus_space_read_1(h->ph_bus_t, h->ph_bus_h, PCIC_REG_DATA);
   1459        1.25      haya }
   1460        1.25      haya 
   1461        1.25      haya static void
   1462       1.109   tsutsui st_pcic_write(struct pcic_handle *h, int idx, uint8_t data)
   1463        1.27  sommerfe {
   1464        1.35     enami 
   1465        1.27  sommerfe 	if (idx != -1)
   1466        1.27  sommerfe 		bus_space_write_1(h->ph_bus_t, h->ph_bus_h, PCIC_REG_INDEX,
   1467        1.27  sommerfe 		    h->sock + idx);
   1468        1.27  sommerfe 	bus_space_write_1(h->ph_bus_t, h->ph_bus_h, PCIC_REG_DATA, data);
   1469         1.2   thorpej }
   1470