pic_distopenpic.c revision 1.1.4.2 1 1.1.4.2 yamt /* $NetBSD: pic_distopenpic.c,v 1.1.4.2 2008/01/21 09:38:27 yamt Exp $ */
2 1.1.4.2 yamt
3 1.1.4.2 yamt /*-
4 1.1.4.2 yamt * Copyright (c) 2008 Tim Rightnour
5 1.1.4.2 yamt * All rights reserved.
6 1.1.4.2 yamt *
7 1.1.4.2 yamt * Redistribution and use in source and binary forms, with or without
8 1.1.4.2 yamt * modification, are permitted provided that the following conditions
9 1.1.4.2 yamt * are met:
10 1.1.4.2 yamt * 1. Redistributions of source code must retain the above copyright
11 1.1.4.2 yamt * notice, this list of conditions and the following disclaimer.
12 1.1.4.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.4.2 yamt * notice, this list of conditions and the following disclaimer in the
14 1.1.4.2 yamt * documentation and/or other materials provided with the distribution.
15 1.1.4.2 yamt * 3. Neither the name of The NetBSD Foundation nor the names of its
16 1.1.4.2 yamt * contributors may be used to endorse or promote products derived
17 1.1.4.2 yamt * from this software without specific prior written permission.
18 1.1.4.2 yamt *
19 1.1.4.2 yamt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1.4.2 yamt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1.4.2 yamt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1.4.2 yamt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1.4.2 yamt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1.4.2 yamt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1.4.2 yamt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1.4.2 yamt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1.4.2 yamt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1.4.2 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1.4.2 yamt * POSSIBILITY OF SUCH DAMAGE.
30 1.1.4.2 yamt */
31 1.1.4.2 yamt
32 1.1.4.2 yamt #include <sys/cdefs.h>
33 1.1.4.2 yamt __KERNEL_RCSID(0, "$NetBSD: pic_distopenpic.c,v 1.1.4.2 2008/01/21 09:38:27 yamt Exp $");
34 1.1.4.2 yamt
35 1.1.4.2 yamt #include <sys/param.h>
36 1.1.4.2 yamt #include <sys/malloc.h>
37 1.1.4.2 yamt #include <sys/kernel.h>
38 1.1.4.2 yamt
39 1.1.4.2 yamt #include <uvm/uvm_extern.h>
40 1.1.4.2 yamt
41 1.1.4.2 yamt #include <machine/pio.h>
42 1.1.4.2 yamt #include <powerpc/openpic.h>
43 1.1.4.2 yamt
44 1.1.4.2 yamt #include <arch/powerpc/pic/picvar.h>
45 1.1.4.2 yamt
46 1.1.4.2 yamt #include "opt_interrupt.h"
47 1.1.4.2 yamt
48 1.1.4.2 yamt /* distributed stuff */
49 1.1.4.2 yamt static int opic_isu_from_irq(struct openpic_ops *, int, int *);
50 1.1.4.2 yamt static u_int distopic_read(struct openpic_ops *, int, int);
51 1.1.4.2 yamt static void distopic_write(struct openpic_ops *, int, int, u_int);
52 1.1.4.2 yamt static void distopic_establish_irq(struct pic_ops *, int, int, int);
53 1.1.4.2 yamt static void distopic_enable_irq(struct pic_ops *, int, int);
54 1.1.4.2 yamt static void distopic_disable_irq(struct pic_ops *, int);
55 1.1.4.2 yamt
56 1.1.4.2 yamt struct pic_ops *
57 1.1.4.2 yamt setup_distributed_openpic(void *addr, int nrofisus, void **isu, int *maps)
58 1.1.4.2 yamt {
59 1.1.4.2 yamt struct openpic_ops *opicops;
60 1.1.4.2 yamt struct pic_ops *pic;
61 1.1.4.2 yamt int irq, i;
62 1.1.4.2 yamt u_int x;
63 1.1.4.2 yamt
64 1.1.4.2 yamt openpic_base = (void *)addr;
65 1.1.4.2 yamt opicops = malloc(sizeof(struct openpic_ops), M_DEVBUF, M_NOWAIT);
66 1.1.4.2 yamt KASSERT(opicops != NULL);
67 1.1.4.2 yamt pic = &opicops->pic;
68 1.1.4.2 yamt
69 1.1.4.2 yamt x = openpic_read(OPENPIC_FEATURE);
70 1.1.4.2 yamt if (((x & 0x07ff0000) >> 16) != 0)
71 1.1.4.2 yamt panic("Can't handle a distributed openpic with internal ISU");
72 1.1.4.2 yamt
73 1.1.4.2 yamt opicops->nrofisus = nrofisus;
74 1.1.4.2 yamt opicops->isu = malloc(sizeof(volatile unsigned char *) * nrofisus,
75 1.1.4.2 yamt M_DEVBUF, M_NOWAIT);
76 1.1.4.2 yamt KASSERT(opicops->isu != NULL);
77 1.1.4.2 yamt opicops->irq_per = malloc(sizeof(uint8_t) * nrofisus,
78 1.1.4.2 yamt M_DEVBUF, M_NOWAIT);
79 1.1.4.2 yamt KASSERT(opicops->irq_per != NULL);
80 1.1.4.2 yamt
81 1.1.4.2 yamt for (irq=0, i=0; i < nrofisus ; i++) {
82 1.1.4.2 yamt opicops->isu[i] = (void *)isu[i];
83 1.1.4.2 yamt opicops->irq_per[i] = maps[i]/0x20;
84 1.1.4.2 yamt irq += maps[i]/0x20;
85 1.1.4.2 yamt aprint_debug("%d: irqtotal=%d, added %d\n", i, irq,
86 1.1.4.2 yamt maps[i]/0x20);
87 1.1.4.2 yamt }
88 1.1.4.2 yamt aprint_normal("OpenPIC Version 1.%d: "
89 1.1.4.2 yamt "Supports %d CPUs and %d interrupt sources.\n",
90 1.1.4.2 yamt x & 0xff, ((x & 0x1f00) >> 8) + 1, irq);
91 1.1.4.2 yamt pic->pic_numintrs = irq;
92 1.1.4.2 yamt pic->pic_cookie = addr;
93 1.1.4.2 yamt pic->pic_enable_irq = distopic_enable_irq;
94 1.1.4.2 yamt pic->pic_reenable_irq = distopic_enable_irq;
95 1.1.4.2 yamt pic->pic_disable_irq = distopic_disable_irq;
96 1.1.4.2 yamt pic->pic_get_irq = opic_get_irq;
97 1.1.4.2 yamt pic->pic_ack_irq = opic_ack_irq;
98 1.1.4.2 yamt pic->pic_establish_irq = distopic_establish_irq;
99 1.1.4.2 yamt pic->pic_finish_setup = opic_finish_setup;
100 1.1.4.2 yamt opicops->flags = OPENPIC_FLAG_DIST;
101 1.1.4.2 yamt strcpy(pic->pic_name, "openpic");
102 1.1.4.2 yamt pic_add(pic);
103 1.1.4.2 yamt
104 1.1.4.2 yamt openpic_set_priority(0, 15);
105 1.1.4.2 yamt
106 1.1.4.2 yamt for (i=0; i < nrofisus; i++) {
107 1.1.4.2 yamt for (irq = 0; irq < opicops->irq_per[i]; irq++) {
108 1.1.4.2 yamt /* make sure to keep disabled */
109 1.1.4.2 yamt distopic_write(opicops, i,
110 1.1.4.2 yamt OPENPIC_DSRC_VECTOR_OFFSET(irq), OPENPIC_IMASK);
111 1.1.4.2 yamt /* send all interrupts to CPU 0 */
112 1.1.4.2 yamt distopic_write(opicops, i,
113 1.1.4.2 yamt OPENPIC_DSRC_IDEST_OFFSET(irq), 1 << 0);
114 1.1.4.2 yamt }
115 1.1.4.2 yamt }
116 1.1.4.2 yamt
117 1.1.4.2 yamt x = openpic_read(OPENPIC_CONFIG);
118 1.1.4.2 yamt x |= OPENPIC_CONFIG_8259_PASSTHRU_DISABLE;
119 1.1.4.2 yamt openpic_write(OPENPIC_CONFIG, x);
120 1.1.4.2 yamt
121 1.1.4.2 yamt openpic_write(OPENPIC_SPURIOUS_VECTOR, 0xff);
122 1.1.4.2 yamt
123 1.1.4.2 yamt openpic_set_priority(0, 0);
124 1.1.4.2 yamt
125 1.1.4.2 yamt /* clear all pending interrunts */
126 1.1.4.2 yamt for (irq = 0; irq < pic->pic_numintrs; irq++) {
127 1.1.4.2 yamt openpic_read_irq(0);
128 1.1.4.2 yamt openpic_eoi(0);
129 1.1.4.2 yamt }
130 1.1.4.2 yamt
131 1.1.4.2 yamt #if 0
132 1.1.4.2 yamt printf("timebase freq=%d\n", openpic_read(0x10f0));
133 1.1.4.2 yamt #endif
134 1.1.4.2 yamt return pic;
135 1.1.4.2 yamt
136 1.1.4.2 yamt }
137 1.1.4.2 yamt
138 1.1.4.2 yamt /* Begin distributed openpic code */
139 1.1.4.2 yamt
140 1.1.4.2 yamt static int
141 1.1.4.2 yamt opic_isu_from_irq(struct openpic_ops *opic, int irq, int *realirq)
142 1.1.4.2 yamt {
143 1.1.4.2 yamt int i;
144 1.1.4.2 yamt
145 1.1.4.2 yamt for (i=0; i < opic->nrofisus; i++) {
146 1.1.4.2 yamt if (irq < opic->irq_per[i]) {
147 1.1.4.2 yamt *realirq = irq;
148 1.1.4.2 yamt return i;
149 1.1.4.2 yamt } else
150 1.1.4.2 yamt irq -= opic->irq_per[i];
151 1.1.4.2 yamt }
152 1.1.4.2 yamt return -1;
153 1.1.4.2 yamt }
154 1.1.4.2 yamt
155 1.1.4.2 yamt static u_int
156 1.1.4.2 yamt distopic_read(struct openpic_ops *opic, int isu, int offset)
157 1.1.4.2 yamt {
158 1.1.4.2 yamt volatile unsigned char *addr = opic->isu[isu] + offset;
159 1.1.4.2 yamt
160 1.1.4.2 yamt return in32rb(addr);
161 1.1.4.2 yamt }
162 1.1.4.2 yamt
163 1.1.4.2 yamt static void
164 1.1.4.2 yamt distopic_write(struct openpic_ops *opic, int isu, int offset, u_int val)
165 1.1.4.2 yamt {
166 1.1.4.2 yamt volatile unsigned char *addr = opic->isu[isu] + offset;
167 1.1.4.2 yamt
168 1.1.4.2 yamt out32rb(addr, val);
169 1.1.4.2 yamt }
170 1.1.4.2 yamt
171 1.1.4.2 yamt static void
172 1.1.4.2 yamt distopic_establish_irq(struct pic_ops *pic, int irq, int type, int pri)
173 1.1.4.2 yamt {
174 1.1.4.2 yamt struct openpic_ops *opic = (struct openpic_ops *)pic;
175 1.1.4.2 yamt int isu, realirq, realpri = max(1, min(15, pri));
176 1.1.4.2 yamt uint32_t x;
177 1.1.4.2 yamt
178 1.1.4.2 yamt isu = opic_isu_from_irq(opic, irq, &realirq);
179 1.1.4.2 yamt KASSERT(isu != -1);
180 1.1.4.2 yamt
181 1.1.4.2 yamt x = irq;
182 1.1.4.2 yamt x |= OPENPIC_IMASK;
183 1.1.4.2 yamt x |= (realirq == 0 && isu == 0) ?
184 1.1.4.2 yamt OPENPIC_POLARITY_POSITIVE : OPENPIC_POLARITY_NEGATIVE;
185 1.1.4.2 yamt x |= (type == IST_EDGE) ? OPENPIC_SENSE_EDGE : OPENPIC_SENSE_LEVEL;
186 1.1.4.2 yamt x |= realpri << OPENPIC_PRIORITY_SHIFT;
187 1.1.4.2 yamt distopic_write(opic, isu, OPENPIC_DSRC_VECTOR_OFFSET(realirq), x);
188 1.1.4.2 yamt
189 1.1.4.2 yamt aprint_debug("%s: setting IRQ %d to priority %d 0x%x\n", __func__,
190 1.1.4.2 yamt irq, realpri, x);
191 1.1.4.2 yamt }
192 1.1.4.2 yamt
193 1.1.4.2 yamt static void
194 1.1.4.2 yamt distopic_enable_irq(struct pic_ops *pic, int irq, int type)
195 1.1.4.2 yamt {
196 1.1.4.2 yamt struct openpic_ops *opic = (struct openpic_ops *)pic;
197 1.1.4.2 yamt int isu, realirq;
198 1.1.4.2 yamt u_int x;
199 1.1.4.2 yamt
200 1.1.4.2 yamt isu = opic_isu_from_irq(opic, irq, &realirq);
201 1.1.4.2 yamt KASSERT(isu != -1);
202 1.1.4.2 yamt x = distopic_read(opic, isu, OPENPIC_DSRC_VECTOR_OFFSET(realirq));
203 1.1.4.2 yamt x &= ~OPENPIC_IMASK;
204 1.1.4.2 yamt distopic_write(opic, isu, OPENPIC_DSRC_VECTOR_OFFSET(realirq), x);
205 1.1.4.2 yamt }
206 1.1.4.2 yamt
207 1.1.4.2 yamt static void
208 1.1.4.2 yamt distopic_disable_irq(struct pic_ops *pic, int irq)
209 1.1.4.2 yamt {
210 1.1.4.2 yamt struct openpic_ops *opic = (struct openpic_ops *)pic;
211 1.1.4.2 yamt int isu, realirq;
212 1.1.4.2 yamt u_int x;
213 1.1.4.2 yamt
214 1.1.4.2 yamt isu = opic_isu_from_irq(opic, irq, &realirq);
215 1.1.4.2 yamt KASSERT(isu != -1);
216 1.1.4.2 yamt x = distopic_read(opic, isu, OPENPIC_DSRC_VECTOR_OFFSET(realirq));
217 1.1.4.2 yamt x |= OPENPIC_IMASK;
218 1.1.4.2 yamt distopic_write(opic, isu, OPENPIC_DSRC_VECTOR_OFFSET(realirq), x);
219 1.1.4.2 yamt }
220