xref: /netbsd-src/sys/arch/hppa/dev/gftfb.c (revision 3117ece4fc4a4ca4489ba793710b60b0d26bab6c)
1 /*	$NetBSD: gftfb.c,v 1.26 2024/10/27 19:23:47 riastradh Exp $	*/
2 
3 /*	$OpenBSD: sti_pci.c,v 1.7 2009/02/06 22:51:04 miod Exp $	*/
4 
5 /*
6  * Copyright (c) 2006, 2007 Miodrag Vallat.
7  ^                     2024 Michael Lorenz
8  *
9  * Permission to use, copy, modify, and distribute this software for any
10  * purpose with or without fee is hereby granted, provided that the above
11  * copyright notice, this permission notice, and the disclaimer below
12  * appear in all copies.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
17  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21  */
22 
23 /*
24  * a native driver for HP Visualize EG PCI graphics cards
25  * STI portions are from Miodrag Vallat's sti_pci.c
26  */
27 
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/kmem.h>
31 #include <sys/device.h>
32 #include <sys/mutex.h>
33 
34 #include <dev/pci/pcivar.h>
35 #include <dev/pci/pcireg.h>
36 #include <dev/pci/pcidevs.h>
37 #include <dev/pci/pciio.h>
38 
39 #include <dev/wscons/wsdisplayvar.h>
40 #include <dev/wscons/wsconsio.h>
41 #include <dev/wsfont/wsfont.h>
42 #include <dev/rasops/rasops.h>
43 #include <dev/wscons/wsdisplay_vconsvar.h>
44 #include <dev/pci/wsdisplay_pci.h>
45 #include <dev/wscons/wsdisplay_glyphcachevar.h>
46 
47 #include <dev/ic/stireg.h>
48 #include <dev/ic/stivar.h>
49 
50 #include "opt_gftfb.h"
51 
52 #ifdef GFTFB_DEBUG
53 #define	DPRINTF(s) printf(s)
54 #else
55 #define	DPRINTF(s) /* */
56 #endif
57 
58 int	gftfb_match(device_t, cfdata_t, void *);
59 void	gftfb_attach(device_t, device_t, void *);
60 
61 struct	gftfb_softc {
62 	device_t		sc_dev;
63 	pci_chipset_tag_t	sc_pc;
64 	pcitag_t		sc_tag;
65 
66 	/* stuff we need in order to use the STI ROM */
67 	struct sti_softc	sc_base;
68 	struct sti_screen 	sc_scr;
69 	bus_space_handle_t	sc_romh;
70 
71 	int sc_width, sc_height;
72 	int sc_locked;
73 	struct vcons_screen sc_console_screen;
74 	struct wsscreen_descr sc_defaultscreen_descr;
75 	const struct wsscreen_descr *sc_screens[1];
76 	struct wsscreen_list sc_screenlist;
77 	struct vcons_data vd;
78 	int sc_mode;
79 	void (*sc_putchar)(void *, int, int, u_int, long);
80 	u_char sc_cmap_red[256];
81 	u_char sc_cmap_green[256];
82 	u_char sc_cmap_blue[256];
83 	kmutex_t sc_hwlock;
84 	uint32_t sc_hwmode;
85 #define HW_FB	0
86 #define HW_FILL	1
87 #define HW_BLIT	2
88 	/* cursor stuff */
89 	int sc_cursor_x, sc_cursor_y;
90 	int sc_hot_x, sc_hot_y, sc_enabled;
91 	int sc_video_on;
92 	glyphcache sc_gc;
93 };
94 
95 CFATTACH_DECL_NEW(gftfb, sizeof(struct gftfb_softc),
96     gftfb_match, gftfb_attach, NULL, NULL);
97 
98 int	gftfb_readbar(struct sti_softc *, struct pci_attach_args *, u_int, int);
99 int	gftfb_check_rom(struct gftfb_softc *, struct pci_attach_args *);
100 void	gftfb_enable_rom(struct sti_softc *);
101 void	gftfb_disable_rom(struct sti_softc *);
102 void	gftfb_enable_rom_internal(struct gftfb_softc *);
103 void	gftfb_disable_rom_internal(struct gftfb_softc *);
104 
105 void 	gftfb_setup(struct gftfb_softc *);
106 
107 #define	ngle_bt458_write(memt, memh, r, v) \
108 	bus_space_write_stream_4(memt, memh, NGLE_REG_RAMDAC + ((r) << 2), (v) << 24)
109 
110 
111 /* XXX these really need to go into their own header */
112 int	sti_pci_is_console(struct pci_attach_args *, bus_addr_t *);
113 int	sti_rom_setup(struct sti_rom *, bus_space_tag_t, bus_space_tag_t,
114 	    bus_space_handle_t, bus_addr_t *, u_int);
115 int	sti_screen_setup(struct sti_screen *, int);
116 void	sti_describe_screen(struct sti_softc *, struct sti_screen *);
117 
118 #define PCI_ROM_SIZE(mr)                                                \
119             (PCI_MAPREG_ROM_ADDR(mr) & -PCI_MAPREG_ROM_ADDR(mr))
120 
121 /* wsdisplay stuff */
122 static int	gftfb_ioctl(void *, void *, u_long, void *, int,
123 			     struct lwp *);
124 static paddr_t	gftfb_mmap(void *, void *, off_t, int);
125 static void	gftfb_init_screen(void *, struct vcons_screen *, int, long *);
126 
127 static int	gftfb_putcmap(struct gftfb_softc *, struct wsdisplay_cmap *);
128 static int 	gftfb_getcmap(struct gftfb_softc *, struct wsdisplay_cmap *);
129 static void	gftfb_restore_palette(struct gftfb_softc *);
130 static int 	gftfb_putpalreg(struct gftfb_softc *, uint8_t, uint8_t,
131 			    uint8_t, uint8_t);
132 
133 static void	gftfb_rectfill(struct gftfb_softc *, int, int, int, int,
134 			    uint32_t);
135 static void	gftfb_bitblt(void *, int, int, int, int, int,
136 			    int, int);
137 
138 static void	gftfb_cursor(void *, int, int, int);
139 static void	gftfb_putchar(void *, int, int, u_int, long);
140 static void	gftfb_putchar_aa(void *, int, int, u_int, long);
141 static void	gftfb_copycols(void *, int, int, int, int);
142 static void	gftfb_erasecols(void *, int, int, int, long);
143 static void	gftfb_copyrows(void *, int, int, int);
144 static void	gftfb_eraserows(void *, int, int, long);
145 
146 static void	gftfb_move_cursor(struct gftfb_softc *, int, int);
147 static int	gftfb_do_cursor(struct gftfb_softc *, struct wsdisplay_cursor *);
148 
149 static void	gftfb_set_video(struct gftfb_softc *, int);
150 
151 struct wsdisplay_accessops gftfb_accessops = {
152 	gftfb_ioctl,
153 	gftfb_mmap,
154 	NULL,	/* alloc_screen */
155 	NULL,	/* free_screen */
156 	NULL,	/* show_screen */
157 	NULL, 	/* load_font */
158 	NULL,	/* pollc */
159 	NULL	/* scroll */
160 };
161 
162 static inline void gftfb_wait_fifo(struct gftfb_softc *, uint32_t);
163 
164 int
165 gftfb_match(device_t parent, cfdata_t cf, void *aux)
166 {
167 	struct pci_attach_args *paa = aux;
168 
169 	if (PCI_VENDOR(paa->pa_id) != PCI_VENDOR_HP)
170 		return 0;
171 
172 	if (PCI_PRODUCT(paa->pa_id) == PCI_PRODUCT_HP_VISUALIZE_EG)
173 		return 10;	/* beat out sti at pci */
174 
175 	return 0;
176 }
177 
178 static inline uint32_t
179 gftfb_read4(struct gftfb_softc *sc, uint32_t offset)
180 {
181 	struct sti_rom *rom = sc->sc_base.sc_rom;
182 	bus_space_tag_t memt = rom->memt;
183 	bus_space_handle_t memh = rom->regh[2];
184 	return bus_space_read_stream_4(memt, memh, offset);
185 }
186 
187 static inline void
188 gftfb_write4(struct gftfb_softc *sc, uint32_t offset, uint32_t val)
189 {
190 	struct sti_rom *rom = sc->sc_base.sc_rom;
191 	bus_space_tag_t memt = rom->memt;
192 	bus_space_handle_t memh = rom->regh[2];
193 	bus_space_write_stream_4(memt, memh, offset, val);
194 }
195 
196 static inline uint8_t
197 gftfb_read1(struct gftfb_softc *sc, uint32_t offset)
198 {
199 	struct sti_rom *rom = sc->sc_base.sc_rom;
200 	bus_space_tag_t memt = rom->memt;
201 	bus_space_handle_t memh = rom->regh[2];
202 	return bus_space_read_1(memt, memh, offset);
203 }
204 
205 static inline void
206 gftfb_write1(struct gftfb_softc *sc, uint32_t offset, uint8_t val)
207 {
208 	struct sti_rom *rom = sc->sc_base.sc_rom;
209 	bus_space_tag_t memt = rom->memt;
210 	bus_space_handle_t memh = rom->regh[2];
211 	bus_space_write_1(memt, memh, offset, val);
212 }
213 
214 void
215 gftfb_attach(device_t parent, device_t self, void *aux)
216 {
217 	struct gftfb_softc *sc = device_private(self);
218 	struct pci_attach_args *paa = aux;
219 	struct sti_rom *rom;
220 	struct rasops_info *ri;
221 	struct wsemuldisplaydev_attach_args aa;
222 	unsigned long defattr = 0;
223 	int ret, is_console = 0;
224 
225 	sc->sc_dev = self;
226 
227 	sc->sc_pc = paa->pa_pc;
228 	sc->sc_tag = paa->pa_tag;
229 	sc->sc_base.sc_dev = self;
230 	sc->sc_base.sc_enable_rom = gftfb_enable_rom;
231 	sc->sc_base.sc_disable_rom = gftfb_disable_rom;
232 
233 	/* we can *not* be interrupted when doing colour map accesses */
234 	mutex_init(&sc->sc_hwlock, MUTEX_DEFAULT, IPL_HIGH);
235 
236 	aprint_normal("\n");
237 
238 	if (gftfb_check_rom(sc, paa) != 0)
239 		return;
240 
241 	ret = sti_pci_is_console(paa, sc->sc_base. bases);
242 	if (ret != 0) {
243 		sc->sc_base.sc_flags |= STI_CONSOLE;
244 		is_console = 1;
245 	}
246 	rom = (struct sti_rom *)kmem_zalloc(sizeof(*rom), KM_SLEEP);
247 	rom->rom_softc = &sc->sc_base;
248 	ret = sti_rom_setup(rom, paa->pa_iot, paa->pa_memt, sc->sc_romh,
249 	    sc->sc_base.bases, STI_CODEBASE_MAIN);
250 	if (ret != 0) {
251 		kmem_free(rom, sizeof(*rom));
252 		return;
253 	}
254 
255 	sc->sc_base.sc_rom = rom;
256 
257 	sc->sc_scr.scr_rom = sc->sc_base.sc_rom;
258 	ret = sti_screen_setup(&sc->sc_scr, STI_FBMODE);
259 
260 	sc->sc_width = sc->sc_scr.scr_cfg.scr_width;
261 	sc->sc_height = sc->sc_scr.scr_cfg.scr_height;
262 
263 	aprint_normal_dev(sc->sc_dev, "%s at %dx%d\n", sc->sc_scr.name,
264 	    sc->sc_width, sc->sc_height);
265 	gftfb_setup(sc);
266 
267 #ifdef GFTFB_DEBUG
268 	sc->sc_height -= 200;
269 #endif
270 
271 	sc->sc_defaultscreen_descr = (struct wsscreen_descr){
272 		"default",
273 		0, 0,
274 		NULL,
275 		8, 16,
276 		WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_UNDERLINE |
277 		      WSSCREEN_RESIZE,
278 		NULL
279 	};
280 
281 	sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
282 	sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
283 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
284 	sc->sc_locked = 0;
285 
286 	vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr,
287 	    &gftfb_accessops);
288 	sc->vd.init_screen = gftfb_init_screen;
289 	sc->vd.show_screen_cookie = &sc->sc_gc;
290 	sc->vd.show_screen_cb = glyphcache_adapt;
291 
292 	ri = &sc->sc_console_screen.scr_ri;
293 
294 	sc->sc_gc.gc_bitblt = gftfb_bitblt;
295 	sc->sc_gc.gc_blitcookie = sc;
296 	sc->sc_gc.gc_rop = RopSrc;
297 
298 	if (is_console) {
299 		vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
300 		    &defattr);
301 		sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
302 
303 		sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
304 		sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
305 		sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
306 		sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
307 
308 		glyphcache_init(&sc->sc_gc, sc->sc_height + 5,
309 				sc->sc_scr.fbheight - sc->sc_height - 5,
310 				sc->sc_scr.fbwidth,
311 				ri->ri_font->fontwidth,
312 				ri->ri_font->fontheight,
313 				defattr);
314 
315 		wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0,
316 		    defattr);
317 
318 		gftfb_rectfill(sc, 0, 0, sc->sc_width, sc->sc_height,
319 		    ri->ri_devcmap[(defattr >> 16) & 0xff]);
320 
321 		vcons_replay_msgbuf(&sc->sc_console_screen);
322 	} else {
323 		/*
324 		 * since we're not the console we can postpone the rest
325 		 * until someone actually allocates a screen for us
326 		 */
327 		if (sc->sc_console_screen.scr_ri.ri_rows == 0) {
328 			/* do some minimal setup to avoid weirdnesses later */
329 			vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
330 			    &defattr);
331 		} else
332 			(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
333 
334 		glyphcache_init(&sc->sc_gc, sc->sc_height + 5,
335 				sc->sc_scr.fbheight - sc->sc_height - 5,
336 				sc->sc_scr.fbwidth,
337 				ri->ri_font->fontwidth,
338 				ri->ri_font->fontheight,
339 				defattr);
340 	}
341 
342 	gftfb_restore_palette(sc);
343 
344 	/* no suspend/resume support yet */
345 	if (!pmf_device_register(sc->sc_dev, NULL, NULL))
346 		aprint_error_dev(sc->sc_dev,
347 		    "couldn't establish power handler\n");
348 
349 	aa.console = is_console;
350 	aa.scrdata = &sc->sc_screenlist;
351 	aa.accessops = &gftfb_accessops;
352 	aa.accesscookie = &sc->vd;
353 
354 	config_found(sc->sc_dev, &aa, wsemuldisplaydevprint, CFARGS_NONE);
355 }
356 
357 /*
358  * Grovel the STI ROM image.
359  */
360 int
361 gftfb_check_rom(struct gftfb_softc *spc, struct pci_attach_args *pa)
362 {
363 	struct sti_softc *sc = &spc->sc_base;
364 	pcireg_t address, mask;
365 	bus_space_handle_t romh;
366 	bus_size_t romsize, subsize, stiromsize;
367 	bus_addr_t selected, offs, suboffs;
368 	uint32_t tmp;
369 	int i;
370 	int rc;
371 
372 	/* sort of inline sti_pci_enable_rom(sc) */
373 	address = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_MAPREG_ROM);
374 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_MAPREG_ROM,
375 	    ~PCI_MAPREG_ROM_ENABLE);
376 	mask = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_MAPREG_ROM);
377 	address |= PCI_MAPREG_ROM_ENABLE;
378 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_MAPREG_ROM, address);
379 	sc->sc_flags |= STI_ROM_ENABLED;
380 	/*
381 	 * Map the complete ROM for now.
382 	 */
383 
384 	romsize = PCI_ROM_SIZE(mask);
385 	DPRINTF(("%s: mapping rom @ %lx for %lx\n", __func__,
386 	    (long)PCI_MAPREG_ROM_ADDR(address), (long)romsize));
387 
388 	rc = bus_space_map(pa->pa_memt, PCI_MAPREG_ROM_ADDR(address), romsize,
389 	    0, &romh);
390 	if (rc != 0) {
391 		aprint_error_dev(sc->sc_dev, "can't map PCI ROM (%d)\n", rc);
392 		goto fail2;
393 	}
394 
395 	gftfb_disable_rom_internal(spc);
396 	/*
397 	 * Iterate over the ROM images, pick the best candidate.
398 	 */
399 
400 	selected = (bus_addr_t)-1;
401 	for (offs = 0; offs < romsize; offs += subsize) {
402 		gftfb_enable_rom_internal(spc);
403 		/*
404 		 * Check for a valid ROM header.
405 		 */
406 		tmp = bus_space_read_4(pa->pa_memt, romh, offs + 0);
407 		tmp = le32toh(tmp);
408 		if (tmp != 0x55aa0000) {
409 			gftfb_disable_rom_internal(spc);
410 			if (offs == 0) {
411 				aprint_error_dev(sc->sc_dev,
412 				    "invalid PCI ROM header signature (%08x)\n",
413 				     tmp);
414 				rc = EINVAL;
415 			}
416 			break;
417 		}
418 
419 		/*
420 		 * Check ROM type.
421 		 */
422 		tmp = bus_space_read_4(pa->pa_memt, romh, offs + 4);
423 		tmp = le32toh(tmp);
424 		if (tmp != 0x00000001) {	/* 1 == STI ROM */
425 			gftfb_disable_rom_internal(spc);
426 			if (offs == 0) {
427 				aprint_error_dev(sc->sc_dev,
428 				    "invalid PCI ROM type (%08x)\n", tmp);
429 				rc = EINVAL;
430 			}
431 			break;
432 		}
433 
434 		subsize = (bus_addr_t)bus_space_read_2(pa->pa_memt, romh,
435 		    offs + 0x0c);
436 		subsize <<= 9;
437 
438 #ifdef GFTFB_DEBUG
439 		gftfb_disable_rom_internal(spc);
440 		DPRINTF(("ROM offset %08x size %08x type %08x",
441 		    (u_int)offs, (u_int)subsize, tmp));
442 		gftfb_enable_rom_internal(spc);
443 #endif
444 
445 		/*
446 		 * Check for a valid ROM data structure.
447 		 * We do not need it except to know what architecture the ROM
448 		 * code is for.
449 		 */
450 
451 		suboffs = offs +(bus_addr_t)bus_space_read_2(pa->pa_memt, romh,
452 		    offs + 0x18);
453 		tmp = bus_space_read_4(pa->pa_memt, romh, suboffs + 0);
454 		tmp = le32toh(tmp);
455 		if (tmp != 0x50434952) {	/* PCIR */
456 			gftfb_disable_rom_internal(spc);
457 			if (offs == 0) {
458 				aprint_error_dev(sc->sc_dev, "invalid PCI data"
459 				    " signature (%08x)\n", tmp);
460 				rc = EINVAL;
461 			} else {
462 				DPRINTF((" invalid PCI data signature %08x\n",
463 				    tmp));
464 				continue;
465 			}
466 		}
467 
468 		tmp = bus_space_read_1(pa->pa_memt, romh, suboffs + 0x14);
469 		gftfb_disable_rom_internal(spc);
470 		DPRINTF((" code %02x", tmp));
471 
472 		switch (tmp) {
473 #ifdef __hppa__
474 		case 0x10:
475 			if (selected == (bus_addr_t)-1)
476 				selected = offs;
477 			break;
478 #endif
479 #ifdef __i386__
480 		case 0x00:
481 			if (selected == (bus_addr_t)-1)
482 				selected = offs;
483 			break;
484 #endif
485 		default:
486 			DPRINTF((" (wrong architecture)"));
487 			break;
488 		}
489 		DPRINTF(("%s\n", selected == offs ? " -> SELECTED" : ""));
490 	}
491 
492 	if (selected == (bus_addr_t)-1) {
493 		if (rc == 0) {
494 			aprint_error_dev(sc->sc_dev, "found no ROM with "
495 			    "correct microcode architecture\n");
496 			rc = ENOEXEC;
497 		}
498 		goto fail;
499 	}
500 
501 	/*
502 	 * Read the STI region BAR assignments.
503 	 */
504 
505 	gftfb_enable_rom_internal(spc);
506 	offs = selected +
507 	    (bus_addr_t)bus_space_read_2(pa->pa_memt, romh, selected + 0x0e);
508 	for (i = 0; i < STI_REGION_MAX; i++) {
509 		rc = gftfb_readbar(sc, pa, i,
510 		    bus_space_read_1(pa->pa_memt, romh, offs + i));
511 		if (rc != 0)
512 			goto fail;
513 	}
514 
515 	/*
516 	 * Find out where the STI ROM itself lies, and its size.
517 	 */
518 
519 	offs = selected +
520 	    (bus_addr_t)bus_space_read_4(pa->pa_memt, romh, selected + 0x08);
521 	stiromsize = (bus_addr_t)bus_space_read_4(pa->pa_memt, romh,
522 	    offs + 0x18);
523 	stiromsize = le32toh(stiromsize);
524 	gftfb_disable_rom_internal(spc);
525 
526 	/*
527 	 * Replace our mapping with a smaller mapping of only the area
528 	 * we are interested in.
529 	 */
530 
531 	DPRINTF(("remapping rom @ %lx for %lx\n",
532 	    (long)(PCI_MAPREG_ROM_ADDR(address) + offs), (long)stiromsize));
533 	bus_space_unmap(pa->pa_memt, romh, romsize);
534 	rc = bus_space_map(pa->pa_memt, PCI_MAPREG_ROM_ADDR(address) + offs,
535 	    stiromsize, 0, &spc->sc_romh);
536 	if (rc != 0) {
537 		aprint_error_dev(sc->sc_dev, "can't map STI ROM (%d)\n",
538 		    rc);
539 		goto fail2;
540 	}
541  	gftfb_disable_rom_internal(spc);
542 	sc->sc_flags &= ~STI_ROM_ENABLED;
543 
544 	return 0;
545 
546 fail:
547 	bus_space_unmap(pa->pa_memt, romh, romsize);
548 fail2:
549 	gftfb_disable_rom_internal(spc);
550 
551 	return rc;
552 }
553 
554 /*
555  * Decode a BAR register.
556  */
557 int
558 gftfb_readbar(struct sti_softc *sc, struct pci_attach_args *pa, u_int region,
559     int bar)
560 {
561 	bus_addr_t addr;
562 	bus_size_t size;
563 	uint32_t cf;
564 	int rc;
565 
566 	if (bar == 0) {
567 		sc->bases[region] = 0;
568 		return (0);
569 	}
570 
571 #ifdef DIAGNOSTIC
572 	if (bar < PCI_MAPREG_START || bar > PCI_MAPREG_PPB_END) {
573 		gftfb_disable_rom(sc);
574 		printf("%s: unexpected bar %02x for region %d\n",
575 		    device_xname(sc->sc_dev), bar, region);
576 		gftfb_enable_rom(sc);
577 	}
578 #endif
579 
580 	cf = pci_conf_read(pa->pa_pc, pa->pa_tag, bar);
581 
582 	rc = pci_mapreg_info(pa->pa_pc, pa->pa_tag, bar, PCI_MAPREG_TYPE(cf),
583 	    &addr, &size, NULL);
584 
585 	if (rc != 0) {
586 		gftfb_disable_rom(sc);
587 		aprint_error_dev(sc->sc_dev, "invalid bar %02x for region %d\n",
588 		    bar, region);
589 		gftfb_enable_rom(sc);
590 		return (rc);
591 	}
592 
593 	sc->bases[region] = addr;
594 	return (0);
595 }
596 
597 /*
598  * Enable PCI ROM.
599  */
600 void
601 gftfb_enable_rom_internal(struct gftfb_softc *spc)
602 {
603 	pcireg_t address;
604 
605 	KASSERT(spc != NULL);
606 
607 	address = pci_conf_read(spc->sc_pc, spc->sc_tag, PCI_MAPREG_ROM);
608 	address |= PCI_MAPREG_ROM_ENABLE;
609 	pci_conf_write(spc->sc_pc, spc->sc_tag, PCI_MAPREG_ROM, address);
610 }
611 
612 void
613 gftfb_enable_rom(struct sti_softc *sc)
614 {
615 	struct gftfb_softc *spc = device_private(sc->sc_dev);
616 
617 	if (!ISSET(sc->sc_flags, STI_ROM_ENABLED)) {
618 		gftfb_enable_rom_internal(spc);
619 	}
620 	SET(sc->sc_flags, STI_ROM_ENABLED);
621 }
622 
623 /*
624  * Disable PCI ROM.
625  */
626 void
627 gftfb_disable_rom_internal(struct gftfb_softc *spc)
628 {
629 	pcireg_t address;
630 
631 	KASSERT(spc != NULL);
632 
633 	address = pci_conf_read(spc->sc_pc, spc->sc_tag, PCI_MAPREG_ROM);
634 	address &= ~PCI_MAPREG_ROM_ENABLE;
635 	pci_conf_write(spc->sc_pc, spc->sc_tag, PCI_MAPREG_ROM, address);
636 }
637 
638 void
639 gftfb_disable_rom(struct sti_softc *sc)
640 {
641 	struct gftfb_softc *spc = device_private(sc->sc_dev);
642 
643 	if (ISSET(sc->sc_flags, STI_ROM_ENABLED)) {
644 		gftfb_disable_rom_internal(spc);
645 	}
646 	CLR(sc->sc_flags, STI_ROM_ENABLED);
647 }
648 
649 static inline void
650 gftfb_wait(struct gftfb_softc *sc)
651 {
652 	uint8_t stat;
653 
654 	do {
655 		stat = gftfb_read1(sc, NGLE_REG_15b0);
656 		if (stat == 0)
657 			stat = gftfb_read1(sc, NGLE_REG_15b0);
658 	} while (stat != 0);
659 }
660 
661 static inline void
662 gftfb_setup_fb(struct gftfb_softc *sc)
663 {
664 	gftfb_wait(sc);
665 	gftfb_write4(sc, NGLE_REG_10,
666 	    BA(IndexedDcd, Otc04, Ots08, AddrByte, 0, BINapp0I, 0));
667 	gftfb_write4(sc, NGLE_REG_14, 0x83000300);
668 	gftfb_wait(sc);
669 	gftfb_write1(sc, NGLE_REG_16b1, 1);
670 	sc->sc_hwmode = HW_FB;
671 }
672 
673 void
674 gftfb_setup(struct gftfb_softc *sc)
675 {
676 	struct sti_rom *rom = sc->sc_base.sc_rom;
677 	bus_space_tag_t memt = rom->memt;
678 	bus_space_handle_t memh = rom->regh[2];
679 	int i;
680 
681 	sc->sc_hwmode = HW_FB;
682 	sc->sc_hot_x = 0;
683 	sc->sc_hot_y = 0;
684 	sc->sc_enabled = 0;
685 	sc->sc_video_on = 1;
686 
687 
688 	/* set Bt458 read mask register to all planes */
689 	gftfb_wait(sc);
690 	ngle_bt458_write(memt, memh, 0x08, 0x04);
691 	ngle_bt458_write(memt, memh, 0x0a, 0xff);
692 
693 	gftfb_setup_fb(sc);
694 
695 	/* attr. planes */
696 	gftfb_wait(sc);
697 	gftfb_write4(sc, NGLE_REG_11, 0x2ea0d000);
698 	gftfb_write4(sc, NGLE_REG_14, 0x23000302);
699 	gftfb_write4(sc, NGLE_REG_12, NGLE_ARTIST_CMAP0);
700 	gftfb_write4(sc, NGLE_REG_8, 0xffffffff);
701 
702 	gftfb_wait(sc);
703 	gftfb_write4(sc, NGLE_REG_6, 0x00000000);
704 	gftfb_write4(sc, NGLE_REG_9,
705 	    (sc->sc_scr.scr_cfg.scr_width << 16) | sc->sc_scr.scr_cfg.scr_height);
706 	/*
707 	 * blit into offscreen memory to force flush previous - apparently
708 	 * some chips have a bug this works around
709 	 */
710 	gftfb_write4(sc, NGLE_REG_6, 0x05000000);
711 	gftfb_write4(sc, NGLE_REG_9, 0x00040001);
712 
713 	gftfb_wait(sc);
714 	gftfb_write4(sc, NGLE_REG_12, 0x00000000);
715 
716 	gftfb_setup_fb(sc);
717 
718 	/* make sure video output is enabled */
719 	gftfb_wait(sc);
720 	gftfb_write4(sc, NGLE_REG_21,
721 	    gftfb_read4(sc, NGLE_REG_21) | 0x0a000000);
722 	gftfb_write4(sc, NGLE_REG_27,
723 	    gftfb_read4(sc, NGLE_REG_27) | 0x00800000);
724 
725 	/* initialize cursor sprite */
726 	gftfb_wait(sc);
727 
728 	/* cursor mask */
729 	gftfb_wait(sc);
730 	gftfb_write4(sc, NGLE_REG_14, 0x300);
731 	gftfb_write4(sc, NGLE_REG_13, 0xffffffff);
732 	gftfb_write4(sc, NGLE_REG_11,
733 	    BA(IndexedDcd, Otc32, 0, AddrLong, 0, BINcmask, 0));
734 	gftfb_write4(sc, NGLE_REG_3, 0);
735 	for (i = 0; i < 64; i++) {
736 		gftfb_write4(sc, NGLE_REG_4, 0xffffffff);
737 		gftfb_write4(sc, NGLE_REG_5, 0xffffffff);
738 	}
739 
740 	/* cursor image */
741 	gftfb_wait(sc);
742 	gftfb_write4(sc, NGLE_REG_14, 0x300);
743 	gftfb_write4(sc, NGLE_REG_13, 0xffffffff);
744 	gftfb_write4(sc, NGLE_REG_11,
745 	    BA(IndexedDcd, Otc32, 0, AddrLong, 0, BINcursor, 0));
746 	gftfb_write4(sc, NGLE_REG_3, 0);
747 	for (i = 0; i < 64; i++) {
748 		gftfb_write4(sc, NGLE_REG_4, 0xff00ff00);
749 		gftfb_write4(sc, NGLE_REG_5, 0xff00ff00);
750 	}
751 
752 	/* colour map */
753 	gftfb_wait(sc);
754 	gftfb_write4(sc, NGLE_REG_10,
755 	    BA(FractDcd, Otc24, Ots08, Addr24, 0, BINcmap, 0));
756 	gftfb_write4(sc, NGLE_REG_14, 0x03000300);
757 	gftfb_write4(sc, NGLE_REG_13, 0xffffffff);
758 	gftfb_wait(sc);
759 	gftfb_write4(sc, NGLE_REG_3, 0);
760 	gftfb_write4(sc, NGLE_REG_4, 0);
761 	gftfb_write4(sc, NGLE_REG_4, 0);
762 	gftfb_write4(sc, NGLE_REG_4, 0x000000ff);	/* BG */
763 	gftfb_write4(sc, NGLE_REG_4, 0x00ff0000);	/* FG */
764 	gftfb_wait(sc);
765 	gftfb_write4(sc, NGLE_REG_2, 0);
766 	gftfb_write4(sc, NGLE_REG_26, 0x80008004);
767 	gftfb_setup_fb(sc);
768 
769 	gftfb_move_cursor(sc, 100, 100);
770 
771 }
772 
773 static int
774 gftfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
775 	struct lwp *l)
776 {
777 	struct vcons_data *vd = v;
778 	struct gftfb_softc *sc = vd->cookie;
779 	struct wsdisplay_fbinfo *wdf;
780 	struct vcons_screen *ms = vd->active;
781 
782 	switch (cmd) {
783 	case WSDISPLAYIO_GTYPE:
784 		*(u_int *)data = WSDISPLAY_TYPE_STI;
785 		return 0;
786 
787 	case GCID:
788 		*(u_int *)data = STI_DD_EG;
789 		return 0;
790 
791 	/* PCI config read/write passthrough. */
792 	case PCI_IOC_CFGREAD:
793 	case PCI_IOC_CFGWRITE:
794 		return pci_devioctl(sc->sc_pc, sc->sc_tag,
795 		    cmd, data, flag, l);
796 
797 	case WSDISPLAYIO_GET_BUSID:
798 		return wsdisplayio_busid_pci(sc->sc_dev, sc->sc_pc,
799 		    sc->sc_tag, data);
800 
801 	case WSDISPLAYIO_GINFO:
802 		if (ms == NULL)
803 			return ENODEV;
804 		wdf = (void *)data;
805 		wdf->height = ms->scr_ri.ri_height;
806 		wdf->width = ms->scr_ri.ri_width;
807 		wdf->depth = ms->scr_ri.ri_depth;
808 		wdf->cmsize = 256;
809 		return 0;
810 
811 	case WSDISPLAYIO_GETCMAP:
812 		return gftfb_getcmap(sc,
813 		    (struct wsdisplay_cmap *)data);
814 
815 	case WSDISPLAYIO_PUTCMAP:
816 		return gftfb_putcmap(sc,
817 		    (struct wsdisplay_cmap *)data);
818 
819 	case WSDISPLAYIO_LINEBYTES:
820 		*(u_int *)data = 2048;
821 		return 0;
822 
823 	case WSDISPLAYIO_SMODE: {
824 		int new_mode = *(int*)data;
825 		if (new_mode != sc->sc_mode) {
826 			sc->sc_mode = new_mode;
827 			if(new_mode == WSDISPLAYIO_MODE_EMUL) {
828 				gftfb_setup(sc);
829 				gftfb_restore_palette(sc);
830 				glyphcache_wipe(&sc->sc_gc);
831 				gftfb_rectfill(sc, 0, 0, sc->sc_width,
832 				    sc->sc_height, ms->scr_ri.ri_devcmap[
833 				    (ms->scr_defattr >> 16) & 0xff]);
834 				vcons_redraw_screen(ms);
835 				gftfb_set_video(sc, 1);
836 			}
837 		}
838 		}
839 		return 0;
840 
841 	case WSDISPLAYIO_GET_FBINFO:
842 		{
843 			struct wsdisplayio_fbinfo *fbi = data;
844 			int ret;
845 
846 			ret = wsdisplayio_get_fbinfo(&ms->scr_ri, fbi);
847 			fbi->fbi_fbsize = sc->sc_scr.fbheight * 2048;
848 			return ret;
849 		}
850 
851 	case WSDISPLAYIO_GCURPOS:
852 		{
853 			struct wsdisplay_curpos *cp = (void *)data;
854 
855 			cp->x = sc->sc_cursor_x;
856 			cp->y = sc->sc_cursor_y;
857 		}
858 		return 0;
859 
860 	case WSDISPLAYIO_SCURPOS:
861 		{
862 			struct wsdisplay_curpos *cp = (void *)data;
863 
864 			gftfb_move_cursor(sc, cp->x, cp->y);
865 		}
866 		return 0;
867 
868 	case WSDISPLAYIO_GCURMAX:
869 		{
870 			struct wsdisplay_curpos *cp = (void *)data;
871 
872 			cp->x = 64;
873 			cp->y = 64;
874 		}
875 		return 0;
876 
877 	case WSDISPLAYIO_SCURSOR:
878 		{
879 			struct wsdisplay_cursor *cursor = (void *)data;
880 
881 			return gftfb_do_cursor(sc, cursor);
882 		}
883 
884 	case WSDISPLAYIO_SVIDEO:
885 		gftfb_set_video(sc, *(int *)data);
886 		return 0;
887 	case WSDISPLAYIO_GVIDEO:
888 		*(u_int *)data = sc->sc_video_on ?
889 		    WSDISPLAYIO_VIDEO_ON : WSDISPLAYIO_VIDEO_OFF;
890 		return 0;
891 	}
892 	return EPASSTHROUGH;
893 }
894 
895 static paddr_t
896 gftfb_mmap(void *v, void *vs, off_t offset, int prot)
897 {
898 	struct vcons_data *vd = v;
899 	struct gftfb_softc *sc = vd->cookie;
900 	struct sti_rom *rom = sc->sc_base.sc_rom;
901 	paddr_t pa = -1;
902 
903 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)
904 		return -1;
905 
906 	if (offset >= 0 && offset < sc->sc_scr.fblen) {
907 		/* framebuffer */
908 		pa = bus_space_mmap(rom->memt, sc->sc_scr.fbaddr, offset,
909 		    prot, BUS_SPACE_MAP_LINEAR);
910 	} else if (offset >= 0x80000000 && offset < 0x80400000) {
911 		/* blitter registers etc. */
912 		pa = bus_space_mmap(rom->memt, rom->regh[2],
913 		    offset - 0x80000000, prot, BUS_SPACE_MAP_LINEAR);
914 	}
915 
916 	return pa;
917 }
918 
919 static void
920 gftfb_init_screen(void *cookie, struct vcons_screen *scr,
921     int existing, long *defattr)
922 {
923 	struct gftfb_softc *sc = cookie;
924 	struct rasops_info *ri = &scr->scr_ri;
925 
926 	ri->ri_depth = 8;
927 	ri->ri_width = sc->sc_width;
928 	ri->ri_height = sc->sc_height;
929 	ri->ri_stride = 2048;
930 	ri->ri_flg = RI_CENTER | RI_8BIT_IS_RGB |
931 		     RI_ENABLE_ALPHA | RI_PREFER_ALPHA;
932 
933 	ri->ri_bits = (void *)sc->sc_scr.fbaddr;
934 	rasops_init(ri, 0, 0);
935 	ri->ri_caps = WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_UNDERLINE |
936 		      WSSCREEN_RESIZE;
937 	scr->scr_flags |= VCONS_LOADFONT;
938 
939 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
940 		    sc->sc_width / ri->ri_font->fontwidth);
941 
942 	ri->ri_hw = scr;
943 	sc->sc_putchar = ri->ri_ops.putchar;
944 	ri->ri_ops.copyrows = gftfb_copyrows;
945 	ri->ri_ops.copycols = gftfb_copycols;
946 	ri->ri_ops.eraserows = gftfb_eraserows;
947 	ri->ri_ops.erasecols = gftfb_erasecols;
948 	ri->ri_ops.cursor = gftfb_cursor;
949 	if (FONT_IS_ALPHA(ri->ri_font)) {
950 		ri->ri_ops.putchar = gftfb_putchar_aa;
951 	} else
952 		ri->ri_ops.putchar = gftfb_putchar;
953 }
954 
955 static int
956 gftfb_putcmap(struct gftfb_softc *sc, struct wsdisplay_cmap *cm)
957 {
958 	u_char *r, *g, *b;
959 	u_int index = cm->index;
960 	u_int count = cm->count;
961 	int i, error;
962 	u_char rbuf[256], gbuf[256], bbuf[256];
963 
964 	if (cm->index >= 256 || cm->count > 256 ||
965 	    (cm->index + cm->count) > 256)
966 		return EINVAL;
967 	error = copyin(cm->red, &rbuf[index], count);
968 	if (error)
969 		return error;
970 	error = copyin(cm->green, &gbuf[index], count);
971 	if (error)
972 		return error;
973 	error = copyin(cm->blue, &bbuf[index], count);
974 	if (error)
975 		return error;
976 
977 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
978 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
979 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
980 
981 	r = &sc->sc_cmap_red[index];
982 	g = &sc->sc_cmap_green[index];
983 	b = &sc->sc_cmap_blue[index];
984 
985 	for (i = 0; i < count; i++) {
986 		gftfb_putpalreg(sc, index, *r, *g, *b);
987 		index++;
988 		r++, g++, b++;
989 	}
990 	return 0;
991 }
992 
993 static int
994 gftfb_getcmap(struct gftfb_softc *sc, struct wsdisplay_cmap *cm)
995 {
996 	u_int index = cm->index;
997 	u_int count = cm->count;
998 	int error;
999 
1000 	if (index >= 255 || count > 256 || index + count > 256)
1001 		return EINVAL;
1002 
1003 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
1004 	if (error)
1005 		return error;
1006 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
1007 	if (error)
1008 		return error;
1009 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
1010 	if (error)
1011 		return error;
1012 
1013 	return 0;
1014 }
1015 
1016 static void
1017 gftfb_restore_palette(struct gftfb_softc *sc)
1018 {
1019 	uint8_t cmap[768];
1020 	int i, j;
1021 
1022 	j = 0;
1023 	rasops_get_cmap(&sc->sc_console_screen.scr_ri, cmap, sizeof(cmap));
1024 	for (i = 0; i < 256; i++) {
1025 		sc->sc_cmap_red[i] = cmap[j];
1026 		sc->sc_cmap_green[i] = cmap[j + 1];
1027 		sc->sc_cmap_blue[i] = cmap[j + 2];
1028 		gftfb_putpalreg(sc, i, cmap[j], cmap[j + 1], cmap[j + 2]);
1029 		j += 3;
1030 	}
1031 }
1032 
1033 static int
1034 gftfb_putpalreg(struct gftfb_softc *sc, uint8_t idx, uint8_t r, uint8_t g,
1035     uint8_t b)
1036 {
1037 	mutex_enter(&sc->sc_hwlock);
1038 	gftfb_wait(sc);
1039 	gftfb_write4(sc, NGLE_REG_10,
1040 	    BA(FractDcd, Otc24, Ots08, Addr24, 0, BINcmap, 0));
1041 	gftfb_write4(sc, NGLE_REG_14, 0x03000300);
1042 	gftfb_write4(sc, NGLE_REG_13, 0xffffffff);
1043 
1044 	gftfb_wait(sc);
1045 	gftfb_write4(sc, NGLE_REG_3, 0x400 | (idx << 2));
1046 	gftfb_write4(sc, NGLE_REG_4, (r << 16) | (g << 8) | b);
1047 
1048 	gftfb_write4(sc, NGLE_REG_2, 0x400);
1049 	gftfb_write4(sc, NGLE_REG_26, 0x80000100);
1050 	gftfb_setup_fb(sc);
1051 	mutex_exit(&sc->sc_hwlock);
1052 	return 0;
1053 }
1054 
1055 static inline void
1056 gftfb_wait_fifo(struct gftfb_softc *sc, uint32_t slots)
1057 {
1058 	uint32_t reg;
1059 
1060 	do {
1061 		reg = gftfb_read4(sc, NGLE_REG_34);
1062 	} while (reg < slots);
1063 }
1064 
1065 static void
1066 gftfb_rectfill(struct gftfb_softc *sc, int x, int y, int wi, int he,
1067 		      uint32_t bg)
1068 {
1069 	uint32_t mask = 0xffffffff;
1070 
1071 	if (sc->sc_hwmode != HW_FILL) {
1072 		gftfb_wait_fifo(sc, 3);
1073 		/* plane mask */
1074 		gftfb_write4(sc, NGLE_REG_13, 0xff);
1075 		/* bitmap op */
1076 		gftfb_write4(sc, NGLE_REG_14,
1077 		    IBOvals(RopSrc, 0, BitmapExtent08, 0, DataDynamic, MaskOtc, 1, 0));
1078 		/* dst bitmap access */
1079 		gftfb_write4(sc, NGLE_REG_11,
1080 		    BA(IndexedDcd, Otc32, OtsIndirect, AddrLong, 0, BINapp0I, 0));
1081 		sc->sc_hwmode = HW_FILL;
1082 	}
1083 	gftfb_wait_fifo(sc, 4);
1084 
1085 	if (wi < 32)
1086 		mask = 0xffffffff << (32 - wi);
1087 	/* transfer data */
1088 	gftfb_write4(sc, NGLE_REG_8, mask);
1089 	gftfb_write4(sc, NGLE_REG_35, bg);
1090 	/* dst XY */
1091 	gftfb_write4(sc, NGLE_REG_6, (x << 16) | y);
1092 	/* len XY start */
1093 	gftfb_write4(sc, NGLE_REG_9, (wi << 16) | he);
1094 
1095 }
1096 
1097 static void
1098 gftfb_bitblt(void *cookie, int xs, int ys, int xd, int yd, int wi,
1099 			    int he, int rop)
1100 {
1101 	struct gftfb_softc *sc = cookie;
1102 
1103 	if (sc->sc_hwmode != HW_BLIT) {
1104 		gftfb_wait(sc);
1105 		gftfb_write4(sc, NGLE_REG_10,
1106 		    BA(IndexedDcd, Otc04, Ots08, AddrLong, 0, BINapp0I, 0));
1107 		sc->sc_hwmode = HW_BLIT;
1108 	}
1109 	gftfb_wait_fifo(sc, 5);
1110 	gftfb_write4(sc, NGLE_REG_14,
1111 	   IBOvals(rop, 0, BitmapExtent08, 1, DataDynamic, MaskOtc, 0, 0));
1112 	gftfb_write4(sc, NGLE_REG_13, 0xff);
1113 	gftfb_write4(sc, NGLE_REG_24, (xs << 16) | ys);
1114 	gftfb_write4(sc, NGLE_REG_7, (wi << 16) | he);
1115 	gftfb_write4(sc, NGLE_REG_25, (xd << 16) | yd);
1116 }
1117 
1118 static void
1119 gftfb_nuke_cursor(struct rasops_info *ri)
1120 {
1121 	struct vcons_screen *scr = ri->ri_hw;
1122 	struct gftfb_softc *sc = scr->scr_cookie;
1123 	int wi, he, x, y;
1124 
1125 	if (ri->ri_flg & RI_CURSOR) {
1126 		wi = ri->ri_font->fontwidth;
1127 		he = ri->ri_font->fontheight;
1128 		x = ri->ri_ccol * wi + ri->ri_xorigin;
1129 		y = ri->ri_crow * he + ri->ri_yorigin;
1130 		gftfb_bitblt(sc, x, y, x, y, wi, he, RopInv);
1131 		ri->ri_flg &= ~RI_CURSOR;
1132 	}
1133 }
1134 
1135 static void
1136 gftfb_cursor(void *cookie, int on, int row, int col)
1137 {
1138 	struct rasops_info *ri = cookie;
1139 	struct vcons_screen *scr = ri->ri_hw;
1140 	struct gftfb_softc *sc = scr->scr_cookie;
1141 	int x, y, wi, he;
1142 
1143 	wi = ri->ri_font->fontwidth;
1144 	he = ri->ri_font->fontheight;
1145 
1146 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
1147 		if (on) {
1148 			if (ri->ri_flg & RI_CURSOR) {
1149 				gftfb_nuke_cursor(ri);
1150 			}
1151 			x = col * wi + ri->ri_xorigin;
1152 			y = row * he + ri->ri_yorigin;
1153 			gftfb_bitblt(sc, x, y, x, y, wi, he, RopInv);
1154 			ri->ri_flg |= RI_CURSOR;
1155 		}
1156 		ri->ri_crow = row;
1157 		ri->ri_ccol = col;
1158 	} else
1159 	{
1160 		ri->ri_crow = row;
1161 		ri->ri_ccol = col;
1162 		ri->ri_flg &= ~RI_CURSOR;
1163 	}
1164 
1165 }
1166 
1167 static void
1168 gftfb_putchar(void *cookie, int row, int col, u_int c, long attr)
1169 {
1170 	struct rasops_info *ri = cookie;
1171 	struct wsdisplay_font *font = PICK_FONT(ri, c);
1172 	struct vcons_screen *scr = ri->ri_hw;
1173 	struct gftfb_softc *sc = scr->scr_cookie;
1174 	void *data;
1175 	int i, x, y, wi, he, rv = GC_NOPE;
1176 	uint32_t bg, fg, mask;
1177 
1178 	if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL)
1179 		return;
1180 
1181 	if (!CHAR_IN_FONT(c, font))
1182 		return;
1183 
1184 	if (row == ri->ri_crow && col == ri->ri_ccol) {
1185 		ri->ri_flg &= ~RI_CURSOR;
1186 	}
1187 
1188 	wi = font->fontwidth;
1189 	he = font->fontheight;
1190 
1191 	x = ri->ri_xorigin + col * wi;
1192 	y = ri->ri_yorigin + row * he;
1193 
1194 	bg = ri->ri_devcmap[(attr >> 16) & 0xf];
1195 
1196 	/* if we're drawing a space we're done here */
1197 	if (c == 0x20) {
1198 		gftfb_rectfill(sc, x, y, wi, he, bg);
1199 		return;
1200 	}
1201 
1202 	fg = ri->ri_devcmap[(attr >> 24) & 0x0f];
1203 
1204 	rv = glyphcache_try(&sc->sc_gc, c, x, y, attr);
1205 	if (rv == GC_OK)
1206 		return;
1207 
1208 	/* clear the character cell */
1209 	gftfb_rectfill(sc, x, y, wi, he, bg);
1210 
1211 	data = WSFONT_GLYPH(c, font);
1212 
1213 	/*
1214 	* we're in rectangle mode with transparency enabled from the call to
1215 	* gftfb_rectfill() above, so all we need to do is reset the starting
1216 	* cordinates, then hammer mask and size/start. Starting coordinates
1217 	* will automatically move down the y-axis
1218 	*/
1219 	gftfb_wait_fifo(sc, 2);
1220 
1221 	/* character colour */
1222 	gftfb_write4(sc, NGLE_REG_35, fg);
1223 	/* dst XY */
1224 	gftfb_write4(sc, NGLE_REG_6, (x << 16) | y);
1225 
1226 	if (ri->ri_font->stride == 1) {
1227 		uint8_t *data8 = data;
1228 		for (i = 0; i < he; i++) {
1229 			gftfb_wait_fifo(sc, 2);
1230 			mask = *data8;
1231 			gftfb_write4(sc, NGLE_REG_8, mask << 24);
1232 			gftfb_write4(sc, NGLE_REG_9, (wi << 16) | 1);
1233 			data8++;
1234 		}
1235 	} else {
1236 		uint16_t *data16 = data;
1237 		for (i = 0; i < he; i++) {
1238 			gftfb_wait_fifo(sc, 2);
1239 			mask = *data16;
1240 			gftfb_write4(sc, NGLE_REG_8, mask << 16);
1241 			gftfb_write4(sc, NGLE_REG_9, (wi << 16) | 1);
1242 			data16++;
1243 		}
1244 	}
1245 
1246 	if (rv == GC_ADD)
1247 		glyphcache_add(&sc->sc_gc, c, x, y);
1248 }
1249 
1250 static void
1251 gftfb_putchar_aa(void *cookie, int row, int col, u_int c, long attr)
1252 {
1253 	struct rasops_info *ri = cookie;
1254 	struct wsdisplay_font *font = PICK_FONT(ri, c);
1255 	struct vcons_screen *scr = ri->ri_hw;
1256 	struct gftfb_softc *sc = scr->scr_cookie;
1257 	int x, y, wi, he, rv = GC_NOPE;
1258 	uint32_t bg;
1259 
1260 	if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL)
1261 		return;
1262 
1263 	if (!CHAR_IN_FONT(c, font))
1264 		return;
1265 
1266 	if (row == ri->ri_crow && col == ri->ri_ccol) {
1267 		ri->ri_flg &= ~RI_CURSOR;
1268 	}
1269 
1270 	wi = font->fontwidth;
1271 	he = font->fontheight;
1272 
1273 	x = ri->ri_xorigin + col * wi;
1274 	y = ri->ri_yorigin + row * he;
1275 
1276 	bg = ri->ri_devcmap[(attr >> 16) & 0xf];
1277 
1278 	if (c == 0x20) {
1279 		gftfb_rectfill(sc, x, y, wi, he, bg);
1280 		return;
1281 	}
1282 
1283 	rv = glyphcache_try(&sc->sc_gc, c, x, y, attr);
1284 	if (rv == GC_OK)
1285 		return;
1286 
1287 	if (sc->sc_hwmode != HW_FB) gftfb_setup_fb(sc);
1288 	sc->sc_putchar(cookie, row, col, c, attr);
1289 
1290 	if (rv == GC_ADD)
1291 		glyphcache_add(&sc->sc_gc, c, x, y);
1292 }
1293 
1294 static void
1295 gftfb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
1296 {
1297 	struct rasops_info *ri = cookie;
1298 	struct vcons_screen *scr = ri->ri_hw;
1299 	struct gftfb_softc *sc = scr->scr_cookie;
1300 	int32_t xs, xd, y, width, height;
1301 
1302 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
1303 		if (ri->ri_crow == row &&
1304 		   (ri->ri_ccol >= srccol && ri->ri_ccol < (srccol + ncols)) &&
1305 		   (ri->ri_flg & RI_CURSOR)) {
1306 			gftfb_nuke_cursor(ri);
1307 		}
1308 
1309 		xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
1310 		xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
1311 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
1312 		width = ri->ri_font->fontwidth * ncols;
1313 		height = ri->ri_font->fontheight;
1314 		gftfb_bitblt(sc, xs, y, xd, y, width, height, RopSrc);
1315 		if (ri->ri_crow == row &&
1316 		   (ri->ri_ccol >= dstcol && ri->ri_ccol < (dstcol + ncols)))
1317 			ri->ri_flg &= ~RI_CURSOR;
1318 	}
1319 }
1320 
1321 static void
1322 gftfb_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
1323 {
1324 	struct rasops_info *ri = cookie;
1325 	struct vcons_screen *scr = ri->ri_hw;
1326 	struct gftfb_softc *sc = scr->scr_cookie;
1327 	int32_t x, y, width, height, fg, bg, ul;
1328 
1329 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
1330 		x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
1331 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
1332 		width = ri->ri_font->fontwidth * ncols;
1333 		height = ri->ri_font->fontheight;
1334 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
1335 
1336 		gftfb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
1337 		if (ri->ri_crow == row &&
1338 		   (ri->ri_ccol >= startcol && ri->ri_ccol < (startcol + ncols)))
1339 			ri->ri_flg &= ~RI_CURSOR;
1340 	}
1341 }
1342 
1343 static void
1344 gftfb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
1345 {
1346 	struct rasops_info *ri = cookie;
1347 	struct vcons_screen *scr = ri->ri_hw;
1348 	struct gftfb_softc *sc = scr->scr_cookie;
1349 	int32_t x, ys, yd, width, height;
1350 
1351 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
1352 		if ((ri->ri_crow >= srcrow && ri->ri_crow < (srcrow + nrows)) &&
1353 		   (ri->ri_flg & RI_CURSOR)) {
1354 			gftfb_nuke_cursor(ri);
1355 		}
1356 		x = ri->ri_xorigin;
1357 		ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
1358 		yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
1359 		width = ri->ri_emuwidth;
1360 		height = ri->ri_font->fontheight * nrows;
1361 		gftfb_bitblt(sc, x, ys, x, yd, width, height, RopSrc);
1362 		if (ri->ri_crow >= dstrow && ri->ri_crow < (dstrow + nrows))
1363 			ri->ri_flg &= ~RI_CURSOR;
1364 	}
1365 }
1366 
1367 static void
1368 gftfb_eraserows(void *cookie, int row, int nrows, long fillattr)
1369 {
1370 	struct rasops_info *ri = cookie;
1371 	struct vcons_screen *scr = ri->ri_hw;
1372 	struct gftfb_softc *sc = scr->scr_cookie;
1373 	int32_t x, y, width, height, fg, bg, ul;
1374 
1375 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
1376 		x = ri->ri_xorigin;
1377 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
1378 		width = ri->ri_emuwidth;
1379 		height = ri->ri_font->fontheight * nrows;
1380 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
1381 
1382 		gftfb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
1383 
1384 		if (ri->ri_crow >= row && ri->ri_crow < (row + nrows))
1385 			ri->ri_flg &= ~RI_CURSOR;
1386 	}
1387 }
1388 
1389 /*
1390  * cursor sprite handling
1391  * like most hw info, xf86 3.3 -> nglehdw.h was used as documentation
1392  * problem is, the PCI EG doesn't quite behave like an S9000_ID_ARTIST
1393  * the cursor position register bahaves like the one on HCRX while using
1394  * the same address as Artist, incuding the enable bit and weird handling
1395  * of negative coordinates. The rest of it, colour map, sprite image etc.,
1396  * behave like Artist.
1397  */
1398 
1399 static void
1400 gftfb_move_cursor(struct gftfb_softc *sc, int x, int y)
1401 {
1402 	uint32_t pos;
1403 
1404 	sc->sc_cursor_x = x;
1405 	x -= sc->sc_hot_x;
1406 	sc->sc_cursor_y = y;
1407 	y -= sc->sc_hot_y;
1408 
1409 	if (x < 0) x = 0x1000 - x;
1410 	if (y < 0) y = 0x1000 - y;
1411 	pos = (x << 16) | y;
1412 	if (sc->sc_enabled) pos |= 0x80000000;
1413 	gftfb_wait(sc);
1414 	gftfb_write4(sc, NGLE_REG_17, pos);
1415 	gftfb_write4(sc, NGLE_REG_18, 0x80);
1416 }
1417 
1418 static int
1419 gftfb_do_cursor(struct gftfb_softc *sc, struct wsdisplay_cursor *cur)
1420 {
1421 	if (cur->which & WSDISPLAY_CURSOR_DOCUR) {
1422 
1423 		sc->sc_enabled = cur->enable;
1424 		cur->which |= WSDISPLAY_CURSOR_DOPOS;
1425 	}
1426 	if (cur->which & WSDISPLAY_CURSOR_DOHOT) {
1427 
1428 		sc->sc_hot_x = cur->hot.x;
1429 		sc->sc_hot_y = cur->hot.y;
1430 		cur->which |= WSDISPLAY_CURSOR_DOPOS;
1431 	}
1432 	if (cur->which & WSDISPLAY_CURSOR_DOPOS) {
1433 
1434 		gftfb_move_cursor(sc, cur->pos.x, cur->pos.y);
1435 	}
1436 	if (cur->which & WSDISPLAY_CURSOR_DOCMAP) {
1437 		uint32_t rgb;
1438 		uint8_t r[2], g[2], b[2];
1439 
1440 		copyin(cur->cmap.blue, b, 2);
1441 		copyin(cur->cmap.green, g, 2);
1442 		copyin(cur->cmap.red, r, 2);
1443 		mutex_enter(&sc->sc_hwlock);
1444 		gftfb_wait(sc);
1445 		gftfb_write4(sc, NGLE_REG_10,
1446 		    BA(FractDcd, Otc24, Ots08, Addr24, 0, BINcmap, 0));
1447 		gftfb_write4(sc, NGLE_REG_14, 0x03000300);
1448 		gftfb_write4(sc, NGLE_REG_13, 0xffffffff);
1449 		gftfb_wait(sc);
1450 		gftfb_write4(sc, NGLE_REG_3, 0);
1451 		gftfb_write4(sc, NGLE_REG_4, 0);
1452 		gftfb_write4(sc, NGLE_REG_4, 0);
1453 		rgb = (r[0] << 16) | (g[0] << 8) | b[0];
1454 		gftfb_write4(sc, NGLE_REG_4, rgb);	/* BG */
1455 		rgb = (r[1] << 16) | (g[1] << 8) | b[1];
1456 		gftfb_write4(sc, NGLE_REG_4, rgb);	/* FG */
1457 		gftfb_write4(sc, NGLE_REG_2, 0);
1458 		gftfb_write4(sc, NGLE_REG_26, 0x80008004);
1459 		gftfb_setup_fb(sc);
1460 		mutex_exit(&sc->sc_hwlock);
1461 
1462 	}
1463 	if (cur->which & WSDISPLAY_CURSOR_DOSHAPE) {
1464 		uint32_t buffer[128], latch, tmp;
1465 		int i;
1466 
1467 		copyin(cur->mask, buffer, 512);
1468 		gftfb_wait(sc);
1469 		gftfb_write4(sc, NGLE_REG_14, 0x300);
1470 		gftfb_write4(sc, NGLE_REG_13, 0xffffffff);
1471 		gftfb_write4(sc, NGLE_REG_11,
1472 		    BA(IndexedDcd, Otc32, 0, AddrLong, 0, BINcmask, 0));
1473 		gftfb_write4(sc, NGLE_REG_3, 0);
1474 		for (i = 0; i < 128; i += 2) {
1475 			latch = 0;
1476 			tmp = buffer[i] & 0x80808080;
1477 			latch |= tmp >> 7;
1478 			tmp = buffer[i] & 0x40404040;
1479 			latch |= tmp >> 5;
1480 			tmp = buffer[i] & 0x20202020;
1481 			latch |= tmp >> 3;
1482 			tmp = buffer[i] & 0x10101010;
1483 			latch |= tmp >> 1;
1484 			tmp = buffer[i] & 0x08080808;
1485 			latch |= tmp << 1;
1486 			tmp = buffer[i] & 0x04040404;
1487 			latch |= tmp << 3;
1488 			tmp = buffer[i] & 0x02020202;
1489 			latch |= tmp << 5;
1490 			tmp = buffer[i] & 0x01010101;
1491 			latch |= tmp << 7;
1492 			gftfb_write4(sc, NGLE_REG_4, latch);
1493 			latch = 0;
1494 			tmp = buffer[i + 1] & 0x80808080;
1495 			latch |= tmp >> 7;
1496 			tmp = buffer[i + 1] & 0x40404040;
1497 			latch |= tmp >> 5;
1498 			tmp = buffer[i + 1] & 0x20202020;
1499 			latch |= tmp >> 3;
1500 			tmp = buffer[i + 1] & 0x10101010;
1501 			latch |= tmp >> 1;
1502 			tmp = buffer[i + 1] & 0x08080808;
1503 			latch |= tmp << 1;
1504 			tmp = buffer[i + 1] & 0x04040404;
1505 			latch |= tmp << 3;
1506 			tmp = buffer[i + 1] & 0x02020202;
1507 			latch |= tmp << 5;
1508 			tmp = buffer[i + 1] & 0x01010101;
1509 			latch |= tmp << 7;
1510 			gftfb_write4(sc, NGLE_REG_5, latch);
1511 		}
1512 
1513 		copyin(cur->image, buffer, 512);
1514 		gftfb_wait(sc);
1515 		gftfb_write4(sc, NGLE_REG_14, 0x300);
1516 		gftfb_write4(sc, NGLE_REG_13, 0xffffffff);
1517 		gftfb_write4(sc, NGLE_REG_11,
1518 		    BA(IndexedDcd, Otc32, 0, AddrLong, 0, BINcursor, 0));
1519 		gftfb_write4(sc, NGLE_REG_3, 0);
1520 		for (i = 0; i < 128; i += 2) {
1521 			latch = 0;
1522 			tmp = buffer[i] & 0x80808080;
1523 			latch |= tmp >> 7;
1524 			tmp = buffer[i] & 0x40404040;
1525 			latch |= tmp >> 5;
1526 			tmp = buffer[i] & 0x20202020;
1527 			latch |= tmp >> 3;
1528 			tmp = buffer[i] & 0x10101010;
1529 			latch |= tmp >> 1;
1530 			tmp = buffer[i] & 0x08080808;
1531 			latch |= tmp << 1;
1532 			tmp = buffer[i] & 0x04040404;
1533 			latch |= tmp << 3;
1534 			tmp = buffer[i] & 0x02020202;
1535 			latch |= tmp << 5;
1536 			tmp = buffer[i] & 0x01010101;
1537 			latch |= tmp << 7;
1538 			gftfb_write4(sc, NGLE_REG_4, latch);
1539 			latch = 0;
1540 			tmp = buffer[i + 1] & 0x80808080;
1541 			latch |= tmp >> 7;
1542 			tmp = buffer[i + 1] & 0x40404040;
1543 			latch |= tmp >> 5;
1544 			tmp = buffer[i + 1] & 0x20202020;
1545 			latch |= tmp >> 3;
1546 			tmp = buffer[i + 1] & 0x10101010;
1547 			latch |= tmp >> 1;
1548 			tmp = buffer[i + 1] & 0x08080808;
1549 			latch |= tmp << 1;
1550 			tmp = buffer[i + 1] & 0x04040404;
1551 			latch |= tmp << 3;
1552 			tmp = buffer[i + 1] & 0x02020202;
1553 			latch |= tmp << 5;
1554 			tmp = buffer[i + 1] & 0x01010101;
1555 			latch |= tmp << 7;
1556 			gftfb_write4(sc, NGLE_REG_5, latch);
1557 		}
1558 		gftfb_setup_fb(sc);
1559 	}
1560 
1561 	return 0;
1562 }
1563 
1564 static void
1565 gftfb_set_video(struct gftfb_softc *sc, int on)
1566 {
1567 	if (sc->sc_video_on == on)
1568 		return;
1569 
1570 	sc->sc_video_on = on;
1571 
1572 	gftfb_wait(sc);
1573 	if (on) {
1574 		gftfb_write4(sc, NGLE_REG_21,
1575 		    gftfb_read4(sc, NGLE_REG_21) | 0x0a000000);
1576 		gftfb_write4(sc, NGLE_REG_27,
1577 		    gftfb_read4(sc, NGLE_REG_27) | 0x00800000);
1578 	} else {
1579 		gftfb_write4(sc, NGLE_REG_21,
1580 		    gftfb_read4(sc, NGLE_REG_21) &  ~0x0a000000);
1581 		gftfb_write4(sc, NGLE_REG_27,
1582 		    gftfb_read4(sc, NGLE_REG_27) & ~0x00800000);
1583 	}
1584 }
1585