1 /* $NetBSD: puc.c,v 1.30 2008/04/10 19:13:37 cegger Exp $ */
4 * Copyright (c) 1996, 1998, 1999
5 * Christopher G. Demetriou. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Christopher G. Demetriou
18 * for the NetBSD Project.
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 * PCI "universal" communication card device driver, glues com, lpt,
36 * and similar ports to PCI via bridge chip often much larger than
37 * the devices being glued.
39 * Author: Christopher G. Demetriou, May 14, 1998 (derived from NetBSD
40 * sys/dev/pci/pciide.c, revision 1.6).
42 * These devices could be (and some times are) described as
43 * communications/{serial,parallel}, etc. devices with known
44 * programming interfaces, but those programming interfaces (in
45 * particular the BAR assignments for devices, etc.) in fact are not
46 * particularly well defined.
48 * After I/we have seen more of these devices, it may be possible
49 * to generalize some of these bits. In particular, devices which
50 * describe themselves as communications/serial/16[45]50, and
51 * communications/parallel/??? might be attached via direct
52 * 'com' and 'lpt' attachments to pci.
55 #include <sys/cdefs.h>
56 __KERNEL_RCSID(0, "$NetBSD: puc.c,v 1.30 2008/04/10 19:13:37 cegger Exp $");
58 #include <sys/param.h>
59 #include <sys/systm.h>
60 #include <sys/device.h>
62 #include <dev/pci/pcireg.h>
63 #include <dev/pci/pcivar.h>
64 #include <dev/pci/pucvar.h>
65 #include <sys/termios.h>
66 #include <dev/ic/comreg.h>
67 #include <dev/ic/comvar.h>
70 #include "opt_puccn.h"
73 /* static configuration data */
74 const struct puc_device_description
*sc_desc
;
76 /* card-global dynamic data */
84 } sc_bar_mappings
[6]; /* XXX constant */
86 /* per-port dynamic data */
90 /* filled in by port attachments */
93 } sc_ports
[PUC_MAX_PORTS
];
96 static int puc_print(void *, const char *);
98 static const char *puc_port_type_name(int);
101 puc_match(device_t parent
, cfdata_t match
, void *aux
)
103 struct pci_attach_args
*pa
= aux
;
104 const struct puc_device_description
*desc
;
105 pcireg_t bhlc
, subsys
;
107 bhlc
= pci_conf_read(pa
->pa_pc
, pa
->pa_tag
, PCI_BHLC_REG
);
108 if (PCI_HDRTYPE_TYPE(bhlc
) != 0)
111 subsys
= pci_conf_read(pa
->pa_pc
, pa
->pa_tag
, PCI_SUBSYS_ID_REG
);
113 desc
= puc_find_description(PCI_VENDOR(pa
->pa_id
),
114 PCI_PRODUCT(pa
->pa_id
), PCI_VENDOR(subsys
), PCI_PRODUCT(subsys
));
120 * XXX this is obviously bogus. eventually, we might want
121 * XXX to match communications/modem, etc., but that needs some
122 * XXX special work in the match fn.
125 * Match class/subclass, so we can tell people to compile kernel
126 * with options that cause this driver to spew.
128 if (PCI_CLASS(pa
->pa_class
) == PCI_CLASS_COMMUNICATIONS
) {
129 switch (PCI_SUBCLASS(pa
->pa_class
)) {
130 case PCI_SUBCLASS_COMMUNICATIONS_SERIAL
:
131 case PCI_SUBCLASS_COMMUNICATIONS_MODEM
:
141 puc_attach(device_t parent
, device_t self
, void *aux
)
143 struct puc_softc
*sc
= device_private(self
);
144 struct pci_attach_args
*pa
= aux
;
145 struct puc_attach_args paa
;
146 pci_intr_handle_t intrhandle
;
152 bus_space_handle_t ioh
;
154 int locs
[PUCCF_NLOCS
];
156 subsys
= pci_conf_read(pa
->pa_pc
, pa
->pa_tag
, PCI_SUBSYS_ID_REG
);
157 sc
->sc_desc
= puc_find_description(PCI_VENDOR(pa
->pa_id
),
158 PCI_PRODUCT(pa
->pa_id
), PCI_VENDOR(subsys
), PCI_PRODUCT(subsys
));
159 if (sc
->sc_desc
== NULL
) {
161 * This was a class/subclass match, so tell people to compile
162 * kernel with options that cause this driver to spew.
164 #ifdef PUC_PRINT_REGS
166 pci_conf_print(pa
->pa_pc
, pa
->pa_tag
, NULL
);
168 printf(": unknown PCI communications device\n");
169 printf("%s: compile kernel with PUC_PRINT_REGS and larger\n",
171 printf("%s: mesage buffer (via 'options MSGBUFSIZE=...'),\n",
173 printf("%s: and report the result with send-pr\n",
179 printf(": %s (", sc
->sc_desc
->name
);
180 for (i
= 0; PUC_PORT_VALID(sc
->sc_desc
, i
); i
++)
181 printf("%s%s", i
? ", " : "",
182 puc_port_type_name(sc
->sc_desc
->ports
[i
].type
));
185 for (i
= 0; i
< 6; i
++) {
188 sc
->sc_bar_mappings
[i
].mapped
= 0;
190 bar
= pci_conf_read(pa
->pa_pc
, pa
->pa_tag
,
191 PCI_MAPREG_START
+ 4 * i
); /* XXX const */
192 if (bar
== 0) /* BAR not implemented(?) */
195 type
= (PCI_MAPREG_TYPE(bar
) == PCI_MAPREG_TYPE_IO
?
196 PCI_MAPREG_TYPE_IO
: PCI_MAPREG_MEM_TYPE(bar
));
198 if (type
== PCI_MAPREG_TYPE_IO
) {
200 base
= PCI_MAPREG_IO_ADDR(bar
);
203 base
= PCI_MAPREG_MEM_ADDR(bar
);
206 if (com_is_console(tag
, base
, &ioh
)) {
207 sc
->sc_bar_mappings
[i
].mapped
= 1;
208 sc
->sc_bar_mappings
[i
].a
= base
;
209 sc
->sc_bar_mappings
[i
].s
= COM_NPORTS
;
210 sc
->sc_bar_mappings
[i
].t
= tag
;
211 sc
->sc_bar_mappings
[i
].h
= ioh
;
215 sc
->sc_bar_mappings
[i
].mapped
= (pci_mapreg_map(pa
,
216 PCI_MAPREG_START
+ 4 * i
, type
, 0,
217 &sc
->sc_bar_mappings
[i
].t
, &sc
->sc_bar_mappings
[i
].h
,
218 &sc
->sc_bar_mappings
[i
].a
, &sc
->sc_bar_mappings
[i
].s
)
220 if (sc
->sc_bar_mappings
[i
].mapped
)
223 aprint_error_dev(self
, "couldn't map BAR at offset 0x%lx\n",
224 (long)(PCI_MAPREG_START
+ 4 * i
));
228 if (pci_intr_map(pa
, &intrhandle
)) {
229 aprint_error_dev(self
, "couldn't map interrupt\n");
233 * XXX the sub-devices establish the interrupts, for the
234 * XXX following reasons:
236 * XXX * we can't really know what IPLs they'd want
238 * XXX * the MD dispatching code can ("should") dispatch
239 * XXX chained interrupts better than we can.
241 * XXX It would be nice if we could indicate to the MD interrupt
242 * XXX handling code that the interrupt line used by the device
243 * XXX was a PCI (level triggered) interrupt.
245 * XXX It's not pretty, but hey, what is?
248 /* Configure each port. */
249 for (i
= 0; PUC_PORT_VALID(sc
->sc_desc
, i
); i
++) {
250 bus_space_handle_t subregion_handle
;
252 /* make sure the base address register is mapped */
253 barindex
= PUC_PORT_BAR_INDEX(sc
->sc_desc
->ports
[i
].bar
);
254 if (!sc
->sc_bar_mappings
[barindex
].mapped
) {
255 printf("%s: %s port uses unmapped BAR (0x%x)\n",
257 puc_port_type_name(sc
->sc_desc
->ports
[i
].type
),
258 sc
->sc_desc
->ports
[i
].bar
);
262 /* set up to configure the child device */
264 paa
.type
= sc
->sc_desc
->ports
[i
].type
;
265 paa
.flags
= sc
->sc_desc
->ports
[i
].flags
;
267 paa
.tag
= pa
->pa_tag
;
268 paa
.intrhandle
= intrhandle
;
269 paa
.a
= sc
->sc_bar_mappings
[barindex
].a
;
270 paa
.t
= sc
->sc_bar_mappings
[barindex
].t
;
271 paa
.dmat
= pa
->pa_dmat
;
272 paa
.dmat64
= pa
->pa_dmat64
;
276 !com_is_console(sc
->sc_bar_mappings
[barindex
].t
,
277 sc
->sc_bar_mappings
[barindex
].a
, &subregion_handle
)
280 bus_space_subregion(sc
->sc_bar_mappings
[barindex
].t
,
281 sc
->sc_bar_mappings
[barindex
].h
,
282 sc
->sc_desc
->ports
[i
].offset
,
283 sc
->sc_bar_mappings
[barindex
].s
-
284 sc
->sc_desc
->ports
[i
].offset
,
285 &subregion_handle
) != 0) {
286 aprint_error_dev(self
, "couldn't get subregion for port %d\n", i
);
289 paa
.h
= subregion_handle
;
292 printf("%s: port %d: %s @ (index %d) 0x%x (0x%lx, 0x%lx)\n",
293 device_xname(self
), paa
.port
,
294 puc_port_type_name(paa
.type
), barindex
, (int)paa
.a
,
295 (long)paa
.t
, (long)paa
.h
);
298 locs
[PUCCF_PORT
] = i
;
300 /* and configure it */
301 sc
->sc_ports
[i
].dev
= config_found_sm_loc(self
, "puc", locs
,
302 &paa
, puc_print
, config_stdsubmatch
);
306 CFATTACH_DECL_NEW(puc
, sizeof(struct puc_softc
),
307 puc_match
, puc_attach
, NULL
, NULL
);
310 puc_print(void *aux
, const char *pnp
)
312 struct puc_attach_args
*paa
= aux
;
315 aprint_normal("%s at %s", puc_port_type_name(paa
->type
), pnp
);
316 aprint_normal(" port %d", paa
->port
);
320 const struct puc_device_description
*
321 puc_find_description(pcireg_t vend
, pcireg_t prod
, pcireg_t svend
,
326 #define checkreg(val, index) \
327 (((val) & puc_devices[i].rmask[(index)]) == puc_devices[i].rval[(index)])
329 for (i
= 0; puc_devices
[i
].name
!= NULL
; i
++) {
330 if (checkreg(vend
, PUC_REG_VEND
) &&
331 checkreg(prod
, PUC_REG_PROD
) &&
332 checkreg(svend
, PUC_REG_SVEND
) &&
333 checkreg(sprod
, PUC_REG_SPROD
))
334 return (&puc_devices
[i
]);
343 puc_port_type_name(int type
)
347 case PUC_PORT_TYPE_COM
:
349 case PUC_PORT_TYPE_LPT
:
352 panic("puc_port_type_name %d", type
);