xref: /openbsd-src/sys/arch/i386/pci/amd756.c (revision f4e7063748a2ac72b2bab4389c0a7efc72d82189)
1 /*	$OpenBSD: amd756.c,v 1.6 2023/01/30 10:49:05 jsg Exp $	*/
2 /*	$NetBSD$	*/
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 /*
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 Advanced Micro Devices AMD756 Peripheral Bus Controller.
61  */
62 
63 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/malloc.h>
66 
67 #include <machine/intr.h>
68 #include <machine/bus.h>
69 
70 #include <dev/pci/pcivar.h>
71 
72 #include <i386/pci/pcibiosvar.h>
73 #include <i386/pci/amd756reg.h>
74 
75 struct viper_handle {
76 	bus_space_tag_t ph_iot;
77 	bus_space_handle_t ph_regs_ioh;
78 	pci_chipset_tag_t ph_pc;
79 	pcitag_t ph_tag;
80 };
81 
82 int amd756_getclink(pciintr_icu_handle_t, int, int *);
83 int amd756_get_intr(pciintr_icu_handle_t, int, int *);
84 int amd756_set_intr(pciintr_icu_handle_t, int, int);
85 int amd756_get_trigger(pciintr_icu_handle_t, int, int *);
86 int amd756_set_trigger(pciintr_icu_handle_t, int, int);
87 #ifdef VIPER_DEBUG
88 static void amd756_pir_dump(struct viper_handle *);
89 #endif
90 
91 const struct pciintr_icu amd756_pci_icu = {
92 	amd756_getclink,
93 	amd756_get_intr,
94 	amd756_set_intr,
95 	amd756_get_trigger,
96 	amd756_set_trigger,
97 };
98 
99 
100 int
amd756_init(pci_chipset_tag_t pc,bus_space_tag_t iot,pcitag_t tag,pciintr_icu_tag_t * ptagp,pciintr_icu_handle_t * phandp)101 amd756_init(pci_chipset_tag_t pc, bus_space_tag_t iot, pcitag_t tag,
102     pciintr_icu_tag_t *ptagp, pciintr_icu_handle_t *phandp)
103 {
104 	struct viper_handle *ph;
105 
106 	ph = malloc(sizeof(*ph), M_DEVBUF, M_NOWAIT);
107 	if (ph == NULL)
108 		return (1);
109 
110 	ph->ph_iot = iot;
111 	ph->ph_pc = pc;
112 	ph->ph_tag = tag;
113 
114 	*ptagp = &amd756_pci_icu;
115 	*phandp = ph;
116 
117 #ifdef VIPER_DEBUG
118 	amd756_pir_dump(ph);
119 #endif
120 
121 	return 0;
122 }
123 
124 int
amd756_getclink(pciintr_icu_handle_t v,int link,int * clinkp)125 amd756_getclink(pciintr_icu_handle_t v, int link, int *clinkp)
126 {
127 	if (AMD756_LEGAL_LINK(link - 1) == 0)
128 		return (1);
129 
130 	*clinkp = link - 1;
131 	return (0);
132 }
133 
134 int
amd756_get_intr(pciintr_icu_handle_t v,int clink,int * irqp)135 amd756_get_intr(pciintr_icu_handle_t v, int clink, int *irqp)
136 {
137 	struct viper_handle *ph = v;
138 	pcireg_t reg;
139 	int val;
140 
141 	if (AMD756_LEGAL_LINK(clink) == 0)
142 		return (1);
143 
144 	reg = AMD756_GET_PIIRQSEL(ph);
145 	val = (reg >> (4*clink)) & 0x0f;
146 	*irqp = (val == 0) ?
147 	    I386_PCI_INTERRUPT_LINE_NO_CONNECTION : val;
148 
149 	return (0);
150 }
151 
152 int
amd756_set_intr(pciintr_icu_handle_t v,int clink,int irq)153 amd756_set_intr(pciintr_icu_handle_t v, int clink, int irq)
154 {
155 	struct viper_handle *ph = v;
156 	int val;
157 	pcireg_t reg;
158 
159 	if (AMD756_LEGAL_LINK(clink) == 0 || AMD756_LEGAL_IRQ(irq) == 0)
160 		return (1);
161 
162 	reg = AMD756_GET_PIIRQSEL(ph);
163 	amd756_get_intr(v, clink, &val);
164 	reg &= ~(0x000f << (4*clink));
165 	reg |= irq << (4*clink);
166 	AMD756_SET_PIIRQSEL(ph, reg);
167 
168 	return 0;
169 }
170 
171 int
amd756_get_trigger(pciintr_icu_handle_t v,int irq,int * triggerp)172 amd756_get_trigger(pciintr_icu_handle_t v, int irq, int *triggerp)
173 {
174 	struct viper_handle *ph = v;
175 	int i, pciirq;
176 	pcireg_t reg;
177 
178 	if (AMD756_LEGAL_IRQ(irq) == 0)
179 		return (1);
180 
181 	for (i = 0; i <= 3; i++) {
182 		amd756_get_intr(v, i, &pciirq);
183 		if (pciirq == irq) {
184 			reg = AMD756_GET_EDGESEL(ph);
185 			if (reg & (1 << i))
186 				*triggerp = IST_EDGE;
187 			else
188 				*triggerp = IST_LEVEL;
189 			break;
190 		}
191 	}
192 
193 	return 0;
194 }
195 
196 int
amd756_set_trigger(pciintr_icu_handle_t v,int irq,int trigger)197 amd756_set_trigger(pciintr_icu_handle_t v, int irq, int trigger)
198 {
199 	struct viper_handle *ph = v;
200 	int i, pciirq;
201 	pcireg_t reg;
202 
203 	if (AMD756_LEGAL_IRQ(irq) == 0)
204 		return (1);
205 
206 	for (i = 0; i <= 3; i++) {
207 		amd756_get_intr(v, i, &pciirq);
208 		if (pciirq == irq) {
209 			reg = AMD756_GET_PIIRQSEL(ph);
210 			if (trigger == IST_LEVEL)
211 				reg &= ~(1 << (4*i));
212 			else
213 				reg |= 1 << (4*i);
214 			AMD756_SET_PIIRQSEL(ph, reg);
215 			break;
216 		}
217 	}
218 
219 	return (0);
220 }
221 
222 #ifdef VIPER_DEBUG
223 static void
amd756_pir_dump(struct viper_handle * ph)224 amd756_pir_dump(struct viper_handle *ph)
225 {
226 	int a, b;
227 
228 	printf ("VIPER PCI INTERRUPT ROUTING REGISTERS:\n");
229 
230 	a = AMD756_GET_EDGESEL(ph);
231 	b = AMD756_GET_PIIRQSEL(ph);
232 
233 	printf ("TRIGGER: %02x, ROUTING: %04x\n", a, b);
234 }
235 #endif
236