Home | History | Annotate | Line # | Download | only in pci
puc.c revision 1.25
      1 /*	$NetBSD: puc.c,v 1.25 2005/08/25 18:35:39 drochner 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/cdefs.h>
     56 __KERNEL_RCSID(0, "$NetBSD: puc.c,v 1.25 2005/08/25 18:35:39 drochner Exp $");
     57 
     58 #include <sys/param.h>
     59 #include <sys/systm.h>
     60 #include <sys/device.h>
     61 
     62 #include <dev/pci/pcireg.h>
     63 #include <dev/pci/pcivar.h>
     64 #include <dev/pci/pucvar.h>
     65 #include <sys/termios.h>
     66 #include <dev/ic/comreg.h>
     67 #include <dev/ic/comvar.h>
     68 
     69 #include "locators.h"
     70 #include "opt_puccn.h"
     71 
     72 struct puc_softc {
     73 	struct device		sc_dev;
     74 
     75 	/* static configuration data */
     76 	const struct puc_device_description *sc_desc;
     77 
     78 	/* card-global dynamic data */
     79 	void			*sc_ih;
     80 	struct {
     81 		int		mapped;
     82 		bus_addr_t	a;
     83 		bus_size_t	s;
     84 		bus_space_tag_t	t;
     85 		bus_space_handle_t h;
     86 	} sc_bar_mappings[6];				/* XXX constant */
     87 
     88 	/* per-port dynamic data */
     89         struct {
     90 		struct device	*dev;
     91 
     92                 /* filled in by port attachments */
     93                 int             (*ihand)(void *);
     94                 void            *ihandarg;
     95         } sc_ports[PUC_MAX_PORTS];
     96 };
     97 
     98 static int	puc_print(void *, const char *);
     99 static int	puc_submatch(struct device *, struct cfdata *,
    100 			     const locdesc_t *, void *);
    101 
    102 static const char *puc_port_type_name(int);
    103 
    104 static int
    105 puc_match(struct device *parent, struct cfdata *match, void *aux)
    106 {
    107 	struct pci_attach_args *pa = aux;
    108 	const struct puc_device_description *desc;
    109 	pcireg_t bhlc, subsys;
    110 
    111 	bhlc = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_BHLC_REG);
    112 	if (PCI_HDRTYPE_TYPE(bhlc) != 0)
    113 		return (0);
    114 
    115 	subsys = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
    116 
    117 	desc = puc_find_description(PCI_VENDOR(pa->pa_id),
    118 	    PCI_PRODUCT(pa->pa_id), PCI_VENDOR(subsys), PCI_PRODUCT(subsys));
    119 	if (desc != NULL)
    120 		return (10);
    121 
    122 #if 0
    123 	/*
    124 	 * XXX this is obviously bogus.  eventually, we might want
    125 	 * XXX to match communications/modem, etc., but that needs some
    126 	 * XXX special work in the match fn.
    127 	 */
    128 	/*
    129 	 * Match class/subclass, so we can tell people to compile kernel
    130 	 * with options that cause this driver to spew.
    131 	 */
    132 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_COMMUNICATIONS) {
    133 		switch (PCI_SUBCLASS(pa->pa_class)) {
    134 		case PCI_SUBCLASS_COMMUNICATIONS_SERIAL:
    135 		case PCI_SUBCLASS_COMMUNICATIONS_MODEM:
    136 			return (1);
    137 		}
    138 	}
    139 #endif
    140 
    141 	return (0);
    142 }
    143 
    144 static void
    145 puc_attach(struct device *parent, struct device *self, void *aux)
    146 {
    147 	struct puc_softc *sc = (struct puc_softc *)self;
    148 	struct pci_attach_args *pa = aux;
    149 	struct puc_attach_args paa;
    150 	pci_intr_handle_t intrhandle;
    151 	pcireg_t subsys;
    152 	int i, barindex;
    153 	bus_addr_t base;
    154 	bus_space_tag_t tag;
    155 #ifdef PUCCN
    156 	bus_space_handle_t ioh;
    157 #endif
    158 	int locs[PUCCF_NLOCS];
    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 #ifdef PUCCN
    210 		if (com_is_console(tag, base, &ioh)) {
    211 			sc->sc_bar_mappings[i].mapped = 1;
    212 			sc->sc_bar_mappings[i].a = base;
    213 			sc->sc_bar_mappings[i].s = COM_NPORTS;
    214 			sc->sc_bar_mappings[i].t = tag;
    215 			sc->sc_bar_mappings[i].h = ioh;
    216 			continue;
    217 		}
    218 #endif
    219 		sc->sc_bar_mappings[i].mapped = (pci_mapreg_map(pa,
    220 		    PCI_MAPREG_START + 4 * i, type, 0,
    221 		    &sc->sc_bar_mappings[i].t, &sc->sc_bar_mappings[i].h,
    222 		    &sc->sc_bar_mappings[i].a, &sc->sc_bar_mappings[i].s)
    223 		      == 0);
    224 		if (sc->sc_bar_mappings[i].mapped)
    225 			continue;
    226 
    227 		printf("%s: couldn't map BAR at offset 0x%lx\n",
    228 		    sc->sc_dev.dv_xname, (long)(PCI_MAPREG_START + 4 * i));
    229 	}
    230 
    231 	/* Map interrupt. */
    232 	if (pci_intr_map(pa, &intrhandle)) {
    233 		printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
    234 		return;
    235 	}
    236 	/*
    237 	 * XXX the sub-devices establish the interrupts, for the
    238 	 * XXX following reasons:
    239 	 * XXX
    240 	 * XXX    * we can't really know what IPLs they'd want
    241 	 * XXX
    242 	 * XXX    * the MD dispatching code can ("should") dispatch
    243 	 * XXX      chained interrupts better than we can.
    244 	 * XXX
    245 	 * XXX It would be nice if we could indicate to the MD interrupt
    246 	 * XXX handling code that the interrupt line used by the device
    247 	 * XXX was a PCI (level triggered) interrupt.
    248 	 * XXX
    249 	 * XXX It's not pretty, but hey, what is?
    250 	 */
    251 
    252 	/* Configure each port. */
    253 	for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
    254 		bus_space_handle_t subregion_handle;
    255 
    256 		/* make sure the base address register is mapped */
    257 		barindex = PUC_PORT_BAR_INDEX(sc->sc_desc->ports[i].bar);
    258 		if (!sc->sc_bar_mappings[barindex].mapped) {
    259 			printf("%s: %s port uses unmapped BAR (0x%x)\n",
    260 			    sc->sc_dev.dv_xname,
    261 			    puc_port_type_name(sc->sc_desc->ports[i].type),
    262 			    sc->sc_desc->ports[i].bar);
    263 			continue;
    264 		}
    265 
    266 		/* set up to configure the child device */
    267 		paa.port = i;
    268 		paa.type = sc->sc_desc->ports[i].type;
    269 		paa.flags = sc->sc_desc->ports[i].flags;
    270 		paa.pc = pa->pa_pc;
    271 		paa.tag = pa->pa_tag;
    272 		paa.intrhandle = intrhandle;
    273 		paa.a = sc->sc_bar_mappings[barindex].a;
    274 		paa.t = sc->sc_bar_mappings[barindex].t;
    275 		paa.dmat = pa->pa_dmat;
    276 		paa.dmat64 = pa->pa_dmat64;
    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 		locs[PUCCF_PORT] = i;
    304 
    305 		/* and configure it */
    306 		sc->sc_ports[i].dev = config_found_sm_loc(self, "puc", locs,
    307 			&paa, puc_print, puc_submatch);
    308 	}
    309 }
    310 
    311 CFATTACH_DECL(puc, sizeof(struct puc_softc),
    312     puc_match, puc_attach, NULL, NULL);
    313 
    314 static int
    315 puc_print(void *aux, const char *pnp)
    316 {
    317 	struct puc_attach_args *paa = aux;
    318 
    319 	if (pnp)
    320 		aprint_normal("%s at %s", puc_port_type_name(paa->type), pnp);
    321 	aprint_normal(" port %d", paa->port);
    322 	return (UNCONF);
    323 }
    324 
    325 static int
    326 puc_submatch(struct device *parent, struct cfdata *cf,
    327     const locdesc_t *locs, void *aux)
    328 {
    329 
    330 	if (cf->cf_loc[PUCCF_PORT] != PUCCF_PORT_DEFAULT &&
    331 	    cf->cf_loc[PUCCF_PORT] != locs[PUCCF_PORT])
    332 		return 0;
    333 
    334 	return (config_match(parent, cf, aux));
    335 }
    336 
    337 const struct puc_device_description *
    338 puc_find_description(pcireg_t vend, pcireg_t prod, pcireg_t svend,
    339     pcireg_t sprod)
    340 {
    341 	int i;
    342 
    343 #define checkreg(val, index) \
    344     (((val) & puc_devices[i].rmask[(index)]) == puc_devices[i].rval[(index)])
    345 
    346 	for (i = 0; puc_devices[i].name != NULL; i++) {
    347 		if (checkreg(vend, PUC_REG_VEND) &&
    348 		    checkreg(prod, PUC_REG_PROD) &&
    349 		    checkreg(svend, PUC_REG_SVEND) &&
    350 		    checkreg(sprod, PUC_REG_SPROD))
    351 			return (&puc_devices[i]);
    352 	}
    353 
    354 #undef checkreg
    355 
    356 	return (NULL);
    357 }
    358 
    359 static const char *
    360 puc_port_type_name(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