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