via8231.c revision 1.2 1 1.2 christos /* $NetBSD: via8231.c,v 1.2 2006/05/15 00:36:55 christos Exp $ */
2 1.1 xtraeme /* OpenBSD: via8231.c,v 1.6 2005/10/27 16:41:06 mickey Exp */
3 1.1 xtraeme
4 1.1 xtraeme /*-
5 1.1 xtraeme * Copyright (c) 1999 The NetBSD Foundation, Inc.
6 1.1 xtraeme * All rights reserved.
7 1.1 xtraeme *
8 1.1 xtraeme * This code is derived from software contributed to The NetBSD Foundation
9 1.1 xtraeme * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
10 1.1 xtraeme * NASA Ames Research Center.
11 1.1 xtraeme *
12 1.1 xtraeme * Redistribution and use in source and binary forms, with or without
13 1.1 xtraeme * modification, are permitted provided that the following conditions
14 1.1 xtraeme * are met:
15 1.1 xtraeme * 1. Redistributions of source code must retain the above copyright
16 1.1 xtraeme * notice, this list of conditions and the following disclaimer.
17 1.1 xtraeme * 2. Redistributions in binary form must reproduce the above copyright
18 1.1 xtraeme * notice, this list of conditions and the following disclaimer in the
19 1.1 xtraeme * documentation and/or other materials provided with the distribution.
20 1.1 xtraeme * 3. All advertising materials mentioning features or use of this software
21 1.1 xtraeme * must display the following acknowledgement:
22 1.1 xtraeme * This product includes software developed by the NetBSD
23 1.1 xtraeme * Foundation, Inc. and its contributors.
24 1.1 xtraeme * 4. Neither the name of The NetBSD Foundation nor the names of its
25 1.1 xtraeme * contributors may be used to endorse or promote products derived
26 1.1 xtraeme * from this software without specific prior written permission.
27 1.1 xtraeme *
28 1.1 xtraeme * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29 1.1 xtraeme * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30 1.1 xtraeme * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31 1.1 xtraeme * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32 1.1 xtraeme * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 1.1 xtraeme * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 1.1 xtraeme * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 1.1 xtraeme * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 1.1 xtraeme * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 1.1 xtraeme * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 1.1 xtraeme * POSSIBILITY OF SUCH DAMAGE.
39 1.1 xtraeme */
40 1.1 xtraeme /*
41 1.1 xtraeme * Copyright (c) 2005, by Michael Shalayeff
42 1.1 xtraeme * Copyright (c) 2003, by Matthew Gream
43 1.1 xtraeme * Copyright (c) 1999, by UCHIYAMA Yasushi
44 1.1 xtraeme * All rights reserved.
45 1.1 xtraeme *
46 1.1 xtraeme * Redistribution and use in source and binary forms, with or without
47 1.1 xtraeme * modification, are permitted provided that the following conditions
48 1.1 xtraeme * are met:
49 1.1 xtraeme * 1. Redistributions of source code must retain the above copyright
50 1.1 xtraeme * notice, this list of conditions and the following disclaimer.
51 1.1 xtraeme * 2. The name of the developer may NOT be used to endorse or promote products
52 1.1 xtraeme * derived from this software without specific prior written permission.
53 1.1 xtraeme *
54 1.1 xtraeme * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
55 1.1 xtraeme * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 1.1 xtraeme * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 1.1 xtraeme * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
58 1.1 xtraeme * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 1.1 xtraeme * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 1.1 xtraeme * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 1.1 xtraeme * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 1.1 xtraeme * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 1.1 xtraeme * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 1.1 xtraeme * SUCH DAMAGE.
65 1.1 xtraeme */
66 1.1 xtraeme
67 1.1 xtraeme /*
68 1.1 xtraeme * Support for the VIA Technologies Inc. VIA VT823[1357] PCI to ISA Bridge
69 1.1 xtraeme * Based upon documentation:
70 1.1 xtraeme * 1. VIA VT8231 South Bridge, Revision 1.85 (March 11, 2002), pg 73
71 1.1 xtraeme * 2. VIA VT8237R South Bridge, Revision 2.06 (December 15, 2004), pg 100
72 1.1 xtraeme * Derived from amd756.c
73 1.1 xtraeme */
74 1.1 xtraeme
75 1.1 xtraeme #include <sys/cdefs.h>
76 1.2 christos __KERNEL_RCSID(0, "$NetBSD: via8231.c,v 1.2 2006/05/15 00:36:55 christos Exp $");
77 1.1 xtraeme
78 1.1 xtraeme #include <sys/param.h>
79 1.1 xtraeme #include <sys/systm.h>
80 1.1 xtraeme #include <sys/device.h>
81 1.1 xtraeme #include <sys/malloc.h>
82 1.1 xtraeme
83 1.1 xtraeme #include <machine/intr.h>
84 1.1 xtraeme #include <machine/bus.h>
85 1.1 xtraeme
86 1.1 xtraeme #include <dev/pci/pcivar.h>
87 1.1 xtraeme #include <dev/pci/pcireg.h>
88 1.1 xtraeme #include <dev/pci/pcidevs.h>
89 1.1 xtraeme
90 1.1 xtraeme #include <i386/pci/pci_intr_fixup.h>
91 1.1 xtraeme #include <i386/pci/via8231reg.h>
92 1.1 xtraeme
93 1.1 xtraeme struct via8231_handle {
94 1.1 xtraeme bus_space_tag_t ph_iot;
95 1.1 xtraeme bus_space_handle_t ph_regs_ioh;
96 1.1 xtraeme pci_chipset_tag_t ph_pc;
97 1.1 xtraeme pcitag_t ph_tag;
98 1.1 xtraeme int flags;
99 1.1 xtraeme #define VT8237 0x0001
100 1.1 xtraeme };
101 1.1 xtraeme
102 1.1 xtraeme int via8231_getclink(pciintr_icu_handle_t, int, int *);
103 1.1 xtraeme int via8231_get_intr(pciintr_icu_handle_t, int, int *);
104 1.1 xtraeme int via8231_set_intr(pciintr_icu_handle_t, int, int);
105 1.1 xtraeme int via8231_get_trigger(pciintr_icu_handle_t, int, int *);
106 1.1 xtraeme int via8231_set_trigger(pciintr_icu_handle_t, int, int);
107 1.1 xtraeme #ifdef VIA8231_DEBUG
108 1.1 xtraeme static void via8231_pir_dump(const char*, struct via8231_handle *);
109 1.1 xtraeme #endif
110 1.1 xtraeme
111 1.1 xtraeme const struct pciintr_icu via8231_pci_icu = {
112 1.1 xtraeme via8231_getclink,
113 1.1 xtraeme via8231_get_intr,
114 1.1 xtraeme via8231_set_intr,
115 1.1 xtraeme via8231_get_trigger,
116 1.1 xtraeme via8231_set_trigger,
117 1.1 xtraeme };
118 1.1 xtraeme
119 1.1 xtraeme struct mask_shft_pair {
120 1.1 xtraeme int mask;
121 1.1 xtraeme int shft;
122 1.1 xtraeme };
123 1.1 xtraeme
124 1.1 xtraeme static const struct mask_shft_pair via8231_routing_cnfg[VIA8231_LINK_MAX+1] = {
125 1.1 xtraeme { 0x0f, 0+4 }, /*PINTA#*/
126 1.1 xtraeme { 0x0f, 8+0 }, /*PINTB#*/
127 1.1 xtraeme { 0x0f, 8+4 }, /*PINTC#*/
128 1.1 xtraeme { 0x0f, 16+4 } /*PINTD#*/
129 1.1 xtraeme };
130 1.1 xtraeme
131 1.1 xtraeme #define VIA8231_GET_TRIGGER_CNFG(reg, pirq) \
132 1.1 xtraeme ((reg) & (1 << (3 - (clink & 3))))
133 1.1 xtraeme #define VIA8231_SET_TRIGGER_CNFG(reg, clink, cfg) \
134 1.1 xtraeme (((reg) & ~(1 << (3 - (clink & 3)))) | ((cfg) << (3 - (clink & 3))))
135 1.1 xtraeme
136 1.1 xtraeme #define VIA8231_GET_ROUTING_CNFG(reg, pirq) \
137 1.1 xtraeme (((reg) >> via8231_routing_cnfg[(pirq)].shft) & \
138 1.1 xtraeme via8231_routing_cnfg[(pirq)].mask)
139 1.1 xtraeme
140 1.1 xtraeme #define VIA8231_SET_ROUTING_CNFG(reg, pirq, cfg) \
141 1.1 xtraeme (((reg) & ~(via8231_routing_cnfg[(pirq)].mask << \
142 1.1 xtraeme via8231_routing_cnfg[(pirq)].shft)) | \
143 1.1 xtraeme (((cfg) & via8231_routing_cnfg[(pirq)].mask) << \
144 1.1 xtraeme via8231_routing_cnfg[(pirq)].shft))
145 1.1 xtraeme
146 1.1 xtraeme int
147 1.1 xtraeme via8231_init(pci_chipset_tag_t pc, bus_space_tag_t iot, pcitag_t tag,
148 1.1 xtraeme pciintr_icu_tag_t *ptagp, pciintr_icu_handle_t *phandp)
149 1.1 xtraeme {
150 1.1 xtraeme struct via8231_handle *ph;
151 1.1 xtraeme pcireg_t id;
152 1.1 xtraeme
153 1.1 xtraeme ph = malloc(sizeof(*ph), M_DEVBUF, M_NOWAIT);
154 1.1 xtraeme if (ph == NULL)
155 1.1 xtraeme return (1);
156 1.1 xtraeme
157 1.1 xtraeme ph->ph_iot = iot;
158 1.1 xtraeme ph->ph_pc = pc;
159 1.1 xtraeme ph->ph_tag = tag;
160 1.1 xtraeme id = pci_conf_read(pc, tag, PCI_ID_REG);
161 1.1 xtraeme ph->flags = PCI_VENDOR(id) == PCI_VENDOR_VIATECH &&
162 1.1 xtraeme PCI_PRODUCT(id) == PCI_PRODUCT_VIATECH_VT8231? 0 : VT8237;
163 1.1 xtraeme
164 1.1 xtraeme *ptagp = &via8231_pci_icu;
165 1.1 xtraeme *phandp = ph;
166 1.1 xtraeme
167 1.1 xtraeme #ifdef VIA8231_DEBUG
168 1.1 xtraeme via8231_pir_dump("via8231_init", ph);
169 1.1 xtraeme #endif
170 1.1 xtraeme
171 1.1 xtraeme return 0;
172 1.1 xtraeme }
173 1.1 xtraeme
174 1.1 xtraeme int
175 1.1 xtraeme via8231_getclink(pciintr_icu_handle_t v, int link, int *clinkp)
176 1.1 xtraeme {
177 1.1 xtraeme struct via8231_handle *ph = v;
178 1.1 xtraeme
179 1.1 xtraeme if ((ph->flags & VT8237) && !VIA8237_LINK_LEGAL(link - 1))
180 1.1 xtraeme return (1);
181 1.1 xtraeme
182 1.1 xtraeme if (!(ph->flags & VT8237) && !VIA8231_LINK_LEGAL(link - 1))
183 1.1 xtraeme return (1);
184 1.1 xtraeme
185 1.1 xtraeme *clinkp = link - 1;
186 1.1 xtraeme return (0);
187 1.1 xtraeme }
188 1.1 xtraeme
189 1.1 xtraeme int
190 1.1 xtraeme via8231_get_intr(pciintr_icu_handle_t v, int clink, int *irqp)
191 1.1 xtraeme {
192 1.1 xtraeme struct via8231_handle *ph = v;
193 1.1 xtraeme int reg, val;
194 1.1 xtraeme
195 1.1 xtraeme if (VIA8237_LINK_LEGAL(clink) == 0)
196 1.1 xtraeme return (1);
197 1.1 xtraeme
198 1.1 xtraeme if (VIA8231_LINK_LEGAL(clink)) {
199 1.1 xtraeme reg = VIA8231_GET_ROUTING(ph);
200 1.1 xtraeme val = VIA8231_GET_ROUTING_CNFG(reg, clink);
201 1.1 xtraeme } else {
202 1.1 xtraeme reg = VIA8237_GET_ROUTING(ph);
203 1.1 xtraeme val = (reg >> ((clink & 3) * 4)) & 0xf;
204 1.1 xtraeme }
205 1.1 xtraeme
206 1.1 xtraeme *irqp = (val == VIA8231_ROUTING_CNFG_DISABLED) ?
207 1.1 xtraeme X86_PCI_INTERRUPT_LINE_NO_CONNECTION : val;
208 1.1 xtraeme
209 1.1 xtraeme return (0);
210 1.1 xtraeme }
211 1.1 xtraeme
212 1.1 xtraeme int
213 1.1 xtraeme via8231_set_intr(pciintr_icu_handle_t v, int clink, int irq)
214 1.1 xtraeme {
215 1.1 xtraeme struct via8231_handle *ph = v;
216 1.1 xtraeme int reg;
217 1.1 xtraeme
218 1.1 xtraeme if (VIA8237_LINK_LEGAL(clink) == 0 || VIA8231_PIRQ_LEGAL(irq) == 0)
219 1.1 xtraeme return (1);
220 1.1 xtraeme
221 1.1 xtraeme #ifdef VIA8231_DEBUG
222 1.1 xtraeme printf("via8231_set_intr: link(%02x) --> irq(%02x)\n", clink, irq);
223 1.1 xtraeme via8231_pir_dump("via8231_set_intr: ", ph);
224 1.1 xtraeme #endif
225 1.1 xtraeme
226 1.1 xtraeme if (VIA8231_LINK_LEGAL(clink)) {
227 1.1 xtraeme reg = VIA8231_GET_ROUTING(ph);
228 1.1 xtraeme VIA8231_SET_ROUTING(ph,
229 1.1 xtraeme VIA8231_SET_ROUTING_CNFG(reg, clink, irq));
230 1.1 xtraeme } else {
231 1.1 xtraeme reg = VIA8237_GET_ROUTING(ph);
232 1.1 xtraeme VIA8237_SET_ROUTING(ph, (reg & ~(0xf << (clink & 3))) |
233 1.1 xtraeme ((irq & 0xf) << (clink & 3)));
234 1.1 xtraeme }
235 1.1 xtraeme
236 1.1 xtraeme return (0);
237 1.1 xtraeme }
238 1.1 xtraeme
239 1.1 xtraeme int
240 1.1 xtraeme via8231_get_trigger(pciintr_icu_handle_t v, int irq, int *triggerp)
241 1.1 xtraeme {
242 1.1 xtraeme struct via8231_handle *ph = v;
243 1.2 christos int reg, clink, m, pciirq = 0; /* XXX: GCC */
244 1.1 xtraeme
245 1.1 xtraeme if (VIA8231_PIRQ_LEGAL(irq) == 0)
246 1.1 xtraeme return (1);
247 1.1 xtraeme
248 1.1 xtraeme m = ph->flags & VT8237? VIA8237_LINK_MAX : VIA8231_LINK_MAX;
249 1.1 xtraeme for (clink = 0; clink <= m; clink++) {
250 1.1 xtraeme via8231_get_intr(v, clink, &pciirq);
251 1.1 xtraeme if (pciirq == irq) {
252 1.1 xtraeme reg = VIA8231_LINK_LEGAL(clink)?
253 1.1 xtraeme VIA8231_GET_TRIGGER(ph):
254 1.1 xtraeme VIA8237_GET_TRIGGER(ph);
255 1.1 xtraeme *triggerp = VIA8231_GET_TRIGGER_CNFG(reg, clink)?
256 1.1 xtraeme IST_EDGE : IST_LEVEL;
257 1.1 xtraeme return (0);
258 1.1 xtraeme }
259 1.1 xtraeme }
260 1.1 xtraeme
261 1.1 xtraeme return (1);
262 1.1 xtraeme }
263 1.1 xtraeme
264 1.1 xtraeme int
265 1.1 xtraeme via8231_set_trigger(pciintr_icu_handle_t v, int irq, int trigger)
266 1.1 xtraeme {
267 1.1 xtraeme struct via8231_handle *ph = v;
268 1.1 xtraeme int reg, clink, m, pciirq;
269 1.1 xtraeme
270 1.1 xtraeme if (VIA8231_PIRQ_LEGAL(irq) == 0 || VIA8231_TRIG_LEGAL(trigger) == 0)
271 1.1 xtraeme return (1);
272 1.1 xtraeme
273 1.1 xtraeme #ifdef VIA8231_DEBUG
274 1.1 xtraeme printf("via8231_set_trig: irq(%02x) --> trig(%02x)\n", irq, trigger);
275 1.1 xtraeme via8231_pir_dump("via8231_set_trig: ", ph);
276 1.1 xtraeme #endif
277 1.1 xtraeme
278 1.1 xtraeme m = ph->flags & VT8237? VIA8237_LINK_MAX : VIA8231_LINK_MAX;
279 1.1 xtraeme for (clink = 0; clink <= VIA8231_LINK_MAX; clink++) {
280 1.1 xtraeme via8231_get_intr(v, clink, &pciirq);
281 1.1 xtraeme if (pciirq == irq) {
282 1.1 xtraeme reg = VIA8231_LINK_LEGAL(clink)?
283 1.1 xtraeme VIA8231_GET_TRIGGER(ph):
284 1.1 xtraeme VIA8237_GET_TRIGGER(ph);
285 1.1 xtraeme switch (trigger) {
286 1.1 xtraeme case IST_LEVEL:
287 1.1 xtraeme reg = VIA8231_SET_TRIGGER_CNFG(reg, clink,
288 1.1 xtraeme VIA8231_TRIGGER_CNFG_LEVEL);
289 1.1 xtraeme break;
290 1.1 xtraeme case IST_EDGE:
291 1.1 xtraeme reg = VIA8231_SET_TRIGGER_CNFG(reg, clink,
292 1.1 xtraeme VIA8231_TRIGGER_CNFG_EDGE);
293 1.1 xtraeme break;
294 1.1 xtraeme default:
295 1.1 xtraeme return (1);
296 1.1 xtraeme }
297 1.1 xtraeme if (VIA8231_LINK_LEGAL(clink))
298 1.1 xtraeme VIA8231_SET_TRIGGER(ph, reg);
299 1.1 xtraeme else
300 1.1 xtraeme VIA8237_SET_TRIGGER(ph, reg);
301 1.1 xtraeme return (0);
302 1.1 xtraeme }
303 1.1 xtraeme }
304 1.1 xtraeme
305 1.1 xtraeme return (1);
306 1.1 xtraeme }
307 1.1 xtraeme
308 1.1 xtraeme #ifdef VIA8231_DEBUG
309 1.1 xtraeme static void
310 1.1 xtraeme via8231_pir_dump(const char *m, struct via8231_handle *ph)
311 1.1 xtraeme {
312 1.1 xtraeme int a, b;
313 1.1 xtraeme
314 1.1 xtraeme a = VIA8231_GET_TRIGGER(ph);
315 1.1 xtraeme b = VIA8231_GET_ROUTING(ph);
316 1.1 xtraeme
317 1.1 xtraeme printf("%s STATE: trigger(%02x), routing(%08x)\n", m, a, b);
318 1.1 xtraeme }
319 1.1 xtraeme #endif
320