Home | History | Annotate | Line # | Download | only in ee
intc.c revision 1.2
      1 /*	$NetBSD: intc.c,v 1.2 2003/07/15 02:54:37 lukem Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2001 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by UCHIYAMA Yasushi.
      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  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: intc.c,v 1.2 2003/07/15 02:54:37 lukem Exp $");
     41 
     42 #include "debug_playstation2.h"
     43 
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 
     47 #include <playstation2/ee/eevar.h>
     48 #include <playstation2/ee/intcvar.h>
     49 #include <playstation2/ee/intcreg.h>
     50 #include <playstation2/ee/gsvar.h>	/* debug monitor */
     51 
     52 #include <playstation2/playstation2/interrupt.h>
     53 
     54 #ifdef DEBUG
     55 #define LEGAL_CHANNEL(x)	((x) >= 0 && (x) <= 15)
     56 #define STATIC
     57 #else
     58 #define STATIC	static
     59 #endif
     60 
     61 #define _INTC_NINTR	16
     62 
     63 u_int32_t __intc_enabled_channel;
     64 
     65 STATIC int __intc_initialized;
     66 STATIC struct _ipl_dispatcher __intc_dispatcher[_INTC_NINTR];
     67 STATIC struct _ipl_holder __intc_ipl_holder[_IPL_N];
     68 
     69 STATIC SLIST_HEAD(, _ipl_dispatcher) __intc_dispatcher_head =
     70  SLIST_HEAD_INITIALIZER(__intc_dispatcher_head);
     71 
     72 void
     73 intc_init()
     74 {
     75 	int i;
     76 
     77 	if (__intc_initialized++)
     78 		return;
     79 
     80 	/* disable all channel */
     81 	for (i = 0; i < _INTC_NINTR; i++)
     82 		intc_intr_disable(i);
     83 
     84 	/* clear interrupts */
     85 	_reg_write_4(I_STAT_REG, _reg_read_4(I_STAT_REG));
     86 
     87 	for (i = 0; i < _IPL_N; i++)
     88 		__intc_ipl_holder[i].mask = 0xffffffff;
     89 }
     90 
     91 int
     92 intc_intr(u_int32_t mask)
     93 {
     94 	struct _ipl_dispatcher *dispatcher;
     95 	u_int32_t r, dispatch, pending;
     96 
     97 	r = _reg_read_4(I_STAT_REG);
     98 	dispatch = r & ~mask & __intc_enabled_channel;
     99 	pending = r & mask & __intc_enabled_channel;
    100 
    101 #if 0
    102 	__gsfb_print(1,
    103 	    "INTC stat=%08x, mask=%08x, pend=%08x, disp=%08x enable=%08x\n",
    104 	    r, mask, pending, dispatch, __intc_enabled_channel);
    105 #endif
    106 	if (dispatch == 0)
    107 		return (pending == 0 ? 1 : 0);
    108 
    109 	/* clear interrupt */
    110 	_reg_write_4(I_STAT_REG, dispatch);
    111 
    112 	/* dispatch interrupt handler */
    113 	SLIST_FOREACH(dispatcher, &__intc_dispatcher_head, link) {
    114 		if (dispatcher->bit & dispatch) {
    115 			KDASSERT(dispatcher->func);
    116 			(*dispatcher->func)(dispatcher->arg);
    117 			dispatch &= ~dispatcher->bit;
    118 		}
    119 	}
    120 
    121 	/* disable spurious interrupt source */
    122 	if (dispatch) {
    123 		int i, bit;
    124 		for (i = 0, bit = 1; i < _INTC_NINTR; i++, bit <<= 1) {
    125 			if (bit & dispatch) {
    126 				intc_intr_disable(i);
    127 				printf("%s: spurious interrupt %d disabled.\n",
    128 				    __FUNCTION__, i);
    129 			}
    130 		}
    131 	}
    132 
    133 	return (pending == 0 ? 1 : 0);
    134 }
    135 
    136 void
    137 intc_intr_enable(enum intc_channel ch)
    138 {
    139 	u_int32_t mask;
    140 
    141 	KDASSERT(LEGAL_CHANNEL(ch));
    142 	mask = 1 << ch;
    143 	_reg_write_4(I_MASK_REG, (_reg_read_4(I_MASK_REG) & mask) ^ mask);
    144 }
    145 
    146 void
    147 intc_intr_disable(enum intc_channel ch)
    148 {
    149 
    150 	KDASSERT(LEGAL_CHANNEL(ch));
    151 	_reg_write_4(I_MASK_REG, _reg_read_4(I_MASK_REG) & (1 << ch));
    152 }
    153 
    154 void
    155 intc_update_mask(u_int32_t mask)
    156 {
    157 	u_int32_t cur_mask;
    158 
    159 	cur_mask = _reg_read_4(I_MASK_REG);
    160 
    161 	_reg_write_4(I_MASK_REG, ((cur_mask ^ ~mask) | (cur_mask & mask)) &
    162 	    __intc_enabled_channel);
    163 }
    164 
    165 void *
    166 intc_intr_establish(enum intc_channel ch, int ipl, int (*func)(void *),
    167     void *arg)
    168 {
    169 	struct _ipl_dispatcher *dispatcher = &__intc_dispatcher[ch];
    170 	struct _ipl_dispatcher *d;
    171 	u_int32_t bit;
    172 	int i, s;
    173 
    174 	KDASSERT(dispatcher->func == NULL);
    175 
    176 	s = _intr_suspend();
    177 	dispatcher->func = func;
    178 	dispatcher->arg = arg;
    179 	dispatcher->ipl = ipl;
    180 	dispatcher->channel = ch;
    181 	dispatcher->bit = bit = (1 << ch);
    182 
    183 	for (i = 0; i < _IPL_N; i++)
    184 		if (i < ipl)
    185 			__intc_ipl_holder[i].mask &= ~bit;
    186 		else
    187 			__intc_ipl_holder[i].mask |= bit;
    188 
    189 	/* insert queue IPL order */
    190 	if (SLIST_EMPTY(&__intc_dispatcher_head)) {
    191 		SLIST_INSERT_HEAD(&__intc_dispatcher_head, dispatcher, link);
    192 	} else {
    193 		SLIST_FOREACH(d, &__intc_dispatcher_head, link) {
    194 			if (SLIST_NEXT(d, link) == 0 ||
    195 			    SLIST_NEXT(d, link)->ipl < ipl) {
    196 				SLIST_INSERT_AFTER(d, dispatcher, link);
    197 				break;
    198 			}
    199 		}
    200 	}
    201 
    202 	md_ipl_register(IPL_INTC, __intc_ipl_holder);
    203 
    204 	intc_intr_enable(ch);
    205 	__intc_enabled_channel |= bit;
    206 
    207 	_intr_resume(s);
    208 
    209 	return ((void *)ch);
    210 }
    211 
    212 void
    213 intc_intr_disestablish(void *handle)
    214 {
    215 	int ch = (int)(handle);
    216 	struct _ipl_dispatcher *dispatcher = &__intc_dispatcher[ch];
    217 	u_int32_t bit;
    218 	int i, s;
    219 
    220 	s = _intr_suspend();
    221 
    222 	intc_intr_disable(ch);
    223 	dispatcher->func = NULL;
    224 
    225 	SLIST_REMOVE(&__intc_dispatcher_head, dispatcher,
    226 	    _ipl_dispatcher, link);
    227 
    228 	bit = dispatcher->bit;
    229 	for (i = 0; i < _IPL_N; i++)
    230 		__intc_ipl_holder[i].mask |= bit;
    231 
    232 	md_ipl_register(IPL_INTC, __intc_ipl_holder);
    233 	__intc_enabled_channel &= ~bit;
    234 
    235 	_intr_resume(s);
    236 }
    237 
    238