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