opti82c700.c revision 1.5 1 /* $NetBSD: opti82c700.c,v 1.5 2004/04/11 06:00:26 kochi 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 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * Copyright (c) 1999, by UCHIYAMA Yasushi
42 * All rights reserved.
43 *
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
46 * are met:
47 * 1. Redistributions of source code must retain the above copyright
48 * notice, this list of conditions and the following disclaimer.
49 * 2. The name of the developer may NOT be used to endorse or promote products
50 * derived from this software without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * SUCH DAMAGE.
63 */
64
65 /*
66 * Support for the Opti 82c700 FireStar PCI-ISA bridge interrupt controller.
67 */
68
69 #include <sys/cdefs.h>
70 __KERNEL_RCSID(0, "$NetBSD: opti82c700.c,v 1.5 2004/04/11 06:00:26 kochi Exp $");
71
72 #include <sys/param.h>
73 #include <sys/systm.h>
74 #include <sys/device.h>
75 #include <sys/malloc.h>
76
77 #include <machine/intr.h>
78 #include <machine/bus.h>
79
80 #include <dev/pci/pcivar.h>
81 #include <dev/pci/pcireg.h>
82 #include <dev/pci/pcidevs.h>
83
84 #include <i386/pci/pci_intr_fixup.h>
85 #include <i386/pci/opti82c700reg.h>
86
87 #ifdef FIRESTARDEBUG
88 #define DPRINTF(arg) printf arg
89 #else
90 #define DPRINTF(arg)
91 #endif
92
93 int opti82c700_getclink(pciintr_icu_handle_t, int, int *);
94 int opti82c700_get_intr(pciintr_icu_handle_t, int, int *);
95 int opti82c700_set_intr(pciintr_icu_handle_t, int, int);
96 int opti82c700_get_trigger(pciintr_icu_handle_t, int, int *);
97 int opti82c700_set_trigger(pciintr_icu_handle_t, int, int);
98
99 const struct pciintr_icu opti82c700_pci_icu = {
100 opti82c700_getclink,
101 opti82c700_get_intr,
102 opti82c700_set_intr,
103 opti82c700_get_trigger,
104 opti82c700_set_trigger,
105 };
106
107 struct opti82c700_handle {
108 pci_chipset_tag_t ph_pc;
109 pcitag_t ph_tag;
110 };
111
112 int opti82c700_addr(int, int *, int *);
113 #ifdef FIRESTARDEBUG
114 void opti82c700_pir_dump(struct opti82c700_handle *);
115 #endif
116
117 int
118 opti82c700_init(pci_chipset_tag_t pc, bus_space_tag_t iot, pcitag_t tag,
119 pciintr_icu_tag_t *ptagp, pciintr_icu_handle_t *phandp)
120 {
121 struct opti82c700_handle *ph;
122
123 ph = malloc(sizeof(*ph), M_DEVBUF, M_NOWAIT);
124 if (ph == NULL)
125 return (1);
126
127 ph->ph_pc = pc;
128 ph->ph_tag = tag;
129 #ifdef FIRESTARDEBUG
130 opti82c700_pir_dump(ph);
131 #endif
132 *ptagp = &opti82c700_pci_icu;
133 *phandp = ph;
134 return (0);
135 }
136
137 int
138 opti82c700_addr(int link, int *addrofs, int *ofs)
139 {
140 int regofs, src;
141
142 regofs = FIRESTAR_PIR_REGOFS(link);
143 src = FIRESTAR_PIR_SELECTSRC(link);
144
145 switch (src) {
146 case FIRESTAR_PIR_SELECT_NONE:
147 return (1);
148
149 case FIRESTAR_PIR_SELECT_IRQ:
150 if (regofs < 0 || regofs > 7)
151 return (1);
152 *addrofs = FIRESTAR_CFG_INTR_IRQ + (regofs >> 2);
153 *ofs = (regofs & 3) << 3;
154 break;
155
156 case FIRESTAR_PIR_SELECT_PIRQ:
157 /* FALLTHROUGH */
158 case FIRESTAR_PIR_SELECT_BRIDGE:
159 if (regofs < 0 || regofs > 3)
160 return (1);
161 *addrofs = FIRESTAR_CFG_INTR_PIRQ;
162 *ofs = regofs << 2;
163 break;
164
165 default:
166 return (1);
167 }
168
169 return (0);
170 }
171
172 int
173 opti82c700_getclink(pciintr_icu_handle_t v, int link, int *clinkp)
174 {
175 DPRINTF(("FireStar link value 0x%x: ", link));
176
177 switch (FIRESTAR_PIR_SELECTSRC(link)) {
178 default:
179 DPRINTF(("bogus IRQ selection source\n"));
180 return (1);
181 case FIRESTAR_PIR_SELECT_NONE:
182 DPRINTF(("No interrupt connection\n"));
183 return (1);
184 case FIRESTAR_PIR_SELECT_IRQ:
185 DPRINTF(("FireStar IRQ pin"));
186 break;
187 case FIRESTAR_PIR_SELECT_PIRQ:
188 DPRINTF(("FireStar PIO pin or Serial IRQ PIRQ#"));
189 break;
190 case FIRESTAR_PIR_SELECT_BRIDGE:
191 DPRINTF(("FireBridge 1 INTx# pin"));
192 break;
193 }
194
195 DPRINTF((" REGOFST:%#x\n", FIRESTAR_PIR_REGOFS(link)));
196 *clinkp = link;
197
198 return (0);
199 }
200
201 int
202 opti82c700_get_intr(pciintr_icu_handle_t v, int clink, int *irqp)
203 {
204 struct opti82c700_handle *ph = v;
205 pcireg_t reg;
206 int val, addrofs, ofs;
207
208 if (opti82c700_addr(clink, &addrofs, &ofs))
209 return (1);
210
211 reg = pci_conf_read(ph->ph_pc, ph->ph_tag, addrofs);
212 val = (reg >> ofs) & FIRESTAR_CFG_PIRQ_MASK;
213
214 *irqp = (val == FIRESTAR_PIRQ_NONE) ?
215 X86_PCI_INTERRUPT_LINE_NO_CONNECTION : val;
216
217 return (0);
218 }
219
220 int
221 opti82c700_set_intr(pciintr_icu_handle_t v, int clink, int irq)
222 {
223 struct opti82c700_handle *ph = v;
224 int addrofs, ofs;
225 pcireg_t reg;
226
227 if (FIRESTAR_LEGAL_IRQ(irq) == 0)
228 return (1);
229
230 if (opti82c700_addr(clink, &addrofs, &ofs))
231 return (1);
232
233 reg = pci_conf_read(ph->ph_pc, ph->ph_tag, addrofs);
234 reg &= ~(FIRESTAR_CFG_PIRQ_MASK << ofs);
235 reg |= (irq << ofs);
236 pci_conf_write(ph->ph_pc, ph->ph_tag, addrofs, reg);
237
238 return (0);
239 }
240
241 int
242 opti82c700_get_trigger(pciintr_icu_handle_t v, int irq, int *triggerp)
243 {
244 struct opti82c700_handle *ph = v;
245 int i, val, addrofs, ofs;
246 pcireg_t reg;
247
248 if (FIRESTAR_LEGAL_IRQ(irq) == 0) {
249 /* ISA IRQ? */
250 *triggerp = IST_EDGE;
251 return (0);
252 }
253
254 /*
255 * Search PCIDV1 registers.
256 */
257 for (i = 0; i < 8; i++) {
258 opti82c700_addr(FIRESTAR_PIR_MAKELINK(FIRESTAR_PIR_SELECT_IRQ,
259 i), &addrofs, &ofs);
260 reg = pci_conf_read(ph->ph_pc, ph->ph_tag, addrofs);
261 val = (reg >> ofs) & FIRESTAR_CFG_PIRQ_MASK;
262 if (val != irq)
263 continue;
264 val = ((reg >> ofs) >> FIRESTAR_TRIGGER_SHIFT) &
265 FIRESTAR_TRIGGER_MASK;
266 *triggerp = val ? IST_LEVEL : IST_EDGE;
267 return (0);
268 }
269
270 /*
271 * Search PIO PCIIRQ.
272 */
273 for (i = 0; i < 4; i++) {
274 opti82c700_addr(FIRESTAR_PIR_MAKELINK(FIRESTAR_PIR_SELECT_PIRQ,
275 i), &addrofs, &ofs);
276 reg = pci_conf_read(ph->ph_pc, ph->ph_tag, addrofs);
277 val = (reg >> ofs) & FIRESTAR_CFG_PIRQ_MASK;
278 if (val != irq)
279 continue;
280 *triggerp = IST_LEVEL;
281 return (0);
282 }
283
284 return (1);
285 }
286
287 int
288 opti82c700_set_trigger(pciintr_icu_handle_t v, int irq, int trigger)
289 {
290 struct opti82c700_handle *ph = v;
291 int i, val, addrofs, ofs;
292 pcireg_t reg;
293
294 if (FIRESTAR_LEGAL_IRQ(irq) == 0) {
295 /* ISA IRQ? */
296 return ((trigger != IST_LEVEL) ? 0 : 1);
297 }
298
299 /*
300 * Search PCIDV1 registers.
301 */
302 for (i = 0; i < 8; i++) {
303 opti82c700_addr(FIRESTAR_PIR_MAKELINK(FIRESTAR_PIR_SELECT_IRQ,
304 i), &addrofs, &ofs);
305 reg = pci_conf_read(ph->ph_pc, ph->ph_tag, addrofs);
306 val = (reg >> ofs) & FIRESTAR_CFG_PIRQ_MASK;
307 if (val != irq)
308 continue;
309 if (trigger == IST_LEVEL)
310 reg |= (FIRESTAR_TRIGGER_MASK <<
311 (FIRESTAR_TRIGGER_SHIFT + ofs));
312 else
313 reg &= ~(FIRESTAR_TRIGGER_MASK <<
314 (FIRESTAR_TRIGGER_SHIFT + ofs));
315 pci_conf_write(ph->ph_pc, ph->ph_tag, addrofs, reg);
316 return (0);
317 }
318
319 /*
320 * Search PIO PCIIRQ.
321 */
322 for (i = 0; i < 4; i++) {
323 opti82c700_addr(FIRESTAR_PIR_MAKELINK(FIRESTAR_PIR_SELECT_PIRQ,
324 i), &addrofs, &ofs);
325 reg = pci_conf_read(ph->ph_pc, ph->ph_tag, addrofs);
326 val = (reg >> ofs) & FIRESTAR_CFG_PIRQ_MASK;
327 if (val != irq)
328 continue;
329 return (trigger == IST_LEVEL ? 0 : 1);
330 }
331
332 return (1);
333 }
334
335 #ifdef FIRESTARDEBUG
336 void
337 opti82c700_pir_dump(struct opti82c700_handle *ph)
338 {
339 pcireg_t r;
340 pcitag_t tag = ph->ph_tag;
341 pci_chipset_tag_t pc = ph->ph_pc;
342 int i, j, k;
343
344 /* FireStar IRQ pin */
345 printf("-FireStar IRQ pin-\n");
346 for (i = j = k = 0; i < 8; i += 4) {
347 r = pci_conf_read(pc, tag, 0xb0 + i);
348 printf ("\t");
349 for (j = 0; j < 4; j++, k++, r >>= 8) {
350 printf("[%d:%s-IRQ%2d] ", k,
351 (r & (FIRESTAR_TRIGGER_MASK <<
352 FIRESTAR_TRIGGER_SHIFT)) ? "PCI" : "ISA",
353 r & FIRESTAR_CFG_PIRQ_MASK);
354 }
355 printf("\n");
356 }
357
358 /* FireStar PIO pin or Serial IRQ PIRQ# */
359 r = pci_conf_read(pc, tag, 0xb8);
360 printf("-FireStar PIO pin or Serial IRQ PIRQ#-\n\t");
361 for (i = 0; i < 4; i++, r >>= 4) {
362 printf("[PCIIRQ%d# %d] ", i, r & FIRESTAR_CFG_PIRQ_MASK);
363 }
364 printf("\n");
365 }
366 #endif /* FIRESTARDEBUG */
367