Deleted Added
full compact
puc.c (155917) puc.c (158124)
1/* $NetBSD: puc.c,v 1.7 2000/07/29 17:43:38 jlam Exp $ */
2
3/*-
1/*-
4 * Copyright (c) 2002 JF Hay. All rights reserved.
5 * Copyright (c) 2000 M. Warner Losh. All rights reserved.
2 * Copyright (c) 2006 Marcel Moolenaar
3 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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 *
8 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29/*-
30 * Copyright (c) 1996, 1998, 1999
31 * Christopher G. Demetriou. All rights reserved.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
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.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by Christopher G. Demetriou
44 * for the NetBSD Project.
45 * 4. The name of the author may not be used to endorse or promote products
46 * derived from this software without specific prior written permission
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
49 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
50 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
51 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
52 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
53 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
54 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
55 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
56 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
57 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58 */
59
60#include <sys/cdefs.h>
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
61__FBSDID("$FreeBSD: head/sys/dev/puc/puc.c 155917 2006-02-22 17:19:10Z jhb $");
28__FBSDID("$FreeBSD: head/sys/dev/puc/puc.c 158124 2006-04-28 21:21:53Z marcel $");
62
29
63/*
64 * PCI "universal" communication card device driver, glues com, lpt,
65 * and similar ports to PCI via bridge chip often much larger than
66 * the devices being glued.
67 *
68 * Author: Christopher G. Demetriou, May 14, 1998 (derived from NetBSD
69 * sys/dev/pci/pciide.c, revision 1.6).
70 *
71 * These devices could be (and some times are) described as
72 * communications/{serial,parallel}, etc. devices with known
73 * programming interfaces, but those programming interfaces (in
74 * particular the BAR assignments for devices, etc.) in fact are not
75 * particularly well defined.
76 *
77 * After I/we have seen more of these devices, it may be possible
78 * to generalize some of these bits. In particular, devices which
79 * describe themselves as communications/serial/16[45]50, and
80 * communications/parallel/??? might be attached via direct
81 * 'com' and 'lpt' attachments to pci.
82 */
83
84#include "opt_puc.h"
85
86#include <sys/param.h>
87#include <sys/systm.h>
88#include <sys/kernel.h>
89#include <sys/bus.h>
90#include <sys/conf.h>
91#include <sys/malloc.h>
30#include <sys/param.h>
31#include <sys/systm.h>
32#include <sys/kernel.h>
33#include <sys/bus.h>
34#include <sys/conf.h>
35#include <sys/malloc.h>
36#include <sys/mutex.h>
92
93#include <machine/bus.h>
94#include <machine/resource.h>
95#include <sys/rman.h>
96
97#include <dev/pci/pcireg.h>
98#include <dev/pci/pcivar.h>
99
37
38#include <machine/bus.h>
39#include <machine/resource.h>
40#include <sys/rman.h>
41
42#include <dev/pci/pcireg.h>
43#include <dev/pci/pcivar.h>
44
100#define PUC_ENTRAILS 1
101#include <dev/puc/pucvar.h>
45#include <dev/puc/puc_bfe.h>
46#include <dev/puc/puc_bus.h>
47#include <dev/puc/puc_cfg.h>
102
48
103struct puc_device {
104 struct resource_list resources;
105 int port;
106 int regshft;
107 u_int serialfreq;
108 u_int subtype;
109};
49#define PUC_ISRCCNT 5
110
50
111static void puc_intr(void *arg);
51struct puc_port {
52 struct puc_bar *p_bar;
53 struct resource *p_rres;
54 struct resource *p_ires;
55 device_t p_dev;
56 int p_nr;
57 int p_type;
58 int p_rclk;
112
59
113static int puc_find_free_unit(char *);
114#ifdef PUC_DEBUG
115static void puc_print_resource_list(struct resource_list *);
116#endif
60 int p_hasintr:1;
117
61
62 driver_intr_t *p_ih;
63 serdev_intr_t *p_ihsrc[PUC_ISRCCNT];
64 void *p_iharg;
65
66 int p_ipend;
67};
68
118devclass_t puc_devclass;
69devclass_t puc_devclass;
70const char puc_driver_name[] = "puc";
119
71
120static int
121puc_port_bar_index(struct puc_softc *sc, int bar)
72MALLOC_DEFINE(M_PUC, "PUC", "PUC driver");
73
74struct puc_bar *
75puc_get_bar(struct puc_softc *sc, int rid)
122{
76{
123 int i;
77 struct puc_bar *bar;
78 struct rman *rm;
79 u_long end, start;
80 int error, i;
124
81
125 for (i = 0; i < PUC_MAX_BAR; i += 1) {
126 if (!sc->sc_bar_mappings[i].used)
127 break;
128 if (sc->sc_bar_mappings[i].bar == bar)
129 return (i);
82 /* Find the BAR entry with the given RID. */
83 i = 0;
84 while (i < PUC_PCI_BARS && sc->sc_bar[i].b_rid != rid)
85 i++;
86 if (i < PUC_PCI_BARS)
87 return (&sc->sc_bar[i]);
88
89 /* Not found. If we're looking for an unused entry, return NULL. */
90 if (rid == -1)
91 return (NULL);
92
93 /* Get an unused entry for us to fill. */
94 bar = puc_get_bar(sc, -1);
95 if (bar == NULL)
96 return (NULL);
97 bar->b_rid = rid;
98 bar->b_type = SYS_RES_IOPORT;
99 bar->b_res = bus_alloc_resource_any(sc->sc_dev, bar->b_type,
100 &bar->b_rid, RF_ACTIVE);
101 if (bar->b_res == NULL) {
102 bar->b_rid = rid;
103 bar->b_type = SYS_RES_MEMORY;
104 bar->b_res = bus_alloc_resource_any(sc->sc_dev, bar->b_type,
105 &bar->b_rid, RF_ACTIVE);
106 if (bar->b_res == NULL) {
107 bar->b_rid = -1;
108 return (NULL);
109 }
130 }
110 }
131 if (i == PUC_MAX_BAR) {
132 printf("%s: out of bars!\n", __func__);
133 return (-1);
111
112 /* Update our managed space. */
113 rm = (bar->b_type == SYS_RES_IOPORT) ? &sc->sc_ioport : &sc->sc_iomem;
114 start = rman_get_start(bar->b_res);
115 end = rman_get_end(bar->b_res);
116 error = rman_manage_region(rm, start, end);
117 if (error) {
118 bus_release_resource(sc->sc_dev, bar->b_type, bar->b_rid,
119 bar->b_res);
120 bar->b_res = NULL;
121 bar->b_rid = -1;
122 bar = NULL;
134 }
123 }
135 sc->sc_bar_mappings[i].bar = bar;
136 sc->sc_bar_mappings[i].used = 1;
137 return (i);
124
125 return (bar);
138}
139
126}
127
140static int
141puc_probe_ilr(struct puc_softc *sc, struct resource *res)
128static void
129puc_intr(void *arg)
142{
130{
143 u_char t1, t2;
144 int i;
131 struct puc_port *port;
132 struct puc_softc *sc = arg;
133 u_long dev, devs;
134 int i, idx, ipend, isrc;
135 uint8_t ilr;
145
136
146 switch (sc->sc_desc.ilr_type) {
147 case PUC_ILR_TYPE_DIGI:
148 sc->ilr_st = rman_get_bustag(res);
149 sc->ilr_sh = rman_get_bushandle(res);
150 for (i = 0; i < 2 && sc->sc_desc.ilr_offset[i] != 0; i++) {
151 t1 = bus_space_read_1(sc->ilr_st, sc->ilr_sh,
152 sc->sc_desc.ilr_offset[i]);
153 t1 = ~t1;
154 bus_space_write_1(sc->ilr_st, sc->ilr_sh,
155 sc->sc_desc.ilr_offset[i], t1);
156 t2 = bus_space_read_1(sc->ilr_st, sc->ilr_sh,
157 sc->sc_desc.ilr_offset[i]);
158 if (t2 == t1)
159 return (0);
137 devs = sc->sc_serdevs;
138 if (sc->sc_ilr == PUC_ILR_DIGI) {
139 idx = 0;
140 while (devs & (0xfful << idx)) {
141 ilr = ~bus_read_1(sc->sc_port[idx].p_rres, 7);
142 devs &= ~0ul ^ ((u_long)ilr << idx);
143 idx += 8;
160 }
144 }
161 return (1);
145 } else if (sc->sc_ilr == PUC_ILR_QUATECH) {
146 /*
147 * Don't trust the value if it's the same as the option
148 * register. It may mean that the ILR is not active and
149 * we're reading the option register instead. This may
150 * lead to false positives on 8-port boards.
151 */
152 ilr = bus_read_1(sc->sc_port[0].p_rres, 7);
153 if (ilr != (sc->sc_cfg_data & 0xff))
154 devs &= (u_long)ilr;
155 }
162
156
163 default:
164 break;
157 ipend = 0;
158 idx = 0, dev = 1UL;
159 while (devs != 0UL) {
160 while ((devs & dev) == 0UL)
161 idx++, dev <<= 1;
162 devs &= ~dev;
163 port = &sc->sc_port[idx];
164 port->p_ipend = SERDEV_IPEND(port->p_dev);
165 ipend |= port->p_ipend;
165 }
166 }
166 return (0);
167
168 i = 0, isrc = SER_INT_OVERRUN;
169 while (ipend) {
170 while (i < PUC_ISRCCNT && !(ipend & isrc))
171 i++, isrc <<= 1;
172 KASSERT(i < PUC_ISRCCNT, ("%s", __func__));
173 ipend &= ~isrc;
174 idx = 0, dev = 1UL;
175 devs = sc->sc_serdevs;
176 while (devs != 0UL) {
177 while ((devs & dev) == 0UL)
178 idx++, dev <<= 1;
179 devs &= ~dev;
180 port = &sc->sc_port[idx];
181 if (!(port->p_ipend & isrc))
182 continue;
183 if (port->p_ihsrc[i] != NULL)
184 (*port->p_ihsrc[i])(port->p_iharg);
185 }
186 }
167}
168
169int
187}
188
189int
170puc_attach(device_t dev, const struct puc_device_description *desc)
190puc_bfe_attach(device_t dev)
171{
191{
172 char *typestr;
173 int bidx, childunit, i, irq_setup, ressz, rid, type;
192 char buffer[64];
193 struct puc_bar *bar;
194 struct puc_port *port;
174 struct puc_softc *sc;
195 struct puc_softc *sc;
175 struct puc_device *pdev;
176 struct resource *res;
177 struct resource_list_entry *rle;
178 bus_space_handle_t bh;
196 struct rman *rm;
197 intptr_t res;
198 bus_addr_t ofs, start;
199 bus_size_t size;
200 bus_space_handle_t bsh;
201 bus_space_tag_t bst;
202 int error, idx;
179
203
180 if (desc == NULL)
181 return (ENXIO);
204 sc = device_get_softc(dev);
182
205
183 sc = (struct puc_softc *)device_get_softc(dev);
184 bzero(sc, sizeof(*sc));
185 sc->sc_desc = *desc;
206 for (idx = 0; idx < PUC_PCI_BARS; idx++)
207 sc->sc_bar[idx].b_rid = -1;
186
208
187#ifdef PUC_DEBUG
188 bootverbose = 1;
209 do {
210 sc->sc_ioport.rm_type = RMAN_ARRAY;
211 error = rman_init(&sc->sc_ioport);
212 if (!error) {
213 sc->sc_iomem.rm_type = RMAN_ARRAY;
214 error = rman_init(&sc->sc_iomem);
215 if (!error) {
216 sc->sc_irq.rm_type = RMAN_ARRAY;
217 error = rman_init(&sc->sc_irq);
218 if (!error)
219 break;
220 rman_fini(&sc->sc_iomem);
221 }
222 rman_fini(&sc->sc_ioport);
223 }
224 return (error);
225 } while (0);
189
226
190 printf("puc: name: %s\n", sc->sc_desc.name);
191#endif
192 rid = 0;
193 res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
194 RF_ACTIVE | RF_SHAREABLE);
195 if (!res)
196 return (ENXIO);
227 snprintf(buffer, sizeof(buffer), "%s I/O port mapping",
228 device_get_nameunit(dev));
229 sc->sc_ioport.rm_descr = strdup(buffer, M_PUC);
230 snprintf(buffer, sizeof(buffer), "%s I/O memory mapping",
231 device_get_nameunit(dev));
232 sc->sc_iomem.rm_descr = strdup(buffer, M_PUC);
233 snprintf(buffer, sizeof(buffer), "%s port numbers",
234 device_get_nameunit(dev));
235 sc->sc_irq.rm_descr = strdup(buffer, M_PUC);
197
236
198 sc->irqres = res;
199 sc->irqrid = rid;
200#ifdef PUC_FASTINTR
201 irq_setup = bus_setup_intr(dev, res,
202 INTR_TYPE_TTY | INTR_FAST, puc_intr, sc, &sc->intr_cookie);
203 if (irq_setup == 0)
204 sc->fastintr = INTR_FAST;
205 else
206#else
207 irq_setup = bus_setup_intr(dev, res,
208 INTR_TYPE_TTY, puc_intr, sc, &sc->intr_cookie);
209#endif
210 if (irq_setup != 0)
211 return (ENXIO);
237 error = puc_config(sc, PUC_CFG_GET_NPORTS, 0, &res);
238 KASSERT(error == 0, ("%s %d", __func__, __LINE__));
239 sc->sc_nports = (int)res;
240 sc->sc_port = malloc(sc->sc_nports * sizeof(struct puc_port),
241 M_PUC, M_WAITOK|M_ZERO);
212
242
213 rid = 0;
214 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
215 if (i > 0 && rid == sc->sc_desc.ports[i].bar)
216 sc->barmuxed = 1;
217 rid = sc->sc_desc.ports[i].bar;
218 bidx = puc_port_bar_index(sc, rid);
243 error = rman_manage_region(&sc->sc_irq, 1, sc->sc_nports);
244 if (error)
245 goto fail;
219
246
220 if (bidx < 0 || sc->sc_bar_mappings[bidx].res != NULL)
221 continue;
247 error = puc_config(sc, PUC_CFG_SETUP, 0, &res);
248 if (error)
249 goto fail;
222
250
223 type = (sc->sc_desc.ports[i].flags & PUC_FLAGS_MEMORY)
224 ? SYS_RES_MEMORY : SYS_RES_IOPORT;
225
226 res = bus_alloc_resource_any(dev, type, &rid,
227 RF_ACTIVE);
228 if (res == NULL &&
229 sc->sc_desc.ports[i].flags & PUC_FLAGS_ALTRES) {
230 type = (type == SYS_RES_IOPORT)
231 ? SYS_RES_MEMORY : SYS_RES_IOPORT;
232 res = bus_alloc_resource_any(dev, type, &rid,
233 RF_ACTIVE);
251 for (idx = 0; idx < sc->sc_nports; idx++) {
252 port = &sc->sc_port[idx];
253 port->p_nr = idx + 1;
254 error = puc_config(sc, PUC_CFG_GET_TYPE, idx, &res);
255 if (error)
256 goto fail;
257 port->p_type = res;
258 error = puc_config(sc, PUC_CFG_GET_RID, idx, &res);
259 if (error)
260 goto fail;
261 bar = puc_get_bar(sc, res);
262 if (bar == NULL) {
263 error = ENXIO;
264 goto fail;
234 }
265 }
235 if (res == NULL) {
236 device_printf(dev, "could not get resource\n");
237 continue;
266 port->p_bar = bar;
267 start = rman_get_start(bar->b_res);
268 error = puc_config(sc, PUC_CFG_GET_OFS, idx, &res);
269 if (error)
270 goto fail;
271 ofs = res;
272 error = puc_config(sc, PUC_CFG_GET_LEN, idx, &res);
273 if (error)
274 goto fail;
275 size = res;
276 rm = (bar->b_type == SYS_RES_IOPORT)
277 ? &sc->sc_ioport: &sc->sc_iomem;
278 port->p_rres = rman_reserve_resource(rm, start + ofs,
279 start + ofs + size - 1, size, 0, NULL);
280 if (port->p_rres != NULL) {
281 bsh = rman_get_bushandle(bar->b_res);
282 bst = rman_get_bustag(bar->b_res);
283 bus_space_subregion(bst, bsh, ofs, size, &bsh);
284 rman_set_bushandle(port->p_rres, bsh);
285 rman_set_bustag(port->p_rres, bst);
238 }
286 }
239 sc->sc_bar_mappings[bidx].type = type;
240 sc->sc_bar_mappings[bidx].res = res;
241
242 if (sc->sc_desc.ilr_type != PUC_ILR_TYPE_NONE) {
243 sc->ilr_enabled = puc_probe_ilr(sc, res);
244 if (sc->ilr_enabled)
245 device_printf(dev, "ILR enabled\n");
246 else
247 device_printf(dev, "ILR disabled\n");
287 port->p_ires = rman_reserve_resource(&sc->sc_irq, port->p_nr,
288 port->p_nr, 1, 0, NULL);
289 if (port->p_ires == NULL) {
290 error = ENXIO;
291 goto fail;
248 }
292 }
249#ifdef PUC_DEBUG
250 printf("%s rid %d bst %lx, start %lx, end %lx\n",
251 (type == SYS_RES_MEMORY) ? "memory" : "port", rid,
252 (u_long)rman_get_bustag(res), (u_long)rman_get_start(res),
253 (u_long)rman_get_end(res));
254#endif
255 }
293 error = puc_config(sc, PUC_CFG_GET_CLOCK, idx, &res);
294 if (error)
295 goto fail;
296 port->p_rclk = res;
256
297
257 if (desc->init != NULL) {
258 i = desc->init(sc);
259 if (i != 0)
260 return (i);
298 port->p_dev = device_add_child(dev, NULL, -1);
299 if (port->p_dev != NULL)
300 device_set_ivars(port->p_dev, (void *)port);
261 }
262
301 }
302
263 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
264 rid = sc->sc_desc.ports[i].bar;
265 bidx = puc_port_bar_index(sc, rid);
266 if (bidx < 0 || sc->sc_bar_mappings[bidx].res == NULL)
267 continue;
303 error = puc_config(sc, PUC_CFG_GET_ILR, 0, &res);
304 if (error)
305 goto fail;
306 sc->sc_ilr = res;
307 if (bootverbose && sc->sc_ilr != 0)
308 device_printf(dev, "using interrupt latch register\n");
268
309
269 switch (sc->sc_desc.ports[i].type & ~PUC_PORT_SUBTYPE_MASK) {
270 case PUC_PORT_TYPE_COM:
271 typestr = "sio";
272 break;
273 case PUC_PORT_TYPE_LPT:
274 typestr = "ppc";
275 break;
276 case PUC_PORT_TYPE_UART:
277 typestr = "uart";
278 break;
279 default:
280 continue;
281 }
282 switch (sc->sc_desc.ports[i].type & PUC_PORT_SUBTYPE_MASK) {
283 case PUC_PORT_UART_SAB82532:
284 ressz = 64;
285 break;
286 case PUC_PORT_UART_Z8530:
287 ressz = 2;
288 break;
289 default:
290 ressz = 8;
291 break;
292 }
293 pdev = malloc(sizeof(struct puc_device), M_DEVBUF,
294 M_NOWAIT | M_ZERO);
295 if (!pdev)
296 continue;
297 resource_list_init(&pdev->resources);
310 sc->sc_irid = 0;
311 sc->sc_ires = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->sc_irid,
312 RF_ACTIVE|RF_SHAREABLE);
313 if (sc->sc_ires != NULL) {
314 error = bus_setup_intr(dev, sc->sc_ires,
315 INTR_TYPE_TTY | INTR_FAST, puc_intr, sc, &sc->sc_icookie);
316 if (error)
317 error = bus_setup_intr(dev, sc->sc_ires,
318 INTR_TYPE_TTY | INTR_MPSAFE, puc_intr, sc,
319 &sc->sc_icookie);
320 else
321 sc->sc_fastintr = 1;
298
322
299 /* First fake up an IRQ resource. */
300 resource_list_add(&pdev->resources, SYS_RES_IRQ, 0,
301 rman_get_start(sc->irqres), rman_get_end(sc->irqres),
302 rman_get_end(sc->irqres) - rman_get_start(sc->irqres) + 1);
303 rle = resource_list_find(&pdev->resources, SYS_RES_IRQ, 0);
304 rle->res = sc->irqres;
305
306 /* Now fake an IOPORT or MEMORY resource */
307 res = sc->sc_bar_mappings[bidx].res;
308 type = sc->sc_bar_mappings[bidx].type;
309 resource_list_add(&pdev->resources, type, 0,
310 rman_get_start(res) + sc->sc_desc.ports[i].offset,
311 rman_get_start(res) + sc->sc_desc.ports[i].offset
312 + ressz - 1, ressz);
313 rle = resource_list_find(&pdev->resources, type, 0);
314
315 if (sc->barmuxed == 0) {
316 rle->res = sc->sc_bar_mappings[bidx].res;
317 } else {
318 rle->res = rman_secret_puc_alloc_resource(M_WAITOK);
319 if (rle->res == NULL) {
320 free(pdev, M_DEVBUF);
321 return (ENOMEM);
322 }
323
324 rman_set_start(rle->res, rman_get_start(res) +
325 sc->sc_desc.ports[i].offset);
326 rman_set_end(rle->res, rman_get_start(rle->res) +
327 ressz - 1);
328 rman_set_bustag(rle->res, rman_get_bustag(res));
329 bus_space_subregion(rman_get_bustag(rle->res),
330 rman_get_bushandle(res),
331 sc->sc_desc.ports[i].offset, ressz,
332 &bh);
333 rman_set_bushandle(rle->res, bh);
323 if (error) {
324 device_printf(dev, "could not activate interrupt\n");
325 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irid,
326 sc->sc_ires);
327 sc->sc_ires = NULL;
334 }
328 }
329 }
330 if (sc->sc_ires == NULL) {
331 /* XXX no interrupt resource. Force polled mode. */
332 sc->sc_polled = 1;
333 }
335
334
336 pdev->port = i + 1;
337 pdev->serialfreq = sc->sc_desc.ports[i].serialfreq;
338 pdev->subtype = sc->sc_desc.ports[i].type &
339 PUC_PORT_SUBTYPE_MASK;
340 pdev->regshft = sc->sc_desc.ports[i].regshft;
341
342 childunit = puc_find_free_unit(typestr);
343 if (childunit < 0 && strcmp(typestr, "uart") != 0) {
344 typestr = "uart";
345 childunit = puc_find_free_unit(typestr);
346 }
347 sc->sc_ports[i].dev = device_add_child(dev, typestr,
348 childunit);
349 if (sc->sc_ports[i].dev == NULL) {
350 if (sc->barmuxed) {
351 bus_space_unmap(rman_get_bustag(rle->res),
352 rman_get_bushandle(rle->res), ressz);
353 rman_secret_puc_free_resource(rle->res);
354 free(pdev, M_DEVBUF);
355 }
335 /* Probe and attach our children. */
336 for (idx = 0; idx < sc->sc_nports; idx++) {
337 port = &sc->sc_port[idx];
338 if (port->p_dev == NULL)
356 continue;
339 continue;
340 error = device_probe_and_attach(port->p_dev);
341 if (error) {
342 device_delete_child(dev, port->p_dev);
343 port->p_dev = NULL;
357 }
344 }
358 device_set_ivars(sc->sc_ports[i].dev, pdev);
359 device_set_desc(sc->sc_ports[i].dev, sc->sc_desc.name);
360#ifdef PUC_DEBUG
361 printf("puc: type %d, bar %x, offset %x\n",
362 sc->sc_desc.ports[i].type,
363 sc->sc_desc.ports[i].bar,
364 sc->sc_desc.ports[i].offset);
365 puc_print_resource_list(&pdev->resources);
366#endif
367 device_set_flags(sc->sc_ports[i].dev,
368 sc->sc_desc.ports[i].flags);
369 if (device_probe_and_attach(sc->sc_ports[i].dev) != 0) {
370 if (sc->barmuxed) {
371 bus_space_unmap(rman_get_bustag(rle->res),
372 rman_get_bushandle(rle->res), ressz);
373 rman_secret_puc_free_resource(rle->res);
374 free(pdev, M_DEVBUF);
375 }
376 }
377 }
378
345 }
346
379#ifdef PUC_DEBUG
380 bootverbose = 0;
381#endif
347 /*
348 * If there are no serdev devices, then our interrupt handler
349 * will do nothing. Tear it down.
350 */
351 if (sc->sc_serdevs == 0UL)
352 bus_teardown_intr(dev, sc->sc_ires, sc->sc_icookie);
353
382 return (0);
354 return (0);
383}
384
355
385static u_int32_t
386puc_ilr_read(struct puc_softc *sc)
387{
388 u_int32_t mask;
389 int i;
390
391 mask = 0;
392 switch (sc->sc_desc.ilr_type) {
393 case PUC_ILR_TYPE_DIGI:
394 for (i = 1; i >= 0 && sc->sc_desc.ilr_offset[i] != 0; i--) {
395 mask = (mask << 8) | (bus_space_read_1(sc->ilr_st,
396 sc->ilr_sh, sc->sc_desc.ilr_offset[i]) & 0xff);
397 }
398 break;
399
400 default:
401 mask = 0xffffffff;
402 break;
356fail:
357 for (idx = 0; idx < sc->sc_nports; idx++) {
358 port = &sc->sc_port[idx];
359 if (port->p_dev != NULL)
360 device_delete_child(dev, port->p_dev);
361 if (port->p_rres != NULL)
362 rman_release_resource(port->p_rres);
363 if (port->p_ires != NULL)
364 rman_release_resource(port->p_ires);
403 }
365 }
404 return (mask);
366 for (idx = 0; idx < PUC_PCI_BARS; idx++) {
367 bar = &sc->sc_bar[idx];
368 if (bar->b_res != NULL)
369 bus_release_resource(sc->sc_dev, bar->b_type,
370 bar->b_rid, bar->b_res);
371 }
372 rman_fini(&sc->sc_irq);
373 free(__DECONST(void *, sc->sc_irq.rm_descr), M_PUC);
374 rman_fini(&sc->sc_iomem);
375 free(__DECONST(void *, sc->sc_iomem.rm_descr), M_PUC);
376 rman_fini(&sc->sc_ioport);
377 free(__DECONST(void *, sc->sc_ioport.rm_descr), M_PUC);
378 free(sc->sc_port, M_PUC);
379 return (error);
405}
406
380}
381
407/*
408 * This is an interrupt handler. For boards that can't tell us which
409 * device generated the interrupt it just calls all the registered
410 * handlers sequencially, but for boards that can tell us which
411 * device(s) generated the interrupt it calls only handlers for devices
412 * that actually generated the interrupt.
413 */
414static void
415puc_intr(void *arg)
382int
383puc_bfe_detach(device_t dev)
416{
384{
417 int i;
418 u_int32_t ilr_mask;
385 struct puc_bar *bar;
386 struct puc_port *port;
419 struct puc_softc *sc;
387 struct puc_softc *sc;
388 int error, idx;
420
389
421 sc = (struct puc_softc *)arg;
422 ilr_mask = sc->ilr_enabled ? puc_ilr_read(sc) : 0xffffffff;
423 for (i = 0; i < PUC_MAX_PORTS; i++)
424 if (sc->sc_ports[i].ihand != NULL &&
425 ((ilr_mask >> i) & 0x00000001))
426 (sc->sc_ports[i].ihand)(sc->sc_ports[i].ihandarg);
427}
390 sc = device_get_softc(dev);
428
391
429static int
430puc_find_free_unit(char *name)
431{
432 devclass_t dc;
433 int start;
434 int unit;
392 /* Detach our children. */
393 error = 0;
394 for (idx = 0; idx < sc->sc_nports; idx++) {
395 port = &sc->sc_port[idx];
396 if (port->p_dev == NULL)
397 continue;
398 if (device_detach(port->p_dev) == 0) {
399 device_delete_child(dev, port->p_dev);
400 if (port->p_rres != NULL)
401 rman_release_resource(port->p_rres);
402 if (port->p_ires != NULL)
403 rman_release_resource(port->p_ires);
404 } else
405 error = ENXIO;
406 }
407 if (error)
408 return (error);
435
409
436 unit = 0;
437 start = 0;
438 while (resource_int_value(name, unit, "port", &start) == 0 &&
439 start > 0)
440 unit++;
441 dc = devclass_find(name);
442 if (dc == NULL)
443 return (-1);
444 while (devclass_get_device(dc, unit))
445 unit++;
446#ifdef PUC_DEBUG
447 printf("puc: Using %s%d\n", name, unit);
448#endif
449 return (unit);
410 if (sc->sc_serdevs != 0UL)
411 bus_teardown_intr(dev, sc->sc_ires, sc->sc_icookie);
412 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irid, sc->sc_ires);
413
414 for (idx = 0; idx < PUC_PCI_BARS; idx++) {
415 bar = &sc->sc_bar[idx];
416 if (bar->b_res != NULL)
417 bus_release_resource(sc->sc_dev, bar->b_type,
418 bar->b_rid, bar->b_res);
419 }
420
421 rman_fini(&sc->sc_irq);
422 free(__DECONST(void *, sc->sc_irq.rm_descr), M_PUC);
423 rman_fini(&sc->sc_iomem);
424 free(__DECONST(void *, sc->sc_iomem.rm_descr), M_PUC);
425 rman_fini(&sc->sc_ioport);
426 free(__DECONST(void *, sc->sc_ioport.rm_descr), M_PUC);
427 free(sc->sc_port, M_PUC);
428 return (0);
450}
451
429}
430
452#ifdef PUC_DEBUG
453static void
454puc_print_resource_list(struct resource_list *rl)
431int
432puc_bfe_probe(device_t dev, const struct puc_cfg *cfg)
455{
433{
456#if 0
457 struct resource_list_entry *rle;
434 struct puc_softc *sc;
435 intptr_t res;
436 int error;
458
437
459 printf("print_resource_list: rl %p\n", rl);
460 SLIST_FOREACH(rle, rl, link)
461 printf(" type %x, rid %x start %lx end %lx count %lx\n",
462 rle->type, rle->rid, rle->start, rle->end, rle->count);
463 printf("print_resource_list: end.\n");
464#endif
438 sc = device_get_softc(dev);
439 sc->sc_dev = dev;
440 sc->sc_cfg = cfg;
441
442 /* We don't attach to single-port serial cards. */
443 if (cfg->ports == PUC_PORT_1S || cfg->ports == PUC_PORT_1P)
444 return (EDOOFUS);
445 error = puc_config(sc, PUC_CFG_GET_NPORTS, 0, &res);
446 if (error)
447 return (error);
448 error = puc_config(sc, PUC_CFG_GET_DESC, 0, &res);
449 if (error)
450 return (error);
451 if (res != 0)
452 device_set_desc(dev, (const char *)res);
453 return (BUS_PROBE_DEFAULT);
465}
454}
466#endif
467
468struct resource *
455
456struct resource *
469puc_alloc_resource(device_t dev, device_t child, int type, int *rid,
457puc_bus_alloc_resource(device_t dev, device_t child, int type, int *rid,
470 u_long start, u_long end, u_long count, u_int flags)
471{
458 u_long start, u_long end, u_long count, u_int flags)
459{
472 struct puc_device *pdev;
473 struct resource *retval;
474 struct resource_list *rl;
475 struct resource_list_entry *rle;
476 device_t my_child;
460 struct puc_port *port;
461 struct resource *res;
462 device_t assigned, originator;
463 int error;
477
464
478 /*
479 * in the case of a child of child we need to find our immediate child
480 */
481 for (my_child = child; device_get_parent(my_child) != dev;
482 my_child = device_get_parent(my_child));
465 /* Get our immediate child. */
466 originator = child;
467 while (child != NULL && device_get_parent(child) != dev)
468 child = device_get_parent(child);
469 if (child == NULL)
470 return (NULL);
483
471
484 pdev = device_get_ivars(my_child);
485 rl = &pdev->resources;
472 port = device_get_ivars(child);
473 KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
486
474
487#ifdef PUC_DEBUG
488 printf("puc_alloc_resource: pdev %p, looking for t %x, r %x\n",
489 pdev, type, *rid);
490 puc_print_resource_list(rl);
491#endif
492 retval = NULL;
493 rle = resource_list_find(rl, type, *rid);
494 if (rle) {
495#ifdef PUC_DEBUG
496 printf("found rle, %lx, %lx, %lx\n", rle->start, rle->end,
497 rle->count);
498#endif
499 retval = rle->res;
500 }
501#ifdef PUC_DEBUG
475 if (rid == NULL || *rid != 0)
476 return (NULL);
477
478 /* We only support default allocations. */
479 if (start != 0UL || end != ~0UL)
480 return (NULL);
481
482 if (type == port->p_bar->b_type)
483 res = port->p_rres;
484 else if (type == SYS_RES_IRQ)
485 res = port->p_ires;
502 else
486 else
503 printf("oops rle is gone\n");
504#endif
487 return (NULL);
505
488
506 return (retval);
489 if (res == NULL)
490 return (NULL);
491
492 assigned = rman_get_device(res);
493 if (assigned == NULL) /* Not allocated */
494 rman_set_device(res, originator);
495 else if (assigned != originator)
496 return (NULL);
497
498 if (flags & RF_ACTIVE) {
499 error = rman_activate_resource(res);
500 if (error) {
501 if (assigned == NULL)
502 rman_set_device(res, NULL);
503 return (NULL);
504 }
505 }
506
507 return (res);
507}
508
509int
508}
509
510int
510puc_release_resource(device_t dev, device_t child, int type, int rid,
511puc_bus_release_resource(device_t dev, device_t child, int type, int rid,
511 struct resource *res)
512{
512 struct resource *res)
513{
514 struct puc_port *port;
515 device_t originator;
516
517 /* Get our immediate child. */
518 originator = child;
519 while (child != NULL && device_get_parent(child) != dev)
520 child = device_get_parent(child);
521 if (child == NULL)
522 return (EINVAL);
523
524 port = device_get_ivars(child);
525 KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
526
527 if (rid != 0 || res == NULL)
528 return (EINVAL);
529
530 if (type == port->p_bar->b_type) {
531 if (res != port->p_rres)
532 return (EINVAL);
533 } else if (type == SYS_RES_IRQ) {
534 if (res != port->p_ires)
535 return (EINVAL);
536 if (port->p_hasintr)
537 return (EBUSY);
538 } else
539 return (EINVAL);
540
541 if (rman_get_device(res) != originator)
542 return (ENXIO);
543 if (rman_get_flags(res) & RF_ACTIVE)
544 rman_deactivate_resource(res);
545 rman_set_device(res, NULL);
513 return (0);
514}
515
516int
546 return (0);
547}
548
549int
517puc_get_resource(device_t dev, device_t child, int type, int rid,
550puc_bus_get_resource(device_t dev, device_t child, int type, int rid,
518 u_long *startp, u_long *countp)
519{
551 u_long *startp, u_long *countp)
552{
520 struct puc_device *pdev;
521 struct resource_list *rl;
522 struct resource_list_entry *rle;
553 struct puc_port *port;
554 struct resource *res;
555 u_long start;
523
556
524 pdev = device_get_ivars(child);
525 rl = &pdev->resources;
557 /* Get our immediate child. */
558 while (child != NULL && device_get_parent(child) != dev)
559 child = device_get_parent(child);
560 if (child == NULL)
561 return (EINVAL);
526
562
527#ifdef PUC_DEBUG
528 printf("puc_get_resource: pdev %p, looking for t %x, r %x\n", pdev,
529 type, rid);
530 puc_print_resource_list(rl);
531#endif
532 rle = resource_list_find(rl, type, rid);
533 if (rle) {
534#ifdef PUC_DEBUG
535 printf("found rle %p,", rle);
536#endif
537 if (startp != NULL)
538 *startp = rle->start;
539 if (countp != NULL)
540 *countp = rle->count;
541#ifdef PUC_DEBUG
542 printf(" %lx, %lx\n", rle->start, rle->count);
543#endif
544 return (0);
545 } else
546 printf("oops rle is gone\n");
547 return (ENXIO);
563 port = device_get_ivars(child);
564 KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
565
566 if (type == port->p_bar->b_type)
567 res = port->p_rres;
568 else if (type == SYS_RES_IRQ)
569 res = port->p_ires;
570 else
571 return (ENXIO);
572
573 if (rid != 0 || res == NULL)
574 return (ENXIO);
575
576 start = rman_get_start(res);
577 if (startp != NULL)
578 *startp = start;
579 if (countp != NULL)
580 *countp = rman_get_end(res) - start + 1;
581 return (0);
548}
549
550int
582}
583
584int
551puc_setup_intr(device_t dev, device_t child, struct resource *r, int flags,
552 void (*ihand)(void *), void *arg, void **cookiep)
585puc_bus_setup_intr(device_t dev, device_t child, struct resource *res,
586 int flags, void (*ihand)(void *), void *arg, void **cookiep)
553{
587{
554 int i;
588 struct puc_port *port;
555 struct puc_softc *sc;
589 struct puc_softc *sc;
590 device_t originator;
591 int i, isrc, serdev;
556
592
557 sc = (struct puc_softc *)device_get_softc(dev);
558 if ((flags & INTR_FAST) != sc->fastintr)
593 sc = device_get_softc(dev);
594
595 /* Get our immediate child. */
596 originator = child;
597 while (child != NULL && device_get_parent(child) != dev)
598 child = device_get_parent(child);
599 if (child == NULL)
600 return (EINVAL);
601
602 port = device_get_ivars(child);
603 KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
604
605 if (ihand == NULL || cookiep == NULL || res != port->p_ires)
606 return (EINVAL);
607 if (rman_get_device(port->p_ires) != originator)
559 return (ENXIO);
608 return (ENXIO);
560 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
561 if (sc->sc_ports[i].dev == child) {
562 if (sc->sc_ports[i].ihand != 0)
563 return (ENXIO);
564 sc->sc_ports[i].ihand = ihand;
565 sc->sc_ports[i].ihandarg = arg;
566 *cookiep = arg;
567 return (0);
609
610 /*
611 * Have non-serdev ports handled by the bus implementation. It
612 * supports multiple handlers for a single interrupt as it is,
613 * so we wouldn't add value if we did it ourselves.
614 */
615 serdev = 0;
616 if (port->p_type == PUC_TYPE_SERIAL) {
617 i = 0, isrc = SER_INT_OVERRUN;
618 while (i < PUC_ISRCCNT) {
619 port->p_ihsrc[i] = SERDEV_IHAND(originator, isrc);
620 if (port->p_ihsrc[i] != NULL)
621 serdev = 1;
622 i++, isrc <<= 1;
568 }
569 }
623 }
624 }
570 return (ENXIO);
625 if (!serdev)
626 return (BUS_SETUP_INTR(device_get_parent(dev), originator,
627 sc->sc_ires, flags, ihand, arg, cookiep));
628
629 /* We demand that serdev devices use fast interrupts. */
630 if (!(flags & INTR_FAST))
631 return (ENXIO);
632
633 sc->sc_serdevs |= 1UL << (port->p_nr - 1);
634
635 port->p_hasintr = 1;
636 port->p_ih = ihand;
637 port->p_iharg = arg;
638
639 *cookiep = port;
640 return (0);
571}
572
573int
641}
642
643int
574puc_teardown_intr(device_t dev, device_t child, struct resource *r,
575 void *cookie)
644puc_bus_teardown_intr(device_t dev, device_t child, struct resource *res,
645 void *cookie)
576{
646{
577 int i;
647 struct puc_port *port;
578 struct puc_softc *sc;
648 struct puc_softc *sc;
649 device_t originator;
650 int i;
579
651
580 sc = (struct puc_softc *)device_get_softc(dev);
581 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
582 if (sc->sc_ports[i].dev == child) {
583 sc->sc_ports[i].ihand = NULL;
584 sc->sc_ports[i].ihandarg = NULL;
585 return (0);
586 }
587 }
588 return (ENXIO);
652 sc = device_get_softc(dev);
653
654 /* Get our immediate child. */
655 originator = child;
656 while (child != NULL && device_get_parent(child) != dev)
657 child = device_get_parent(child);
658 if (child == NULL)
659 return (EINVAL);
660
661 port = device_get_ivars(child);
662 KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
663
664 if (res != port->p_ires)
665 return (EINVAL);
666 if (rman_get_device(port->p_ires) != originator)
667 return (ENXIO);
668
669 if (!port->p_hasintr)
670 return (BUS_TEARDOWN_INTR(device_get_parent(dev), originator,
671 sc->sc_ires, cookie));
672
673 if (cookie != port)
674 return (EINVAL);
675
676 port->p_hasintr = 0;
677 port->p_ih = NULL;
678 port->p_iharg = NULL;
679
680 for (i = 0; i < PUC_ISRCCNT; i++)
681 port->p_ihsrc[i] = NULL;
682
683 return (0);
589}
590
591int
684}
685
686int
592puc_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
687puc_bus_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
593{
688{
594 struct puc_device *pdev;
689 struct puc_port *port;
595
690
596 pdev = device_get_ivars(child);
597 if (pdev == NULL)
598 return (ENOENT);
691 /* Get our immediate child. */
692 while (child != NULL && device_get_parent(child) != dev)
693 child = device_get_parent(child);
694 if (child == NULL)
695 return (EINVAL);
599
696
697 port = device_get_ivars(child);
698 KASSERT(port != NULL, ("%s %d", __func__, __LINE__));
699
700 if (result == NULL)
701 return (EINVAL);
702
600 switch(index) {
703 switch(index) {
601 case PUC_IVAR_FREQ:
602 *result = pdev->serialfreq;
704 case PUC_IVAR_CLOCK:
705 *result = port->p_rclk;
603 break;
706 break;
604 case PUC_IVAR_PORT:
605 *result = pdev->port;
707 case PUC_IVAR_TYPE:
708 *result = port->p_type;
606 break;
709 break;
607 case PUC_IVAR_REGSHFT:
608 *result = pdev->regshft;
609 break;
610 case PUC_IVAR_SUBTYPE:
611 *result = pdev->subtype;
612 break;
613 default:
614 return (ENOENT);
615 }
616 return (0);
617}
710 default:
711 return (ENOENT);
712 }
713 return (0);
714}