Home | History | Annotate | Line # | Download | only in pic
pic_openpic.c revision 1.1.2.7
      1 /*	$NetBSD: pic_openpic.c,v 1.1.2.7 2007/05/04 02:54:33 macallan 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.1.2.7 2007/05/04 02:54:33 macallan 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 static void openpic_set_priority(int cpu, int pri);
     47 static void opic_enable_irq(struct pic_ops *, int, int);
     48 static void opic_disable_irq(struct pic_ops *, int);
     49 static int  opic_get_irq(struct pic_ops *);
     50 static void opic_ack_irq(struct pic_ops *, int);
     51 static void opic_establish_irq(struct pic_ops*, int, int, int);
     52 
     53 struct openpic_ops {
     54 	struct pic_ops pic;
     55 	uint32_t enable_mask;
     56 };
     57 
     58 volatile unsigned char *openpic_base;
     59 
     60 struct pic_ops *
     61 setup_openpic(void *addr, int passthrough)
     62 {
     63 	struct openpic_ops *openpic;
     64 	struct pic_ops *pic;
     65 	int irq;
     66 	u_int x;
     67 
     68 	openpic_base = (void *)addr;
     69 	openpic = malloc(sizeof(struct openpic_ops), M_DEVBUF, M_NOWAIT);
     70 	KASSERT(openpic != NULL);
     71 	pic = &openpic->pic;
     72 
     73 	x = openpic_read(OPENPIC_FEATURE);
     74 	aprint_normal("OpenPIC Version 1.%d: "
     75 	    "Supports %d CPUs and %d interrupt sources.\n",
     76 	    x & 0xff, ((x & 0x1f00) >> 8) + 1, ((x & 0x07ff0000) >> 16) + 1);
     77 
     78 	pic->pic_numintrs = ((x & 0x07ff0000) >> 16) + 1;
     79 	pic->pic_cookie = addr;
     80 	pic->pic_enable_irq = opic_enable_irq;
     81 	pic->pic_reenable_irq = opic_enable_irq;
     82 	pic->pic_disable_irq = opic_disable_irq;
     83 	pic->pic_get_irq = opic_get_irq;
     84 	pic->pic_ack_irq = opic_ack_irq;
     85 	pic->pic_establish_irq = opic_establish_irq;
     86 	strcpy(pic->pic_name, "openpic");
     87 	pic_add(pic);
     88 
     89 	/* disable all interrupts */
     90 	for (irq = 0; irq < pic->pic_numintrs; irq++)
     91 		openpic_write(OPENPIC_SRC_VECTOR(irq), OPENPIC_IMASK);
     92 
     93 	openpic_set_priority(0, 15);
     94 
     95 	x = openpic_read(OPENPIC_CONFIG);
     96 	if (passthrough) {
     97 		x &= ~OPENPIC_CONFIG_8259_PASSTHRU_DISABLE;
     98 	} else {
     99 		x |= OPENPIC_CONFIG_8259_PASSTHRU_DISABLE;
    100 	}
    101 	openpic_write(OPENPIC_CONFIG, x);
    102 
    103 	for (irq = 0; irq < pic->pic_numintrs; irq++) {
    104 		x = irq;
    105 		x |= OPENPIC_IMASK;
    106 		x |= (irq == 0) ?
    107 		    OPENPIC_POLARITY_POSITIVE :	OPENPIC_POLARITY_NEGATIVE;
    108 		x |= OPENPIC_SENSE_LEVEL;
    109 		x |= 8 << OPENPIC_PRIORITY_SHIFT;
    110 		openpic_write(OPENPIC_SRC_VECTOR(irq), x);
    111 		/* send all interrupts to CPU 0 */
    112 		openpic_write(OPENPIC_IDEST(irq), 1 << 0);
    113 	}
    114 
    115 	openpic_write(OPENPIC_SPURIOUS_VECTOR, 0xff);
    116 
    117 	openpic_set_priority(0, 0);
    118 
    119 	/* clear all pending interrunts */
    120 	for (irq = 0; irq < pic->pic_numintrs; irq++) {
    121 		openpic_read_irq(0);
    122 		openpic_eoi(0);
    123 	}
    124 
    125 	for (irq = 0; irq < pic->pic_numintrs; irq++)
    126 		opic_disable_irq(pic, irq);
    127 
    128 #ifdef MULTIPROCESSOR
    129 	x = openpic_read(OPENPIC_IPI_VECTOR(1));
    130 	x &= ~(OPENPIC_IMASK | OPENPIC_PRIORITY_MASK | OPENPIC_VECTOR_MASK);
    131 	x |= (15 << OPENPIC_PRIORITY_SHIFT) | IPI_VECTOR;
    132 	openpic_write(OPENPIC_IPI_VECTOR(1), x);
    133 #endif
    134 
    135 	return pic;
    136 }
    137 static void
    138 opic_establish_irq(struct pic_ops *pic, int irq, int type, int pri)
    139 {
    140 	int realpri = max(1, min(15, pri));
    141 	uint32_t x;
    142 
    143 	x = irq;
    144 	x |= OPENPIC_IMASK;
    145 	x |= (irq == 0) ?
    146 	    OPENPIC_POLARITY_POSITIVE :	OPENPIC_POLARITY_NEGATIVE;
    147 	x |= (type == IST_EDGE) ? OPENPIC_SENSE_EDGE : OPENPIC_SENSE_LEVEL;
    148 	x |= realpri << OPENPIC_PRIORITY_SHIFT;
    149 	openpic_write(OPENPIC_SRC_VECTOR(irq), x);
    150 	/* send all interrupts to CPU 0 */
    151 	openpic_write(OPENPIC_IDEST(irq), 1 << 0);
    152 	aprint_debug("%s: setting IRQ %d to priority %d\n", __func__, irq, realpri);
    153 }
    154 
    155 static void
    156 openpic_set_priority(int cpu, int pri)
    157 {
    158 	u_int x;
    159 
    160 	x = openpic_read(OPENPIC_CPU_PRIORITY(cpu));
    161 	x &= ~OPENPIC_CPU_PRIORITY_MASK;
    162 	x |= pri;
    163 	openpic_write(OPENPIC_CPU_PRIORITY(cpu), x);
    164 }
    165 
    166 static void
    167 opic_enable_irq(struct pic_ops *pic, int irq, int type)
    168 {
    169 	u_int x;
    170 
    171 	x = openpic_read(OPENPIC_SRC_VECTOR(irq));
    172 	x &= ~(OPENPIC_IMASK | OPENPIC_SENSE_LEVEL | OPENPIC_SENSE_EDGE);
    173 	if (type == IST_LEVEL)
    174 		x |= OPENPIC_SENSE_LEVEL;
    175 	else
    176 		x |= OPENPIC_SENSE_EDGE;
    177 	openpic_write(OPENPIC_SRC_VECTOR(irq), x);
    178 }
    179 
    180 static void
    181 opic_disable_irq(struct pic_ops *pic, int irq)
    182 {
    183 	u_int x;
    184 
    185 	x = openpic_read(OPENPIC_SRC_VECTOR(irq));
    186 	x |= OPENPIC_IMASK;
    187 	openpic_write(OPENPIC_SRC_VECTOR(irq), x);
    188 }
    189 
    190 static int
    191 opic_get_irq(struct pic_ops *pic)
    192 {
    193 
    194 	return openpic_read_irq(cpu_number());
    195 }
    196 
    197 static void
    198 opic_ack_irq(struct pic_ops *pic, int irq)
    199 {
    200 
    201 	openpic_eoi(cpu_number());
    202 }
    203