2 * Copyright (c) 2004-2006 Marcel Moolenaar
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/queue.h>
37 #include <sys/serial.h>
39 #include <machine/bus.h>
40 #include <machine/resource.h>
43 #include <dev/scc/scc_bfe.h>
44 #include <dev/scc/scc_bus.h>
48 devclass_t scc_devclass;
49 const char scc_driver_name[] = "scc";
51 static MALLOC_DEFINE(M_SCC, "SCC", "SCC driver");
54 scc_bfe_intr(void *arg)
56 struct scc_softc *sc = arg;
60 int c, i, ipend, isrc;
63 while (!sc->sc_leaving && (ipend = SCC_IPEND(sc)) != 0) {
64 i = 0, isrc = SER_INT_OVERRUN;
66 while (i < SCC_ISRCCNT && !(ipend & isrc))
68 KASSERT(i < SCC_ISRCCNT, ("%s", __func__));
70 for (c = 0; c < cl->cl_channels; c++) {
72 if (!(ch->ch_ipend & isrc))
75 if (m->ih_src[i] == NULL)
77 if ((*m->ih_src[i])(m->ih_arg))
78 ch->ch_ipend &= ~isrc;
81 for (c = 0; c < cl->cl_channels; c++) {
91 return (FILTER_HANDLED);
93 return (FILTER_STRAY);
97 scc_bfe_attach(device_t dev, u_int ipc)
99 struct resource_list_entry *rle;
101 struct scc_class *cl;
103 struct scc_softc *sc, *sc0;
105 bus_space_handle_t bh;
106 rman_res_t base, size, start, sz;
107 int c, error, mode, sysdev;
110 * The sc_class field defines the type of SCC we're going to work
111 * with and thus the size of the softc. Replace the generic softc
112 * with one that matches the SCC now that we're certain we handle
115 sc0 = device_get_softc(dev);
117 if (cl->size > sizeof(*sc)) {
118 sc = malloc(cl->size, M_SCC, M_WAITOK|M_ZERO);
119 bcopy(sc0, sc, sizeof(*sc));
120 device_set_softc(dev, sc);
124 size = abs(cl->cl_range) << sc->sc_bas.regshft;
126 mtx_init(&sc->sc_hwmtx, "scc_hwmtx", NULL, MTX_SPIN);
129 * Re-allocate. We expect that the softc contains the information
130 * collected by scc_bfe_probe() intact.
132 sc->sc_rres = bus_alloc_resource_anywhere(dev, sc->sc_rtype,
133 &sc->sc_rrid, cl->cl_channels * size, RF_ACTIVE);
134 if (sc->sc_rres == NULL)
136 sc->sc_bas.bsh = rman_get_bushandle(sc->sc_rres);
137 sc->sc_bas.bst = rman_get_bustag(sc->sc_rres);
140 * Allocate interrupt resources. There may be a different interrupt
141 * per channel. We allocate them all...
143 sc->sc_chan = malloc(sizeof(struct scc_chan) * cl->cl_channels,
144 M_SCC, M_WAITOK | M_ZERO);
145 for (c = 0; c < cl->cl_channels; c++) {
146 ch = &sc->sc_chan[c];
148 * XXX temporary hack. If we have more than 1 interrupt
149 * per channel, allocate the first for the channel. At
150 * this time only the macio bus front-end has more than
151 * 1 interrupt per channel and we don't use the 2nd and
152 * 3rd, because we don't support DMA yet.
154 ch->ch_irid = c * ipc;
155 ch->ch_ires = bus_alloc_resource_any(dev, SYS_RES_IRQ,
156 &ch->ch_irid, RF_ACTIVE | RF_SHAREABLE);
162 * Create the control structures for our children. Probe devices
163 * and query them to see if we can reset the hardware.
166 base = rman_get_start(sc->sc_rres);
167 sz = (size != 0) ? size : rman_get_size(sc->sc_rres);
168 start = base + ((cl->cl_range < 0) ? size * (cl->cl_channels - 1) : 0);
169 for (c = 0; c < cl->cl_channels; c++) {
170 ch = &sc->sc_chan[c];
171 resource_list_init(&ch->ch_rlist);
174 if (!SCC_ENABLED(sc, ch))
178 resource_list_add(&ch->ch_rlist, sc->sc_rtype, 0, start,
180 rle = resource_list_find(&ch->ch_rlist, sc->sc_rtype, 0);
181 rle->res = &ch->ch_rres;
182 bus_space_subregion(rman_get_bustag(sc->sc_rres),
183 rman_get_bushandle(sc->sc_rres), start - base, sz, &bh);
184 rman_set_bushandle(rle->res, bh);
185 rman_set_bustag(rle->res, rman_get_bustag(sc->sc_rres));
187 resource_list_add(&ch->ch_rlist, SYS_RES_IRQ, 0, c, c, 1);
188 rle = resource_list_find(&ch->ch_rlist, SYS_RES_IRQ, 0);
189 rle->res = (ch->ch_ires != NULL) ? ch->ch_ires :
190 sc->sc_chan[0].ch_ires;
192 for (mode = 0; mode < SCC_NMODES; mode++) {
193 m = &ch->ch_mode[mode];
195 m->m_mode = 1U << mode;
196 if ((cl->cl_modes & m->m_mode) == 0 || ch->ch_sysdev)
198 m->m_dev = device_add_child(dev, NULL, -1);
199 device_set_ivars(m->m_dev, (void *)m);
200 error = device_probe_child(dev, m->m_dev);
203 m->m_sysdev = SERDEV_SYSDEV(m->m_dev) ? 1 : 0;
204 ch->ch_sysdev |= m->m_sysdev;
209 start += (cl->cl_range < 0) ? -size : size;
210 sysdev |= ch->ch_sysdev;
214 * Have the hardware driver initialize the hardware. Tell it
215 * whether or not a hardware reset should be performed.
218 device_printf(dev, "%sresetting hardware\n",
219 (sysdev) ? "not " : "");
221 error = SCC_ATTACH(sc, !sysdev);
226 * Setup our interrupt handler. Make it FAST under the assumption
227 * that our children's are fast as well. We make it MPSAFE as soon
228 * as a child sets up a MPSAFE interrupt handler.
229 * Of course, if we can't setup a fast handler, we make it MPSAFE
232 for (c = 0; c < cl->cl_channels; c++) {
233 ch = &sc->sc_chan[c];
234 if (ch->ch_ires == NULL)
236 error = bus_setup_intr(dev, ch->ch_ires,
237 INTR_TYPE_TTY, scc_bfe_intr, NULL, sc,
240 error = bus_setup_intr(dev, ch->ch_ires,
241 INTR_TYPE_TTY | INTR_MPSAFE, NULL,
242 (driver_intr_t *)scc_bfe_intr, sc, &ch->ch_icookie);
247 device_printf(dev, "could not activate interrupt\n");
248 bus_release_resource(dev, SYS_RES_IRQ, ch->ch_irid,
254 for (c = 0; c < cl->cl_channels; c++) {
255 if (sc->sc_chan[0].ch_ires != NULL)
260 * Attach all child devices that were probed successfully.
262 for (c = 0; c < cl->cl_channels; c++) {
263 ch = &sc->sc_chan[c];
264 for (mode = 0; mode < SCC_NMODES; mode++) {
265 m = &ch->ch_mode[mode];
268 error = device_attach(m->m_dev);
275 if (bootverbose && (sc->sc_fastintr || sc->sc_polled)) {
277 device_print_prettyname(dev);
278 if (sc->sc_fastintr) {
279 printf("%sfast interrupt", sep);
283 printf("%spolled mode", sep);
292 for (c = 0; c < cl->cl_channels; c++) {
293 ch = &sc->sc_chan[c];
294 if (ch->ch_ires == NULL)
296 bus_release_resource(dev, SYS_RES_IRQ, ch->ch_irid,
299 bus_release_resource(dev, sc->sc_rtype, sc->sc_rrid, sc->sc_rres);
304 scc_bfe_detach(device_t dev)
307 struct scc_class *cl;
309 struct scc_softc *sc;
310 int chan, error, mode;
312 sc = device_get_softc(dev);
315 /* Detach our children. */
317 for (chan = 0; chan < cl->cl_channels; chan++) {
318 ch = &sc->sc_chan[chan];
319 for (mode = 0; mode < SCC_NMODES; mode++) {
320 m = &ch->ch_mode[mode];
323 if (device_detach(m->m_dev) != 0)
333 for (chan = 0; chan < cl->cl_channels; chan++) {
334 ch = &sc->sc_chan[chan];
335 if (ch->ch_ires == NULL)
337 bus_teardown_intr(dev, ch->ch_ires, ch->ch_icookie);
338 bus_release_resource(dev, SYS_RES_IRQ, ch->ch_irid,
341 bus_release_resource(dev, sc->sc_rtype, sc->sc_rrid, sc->sc_rres);
343 free(sc->sc_chan, M_SCC);
345 mtx_destroy(&sc->sc_hwmtx);
350 scc_bfe_probe(device_t dev, u_int regshft, u_int rclk, u_int rid)
352 struct scc_softc *sc;
353 struct scc_class *cl;
358 * Initialize the instance. Note that the instance (=softc) does
359 * not necessarily match the hardware specific softc. We can't do
360 * anything about it now, because we may not attach to the device.
361 * Hardware drivers cannot use any of the class specific fields
364 sc = device_get_softc(dev);
366 kobj_init((kobj_t)sc, (kobj_class_t)cl);
368 if (device_get_desc(dev) == NULL)
369 device_set_desc(dev, cl->name);
371 size = abs(cl->cl_range) << regshft;
374 * Allocate the register resource. We assume that all SCCs have a
375 * single register window in either I/O port space or memory mapped
376 * I/O space. Any SCC that needs multiple windows will consequently
377 * not be supported by this driver as-is.
380 sc->sc_rtype = SYS_RES_MEMORY;
381 sc->sc_rres = bus_alloc_resource_anywhere(dev, sc->sc_rtype,
382 &sc->sc_rrid, cl->cl_channels * size, RF_ACTIVE);
383 if (sc->sc_rres == NULL) {
385 sc->sc_rtype = SYS_RES_IOPORT;
386 sc->sc_rres = bus_alloc_resource_anywhere(dev, sc->sc_rtype,
387 &sc->sc_rrid, cl->cl_channels * size, RF_ACTIVE);
388 if (sc->sc_rres == NULL)
393 * Fill in the bus access structure and call the hardware specific
396 sz = (size != 0) ? size : rman_get_size(sc->sc_rres);
397 sc->sc_bas.bsh = rman_get_bushandle(sc->sc_rres);
398 sc->sc_bas.bst = rman_get_bustag(sc->sc_rres);
399 sc->sc_bas.range = sz;
400 sc->sc_bas.rclk = rclk;
401 sc->sc_bas.regshft = regshft;
403 error = SCC_PROBE(sc);
404 bus_release_resource(dev, sc->sc_rtype, sc->sc_rrid, sc->sc_rres);
405 return ((error == 0) ? BUS_PROBE_DEFAULT : error);
409 scc_bus_alloc_resource(device_t dev, device_t child, int type, int *rid,
410 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
412 struct resource_list_entry *rle;
416 if (device_get_parent(child) != dev)
419 /* We only support default allocations. */
420 if (!RMAN_IS_DEFAULT_RANGE(start, end))
423 m = device_get_ivars(child);
425 rle = resource_list_find(&ch->ch_rlist, type, 0);
433 scc_bus_get_resource(device_t dev, device_t child, int type, int rid,
434 rman_res_t *startp, rman_res_t *countp)
436 struct resource_list_entry *rle;
440 if (device_get_parent(child) != dev)
443 m = device_get_ivars(child);
445 rle = resource_list_find(&ch->ch_rlist, type, rid);
450 *startp = rle->start;
452 *countp = rle->count;
457 scc_bus_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
460 struct scc_class *cl;
462 struct scc_softc *sc;
464 if (device_get_parent(child) != dev)
467 sc = device_get_softc(dev);
469 m = device_get_ivars(child);
473 case SCC_IVAR_CHANNEL:
477 *result = cl->cl_class;
480 *result = sc->sc_bas.rclk;
485 case SCC_IVAR_REGSHFT:
486 *result = sc->sc_bas.regshft;
489 *result = (uintptr_t)&sc->sc_hwmtx;
498 scc_bus_release_resource(device_t dev, device_t child, int type, int rid,
499 struct resource *res)
501 struct resource_list_entry *rle;
505 if (device_get_parent(child) != dev)
508 m = device_get_ivars(child);
510 rle = resource_list_find(&ch->ch_rlist, type, rid);
511 return ((rle == NULL) ? EINVAL : 0);
515 scc_bus_setup_intr(device_t dev, device_t child, struct resource *r, int flags,
516 driver_filter_t *filt, void (*ihand)(void *), void *arg, void **cookiep)
520 struct scc_softc *sc;
523 if (device_get_parent(child) != dev)
526 /* Interrupt handlers must be FAST or MPSAFE. */
527 if (filt == NULL && !(flags & INTR_MPSAFE))
530 sc = device_get_softc(dev);
534 if (sc->sc_fastintr && filt == NULL) {
536 for (c = 0; c < sc->sc_class->cl_channels; c++) {
537 ch = &sc->sc_chan[c];
538 if (ch->ch_ires == NULL)
540 bus_teardown_intr(dev, ch->ch_ires, ch->ch_icookie);
541 bus_setup_intr(dev, ch->ch_ires,
542 INTR_TYPE_TTY | INTR_MPSAFE, NULL,
543 (driver_intr_t *)scc_bfe_intr, sc, &ch->ch_icookie);
547 m = device_get_ivars(child);
549 m->m_fastintr = (filt != NULL) ? 1 : 0;
550 m->ih = (filt != NULL) ? filt : (driver_filter_t *)ihand;
553 i = 0, isrc = SER_INT_OVERRUN;
554 while (i < SCC_ISRCCNT) {
555 m->ih_src[i] = SERDEV_IHAND(child, isrc);
556 if (m->ih_src[i] != NULL)
564 scc_bus_teardown_intr(device_t dev, device_t child, struct resource *r,
570 if (device_get_parent(child) != dev)
573 m = device_get_ivars(child);
581 for (i = 0; i < SCC_ISRCCNT; i++)