summitfb.c revision 1.26 1 /* $NetBSD: summitfb.c,v 1.26 2025/01/03 13:18:30 skrll 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 FX graphics cards, so far tested only on
25 * my FX4
26 * STI portions are from Miodrag Vallat's sti_pci.c
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: summitfb.c,v 1.26 2025/01/03 13:18:30 skrll Exp $");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kmem.h>
35 #include <sys/device.h>
36 #include <sys/mutex.h>
37
38 #include <dev/pci/pcivar.h>
39 #include <dev/pci/pcireg.h>
40 #include <dev/pci/pcidevs.h>
41 #include <dev/pci/pciio.h>
42
43 #include <dev/wscons/wsdisplayvar.h>
44 #include <dev/wscons/wsconsio.h>
45 #include <dev/wsfont/wsfont.h>
46 #include <dev/rasops/rasops.h>
47 #include <dev/wscons/wsdisplay_vconsvar.h>
48 #include <dev/pci/wsdisplay_pci.h>
49 #include <dev/wscons/wsdisplay_glyphcachevar.h>
50
51 #include <dev/ic/stireg.h>
52 #include <dev/ic/summitreg.h>
53 #include <dev/ic/stivar.h>
54
55 #include "opt_summitfb.h"
56
57 #ifdef SUMMITFB_DEBUG
58 #define DPRINTF(s) printf s
59 #else
60 #define DPRINTF(s) __nothing
61 #endif
62
63 //#define SUMMITFB_ENABLE_GC
64
65 int summitfb_match(device_t, cfdata_t, void *);
66 void summitfb_attach(device_t, device_t, void *);
67
68 struct summitfb_softc {
69 device_t sc_dev;
70 pci_chipset_tag_t sc_pc;
71 pcitag_t sc_tag;
72
73 /* stuff we need in order to use the STI ROM */
74 struct sti_softc sc_base;
75 struct sti_screen sc_scr;
76 bus_space_handle_t sc_romh;
77
78 int sc_width, sc_height;
79 int sc_locked;
80 struct vcons_screen sc_console_screen;
81 struct wsscreen_descr sc_defaultscreen_descr;
82 const struct wsscreen_descr *sc_screens[1];
83 struct wsscreen_list sc_screenlist;
84 struct vcons_data vd;
85 int sc_mode;
86 u_char sc_cmap_red[256];
87 u_char sc_cmap_green[256];
88 u_char sc_cmap_blue[256];
89 uint32_t sc_write_mode;
90 /* cursor stuff */
91 int sc_cursor_x, sc_cursor_y;
92 int sc_hot_x, sc_hot_y, sc_enabled;
93 /* font-in-vram */
94 struct wsdisplay_font *sc_font;
95 int sc_font_start; /* x of font area */
96 int sc_cols; /* chars per line in font area */
97
98 int sc_video_on;
99 #ifdef SUMMITFB_ENABLE_GC
100 void (*sc_putchar)(void *, int, int, u_int, long);
101 glyphcache sc_gc;
102 #endif
103 };
104
105 CFATTACH_DECL_NEW(summitfb, sizeof(struct summitfb_softc),
106 summitfb_match, summitfb_attach, NULL, NULL);
107
108 int summitfb_readbar(struct sti_softc *, struct pci_attach_args *, u_int,
109 int);
110 int summitfb_check_rom(struct summitfb_softc *, struct pci_attach_args *);
111 void summitfb_enable_rom(struct sti_softc *);
112 void summitfb_disable_rom(struct sti_softc *);
113 void summitfb_enable_rom_internal(struct summitfb_softc *);
114 void summitfb_disable_rom_internal(struct summitfb_softc *);
115
116 void summitfb_setup(struct summitfb_softc *);
117
118 /* XXX these really need to go into their own header */
119 int sti_pci_is_console(struct pci_attach_args *, bus_addr_t *);
120 int sti_rom_setup(struct sti_rom *, bus_space_tag_t, bus_space_tag_t,
121 bus_space_handle_t, bus_addr_t *, u_int);
122 int sti_screen_setup(struct sti_screen *, int);
123 void sti_describe_screen(struct sti_softc *, struct sti_screen *);
124
125 #define PCI_ROM_SIZE(mr) \
126 (PCI_MAPREG_ROM_ADDR(mr) & -PCI_MAPREG_ROM_ADDR(mr))
127
128 /* wsdisplay stuff */
129 static int summitfb_ioctl(void *, void *, u_long, void *, int,
130 struct lwp *);
131 static paddr_t summitfb_mmap(void *, void *, off_t, int);
132 static void summitfb_init_screen(void *, struct vcons_screen *, int,
133 long *);
134
135 static int summitfb_putcmap(struct summitfb_softc *,
136 struct wsdisplay_cmap *);
137 static int summitfb_getcmap(struct summitfb_softc *,
138 struct wsdisplay_cmap *);
139 static void summitfb_restore_palette(struct summitfb_softc *);
140 static int summitfb_putpalreg(struct summitfb_softc *, uint8_t, uint8_t,
141 uint8_t, uint8_t);
142
143 static inline void summitfb_setup_fb(struct summitfb_softc *);
144
145 static void summitfb_rectfill(struct summitfb_softc *, int, int, int, int,
146 uint32_t);
147 static void summitfb_bitblt(void *, int, int, int, int, int,
148 int, int);
149
150 static void summitfb_cursor(void *, int, int, int);
151 static void summitfb_putchar(void *, int, int, u_int, long);
152 static void summitfb_putchar_fast(void *, int, int, u_int, long);
153 static void summitfb_loadfont(struct summitfb_softc *);
154 #ifdef SUMMITFB_ENABLE_GC
155 static void summitfb_putchar_aa(void *, int, int, u_int, long);
156 #endif
157 static void summitfb_copycols(void *, int, int, int, int);
158 static void summitfb_erasecols(void *, int, int, int, long);
159 static void summitfb_copyrows(void *, int, int, int);
160 static void summitfb_eraserows(void *, int, int, long);
161
162 static void summitfb_move_cursor(struct summitfb_softc *, int, int);
163 static int summitfb_do_cursor(struct summitfb_softc *,
164 struct wsdisplay_cursor *);
165
166 static void summitfb_set_video(struct summitfb_softc *, int);
167
168 static void summitfb_copyfont(struct summitfb_softc *);
169
170 struct wsdisplay_accessops summitfb_accessops = {
171 .ioctl = summitfb_ioctl,
172 .mmap = summitfb_mmap,
173 .alloc_screen = NULL,
174 .free_screen = NULL,
175 .show_screen = NULL,
176 .load_font = NULL,
177 .pollc = NULL,
178 .scroll = NULL,
179 };
180
181 static inline void summitfb_wait_fifo(struct summitfb_softc *, uint32_t);
182 static inline void summitfb_wait(struct summitfb_softc *);
183
184 int sti_fetchfonts(struct sti_screen *, struct sti_inqconfout *, uint32_t,
185 u_int);
186
187 int
188 summitfb_match(device_t parent, cfdata_t cf, void *aux)
189 {
190 struct pci_attach_args *paa = aux;
191
192 if (PCI_VENDOR(paa->pa_id) != PCI_VENDOR_HP)
193 return 0;
194
195 if (PCI_PRODUCT(paa->pa_id) == PCI_PRODUCT_HP_VISUALIZE_FX4)
196 return 10; /* beat out sti at pci */
197
198 return 0;
199 }
200
201 static inline uint32_t
202 summitfb_read4(struct summitfb_softc *sc, uint32_t offset)
203 {
204 struct sti_rom *rom = sc->sc_base.sc_rom;
205 bus_space_tag_t memt = rom->memt;
206 bus_space_handle_t memh = rom->regh[2];
207
208 return bus_space_read_stream_4(memt, memh, offset - 0x400000);
209 }
210
211 static inline void
212 summitfb_write4(struct summitfb_softc *sc, uint32_t offset, uint32_t val)
213 {
214 struct sti_rom *rom = sc->sc_base.sc_rom;
215 bus_space_tag_t memt = rom->memt;
216 bus_space_handle_t memh = rom->regh[2];
217
218 bus_space_write_stream_4(memt, memh, offset - 0x400000, val);
219 }
220
221 void
222 summitfb_attach(device_t parent, device_t self, void *aux)
223 {
224 struct summitfb_softc *sc = device_private(self);
225 struct pci_attach_args *paa = aux;
226 struct sti_rom *rom;
227 struct rasops_info *ri;
228 struct wsemuldisplaydev_attach_args aa;
229 struct sti_dd *dd;
230 unsigned long defattr = 0;
231 int ret, is_console = 0;
232
233 sc->sc_dev = self;
234
235 sc->sc_pc = paa->pa_pc;
236 sc->sc_tag = paa->pa_tag;
237 sc->sc_base.sc_dev = self;
238 sc->sc_base.sc_enable_rom = summitfb_enable_rom;
239 sc->sc_base.sc_disable_rom = summitfb_disable_rom;
240
241 aprint_normal("\n");
242
243 if (summitfb_check_rom(sc, paa) != 0)
244 return;
245
246 ret = sti_pci_is_console(paa, sc->sc_base. bases);
247 if (ret != 0) {
248 sc->sc_base.sc_flags |= STI_CONSOLE;
249 is_console = 1;
250 }
251 rom = kmem_zalloc(sizeof(*rom), KM_SLEEP);
252 rom->rom_softc = &sc->sc_base;
253 ret = sti_rom_setup(rom, paa->pa_iot, paa->pa_memt, sc->sc_romh,
254 sc->sc_base.bases, STI_CODEBASE_MAIN);
255 if (ret != 0) {
256 kmem_free(rom, sizeof(*rom));
257 return;
258 }
259
260 sc->sc_base.sc_rom = rom;
261 dd = &rom->rom_dd;
262
263 sc->sc_scr.scr_rom = sc->sc_base.sc_rom;
264 ret = sti_screen_setup(&sc->sc_scr, STI_FBMODE);
265
266 sti_fetchfonts(&sc->sc_scr, NULL, dd->dd_fntaddr, 0);
267 wsfont_init();
268 summitfb_copyfont(sc);
269
270 sc->sc_width = sc->sc_scr.scr_cfg.scr_width;
271 sc->sc_height = sc->sc_scr.scr_cfg.scr_height;
272 sc->sc_write_mode = 0xffffffff;
273
274 #ifdef SUMMITFB_DEBUG
275 sc->sc_height -= 200;
276 #endif
277
278 sc->sc_defaultscreen_descr = (struct wsscreen_descr){
279 .name = "default",
280 .ncols = 0, .nrows = 0,
281 .textops = NULL,
282 .fontwidth = 8, .fontheight = 16,
283 .capabilities = WSSCREEN_WSCOLORS | WSSCREEN_HILIT |
284 WSSCREEN_UNDERLINE | WSSCREEN_RESIZE,
285 .modecookie = NULL,
286 };
287
288 sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
289 sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
290 sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
291 sc->sc_locked = 0;
292
293 vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr,
294 &summitfb_accessops);
295 sc->vd.init_screen = summitfb_init_screen;
296 #ifdef SUMMITFB_ENABLE_GC
297 sc->vd.show_screen_cookie = &sc->sc_gc;
298 sc->vd.show_screen_cb = glyphcache_adapt;
299 #endif
300 ri = &sc->sc_console_screen.scr_ri;
301
302 #ifdef SUMMITFB_ENABLE_GC
303 sc->sc_gc.gc_bitblt = summitfb_bitblt;
304 sc->sc_gc.gc_blitcookie = sc;
305 sc->sc_gc.gc_rop = RopSrc;
306 #endif
307
308 summitfb_setup(sc);
309
310 vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1, &defattr);
311 sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
312
313 sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
314 sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
315 sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
316 sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
317
318 /*
319 * STI lies to us - it reports a 2048x2048 framebuffer but blitter
320 * ops wrap around below 1024 and we seem to have only about 250
321 * usable columns to the right. Should still be enough to cache
322 * a font or four.
323 * So, the framebuffer seems to be 1536x1024, which is odd since the
324 * FX4 is supposed to support resolutions higher than 1280x1024.
325 * I guess video memory is allocated in 512x512 chunks
326 */
327 #ifdef SUMMITFB_ENABLE_GC
328 glyphcache_init_x(&sc->sc_gc, sc->sc_width, 0,
329 sc->sc_height,
330 1536 - sc->sc_width - 4,
331 ri->ri_font->fontwidth,
332 ri->ri_font->fontheight,
333 defattr);
334 #endif
335
336 summitfb_restore_palette(sc);
337 summitfb_rectfill(sc, 0, 0, sc->sc_width, sc->sc_height,
338 ri->ri_devcmap[(defattr >> 16) & 0xff]);
339 summitfb_setup_fb(sc);
340
341 if (is_console) {
342 wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0,
343 defattr);
344
345 vcons_replay_msgbuf(&sc->sc_console_screen);
346 }
347
348 aprint_normal_dev(sc->sc_dev, "%s at %dx%d\n", sc->sc_scr.name,
349 sc->sc_width, sc->sc_height);
350
351 /* no suspend/resume support yet */
352 pmf_device_register(sc->sc_dev, NULL, NULL);
353
354 aa.console = is_console;
355 aa.scrdata = &sc->sc_screenlist;
356 aa.accessops = &summitfb_accessops;
357 aa.accesscookie = &sc->vd;
358
359 config_found(sc->sc_dev, &aa, wsemuldisplaydevprint, CFARGS_NONE);
360 }
361
362 /*
363 * Grovel the STI ROM image.
364 */
365 int
366 summitfb_check_rom(struct summitfb_softc *spc, struct pci_attach_args *pa)
367 {
368 struct sti_softc *sc = &spc->sc_base;
369 pcireg_t address, mask;
370 bus_space_handle_t romh;
371 bus_size_t romsize, subsize, stiromsize;
372 bus_addr_t selected, offs, suboffs;
373 uint32_t tmp;
374 int i;
375 int rc;
376
377 /* sort of inline sti_pci_enable_rom(sc) */
378 address = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_MAPREG_ROM);
379 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_MAPREG_ROM,
380 ~PCI_MAPREG_ROM_ENABLE);
381 mask = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_MAPREG_ROM);
382 address |= PCI_MAPREG_ROM_ENABLE;
383 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_MAPREG_ROM, address);
384 sc->sc_flags |= STI_ROM_ENABLED;
385
386 /*
387 * Map the complete ROM for now.
388 */
389 romsize = PCI_ROM_SIZE(mask);
390 DPRINTF(("%s: mapping rom @ %lx for %lx\n", __func__,
391 (long)PCI_MAPREG_ROM_ADDR(address), (long)romsize));
392
393 rc = bus_space_map(pa->pa_memt, PCI_MAPREG_ROM_ADDR(address), romsize,
394 0, &romh);
395 if (rc != 0) {
396 aprint_error_dev(sc->sc_dev, "can't map PCI ROM (%d)\n", rc);
397 goto fail2;
398 }
399
400 summitfb_disable_rom_internal(spc);
401
402 /*
403 * Iterate over the ROM images, pick the best candidate.
404 */
405 selected = (bus_addr_t)-1;
406 for (offs = 0; offs < romsize; offs += subsize) {
407 summitfb_enable_rom_internal(spc);
408 /*
409 * Check for a valid ROM header.
410 */
411 tmp = bus_space_read_4(pa->pa_memt, romh, offs + 0);
412 tmp = le32toh(tmp);
413 if (tmp != 0x55aa0000) {
414 summitfb_disable_rom_internal(spc);
415 if (offs == 0) {
416 aprint_error_dev(sc->sc_dev,
417 "invalid PCI ROM header signature"
418 " (%08x)\n", tmp);
419 rc = EINVAL;
420 }
421 break;
422 }
423
424 /*
425 * Check ROM type.
426 */
427 tmp = bus_space_read_4(pa->pa_memt, romh, offs + 4);
428 tmp = le32toh(tmp);
429 if (tmp != 0x00000001) { /* 1 == STI ROM */
430 summitfb_disable_rom_internal(spc);
431 if (offs == 0) {
432 aprint_error_dev(sc->sc_dev,
433 "invalid PCI ROM type (%08x)\n", tmp);
434 rc = EINVAL;
435 }
436 break;
437 }
438
439 subsize = (bus_addr_t)bus_space_read_2(pa->pa_memt, romh,
440 offs + 0x0c);
441 subsize <<= 9;
442
443 #ifdef SUMMITFB_DEBUG
444 summitfb_disable_rom_internal(spc);
445 DPRINTF(("ROM offset %08x size %08x type %08x",
446 (u_int)offs, (u_int)subsize, tmp));
447 summitfb_enable_rom_internal(spc);
448 #endif
449
450 /*
451 * Check for a valid ROM data structure.
452 * We do not need it except to know what architecture the ROM
453 * code is for.
454 */
455
456 suboffs = offs + bus_space_read_2(pa->pa_memt, romh,
457 offs + 0x18);
458 tmp = bus_space_read_4(pa->pa_memt, romh, suboffs + 0);
459 tmp = le32toh(tmp);
460 if (tmp != 0x50434952) { /* PCIR */
461 summitfb_disable_rom_internal(spc);
462 if (offs == 0) {
463 aprint_error_dev(sc->sc_dev, "invalid PCI data"
464 " signature (%08x)\n", tmp);
465 rc = EINVAL;
466 } else {
467 DPRINTF((" invalid PCI data signature %08x\n",
468 tmp));
469 continue;
470 }
471 }
472
473 tmp = bus_space_read_1(pa->pa_memt, romh, suboffs + 0x14);
474 summitfb_disable_rom_internal(spc);
475 DPRINTF((" code %02x", tmp));
476
477 switch (tmp) {
478 #ifdef __hppa__
479 case 0x10:
480 if (selected == (bus_addr_t)-1)
481 selected = offs;
482 break;
483 #endif
484 #ifdef __i386__
485 case 0x00:
486 if (selected == (bus_addr_t)-1)
487 selected = offs;
488 break;
489 #endif
490 default:
491 DPRINTF((" (wrong architecture)"));
492 break;
493 }
494 DPRINTF(("%s\n", selected == offs ? " -> SELECTED" : ""));
495 }
496
497 if (selected == (bus_addr_t)-1) {
498 if (rc == 0) {
499 aprint_error_dev(sc->sc_dev, "found no ROM with "
500 "correct microcode architecture\n");
501 rc = ENOEXEC;
502 }
503 goto fail;
504 }
505
506 /*
507 * Read the STI region BAR assignments.
508 */
509
510 summitfb_enable_rom_internal(spc);
511 offs = selected + bus_space_read_2(pa->pa_memt, romh, selected + 0x0e);
512 for (i = 0; i < STI_REGION_MAX; i++) {
513 rc = summitfb_readbar(sc, pa, i,
514 bus_space_read_1(pa->pa_memt, romh, offs + i));
515 if (rc != 0)
516 goto fail;
517 }
518
519 /*
520 * Find out where the STI ROM itself lies, and its size.
521 */
522
523 offs = selected +
524 bus_space_read_4(pa->pa_memt, romh, selected + 0x08);
525 stiromsize = bus_space_read_4(pa->pa_memt, romh, offs + 0x18);
526 stiromsize = le32toh(stiromsize);
527 summitfb_disable_rom_internal(spc);
528
529 /*
530 * Replace our mapping with a smaller mapping of only the area
531 * we are interested in.
532 */
533
534 DPRINTF(("remapping rom @ %lx for %lx\n",
535 (long)(PCI_MAPREG_ROM_ADDR(address) + offs), (long)stiromsize));
536 bus_space_unmap(pa->pa_memt, romh, romsize);
537 rc = bus_space_map(pa->pa_memt, PCI_MAPREG_ROM_ADDR(address) + offs,
538 stiromsize, 0, &spc->sc_romh);
539 if (rc != 0) {
540 aprint_error_dev(sc->sc_dev, "can't map STI ROM (%d)\n",
541 rc);
542 goto fail2;
543 }
544 summitfb_disable_rom_internal(spc);
545 sc->sc_flags &= ~STI_ROM_ENABLED;
546
547 return 0;
548
549 fail:
550 bus_space_unmap(pa->pa_memt, romh, romsize);
551 fail2:
552 summitfb_disable_rom_internal(spc);
553
554 return rc;
555 }
556
557 /*
558 * Decode a BAR register.
559 */
560 int
561 summitfb_readbar(struct sti_softc *sc, struct pci_attach_args *pa,
562 u_int region, int bar)
563 {
564 bus_addr_t addr;
565 bus_size_t size;
566 uint32_t cf;
567 int rc;
568
569 if (bar == 0) {
570 sc->bases[region] = 0;
571 return 0;
572 }
573
574 #ifdef DIAGNOSTIC
575 if (bar < PCI_MAPREG_START || bar > PCI_MAPREG_PPB_END) {
576 summitfb_disable_rom(sc);
577 printf("%s: unexpected bar %02x for region %d\n",
578 device_xname(sc->sc_dev), bar, region);
579 summitfb_enable_rom(sc);
580 }
581 #endif
582
583 cf = pci_conf_read(pa->pa_pc, pa->pa_tag, bar);
584
585 rc = pci_mapreg_info(pa->pa_pc, pa->pa_tag, bar, PCI_MAPREG_TYPE(cf),
586 &addr, &size, NULL);
587
588 if (rc != 0) {
589 summitfb_disable_rom(sc);
590 aprint_error_dev(sc->sc_dev, "invalid bar %02x"
591 " for region %d\n",
592 bar, region);
593 summitfb_enable_rom(sc);
594 return rc;
595 }
596
597 sc->bases[region] = addr;
598 return 0;
599 }
600
601 /*
602 * Enable PCI ROM.
603 */
604 void
605 summitfb_enable_rom_internal(struct summitfb_softc *spc)
606 {
607 pcireg_t address;
608
609 KASSERT(spc != NULL);
610
611 address = pci_conf_read(spc->sc_pc, spc->sc_tag, PCI_MAPREG_ROM);
612 address |= PCI_MAPREG_ROM_ENABLE;
613 pci_conf_write(spc->sc_pc, spc->sc_tag, PCI_MAPREG_ROM, address);
614 }
615
616 void
617 summitfb_enable_rom(struct sti_softc *sc)
618 {
619 struct summitfb_softc *spc = device_private(sc->sc_dev);
620
621 if (!ISSET(sc->sc_flags, STI_ROM_ENABLED)) {
622 summitfb_enable_rom_internal(spc);
623 }
624 SET(sc->sc_flags, STI_ROM_ENABLED);
625 }
626
627 /*
628 * Disable PCI ROM.
629 */
630 void
631 summitfb_disable_rom_internal(struct summitfb_softc *spc)
632 {
633 pcireg_t address;
634
635 KASSERT(spc != NULL);
636
637 address = pci_conf_read(spc->sc_pc, spc->sc_tag, PCI_MAPREG_ROM);
638 address &= ~PCI_MAPREG_ROM_ENABLE;
639 pci_conf_write(spc->sc_pc, spc->sc_tag, PCI_MAPREG_ROM, address);
640 }
641
642 void
643 summitfb_disable_rom(struct sti_softc *sc)
644 {
645 struct summitfb_softc *spc = device_private(sc->sc_dev);
646
647 if (ISSET(sc->sc_flags, STI_ROM_ENABLED)) {
648 summitfb_disable_rom_internal(spc);
649 }
650 CLR(sc->sc_flags, STI_ROM_ENABLED);
651 }
652
653 static inline void
654 summitfb_wait(struct summitfb_softc *sc)
655 {
656
657 while (summitfb_read4(sc, VISFX_STATUS) != 0)
658 continue;
659 }
660
661 static inline void
662 summitfb_write_mode(struct summitfb_softc *sc, uint32_t mode)
663 {
664 if (sc->sc_write_mode == mode)
665 return;
666 summitfb_wait(sc);
667 summitfb_write4(sc, VISFX_VRAM_WRITE_MODE, mode);
668 summitfb_write4(sc, VISFX_VRAM_READ_MODE,mode & 0x07fff000);
669 sc->sc_write_mode = mode;
670 }
671
672 static inline void
673 summitfb_setup_fb(struct summitfb_softc *sc)
674 {
675
676 summitfb_wait(sc);
677 if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
678 summitfb_write_mode(sc, VISFX_WRITE_MODE_PLAIN);
679 summitfb_write4(sc, VISFX_APERTURE_ACCESS, VISFX_DEPTH_8);
680 summitfb_write4(sc, VISFX_OTR, OTR_T | OTR_L1 | OTR_L0); // opaque
681 } else {
682 summitfb_write_mode(sc, OTC01 | BIN8F | BUFFL);
683 summitfb_write4(sc, VISFX_APERTURE_ACCESS, VISFX_DEPTH_32);
684 summitfb_write4(sc, VISFX_OTR, OTR_A); // all transparent
685 }
686 summitfb_write4(sc, VISFX_IBO, RopSrc);
687 //summitfb_write4(sc, VISFX_CONTROL, CONTROL_WFC);
688 }
689
690 void
691 summitfb_setup(struct summitfb_softc *sc)
692 {
693 int i;
694
695 sc->sc_hot_x = 0;
696 sc->sc_hot_y = 0;
697 sc->sc_enabled = 0;
698 sc->sc_video_on = 1;
699
700 summitfb_wait(sc);
701 #if 1
702 /* these control byte swapping */
703 summitfb_write4(sc, 0xb08044, 0x1b); /* MFU_BSCTD */
704 summitfb_write4(sc, 0xb08048, 0x1b); /* MFU_BSCCTL */
705
706 summitfb_write4(sc, 0x920860, 0xe4); /* FBC_RBS */
707 summitfb_write4(sc, 0x921114, 0); /* CPE, ckip plane enable */
708 summitfb_write4(sc, 0x9211d8, 0); /* FCDA */
709
710 summitfb_write4(sc, 0xa00818, 0); /* WORG window origin */
711 summitfb_write4(sc, 0xa0081c, 0); /* FBS front buffer select*/
712 summitfb_write4(sc, 0xa00850, 0); /* MISC_CTL */
713 summitfb_write4(sc, 0xa0086c, 0); /* WCE window clipping enable */
714 #endif
715 /* initialize drawiing engine */
716 summitfb_wait(sc);
717 summitfb_write4(sc, VISFX_CONTROL, 0); // clear WFC
718 summitfb_write4(sc, VISFX_APERTURE_ACCESS, VISFX_DEPTH_8);
719 summitfb_write4(sc, VISFX_PIXEL_MASK, 0xffffffff);
720 summitfb_write4(sc, VISFX_PLANE_MASK, 0xffffffff);
721 summitfb_write4(sc, VISFX_FOE, FOE_BLEND_ROP);
722 summitfb_write4(sc, VISFX_IBO, RopSrc);
723 summitfb_write_mode(sc, VISFX_WRITE_MODE_PLAIN);
724 summitfb_write4(sc, VISFX_CLIP_TL, 0);
725 summitfb_write4(sc, VISFX_CLIP_WH,
726 ((sc->sc_scr.fbwidth) << 16) | (sc->sc_scr.fbheight));
727 /* turn off the cursor sprite */
728 summitfb_write4(sc, VISFX_CURSOR_POS, 0);
729 summitfb_write4(sc, VISFX_TCR, 0x10001000); /* disable throttling */
730
731 /* make sure the overlay is opaque */
732 summitfb_write4(sc, VISFX_OTR, OTR_T | OTR_L1 | OTR_L0);
733
734 /*
735 * initialize XLUT
736 */
737 for (i = 0; i < 16; i++)
738 summitfb_write4(sc, VISFX_IAA(i), IAA_8F | IAA_CFS1); /* RGB, CFS1 */
739 summitfb_write4(sc, VISFX_CFS(1), CFS_8F | CFS_BYPASS);
740 /* overlay is 8bit, uses LUT 0 */
741 summitfb_write4(sc, VISFX_CFS(16), CFS_8I | CFS_LUT0);
742 summitfb_write4(sc, VISFX_CFS(17), CFS_8I | CFS_LUT0);
743
744 /* zero the attribute plane */
745 summitfb_write_mode(sc, OTC04 | BINapln);
746 summitfb_wait_fifo(sc, 4);
747 summitfb_write4(sc, VISFX_PLANE_MASK, 0xff);
748 summitfb_write4(sc, VISFX_IBO, 0);
749 summitfb_write4(sc, VISFX_START, 0);
750 summitfb_write4(sc, VISFX_SIZE, (sc->sc_width << 16) | sc->sc_height);
751 summitfb_wait(sc);
752 summitfb_write4(sc, VISFX_PLANE_MASK, 0xffffffff);
753
754 /* turn off force attr so the above takes effect */
755 summitfb_write4(sc, VISFX_FATTR, 0);
756
757 summitfb_setup_fb(sc);
758 }
759
760 static int
761 summitfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
762 struct lwp *l)
763 {
764 struct vcons_data *vd = v;
765 struct summitfb_softc *sc = vd->cookie;
766 struct wsdisplay_fbinfo *wdf;
767 struct vcons_screen *ms = vd->active;
768
769 switch (cmd) {
770 case WSDISPLAYIO_GTYPE:
771 *(u_int *)data = WSDISPLAY_TYPE_STI;
772 return 0;
773
774 case GCID:
775 *(u_int *)data = sc->sc_scr.scr_rom->rom_dd.dd_grid[0];
776 return 0;
777
778 /* PCI config read/write passthrough. */
779 case PCI_IOC_CFGREAD:
780 case PCI_IOC_CFGWRITE:
781 return pci_devioctl(sc->sc_pc, sc->sc_tag,
782 cmd, data, flag, l);
783
784 case WSDISPLAYIO_GET_BUSID:
785 return wsdisplayio_busid_pci(sc->sc_dev, sc->sc_pc,
786 sc->sc_tag, data);
787
788 case WSDISPLAYIO_GINFO:
789 if (ms == NULL)
790 return ENODEV;
791 wdf = data;
792 wdf->height = ms->scr_ri.ri_height;
793 wdf->width = ms->scr_ri.ri_width;
794 wdf->depth = ms->scr_ri.ri_depth;
795 wdf->cmsize = 256;
796 return 0;
797
798 case WSDISPLAYIO_GETCMAP:
799 return summitfb_getcmap(sc,
800 (struct wsdisplay_cmap *)data);
801
802 case WSDISPLAYIO_PUTCMAP:
803 return summitfb_putcmap(sc,
804 (struct wsdisplay_cmap *)data);
805
806 case WSDISPLAYIO_LINEBYTES:
807 *(u_int *)data = 2048;
808 return 0;
809
810 case WSDISPLAYIO_SMODE: {
811 int new_mode = *(int *)data;
812
813 if (new_mode != sc->sc_mode) {
814 sc->sc_mode = new_mode;
815 if(new_mode == WSDISPLAYIO_MODE_EMUL) {
816 summitfb_setup(sc);
817 summitfb_restore_palette(sc);
818 #ifdef SUMMITFB_ENABLE_GC
819 glyphcache_wipe(&sc->sc_gc);
820 #endif
821 summitfb_loadfont(sc);
822 summitfb_rectfill(sc, 0, 0, sc->sc_width,
823 sc->sc_height, ms->scr_ri.ri_devcmap[
824 (ms->scr_defattr >> 16) & 0xff]);
825 vcons_redraw_screen(ms);
826 summitfb_set_video(sc, 1);
827 }
828 summitfb_setup_fb(sc);
829 }
830 return 0;
831 }
832
833 case WSDISPLAYIO_GET_FBINFO: {
834 struct wsdisplayio_fbinfo *fbi = data;
835 int ret;
836
837 ret = wsdisplayio_get_fbinfo(&ms->scr_ri, fbi);
838 //fbi->fbi_fbsize = sc->sc_height * 2048;
839 fbi->fbi_stride = 8192;
840 fbi->fbi_bitsperpixel = 32;
841 fbi->fbi_pixeltype = WSFB_RGB;
842 fbi->fbi_subtype.fbi_rgbmasks.red_offset = 16;
843 fbi->fbi_subtype.fbi_rgbmasks.red_size = 8;
844 fbi->fbi_subtype.fbi_rgbmasks.green_offset = 8;
845 fbi->fbi_subtype.fbi_rgbmasks.green_size = 8;
846 fbi->fbi_subtype.fbi_rgbmasks.blue_offset = 0;
847 fbi->fbi_subtype.fbi_rgbmasks.blue_size = 8;
848 fbi->fbi_subtype.fbi_rgbmasks.alpha_size = 0;
849 fbi->fbi_fbsize = sc->sc_scr.fbheight * 8192;
850 return ret;
851 }
852
853 case WSDISPLAYIO_GCURPOS: {
854 struct wsdisplay_curpos *cp = data;
855
856 cp->x = sc->sc_cursor_x;
857 cp->y = sc->sc_cursor_y;
858 return 0;
859 }
860
861 case WSDISPLAYIO_SCURPOS: {
862 struct wsdisplay_curpos *cp = data;
863
864 summitfb_move_cursor(sc, cp->x, cp->y);
865 return 0;
866 }
867
868 case WSDISPLAYIO_GCURMAX: {
869 struct wsdisplay_curpos *cp = data;
870
871 cp->x = 64;
872 cp->y = 64;
873 return 0;
874 }
875
876 case WSDISPLAYIO_SCURSOR: {
877 struct wsdisplay_cursor *cursor = data;
878
879 return summitfb_do_cursor(sc, cursor);
880 }
881
882 case WSDISPLAYIO_SVIDEO:
883 summitfb_set_video(sc, *(int *)data);
884 return 0;
885 case WSDISPLAYIO_GVIDEO:
886 *(u_int *)data = sc->sc_video_on ?
887 WSDISPLAYIO_VIDEO_ON : WSDISPLAYIO_VIDEO_OFF;
888 return 0;
889 }
890 return EPASSTHROUGH;
891 }
892
893 static paddr_t
894 summitfb_mmap(void *v, void *vs, off_t offset, int prot)
895 {
896 struct vcons_data *vd = v;
897 struct summitfb_softc *sc = vd->cookie;
898 struct sti_rom *rom = sc->sc_base.sc_rom;
899 paddr_t pa = -1;
900
901 if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)
902 return -1;
903
904 if (offset >= 0 && offset < 0x01000000) {
905 /* framebuffer */
906 pa = bus_space_mmap(rom->memt, sc->sc_scr.fbaddr, offset,
907 prot, BUS_SPACE_MAP_LINEAR);
908 } else if (offset >= 0x80000000 && offset < 0x81000000) {
909 /* blitter registers etc. */
910 pa = bus_space_mmap(rom->memt, rom->regh[0],
911 offset - 0x80000000, prot, BUS_SPACE_MAP_LINEAR);
912 }
913
914 return pa;
915 }
916
917 static void
918 summitfb_init_screen(void *cookie, struct vcons_screen *scr,
919 int existing, long *defattr)
920 {
921 struct summitfb_softc *sc = cookie;
922 struct rasops_info *ri = &scr->scr_ri;
923
924 ri->ri_depth = 8;
925 ri->ri_width = sc->sc_width;
926 ri->ri_height = sc->sc_height;
927 ri->ri_stride = 2048;
928 ri->ri_flg = RI_CENTER | RI_8BIT_IS_RGB
929 #ifdef SUMMITFB_ENABLE_GC
930 | RI_ENABLE_ALPHA | RI_PREFER_ALPHA
931 #endif
932 ;
933
934 ri->ri_bits = (void *)sc->sc_scr.fbaddr;
935 rasops_init(ri, 0, 0);
936 ri->ri_caps = WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_UNDERLINE |
937 WSSCREEN_RESIZE;
938 scr->scr_flags |= VCONS_LOADFONT/* | VCONS_NO_CURSOR*/;
939
940 rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
941 sc->sc_width / ri->ri_font->fontwidth);
942
943 ri->ri_hw = scr;
944
945 ri->ri_ops.copyrows = summitfb_copyrows;
946 ri->ri_ops.copycols = summitfb_copycols;
947 ri->ri_ops.eraserows = summitfb_eraserows;
948 ri->ri_ops.erasecols = summitfb_erasecols;
949 ri->ri_ops.cursor = summitfb_cursor;
950 #ifdef SUMMITFB_ENABLE_GC
951 sc->sc_putchar = ri->ri_ops.putchar;
952 if (FONT_IS_ALPHA(ri->ri_font)) {
953 ri->ri_ops.putchar = summitfb_putchar_aa;
954 } else
955 #endif
956 {
957 int fbwidth = (sc->sc_width + 511) & ~511;
958 int fcols = (fbwidth - sc->sc_width - 2) / ri->ri_font->fontwidth;
959 int frows = sc->sc_height / ri->ri_font->fontheight;
960 sc->sc_font_start = sc->sc_width + 2;
961 if ((fcols * frows) >= ri->ri_font->numchars) {
962 /* ok, we can do this */
963 sc->sc_cols = fcols;
964 sc->sc_font = ri->ri_font;
965 summitfb_loadfont(sc);
966 ri->ri_ops.putchar = summitfb_putchar_fast;
967 } else
968 ri->ri_ops.putchar = summitfb_putchar;
969 }
970 }
971
972 static int
973 summitfb_putcmap(struct summitfb_softc *sc, struct wsdisplay_cmap *cm)
974 {
975 u_char *r, *g, *b;
976 u_int index = cm->index;
977 u_int count = cm->count;
978 int i, error;
979 u_char rbuf[256], gbuf[256], bbuf[256];
980
981 if (cm->index >= 256 || cm->count > 256 ||
982 (cm->index + cm->count) > 256)
983 return EINVAL;
984 error = copyin(cm->red, &rbuf[index], count);
985 if (error)
986 return error;
987 error = copyin(cm->green, &gbuf[index], count);
988 if (error)
989 return error;
990 error = copyin(cm->blue, &bbuf[index], count);
991 if (error)
992 return error;
993
994 memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
995 memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
996 memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
997
998 r = &sc->sc_cmap_red[index];
999 g = &sc->sc_cmap_green[index];
1000 b = &sc->sc_cmap_blue[index];
1001
1002 for (i = 0; i < count; i++) {
1003 summitfb_putpalreg(sc, index, *r, *g, *b);
1004 index++;
1005 r++, g++, b++;
1006 }
1007 return 0;
1008 }
1009
1010 static int
1011 summitfb_getcmap(struct summitfb_softc *sc, struct wsdisplay_cmap *cm)
1012 {
1013 u_int index = cm->index;
1014 u_int count = cm->count;
1015 int error;
1016
1017 if (index >= 255 || count > 256 || index + count > 256)
1018 return EINVAL;
1019
1020 error = copyout(&sc->sc_cmap_red[index], cm->red, count);
1021 if (error)
1022 return error;
1023 error = copyout(&sc->sc_cmap_green[index], cm->green, count);
1024 if (error)
1025 return error;
1026 error = copyout(&sc->sc_cmap_blue[index], cm->blue, count);
1027 if (error)
1028 return error;
1029
1030 return 0;
1031 }
1032
1033 static void
1034 summitfb_restore_palette(struct summitfb_softc *sc)
1035 {
1036 uint8_t cmap[768];
1037 int i, j;
1038
1039 j = 0;
1040 rasops_get_cmap(&sc->sc_console_screen.scr_ri, cmap, sizeof(cmap));
1041 for (i = 0; i < 256; i++) {
1042 sc->sc_cmap_red[i] = cmap[j];
1043 sc->sc_cmap_green[i] = cmap[j + 1];
1044 sc->sc_cmap_blue[i] = cmap[j + 2];
1045 summitfb_putpalreg(sc, i, cmap[j], cmap[j + 1], cmap[j + 2]);
1046 j += 3;
1047 }
1048 }
1049
1050 static int
1051 summitfb_putpalreg(struct summitfb_softc *sc, uint8_t idx,
1052 uint8_t r, uint8_t g, uint8_t b)
1053 {
1054
1055 summitfb_write4(sc, VISFX_COLOR_INDEX, idx);
1056 summitfb_write4(sc, VISFX_COLOR_VALUE, (r << 16) | ( g << 8) | b);
1057 summitfb_write4(sc, VISFX_COLOR_MASK, 0xff);
1058 return 0;
1059 }
1060
1061 static inline void
1062 summitfb_wait_fifo(struct summitfb_softc *sc, uint32_t slots)
1063 {
1064 uint32_t reg;
1065
1066 do {
1067 reg = summitfb_read4(sc, VISFX_FIFO);
1068 } while (reg < slots);
1069 }
1070
1071 static void
1072 summitfb_rectfill(struct summitfb_softc *sc, int x, int y, int wi, int he,
1073 uint32_t bg)
1074 {
1075
1076 summitfb_write_mode(sc, VISFX_WRITE_MODE_FILL);
1077 summitfb_wait_fifo(sc, 4);
1078 summitfb_write4(sc, VISFX_IBO, RopSrc);
1079 summitfb_write4(sc, VISFX_FG_COLOUR, bg);
1080 summitfb_write4(sc, VISFX_START, (x << 16) | y);
1081 summitfb_write4(sc, VISFX_SIZE, (wi << 16) | he);
1082 }
1083
1084 static void
1085 summitfb_bitblt(void *cookie, int xs, int ys, int xd, int yd, int wi,
1086 int he, int rop)
1087 {
1088 struct summitfb_softc *sc = cookie;
1089
1090 summitfb_write_mode(sc, VISFX_WRITE_MODE_PLAIN);
1091 summitfb_wait_fifo(sc, 4);
1092 summitfb_write4(sc, VISFX_IBO, rop);
1093 summitfb_write4(sc, VISFX_COPY_SRC, (xs << 16) | ys);
1094 summitfb_write4(sc, VISFX_COPY_WH, (wi << 16) | he);
1095 summitfb_write4(sc, VISFX_COPY_DST, (xd << 16) | yd);
1096
1097 }
1098
1099 static void
1100 summitfb_nuke_cursor(struct rasops_info *ri)
1101 {
1102 struct vcons_screen *scr = ri->ri_hw;
1103 struct summitfb_softc *sc = scr->scr_cookie;
1104 int wi, he, x, y;
1105
1106 if (ri->ri_flg & RI_CURSOR) {
1107 wi = ri->ri_font->fontwidth;
1108 he = ri->ri_font->fontheight;
1109 x = ri->ri_ccol * wi + ri->ri_xorigin;
1110 y = ri->ri_crow * he + ri->ri_yorigin;
1111 summitfb_bitblt(sc, x, y, x, y, wi, he, RopInv);
1112 ri->ri_flg &= ~RI_CURSOR;
1113 }
1114 }
1115
1116 static void
1117 summitfb_cursor(void *cookie, int on, int row, int col)
1118 {
1119 struct rasops_info *ri = cookie;
1120 struct vcons_screen *scr = ri->ri_hw;
1121 struct summitfb_softc *sc = scr->scr_cookie;
1122 int x, y, wi, he;
1123
1124 wi = ri->ri_font->fontwidth;
1125 he = ri->ri_font->fontheight;
1126
1127 if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
1128 if (on) {
1129 if (ri->ri_flg & RI_CURSOR) {
1130 summitfb_nuke_cursor(ri);
1131 }
1132 x = col * wi + ri->ri_xorigin;
1133 y = row * he + ri->ri_yorigin;
1134 summitfb_bitblt(sc, x, y, x, y, wi, he, RopInv);
1135 ri->ri_flg |= RI_CURSOR;
1136 }
1137 ri->ri_crow = row;
1138 ri->ri_ccol = col;
1139 } else {
1140 ri->ri_crow = row;
1141 ri->ri_ccol = col;
1142 ri->ri_flg &= ~RI_CURSOR;
1143 }
1144
1145 }
1146
1147 static void
1148 summitfb_putchar(void *cookie, int row, int col, u_int c, long attr)
1149 {
1150 struct rasops_info *ri = cookie;
1151 struct wsdisplay_font *font = PICK_FONT(ri, c);
1152 struct vcons_screen *scr = ri->ri_hw;
1153 struct summitfb_softc *sc = scr->scr_cookie;
1154 void *data;
1155 int i, x, y, wi, he;
1156 uint32_t bg, fg, mask;
1157
1158 if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL)
1159 return;
1160
1161 if (!CHAR_IN_FONT(c, font))
1162 return;
1163
1164 if (row == ri->ri_crow && col == ri->ri_ccol) {
1165 ri->ri_flg &= ~RI_CURSOR;
1166 }
1167
1168 wi = font->fontwidth;
1169 he = font->fontheight;
1170
1171 x = ri->ri_xorigin + col * wi;
1172 y = ri->ri_yorigin + row * he;
1173
1174 bg = ri->ri_devcmap[(attr >> 16) & 0xf];
1175
1176 /* if we're drawing a space we're done here */
1177 if (c == 0x20) {
1178 summitfb_rectfill(sc, x, y, wi, he, bg);
1179 return;
1180 }
1181
1182 fg = ri->ri_devcmap[(attr >> 24) & 0x0f];
1183
1184 summitfb_write_mode(sc, VISFX_WRITE_MODE_EXPAND);
1185 summitfb_wait_fifo(sc, 6);
1186 summitfb_write4(sc, VISFX_IBO, RopSrc);
1187 summitfb_write4(sc, VISFX_FG_COLOUR, fg);
1188 summitfb_write4(sc, VISFX_BG_COLOUR, bg);
1189 mask = 0xffffffff << (32 - wi);
1190 summitfb_write4(sc, VISFX_PIXEL_MASK, mask);
1191 /* not a tpyo, coordinates *are* backwards for this register */
1192 summitfb_write4(sc, VISFX_VRAM_WRITE_DEST, (y << 16) | x);
1193
1194 data = WSFONT_GLYPH(c, font);
1195
1196 if (ri->ri_font->stride == 1) {
1197 uint8_t *data8 = data;
1198 for (i = 0; i < he; i++) {
1199 mask = *data8;
1200 summitfb_write4(sc, VISFX_VRAM_WRITE_DATA_INCRY,
1201 mask << 24);
1202 data8++;
1203 }
1204 } else {
1205 uint16_t *data16 = data;
1206 for (i = 0; i < he; i++) {
1207 mask = *data16;
1208 summitfb_write4(sc, VISFX_VRAM_WRITE_DATA_INCRY,
1209 mask << 16);
1210 data16++;
1211 }
1212 }
1213 }
1214
1215 static void
1216 summitfb_loadfont(struct summitfb_softc *sc)
1217 {
1218 int i, c, x, y;
1219 uint8_t *data;
1220 uint16_t *data16;
1221 uint32_t mask;
1222
1223 summitfb_write_mode(sc, VISFX_WRITE_MODE_EXPAND);
1224 summitfb_write4(sc, VISFX_IBO, RopSrc);
1225 summitfb_write4(sc, VISFX_FG_COLOUR, 0xffffffff);
1226 summitfb_write4(sc, VISFX_BG_COLOUR, 0);
1227
1228 mask = 0xffffffff << (32 - sc->sc_font->fontwidth);
1229 summitfb_write4(sc, VISFX_PIXEL_MASK, mask);
1230
1231 for (c = 0; c < sc->sc_font->numchars; c++) {
1232 x = sc->sc_font_start + (c % sc->sc_cols) * sc->sc_font->fontwidth;
1233 y = (c / sc->sc_cols) * sc->sc_font->fontheight;
1234 data = WSFONT_GLYPH(sc->sc_font->firstchar + c, sc->sc_font);
1235 summitfb_write4(sc, VISFX_VRAM_WRITE_DEST, (y << 16) | x);
1236 if (sc->sc_font->stride == 1) {
1237 for (i = 0; i < sc->sc_font->fontheight; i++) {
1238 mask = *data;
1239 summitfb_write4(sc, VISFX_VRAM_WRITE_DATA_INCRY,
1240 mask << 24);
1241 data++;
1242 }
1243 } else {
1244 data16 = (uint16_t *)data;
1245 for (i = 0; i < sc->sc_font->fontheight; i++) {
1246 mask = *data16;
1247 summitfb_write4(sc, VISFX_VRAM_WRITE_DATA_INCRY,
1248 mask << 16);
1249 data16++;
1250 }
1251 }
1252 }
1253 }
1254
1255 static void
1256 summitfb_putchar_fast(void *cookie, int row, int col, u_int c, long attr)
1257 {
1258 struct rasops_info *ri = cookie;
1259 struct wsdisplay_font *font = PICK_FONT(ri, c);
1260 struct vcons_screen *scr = ri->ri_hw;
1261 struct summitfb_softc *sc = scr->scr_cookie;
1262 int i, x, y, wi, he, xs, ys;
1263 uint32_t bg, fg;
1264
1265 if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL)
1266 return;
1267
1268 if (!CHAR_IN_FONT(c, font))
1269 return;
1270
1271 /* for autogenerated line drawing characters */
1272 if (font != sc->sc_font) {
1273 summitfb_putchar(cookie, row, col, c, attr);
1274 return;
1275 }
1276
1277 if (row == ri->ri_crow && col == ri->ri_ccol) {
1278 ri->ri_flg &= ~RI_CURSOR;
1279 }
1280
1281 wi = font->fontwidth;
1282 he = font->fontheight;
1283
1284 x = ri->ri_xorigin + col * wi;
1285 y = ri->ri_yorigin + row * he;
1286
1287 bg = ri->ri_devcmap[(attr >> 16) & 0xf];
1288
1289 /* if we're drawing a space we're done here */
1290 if (c == 0x20) {
1291 summitfb_rectfill(sc, x, y, wi, he, bg);
1292 return;
1293 }
1294
1295 fg = ri->ri_devcmap[(attr >> 24) & 0x0f];
1296
1297 summitfb_write_mode(sc, 0x050000c0);
1298 summitfb_wait_fifo(sc, 6);
1299 summitfb_write4(sc, VISFX_IBO, RopSrc);
1300 summitfb_write4(sc, VISFX_FG_COLOUR, fg);
1301 summitfb_write4(sc, VISFX_BG_COLOUR, bg);
1302
1303 i = c - font->firstchar;
1304 xs = sc->sc_font_start + (i % sc->sc_cols) * sc->sc_font->fontwidth;
1305 ys = (i / sc->sc_cols) * sc->sc_font->fontheight;
1306
1307 summitfb_write4(sc, VISFX_COPY_SRC, (xs << 16) | ys);
1308 summitfb_write4(sc, VISFX_COPY_WH, (wi << 16) | he);
1309 summitfb_write4(sc, VISFX_COPY_DST, (x << 16) | y);
1310
1311 }
1312
1313 #ifdef SUMMITFB_ENABLE_GC
1314 static void
1315 summitfb_putchar_aa(void *cookie, int row, int col, u_int c, long attr)
1316 {
1317 struct rasops_info *ri = cookie;
1318 struct wsdisplay_font *font = PICK_FONT(ri, c);
1319 struct vcons_screen *scr = ri->ri_hw;
1320 struct summitfb_softc *sc = scr->scr_cookie;
1321 int x, y, wi, he, rv = GC_NOPE;
1322 uint32_t bg;
1323
1324 if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL)
1325 return;
1326
1327 if (!CHAR_IN_FONT(c, font))
1328 return;
1329
1330 if (row == ri->ri_crow && col == ri->ri_ccol) {
1331 ri->ri_flg &= ~RI_CURSOR;
1332 }
1333
1334 wi = font->fontwidth;
1335 he = font->fontheight;
1336
1337 x = ri->ri_xorigin + col * wi;
1338 y = ri->ri_yorigin + row * he;
1339
1340 bg = ri->ri_devcmap[(attr >> 16) & 0xf];
1341
1342 if (c == 0x20) {
1343 summitfb_rectfill(sc, x, y, wi, he, bg);
1344 return;
1345 }
1346
1347 rv = glyphcache_try(&sc->sc_gc, c, x, y, attr);
1348 if (rv == GC_OK)
1349 return;
1350
1351 summitfb_setup_fb(sc);
1352 summitfb_wait(sc);
1353 sc->sc_putchar(cookie, row, col, c, attr);
1354
1355 if (rv == GC_ADD)
1356 glyphcache_add(&sc->sc_gc, c, x, y);
1357 }
1358 #endif
1359
1360 static void
1361 summitfb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
1362 {
1363 struct rasops_info *ri = cookie;
1364 struct vcons_screen *scr = ri->ri_hw;
1365 struct summitfb_softc *sc = scr->scr_cookie;
1366 int32_t xs, xd, y, width, height;
1367
1368 if (sc->sc_locked == 0 && sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
1369
1370 if (ri->ri_crow == row &&
1371 ri->ri_ccol >= srccol && ri->ri_ccol < (srccol + ncols) &&
1372 (ri->ri_flg & RI_CURSOR)) {
1373 summitfb_nuke_cursor(ri);
1374 }
1375
1376 xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
1377 xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
1378 y = ri->ri_yorigin + ri->ri_font->fontheight * row;
1379 width = ri->ri_font->fontwidth * ncols;
1380 height = ri->ri_font->fontheight;
1381 summitfb_bitblt(sc, xs, y, xd, y, width, height, RopSrc);
1382
1383 if (ri->ri_crow == row &&
1384 ri->ri_ccol >= dstcol && ri->ri_ccol < (dstcol + ncols))
1385 ri->ri_flg &= ~RI_CURSOR;
1386 }
1387 }
1388
1389 static void
1390 summitfb_erasecols(void *cookie, int row, int startcol, int ncols,
1391 long fillattr)
1392 {
1393 struct rasops_info *ri = cookie;
1394 struct vcons_screen *scr = ri->ri_hw;
1395 struct summitfb_softc *sc = scr->scr_cookie;
1396 int32_t x, y, width, height, fg, bg, ul;
1397
1398 if (sc->sc_locked == 0 && sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
1399 x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
1400 y = ri->ri_yorigin + ri->ri_font->fontheight * row;
1401 width = ri->ri_font->fontwidth * ncols;
1402 height = ri->ri_font->fontheight;
1403 rasops_unpack_attr(fillattr, &fg, &bg, &ul);
1404
1405 summitfb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
1406
1407 if (ri->ri_crow == row &&
1408 ri->ri_ccol >= startcol &&
1409 ri->ri_ccol < (startcol + ncols))
1410 ri->ri_flg &= ~RI_CURSOR;
1411 }
1412 }
1413
1414 static void
1415 summitfb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
1416 {
1417 struct rasops_info *ri = cookie;
1418 struct vcons_screen *scr = ri->ri_hw;
1419 struct summitfb_softc *sc = scr->scr_cookie;
1420 int32_t x, ys, yd, width, height;
1421
1422 if (sc->sc_locked == 0 && sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
1423
1424 if (ri->ri_crow >= srcrow && ri->ri_crow < (srcrow + nrows) &&
1425 (ri->ri_flg & RI_CURSOR)) {
1426 summitfb_nuke_cursor(ri);
1427 }
1428
1429 x = ri->ri_xorigin;
1430 ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
1431 yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
1432 width = ri->ri_emuwidth;
1433 height = ri->ri_font->fontheight * nrows;
1434 summitfb_bitblt(sc, x, ys, x, yd, width, height, RopSrc);
1435
1436 if (ri->ri_crow >= dstrow && ri->ri_crow < (dstrow + nrows))
1437 ri->ri_flg &= ~RI_CURSOR;
1438 }
1439 }
1440
1441 static void
1442 summitfb_eraserows(void *cookie, int row, int nrows, long fillattr)
1443 {
1444 struct rasops_info *ri = cookie;
1445 struct vcons_screen *scr = ri->ri_hw;
1446 struct summitfb_softc *sc = scr->scr_cookie;
1447 int32_t x, y, width, height, fg, bg, ul;
1448
1449 if (sc->sc_locked == 0 && sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
1450 x = ri->ri_xorigin;
1451 y = ri->ri_yorigin + ri->ri_font->fontheight * row;
1452 width = ri->ri_emuwidth;
1453 height = ri->ri_font->fontheight * nrows;
1454 rasops_unpack_attr(fillattr, &fg, &bg, &ul);
1455
1456 summitfb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
1457
1458 if (ri->ri_crow >= row && ri->ri_crow < (row + nrows))
1459 ri->ri_flg &= ~RI_CURSOR;
1460 }
1461 }
1462
1463 static void
1464 summitfb_move_cursor(struct summitfb_softc *sc, int x, int y)
1465 {
1466 uint32_t pos;
1467
1468 sc->sc_cursor_x = x;
1469 x -= sc->sc_hot_x;
1470 sc->sc_cursor_y = y;
1471 y -= sc->sc_hot_y;
1472
1473 if (x < 0)
1474 x = 0x1000 - x;
1475 if (y < 0)
1476 y = 0x1000 - y;
1477 pos = (x << 16) | y;
1478 if (sc->sc_enabled)
1479 pos |= 0x80000000;
1480 summitfb_write4(sc, VISFX_CURSOR_POS, pos);
1481 }
1482
1483 static int
1484 summitfb_do_cursor(struct summitfb_softc *sc, struct wsdisplay_cursor *cur)
1485 {
1486
1487 if (cur->which & WSDISPLAY_CURSOR_DOCUR) {
1488 sc->sc_enabled = cur->enable;
1489 cur->which |= WSDISPLAY_CURSOR_DOPOS;
1490 }
1491 if (cur->which & WSDISPLAY_CURSOR_DOHOT) {
1492 sc->sc_hot_x = cur->hot.x;
1493 sc->sc_hot_y = cur->hot.y;
1494 cur->which |= WSDISPLAY_CURSOR_DOPOS;
1495 }
1496 if (cur->which & WSDISPLAY_CURSOR_DOPOS) {
1497 summitfb_move_cursor(sc, cur->pos.x, cur->pos.y);
1498 }
1499 if (cur->which & WSDISPLAY_CURSOR_DOCMAP) {
1500 uint32_t rgb;
1501 uint8_t r[2], g[2], b[2];
1502
1503 copyin(cur->cmap.blue, b, 2);
1504 copyin(cur->cmap.green, g, 2);
1505 copyin(cur->cmap.red, r, 2);
1506 summitfb_write4(sc, VISFX_CURSOR_INDEX, 0);
1507 rgb = r[0] << 16 | g[0] << 8 | b[0];
1508 summitfb_write4(sc, VISFX_CURSOR_BG, rgb);
1509 rgb = r[1] << 16 | g[1] << 8 | b[1];
1510 summitfb_write4(sc, VISFX_CURSOR_FG, rgb);
1511
1512 }
1513 if (cur->which & WSDISPLAY_CURSOR_DOSHAPE) {
1514
1515 uint32_t buffer[128], latch, tmp;
1516 int i;
1517
1518 copyin(cur->mask, buffer, 512);
1519 summitfb_write4(sc, VISFX_CURSOR_INDEX, 0);
1520 for (i = 0; i < 128; i += 2) {
1521 latch = 0;
1522 tmp = buffer[i] & 0x80808080;
1523 latch |= tmp >> 7;
1524 tmp = buffer[i] & 0x40404040;
1525 latch |= tmp >> 5;
1526 tmp = buffer[i] & 0x20202020;
1527 latch |= tmp >> 3;
1528 tmp = buffer[i] & 0x10101010;
1529 latch |= tmp >> 1;
1530 tmp = buffer[i] & 0x08080808;
1531 latch |= tmp << 1;
1532 tmp = buffer[i] & 0x04040404;
1533 latch |= tmp << 3;
1534 tmp = buffer[i] & 0x02020202;
1535 latch |= tmp << 5;
1536 tmp = buffer[i] & 0x01010101;
1537 latch |= tmp << 7;
1538 summitfb_write4(sc, VISFX_CURSOR_DATA, latch);
1539 latch = 0;
1540 tmp = buffer[i + 1] & 0x80808080;
1541 latch |= tmp >> 7;
1542 tmp = buffer[i + 1] & 0x40404040;
1543 latch |= tmp >> 5;
1544 tmp = buffer[i + 1] & 0x20202020;
1545 latch |= tmp >> 3;
1546 tmp = buffer[i + 1] & 0x10101010;
1547 latch |= tmp >> 1;
1548 tmp = buffer[i + 1] & 0x08080808;
1549 latch |= tmp << 1;
1550 tmp = buffer[i + 1] & 0x04040404;
1551 latch |= tmp << 3;
1552 tmp = buffer[i + 1] & 0x02020202;
1553 latch |= tmp << 5;
1554 tmp = buffer[i + 1] & 0x01010101;
1555 latch |= tmp << 7;
1556 summitfb_write4(sc, VISFX_CURSOR_DATA, latch);
1557 }
1558
1559 summitfb_write4(sc, VISFX_CURSOR_INDEX, 0x80);
1560 copyin(cur->image, buffer, 512);
1561 for (i = 0; i < 128; i += 2) {
1562 latch = 0;
1563 tmp = buffer[i] & 0x80808080;
1564 latch |= tmp >> 7;
1565 tmp = buffer[i] & 0x40404040;
1566 latch |= tmp >> 5;
1567 tmp = buffer[i] & 0x20202020;
1568 latch |= tmp >> 3;
1569 tmp = buffer[i] & 0x10101010;
1570 latch |= tmp >> 1;
1571 tmp = buffer[i] & 0x08080808;
1572 latch |= tmp << 1;
1573 tmp = buffer[i] & 0x04040404;
1574 latch |= tmp << 3;
1575 tmp = buffer[i] & 0x02020202;
1576 latch |= tmp << 5;
1577 tmp = buffer[i] & 0x01010101;
1578 latch |= tmp << 7;
1579 summitfb_write4(sc, VISFX_CURSOR_DATA, latch);
1580 latch = 0;
1581 tmp = buffer[i + 1] & 0x80808080;
1582 latch |= tmp >> 7;
1583 tmp = buffer[i + 1] & 0x40404040;
1584 latch |= tmp >> 5;
1585 tmp = buffer[i + 1] & 0x20202020;
1586 latch |= tmp >> 3;
1587 tmp = buffer[i + 1] & 0x10101010;
1588 latch |= tmp >> 1;
1589 tmp = buffer[i + 1] & 0x08080808;
1590 latch |= tmp << 1;
1591 tmp = buffer[i + 1] & 0x04040404;
1592 latch |= tmp << 3;
1593 tmp = buffer[i + 1] & 0x02020202;
1594 latch |= tmp << 5;
1595 tmp = buffer[i + 1] & 0x01010101;
1596 latch |= tmp << 7;
1597 summitfb_write4(sc, VISFX_CURSOR_DATA, latch);
1598 }
1599 }
1600
1601 return 0;
1602 }
1603
1604 static void
1605 summitfb_set_video(struct summitfb_softc *sc, int on)
1606 {
1607
1608 if (sc->sc_video_on == on)
1609 return;
1610
1611 sc->sc_video_on = on;
1612
1613 summitfb_wait(sc);
1614 if (on) {
1615 summitfb_write4(sc, VISFX_MPC, MPC_VIDEO_ON);
1616 } else {
1617 summitfb_write4(sc, VISFX_MPC, MPC_VSYNC_OFF | MPC_HSYNC_OFF);
1618 }
1619 }
1620
1621 static void
1622 summitfb_copyfont(struct summitfb_softc *sc)
1623 {
1624 struct sti_font *fp = &sc->sc_scr.scr_curfont;
1625 uint8_t *font = sc->sc_scr.scr_romfont;
1626 uint8_t *fontbuf, *fontdata, *src, *dst;
1627 struct wsdisplay_font *f;
1628 int bufsize, i, si;
1629
1630 if (font == NULL)
1631 return;
1632
1633 bufsize = sizeof(struct wsdisplay_font) + 32 + fp->bpc * (fp->last - fp->first);
1634 DPRINTF(("%s: %dx%d %d\n", __func__, fp->width, fp->height, bufsize));
1635 fontbuf = kmem_alloc(bufsize, KM_SLEEP);
1636 f = (struct wsdisplay_font *)fontbuf;
1637 f->name = fontbuf + sizeof(struct wsdisplay_font);
1638 fontdata = fontbuf + sizeof(struct wsdisplay_font) + 32;
1639 strcpy(fontbuf + sizeof(struct wsdisplay_font), "HP ROM");
1640 f->firstchar = fp->first;
1641 f->numchars = (fp->last + 1) - fp->first;
1642 f->encoding = WSDISPLAY_FONTENC_ISO;
1643 f->fontwidth = fp->width;
1644 f->fontheight = fp->height;
1645 f->stride = (fp->width + 7) >> 3;
1646 f->bitorder = WSDISPLAY_FONTORDER_L2R;
1647 f->byteorder = WSDISPLAY_FONTORDER_L2R;
1648 f->data = fontdata;
1649 /* skip over font struct */
1650 font += sizeof(struct sti_font);
1651 /* now copy and rearrange the glyphs into ISO order */
1652 /* first, copy the characters up to 0x7f */
1653 memcpy(fontdata, font, (0x80 - fp->first) * fp->bpc);
1654 /* zero 0x80 to 0x9f */
1655 memset(fontdata + 0x80 * fp->bpc, 0, 0x20 * fp->bpc);
1656 /* rearrange 0xa0 till last */
1657 for (i = 0xa0; i < (fp->last + 1); i++) {
1658 dst = fontdata + fp->bpc * i;
1659 si = sti_unitoroman[i - 0xa0];
1660 if (si != 0) {
1661 src = font + fp->bpc * si;
1662 memcpy(dst, src, fp->bpc);
1663 } else {
1664 /* no mapping - zeeo this cell */
1665 memset(dst, 0, fp->bpc);
1666 }
1667 }
1668 wsfont_add(f, 0);
1669 }
1670