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