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