xref: /freebsd/sys/dev/puc/puc.c (revision 729362425c09cf6b362366aabc6fb547eee8035a)
1 /*	$NetBSD: puc.c,v 1.7 2000/07/29 17:43:38 jlam Exp $	*/
2 
3 /*-
4  * Copyright (c) 2002 JF Hay.  All rights reserved.
5  * Copyright (c) 2000 M. Warner Losh.  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 unmodified, this list of conditions, and the following
12  *    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  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /*
30  * Copyright (c) 1996, 1998, 1999
31  *	Christopher G. Demetriou.  All rights reserved.
32  *
33  * Redistribution and use in source and binary forms, with or without
34  * modification, are permitted provided that the following conditions
35  * are met:
36  * 1. Redistributions of source code must retain the above copyright
37  *    notice, this list of conditions and the following disclaimer.
38  * 2. Redistributions in binary form must reproduce the above copyright
39  *    notice, this list of conditions and the following disclaimer in the
40  *    documentation and/or other materials provided with the distribution.
41  * 3. All advertising materials mentioning features or use of this software
42  *    must display the following acknowledgement:
43  *      This product includes software developed by Christopher G. Demetriou
44  *	for the NetBSD Project.
45  * 4. The name of the author may not be used to endorse or promote products
46  *    derived from this software without specific prior written permission
47  *
48  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
49  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
50  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
51  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
52  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
53  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
54  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
55  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
56  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
57  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58  */
59 
60 #include <sys/cdefs.h>
61 __FBSDID("$FreeBSD$");
62 
63 /*
64  * PCI "universal" communication card device driver, glues com, lpt,
65  * and similar ports to PCI via bridge chip often much larger than
66  * the devices being glued.
67  *
68  * Author: Christopher G. Demetriou, May 14, 1998 (derived from NetBSD
69  * sys/dev/pci/pciide.c, revision 1.6).
70  *
71  * These devices could be (and some times are) described as
72  * communications/{serial,parallel}, etc. devices with known
73  * programming interfaces, but those programming interfaces (in
74  * particular the BAR assignments for devices, etc.) in fact are not
75  * particularly well defined.
76  *
77  * After I/we have seen more of these devices, it may be possible
78  * to generalize some of these bits.  In particular, devices which
79  * describe themselves as communications/serial/16[45]50, and
80  * communications/parallel/??? might be attached via direct
81  * 'com' and 'lpt' attachments to pci.
82  */
83 
84 #include "opt_puc.h"
85 
86 #include <sys/param.h>
87 #include <sys/systm.h>
88 #include <sys/kernel.h>
89 #include <sys/bus.h>
90 #include <sys/conf.h>
91 #include <sys/malloc.h>
92 
93 #include <machine/bus.h>
94 #include <machine/resource.h>
95 #include <sys/rman.h>
96 
97 #include <dev/pci/pcireg.h>
98 #include <dev/pci/pcivar.h>
99 
100 #define PUC_ENTRAILS	1
101 #include <dev/puc/pucvar.h>
102 
103 struct puc_device {
104 	struct resource_list resources;
105 	u_int serialfreq;
106 };
107 
108 static void puc_intr(void *arg);
109 
110 static int puc_find_free_unit(char *);
111 #ifdef PUC_DEBUG
112 static void puc_print_resource_list(struct resource_list *);
113 #endif
114 
115 devclass_t puc_devclass;
116 
117 static int
118 puc_port_bar_index(struct puc_softc *sc, int bar)
119 {
120 	int i;
121 
122 	for (i = 0; i < PUC_MAX_BAR; i += 1) {
123 		if (!sc->sc_bar_mappings[i].used)
124 			break;
125 		if (sc->sc_bar_mappings[i].bar == bar)
126 			return (i);
127 	}
128 	sc->sc_bar_mappings[i].bar = bar;
129 	sc->sc_bar_mappings[i].used = 1;
130 	return (i);
131 }
132 
133 static int
134 puc_probe_ilr(struct puc_softc *sc, struct resource *res)
135 {
136 	u_char t1, t2;
137 	int i;
138 
139 	switch (sc->sc_desc->ilr_type) {
140 	case PUC_ILR_TYPE_DIGI:
141 		sc->ilr_st = rman_get_bustag(res);
142 		sc->ilr_sh = rman_get_bushandle(res);
143 		for (i = 0; i < 2; i++) {
144 			t1 = bus_space_read_1(sc->ilr_st, sc->ilr_sh,
145 			    sc->sc_desc->ilr_offset[i]);
146 			t1 = ~t1;
147 			bus_space_write_1(sc->ilr_st, sc->ilr_sh,
148 			    sc->sc_desc->ilr_offset[i], t1);
149 			t2 = bus_space_read_1(sc->ilr_st, sc->ilr_sh,
150 			    sc->sc_desc->ilr_offset[i]);
151 			if (t2 == t1)
152 				return (0);
153 		}
154 		return (1);
155 
156 	default:
157 		break;
158 	}
159 	return (0);
160 }
161 
162 int
163 puc_attach(device_t dev, const struct puc_device_description *desc)
164 {
165 	char *typestr;
166 	int bidx, childunit, i, irq_setup, rid, type;
167 	struct puc_softc *sc;
168 	struct puc_device *pdev;
169 	struct resource *res;
170 	struct resource_list_entry *rle;
171 
172 	sc = (struct puc_softc *)device_get_softc(dev);
173 	bzero(sc, sizeof(*sc));
174 	sc->sc_desc = desc;
175 	if (sc->sc_desc == NULL)
176 		return (ENXIO);
177 
178 #ifdef PUC_DEBUG
179 	bootverbose = 1;
180 
181 	printf("puc: name: %s\n", sc->sc_desc->name);
182 #endif
183 	rid = 0;
184 	res = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
185 	    RF_ACTIVE | RF_SHAREABLE);
186 	if (!res)
187 		return (ENXIO);
188 
189 	sc->irqres = res;
190 	sc->irqrid = rid;
191 #ifdef PUC_FASTINTR
192 	irq_setup = BUS_SETUP_INTR(device_get_parent(dev), dev, res,
193 	    INTR_TYPE_TTY | INTR_FAST, puc_intr, sc, &sc->intr_cookie);
194 	if (irq_setup == 0)
195 		sc->fastintr = INTR_FAST;
196 	else
197 		irq_setup = BUS_SETUP_INTR(device_get_parent(dev), dev, res,
198 		    INTR_TYPE_TTY, puc_intr, sc, &sc->intr_cookie);
199 #else
200 	irq_setup = BUS_SETUP_INTR(device_get_parent(dev), dev, res,
201 	    INTR_TYPE_TTY, puc_intr, sc, &sc->intr_cookie);
202 #endif
203 	if (irq_setup != 0)
204 		return (ENXIO);
205 
206 	rid = 0;
207 	for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
208 		if (i > 0 && rid == sc->sc_desc->ports[i].bar)
209 			sc->barmuxed = 1;
210 		rid = sc->sc_desc->ports[i].bar;
211 		bidx = puc_port_bar_index(sc, rid);
212 
213 		if (sc->sc_bar_mappings[bidx].res != NULL)
214 			continue;
215 
216 		type = (sc->sc_desc->ports[i].flags & PUC_FLAGS_MEMORY)
217 		    ? SYS_RES_MEMORY : SYS_RES_IOPORT;
218 
219 		res = bus_alloc_resource(dev, type, &rid, 0ul, ~0ul, 1,
220 		    RF_ACTIVE);
221 		if (res == NULL) {
222 			printf("could not get resource\n");
223 			continue;
224 		}
225 		sc->sc_bar_mappings[bidx].type = type;
226 		sc->sc_bar_mappings[bidx].res = res;
227 
228 		if (sc->sc_desc->ilr_type != PUC_ILR_TYPE_NONE) {
229 			sc->ilr_enabled = puc_probe_ilr(sc, res);
230 			if (sc->ilr_enabled)
231 				device_printf(dev, "ILR enabled\n");
232 			else
233 				device_printf(dev, "ILR disabled\n");
234 		}
235 #ifdef PUC_DEBUG
236 		printf("%s rid %d bst %x, start %x, end %x\n",
237 		    (type == SYS_RES_MEMORY) ? "memory" : "port", rid,
238 		    (u_int)rman_get_bustag(res), (u_int)rman_get_start(res),
239 		    (u_int)rman_get_end(res));
240 #endif
241 	}
242 
243 	if (desc->init != NULL) {
244 		i = desc->init(sc);
245 		if (i != 0)
246 			return (i);
247 	}
248 
249 	for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
250 		rid = sc->sc_desc->ports[i].bar;
251 		bidx = puc_port_bar_index(sc, rid);
252 		if (sc->sc_bar_mappings[bidx].res == NULL)
253 			continue;
254 
255 		switch (sc->sc_desc->ports[i].type) {
256 		case PUC_PORT_TYPE_COM:
257 			typestr = "sio";
258 			break;
259 		default:
260 			continue;
261 		}
262 		pdev = malloc(sizeof(struct puc_device), M_DEVBUF,
263 		    M_NOWAIT | M_ZERO);
264 		if (!pdev)
265 			continue;
266 		resource_list_init(&pdev->resources);
267 
268 		/* First fake up an IRQ resource. */
269 		resource_list_add(&pdev->resources, SYS_RES_IRQ, 0,
270 		    rman_get_start(sc->irqres), rman_get_end(sc->irqres),
271 		    rman_get_end(sc->irqres) - rman_get_start(sc->irqres) + 1);
272 		rle = resource_list_find(&pdev->resources, SYS_RES_IRQ, 0);
273 		rle->res = sc->irqres;
274 
275 		/* Now fake an IOPORT or MEMORY resource */
276 		res = sc->sc_bar_mappings[bidx].res;
277 		type = sc->sc_bar_mappings[bidx].type;
278 		resource_list_add(&pdev->resources, type, 0,
279 		    rman_get_start(res) + sc->sc_desc->ports[i].offset,
280 		    rman_get_start(res) + sc->sc_desc->ports[i].offset + 8 - 1,
281 		    8);
282 		rle = resource_list_find(&pdev->resources, type, 0);
283 
284 		if (sc->barmuxed == 0) {
285 			rle->res = sc->sc_bar_mappings[bidx].res;
286 		} else {
287 			rle->res = malloc(sizeof(struct resource), M_DEVBUF,
288 			    M_WAITOK | M_ZERO);
289 			if (rle->res == NULL) {
290 				free(pdev, M_DEVBUF);
291 				return (ENOMEM);
292 			}
293 
294 			rle->res->r_start = rman_get_start(res) +
295 			    sc->sc_desc->ports[i].offset;
296 			rle->res->r_end = rle->res->r_start + 8 - 1;
297 			rle->res->r_bustag = rman_get_bustag(res);
298 			bus_space_subregion(rle->res->r_bustag,
299 			    rman_get_bushandle(res),
300 			    sc->sc_desc->ports[i].offset, 8,
301 			    &rle->res->r_bushandle);
302 		}
303 
304 		pdev->serialfreq = sc->sc_desc->ports[i].serialfreq;
305 
306 		childunit = puc_find_free_unit(typestr);
307 		sc->sc_ports[i].dev = device_add_child(dev, typestr, childunit);
308 		if (sc->sc_ports[i].dev == NULL) {
309 			if (sc->barmuxed) {
310 				bus_space_unmap(rman_get_bustag(rle->res),
311 				    rman_get_bushandle(rle->res), 8);
312 				free(rle->res, M_DEVBUF);
313 				free(pdev, M_DEVBUF);
314 			}
315 			continue;
316 		}
317 		device_set_ivars(sc->sc_ports[i].dev, pdev);
318 		device_set_desc(sc->sc_ports[i].dev, sc->sc_desc->name);
319 		if (!bootverbose)
320 			device_quiet(sc->sc_ports[i].dev);
321 #ifdef PUC_DEBUG
322 		printf("puc: type %d, bar %x, offset %x\n",
323 		    sc->sc_desc->ports[i].type,
324 		    sc->sc_desc->ports[i].bar,
325 		    sc->sc_desc->ports[i].offset);
326 		puc_print_resource_list(&pdev->resources);
327 #endif
328 		device_set_flags(sc->sc_ports[i].dev,
329 		    sc->sc_desc->ports[i].flags);
330 		if (device_probe_and_attach(sc->sc_ports[i].dev) != 0) {
331 			if (sc->barmuxed) {
332 				bus_space_unmap(rman_get_bustag(rle->res),
333 						rman_get_bushandle(rle->res),
334 						8);
335 				free(rle->res, M_DEVBUF);
336 				free(pdev, M_DEVBUF);
337 			}
338 		}
339 	}
340 
341 #ifdef PUC_DEBUG
342 	bootverbose = 0;
343 #endif
344 	return (0);
345 }
346 
347 static u_int32_t
348 puc_ilr_read(struct puc_softc *sc)
349 {
350 	u_int32_t mask;
351 	int i;
352 
353 	mask = 0;
354 	switch (sc->sc_desc->ilr_type) {
355 	case PUC_ILR_TYPE_DIGI:
356 		for (i = 1; i >= 0; i--) {
357 			mask = (mask << 8) | (bus_space_read_1(sc->ilr_st,
358 			    sc->ilr_sh, sc->sc_desc->ilr_offset[i]) & 0xff);
359 		}
360 		break;
361 
362 	default:
363 		mask = 0xffffffff;
364 		break;
365 	}
366 	return (mask);
367 }
368 
369 /*
370  * This is an interrupt handler. For boards that can't tell us which
371  * device generated the interrupt it just calls all the registered
372  * handlers sequencially, but for boards that can tell us which
373  * device(s) generated the interrupt it calls only handlers for devices
374  * that actually generated the interrupt.
375  */
376 static void
377 puc_intr(void *arg)
378 {
379 	int i;
380 	u_int32_t ilr_mask;
381 	struct puc_softc *sc;
382 
383 	sc = (struct puc_softc *)arg;
384 	ilr_mask = sc->ilr_enabled ? puc_ilr_read(sc) : 0xffffffff;
385 	for (i = 0; i < PUC_MAX_PORTS; i++)
386 		if (sc->sc_ports[i].ihand != NULL &&
387 		    ((ilr_mask >> i) & 0x00000001))
388 			(sc->sc_ports[i].ihand)(sc->sc_ports[i].ihandarg);
389 }
390 
391 const struct puc_device_description *
392 puc_find_description(uint32_t vend, uint32_t prod, uint32_t svend,
393     uint32_t sprod)
394 {
395 	int i;
396 
397 #define checkreg(val, index) \
398     (((val) & puc_devices[i].rmask[(index)]) == puc_devices[i].rval[(index)])
399 
400 	for (i = 0; puc_devices[i].name != NULL; i++) {
401 		if (checkreg(vend, PUC_REG_VEND) &&
402 		    checkreg(prod, PUC_REG_PROD) &&
403 		    checkreg(svend, PUC_REG_SVEND) &&
404 		    checkreg(sprod, PUC_REG_SPROD))
405 			return (&puc_devices[i]);
406 	}
407 
408 #undef checkreg
409 
410 	return (NULL);
411 }
412 
413 static int
414 puc_find_free_unit(char *name)
415 {
416 	devclass_t dc;
417 	int start;
418 	int unit;
419 
420 	unit = 0;
421 	start = 0;
422 	while (resource_int_value(name, unit, "port", &start) == 0 &&
423 	    start > 0)
424 		unit++;
425 	dc = devclass_find(name);
426 	if (dc == NULL)
427 		return (-1);
428 	while (devclass_get_device(dc, unit))
429 		unit++;
430 #ifdef PUC_DEBUG
431 	printf("puc: Using %s%d\n", name, unit);
432 #endif
433 	return (unit);
434 }
435 
436 #ifdef PUC_DEBUG
437 static void
438 puc_print_resource_list(struct resource_list *rl)
439 {
440 #if 0
441 	struct resource_list_entry *rle;
442 
443 	printf("print_resource_list: rl %p\n", rl);
444 	SLIST_FOREACH(rle, rl, link)
445 		printf("  type %x, rid %x start %x end %x count %x\n",
446 		    rle->type, rle->rid, rle->start, rle->end, rle->count);
447 	printf("print_resource_list: end.\n");
448 #endif
449 }
450 #endif
451 
452 struct resource *
453 puc_alloc_resource(device_t dev, device_t child, int type, int *rid,
454     u_long start, u_long end, u_long count, u_int flags)
455 {
456 	struct puc_device *pdev;
457 	struct resource *retval;
458 	struct resource_list *rl;
459 	struct resource_list_entry *rle;
460 
461 	pdev = device_get_ivars(child);
462 	rl = &pdev->resources;
463 
464 #ifdef PUC_DEBUG
465 	printf("puc_alloc_resource: pdev %p, looking for t %x, r %x\n",
466 	    pdev, type, *rid);
467 	puc_print_resource_list(rl);
468 #endif
469 	retval = NULL;
470 	rle = resource_list_find(rl, type, *rid);
471 	if (rle) {
472 		start = rle->start;
473 		end = rle->end;
474 		count = rle->count;
475 #ifdef PUC_DEBUG
476 		printf("found rle, %lx, %lx, %lx\n", start, end, count);
477 #endif
478 		retval = rle->res;
479 	} else
480 		printf("oops rle is gone\n");
481 
482 	return (retval);
483 }
484 
485 int
486 puc_release_resource(device_t dev, device_t child, int type, int rid,
487     struct resource *res)
488 {
489 	return (0);
490 }
491 
492 int
493 puc_get_resource(device_t dev, device_t child, int type, int rid,
494     u_long *startp, u_long *countp)
495 {
496 	struct puc_device *pdev;
497 	struct resource_list *rl;
498 	struct resource_list_entry *rle;
499 
500 	pdev = device_get_ivars(child);
501 	rl = &pdev->resources;
502 
503 #ifdef PUC_DEBUG
504 	printf("puc_get_resource: pdev %p, looking for t %x, r %x\n", pdev,
505 	    type, rid);
506 	puc_print_resource_list(rl);
507 #endif
508 	rle = resource_list_find(rl, type, rid);
509 	if (rle) {
510 #ifdef PUC_DEBUG
511 		printf("found rle %p,", rle);
512 #endif
513 		if (startp != NULL)
514 			*startp = rle->start;
515 		if (countp != NULL)
516 			*countp = rle->count;
517 #ifdef PUC_DEBUG
518 		printf(" %lx, %lx\n", rle->start, rle->count);
519 #endif
520 		return (0);
521 	} else
522 		printf("oops rle is gone\n");
523 	return (ENXIO);
524 }
525 
526 int
527 puc_setup_intr(device_t dev, device_t child, struct resource *r, int flags,
528 	       void (*ihand)(void *), void *arg, void **cookiep)
529 {
530 	int i;
531 	struct puc_softc *sc;
532 
533 	sc = (struct puc_softc *)device_get_softc(dev);
534 	if ((flags & INTR_FAST) != sc->fastintr)
535 		return (ENXIO);
536 	for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
537 		if (sc->sc_ports[i].dev == child) {
538 			if (sc->sc_ports[i].ihand != 0)
539 				return (ENXIO);
540 			sc->sc_ports[i].ihand = ihand;
541 			sc->sc_ports[i].ihandarg = arg;
542 			*cookiep = arg;
543 			return (0);
544 		}
545 	}
546 	return (ENXIO);
547 }
548 
549 int
550 puc_teardown_intr(device_t dev, device_t child, struct resource *r,
551 		  void *cookie)
552 {
553 	int i;
554 	struct puc_softc *sc;
555 
556 	sc = (struct puc_softc *)device_get_softc(dev);
557 	for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
558 		if (sc->sc_ports[i].dev == child) {
559 			sc->sc_ports[i].ihand = NULL;
560 			sc->sc_ports[i].ihandarg = NULL;
561 			return (0);
562 		}
563 	}
564 	return (ENXIO);
565 }
566 
567 int
568 puc_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
569 {
570 	struct puc_device *pdev;
571 
572 	pdev = device_get_ivars(child);
573 	if (pdev == NULL)
574 		return (ENOENT);
575 
576 	switch(index) {
577 	case PUC_IVAR_FREQ:
578 		*result = pdev->serialfreq;
579 		break;
580 	default:
581 		return (ENOENT);
582 	}
583 	return (0);
584 }
585