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