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