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