Home | History | Annotate | Line # | Download | only in mace
mace.c revision 1.24
      1 /*	$NetBSD: mace.c,v 1.24 2021/04/24 23:36:48 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2003 Christopher Sekiya
      5  * Copyright (c) 2002,2003 Rafal K. Boni
      6  * Copyright (c) 2000 Soren S. Jorvang
      7  * All rights reserved.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *          This product includes software developed for the
     20  *          NetBSD Project.  See http://www.NetBSD.org/ for
     21  *          information about NetBSD.
     22  * 4. The name of the author may not be used to endorse or promote products
     23  *    derived from this software without specific prior written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     26  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     27  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     28  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     30  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     31  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     32  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     33  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     34  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     35  */
     36 
     37 /*
     38  * O2 MACE
     39  *
     40  * The MACE is weird -- although it is a 32-bit device, writes only seem to
     41  * work properly if they are 64-bit-at-once writes (at least, out in ISA
     42  * space and probably MEC space -- the PCI stuff seems to be okay with _4).
     43  * Therefore, the _8* routines are used even though the top 32 bits are
     44  * thrown away.
     45  */
     46 
     47 #include <sys/cdefs.h>
     48 __KERNEL_RCSID(0, "$NetBSD: mace.c,v 1.24 2021/04/24 23:36:48 thorpej Exp $");
     49 
     50 #include <sys/param.h>
     51 #include <sys/systm.h>
     52 #include <sys/device.h>
     53 #include <sys/callout.h>
     54 #include <sys/mbuf.h>
     55 #include <sys/malloc.h>
     56 #include <sys/kernel.h>
     57 #include <sys/socket.h>
     58 #include <sys/ioctl.h>
     59 #include <sys/errno.h>
     60 #include <sys/syslog.h>
     61 
     62 #include <uvm/uvm_extern.h>
     63 
     64 #include <sys/bus.h>
     65 #include <machine/cpu.h>
     66 #include <machine/locore.h>
     67 #include <machine/autoconf.h>
     68 #include <machine/machtype.h>
     69 
     70 #include <sgimips/mace/macevar.h>
     71 #include <sgimips/mace/macereg.h>
     72 #include <sgimips/dev/crimevar.h>
     73 #include <sgimips/dev/crimereg.h>
     74 
     75 #include "locators.h"
     76 
     77 #define MACE_NINTR 32 /* actually only 8, but interrupts are shared */
     78 
     79 struct {
     80 	unsigned int	irq;
     81 	unsigned int	intrmask;
     82 	int	(*func)(void *);
     83 	void	*arg;
     84 	struct evcnt evcnt;
     85 	char	evname[32];
     86 } maceintrtab[MACE_NINTR];
     87 
     88 struct mace_softc {
     89 	device_t sc_dev;
     90 
     91 	bus_space_tag_t iot;
     92 	bus_space_handle_t ioh;
     93 	bus_dma_tag_t dmat; /* 32KB ring buffers, 4KB segments, for ISA  */
     94 	int nsegs;
     95 	bus_dma_segment_t seg;
     96 	bus_dmamap_t map;
     97 
     98 	void *isa_ringbuffer;
     99 };
    100 
    101 static int	mace_match(device_t, cfdata_t, void *);
    102 static void	mace_attach(device_t, device_t, void *);
    103 static int	mace_print(void *, const char *);
    104 static int	mace_search(device_t, cfdata_t, const int *, void *);
    105 
    106 CFATTACH_DECL_NEW(mace, sizeof(struct mace_softc),
    107     mace_match, mace_attach, NULL, NULL);
    108 
    109 static void mace_isa_bus_mem_init(bus_space_tag_t, void *);
    110 
    111 static struct mips_bus_space	mace_isa_mbst;
    112 bus_space_tag_t	mace_isa_memt = NULL;
    113 static int mace_isa_init = 0;
    114 
    115 #if defined(BLINK)
    116 static callout_t mace_blink_ch;
    117 static void	mace_blink(void *);
    118 #endif
    119 
    120 static int
    121 mace_match(device_t parent, struct cfdata *match, void *aux)
    122 {
    123 
    124 	/*
    125 	 * The MACE is in the O2.
    126 	 */
    127 	if (mach_type == MACH_SGI_IP32)
    128 		return 1;
    129 
    130 	return 0;
    131 }
    132 
    133 void
    134 mace_init_bus(void)
    135 {
    136 	if (mace_isa_init == 1)
    137 		return;
    138 	mace_isa_init = 1;
    139 	mace_isa_bus_mem_init(&mace_isa_mbst, NULL);
    140 	mace_isa_memt = &mace_isa_mbst;
    141 }
    142 
    143 static void
    144 mace_attach(device_t parent, device_t self, void *aux)
    145 {
    146 	struct mace_softc *sc = device_private(self);
    147 	struct mainbus_attach_args *ma = aux;
    148 	uint32_t scratch;
    149 
    150 	sc->sc_dev = self;
    151 #ifdef BLINK
    152 	callout_init(&mace_blink_ch, 0);
    153 #endif
    154 
    155 	sc->iot = normal_memt;	/* for mace registers */
    156 	sc->dmat = &sgimips_default_bus_dma_tag;
    157 
    158 	if (bus_space_map(sc->iot, ma->ma_addr, 0,
    159 	    BUS_SPACE_MAP_LINEAR, &sc->ioh))
    160 		panic("mace_attach: could not allocate memory\n");
    161 
    162 	aprint_normal("\n");
    163 
    164 	aprint_debug("%s: isa sts %#"PRIx64"\n", device_xname(self),
    165 	    bus_space_read_8(sc->iot, sc->ioh, MACE_ISA_INT_STATUS));
    166 	aprint_debug("%s: isa msk %#"PRIx64"\n", device_xname(self),
    167 	    bus_space_read_8(sc->iot, sc->ioh, MACE_ISA_INT_MASK));
    168 
    169 	mace_init_bus();
    170 
    171 	/*
    172 	 * Turn on most ISA interrupts.  These are actually masked and
    173 	 * registered via the CRIME, as the MACE ISA interrupt mask is
    174 	 * really whacky and nigh on impossible to map to a sane autoconfig
    175 	 * scheme.  We do, however, turn off the count/compare timer and RTC
    176 	 * interrupts as they are unused and conflict with the PS/2
    177 	 * keyboard and mouse interrupts.
    178 	 */
    179 
    180 	bus_space_write_8(sc->iot, sc->ioh, MACE_ISA_INT_MASK, 0xffff0aff);
    181 	bus_space_write_8(sc->iot, sc->ioh, MACE_ISA_INT_STATUS, 0);
    182 
    183 	/* set up LED for solid green or blink, if that's your fancy */
    184 	scratch = bus_space_read_8(sc->iot, sc->ioh, MACE_ISA_FLASH_NIC_REG);
    185 	scratch |= MACE_ISA_LED_RED;
    186 	scratch &= ~(MACE_ISA_LED_GREEN);
    187 	bus_space_write_8(sc->iot, sc->ioh, MACE_ISA_FLASH_NIC_REG, scratch);
    188 
    189 #if defined(BLINK)
    190 	mace_blink(sc);
    191 #endif
    192 
    193 	/* Initialize the maceintr elements to sane values */
    194 	for (scratch = 0; scratch < MACE_NINTR; scratch++) {
    195 		maceintrtab[scratch].func = NULL;
    196 		maceintrtab[scratch].irq = 0;
    197 	}
    198 
    199 	config_search(self, NULL,
    200 	    CFARG_SEARCH, mace_search,
    201 	    CFARG_EOL);
    202 }
    203 
    204 
    205 static int
    206 mace_print(void *aux, const char *pnp)
    207 {
    208 	struct mace_attach_args *maa = aux;
    209 
    210 	if (pnp != 0)
    211 		return QUIET;
    212 
    213 	if (maa->maa_offset != MACECF_OFFSET_DEFAULT)
    214 		aprint_normal(" offset 0x%lx", maa->maa_offset);
    215 	if (maa->maa_intr != MACECF_INTR_DEFAULT)
    216 		aprint_normal(" intr %d", maa->maa_intr);
    217 	if (maa->maa_offset != MACECF_INTRMASK_DEFAULT)
    218 		aprint_normal(" intrmask 0x%x", maa->maa_intrmask);
    219 
    220 	return UNCONF;
    221 }
    222 
    223 static int
    224 mace_search(device_t parent, struct cfdata *cf, const int *ldesc, void *aux)
    225 {
    226 	struct mace_softc *sc = device_private(parent);
    227 	struct mace_attach_args maa;
    228 	int tryagain;
    229 
    230 	do {
    231 		maa.maa_offset = cf->cf_loc[MACECF_OFFSET];
    232 		maa.maa_intr = cf->cf_loc[MACECF_INTR];
    233 		maa.maa_intrmask = cf->cf_loc[MACECF_INTRMASK];
    234 		maa.maa_st = normal_memt;
    235 		maa.maa_sh = sc->ioh;	/* XXX */
    236 		maa.maa_dmat = &sgimips_default_bus_dma_tag;
    237 		maa.isa_ringbuffer = sc->isa_ringbuffer;
    238 
    239 		tryagain = 0;
    240 		if (config_probe(parent, cf, &maa)) {
    241 			config_attach(parent, cf, &maa, mace_print, CFARG_EOL);
    242 			tryagain = (cf->cf_fstate == FSTATE_STAR);
    243 		}
    244 
    245 	} while (tryagain);
    246 
    247 	return 0;
    248 }
    249 
    250 void *
    251 mace_intr_establish(int intr, int level, int (*func)(void *), void *arg)
    252 {
    253 	int i;
    254 
    255 	if (intr < 0 || intr >= 16)
    256 		panic("invalid interrupt number");
    257 
    258 	for (i = 0; i < MACE_NINTR; i++)
    259 		if (maceintrtab[i].func == NULL) {
    260 		        maceintrtab[i].func = func;
    261 		        maceintrtab[i].arg = arg;
    262 			maceintrtab[i].irq = (1 << intr);
    263 			maceintrtab[i].intrmask = level;
    264 			snprintf(maceintrtab[i].evname,
    265 			    sizeof(maceintrtab[i].evname),
    266 			    "intr %d lv 0x%x", intr, level);
    267 			evcnt_attach_dynamic(&maceintrtab[i].evcnt,
    268 			    EVCNT_TYPE_INTR, NULL,
    269 			    "mace", maceintrtab[i].evname);
    270 			break;
    271 		}
    272 
    273 	crime_intr_mask(intr);
    274 	aprint_debug("mace: established interrupt %d (level %x)\n",
    275 	    intr, level);
    276 	return (void *)&maceintrtab[i];
    277 }
    278 
    279 void
    280 mace_intr_disestablish(void *cookie)
    281 {
    282 	int intr = -1, level = 0, irq = 0, i;
    283 
    284 	for (i = 0; i < MACE_NINTR; i++)
    285 		if (&maceintrtab[i] == cookie) {
    286 			evcnt_detach(&maceintrtab[i].evcnt);
    287 			for (intr = 0;
    288 			    maceintrtab[i].irq == (1 << intr); intr++);
    289 			level = maceintrtab[i].intrmask;
    290 			irq = maceintrtab[i].irq;
    291 
    292 			maceintrtab[i].irq = 0;
    293 			maceintrtab[i].intrmask = 0;
    294 		        maceintrtab[i].func = NULL;
    295 		        maceintrtab[i].arg = NULL;
    296 			memset(&maceintrtab[i].evcnt, 0, sizeof (struct evcnt));
    297 			memset(&maceintrtab[i].evname, 0,
    298 			    sizeof (maceintrtab[i].evname));
    299 			break;
    300 		}
    301 	if (intr == -1)
    302 		panic("mace: lost maceintrtab");
    303 
    304 	/* do not do an unmask when irq is shared. */
    305 	for (i = 0; i < MACE_NINTR; i++)
    306 		if (&maceintrtab[i].func != NULL && maceintrtab[i].irq == irq)
    307 			break;
    308 	if (i == MACE_NINTR)
    309 		crime_intr_unmask(intr);
    310 	aprint_debug("mace: disestablished interrupt %d (level %x)\n",
    311 	    intr, level);
    312 }
    313 
    314 void
    315 mace_intr(int irqs)
    316 {
    317 	uint64_t isa_irq;
    318 	int i;
    319 
    320 	/* irq 4 is the ISA cascade interrupt.  Must handle with care. */
    321 	if (irqs & (1 << 4)) {
    322 		isa_irq = mips3_ld(MIPS_PHYS_TO_KSEG1(MACE_BASE
    323 		    + MACE_ISA_INT_STATUS));
    324 		for (i = 0; i < MACE_NINTR; i++) {
    325 			if ((maceintrtab[i].irq == (1 << 4)) &&
    326 			    (isa_irq & maceintrtab[i].intrmask)) {
    327 		  		(maceintrtab[i].func)(maceintrtab[i].arg);
    328 				maceintrtab[i].evcnt.ev_count++;
    329 	        	}
    330 		}
    331 		irqs &= ~(1 << 4);
    332 	}
    333 
    334 	for (i = 0; i < MACE_NINTR; i++)
    335 		if ((irqs & maceintrtab[i].irq)) {
    336 		  	(maceintrtab[i].func)(maceintrtab[i].arg);
    337 			maceintrtab[i].evcnt.ev_count++;
    338 		}
    339 }
    340 
    341 #if defined(BLINK)
    342 static void
    343 mace_blink(void *self)
    344 {
    345 	struct mace_softc *sc = device_private(self);
    346 	register int s;
    347 	int value;
    348 
    349 	s = splhigh();
    350 	value = bus_space_read_8(sc->iot, sc->ioh, MACE_ISA_FLASH_NIC_REG);
    351 	value ^= MACE_ISA_LED_GREEN;
    352 	bus_space_write_8(sc->iot, sc->ioh, MACE_ISA_FLASH_NIC_REG, value);
    353 	splx(s);
    354 	/*
    355 	 * Blink rate is:
    356 	 *      full cycle every second if completely idle (loadav = 0)
    357 	 *      full cycle every 2 seconds if loadav = 1
    358 	 *      full cycle every 3 seconds if loadav = 2
    359 	 * etc.
    360 	 */
    361 	s = (((averunnable.ldavg[0] + FSCALE) * hz) >> (FSHIFT + 1));
    362 	callout_reset(&mace_blink_ch, s, mace_blink, sc);
    363 
    364 }
    365 #endif
    366 
    367 #define CHIP	   		mace_isa
    368 #define	CHIP_MEM		/* defined */
    369 #define CHIP_ALIGN_STRIDE	8
    370 #define CHIP_ACCESS_SIZE	8
    371 #define	CHIP_W1_BUS_START(v)	0x00000000UL
    372 #define CHIP_W1_BUS_END(v)	0xffffffffUL
    373 #define	CHIP_W1_SYS_START(v)	0x00000000UL
    374 #define	CHIP_W1_SYS_END(v)	0xffffffffUL
    375 
    376 #include <mips/mips/bus_space_alignstride_chipdep.c>
    377