gftfb.c revision 1.29 1 /* $NetBSD: gftfb.c,v 1.29 2025/03/11 06:04:19 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 /* 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 vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1, &defattr);
299 sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
300
301 sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
302 sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
303 sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
304 sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
305
306 glyphcache_init(&sc->sc_gc, sc->sc_height + 5,
307 sc->sc_scr.fbheight - sc->sc_height - 5,
308 sc->sc_scr.fbwidth,
309 ri->ri_font->fontwidth,
310 ri->ri_font->fontheight,
311 defattr);
312
313 gftfb_restore_palette(sc);
314 gftfb_rectfill(sc, 0, 0, sc->sc_width, sc->sc_height,
315 ri->ri_devcmap[(defattr >> 16) & 0xff]);
316
317 if (is_console) {
318 wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0,
319 defattr);
320
321 vcons_replay_msgbuf(&sc->sc_console_screen);
322 }
323
324 /* no suspend/resume support yet */
325 if (!pmf_device_register(sc->sc_dev, NULL, NULL))
326 aprint_error_dev(sc->sc_dev,
327 "couldn't establish power handler\n");
328
329 aa.console = is_console;
330 aa.scrdata = &sc->sc_screenlist;
331 aa.accessops = &gftfb_accessops;
332 aa.accesscookie = &sc->vd;
333
334 config_found(sc->sc_dev, &aa, wsemuldisplaydevprint, CFARGS_NONE);
335 }
336
337 /*
338 * Grovel the STI ROM image.
339 */
340 int
341 gftfb_check_rom(struct gftfb_softc *spc, struct pci_attach_args *pa)
342 {
343 struct sti_softc *sc = &spc->sc_base;
344 pcireg_t address, mask;
345 bus_space_handle_t romh;
346 bus_size_t romsize, subsize, stiromsize;
347 bus_addr_t selected, offs, suboffs;
348 uint32_t tmp;
349 int i;
350 int rc;
351
352 /* sort of inline sti_pci_enable_rom(sc) */
353 address = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_MAPREG_ROM);
354 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_MAPREG_ROM,
355 ~PCI_MAPREG_ROM_ENABLE);
356 mask = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_MAPREG_ROM);
357 address |= PCI_MAPREG_ROM_ENABLE;
358 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_MAPREG_ROM, address);
359 sc->sc_flags |= STI_ROM_ENABLED;
360 /*
361 * Map the complete ROM for now.
362 */
363
364 romsize = PCI_ROM_SIZE(mask);
365 DPRINTF(("%s: mapping rom @ %lx for %lx\n", __func__,
366 (long)PCI_MAPREG_ROM_ADDR(address), (long)romsize));
367
368 rc = bus_space_map(pa->pa_memt, PCI_MAPREG_ROM_ADDR(address), romsize,
369 0, &romh);
370 if (rc != 0) {
371 aprint_error_dev(sc->sc_dev, "can't map PCI ROM (%d)\n", rc);
372 goto fail2;
373 }
374
375 gftfb_disable_rom_internal(spc);
376 /*
377 * Iterate over the ROM images, pick the best candidate.
378 */
379
380 selected = (bus_addr_t)-1;
381 for (offs = 0; offs < romsize; offs += subsize) {
382 gftfb_enable_rom_internal(spc);
383 /*
384 * Check for a valid ROM header.
385 */
386 tmp = bus_space_read_4(pa->pa_memt, romh, offs + 0);
387 tmp = le32toh(tmp);
388 if (tmp != 0x55aa0000) {
389 gftfb_disable_rom_internal(spc);
390 if (offs == 0) {
391 aprint_error_dev(sc->sc_dev,
392 "invalid PCI ROM header signature (%08x)\n",
393 tmp);
394 rc = EINVAL;
395 }
396 break;
397 }
398
399 /*
400 * Check ROM type.
401 */
402 tmp = bus_space_read_4(pa->pa_memt, romh, offs + 4);
403 tmp = le32toh(tmp);
404 if (tmp != 0x00000001) { /* 1 == STI ROM */
405 gftfb_disable_rom_internal(spc);
406 if (offs == 0) {
407 aprint_error_dev(sc->sc_dev,
408 "invalid PCI ROM type (%08x)\n", tmp);
409 rc = EINVAL;
410 }
411 break;
412 }
413
414 subsize = (bus_addr_t)bus_space_read_2(pa->pa_memt, romh,
415 offs + 0x0c);
416 subsize <<= 9;
417
418 #ifdef GFTFB_DEBUG
419 gftfb_disable_rom_internal(spc);
420 DPRINTF(("ROM offset %08x size %08x type %08x",
421 (u_int)offs, (u_int)subsize, tmp));
422 gftfb_enable_rom_internal(spc);
423 #endif
424
425 /*
426 * Check for a valid ROM data structure.
427 * We do not need it except to know what architecture the ROM
428 * code is for.
429 */
430
431 suboffs = offs +(bus_addr_t)bus_space_read_2(pa->pa_memt, romh,
432 offs + 0x18);
433 tmp = bus_space_read_4(pa->pa_memt, romh, suboffs + 0);
434 tmp = le32toh(tmp);
435 if (tmp != 0x50434952) { /* PCIR */
436 gftfb_disable_rom_internal(spc);
437 if (offs == 0) {
438 aprint_error_dev(sc->sc_dev, "invalid PCI data"
439 " signature (%08x)\n", tmp);
440 rc = EINVAL;
441 } else {
442 DPRINTF((" invalid PCI data signature %08x\n",
443 tmp));
444 continue;
445 }
446 }
447
448 tmp = bus_space_read_1(pa->pa_memt, romh, suboffs + 0x14);
449 gftfb_disable_rom_internal(spc);
450 DPRINTF((" code %02x", tmp));
451
452 switch (tmp) {
453 #ifdef __hppa__
454 case 0x10:
455 if (selected == (bus_addr_t)-1)
456 selected = offs;
457 break;
458 #endif
459 #ifdef __i386__
460 case 0x00:
461 if (selected == (bus_addr_t)-1)
462 selected = offs;
463 break;
464 #endif
465 default:
466 DPRINTF((" (wrong architecture)"));
467 break;
468 }
469 DPRINTF(("%s\n", selected == offs ? " -> SELECTED" : ""));
470 }
471
472 if (selected == (bus_addr_t)-1) {
473 if (rc == 0) {
474 aprint_error_dev(sc->sc_dev, "found no ROM with "
475 "correct microcode architecture\n");
476 rc = ENOEXEC;
477 }
478 goto fail;
479 }
480
481 /*
482 * Read the STI region BAR assignments.
483 */
484
485 gftfb_enable_rom_internal(spc);
486 offs = selected +
487 (bus_addr_t)bus_space_read_2(pa->pa_memt, romh, selected + 0x0e);
488 for (i = 0; i < STI_REGION_MAX; i++) {
489 rc = gftfb_readbar(sc, pa, i,
490 bus_space_read_1(pa->pa_memt, romh, offs + i));
491 if (rc != 0)
492 goto fail;
493 }
494
495 /*
496 * Find out where the STI ROM itself lies, and its size.
497 */
498
499 offs = selected +
500 (bus_addr_t)bus_space_read_4(pa->pa_memt, romh, selected + 0x08);
501 stiromsize = (bus_addr_t)bus_space_read_4(pa->pa_memt, romh,
502 offs + 0x18);
503 stiromsize = le32toh(stiromsize);
504 gftfb_disable_rom_internal(spc);
505
506 /*
507 * Replace our mapping with a smaller mapping of only the area
508 * we are interested in.
509 */
510
511 DPRINTF(("remapping rom @ %lx for %lx\n",
512 (long)(PCI_MAPREG_ROM_ADDR(address) + offs), (long)stiromsize));
513 bus_space_unmap(pa->pa_memt, romh, romsize);
514 rc = bus_space_map(pa->pa_memt, PCI_MAPREG_ROM_ADDR(address) + offs,
515 stiromsize, 0, &spc->sc_romh);
516 if (rc != 0) {
517 aprint_error_dev(sc->sc_dev, "can't map STI ROM (%d)\n",
518 rc);
519 goto fail2;
520 }
521 gftfb_disable_rom_internal(spc);
522 sc->sc_flags &= ~STI_ROM_ENABLED;
523
524 return 0;
525
526 fail:
527 bus_space_unmap(pa->pa_memt, romh, romsize);
528 fail2:
529 gftfb_disable_rom_internal(spc);
530
531 return rc;
532 }
533
534 /*
535 * Decode a BAR register.
536 */
537 int
538 gftfb_readbar(struct sti_softc *sc, struct pci_attach_args *pa, u_int region,
539 int bar)
540 {
541 bus_addr_t addr;
542 bus_size_t size;
543 uint32_t cf;
544 int rc;
545
546 if (bar == 0) {
547 sc->bases[region] = 0;
548 return (0);
549 }
550
551 #ifdef DIAGNOSTIC
552 if (bar < PCI_MAPREG_START || bar > PCI_MAPREG_PPB_END) {
553 gftfb_disable_rom(sc);
554 printf("%s: unexpected bar %02x for region %d\n",
555 device_xname(sc->sc_dev), bar, region);
556 gftfb_enable_rom(sc);
557 }
558 #endif
559
560 cf = pci_conf_read(pa->pa_pc, pa->pa_tag, bar);
561
562 rc = pci_mapreg_info(pa->pa_pc, pa->pa_tag, bar, PCI_MAPREG_TYPE(cf),
563 &addr, &size, NULL);
564
565 if (rc != 0) {
566 gftfb_disable_rom(sc);
567 aprint_error_dev(sc->sc_dev, "invalid bar %02x for region %d\n",
568 bar, region);
569 gftfb_enable_rom(sc);
570 return (rc);
571 }
572
573 sc->bases[region] = addr;
574 return (0);
575 }
576
577 /*
578 * Enable PCI ROM.
579 */
580 void
581 gftfb_enable_rom_internal(struct gftfb_softc *spc)
582 {
583 pcireg_t address;
584
585 KASSERT(spc != NULL);
586
587 address = pci_conf_read(spc->sc_pc, spc->sc_tag, PCI_MAPREG_ROM);
588 address |= PCI_MAPREG_ROM_ENABLE;
589 pci_conf_write(spc->sc_pc, spc->sc_tag, PCI_MAPREG_ROM, address);
590 }
591
592 void
593 gftfb_enable_rom(struct sti_softc *sc)
594 {
595 struct gftfb_softc *spc = device_private(sc->sc_dev);
596
597 if (!ISSET(sc->sc_flags, STI_ROM_ENABLED)) {
598 gftfb_enable_rom_internal(spc);
599 }
600 SET(sc->sc_flags, STI_ROM_ENABLED);
601 }
602
603 /*
604 * Disable PCI ROM.
605 */
606 void
607 gftfb_disable_rom_internal(struct gftfb_softc *spc)
608 {
609 pcireg_t address;
610
611 KASSERT(spc != NULL);
612
613 address = pci_conf_read(spc->sc_pc, spc->sc_tag, PCI_MAPREG_ROM);
614 address &= ~PCI_MAPREG_ROM_ENABLE;
615 pci_conf_write(spc->sc_pc, spc->sc_tag, PCI_MAPREG_ROM, address);
616 }
617
618 void
619 gftfb_disable_rom(struct sti_softc *sc)
620 {
621 struct gftfb_softc *spc = device_private(sc->sc_dev);
622
623 if (ISSET(sc->sc_flags, STI_ROM_ENABLED)) {
624 gftfb_disable_rom_internal(spc);
625 }
626 CLR(sc->sc_flags, STI_ROM_ENABLED);
627 }
628
629 static inline void
630 gftfb_wait(struct gftfb_softc *sc)
631 {
632 uint8_t stat;
633
634 do {
635 stat = gftfb_read1(sc, NGLE_REG_15b0);
636 if (stat == 0)
637 stat = gftfb_read1(sc, NGLE_REG_15b0);
638 } while (stat != 0);
639 }
640
641 static inline void
642 gftfb_setup_fb(struct gftfb_softc *sc)
643 {
644 gftfb_wait(sc);
645 gftfb_write4(sc, NGLE_REG_10,
646 BA(IndexedDcd, Otc04, Ots08, AddrByte, 0, BINapp0I, 0));
647 gftfb_write4(sc, NGLE_REG_14, 0x83000300);
648 gftfb_wait(sc);
649 gftfb_write1(sc, NGLE_REG_16b1, 1);
650 sc->sc_hwmode = HW_FB;
651 }
652
653 void
654 gftfb_setup(struct gftfb_softc *sc)
655 {
656 struct sti_rom *rom = sc->sc_base.sc_rom;
657 bus_space_tag_t memt = rom->memt;
658 bus_space_handle_t memh = rom->regh[2];
659 int i;
660
661 sc->sc_hwmode = HW_FB;
662 sc->sc_hot_x = 0;
663 sc->sc_hot_y = 0;
664 sc->sc_enabled = 0;
665 sc->sc_video_on = 1;
666
667
668 /* set Bt458 read mask register to all planes */
669 gftfb_wait(sc);
670 ngle_bt458_write(memt, memh, 0x08, 0x04);
671 ngle_bt458_write(memt, memh, 0x0a, 0xff);
672
673 gftfb_setup_fb(sc);
674
675 /* attr. planes */
676 gftfb_wait(sc);
677 gftfb_write4(sc, NGLE_REG_11, 0x2ea0d000);
678 gftfb_write4(sc, NGLE_REG_14, 0x23000302);
679 gftfb_write4(sc, NGLE_REG_12, NGLE_ARTIST_CMAP0);
680 gftfb_write4(sc, NGLE_REG_8, 0xffffffff);
681
682 gftfb_wait(sc);
683 gftfb_write4(sc, NGLE_REG_6, 0x00000000);
684 gftfb_write4(sc, NGLE_REG_9,
685 (sc->sc_scr.scr_cfg.scr_width << 16) | sc->sc_scr.scr_cfg.scr_height);
686 /*
687 * blit into offscreen memory to force flush previous - apparently
688 * some chips have a bug this works around
689 */
690 gftfb_write4(sc, NGLE_REG_6, 0x05000000);
691 gftfb_write4(sc, NGLE_REG_9, 0x00040001);
692
693 gftfb_wait(sc);
694 gftfb_write4(sc, NGLE_REG_12, 0x00000000);
695
696 gftfb_setup_fb(sc);
697
698 /* make sure video output is enabled */
699 gftfb_wait(sc);
700 gftfb_write4(sc, NGLE_REG_21,
701 gftfb_read4(sc, NGLE_REG_21) | 0x0a000000);
702 gftfb_write4(sc, NGLE_REG_27,
703 gftfb_read4(sc, NGLE_REG_27) | 0x00800000);
704
705 /* initialize cursor sprite */
706 gftfb_wait(sc);
707
708 /* cursor mask */
709 gftfb_wait(sc);
710 gftfb_write4(sc, NGLE_REG_14, 0x300);
711 gftfb_write4(sc, NGLE_REG_13, 0xffffffff);
712 gftfb_write4(sc, NGLE_REG_11,
713 BA(IndexedDcd, Otc32, 0, AddrLong, 0, BINcmask, 0));
714 gftfb_write4(sc, NGLE_REG_3, 0);
715 for (i = 0; i < 64; i++) {
716 gftfb_write4(sc, NGLE_REG_4, 0xffffffff);
717 gftfb_write4(sc, NGLE_REG_5, 0xffffffff);
718 }
719
720 /* cursor image */
721 gftfb_wait(sc);
722 gftfb_write4(sc, NGLE_REG_14, 0x300);
723 gftfb_write4(sc, NGLE_REG_13, 0xffffffff);
724 gftfb_write4(sc, NGLE_REG_11,
725 BA(IndexedDcd, Otc32, 0, AddrLong, 0, BINcursor, 0));
726 gftfb_write4(sc, NGLE_REG_3, 0);
727 for (i = 0; i < 64; i++) {
728 gftfb_write4(sc, NGLE_REG_4, 0xff00ff00);
729 gftfb_write4(sc, NGLE_REG_5, 0xff00ff00);
730 }
731
732 /* colour map */
733 gftfb_wait(sc);
734 gftfb_write4(sc, NGLE_REG_10,
735 BA(FractDcd, Otc24, Ots08, Addr24, 0, BINcmap, 0));
736 gftfb_write4(sc, NGLE_REG_14, 0x03000300);
737 gftfb_write4(sc, NGLE_REG_13, 0xffffffff);
738 gftfb_wait(sc);
739 gftfb_write4(sc, NGLE_REG_3, 0);
740 gftfb_write4(sc, NGLE_REG_4, 0);
741 gftfb_write4(sc, NGLE_REG_4, 0);
742 gftfb_write4(sc, NGLE_REG_4, 0x000000ff); /* BG */
743 gftfb_write4(sc, NGLE_REG_4, 0x00ff0000); /* FG */
744 gftfb_wait(sc);
745 gftfb_write4(sc, NGLE_REG_2, 0);
746 gftfb_write4(sc, NGLE_REG_26, 0x80008004);
747 gftfb_setup_fb(sc);
748
749 gftfb_move_cursor(sc, 100, 100);
750
751 }
752
753 static int
754 gftfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
755 struct lwp *l)
756 {
757 struct vcons_data *vd = v;
758 struct gftfb_softc *sc = vd->cookie;
759 struct wsdisplay_fbinfo *wdf;
760 struct vcons_screen *ms = vd->active;
761
762 switch (cmd) {
763 case WSDISPLAYIO_GTYPE:
764 *(u_int *)data = WSDISPLAY_TYPE_STI;
765 return 0;
766
767 case GCID:
768 *(u_int *)data = STI_DD_EG;
769 return 0;
770
771 /* PCI config read/write passthrough. */
772 case PCI_IOC_CFGREAD:
773 case PCI_IOC_CFGWRITE:
774 return pci_devioctl(sc->sc_pc, sc->sc_tag,
775 cmd, data, flag, l);
776
777 case WSDISPLAYIO_GET_BUSID:
778 return wsdisplayio_busid_pci(sc->sc_dev, sc->sc_pc,
779 sc->sc_tag, data);
780
781 case WSDISPLAYIO_GINFO:
782 if (ms == NULL)
783 return ENODEV;
784 wdf = (void *)data;
785 wdf->height = ms->scr_ri.ri_height;
786 wdf->width = ms->scr_ri.ri_width;
787 wdf->depth = ms->scr_ri.ri_depth;
788 wdf->cmsize = 256;
789 return 0;
790
791 case WSDISPLAYIO_GETCMAP:
792 return gftfb_getcmap(sc,
793 (struct wsdisplay_cmap *)data);
794
795 case WSDISPLAYIO_PUTCMAP:
796 return gftfb_putcmap(sc,
797 (struct wsdisplay_cmap *)data);
798
799 case WSDISPLAYIO_LINEBYTES:
800 *(u_int *)data = 2048;
801 return 0;
802
803 case WSDISPLAYIO_SMODE: {
804 int new_mode = *(int*)data;
805 if (new_mode != sc->sc_mode) {
806 sc->sc_mode = new_mode;
807 if(new_mode == WSDISPLAYIO_MODE_EMUL) {
808 gftfb_setup(sc);
809 gftfb_restore_palette(sc);
810 glyphcache_wipe(&sc->sc_gc);
811 gftfb_rectfill(sc, 0, 0, sc->sc_width,
812 sc->sc_height, ms->scr_ri.ri_devcmap[
813 (ms->scr_defattr >> 16) & 0xff]);
814 vcons_redraw_screen(ms);
815 gftfb_set_video(sc, 1);
816 }
817 }
818 }
819 return 0;
820
821 case WSDISPLAYIO_GET_FBINFO:
822 {
823 struct wsdisplayio_fbinfo *fbi = data;
824 int ret;
825
826 ret = wsdisplayio_get_fbinfo(&ms->scr_ri, fbi);
827 fbi->fbi_fbsize = sc->sc_scr.fbheight * 2048;
828 return ret;
829 }
830
831 case WSDISPLAYIO_GCURPOS:
832 {
833 struct wsdisplay_curpos *cp = (void *)data;
834
835 cp->x = sc->sc_cursor_x;
836 cp->y = sc->sc_cursor_y;
837 }
838 return 0;
839
840 case WSDISPLAYIO_SCURPOS:
841 {
842 struct wsdisplay_curpos *cp = (void *)data;
843
844 gftfb_move_cursor(sc, cp->x, cp->y);
845 }
846 return 0;
847
848 case WSDISPLAYIO_GCURMAX:
849 {
850 struct wsdisplay_curpos *cp = (void *)data;
851
852 cp->x = 64;
853 cp->y = 64;
854 }
855 return 0;
856
857 case WSDISPLAYIO_SCURSOR:
858 {
859 struct wsdisplay_cursor *cursor = (void *)data;
860
861 return gftfb_do_cursor(sc, cursor);
862 }
863
864 case WSDISPLAYIO_SVIDEO:
865 gftfb_set_video(sc, *(int *)data);
866 return 0;
867 case WSDISPLAYIO_GVIDEO:
868 *(u_int *)data = sc->sc_video_on ?
869 WSDISPLAYIO_VIDEO_ON : WSDISPLAYIO_VIDEO_OFF;
870 return 0;
871 }
872 return EPASSTHROUGH;
873 }
874
875 static paddr_t
876 gftfb_mmap(void *v, void *vs, off_t offset, int prot)
877 {
878 struct vcons_data *vd = v;
879 struct gftfb_softc *sc = vd->cookie;
880 struct sti_rom *rom = sc->sc_base.sc_rom;
881 paddr_t pa = -1;
882
883 if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)
884 return -1;
885
886 if (offset >= 0 && offset < sc->sc_scr.fblen) {
887 /* framebuffer */
888 pa = bus_space_mmap(rom->memt, sc->sc_scr.fbaddr, offset,
889 prot, BUS_SPACE_MAP_LINEAR);
890 } else if (offset >= 0x80000000 && offset < 0x80400000) {
891 /* blitter registers etc. */
892 pa = bus_space_mmap(rom->memt, rom->regh[2],
893 offset - 0x80000000, prot, BUS_SPACE_MAP_LINEAR);
894 }
895
896 return pa;
897 }
898
899 static void
900 gftfb_init_screen(void *cookie, struct vcons_screen *scr,
901 int existing, long *defattr)
902 {
903 struct gftfb_softc *sc = cookie;
904 struct rasops_info *ri = &scr->scr_ri;
905
906 ri->ri_depth = 8;
907 ri->ri_width = sc->sc_width;
908 ri->ri_height = sc->sc_height;
909 ri->ri_stride = 2048;
910 ri->ri_flg = RI_CENTER | RI_8BIT_IS_RGB |
911 RI_ENABLE_ALPHA | RI_PREFER_ALPHA;
912
913 ri->ri_bits = (void *)sc->sc_scr.fbaddr;
914 rasops_init(ri, 0, 0);
915 ri->ri_caps = WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_UNDERLINE |
916 WSSCREEN_RESIZE;
917 scr->scr_flags |= VCONS_LOADFONT;
918
919 rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
920 sc->sc_width / ri->ri_font->fontwidth);
921
922 ri->ri_hw = scr;
923 sc->sc_putchar = ri->ri_ops.putchar;
924 ri->ri_ops.copyrows = gftfb_copyrows;
925 ri->ri_ops.copycols = gftfb_copycols;
926 ri->ri_ops.eraserows = gftfb_eraserows;
927 ri->ri_ops.erasecols = gftfb_erasecols;
928 ri->ri_ops.cursor = gftfb_cursor;
929 if (FONT_IS_ALPHA(ri->ri_font)) {
930 ri->ri_ops.putchar = gftfb_putchar_aa;
931 } else
932 ri->ri_ops.putchar = gftfb_putchar;
933 }
934
935 static int
936 gftfb_putcmap(struct gftfb_softc *sc, struct wsdisplay_cmap *cm)
937 {
938 u_char *r, *g, *b;
939 u_int index = cm->index;
940 u_int count = cm->count;
941 int i, error;
942 u_char rbuf[256], gbuf[256], bbuf[256];
943
944 if (cm->index >= 256 || cm->count > 256 ||
945 (cm->index + cm->count) > 256)
946 return EINVAL;
947 error = copyin(cm->red, &rbuf[index], count);
948 if (error)
949 return error;
950 error = copyin(cm->green, &gbuf[index], count);
951 if (error)
952 return error;
953 error = copyin(cm->blue, &bbuf[index], count);
954 if (error)
955 return error;
956
957 memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
958 memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
959 memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
960
961 r = &sc->sc_cmap_red[index];
962 g = &sc->sc_cmap_green[index];
963 b = &sc->sc_cmap_blue[index];
964
965 for (i = 0; i < count; i++) {
966 gftfb_putpalreg(sc, index, *r, *g, *b);
967 index++;
968 r++, g++, b++;
969 }
970 return 0;
971 }
972
973 static int
974 gftfb_getcmap(struct gftfb_softc *sc, struct wsdisplay_cmap *cm)
975 {
976 u_int index = cm->index;
977 u_int count = cm->count;
978 int error;
979
980 if (index >= 255 || count > 256 || index + count > 256)
981 return EINVAL;
982
983 error = copyout(&sc->sc_cmap_red[index], cm->red, count);
984 if (error)
985 return error;
986 error = copyout(&sc->sc_cmap_green[index], cm->green, count);
987 if (error)
988 return error;
989 error = copyout(&sc->sc_cmap_blue[index], cm->blue, count);
990 if (error)
991 return error;
992
993 return 0;
994 }
995
996 static void
997 gftfb_restore_palette(struct gftfb_softc *sc)
998 {
999 uint8_t cmap[768];
1000 int i, j;
1001
1002 j = 0;
1003 rasops_get_cmap(&sc->sc_console_screen.scr_ri, cmap, sizeof(cmap));
1004 for (i = 0; i < 256; i++) {
1005 sc->sc_cmap_red[i] = cmap[j];
1006 sc->sc_cmap_green[i] = cmap[j + 1];
1007 sc->sc_cmap_blue[i] = cmap[j + 2];
1008 gftfb_putpalreg(sc, i, cmap[j], cmap[j + 1], cmap[j + 2]);
1009 j += 3;
1010 }
1011 }
1012
1013 static int
1014 gftfb_putpalreg(struct gftfb_softc *sc, uint8_t idx, uint8_t r, uint8_t g,
1015 uint8_t b)
1016 {
1017 mutex_enter(&sc->sc_hwlock);
1018 gftfb_wait(sc);
1019 gftfb_write4(sc, NGLE_REG_10,
1020 BA(FractDcd, Otc24, Ots08, Addr24, 0, BINcmap, 0));
1021 gftfb_write4(sc, NGLE_REG_14, 0x03000300);
1022 gftfb_write4(sc, NGLE_REG_13, 0xffffffff);
1023
1024 gftfb_wait(sc);
1025 gftfb_write4(sc, NGLE_REG_3, 0x400 | (idx << 2));
1026 gftfb_write4(sc, NGLE_REG_4, (r << 16) | (g << 8) | b);
1027
1028 gftfb_write4(sc, NGLE_REG_2, 0x400);
1029 gftfb_write4(sc, NGLE_REG_26, 0x80000100);
1030 gftfb_setup_fb(sc);
1031 mutex_exit(&sc->sc_hwlock);
1032 return 0;
1033 }
1034
1035 static inline void
1036 gftfb_wait_fifo(struct gftfb_softc *sc, uint32_t slots)
1037 {
1038 uint32_t reg;
1039
1040 do {
1041 reg = gftfb_read4(sc, NGLE_REG_34);
1042 } while (reg < slots);
1043 }
1044
1045 static inline void
1046 gftfb_fillmode(struct gftfb_softc *sc)
1047 {
1048 if (sc->sc_hwmode != HW_FILL) {
1049 gftfb_wait_fifo(sc, 3);
1050 /* plane mask */
1051 gftfb_write4(sc, NGLE_REG_13, 0xff);
1052 /* bitmap op */
1053 gftfb_write4(sc, NGLE_REG_14,
1054 IBOvals(RopSrc, 0, BitmapExtent08, 1, DataDynamic, 0,
1055 0, 0));
1056 /* dst bitmap access */
1057 gftfb_write4(sc, NGLE_REG_11,
1058 BA(IndexedDcd, Otc32, OtsIndirect, AddrLong, 0, BINapp0I,
1059 0));
1060 sc->sc_hwmode = HW_FILL;
1061 }
1062 }
1063
1064 static void
1065 gftfb_rectfill(struct gftfb_softc *sc, int x, int y, int wi, int he,
1066 uint32_t bg)
1067 {
1068 gftfb_fillmode(sc);
1069
1070 gftfb_wait_fifo(sc, 4);
1071
1072 /* transfer data */
1073 gftfb_write4(sc, NGLE_REG_8, 0xffffffff);
1074 gftfb_write4(sc, NGLE_REG_35, bg);
1075 /* dst XY */
1076 gftfb_write4(sc, NGLE_REG_6, (x << 16) | y);
1077 /* len XY start */
1078 gftfb_write4(sc, NGLE_REG_9, (wi << 16) | he);
1079
1080 }
1081
1082 static void
1083 gftfb_bitblt(void *cookie, int xs, int ys, int xd, int yd, int wi,
1084 int he, int rop)
1085 {
1086 struct gftfb_softc *sc = cookie;
1087
1088 if (sc->sc_hwmode != HW_BLIT) {
1089 gftfb_wait(sc);
1090 gftfb_write4(sc, NGLE_REG_10,
1091 BA(IndexedDcd, Otc04, Ots08, AddrLong, 0, BINapp0I, 0));
1092 sc->sc_hwmode = HW_BLIT;
1093 }
1094 gftfb_wait_fifo(sc, 5);
1095 gftfb_write4(sc, NGLE_REG_14,
1096 IBOvals(rop, 0, BitmapExtent08, 1, DataDynamic, MaskOtc, 0, 0));
1097 gftfb_write4(sc, NGLE_REG_13, 0xff);
1098 gftfb_write4(sc, NGLE_REG_24, (xs << 16) | ys);
1099 gftfb_write4(sc, NGLE_REG_7, (wi << 16) | he);
1100 gftfb_write4(sc, NGLE_REG_25, (xd << 16) | yd);
1101 }
1102
1103 static void
1104 gftfb_nuke_cursor(struct rasops_info *ri)
1105 {
1106 struct vcons_screen *scr = ri->ri_hw;
1107 struct gftfb_softc *sc = scr->scr_cookie;
1108 int wi, he, x, y;
1109
1110 if (ri->ri_flg & RI_CURSOR) {
1111 wi = ri->ri_font->fontwidth;
1112 he = ri->ri_font->fontheight;
1113 x = ri->ri_ccol * wi + ri->ri_xorigin;
1114 y = ri->ri_crow * he + ri->ri_yorigin;
1115 gftfb_bitblt(sc, x, y, x, y, wi, he, RopInv);
1116 ri->ri_flg &= ~RI_CURSOR;
1117 }
1118 }
1119
1120 static void
1121 gftfb_cursor(void *cookie, int on, int row, int col)
1122 {
1123 struct rasops_info *ri = cookie;
1124 struct vcons_screen *scr = ri->ri_hw;
1125 struct gftfb_softc *sc = scr->scr_cookie;
1126 int x, y, wi, he;
1127
1128 wi = ri->ri_font->fontwidth;
1129 he = ri->ri_font->fontheight;
1130
1131 if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
1132 if (on) {
1133 if (ri->ri_flg & RI_CURSOR) {
1134 gftfb_nuke_cursor(ri);
1135 }
1136 x = col * wi + ri->ri_xorigin;
1137 y = row * he + ri->ri_yorigin;
1138 gftfb_bitblt(sc, x, y, x, y, wi, he, RopInv);
1139 ri->ri_flg |= RI_CURSOR;
1140 }
1141 ri->ri_crow = row;
1142 ri->ri_ccol = col;
1143 } else
1144 {
1145 ri->ri_crow = row;
1146 ri->ri_ccol = col;
1147 ri->ri_flg &= ~RI_CURSOR;
1148 }
1149
1150 }
1151
1152 static void
1153 gftfb_putchar(void *cookie, int row, int col, u_int c, long attr)
1154 {
1155 struct rasops_info *ri = cookie;
1156 struct wsdisplay_font *font = PICK_FONT(ri, c);
1157 struct vcons_screen *scr = ri->ri_hw;
1158 struct gftfb_softc *sc = scr->scr_cookie;
1159 void *data;
1160 int i, x, y, wi, he, rv = GC_NOPE;
1161 uint32_t bg, fg, mask;
1162
1163 if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL)
1164 return;
1165
1166 if (!CHAR_IN_FONT(c, font))
1167 return;
1168
1169 if (row == ri->ri_crow && col == ri->ri_ccol) {
1170 ri->ri_flg &= ~RI_CURSOR;
1171 }
1172
1173 wi = font->fontwidth;
1174 he = font->fontheight;
1175
1176 x = ri->ri_xorigin + col * wi;
1177 y = ri->ri_yorigin + row * he;
1178
1179 bg = ri->ri_devcmap[(attr >> 16) & 0xf];
1180
1181 /* if we're drawing a space we're done here */
1182 if (c == 0x20) {
1183 gftfb_rectfill(sc, x, y, wi, he, bg);
1184 return;
1185 }
1186
1187
1188 rv = glyphcache_try(&sc->sc_gc, c, x, y, attr);
1189 if (rv == GC_OK)
1190 return;
1191
1192 data = WSFONT_GLYPH(c, font);
1193 fg = ri->ri_devcmap[(attr >> 24) & 0x0f];
1194
1195 gftfb_fillmode(sc);
1196 gftfb_wait_fifo(sc, 3);
1197
1198 /* character colour */
1199 gftfb_write4(sc, NGLE_REG_35, fg);
1200 gftfb_write4(sc, NGLE_REG_36, bg);
1201 /* dst XY */
1202 gftfb_write4(sc, NGLE_REG_6, (x << 16) | y);
1203
1204 if (ri->ri_font->stride == 1) {
1205 uint8_t *data8 = data;
1206 for (i = 0; i < he; i++) {
1207 gftfb_wait_fifo(sc, 2);
1208 mask = *data8;
1209 gftfb_write4(sc, NGLE_REG_8, mask << 24);
1210 gftfb_write4(sc, NGLE_REG_9, (wi << 16) | 1);
1211 data8++;
1212 }
1213 } else {
1214 uint16_t *data16 = data;
1215 for (i = 0; i < he; i++) {
1216 gftfb_wait_fifo(sc, 2);
1217 mask = *data16;
1218 gftfb_write4(sc, NGLE_REG_8, mask << 16);
1219 gftfb_write4(sc, NGLE_REG_9, (wi << 16) | 1);
1220 data16++;
1221 }
1222 }
1223
1224 if (rv == GC_ADD)
1225 glyphcache_add(&sc->sc_gc, c, x, y);
1226 }
1227
1228 static void
1229 gftfb_putchar_aa(void *cookie, int row, int col, u_int c, long attr)
1230 {
1231 struct rasops_info *ri = cookie;
1232 struct wsdisplay_font *font = PICK_FONT(ri, c);
1233 struct vcons_screen *scr = ri->ri_hw;
1234 struct gftfb_softc *sc = scr->scr_cookie;
1235 int x, y, wi, he, rv = GC_NOPE;
1236 uint32_t bg;
1237
1238 if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL)
1239 return;
1240
1241 if (!CHAR_IN_FONT(c, font))
1242 return;
1243
1244 if (row == ri->ri_crow && col == ri->ri_ccol) {
1245 ri->ri_flg &= ~RI_CURSOR;
1246 }
1247
1248 wi = font->fontwidth;
1249 he = font->fontheight;
1250
1251 x = ri->ri_xorigin + col * wi;
1252 y = ri->ri_yorigin + row * he;
1253
1254 bg = ri->ri_devcmap[(attr >> 16) & 0xf];
1255
1256 if (c == 0x20) {
1257 gftfb_rectfill(sc, x, y, wi, he, bg);
1258 return;
1259 }
1260
1261 rv = glyphcache_try(&sc->sc_gc, c, x, y, attr);
1262 if (rv == GC_OK)
1263 return;
1264
1265 if (sc->sc_hwmode != HW_FB) gftfb_setup_fb(sc);
1266 sc->sc_putchar(cookie, row, col, c, attr);
1267
1268 if (rv == GC_ADD)
1269 glyphcache_add(&sc->sc_gc, c, x, y);
1270 }
1271
1272 static void
1273 gftfb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
1274 {
1275 struct rasops_info *ri = cookie;
1276 struct vcons_screen *scr = ri->ri_hw;
1277 struct gftfb_softc *sc = scr->scr_cookie;
1278 int32_t xs, xd, y, width, height;
1279
1280 if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
1281 if (ri->ri_crow == row &&
1282 (ri->ri_ccol >= srccol && ri->ri_ccol < (srccol + ncols)) &&
1283 (ri->ri_flg & RI_CURSOR)) {
1284 gftfb_nuke_cursor(ri);
1285 }
1286
1287 xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
1288 xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
1289 y = ri->ri_yorigin + ri->ri_font->fontheight * row;
1290 width = ri->ri_font->fontwidth * ncols;
1291 height = ri->ri_font->fontheight;
1292 gftfb_bitblt(sc, xs, y, xd, y, width, height, RopSrc);
1293 if (ri->ri_crow == row &&
1294 (ri->ri_ccol >= dstcol && ri->ri_ccol < (dstcol + ncols)))
1295 ri->ri_flg &= ~RI_CURSOR;
1296 }
1297 }
1298
1299 static void
1300 gftfb_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
1301 {
1302 struct rasops_info *ri = cookie;
1303 struct vcons_screen *scr = ri->ri_hw;
1304 struct gftfb_softc *sc = scr->scr_cookie;
1305 int32_t x, y, width, height, fg, bg, ul;
1306
1307 if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
1308 x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
1309 y = ri->ri_yorigin + ri->ri_font->fontheight * row;
1310 width = ri->ri_font->fontwidth * ncols;
1311 height = ri->ri_font->fontheight;
1312 rasops_unpack_attr(fillattr, &fg, &bg, &ul);
1313
1314 gftfb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
1315 if (ri->ri_crow == row &&
1316 (ri->ri_ccol >= startcol && ri->ri_ccol < (startcol + ncols)))
1317 ri->ri_flg &= ~RI_CURSOR;
1318 }
1319 }
1320
1321 static void
1322 gftfb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
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, ys, yd, width, height;
1328
1329 if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
1330 if ((ri->ri_crow >= srcrow && ri->ri_crow < (srcrow + nrows)) &&
1331 (ri->ri_flg & RI_CURSOR)) {
1332 gftfb_nuke_cursor(ri);
1333 }
1334 x = ri->ri_xorigin;
1335 ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
1336 yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
1337 width = ri->ri_emuwidth;
1338 height = ri->ri_font->fontheight * nrows;
1339 gftfb_bitblt(sc, x, ys, x, yd, width, height, RopSrc);
1340 if (ri->ri_crow >= dstrow && ri->ri_crow < (dstrow + nrows))
1341 ri->ri_flg &= ~RI_CURSOR;
1342 }
1343 }
1344
1345 static void
1346 gftfb_eraserows(void *cookie, int row, int nrows, long fillattr)
1347 {
1348 struct rasops_info *ri = cookie;
1349 struct vcons_screen *scr = ri->ri_hw;
1350 struct gftfb_softc *sc = scr->scr_cookie;
1351 int32_t x, y, width, height, fg, bg, ul;
1352
1353 if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
1354 x = ri->ri_xorigin;
1355 y = ri->ri_yorigin + ri->ri_font->fontheight * row;
1356 width = ri->ri_emuwidth;
1357 height = ri->ri_font->fontheight * nrows;
1358 rasops_unpack_attr(fillattr, &fg, &bg, &ul);
1359
1360 gftfb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
1361
1362 if (ri->ri_crow >= row && ri->ri_crow < (row + nrows))
1363 ri->ri_flg &= ~RI_CURSOR;
1364 }
1365 }
1366
1367 /*
1368 * cursor sprite handling
1369 * like most hw info, xf86 3.3 -> nglehdw.h was used as documentation
1370 * problem is, the PCI EG doesn't quite behave like an S9000_ID_ARTIST
1371 * the cursor position register behaves like the one on HCRX while using
1372 * the same address as Artist, incuding the enable bit and weird handling
1373 * of negative coordinates. The rest of it, colour map, sprite image etc.,
1374 * behave like Artist.
1375 */
1376
1377 static void
1378 gftfb_move_cursor(struct gftfb_softc *sc, int x, int y)
1379 {
1380 uint32_t pos;
1381
1382 sc->sc_cursor_x = x;
1383 x -= sc->sc_hot_x;
1384 sc->sc_cursor_y = y;
1385 y -= sc->sc_hot_y;
1386
1387 if (x < 0) x = 0x1000 - x;
1388 if (y < 0) y = 0x1000 - y;
1389 pos = (x << 16) | y;
1390 if (sc->sc_enabled) pos |= 0x80000000;
1391 gftfb_wait_fifo(sc, 2);
1392 gftfb_write4(sc, NGLE_REG_17, pos);
1393 }
1394
1395 static int
1396 gftfb_do_cursor(struct gftfb_softc *sc, struct wsdisplay_cursor *cur)
1397 {
1398 if (cur->which & WSDISPLAY_CURSOR_DOCUR) {
1399
1400 sc->sc_enabled = cur->enable;
1401 cur->which |= WSDISPLAY_CURSOR_DOPOS;
1402 }
1403 if (cur->which & WSDISPLAY_CURSOR_DOHOT) {
1404
1405 sc->sc_hot_x = cur->hot.x;
1406 sc->sc_hot_y = cur->hot.y;
1407 cur->which |= WSDISPLAY_CURSOR_DOPOS;
1408 }
1409 if (cur->which & WSDISPLAY_CURSOR_DOPOS) {
1410
1411 gftfb_move_cursor(sc, cur->pos.x, cur->pos.y);
1412 }
1413 if (cur->which & WSDISPLAY_CURSOR_DOCMAP) {
1414 uint32_t rgb;
1415 uint8_t r[2], g[2], b[2];
1416
1417 copyin(cur->cmap.blue, b, 2);
1418 copyin(cur->cmap.green, g, 2);
1419 copyin(cur->cmap.red, r, 2);
1420 mutex_enter(&sc->sc_hwlock);
1421 gftfb_wait(sc);
1422 gftfb_write4(sc, NGLE_REG_10,
1423 BA(FractDcd, Otc24, Ots08, Addr24, 0, BINcmap, 0));
1424 gftfb_write4(sc, NGLE_REG_14, 0x03000300);
1425 gftfb_write4(sc, NGLE_REG_13, 0xffffffff);
1426 gftfb_wait(sc);
1427 gftfb_write4(sc, NGLE_REG_3, 0);
1428 gftfb_write4(sc, NGLE_REG_4, 0);
1429 gftfb_write4(sc, NGLE_REG_4, 0);
1430 rgb = (r[0] << 16) | (g[0] << 8) | b[0];
1431 gftfb_write4(sc, NGLE_REG_4, rgb); /* BG */
1432 rgb = (r[1] << 16) | (g[1] << 8) | b[1];
1433 gftfb_write4(sc, NGLE_REG_4, rgb); /* FG */
1434 gftfb_write4(sc, NGLE_REG_2, 0);
1435 gftfb_write4(sc, NGLE_REG_26, 0x80008004);
1436 gftfb_setup_fb(sc);
1437 mutex_exit(&sc->sc_hwlock);
1438
1439 }
1440 if (cur->which & WSDISPLAY_CURSOR_DOSHAPE) {
1441 uint32_t buffer[128], latch, tmp;
1442 int i;
1443
1444 copyin(cur->mask, buffer, 512);
1445 gftfb_wait(sc);
1446 gftfb_write4(sc, NGLE_REG_14, 0x300);
1447 gftfb_write4(sc, NGLE_REG_13, 0xffffffff);
1448 gftfb_write4(sc, NGLE_REG_11,
1449 BA(IndexedDcd, Otc32, 0, AddrLong, 0, BINcmask, 0));
1450 gftfb_write4(sc, NGLE_REG_3, 0);
1451 for (i = 0; i < 128; i += 2) {
1452 latch = 0;
1453 tmp = buffer[i] & 0x80808080;
1454 latch |= tmp >> 7;
1455 tmp = buffer[i] & 0x40404040;
1456 latch |= tmp >> 5;
1457 tmp = buffer[i] & 0x20202020;
1458 latch |= tmp >> 3;
1459 tmp = buffer[i] & 0x10101010;
1460 latch |= tmp >> 1;
1461 tmp = buffer[i] & 0x08080808;
1462 latch |= tmp << 1;
1463 tmp = buffer[i] & 0x04040404;
1464 latch |= tmp << 3;
1465 tmp = buffer[i] & 0x02020202;
1466 latch |= tmp << 5;
1467 tmp = buffer[i] & 0x01010101;
1468 latch |= tmp << 7;
1469 gftfb_write4(sc, NGLE_REG_4, latch);
1470 latch = 0;
1471 tmp = buffer[i + 1] & 0x80808080;
1472 latch |= tmp >> 7;
1473 tmp = buffer[i + 1] & 0x40404040;
1474 latch |= tmp >> 5;
1475 tmp = buffer[i + 1] & 0x20202020;
1476 latch |= tmp >> 3;
1477 tmp = buffer[i + 1] & 0x10101010;
1478 latch |= tmp >> 1;
1479 tmp = buffer[i + 1] & 0x08080808;
1480 latch |= tmp << 1;
1481 tmp = buffer[i + 1] & 0x04040404;
1482 latch |= tmp << 3;
1483 tmp = buffer[i + 1] & 0x02020202;
1484 latch |= tmp << 5;
1485 tmp = buffer[i + 1] & 0x01010101;
1486 latch |= tmp << 7;
1487 gftfb_write4(sc, NGLE_REG_5, latch);
1488 }
1489
1490 copyin(cur->image, buffer, 512);
1491 gftfb_wait(sc);
1492 gftfb_write4(sc, NGLE_REG_14, 0x300);
1493 gftfb_write4(sc, NGLE_REG_13, 0xffffffff);
1494 gftfb_write4(sc, NGLE_REG_11,
1495 BA(IndexedDcd, Otc32, 0, AddrLong, 0, BINcursor, 0));
1496 gftfb_write4(sc, NGLE_REG_3, 0);
1497 for (i = 0; i < 128; i += 2) {
1498 latch = 0;
1499 tmp = buffer[i] & 0x80808080;
1500 latch |= tmp >> 7;
1501 tmp = buffer[i] & 0x40404040;
1502 latch |= tmp >> 5;
1503 tmp = buffer[i] & 0x20202020;
1504 latch |= tmp >> 3;
1505 tmp = buffer[i] & 0x10101010;
1506 latch |= tmp >> 1;
1507 tmp = buffer[i] & 0x08080808;
1508 latch |= tmp << 1;
1509 tmp = buffer[i] & 0x04040404;
1510 latch |= tmp << 3;
1511 tmp = buffer[i] & 0x02020202;
1512 latch |= tmp << 5;
1513 tmp = buffer[i] & 0x01010101;
1514 latch |= tmp << 7;
1515 gftfb_write4(sc, NGLE_REG_4, latch);
1516 latch = 0;
1517 tmp = buffer[i + 1] & 0x80808080;
1518 latch |= tmp >> 7;
1519 tmp = buffer[i + 1] & 0x40404040;
1520 latch |= tmp >> 5;
1521 tmp = buffer[i + 1] & 0x20202020;
1522 latch |= tmp >> 3;
1523 tmp = buffer[i + 1] & 0x10101010;
1524 latch |= tmp >> 1;
1525 tmp = buffer[i + 1] & 0x08080808;
1526 latch |= tmp << 1;
1527 tmp = buffer[i + 1] & 0x04040404;
1528 latch |= tmp << 3;
1529 tmp = buffer[i + 1] & 0x02020202;
1530 latch |= tmp << 5;
1531 tmp = buffer[i + 1] & 0x01010101;
1532 latch |= tmp << 7;
1533 gftfb_write4(sc, NGLE_REG_5, latch);
1534 }
1535 gftfb_setup_fb(sc);
1536 }
1537
1538 return 0;
1539 }
1540
1541 static void
1542 gftfb_set_video(struct gftfb_softc *sc, int on)
1543 {
1544 if (sc->sc_video_on == on)
1545 return;
1546
1547 sc->sc_video_on = on;
1548
1549 gftfb_wait(sc);
1550 if (on) {
1551 gftfb_write4(sc, NGLE_REG_21,
1552 gftfb_read4(sc, NGLE_REG_21) | 0x0a000000);
1553 gftfb_write4(sc, NGLE_REG_27,
1554 gftfb_read4(sc, NGLE_REG_27) | 0x00800000);
1555 } else {
1556 gftfb_write4(sc, NGLE_REG_21,
1557 gftfb_read4(sc, NGLE_REG_21) & ~0x0a000000);
1558 gftfb_write4(sc, NGLE_REG_27,
1559 gftfb_read4(sc, NGLE_REG_27) & ~0x00800000);
1560 }
1561 }
1562