xref: /netbsd-src/sys/dev/pci/puccn.c (revision 5aefcfdc06931dd97e76246d2fe0302f7b3fe094)
1 /*	$NetBSD: puccn.c,v 1.2 2000/12/09 06:54:27 imp 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 <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/conf.h>
47 #include <sys/device.h>
48 
49 #include <dev/pci/pcireg.h>
50 #include <dev/pci/pcivar.h>
51 #include <dev/pci/pcidevs.h>
52 
53 #include <sys/termios.h>
54 #include <dev/ic/comreg.h>
55 #include <dev/ic/comvar.h>
56 
57 #include <dev/cons.h>
58 
59 #include <dev/pci/pucvar.h>
60 #include <dev/pci/puccn.h>
61 
62 #ifndef CONSPEED
63 #define CONSPEED	TTYDEF_SPEED
64 #endif
65 #ifndef CONMODE
66 #define	CONMODE		((TTYDEF_CFLAG & ~(CSIZE|CSTOPB|PARENB))|CS8) /* 8N1 */
67 #endif
68 
69 #ifdef i386		/* Handle i386 directly */
70 int
71 cpu_comcnprobe(struct consdev *cn, struct pci_attach_args *pa)
72 {
73 	pci_mode_detect();
74 	pa->pa_iot = I386_BUS_SPACE_IO;
75 	pa->pa_pc = 0;
76 	pa->pa_tag = pci_make_tag(0, 0, 31, 0);
77 	return 0;
78 }
79 #endif
80 
81 cons_decl(com);
82 
83 static bus_addr_t puccnbase;
84 static bus_space_tag_t puctag;
85 
86 #ifdef KGDB
87 static bus_addr_t pucgdbbase;
88 #endif
89 
90 /*
91  * Static dev/func variables allow pucprobe to be called multiple times,
92  * resuming the search where it left off, never retrying the same adaptor.
93  */
94 
95 static bus_addr_t
96 pucprobe_doit(struct consdev *cn)
97 {
98 	struct pci_attach_args pa;
99 	int bus;
100 	static int dev = 0, func = 0;
101 	int maxdev, nfunctions, i;
102 	pcireg_t reg, bhlcr, subsys;
103 	int foundport = 0;
104 	const struct puc_device_description *desc;
105 	pcireg_t base;
106 
107 	/* Fetch our tags */
108 	if (cpu_comcnprobe(cn, &pa) != 0) {
109 		return 0;
110 	}
111 	puctag = pa.pa_iot;
112 	pci_decompose_tag(pa.pa_pc, pa.pa_tag, &bus, &maxdev, NULL);
113 
114 	/* scan through devices */
115 
116 	for (; dev <= maxdev ; dev++) {
117 		pa.pa_tag = pci_make_tag(pa.pa_pc, bus, dev, 0);
118 		reg = pci_conf_read(pa.pa_pc, pa.pa_tag, PCI_ID_REG);
119 		if (PCI_VENDOR(reg) == PCI_VENDOR_INVALID
120 		    || PCI_VENDOR(reg) == 0)
121 			continue;
122 		bhlcr = pci_conf_read(pa.pa_pc, pa.pa_tag, PCI_BHLC_REG);
123 		if (PCI_HDRTYPE_MULTIFN(bhlcr)) {
124 			nfunctions = 8;
125 		} else {
126 			nfunctions = 1;
127 		}
128 resume_scan:
129 		for (; func < nfunctions; func++)  {
130 			pa.pa_tag = pci_make_tag(pa.pa_pc, bus, dev, func);
131 			reg = pci_conf_read(pa.pa_pc, pa.pa_tag, PCI_CLASS_REG);
132 			if (PCI_CLASS(reg)  == PCI_CLASS_COMMUNICATIONS
133 			    && PCI_SUBCLASS(reg)
134 			       == PCI_SUBCLASS_COMMUNICATIONS_SERIAL) {
135 				pa.pa_id = pci_conf_read(pa.pa_pc, pa.pa_tag, PCI_ID_REG);
136 				subsys = pci_conf_read(pa.pa_pc, pa.pa_tag,
137 				    PCI_SUBSYS_ID_REG);
138 				foundport = 1;
139 				break;
140 			}
141 		}
142 		if (foundport)
143 			break;
144 
145 		func = 0;
146 	}
147 	if (!foundport)
148 		return 0;
149 	foundport = 0;
150 
151 	desc = puc_find_description(PCI_VENDOR(pa.pa_id),
152 	    PCI_PRODUCT(pa.pa_id), PCI_VENDOR(subsys), PCI_PRODUCT(subsys));
153 	if (desc == NULL) {
154 		func++;
155 		goto resume_scan;
156 	}
157 
158 	for (i = 0; PUC_PORT_VALID(desc, i); i++)
159 	{
160 		if (desc->ports[i].type != PUC_PORT_TYPE_COM)
161 			continue;
162 		base = pci_conf_read(pa.pa_pc, pa.pa_tag, desc->ports[i].bar);
163 		base += desc->ports[i].offset;
164 
165 		if (PCI_MAPREG_TYPE(base) != PCI_MAPREG_TYPE_IO)
166 			continue;
167 		base = PCI_MAPREG_IO_ADDR(base);
168 		if (com_is_console(puctag, base, NULL))
169 			continue;
170 		foundport = 1;
171 		break;
172 	}
173 
174 	if (foundport == 0) {
175 		func++;
176 		goto resume_scan;
177 	}
178 
179 	cn->cn_pri = CN_REMOTE;
180 	return PCI_MAPREG_IO_ADDR(base);
181 }
182 
183 #ifdef KGDB
184 void comgdbprobe(struct consdev *);
185 void comgdbinit(struct consdev *);
186 
187 void
188 comgdbprobe(struct consdev *cn)
189 {
190 	pucgdbbase = pucprobe_doit(cn);
191 }
192 
193 void
194 comgdbinit(struct consdev *cn)
195 {
196 	if (pucgdbbase == 0) {
197 		return;
198 	}
199 	com_kgdb_attach(puctag, pucgdbbase, CONSPEED, COM_FREQ, CONMODE);
200 }
201 #endif
202 
203 void
204 comcnprobe(struct consdev *cn)
205 {
206 	puccnbase = pucprobe_doit(cn);
207 }
208 
209 void
210 comcninit(struct consdev *cn)
211 {
212 	if (puccnbase == 0) {
213 		return;
214 	}
215 	comcnattach(puctag, puccnbase, CONSPEED, COM_FREQ, CONMODE);
216 }
217 
218 /* comcngetc, comcnputc, comcnpollc provided by dev/ic/com.c */
219