xref: /netbsd-src/sys/arch/hp300/dev/sti_sgc.c (revision 8448487e704668efb4dc7a0968d659270341fd19)
1 /*	$NetBSD: sti_sgc.c,v 1.8 2023/01/15 06:19:45 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 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: sti_sgc.c,v 1.8 2023/01/15 06:19:45 tsutsui Exp $");
31 
32 #include <sys/param.h>
33 #include <sys/device.h>
34 #include <sys/bus.h>
35 
36 #include <uvm/uvm_extern.h>
37 
38 #include <dev/wscons/wsconsio.h>
39 #include <dev/wscons/wsdisplayvar.h>
40 
41 #include <dev/ic/stireg.h>
42 #include <dev/ic/stivar.h>
43 
44 #include <hp300/dev/sgcvar.h>
45 #include <hp300/dev/sti_sgcvar.h>
46 #include <machine/autoconf.h>
47 
48 struct sti_sgc_softc {
49 	struct sti_softc sc_sti;
50 
51 	paddr_t sc_bitmap;
52 };
53 
54 /*
55  * 425e EVRX specific hardware
56  */
57 /*
58  * EVRX RAMDAC (Bt458) is found at offset 0x060000 from SGC bus PA and
59  * offset 0x040000 length 0x1c0000 is mapped in MI sti via ROM region 2
60  */
61 #define STI_EVRX_REGNO2OFFSET	0x020000
62 #define STI_EVRX_FBOFFSET	0x200000
63 
64 #define EVRX_BT458_ADDR		(STI_EVRX_REGNO2OFFSET + 0x200 + 2)
65 #define EVRX_BT458_CMAP		(STI_EVRX_REGNO2OFFSET + 0x204 + 2)
66 #define EVRX_BT458_CTRL		(STI_EVRX_REGNO2OFFSET + 0x208 + 2)
67 #define EVRX_BT458_OMAP		(STI_EVRX_REGNO2OFFSET + 0x20C + 2)
68 
69 /* from HP-UX /usr/lib/libddevrx.a */
70 #define EVRX_MAGIC00		(STI_EVRX_REGNO2OFFSET + 0x600)
71 #define EVRX_MAGIC04		(STI_EVRX_REGNO2OFFSET + 0x604)
72 #define EVRX_MAGIC08		(STI_EVRX_REGNO2OFFSET + 0x608)
73 #define EVRX_MAGIC0C		(STI_EVRX_REGNO2OFFSET + 0x60c)
74 #define EVRX_MAGIC10		(STI_EVRX_REGNO2OFFSET + 0x610)
75 #define EVRX_MAGIC10_BSY	0x00010000
76 #define EVRX_MAGIC18		(STI_EVRX_REGNO2OFFSET + 0x618)
77 #define EVRX_MAGIC1C		(STI_EVRX_REGNO2OFFSET + 0x61c)
78 
79 /*
80  * HP A1659A CRX specific hardware
81  */
82 #define STI_CRX_FBOFFSET	0x01000000
83 
84 static int sticonslot = -1;
85 static struct bus_space_tag sticn_tag;
86 static struct sti_rom sticn_rom;
87 static struct sti_screen sticn_scr;
88 static bus_addr_t sticn_bases[STI_REGION_MAX];
89 
90 static int sti_sgc_match(device_t, struct cfdata *, void *);
91 static void sti_sgc_attach(device_t, device_t, void *);
92 
93 static int sti_sgc_probe(bus_space_tag_t, int);
94 
95 CFATTACH_DECL_NEW(sti_sgc, sizeof(struct sti_sgc_softc),
96     sti_sgc_match, sti_sgc_attach, NULL, NULL);
97 
98 /* 425e EVRX/CRX specific access functions */
99 static int sti_evrx_putcmap(struct sti_screen *, u_int, u_int);
100 static void sti_evrx_resetramdac(struct sti_screen *);
101 static void sti_evrx_resetcmap(struct sti_screen *);
102 static void sti_evrx_setupfb(struct sti_screen *);
103 static paddr_t sti_m68k_mmap(void *, void *, off_t, int);
104 
105 static const struct wsdisplay_accessops sti_m68k_accessops = {
106 	sti_ioctl,
107 	sti_m68k_mmap,
108 	sti_alloc_screen,
109 	sti_free_screen,
110 	sti_show_screen,
111 	sti_load_font
112 };
113 
114 static int
sti_sgc_match(device_t parent,struct cfdata * cf,void * aux)115 sti_sgc_match(device_t parent, struct cfdata *cf, void *aux)
116 {
117 	struct sgc_attach_args *saa = aux;
118 
119 	/*
120 	 * If we already probed it successfully as a console device, go ahead,
121 	 * since we will not be able to bus_space_map() again.
122 	 */
123 	if (saa->saa_slot == sticonslot)
124 		return 1;
125 
126 	return sti_sgc_probe(saa->saa_iot, saa->saa_slot);
127 }
128 
129 static void
sti_sgc_attach(device_t parent,device_t self,void * aux)130 sti_sgc_attach(device_t parent, device_t self, void *aux)
131 {
132 	struct sti_sgc_softc *sc = device_private(self);
133 	struct sti_softc *ssc = &sc->sc_sti;
134 	struct sgc_attach_args *saa = aux;
135 	struct sti_screen *scr;
136 	bus_space_tag_t bst;
137 	bus_space_handle_t romh;
138 	bus_addr_t base;
139 	struct wsemuldisplaydev_attach_args waa;
140 	u_int romend;
141 	struct sti_dd *rom_dd;
142 	uint32_t grid0;
143 	int i;
144 
145 	ssc->sc_dev = self;
146 	bst = saa->saa_iot;
147 	base = (bus_addr_t)sgc_slottopa(saa->saa_slot);
148 
149 	if (saa->saa_slot == sticonslot) {
150 		ssc->sc_flags |= STI_CONSOLE | STI_ATTACHED;
151 		ssc->sc_rom = &sticn_rom;
152 		ssc->sc_rom->rom_softc = ssc;
153 		ssc->sc_scr = &sticn_scr;
154 		ssc->sc_scr->scr_rom = ssc->sc_rom;
155 		memcpy(ssc->bases, sticn_bases, sizeof(ssc->bases));
156 
157 		sti_describe(ssc);
158 	} else {
159 		if (bus_space_map(bst, base, PAGE_SIZE, 0, &romh)) {
160 			aprint_error(": can't map ROM");
161 			return;
162 		}
163 		/*
164 		 * Compute real PROM size
165 		 */
166 		romend = sti_rom_size(bst, romh);
167 
168 		bus_space_unmap(bst, romh, PAGE_SIZE);
169 
170 		if (bus_space_map(bst, base, romend, 0, &romh)) {
171 			aprint_error(": can't map frame buffer");
172 			return;
173 		}
174 
175 		ssc->bases[0] = romh;
176 		for (i = 0; i < STI_REGION_MAX; i++)
177 			ssc->bases[i] = base;
178 
179 		if (sti_attach_common(ssc, bst, bst, romh,
180 		    STI_CODEBASE_ALT) != 0)
181 			return;
182 	}
183 
184 	/* Identify the board model by dd_grid */
185 	rom_dd = &ssc->sc_rom->rom_dd;
186 	grid0 = rom_dd->dd_grid[0];
187 	scr = ssc->sc_scr;
188 
189 	switch (grid0) {
190 	case STI_DD_EVRX:
191 		/*
192 		 * 425e on-board EVRX framebuffer.
193 		 * bitmap memory can be accessed at offset +0x200000.
194 		 */
195 		sc->sc_bitmap = base + STI_EVRX_FBOFFSET;
196 
197 		aprint_normal_dev(self, "Enable mmap support\n");
198 
199 		/*
200 		 * initialize Bt458 RAMDAC and preserve initial color map
201 		 */
202 		sti_evrx_resetramdac(scr);
203 		sti_evrx_resetcmap(scr);
204 
205 		scr->setupfb = sti_evrx_setupfb;
206 		scr->putcmap = sti_evrx_putcmap;
207 
208 		scr->scr_wsmode = WSDISPLAYIO_MODE_EMUL;
209 		waa.console = ssc->sc_flags & STI_CONSOLE ? 1 : 0;
210 		waa.scrdata = &scr->scr_screenlist;
211 		waa.accessops = &sti_m68k_accessops;
212 		waa.accesscookie = scr;
213 
214 		config_found(ssc->sc_dev, &waa, wsemuldisplaydevprint,
215 		    CFARGS_NONE);
216 		break;
217 
218 	case STI_DD_CRX:
219 		/*
220 		 * HP A1659A CRX on some 425t variants.
221 		 * bitmap memory can be accessed at offset +0x1000000.
222 		 */
223 		sc->sc_bitmap = base + STI_CRX_FBOFFSET;
224 
225 		aprint_normal_dev(self, "Enable mmap support\n");
226 
227 		scr->scr_wsmode = WSDISPLAYIO_MODE_EMUL;
228 		waa.console = ssc->sc_flags & STI_CONSOLE ? 1 : 0;
229 		waa.scrdata = &scr->scr_screenlist;
230 		waa.accessops = &sti_m68k_accessops;
231 		waa.accesscookie = scr;
232 
233 		config_found(ssc->sc_dev, &waa, wsemuldisplaydevprint,
234 		    CFARGS_NONE);
235 		break;
236 	default:
237 		/*
238 		 * Unsupported variants.
239 		 * Use default common sti(4) attachment (no bitmap support).
240 		 */
241 		sti_end_attach(ssc);
242 		break;
243 	}
244 }
245 
246 static int
sti_sgc_probe(bus_space_tag_t iot,int slot)247 sti_sgc_probe(bus_space_tag_t iot, int slot)
248 {
249 	bus_space_handle_t ioh;
250 	int devtype;
251 
252 	if (bus_space_map(iot, (bus_addr_t)sgc_slottopa(slot),
253 	    PAGE_SIZE, 0, &ioh))
254 		return 0;
255 
256 	devtype = bus_space_read_1(iot, ioh, 3);
257 
258 	bus_space_unmap(iot, ioh, PAGE_SIZE);
259 
260 	/*
261 	 * This might not be reliable enough. On the other hand, non-STI
262 	 * SGC cards will apparently not initialize in the hp300, to the
263 	 * point of not even answering bus probes (checked with an
264 	 * Harmony/FDDI SGC card).
265 	 */
266 	if (devtype != STI_DEVTYPE1 && devtype != STI_DEVTYPE4)
267 		return 0;
268 
269 	return 1;
270 }
271 
272 static int
sti_evrx_putcmap(struct sti_screen * scr,u_int index,u_int count)273 sti_evrx_putcmap(struct sti_screen *scr, u_int index, u_int count)
274 {
275 	struct sti_rom *rom = scr->scr_rom;
276 	bus_space_tag_t bst = rom->memt;
277 	bus_space_handle_t bsh = rom->regh[2];
278 	int i;
279 
280 	/* magic setup from HP-UX */
281 	bus_space_write_4(bst, bsh, EVRX_MAGIC08, 0x00000001);
282 	bus_space_write_4(bst, bsh, EVRX_MAGIC00, 0x00000001);
283 	for (i = index; i < index + count; i++) {
284 		/* this is what HP-UX woodDownloadCmap() does */
285 		while ((bus_space_read_4(bst, bsh, EVRX_MAGIC10) &
286 		    EVRX_MAGIC10_BSY) != 0)
287 			continue;
288 		bus_space_write_1(bst, bsh, EVRX_BT458_ADDR, i);
289 		bus_space_write_1(bst, bsh, EVRX_BT458_CMAP, scr->scr_rcmap[i]);
290 		bus_space_write_1(bst, bsh, EVRX_BT458_CMAP, scr->scr_gcmap[i]);
291 		bus_space_write_4(bst, bsh, EVRX_MAGIC10,   scr->scr_bcmap[i]);
292 	}
293 	return 0;
294 }
295 
296 static void
sti_evrx_resetramdac(struct sti_screen * scr)297 sti_evrx_resetramdac(struct sti_screen *scr)
298 {
299 	struct sti_rom *rom = scr->scr_rom;
300 	bus_space_tag_t bst = rom->memt;
301 	bus_space_handle_t bsh = rom->regh[2];
302 #if 0
303 	int i;
304 #endif
305 
306 	/*
307 	 * Initialize the Bt458.  When we write to control registers,
308 	 * the address is not incremented automatically. So we specify
309 	 * it ourselves for each control register.
310 	 */
311 
312 	/* all planes will be read */
313 	bus_space_write_1(bst, bsh, EVRX_BT458_ADDR, 0x04);
314 	bus_space_write_1(bst, bsh, EVRX_BT458_CTRL, 0xff);
315 
316 	/* all planes have non-blink */
317 	bus_space_write_1(bst, bsh, EVRX_BT458_ADDR, 0x05);
318 	bus_space_write_1(bst, bsh, EVRX_BT458_CTRL, 0x00);
319 
320 	/* palette enabled, ovly plane disabled */
321 	bus_space_write_1(bst, bsh, EVRX_BT458_ADDR, 0x06);
322 	bus_space_write_1(bst, bsh, EVRX_BT458_CTRL, 0x40);
323 
324 	/* no test mode */
325 	bus_space_write_1(bst, bsh, EVRX_BT458_ADDR, 0x07);
326 	bus_space_write_1(bst, bsh, EVRX_BT458_CTRL, 0x00);
327 
328 	/* magic initialization from HP-UX woodInitializeHardware() */
329 	bus_space_write_4(bst, bsh, EVRX_MAGIC00, 0x00000001);
330 	bus_space_write_4(bst, bsh, EVRX_MAGIC04, 0x00000001);
331 	bus_space_write_4(bst, bsh, EVRX_MAGIC08, 0x00000001);
332 	bus_space_write_4(bst, bsh, EVRX_MAGIC0C, 0x00000001);
333 	bus_space_write_4(bst, bsh, EVRX_MAGIC18, 0xFFFFFFFF);
334 	bus_space_write_4(bst, bsh, EVRX_MAGIC1C, 0x00000000);
335 
336 #if 0
337 	bus_space_write_1(bst, bsh, EVRX_BT458_ADDR, 0x00);
338 	for (i = 0; i < 4; i++) {
339 		bus_space_write_1(bst, bsh, EVRX_BT458_OMAP, 0x00);
340 		bus_space_write_1(bst, bsh, EVRX_BT458_OMAP, 0x00);
341 		bus_space_write_1(bst, bsh, EVRX_BT458_OMAP, 0x00);
342 	}
343 #endif
344 }
345 
346 static void
sti_evrx_resetcmap(struct sti_screen * scr)347 sti_evrx_resetcmap(struct sti_screen *scr)
348 {
349 	struct sti_rom *rom = scr->scr_rom;
350 	bus_space_tag_t bst = rom->memt;
351 	bus_space_handle_t bsh = rom->regh[2];
352 	int i;
353 
354 	/* magic setup from HP-UX */
355 	bus_space_write_4(bst, bsh, EVRX_MAGIC08, 0x00000001);
356 	bus_space_write_4(bst, bsh, EVRX_MAGIC00, 0x00000001);
357 
358 	/* preserve palette values initialized by STI firmware */
359 	for (i = 0; i < STI_NCMAP; i++) {
360 		/* this is what HP-UX woodUploadCmap() does */
361 		while ((bus_space_read_4(bst, bsh, EVRX_MAGIC10) &
362 		    EVRX_MAGIC10_BSY) != 0)
363 			continue;
364 		bus_space_write_1(bst, bsh, EVRX_BT458_ADDR, i);
365 		scr->scr_rcmap[i] = bus_space_read_1(bst, bsh, EVRX_BT458_CMAP);
366 		scr->scr_gcmap[i] = bus_space_read_1(bst, bsh, EVRX_BT458_CMAP);
367 		scr->scr_bcmap[i] = bus_space_read_1(bst, bsh, EVRX_BT458_CMAP);
368 	}
369 }
370 
371 static void
sti_evrx_setupfb(struct sti_screen * scr)372 sti_evrx_setupfb(struct sti_screen *scr)
373 {
374 
375 	sti_init(scr, 0);
376 	sti_evrx_resetramdac(scr);
377 }
378 
379 static paddr_t
sti_m68k_mmap(void * v,void * vs,off_t offset,int prot)380 sti_m68k_mmap(void *v, void *vs, off_t offset, int prot)
381 {
382 	struct sti_screen *scr = (struct sti_screen *)v;
383 	struct sti_rom *rom = scr->scr_rom;
384 	struct sti_softc *ssc = rom->rom_softc;
385 	struct sti_sgc_softc *sc = device_private(ssc->sc_dev);
386 	paddr_t cookie = -1;
387 
388 	if ((offset & PAGE_MASK) != 0)
389 		return -1;
390 
391 	switch (scr->scr_wsmode) {
392 	case WSDISPLAYIO_MODE_MAPPED:
393 		/* not implemented yet; what should be shown? */
394 		break;
395 	case WSDISPLAYIO_MODE_DUMBFB:
396 		if (offset >= 0 && offset < (scr->fbwidth * scr->fbheight))
397 			cookie = m68k_btop(sc->sc_bitmap + offset);
398 		break;
399 	default:
400 		break;
401 	}
402 
403 	return cookie;
404 }
405 
406 int
sti_sgc_cnprobe(bus_space_tag_t bst,bus_addr_t addr,int slot)407 sti_sgc_cnprobe(bus_space_tag_t bst, bus_addr_t addr, int slot)
408 {
409 	void *va;
410 	bus_space_handle_t romh;
411 	int devtype, rv = 0;
412 
413 	if (bus_space_map(bst, addr, PAGE_SIZE, 0, &romh))
414 		return 0;
415 
416 	va = bus_space_vaddr(bst, romh);
417 	if (badaddr(va))
418 		goto out;
419 
420 	devtype = bus_space_read_1(bst, romh, 3);
421 	if (devtype == STI_DEVTYPE1 || devtype == STI_DEVTYPE4)
422 		rv = 1;
423 
424  out:
425 	bus_space_unmap(bst, romh, PAGE_SIZE);
426 	return rv;
427 }
428 
429 void
sti_sgc_cnattach(bus_space_tag_t bst,bus_addr_t addr,int slot)430 sti_sgc_cnattach(bus_space_tag_t bst, bus_addr_t addr, int slot)
431 {
432 	int i;
433 
434 	sticn_tag = *bst;
435 
436 	/* sticn_bases[0] will be fixed in sti_cnattach() */
437 	for (i = 0; i < STI_REGION_MAX; i++)
438 		sticn_bases[i] = addr;
439 
440 	sti_cnattach(&sticn_rom, &sticn_scr, &sticn_tag, sticn_bases,
441 	    STI_CODEBASE_ALT);
442 
443 	sticonslot = slot;
444 }
445