Home | History | Annotate | Line # | Download | only in dev
sbus.c revision 1.5
      1 /*	$NetBSD: sbus.c,v 1.5 2002/10/02 04:17:21 thorpej 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 /*
     40  * PlayStation 2 internal PCMCIA/USB interface unit.
     41  */
     42 
     43 #include <sys/param.h>
     44 #include <sys/systm.h>
     45 
     46 #include <machine/bootinfo.h>
     47 #include <machine/autoconf.h>
     48 
     49 #include <playstation2/playstation2/interrupt.h>
     50 
     51 #include <playstation2/ee/eevar.h>
     52 #include <playstation2/ee/intcvar.h>
     53 #include <playstation2/dev/sbusvar.h>
     54 #include <playstation2/dev/sbusreg.h>
     55 
     56 #ifdef DEBUG
     57 #define STATIC
     58 #else
     59 #define STATIC static
     60 #endif
     61 
     62 STATIC void sbus_type2_pcmcia_intr_clear(void);
     63 STATIC void sbus_type2_pcmcia_intr_enable(void);
     64 STATIC void sbus_type2_pcmcia_intr_disable(void);
     65 STATIC void sbus_type2_pcmcia_intr_reinstall(void);
     66 STATIC void sbus_type3_pcmcia_intr_clear(void);
     67 STATIC void sbus_type3_pcmcia_intr_enable(void);
     68 STATIC void sbus_type3_pcmcia_intr_disable(void);
     69 STATIC void sbus_type3_pcmcia_intr_reinstall(void);
     70 STATIC int sbus_spurious_intr(void *);
     71 
     72 STATIC void (*sbus_pcmcia_intr_clear)(void);
     73 STATIC void (*sbus_pcmcia_intr_enable)(void);
     74 STATIC void (*sbus_pcmcia_intr_disable)(void);
     75 STATIC void (*sbus_pcmcia_intr_reinstall)(void);
     76 
     77 STATIC int (*sbus_pcmcia_intr)(void *) = sbus_spurious_intr;
     78 STATIC void *sbus_pcmcia_context;
     79 STATIC int (*sbus_usb_intr)(void *) = sbus_spurious_intr;
     80 STATIC void *sbus_usb_context;
     81 
     82 STATIC void sbus_init(int);
     83 STATIC int sbus_intr(void *);
     84 
     85 STATIC int sbus_match(struct device *, struct cfdata *, void *);
     86 STATIC void sbus_attach(struct device *, struct device *, void *);
     87 STATIC int sbus_search(struct device *, struct cfdata *, void *);
     88 STATIC int sbus_print(void *, const char *);
     89 
     90 CFATTACH_DECL(sbus, sizeof (struct device),
     91     sbus_match, sbus_attach, NULL, NULL);
     92 
     93 extern struct cfdriver sbus_cd;
     94 STATIC int __sbus_attached;
     95 
     96 int
     97 sbus_match(struct device *parent, struct cfdata *cf, void *aux)
     98 {
     99 	struct mainbus_attach_args *ma = aux;
    100 
    101 	if (strcmp(ma->ma_name, sbus_cd.cd_name) != 0)
    102 		return (0);
    103 
    104 	return (!__sbus_attached);
    105 }
    106 
    107 void
    108 sbus_attach(struct device *parent, struct device *self, void *aux)
    109 {
    110 	int type = BOOTINFO_REF(BOOTINFO_PCMCIA_TYPE);
    111 
    112 	printf(": controller type %d\n", type);
    113 
    114 	/* Initialize SBUS controller */
    115 	sbus_init(type);
    116 
    117 	config_search(sbus_search, self, 0);
    118 }
    119 
    120 int
    121 sbus_search(struct device *parent, struct cfdata *cf, void *aux)
    122 {
    123 	struct sbus_attach_args sa;
    124 
    125 	if (config_match(parent, cf, &sa))
    126 		config_attach(parent, cf, &sa, sbus_print);
    127 
    128 	return (0);
    129 }
    130 
    131 int
    132 sbus_print(void *aux, const char *pnp)
    133 {
    134 
    135 	return (pnp ? QUIET : UNCONF);
    136 }
    137 
    138 void
    139 sbus_init(int type)
    140 {
    141 	/* install model dependent hook */
    142 #define SET_PCMCIA_INTR_OPS(x)	 					\
    143 	sbus_pcmcia_intr_clear = sbus_type##x##_pcmcia_intr_clear;	\
    144 	sbus_pcmcia_intr_enable = sbus_type##x##_pcmcia_intr_enable;	\
    145 	sbus_pcmcia_intr_disable = sbus_type##x##_pcmcia_intr_disable;	\
    146 	sbus_pcmcia_intr_reinstall = sbus_type##x##_pcmcia_intr_reinstall
    147 
    148 	switch (type) {
    149 	default:
    150 		panic("unknown pcmcia controller type = %d", type);
    151 		break;
    152 	case 0:
    153 		/* FALLTHROUGH */
    154 	case 1:
    155 		/* FALLTHROUGH */
    156 	case 2:
    157 		SET_PCMCIA_INTR_OPS(2);
    158 		break;
    159 	case 3:
    160 		SET_PCMCIA_INTR_OPS(3);
    161 		break;
    162 	}
    163 #undef SET_PCMCIA_INTR_OPS
    164 	/* disable interrupt */
    165 	(*sbus_pcmcia_intr_disable)();
    166 
    167 	/* clear interrupt */
    168 	(*sbus_pcmcia_intr_clear)();
    169 	_reg_write_4(SBUS_SMFLG_REG, SMFLG_PCMCIA_INT);
    170 	_reg_write_4(SBUS_SMFLG_REG, SMFLG_USB_INT);
    171 
    172 	/* connect to INTC */
    173 	intc_intr_establish(I_CH1_SBUS, IPL_BIO, sbus_intr, 0);
    174 }
    175 
    176 void *
    177 sbus_intr_establish(enum sbus_irq irq, int (*ih_func)(void *), void *ih_arg)
    178 {
    179 	switch (irq) {
    180 	default:
    181 		panic("unknown IRQ");
    182 		break;
    183 	case SBUS_IRQ_PCMCIA:
    184 		sbus_pcmcia_intr = ih_func;
    185 		sbus_pcmcia_context = ih_arg;
    186 		(*sbus_pcmcia_intr_enable)();
    187 		break;
    188 	case SBUS_IRQ_USB:
    189 		sbus_usb_intr = ih_func;
    190 		sbus_usb_context = ih_arg;
    191 		break;
    192 	}
    193 
    194 	return (void *)irq;
    195 }
    196 
    197 void
    198 sbus_intr_disestablish(void *handle)
    199 {
    200 	int irq = (int)handle;
    201 
    202 	switch (irq) {
    203 	default:
    204 		panic("unknown IRQ");
    205 		break;
    206 	case SBUS_IRQ_PCMCIA:
    207 		sbus_pcmcia_intr = sbus_spurious_intr;
    208 		(*sbus_pcmcia_intr_disable)();
    209 		break;
    210 	case SBUS_IRQ_USB:
    211 		sbus_usb_intr = sbus_spurious_intr;
    212 		break;
    213 	}
    214 }
    215 
    216 int
    217 sbus_intr(void *arg)
    218 {
    219 	u_int32_t stat;
    220 
    221 	_playstation2_evcnt.sbus.ev_count++;
    222 	stat = _reg_read_4(SBUS_SMFLG_REG);
    223 
    224 	if (stat & SMFLG_PCMCIA_INT) {
    225 		(*sbus_pcmcia_intr_clear)();
    226 		_reg_write_4(SBUS_SMFLG_REG, SMFLG_PCMCIA_INT);
    227 		(*sbus_pcmcia_intr)(sbus_pcmcia_context);
    228 	}
    229 
    230 	if (stat & SMFLG_USB_INT) {
    231 		_reg_write_4(SBUS_SMFLG_REG, SMFLG_USB_INT);
    232 		(*sbus_usb_intr)(sbus_usb_context);
    233 	}
    234 
    235 	(*sbus_pcmcia_intr_reinstall)();
    236 
    237 	return (1);
    238 }
    239 
    240 int
    241 sbus_spurious_intr(void *arg)
    242 {
    243 
    244 	printf("spurious interrupt.\n");
    245 
    246 	return (1);
    247 }
    248 
    249 /* SCPH-18000 */
    250 void
    251 sbus_type2_pcmcia_intr_clear()
    252 {
    253 
    254 	if (_reg_read_2(SBUS_PCMCIA_CSC1_REG16) & 0x080)
    255 		_reg_write_2(SBUS_PCMCIA_CSC1_REG16, 0xffff);
    256 }
    257 
    258 void
    259 sbus_type2_pcmcia_intr_enable()
    260 {
    261 
    262 	_reg_write_2(SBUS_PCMCIA_TIMR_REG16, 0);
    263 }
    264 
    265 void
    266 sbus_type2_pcmcia_intr_disable()
    267 {
    268 
    269 	_reg_write_2(SBUS_PCMCIA_TIMR_REG16, 1);
    270 }
    271 
    272 void
    273 sbus_type2_pcmcia_intr_reinstall()
    274 {
    275 	u_int16_t r = _reg_read_2(SBUS_PCMCIA_TIMR_REG16);
    276 
    277 	_reg_write_2(SBUS_PCMCIA_TIMR_REG16, 1);
    278 	_reg_write_2(SBUS_PCMCIA_TIMR_REG16, r);
    279 }
    280 
    281 /* SCPH-30000/35000 */
    282 void
    283 sbus_type3_pcmcia_intr_clear()
    284 {
    285 	/* nothing */
    286 }
    287 
    288 void
    289 sbus_type3_pcmcia_intr_enable()
    290 {
    291 
    292 	_reg_write_2(SBUS_PCMCIA3_TIMR_REG16, 0);
    293 }
    294 
    295 void
    296 sbus_type3_pcmcia_intr_disable()
    297 {
    298 
    299 	_reg_write_2(SBUS_PCMCIA3_TIMR_REG16, 1);
    300 }
    301 
    302 void
    303 sbus_type3_pcmcia_intr_reinstall()
    304 {
    305 	u_int16_t r = _reg_read_2(SBUS_PCMCIA3_TIMR_REG16);
    306 
    307 	_reg_write_2(SBUS_PCMCIA3_TIMR_REG16, 1);
    308 	_reg_write_2(SBUS_PCMCIA3_TIMR_REG16, r);
    309 }
    310