pic_openpic.c revision 1.4 1 1.4 garbled /* $NetBSD: pic_openpic.c,v 1.4 2008/01/17 23:43:00 garbled 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 * 3. Neither the name of The NetBSD Foundation nor the names of its
16 1.2 garbled * contributors may be used to endorse or promote products derived
17 1.2 garbled * from this software without specific prior written permission.
18 1.2 garbled *
19 1.2 garbled * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.2 garbled * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.2 garbled * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.2 garbled * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.2 garbled * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.2 garbled * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.2 garbled * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.2 garbled * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.2 garbled * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.2 garbled * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.2 garbled * POSSIBILITY OF SUCH DAMAGE.
30 1.2 garbled */
31 1.2 garbled
32 1.2 garbled #include <sys/cdefs.h>
33 1.4 garbled __KERNEL_RCSID(0, "$NetBSD: pic_openpic.c,v 1.4 2008/01/17 23:43:00 garbled Exp $");
34 1.2 garbled
35 1.2 garbled #include <sys/param.h>
36 1.2 garbled #include <sys/malloc.h>
37 1.2 garbled #include <sys/kernel.h>
38 1.2 garbled
39 1.2 garbled #include <uvm/uvm_extern.h>
40 1.2 garbled
41 1.2 garbled #include <machine/pio.h>
42 1.2 garbled #include <powerpc/openpic.h>
43 1.2 garbled
44 1.2 garbled #include <arch/powerpc/pic/picvar.h>
45 1.2 garbled
46 1.2 garbled #include "opt_interrupt.h"
47 1.2 garbled
48 1.2 garbled static void opic_enable_irq(struct pic_ops *, int, int);
49 1.2 garbled static void opic_disable_irq(struct pic_ops *, int);
50 1.2 garbled static void opic_establish_irq(struct pic_ops*, int, int, int);
51 1.2 garbled
52 1.2 garbled struct pic_ops *
53 1.2 garbled setup_openpic(void *addr, int passthrough)
54 1.2 garbled {
55 1.4 garbled struct openpic_ops *opicops;
56 1.2 garbled struct pic_ops *pic;
57 1.2 garbled int irq;
58 1.2 garbled u_int x;
59 1.2 garbled
60 1.2 garbled openpic_base = (void *)addr;
61 1.4 garbled opicops = malloc(sizeof(struct openpic_ops), M_DEVBUF, M_NOWAIT);
62 1.4 garbled KASSERT(opicops != NULL);
63 1.4 garbled pic = &opicops->pic;
64 1.2 garbled
65 1.2 garbled x = openpic_read(OPENPIC_FEATURE);
66 1.4 garbled if (((x & 0x07ff0000) >> 16) == 0)
67 1.4 garbled panic("setup_openpic() called on distributed openpic");
68 1.4 garbled
69 1.2 garbled aprint_normal("OpenPIC Version 1.%d: "
70 1.2 garbled "Supports %d CPUs and %d interrupt sources.\n",
71 1.2 garbled x & 0xff, ((x & 0x1f00) >> 8) + 1, ((x & 0x07ff0000) >> 16) + 1);
72 1.2 garbled
73 1.2 garbled pic->pic_numintrs = ((x & 0x07ff0000) >> 16) + 1;
74 1.2 garbled pic->pic_cookie = addr;
75 1.2 garbled pic->pic_enable_irq = opic_enable_irq;
76 1.2 garbled pic->pic_reenable_irq = opic_enable_irq;
77 1.2 garbled pic->pic_disable_irq = opic_disable_irq;
78 1.2 garbled pic->pic_get_irq = opic_get_irq;
79 1.2 garbled pic->pic_ack_irq = opic_ack_irq;
80 1.2 garbled pic->pic_establish_irq = opic_establish_irq;
81 1.2 garbled pic->pic_finish_setup = opic_finish_setup;
82 1.4 garbled opicops->isu = NULL;
83 1.4 garbled opicops->nrofisus = 0; /* internal only */
84 1.4 garbled opicops->flags = 0; /* no flags (yet) */
85 1.4 garbled opicops->irq_per = NULL; /* internal ISU only */
86 1.2 garbled strcpy(pic->pic_name, "openpic");
87 1.2 garbled pic_add(pic);
88 1.2 garbled
89 1.2 garbled /*
90 1.2 garbled * the following sequence should make the same effects as openpic
91 1.2 garbled * controller reset by writing a one at the self-clearing
92 1.2 garbled * OPENPIC_CONFIG_RESET bit. Please check the document of your
93 1.2 garbled * OpenPIC compliant interrupt controller and see whether #else
94 1.2 garbled * portion can work as described.
95 1.2 garbled */
96 1.2 garbled #if 1
97 1.2 garbled openpic_set_priority(0, 15);
98 1.2 garbled
99 1.2 garbled for (irq = 0; irq < pic->pic_numintrs; irq++) {
100 1.2 garbled /* make sure to keep disabled */
101 1.2 garbled openpic_write(OPENPIC_SRC_VECTOR(irq), OPENPIC_IMASK);
102 1.2 garbled /* send all interrupts to CPU 0 */
103 1.2 garbled openpic_write(OPENPIC_IDEST(irq), 1 << 0);
104 1.2 garbled }
105 1.2 garbled
106 1.2 garbled x = openpic_read(OPENPIC_CONFIG);
107 1.2 garbled if (passthrough)
108 1.2 garbled x &= ~OPENPIC_CONFIG_8259_PASSTHRU_DISABLE;
109 1.2 garbled else
110 1.2 garbled x |= OPENPIC_CONFIG_8259_PASSTHRU_DISABLE;
111 1.2 garbled openpic_write(OPENPIC_CONFIG, x);
112 1.2 garbled
113 1.2 garbled openpic_write(OPENPIC_SPURIOUS_VECTOR, 0xff);
114 1.2 garbled
115 1.2 garbled openpic_set_priority(0, 0);
116 1.2 garbled
117 1.2 garbled /* clear all pending interrunts */
118 1.2 garbled for (irq = 0; irq < pic->pic_numintrs; irq++) {
119 1.2 garbled openpic_read_irq(0);
120 1.2 garbled openpic_eoi(0);
121 1.2 garbled }
122 1.2 garbled #else
123 1.2 garbled irq = 0;
124 1.2 garbled openpic_write(OPENPIC_CONFIG, OPENPIC_CONFIG_RESET);
125 1.2 garbled do {
126 1.2 garbled x = openpic_read(OPENPIC_CONFIG);
127 1.2 garbled } while (x & OPENPIC_CONFIG_RESET); /* S1C bit */
128 1.2 garbled if (passthrough)
129 1.2 garbled x &= ~OPENPIC_CONFIG_8259_PASSTHRU_DISABLE;
130 1.2 garbled else
131 1.2 garbled x |= OPENPIC_CONFIG_8259_PASSTHRU_DISABLE;
132 1.2 garbled openpic_write(OPENPIC_CONFIG, x);
133 1.2 garbled openpic_set_priority(0, 0);
134 1.2 garbled #endif
135 1.2 garbled
136 1.2 garbled #if 0
137 1.2 garbled printf("timebase freq=%d\n", openpic_read(0x10f0));
138 1.2 garbled #endif
139 1.2 garbled return pic;
140 1.2 garbled }
141 1.2 garbled
142 1.2 garbled static void
143 1.2 garbled opic_establish_irq(struct pic_ops *pic, int irq, int type, int pri)
144 1.2 garbled {
145 1.2 garbled int realpri = max(1, min(15, pri));
146 1.2 garbled uint32_t x;
147 1.2 garbled
148 1.2 garbled x = irq;
149 1.2 garbled x |= OPENPIC_IMASK;
150 1.2 garbled x |= (irq == 0) ?
151 1.2 garbled OPENPIC_POLARITY_POSITIVE : OPENPIC_POLARITY_NEGATIVE;
152 1.2 garbled x |= (type == IST_EDGE) ? OPENPIC_SENSE_EDGE : OPENPIC_SENSE_LEVEL;
153 1.2 garbled x |= realpri << OPENPIC_PRIORITY_SHIFT;
154 1.2 garbled openpic_write(OPENPIC_SRC_VECTOR(irq), x);
155 1.2 garbled
156 1.4 garbled aprint_debug("%s: setting IRQ %d to priority %d\n", __func__, irq,
157 1.4 garbled realpri);
158 1.2 garbled }
159 1.2 garbled
160 1.2 garbled static void
161 1.2 garbled opic_enable_irq(struct pic_ops *pic, int irq, int type)
162 1.2 garbled {
163 1.2 garbled u_int x;
164 1.2 garbled
165 1.2 garbled x = openpic_read(OPENPIC_SRC_VECTOR(irq));
166 1.2 garbled x &= ~OPENPIC_IMASK;
167 1.2 garbled openpic_write(OPENPIC_SRC_VECTOR(irq), x);
168 1.2 garbled }
169 1.2 garbled
170 1.2 garbled static void
171 1.2 garbled opic_disable_irq(struct pic_ops *pic, int irq)
172 1.2 garbled {
173 1.2 garbled u_int x;
174 1.2 garbled
175 1.2 garbled x = openpic_read(OPENPIC_SRC_VECTOR(irq));
176 1.2 garbled x |= OPENPIC_IMASK;
177 1.2 garbled openpic_write(OPENPIC_SRC_VECTOR(irq), x);
178 1.2 garbled }
179