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