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