xref: /netbsd-src/sys/arch/rs6000/rs6000/pic_iocc.c (revision d206c3458b7848902ac12dfb5de8f3a16c6f4067)
1 /*	$NetBSD: pic_iocc.c,v 1.6 2020/11/21 15:52:32 thorpej 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.6 2020/11/21 15:52:32 thorpej Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/kmem.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 *
setup_iocc(void)57 setup_iocc(void)
58 {
59 	struct pic_ops *pic;
60 	int i;
61 
62 	pic = kmem_alloc(sizeof(struct pic_ops), KM_SLEEP);
63 	pic->pic_numintrs = 16;
64 	pic->pic_cookie = (void *)NULL;
65 	pic->pic_enable_irq = iocc_enable_irq;
66 	pic->pic_reenable_irq = iocc_enable_irq;
67 	pic->pic_disable_irq = iocc_disable_irq;
68 	pic->pic_get_irq = iocc_get_irq;
69 	pic->pic_ack_irq = iocc_ack_irq;
70 	pic->pic_establish_irq = dummy_pic_establish_intr;
71 	pic->pic_finish_setup = NULL;
72 	strcpy(pic->pic_name, "iocc");
73 	pic_add(pic);
74 
75 	/* set all priorities as high */
76 	for (i=0; i < pic->pic_numintrs; i++)
77 		iocc_set_priority(0, 0, i);
78 	return(pic);
79 }
80 
81 /*
82  * the IOCC IER is a bitmask of pending IRQs, where only 0-15 are valid
83  */
84 static int
iocc_get_irq(struct pic_ops * pic,int mode)85 iocc_get_irq(struct pic_ops *pic, int mode)
86 {
87         int irq;
88         uint32_t rv = 0;
89 
90         rv = in32rb(RS6000_BUS_SPACE_IO + IOCC_IRR);
91         if (rv == 0)
92                 return 255;
93 
94         irq = 31 - __builtin_clz(rv);
95         if (irq >= 0 && irq < 16)
96                 return irq;
97         return 255;
98 }
99 
100 /* enable an IRQ on the IOCC */
101 static void
iocc_enable_irq(struct pic_ops * pic,int irq,int type)102 iocc_enable_irq(struct pic_ops *pic, int irq, int type)
103 {
104         uint32_t mask;
105 
106         mask = in32rb(RS6000_BUS_SPACE_IO + IOCC_IEE);
107         mask |= 1 << irq;
108         out32rb(RS6000_BUS_SPACE_IO + IOCC_IEE, mask);
109 }
110 
111 /* disable an IRQ on the IOCC */
112 static void
iocc_disable_irq(struct pic_ops * pic,int irq)113 iocc_disable_irq(struct pic_ops *pic, int irq)
114 {
115         uint32_t mask;
116 
117         mask = in32rb(RS6000_BUS_SPACE_IO + IOCC_IEE);
118         mask &= ~(1 << irq);
119         out32rb(RS6000_BUS_SPACE_IO + IOCC_IEE, mask);
120 }
121 
122 /* Issue an EOI to the IOCC */
123 static void
iocc_ack_irq(struct pic_ops * pic,int irq)124 iocc_ack_irq(struct pic_ops *pic, int irq)
125 {
126         out32rb(RS6000_BUS_SPACE_IO + IOCC_EOI(irq), 0x1); /* val is ignored */
127 }
128 
129 /* set interrupt priority and destination */
130 static void
iocc_set_priority(int cpu,int pri,int irq)131 iocc_set_priority(int cpu, int pri, int irq)
132 {
133         uint32_t x;
134 
135         x = pri;
136         x |= cpu<<8;
137         out32rb(RS6000_BUS_SPACE_IO + IOCC_XIVR(irq), x);
138 }
139