pic_openpic.c revision 1.1.2.8 1 1.1.2.8 nisimura /* $NetBSD: pic_openpic.c,v 1.1.2.8 2007/06/03 06:49:44 nisimura Exp $ */
2 1.1.2.1 macallan
3 1.1.2.1 macallan /*-
4 1.1.2.1 macallan * Copyright (c) 2007 Michael Lorenz
5 1.1.2.1 macallan * All rights reserved.
6 1.1.2.1 macallan *
7 1.1.2.1 macallan * Redistribution and use in source and binary forms, with or without
8 1.1.2.1 macallan * modification, are permitted provided that the following conditions
9 1.1.2.1 macallan * are met:
10 1.1.2.1 macallan * 1. Redistributions of source code must retain the above copyright
11 1.1.2.1 macallan * notice, this list of conditions and the following disclaimer.
12 1.1.2.1 macallan * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.2.1 macallan * notice, this list of conditions and the following disclaimer in the
14 1.1.2.1 macallan * documentation and/or other materials provided with the distribution.
15 1.1.2.1 macallan * 3. Neither the name of The NetBSD Foundation nor the names of its
16 1.1.2.1 macallan * contributors may be used to endorse or promote products derived
17 1.1.2.1 macallan * from this software without specific prior written permission.
18 1.1.2.1 macallan *
19 1.1.2.1 macallan * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1.2.1 macallan * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1.2.1 macallan * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1.2.1 macallan * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1.2.1 macallan * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1.2.1 macallan * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1.2.1 macallan * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1.2.1 macallan * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1.2.1 macallan * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1.2.1 macallan * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1.2.1 macallan * POSSIBILITY OF SUCH DAMAGE.
30 1.1.2.1 macallan */
31 1.1.2.1 macallan
32 1.1.2.1 macallan #include <sys/cdefs.h>
33 1.1.2.8 nisimura __KERNEL_RCSID(0, "$NetBSD: pic_openpic.c,v 1.1.2.8 2007/06/03 06:49:44 nisimura Exp $");
34 1.1.2.1 macallan
35 1.1.2.1 macallan #include <sys/param.h>
36 1.1.2.1 macallan #include <sys/malloc.h>
37 1.1.2.1 macallan #include <sys/kernel.h>
38 1.1.2.1 macallan
39 1.1.2.1 macallan #include <uvm/uvm_extern.h>
40 1.1.2.1 macallan
41 1.1.2.1 macallan #include <machine/pio.h>
42 1.1.2.1 macallan #include <powerpc/openpic.h>
43 1.1.2.1 macallan
44 1.1.2.1 macallan #include <arch/powerpc/pic/picvar.h>
45 1.1.2.1 macallan
46 1.1.2.6 garbled static void openpic_set_priority(int cpu, int pri);
47 1.1.2.1 macallan static void opic_enable_irq(struct pic_ops *, int, int);
48 1.1.2.1 macallan static void opic_disable_irq(struct pic_ops *, int);
49 1.1.2.1 macallan static int opic_get_irq(struct pic_ops *);
50 1.1.2.1 macallan static void opic_ack_irq(struct pic_ops *, int);
51 1.1.2.7 macallan static void opic_establish_irq(struct pic_ops*, int, int, int);
52 1.1.2.1 macallan
53 1.1.2.6 garbled volatile unsigned char *openpic_base;
54 1.1.2.6 garbled
55 1.1.2.2 macallan struct pic_ops *
56 1.1.2.7 macallan setup_openpic(void *addr, int passthrough)
57 1.1.2.1 macallan {
58 1.1.2.1 macallan struct pic_ops *pic;
59 1.1.2.1 macallan int irq;
60 1.1.2.1 macallan u_int x;
61 1.1.2.1 macallan
62 1.1.2.1 macallan openpic_base = (void *)addr;
63 1.1.2.8 nisimura pic = malloc(sizeof(struct pic_ops), M_DEVBUF, M_NOWAIT);
64 1.1.2.8 nisimura KASSERT(pic != NULL);
65 1.1.2.1 macallan
66 1.1.2.6 garbled x = openpic_read(OPENPIC_FEATURE);
67 1.1.2.6 garbled aprint_normal("OpenPIC Version 1.%d: "
68 1.1.2.6 garbled "Supports %d CPUs and %d interrupt sources.\n",
69 1.1.2.6 garbled x & 0xff, ((x & 0x1f00) >> 8) + 1, ((x & 0x07ff0000) >> 16) + 1);
70 1.1.2.4 nisimura
71 1.1.2.6 garbled pic->pic_numintrs = ((x & 0x07ff0000) >> 16) + 1;
72 1.1.2.7 macallan pic->pic_cookie = addr;
73 1.1.2.1 macallan pic->pic_enable_irq = opic_enable_irq;
74 1.1.2.1 macallan pic->pic_reenable_irq = opic_enable_irq;
75 1.1.2.1 macallan pic->pic_disable_irq = opic_disable_irq;
76 1.1.2.1 macallan pic->pic_get_irq = opic_get_irq;
77 1.1.2.1 macallan pic->pic_ack_irq = opic_ack_irq;
78 1.1.2.7 macallan pic->pic_establish_irq = opic_establish_irq;
79 1.1.2.1 macallan strcpy(pic->pic_name, "openpic");
80 1.1.2.1 macallan pic_add(pic);
81 1.1.2.1 macallan
82 1.1.2.8 nisimura /*
83 1.1.2.8 nisimura * the following sequence should make the same effects as openpic
84 1.1.2.8 nisimura * controller reset by writing a one at the self-clearing
85 1.1.2.8 nisimura * OPENPIC_CONFIG_RESET bit. Please check the document of your
86 1.1.2.8 nisimura * OpenPIC compliant interrupt controller and see whether #else
87 1.1.2.8 nisimura * portion can work as described.
88 1.1.2.8 nisimura */
89 1.1.2.8 nisimura #if 1
90 1.1.2.1 macallan openpic_set_priority(0, 15);
91 1.1.2.1 macallan
92 1.1.2.6 garbled for (irq = 0; irq < pic->pic_numintrs; irq++) {
93 1.1.2.8 nisimura /* make sure to keep disabled */
94 1.1.2.8 nisimura openpic_write(OPENPIC_SRC_VECTOR(irq), OPENPIC_IMASK);
95 1.1.2.6 garbled /* send all interrupts to CPU 0 */
96 1.1.2.6 garbled openpic_write(OPENPIC_IDEST(irq), 1 << 0);
97 1.1.2.1 macallan }
98 1.1.2.1 macallan
99 1.1.2.8 nisimura x = openpic_read(OPENPIC_CONFIG);
100 1.1.2.8 nisimura if (passthrough)
101 1.1.2.8 nisimura x &= ~OPENPIC_CONFIG_8259_PASSTHRU_DISABLE;
102 1.1.2.8 nisimura else
103 1.1.2.8 nisimura x |= OPENPIC_CONFIG_8259_PASSTHRU_DISABLE;
104 1.1.2.8 nisimura openpic_write(OPENPIC_CONFIG, x);
105 1.1.2.8 nisimura
106 1.1.2.6 garbled openpic_write(OPENPIC_SPURIOUS_VECTOR, 0xff);
107 1.1.2.6 garbled
108 1.1.2.1 macallan openpic_set_priority(0, 0);
109 1.1.2.1 macallan
110 1.1.2.1 macallan /* clear all pending interrunts */
111 1.1.2.6 garbled for (irq = 0; irq < pic->pic_numintrs; irq++) {
112 1.1.2.1 macallan openpic_read_irq(0);
113 1.1.2.1 macallan openpic_eoi(0);
114 1.1.2.1 macallan }
115 1.1.2.8 nisimura #else
116 1.1.2.8 nisimura irq = 0;
117 1.1.2.8 nisimura openpic_write(OPENPIC_CONFIG, OPENPIC_CONFIG_RESET);
118 1.1.2.8 nisimura do {
119 1.1.2.8 nisimura x = openpic_read(OPENPIC_CONFIG);
120 1.1.2.8 nisimura } while (x & OPENPIC_CONFIG_RESET); /* S1C bit */
121 1.1.2.8 nisimura if (passthrough)
122 1.1.2.8 nisimura x &= ~OPENPIC_CONFIG_8259_PASSTHRU_DISABLE;
123 1.1.2.8 nisimura else
124 1.1.2.8 nisimura x |= OPENPIC_CONFIG_8259_PASSTHRU_DISABLE;
125 1.1.2.8 nisimura openpic_write(OPENPIC_CONFIG, x);
126 1.1.2.8 nisimura openpic_set_priority(0, 0);
127 1.1.2.8 nisimura #endif
128 1.1.2.1 macallan
129 1.1.2.1 macallan #ifdef MULTIPROCESSOR
130 1.1.2.1 macallan x = openpic_read(OPENPIC_IPI_VECTOR(1));
131 1.1.2.1 macallan x &= ~(OPENPIC_IMASK | OPENPIC_PRIORITY_MASK | OPENPIC_VECTOR_MASK);
132 1.1.2.1 macallan x |= (15 << OPENPIC_PRIORITY_SHIFT) | IPI_VECTOR;
133 1.1.2.1 macallan openpic_write(OPENPIC_IPI_VECTOR(1), x);
134 1.1.2.1 macallan #endif
135 1.1.2.1 macallan
136 1.1.2.2 macallan return pic;
137 1.1.2.1 macallan }
138 1.1.2.8 nisimura
139 1.1.2.7 macallan static void
140 1.1.2.7 macallan opic_establish_irq(struct pic_ops *pic, int irq, int type, int pri)
141 1.1.2.7 macallan {
142 1.1.2.7 macallan int realpri = max(1, min(15, pri));
143 1.1.2.7 macallan uint32_t x;
144 1.1.2.7 macallan
145 1.1.2.7 macallan x = irq;
146 1.1.2.7 macallan x |= OPENPIC_IMASK;
147 1.1.2.7 macallan x |= (irq == 0) ?
148 1.1.2.7 macallan OPENPIC_POLARITY_POSITIVE : OPENPIC_POLARITY_NEGATIVE;
149 1.1.2.7 macallan x |= (type == IST_EDGE) ? OPENPIC_SENSE_EDGE : OPENPIC_SENSE_LEVEL;
150 1.1.2.7 macallan x |= realpri << OPENPIC_PRIORITY_SHIFT;
151 1.1.2.7 macallan openpic_write(OPENPIC_SRC_VECTOR(irq), x);
152 1.1.2.7 macallan /* send all interrupts to CPU 0 */
153 1.1.2.7 macallan openpic_write(OPENPIC_IDEST(irq), 1 << 0);
154 1.1.2.7 macallan aprint_debug("%s: setting IRQ %d to priority %d\n", __func__, irq, realpri);
155 1.1.2.7 macallan }
156 1.1.2.1 macallan
157 1.1.2.6 garbled static void
158 1.1.2.6 garbled openpic_set_priority(int cpu, int pri)
159 1.1.2.6 garbled {
160 1.1.2.6 garbled u_int x;
161 1.1.2.6 garbled
162 1.1.2.6 garbled x = openpic_read(OPENPIC_CPU_PRIORITY(cpu));
163 1.1.2.6 garbled x &= ~OPENPIC_CPU_PRIORITY_MASK;
164 1.1.2.6 garbled x |= pri;
165 1.1.2.6 garbled openpic_write(OPENPIC_CPU_PRIORITY(cpu), x);
166 1.1.2.6 garbled }
167 1.1.2.6 garbled
168 1.1.2.1 macallan static void
169 1.1.2.1 macallan opic_enable_irq(struct pic_ops *pic, int irq, int type)
170 1.1.2.1 macallan {
171 1.1.2.6 garbled u_int x;
172 1.1.2.1 macallan
173 1.1.2.6 garbled x = openpic_read(OPENPIC_SRC_VECTOR(irq));
174 1.1.2.8 nisimura x &= ~OPENPIC_IMASK;
175 1.1.2.6 garbled openpic_write(OPENPIC_SRC_VECTOR(irq), x);
176 1.1.2.1 macallan }
177 1.1.2.1 macallan
178 1.1.2.1 macallan static void
179 1.1.2.1 macallan opic_disable_irq(struct pic_ops *pic, int irq)
180 1.1.2.1 macallan {
181 1.1.2.6 garbled u_int x;
182 1.1.2.1 macallan
183 1.1.2.6 garbled x = openpic_read(OPENPIC_SRC_VECTOR(irq));
184 1.1.2.6 garbled x |= OPENPIC_IMASK;
185 1.1.2.6 garbled openpic_write(OPENPIC_SRC_VECTOR(irq), x);
186 1.1.2.1 macallan }
187 1.1.2.1 macallan
188 1.1.2.1 macallan static int
189 1.1.2.1 macallan opic_get_irq(struct pic_ops *pic)
190 1.1.2.1 macallan {
191 1.1.2.1 macallan
192 1.1.2.1 macallan return openpic_read_irq(cpu_number());
193 1.1.2.1 macallan }
194 1.1.2.1 macallan
195 1.1.2.1 macallan static void
196 1.1.2.1 macallan opic_ack_irq(struct pic_ops *pic, int irq)
197 1.1.2.1 macallan {
198 1.1.2.1 macallan
199 1.1.2.1 macallan openpic_eoi(cpu_number());
200 1.1.2.1 macallan }
201