Home | History | Annotate | Line # | Download | only in newsmips
      1  1.23    andvar /*	$NetBSD: news5000.c,v 1.23 2024/06/02 13:28:44 andvar Exp $	*/
      2   1.1    tsubai 
      3   1.1    tsubai /*-
      4   1.1    tsubai  * Copyright (C) 1999 SHIMIZU Ryo.  All rights reserved.
      5   1.1    tsubai  *
      6   1.1    tsubai  * Redistribution and use in source and binary forms, with or without
      7   1.1    tsubai  * modification, are permitted provided that the following conditions
      8   1.1    tsubai  * are met:
      9   1.1    tsubai  * 1. Redistributions of source code must retain the above copyright
     10   1.1    tsubai  *    notice, this list of conditions and the following disclaimer.
     11   1.1    tsubai  * 2. Redistributions in binary form must reproduce the above copyright
     12   1.1    tsubai  *    notice, this list of conditions and the following disclaimer in the
     13   1.1    tsubai  *    documentation and/or other materials provided with the distribution.
     14   1.1    tsubai  * 3. The name of the author may not be used to endorse or promote products
     15   1.1    tsubai  *    derived from this software without specific prior written permission.
     16   1.1    tsubai  *
     17   1.1    tsubai  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18   1.1    tsubai  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19   1.1    tsubai  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20   1.1    tsubai  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21   1.1    tsubai  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22   1.1    tsubai  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23   1.1    tsubai  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24   1.1    tsubai  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25   1.1    tsubai  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     26   1.1    tsubai  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27   1.1    tsubai  */
     28  1.11     lukem 
     29  1.11     lukem #include <sys/cdefs.h>
     30  1.23    andvar __KERNEL_RCSID(0, "$NetBSD: news5000.c,v 1.23 2024/06/02 13:28:44 andvar Exp $");
     31   1.1    tsubai 
     32  1.18      matt #define __INTR_PRIVATE
     33   1.1    tsubai #include <sys/param.h>
     34   1.1    tsubai #include <sys/systm.h>
     35   1.1    tsubai #include <sys/kernel.h>
     36  1.16   tsutsui #include <sys/timetc.h>
     37  1.17        ad #include <sys/cpu.h>
     38  1.18      matt #include <sys/intr.h>
     39   1.1    tsubai 
     40  1.21  christos #include <mips/locore.h>
     41  1.21  christos 
     42   1.1    tsubai #include <machine/adrsmap.h>
     43   1.1    tsubai 
     44   1.1    tsubai #include <newsmips/apbus/apbusvar.h>
     45   1.1    tsubai #include <newsmips/newsmips/machid.h>
     46   1.1    tsubai 
     47   1.9   tsutsui static void news5000_level1_intr(void);
     48   1.9   tsutsui static void news5000_level0_intr(void);
     49   1.7      matt 
     50   1.9   tsutsui static void news5000_enable_intr(void);
     51   1.9   tsutsui static void news5000_disable_intr(void);
     52  1.12   tsutsui static void news5000_enable_timer(void);
     53  1.14   tsutsui static void news5000_readidrom(uint8_t *);
     54  1.16   tsutsui static void news5000_tc_init(void);
     55  1.16   tsutsui static uint32_t news5000_getfreerun(struct timecounter *);
     56   1.6      onoe 
     57   1.1    tsubai /*
     58  1.19   tsutsui  * This is a mask of bits to clear in the SR when we go to a
     59  1.19   tsutsui  * given interrupt priority level.
     60  1.19   tsutsui  */
     61  1.19   tsutsui static const struct ipl_sr_map news5000_ipl_sr_map = {
     62  1.19   tsutsui     .sr_bits = {
     63  1.19   tsutsui 	[IPL_NONE] =		0,
     64  1.19   tsutsui 	[IPL_SOFTCLOCK] =	MIPS_SOFT_INT_MASK_0,
     65  1.19   tsutsui 	[IPL_SOFTNET] =		MIPS_SOFT_INT_MASK,
     66  1.19   tsutsui 	[IPL_VM] =		MIPS_SOFT_INT_MASK
     67  1.19   tsutsui 				| MIPS_INT_MASK_0
     68  1.19   tsutsui 				| MIPS_INT_MASK_1,
     69  1.19   tsutsui 	[IPL_SCHED] =		MIPS_SOFT_INT_MASK
     70  1.19   tsutsui 				| MIPS_INT_MASK_0
     71  1.19   tsutsui 				| MIPS_INT_MASK_1
     72  1.19   tsutsui 				| MIPS_INT_MASK_2,
     73  1.19   tsutsui 	[IPL_DDB] =		MIPS_INT_MASK,
     74  1.19   tsutsui 	[IPL_HIGH] =		MIPS_INT_MASK,
     75  1.19   tsutsui     },
     76  1.19   tsutsui };
     77  1.19   tsutsui 
     78  1.19   tsutsui /*
     79   1.1    tsubai  * Handle news5000 interrupts.
     80   1.1    tsubai  */
     81   1.4    tsubai void
     82  1.18      matt news5000_intr(int ppl, vaddr_t pc, uint32_t status)
     83   1.1    tsubai {
     84  1.18      matt 	uint32_t ipending;
     85  1.18      matt 	int ipl;
     86  1.17        ad 
     87  1.18      matt 	while (ppl < (ipl = splintr(&ipending))) {
     88  1.17        ad 
     89  1.18      matt 		if (ipending & MIPS_INT_MASK_2) {
     90   1.2    tsubai #ifdef DEBUG
     91  1.18      matt 			static int l2cnt = 0;
     92   1.2    tsubai #endif
     93  1.18      matt 			uint32_t int2stat;
     94   1.2    tsubai 
     95  1.18      matt 			int2stat = *(volatile uint32_t *)NEWS5000_INTST2;
     96   1.2    tsubai 
     97   1.2    tsubai #ifdef DEBUG
     98  1.18      matt 			l2cnt++;
     99  1.18      matt 			if (l2cnt == 50) {
    100  1.18      matt 				*(volatile uint32_t *)NEWS5000_LED_SEC = 1;
    101  1.18      matt 			}
    102  1.18      matt 			if (l2cnt == 100) {
    103  1.18      matt 				*(volatile uint32_t *)NEWS5000_LED_SEC = 0;
    104  1.18      matt 				l2cnt = 0;
    105  1.18      matt 			}
    106   1.2    tsubai #endif
    107   1.2    tsubai 
    108  1.18      matt 			if (int2stat & NEWS5000_INT2_TIMER0) {
    109  1.20   tsutsui 				struct clockframe cf = {
    110  1.20   tsutsui 					.pc = pc,
    111  1.20   tsutsui 					.sr = status,
    112  1.20   tsutsui 					.intr = (curcpu()->ci_idepth > 1),
    113  1.20   tsutsui 				};
    114  1.18      matt 				*(volatile uint32_t *)NEWS5000_TIMER0 = 1;
    115  1.18      matt 				hardclock(&cf);
    116  1.18      matt 				intrcnt[HARDCLOCK_INTR]++;
    117  1.18      matt 			}
    118   1.2    tsubai 
    119  1.18      matt 			apbus_wbflush();
    120   1.2    tsubai 		}
    121   1.2    tsubai 
    122  1.18      matt 		if (ipending & MIPS_INT_MASK_5) {
    123  1.18      matt 			uint32_t int5stat;
    124   1.1    tsubai 
    125  1.18      matt 			int5stat = *(volatile u_int *)NEWS5000_INTST5;
    126  1.18      matt 			printf("level5 interrupt (%08x)\n", int5stat);
    127  1.14   tsutsui 
    128  1.18      matt 			apbus_wbflush();
    129  1.18      matt 		}
    130   1.2    tsubai 
    131  1.18      matt 		if (ipending & MIPS_INT_MASK_4) {
    132  1.18      matt 			uint32_t int4stat;
    133   1.1    tsubai 
    134  1.18      matt 			int4stat = *(volatile uint32_t *)NEWS5000_INTST4;
    135  1.18      matt 			printf("level4 interrupt (%08x)\n", int4stat);
    136  1.18      matt 			if (int4stat & NEWS5000_INT4_APBUS) {
    137  1.18      matt 				uint32_t stat;
    138  1.18      matt 
    139  1.18      matt 				stat = *(volatile uint32_t *)NEWS5000_APBUS_INTST;
    140  1.18      matt 				printf("APbus error 0x%04x\n", stat & 0xffff);
    141  1.18      matt 				if (stat & NEWS5000_APBUS_INT_DMAADDR) {
    142  1.18      matt 					printf("DMA Address Error: "
    143  1.18      matt 					    "slot=%x, addr=0x%08x\n",
    144  1.18      matt 					    *(volatile uint32_t *)NEWS5000_APBUS_DER_S,
    145  1.18      matt 					    *(volatile uint32_t *)NEWS5000_APBUS_DER_A);
    146  1.18      matt 				}
    147  1.18      matt 				if (stat & NEWS5000_APBUS_INT_RDTIMEO)
    148  1.18      matt 					printf("IO Read Timeout: addr=0x%08x\n",
    149  1.18      matt 					    *(volatile uint32_t *)NEWS5000_APBUS_BER_A);
    150  1.18      matt 				if (stat & NEWS5000_APBUS_INT_WRTIMEO)
    151  1.18      matt 					printf("IO Write Timeout: addr=0x%08x\n",
    152  1.18      matt 					    *(volatile uint32_t *)NEWS5000_APBUS_BER_A);
    153  1.18      matt 				*(volatile uint32_t *)0xb4c00014 = stat;
    154  1.18      matt 			}
    155  1.14   tsutsui 
    156  1.18      matt 			apbus_wbflush();
    157   1.6      onoe 		}
    158   1.2    tsubai 
    159  1.18      matt 		if (ipending & MIPS_INT_MASK_3) {
    160  1.18      matt 			uint32_t int3stat;
    161   1.1    tsubai 
    162  1.18      matt 			int3stat = *(volatile uint32_t *)NEWS5000_INTST3;
    163  1.18      matt 			printf("level3 interrupt (%08x)\n", int3stat);
    164  1.14   tsutsui 
    165  1.18      matt 			apbus_wbflush();
    166  1.18      matt 		}
    167   1.2    tsubai 
    168  1.18      matt 		if (ipending & MIPS_INT_MASK_1) {
    169  1.18      matt 			news5000_level1_intr();
    170  1.18      matt 			apbus_wbflush();
    171  1.18      matt 		}
    172   1.1    tsubai 
    173  1.18      matt 		if (ipending & MIPS_INT_MASK_0) {
    174  1.18      matt 			news5000_level0_intr();
    175  1.18      matt 			apbus_wbflush();
    176  1.18      matt 		}
    177   1.1    tsubai 	}
    178   1.1    tsubai }
    179   1.1    tsubai 
    180   1.1    tsubai 
    181   1.7      matt static void
    182   1.9   tsutsui news5000_level1_intr(void)
    183   1.1    tsubai {
    184  1.14   tsutsui 	uint32_t int1stat;
    185   1.1    tsubai 
    186  1.14   tsutsui 	int1stat = *(volatile uint32_t *)NEWS5000_INTST1;
    187   1.1    tsubai 
    188   1.1    tsubai 	if (int1stat) {
    189  1.10   tsutsui 		if (apbus_intr_dispatch(1, int1stat) == 0)
    190   1.3    tsubai 			printf("level1_intr: no handler (mask 0x%04x)\n",
    191   1.2    tsubai 			       int1stat);
    192   1.2    tsubai 	} else
    193   1.1    tsubai 		printf("level1 stray interrupt?\n");
    194   1.1    tsubai }
    195   1.1    tsubai 
    196   1.7      matt static void
    197   1.9   tsutsui news5000_level0_intr(void)
    198   1.1    tsubai {
    199  1.14   tsutsui 	uint32_t int0stat;
    200   1.1    tsubai 
    201  1.14   tsutsui 	int0stat = *(volatile uint32_t *)NEWS5000_INTST0;
    202   1.1    tsubai 
    203   1.1    tsubai 	if (int0stat) {
    204  1.10   tsutsui 		if (apbus_intr_dispatch(0, int0stat) == 0)
    205   1.2    tsubai 			printf("level0_intr: no handler (mask 0x%04x)\n",
    206   1.2    tsubai 			       int0stat);
    207   1.2    tsubai 	} else
    208   1.1    tsubai 		printf("level0 stray interrupt?\n");
    209   1.1    tsubai }
    210   1.1    tsubai 
    211   1.7      matt static void
    212   1.9   tsutsui news5000_enable_intr(void)
    213   1.1    tsubai {
    214   1.2    tsubai 
    215   1.6      onoe 	/* INT0 and INT1 has been enabled at attach */
    216   1.6      onoe 	/* INT2 -- It's not a time to enable timer yet. */
    217   1.6      onoe 	/* INT3 -- not used for NWS-5000 */
    218   1.2    tsubai 
    219  1.14   tsutsui 	*(volatile uint32_t *)NEWS5000_INTEN4 = NEWS5000_INT4_APBUS;
    220  1.14   tsutsui 	*(volatile uint32_t *)NEWS5000_APBUS_INTMSK = 0xffff;
    221   1.2    tsubai 
    222   1.6      onoe 	/* INT5 -- currently ignored */
    223  1.14   tsutsui 	*(volatile uint32_t *)NEWS5000_INTEN5 = 0;
    224   1.1    tsubai }
    225   1.1    tsubai 
    226   1.7      matt static void
    227   1.9   tsutsui news5000_disable_intr(void)
    228   1.1    tsubai {
    229   1.9   tsutsui 
    230  1.14   tsutsui 	*(volatile uint32_t *)NEWS5000_INTEN0 = 0;
    231  1.14   tsutsui 	*(volatile uint32_t *)NEWS5000_INTEN1 = 0;
    232  1.14   tsutsui 	*(volatile uint32_t *)NEWS5000_INTEN2 = 0;
    233  1.14   tsutsui 	*(volatile uint32_t *)NEWS5000_INTEN3 = 0;
    234  1.14   tsutsui 	*(volatile uint32_t *)NEWS5000_INTEN4 = 0;
    235  1.14   tsutsui 	*(volatile uint32_t *)NEWS5000_INTEN5 = 0;
    236   1.1    tsubai }
    237   1.1    tsubai 
    238   1.7      matt static void
    239  1.12   tsutsui news5000_enable_timer(void)
    240  1.12   tsutsui {
    241  1.12   tsutsui 
    242  1.16   tsutsui 	news5000_tc_init();
    243  1.16   tsutsui 
    244  1.23    andvar 	/* enable timer interrupt */
    245  1.13   tsutsui 	*(volatile uint32_t *)NEWS5000_INTEN2 = NEWS5000_INT2_TIMER0;
    246  1.12   tsutsui }
    247  1.12   tsutsui 
    248  1.16   tsutsui static uint32_t
    249  1.16   tsutsui news5000_getfreerun(struct timecounter *tc)
    250  1.16   tsutsui {
    251  1.16   tsutsui 	return *(volatile uint32_t *)NEWS5000_FREERUN;
    252  1.16   tsutsui }
    253  1.16   tsutsui 
    254  1.12   tsutsui static void
    255  1.16   tsutsui news5000_tc_init(void)
    256   1.6      onoe {
    257  1.16   tsutsui 	static struct timecounter tc = {
    258  1.16   tsutsui 		.tc_get_timecount = news5000_getfreerun,
    259  1.16   tsutsui 		.tc_frequency = 1000000,
    260  1.16   tsutsui 		.tc_counter_mask = ~0,
    261  1.16   tsutsui 		.tc_name = "news5000_freerun",
    262  1.16   tsutsui 		.tc_quality = 100,
    263  1.16   tsutsui 	};
    264   1.6      onoe 
    265  1.16   tsutsui 	tc_init(&tc);
    266   1.6      onoe }
    267  1.16   tsutsui 
    268   1.6      onoe 
    269   1.7      matt static void
    270  1.14   tsutsui news5000_readidrom(uint8_t *rom)
    271   1.1    tsubai {
    272  1.13   tsutsui 	uint32_t *p = (void *)NEWS5000_IDROM;
    273   1.1    tsubai 	int i;
    274   1.1    tsubai 
    275  1.14   tsutsui 	for (i = 0; i < sizeof(struct idrom); i++, p += 2)
    276   1.1    tsubai 		*rom++ = ((*p & 0x0f) << 4) + (*(p + 1) & 0x0f);
    277   1.1    tsubai }
    278   1.1    tsubai 
    279   1.1    tsubai extern struct idrom idrom;
    280   1.1    tsubai 
    281   1.1    tsubai void
    282   1.7      matt news5000_init(void)
    283   1.1    tsubai {
    284   1.1    tsubai 
    285  1.19   tsutsui 	ipl_sr_map = news5000_ipl_sr_map;
    286  1.19   tsutsui 
    287   1.9   tsutsui 	enable_intr = news5000_enable_intr;
    288   1.9   tsutsui 	disable_intr = news5000_disable_intr;
    289  1.12   tsutsui 	enable_timer = news5000_enable_timer;
    290   1.9   tsutsui 
    291  1.22   tsutsui 	news_wbflush = (uint32_t *)NEWS5000_WBFLUSH;
    292  1.22   tsutsui 
    293  1.14   tsutsui 	news5000_readidrom((uint8_t *)&idrom);
    294   1.5    tsubai 	hostid = idrom.id_serial;
    295   1.8   tsutsui 
    296   1.8   tsutsui 	/* XXX reset uPD72067 FDC to avoid spurious interrupts */
    297   1.8   tsutsui #define NEWS5000_FDC_FDOUT 0xbed20000
    298   1.8   tsutsui #define FDO_FRST 0x04
    299  1.13   tsutsui 	*(volatile uint8_t *)NEWS5000_FDC_FDOUT = FDO_FRST;
    300   1.1    tsubai }
    301