1 /* $NetBSD: pic_prepivr.c,v 1.9 2017/06/01 02:45:07 chs 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_prepivr.c,v 1.9 2017/06/01 02:45:07 chs Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/kmem.h>
37 #include <sys/kernel.h>
38 #include <sys/intr.h>
39 
40 #include <uvm/uvm_extern.h>
41 
42 #include <machine/pio.h>
43 
44 #include <powerpc/pic/picvar.h>
45 
46 #include <dev/isa/isareg.h>
47 #include <dev/isa/isavar.h>
48 
49 static int  prepivr_get_irq(struct pic_ops *, int);
50 static int  motivr_get_irq(struct pic_ops *, int);
51 static void prepivr_establish_irq(struct pic_ops *, int, int, int);
52 
53 extern vaddr_t prep_intr_reg; /* PReP interrupt vector register */
54 extern uint32_t prep_intr_reg_off; /* IVR offset within the mapped page */
55 
56 #define IO_ELCR1    0x4d0
57 #define IO_ELCR2    0x4d1
58 
59 /*
60  * Prior to calling init_prepivr, the port is expected to have mapped the
61  * page containing the IVR with mapiodev to prep_intr_reg and set up the
62  * prep_intr_reg_off.
63  */
64 
65 struct pic_ops *
setup_prepivr(int ivrtype)66 setup_prepivr(int ivrtype)
67 {
68           struct i8259_ops *prepivr;
69           struct pic_ops *pic;
70           uint32_t pivr;
71 
72           prepivr = kmem_alloc(sizeof(*prepivr), KM_SLEEP);
73           pic = &prepivr->pic;
74 
75           pivr = prep_intr_reg + prep_intr_reg_off;
76           pic->pic_numintrs = 16;
77           pic->pic_cookie = (void *)pivr;
78           pic->pic_enable_irq = i8259_enable_irq;
79           pic->pic_reenable_irq = i8259_enable_irq;
80           pic->pic_disable_irq = i8259_disable_irq;
81           if (ivrtype == PIC_IVR_MOT)
82                     pic->pic_get_irq = motivr_get_irq;
83           else
84                     pic->pic_get_irq = prepivr_get_irq;
85           pic->pic_ack_irq = i8259_ack_irq;
86           pic->pic_establish_irq = prepivr_establish_irq;
87           pic->pic_finish_setup = NULL;
88           strcpy(pic->pic_name, "prepivr");
89           pic_add(pic);
90           prepivr->pending_events = 0;
91           prepivr->enable_mask = 0xffffffff;
92           prepivr->irqs = 0;
93 
94           /* initialize the ELCR */
95           isa_outb(IO_ELCR1, (0 >> 0) & 0xff);
96           isa_outb(IO_ELCR2, (0 >> 8) & 0xff);
97           i8259_initialize();
98 
99           return pic;
100 }
101 
102 static void
prepivr_establish_irq(struct pic_ops * pic,int irq,int type,int maxlevel)103 prepivr_establish_irq(struct pic_ops *pic, int irq, int type, int maxlevel)
104 {
105           u_int8_t elcr[2];
106           int icu, bit;
107 
108           icu = irq / 8;
109           bit = irq % 8;
110           elcr[0] = isa_inb(IO_ELCR1);
111           elcr[1] = isa_inb(IO_ELCR2);
112 
113           if (type == IST_LEVEL)
114                     elcr[icu] |= 1 << bit;
115           else
116                     elcr[icu] &= ~(1 << bit);
117 
118           isa_outb(IO_ELCR1, elcr[0]);
119           isa_outb(IO_ELCR2, elcr[1]);
120 }
121 
122 static int
motivr_get_irq(struct pic_ops * pic,int mode)123 motivr_get_irq(struct pic_ops *pic, int mode)
124 {
125           int irq;
126 
127           irq = i8259_get_irq(pic, mode);
128           /* read the IVR to ack it */
129           (void)inb(pic->pic_cookie);
130 
131           /* i8259_get_irq will return 255 by itself, so just pass it on */
132           return irq;
133 }
134 
135 static int
prepivr_get_irq(struct pic_ops * pic,int mode)136 prepivr_get_irq(struct pic_ops *pic, int mode)
137 {
138           int irq;
139 
140           irq = inb(pic->pic_cookie);
141 
142           if (irq == 0 && mode == PIC_GET_IRQ)
143                     return 255;
144           if (irq == 7 && mode == PIC_GET_RECHECK)
145                     return 255;
146 
147           return irq;
148 }
149