puc.c revision 102717
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: head/sys/dev/puc/puc.c 102717 2002-08-31 18:42:31Z phk $");
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 <sys/param.h>
85#include <sys/systm.h>
86#include <sys/kernel.h>
87#include <sys/bus.h>
88#include <sys/conf.h>
89#include <sys/malloc.h>
90
91#include <machine/bus.h>
92#include <machine/resource.h>
93#include <sys/rman.h>
94
95#include <dev/pci/pcireg.h>
96#include <dev/pci/pcivar.h>
97
98#define PUC_ENTRAILS	1
99#include <dev/puc/pucvar.h>
100
101#include <opt_puc.h>
102
103
104struct puc_device {
105	struct resource_list resources;
106	u_int serialfreq;
107};
108
109static void puc_intr(void *arg);
110
111static void puc_config_superio(device_t);
112static void puc_config_win877(struct resource *);
113static int puc_find_free_unit(char *);
114#ifdef PUC_DEBUG
115static void puc_print_win877(bus_space_tag_t, bus_space_handle_t, u_int,
116    u_int);
117static void puc_print_resource_list(struct resource_list *);
118#endif
119
120int
121puc_attach(device_t dev, const struct puc_device_description *desc)
122{
123	char *typestr;
124	int bidx, childunit, i, irq_setup, rid;
125	struct puc_softc *sc;
126	struct puc_device *pdev;
127	struct resource *res;
128	struct resource_list_entry *rle;
129
130	sc = (struct puc_softc *)device_get_softc(dev);
131	bzero(sc, sizeof(*sc));
132	sc->sc_desc = desc;
133
134	if (sc->sc_desc == NULL)
135		return (ENXIO);
136
137#ifdef PUC_DEBUG
138	bootverbose = 1;
139
140	printf("puc: name: %s\n", sc->sc_desc->name);
141#endif
142	rid = 0;
143	res = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
144	    RF_ACTIVE | RF_SHAREABLE);
145	if (!res)
146		return (ENXIO);
147
148	sc->irqres = res;
149	sc->irqrid = rid;
150#ifdef PUC_FASTINTR
151	irq_setup = BUS_SETUP_INTR(device_get_parent(dev), dev, res,
152	    INTR_TYPE_TTY | INTR_FAST, puc_intr, sc, &sc->intr_cookie);
153#else
154	irq_setup = ENXIO;
155#endif
156	if (irq_setup != 0)
157		irq_setup = BUS_SETUP_INTR(device_get_parent(dev), dev, res,
158		    INTR_TYPE_TTY, puc_intr, sc, &sc->intr_cookie);
159	if (irq_setup != 0)
160		return (ENXIO);
161
162	rid = 0;
163	for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
164		if (rid == sc->sc_desc->ports[i].bar)
165			sc->barmuxed = 1;
166		rid = sc->sc_desc->ports[i].bar;
167		bidx = PUC_PORT_BAR_INDEX(rid);
168
169		if (sc->sc_bar_mappings[bidx].res != NULL)
170			continue;
171		res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
172		    0ul, ~0ul, 1, RF_ACTIVE);
173		if (res == NULL) {
174			printf("could not get resource\n");
175			continue;
176		}
177		sc->sc_bar_mappings[bidx].res = res;
178#ifdef PUC_DEBUG
179		printf("port bst %x, start %x, end %x\n",
180		    (u_int)rman_get_bustag(res), (u_int)rman_get_start(res),
181		    (u_int)rman_get_end(res));
182#endif
183	}
184
185	puc_config_superio(dev);
186
187	for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
188		rid = sc->sc_desc->ports[i].bar;
189		bidx = PUC_PORT_BAR_INDEX(rid);
190		if (sc->sc_bar_mappings[bidx].res == NULL)
191			continue;
192
193		switch (sc->sc_desc->ports[i].type) {
194		case PUC_PORT_TYPE_COM:
195			typestr = "sio";
196			break;
197		default:
198			continue;
199		}
200		pdev = malloc(sizeof(struct puc_device), M_DEVBUF,
201		    M_NOWAIT | M_ZERO);
202		if (!pdev)
203			continue;
204		resource_list_init(&pdev->resources);
205
206		/* First fake up an IRQ resource. */
207		resource_list_add(&pdev->resources, SYS_RES_IRQ, 0,
208		    rman_get_start(sc->irqres), rman_get_end(sc->irqres),
209		    rman_get_end(sc->irqres) - rman_get_start(sc->irqres) + 1);
210		rle = resource_list_find(&pdev->resources, SYS_RES_IRQ, 0);
211		rle->res = sc->irqres;
212
213		/* Now fake an IOPORT resource */
214		res = sc->sc_bar_mappings[bidx].res;
215		resource_list_add(&pdev->resources, SYS_RES_IOPORT, 0,
216		    rman_get_start(res) + sc->sc_desc->ports[i].offset,
217		    rman_get_end(res) + sc->sc_desc->ports[i].offset + 8 - 1,
218		    8);
219		rle = resource_list_find(&pdev->resources, SYS_RES_IOPORT, 0);
220
221		if (sc->barmuxed == 0) {
222			rle->res = sc->sc_bar_mappings[bidx].res;
223		} else {
224			rle->res = malloc(sizeof(struct resource), M_DEVBUF,
225			    M_WAITOK | M_ZERO);
226			if (rle->res == NULL) {
227				free(pdev, M_DEVBUF);
228				return (ENOMEM);
229			}
230
231			rle->res->r_start = rman_get_start(res) +
232			    sc->sc_desc->ports[i].offset;
233			rle->res->r_end = rle->res->r_start + 8 - 1;
234			rle->res->r_bustag = rman_get_bustag(res);
235			bus_space_subregion(rle->res->r_bustag,
236			    rman_get_bushandle(res),
237			    sc->sc_desc->ports[i].offset, 8,
238			    &rle->res->r_bushandle);
239		}
240
241		pdev->serialfreq = sc->sc_desc->ports[i].serialfreq;
242
243		childunit = puc_find_free_unit(typestr);
244		sc->sc_ports[i].dev = device_add_child(dev, typestr, childunit);
245		if (sc->sc_ports[i].dev == NULL) {
246			if (sc->barmuxed) {
247				bus_space_unmap(rman_get_bustag(rle->res),
248						rman_get_bushandle(rle->res),
249						8);
250				free(rle->res, M_DEVBUF);
251				free(pdev, M_DEVBUF);
252			}
253			continue;
254		}
255		device_set_ivars(sc->sc_ports[i].dev, pdev);
256		device_set_desc(sc->sc_ports[i].dev, sc->sc_desc->name);
257		if (!bootverbose)
258			device_quiet(sc->sc_ports[i].dev);
259#ifdef PUC_DEBUG
260		printf("puc: type %d, bar %x, offset %x\n",
261		    sc->sc_desc->ports[i].type,
262		    sc->sc_desc->ports[i].bar,
263		    sc->sc_desc->ports[i].offset);
264		puc_print_resource_list(&pdev->resources);
265#endif
266		if (device_probe_and_attach(sc->sc_ports[i].dev) != 0) {
267			if (sc->barmuxed) {
268				bus_space_unmap(rman_get_bustag(rle->res),
269						rman_get_bushandle(rle->res),
270						8);
271				free(rle->res, M_DEVBUF);
272				free(pdev, M_DEVBUF);
273			}
274		}
275	}
276
277#ifdef PUC_DEBUG
278	bootverbose = 0;
279#endif
280	return (0);
281}
282
283/*
284 * This is just an brute force interrupt handler. It just calls all the
285 * registered handlers sequencially.
286 *
287 * Later on we should maybe have a different handler for boards that can
288 * tell us which device generated the interrupt.
289 */
290static void
291puc_intr(void *arg)
292{
293	int i;
294	struct puc_softc *sc;
295
296	sc = (struct puc_softc *)arg;
297	for (i = 0; i < PUC_MAX_PORTS; i++)
298		if (sc->sc_ports[i].ihand != NULL)
299			(sc->sc_ports[i].ihand)(sc->sc_ports[i].ihandarg);
300}
301
302const struct puc_device_description *
303puc_find_description(uint32_t vend, uint32_t prod, uint32_t svend,
304    uint32_t sprod)
305{
306	int i;
307
308#define checkreg(val, index) \
309    (((val) & puc_devices[i].rmask[(index)]) == puc_devices[i].rval[(index)])
310
311	for (i = 0; puc_devices[i].name != NULL; i++) {
312		if (checkreg(vend, PUC_REG_VEND) &&
313		    checkreg(prod, PUC_REG_PROD) &&
314		    checkreg(svend, PUC_REG_SVEND) &&
315		    checkreg(sprod, PUC_REG_SPROD))
316			return (&puc_devices[i]);
317	}
318
319#undef checkreg
320
321	return (NULL);
322}
323
324/*
325 * It might be possible to make these more generic if we can detect patterns.
326 * For instance maybe if the size of a bar is 0x400 (the old isa space) it
327 * might contain one or more superio chips.
328 */
329static void
330puc_config_superio(device_t dev)
331{
332	struct puc_softc *sc = (struct puc_softc *)device_get_softc(dev);
333
334	if (sc->sc_desc->rval[PUC_REG_VEND] == 0x1592 &&
335	    sc->sc_desc->rval[PUC_REG_PROD] == 0x0781)
336		puc_config_win877(sc->sc_bar_mappings[0].res);
337}
338
339#define rdspio(indx)		(bus_space_write_1(bst, bsh, efir, indx), \
340				bus_space_read_1(bst, bsh, efdr))
341#define wrspio(indx,data)	(bus_space_write_1(bst, bsh, efir, indx), \
342				bus_space_write_1(bst, bsh, efdr, data))
343
344#ifdef PUC_DEBUG
345static void
346puc_print_win877(bus_space_tag_t bst, bus_space_handle_t bsh, u_int efir,
347	u_int efdr)
348{
349	u_char cr00, cr01, cr04, cr09, cr0d, cr14, cr15, cr16, cr17;
350	u_char cr18, cr19, cr24, cr25, cr28, cr2c, cr31, cr32;
351
352	cr00 = rdspio(0x00);
353	cr01 = rdspio(0x01);
354	cr04 = rdspio(0x04);
355	cr09 = rdspio(0x09);
356	cr0d = rdspio(0x0d);
357	cr14 = rdspio(0x14);
358	cr15 = rdspio(0x15);
359	cr16 = rdspio(0x16);
360	cr17 = rdspio(0x17);
361	cr18 = rdspio(0x18);
362	cr19 = rdspio(0x19);
363	cr24 = rdspio(0x24);
364	cr25 = rdspio(0x25);
365	cr28 = rdspio(0x28);
366	cr2c = rdspio(0x2c);
367	cr31 = rdspio(0x31);
368	cr32 = rdspio(0x32);
369	printf("877T: cr00 %x, cr01 %x, cr04 %x, cr09 %x, cr0d %x, cr14 %x, "
370	    "cr15 %x, cr16 %x, cr17 %x, cr18 %x, cr19 %x, cr24 %x, cr25 %x, "
371	    "cr28 %x, cr2c %x, cr31 %x, cr32 %x\n", cr00, cr01, cr04, cr09,
372	    cr0d, cr14, cr15, cr16, cr17,
373	    cr18, cr19, cr24, cr25, cr28, cr2c, cr31, cr32);
374}
375#endif
376
377static void
378puc_config_win877(struct resource *res)
379{
380	u_char val;
381	u_int efir, efdr;
382	bus_space_tag_t bst;
383	bus_space_handle_t bsh;
384
385	bst = rman_get_bustag(res);
386	bsh = rman_get_bushandle(res);
387
388	/* configure the first W83877TF */
389	bus_space_write_1(bst, bsh, 0x250, 0x89);
390	efir = 0x251;
391	efdr = 0x252;
392	val = rdspio(0x09) & 0x0f;
393	if (val != 0x0c) {
394		printf("conf_win877: Oops not a W83877TF\n");
395		return;
396	}
397
398#ifdef PUC_DEBUG
399	printf("before: ");
400	puc_print_win877(bst, bsh, efir, efdr);
401#endif
402
403	val = rdspio(0x16);
404	val |= 0x04;
405	wrspio(0x16, val);
406	val &= ~0x04;
407	wrspio(0x16, val);
408
409	wrspio(0x24, 0x2e8 >> 2);
410	wrspio(0x25, 0x2f8 >> 2);
411	wrspio(0x17, 0x03);
412	wrspio(0x28, 0x43);
413
414#ifdef PUC_DEBUG
415	printf("after: ");
416	puc_print_win877(bst, bsh, efir, efdr);
417#endif
418
419	bus_space_write_1(bst, bsh, 0x250, 0xaa);
420
421	/* configure the second W83877TF */
422	bus_space_write_1(bst, bsh, 0x3f0, 0x87);
423	bus_space_write_1(bst, bsh, 0x3f0, 0x87);
424	efir = 0x3f0;
425	efdr = 0x3f1;
426	val = rdspio(0x09) & 0x0f;
427	if (val != 0x0c) {
428		printf("conf_win877: Oops not a W83877TF\n");
429		return;
430	}
431
432#ifdef PUC_DEBUG
433	printf("before: ");
434	puc_print_win877(bst, bsh, efir, efdr);
435#endif
436
437	val = rdspio(0x16);
438	val |= 0x04;
439	wrspio(0x16, val);
440	val &= ~0x04;
441	wrspio(0x16, val);
442
443	wrspio(0x24, 0x3e8 >> 2);
444	wrspio(0x25, 0x3f8 >> 2);
445	wrspio(0x17, 0x03);
446	wrspio(0x28, 0x43);
447
448#ifdef PUC_DEBUG
449	printf("after: ");
450	puc_print_win877(bst, bsh, efir, efdr);
451#endif
452
453	bus_space_write_1(bst, bsh, 0x3f0, 0xaa);
454}
455
456#undef rdspio
457#undef wrspio
458
459static int puc_find_free_unit(char *name)
460{
461	devclass_t dc;
462	int start;
463	int unit;
464
465	unit = 0;
466	start = 0;
467	while (resource_int_value(name, unit, "port", &start) == 0 &&
468	    start > 0)
469		unit++;
470	dc = devclass_find(name);
471	if (dc == NULL)
472		return (-1);
473	while (devclass_get_device(dc, unit))
474		unit++;
475#ifdef PUC_DEBUG
476	printf("puc: Using %s%d\n", name, unit);
477#endif
478	return (unit);
479}
480
481#ifdef PUC_DEBUG
482static void
483puc_print_resource_list(struct resource_list *rl)
484{
485	struct resource_list_entry *rle;
486
487	printf("print_resource_list: rl %p\n", rl);
488	SLIST_FOREACH(rle, rl, link)
489		printf("type %x, rid %x\n", rle->type, rle->rid);
490	printf("print_resource_list: end.\n");
491}
492#endif
493
494struct resource *
495puc_alloc_resource(device_t dev, device_t child, int type, int *rid,
496    u_long start, u_long end, u_long count, u_int flags)
497{
498	struct puc_device *pdev;
499	struct resource *retval;
500	struct resource_list *rl;
501	struct resource_list_entry *rle;
502
503	pdev = device_get_ivars(child);
504	rl = &pdev->resources;
505
506#ifdef PUC_DEBUG
507	printf("puc_alloc_resource: pdev %p, looking for t %x, r %x\n",
508	    pdev, type, *rid);
509	puc_print_resource_list(rl);
510#endif
511	retval = NULL;
512	rle = resource_list_find(rl, type, *rid);
513	if (rle) {
514		start = rle->start;
515		end = rle->end;
516		count = rle->count;
517#ifdef PUC_DEBUG
518		printf("found rle, %lx, %lx, %lx\n", start, end, count);
519#endif
520		retval = rle->res;
521	} else
522		printf("oops rle is gone\n");
523
524	return (retval);
525}
526
527int
528puc_release_resource(device_t dev, device_t child, int type, int rid,
529    struct resource *res)
530{
531	return (0);
532}
533
534int
535puc_get_resource(device_t dev, device_t child, int type, int rid,
536    u_long *startp, u_long *countp)
537{
538	struct puc_device *pdev;
539	struct resource_list *rl;
540	struct resource_list_entry *rle;
541
542	pdev = device_get_ivars(child);
543	rl = &pdev->resources;
544
545#ifdef PUC_DEBUG
546	printf("puc_get_resource: pdev %p, looking for t %x, r %x\n", pdev,
547	    type, rid);
548	puc_print_resource_list(rl);
549#endif
550	rle = resource_list_find(rl, type, rid);
551	if (rle) {
552#ifdef PUC_DEBUG
553		printf("found rle %p,", rle);
554#endif
555		if (startp != NULL)
556			*startp = rle->start;
557		if (countp != NULL)
558			*countp = rle->count;
559#ifdef PUC_DEBUG
560		printf(" %lx, %lx\n", rle->start, rle->count);
561#endif
562		return (0);
563	} else
564		printf("oops rle is gone\n");
565	return (ENXIO);
566}
567
568int
569puc_setup_intr(device_t dev, device_t child, struct resource *r, int flags,
570	       void (*ihand)(void *), void *arg, void **cookiep)
571{
572	int i;
573	struct puc_softc *sc;
574
575	sc = (struct puc_softc *)device_get_softc(dev);
576	for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
577		if (sc->sc_ports[i].dev == child) {
578			if (sc->sc_ports[i].ihand != 0)
579				return (ENXIO);
580			sc->sc_ports[i].ihand = ihand;
581			sc->sc_ports[i].ihandarg = arg;
582			*cookiep = arg;
583			return (0);
584		}
585	}
586	return (ENXIO);
587}
588
589int
590puc_teardown_intr(device_t dev, device_t child, struct resource *r,
591		  void *cookie)
592{
593	int i;
594	struct puc_softc *sc;
595
596	sc = (struct puc_softc *)device_get_softc(dev);
597	for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
598		if (sc->sc_ports[i].dev == child) {
599			sc->sc_ports[i].ihand = NULL;
600			sc->sc_ports[i].ihandarg = NULL;
601			return (0);
602		}
603	}
604	return (ENXIO);
605}
606
607int
608puc_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
609{
610	struct puc_device *pdev;
611
612	pdev = device_get_ivars(child);
613	if (pdev == NULL)
614		return (ENOENT);
615
616	switch(index) {
617	case PUC_IVAR_FREQ:
618		*result = pdev->serialfreq;
619		break;
620	default:
621		return (ENOENT);
622	}
623	return (0);
624}
625
626devclass_t puc_devclass;
627
628