xref: /openbsd-src/sys/dev/pci/puc.c (revision 46035553bfdd96e63c94e32da0210227ec2e3cf1)
1 /*	$OpenBSD: puc.c,v 1.30 2020/08/14 18:14:11 jcs Exp $	*/
2 /*	$NetBSD: puc.c,v 1.3 1999/02/06 06:29:54 cgd Exp $	*/
3 
4 /*
5  * Copyright (c) 1996, 1998, 1999
6  *	Christopher G. Demetriou.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by Christopher G. Demetriou
19  *	for the NetBSD Project.
20  * 4. The name of the author may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 /*
36  * PCI "universal" communication card device driver, glues com, lpt,
37  * and similar ports to PCI via bridge chip often much larger than
38  * the devices being glued.
39  *
40  * Author: Christopher G. Demetriou, May 14, 1998 (derived from NetBSD
41  * sys/dev/pci/pciide.c, revision 1.6).
42  *
43  * These devices could be (and some times are) described as
44  * communications/{serial,parallel}, etc. devices with known
45  * programming interfaces, but those programming interfaces (in
46  * particular the BAR assignments for devices, etc.) in fact are not
47  * particularly well defined.
48  *
49  * After I/we have seen more of these devices, it may be possible
50  * to generalize some of these bits.  In particular, devices which
51  * describe themselves as communications/serial/16[45]50, and
52  * communications/parallel/??? might be attached via direct
53  * 'com' and 'lpt' attachments to pci.
54  */
55 
56 #include <sys/param.h>
57 #include <sys/systm.h>
58 #include <sys/device.h>
59 #include <sys/tty.h>
60 
61 #include <dev/pci/pcireg.h>
62 #include <dev/pci/pcivar.h>
63 #include <dev/pci/pucvar.h>
64 #include <dev/pci/pcidevs.h>
65 
66 #include <dev/ic/comreg.h>
67 #include <dev/ic/comvar.h>
68 
69 #include "com.h"
70 
71 struct puc_pci_softc {
72 	struct puc_softc	sc_psc;
73 
74 	pci_chipset_tag_t	pc;
75 	pci_intr_handle_t	ih;
76 };
77 
78 int	puc_pci_match(struct device *, void *, void *);
79 void	puc_pci_attach(struct device *, struct device *, void *);
80 int	puc_pci_detach(struct device *, int);
81 const char *puc_pci_intr_string(struct puc_attach_args *);
82 void	*puc_pci_intr_establish(struct puc_attach_args *, int,
83     int (*)(void *), void *, char *);
84 int	puc_pci_xr17v35x_intr(void *arg);
85 
86 struct cfattach puc_pci_ca = {
87 	sizeof(struct puc_pci_softc), puc_pci_match,
88 	puc_pci_attach, puc_pci_detach
89 };
90 
91 struct cfdriver puc_cd = {
92 	NULL, "puc", DV_DULL
93 };
94 
95 const char *puc_port_type_name(int);
96 
97 int
98 puc_pci_match(struct device *parent, void *match, void *aux)
99 {
100 	struct pci_attach_args *pa = aux;
101 	const struct puc_device_description *desc;
102 	pcireg_t bhlc, subsys;
103 
104 	bhlc = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_BHLC_REG);
105 	if (PCI_HDRTYPE_TYPE(bhlc) != 0)
106 		return (0);
107 
108 	subsys = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
109 
110 	desc = puc_find_description(PCI_VENDOR(pa->pa_id),
111 	    PCI_PRODUCT(pa->pa_id), PCI_VENDOR(subsys), PCI_PRODUCT(subsys));
112 	if (desc != NULL)
113 		return (1);
114 
115 	return (0);
116 }
117 
118 const char *
119 puc_pci_intr_string(struct puc_attach_args *paa)
120 {
121 	struct puc_pci_softc *sc = paa->puc;
122 
123 	return (pci_intr_string(sc->pc, sc->ih));
124 }
125 
126 void *
127 puc_pci_intr_establish(struct puc_attach_args *paa, int type,
128     int (*func)(void *), void *arg, char *name)
129 {
130 	struct puc_pci_softc *sc = paa->puc;
131 	struct puc_softc *psc = &sc->sc_psc;
132 
133 	if (psc->sc_xr17v35x) {
134 		psc->sc_ports[paa->port].real_intrhand = func;
135 		psc->sc_ports[paa->port].real_intrhand_arg = arg;
136 		if (paa->port == 0)
137 			psc->sc_ports[paa->port].intrhand =
138 			    pci_intr_establish(sc->pc, sc->ih, type,
139 			    puc_pci_xr17v35x_intr, sc, name);
140 		return (psc->sc_ports[paa->port].real_intrhand);
141 	}
142 
143 	psc->sc_ports[paa->port].intrhand =
144 	    pci_intr_establish(sc->pc, sc->ih, type, func, arg, name);
145 
146 	return (psc->sc_ports[paa->port].intrhand);
147 }
148 
149 void
150 puc_pci_attach(struct device *parent, struct device *self, void *aux)
151 {
152 	struct puc_pci_softc *psc = (struct puc_pci_softc *)self;
153 	struct puc_softc *sc = &psc->sc_psc;
154 	struct pci_attach_args *pa = aux;
155 	struct puc_attach_args paa;
156 	pcireg_t subsys;
157 	int i;
158 
159 	subsys = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
160 	sc->sc_desc = puc_find_description(PCI_VENDOR(pa->pa_id),
161 	    PCI_PRODUCT(pa->pa_id), PCI_VENDOR(subsys), PCI_PRODUCT(subsys));
162 
163 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_EXAR &&
164 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_EXAR_XR17V354)
165 		sc->sc_xr17v35x = 1;
166 
167 	puc_print_ports(sc->sc_desc);
168 
169 	for (i = 0; i < PUC_NBARS; i++) {
170 		pcireg_t type;
171 		int bar;
172 
173 		sc->sc_bar_mappings[i].mapped = 0;
174 		bar = PCI_MAPREG_START + 4 * i;
175 		if (!pci_mapreg_probe(pa->pa_pc, pa->pa_tag, bar, &type))
176 			continue;
177 
178 		sc->sc_bar_mappings[i].mapped = (pci_mapreg_map(pa, bar, type,
179 		    0, &sc->sc_bar_mappings[i].t, &sc->sc_bar_mappings[i].h,
180 		    &sc->sc_bar_mappings[i].a, &sc->sc_bar_mappings[i].s, 0)
181 		      == 0);
182 		if (sc->sc_bar_mappings[i].mapped) {
183 			if (type == PCI_MAPREG_MEM_TYPE_64BIT)
184 				i++;
185 			continue;
186 		}
187 
188 #if NCOM > 0
189 		/*
190 		 * If a port on this card is used as serial console,
191 		 * mapping the associated BAR will fail because the
192 		 * bus space is already mapped.  In that case, we try
193 		 * to re-use the already existing mapping.
194 		 * Unfortunately this means that if a BAR is used to
195 		 * support multiple ports, only the first port will
196 		 * work.
197 		 */
198 		if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, bar, type,
199 		    &sc->sc_bar_mappings[i].a, NULL, NULL) == 0 &&
200 		    pa->pa_iot == comconsiot &&
201 		    sc->sc_bar_mappings[i].a == comconsaddr) {
202 			sc->sc_bar_mappings[i].t = comconsiot;
203 			sc->sc_bar_mappings[i].h = comconsioh;
204 			sc->sc_bar_mappings[i].s = COM_NPORTS;
205 			sc->sc_bar_mappings[i].mapped = 1;
206 			if (type == PCI_MAPREG_MEM_TYPE_64BIT)
207 				i++;
208 			continue;
209 		}
210 #endif
211 
212 		printf("%s: couldn't map BAR at offset 0x%lx\n",
213 		    sc->sc_dev.dv_xname, (long)bar);
214 	}
215 
216 	/* Map interrupt. */
217 	psc->pc = pa->pa_pc;
218 	if (pci_intr_map(pa, &psc->ih)) {
219 		printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
220 		return;
221 	}
222 
223 	paa.puc = sc;
224 	paa.intr_string = &puc_pci_intr_string;
225 	paa.intr_establish = &puc_pci_intr_establish;
226 
227 	puc_common_attach(sc, &paa);
228 }
229 
230 void
231 puc_common_attach(struct puc_softc *sc, struct puc_attach_args *paa)
232 {
233 	const struct puc_device_description *desc = sc->sc_desc;
234 	int i, bar;
235 
236 	/* Configure each port. */
237 	for (i = 0; i < PUC_MAX_PORTS; i++) {
238 		if (desc->ports[i].type == 0)	/* neither com or lpt */
239 			continue;
240 		/* make sure the base address register is mapped */
241 		bar = PUC_PORT_BAR_INDEX(desc->ports[i].bar);
242 		if (!sc->sc_bar_mappings[bar].mapped) {
243 			printf("%s: %s port uses unmapped BAR (0x%x)\n",
244 			    sc->sc_dev.dv_xname,
245 			    puc_port_type_name(desc->ports[i].type),
246 			    desc->ports[i].bar);
247 			continue;
248 		}
249 
250 		/* set up to configure the child device */
251 		paa->port = i;
252 		paa->a = sc->sc_bar_mappings[bar].a;
253 		paa->t = sc->sc_bar_mappings[bar].t;
254 
255 		paa->type = desc->ports[i].type;
256 
257 		if (desc->ports[i].offset >= sc->sc_bar_mappings[bar].s ||
258 		    bus_space_subregion(sc->sc_bar_mappings[bar].t,
259 		    sc->sc_bar_mappings[bar].h, desc->ports[i].offset,
260 		    sc->sc_bar_mappings[bar].s - desc->ports[i].offset,
261 		    &paa->h)) {
262 			printf("%s: couldn't get subregion for port %d\n",
263 			    sc->sc_dev.dv_xname, i);
264 			continue;
265 		}
266 
267 #if 0
268 		if (autoconf_verbose)
269 			printf("%s: port %d: %s @ (index %d) 0x%x "
270 			    "(0x%lx, 0x%lx)\n", sc->sc_dev.dv_xname, paa->port,
271 			    puc_port_type_name(paa->type), bar, (int)paa->a,
272 			    (long)paa->t, (long)paa->h);
273 #endif
274 
275 		/* and configure it */
276 		sc->sc_ports[i].dev = config_found_sm(&sc->sc_dev, paa,
277 		    puc_print, puc_submatch);
278 	}
279 }
280 
281 int
282 puc_pci_detach(struct device *self, int flags)
283 {
284 	struct puc_pci_softc *sc = (struct puc_pci_softc *)self;
285 	struct puc_softc *psc = &sc->sc_psc;
286 	int i, rv;
287 
288 	for (i = PUC_MAX_PORTS; i--; ) {
289 		if (psc->sc_ports[i].intrhand)
290 			pci_intr_disestablish(sc->pc,
291 			    psc->sc_ports[i].intrhand);
292 		if (psc->sc_ports[i].dev)
293 			if ((rv = config_detach(psc->sc_ports[i].dev, flags)))
294 				return (rv);
295 	}
296 
297 	for (i = PUC_NBARS; i--; )
298 		if (psc->sc_bar_mappings[i].mapped)
299 			bus_space_unmap(psc->sc_bar_mappings[i].t,
300 			    psc->sc_bar_mappings[i].h,
301 			    psc->sc_bar_mappings[i].s);
302 
303 	return (0);
304 }
305 
306 int
307 puc_print(void *aux, const char *pnp)
308 {
309 	struct puc_attach_args *paa = aux;
310 
311 	if (pnp)
312 		printf("%s at %s", puc_port_type_name(paa->type), pnp);
313 	printf(" port %d", paa->port);
314 	return (UNCONF);
315 }
316 
317 int
318 puc_submatch(struct device *parent, void *vcf, void *aux)
319 {
320 	struct cfdata *cf = (struct cfdata *)vcf;
321 	struct puc_attach_args *aa = aux;
322 
323 	if (cf->cf_loc[0] != -1 && cf->cf_loc[0] != aa->port)
324 		return 0;
325 	return ((*cf->cf_attach->ca_match)(parent, cf, aux));
326 }
327 
328 const struct puc_device_description *
329 puc_find_description(u_int16_t vend, u_int16_t prod,
330     u_int16_t svend, u_int16_t sprod)
331 {
332 	int i;
333 
334 	for (i = 0; i < puc_ndevs; i++)
335 		if ((vend & puc_devs[i].rmask[0]) == puc_devs[i].rval[0] &&
336 		    (prod & puc_devs[i].rmask[1]) == puc_devs[i].rval[1] &&
337 		    (svend & puc_devs[i].rmask[2]) == puc_devs[i].rval[2] &&
338 		    (sprod & puc_devs[i].rmask[3]) == puc_devs[i].rval[3])
339 			return (&puc_devs[i]);
340 
341 	return (NULL);
342 }
343 
344 const char *
345 puc_port_type_name(int type)
346 {
347 	if (PUC_IS_COM(type))
348 		return "com";
349 	if (PUC_IS_LPT(type))
350 		return "lpt";
351 	return (NULL);
352 }
353 
354 void
355 puc_print_ports(const struct puc_device_description *desc)
356 {
357 	int i, ncom, nlpt;
358 
359 	printf(": ports: ");
360 	for (i = ncom = nlpt = 0; i < PUC_MAX_PORTS; i++) {
361 		if (PUC_IS_COM(desc->ports[i].type))
362 			ncom++;
363 		else if (PUC_IS_LPT(desc->ports[i].type))
364 			nlpt++;
365 	}
366 	if (ncom)
367 		printf("%d com", ncom);
368 	if (nlpt) {
369 		if (ncom)
370 			printf(", ");
371 		printf("%d lpt", nlpt);
372 	}
373 	printf("\n");
374 }
375 
376 int
377 puc_pci_xr17v35x_intr(void *arg)
378 {
379 	struct puc_pci_softc *sc = arg;
380 	struct puc_softc *psc = &sc->sc_psc;
381 	int ports, i;
382 
383 	ports = bus_space_read_1(psc->sc_bar_mappings[0].t,
384 	    psc->sc_bar_mappings[0].h, UART_EXAR_INT0);
385 
386 	for (i = 0; i < 8; i++) {
387 		if ((ports & (1 << i)) && psc->sc_ports[i].real_intrhand)
388 			(*(psc->sc_ports[i].real_intrhand))(
389 			    psc->sc_ports[i].real_intrhand_arg);
390 	}
391 
392 	return (1);
393 }
394