Home | History | Annotate | Line # | Download | only in marvell
pic_discovery.c revision 1.3
      1 /*	$NetBSD: pic_discovery.c,v 1.3 2010/04/28 13:51:55 kiyohara Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2002 Allegro Networks, Inc., Wasabi Systems, Inc.
      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. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *      This product includes software developed for the NetBSD Project by
     18  *      Allegro Networks, Inc., and Wasabi Systems, Inc.
     19  * 4. The name of Allegro Networks, Inc. may not be used to endorse
     20  *    or promote products derived from this software without specific prior
     21  *    written permission.
     22  * 5. The name of Wasabi Systems, Inc. may not be used to endorse
     23  *    or promote products derived from this software without specific prior
     24  *    written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY ALLEGRO NETWORKS, INC. AND
     27  * WASABI SYSTEMS, INC. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
     28  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
     29  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     30  * IN NO EVENT SHALL EITHER ALLEGRO NETWORKS, INC. OR WASABI SYSTEMS, INC.
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 #include <sys/cdefs.h>
     41 __KERNEL_RCSID(0, "$NetBSD: pic_discovery.c,v 1.3 2010/04/28 13:51:55 kiyohara Exp $");
     42 
     43 #include <sys/param.h>
     44 #include <sys/bus.h>
     45 #include <sys/malloc.h>
     46 
     47 #include <machine/intr.h>
     48 
     49 #include <powerpc/pic/picvar.h>
     50 
     51 #include <dev/marvell/gtvar.h>
     52 #include <dev/marvell/gtintrreg.h>
     53 #include <dev/marvell/gtintrvar.h>
     54 
     55 
     56 struct discovery_gpp_pic_ops;
     57 
     58 struct discovery_pic_ops {
     59 	struct pic_ops pic;
     60 	union {
     61 		uint64_t mask64;
     62 		uint32_t mask32[2];
     63 	} _mask;
     64 #define enable_mask		_mask.mask64
     65 #define enable_mask_high	_mask.mask32[1]
     66 #define enable_mask_low		_mask.mask32[0]
     67 };
     68 struct discovery_gpp_pic_ops {
     69 	struct pic_ops pic;
     70 
     71 	int gpp_base;
     72 };
     73 
     74 
     75 static void discovery_enable_irq(struct pic_ops *, int, int);
     76 static void discovery_disable_irq(struct pic_ops *, int);
     77 static int discovery_get_irq(struct pic_ops *, int);
     78 static void discovery_ack_irq(struct pic_ops *, int);
     79 
     80 static void discovery_gpp_enable_irq(struct pic_ops *, int, int);
     81 static void discovery_gpp_disable_irq(struct pic_ops *, int);
     82 static int discovery_gpp_get_irq(struct pic_ops *, int);
     83 static void discovery_gpp_ack_irq(struct pic_ops *, int);
     84 
     85 
     86 struct pic_ops *
     87 setup_discovery_pic()
     88 {
     89 	struct discovery_pic_ops *discovery;
     90 	struct pic_ops *pic;
     91 
     92 	discovery =
     93 	    malloc(sizeof(struct discovery_pic_ops), M_DEVBUF, M_NOWAIT);
     94 	KASSERT(discovery != NULL);
     95 
     96 	pic = &discovery->pic;
     97 	pic->pic_numintrs = 64;
     98 	pic->pic_cookie = (void *)NULL;			/* set later */
     99 	pic->pic_enable_irq = discovery_enable_irq;
    100 	pic->pic_reenable_irq = discovery_enable_irq;
    101 	pic->pic_disable_irq = discovery_disable_irq;
    102 	pic->pic_get_irq = discovery_get_irq;
    103 	pic->pic_ack_irq = discovery_ack_irq;
    104 	pic->pic_establish_irq = dummy_pic_establish_intr;
    105 	pic->pic_finish_setup = NULL;
    106 	strcpy(pic->pic_name, "discovery");
    107 	pic_add(pic);
    108 
    109 	discovery->enable_mask = 0;
    110 
    111 	return pic;
    112 }
    113 
    114 static void
    115 discovery_enable_irq(struct pic_ops *pic, int irq, int type)
    116 {
    117 	struct discovery_pic_ops *discovery = (struct discovery_pic_ops *)pic;
    118 
    119 	discovery->enable_mask |= (1 << irq);
    120 	discovery_enable_intr(pic->pic_cookie, irq);
    121 }
    122 
    123 static void
    124 discovery_disable_irq(struct pic_ops *pic, int irq)
    125 {
    126 	struct discovery_pic_ops *discovery = (struct discovery_pic_ops *)pic;
    127 
    128 	discovery->enable_mask &= ~(1 << irq);
    129 	discovery_disable_intr(pic->pic_cookie, irq);
    130 }
    131 
    132 static int
    133 discovery_get_irq(struct pic_ops *pic, int mode)
    134 {
    135 	struct discovery_pic_ops *discovery = (struct discovery_pic_ops *)pic;
    136 	uint32_t cause;
    137 	int irq, base, msk;
    138 
    139 	cause = discovery_mic_low(pic->pic_cookie) & discovery->enable_mask_low;
    140 	if (cause)
    141 		base = 0;
    142 	else {
    143 		cause = discovery_mic_high(pic->pic_cookie);
    144 		cause &= discovery->enable_mask_high;
    145 		if (!cause)
    146 			return 255;
    147 		base = 32;
    148 	}
    149 	for (irq = base, msk = 0x00000001; !(cause & msk); irq++, msk <<= 1);
    150 
    151 	return irq;
    152 }
    153 
    154 static void
    155 discovery_ack_irq(struct pic_ops *pic, int irq)
    156 {
    157 
    158 	/* nothing */
    159 }
    160 
    161 
    162 struct pic_ops *
    163 setup_discovery_gpp_pic(void *discovery, int gpp_base)
    164 {
    165 	struct discovery_gpp_pic_ops *discovery_gpp;
    166 	struct pic_ops *pic;
    167 
    168 	discovery_gpp =
    169 	    malloc(sizeof(struct discovery_gpp_pic_ops), M_DEVBUF, M_NOWAIT);
    170 	KASSERT(discovery_gpp != NULL);
    171 
    172 	pic = &discovery_gpp->pic;
    173 	pic->pic_numintrs = 32;
    174 	pic->pic_cookie = discovery;
    175 	pic->pic_enable_irq = discovery_gpp_enable_irq;
    176 	pic->pic_reenable_irq = discovery_gpp_enable_irq;
    177 	pic->pic_disable_irq = discovery_gpp_disable_irq;
    178 	pic->pic_get_irq = discovery_gpp_get_irq;
    179 	pic->pic_ack_irq = discovery_gpp_ack_irq;
    180 	pic->pic_establish_irq = dummy_pic_establish_intr;
    181 	pic->pic_finish_setup = NULL;
    182 	strcpy(pic->pic_name, "discovery_gpp");
    183 	pic_add(pic);
    184 
    185 	discovery_gpp->gpp_base = gpp_base;
    186 
    187 	return pic;
    188 }
    189 
    190 static void
    191 discovery_gpp_enable_irq(struct pic_ops *pic, int irq, int type)
    192 {
    193 	struct discovery_gpp_pic_ops *discovery_gpp =
    194 	    (struct discovery_gpp_pic_ops *)pic;
    195 	struct discovery_pic_ops *discovery = discovery_gpp->pic.pic_cookie;
    196 
    197 	discovery_gpp_enable_intr(discovery->pic.pic_cookie,
    198 	    discovery_gpp->gpp_base + irq);
    199 }
    200 
    201 static void
    202 discovery_gpp_disable_irq(struct pic_ops *pic, int irq)
    203 {
    204 	struct discovery_gpp_pic_ops *discovery_gpp =
    205 	    (struct discovery_gpp_pic_ops *)pic;
    206 	struct discovery_pic_ops *discovery = discovery_gpp->pic.pic_cookie;
    207 
    208 	discovery_gpp_disable_intr(discovery->pic.pic_cookie,
    209 	    discovery_gpp->gpp_base + irq);
    210 }
    211 
    212 static int
    213 discovery_gpp_get_irq(struct pic_ops *pic, int mode)
    214 {
    215 	struct discovery_gpp_pic_ops *discovery_gpp =
    216 	    (struct discovery_gpp_pic_ops *)pic;
    217 	struct discovery_pic_ops *discovery = discovery_gpp->pic.pic_cookie;
    218 	uint32_t cause, mask;
    219 	int irq, msk, base;
    220 
    221 	cause = discovery_gpp_cause(discovery->pic.pic_cookie);
    222 	mask = discovery_gpp_mask(discovery->pic.pic_cookie);
    223 
    224 	base = discovery_gpp->gpp_base;
    225 	cause &= (mask & (0xff << base));
    226 	if (!cause)
    227 		return 255;
    228 
    229 	for (irq = base, msk = (1 << base); !(cause & msk); irq++, msk <<= 1);
    230 
    231 	return irq;
    232 }
    233 
    234 static void
    235 discovery_gpp_ack_irq(struct pic_ops *pic, int irq)
    236 {
    237 	struct discovery_gpp_pic_ops *discovery_gpp =
    238 	    (struct discovery_gpp_pic_ops *)pic;
    239 	struct discovery_pic_ops *discovery = discovery_gpp->pic.pic_cookie;
    240 
    241 	discovery_gpp_clear_cause(discovery->pic.pic_cookie,
    242 	    discovery_gpp->gpp_base + irq);
    243 }
    244