Home | History | Annotate | Line # | Download | only in pci
puc.c revision 1.5
      1 /*	$NetBSD: puc.c,v 1.5 2000/07/25 23:18:42 jeffs 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 struct puc_softc {
     67 	struct device		sc_dev;
     68 
     69 	/* static configuration data */
     70 	const struct puc_device_description *sc_desc;
     71 
     72 	/* card-global dynamic data */
     73 	void			*sc_ih;
     74 	struct {
     75 		int		mapped;
     76 		bus_addr_t	a;
     77 		bus_size_t	s;
     78 		bus_space_tag_t	t;
     79 		bus_space_handle_t h;
     80 	} sc_bar_mappings[6];				/* XXX constant */
     81 
     82 	/* per-port dynamic data */
     83         struct {
     84 		struct device	*dev;
     85 
     86                 /* filled in by port attachments */
     87                 int             (*ihand) __P((void *));
     88                 void            *ihandarg;
     89         } sc_ports[PUC_MAX_PORTS];
     90 };
     91 
     92 int	puc_match __P((struct device *, struct cfdata *, void *));
     93 void	puc_attach __P((struct device *, struct device *, void *));
     94 int	puc_print __P((void *, const char *));
     95 int	puc_submatch __P((struct device *, struct cfdata *, void *));
     96 
     97 struct cfattach puc_ca = {
     98 	sizeof(struct puc_softc), puc_match, puc_attach
     99 };
    100 
    101 const struct puc_device_description *
    102 	puc_find_description __P((pcireg_t, pcireg_t, pcireg_t, pcireg_t));
    103 static const char *
    104 	puc_port_type_name __P((int));
    105 
    106 int
    107 puc_match(parent, match, aux)
    108 	struct device *parent;
    109 	struct cfdata *match;
    110 	void *aux;
    111 {
    112 	struct pci_attach_args *pa = aux;
    113 	const struct puc_device_description *desc;
    114 	pcireg_t bhlc, subsys;
    115 
    116 	bhlc = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_BHLC_REG);
    117 	if (PCI_HDRTYPE_TYPE(bhlc) != 0)
    118 		return (0);
    119 
    120 	subsys = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
    121 
    122 	desc = puc_find_description(PCI_VENDOR(pa->pa_id),
    123 	    PCI_PRODUCT(pa->pa_id), PCI_VENDOR(subsys), PCI_PRODUCT(subsys));
    124 	if (desc != NULL)
    125 		return (10);
    126 
    127 #if 0
    128 	/*
    129 	 * XXX this is obviously bogus.  eventually, we might want
    130 	 * XXX to match communications/modem, etc., but that needs some
    131 	 * XXX special work in the match fn.
    132 	 */
    133 	/*
    134 	 * Match class/subclass, so we can tell people to compile kernel
    135 	 * with options that cause this driver to spew.
    136 	 */
    137 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_COMMUNICATIONS &&
    138 	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_BRIDGE_PCI)
    139 		return (1);
    140 #endif
    141 
    142 	return (0);
    143 }
    144 
    145 void
    146 puc_attach(parent, self, aux)
    147 	struct device *parent, *self;
    148 	void *aux;
    149 {
    150 	struct puc_softc *sc = (struct puc_softc *)self;
    151 	struct pci_attach_args *pa = aux;
    152 	struct puc_attach_args paa;
    153 	pci_intr_handle_t intrhandle;
    154 	pcireg_t subsys;
    155 	int i, barindex;
    156 	bus_addr_t base;
    157 	bus_space_tag_t tag;
    158 	bus_space_handle_t ioh;
    159 
    160 	subsys = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
    161 	sc->sc_desc = puc_find_description(PCI_VENDOR(pa->pa_id),
    162 	    PCI_PRODUCT(pa->pa_id), PCI_VENDOR(subsys), PCI_PRODUCT(subsys));
    163 	if (sc->sc_desc == NULL) {
    164 		/*
    165 		 * This was a class/subclass match, so tell people to compile
    166 		 * kernel with options that cause this driver to spew.
    167 		 */
    168 #ifdef PUC_PRINT_REGS
    169 		printf(":\n");
    170 		pci_conf_print(pa->pa_pc, pa->pa_tag, NULL);
    171 #else
    172 		printf(": unknown PCI communications device\n");
    173 		printf("%s: compile kernel with PUC_PRINT_REGS and larger\n",
    174 		    sc->sc_dev.dv_xname);
    175 		printf("%s: mesage buffer (via 'options MSGBUFSIZE=...'),\n",
    176 		    sc->sc_dev.dv_xname);
    177 		printf("%s: and report the result with send-pr\n",
    178 		    sc->sc_dev.dv_xname);
    179 #endif
    180 		return;
    181 	}
    182 
    183 	printf(": %s (", sc->sc_desc->name);
    184 	for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++)
    185 		printf("%s%s", i ? ", " : "",
    186 		    puc_port_type_name(sc->sc_desc->ports[i].type));
    187 	printf(")\n");
    188 
    189 	for (i = 0; i < 6; i++) {
    190 		pcireg_t bar, type;
    191 
    192 		sc->sc_bar_mappings[i].mapped = 0;
    193 
    194 		bar = pci_conf_read(pa->pa_pc, pa->pa_tag,
    195 		    PCI_MAPREG_START + 4 * i);	/* XXX const */
    196 		if (bar == 0)			/* BAR not implemented(?) */
    197 			continue;
    198 
    199 		type = (PCI_MAPREG_TYPE(bar) == PCI_MAPREG_TYPE_IO ?
    200 		    PCI_MAPREG_TYPE_IO : PCI_MAPREG_MEM_TYPE(bar));
    201 
    202 		if (type == PCI_MAPREG_TYPE_IO) {
    203 			tag = pa->pa_iot;
    204 			base =  PCI_MAPREG_IO_ADDR(bar);
    205 		} else {
    206 			tag = pa->pa_memt;
    207 			base =  PCI_MAPREG_MEM_ADDR(bar);
    208 		}
    209 		if (com_is_console(tag, base, &ioh)) {
    210 			sc->sc_bar_mappings[i].mapped = 1;
    211 			sc->sc_bar_mappings[i].a = base;
    212 			sc->sc_bar_mappings[i].s = COM_NPORTS;
    213 			sc->sc_bar_mappings[i].t = tag;
    214 			sc->sc_bar_mappings[i].h = ioh;
    215 			continue;
    216 		}
    217 		sc->sc_bar_mappings[i].mapped = (pci_mapreg_map(pa,
    218 		    PCI_MAPREG_START + 4 * i, type, 0,
    219 		    &sc->sc_bar_mappings[i].t, &sc->sc_bar_mappings[i].h,
    220 		    &sc->sc_bar_mappings[i].a, &sc->sc_bar_mappings[i].s)
    221 		      == 0);
    222 		if (sc->sc_bar_mappings[i].mapped)
    223 			continue;
    224 
    225 		printf("%s: couldn't map BAR at offset 0x%lx\n",
    226 		    sc->sc_dev.dv_xname, (long)(PCI_MAPREG_START + 4 * i));
    227 	}
    228 
    229 	/* Map interrupt. */
    230 	if (pci_intr_map(pa->pa_pc, pa->pa_intrtag, pa->pa_intrpin,
    231 	    pa->pa_intrline, &intrhandle)) {
    232 		printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
    233 		return;
    234 	}
    235 	/*
    236 	 * XXX the sub-devices establish the interrupts, for the
    237 	 * XXX following reasons:
    238 	 * XXX
    239 	 * XXX    * we can't really know what IPLs they'd want
    240 	 * XXX
    241 	 * XXX    * the MD dispatching code can ("should") dispatch
    242 	 * XXX      chained interrupts better than we can.
    243 	 * XXX
    244 	 * XXX It would be nice if we could indicate to the MD interrupt
    245 	 * XXX handling code that the interrupt line used by the device
    246 	 * XXX was a PCI (level triggered) interrupt.
    247 	 * XXX
    248 	 * XXX It's not pretty, but hey, what is?
    249 	 */
    250 
    251 	/* Configure each port. */
    252 	for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
    253 		bus_space_handle_t subregion_handle;
    254 
    255 		/* make sure the base address register is mapped */
    256 		barindex = PUC_PORT_BAR_INDEX(sc->sc_desc->ports[i].bar);
    257 		if (!sc->sc_bar_mappings[barindex].mapped) {
    258 			printf("%s: %s port uses unmapped BAR (0x%x)\n",
    259 			    sc->sc_dev.dv_xname,
    260 			    puc_port_type_name(sc->sc_desc->ports[i].type),
    261 			    sc->sc_desc->ports[i].bar);
    262 			continue;
    263 		}
    264 
    265 		/* set up to configure the child device */
    266 		paa.port = i;
    267 		paa.type = sc->sc_desc->ports[i].type;
    268 		paa.pc = pa->pa_pc;
    269 		paa.intrhandle = intrhandle;
    270 		paa.a = sc->sc_bar_mappings[barindex].a;
    271 		paa.t = sc->sc_bar_mappings[barindex].t;
    272 
    273 		if (!com_is_console(sc->sc_bar_mappings[barindex].t,
    274 		    sc->sc_bar_mappings[barindex].a, &subregion_handle)
    275 		   && bus_space_subregion(sc->sc_bar_mappings[barindex].t,
    276 		    sc->sc_bar_mappings[barindex].h,
    277 		    sc->sc_desc->ports[i].offset,
    278 		    sc->sc_bar_mappings[barindex].s -
    279 		      sc->sc_desc->ports[i].offset,
    280 		    &subregion_handle) != 0) {
    281 			printf("%s: couldn't get subregion for port %d\n",
    282 			    sc->sc_dev.dv_xname, i);
    283 			continue;
    284 		}
    285 		paa.h = subregion_handle;
    286 
    287 #if 0
    288 		printf("%s: port %d: %s @ (index %d) 0x%x (0x%lx, 0x%lx)\n",
    289 		    sc->sc_dev.dv_xname, paa.port,
    290 		    puc_port_type_name(paa.type), barindex, (int)paa.a,
    291 		    (long)paa.t, (long)paa.h);
    292 #endif
    293 
    294 		/* and configure it */
    295 		sc->sc_ports[i].dev = config_found_sm(self, &paa, puc_print,
    296 		    puc_submatch);
    297 	}
    298 }
    299 
    300 int
    301 puc_print(aux, pnp)
    302 	void *aux;
    303 	const char *pnp;
    304 {
    305 	struct puc_attach_args *paa = aux;
    306 
    307 	if (pnp)
    308 		printf("%s at %s", puc_port_type_name(paa->type), pnp);
    309 	printf(" port %d", paa->port);
    310 	return (UNCONF);
    311 }
    312 
    313 int
    314 puc_submatch(parent, cf, aux)
    315 	struct device *parent;
    316 	struct cfdata *cf;
    317 	void *aux;
    318 {
    319 	struct puc_attach_args *aa = aux;
    320 
    321 	if (cf->cf_loc[PUCCF_PORT] != PUCCF_PORT_DEFAULT &&
    322 	    cf->cf_loc[PUCCF_PORT] != aa->port)
    323 		return 0;
    324 	return ((*cf->cf_attach->ca_match)(parent, cf, aux));
    325 }
    326 
    327 const struct puc_device_description *
    328 puc_find_description(vend, prod, svend, sprod)
    329 	pcireg_t vend, prod, svend, sprod;
    330 {
    331 	int i;
    332 
    333 #define checkreg(val, index) \
    334     (((val) & puc_devices[i].rmask[(index)]) == puc_devices[i].rval[(index)])
    335 
    336 	for (i = 0; puc_devices[i].name != NULL; i++) {
    337 		if (checkreg(vend, PUC_REG_VEND) &&
    338 		    checkreg(prod, PUC_REG_PROD) &&
    339 		    checkreg(svend, PUC_REG_SVEND) &&
    340 		    checkreg(sprod, PUC_REG_SPROD))
    341 			return (&puc_devices[i]);
    342 	}
    343 
    344 #undef checkreg
    345 
    346 	return (NULL);
    347 }
    348 
    349 static const char *
    350 puc_port_type_name(type)
    351 	int type;
    352 {
    353 
    354 	switch (type) {
    355 	case PUC_PORT_TYPE_COM:
    356 		return "com";
    357 	case PUC_PORT_TYPE_LPT:
    358 		return "lpt";
    359 	default:
    360 		panic("puc_port_type_name %d", type);
    361 	}
    362 }
    363