Home | History | Annotate | Line # | Download | only in pic
pic_openpic.c revision 1.1.2.2
      1 /*	$NetBSD: pic_openpic.c,v 1.1.2.2 2007/05/03 03:18: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.2 2007/05/03 03:18:33 macallan Exp $");
     34 
     35 #include "opt_interrupt.h"
     36 
     37 #ifdef PIC_OPENPIC
     38 
     39 #include <sys/param.h>
     40 #include <sys/malloc.h>
     41 #include <sys/kernel.h>
     42 
     43 #include <uvm/uvm_extern.h>
     44 
     45 #include <machine/pio.h>
     46 #include <powerpc/openpic.h>
     47 
     48 #include <dev/ofw/openfirm.h>
     49 
     50 #include <machine/autoconf.h>
     51 #include <arch/powerpc/pic/picvar.h>
     52 
     53 static int  opic_irq_is_enabled(struct pic_ops *, int);
     54 static void opic_enable_irq(struct pic_ops *, int, int);
     55 static void opic_disable_irq(struct pic_ops *, int);
     56 static void opic_clear_irq(struct pic_ops *, int);
     57 static int  opic_get_irq(struct pic_ops *);
     58 static void opic_ack_irq(struct pic_ops *, int);
     59 
     60 struct openpic_ops {
     61 	struct pic_ops pic;
     62 	uint32_t enable_mask;
     63 };
     64 
     65 struct pic_ops *
     66 setup_openpic(uint32_t addr, int num, int passthrough)
     67 {
     68 	struct openpic_ops *openpic;
     69 	struct pic_ops *pic;
     70 	int irq;
     71 	u_int x;
     72 
     73 	openpic_base = (void *)addr;
     74 	openpic = malloc(sizeof(struct openpic_ops), M_DEVBUF, M_NOWAIT);
     75 	KASSERT(openpic != NULL);
     76 	pic = &openpic->pic;
     77 
     78 	pic->pic_numintrs = num;
     79 	pic->pic_cookie = (void *)addr;
     80 	pic->pic_irq_is_enabled = opic_irq_is_enabled;
     81 	pic->pic_enable_irq = opic_enable_irq;
     82 	pic->pic_reenable_irq = opic_enable_irq;
     83 	pic->pic_disable_irq = opic_disable_irq;
     84 	pic->pic_clear_irq = opic_clear_irq;
     85 	pic->pic_get_irq = opic_get_irq;
     86 	pic->pic_ack_irq = opic_ack_irq;
     87 	strcpy(pic->pic_name, "openpic");
     88 	pic_add(pic);
     89 
     90 	/* disable all interrupts */
     91 	for (irq = 0; irq < 256; irq++)
     92 		openpic_write(OPENPIC_SRC_VECTOR(irq), OPENPIC_IMASK);
     93 
     94 	openpic_set_priority(0, 15);
     95 
     96 	x = openpic_read(OPENPIC_CONFIG);
     97 	if (passthrough) {
     98 		x &= ~OPENPIC_CONFIG_8259_PASSTHRU_DISABLE;
     99 	} else {
    100 		x |= OPENPIC_CONFIG_8259_PASSTHRU_DISABLE;
    101 	}
    102 	openpic_write(OPENPIC_CONFIG, x);
    103 
    104 	/* send all interrupts to CPU 0 */
    105 	for (irq = 0; irq < ICU_LEN; irq++)
    106 		openpic_write(OPENPIC_IDEST(irq), 1 << 0);
    107 
    108 	for (irq = 0; irq < ICU_LEN; irq++) {
    109 		x = irq;
    110 		x |= OPENPIC_IMASK;
    111 		x |= OPENPIC_POLARITY_POSITIVE;
    112 		x |= OPENPIC_SENSE_LEVEL;
    113 		x |= 8 << OPENPIC_PRIORITY_SHIFT;
    114 		openpic_write(OPENPIC_SRC_VECTOR(irq), x);
    115 	}
    116 
    117 	openpic_set_priority(0, 0);
    118 
    119 	/* clear all pending interrunts */
    120 	for (irq = 0; irq < 256; irq++) {
    121 		openpic_read_irq(0);
    122 		openpic_eoi(0);
    123 	}
    124 
    125 	for (irq = 0; irq < num; irq++)
    126 		openpic_disable_irq(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 
    138 static int
    139 opic_irq_is_enabled(struct pic_ops *pic, int irq)
    140 {
    141 	/* XXX */
    142 
    143 	return 1;
    144 }
    145 
    146 static void
    147 opic_enable_irq(struct pic_ops *pic, int irq, int type)
    148 {
    149 
    150 	openpic_enable_irq(irq, type);
    151 }
    152 
    153 static void
    154 opic_disable_irq(struct pic_ops *pic, int irq)
    155 {
    156 
    157 	openpic_disable_irq(irq);
    158 }
    159 
    160 static void
    161 opic_clear_irq(struct pic_ops *pic, int irq)
    162 {
    163 
    164 	/* we only use ack here */
    165 }
    166 
    167 static int
    168 opic_get_irq(struct pic_ops *pic)
    169 {
    170 
    171 	return openpic_read_irq(cpu_number());
    172 }
    173 
    174 static void
    175 opic_ack_irq(struct pic_ops *pic, int irq)
    176 {
    177 
    178 	openpic_eoi(cpu_number());
    179 }
    180 
    181 #endif /* PIC_OPENPIC */
    182