2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2004-2006 Marcel Moolenaar
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/queue.h>
39 #include <sys/serial.h>
41 #include <machine/bus.h>
42 #include <machine/resource.h>
45 #include <dev/scc/scc_bfe.h>
46 #include <dev/scc/scc_bus.h>
50 devclass_t scc_devclass;
51 const char scc_driver_name[] = "scc";
53 static MALLOC_DEFINE(M_SCC, "SCC", "SCC driver");
56 scc_bfe_intr(void *arg)
58 struct scc_softc *sc = arg;
62 int c, i, ipend, isrc;
65 while (!sc->sc_leaving && (ipend = SCC_IPEND(sc)) != 0) {
66 i = 0, isrc = SER_INT_OVERRUN;
68 while (i < SCC_ISRCCNT && !(ipend & isrc))
70 KASSERT(i < SCC_ISRCCNT, ("%s", __func__));
72 for (c = 0; c < cl->cl_channels; c++) {
74 if (!(ch->ch_ipend & isrc))
77 if (m->ih_src[i] == NULL)
79 if ((*m->ih_src[i])(m->ih_arg))
80 ch->ch_ipend &= ~isrc;
83 for (c = 0; c < cl->cl_channels; c++) {
93 return (FILTER_HANDLED);
95 return (FILTER_STRAY);
99 scc_bfe_attach(device_t dev, u_int ipc)
101 struct resource_list_entry *rle;
103 struct scc_class *cl;
105 struct scc_softc *sc, *sc0;
107 bus_space_handle_t bh;
108 rman_res_t base, size, start, sz;
109 int c, error, mode, sysdev;
112 * The sc_class field defines the type of SCC we're going to work
113 * with and thus the size of the softc. Replace the generic softc
114 * with one that matches the SCC now that we're certain we handle
117 sc0 = device_get_softc(dev);
119 if (cl->size > sizeof(*sc)) {
120 sc = malloc(cl->size, M_SCC, M_WAITOK|M_ZERO);
121 bcopy(sc0, sc, sizeof(*sc));
122 device_set_softc(dev, sc);
126 size = abs(cl->cl_range) << sc->sc_bas.regshft;
128 mtx_init(&sc->sc_hwmtx, "scc_hwmtx", NULL, MTX_SPIN);
131 * Re-allocate. We expect that the softc contains the information
132 * collected by scc_bfe_probe() intact.
134 sc->sc_rres = bus_alloc_resource_anywhere(dev, sc->sc_rtype,
135 &sc->sc_rrid, cl->cl_channels * size, RF_ACTIVE);
136 if (sc->sc_rres == NULL)
138 sc->sc_bas.bsh = rman_get_bushandle(sc->sc_rres);
139 sc->sc_bas.bst = rman_get_bustag(sc->sc_rres);
142 * Allocate interrupt resources. There may be a different interrupt
143 * per channel. We allocate them all...
145 sc->sc_chan = malloc(sizeof(struct scc_chan) * cl->cl_channels,
146 M_SCC, M_WAITOK | M_ZERO);
147 for (c = 0; c < cl->cl_channels; c++) {
148 ch = &sc->sc_chan[c];
150 * XXX temporary hack. If we have more than 1 interrupt
151 * per channel, allocate the first for the channel. At
152 * this time only the macio bus front-end has more than
153 * 1 interrupt per channel and we don't use the 2nd and
154 * 3rd, because we don't support DMA yet.
156 ch->ch_irid = c * ipc;
157 ch->ch_ires = bus_alloc_resource_any(dev, SYS_RES_IRQ,
158 &ch->ch_irid, RF_ACTIVE | RF_SHAREABLE);
164 * Create the control structures for our children. Probe devices
165 * and query them to see if we can reset the hardware.
168 base = rman_get_start(sc->sc_rres);
169 sz = (size != 0) ? size : rman_get_size(sc->sc_rres);
170 start = base + ((cl->cl_range < 0) ? size * (cl->cl_channels - 1) : 0);
171 for (c = 0; c < cl->cl_channels; c++) {
172 ch = &sc->sc_chan[c];
173 resource_list_init(&ch->ch_rlist);
176 if (!SCC_ENABLED(sc, ch))
180 resource_list_add(&ch->ch_rlist, sc->sc_rtype, 0, start,
182 rle = resource_list_find(&ch->ch_rlist, sc->sc_rtype, 0);
183 rle->res = &ch->ch_rres;
184 bus_space_subregion(rman_get_bustag(sc->sc_rres),
185 rman_get_bushandle(sc->sc_rres), start - base, sz, &bh);
186 rman_set_bushandle(rle->res, bh);
187 rman_set_bustag(rle->res, rman_get_bustag(sc->sc_rres));
189 resource_list_add(&ch->ch_rlist, SYS_RES_IRQ, 0, c, c, 1);
190 rle = resource_list_find(&ch->ch_rlist, SYS_RES_IRQ, 0);
191 rle->res = (ch->ch_ires != NULL) ? ch->ch_ires :
192 sc->sc_chan[0].ch_ires;
194 for (mode = 0; mode < SCC_NMODES; mode++) {
195 m = &ch->ch_mode[mode];
197 m->m_mode = 1U << mode;
198 if ((cl->cl_modes & m->m_mode) == 0 || ch->ch_sysdev)
200 m->m_dev = device_add_child(dev, NULL, -1);
201 device_set_ivars(m->m_dev, (void *)m);
202 error = device_probe_child(dev, m->m_dev);
205 m->m_sysdev = SERDEV_SYSDEV(m->m_dev) ? 1 : 0;
206 ch->ch_sysdev |= m->m_sysdev;
211 start += (cl->cl_range < 0) ? -size : size;
212 sysdev |= ch->ch_sysdev;
216 * Have the hardware driver initialize the hardware. Tell it
217 * whether or not a hardware reset should be performed.
220 device_printf(dev, "%sresetting hardware\n",
221 (sysdev) ? "not " : "");
223 error = SCC_ATTACH(sc, !sysdev);
228 * Setup our interrupt handler. Make it FAST under the assumption
229 * that our children's are fast as well. We make it MPSAFE as soon
230 * as a child sets up a MPSAFE interrupt handler.
231 * Of course, if we can't setup a fast handler, we make it MPSAFE
234 for (c = 0; c < cl->cl_channels; c++) {
235 ch = &sc->sc_chan[c];
236 if (ch->ch_ires == NULL)
238 error = bus_setup_intr(dev, ch->ch_ires,
239 INTR_TYPE_TTY, scc_bfe_intr, NULL, sc,
242 error = bus_setup_intr(dev, ch->ch_ires,
243 INTR_TYPE_TTY | INTR_MPSAFE, NULL,
244 (driver_intr_t *)scc_bfe_intr, sc, &ch->ch_icookie);
249 device_printf(dev, "could not activate interrupt\n");
250 bus_release_resource(dev, SYS_RES_IRQ, ch->ch_irid,
256 for (c = 0; c < cl->cl_channels; c++) {
257 if (sc->sc_chan[0].ch_ires != NULL)
262 * Attach all child devices that were probed successfully.
264 for (c = 0; c < cl->cl_channels; c++) {
265 ch = &sc->sc_chan[c];
266 for (mode = 0; mode < SCC_NMODES; mode++) {
267 m = &ch->ch_mode[mode];
270 error = device_attach(m->m_dev);
277 if (bootverbose && (sc->sc_fastintr || sc->sc_polled)) {
279 device_print_prettyname(dev);
280 if (sc->sc_fastintr) {
281 printf("%sfast interrupt", sep);
285 printf("%spolled mode", sep);
294 for (c = 0; c < cl->cl_channels; c++) {
295 ch = &sc->sc_chan[c];
296 if (ch->ch_ires == NULL)
298 bus_release_resource(dev, SYS_RES_IRQ, ch->ch_irid,
301 bus_release_resource(dev, sc->sc_rtype, sc->sc_rrid, sc->sc_rres);
306 scc_bfe_detach(device_t dev)
309 struct scc_class *cl;
311 struct scc_softc *sc;
312 int chan, error, mode;
314 sc = device_get_softc(dev);
317 /* Detach our children. */
319 for (chan = 0; chan < cl->cl_channels; chan++) {
320 ch = &sc->sc_chan[chan];
321 for (mode = 0; mode < SCC_NMODES; mode++) {
322 m = &ch->ch_mode[mode];
325 if (device_detach(m->m_dev) != 0)
335 for (chan = 0; chan < cl->cl_channels; chan++) {
336 ch = &sc->sc_chan[chan];
337 if (ch->ch_ires == NULL)
339 bus_teardown_intr(dev, ch->ch_ires, ch->ch_icookie);
340 bus_release_resource(dev, SYS_RES_IRQ, ch->ch_irid,
343 bus_release_resource(dev, sc->sc_rtype, sc->sc_rrid, sc->sc_rres);
345 free(sc->sc_chan, M_SCC);
347 mtx_destroy(&sc->sc_hwmtx);
352 scc_bfe_probe(device_t dev, u_int regshft, u_int rclk, u_int rid)
354 struct scc_softc *sc;
355 struct scc_class *cl;
360 * Initialize the instance. Note that the instance (=softc) does
361 * not necessarily match the hardware specific softc. We can't do
362 * anything about it now, because we may not attach to the device.
363 * Hardware drivers cannot use any of the class specific fields
366 sc = device_get_softc(dev);
368 kobj_init((kobj_t)sc, (kobj_class_t)cl);
370 if (device_get_desc(dev) == NULL)
371 device_set_desc(dev, cl->name);
373 size = abs(cl->cl_range) << regshft;
376 * Allocate the register resource. We assume that all SCCs have a
377 * single register window in either I/O port space or memory mapped
378 * I/O space. Any SCC that needs multiple windows will consequently
379 * not be supported by this driver as-is.
382 sc->sc_rtype = SYS_RES_MEMORY;
383 sc->sc_rres = bus_alloc_resource_anywhere(dev, sc->sc_rtype,
384 &sc->sc_rrid, cl->cl_channels * size, RF_ACTIVE);
385 if (sc->sc_rres == NULL) {
387 sc->sc_rtype = SYS_RES_IOPORT;
388 sc->sc_rres = bus_alloc_resource_anywhere(dev, sc->sc_rtype,
389 &sc->sc_rrid, cl->cl_channels * size, RF_ACTIVE);
390 if (sc->sc_rres == NULL)
395 * Fill in the bus access structure and call the hardware specific
398 sz = (size != 0) ? size : rman_get_size(sc->sc_rres);
399 sc->sc_bas.bsh = rman_get_bushandle(sc->sc_rres);
400 sc->sc_bas.bst = rman_get_bustag(sc->sc_rres);
401 sc->sc_bas.range = sz;
402 sc->sc_bas.rclk = rclk;
403 sc->sc_bas.regshft = regshft;
405 error = SCC_PROBE(sc);
406 bus_release_resource(dev, sc->sc_rtype, sc->sc_rrid, sc->sc_rres);
407 return ((error == 0) ? BUS_PROBE_DEFAULT : error);
411 scc_bus_alloc_resource(device_t dev, device_t child, int type, int *rid,
412 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
414 struct resource_list_entry *rle;
418 if (device_get_parent(child) != dev)
421 /* We only support default allocations. */
422 if (!RMAN_IS_DEFAULT_RANGE(start, end))
425 m = device_get_ivars(child);
427 rle = resource_list_find(&ch->ch_rlist, type, 0);
435 scc_bus_get_resource(device_t dev, device_t child, int type, int rid,
436 rman_res_t *startp, rman_res_t *countp)
438 struct resource_list_entry *rle;
442 if (device_get_parent(child) != dev)
445 m = device_get_ivars(child);
447 rle = resource_list_find(&ch->ch_rlist, type, rid);
452 *startp = rle->start;
454 *countp = rle->count;
459 scc_bus_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
462 struct scc_class *cl;
464 struct scc_softc *sc;
466 if (device_get_parent(child) != dev)
469 sc = device_get_softc(dev);
471 m = device_get_ivars(child);
475 case SCC_IVAR_CHANNEL:
479 *result = cl->cl_class;
482 *result = sc->sc_bas.rclk;
487 case SCC_IVAR_REGSHFT:
488 *result = sc->sc_bas.regshft;
491 *result = (uintptr_t)&sc->sc_hwmtx;
500 scc_bus_release_resource(device_t dev, device_t child, int type, int rid,
501 struct resource *res)
503 struct resource_list_entry *rle;
507 if (device_get_parent(child) != dev)
510 m = device_get_ivars(child);
512 rle = resource_list_find(&ch->ch_rlist, type, rid);
513 return ((rle == NULL) ? EINVAL : 0);
517 scc_bus_setup_intr(device_t dev, device_t child, struct resource *r, int flags,
518 driver_filter_t *filt, void (*ihand)(void *), void *arg, void **cookiep)
522 struct scc_softc *sc;
525 if (device_get_parent(child) != dev)
528 /* Interrupt handlers must be FAST or MPSAFE. */
529 if (filt == NULL && !(flags & INTR_MPSAFE))
532 sc = device_get_softc(dev);
536 if (sc->sc_fastintr && filt == NULL) {
538 for (c = 0; c < sc->sc_class->cl_channels; c++) {
539 ch = &sc->sc_chan[c];
540 if (ch->ch_ires == NULL)
542 bus_teardown_intr(dev, ch->ch_ires, ch->ch_icookie);
543 bus_setup_intr(dev, ch->ch_ires,
544 INTR_TYPE_TTY | INTR_MPSAFE, NULL,
545 (driver_intr_t *)scc_bfe_intr, sc, &ch->ch_icookie);
549 m = device_get_ivars(child);
551 m->m_fastintr = (filt != NULL) ? 1 : 0;
552 m->ih = (filt != NULL) ? filt : (driver_filter_t *)ihand;
555 i = 0, isrc = SER_INT_OVERRUN;
556 while (i < SCC_ISRCCNT) {
557 m->ih_src[i] = SERDEV_IHAND(child, isrc);
558 if (m->ih_src[i] != NULL)
566 scc_bus_teardown_intr(device_t dev, device_t child, struct resource *r,
572 if (device_get_parent(child) != dev)
575 m = device_get_ivars(child);
583 for (i = 0; i < SCC_ISRCCNT; i++)