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