gftfb.c revision 1.23 1 /* $NetBSD: gftfb.c,v 1.23 2024/10/16 09:56:34 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 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, 0x13601000);
666 gftfb_write4(sc, NGLE_REG_14, 0x83000300);
667 gftfb_wait(sc);
668 gftfb_write1(sc, NGLE_REG_16b1, 1);
669 sc->sc_hwmode = HW_FB;
670 }
671
672 void
673 gftfb_setup(struct gftfb_softc *sc)
674 {
675 struct sti_rom *rom = sc->sc_base.sc_rom;
676 bus_space_tag_t memt = rom->memt;
677 bus_space_handle_t memh = rom->regh[2];
678 int i;
679
680 sc->sc_hwmode = HW_FB;
681 sc->sc_hot_x = 0;
682 sc->sc_hot_y = 0;
683 sc->sc_enabled = 0;
684 sc->sc_video_on = 1;
685
686
687 /* set Bt458 read mask register to all planes */
688 gftfb_wait(sc);
689 ngle_bt458_write(memt, memh, 0x08, 0x04);
690 ngle_bt458_write(memt, memh, 0x0a, 0xff);
691
692 gftfb_setup_fb(sc);
693
694 /* attr. planes */
695 gftfb_wait(sc);
696 gftfb_write4(sc, NGLE_REG_11, 0x2ea0d000);
697 gftfb_write4(sc, NGLE_REG_14, 0x23000302);
698 gftfb_write4(sc, NGLE_REG_12, NGLE_ARTIST_CMAP0);
699 gftfb_write4(sc, NGLE_REG_8, 0xffffffff);
700
701 gftfb_wait(sc);
702 gftfb_write4(sc, NGLE_REG_6, 0x00000000);
703 gftfb_write4(sc, NGLE_REG_9,
704 (sc->sc_scr.scr_cfg.scr_width << 16) | sc->sc_scr.scr_cfg.scr_height);
705 /*
706 * blit into offscreen memory to force flush previous - apparently
707 * some chips have a bug this works around
708 */
709 gftfb_write4(sc, NGLE_REG_6, 0x05000000);
710 gftfb_write4(sc, NGLE_REG_9, 0x00040001);
711
712 gftfb_wait(sc);
713 gftfb_write4(sc, NGLE_REG_12, 0x00000000);
714
715 gftfb_setup_fb(sc);
716
717 /* make sure video output is enabled */
718 gftfb_wait(sc);
719 gftfb_write4(sc, NGLE_REG_21,
720 gftfb_read4(sc, NGLE_REG_21) | 0x0a000000);
721 gftfb_write4(sc, NGLE_REG_27,
722 gftfb_read4(sc, NGLE_REG_27) | 0x00800000);
723
724 /* initialize cursor sprite */
725 gftfb_wait(sc);
726
727 /* cursor mask */
728 gftfb_wait(sc);
729 gftfb_write4(sc, NGLE_REG_14, 0x300);
730 gftfb_write4(sc, NGLE_REG_13, 0xffffffff);
731 gftfb_write4(sc, NGLE_REG_11, 0x28A07000);
732 gftfb_write4(sc, NGLE_REG_3, 0);
733 for (i = 0; i < 64; i++) {
734 gftfb_write4(sc, NGLE_REG_4, 0xffffffff);
735 gftfb_write4(sc, NGLE_REG_5, 0xffffffff);
736 }
737
738 /* cursor image */
739 gftfb_wait(sc);
740 gftfb_write4(sc, NGLE_REG_14, 0x300);
741 gftfb_write4(sc, NGLE_REG_13, 0xffffffff);
742 gftfb_write4(sc, NGLE_REG_11, 0x28A06000);
743 gftfb_write4(sc, NGLE_REG_3, 0);
744 for (i = 0; i < 64; i++) {
745 gftfb_write4(sc, NGLE_REG_4, 0xff00ff00);
746 gftfb_write4(sc, NGLE_REG_5, 0xff00ff00);
747 }
748
749 /* colour map */
750 gftfb_wait(sc);
751 gftfb_write4(sc, NGLE_REG_10, 0xBBE0F000);
752 gftfb_write4(sc, NGLE_REG_14, 0x03000300);
753 gftfb_write4(sc, NGLE_REG_13, 0xffffffff);
754 gftfb_wait(sc);
755 gftfb_write4(sc, NGLE_REG_3, 0);
756 gftfb_write4(sc, NGLE_REG_4, 0);
757 gftfb_write4(sc, NGLE_REG_4, 0);
758 gftfb_write4(sc, NGLE_REG_4, 0x000000ff); /* BG */
759 gftfb_write4(sc, NGLE_REG_4, 0x00ff0000); /* FG */
760 gftfb_wait(sc);
761 gftfb_write4(sc, NGLE_REG_2, 0);
762 gftfb_write4(sc, NGLE_REG_26, 0x80008004);
763 gftfb_setup_fb(sc);
764
765 gftfb_move_cursor(sc, 100, 100);
766
767 }
768
769 static int
770 gftfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
771 struct lwp *l)
772 {
773 struct vcons_data *vd = v;
774 struct gftfb_softc *sc = vd->cookie;
775 struct wsdisplay_fbinfo *wdf;
776 struct vcons_screen *ms = vd->active;
777
778 switch (cmd) {
779 case WSDISPLAYIO_GTYPE:
780 *(u_int *)data = WSDISPLAY_TYPE_STI;
781 return 0;
782
783 case GCID:
784 *(u_int *)data = STI_DD_EG;
785 return 0;
786
787 /* PCI config read/write passthrough. */
788 case PCI_IOC_CFGREAD:
789 case PCI_IOC_CFGWRITE:
790 return pci_devioctl(sc->sc_pc, sc->sc_tag,
791 cmd, data, flag, l);
792
793 case WSDISPLAYIO_GET_BUSID:
794 return wsdisplayio_busid_pci(sc->sc_dev, sc->sc_pc,
795 sc->sc_tag, data);
796
797 case WSDISPLAYIO_GINFO:
798 if (ms == NULL)
799 return ENODEV;
800 wdf = (void *)data;
801 wdf->height = ms->scr_ri.ri_height;
802 wdf->width = ms->scr_ri.ri_width;
803 wdf->depth = ms->scr_ri.ri_depth;
804 wdf->cmsize = 256;
805 return 0;
806
807 case WSDISPLAYIO_GETCMAP:
808 return gftfb_getcmap(sc,
809 (struct wsdisplay_cmap *)data);
810
811 case WSDISPLAYIO_PUTCMAP:
812 return gftfb_putcmap(sc,
813 (struct wsdisplay_cmap *)data);
814
815 case WSDISPLAYIO_LINEBYTES:
816 *(u_int *)data = 2048;
817 return 0;
818
819 case WSDISPLAYIO_SMODE: {
820 int new_mode = *(int*)data;
821 if (new_mode != sc->sc_mode) {
822 sc->sc_mode = new_mode;
823 if(new_mode == WSDISPLAYIO_MODE_EMUL) {
824 gftfb_setup(sc);
825 gftfb_restore_palette(sc);
826 glyphcache_wipe(&sc->sc_gc);
827 gftfb_rectfill(sc, 0, 0, sc->sc_width,
828 sc->sc_height, ms->scr_ri.ri_devcmap[
829 (ms->scr_defattr >> 16) & 0xff]);
830 vcons_redraw_screen(ms);
831 gftfb_set_video(sc, 1);
832 }
833 }
834 }
835 return 0;
836
837 case WSDISPLAYIO_GET_FBINFO:
838 {
839 struct wsdisplayio_fbinfo *fbi = data;
840 int ret;
841
842 ret = wsdisplayio_get_fbinfo(&ms->scr_ri, fbi);
843 fbi->fbi_fbsize = sc->sc_scr.fbheight * 2048;
844 return ret;
845 }
846
847 case WSDISPLAYIO_GCURPOS:
848 {
849 struct wsdisplay_curpos *cp = (void *)data;
850
851 cp->x = sc->sc_cursor_x;
852 cp->y = sc->sc_cursor_y;
853 }
854 return 0;
855
856 case WSDISPLAYIO_SCURPOS:
857 {
858 struct wsdisplay_curpos *cp = (void *)data;
859
860 gftfb_move_cursor(sc, cp->x, cp->y);
861 }
862 return 0;
863
864 case WSDISPLAYIO_GCURMAX:
865 {
866 struct wsdisplay_curpos *cp = (void *)data;
867
868 cp->x = 64;
869 cp->y = 64;
870 }
871 return 0;
872
873 case WSDISPLAYIO_SCURSOR:
874 {
875 struct wsdisplay_cursor *cursor = (void *)data;
876
877 return gftfb_do_cursor(sc, cursor);
878 }
879
880 case WSDISPLAYIO_SVIDEO:
881 gftfb_set_video(sc, *(int *)data);
882 return 0;
883 case WSDISPLAYIO_GVIDEO:
884 *(u_int *)data = sc->sc_video_on ?
885 WSDISPLAYIO_VIDEO_ON : WSDISPLAYIO_VIDEO_OFF;
886 return 0;
887 }
888 return EPASSTHROUGH;
889 }
890
891 static paddr_t
892 gftfb_mmap(void *v, void *vs, off_t offset, int prot)
893 {
894 struct vcons_data *vd = v;
895 struct gftfb_softc *sc = vd->cookie;
896 struct sti_rom *rom = sc->sc_base.sc_rom;
897 paddr_t pa = -1;
898
899 if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)
900 return -1;
901
902 if (offset >= 0 && offset < sc->sc_scr.fblen) {
903 /* framebuffer */
904 pa = bus_space_mmap(rom->memt, sc->sc_scr.fbaddr, offset,
905 prot, BUS_SPACE_MAP_LINEAR);
906 } else if (offset >= 0x80000000 && offset < 0x80400000) {
907 /* blitter registers etc. */
908 pa = bus_space_mmap(rom->memt, rom->regh[2],
909 offset - 0x80000000, prot, BUS_SPACE_MAP_LINEAR);
910 }
911
912 return pa;
913 }
914
915 static void
916 gftfb_init_screen(void *cookie, struct vcons_screen *scr,
917 int existing, long *defattr)
918 {
919 struct gftfb_softc *sc = cookie;
920 struct rasops_info *ri = &scr->scr_ri;
921
922 ri->ri_depth = 8;
923 ri->ri_width = sc->sc_width;
924 ri->ri_height = sc->sc_height;
925 ri->ri_stride = 2048;
926 ri->ri_flg = RI_CENTER | RI_8BIT_IS_RGB |
927 RI_ENABLE_ALPHA | RI_PREFER_ALPHA;
928
929 ri->ri_bits = (void *)sc->sc_scr.fbaddr;
930 rasops_init(ri, 0, 0);
931 ri->ri_caps = WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_UNDERLINE |
932 WSSCREEN_RESIZE;
933 scr->scr_flags |= VCONS_LOADFONT;
934
935 rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
936 sc->sc_width / ri->ri_font->fontwidth);
937
938 ri->ri_hw = scr;
939 sc->sc_putchar = ri->ri_ops.putchar;
940 ri->ri_ops.copyrows = gftfb_copyrows;
941 ri->ri_ops.copycols = gftfb_copycols;
942 ri->ri_ops.eraserows = gftfb_eraserows;
943 ri->ri_ops.erasecols = gftfb_erasecols;
944 ri->ri_ops.cursor = gftfb_cursor;
945 if (FONT_IS_ALPHA(ri->ri_font)) {
946 ri->ri_ops.putchar = gftfb_putchar_aa;
947 } else
948 ri->ri_ops.putchar = gftfb_putchar;
949 }
950
951 static int
952 gftfb_putcmap(struct gftfb_softc *sc, struct wsdisplay_cmap *cm)
953 {
954 u_char *r, *g, *b;
955 u_int index = cm->index;
956 u_int count = cm->count;
957 int i, error;
958 u_char rbuf[256], gbuf[256], bbuf[256];
959
960 if (cm->index >= 256 || cm->count > 256 ||
961 (cm->index + cm->count) > 256)
962 return EINVAL;
963 error = copyin(cm->red, &rbuf[index], count);
964 if (error)
965 return error;
966 error = copyin(cm->green, &gbuf[index], count);
967 if (error)
968 return error;
969 error = copyin(cm->blue, &bbuf[index], count);
970 if (error)
971 return error;
972
973 memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
974 memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
975 memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
976
977 r = &sc->sc_cmap_red[index];
978 g = &sc->sc_cmap_green[index];
979 b = &sc->sc_cmap_blue[index];
980
981 for (i = 0; i < count; i++) {
982 gftfb_putpalreg(sc, index, *r, *g, *b);
983 index++;
984 r++, g++, b++;
985 }
986 return 0;
987 }
988
989 static int
990 gftfb_getcmap(struct gftfb_softc *sc, struct wsdisplay_cmap *cm)
991 {
992 u_int index = cm->index;
993 u_int count = cm->count;
994 int error;
995
996 if (index >= 255 || count > 256 || index + count > 256)
997 return EINVAL;
998
999 error = copyout(&sc->sc_cmap_red[index], cm->red, count);
1000 if (error)
1001 return error;
1002 error = copyout(&sc->sc_cmap_green[index], cm->green, count);
1003 if (error)
1004 return error;
1005 error = copyout(&sc->sc_cmap_blue[index], cm->blue, count);
1006 if (error)
1007 return error;
1008
1009 return 0;
1010 }
1011
1012 static void
1013 gftfb_restore_palette(struct gftfb_softc *sc)
1014 {
1015 uint8_t cmap[768];
1016 int i, j;
1017
1018 j = 0;
1019 rasops_get_cmap(&sc->sc_console_screen.scr_ri, cmap, sizeof(cmap));
1020 for (i = 0; i < 256; i++) {
1021 sc->sc_cmap_red[i] = cmap[j];
1022 sc->sc_cmap_green[i] = cmap[j + 1];
1023 sc->sc_cmap_blue[i] = cmap[j + 2];
1024 gftfb_putpalreg(sc, i, cmap[j], cmap[j + 1], cmap[j + 2]);
1025 j += 3;
1026 }
1027 }
1028
1029 static int
1030 gftfb_putpalreg(struct gftfb_softc *sc, uint8_t idx, uint8_t r, uint8_t g,
1031 uint8_t b)
1032 {
1033 mutex_enter(&sc->sc_hwlock);
1034 gftfb_wait(sc);
1035 gftfb_write4(sc, NGLE_REG_10, 0xbbe0f000);
1036 gftfb_write4(sc, NGLE_REG_14, 0x03000300);
1037 gftfb_write4(sc, NGLE_REG_13, 0xffffffff);
1038
1039 gftfb_wait(sc);
1040 gftfb_write4(sc, NGLE_REG_3, 0x400 | (idx << 2));
1041 gftfb_write4(sc, NGLE_REG_4, (r << 16) | (g << 8) | b);
1042
1043 gftfb_write4(sc, NGLE_REG_2, 0x400);
1044 gftfb_write4(sc, NGLE_REG_26, 0x80000100);
1045 gftfb_setup_fb(sc);
1046 mutex_exit(&sc->sc_hwlock);
1047 return 0;
1048 }
1049
1050 static inline void
1051 gftfb_wait_fifo(struct gftfb_softc *sc, uint32_t slots)
1052 {
1053 uint32_t reg;
1054
1055 do {
1056 reg = gftfb_read4(sc, NGLE_REG_34);
1057 } while (reg < slots);
1058 }
1059
1060 static void
1061 gftfb_rectfill(struct gftfb_softc *sc, int x, int y, int wi, int he,
1062 uint32_t bg)
1063 {
1064 uint32_t mask = 0xffffffff;
1065
1066 if (sc->sc_hwmode != HW_FILL) {
1067 gftfb_wait_fifo(sc, 3);
1068 /* plane mask */
1069 gftfb_write4(sc, NGLE_REG_13, 0xff);
1070 /* bitmap op */
1071 gftfb_write4(sc, NGLE_REG_14,
1072 IBOvals(RopSrc, 0, BitmapExtent08, 0, DataDynamic, MaskOtc, 1, 0));
1073 /* dst bitmap access */
1074 gftfb_write4(sc, NGLE_REG_11,
1075 BA(IndexedDcd, Otc32, OtsIndirect, AddrLong, 0, BINapp0I, 0));
1076 sc->sc_hwmode = HW_FILL;
1077 }
1078 gftfb_wait_fifo(sc, 4);
1079
1080 if (wi < 32)
1081 mask = 0xffffffff << (32 - wi);
1082 /* transfer data */
1083 gftfb_write4(sc, NGLE_REG_8, mask);
1084 gftfb_write4(sc, NGLE_REG_35, bg);
1085 /* dst XY */
1086 gftfb_write4(sc, NGLE_REG_6, (x << 16) | y);
1087 /* len XY start */
1088 gftfb_write4(sc, NGLE_REG_9, (wi << 16) | he);
1089
1090 }
1091
1092 static void
1093 gftfb_bitblt(void *cookie, int xs, int ys, int xd, int yd, int wi,
1094 int he, int rop)
1095 {
1096 struct gftfb_softc *sc = cookie;
1097
1098 if (sc->sc_hwmode != HW_BLIT) {
1099 gftfb_wait(sc);
1100 gftfb_write4(sc, NGLE_REG_10, 0x13a01000);
1101 sc->sc_hwmode = HW_BLIT;
1102 }
1103 gftfb_wait_fifo(sc, 5);
1104 gftfb_write4(sc, NGLE_REG_14, ((rop << 8) & 0xf00) | 0x23000000);
1105 gftfb_write4(sc, NGLE_REG_13, 0xff);
1106 gftfb_write4(sc, NGLE_REG_24, (xs << 16) | ys);
1107 gftfb_write4(sc, NGLE_REG_7, (wi << 16) | he);
1108 gftfb_write4(sc, NGLE_REG_25, (xd << 16) | yd);
1109 }
1110
1111 static void
1112 gftfb_nuke_cursor(struct rasops_info *ri)
1113 {
1114 struct vcons_screen *scr = ri->ri_hw;
1115 struct gftfb_softc *sc = scr->scr_cookie;
1116 int wi, he, x, y;
1117
1118 if (ri->ri_flg & RI_CURSOR) {
1119 wi = ri->ri_font->fontwidth;
1120 he = ri->ri_font->fontheight;
1121 x = ri->ri_ccol * wi + ri->ri_xorigin;
1122 y = ri->ri_crow * he + ri->ri_yorigin;
1123 gftfb_bitblt(sc, x, y, x, y, wi, he, RopInv);
1124 ri->ri_flg &= ~RI_CURSOR;
1125 }
1126 }
1127
1128 static void
1129 gftfb_cursor(void *cookie, int on, int row, int col)
1130 {
1131 struct rasops_info *ri = cookie;
1132 struct vcons_screen *scr = ri->ri_hw;
1133 struct gftfb_softc *sc = scr->scr_cookie;
1134 int x, y, wi, he;
1135
1136 wi = ri->ri_font->fontwidth;
1137 he = ri->ri_font->fontheight;
1138
1139 if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
1140 if (on) {
1141 if (ri->ri_flg & RI_CURSOR) {
1142 gftfb_nuke_cursor(ri);
1143 }
1144 x = col * wi + ri->ri_xorigin;
1145 y = row * he + ri->ri_yorigin;
1146 gftfb_bitblt(sc, x, y, x, y, wi, he, RopInv);
1147 ri->ri_flg |= RI_CURSOR;
1148 }
1149 ri->ri_crow = row;
1150 ri->ri_ccol = col;
1151 } else
1152 {
1153 ri->ri_crow = row;
1154 ri->ri_ccol = col;
1155 ri->ri_flg &= ~RI_CURSOR;
1156 }
1157
1158 }
1159
1160 static void
1161 gftfb_putchar(void *cookie, int row, int col, u_int c, long attr)
1162 {
1163 struct rasops_info *ri = cookie;
1164 struct wsdisplay_font *font = PICK_FONT(ri, c);
1165 struct vcons_screen *scr = ri->ri_hw;
1166 struct gftfb_softc *sc = scr->scr_cookie;
1167 void *data;
1168 int i, x, y, wi, he, rv = GC_NOPE;
1169 uint32_t bg, fg, mask;
1170
1171 if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL)
1172 return;
1173
1174 if (!CHAR_IN_FONT(c, font))
1175 return;
1176
1177 if (row == ri->ri_crow && col == ri->ri_ccol) {
1178 ri->ri_flg &= ~RI_CURSOR;
1179 }
1180
1181 wi = font->fontwidth;
1182 he = font->fontheight;
1183
1184 x = ri->ri_xorigin + col * wi;
1185 y = ri->ri_yorigin + row * he;
1186
1187 bg = ri->ri_devcmap[(attr >> 16) & 0xf];
1188
1189 /* if we're drawing a space we're done here */
1190 if (c == 0x20) {
1191 gftfb_rectfill(sc, x, y, wi, he, bg);
1192 return;
1193 }
1194
1195 fg = ri->ri_devcmap[(attr >> 24) & 0x0f];
1196
1197 rv = glyphcache_try(&sc->sc_gc, c, x, y, attr);
1198 if (rv == GC_OK)
1199 return;
1200
1201 /* clear the character cell */
1202 gftfb_rectfill(sc, x, y, wi, he, bg);
1203
1204 data = WSFONT_GLYPH(c, font);
1205
1206 /*
1207 * we're in rectangle mode with transparency enabled from the call to
1208 * gftfb_rectfill() above, so all we need to do is reset the starting
1209 * cordinates, then hammer mask and size/start. Starting coordinates
1210 * will automatically move down the y-axis
1211 */
1212 gftfb_wait_fifo(sc, 2);
1213
1214 /* character colour */
1215 gftfb_write4(sc, NGLE_REG_35, fg);
1216 /* dst XY */
1217 gftfb_write4(sc, NGLE_REG_6, (x << 16) | y);
1218
1219 if (ri->ri_font->stride == 1) {
1220 uint8_t *data8 = data;
1221 for (i = 0; i < he; i++) {
1222 gftfb_wait_fifo(sc, 2);
1223 mask = *data8;
1224 gftfb_write4(sc, NGLE_REG_8, mask << 24);
1225 gftfb_write4(sc, NGLE_REG_9, (wi << 16) | 1);
1226 data8++;
1227 }
1228 } else {
1229 uint16_t *data16 = data;
1230 for (i = 0; i < he; i++) {
1231 gftfb_wait_fifo(sc, 2);
1232 mask = *data16;
1233 gftfb_write4(sc, NGLE_REG_8, mask << 16);
1234 gftfb_write4(sc, NGLE_REG_9, (wi << 16) | 1);
1235 data16++;
1236 }
1237 }
1238
1239 if (rv == GC_ADD)
1240 glyphcache_add(&sc->sc_gc, c, x, y);
1241 }
1242
1243 static void
1244 gftfb_putchar_aa(void *cookie, int row, int col, u_int c, long attr)
1245 {
1246 struct rasops_info *ri = cookie;
1247 struct wsdisplay_font *font = PICK_FONT(ri, c);
1248 struct vcons_screen *scr = ri->ri_hw;
1249 struct gftfb_softc *sc = scr->scr_cookie;
1250 int x, y, wi, he, rv = GC_NOPE;
1251 uint32_t bg;
1252
1253 if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL)
1254 return;
1255
1256 if (!CHAR_IN_FONT(c, font))
1257 return;
1258
1259 if (row == ri->ri_crow && col == ri->ri_ccol) {
1260 ri->ri_flg &= ~RI_CURSOR;
1261 }
1262
1263 wi = font->fontwidth;
1264 he = font->fontheight;
1265
1266 x = ri->ri_xorigin + col * wi;
1267 y = ri->ri_yorigin + row * he;
1268
1269 bg = ri->ri_devcmap[(attr >> 16) & 0xf];
1270
1271 if (c == 0x20) {
1272 gftfb_rectfill(sc, x, y, wi, he, bg);
1273 return;
1274 }
1275
1276 rv = glyphcache_try(&sc->sc_gc, c, x, y, attr);
1277 if (rv == GC_OK)
1278 return;
1279
1280 if (sc->sc_hwmode != HW_FB) gftfb_setup_fb(sc);
1281 sc->sc_putchar(cookie, row, col, c, attr);
1282
1283 if (rv == GC_ADD)
1284 glyphcache_add(&sc->sc_gc, c, x, y);
1285 }
1286
1287 static void
1288 gftfb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
1289 {
1290 struct rasops_info *ri = cookie;
1291 struct vcons_screen *scr = ri->ri_hw;
1292 struct gftfb_softc *sc = scr->scr_cookie;
1293 int32_t xs, xd, y, width, height;
1294
1295 if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
1296 if (ri->ri_crow == row &&
1297 (ri->ri_ccol >= srccol && ri->ri_ccol < (srccol + ncols)) &&
1298 (ri->ri_flg & RI_CURSOR)) {
1299 gftfb_nuke_cursor(ri);
1300 }
1301
1302 xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
1303 xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
1304 y = ri->ri_yorigin + ri->ri_font->fontheight * row;
1305 width = ri->ri_font->fontwidth * ncols;
1306 height = ri->ri_font->fontheight;
1307 gftfb_bitblt(sc, xs, y, xd, y, width, height, RopSrc);
1308 if (ri->ri_crow == row &&
1309 (ri->ri_ccol >= dstcol && ri->ri_ccol < (dstcol + ncols)))
1310 ri->ri_flg &= ~RI_CURSOR;
1311 }
1312 }
1313
1314 static void
1315 gftfb_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
1316 {
1317 struct rasops_info *ri = cookie;
1318 struct vcons_screen *scr = ri->ri_hw;
1319 struct gftfb_softc *sc = scr->scr_cookie;
1320 int32_t x, y, width, height, fg, bg, ul;
1321
1322 if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
1323 x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
1324 y = ri->ri_yorigin + ri->ri_font->fontheight * row;
1325 width = ri->ri_font->fontwidth * ncols;
1326 height = ri->ri_font->fontheight;
1327 rasops_unpack_attr(fillattr, &fg, &bg, &ul);
1328
1329 gftfb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
1330 if (ri->ri_crow == row &&
1331 (ri->ri_ccol >= startcol && ri->ri_ccol < (startcol + ncols)))
1332 ri->ri_flg &= ~RI_CURSOR;
1333 }
1334 }
1335
1336 static void
1337 gftfb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
1338 {
1339 struct rasops_info *ri = cookie;
1340 struct vcons_screen *scr = ri->ri_hw;
1341 struct gftfb_softc *sc = scr->scr_cookie;
1342 int32_t x, ys, yd, width, height;
1343
1344 if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
1345 if ((ri->ri_crow >= srcrow && ri->ri_crow < (srcrow + nrows)) &&
1346 (ri->ri_flg & RI_CURSOR)) {
1347 gftfb_nuke_cursor(ri);
1348 }
1349 x = ri->ri_xorigin;
1350 ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
1351 yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
1352 width = ri->ri_emuwidth;
1353 height = ri->ri_font->fontheight * nrows;
1354 gftfb_bitblt(sc, x, ys, x, yd, width, height, RopSrc);
1355 if (ri->ri_crow >= dstrow && ri->ri_crow < (dstrow + nrows))
1356 ri->ri_flg &= ~RI_CURSOR;
1357 }
1358 }
1359
1360 static void
1361 gftfb_eraserows(void *cookie, int row, int nrows, long fillattr)
1362 {
1363 struct rasops_info *ri = cookie;
1364 struct vcons_screen *scr = ri->ri_hw;
1365 struct gftfb_softc *sc = scr->scr_cookie;
1366 int32_t x, y, width, height, fg, bg, ul;
1367
1368 if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
1369 x = ri->ri_xorigin;
1370 y = ri->ri_yorigin + ri->ri_font->fontheight * row;
1371 width = ri->ri_emuwidth;
1372 height = ri->ri_font->fontheight * nrows;
1373 rasops_unpack_attr(fillattr, &fg, &bg, &ul);
1374
1375 gftfb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
1376
1377 if (ri->ri_crow >= row && ri->ri_crow < (row + nrows))
1378 ri->ri_flg &= ~RI_CURSOR;
1379 }
1380 }
1381
1382 /*
1383 * cursor sprite handling
1384 * like most hw info, xf86 3.3 -> nglehdw.h was used as documentation
1385 * problem is, the PCI EG doesn't quite behave like an S9000_ID_ARTIST
1386 * the cursor position register bahaves like the one on HCRX while using
1387 * the same address as Artist, incuding the enable bit and weird handling
1388 * of negative coordinates. The rest of it, colour map, sprite image etc.,
1389 * behave like Artist.
1390 */
1391
1392 static void
1393 gftfb_move_cursor(struct gftfb_softc *sc, int x, int y)
1394 {
1395 uint32_t pos;
1396
1397 sc->sc_cursor_x = x;
1398 x -= sc->sc_hot_x;
1399 sc->sc_cursor_y = y;
1400 y -= sc->sc_hot_y;
1401
1402 if (x < 0) x = 0x1000 - x;
1403 if (y < 0) y = 0x1000 - y;
1404 pos = (x << 16) | y;
1405 if (sc->sc_enabled) pos |= 0x80000000;
1406 gftfb_wait(sc);
1407 gftfb_write4(sc, NGLE_REG_17, pos);
1408 gftfb_write4(sc, NGLE_REG_18, 0x80);
1409 }
1410
1411 static int
1412 gftfb_do_cursor(struct gftfb_softc *sc, struct wsdisplay_cursor *cur)
1413 {
1414 if (cur->which & WSDISPLAY_CURSOR_DOCUR) {
1415
1416 sc->sc_enabled = cur->enable;
1417 cur->which |= WSDISPLAY_CURSOR_DOPOS;
1418 }
1419 if (cur->which & WSDISPLAY_CURSOR_DOHOT) {
1420
1421 sc->sc_hot_x = cur->hot.x;
1422 sc->sc_hot_y = cur->hot.y;
1423 cur->which |= WSDISPLAY_CURSOR_DOPOS;
1424 }
1425 if (cur->which & WSDISPLAY_CURSOR_DOPOS) {
1426
1427 gftfb_move_cursor(sc, cur->pos.x, cur->pos.y);
1428 }
1429 if (cur->which & WSDISPLAY_CURSOR_DOCMAP) {
1430 uint32_t rgb;
1431 uint8_t r[2], g[2], b[2];
1432
1433 copyin(cur->cmap.blue, b, 2);
1434 copyin(cur->cmap.green, g, 2);
1435 copyin(cur->cmap.red, r, 2);
1436 mutex_enter(&sc->sc_hwlock);
1437 gftfb_wait(sc);
1438 gftfb_write4(sc, NGLE_REG_10, 0xBBE0F000);
1439 gftfb_write4(sc, NGLE_REG_14, 0x03000300);
1440 gftfb_write4(sc, NGLE_REG_13, 0xffffffff);
1441 gftfb_wait(sc);
1442 gftfb_write4(sc, NGLE_REG_3, 0);
1443 gftfb_write4(sc, NGLE_REG_4, 0);
1444 gftfb_write4(sc, NGLE_REG_4, 0);
1445 rgb = (r[0] << 16) | (g[0] << 8) | b[0];
1446 gftfb_write4(sc, NGLE_REG_4, rgb); /* BG */
1447 rgb = (r[1] << 16) | (g[1] << 8) | b[1];
1448 gftfb_write4(sc, NGLE_REG_4, rgb); /* FG */
1449 gftfb_write4(sc, NGLE_REG_2, 0);
1450 gftfb_write4(sc, NGLE_REG_26, 0x80008004);
1451 gftfb_setup_fb(sc);
1452 mutex_exit(&sc->sc_hwlock);
1453
1454 }
1455 if (cur->which & WSDISPLAY_CURSOR_DOSHAPE) {
1456 uint32_t buffer[128], latch, tmp;
1457 int i;
1458
1459 copyin(cur->mask, buffer, 512);
1460 gftfb_wait(sc);
1461 gftfb_write4(sc, NGLE_REG_14, 0x300);
1462 gftfb_write4(sc, NGLE_REG_13, 0xffffffff);
1463 gftfb_write4(sc, NGLE_REG_11, 0x28A07000);
1464 gftfb_write4(sc, NGLE_REG_3, 0);
1465 for (i = 0; i < 128; i += 2) {
1466 latch = 0;
1467 tmp = buffer[i] & 0x80808080;
1468 latch |= tmp >> 7;
1469 tmp = buffer[i] & 0x40404040;
1470 latch |= tmp >> 5;
1471 tmp = buffer[i] & 0x20202020;
1472 latch |= tmp >> 3;
1473 tmp = buffer[i] & 0x10101010;
1474 latch |= tmp >> 1;
1475 tmp = buffer[i] & 0x08080808;
1476 latch |= tmp << 1;
1477 tmp = buffer[i] & 0x04040404;
1478 latch |= tmp << 3;
1479 tmp = buffer[i] & 0x02020202;
1480 latch |= tmp << 5;
1481 tmp = buffer[i] & 0x01010101;
1482 latch |= tmp << 7;
1483 gftfb_write4(sc, NGLE_REG_4, latch);
1484 latch = 0;
1485 tmp = buffer[i + 1] & 0x80808080;
1486 latch |= tmp >> 7;
1487 tmp = buffer[i + 1] & 0x40404040;
1488 latch |= tmp >> 5;
1489 tmp = buffer[i + 1] & 0x20202020;
1490 latch |= tmp >> 3;
1491 tmp = buffer[i + 1] & 0x10101010;
1492 latch |= tmp >> 1;
1493 tmp = buffer[i + 1] & 0x08080808;
1494 latch |= tmp << 1;
1495 tmp = buffer[i + 1] & 0x04040404;
1496 latch |= tmp << 3;
1497 tmp = buffer[i + 1] & 0x02020202;
1498 latch |= tmp << 5;
1499 tmp = buffer[i + 1] & 0x01010101;
1500 latch |= tmp << 7;
1501 gftfb_write4(sc, NGLE_REG_5, latch);
1502 }
1503
1504 copyin(cur->image, buffer, 512);
1505 gftfb_wait(sc);
1506 gftfb_write4(sc, NGLE_REG_14, 0x300);
1507 gftfb_write4(sc, NGLE_REG_13, 0xffffffff);
1508 gftfb_write4(sc, NGLE_REG_11, 0x28A06000);
1509 gftfb_write4(sc, NGLE_REG_3, 0);
1510 for (i = 0; i < 128; i += 2) {
1511 latch = 0;
1512 tmp = buffer[i] & 0x80808080;
1513 latch |= tmp >> 7;
1514 tmp = buffer[i] & 0x40404040;
1515 latch |= tmp >> 5;
1516 tmp = buffer[i] & 0x20202020;
1517 latch |= tmp >> 3;
1518 tmp = buffer[i] & 0x10101010;
1519 latch |= tmp >> 1;
1520 tmp = buffer[i] & 0x08080808;
1521 latch |= tmp << 1;
1522 tmp = buffer[i] & 0x04040404;
1523 latch |= tmp << 3;
1524 tmp = buffer[i] & 0x02020202;
1525 latch |= tmp << 5;
1526 tmp = buffer[i] & 0x01010101;
1527 latch |= tmp << 7;
1528 gftfb_write4(sc, NGLE_REG_4, latch);
1529 latch = 0;
1530 tmp = buffer[i + 1] & 0x80808080;
1531 latch |= tmp >> 7;
1532 tmp = buffer[i + 1] & 0x40404040;
1533 latch |= tmp >> 5;
1534 tmp = buffer[i + 1] & 0x20202020;
1535 latch |= tmp >> 3;
1536 tmp = buffer[i + 1] & 0x10101010;
1537 latch |= tmp >> 1;
1538 tmp = buffer[i + 1] & 0x08080808;
1539 latch |= tmp << 1;
1540 tmp = buffer[i + 1] & 0x04040404;
1541 latch |= tmp << 3;
1542 tmp = buffer[i + 1] & 0x02020202;
1543 latch |= tmp << 5;
1544 tmp = buffer[i + 1] & 0x01010101;
1545 latch |= tmp << 7;
1546 gftfb_write4(sc, NGLE_REG_5, latch);
1547 }
1548 gftfb_setup_fb(sc);
1549 }
1550
1551 return 0;
1552 }
1553
1554 static void
1555 gftfb_set_video(struct gftfb_softc *sc, int on)
1556 {
1557 if (sc->sc_video_on == on)
1558 return;
1559
1560 sc->sc_video_on = on;
1561
1562 gftfb_wait(sc);
1563 if (on) {
1564 gftfb_write4(sc, NGLE_REG_21,
1565 gftfb_read4(sc, NGLE_REG_21) | 0x0a000000);
1566 gftfb_write4(sc, NGLE_REG_27,
1567 gftfb_read4(sc, NGLE_REG_27) | 0x00800000);
1568 } else {
1569 gftfb_write4(sc, NGLE_REG_21,
1570 gftfb_read4(sc, NGLE_REG_21) & ~0x0a000000);
1571 gftfb_write4(sc, NGLE_REG_27,
1572 gftfb_read4(sc, NGLE_REG_27) & ~0x00800000);
1573 }
1574 }
1575