Home | History | Annotate | Line # | Download | only in pic
pic_openpic.c revision 1.2
      1  1.2  garbled /*	$NetBSD: pic_openpic.c,v 1.2 2007/10/17 19:56:46 garbled Exp $ */
      2  1.2  garbled 
      3  1.2  garbled /*-
      4  1.2  garbled  * Copyright (c) 2007 Michael Lorenz
      5  1.2  garbled  * All rights reserved.
      6  1.2  garbled  *
      7  1.2  garbled  * Redistribution and use in source and binary forms, with or without
      8  1.2  garbled  * modification, are permitted provided that the following conditions
      9  1.2  garbled  * are met:
     10  1.2  garbled  * 1. Redistributions of source code must retain the above copyright
     11  1.2  garbled  *    notice, this list of conditions and the following disclaimer.
     12  1.2  garbled  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.2  garbled  *    notice, this list of conditions and the following disclaimer in the
     14  1.2  garbled  *    documentation and/or other materials provided with the distribution.
     15  1.2  garbled  * 3. Neither the name of The NetBSD Foundation nor the names of its
     16  1.2  garbled  *    contributors may be used to endorse or promote products derived
     17  1.2  garbled  *    from this software without specific prior written permission.
     18  1.2  garbled  *
     19  1.2  garbled  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.2  garbled  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.2  garbled  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.2  garbled  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.2  garbled  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.2  garbled  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.2  garbled  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.2  garbled  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.2  garbled  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.2  garbled  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.2  garbled  * POSSIBILITY OF SUCH DAMAGE.
     30  1.2  garbled  */
     31  1.2  garbled 
     32  1.2  garbled #include <sys/cdefs.h>
     33  1.2  garbled __KERNEL_RCSID(0, "$NetBSD: pic_openpic.c,v 1.2 2007/10/17 19:56:46 garbled Exp $");
     34  1.2  garbled 
     35  1.2  garbled #include <sys/param.h>
     36  1.2  garbled #include <sys/malloc.h>
     37  1.2  garbled #include <sys/kernel.h>
     38  1.2  garbled 
     39  1.2  garbled #include <uvm/uvm_extern.h>
     40  1.2  garbled 
     41  1.2  garbled #include <machine/pio.h>
     42  1.2  garbled #include <powerpc/openpic.h>
     43  1.2  garbled 
     44  1.2  garbled #include <arch/powerpc/pic/picvar.h>
     45  1.2  garbled 
     46  1.2  garbled #include "opt_interrupt.h"
     47  1.2  garbled 
     48  1.2  garbled void openpic_set_priority(int cpu, int pri);
     49  1.2  garbled static void opic_enable_irq(struct pic_ops *, int, int);
     50  1.2  garbled static void opic_disable_irq(struct pic_ops *, int);
     51  1.2  garbled static int  opic_get_irq(struct pic_ops *);
     52  1.2  garbled static void opic_ack_irq(struct pic_ops *, int);
     53  1.2  garbled static void opic_establish_irq(struct pic_ops*, int, int, int);
     54  1.2  garbled static void opic_finish_setup(struct pic_ops *);
     55  1.2  garbled 
     56  1.2  garbled volatile unsigned char *openpic_base;
     57  1.2  garbled 
     58  1.2  garbled struct pic_ops *
     59  1.2  garbled setup_openpic(void *addr, int passthrough)
     60  1.2  garbled {
     61  1.2  garbled 	struct pic_ops *pic;
     62  1.2  garbled 	int irq;
     63  1.2  garbled 	u_int x;
     64  1.2  garbled 
     65  1.2  garbled 	openpic_base = (void *)addr;
     66  1.2  garbled 	pic = malloc(sizeof(struct pic_ops), M_DEVBUF, M_NOWAIT);
     67  1.2  garbled 	KASSERT(pic != NULL);
     68  1.2  garbled 
     69  1.2  garbled 	x = openpic_read(OPENPIC_FEATURE);
     70  1.2  garbled 	aprint_normal("OpenPIC Version 1.%d: "
     71  1.2  garbled 	    "Supports %d CPUs and %d interrupt sources.\n",
     72  1.2  garbled 	    x & 0xff, ((x & 0x1f00) >> 8) + 1, ((x & 0x07ff0000) >> 16) + 1);
     73  1.2  garbled 
     74  1.2  garbled 	pic->pic_numintrs = ((x & 0x07ff0000) >> 16) + 1;
     75  1.2  garbled 	pic->pic_cookie = addr;
     76  1.2  garbled 	pic->pic_enable_irq = opic_enable_irq;
     77  1.2  garbled 	pic->pic_reenable_irq = opic_enable_irq;
     78  1.2  garbled 	pic->pic_disable_irq = opic_disable_irq;
     79  1.2  garbled 	pic->pic_get_irq = opic_get_irq;
     80  1.2  garbled 	pic->pic_ack_irq = opic_ack_irq;
     81  1.2  garbled 	pic->pic_establish_irq = opic_establish_irq;
     82  1.2  garbled 	pic->pic_finish_setup = opic_finish_setup;
     83  1.2  garbled 	strcpy(pic->pic_name, "openpic");
     84  1.2  garbled 	pic_add(pic);
     85  1.2  garbled 
     86  1.2  garbled 	/*
     87  1.2  garbled 	 * the following sequence should make the same effects as openpic
     88  1.2  garbled 	 * controller reset by writing a one at the self-clearing
     89  1.2  garbled 	 * OPENPIC_CONFIG_RESET bit.  Please check the document of your
     90  1.2  garbled 	 * OpenPIC compliant interrupt controller and see whether #else
     91  1.2  garbled 	 * portion can work as described.
     92  1.2  garbled 	 */
     93  1.2  garbled #if 1
     94  1.2  garbled 	openpic_set_priority(0, 15);
     95  1.2  garbled 
     96  1.2  garbled 	for (irq = 0; irq < pic->pic_numintrs; irq++) {
     97  1.2  garbled 		/* make sure to keep disabled */
     98  1.2  garbled 		openpic_write(OPENPIC_SRC_VECTOR(irq), OPENPIC_IMASK);
     99  1.2  garbled 		/* send all interrupts to CPU 0 */
    100  1.2  garbled 		openpic_write(OPENPIC_IDEST(irq), 1 << 0);
    101  1.2  garbled 	}
    102  1.2  garbled 
    103  1.2  garbled 	x = openpic_read(OPENPIC_CONFIG);
    104  1.2  garbled 	if (passthrough)
    105  1.2  garbled 		x &= ~OPENPIC_CONFIG_8259_PASSTHRU_DISABLE;
    106  1.2  garbled 	else
    107  1.2  garbled 		x |= OPENPIC_CONFIG_8259_PASSTHRU_DISABLE;
    108  1.2  garbled 	openpic_write(OPENPIC_CONFIG, x);
    109  1.2  garbled 
    110  1.2  garbled 	openpic_write(OPENPIC_SPURIOUS_VECTOR, 0xff);
    111  1.2  garbled 
    112  1.2  garbled 	openpic_set_priority(0, 0);
    113  1.2  garbled 
    114  1.2  garbled 	/* clear all pending interrunts */
    115  1.2  garbled 	for (irq = 0; irq < pic->pic_numintrs; irq++) {
    116  1.2  garbled 		openpic_read_irq(0);
    117  1.2  garbled 		openpic_eoi(0);
    118  1.2  garbled 	}
    119  1.2  garbled #else
    120  1.2  garbled 	irq = 0;
    121  1.2  garbled 	openpic_write(OPENPIC_CONFIG, OPENPIC_CONFIG_RESET);
    122  1.2  garbled 	do {
    123  1.2  garbled 		x = openpic_read(OPENPIC_CONFIG);
    124  1.2  garbled 	} while (x & OPENPIC_CONFIG_RESET); /* S1C bit */
    125  1.2  garbled 	if (passthrough)
    126  1.2  garbled 		x &= ~OPENPIC_CONFIG_8259_PASSTHRU_DISABLE;
    127  1.2  garbled 	else
    128  1.2  garbled 		x |= OPENPIC_CONFIG_8259_PASSTHRU_DISABLE;
    129  1.2  garbled 	openpic_write(OPENPIC_CONFIG, x);
    130  1.2  garbled 	openpic_set_priority(0, 0);
    131  1.2  garbled #endif
    132  1.2  garbled 
    133  1.2  garbled #if 0
    134  1.2  garbled 	printf("timebase freq=%d\n", openpic_read(0x10f0));
    135  1.2  garbled #endif
    136  1.2  garbled 	return pic;
    137  1.2  garbled }
    138  1.2  garbled 
    139  1.2  garbled static void
    140  1.2  garbled opic_finish_setup(struct pic_ops *pic)
    141  1.2  garbled {
    142  1.2  garbled 	uint32_t cpumask = 0;
    143  1.2  garbled 	int i;
    144  1.2  garbled 
    145  1.2  garbled #ifdef OPENPIC_DISTRIBUTE
    146  1.2  garbled 	for (i = 0; i < ncpu; i++)
    147  1.2  garbled 		cpumask |= (1 << cpu_info[i].ci_cpuid);
    148  1.2  garbled #else
    149  1.2  garbled 	cpumask = 1;
    150  1.2  garbled #endif
    151  1.2  garbled 	for (i = 0; i < pic->pic_numintrs; i++) {
    152  1.2  garbled 		/* send all interrupts to all active CPUs */
    153  1.2  garbled 		openpic_write(OPENPIC_IDEST(i), cpumask);
    154  1.2  garbled 	}
    155  1.2  garbled }
    156  1.2  garbled 
    157  1.2  garbled static void
    158  1.2  garbled opic_establish_irq(struct pic_ops *pic, int irq, int type, int pri)
    159  1.2  garbled {
    160  1.2  garbled 	int realpri = max(1, min(15, pri));
    161  1.2  garbled 	uint32_t x;
    162  1.2  garbled 
    163  1.2  garbled 	x = irq;
    164  1.2  garbled 	x |= OPENPIC_IMASK;
    165  1.2  garbled 	x |= (irq == 0) ?
    166  1.2  garbled 	    OPENPIC_POLARITY_POSITIVE :	OPENPIC_POLARITY_NEGATIVE;
    167  1.2  garbled 	x |= (type == IST_EDGE) ? OPENPIC_SENSE_EDGE : OPENPIC_SENSE_LEVEL;
    168  1.2  garbled 	x |= realpri << OPENPIC_PRIORITY_SHIFT;
    169  1.2  garbled 	openpic_write(OPENPIC_SRC_VECTOR(irq), x);
    170  1.2  garbled 
    171  1.2  garbled 	aprint_debug("%s: setting IRQ %d to priority %d\n", __func__, irq, realpri);
    172  1.2  garbled }
    173  1.2  garbled 
    174  1.2  garbled void
    175  1.2  garbled openpic_set_priority(int cpu, int pri)
    176  1.2  garbled {
    177  1.2  garbled 	u_int x;
    178  1.2  garbled 
    179  1.2  garbled 	x = openpic_read(OPENPIC_CPU_PRIORITY(cpu));
    180  1.2  garbled 	x &= ~OPENPIC_CPU_PRIORITY_MASK;
    181  1.2  garbled 	x |= pri;
    182  1.2  garbled 	openpic_write(OPENPIC_CPU_PRIORITY(cpu), x);
    183  1.2  garbled }
    184  1.2  garbled 
    185  1.2  garbled static void
    186  1.2  garbled opic_enable_irq(struct pic_ops *pic, int irq, int type)
    187  1.2  garbled {
    188  1.2  garbled 	u_int x;
    189  1.2  garbled 
    190  1.2  garbled 	x = openpic_read(OPENPIC_SRC_VECTOR(irq));
    191  1.2  garbled 	x &= ~OPENPIC_IMASK;
    192  1.2  garbled 	openpic_write(OPENPIC_SRC_VECTOR(irq), x);
    193  1.2  garbled }
    194  1.2  garbled 
    195  1.2  garbled static void
    196  1.2  garbled opic_disable_irq(struct pic_ops *pic, int irq)
    197  1.2  garbled {
    198  1.2  garbled 	u_int x;
    199  1.2  garbled 
    200  1.2  garbled 	x = openpic_read(OPENPIC_SRC_VECTOR(irq));
    201  1.2  garbled 	x |= OPENPIC_IMASK;
    202  1.2  garbled 	openpic_write(OPENPIC_SRC_VECTOR(irq), x);
    203  1.2  garbled }
    204  1.2  garbled 
    205  1.2  garbled static int
    206  1.2  garbled opic_get_irq(struct pic_ops *pic)
    207  1.2  garbled {
    208  1.2  garbled 
    209  1.2  garbled 	return openpic_read_irq(cpu_number());
    210  1.2  garbled }
    211  1.2  garbled 
    212  1.2  garbled static void
    213  1.2  garbled opic_ack_irq(struct pic_ops *pic, int irq)
    214  1.2  garbled {
    215  1.2  garbled 
    216  1.2  garbled 	openpic_eoi(cpu_number());
    217  1.2  garbled }
    218