1 /* $NetBSD: sun3.c,v 1.11 2020/06/20 18:46:14 riastradh 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 *
dev3_mapin(int maptype,u_long physaddr,int length)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 * Note that this assumes that standalone programs will do I/O
151 * operations only within range (SA_MIN_VA .. SA_MAX_VA) checked.
152 */
153
154 #define DVMA_BASE 0xFFf00000
155 #define DVMA_MAPLEN 0xE0000 /* 1 MB - 128K (save MONSHORTSEG) */
156
157 #define SA_MIN_VA 0x200000
158 #define SA_MAX_VA (SA_MIN_VA + DVMA_MAPLEN)
159
160 /* This points to the end of the free DVMA space. */
161 u_int dvma3_end = DVMA_BASE + DVMA_MAPLEN;
162
163 static void
dvma3_init(void)164 dvma3_init(void)
165 {
166 int segva, dmava, sme;
167
168 segva = SA_MIN_VA;
169 dmava = DVMA_BASE;
170
171 while (segva < SA_MAX_VA) {
172 sme = sun3_get_segmap(segva);
173 sun3_set_segmap(dmava, sme);
174 segva += NBSG;
175 dmava += NBSG;
176 }
177 }
178
179 /* Convert a local address to a DVMA address. */
180 static char *
dvma3_mapin(char * addr,int len)181 dvma3_mapin(char *addr, int len)
182 {
183 int va = (int)addr;
184
185 /* Make sure the address is in the DVMA map. */
186 if ((va < SA_MIN_VA) || (va >= SA_MAX_VA))
187 panic("dvma3_mapin");
188
189 va -= SA_MIN_VA;
190 va += DVMA_BASE;
191
192 return ((char *) va);
193 }
194
195 /* Destroy a DVMA address alias. */
196 static void
dvma3_mapout(char * addr,int len)197 dvma3_mapout(char *addr, int len)
198 {
199 int va = (int)addr;
200
201 /* Make sure the address is in the DVMA map. */
202 if ((va < DVMA_BASE) || (va >= (DVMA_BASE + DVMA_MAPLEN)))
203 panic("dvma3_mapout");
204 }
205
206 static char *
dvma3_alloc(int len)207 dvma3_alloc(int len)
208 {
209 len = m68k_round_page(len);
210 dvma3_end -= len;
211 return((char*)dvma3_end);
212 }
213
214 static void
dvma3_free(char * dvma,int len)215 dvma3_free(char *dvma, int len)
216 {
217 /* not worth the trouble */
218 }
219
220 /*****************************************************************
221 * Control space stuff...
222 */
223
224 #ifdef DEBUG_PROM
225 static u_int
sun3_get_pte(vaddr_t va)226 sun3_get_pte(vaddr_t va)
227 {
228 va = CONTROL_ADDR_BUILD(PGMAP_BASE, va);
229 return (get_control_word(va));
230 }
231 #endif
232
233 static void
sun3_set_pte(vaddr_t va,u_int pte)234 sun3_set_pte(vaddr_t va, u_int pte)
235 {
236 va = CONTROL_ADDR_BUILD(PGMAP_BASE, va);
237 set_control_word(va, pte);
238 }
239
240 static int
sun3_get_segmap(vaddr_t va)241 sun3_get_segmap(vaddr_t va)
242 {
243 va = CONTROL_ADDR_BUILD(SEGMAP_BASE, va);
244 return (get_control_byte(va));
245 }
246
247 static void
sun3_set_segmap(vaddr_t va,int sme)248 sun3_set_segmap(vaddr_t va, int sme)
249 {
250 va = CONTROL_ADDR_BUILD(SEGMAP_BASE, va);
251 set_control_byte(va, sme);
252 }
253
254 /*
255 * Copy the IDPROM contents into the passed buffer.
256 * The caller (idprom.c) will do the checksum.
257 */
258 void
sun3_getidprom(u_char * dst)259 sun3_getidprom(u_char *dst)
260 {
261 vaddr_t src; /* control space address */
262 int len, x;
263
264 src = IDPROM_BASE;
265 len = sizeof(struct idprom);
266 do {
267 x = get_control_byte(src++);
268 *dst++ = x;
269 } while (--len > 0);
270 }
271
272 /*****************************************************************
273 * Init our function pointers, etc.
274 */
275
276 void
sun3_init(void)277 sun3_init(void)
278 {
279
280 /* Set the function pointers. */
281 dev_mapin_p = dev3_mapin;
282 dvma_alloc_p = dvma3_alloc;
283 dvma_free_p = dvma3_free;
284 dvma_mapin_p = dvma3_mapin;
285 dvma_mapout_p = dvma3_mapout;
286
287 /* Prepare DVMA segment. */
288 dvma3_init();
289 }
290