Home | History | Annotate | Line # | Download | only in dev
ipaq_pcic.c revision 1.22.8.1
      1  1.22.8.1  thorpej /*      $NetBSD: ipaq_pcic.c,v 1.22.8.1 2021/08/04 01:08:47 thorpej Exp $        */
      2       1.1   ichiro 
      3       1.1   ichiro /*-
      4       1.1   ichiro  * Copyright (c) 2001 The NetBSD Foundation, Inc.
      5       1.1   ichiro  * All rights reserved.
      6       1.1   ichiro  *
      7       1.1   ichiro  * This code is derived from software contributed to The NetBSD Foundation
      8       1.1   ichiro  * by Ichiro FUKUHARA (ichiro (at) ichiro.org).
      9       1.1   ichiro  *
     10       1.1   ichiro  * Redistribution and use in source and binary forms, with or without
     11       1.1   ichiro  * modification, are permitted provided that the following conditions
     12       1.1   ichiro  * are met:
     13       1.1   ichiro  * 1. Redistributions of source code must retain the above copyright
     14       1.1   ichiro  *    notice, this list of conditions and the following disclaimer.
     15       1.1   ichiro  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1   ichiro  *    notice, this list of conditions and the following disclaimer in the
     17       1.1   ichiro  *    documentation and/or other materials provided with the distribution.
     18       1.1   ichiro  *
     19       1.1   ichiro  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.1   ichiro  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.1   ichiro  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.1   ichiro  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.1   ichiro  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.1   ichiro  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.1   ichiro  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.1   ichiro  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.1   ichiro  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.1   ichiro  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.1   ichiro  * POSSIBILITY OF SUCH DAMAGE.
     30       1.1   ichiro  */
     31      1.12    lukem 
     32      1.12    lukem #include <sys/cdefs.h>
     33  1.22.8.1  thorpej __KERNEL_RCSID(0, "$NetBSD: ipaq_pcic.c,v 1.22.8.1 2021/08/04 01:08:47 thorpej Exp $");
     34       1.1   ichiro 
     35       1.1   ichiro #include <sys/param.h>
     36       1.1   ichiro #include <sys/systm.h>
     37       1.1   ichiro #include <sys/types.h>
     38       1.1   ichiro #include <sys/conf.h>
     39       1.1   ichiro #include <sys/file.h>
     40       1.1   ichiro #include <sys/device.h>
     41       1.1   ichiro #include <sys/kernel.h>
     42       1.1   ichiro #include <sys/kthread.h>
     43       1.1   ichiro #include <sys/malloc.h>
     44      1.20   dyoung #include <sys/bus.h>
     45       1.1   ichiro 
     46       1.1   ichiro #include <dev/pcmcia/pcmciachip.h>
     47       1.1   ichiro #include <dev/pcmcia/pcmciavar.h>
     48       1.1   ichiro 
     49       1.1   ichiro #include <hpcarm/dev/ipaq_saipvar.h>
     50       1.1   ichiro #include <hpcarm/dev/ipaq_pcicreg.h>
     51       1.1   ichiro #include <hpcarm/dev/ipaq_gpioreg.h>
     52       1.7   ichiro 
     53       1.7   ichiro #include <arm/sa11x0/sa11x0_gpioreg.h>
     54       1.7   ichiro #include <arm/sa11x0/sa11x0_var.h>
     55       1.7   ichiro #include <arm/sa11x0/sa11xx_pcicvar.h>
     56       1.1   ichiro 
     57       1.1   ichiro #include "ipaqpcic.h"
     58       1.1   ichiro 
     59      1.19      rjs static	int	ipaqpcic_match(device_t, cfdata_t, void *);
     60      1.19      rjs static	void	ipaqpcic_attach(device_t, device_t, void *);
     61       1.1   ichiro static	int	ipaqpcic_print(void *, const char *);
     62       1.1   ichiro 
     63       1.1   ichiro static	int	ipaqpcic_read(struct sapcic_socket *, int);
     64       1.1   ichiro static	void	ipaqpcic_write(struct sapcic_socket *, int, int);
     65       1.1   ichiro static	void	ipaqpcic_set_power(struct sapcic_socket *, int);
     66       1.1   ichiro static	void	ipaqpcic_clear_intr(int);
     67       1.1   ichiro static	void	*ipaqpcic_intr_establish(struct sapcic_socket *, int,
     68       1.1   ichiro 				       int (*)(void *), void *);
     69       1.1   ichiro static	void	ipaqpcic_intr_disestablish(struct sapcic_socket *, void *);
     70       1.1   ichiro 
     71       1.1   ichiro struct ipaqpcic_softc {
     72       1.5   ichiro 	struct sapcic_softc	sc_pc;
     73       1.5   ichiro 	bus_space_handle_t	sc_ioh;
     74       1.5   ichiro 	struct ipaq_softc	*sc_parent;
     75       1.5   ichiro 	struct sapcic_socket	sc_socket[2];
     76       1.1   ichiro };
     77       1.1   ichiro 
     78       1.5   ichiro static	void	ipaqpcic_init(struct ipaqpcic_softc *);
     79       1.5   ichiro 
     80       1.1   ichiro static struct sapcic_tag ipaqpcic_functions = {
     81       1.1   ichiro 	ipaqpcic_read,
     82       1.1   ichiro 	ipaqpcic_write,
     83       1.1   ichiro 	ipaqpcic_set_power,
     84       1.1   ichiro 	ipaqpcic_clear_intr,
     85       1.1   ichiro 	ipaqpcic_intr_establish,
     86       1.1   ichiro 	ipaqpcic_intr_disestablish
     87       1.1   ichiro };
     88       1.1   ichiro 
     89      1.19      rjs CFATTACH_DECL_NEW(ipaqpcic, sizeof(struct ipaqpcic_softc),
     90      1.11  thorpej     ipaqpcic_match, ipaqpcic_attach, NULL, NULL);
     91       1.1   ichiro 
     92       1.1   ichiro static int
     93      1.19      rjs ipaqpcic_match(device_t parent, cfdata_t cf, void *aux)
     94       1.1   ichiro {
     95       1.1   ichiro 	return (1);
     96       1.1   ichiro }
     97       1.1   ichiro 
     98       1.1   ichiro static void
     99      1.19      rjs ipaqpcic_attach(device_t parent, device_t self, void *aux)
    100       1.1   ichiro {
    101       1.1   ichiro 	int i;
    102       1.1   ichiro 	struct pcmciabus_attach_args paa;
    103      1.19      rjs 	struct ipaqpcic_softc *sc = device_private(self);
    104      1.19      rjs 	struct ipaq_softc *psc = device_private(parent);
    105       1.1   ichiro 
    106      1.19      rjs 	aprint_normal("\n");
    107       1.1   ichiro 
    108      1.19      rjs 	sc->sc_pc.sc_dev = self;
    109       1.1   ichiro 	sc->sc_pc.sc_iot = psc->sc_iot;
    110       1.1   ichiro 	sc->sc_ioh = psc->sc_ioh;
    111      1.19      rjs 	sc->sc_parent = psc;
    112       1.1   ichiro 
    113       1.5   ichiro 	ipaqpcic_init(sc);
    114       1.1   ichiro 
    115       1.1   ichiro 	for(i = 0; i < 2; i++) {
    116       1.1   ichiro 		sc->sc_socket[i].sc = (struct sapcic_softc *)sc;
    117       1.1   ichiro 		sc->sc_socket[i].socket = i;
    118       1.3   toshii 		sc->sc_socket[i].pcictag_cookie = psc;
    119       1.1   ichiro 		sc->sc_socket[i].pcictag = &ipaqpcic_functions;
    120       1.1   ichiro 		sc->sc_socket[i].event_thread = NULL;
    121       1.1   ichiro 		sc->sc_socket[i].event = 0;
    122       1.1   ichiro 		sc->sc_socket[i].laststatus = SAPCIC_CARD_INVALID;
    123       1.1   ichiro 		sc->sc_socket[i].shutdown = 0;
    124       1.1   ichiro 
    125       1.1   ichiro 		paa.paa_busname = "pcmcia";
    126       1.1   ichiro 		paa.pct = (pcmcia_chipset_tag_t)&sa11x0_pcmcia_functions;
    127       1.1   ichiro 		paa.pch = (pcmcia_chipset_handle_t)&sc->sc_socket[i];
    128       1.1   ichiro 
    129       1.1   ichiro 		sc->sc_socket[i].pcmcia =
    130      1.22  thorpej 		    config_found(sc->sc_pc.sc_dev, &paa, ipaqpcic_print,
    131  1.22.8.1  thorpej 				 CFARGS_NONE);
    132       1.1   ichiro 
    133       1.1   ichiro 		sa11x0_intr_establish((sa11x0_chipset_tag_t)psc,
    134       1.1   ichiro 			    i ? IRQ_CD1 : IRQ_CD0,
    135       1.1   ichiro 			    1, IPL_BIO, sapcic_intr,
    136       1.1   ichiro 			    &sc->sc_socket[i]);
    137       1.1   ichiro 
    138       1.1   ichiro 		/* schedule kthread creation */
    139      1.16       ad 		sapcic_kthread_create(&sc->sc_socket[i]);
    140       1.1   ichiro 
    141       1.1   ichiro #if 0 /* XXX */
    142       1.1   ichiro 		/* establish_intr should be after creating the kthread */
    143       1.1   ichiro 		config_interrupt(&sc->sc_socket[i], ipaqpcic_config_intr);
    144       1.1   ichiro #endif
    145       1.1   ichiro 	}
    146       1.1   ichiro }
    147       1.1   ichiro 
    148       1.1   ichiro static int
    149      1.15      rjs ipaqpcic_print(void *aux, const char *name)
    150       1.1   ichiro {
    151       1.1   ichiro 	return (UNCONF);
    152       1.1   ichiro }
    153       1.1   ichiro 
    154       1.5   ichiro static void
    155      1.15      rjs ipaqpcic_init(struct ipaqpcic_softc *sc)
    156       1.5   ichiro {
    157       1.6   ichiro 	int cr;
    158       1.6   ichiro 
    159       1.6   ichiro 	/* All those are inputs */
    160       1.6   ichiro 	cr = bus_space_read_4(sc->sc_pc.sc_iot, sc->sc_parent->sc_gpioh, SAGPIO_PDR);
    161       1.6   ichiro 	cr &= ~(GPIO_H3600_PCMCIA_CD0 | GPIO_H3600_PCMCIA_CD1 | GPIO_H3600_PCMCIA_IRQ0 |
    162       1.6   ichiro 		GPIO_H3600_PCMCIA_IRQ1);
    163       1.6   ichiro 	bus_space_write_4(sc->sc_pc.sc_iot, sc->sc_parent->sc_gpioh, SAGPIO_PDR, cr);
    164       1.6   ichiro 
    165       1.5   ichiro 	sc->sc_parent->ipaq_egpio |=
    166       1.5   ichiro 		EGPIO_H3600_OPT_NVRAM_ON | EGPIO_H3600_OPT_ON ;
    167       1.5   ichiro 	sc->sc_parent->ipaq_egpio &=
    168       1.5   ichiro 		~(EGPIO_H3600_CARD_RESET | EGPIO_H3600_OPT_RESET);
    169       1.5   ichiro 	bus_space_write_2(sc->sc_pc.sc_iot, sc->sc_parent->sc_egpioh,
    170       1.5   ichiro 			  0, sc->sc_parent->ipaq_egpio);
    171       1.5   ichiro }
    172       1.1   ichiro 
    173       1.1   ichiro static int
    174      1.15      rjs ipaqpcic_read(struct sapcic_socket *so, int reg)
    175       1.1   ichiro {
    176       1.1   ichiro 	int cr, bit;
    177       1.1   ichiro 	struct ipaqpcic_softc *sc = (struct ipaqpcic_softc *)so->sc;
    178       1.1   ichiro 
    179       1.5   ichiro 	cr = bus_space_read_4(sc->sc_pc.sc_iot, sc->sc_parent->sc_gpioh, SAGPIO_PLR);
    180       1.1   ichiro 
    181       1.1   ichiro 	switch (reg) {
    182       1.1   ichiro 	case SAPCIC_STATUS_CARD:
    183       1.6   ichiro 		bit = (so->socket ? GPIO_H3600_PCMCIA_CD0 :
    184       1.6   ichiro 				GPIO_H3600_PCMCIA_CD1) & cr;
    185       1.6   ichiro 		if (!bit)
    186       1.1   ichiro 			return SAPCIC_CARD_INVALID;
    187       1.1   ichiro 		else
    188       1.1   ichiro 			return SAPCIC_CARD_VALID;
    189       1.1   ichiro 	case SAPCIC_STATUS_VS1:
    190       1.1   ichiro         case SAPCIC_STATUS_VS2:
    191       1.6   ichiro 	case SAPCIC_STATUS_READY:
    192       1.6   ichiro 		bit = (so->socket ? GPIO_H3600_PCMCIA_IRQ0:
    193       1.6   ichiro 				GPIO_H3600_PCMCIA_IRQ1);
    194       1.6   ichiro 		return (bit & cr);
    195       1.1   ichiro 	default:
    196       1.9   provos 		panic("ipaqpcic_read: bogus register");
    197       1.1   ichiro 	}
    198       1.1   ichiro }
    199       1.1   ichiro 
    200       1.1   ichiro static void
    201      1.15      rjs ipaqpcic_write(struct sapcic_socket *so, int reg, int arg)
    202       1.1   ichiro {
    203       1.5   ichiro 	int s;
    204       1.1   ichiro 	struct ipaqpcic_softc *sc = (struct ipaqpcic_softc *)so->sc;
    205       1.1   ichiro 
    206       1.1   ichiro 	s = splhigh();
    207       1.1   ichiro 	switch (reg) {
    208       1.1   ichiro 	case SAPCIC_CONTROL_RESET:
    209       1.5   ichiro 		sc->sc_parent->ipaq_egpio |= EGPIO_H3600_CARD_RESET;
    210       1.1   ichiro 		break;
    211       1.1   ichiro 	case SAPCIC_CONTROL_LINEENABLE:
    212       1.1   ichiro 	case SAPCIC_CONTROL_WAITENABLE:
    213       1.1   ichiro 	case SAPCIC_CONTROL_POWERSELECT:
    214       1.1   ichiro 		break;
    215       1.1   ichiro 
    216       1.1   ichiro 	default:
    217       1.1   ichiro 		splx(s);
    218       1.1   ichiro 		panic("ipaqpcic_write: bogus register");
    219       1.1   ichiro 	}
    220       1.5   ichiro 	bus_space_write_2(sc->sc_pc.sc_iot, sc->sc_parent->sc_egpioh, 0,
    221       1.5   ichiro 			  sc->sc_parent->ipaq_egpio);
    222       1.1   ichiro 	splx(s);
    223       1.1   ichiro }
    224       1.1   ichiro 
    225       1.1   ichiro static void
    226      1.15      rjs ipaqpcic_set_power(struct sapcic_socket *so, int arg)
    227       1.1   ichiro {
    228       1.5   ichiro 	int s;
    229       1.5   ichiro 	struct ipaqpcic_softc *sc = (struct ipaqpcic_softc *)so->sc;
    230       1.1   ichiro 
    231       1.1   ichiro 	s = splbio();
    232       1.1   ichiro 	switch (arg) {
    233       1.1   ichiro 	case SAPCIC_POWER_OFF:
    234       1.5   ichiro 		sc->sc_parent->ipaq_egpio &=
    235       1.5   ichiro 			~(EGPIO_H3600_OPT_NVRAM_ON | EGPIO_H3600_OPT_ON);
    236       1.1   ichiro 		break;
    237       1.1   ichiro 	case SAPCIC_POWER_3V:
    238       1.1   ichiro 	case SAPCIC_POWER_5V:
    239       1.5   ichiro 		sc->sc_parent->ipaq_egpio |=
    240       1.5   ichiro 			EGPIO_H3600_OPT_NVRAM_ON | EGPIO_H3600_OPT_ON;
    241       1.1   ichiro 		break;
    242       1.1   ichiro 	default:
    243       1.9   provos 		panic("ipaqpcic_set_power: bogus arg");
    244       1.1   ichiro 	}
    245       1.5   ichiro 	bus_space_write_2(sc->sc_pc.sc_iot, sc->sc_parent->sc_egpioh,
    246       1.5   ichiro 			  0, sc->sc_parent->ipaq_egpio);
    247       1.1   ichiro 	splx(s);
    248       1.1   ichiro }
    249       1.1   ichiro 
    250       1.1   ichiro static void
    251      1.15      rjs ipaqpcic_clear_intr(int arg)
    252       1.1   ichiro {
    253       1.1   ichiro }
    254       1.1   ichiro 
    255       1.1   ichiro static void *
    256      1.15      rjs ipaqpcic_intr_establish(struct sapcic_socket *so, int level,
    257      1.15      rjs 			int (*ih_fun)(void *), void *ih_arg)
    258       1.1   ichiro {
    259       1.1   ichiro 	int irq;
    260       1.1   ichiro 
    261       1.1   ichiro 	irq = so->socket ? IRQ_IRQ0 : IRQ_IRQ1;
    262       1.3   toshii 	return (sa11x0_intr_establish((sa11x0_chipset_tag_t)so->pcictag_cookie,
    263       1.1   ichiro 				    irq-16, 1, level, ih_fun, ih_arg));
    264       1.1   ichiro }
    265       1.1   ichiro 
    266       1.1   ichiro static void
    267      1.15      rjs ipaqpcic_intr_disestablish(struct sapcic_socket *so, void *ih)
    268       1.1   ichiro {
    269       1.3   toshii 	sa11x0_intr_disestablish((sa11x0_chipset_tag_t)so->pcictag_cookie, ih);
    270       1.1   ichiro }
    271