Home | History | Annotate | Line # | Download | only in libsa
      1 /*	$NetBSD: sun3.c,v 1.13 2026/08/19 18:14:08 tsutsui Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Gordon W. Ross.
      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  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Standalone functions specific to the Sun3.
     34  */
     35 
     36 #define _SUN3_ XXX
     37 
     38 /*
     39  * We need to get the sun3 NBSG definition, even if we're
     40  * building this with a different sun68k target.
     41  */
     42 #include <arch/sun3/include/pmap3.h>
     43 
     44 #include <sys/param.h>
     45 #include <machine/idprom.h>
     46 #include <machine/mon.h>
     47 
     48 #include <arch/sun3/include/pte3.h>
     49 #include <arch/sun3/sun3/control.h>
     50 #include <arch/sun3/sun3/vme.h>
     51 
     52 #include <stand.h>
     53 
     54 #include "libsa.h"
     55 #include "dvma.h"
     56 #include "saio.h"	/* enum MAPTYPES */
     57 
     58 #define OBIO_MASK 0xFFFFFF
     59 
     60 #ifdef	DEBUG_PROM
     61 static u_int	sun3_get_pte(vaddr_t);
     62 #endif
     63 static void	sun3_set_pte(vaddr_t, u_int);
     64 static void	dvma3_init(void);
     65 static char *	dvma3_alloc(int);
     66 static void	dvma3_free(char *, int);
     67 static char *	dvma3_mapin(char *, int);
     68 static void	dvma3_mapout(char *, int);
     69 static char *	dev3_mapin(int, u_long, int);
     70 static int	sun3_get_segmap(vaddr_t);
     71 static void	sun3_set_segmap(vaddr_t, int);
     72 
     73 struct mapinfo {
     74 	int maptype;
     75 	int pgtype;
     76 	u_int base;
     77 	u_int mask;
     78 };
     79 
     80 struct mapinfo
     81 sun3_mapinfo[MAP__NTYPES] = {
     82 	/* On-board memory, I/O */
     83 	{ MAP_MAINMEM,   PGT_OBMEM,   0,          ~0 },
     84 	{ MAP_OBIO,      PGT_OBIO,    0,          OBIO_MASK },
     85 	/* Multibus adapter (A24,A16) */
     86 	{ MAP_MBMEM,     PGT_VME_D16, VME24_BASE, VME24_MASK },
     87 	{ MAP_MBIO,      PGT_VME_D16, VME16_BASE, VME16_MASK },
     88 	/* VME A16 */
     89 	{ MAP_VME16A16D, PGT_VME_D16, VME16_BASE, VME16_MASK },
     90 	{ MAP_VME16A32D, PGT_VME_D32, VME16_BASE, VME16_MASK },
     91 	/* VME A24 */
     92 	{ MAP_VME24A16D, PGT_VME_D16, VME24_BASE, VME24_MASK },
     93 	{ MAP_VME24A32D, PGT_VME_D32, VME24_BASE, VME24_MASK },
     94 	/* VME A32 */
     95 	{ MAP_VME32A16D, PGT_VME_D16, VME32_BASE, VME32_MASK },
     96 	{ MAP_VME32A32D, PGT_VME_D32, VME32_BASE, VME32_MASK },
     97 };
     98 
     99 /* The virtual address we will use for PROM device mappings. */
    100 int sun3_devmap = SUN3_MONSHORTSEG;
    101 
    102 static char *
    103 dev3_mapin(int maptype, u_long physaddr, int length)
    104 {
    105 	u_int i, pa, pte, pgva, va;
    106 
    107 	if ((sun3_devmap + length) > SUN3_MONSHORTPAGE)
    108 		panic("dev3_mapin: length=%d", length);
    109 
    110 	for (i = 0; i < MAP__NTYPES; i++)
    111 		if (sun3_mapinfo[i].maptype == maptype)
    112 			goto found;
    113 	panic("dev3_mapin: bad maptype");
    114 found:
    115 
    116 	if (physaddr & ~(sun3_mapinfo[i].mask))
    117 		panic("dev3_mapin: bad address");
    118 	pa = sun3_mapinfo[i].base + physaddr;
    119 
    120 	pte = PA_PGNUM(pa) | PG_PERM | sun3_mapinfo[i].pgtype;
    121 
    122 	va = pgva = sun3_devmap;
    123 	do {
    124 		sun3_set_pte(pgva, pte);
    125 		pgva += NBPG;
    126 		pte += 1;
    127 		length -= NBPG;
    128 	} while (length > 0);
    129 	sun3_devmap = pgva;
    130 	va += (physaddr & PGOFSET);
    131 
    132 #ifdef	DEBUG_PROM
    133 	if (debug)
    134 		printf("dev3_mapin: va=0x%x pte=0x%x\n",
    135 			   va, sun3_get_pte(va));
    136 #endif
    137 	return ((char*)va);
    138 }
    139 
    140 /*****************************************************************
    141  * DVMA support
    142  */
    143 
    144 /*
    145  * The easiest way to deal with the need for DVMA mappings is to
    146  * create a DVMA alias mapping of the entire address range used by
    147  * the boot program.  That way, dvma_mapin can just compute the
    148  * DVMA alias address, and dvma_mapout does nothing.
    149  *
    150  * Leave the lower 256 KB of the PROM DVMA space untouched,
    151  * since 3/60 PROM SCSI drivers may use that range
    152  * (at least around 0xFFF00000 to 0xFF009FFF) on booting kernels
    153  * for their own mappings.
    154  *
    155  * Note that this assumes that standalone programs will do I/O
    156  * operations only within range (SA_MIN_VA .. SA_MAX_VA) checked.
    157  */
    158 
    159 #define DVMA_BASE	0xFFf40000
    160 #define DVMA_MAPLEN 	0xA0000	/* 640 KB (save MONSHORTSEG and PROM work) */
    161 
    162 #define SA_MIN_VA	0x240000
    163 #define SA_MAX_VA	(SA_MIN_VA + DVMA_MAPLEN)
    164 
    165 /* This points to the end of the free DVMA space. */
    166 u_int dvma3_end = DVMA_BASE + DVMA_MAPLEN;
    167 
    168 static void
    169 dvma3_init(void)
    170 {
    171 	int segva, dmava, sme;
    172 
    173 	segva = SA_MIN_VA;
    174 	dmava = DVMA_BASE;
    175 
    176 	while (segva < SA_MAX_VA) {
    177 		sme = sun3_get_segmap(segva);
    178 		sun3_set_segmap(dmava, sme);
    179 		segva += NBSG;
    180 		dmava += NBSG;
    181 	}
    182 }
    183 
    184 /* Convert a local address to a DVMA address. */
    185 static char *
    186 dvma3_mapin(char *addr, int len)
    187 {
    188 	int va = (int)addr;
    189 
    190 	/* Make sure the address is in the DVMA map. */
    191 	if ((va < SA_MIN_VA) || (va >= SA_MAX_VA))
    192 		panic("dvma3_mapin");
    193 
    194 	va -= SA_MIN_VA;
    195 	va += DVMA_BASE;
    196 
    197 	return ((char *) va);
    198 }
    199 
    200 /* Destroy a DVMA address alias. */
    201 static void
    202 dvma3_mapout(char *addr, int len)
    203 {
    204 	int va = (int)addr;
    205 
    206 	/* Make sure the address is in the DVMA map. */
    207 	if ((va < DVMA_BASE) || (va >= (DVMA_BASE + DVMA_MAPLEN)))
    208 		panic("dvma3_mapout");
    209 }
    210 
    211 static char *
    212 dvma3_alloc(int len)
    213 {
    214 	len = m68k_round_page(len);
    215 	dvma3_end -= len;
    216 	return((char*)dvma3_end);
    217 }
    218 
    219 static void
    220 dvma3_free(char *dvma, int len)
    221 {
    222 	/* not worth the trouble */
    223 }
    224 
    225 /*****************************************************************
    226  * Control space stuff...
    227  */
    228 
    229 #ifdef	DEBUG_PROM
    230 static u_int
    231 sun3_get_pte(vaddr_t va)
    232 {
    233 	va = CONTROL_ADDR_BUILD(PGMAP_BASE, va);
    234 	return (get_control_word(va));
    235 }
    236 #endif
    237 
    238 static void
    239 sun3_set_pte(vaddr_t va, u_int pte)
    240 {
    241 	va = CONTROL_ADDR_BUILD(PGMAP_BASE, va);
    242 	set_control_word(va, pte);
    243 }
    244 
    245 static int
    246 sun3_get_segmap(vaddr_t va)
    247 {
    248 	va = CONTROL_ADDR_BUILD(SEGMAP_BASE, va);
    249 	return (get_control_byte(va));
    250 }
    251 
    252 static void
    253 sun3_set_segmap(vaddr_t va, int sme)
    254 {
    255 	va = CONTROL_ADDR_BUILD(SEGMAP_BASE, va);
    256 	set_control_byte(va, sme);
    257 }
    258 
    259 /*
    260  * Copy the IDPROM contents into the passed buffer.
    261  * The caller (idprom.c) will do the checksum.
    262  */
    263 void
    264 sun3_getidprom(u_char *dst)
    265 {
    266 	vaddr_t src;	/* control space address */
    267 	int len, x;
    268 
    269 	src = IDPROM_BASE;
    270 	len = sizeof(struct idprom);
    271 	do {
    272 		x = get_control_byte(src++);
    273 		*dst++ = x;
    274 	} while (--len > 0);
    275 }
    276 
    277 /*****************************************************************
    278  * Init our function pointers, etc.
    279  */
    280 
    281 void
    282 sun3_init(void)
    283 {
    284 
    285 	/* Set the function pointers. */
    286 	dev_mapin_p   = dev3_mapin;
    287 	dvma_alloc_p  = dvma3_alloc;
    288 	dvma_free_p   = dvma3_free;
    289 	dvma_mapin_p  = dvma3_mapin;
    290 	dvma_mapout_p = dvma3_mapout;
    291 
    292 	/* Prepare DVMA segment. */
    293 	dvma3_init();
    294 }
    295