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