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