puc.c revision 1.1 1 /* $NetBSD: puc.c,v 1.1 1998/06/26 18:52:41 cgd Exp $ */
2
3 /*
4 * Copyright (c) 1996, 1998 Christopher G. Demetriou. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Christopher G. Demetriou
17 * for the NetBSD Project.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * PCI "universal" communication card device driver, glues com, lpt,
35 * and similar ports to PCI via bridge chip often much larger than
36 * the devices being glued.
37 *
38 * Author: Christopher G. Demetriou, May 14, 1998 (derived from NetBSD
39 * sys/dev/pci/pciide.c, revision 1.6).
40 *
41 * These devices could be (and some times are) described as
42 * communications/{serial,parallel}, etc. devices with known
43 * programming interfaces, but those programming interfaces (in
44 * particular the BAR assignments for devices, etc.) in fact are not
45 * particularly well defined.
46 *
47 * After I/we have seen more of these devices, it may be possible
48 * to generalize some of these bits. In particular, devices which
49 * describe themselves as communications/serial/16[45]50, and
50 * communications/parallel/??? might be attached via direct
51 * 'com' and 'lpt' attachments to pci.
52 */
53
54 #include <sys/param.h>
55 #include <sys/systm.h>
56 #include <sys/device.h>
57
58 #include <dev/pci/pcireg.h>
59 #include <dev/pci/pcivar.h>
60 #include <dev/pci/pucvar.h>
61
62 struct puc_softc {
63 struct device sc_dev;
64
65 /* static configuration data */
66 const struct puc_device_description *sc_desc;
67
68 /* card-global dynamic data */
69 void *sc_ih;
70 struct {
71 int mapped;
72 bus_addr_t a;
73 bus_size_t s;
74 bus_space_tag_t t;
75 bus_space_handle_t h;
76 } sc_bar_mappings[6]; /* XXX constant */
77
78 /* per-port dynamic data */
79 struct {
80 struct device *dev;
81
82 /* filled in by port attachments */
83 int (*ihand) __P((void *));
84 void *ihandarg;
85 } sc_ports[PUC_MAX_PORTS];
86 };
87
88 int puc_match __P((struct device *, struct cfdata *, void *));
89 void puc_attach __P((struct device *, struct device *, void *));
90 int puc_print __P((void *, const char *));
91 int puc_submatch __P((struct device *, struct cfdata *, void *));
92
93 struct cfattach puc_ca = {
94 sizeof(struct puc_softc), puc_match, puc_attach
95 };
96
97 static const struct puc_device_description *
98 puc_find_description __P((pcireg_t, pcireg_t, pcireg_t, pcireg_t));
99 static const char *
100 puc_port_type_name __P((int));
101
102 int
103 puc_match(parent, match, aux)
104 struct device *parent;
105 struct cfdata *match;
106 void *aux;
107 {
108 struct pci_attach_args *pa = aux;
109 const struct puc_device_description *desc;
110 pcireg_t bhlc, subsys;
111
112 bhlc = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_BHLC_REG);
113 if (PCI_HDRTYPE_TYPE(bhlc) != 0)
114 return (0);
115
116 subsys = pci_conf_read(pa->pa_pc, pa->pa_tag, 0x2c); /* XXX const */
117
118 desc = puc_find_description(PCI_VENDOR(pa->pa_id),
119 PCI_PRODUCT(pa->pa_id), PCI_VENDOR(subsys), PCI_PRODUCT(subsys));
120 if (desc != NULL)
121 return (10);
122
123 /*
124 * Match class/subclass, so we can tell people to compile kernel
125 * with options that cause this driver to spew.
126 */
127 if (PCI_CLASS(pa->pa_class) == PCI_CLASS_COMMUNICATIONS &&
128 PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_BRIDGE_PCI)
129 return (1);
130
131 return (0);
132 }
133
134 void
135 puc_attach(parent, self, aux)
136 struct device *parent, *self;
137 void *aux;
138 {
139 struct puc_softc *sc = (struct puc_softc *)self;
140 struct pci_attach_args *pa = aux;
141 struct puc_attach_args paa;
142 pci_intr_handle_t intrhandle;
143 pcireg_t subsys;
144 int i, barindex;
145
146 subsys = pci_conf_read(pa->pa_pc, pa->pa_tag, 0x2c); /* XXX const */
147 sc->sc_desc = puc_find_description(PCI_VENDOR(pa->pa_id),
148 PCI_PRODUCT(pa->pa_id), PCI_VENDOR(subsys), PCI_PRODUCT(subsys));
149 if (sc->sc_desc == NULL) {
150 /*
151 * This was a class/subclass match, so tell people to compile
152 * kernel with options that cause this driver to spew.
153 */
154 #ifdef PUC_PRINT_REGS
155 printf(":\n");
156 pci_conf_print(pa->pa_pc, pa->pa_tag, NULL);
157 #else
158 printf(": unknown PCI communications device\n");
159 printf("%s: compile kernel with PUC_PRINT_REGS and larger\n",
160 sc->sc_dev.dv_xname);
161 printf("%s: mesage buffer (via 'options MSGBUFSIZE=...'),\n",
162 sc->sc_dev.dv_xname);
163 printf("%s: and report the result with send-pr\n",
164 sc->sc_dev.dv_xname);
165 #endif
166 return;
167 }
168
169 printf(": %s (", sc->sc_desc->name);
170 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++)
171 printf("%s%s", i ? ", " : "",
172 puc_port_type_name(sc->sc_desc->ports[i].type));
173 printf(")\n");
174
175 /*
176 * XXX This driver assumes that 'com' ports attached to it
177 * XXX can not be console. That isn't unreasonable, because PCI
178 * XXX devices are supposed to be dynamically mapped, and com
179 * XXX console ports want fixed addresses. When/if baseboard
180 * XXX 'com' ports are identified as PCI/communications/serial
181 * XXX devices and are known to be mapped at the standard
182 * XXX addresses, if they can be the system console then we have
183 * XXX to cope with doing the mapping right. Then this will get
184 * XXX really ugly. Of course, by then we might know the real
185 * XXX definition of PCI/communications/serial, and attach 'com'
186 * XXX directly on PCI.
187 */
188 for (i = 0; i < 6; i++) {
189 pcireg_t bar, type;
190
191 sc->sc_bar_mappings[i].mapped = 0;
192
193 bar = pci_conf_read(pa->pa_pc, pa->pa_tag,
194 PCI_MAPREG_START + 4 * i); /* XXX const */
195 if (bar == 0) /* BAR not implemented(?) */
196 continue;
197
198 type = (PCI_MAPREG_TYPE(bar) == PCI_MAPREG_TYPE_IO ?
199 PCI_MAPREG_TYPE_IO : PCI_MAPREG_MEM_TYPE(bar));
200
201 sc->sc_bar_mappings[i].mapped = (pci_mapreg_map(pa,
202 PCI_MAPREG_START + 4 * i, type, 0,
203 &sc->sc_bar_mappings[i].t, &sc->sc_bar_mappings[i].h,
204 &sc->sc_bar_mappings[i].a, &sc->sc_bar_mappings[i].s)
205 == 0);
206 if (sc->sc_bar_mappings[i].mapped)
207 continue;
208
209 printf("%s: couldn't map BAR at offset 0x%lx\n",
210 sc->sc_dev.dv_xname, (long)(PCI_MAPREG_START + 4 * i));
211 }
212
213 /* Map interrupt. */
214 if (pci_intr_map(pa->pa_pc, pa->pa_intrtag, pa->pa_intrpin,
215 pa->pa_intrline, &intrhandle)) {
216 printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
217 return;
218 }
219 /*
220 * XXX the sub-devices establish the interrupts, for the
221 * XXX following reasons:
222 * XXX
223 * XXX * we can't really know what IPLs they'd want
224 * XXX
225 * XXX * the MD dispatching code can ("should") dispatch
226 * XXX chained interrupts better than we can.
227 * XXX
228 * XXX It would be nice if we could indicate to the MD interrupt
229 * XXX handling code that the interrupt line used by the device
230 * XXX was a PCI (level triggered) interrupt.
231 * XXX
232 * XXX It's not pretty, but hey, what is?
233 */
234
235 /* Configure each port. */
236 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
237
238 /* make sure the base address register is mapped */
239 barindex = PUC_PORT_BAR_INDEX(sc->sc_desc->ports[i].bar);
240 if (!sc->sc_bar_mappings[barindex].mapped) {
241 printf("%s: %s port uses unmapped BAR (0x%x)\n",
242 sc->sc_dev.dv_xname,
243 puc_port_type_name(sc->sc_desc->ports[i].type),
244 sc->sc_desc->ports[i].bar);
245 continue;
246 }
247
248 /* set up to configure the child device */
249 paa.port = i;
250 paa.type = sc->sc_desc->ports[i].type;
251 paa.pc = pa->pa_pc;
252 paa.intrhandle = intrhandle;
253 paa.a = sc->sc_bar_mappings[barindex].a;
254 paa.s = sc->sc_bar_mappings[barindex].s;
255 paa.t = sc->sc_bar_mappings[barindex].t;
256 paa.h = sc->sc_bar_mappings[barindex].h;
257
258 #if 0
259 printf("%s: port %d: %s @ (index %d) 0x%x/%d (0x%lx, 0x%lx)\n",
260 sc->sc_dev.dv_xname, paa.port,
261 puc_port_type_name(paa.type), barindex, (int)paa.a,
262 (int)paa.s, (long)paa.t, (long)paa.h);
263 #endif
264
265 /* and configure it */
266 sc->sc_ports[i].dev = config_found_sm(self, &paa, puc_print,
267 puc_submatch);
268 }
269 }
270
271 int
272 puc_print(aux, pnp)
273 void *aux;
274 const char *pnp;
275 {
276 struct puc_attach_args *paa = aux;
277
278 if (pnp)
279 printf("%s at %s", puc_port_type_name(paa->type), pnp);
280 printf(" port %d", paa->port);
281 return (UNCONF);
282 }
283
284 int
285 puc_submatch(parent, cf, aux)
286 struct device *parent;
287 struct cfdata *cf;
288 void *aux;
289 {
290 struct puc_attach_args *aa = aux;
291
292 if (cf->cf_loc[PUCCF_PORT] != PUCCF_PORT_DEFAULT &&
293 cf->cf_loc[PUCCF_PORT] != aa->port)
294 return 0;
295 return ((*cf->cf_attach->ca_match)(parent, cf, aux));
296 }
297
298 static const struct puc_device_description *
299 puc_find_description(vend, prod, svend, sprod)
300 pcireg_t vend, prod, svend, sprod;
301 {
302 int i;
303
304 #define checkreg(val, index) \
305 (((val) & puc_devices[i].rmask[(index)]) == puc_devices[i].rval[(index)])
306
307 for (i = 0; puc_devices[i].name != NULL; i++) {
308 if (checkreg(vend, PUC_REG_VEND) &&
309 checkreg(prod, PUC_REG_PROD) &&
310 checkreg(svend, PUC_REG_SVEND) &&
311 checkreg(sprod, PUC_REG_SPROD))
312 return (&puc_devices[i]);
313 }
314
315 #undef checkreg
316
317 return (NULL);
318 }
319
320 static const char *
321 puc_port_type_name(type)
322 int type;
323 {
324
325 switch (type) {
326 case PUC_PORT_TYPE_COM:
327 return "com";
328 case PUC_PORT_TYPE_LPT:
329 return "lpt";
330 default:
331 panic("puc_port_type_name %d", type);
332 }
333 }
334