ali1543.c revision 1.1.6.2 1 1.1.6.2 thorpej /* $NetBSD: ali1543.c,v 1.1.6.2 2001/09/13 01:13:50 thorpej Exp $ */
2 1.1.6.2 thorpej
3 1.1.6.2 thorpej /*
4 1.1.6.2 thorpej * Copyright (c) 2001
5 1.1.6.2 thorpej * HAYAKAWA Koichi. All rights reserved.
6 1.1.6.2 thorpej *
7 1.1.6.2 thorpej * Redistribution and use in source and binary forms, with or without
8 1.1.6.2 thorpej * modification, are permitted provided that the following conditions
9 1.1.6.2 thorpej * are met:
10 1.1.6.2 thorpej * 1. Redistributions of source code must retain the above copyright
11 1.1.6.2 thorpej * notice, this list of conditions and the following disclaimer.
12 1.1.6.2 thorpej * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.6.2 thorpej * notice, this list of conditions and the following disclaimer in the
14 1.1.6.2 thorpej * documentation and/or other materials provided with the distribution.
15 1.1.6.2 thorpej * 3. The name of the author may not be used to endorse or promote products
16 1.1.6.2 thorpej * derived from this software without specific prior written permission.
17 1.1.6.2 thorpej *
18 1.1.6.2 thorpej *
19 1.1.6.2 thorpej * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 1.1.6.2 thorpej * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 1.1.6.2 thorpej * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 1.1.6.2 thorpej * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
23 1.1.6.2 thorpej * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 1.1.6.2 thorpej * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 1.1.6.2 thorpej * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1.6.2 thorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 1.1.6.2 thorpej * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28 1.1.6.2 thorpej * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1.6.2 thorpej * POSSIBILITY OF SUCH DAMAGE.
30 1.1.6.2 thorpej */
31 1.1.6.2 thorpej
32 1.1.6.2 thorpej /*-
33 1.1.6.2 thorpej * Copyright (c) 1999 The NetBSD Foundation, Inc.
34 1.1.6.2 thorpej * All rights reserved.
35 1.1.6.2 thorpej *
36 1.1.6.2 thorpej * This code is derived from software contributed to The NetBSD Foundation
37 1.1.6.2 thorpej * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
38 1.1.6.2 thorpej * NASA Ames Research Center.
39 1.1.6.2 thorpej *
40 1.1.6.2 thorpej * Redistribution and use in source and binary forms, with or without
41 1.1.6.2 thorpej * modification, are permitted provided that the following conditions
42 1.1.6.2 thorpej * are met:
43 1.1.6.2 thorpej * 1. Redistributions of source code must retain the above copyright
44 1.1.6.2 thorpej * notice, this list of conditions and the following disclaimer.
45 1.1.6.2 thorpej * 2. Redistributions in binary form must reproduce the above copyright
46 1.1.6.2 thorpej * notice, this list of conditions and the following disclaimer in the
47 1.1.6.2 thorpej * documentation and/or other materials provided with the distribution.
48 1.1.6.2 thorpej * 3. All advertising materials mentioning features or use of this software
49 1.1.6.2 thorpej * must display the following acknowledgement:
50 1.1.6.2 thorpej * This product includes software developed by the NetBSD
51 1.1.6.2 thorpej * Foundation, Inc. and its contributors.
52 1.1.6.2 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
53 1.1.6.2 thorpej * contributors may be used to endorse or promote products derived
54 1.1.6.2 thorpej * from this software without specific prior written permission.
55 1.1.6.2 thorpej *
56 1.1.6.2 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
57 1.1.6.2 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
58 1.1.6.2 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
59 1.1.6.2 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
60 1.1.6.2 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
61 1.1.6.2 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
62 1.1.6.2 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
63 1.1.6.2 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
64 1.1.6.2 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
65 1.1.6.2 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
66 1.1.6.2 thorpej * POSSIBILITY OF SUCH DAMAGE.
67 1.1.6.2 thorpej */
68 1.1.6.2 thorpej
69 1.1.6.2 thorpej /*
70 1.1.6.2 thorpej * Copyright (c) 1999, by UCHIYAMA Yasushi
71 1.1.6.2 thorpej * All rights reserved.
72 1.1.6.2 thorpej *
73 1.1.6.2 thorpej * Redistribution and use in source and binary forms, with or without
74 1.1.6.2 thorpej * modification, are permitted provided that the following conditions
75 1.1.6.2 thorpej * are met:
76 1.1.6.2 thorpej * 1. Redistributions of source code must retain the above copyright
77 1.1.6.2 thorpej * notice, this list of conditions and the following disclaimer.
78 1.1.6.2 thorpej * 2. The name of the developer may NOT be used to endorse or promote products
79 1.1.6.2 thorpej * derived from this software without specific prior written permission.
80 1.1.6.2 thorpej *
81 1.1.6.2 thorpej * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
82 1.1.6.2 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
83 1.1.6.2 thorpej * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
84 1.1.6.2 thorpej * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
85 1.1.6.2 thorpej * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
86 1.1.6.2 thorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
87 1.1.6.2 thorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
88 1.1.6.2 thorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
89 1.1.6.2 thorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
90 1.1.6.2 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
91 1.1.6.2 thorpej * SUCH DAMAGE.
92 1.1.6.2 thorpej */
93 1.1.6.2 thorpej
94 1.1.6.2 thorpej /* HAYAKAWA Koichi wrote ALi 1543 PCI ICU code basing on VIA82C586 driver */
95 1.1.6.2 thorpej
96 1.1.6.2 thorpej #include <sys/param.h>
97 1.1.6.2 thorpej #include <sys/systm.h>
98 1.1.6.2 thorpej #include <sys/errno.h>
99 1.1.6.2 thorpej #include <sys/device.h>
100 1.1.6.2 thorpej #include <sys/malloc.h>
101 1.1.6.2 thorpej #include <sys/proc.h>
102 1.1.6.2 thorpej
103 1.1.6.2 thorpej #include <machine/intr.h>
104 1.1.6.2 thorpej #include <machine/bus.h>
105 1.1.6.2 thorpej
106 1.1.6.2 thorpej #include <dev/pci/pcivar.h>
107 1.1.6.2 thorpej #include <dev/pci/pcireg.h>
108 1.1.6.2 thorpej #include <dev/pci/pcidevs.h>
109 1.1.6.2 thorpej
110 1.1.6.2 thorpej #include <i386/pci/pci_intr_fixup.h>
111 1.1.6.2 thorpej #include <i386/pci/piixvar.h>
112 1.1.6.2 thorpej
113 1.1.6.2 thorpej
114 1.1.6.2 thorpej int ali1543_getclink (pciintr_icu_handle_t, int, int *);
115 1.1.6.2 thorpej int ali1543_get_intr (pciintr_icu_handle_t, int, int *);
116 1.1.6.2 thorpej int ali1543_set_intr (pciintr_icu_handle_t, int, int);
117 1.1.6.2 thorpej int ali1543_get_trigger (pciintr_icu_handle_t, int, int *);
118 1.1.6.2 thorpej int ali1543_set_trigger (pciintr_icu_handle_t, int, int);
119 1.1.6.2 thorpej
120 1.1.6.2 thorpej
121 1.1.6.2 thorpej const struct pciintr_icu ali1543_icu = {
122 1.1.6.2 thorpej ali1543_getclink,
123 1.1.6.2 thorpej ali1543_get_intr,
124 1.1.6.2 thorpej ali1543_set_intr,
125 1.1.6.2 thorpej ali1543_get_trigger,
126 1.1.6.2 thorpej ali1543_set_trigger,
127 1.1.6.2 thorpej };
128 1.1.6.2 thorpej
129 1.1.6.2 thorpej
130 1.1.6.2 thorpej /*
131 1.1.6.2 thorpej * Linux source code (linux/arch/i386/kernel/pci-irq.c) says that the
132 1.1.6.2 thorpej * irq order of ALi PCI ICU is shuffled.
133 1.1.6.2 thorpej */
134 1.1.6.2 thorpej const static int ali1543_intr_shuffle_get[16] = {
135 1.1.6.2 thorpej 0, 9, 3, 10, 4, 5, 7, 6, 1, 11, 0, 12, 0, 14, 0, 15
136 1.1.6.2 thorpej };
137 1.1.6.2 thorpej const static int ali1543_intr_shuffle_set[16] = {
138 1.1.6.2 thorpej 0, 8, 0, 2, 4, 5, 7, 6, 0, 1, 3, 9, 11, 0, 13, 15
139 1.1.6.2 thorpej };
140 1.1.6.2 thorpej
141 1.1.6.2 thorpej #define ALI1543_IRQ_MASK 0xdefa
142 1.1.6.2 thorpej
143 1.1.6.2 thorpej #define ALI1543_LEGAL_LINK(link) (((link) >= 0) && ((link) <= 7))
144 1.1.6.2 thorpej #define ALI1543_LEGAL_IRQ(irq) ((1 << (irq)) & ALI1543_IRQ_MASK)
145 1.1.6.2 thorpej
146 1.1.6.2 thorpej #define ALI1543_INTR_CFG_REG 0x48
147 1.1.6.2 thorpej #define ALI1543_INTR_TRIG_REG 0x4c
148 1.1.6.2 thorpej
149 1.1.6.2 thorpej #define ALI1543_INTR_PIRQA_SHIFT 0
150 1.1.6.2 thorpej #define ALI1543_INTR_PIRQA_MASK 0x0000000f
151 1.1.6.2 thorpej #define ALI1543_INTR_PIRQB_SHIFT 4
152 1.1.6.2 thorpej #define ALI1543_INTR_PIRQB_MASK 0x000000f0
153 1.1.6.2 thorpej #define ALI1543_INTR_PIRQC_SHIFT 8
154 1.1.6.2 thorpej #define ALI1543_INTR_PIRQC_MASK 0x00000f00
155 1.1.6.2 thorpej #define ALI1543_INTR_PIRQD_SHIFT 12
156 1.1.6.2 thorpej #define ALI1543_INTR_PIRQD_MASK 0x0000f000
157 1.1.6.2 thorpej
158 1.1.6.2 thorpej #define ALI1543_INTR_PIRQ_SHIFT(clink) ((clink)*4)
159 1.1.6.2 thorpej #define ALI1543_INTR_PIRQ_IRQ(reg, clink) \
160 1.1.6.2 thorpej (((reg) >> ((clink)*4)) & 0x0f)
161 1.1.6.2 thorpej #define ALI1543_PIRQ(reg, clink) \
162 1.1.6.2 thorpej ali1543_intr_shuffle_get[ALI1543_INTR_PIRQ_IRQ((reg), (clink))]
163 1.1.6.2 thorpej
164 1.1.6.2 thorpej #define ALI1543_INTR_TRIG_MASK 0x000000ff
165 1.1.6.2 thorpej #define ALI1543_INTR_TRIG_LEVEL 0
166 1.1.6.2 thorpej #define ALI1543_INTR_TRIG_EDGE 1
167 1.1.6.2 thorpej #define ALI1543_INTR_TRIG_GET(reg, link) \
168 1.1.6.2 thorpej (((reg) >> (link)) & 0x01)
169 1.1.6.2 thorpej #define ALI1543_INTR_TRIG_SET(reg, link, val) \
170 1.1.6.2 thorpej do { \
171 1.1.6.2 thorpej (reg) &= ~(1 << (link)); \
172 1.1.6.2 thorpej (reg) |= (((val) & 0x01) << (link)); \
173 1.1.6.2 thorpej } while (/* CONSTCOND */0)
174 1.1.6.2 thorpej
175 1.1.6.2 thorpej
176 1.1.6.2 thorpej
177 1.1.6.2 thorpej
178 1.1.6.2 thorpej int
179 1.1.6.2 thorpej ali1543_init(pci_chipset_tag_t pc, bus_space_tag_t iot, pcitag_t tag,
180 1.1.6.2 thorpej pciintr_icu_tag_t *ptagp, pciintr_icu_handle_t *phandp)
181 1.1.6.2 thorpej {
182 1.1.6.2 thorpej
183 1.1.6.2 thorpej if (piix_init(pc, iot, tag, ptagp, phandp) == 0) {
184 1.1.6.2 thorpej *ptagp = &ali1543_icu;
185 1.1.6.2 thorpej
186 1.1.6.2 thorpej return (0);
187 1.1.6.2 thorpej }
188 1.1.6.2 thorpej
189 1.1.6.2 thorpej return (1);
190 1.1.6.2 thorpej }
191 1.1.6.2 thorpej
192 1.1.6.2 thorpej
193 1.1.6.2 thorpej
194 1.1.6.2 thorpej
195 1.1.6.2 thorpej int
196 1.1.6.2 thorpej ali1543_getclink(pciintr_icu_handle_t v, int link, int *clinkp)
197 1.1.6.2 thorpej {
198 1.1.6.2 thorpej
199 1.1.6.2 thorpej if (ALI1543_LEGAL_LINK(link - 1)) {
200 1.1.6.2 thorpej *clinkp = link - 1;
201 1.1.6.2 thorpej return (0);
202 1.1.6.2 thorpej }
203 1.1.6.2 thorpej
204 1.1.6.2 thorpej return (1);
205 1.1.6.2 thorpej }
206 1.1.6.2 thorpej
207 1.1.6.2 thorpej
208 1.1.6.2 thorpej
209 1.1.6.2 thorpej
210 1.1.6.2 thorpej int
211 1.1.6.2 thorpej ali1543_get_intr(pciintr_icu_handle_t v, int clink, int *irqp)
212 1.1.6.2 thorpej {
213 1.1.6.2 thorpej struct piix_handle *ph = v;
214 1.1.6.2 thorpej pcireg_t reg;
215 1.1.6.2 thorpej int val;
216 1.1.6.2 thorpej
217 1.1.6.2 thorpej if (ALI1543_LEGAL_LINK(clink) == 0)
218 1.1.6.2 thorpej return (1);
219 1.1.6.2 thorpej
220 1.1.6.2 thorpej reg = pci_conf_read(ph->ph_pc, ph->ph_tag, ALI1543_INTR_CFG_REG);
221 1.1.6.2 thorpej #ifdef DEBUG_1543
222 1.1.6.2 thorpej printf("ali1543: PIRQ reg 0x%08x\n", reg); /* XXX debug */
223 1.1.6.2 thorpej #endif /* DEBUG_1543 */
224 1.1.6.2 thorpej val = ALI1543_PIRQ(reg, clink);
225 1.1.6.2 thorpej *irqp = (val == 0) ?
226 1.1.6.2 thorpej I386_PCI_INTERRUPT_LINE_NO_CONNECTION : val;
227 1.1.6.2 thorpej
228 1.1.6.2 thorpej return (0);
229 1.1.6.2 thorpej }
230 1.1.6.2 thorpej
231 1.1.6.2 thorpej
232 1.1.6.2 thorpej
233 1.1.6.2 thorpej
234 1.1.6.2 thorpej int
235 1.1.6.2 thorpej ali1543_set_intr(pciintr_icu_handle_t v, int clink, int irq)
236 1.1.6.2 thorpej {
237 1.1.6.2 thorpej struct piix_handle *ph = v;
238 1.1.6.2 thorpej int shift, val;
239 1.1.6.2 thorpej pcireg_t reg;
240 1.1.6.2 thorpej
241 1.1.6.2 thorpej if (ALI1543_LEGAL_LINK(clink) == 0 || ALI1543_LEGAL_IRQ(irq) == 0)
242 1.1.6.2 thorpej return (1);
243 1.1.6.2 thorpej
244 1.1.6.2 thorpej reg = pci_conf_read(ph->ph_pc, ph->ph_tag, ALI1543_INTR_CFG_REG);
245 1.1.6.2 thorpej ali1543_get_intr(v, clink, &val);
246 1.1.6.2 thorpej shift = ALI1543_INTR_PIRQ_SHIFT(clink);
247 1.1.6.2 thorpej reg &= ~(0x0f << shift);
248 1.1.6.2 thorpej reg |= (ali1543_intr_shuffle_set[irq] << shift);
249 1.1.6.2 thorpej pci_conf_write(ph->ph_pc, ph->ph_tag, ALI1543_INTR_CFG_REG, reg);
250 1.1.6.2 thorpej if (ali1543_get_intr(v, clink, &val) != 0 || val != irq)
251 1.1.6.2 thorpej return (1);
252 1.1.6.2 thorpej
253 1.1.6.2 thorpej return (0);
254 1.1.6.2 thorpej }
255 1.1.6.2 thorpej
256 1.1.6.2 thorpej
257 1.1.6.2 thorpej
258 1.1.6.2 thorpej int
259 1.1.6.2 thorpej ali1543_get_trigger(pciintr_icu_handle_t v, int irq, int *triggerp)
260 1.1.6.2 thorpej {
261 1.1.6.2 thorpej struct piix_handle *ph = v;
262 1.1.6.2 thorpej int i, error, check_consistency, pciirq, pcitrigger = IST_NONE;
263 1.1.6.2 thorpej pcireg_t reg;
264 1.1.6.2 thorpej
265 1.1.6.2 thorpej if (ALI1543_LEGAL_IRQ(irq) == 0) {
266 1.1.6.2 thorpej return 1;
267 1.1.6.2 thorpej }
268 1.1.6.2 thorpej
269 1.1.6.2 thorpej check_consistency = 0;
270 1.1.6.2 thorpej for (i = 0; i <= 7; i++) {
271 1.1.6.2 thorpej ali1543_get_intr(v, i, &pciirq);
272 1.1.6.2 thorpej if (pciirq == irq) {
273 1.1.6.2 thorpej reg = pci_conf_read(ph->ph_pc, ph->ph_tag,
274 1.1.6.2 thorpej ALI1543_INTR_TRIG_REG);
275 1.1.6.2 thorpej if (ALI1543_INTR_TRIG_GET(reg, i) == ALI1543_INTR_TRIG_EDGE)
276 1.1.6.2 thorpej pcitrigger = IST_EDGE;
277 1.1.6.2 thorpej else
278 1.1.6.2 thorpej pcitrigger = IST_LEVEL;
279 1.1.6.2 thorpej check_consistency = 1;
280 1.1.6.2 thorpej break;
281 1.1.6.2 thorpej }
282 1.1.6.2 thorpej }
283 1.1.6.2 thorpej
284 1.1.6.2 thorpej error = piix_get_trigger(v, irq, triggerp);
285 1.1.6.2 thorpej if (error == 0 && check_consistency && pcitrigger != *triggerp)
286 1.1.6.2 thorpej return (1);
287 1.1.6.2 thorpej return (error);
288 1.1.6.2 thorpej }
289 1.1.6.2 thorpej
290 1.1.6.2 thorpej
291 1.1.6.2 thorpej
292 1.1.6.2 thorpej int
293 1.1.6.2 thorpej ali1543_set_trigger(pciintr_icu_handle_t v, int irq, int trigger)
294 1.1.6.2 thorpej {
295 1.1.6.2 thorpej struct piix_handle *ph = v;
296 1.1.6.2 thorpej int i, pciirq, testtrig;
297 1.1.6.2 thorpej pcireg_t reg;
298 1.1.6.2 thorpej
299 1.1.6.2 thorpej if (ALI1543_LEGAL_IRQ(irq) == 0) {
300 1.1.6.2 thorpej return 1;
301 1.1.6.2 thorpej }
302 1.1.6.2 thorpej
303 1.1.6.2 thorpej for (i = 0; i <= 7; i++) {
304 1.1.6.2 thorpej ali1543_get_intr(v, i, &pciirq);
305 1.1.6.2 thorpej if (pciirq == irq) {
306 1.1.6.2 thorpej reg = pci_conf_read(ph->ph_pc, ph->ph_tag,
307 1.1.6.2 thorpej ALI1543_INTR_TRIG_REG);
308 1.1.6.2 thorpej if (trigger == IST_LEVEL) {
309 1.1.6.2 thorpej ALI1543_INTR_TRIG_SET(reg, i,
310 1.1.6.2 thorpej ALI1543_INTR_TRIG_LEVEL);
311 1.1.6.2 thorpej } else {
312 1.1.6.2 thorpej ALI1543_INTR_TRIG_SET(reg, i,
313 1.1.6.2 thorpej ALI1543_INTR_TRIG_EDGE);
314 1.1.6.2 thorpej }
315 1.1.6.2 thorpej
316 1.1.6.2 thorpej pci_conf_write(ph->ph_pc, ph->ph_tag,
317 1.1.6.2 thorpej ALI1543_INTR_TRIG_REG, reg);
318 1.1.6.2 thorpej break;
319 1.1.6.2 thorpej }
320 1.1.6.2 thorpej }
321 1.1.6.2 thorpej
322 1.1.6.2 thorpej if (piix_set_trigger(v, irq, trigger) != 0 ||
323 1.1.6.2 thorpej ali1543_get_trigger(v, irq, &testtrig) != 0 ||
324 1.1.6.2 thorpej testtrig != trigger)
325 1.1.6.2 thorpej return (1);
326 1.1.6.2 thorpej
327 1.1.6.2 thorpej return (0);
328 1.1.6.2 thorpej }
329