Home | History | Annotate | Line # | Download | only in pci
puc.c revision 1.9
      1 /*	$NetBSD: puc.c,v 1.9 2000/12/28 22:59:15 sommerfeld Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1996, 1998, 1999
      5  *	Christopher G. Demetriou.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *      This product includes software developed by Christopher G. Demetriou
     18  *	for the NetBSD Project.
     19  * 4. The name of the author may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 /*
     35  * PCI "universal" communication card device driver, glues com, lpt,
     36  * and similar ports to PCI via bridge chip often much larger than
     37  * the devices being glued.
     38  *
     39  * Author: Christopher G. Demetriou, May 14, 1998 (derived from NetBSD
     40  * sys/dev/pci/pciide.c, revision 1.6).
     41  *
     42  * These devices could be (and some times are) described as
     43  * communications/{serial,parallel}, etc. devices with known
     44  * programming interfaces, but those programming interfaces (in
     45  * particular the BAR assignments for devices, etc.) in fact are not
     46  * particularly well defined.
     47  *
     48  * After I/we have seen more of these devices, it may be possible
     49  * to generalize some of these bits.  In particular, devices which
     50  * describe themselves as communications/serial/16[45]50, and
     51  * communications/parallel/??? might be attached via direct
     52  * 'com' and 'lpt' attachments to pci.
     53  */
     54 
     55 #include <sys/param.h>
     56 #include <sys/systm.h>
     57 #include <sys/device.h>
     58 
     59 #include <dev/pci/pcireg.h>
     60 #include <dev/pci/pcivar.h>
     61 #include <dev/pci/pucvar.h>
     62 #include <sys/termios.h>
     63 #include <dev/ic/comreg.h>
     64 #include <dev/ic/comvar.h>
     65 
     66 #include "opt_puccn.h"
     67 
     68 struct puc_softc {
     69 	struct device		sc_dev;
     70 
     71 	/* static configuration data */
     72 	const struct puc_device_description *sc_desc;
     73 
     74 	/* card-global dynamic data */
     75 	void			*sc_ih;
     76 	struct {
     77 		int		mapped;
     78 		bus_addr_t	a;
     79 		bus_size_t	s;
     80 		bus_space_tag_t	t;
     81 		bus_space_handle_t h;
     82 	} sc_bar_mappings[6];				/* XXX constant */
     83 
     84 	/* per-port dynamic data */
     85         struct {
     86 		struct device	*dev;
     87 
     88                 /* filled in by port attachments */
     89                 int             (*ihand) __P((void *));
     90                 void            *ihandarg;
     91         } sc_ports[PUC_MAX_PORTS];
     92 };
     93 
     94 int	puc_match __P((struct device *, struct cfdata *, void *));
     95 void	puc_attach __P((struct device *, struct device *, void *));
     96 int	puc_print __P((void *, const char *));
     97 int	puc_submatch __P((struct device *, struct cfdata *, void *));
     98 
     99 struct cfattach puc_ca = {
    100 	sizeof(struct puc_softc), puc_match, puc_attach
    101 };
    102 
    103 const struct puc_device_description *
    104 	puc_find_description __P((pcireg_t, pcireg_t, pcireg_t, pcireg_t));
    105 static const char *
    106 	puc_port_type_name __P((int));
    107 
    108 int
    109 puc_match(parent, match, aux)
    110 	struct device *parent;
    111 	struct cfdata *match;
    112 	void *aux;
    113 {
    114 	struct pci_attach_args *pa = aux;
    115 	const struct puc_device_description *desc;
    116 	pcireg_t bhlc, subsys;
    117 
    118 	bhlc = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_BHLC_REG);
    119 	if (PCI_HDRTYPE_TYPE(bhlc) != 0)
    120 		return (0);
    121 
    122 	subsys = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
    123 
    124 	desc = puc_find_description(PCI_VENDOR(pa->pa_id),
    125 	    PCI_PRODUCT(pa->pa_id), PCI_VENDOR(subsys), PCI_PRODUCT(subsys));
    126 	if (desc != NULL)
    127 		return (10);
    128 
    129 #if 0
    130 	/*
    131 	 * XXX this is obviously bogus.  eventually, we might want
    132 	 * XXX to match communications/modem, etc., but that needs some
    133 	 * XXX special work in the match fn.
    134 	 */
    135 	/*
    136 	 * Match class/subclass, so we can tell people to compile kernel
    137 	 * with options that cause this driver to spew.
    138 	 */
    139 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_COMMUNICATIONS &&
    140 	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_BRIDGE_PCI)
    141 		return (1);
    142 #endif
    143 
    144 	return (0);
    145 }
    146 
    147 void
    148 puc_attach(parent, self, aux)
    149 	struct device *parent, *self;
    150 	void *aux;
    151 {
    152 	struct puc_softc *sc = (struct puc_softc *)self;
    153 	struct pci_attach_args *pa = aux;
    154 	struct puc_attach_args paa;
    155 	pci_intr_handle_t intrhandle;
    156 	pcireg_t subsys;
    157 	int i, barindex;
    158 	bus_addr_t base;
    159 	bus_space_tag_t tag;
    160 #ifdef PUCCN
    161 	bus_space_handle_t ioh;
    162 #endif
    163 
    164 	subsys = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
    165 	sc->sc_desc = puc_find_description(PCI_VENDOR(pa->pa_id),
    166 	    PCI_PRODUCT(pa->pa_id), PCI_VENDOR(subsys), PCI_PRODUCT(subsys));
    167 	if (sc->sc_desc == NULL) {
    168 		/*
    169 		 * This was a class/subclass match, so tell people to compile
    170 		 * kernel with options that cause this driver to spew.
    171 		 */
    172 #ifdef PUC_PRINT_REGS
    173 		printf(":\n");
    174 		pci_conf_print(pa->pa_pc, pa->pa_tag, NULL);
    175 #else
    176 		printf(": unknown PCI communications device\n");
    177 		printf("%s: compile kernel with PUC_PRINT_REGS and larger\n",
    178 		    sc->sc_dev.dv_xname);
    179 		printf("%s: mesage buffer (via 'options MSGBUFSIZE=...'),\n",
    180 		    sc->sc_dev.dv_xname);
    181 		printf("%s: and report the result with send-pr\n",
    182 		    sc->sc_dev.dv_xname);
    183 #endif
    184 		return;
    185 	}
    186 
    187 	printf(": %s (", sc->sc_desc->name);
    188 	for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++)
    189 		printf("%s%s", i ? ", " : "",
    190 		    puc_port_type_name(sc->sc_desc->ports[i].type));
    191 	printf(")\n");
    192 
    193 	for (i = 0; i < 6; i++) {
    194 		pcireg_t bar, type;
    195 
    196 		sc->sc_bar_mappings[i].mapped = 0;
    197 
    198 		bar = pci_conf_read(pa->pa_pc, pa->pa_tag,
    199 		    PCI_MAPREG_START + 4 * i);	/* XXX const */
    200 		if (bar == 0)			/* BAR not implemented(?) */
    201 			continue;
    202 
    203 		type = (PCI_MAPREG_TYPE(bar) == PCI_MAPREG_TYPE_IO ?
    204 		    PCI_MAPREG_TYPE_IO : PCI_MAPREG_MEM_TYPE(bar));
    205 
    206 		if (type == PCI_MAPREG_TYPE_IO) {
    207 			tag = pa->pa_iot;
    208 			base =  PCI_MAPREG_IO_ADDR(bar);
    209 		} else {
    210 			tag = pa->pa_memt;
    211 			base =  PCI_MAPREG_MEM_ADDR(bar);
    212 		}
    213 #ifdef PUCCN
    214 		if (com_is_console(tag, base, &ioh)) {
    215 			sc->sc_bar_mappings[i].mapped = 1;
    216 			sc->sc_bar_mappings[i].a = base;
    217 			sc->sc_bar_mappings[i].s = COM_NPORTS;
    218 			sc->sc_bar_mappings[i].t = tag;
    219 			sc->sc_bar_mappings[i].h = ioh;
    220 			continue;
    221 		}
    222 #endif
    223 		sc->sc_bar_mappings[i].mapped = (pci_mapreg_map(pa,
    224 		    PCI_MAPREG_START + 4 * i, type, 0,
    225 		    &sc->sc_bar_mappings[i].t, &sc->sc_bar_mappings[i].h,
    226 		    &sc->sc_bar_mappings[i].a, &sc->sc_bar_mappings[i].s)
    227 		      == 0);
    228 		if (sc->sc_bar_mappings[i].mapped)
    229 			continue;
    230 
    231 		printf("%s: couldn't map BAR at offset 0x%lx\n",
    232 		    sc->sc_dev.dv_xname, (long)(PCI_MAPREG_START + 4 * i));
    233 	}
    234 
    235 	/* Map interrupt. */
    236 	if (pci_intr_map(pa, &intrhandle)) {
    237 		printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
    238 		return;
    239 	}
    240 	/*
    241 	 * XXX the sub-devices establish the interrupts, for the
    242 	 * XXX following reasons:
    243 	 * XXX
    244 	 * XXX    * we can't really know what IPLs they'd want
    245 	 * XXX
    246 	 * XXX    * the MD dispatching code can ("should") dispatch
    247 	 * XXX      chained interrupts better than we can.
    248 	 * XXX
    249 	 * XXX It would be nice if we could indicate to the MD interrupt
    250 	 * XXX handling code that the interrupt line used by the device
    251 	 * XXX was a PCI (level triggered) interrupt.
    252 	 * XXX
    253 	 * XXX It's not pretty, but hey, what is?
    254 	 */
    255 
    256 	/* Configure each port. */
    257 	for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
    258 		bus_space_handle_t subregion_handle;
    259 
    260 		/* make sure the base address register is mapped */
    261 		barindex = PUC_PORT_BAR_INDEX(sc->sc_desc->ports[i].bar);
    262 		if (!sc->sc_bar_mappings[barindex].mapped) {
    263 			printf("%s: %s port uses unmapped BAR (0x%x)\n",
    264 			    sc->sc_dev.dv_xname,
    265 			    puc_port_type_name(sc->sc_desc->ports[i].type),
    266 			    sc->sc_desc->ports[i].bar);
    267 			continue;
    268 		}
    269 
    270 		/* set up to configure the child device */
    271 		paa.port = i;
    272 		paa.type = sc->sc_desc->ports[i].type;
    273 		paa.pc = pa->pa_pc;
    274 		paa.intrhandle = intrhandle;
    275 		paa.a = sc->sc_bar_mappings[barindex].a;
    276 		paa.t = sc->sc_bar_mappings[barindex].t;
    277 
    278 		if (
    279 #ifdef PUCCN
    280 		    !com_is_console(sc->sc_bar_mappings[barindex].t,
    281 		    sc->sc_bar_mappings[barindex].a, &subregion_handle)
    282 		   &&
    283 #endif
    284 		    bus_space_subregion(sc->sc_bar_mappings[barindex].t,
    285 		    sc->sc_bar_mappings[barindex].h,
    286 		    sc->sc_desc->ports[i].offset,
    287 		    sc->sc_bar_mappings[barindex].s -
    288 		      sc->sc_desc->ports[i].offset,
    289 		    &subregion_handle) != 0) {
    290 			printf("%s: couldn't get subregion for port %d\n",
    291 			    sc->sc_dev.dv_xname, i);
    292 			continue;
    293 		}
    294 		paa.h = subregion_handle;
    295 
    296 #if 0
    297 		printf("%s: port %d: %s @ (index %d) 0x%x (0x%lx, 0x%lx)\n",
    298 		    sc->sc_dev.dv_xname, paa.port,
    299 		    puc_port_type_name(paa.type), barindex, (int)paa.a,
    300 		    (long)paa.t, (long)paa.h);
    301 #endif
    302 
    303 		/* and configure it */
    304 		sc->sc_ports[i].dev = config_found_sm(self, &paa, puc_print,
    305 		    puc_submatch);
    306 	}
    307 }
    308 
    309 int
    310 puc_print(aux, pnp)
    311 	void *aux;
    312 	const char *pnp;
    313 {
    314 	struct puc_attach_args *paa = aux;
    315 
    316 	if (pnp)
    317 		printf("%s at %s", puc_port_type_name(paa->type), pnp);
    318 	printf(" port %d", paa->port);
    319 	return (UNCONF);
    320 }
    321 
    322 int
    323 puc_submatch(parent, cf, aux)
    324 	struct device *parent;
    325 	struct cfdata *cf;
    326 	void *aux;
    327 {
    328 	struct puc_attach_args *aa = aux;
    329 
    330 	if (cf->cf_loc[PUCCF_PORT] != PUCCF_PORT_DEFAULT &&
    331 	    cf->cf_loc[PUCCF_PORT] != aa->port)
    332 		return 0;
    333 	return ((*cf->cf_attach->ca_match)(parent, cf, aux));
    334 }
    335 
    336 const struct puc_device_description *
    337 puc_find_description(vend, prod, svend, sprod)
    338 	pcireg_t vend, prod, svend, sprod;
    339 {
    340 	int i;
    341 
    342 #define checkreg(val, index) \
    343     (((val) & puc_devices[i].rmask[(index)]) == puc_devices[i].rval[(index)])
    344 
    345 	for (i = 0; puc_devices[i].name != NULL; i++) {
    346 		if (checkreg(vend, PUC_REG_VEND) &&
    347 		    checkreg(prod, PUC_REG_PROD) &&
    348 		    checkreg(svend, PUC_REG_SVEND) &&
    349 		    checkreg(sprod, PUC_REG_SPROD))
    350 			return (&puc_devices[i]);
    351 	}
    352 
    353 #undef checkreg
    354 
    355 	return (NULL);
    356 }
    357 
    358 static const char *
    359 puc_port_type_name(type)
    360 	int type;
    361 {
    362 
    363 	switch (type) {
    364 	case PUC_PORT_TYPE_COM:
    365 		return "com";
    366 	case PUC_PORT_TYPE_LPT:
    367 		return "lpt";
    368 	default:
    369 		panic("puc_port_type_name %d", type);
    370 	}
    371 }
    372