xref: /netbsd-src/sys/dev/pci/puccn.c (revision 3b01aba77a7a698587faaae455bbfe740923c1f5)
1 /*	$NetBSD: puccn.c,v 1.3 2001/05/30 15:24:25 lukem Exp $ */
2 
3 /*
4  * Derived from  pci.c
5  * Copyright (c) 2000 Geocast Networks Systems.  All rights reserved.
6  *
7  * Copyright (c) 1995, 1996, 1997, 1998
8  *     Christopher G. Demetriou.  All rights reserved.
9  * Copyright (c) 1994 Charles M. Hannum.  All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by Charles M. Hannum.
22  * 4. The name of the author may not be used to endorse or promote products
23  *    derived from this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35  */
36 
37 
38 /*
39  * Machine independent support for PCI serial console support.
40  *
41  * Scan the PCI bus for something which resembles a 16550
42  */
43 
44 #include "opt_kgdb.h"
45 
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/conf.h>
49 #include <sys/device.h>
50 
51 #include <dev/pci/pcireg.h>
52 #include <dev/pci/pcivar.h>
53 #include <dev/pci/pcidevs.h>
54 
55 #include <sys/termios.h>
56 #include <dev/ic/comreg.h>
57 #include <dev/ic/comvar.h>
58 
59 #include <dev/cons.h>
60 
61 #include <dev/pci/pucvar.h>
62 #include <dev/pci/puccn.h>
63 
64 #ifndef CONSPEED
65 #define CONSPEED	TTYDEF_SPEED
66 #endif
67 #ifndef CONMODE
68 #define	CONMODE		((TTYDEF_CFLAG & ~(CSIZE|CSTOPB|PARENB))|CS8) /* 8N1 */
69 #endif
70 
71 #ifdef i386		/* Handle i386 directly */
72 int
73 cpu_comcnprobe(struct consdev *cn, struct pci_attach_args *pa)
74 {
75 	pci_mode_detect();
76 	pa->pa_iot = I386_BUS_SPACE_IO;
77 	pa->pa_pc = 0;
78 	pa->pa_tag = pci_make_tag(0, 0, 31, 0);
79 	return 0;
80 }
81 #endif
82 
83 cons_decl(com);
84 
85 static bus_addr_t puccnbase;
86 static bus_space_tag_t puctag;
87 
88 #ifdef KGDB
89 static bus_addr_t pucgdbbase;
90 #endif
91 
92 /*
93  * Static dev/func variables allow pucprobe to be called multiple times,
94  * resuming the search where it left off, never retrying the same adaptor.
95  */
96 
97 static bus_addr_t
98 pucprobe_doit(struct consdev *cn)
99 {
100 	struct pci_attach_args pa;
101 	int bus;
102 	static int dev = 0, func = 0;
103 	int maxdev, nfunctions, i;
104 	pcireg_t reg, bhlcr, subsys;
105 	int foundport = 0;
106 	const struct puc_device_description *desc;
107 	pcireg_t base;
108 
109 	/* Fetch our tags */
110 	if (cpu_comcnprobe(cn, &pa) != 0) {
111 		return 0;
112 	}
113 	puctag = pa.pa_iot;
114 	pci_decompose_tag(pa.pa_pc, pa.pa_tag, &bus, &maxdev, NULL);
115 
116 	/* scan through devices */
117 
118 	for (; dev <= maxdev ; dev++) {
119 		pa.pa_tag = pci_make_tag(pa.pa_pc, bus, dev, 0);
120 		reg = pci_conf_read(pa.pa_pc, pa.pa_tag, PCI_ID_REG);
121 		if (PCI_VENDOR(reg) == PCI_VENDOR_INVALID
122 		    || PCI_VENDOR(reg) == 0)
123 			continue;
124 		bhlcr = pci_conf_read(pa.pa_pc, pa.pa_tag, PCI_BHLC_REG);
125 		if (PCI_HDRTYPE_MULTIFN(bhlcr)) {
126 			nfunctions = 8;
127 		} else {
128 			nfunctions = 1;
129 		}
130 resume_scan:
131 		for (; func < nfunctions; func++)  {
132 			pa.pa_tag = pci_make_tag(pa.pa_pc, bus, dev, func);
133 			reg = pci_conf_read(pa.pa_pc, pa.pa_tag, PCI_CLASS_REG);
134 			if (PCI_CLASS(reg)  == PCI_CLASS_COMMUNICATIONS
135 			    && PCI_SUBCLASS(reg)
136 			       == PCI_SUBCLASS_COMMUNICATIONS_SERIAL) {
137 				pa.pa_id = pci_conf_read(pa.pa_pc, pa.pa_tag, PCI_ID_REG);
138 				subsys = pci_conf_read(pa.pa_pc, pa.pa_tag,
139 				    PCI_SUBSYS_ID_REG);
140 				foundport = 1;
141 				break;
142 			}
143 		}
144 		if (foundport)
145 			break;
146 
147 		func = 0;
148 	}
149 	if (!foundport)
150 		return 0;
151 	foundport = 0;
152 
153 	desc = puc_find_description(PCI_VENDOR(pa.pa_id),
154 	    PCI_PRODUCT(pa.pa_id), PCI_VENDOR(subsys), PCI_PRODUCT(subsys));
155 	if (desc == NULL) {
156 		func++;
157 		goto resume_scan;
158 	}
159 
160 	for (i = 0; PUC_PORT_VALID(desc, i); i++)
161 	{
162 		if (desc->ports[i].type != PUC_PORT_TYPE_COM)
163 			continue;
164 		base = pci_conf_read(pa.pa_pc, pa.pa_tag, desc->ports[i].bar);
165 		base += desc->ports[i].offset;
166 
167 		if (PCI_MAPREG_TYPE(base) != PCI_MAPREG_TYPE_IO)
168 			continue;
169 		base = PCI_MAPREG_IO_ADDR(base);
170 		if (com_is_console(puctag, base, NULL))
171 			continue;
172 		foundport = 1;
173 		break;
174 	}
175 
176 	if (foundport == 0) {
177 		func++;
178 		goto resume_scan;
179 	}
180 
181 	cn->cn_pri = CN_REMOTE;
182 	return PCI_MAPREG_IO_ADDR(base);
183 }
184 
185 #ifdef KGDB
186 void comgdbprobe(struct consdev *);
187 void comgdbinit(struct consdev *);
188 
189 void
190 comgdbprobe(struct consdev *cn)
191 {
192 	pucgdbbase = pucprobe_doit(cn);
193 }
194 
195 void
196 comgdbinit(struct consdev *cn)
197 {
198 	if (pucgdbbase == 0) {
199 		return;
200 	}
201 	com_kgdb_attach(puctag, pucgdbbase, CONSPEED, COM_FREQ, CONMODE);
202 }
203 #endif
204 
205 void
206 comcnprobe(struct consdev *cn)
207 {
208 	puccnbase = pucprobe_doit(cn);
209 }
210 
211 void
212 comcninit(struct consdev *cn)
213 {
214 	if (puccnbase == 0) {
215 		return;
216 	}
217 	comcnattach(puctag, puccnbase, CONSPEED, COM_FREQ, CONMODE);
218 }
219 
220 /* comcngetc, comcnputc, comcnpollc provided by dev/ic/com.c */
221