sti_machdep.c revision 1.4 1 /* $NetBSD: sti_machdep.c,v 1.4 2025/05/25 02:08:03 tsutsui Exp $ */
2 /* $OpenBSD: sti_sgc.c,v 1.14 2007/05/26 00:36:03 krw Exp $ */
3
4 /*
5 * Copyright (c) 2005, Miodrag Vallat
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
29 /*-
30 * Copyright (c) 2020, 2025 Izumi Tsutsui. All rights reserved.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 *
41 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
42 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
44 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
45 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
47 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
48 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
49 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
50 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51 */
52
53 #include <sys/cdefs.h>
54 __KERNEL_RCSID(0, "$NetBSD: sti_machdep.c,v 1.4 2025/05/25 02:08:03 tsutsui Exp $");
55
56 #include <sys/param.h>
57 #include <sys/device.h>
58 #include <sys/bus.h>
59
60 #include <dev/wscons/wsconsio.h>
61
62 #include <hp300/dev/sti_machdep.h>
63
64 /*
65 * 425e and 362/382 EVRX specific hardware
66 */
67 /*
68 * RAMDAC (Bt458) on 425e EVRX is found at offset 0x00060000 from SGC bus PA.
69 * RAMDAC (Bt474) on 362/382 EVRX is also found at offset 0x00060000
70 * from DIO scode (132) + STI_DIO_SCODE_OFFSET PA (i.e. 0x01800000).
71 *
72 * Offset 0x040000 length 0x1c0000 is mapped in MI sti via ROM region 2
73 * on both 425e and 362/382.
74 */
75 #define STI_EVRX_REGNO2OFFSET 0x00020000 /* 0x00060000 - 0x00040000 */
76
77 /* bitmap memory can be accessed at offset +0x200000 */
78 #define STI_EVRX_FBOFFSET 0x00200000
79
80 #define EVRX_BT458_ADDR (STI_EVRX_REGNO2OFFSET + 0x200 + 2)
81 #define EVRX_BT458_CMAP (STI_EVRX_REGNO2OFFSET + 0x204 + 2)
82 #define EVRX_BT458_CTRL (STI_EVRX_REGNO2OFFSET + 0x208 + 2)
83 #define EVRX_BT458_OMAP (STI_EVRX_REGNO2OFFSET + 0x20C + 2)
84
85 /* These registers are partially common between Bt474 and Bt458 */
86 #define EVRX_BT4xx_ADDR EVRX_BT458_ADDR
87 #define EVRX_BT4xx_CMAP EVRX_BT458_CMAP
88 #define EVRX_BT4xx_CTRL EVRX_BT458_CTRL
89
90 /* from HP-UX /usr/lib/libddevrx.a */
91 #define EVRX_MAGIC00 (STI_EVRX_REGNO2OFFSET + 0x600)
92 #define EVRX_MAGIC04 (STI_EVRX_REGNO2OFFSET + 0x604)
93 #define EVRX_MAGIC08 (STI_EVRX_REGNO2OFFSET + 0x608)
94 #define EVRX_MAGIC0C (STI_EVRX_REGNO2OFFSET + 0x60c)
95 #define EVRX_MAGIC10 (STI_EVRX_REGNO2OFFSET + 0x610)
96 #define EVRX_MAGIC10_BSY 0x00010000
97 #define EVRX_MAGIC18 (STI_EVRX_REGNO2OFFSET + 0x618)
98 #define EVRX_MAGIC1C (STI_EVRX_REGNO2OFFSET + 0x61c)
99
100 /*
101 * HP A1659A CRX specific hardware
102 */
103 /* bitmap memory can be accessed at offset +0x1000000 */
104 #define STI_CRX_FBOFFSET 0x01000000
105
106 /* hp300 EVRX and CRX specific access functions */
107 static int sti_evrx_putcmap(struct sti_screen *, u_int, u_int);
108 static void sti_evrx_resetramdac(struct sti_screen *);
109 static void sti_evrx_resetcmap(struct sti_screen *);
110 static void sti_evrx_setupfb(struct sti_screen *);
111 static paddr_t sti_m68k_mmap(void *, void *, off_t, int);
112
113 static struct bus_space_tag sticn_tag;
114 static struct sti_rom sticn_rom;
115 static struct sti_screen sticn_scr;
116 static bus_addr_t sticn_bases[STI_REGION_MAX];
117
118 static const struct wsdisplay_accessops sti_m68k_accessops = {
119 sti_ioctl,
120 sti_m68k_mmap,
121 sti_alloc_screen,
122 sti_free_screen,
123 sti_show_screen,
124 sti_load_font
125 };
126
127 void
128 sti_machdep_attach_console(struct sti_machdep_softc *sc)
129 {
130 struct sti_softc *ssc = &sc->sc_sti;
131
132 ssc->sc_flags |= STI_CONSOLE | STI_ATTACHED;
133 ssc->sc_rom = &sticn_rom;
134 ssc->sc_rom->rom_softc = ssc;
135 ssc->sc_scr = &sticn_scr;
136 ssc->sc_scr->scr_rom = ssc->sc_rom;
137 memcpy(ssc->bases, sticn_bases, sizeof(ssc->bases));
138
139 sti_describe(ssc);
140 }
141
142 void
143 sti_machdep_attach(struct sti_machdep_softc *sc)
144 {
145 struct sti_softc *ssc = &sc->sc_sti;
146 struct sti_screen *scr;
147 paddr_t base = sc->sc_base;
148 struct wsemuldisplaydev_attach_args waa;
149 struct sti_dd *rom_dd;
150 uint32_t grid0;
151
152 /* Identify the board model by dd_grid */
153 rom_dd = &ssc->sc_rom->rom_dd;
154 grid0 = rom_dd->dd_grid[0];
155 scr = ssc->sc_scr;
156
157 switch (grid0) {
158 case STI_DD_EVRX:
159 case STI_DD_382C:
160 case STI_DD_3X2V:
161 /*
162 * 425e and 362/382 on-board EVRX framebuffer.
163 */
164 sc->sc_bitmap = base + STI_EVRX_FBOFFSET;
165
166 aprint_normal_dev(ssc->sc_dev, "Enable mmap support\n");
167
168 /*
169 * initialize Bt458/474 RAMDAC and preserve initial color map
170 */
171 sti_evrx_resetramdac(scr);
172 sti_evrx_resetcmap(scr);
173
174 scr->setupfb = sti_evrx_setupfb;
175 scr->putcmap = sti_evrx_putcmap;
176
177 scr->scr_wsmode = WSDISPLAYIO_MODE_EMUL;
178 waa.console = ssc->sc_flags & STI_CONSOLE ? 1 : 0;
179 waa.scrdata = &scr->scr_screenlist;
180 waa.accessops = &sti_m68k_accessops;
181 waa.accesscookie = scr;
182
183 config_found(ssc->sc_dev, &waa, wsemuldisplaydevprint,
184 CFARGS_NONE);
185 break;
186
187 case STI_DD_CRX:
188 /*
189 * HP A1659A CRX on some 425t variants.
190 */
191 sc->sc_bitmap = base + STI_CRX_FBOFFSET;
192
193 aprint_normal_dev(ssc->sc_dev, "Enable mmap support\n");
194
195 scr->scr_wsmode = WSDISPLAYIO_MODE_EMUL;
196 waa.console = ssc->sc_flags & STI_CONSOLE ? 1 : 0;
197 waa.scrdata = &scr->scr_screenlist;
198 waa.accessops = &sti_m68k_accessops;
199 waa.accesscookie = scr;
200
201 config_found(ssc->sc_dev, &waa, wsemuldisplaydevprint,
202 CFARGS_NONE);
203 break;
204 default:
205 /*
206 * Unsupported variants.
207 * Use default common sti(4) attachment (no bitmap support).
208 */
209 sti_end_attach(ssc);
210 break;
211 }
212 }
213
214 static int
215 sti_evrx_putcmap(struct sti_screen *scr, u_int index, u_int count)
216 {
217 struct sti_rom *rom = scr->scr_rom;
218 bus_space_tag_t bst = rom->memt;
219 bus_space_handle_t bsh = rom->regh[2];
220 int i;
221
222 /* magic setup from HP-UX */
223 bus_space_write_4(bst, bsh, EVRX_MAGIC08, 0x00000001);
224 bus_space_write_4(bst, bsh, EVRX_MAGIC00, 0x00000001);
225 for (i = index; i < index + count; i++) {
226 /* this is what HP-UX woodDownloadCmap() does */
227 while ((bus_space_read_4(bst, bsh, EVRX_MAGIC10) &
228 EVRX_MAGIC10_BSY) != 0)
229 continue;
230 bus_space_write_1(bst, bsh, EVRX_BT4xx_ADDR, i);
231 bus_space_write_1(bst, bsh, EVRX_BT4xx_CMAP, scr->scr_rcmap[i]);
232 bus_space_write_1(bst, bsh, EVRX_BT4xx_CMAP, scr->scr_gcmap[i]);
233 bus_space_write_4(bst, bsh, EVRX_MAGIC10, scr->scr_bcmap[i]);
234 }
235 return 0;
236 }
237
238 static void
239 sti_evrx_resetramdac(struct sti_screen *scr)
240 {
241 struct sti_rom *rom = scr->scr_rom;
242 bus_space_tag_t bst = rom->memt;
243 bus_space_handle_t bsh = rom->regh[2];
244 #if 0
245 int i;
246 #endif
247
248 /*
249 * Initialize the Bt458. When we write to control registers,
250 * the address is not incremented automatically. So we specify
251 * it ourselves for each control register.
252 */
253
254 /* all planes will be read */
255 bus_space_write_1(bst, bsh, EVRX_BT4xx_ADDR, 0x04);
256 bus_space_write_1(bst, bsh, EVRX_BT4xx_CTRL, 0xff);
257
258 #if 0
259 /*
260 * HP-UX woodInitializeHardware() doesn't touch these
261 * Bt458 specific registers. Maybe initialized by STI ROM?
262 */
263 /* all planes have non-blink */
264 bus_space_write_1(bst, bsh, EVRX_BT458_ADDR, 0x05);
265 bus_space_write_1(bst, bsh, EVRX_BT458_CTRL, 0x00);
266
267 /* palette enabled, ovly plane disabled */
268 bus_space_write_1(bst, bsh, EVRX_BT458_ADDR, 0x06);
269 bus_space_write_1(bst, bsh, EVRX_BT458_CTRL, 0x40);
270
271 /* no test mode */
272 bus_space_write_1(bst, bsh, EVRX_BT458_ADDR, 0x07);
273 bus_space_write_1(bst, bsh, EVRX_BT458_CTRL, 0x00);
274 #endif
275
276 /* magic initialization from HP-UX woodInitializeHardware() */
277 bus_space_write_4(bst, bsh, EVRX_MAGIC00, 0x00000001);
278 bus_space_write_4(bst, bsh, EVRX_MAGIC04, 0x00000001);
279 bus_space_write_4(bst, bsh, EVRX_MAGIC08, 0x00000001);
280 bus_space_write_4(bst, bsh, EVRX_MAGIC0C, 0x00000001);
281 bus_space_write_4(bst, bsh, EVRX_MAGIC18, 0xFFFFFFFF);
282 bus_space_write_4(bst, bsh, EVRX_MAGIC1C, 0x00000000);
283
284 #if 0
285 bus_space_write_1(bst, bsh, EVRX_BT458_ADDR, 0x00);
286 for (i = 0; i < 4; i++) {
287 bus_space_write_1(bst, bsh, EVRX_BT458_OMAP, 0x00);
288 bus_space_write_1(bst, bsh, EVRX_BT458_OMAP, 0x00);
289 bus_space_write_1(bst, bsh, EVRX_BT458_OMAP, 0x00);
290 }
291 #endif
292 }
293
294 static void
295 sti_evrx_resetcmap(struct sti_screen *scr)
296 {
297 struct sti_rom *rom = scr->scr_rom;
298 bus_space_tag_t bst = rom->memt;
299 bus_space_handle_t bsh = rom->regh[2];
300 int i;
301
302 /* magic setup from HP-UX */
303 bus_space_write_4(bst, bsh, EVRX_MAGIC08, 0x00000001);
304 bus_space_write_4(bst, bsh, EVRX_MAGIC00, 0x00000001);
305
306 /* preserve palette values initialized by STI firmware */
307 for (i = 0; i < STI_NCMAP; i++) {
308 /* this is what HP-UX woodUploadCmap() does */
309 while ((bus_space_read_4(bst, bsh, EVRX_MAGIC10) &
310 EVRX_MAGIC10_BSY) != 0)
311 continue;
312 bus_space_write_1(bst, bsh, EVRX_BT458_ADDR, i);
313 scr->scr_rcmap[i] = bus_space_read_1(bst, bsh, EVRX_BT4xx_CMAP);
314 scr->scr_gcmap[i] = bus_space_read_1(bst, bsh, EVRX_BT4xx_CMAP);
315 scr->scr_bcmap[i] = bus_space_read_1(bst, bsh, EVRX_BT4xx_CMAP);
316 }
317 }
318
319 static void
320 sti_evrx_setupfb(struct sti_screen *scr)
321 {
322
323 sti_init(scr, 0);
324 sti_evrx_resetramdac(scr);
325 }
326
327 static paddr_t
328 sti_m68k_mmap(void *v, void *vs, off_t offset, int prot)
329 {
330 struct sti_screen *scr = (struct sti_screen *)v;
331 struct sti_rom *rom = scr->scr_rom;
332 struct sti_softc *ssc = rom->rom_softc;
333 struct sti_machdep_softc *sc = device_private(ssc->sc_dev);
334 paddr_t cookie = -1;
335
336 if ((offset & PAGE_MASK) != 0)
337 return -1;
338
339 switch (scr->scr_wsmode) {
340 case WSDISPLAYIO_MODE_MAPPED:
341 /* not implemented yet; what should be shown? */
342 break;
343 case WSDISPLAYIO_MODE_DUMBFB:
344 if (offset >= 0 && offset < (scr->fbwidth * scr->fbheight))
345 cookie = m68k_btop(sc->sc_bitmap + offset);
346 break;
347 default:
348 break;
349 }
350
351 return cookie;
352 }
353
354 void
355 sti_machdep_cnattach(bus_space_tag_t bst, paddr_t base)
356 {
357 int i;
358
359 sticn_tag = *bst;
360
361 /* sticn_bases[0] will be fixed in sti_cnattach() */
362 for (i = 0; i < STI_REGION_MAX; i++)
363 sticn_bases[i] = (bus_addr_t)base;
364
365 sti_cnattach(&sticn_rom, &sticn_scr, &sticn_tag, sticn_bases,
366 STI_CODEBASE_ALT);
367 }
368