]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/scc/scc_core.c
This commit was generated by cvs2svn to compensate for changes in r161863,
[FreeBSD/FreeBSD.git] / sys / dev / scc / scc_core.c
1 /*-
2  * Copyright (c) 2004-2006 Marcel Moolenaar
3  * All rights reserved.
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:
8  *
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.
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>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/bus.h>
33 #include <sys/conf.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/queue.h>
37 #include <sys/serial.h>
38
39 #include <machine/bus.h>
40 #include <machine/resource.h>
41 #include <sys/rman.h>
42
43 #include <dev/scc/scc_bfe.h>
44 #include <dev/scc/scc_bus.h>
45
46 #include "scc_if.h"
47
48 devclass_t scc_devclass;
49 char scc_driver_name[] = "scc";
50
51 MALLOC_DEFINE(M_SCC, "SCC", "SCC driver");
52
53 static void
54 scc_bfe_intr(void *arg)
55 {
56         struct scc_softc *sc = arg;
57         struct scc_chan *ch;
58         struct scc_class *cl;
59         struct scc_mode *m;
60         int c, i, ipend, isrc;
61
62         cl = sc->sc_class;
63         while (!sc->sc_leaving && (ipend = SCC_IPEND(sc)) != 0) {
64                 i = 0, isrc = SER_INT_OVERRUN;
65                 while (ipend) {
66                         while (i < SCC_ISRCCNT && !(ipend & isrc))
67                                 i++, isrc <<= 1;
68                         KASSERT(i < SCC_ISRCCNT, ("%s", __func__));
69                         ipend &= ~isrc;
70                         for (c = 0; c < cl->cl_channels; c++) {
71                                 ch = &sc->sc_chan[c];
72                                 if (!(ch->ch_ipend & isrc))
73                                         continue;
74                                 m = &ch->ch_mode[0];
75                                 if (m->ih_src[i] == NULL)
76                                         continue;
77                                 if ((*m->ih_src[i])(m->ih_arg))
78                                         ch->ch_ipend &= ~isrc;
79                         }
80                 }
81                 for (c = 0; c < cl->cl_channels; c++) {
82                         ch = &sc->sc_chan[c];
83                         if (!ch->ch_ipend)
84                                 continue;
85                         m = &ch->ch_mode[0];
86                         if (m->ih != NULL)
87                                 (*m->ih)(m->ih_arg);
88                         else
89                                 SCC_ICLEAR(sc, ch);
90                 }
91         }
92 }
93
94 int
95 scc_bfe_attach(device_t dev)
96 {
97         struct resource_list_entry *rle;
98         struct scc_chan *ch;
99         struct scc_class *cl;
100         struct scc_mode *m;
101         struct scc_softc *sc, *sc0;
102         const char *sep;
103         bus_space_handle_t bh;
104         u_long base, size, start;
105         int c, error, mode, sysdev;
106
107         /*
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
111          * the device.
112          */
113         sc0 = device_get_softc(dev);
114         cl = sc0->sc_class;
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);
119         } else
120                 sc = sc0;
121
122         size = abs(cl->cl_range) << sc->sc_bas.regshft;
123
124         mtx_init(&sc->sc_hwmtx, "scc_hwmtx", NULL, MTX_SPIN);
125
126         /*
127          * Re-allocate. We expect that the softc contains the information
128          * collected by scc_bfe_probe() intact.
129          */
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)
133                 return (ENXIO);
134         sc->sc_bas.bsh = rman_get_bushandle(sc->sc_rres);
135         sc->sc_bas.bst = rman_get_bustag(sc->sc_rres);
136
137         /*
138          * Allocate interrupt resources. There may be a different interrupt
139          * per channel. We allocate them all...
140          */
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];
145                 ch->ch_irid = c;
146                 ch->ch_ires = bus_alloc_resource_any(dev, SYS_RES_IRQ,
147                     &ch->ch_irid, RF_ACTIVE | RF_SHAREABLE);
148         }
149
150         /*
151          * Create the control structures for our children. Probe devices
152          * and query them to see if we can reset the hardware.
153          */
154         sysdev = 0;
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);
160                 ch->ch_nr = c + 1;
161
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));
170
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;
175
176                 for (mode = 0; mode < SCC_NMODES; mode++) {
177                         m = &ch->ch_mode[mode];
178                         m->m_chan = ch;
179                         m->m_mode = 1U << mode;
180                         if ((cl->cl_modes & m->m_mode) == 0 || ch->ch_sysdev)
181                                 continue;
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);
185                         if (!error) {
186                                 m->m_probed = 1;
187                                 m->m_sysdev = SERDEV_SYSDEV(m->m_dev) ? 1 : 0;
188                                 ch->ch_sysdev |= m->m_sysdev;
189                         }
190                 }
191
192                 start += (cl->cl_range < 0) ? -size : size;
193                 sysdev |= ch->ch_sysdev;
194         }
195
196         /*
197          * Have the hardware driver initialize the hardware. Tell it
198          * whether or not a hardware reset should be performed.
199          */
200         if (bootverbose) {
201                 device_printf(dev, "%sresetting hardware\n",
202                     (sysdev) ? "not " : "");
203         }
204         error = SCC_ATTACH(sc, !sysdev);
205         if (error)
206                 goto fail;
207
208         /*
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
213          * right away.
214          */
215         for (c = 0; c < cl->cl_channels; c++) {
216                 ch = &sc->sc_chan[c];
217                 if (ch->ch_ires == NULL)
218                         continue;
219                 error = bus_setup_intr(dev, ch->ch_ires,
220                     INTR_TYPE_TTY | INTR_FAST, scc_bfe_intr, sc,
221                     &ch->ch_icookie);
222                 if (error) {
223                         error = bus_setup_intr(dev, ch->ch_ires,
224                             INTR_TYPE_TTY | INTR_MPSAFE, scc_bfe_intr, sc,
225                             &ch->ch_icookie);
226                 } else
227                         sc->sc_fastintr = 1;
228
229                 if (error) {
230                         device_printf(dev, "could not activate interrupt\n");
231                         bus_release_resource(dev, SYS_RES_IRQ, ch->ch_irid,
232                             ch->ch_ires);
233                         ch->ch_ires = NULL;
234                 }
235         }
236         sc->sc_polled = 1;
237         for (c = 0; c < cl->cl_channels; c++) {
238                 if (sc->sc_chan[0].ch_ires != NULL)
239                         sc->sc_polled = 0;
240         }
241
242         /*
243          * Attach all child devices that were probed successfully.
244          */
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];
249                         if (!m->m_probed)
250                                 continue;
251                         error = device_attach(m->m_dev);
252                         if (error)
253                                 continue;
254                         m->m_attached = 1;
255                 }
256         }
257
258         if (bootverbose && (sc->sc_fastintr || sc->sc_polled)) {
259                 sep = "";
260                 device_print_prettyname(dev);
261                 if (sc->sc_fastintr) {
262                         printf("%sfast interrupt", sep);
263                         sep = ", ";
264                 }
265                 if (sc->sc_polled) {
266                         printf("%spolled mode", sep);
267                         sep = ", ";
268                 }
269                 printf("\n");
270         }
271
272         return (0);
273
274  fail:
275         for (c = 0; c < cl->cl_channels; c++) {
276                 ch = &sc->sc_chan[c];
277                 if (ch->ch_ires == NULL)
278                         continue;
279                 bus_release_resource(dev, SYS_RES_IRQ, ch->ch_irid,
280                     ch->ch_ires);
281         }
282         bus_release_resource(dev, sc->sc_rtype, sc->sc_rrid, sc->sc_rres);
283         return (error);
284 }
285
286 int
287 scc_bfe_detach(device_t dev)
288 {
289         struct scc_chan *ch;
290         struct scc_class *cl;
291         struct scc_mode *m;
292         struct scc_softc *sc;
293         int chan, error, mode;
294
295         sc = device_get_softc(dev);
296         cl = sc->sc_class;
297
298         /* Detach our children. */
299         error = 0;
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];
304                         if (!m->m_attached)
305                                 continue;
306                         if (device_detach(m->m_dev) != 0)
307                                 error = ENXIO;
308                         else
309                                 m->m_attached = 0;
310                 }
311         }
312
313         if (error)
314                 return (error);
315
316         for (chan = 0; chan < cl->cl_channels; chan++) {
317                 ch = &sc->sc_chan[chan];
318                 if (ch->ch_ires == NULL)
319                         continue;
320                 bus_teardown_intr(dev, ch->ch_ires, ch->ch_icookie);
321                 bus_release_resource(dev, SYS_RES_IRQ, ch->ch_irid,
322                     ch->ch_ires);
323         }
324         bus_release_resource(dev, sc->sc_rtype, sc->sc_rrid, sc->sc_rres);
325
326         free(sc->sc_chan, M_SCC);
327
328         mtx_destroy(&sc->sc_hwmtx);
329         return (0);
330 }
331
332 int
333 scc_bfe_probe(device_t dev, u_int regshft, u_int rclk)
334 {
335         struct scc_softc *sc;
336         struct scc_class *cl;
337         u_long size;
338         int error;
339
340         /*
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
345          * while probing.
346          */
347         sc = device_get_softc(dev);
348         cl = sc->sc_class;
349         kobj_init((kobj_t)sc, (kobj_class_t)cl);
350         sc->sc_dev = dev;
351         if (device_get_desc(dev) == NULL)
352                 device_set_desc(dev, cl->name);
353
354         size = abs(cl->cl_range) << regshft;
355
356         /*
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.
361          */
362         sc->sc_rrid = 0;
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) {
367                 sc->sc_rrid = 0;
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)
372                         return (ENXIO);
373         }
374
375         /*
376          * Fill in the bus access structure and call the hardware specific
377          * probe method.
378          */
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;
384
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);
388 }
389
390 struct resource *
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)
393 {
394         struct resource_list_entry *rle;
395         struct scc_chan *ch;
396         struct scc_mode *m;
397
398         if (device_get_parent(child) != dev)
399                 return (NULL);
400
401         /* We only support default allocations. */
402         if (start != 0UL || end != ~0UL)
403                 return (NULL);
404
405         m = device_get_ivars(child);
406         ch = m->m_chan;
407         rle = resource_list_find(&ch->ch_rlist, type, 0);
408         if (rle == NULL)
409                 return (NULL);
410         *rid = 0;
411         return (rle->res);
412 }
413
414 int
415 scc_bus_get_resource(device_t dev, device_t child, int type, int rid,
416     u_long *startp, u_long *countp)
417 {
418         struct resource_list_entry *rle;
419         struct scc_chan *ch;
420         struct scc_mode *m;
421
422         if (device_get_parent(child) != dev)
423                 return (EINVAL);
424
425         m = device_get_ivars(child);
426         ch = m->m_chan;
427         rle = resource_list_find(&ch->ch_rlist, type, rid);
428         if (rle == NULL)
429                 return (EINVAL);
430
431         if (startp != NULL)
432                 *startp = rle->start;
433         if (countp != NULL)
434                 *countp = rle->count;
435         return (0);
436 }
437
438 int
439 scc_bus_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
440 {
441         struct scc_chan *ch;
442         struct scc_class *cl;
443         struct scc_mode *m;
444         struct scc_softc *sc;
445
446         if (device_get_parent(child) != dev)
447                 return (EINVAL);
448
449         sc = device_get_softc(dev);
450         cl = sc->sc_class;
451         m = device_get_ivars(child);
452         ch = m->m_chan;
453
454         switch (index) {
455         case SCC_IVAR_CHANNEL:
456                 *result = ch->ch_nr;
457                 break;
458         case SCC_IVAR_CLASS:
459                 *result = cl->cl_class;
460                 break;
461         case SCC_IVAR_CLOCK:
462                 *result = sc->sc_bas.rclk;
463                 break;
464         case SCC_IVAR_MODE:
465                 *result = m->m_mode;
466                 break;
467         case SCC_IVAR_REGSHFT:
468                 *result = sc->sc_bas.regshft;
469                 break;
470         case SCC_IVAR_HWMTX:
471                 *result = (uintptr_t)&sc->sc_hwmtx;
472                 break;
473         default:
474                 return (EINVAL);
475         }
476         return (0);
477 }
478
479 int
480 scc_bus_release_resource(device_t dev, device_t child, int type, int rid,
481     struct resource *res)
482 {
483         struct resource_list_entry *rle;
484         struct scc_chan *ch;
485         struct scc_mode *m;
486
487         if (device_get_parent(child) != dev)
488                 return (EINVAL);
489
490         m = device_get_ivars(child);
491         ch = m->m_chan;
492         rle = resource_list_find(&ch->ch_rlist, type, rid);
493         return ((rle == NULL) ? EINVAL : 0);
494 }
495
496 int
497 scc_bus_setup_intr(device_t dev, device_t child, struct resource *r, int flags,
498     void (*ihand)(void *), void *arg, void **cookiep)
499 {
500         struct scc_chan *ch;
501         struct scc_mode *m;
502         struct scc_softc *sc;
503         int c, i, isrc;
504
505         if (device_get_parent(child) != dev)
506                 return (EINVAL);
507
508         /* Interrupt handlers must be FAST or MPSAFE. */
509         if ((flags & (INTR_FAST|INTR_MPSAFE)) == 0)
510                 return (EINVAL);
511
512         sc = device_get_softc(dev);
513         if (sc->sc_polled)
514                 return (ENXIO);
515
516         if (sc->sc_fastintr && !(flags & INTR_FAST)) {
517                 sc->sc_fastintr = 0;
518                 for (c = 0; c < sc->sc_class->cl_channels; c++) {
519                         ch = &sc->sc_chan[c];
520                         if (ch->ch_ires == NULL)
521                                 continue;
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,
525                             &ch->ch_icookie);
526                 }
527         }
528
529         m = device_get_ivars(child);
530         m->m_hasintr = 1;
531         m->m_fastintr = (flags & INTR_FAST) ? 1 : 0;
532         m->ih = ihand;
533         m->ih_arg = arg;
534
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)
539                         m->ih = NULL;
540                 i++, isrc <<= 1;
541         }
542         return (0);
543 }
544
545 int
546 scc_bus_teardown_intr(device_t dev, device_t child, struct resource *r,
547     void *cookie)
548 {
549         struct scc_mode *m;
550         int i;
551
552         if (device_get_parent(child) != dev)
553                 return (EINVAL);
554
555         m = device_get_ivars(child);
556         if (!m->m_hasintr)
557                 return (EINVAL);
558
559         m->m_hasintr = 0;
560         m->m_fastintr = 0;
561         m->ih = NULL;
562         m->ih_arg = NULL;
563         for (i = 0; i < SCC_ISRCCNT; i++)
564                 m->ih_src[i] = NULL;
565         return (0);
566 }