xref: /netbsd-src/sys/arch/rs6000/rs6000/pic_iocc.c (revision c38e7cc395b1472a774ff828e46123de44c628e9)
1 /*	$NetBSD: pic_iocc.c,v 1.4 2011/07/18 17:26:56 dyoung Exp $	*/
2 
3 /*-
4  * Copyright (c) 2007 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Tim Rightnour
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
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_iocc.c,v 1.4 2011/07/18 17:26:56 dyoung Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/malloc.h>
37 #include <sys/kernel.h>
38 #include <sys/bus.h>
39 
40 #include <uvm/uvm_extern.h>
41 
42 #include <machine/pio.h>
43 #include <machine/intr.h>
44 #include <machine/iocc.h>
45 
46 #include <arch/powerpc/pic/picvar.h>
47 
48 struct pic_ops *setup_iocc(void);
49 
50 static void iocc_set_priority(int, int, int);
51 static int iocc_get_irq(struct pic_ops *, int);
52 static void iocc_enable_irq(struct pic_ops *, int, int);
53 static void iocc_ack_irq(struct pic_ops *, int);
54 static void iocc_disable_irq(struct pic_ops *, int);
55 
56 struct pic_ops *
57 setup_iocc(void)
58 {
59 	struct pic_ops *pic;
60 	int i;
61 
62 	pic = malloc(sizeof(struct pic_ops), M_DEVBUF, M_NOWAIT);
63         KASSERT(pic != NULL);
64 
65 	pic->pic_numintrs = 16;
66 	pic->pic_cookie = (void *)NULL;
67 	pic->pic_enable_irq = iocc_enable_irq;
68 	pic->pic_reenable_irq = iocc_enable_irq;
69 	pic->pic_disable_irq = iocc_disable_irq;
70 	pic->pic_get_irq = iocc_get_irq;
71 	pic->pic_ack_irq = iocc_ack_irq;
72 	pic->pic_establish_irq = dummy_pic_establish_intr;
73 	pic->pic_finish_setup = NULL;
74 	strcpy(pic->pic_name, "iocc");
75 	pic_add(pic);
76 
77 	/* set all priorities as high */
78 	for (i=0; i < pic->pic_numintrs; i++)
79 		iocc_set_priority(0, 0, i);
80 	return(pic);
81 }
82 
83 /*
84  * the IOCC IER is a bitmask of pending IRQs, where only 0-15 are valid
85  */
86 static int
87 iocc_get_irq(struct pic_ops *pic, int mode)
88 {
89         int irq;
90         uint32_t rv = 0;
91 
92         rv = in32rb(RS6000_BUS_SPACE_IO + IOCC_IRR);
93         if (rv == 0)
94                 return 255;
95 
96         irq = 31 - __builtin_clz(rv);
97         if (irq >= 0 && irq < 16)
98                 return irq;
99         return 255;
100 }
101 
102 /* enable an IRQ on the IOCC */
103 static void
104 iocc_enable_irq(struct pic_ops *pic, int irq, int type)
105 {
106         uint32_t mask;
107 
108         mask = in32rb(RS6000_BUS_SPACE_IO + IOCC_IEE);
109         mask |= 1 << irq;
110         out32rb(RS6000_BUS_SPACE_IO + IOCC_IEE, mask);
111 }
112 
113 /* disable an IRQ on the IOCC */
114 static void
115 iocc_disable_irq(struct pic_ops *pic, int irq)
116 {
117         uint32_t mask;
118 
119         mask = in32rb(RS6000_BUS_SPACE_IO + IOCC_IEE);
120         mask &= ~(1 << irq);
121         out32rb(RS6000_BUS_SPACE_IO + IOCC_IEE, mask);
122 }
123 
124 /* Issue an EOI to the IOCC */
125 static void
126 iocc_ack_irq(struct pic_ops *pic, int irq)
127 {
128         out32rb(RS6000_BUS_SPACE_IO + IOCC_EOI(irq), 0x1); /* val is ignored */
129 }
130 
131 /* set interrupt priority and destination */
132 static void
133 iocc_set_priority(int cpu, int pri, int irq)
134 {
135         uint32_t x;
136 
137         x = pri;
138         x |= cpu<<8;
139         out32rb(RS6000_BUS_SPACE_IO + IOCC_XIVR(irq), x);
140 }
141