pic_openpic.c revision 1.1.2.4 1 1.1.2.4 nisimura /* $NetBSD: pic_openpic.c,v 1.1.2.4 2007/05/03 16:00:15 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.4 nisimura __KERNEL_RCSID(0, "$NetBSD: pic_openpic.c,v 1.1.2.4 2007/05/03 16:00:15 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 <machine/autoconf.h>
45 1.1.2.1 macallan #include <arch/powerpc/pic/picvar.h>
46 1.1.2.1 macallan
47 1.1.2.1 macallan static int opic_irq_is_enabled(struct pic_ops *, int);
48 1.1.2.1 macallan static void opic_enable_irq(struct pic_ops *, int, int);
49 1.1.2.1 macallan static void opic_disable_irq(struct pic_ops *, int);
50 1.1.2.1 macallan static void opic_clear_irq(struct pic_ops *, int);
51 1.1.2.1 macallan static int opic_get_irq(struct pic_ops *);
52 1.1.2.1 macallan static void opic_ack_irq(struct pic_ops *, int);
53 1.1.2.1 macallan
54 1.1.2.1 macallan struct openpic_ops {
55 1.1.2.1 macallan struct pic_ops pic;
56 1.1.2.1 macallan uint32_t enable_mask;
57 1.1.2.1 macallan };
58 1.1.2.1 macallan
59 1.1.2.2 macallan struct pic_ops *
60 1.1.2.4 nisimura setup_openpic(uint32_t addr, int maxirq, int passthrough)
61 1.1.2.1 macallan {
62 1.1.2.1 macallan struct openpic_ops *openpic;
63 1.1.2.1 macallan struct pic_ops *pic;
64 1.1.2.1 macallan int irq;
65 1.1.2.1 macallan u_int x;
66 1.1.2.1 macallan
67 1.1.2.1 macallan openpic_base = (void *)addr;
68 1.1.2.1 macallan openpic = malloc(sizeof(struct openpic_ops), M_DEVBUF, M_NOWAIT);
69 1.1.2.1 macallan KASSERT(openpic != NULL);
70 1.1.2.1 macallan pic = &openpic->pic;
71 1.1.2.1 macallan
72 1.1.2.4 nisimura if (maxirq == -1)
73 1.1.2.4 nisimura maxirq = 1 + ((openpic_read(OPENPIC_FEATURE) >> 16) & 0x7ff);
74 1.1.2.4 nisimura
75 1.1.2.4 nisimura pic->pic_numintrs = maxirq;
76 1.1.2.1 macallan pic->pic_cookie = (void *)addr;
77 1.1.2.1 macallan pic->pic_irq_is_enabled = opic_irq_is_enabled;
78 1.1.2.1 macallan pic->pic_enable_irq = opic_enable_irq;
79 1.1.2.1 macallan pic->pic_reenable_irq = opic_enable_irq;
80 1.1.2.1 macallan pic->pic_disable_irq = opic_disable_irq;
81 1.1.2.1 macallan pic->pic_clear_irq = opic_clear_irq;
82 1.1.2.1 macallan pic->pic_get_irq = opic_get_irq;
83 1.1.2.1 macallan pic->pic_ack_irq = opic_ack_irq;
84 1.1.2.3 macallan pic->pic_establish_irq = dummy_pic_establish_intr;
85 1.1.2.1 macallan strcpy(pic->pic_name, "openpic");
86 1.1.2.1 macallan pic_add(pic);
87 1.1.2.1 macallan
88 1.1.2.1 macallan /* disable all interrupts */
89 1.1.2.4 nisimura for (irq = 0; irq < maxirq; irq++)
90 1.1.2.1 macallan openpic_write(OPENPIC_SRC_VECTOR(irq), OPENPIC_IMASK);
91 1.1.2.1 macallan
92 1.1.2.1 macallan openpic_set_priority(0, 15);
93 1.1.2.1 macallan
94 1.1.2.1 macallan x = openpic_read(OPENPIC_CONFIG);
95 1.1.2.1 macallan if (passthrough) {
96 1.1.2.1 macallan x &= ~OPENPIC_CONFIG_8259_PASSTHRU_DISABLE;
97 1.1.2.1 macallan } else {
98 1.1.2.1 macallan x |= OPENPIC_CONFIG_8259_PASSTHRU_DISABLE;
99 1.1.2.1 macallan }
100 1.1.2.1 macallan openpic_write(OPENPIC_CONFIG, x);
101 1.1.2.1 macallan
102 1.1.2.1 macallan /* send all interrupts to CPU 0 */
103 1.1.2.4 nisimura for (irq = 0; irq < maxirq; irq++)
104 1.1.2.1 macallan openpic_write(OPENPIC_IDEST(irq), 1 << 0);
105 1.1.2.1 macallan
106 1.1.2.4 nisimura for (irq = 0; irq < maxirq; irq++) {
107 1.1.2.1 macallan x = irq;
108 1.1.2.1 macallan x |= OPENPIC_IMASK;
109 1.1.2.1 macallan x |= OPENPIC_POLARITY_POSITIVE;
110 1.1.2.1 macallan x |= OPENPIC_SENSE_LEVEL;
111 1.1.2.1 macallan x |= 8 << OPENPIC_PRIORITY_SHIFT;
112 1.1.2.1 macallan openpic_write(OPENPIC_SRC_VECTOR(irq), x);
113 1.1.2.1 macallan }
114 1.1.2.1 macallan
115 1.1.2.1 macallan openpic_set_priority(0, 0);
116 1.1.2.1 macallan
117 1.1.2.1 macallan /* clear all pending interrunts */
118 1.1.2.1 macallan for (irq = 0; irq < 256; irq++) {
119 1.1.2.1 macallan openpic_read_irq(0);
120 1.1.2.1 macallan openpic_eoi(0);
121 1.1.2.1 macallan }
122 1.1.2.1 macallan
123 1.1.2.4 nisimura for (irq = 0; irq < maxirq; irq++)
124 1.1.2.1 macallan openpic_disable_irq(irq);
125 1.1.2.1 macallan
126 1.1.2.1 macallan #ifdef MULTIPROCESSOR
127 1.1.2.1 macallan x = openpic_read(OPENPIC_IPI_VECTOR(1));
128 1.1.2.1 macallan x &= ~(OPENPIC_IMASK | OPENPIC_PRIORITY_MASK | OPENPIC_VECTOR_MASK);
129 1.1.2.1 macallan x |= (15 << OPENPIC_PRIORITY_SHIFT) | IPI_VECTOR;
130 1.1.2.1 macallan openpic_write(OPENPIC_IPI_VECTOR(1), x);
131 1.1.2.1 macallan #endif
132 1.1.2.1 macallan
133 1.1.2.2 macallan return pic;
134 1.1.2.1 macallan }
135 1.1.2.1 macallan
136 1.1.2.1 macallan static int
137 1.1.2.1 macallan opic_irq_is_enabled(struct pic_ops *pic, int irq)
138 1.1.2.1 macallan {
139 1.1.2.1 macallan /* XXX */
140 1.1.2.1 macallan
141 1.1.2.1 macallan return 1;
142 1.1.2.1 macallan }
143 1.1.2.1 macallan
144 1.1.2.1 macallan static void
145 1.1.2.1 macallan opic_enable_irq(struct pic_ops *pic, int irq, int type)
146 1.1.2.1 macallan {
147 1.1.2.1 macallan
148 1.1.2.1 macallan openpic_enable_irq(irq, type);
149 1.1.2.1 macallan }
150 1.1.2.1 macallan
151 1.1.2.1 macallan static void
152 1.1.2.1 macallan opic_disable_irq(struct pic_ops *pic, int irq)
153 1.1.2.1 macallan {
154 1.1.2.1 macallan
155 1.1.2.1 macallan openpic_disable_irq(irq);
156 1.1.2.1 macallan }
157 1.1.2.1 macallan
158 1.1.2.1 macallan static void
159 1.1.2.1 macallan opic_clear_irq(struct pic_ops *pic, int irq)
160 1.1.2.1 macallan {
161 1.1.2.1 macallan
162 1.1.2.1 macallan /* we only use ack here */
163 1.1.2.1 macallan }
164 1.1.2.1 macallan
165 1.1.2.1 macallan static int
166 1.1.2.1 macallan opic_get_irq(struct pic_ops *pic)
167 1.1.2.1 macallan {
168 1.1.2.1 macallan
169 1.1.2.1 macallan return openpic_read_irq(cpu_number());
170 1.1.2.1 macallan }
171 1.1.2.1 macallan
172 1.1.2.1 macallan static void
173 1.1.2.1 macallan opic_ack_irq(struct pic_ops *pic, int irq)
174 1.1.2.1 macallan {
175 1.1.2.1 macallan
176 1.1.2.1 macallan openpic_eoi(cpu_number());
177 1.1.2.1 macallan }
178