Home | History | Annotate | Line # | Download | only in ibm4xx
pic_uic.c revision 1.7.2.1
      1  1.7.2.1  thorpej /*	$NetBSD: pic_uic.c,v 1.7.2.1 2021/04/03 22:28:34 thorpej Exp $	*/
      2      1.1     matt 
      3      1.1     matt /*
      4      1.1     matt  * Copyright 2002 Wasabi Systems, Inc.
      5      1.1     matt  * All rights reserved.
      6      1.1     matt  *
      7      1.1     matt  * Written by Eduardo Horvath and Simon Burge for Wasabi Systems, Inc.
      8      1.1     matt  *
      9      1.1     matt  * Redistribution and use in source and binary forms, with or without
     10      1.1     matt  * modification, are permitted provided that the following conditions
     11      1.1     matt  * are met:
     12      1.1     matt  * 1. Redistributions of source code must retain the above copyright
     13      1.1     matt  *    notice, this list of conditions and the following disclaimer.
     14      1.1     matt  * 2. Redistributions in binary form must reproduce the above copyright
     15      1.1     matt  *    notice, this list of conditions and the following disclaimer in the
     16      1.1     matt  *    documentation and/or other materials provided with the distribution.
     17      1.1     matt  * 3. All advertising materials mentioning features or use of this software
     18      1.1     matt  *    must display the following acknowledgement:
     19      1.1     matt  *      This product includes software developed for the NetBSD Project by
     20      1.1     matt  *      Wasabi Systems, Inc.
     21      1.1     matt  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22      1.1     matt  *    or promote products derived from this software without specific prior
     23      1.1     matt  *    written permission.
     24      1.1     matt  *
     25      1.1     matt  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26      1.1     matt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27      1.1     matt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28      1.1     matt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29      1.1     matt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30      1.1     matt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31      1.1     matt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32      1.1     matt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33      1.1     matt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34      1.1     matt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35      1.1     matt  * POSSIBILITY OF SUCH DAMAGE.
     36      1.1     matt  */
     37      1.1     matt 
     38      1.1     matt #include <sys/cdefs.h>
     39  1.7.2.1  thorpej __KERNEL_RCSID(0, "$NetBSD: pic_uic.c,v 1.7.2.1 2021/04/03 22:28:34 thorpej Exp $");
     40      1.7      rin 
     41      1.7      rin #ifdef _KERNEL_OPT
     42      1.7      rin #include "opt_ppcarch.h"
     43      1.7      rin #include "opt_uic.h"
     44      1.7      rin #endif
     45      1.1     matt 
     46      1.1     matt #include <sys/param.h>
     47      1.1     matt #include <sys/kernel.h>
     48      1.1     matt #include <sys/evcnt.h>
     49      1.1     matt #include <sys/cpu.h>
     50      1.1     matt 
     51      1.1     matt #include <machine/intr.h>
     52      1.1     matt #include <machine/psl.h>
     53      1.1     matt 
     54      1.1     matt #include <powerpc/spr.h>
     55      1.1     matt #include <powerpc/ibm4xx/spr.h>
     56      1.1     matt #include <powerpc/ibm4xx/cpu.h>
     57      1.1     matt 
     58      1.1     matt #include <powerpc/pic/picvar.h>
     59      1.1     matt 
     60      1.1     matt /*
     61      1.1     matt  * Number of interrupts (hard + soft), irq number legality test,
     62      1.1     matt  * mapping of irq number to mask and a way to pick irq number
     63      1.1     matt  * off a mask of active intrs.
     64      1.1     matt  */
     65      1.1     matt #define	IRQ_TO_MASK(irq) 	(0x80000000UL >> ((irq) & 0x1f))
     66      1.1     matt #define	IRQ_OF_MASK(mask) 	__builtin_clz(mask)
     67      1.1     matt 
     68      1.1     matt static void	uic_enable_irq(struct pic_ops *, int, int);
     69      1.1     matt static void	uic_disable_irq(struct pic_ops *, int);
     70      1.1     matt static int	uic_get_irq(struct pic_ops *, int);
     71      1.1     matt static void	uic_ack_irq(struct pic_ops *, int);
     72      1.1     matt static void	uic_establish_irq(struct pic_ops *, int, int, int);
     73      1.1     matt 
     74      1.1     matt struct uic {
     75      1.1     matt 	uint32_t uic_intr_enable;	/* cached intr enable mask */
     76  1.7.2.1  thorpej #ifdef PPC_IBM403
     77  1.7.2.1  thorpej 	/*
     78  1.7.2.1  thorpej 	 * Not clearly documented in reference manual, but DCR_EXISR
     79  1.7.2.1  thorpej 	 * register is not updated immediately after some bits are
     80  1.7.2.1  thorpej 	 * cleared by mtdcr, no matter whether sync (= eieio) and/or
     81  1.7.2.1  thorpej 	 * isync are issued.
     82  1.7.2.1  thorpej 	 *
     83  1.7.2.1  thorpej 	 * Therefore, we have to manage our own status mask in the
     84  1.7.2.1  thorpej 	 * interrupt handler; see uic_{ack,get}_irq() for more details.
     85  1.7.2.1  thorpej 	 * This is what we did in obsoleted powerpc/ibm4xx/intr.c.
     86  1.7.2.1  thorpej 	 */
     87  1.7.2.1  thorpej 	uint32_t uic_intr_status;
     88  1.7.2.1  thorpej #endif
     89      1.1     matt 	uint32_t (*uic_mf_intr_status)(void);
     90      1.1     matt 	uint32_t (*uic_mf_intr_enable)(void);
     91      1.1     matt 	void (*uic_mt_intr_enable)(uint32_t);
     92      1.1     matt 	void (*uic_mt_intr_ack)(uint32_t);
     93      1.1     matt };
     94      1.1     matt 
     95      1.1     matt /*
     96      1.1     matt  * Platform specific code may override any of the above.
     97      1.1     matt  */
     98      1.1     matt #ifdef PPC_IBM403
     99      1.1     matt 
    100      1.1     matt #include <powerpc/ibm4xx/dcr403cgx.h>
    101      1.1     matt 
    102      1.1     matt static uint32_t
    103      1.1     matt uic403_mfdcr_intr_status(void)
    104      1.1     matt {
    105      1.1     matt 	return mfdcr(DCR_EXISR);
    106      1.1     matt }
    107      1.1     matt 
    108      1.1     matt static uint32_t
    109      1.1     matt uic403_mfdcr_intr_enable(void)
    110      1.1     matt {
    111      1.1     matt 	return mfdcr(DCR_EXIER);
    112      1.1     matt }
    113      1.1     matt 
    114      1.1     matt static void
    115      1.1     matt uic403_mtdcr_intr_ack(uint32_t v)
    116      1.1     matt {
    117      1.1     matt 	mtdcr(DCR_EXISR, v);
    118      1.1     matt }
    119      1.1     matt 
    120      1.1     matt static void
    121      1.1     matt uic403_mtdcr_intr_enable(uint32_t v)
    122      1.1     matt {
    123      1.1     matt 	mtdcr(DCR_EXIER, v);
    124      1.1     matt }
    125      1.1     matt 
    126      1.1     matt struct uic uic403 = {
    127      1.1     matt 	.uic_intr_enable =	0,
    128      1.1     matt 	.uic_mf_intr_status =	uic403_mfdcr_intr_status,
    129      1.1     matt 	.uic_mf_intr_enable =	uic403_mfdcr_intr_enable,
    130      1.1     matt 	.uic_mt_intr_enable =	uic403_mtdcr_intr_enable,
    131      1.1     matt 	.uic_mt_intr_ack =	uic403_mtdcr_intr_ack,
    132      1.1     matt };
    133      1.1     matt 
    134      1.1     matt struct pic_ops pic_uic403 = {
    135      1.1     matt 	.pic_cookie = &uic403,
    136      1.1     matt 	.pic_numintrs = 32,
    137      1.1     matt 	.pic_enable_irq = uic_enable_irq,
    138      1.1     matt 	.pic_reenable_irq = uic_enable_irq,
    139      1.1     matt 	.pic_disable_irq = uic_disable_irq,
    140      1.1     matt 	.pic_establish_irq = uic_establish_irq,
    141      1.1     matt 	.pic_get_irq = uic_get_irq,
    142      1.1     matt 	.pic_ack_irq = uic_ack_irq,
    143      1.1     matt 	.pic_finish_setup = NULL,
    144      1.1     matt 	.pic_name = "uic0"
    145      1.1     matt };
    146      1.1     matt 
    147      1.1     matt #else /* Generic 405/440/460 Universal Interrupt Controller */
    148      1.1     matt 
    149      1.1     matt #include <powerpc/ibm4xx/dcr4xx.h>
    150      1.1     matt 
    151      1.1     matt #include "opt_uic.h"
    152      1.1     matt 
    153      1.1     matt /* 405EP/405GP/405GPr/Virtex-4 */
    154      1.1     matt 
    155      1.1     matt static uint32_t
    156      1.1     matt uic0_mfdcr_intr_status(void)
    157      1.1     matt {
    158      1.1     matt 	return mfdcr(DCR_UIC0_BASE + DCR_UIC_MSR);
    159      1.1     matt }
    160      1.1     matt 
    161      1.1     matt static uint32_t
    162      1.1     matt uic0_mfdcr_intr_enable(void)
    163      1.1     matt {
    164      1.1     matt 	return mfdcr(DCR_UIC0_BASE + DCR_UIC_ER);
    165      1.1     matt }
    166      1.1     matt 
    167      1.1     matt static void
    168      1.1     matt uic0_mtdcr_intr_ack(uint32_t v)
    169      1.1     matt {
    170      1.1     matt 	mtdcr(DCR_UIC0_BASE + DCR_UIC_SR, v);
    171      1.1     matt }
    172      1.1     matt 
    173      1.1     matt static void
    174      1.1     matt uic0_mtdcr_intr_enable(uint32_t v)
    175      1.1     matt {
    176      1.1     matt 	mtdcr(DCR_UIC0_BASE + DCR_UIC_ER, v);
    177      1.1     matt }
    178      1.1     matt 
    179      1.1     matt struct uic uic0 = {
    180      1.1     matt 	.uic_intr_enable =	0,
    181      1.1     matt 	.uic_mf_intr_status =	uic0_mfdcr_intr_status,
    182      1.1     matt 	.uic_mf_intr_enable =	uic0_mfdcr_intr_enable,
    183      1.1     matt 	.uic_mt_intr_enable =	uic0_mtdcr_intr_enable,
    184      1.1     matt 	.uic_mt_intr_ack =	uic0_mtdcr_intr_ack,
    185      1.1     matt };
    186      1.1     matt 
    187      1.1     matt struct pic_ops pic_uic0 = {
    188      1.1     matt 	.pic_cookie = &uic0,
    189      1.1     matt 	.pic_numintrs = 32,
    190      1.1     matt 	.pic_enable_irq = uic_enable_irq,
    191      1.1     matt 	.pic_reenable_irq = uic_enable_irq,
    192      1.1     matt 	.pic_disable_irq = uic_disable_irq,
    193      1.1     matt 	.pic_establish_irq = uic_establish_irq,
    194      1.1     matt 	.pic_get_irq = uic_get_irq,
    195      1.1     matt 	.pic_ack_irq = uic_ack_irq,
    196      1.1     matt 	.pic_finish_setup = NULL,
    197      1.1     matt 	.pic_name = "uic0"
    198      1.1     matt };
    199      1.1     matt 
    200      1.1     matt #ifdef MULTIUIC
    201      1.1     matt 
    202      1.1     matt /* 440EP/440GP/440SP/405EX/440SPe/440GX */
    203      1.1     matt 
    204      1.1     matt static uint32_t
    205      1.1     matt uic1_mfdcr_intr_status(void)
    206      1.1     matt {
    207      1.1     matt 	return mfdcr(DCR_UIC1_BASE + DCR_UIC_MSR);
    208      1.1     matt }
    209      1.1     matt 
    210      1.1     matt static uint32_t
    211      1.1     matt uic1_mfdcr_intr_enable(void)
    212      1.1     matt {
    213      1.1     matt 	return mfdcr(DCR_UIC1_BASE + DCR_UIC_ER);
    214      1.1     matt }
    215      1.1     matt 
    216      1.1     matt static void
    217      1.1     matt uic1_mtdcr_intr_ack(uint32_t v)
    218      1.1     matt {
    219      1.1     matt 	mtdcr(DCR_UIC1_BASE + DCR_UIC_SR, v);
    220      1.1     matt }
    221      1.1     matt 
    222      1.1     matt static void
    223      1.1     matt uic1_mtdcr_intr_enable(uint32_t v)
    224      1.1     matt {
    225      1.1     matt 	mtdcr(DCR_UIC1_BASE + DCR_UIC_ER, v);
    226      1.1     matt }
    227      1.1     matt 
    228      1.1     matt extern struct pic_ops pic_uic1;
    229      1.1     matt 
    230      1.1     matt static void
    231      1.1     matt uic1_finish_setup(struct pic_ops *pic)
    232      1.1     matt {
    233  1.7.2.1  thorpej 	intr_establish_xname(30, IST_LEVEL, IPL_HIGH, pic_handle_intr,
    234  1.7.2.1  thorpej 	    &pic_uic1, "uic1");
    235      1.1     matt }
    236      1.1     matt 
    237      1.1     matt struct uic uic1 = {
    238      1.1     matt 	.uic_intr_enable =	0,
    239      1.1     matt 	.uic_mf_intr_status =	uic1_mfdcr_intr_status,
    240      1.1     matt 	.uic_mf_intr_enable =	uic1_mfdcr_intr_enable,
    241      1.1     matt 	.uic_mt_intr_enable =	uic1_mtdcr_intr_enable,
    242      1.1     matt 	.uic_mt_intr_ack =	uic1_mtdcr_intr_ack,
    243      1.1     matt };
    244      1.1     matt 
    245      1.1     matt struct pic_ops pic_uic1 = {
    246      1.1     matt 	.pic_cookie = &uic1,
    247      1.1     matt 	.pic_numintrs = 32,
    248      1.1     matt 	.pic_enable_irq = uic_enable_irq,
    249      1.1     matt 	.pic_reenable_irq = uic_enable_irq,
    250      1.1     matt 	.pic_disable_irq = uic_disable_irq,
    251      1.1     matt 	.pic_establish_irq = uic_establish_irq,
    252      1.1     matt 	.pic_get_irq = uic_get_irq,
    253      1.1     matt 	.pic_ack_irq = uic_ack_irq,
    254      1.1     matt 	.pic_finish_setup = uic1_finish_setup,
    255      1.1     matt 	.pic_name = "uic1"
    256      1.1     matt };
    257      1.1     matt 
    258      1.1     matt /* 440EP/440GP/440SP/405EX/440SPe */
    259      1.1     matt 
    260      1.1     matt static uint32_t
    261      1.1     matt uic2_mfdcr_intr_status(void)
    262      1.1     matt {
    263      1.1     matt 	return mfdcr(DCR_UIC2_BASE + DCR_UIC_MSR);
    264      1.1     matt }
    265      1.1     matt 
    266      1.1     matt static uint32_t
    267      1.1     matt uic2_mfdcr_intr_enable(void)
    268      1.1     matt {
    269      1.1     matt 	return mfdcr(DCR_UIC2_BASE + DCR_UIC_ER);
    270      1.1     matt }
    271      1.1     matt 
    272      1.1     matt static void
    273      1.1     matt uic2_mtdcr_intr_ack(uint32_t v)
    274      1.1     matt {
    275      1.1     matt 	mtdcr(DCR_UIC2_BASE + DCR_UIC_SR, v);
    276      1.1     matt }
    277      1.1     matt 
    278      1.1     matt static void
    279      1.1     matt uic2_mtdcr_intr_enable(uint32_t v)
    280      1.1     matt {
    281      1.1     matt 	mtdcr(DCR_UIC2_BASE + DCR_UIC_ER, v);
    282      1.1     matt }
    283      1.1     matt 
    284      1.1     matt extern struct pic_ops pic_uic2;
    285      1.1     matt 
    286      1.1     matt static void
    287      1.1     matt uic2_finish_setup(struct pic_ops *pic)
    288      1.1     matt {
    289  1.7.2.1  thorpej 	intr_establish_xname(28, IST_LEVEL, IPL_HIGH, pic_handle_intr,
    290  1.7.2.1  thorpej 	    &pic_uic2, "uic2");
    291      1.1     matt }
    292      1.1     matt 
    293      1.1     matt static struct uic uic2 = {
    294      1.1     matt 	.uic_intr_enable =	0,
    295      1.1     matt 	.uic_mf_intr_status =	uic2_mfdcr_intr_status,
    296      1.1     matt 	.uic_mf_intr_enable =	uic2_mfdcr_intr_enable,
    297      1.1     matt 	.uic_mt_intr_enable =	uic2_mtdcr_intr_enable,
    298      1.1     matt 	.uic_mt_intr_ack =	uic2_mtdcr_intr_ack,
    299      1.1     matt };
    300      1.1     matt 
    301      1.1     matt struct pic_ops pic_uic2 = {
    302      1.1     matt 	.pic_cookie = &uic2,
    303      1.1     matt 	.pic_numintrs = 32,
    304      1.1     matt 	.pic_enable_irq = uic_enable_irq,
    305      1.1     matt 	.pic_reenable_irq = uic_enable_irq,
    306      1.1     matt 	.pic_disable_irq = uic_disable_irq,
    307      1.1     matt 	.pic_establish_irq = uic_establish_irq,
    308      1.1     matt 	.pic_get_irq = uic_get_irq,
    309      1.1     matt 	.pic_ack_irq = uic_ack_irq,
    310      1.1     matt 	.pic_finish_setup = uic2_finish_setup,
    311      1.1     matt 	.pic_name = "uic2"
    312      1.1     matt };
    313      1.1     matt 
    314      1.1     matt #endif /* MULTIUIC */
    315      1.1     matt #endif /* !PPC_IBM403 */
    316      1.1     matt 
    317      1.1     matt /*
    318      1.1     matt  * Set up interrupt mapping array.
    319      1.1     matt  */
    320      1.1     matt void
    321      1.1     matt intr_init(void)
    322      1.1     matt {
    323      1.1     matt #ifdef PPC_IBM403
    324      1.1     matt 	struct pic_ops * const pic = &pic_uic403;
    325      1.1     matt #else
    326      1.1     matt 	struct pic_ops * const pic = &pic_uic0;
    327      1.1     matt #endif
    328      1.1     matt 	struct uic * const uic = pic->pic_cookie;
    329      1.1     matt 
    330      1.1     matt 	uic->uic_mt_intr_enable(0x00000000); 	/* mask all */
    331      1.1     matt 	uic->uic_mt_intr_ack(0xffffffff);	/* acknowledge all */
    332      1.1     matt 
    333      1.1     matt 	pic_add(pic);
    334      1.1     matt }
    335      1.1     matt 
    336      1.1     matt static void
    337      1.1     matt uic_disable_irq(struct pic_ops *pic, int irq)
    338      1.1     matt {
    339      1.1     matt 	struct uic * const uic = pic->pic_cookie;
    340      1.1     matt 	const uint32_t irqmask = IRQ_TO_MASK(irq);
    341      1.1     matt 	if ((uic->uic_intr_enable & irqmask) == 0)
    342      1.1     matt 		return;
    343      1.1     matt 	uic->uic_intr_enable ^= irqmask;
    344      1.1     matt 	(*uic->uic_mt_intr_enable)(uic->uic_intr_enable);
    345      1.1     matt #ifdef IRQ_DEBUG
    346      1.1     matt 	printf("%s: %s: irq=%d, mask=%08x\n", __func__,
    347      1.1     matt 	    pic->pic_name, irq, irqmask);
    348      1.1     matt #endif
    349      1.1     matt }
    350      1.1     matt 
    351      1.1     matt static void
    352      1.1     matt uic_enable_irq(struct pic_ops *pic, int irq, int type)
    353      1.1     matt {
    354      1.1     matt 	struct uic * const uic = pic->pic_cookie;
    355      1.1     matt 	const uint32_t irqmask = IRQ_TO_MASK(irq);
    356      1.1     matt 	if ((uic->uic_intr_enable & irqmask) != 0)
    357      1.1     matt 		return;
    358      1.1     matt 	uic->uic_intr_enable ^= irqmask;
    359      1.1     matt 	(*uic->uic_mt_intr_enable)(uic->uic_intr_enable);
    360      1.1     matt #ifdef IRQ_DEBUG
    361      1.1     matt 	printf("%s: %s: irq=%d, mask=%08x\n", __func__,
    362      1.1     matt 	    pic->pic_name, irq, irqmask);
    363      1.1     matt #endif
    364      1.1     matt }
    365      1.1     matt 
    366      1.1     matt static void
    367      1.1     matt uic_ack_irq(struct pic_ops *pic, int irq)
    368      1.1     matt {
    369      1.1     matt 	struct uic * const uic = pic->pic_cookie;
    370      1.1     matt 	const uint32_t irqmask = IRQ_TO_MASK(irq);
    371      1.1     matt 
    372  1.7.2.1  thorpej #ifdef PPC_IBM403
    373  1.7.2.1  thorpej 	uic->uic_intr_status &= ~irqmask;
    374  1.7.2.1  thorpej #endif
    375  1.7.2.1  thorpej 
    376      1.1     matt 	(*uic->uic_mt_intr_ack)(irqmask);
    377      1.1     matt }
    378      1.1     matt 
    379      1.1     matt static int
    380  1.7.2.1  thorpej uic_get_irq(struct pic_ops *pic, int req)
    381      1.1     matt {
    382      1.1     matt 	struct uic * const uic = pic->pic_cookie;
    383      1.1     matt 
    384  1.7.2.1  thorpej #ifdef PPC_IBM403
    385  1.7.2.1  thorpej 	if (req == PIC_GET_IRQ)
    386  1.7.2.1  thorpej 		uic->uic_intr_status = (*uic->uic_mf_intr_status)();
    387  1.7.2.1  thorpej 	const uint32_t irqmask = uic->uic_intr_status;
    388  1.7.2.1  thorpej #else
    389      1.1     matt 	const uint32_t irqmask = (*uic->uic_mf_intr_status)();
    390  1.7.2.1  thorpej #endif
    391  1.7.2.1  thorpej 
    392      1.1     matt 	if (irqmask == 0)
    393      1.1     matt 		return 255;
    394      1.1     matt 	return IRQ_OF_MASK(irqmask);
    395      1.1     matt }
    396      1.1     matt 
    397      1.1     matt /*
    398      1.1     matt  * Register an interrupt handler.
    399      1.1     matt  */
    400      1.1     matt static void
    401      1.1     matt uic_establish_irq(struct pic_ops *pic, int irq, int type, int ipl)
    402      1.1     matt {
    403      1.1     matt }
    404