ali1543.c revision 1.10
1/*	$NetBSD: ali1543.c,v 1.10 2008/04/28 20:23:24 martin Exp $	*/
2
3/*
4 * Copyright (c) 2001
5 *       HAYAKAWA Koichi.  All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 *    derived from this software without specific prior written permission.
17 *
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
23 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32/*-
33 * Copyright (c) 1999 The NetBSD Foundation, Inc.
34 * All rights reserved.
35 *
36 * This code is derived from software contributed to The NetBSD Foundation
37 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
38 * NASA Ames Research Center.
39 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 * 1. Redistributions of source code must retain the above copyright
44 *    notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 *    notice, this list of conditions and the following disclaimer in the
47 *    documentation and/or other materials provided with the distribution.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
50 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
51 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
52 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
53 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
54 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
55 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
56 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
57 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
58 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
59 * POSSIBILITY OF SUCH DAMAGE.
60 */
61
62/*
63 * Copyright (c) 1999, by UCHIYAMA Yasushi
64 * All rights reserved.
65 *
66 * Redistribution and use in source and binary forms, with or without
67 * modification, are permitted provided that the following conditions
68 * are met:
69 * 1. Redistributions of source code must retain the above copyright
70 *    notice, this list of conditions and the following disclaimer.
71 * 2. The name of the developer may NOT be used to endorse or promote products
72 *    derived from this software without specific prior written permission.
73 *
74 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
75 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
76 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
77 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
78 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
79 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
80 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
81 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
82 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
83 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
84 * SUCH DAMAGE.
85 */
86
87/* HAYAKAWA Koichi wrote ALi 1543 PCI ICU code basing on VIA82C586 driver */
88
89#include <sys/cdefs.h>
90__KERNEL_RCSID(0, "$NetBSD: ali1543.c,v 1.10 2008/04/28 20:23:24 martin Exp $");
91
92#include <sys/param.h>
93#include <sys/systm.h>
94#include <sys/errno.h>
95#include <sys/device.h>
96#include <sys/malloc.h>
97#include <sys/proc.h>
98
99#include <machine/intr.h>
100#include <machine/bus.h>
101
102#include <dev/pci/pcivar.h>
103#include <dev/pci/pcireg.h>
104#include <dev/pci/pcidevs.h>
105
106#include <i386/pci/pci_intr_fixup.h>
107#include <i386/pci/piixvar.h>
108
109
110int ali1543_getclink(pciintr_icu_handle_t, int, int *);
111int ali1543_get_intr(pciintr_icu_handle_t, int, int *);
112int ali1543_set_intr(pciintr_icu_handle_t, int, int);
113
114
115const struct pciintr_icu ali1543_icu = {
116	ali1543_getclink,
117	ali1543_get_intr,
118	ali1543_set_intr,
119	piix_get_trigger,
120	piix_set_trigger,
121};
122
123
124/*
125 * Linux source code (linux/arch/i386/kernel/pci-irq.c) says that the
126 * irq order of ALi PCI ICU is shuffled.
127 */
128static const int ali1543_intr_shuffle_get[16] = {
129	0, 9, 3, 10, 4, 5, 7, 6, 1, 11, 0, 12, 0, 14, 0, 15
130};
131static const int ali1543_intr_shuffle_set[16] = {
132	0, 8, 0, 2, 4, 5, 7, 6, 0, 1, 3, 9, 11, 0, 13, 15
133};
134
135#define ALI1543_IRQ_MASK		0xdefa
136
137#define ALI1543_LEGAL_LINK(link)	(((link) >= 0) && ((link) <= 7))
138#define ALI1543_LEGAL_IRQ(irq)		((1 << (irq)) & ALI1543_IRQ_MASK)
139
140#define ALI1543_INTR_CFG_REG		0x48
141
142#define ALI1543_INTR_PIRQA_SHIFT	0
143#define ALI1543_INTR_PIRQA_MASK		0x0000000f
144#define ALI1543_INTR_PIRQB_SHIFT	4
145#define ALI1543_INTR_PIRQB_MASK		0x000000f0
146#define ALI1543_INTR_PIRQC_SHIFT	8
147#define ALI1543_INTR_PIRQC_MASK		0x00000f00
148#define ALI1543_INTR_PIRQD_SHIFT	12
149#define ALI1543_INTR_PIRQD_MASK		0x0000f000
150
151#define ALI1543_INTR_PIRQ_SHIFT(clink)	((clink)*4)
152#define ALI1543_INTR_PIRQ_IRQ(reg, clink)				\
153	(((reg) >> ((clink)*4)) & 0x0f)
154#define ALI1543_PIRQ(reg, clink)					\
155	ali1543_intr_shuffle_get[ALI1543_INTR_PIRQ_IRQ((reg), (clink))]
156
157
158int
159ali1543_init(pci_chipset_tag_t pc, bus_space_tag_t iot, pcitag_t tag,
160    pciintr_icu_tag_t *ptagp, pciintr_icu_handle_t *phandp)
161{
162
163	if (piix_init(pc, iot, tag, ptagp, phandp) == 0) {
164		*ptagp = &ali1543_icu;
165
166		return (0);
167	}
168
169	return (1);
170}
171
172int
173ali1543_getclink(pciintr_icu_handle_t v, int link, int *clinkp)
174{
175
176	if (ALI1543_LEGAL_LINK(link - 1)) {
177		*clinkp = link - 1;
178		return (0);
179	}
180
181	return (1);
182}
183
184int
185ali1543_get_intr(pciintr_icu_handle_t v, int clink, int *irqp)
186{
187	struct piix_handle *ph = v;
188	pcireg_t reg;
189	int val;
190
191	if (ALI1543_LEGAL_LINK(clink) == 0)
192		return (1);
193
194	reg = pci_conf_read(ph->ph_pc, ph->ph_tag, ALI1543_INTR_CFG_REG);
195#ifdef DEBUG_1543
196	printf("ali1543: PIRQ reg 0x%08x\n", reg); /* XXX debug */
197#endif /* DEBUG_1543 */
198	val = ALI1543_PIRQ(reg, clink);
199	*irqp = (val == 0) ?
200	    X86_PCI_INTERRUPT_LINE_NO_CONNECTION : val;
201
202	return (0);
203}
204
205int
206ali1543_set_intr(pciintr_icu_handle_t v, int clink, int irq)
207{
208	struct piix_handle *ph = v;
209	int shift, val;
210	pcireg_t reg;
211
212	if (ALI1543_LEGAL_LINK(clink) == 0 || ALI1543_LEGAL_IRQ(irq) == 0)
213		return (1);
214
215	reg = pci_conf_read(ph->ph_pc, ph->ph_tag, ALI1543_INTR_CFG_REG);
216	ali1543_get_intr(v, clink, &val);
217	shift = ALI1543_INTR_PIRQ_SHIFT(clink);
218	reg &= ~(0x0f << shift);
219	reg |= (ali1543_intr_shuffle_set[irq] << shift);
220	pci_conf_write(ph->ph_pc, ph->ph_tag, ALI1543_INTR_CFG_REG, reg);
221	if (ali1543_get_intr(v, clink, &val) != 0 || val != irq)
222		return (1);
223
224	return (0);
225}
226