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