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