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