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