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