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