1 /* $NetBSD: hd6446xintc.c,v 1.8 2008/04/28 20:23:22 martin Exp $ */
2
3 /*-
4 * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by UCHIYAMA Yasushi.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: hd6446xintc.c,v 1.8 2008/04/28 20:23:22 martin Exp $");
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37
38 #include <sh3/devreg.h>
39 #include <hpcsh/dev/hd6446x/hd6446xintcreg.h>
40 #include <hpcsh/dev/hd6446x/hd6446xintcvar.h>
41
42 struct hd6446x_intrhand hd6446x_intrhand[_HD6446X_INTR_N];
43 uint16_t hd6446x_imask[_IPL_N];
44 uint16_t hd6446x_ienable;
45
46 static void hd6446x_intr_priority_update(void);
47
48
49 void
hd6446x_intr_init(void)50 hd6446x_intr_init(void)
51 {
52
53 /* Initialize interrupt priority masks. */
54 hd6446x_intr_priority_update();
55 }
56
57 void *
hd6446x_intr_establish(int irq,int mode,int level,int (* func)(void *),void * arg)58 hd6446x_intr_establish(int irq, int mode, int level,
59 int (*func)(void *), void *arg)
60 {
61 struct hd6446x_intrhand *hh = &hd6446x_intrhand[ffs(irq) - 1];
62 int s;
63
64 s = splhigh();
65
66 /* Register interrupt handler */
67 hh->hh_func = func;
68 hh->hh_arg = arg;
69 hh->hh_ipl = level << 4;
70 hh->hh_imask = irq;
71 hd6446x_ienable |= hh->hh_imask;
72
73 /* Update interrupt priority masks. */
74 hd6446x_intr_priority_update();
75
76 splx(s);
77
78 return (hh);
79 }
80
81 void
hd6446x_intr_disestablish(void * handle)82 hd6446x_intr_disestablish(void *handle)
83 {
84 struct hd6446x_intrhand *hh = handle;
85 int s;
86
87 s = splhigh();
88
89 /* Update interrupt priority masks */
90 hd6446x_ienable &= ~hh->hh_imask;
91 memset(hh, 0, sizeof(*hh));
92 hd6446x_intr_priority_update();
93
94 splx(s);
95 }
96
97 void
hd6446x_intr_priority(int irq,int level)98 hd6446x_intr_priority(int irq, int level)
99 {
100 struct hd6446x_intrhand *hh = &hd6446x_intrhand[ffs(irq) - 1];
101 int s;
102
103 KDASSERT(hh->hh_func != NULL);
104 s = splhigh();
105 hh->hh_ipl = level << 4;
106 hd6446x_intr_priority_update();
107 splx(s);
108 }
109
110 static void
hd6446x_intr_priority_update(void)111 hd6446x_intr_priority_update(void)
112 {
113 int ipl, src;
114
115 for (ipl = 0; ipl < _IPL_N; ipl++) {
116 uint16_t mask = ~hd6446x_ienable; /* mask disabled */
117
118 /* mask sources interrupting at <= ipl */
119 for (src = 0; src < _HD6446X_INTR_N; ++src) {
120 struct hd6446x_intrhand *hh = &hd6446x_intrhand[src];
121
122 if (hh->hh_func != NULL && hh->hh_ipl <= (ipl << 4))
123 mask |= 1 << src;
124 }
125
126 hd6446x_imask[ipl] = mask;
127 }
128 }
129