1 /*        $NetBSD: pic_discovery.c,v 1.8 2017/06/01 02:45:07 chs Exp $          */
2 
3 /*
4  * Copyright (c) 2002 Allegro Networks, Inc., Wasabi Systems, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *      This product includes software developed for the NetBSD Project by
18  *      Allegro Networks, Inc., and Wasabi Systems, Inc.
19  * 4. The name of Allegro Networks, Inc. may not be used to endorse
20  *    or promote products derived from this software without specific prior
21  *    written permission.
22  * 5. The name of Wasabi Systems, Inc. may not be used to endorse
23  *    or promote products derived from this software without specific prior
24  *    written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY ALLEGRO NETWORKS, INC. AND
27  * WASABI SYSTEMS, INC. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
28  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
29  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30  * IN NO EVENT SHALL EITHER ALLEGRO NETWORKS, INC. OR WASABI SYSTEMS, INC.
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 #include <sys/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: pic_discovery.c,v 1.8 2017/06/01 02:45:07 chs Exp $");
42 
43 #include <sys/param.h>
44 #include <sys/bus.h>
45 #include <sys/kmem.h>
46 #include <sys/intr.h>
47 
48 #include <powerpc/pic/picvar.h>
49 
50 #include <dev/marvell/gtvar.h>
51 #include <dev/marvell/gtintrreg.h>
52 #include <dev/marvell/gtintrvar.h>
53 
54 
55 __CTASSERT(sizeof(imask_t) == sizeof(uint64_t));
56 
57 struct discovery_gpp_pic_ops;
58 
59 struct discovery_pic_ops {
60           struct pic_ops pic;
61           union {
62                     uint64_t mask64;
63                     uint32_t mask32[2];
64           } _mask;
65 #define enable_mask           _mask.mask64
66 #define enable_mask_high      _mask.mask32[1]
67 #define enable_mask_low                 _mask.mask32[0]
68 };
69 struct discovery_gpp_pic_ops {
70           struct pic_ops pic;
71 
72           int gpp_base;
73 };
74 
75 
76 static void discovery_enable_irq(struct pic_ops *, int, int);
77 static void discovery_disable_irq(struct pic_ops *, int);
78 static int discovery_get_irq(struct pic_ops *, int);
79 static void discovery_ack_irq(struct pic_ops *, int);
80 
81 static void discovery_gpp_enable_irq(struct pic_ops *, int, int);
82 static void discovery_gpp_disable_irq(struct pic_ops *, int);
83 static int discovery_gpp_get_irq(struct pic_ops *, int);
84 static void discovery_gpp_ack_irq(struct pic_ops *, int);
85 
86 
87 struct pic_ops *
setup_discovery_pic(void)88 setup_discovery_pic(void)
89 {
90           struct discovery_pic_ops *discovery;
91           struct pic_ops *pic;
92 
93           discovery = kmem_alloc(sizeof(*discovery), KM_SLEEP);
94 
95           pic = &discovery->pic;
96           pic->pic_numintrs = 64;
97           pic->pic_cookie = (void *)NULL;                             /* set later */
98           pic->pic_enable_irq = discovery_enable_irq;
99           pic->pic_reenable_irq = discovery_enable_irq;
100           pic->pic_disable_irq = discovery_disable_irq;
101           pic->pic_get_irq = discovery_get_irq;
102           pic->pic_ack_irq = discovery_ack_irq;
103           pic->pic_establish_irq = dummy_pic_establish_intr;
104           pic->pic_finish_setup = NULL;
105           strcpy(pic->pic_name, "discovery");
106           pic_add(pic);
107 
108           discovery->enable_mask = 0;
109 
110           return pic;
111 }
112 
113 static void
discovery_enable_irq(struct pic_ops * pic,int irq,int type)114 discovery_enable_irq(struct pic_ops *pic, int irq, int type)
115 {
116           struct discovery_pic_ops *discovery = (struct discovery_pic_ops *)pic;
117 
118           discovery->enable_mask |= (1 << irq);
119           discovery_enable_intr(pic->pic_cookie, irq);
120 }
121 
122 static void
discovery_disable_irq(struct pic_ops * pic,int irq)123 discovery_disable_irq(struct pic_ops *pic, int irq)
124 {
125           struct discovery_pic_ops *discovery = (struct discovery_pic_ops *)pic;
126 
127           discovery->enable_mask &= ~(1 << irq);
128           discovery_disable_intr(pic->pic_cookie, irq);
129 }
130 
131 static int
discovery_get_irq(struct pic_ops * pic,int mode)132 discovery_get_irq(struct pic_ops *pic, int mode)
133 {
134           struct discovery_pic_ops *discovery = (struct discovery_pic_ops *)pic;
135           uint32_t cause;
136           int irq, base, msk;
137 
138           cause = discovery_mic_low(pic->pic_cookie) & discovery->enable_mask_low;
139           if (cause)
140                     base = 0;
141           else {
142                     cause = discovery_mic_high(pic->pic_cookie);
143                     cause &= discovery->enable_mask_high;
144                     if (!cause)
145                               return 255;
146                     base = 32;
147           }
148           for (irq = base, msk = 0x00000001; !(cause & msk); irq++, msk <<= 1);
149 
150           return irq;
151 }
152 
153 static void
discovery_ack_irq(struct pic_ops * pic,int irq)154 discovery_ack_irq(struct pic_ops *pic, int irq)
155 {
156 
157           /* nothing */
158 }
159 
160 
161 struct pic_ops *
setup_discovery_gpp_pic(void * discovery,int gpp_base)162 setup_discovery_gpp_pic(void *discovery, int gpp_base)
163 {
164           struct discovery_gpp_pic_ops *discovery_gpp;
165           struct pic_ops *pic;
166 
167           discovery_gpp = kmem_alloc(sizeof(*discovery_gpp), KM_SLEEP);
168 
169           pic = &discovery_gpp->pic;
170           pic->pic_numintrs = 32;
171           pic->pic_cookie = discovery;
172           pic->pic_enable_irq = discovery_gpp_enable_irq;
173           pic->pic_reenable_irq = discovery_gpp_enable_irq;
174           pic->pic_disable_irq = discovery_gpp_disable_irq;
175           pic->pic_get_irq = discovery_gpp_get_irq;
176           pic->pic_ack_irq = discovery_gpp_ack_irq;
177           pic->pic_establish_irq = dummy_pic_establish_intr;
178           pic->pic_finish_setup = NULL;
179           strcpy(pic->pic_name, "discovery_gpp");
180           pic_add(pic);
181 
182           discovery_gpp->gpp_base = gpp_base;
183 
184           return pic;
185 }
186 
187 static void
discovery_gpp_enable_irq(struct pic_ops * pic,int irq,int type)188 discovery_gpp_enable_irq(struct pic_ops *pic, int irq, int type)
189 {
190           struct discovery_gpp_pic_ops *discovery_gpp =
191               (struct discovery_gpp_pic_ops *)pic;
192           struct discovery_pic_ops *discovery = discovery_gpp->pic.pic_cookie;
193 
194           discovery_gpp_enable_intr(discovery->pic.pic_cookie,
195               discovery_gpp->gpp_base + irq);
196 }
197 
198 static void
discovery_gpp_disable_irq(struct pic_ops * pic,int irq)199 discovery_gpp_disable_irq(struct pic_ops *pic, int irq)
200 {
201           struct discovery_gpp_pic_ops *discovery_gpp =
202               (struct discovery_gpp_pic_ops *)pic;
203           struct discovery_pic_ops *discovery = discovery_gpp->pic.pic_cookie;
204 
205           discovery_gpp_disable_intr(discovery->pic.pic_cookie,
206               discovery_gpp->gpp_base + irq);
207 }
208 
209 static int
discovery_gpp_get_irq(struct pic_ops * pic,int mode)210 discovery_gpp_get_irq(struct pic_ops *pic, int mode)
211 {
212           struct discovery_gpp_pic_ops *discovery_gpp =
213               (struct discovery_gpp_pic_ops *)pic;
214           struct discovery_pic_ops *discovery = discovery_gpp->pic.pic_cookie;
215           uint32_t cause, mask;
216           int irq, msk, base;
217 
218           cause = discovery_gpp_cause(discovery->pic.pic_cookie);
219           mask = discovery_gpp_mask(discovery->pic.pic_cookie);
220 
221           base = discovery_gpp->gpp_base;
222           cause &= (mask & (0xff << base));
223           if (!cause)
224                     return 255;
225 
226           for (irq = base, msk = (1 << base); !(cause & msk); irq++, msk <<= 1);
227 
228           return irq;
229 }
230 
231 static void
discovery_gpp_ack_irq(struct pic_ops * pic,int irq)232 discovery_gpp_ack_irq(struct pic_ops *pic, int irq)
233 {
234           struct discovery_gpp_pic_ops *discovery_gpp =
235               (struct discovery_gpp_pic_ops *)pic;
236           struct discovery_pic_ops *discovery = discovery_gpp->pic.pic_cookie;
237 
238           discovery_gpp_clear_cause(discovery->pic.pic_cookie,
239               discovery_gpp->gpp_base + irq);
240 }
241