xref: /netbsd/sys/arch/i386/pci/via8231.c (revision 2cf5ee49)
1 /*	$NetBSD: via8231.c,v 1.5 2014/01/25 21:11:03 christos Exp $	*/
2 /*	OpenBSD: via8231.c,v 1.6 2005/10/27 16:41:06 mickey Exp 	*/
3 
4 /*-
5  * Copyright (c) 1999 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
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.
11  *
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.
20  *
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.
32  */
33 /*
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.
38  *
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.
46  *
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.
58  */
59 
60 /*
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
66  */
67 
68 #include <sys/cdefs.h>
69 __KERNEL_RCSID(0, "$NetBSD: via8231.c,v 1.5 2014/01/25 21:11:03 christos Exp $");
70 
71 #include <sys/param.h>
72 #include <sys/systm.h>
73 #include <sys/device.h>
74 #include <sys/malloc.h>
75 
76 #include <machine/intr.h>
77 #include <sys/bus.h>
78 
79 #include <dev/pci/pcivar.h>
80 #include <dev/pci/pcireg.h>
81 #include <dev/pci/pcidevs.h>
82 
83 #include <i386/pci/pci_intr_fixup.h>
84 #include <i386/pci/via8231reg.h>
85 
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
93 };
94 
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
103 
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,
110 };
111 
112 struct mask_shft_pair {
113 	int mask;
114 	int shft;
115 };
116 
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#*/
122 };
123 
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))))
128 
129 #define VIA8231_GET_ROUTING_CNFG(reg, pirq) \
130 	(((reg) >> via8231_routing_cnfg[(pirq)].shft) & \
131 	    via8231_routing_cnfg[(pirq)].mask)
132 
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))
138 
139 int
via8231_init(pci_chipset_tag_t pc,bus_space_tag_t iot,pcitag_t tag,pciintr_icu_tag_t * ptagp,pciintr_icu_handle_t * phandp)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)
142 {
143 	struct via8231_handle *ph;
144 	pcireg_t id;
145 
146 	ph = malloc(sizeof(*ph), M_DEVBUF, M_NOWAIT);
147 	if (ph == NULL)
148 		return (1);
149 
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;
156 
157 	*ptagp = &via8231_pci_icu;
158 	*phandp = ph;
159 
160 #ifdef VIA8231_DEBUG
161 	via8231_pir_dump("via8231_init", ph);
162 #endif
163 
164 	return 0;
165 }
166 
167 int
via8231_getclink(pciintr_icu_handle_t v,int link,int * clinkp)168 via8231_getclink(pciintr_icu_handle_t v, int link, int *clinkp)
169 {
170 	struct via8231_handle *ph = v;
171 
172 	if ((ph->flags & VT8237) && !VIA8237_LINK_LEGAL(link - 1))
173 		return (1);
174 
175 	if (!(ph->flags & VT8237) && !VIA8231_LINK_LEGAL(link - 1))
176 		return (1);
177 
178 	*clinkp = link - 1;
179 	return (0);
180 }
181 
182 int
via8231_get_intr(pciintr_icu_handle_t v,int clink,int * irqp)183 via8231_get_intr(pciintr_icu_handle_t v, int clink, int *irqp)
184 {
185 	struct via8231_handle *ph = v;
186 	int reg, val;
187 
188 	if (VIA8237_LINK_LEGAL(clink) == 0)
189 		return (1);
190 
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;
197 	}
198 
199 	*irqp = (val == VIA8231_ROUTING_CNFG_DISABLED) ?
200 	    X86_PCI_INTERRUPT_LINE_NO_CONNECTION : val;
201 
202 	return (0);
203 }
204 
205 int
via8231_set_intr(pciintr_icu_handle_t v,int clink,int irq)206 via8231_set_intr(pciintr_icu_handle_t v, int clink, int irq)
207 {
208 	struct via8231_handle *ph = v;
209 	int reg;
210 
211 	if (VIA8237_LINK_LEGAL(clink) == 0 || VIA8231_PIRQ_LEGAL(irq) == 0)
212 		return (1);
213 
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
218 
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)));
227 	}
228 
229 	return (0);
230 }
231 
232 int
via8231_get_trigger(pciintr_icu_handle_t v,int irq,int * triggerp)233 via8231_get_trigger(pciintr_icu_handle_t v, int irq, int *triggerp)
234 {
235 	struct via8231_handle *ph = v;
236 	int reg, clink, m, pciirq = 0;	/* XXX: GCC */
237 
238 	if (VIA8231_PIRQ_LEGAL(irq) == 0)
239 		return (1);
240 
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);
251 		}
252 	}
253 
254 	return (1);
255 }
256 
257 int
via8231_set_trigger(pciintr_icu_handle_t v,int irq,int trigger)258 via8231_set_trigger(pciintr_icu_handle_t v, int irq, int trigger)
259 {
260 	struct via8231_handle *ph = v;
261 	int reg, clink, pciirq;
262 
263 	if (VIA8231_PIRQ_LEGAL(irq) == 0 || VIA8231_TRIG_LEGAL(trigger) == 0)
264 		return (1);
265 
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
270 
271 	for (clink = 0; clink <= VIA8231_LINK_MAX; clink++) {
272 		via8231_get_intr(v, clink, &pciirq);
273 		if (pciirq == irq) {
274 			reg = VIA8231_LINK_LEGAL(clink)?
275 			    VIA8231_GET_TRIGGER(ph):
276 			    VIA8237_GET_TRIGGER(ph);
277 			switch (trigger) {
278 			case IST_LEVEL:
279 				reg = VIA8231_SET_TRIGGER_CNFG(reg, clink,
280 					VIA8231_TRIGGER_CNFG_LEVEL);
281 				break;
282 			case IST_EDGE:
283 				reg = VIA8231_SET_TRIGGER_CNFG(reg, clink,
284 					VIA8231_TRIGGER_CNFG_EDGE);
285 				break;
286 			default:
287 				return (1);
288 			}
289 			if (VIA8231_LINK_LEGAL(clink))
290 				VIA8231_SET_TRIGGER(ph, reg);
291 			else
292 				VIA8237_SET_TRIGGER(ph, reg);
293 			return (0);
294 		}
295 	}
296 
297 	return (1);
298 }
299 
300 #ifdef VIA8231_DEBUG
301 static void
via8231_pir_dump(const char * m,struct via8231_handle * ph)302 via8231_pir_dump(const char *m, struct via8231_handle *ph)
303 {
304 	int a, b;
305 
306 	a = VIA8231_GET_TRIGGER(ph);
307 	b = VIA8231_GET_ROUTING(ph);
308 
309 	printf("%s STATE: trigger(%02x), routing(%08x)\n", m, a, b);
310 }
311 #endif
312