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