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 char scc_driver_name[] = "scc";
51 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++) {
95 scc_bfe_attach(device_t dev)
97 struct resource_list_entry *rle;
101 struct scc_softc *sc, *sc0;
103 bus_space_handle_t bh;
104 u_long base, size, start;
105 int c, error, mode, sysdev;
108 * The sc_class field defines the type of SCC we're going to work
109 * with and thus the size of the softc. Replace the generic softc
110 * with one that matches the SCC now that we're certain we handle
113 sc0 = device_get_softc(dev);
115 if (cl->size > sizeof(*sc)) {
116 sc = malloc(cl->size, M_SCC, M_WAITOK|M_ZERO);
117 bcopy(sc0, sc, sizeof(*sc));
118 device_set_softc(dev, sc);
122 size = abs(cl->cl_range) << sc->sc_bas.regshft;
124 mtx_init(&sc->sc_hwmtx, "scc_hwmtx", NULL, MTX_SPIN);
127 * Re-allocate. We expect that the softc contains the information
128 * collected by scc_bfe_probe() intact.
130 sc->sc_rres = bus_alloc_resource(dev, sc->sc_rtype, &sc->sc_rrid,
131 0, ~0, cl->cl_channels * size, RF_ACTIVE);
132 if (sc->sc_rres == NULL)
134 sc->sc_bas.bsh = rman_get_bushandle(sc->sc_rres);
135 sc->sc_bas.bst = rman_get_bustag(sc->sc_rres);
138 * Allocate interrupt resources. There may be a different interrupt
139 * per channel. We allocate them all...
141 sc->sc_chan = malloc(sizeof(struct scc_chan) * cl->cl_channels,
142 M_SCC, M_WAITOK | M_ZERO);
143 for (c = 0; c < cl->cl_channels; c++) {
144 ch = &sc->sc_chan[c];
146 ch->ch_ires = bus_alloc_resource_any(dev, SYS_RES_IRQ,
147 &ch->ch_irid, RF_ACTIVE | RF_SHAREABLE);
151 * Create the control structures for our children. Probe devices
152 * and query them to see if we can reset the hardware.
155 base = rman_get_start(sc->sc_rres);
156 start = base + ((cl->cl_range < 0) ? size * (cl->cl_channels - 1) : 0);
157 for (c = 0; c < cl->cl_channels; c++) {
158 ch = &sc->sc_chan[c];
159 resource_list_init(&ch->ch_rlist);
162 resource_list_add(&ch->ch_rlist, sc->sc_rtype, 0, start,
163 start + size - 1, size);
164 rle = resource_list_find(&ch->ch_rlist, sc->sc_rtype, 0);
165 rle->res = &ch->ch_rres;
166 bus_space_subregion(rman_get_bustag(sc->sc_rres),
167 rman_get_bushandle(sc->sc_rres), start - base, size, &bh);
168 rman_set_bushandle(rle->res, bh);
169 rman_set_bustag(rle->res, rman_get_bustag(sc->sc_rres));
171 resource_list_add(&ch->ch_rlist, SYS_RES_IRQ, 0, c, c, 1);
172 rle = resource_list_find(&ch->ch_rlist, SYS_RES_IRQ, 0);
173 rle->res = (ch->ch_ires != NULL) ? ch->ch_ires :
174 sc->sc_chan[0].ch_ires;
176 for (mode = 0; mode < SCC_NMODES; mode++) {
177 m = &ch->ch_mode[mode];
179 m->m_mode = 1U << mode;
180 if ((cl->cl_modes & m->m_mode) == 0 || ch->ch_sysdev)
182 m->m_dev = device_add_child(dev, NULL, -1);
183 device_set_ivars(m->m_dev, (void *)m);
184 error = device_probe_child(dev, m->m_dev);
187 m->m_sysdev = SERDEV_SYSDEV(m->m_dev) ? 1 : 0;
188 ch->ch_sysdev |= m->m_sysdev;
192 start += (cl->cl_range < 0) ? -size : size;
193 sysdev |= ch->ch_sysdev;
197 * Have the hardware driver initialize the hardware. Tell it
198 * whether or not a hardware reset should be performed.
201 device_printf(dev, "%sresetting hardware\n",
202 (sysdev) ? "not " : "");
204 error = SCC_ATTACH(sc, !sysdev);
209 * Setup our interrupt handler. Make it FAST under the assumption
210 * that our children's are fast as well. We make it MPSAFE as soon
211 * as a child sets up a MPSAFE interrupt handler.
212 * Of course, if we can't setup a fast handler, we make it MPSAFE
215 for (c = 0; c < cl->cl_channels; c++) {
216 ch = &sc->sc_chan[c];
217 if (ch->ch_ires == NULL)
219 error = bus_setup_intr(dev, ch->ch_ires,
220 INTR_TYPE_TTY | INTR_FAST, scc_bfe_intr, sc,
223 error = bus_setup_intr(dev, ch->ch_ires,
224 INTR_TYPE_TTY | INTR_MPSAFE, scc_bfe_intr, sc,
230 device_printf(dev, "could not activate interrupt\n");
231 bus_release_resource(dev, SYS_RES_IRQ, ch->ch_irid,
237 for (c = 0; c < cl->cl_channels; c++) {
238 if (sc->sc_chan[0].ch_ires != NULL)
243 * Attach all child devices that were probed successfully.
245 for (c = 0; c < cl->cl_channels; c++) {
246 ch = &sc->sc_chan[c];
247 for (mode = 0; mode < SCC_NMODES; mode++) {
248 m = &ch->ch_mode[mode];
251 error = device_attach(m->m_dev);
258 if (bootverbose && (sc->sc_fastintr || sc->sc_polled)) {
260 device_print_prettyname(dev);
261 if (sc->sc_fastintr) {
262 printf("%sfast interrupt", sep);
266 printf("%spolled mode", sep);
275 for (c = 0; c < cl->cl_channels; c++) {
276 ch = &sc->sc_chan[c];
277 if (ch->ch_ires == NULL)
279 bus_release_resource(dev, SYS_RES_IRQ, ch->ch_irid,
282 bus_release_resource(dev, sc->sc_rtype, sc->sc_rrid, sc->sc_rres);
287 scc_bfe_detach(device_t dev)
290 struct scc_class *cl;
292 struct scc_softc *sc;
293 int chan, error, mode;
295 sc = device_get_softc(dev);
298 /* Detach our children. */
300 for (chan = 0; chan < cl->cl_channels; chan++) {
301 ch = &sc->sc_chan[chan];
302 for (mode = 0; mode < SCC_NMODES; mode++) {
303 m = &ch->ch_mode[mode];
306 if (device_detach(m->m_dev) != 0)
316 for (chan = 0; chan < cl->cl_channels; chan++) {
317 ch = &sc->sc_chan[chan];
318 if (ch->ch_ires == NULL)
320 bus_teardown_intr(dev, ch->ch_ires, ch->ch_icookie);
321 bus_release_resource(dev, SYS_RES_IRQ, ch->ch_irid,
324 bus_release_resource(dev, sc->sc_rtype, sc->sc_rrid, sc->sc_rres);
326 free(sc->sc_chan, M_SCC);
328 mtx_destroy(&sc->sc_hwmtx);
333 scc_bfe_probe(device_t dev, u_int regshft, u_int rclk)
335 struct scc_softc *sc;
336 struct scc_class *cl;
341 * Initialize the instance. Note that the instance (=softc) does
342 * not necessarily match the hardware specific softc. We can't do
343 * anything about it now, because we may not attach to the device.
344 * Hardware drivers cannot use any of the class specific fields
347 sc = device_get_softc(dev);
349 kobj_init((kobj_t)sc, (kobj_class_t)cl);
351 if (device_get_desc(dev) == NULL)
352 device_set_desc(dev, cl->name);
354 size = abs(cl->cl_range) << regshft;
357 * Allocate the register resource. We assume that all SCCs have a
358 * single register window in either I/O port space or memory mapped
359 * I/O space. Any SCC that needs multiple windows will consequently
360 * not be supported by this driver as-is.
363 sc->sc_rtype = SYS_RES_MEMORY;
364 sc->sc_rres = bus_alloc_resource(dev, sc->sc_rtype, &sc->sc_rrid,
365 0, ~0, cl->cl_channels * size, RF_ACTIVE);
366 if (sc->sc_rres == NULL) {
368 sc->sc_rtype = SYS_RES_IOPORT;
369 sc->sc_rres = bus_alloc_resource(dev, sc->sc_rtype,
370 &sc->sc_rrid, 0, ~0, cl->cl_channels * size, RF_ACTIVE);
371 if (sc->sc_rres == NULL)
376 * Fill in the bus access structure and call the hardware specific
379 sc->sc_bas.bsh = rman_get_bushandle(sc->sc_rres);
380 sc->sc_bas.bst = rman_get_bustag(sc->sc_rres);
381 sc->sc_bas.range = size;
382 sc->sc_bas.rclk = rclk;
383 sc->sc_bas.regshft = regshft;
385 error = SCC_PROBE(sc);
386 bus_release_resource(dev, sc->sc_rtype, sc->sc_rrid, sc->sc_rres);
387 return ((error == 0) ? BUS_PROBE_DEFAULT : error);
391 scc_bus_alloc_resource(device_t dev, device_t child, int type, int *rid,
392 u_long start, u_long end, u_long count, u_int flags)
394 struct resource_list_entry *rle;
398 if (device_get_parent(child) != dev)
401 /* We only support default allocations. */
402 if (start != 0UL || end != ~0UL)
405 m = device_get_ivars(child);
407 rle = resource_list_find(&ch->ch_rlist, type, 0);
415 scc_bus_get_resource(device_t dev, device_t child, int type, int rid,
416 u_long *startp, u_long *countp)
418 struct resource_list_entry *rle;
422 if (device_get_parent(child) != dev)
425 m = device_get_ivars(child);
427 rle = resource_list_find(&ch->ch_rlist, type, rid);
432 *startp = rle->start;
434 *countp = rle->count;
439 scc_bus_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
442 struct scc_class *cl;
444 struct scc_softc *sc;
446 if (device_get_parent(child) != dev)
449 sc = device_get_softc(dev);
451 m = device_get_ivars(child);
455 case SCC_IVAR_CHANNEL:
459 *result = cl->cl_class;
462 *result = sc->sc_bas.rclk;
467 case SCC_IVAR_REGSHFT:
468 *result = sc->sc_bas.regshft;
471 *result = (uintptr_t)&sc->sc_hwmtx;
480 scc_bus_release_resource(device_t dev, device_t child, int type, int rid,
481 struct resource *res)
483 struct resource_list_entry *rle;
487 if (device_get_parent(child) != dev)
490 m = device_get_ivars(child);
492 rle = resource_list_find(&ch->ch_rlist, type, rid);
493 return ((rle == NULL) ? EINVAL : 0);
497 scc_bus_setup_intr(device_t dev, device_t child, struct resource *r, int flags,
498 void (*ihand)(void *), void *arg, void **cookiep)
502 struct scc_softc *sc;
505 if (device_get_parent(child) != dev)
508 /* Interrupt handlers must be FAST or MPSAFE. */
509 if ((flags & (INTR_FAST|INTR_MPSAFE)) == 0)
512 sc = device_get_softc(dev);
516 if (sc->sc_fastintr && !(flags & INTR_FAST)) {
518 for (c = 0; c < sc->sc_class->cl_channels; c++) {
519 ch = &sc->sc_chan[c];
520 if (ch->ch_ires == NULL)
522 bus_teardown_intr(dev, ch->ch_ires, ch->ch_icookie);
523 bus_setup_intr(dev, ch->ch_ires,
524 INTR_TYPE_TTY | INTR_MPSAFE, scc_bfe_intr, sc,
529 m = device_get_ivars(child);
531 m->m_fastintr = (flags & INTR_FAST) ? 1 : 0;
535 i = 0, isrc = SER_INT_OVERRUN;
536 while (i < SCC_ISRCCNT) {
537 m->ih_src[i] = SERDEV_IHAND(child, isrc);
538 if (m->ih_src[i] != NULL)
546 scc_bus_teardown_intr(device_t dev, device_t child, struct resource *r,
552 if (device_get_parent(child) != dev)
555 m = device_get_ivars(child);
563 for (i = 0; i < SCC_ISRCCNT; i++)