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