Home | History | Annotate | Line # | Download | only in pcmcia
com_pcmcia.c revision 1.56.16.2
      1  1.56.16.1       mjf /*	$NetBSD: com_pcmcia.c,v 1.56.16.2 2008/06/02 13:23:45 mjf Exp $	 */
      2       1.13   mycroft 
      3       1.13   mycroft /*-
      4       1.45   mycroft  * Copyright (c) 1998, 2004 The NetBSD Foundation, Inc.
      5       1.13   mycroft  * All rights reserved.
      6       1.13   mycroft  *
      7       1.13   mycroft  * This code is derived from software contributed to The NetBSD Foundation
      8       1.13   mycroft  * by Charles M. Hannum.
      9       1.13   mycroft  *
     10       1.13   mycroft  * Redistribution and use in source and binary forms, with or without
     11       1.13   mycroft  * modification, are permitted provided that the following conditions
     12       1.13   mycroft  * are met:
     13       1.13   mycroft  * 1. Redistributions of source code must retain the above copyright
     14       1.13   mycroft  *    notice, this list of conditions and the following disclaimer.
     15       1.13   mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.13   mycroft  *    notice, this list of conditions and the following disclaimer in the
     17       1.13   mycroft  *    documentation and/or other materials provided with the distribution.
     18       1.13   mycroft  *
     19       1.13   mycroft  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.13   mycroft  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.13   mycroft  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.13   mycroft  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.13   mycroft  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.13   mycroft  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.13   mycroft  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.13   mycroft  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.13   mycroft  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.13   mycroft  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.13   mycroft  * POSSIBILITY OF SUCH DAMAGE.
     30       1.13   mycroft  */
     31        1.2   thorpej 
     32        1.2   thorpej /*-
     33        1.2   thorpej  * Copyright (c) 1991 The Regents of the University of California.
     34        1.2   thorpej  * All rights reserved.
     35        1.2   thorpej  *
     36        1.2   thorpej  * Redistribution and use in source and binary forms, with or without
     37        1.2   thorpej  * modification, are permitted provided that the following conditions
     38        1.2   thorpej  * are met:
     39        1.2   thorpej  * 1. Redistributions of source code must retain the above copyright
     40        1.2   thorpej  *    notice, this list of conditions and the following disclaimer.
     41        1.2   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     42        1.2   thorpej  *    notice, this list of conditions and the following disclaimer in the
     43        1.2   thorpej  *    documentation and/or other materials provided with the distribution.
     44       1.33       agc  * 3. Neither the name of the University nor the names of its contributors
     45        1.2   thorpej  *    may be used to endorse or promote products derived from this software
     46        1.2   thorpej  *    without specific prior written permission.
     47        1.2   thorpej  *
     48        1.2   thorpej  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     49        1.2   thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     50        1.2   thorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     51        1.2   thorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     52        1.2   thorpej  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     53        1.2   thorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     54        1.2   thorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     55        1.2   thorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     56        1.2   thorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     57        1.2   thorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     58        1.2   thorpej  * SUCH DAMAGE.
     59        1.2   thorpej  *
     60        1.2   thorpej  *	@(#)com.c	7.5 (Berkeley) 5/16/91
     61        1.2   thorpej  */
     62       1.22     lukem 
     63       1.22     lukem #include <sys/cdefs.h>
     64  1.56.16.1       mjf __KERNEL_RCSID(0, "$NetBSD: com_pcmcia.c,v 1.56.16.2 2008/06/02 13:23:45 mjf Exp $");
     65        1.2   thorpej 
     66        1.2   thorpej #include <sys/param.h>
     67        1.2   thorpej #include <sys/systm.h>
     68        1.2   thorpej #include <sys/ioctl.h>
     69        1.2   thorpej #include <sys/select.h>
     70        1.2   thorpej #include <sys/tty.h>
     71        1.2   thorpej #include <sys/proc.h>
     72        1.2   thorpej #include <sys/user.h>
     73        1.2   thorpej #include <sys/conf.h>
     74        1.2   thorpej #include <sys/file.h>
     75        1.2   thorpej #include <sys/uio.h>
     76        1.2   thorpej #include <sys/kernel.h>
     77        1.2   thorpej #include <sys/syslog.h>
     78        1.2   thorpej #include <sys/device.h>
     79        1.2   thorpej 
     80       1.56        ad #include <sys/intr.h>
     81       1.56        ad #include <sys/bus.h>
     82        1.2   thorpej 
     83        1.2   thorpej #include <dev/pcmcia/pcmciavar.h>
     84        1.2   thorpej #include <dev/pcmcia/pcmciareg.h>
     85        1.9  christos #include <dev/pcmcia/pcmciadevs.h>
     86        1.2   thorpej 
     87        1.2   thorpej #include <dev/ic/comreg.h>
     88        1.2   thorpej #include <dev/ic/comvar.h>
     89        1.2   thorpej 
     90        1.2   thorpej #include <dev/isa/isareg.h>
     91        1.2   thorpej 
     92  1.56.16.1       mjf int com_pcmcia_match(device_t, cfdata_t , void *);
     93       1.50     perry int com_pcmcia_validate_config(struct pcmcia_config_entry *);
     94  1.56.16.1       mjf void com_pcmcia_attach(device_t, device_t, void *);
     95  1.56.16.1       mjf int com_pcmcia_detach(device_t, int);
     96        1.2   thorpej 
     97       1.50     perry int com_pcmcia_enable(struct com_softc *);
     98       1.50     perry void com_pcmcia_disable(struct com_softc *);
     99        1.3      marc 
    100        1.2   thorpej struct com_pcmcia_softc {
    101        1.2   thorpej 	struct com_softc sc_com;		/* real "com" softc */
    102        1.2   thorpej 
    103        1.2   thorpej 	/* PCMCIA-specific goo */
    104        1.2   thorpej 	struct pcmcia_function *sc_pf;		/* our PCMCIA function */
    105        1.2   thorpej 	void *sc_ih;				/* interrupt handler */
    106       1.44   mycroft 	int sc_attached;
    107        1.2   thorpej };
    108        1.2   thorpej 
    109  1.56.16.1       mjf CFATTACH_DECL_NEW(com_pcmcia, sizeof(struct com_pcmcia_softc),
    110       1.32   thorpej     com_pcmcia_match, com_pcmcia_attach, com_pcmcia_detach, com_activate);
    111        1.2   thorpej 
    112       1.47   mycroft static const struct pcmcia_product com_pcmcia_products[] = {
    113       1.47   mycroft 	{ PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
    114       1.47   mycroft 	  PCMCIA_CIS_MEGAHERTZ_XJ2288 },
    115       1.55  kiyohara 	{ PCMCIA_VENDOR_TDK, PCMCIA_PRODUCT_TDK_BLUETOOTH_PCCARD,
    116       1.55  kiyohara 	  PCMCIA_CIS_INVALID },
    117       1.47   mycroft };
    118       1.47   mycroft static const size_t com_pcmcia_nproducts =
    119       1.47   mycroft     sizeof(com_pcmcia_products) / sizeof(com_pcmcia_products[0]);
    120        1.3      marc 
    121        1.2   thorpej int
    122  1.56.16.1       mjf com_pcmcia_match(device_t parent, cfdata_t match, void *aux)
    123        1.2   thorpej {
    124       1.11   nathanw 	int comportmask;
    125        1.2   thorpej 	struct pcmcia_attach_args *pa = aux;
    126        1.2   thorpej 	struct pcmcia_config_entry *cfe;
    127        1.2   thorpej 
    128       1.11   nathanw 	/* 1. Does it claim to be a serial device? */
    129       1.11   nathanw 	if (pa->pf->function == PCMCIA_FUNCTION_SERIAL)
    130       1.20  christos 		return 1;
    131       1.11   nathanw 
    132       1.11   nathanw 	/* 2. Does it have all four 'standard' port ranges? */
    133       1.11   nathanw 	comportmask = 0;
    134       1.29     lukem 	SIMPLEQ_FOREACH(cfe, &pa->pf->cfe_head, cfe_list) {
    135       1.20  christos 		switch (cfe->iospace[0].start) {
    136       1.20  christos 		case IO_COM1:
    137       1.20  christos 			comportmask |= 1;
    138       1.20  christos 			break;
    139       1.20  christos 		case IO_COM2:
    140       1.20  christos 			comportmask |= 2;
    141       1.20  christos 			break;
    142       1.20  christos 		case IO_COM3:
    143       1.20  christos 			comportmask |= 4;
    144       1.20  christos 			break;
    145       1.20  christos 		case IO_COM4:
    146       1.20  christos 			comportmask |= 8;
    147       1.20  christos 			break;
    148       1.20  christos 		}
    149       1.11   nathanw 	}
    150       1.11   nathanw 
    151       1.11   nathanw 	if (comportmask == 15)
    152       1.20  christos 		return 1;
    153       1.11   nathanw 
    154       1.11   nathanw 	/* 3. Is this a card we know about? */
    155       1.47   mycroft 	if (pcmcia_product_lookup(pa, com_pcmcia_products, com_pcmcia_nproducts,
    156       1.47   mycroft 	    sizeof(com_pcmcia_products[0]), NULL))
    157       1.20  christos 		return 1;
    158        1.2   thorpej 
    159       1.11   nathanw 	return 0;
    160        1.2   thorpej }
    161        1.2   thorpej 
    162       1.44   mycroft int
    163  1.56.16.1       mjf com_pcmcia_validate_config(struct pcmcia_config_entry *cfe)
    164       1.44   mycroft {
    165       1.44   mycroft 	if (cfe->iftype != PCMCIA_IFTYPE_IO ||
    166       1.44   mycroft 	    cfe->num_iospace != 1)
    167       1.44   mycroft 		return (EINVAL);
    168       1.44   mycroft 	/* Some cards have a memory space, but we don't use it. */
    169       1.44   mycroft 	cfe->num_memspace = 0;
    170       1.44   mycroft 	return (0);
    171       1.44   mycroft }
    172       1.44   mycroft 
    173        1.2   thorpej void
    174  1.56.16.1       mjf com_pcmcia_attach(device_t parent, device_t self, void *aux)
    175        1.2   thorpej {
    176  1.56.16.1       mjf 	struct com_pcmcia_softc *psc = device_private(self);
    177        1.2   thorpej 	struct com_softc *sc = &psc->sc_com;
    178        1.2   thorpej 	struct pcmcia_attach_args *pa = aux;
    179        1.2   thorpej 	struct pcmcia_config_entry *cfe;
    180       1.44   mycroft 	int error;
    181        1.2   thorpej 
    182  1.56.16.1       mjf 	sc->sc_dev = self;
    183        1.2   thorpej 	psc->sc_pf = pa->pf;
    184        1.2   thorpej 
    185       1.44   mycroft 	error = pcmcia_function_configure(pa->pf, com_pcmcia_validate_config);
    186       1.44   mycroft 	if (error) {
    187  1.56.16.1       mjf 		aprint_error_dev(self, "configure failed, error=%d\n", error);
    188        1.2   thorpej 		return;
    189        1.2   thorpej 	}
    190       1.42   mycroft 
    191       1.44   mycroft 	cfe = pa->pf->cfe;
    192       1.52   gdamore 	COM_INIT_REGS(sc->sc_regs, cfe->iospace[0].handle.iot,
    193       1.52   gdamore 	    cfe->iospace[0].handle.ioh, -1);
    194       1.44   mycroft 
    195       1.44   mycroft 	error = com_pcmcia_enable(sc);
    196       1.48   mycroft 	if (error)
    197       1.44   mycroft 		goto fail;
    198       1.42   mycroft 
    199       1.42   mycroft 	sc->enabled = 1;
    200       1.42   mycroft 
    201        1.2   thorpej 	sc->sc_frequency = COM_FREQ;
    202        1.2   thorpej 
    203        1.3      marc 	sc->enable = com_pcmcia_enable;
    204        1.3      marc 	sc->disable = com_pcmcia_disable;
    205       1.20  christos 
    206  1.56.16.1       mjf 	aprint_normal("%s", device_xname(self));
    207        1.5  christos 
    208        1.2   thorpej 	com_attach_subr(sc);
    209        1.2   thorpej 
    210        1.3      marc 	sc->enabled = 0;
    211       1.20  christos 
    212       1.44   mycroft 	psc->sc_attached = 1;
    213       1.42   mycroft 	com_pcmcia_disable(sc);
    214       1.44   mycroft 	return;
    215       1.44   mycroft 
    216       1.44   mycroft fail:
    217       1.44   mycroft 	pcmcia_function_unconfigure(pa->pf);
    218       1.16   thorpej }
    219       1.16   thorpej 
    220       1.16   thorpej int
    221  1.56.16.1       mjf com_pcmcia_detach(device_t self, int flags)
    222       1.16   thorpej {
    223  1.56.16.1       mjf 	struct com_pcmcia_softc *psc = device_private(self);
    224       1.16   thorpej 	int error;
    225       1.16   thorpej 
    226       1.44   mycroft 	if (!psc->sc_attached)
    227       1.44   mycroft 		return (0);
    228       1.28  christos 
    229       1.28  christos 	if ((error = com_detach(self, flags)) != 0)
    230       1.28  christos 		return error;
    231       1.16   thorpej 
    232       1.44   mycroft 	pcmcia_function_unconfigure(psc->sc_pf);
    233       1.44   mycroft 	return (0);
    234        1.3      marc }
    235        1.3      marc 
    236        1.5  christos int
    237  1.56.16.1       mjf com_pcmcia_enable(struct com_softc *sc)
    238        1.3      marc {
    239        1.3      marc 	struct com_pcmcia_softc *psc = (struct com_pcmcia_softc *) sc;
    240        1.3      marc 	struct pcmcia_function *pf = psc->sc_pf;
    241       1.25  christos 	int error;
    242       1.25  christos 
    243        1.2   thorpej 	/* establish the interrupt. */
    244        1.3      marc 	psc->sc_ih = pcmcia_intr_establish(pf, IPL_SERIAL, comintr, sc);
    245        1.2   thorpej 	if (psc->sc_ih == NULL) {
    246  1.56.16.1       mjf 		aprint_error_dev(sc->sc_dev, "couldn't establish interrupt\n");
    247       1.43   mycroft 		return (1);
    248        1.3      marc 	}
    249       1.42   mycroft 
    250       1.43   mycroft 	if ((error = pcmcia_function_enable(pf)) != 0) {
    251       1.42   mycroft 		pcmcia_intr_disestablish(pf, psc->sc_ih);
    252       1.44   mycroft 		psc->sc_ih = 0;
    253       1.43   mycroft 		return (error);
    254       1.42   mycroft 	}
    255       1.42   mycroft 
    256       1.19      marc 	if ((psc->sc_pf->sc->card.product == PCMCIA_PRODUCT_3COM_3C562) ||
    257       1.21      tron 	    (psc->sc_pf->sc->card.product == PCMCIA_PRODUCT_3COM_3CXEM556) ||
    258       1.21      tron 	    (psc->sc_pf->sc->card.product == PCMCIA_PRODUCT_3COM_3CXEM556INT)) {
    259        1.3      marc 		int reg;
    260        1.3      marc 
    261        1.3      marc 		/* turn off the ethernet-disable bit */
    262        1.3      marc 		reg = pcmcia_ccr_read(pf, PCMCIA_CCR_OPTION);
    263        1.3      marc 		if (reg & 0x08) {
    264       1.20  christos 			reg &= ~0x08;
    265       1.20  christos 			pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
    266        1.3      marc 		}
    267        1.2   thorpej 	}
    268       1.43   mycroft 
    269       1.43   mycroft 	return (0);
    270        1.3      marc }
    271        1.3      marc 
    272        1.5  christos void
    273  1.56.16.1       mjf com_pcmcia_disable(struct com_softc *sc)
    274        1.5  christos {
    275        1.5  christos 	struct com_pcmcia_softc *psc = (struct com_pcmcia_softc *) sc;
    276        1.5  christos 
    277       1.43   mycroft 	pcmcia_function_disable(psc->sc_pf);
    278       1.24  christos 	pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
    279       1.44   mycroft 	psc->sc_ih = 0;
    280        1.2   thorpej }
    281