gftfb.c revision 1.6 1 /* $NetBSD: gftfb.c,v 1.6 2024/02/21 13:24:40 macallan Exp $ */
2
3 /* $OpenBSD: sti_pci.c,v 1.7 2009/02/06 22:51:04 miod Exp $ */
4
5 /*
6 * Copyright (c) 2006, 2007 Miodrag Vallat.
7 ^ 2024 Michael Lorenz
8 *
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice, this permission notice, and the disclaimer below
12 * appear in all copies.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
17 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 */
22
23 /*
24 * a native driver for HP Visualize EG PCI graphics cards
25 * STI portions are from Miodrag Vallat's sti_pci.c
26 */
27
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/kmem.h>
31 #include <sys/device.h>
32
33 #include <dev/pci/pcivar.h>
34 #include <dev/pci/pcireg.h>
35 #include <dev/pci/pcidevs.h>
36 #include <dev/pci/pciio.h>
37
38 #include <dev/wscons/wsdisplayvar.h>
39 #include <dev/wscons/wsconsio.h>
40 #include <dev/wsfont/wsfont.h>
41 #include <dev/rasops/rasops.h>
42 #include <dev/wscons/wsdisplay_vconsvar.h>
43 #include <dev/pci/wsdisplay_pci.h>
44 #include <dev/wscons/wsdisplay_glyphcachevar.h>
45
46 #include <dev/ic/stireg.h>
47 #include <dev/ic/stivar.h>
48
49 #ifdef STIDEBUG
50 #define DPRINTF(s) do { \
51 if (stidebug) \
52 printf s; \
53 } while(0)
54
55 extern int stidebug;
56 #else
57 #define DPRINTF(s) /* */
58 #endif
59
60 int gftfb_match(device_t, cfdata_t, void *);
61 void gftfb_attach(device_t, device_t, void *);
62
63 struct gftfb_softc {
64 device_t sc_dev;
65 pci_chipset_tag_t sc_pc;
66 pcitag_t sc_tag;
67
68 /* stuff we need in order to use the STI ROM */
69 struct sti_softc sc_base;
70 struct sti_screen sc_scr;
71 bus_space_handle_t sc_romh;
72
73 int sc_width, sc_height;
74 int sc_locked;
75 struct vcons_screen sc_console_screen;
76 struct wsscreen_descr sc_defaultscreen_descr;
77 const struct wsscreen_descr *sc_screens[1];
78 struct wsscreen_list sc_screenlist;
79 struct vcons_data vd;
80 int sc_mode;
81 void (*sc_putchar)(void *, int, int, u_int, long);
82 u_char sc_cmap_red[256];
83 u_char sc_cmap_green[256];
84 u_char sc_cmap_blue[256];
85 uint32_t sc_hwmode;
86 #define HW_FB 0
87 #define HW_FILL 1
88 #define HW_BLIT 2
89 glyphcache sc_gc;
90 };
91
92 CFATTACH_DECL_NEW(gftfb, sizeof(struct gftfb_softc),
93 gftfb_match, gftfb_attach, NULL, NULL);
94
95 int gftfb_readbar(struct sti_softc *, struct pci_attach_args *, u_int, int);
96 int gftfb_check_rom(struct gftfb_softc *, struct pci_attach_args *);
97 void gftfb_enable_rom(struct sti_softc *);
98 void gftfb_disable_rom(struct sti_softc *);
99 void gftfb_enable_rom_internal(struct gftfb_softc *);
100 void gftfb_disable_rom_internal(struct gftfb_softc *);
101
102 void gftfb_setup(struct gftfb_softc *);
103
104 #define ngle_bt458_write(memt, memh, r, v) \
105 bus_space_write_stream_4(memt, memh, NGLE_REG_RAMDAC + ((r) << 2), (v) << 24)
106
107
108 /* XXX these really need to go into their own header */
109 int sti_pci_is_console(struct pci_attach_args *, bus_addr_t *);
110 int sti_rom_setup(struct sti_rom *, bus_space_tag_t, bus_space_tag_t,
111 bus_space_handle_t, bus_addr_t *, u_int);
112 int sti_screen_setup(struct sti_screen *, int);
113 void sti_describe_screen(struct sti_softc *, struct sti_screen *);
114
115 #define PCI_ROM_SIZE(mr) \
116 (PCI_MAPREG_ROM_ADDR(mr) & -PCI_MAPREG_ROM_ADDR(mr))
117
118 /* wsdisplay stuff */
119 static int gftfb_ioctl(void *, void *, u_long, void *, int,
120 struct lwp *);
121 static paddr_t gftfb_mmap(void *, void *, off_t, int);
122 static void gftfb_init_screen(void *, struct vcons_screen *, int, long *);
123
124 static int gftfb_putcmap(struct gftfb_softc *, struct wsdisplay_cmap *);
125 static int gftfb_getcmap(struct gftfb_softc *, struct wsdisplay_cmap *);
126 static void gftfb_restore_palette(struct gftfb_softc *);
127 static int gftfb_putpalreg(struct gftfb_softc *, uint8_t, uint8_t,
128 uint8_t, uint8_t);
129
130 static void gftfb_rectfill(struct gftfb_softc *, int, int, int, int,
131 uint32_t);
132 static void gftfb_bitblt(void *, int, int, int, int, int,
133 int, int);
134
135 static void gftfb_cursor(void *, int, int, int);
136 static void gftfb_putchar(void *, int, int, u_int, long);
137 static void gftfb_copycols(void *, int, int, int, int);
138 static void gftfb_erasecols(void *, int, int, int, long);
139 static void gftfb_copyrows(void *, int, int, int);
140 static void gftfb_eraserows(void *, int, int, long);
141
142 struct wsdisplay_accessops gftfb_accessops = {
143 gftfb_ioctl,
144 gftfb_mmap,
145 NULL, /* alloc_screen */
146 NULL, /* free_screen */
147 NULL, /* show_screen */
148 NULL, /* load_font */
149 NULL, /* pollc */
150 NULL /* scroll */
151 };
152
153 #define BA(F,C,S,A,J,B,I) \
154 (((F)<<31)|((C)<<27)|((S)<<24)|((A)<<21)|((J)<<16)|((B)<<12)|(I))
155
156 #define IBOvals(R,M,X,S,D,L,B,F) \
157 (((R)<<8)|((M)<<16)|((X)<<24)|((S)<<29)|((D)<<28)|((L)<<31)|((B)<<1)|(F))
158
159 #define IndexedDcd 0 /* Pixel data is indexed (pseudo) color */
160 #define Otc04 2 /* Pixels in each longword transfer (4) */
161 #define Otc32 5 /* Pixels in each longword transfer (32) */
162 #define Ots08 3 /* Each pixel is size (8)d transfer (1) */
163 #define OtsIndirect 6 /* Each bit goes through FG/BG color(8) */
164 #define AddrLong 5 /* FB address is Long aligned (pixel) */
165 #define BINovly 0x2 /* 8 bit overlay */
166 #define BINapp0I 0x0 /* Application Buffer 0, Indexed */
167 #define BINapp1I 0x1 /* Application Buffer 1, Indexed */
168 #define BINapp0F8 0xa /* Application Buffer 0, Fractional 8-8-8 */
169 #define BINattr 0xd /* Attribute Bitmap */
170 #define RopSrc 0x3
171 #define RopInv 0xc
172 #define BitmapExtent08 3 /* Each write hits ( 8) bits in depth */
173 #define BitmapExtent32 5 /* Each write hits (32) bits in depth */
174 #define DataDynamic 0 /* Data register reloaded by direct access */
175 #define MaskDynamic 1 /* Mask register reloaded by direct access */
176 #define MaskOtc 0 /* Mask contains Object Count valid bits */
177
178 int
179 gftfb_match(device_t parent, cfdata_t cf, void *aux)
180 {
181 struct pci_attach_args *paa = aux;
182
183 if (PCI_VENDOR(paa->pa_id) != PCI_VENDOR_HP)
184 return 0;
185
186 if (PCI_PRODUCT(paa->pa_id) == PCI_PRODUCT_HP_VISUALIZE_EG)
187 return 10; /* beat out sti at pci */
188
189 return 0;
190 }
191
192 void
193 gftfb_attach(device_t parent, device_t self, void *aux)
194 {
195 struct gftfb_softc *sc = device_private(self);
196 struct pci_attach_args *paa = aux;
197 struct sti_rom *rom;
198 struct rasops_info *ri;
199 struct wsemuldisplaydev_attach_args aa;
200 unsigned long defattr = 0;
201 int ret, is_console = 0, i, j;
202 uint8_t cmap[768];
203
204 sc->sc_dev = self;
205
206 sc->sc_pc = paa->pa_pc;
207 sc->sc_tag = paa->pa_tag;
208 sc->sc_base.sc_dev = self;
209 sc->sc_base.sc_enable_rom = gftfb_enable_rom;
210 sc->sc_base.sc_disable_rom = gftfb_disable_rom;
211
212 aprint_normal("\n");
213
214 if (gftfb_check_rom(sc, paa) != 0)
215 return;
216
217 ret = sti_pci_is_console(paa, sc->sc_base. bases);
218 if (ret != 0) {
219 sc->sc_base.sc_flags |= STI_CONSOLE;
220 is_console = 1;
221 }
222 rom = (struct sti_rom *)kmem_zalloc(sizeof(*rom), KM_SLEEP);
223 rom->rom_softc = &sc->sc_base;
224 ret = sti_rom_setup(rom, paa->pa_iot, paa->pa_memt, sc->sc_romh, sc->sc_base.bases, STI_CODEBASE_MAIN);
225 if (ret != 0) {
226 kmem_free(rom, sizeof(*rom));
227 return;
228 }
229
230 sc->sc_base.sc_rom = rom;
231
232 sc->sc_scr.scr_rom = sc->sc_base.sc_rom;
233 ret = sti_screen_setup(&sc->sc_scr, STI_FBMODE);
234
235 sc->sc_width = sc->sc_scr.scr_cfg.scr_width;
236 sc->sc_height = sc->sc_scr.scr_cfg.scr_height;
237
238 aprint_normal_dev(sc->sc_dev, "%s at %dx%d\n", sc->sc_scr.name,
239 sc->sc_width, sc->sc_height);
240 gftfb_setup(sc);
241
242 sc->sc_defaultscreen_descr = (struct wsscreen_descr){
243 "default",
244 0, 0,
245 NULL,
246 8, 16,
247 WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_UNDERLINE |
248 WSSCREEN_RESIZE,
249 NULL
250 };
251
252 sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
253 sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
254 sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
255 sc->sc_locked = 0;
256
257 vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr,
258 &gftfb_accessops);
259 sc->vd.init_screen = gftfb_init_screen;
260 sc->vd.show_screen_cookie = &sc->sc_gc;
261 sc->vd.show_screen_cb = glyphcache_adapt;
262
263 ri = &sc->sc_console_screen.scr_ri;
264
265 sc->sc_gc.gc_bitblt = gftfb_bitblt;
266 sc->sc_gc.gc_blitcookie = sc;
267 sc->sc_gc.gc_rop = RopSrc;
268
269 if (is_console) {
270 vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
271 &defattr);
272 sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
273
274 sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
275 sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
276 sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
277 sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
278
279 glyphcache_init(&sc->sc_gc, sc->sc_height + 5,
280 sc->sc_scr.fbheight - sc->sc_height - 5,
281 sc->sc_width,
282 ri->ri_font->fontwidth,
283 ri->ri_font->fontheight,
284 defattr);
285
286 wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0,
287 defattr);
288
289 gftfb_rectfill(sc, 0, 0, sc->sc_width, sc->sc_height,
290 ri->ri_devcmap[(defattr >> 16) & 0xff]);
291
292 vcons_replay_msgbuf(&sc->sc_console_screen);
293 } else {
294 /*
295 * since we're not the console we can postpone the rest
296 * until someone actually allocates a screen for us
297 */
298 if (sc->sc_console_screen.scr_ri.ri_rows == 0) {
299 /* do some minimal setup to avoid weirdnesses later */
300 vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
301 &defattr);
302 } else
303 (*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
304
305 glyphcache_init(&sc->sc_gc, sc->sc_height + 5,
306 sc->sc_scr.fbheight - sc->sc_height - 5,
307 sc->sc_width,
308 ri->ri_font->fontwidth,
309 ri->ri_font->fontheight,
310 defattr);
311 }
312
313 j = 0;
314 rasops_get_cmap(ri, cmap, sizeof(cmap));
315 for (i = 0; i < 256; i++) {
316 sc->sc_cmap_red[i] = cmap[j];
317 sc->sc_cmap_green[i] = cmap[j + 1];
318 sc->sc_cmap_blue[i] = cmap[j + 2];
319 gftfb_putpalreg(sc, i, cmap[j], cmap[j + 1], cmap[j + 2]);
320 j += 3;
321 }
322
323 /* no suspend/resume support yet */
324 if (!pmf_device_register(sc->sc_dev, NULL, NULL))
325 aprint_error_dev(sc->sc_dev,
326 "couldn't establish power handler\n");
327
328 aa.console = is_console;
329 aa.scrdata = &sc->sc_screenlist;
330 aa.accessops = &gftfb_accessops;
331 aa.accesscookie = &sc->vd;
332
333 config_found(sc->sc_dev, &aa, wsemuldisplaydevprint, CFARGS_NONE);
334 }
335
336 /*
337 * Grovel the STI ROM image.
338 */
339 int
340 gftfb_check_rom(struct gftfb_softc *spc, struct pci_attach_args *pa)
341 {
342 struct sti_softc *sc = &spc->sc_base;
343 pcireg_t address, mask;
344 bus_space_handle_t romh;
345 bus_size_t romsize, subsize, stiromsize;
346 bus_addr_t selected, offs, suboffs;
347 uint32_t tmp;
348 int i;
349 int rc;
350
351 /* sort of inline sti_pci_enable_rom(sc) */
352 address = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_MAPREG_ROM);
353 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_MAPREG_ROM,
354 ~PCI_MAPREG_ROM_ENABLE);
355 mask = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_MAPREG_ROM);
356 address |= PCI_MAPREG_ROM_ENABLE;
357 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_MAPREG_ROM, address);
358 sc->sc_flags |= STI_ROM_ENABLED;
359 /*
360 * Map the complete ROM for now.
361 */
362
363 romsize = PCI_ROM_SIZE(mask);
364 DPRINTF(("%s: mapping rom @ %lx for %lx\n", __func__,
365 (long)PCI_MAPREG_ROM_ADDR(address), (long)romsize));
366
367 rc = bus_space_map(pa->pa_memt, PCI_MAPREG_ROM_ADDR(address), romsize,
368 0, &romh);
369 if (rc != 0) {
370 aprint_error_dev(sc->sc_dev, "can't map PCI ROM (%d)\n", rc);
371 goto fail2;
372 }
373
374 gftfb_disable_rom_internal(spc);
375 /*
376 * Iterate over the ROM images, pick the best candidate.
377 */
378
379 selected = (bus_addr_t)-1;
380 for (offs = 0; offs < romsize; offs += subsize) {
381 gftfb_enable_rom_internal(spc);
382 /*
383 * Check for a valid ROM header.
384 */
385 tmp = bus_space_read_4(pa->pa_memt, romh, offs + 0);
386 tmp = le32toh(tmp);
387 if (tmp != 0x55aa0000) {
388 gftfb_disable_rom_internal(spc);
389 if (offs == 0) {
390 aprint_error_dev(sc->sc_dev,
391 "invalid PCI ROM header signature (%08x)\n",
392 tmp);
393 rc = EINVAL;
394 }
395 break;
396 }
397
398 /*
399 * Check ROM type.
400 */
401 tmp = bus_space_read_4(pa->pa_memt, romh, offs + 4);
402 tmp = le32toh(tmp);
403 if (tmp != 0x00000001) { /* 1 == STI ROM */
404 gftfb_disable_rom_internal(spc);
405 if (offs == 0) {
406 aprint_error_dev(sc->sc_dev,
407 "invalid PCI ROM type (%08x)\n", tmp);
408 rc = EINVAL;
409 }
410 break;
411 }
412
413 subsize = (bus_addr_t)bus_space_read_2(pa->pa_memt, romh,
414 offs + 0x0c);
415 subsize <<= 9;
416
417 #ifdef STIDEBUG
418 gftfb_disable_rom_internal(spc);
419 DPRINTF(("ROM offset %08x size %08x type %08x",
420 (u_int)offs, (u_int)subsize, tmp));
421 gftfb_enable_rom_internal(spc);
422 #endif
423
424 /*
425 * Check for a valid ROM data structure.
426 * We do not need it except to know what architecture the ROM
427 * code is for.
428 */
429
430 suboffs = offs +(bus_addr_t)bus_space_read_2(pa->pa_memt, romh,
431 offs + 0x18);
432 tmp = bus_space_read_4(pa->pa_memt, romh, suboffs + 0);
433 tmp = le32toh(tmp);
434 if (tmp != 0x50434952) { /* PCIR */
435 gftfb_disable_rom_internal(spc);
436 if (offs == 0) {
437 aprint_error_dev(sc->sc_dev, "invalid PCI data"
438 " signature (%08x)\n", tmp);
439 rc = EINVAL;
440 } else {
441 DPRINTF((" invalid PCI data signature %08x\n",
442 tmp));
443 continue;
444 }
445 }
446
447 tmp = bus_space_read_1(pa->pa_memt, romh, suboffs + 0x14);
448 gftfb_disable_rom_internal(spc);
449 DPRINTF((" code %02x", tmp));
450
451 switch (tmp) {
452 #ifdef __hppa__
453 case 0x10:
454 if (selected == (bus_addr_t)-1)
455 selected = offs;
456 break;
457 #endif
458 #ifdef __i386__
459 case 0x00:
460 if (selected == (bus_addr_t)-1)
461 selected = offs;
462 break;
463 #endif
464 default:
465 #ifdef STIDEBUG
466 DPRINTF((" (wrong architecture)"));
467 #endif
468 break;
469 }
470 DPRINTF(("%s\n", selected == offs ? " -> SELECTED" : ""));
471 }
472
473 if (selected == (bus_addr_t)-1) {
474 if (rc == 0) {
475 aprint_error_dev(sc->sc_dev, "found no ROM with "
476 "correct microcode architecture\n");
477 rc = ENOEXEC;
478 }
479 goto fail;
480 }
481
482 /*
483 * Read the STI region BAR assignments.
484 */
485
486 gftfb_enable_rom_internal(spc);
487 offs = selected +
488 (bus_addr_t)bus_space_read_2(pa->pa_memt, romh, selected + 0x0e);
489 for (i = 0; i < STI_REGION_MAX; i++) {
490 rc = gftfb_readbar(sc, pa, i,
491 bus_space_read_1(pa->pa_memt, romh, offs + i));
492 if (rc != 0)
493 goto fail;
494 }
495
496 /*
497 * Find out where the STI ROM itself lies, and its size.
498 */
499
500 offs = selected +
501 (bus_addr_t)bus_space_read_4(pa->pa_memt, romh, selected + 0x08);
502 stiromsize = (bus_addr_t)bus_space_read_4(pa->pa_memt, romh,
503 offs + 0x18);
504 stiromsize = le32toh(stiromsize);
505 gftfb_disable_rom_internal(spc);
506
507 /*
508 * Replace our mapping with a smaller mapping of only the area
509 * we are interested in.
510 */
511
512 DPRINTF(("remapping rom @ %lx for %lx\n",
513 (long)(PCI_MAPREG_ROM_ADDR(address) + offs), (long)stiromsize));
514 bus_space_unmap(pa->pa_memt, romh, romsize);
515 rc = bus_space_map(pa->pa_memt, PCI_MAPREG_ROM_ADDR(address) + offs,
516 stiromsize, 0, &spc->sc_romh);
517 if (rc != 0) {
518 aprint_error_dev(sc->sc_dev, "can't map STI ROM (%d)\n",
519 rc);
520 goto fail2;
521 }
522 gftfb_disable_rom_internal(spc);
523 sc->sc_flags &= ~STI_ROM_ENABLED;
524
525 return 0;
526
527 fail:
528 bus_space_unmap(pa->pa_memt, romh, romsize);
529 fail2:
530 gftfb_disable_rom_internal(spc);
531
532 return rc;
533 }
534
535 /*
536 * Decode a BAR register.
537 */
538 int
539 gftfb_readbar(struct sti_softc *sc, struct pci_attach_args *pa, u_int region,
540 int bar)
541 {
542 bus_addr_t addr;
543 bus_size_t size;
544 uint32_t cf;
545 int rc;
546
547 if (bar == 0) {
548 sc->bases[region] = 0;
549 return (0);
550 }
551
552 #ifdef DIAGNOSTIC
553 if (bar < PCI_MAPREG_START || bar > PCI_MAPREG_PPB_END) {
554 gftfb_disable_rom(sc);
555 printf("%s: unexpected bar %02x for region %d\n",
556 device_xname(sc->sc_dev), bar, region);
557 gftfb_enable_rom(sc);
558 }
559 #endif
560
561 cf = pci_conf_read(pa->pa_pc, pa->pa_tag, bar);
562
563 rc = pci_mapreg_info(pa->pa_pc, pa->pa_tag, bar, PCI_MAPREG_TYPE(cf),
564 &addr, &size, NULL);
565
566 if (rc != 0) {
567 gftfb_disable_rom(sc);
568 aprint_error_dev(sc->sc_dev, "invalid bar %02x for region %d\n",
569 bar, region);
570 gftfb_enable_rom(sc);
571 return (rc);
572 }
573
574 sc->bases[region] = addr;
575 return (0);
576 }
577
578 /*
579 * Enable PCI ROM.
580 */
581 void
582 gftfb_enable_rom_internal(struct gftfb_softc *spc)
583 {
584 pcireg_t address;
585
586 KASSERT(spc != NULL);
587
588 address = pci_conf_read(spc->sc_pc, spc->sc_tag, PCI_MAPREG_ROM);
589 address |= PCI_MAPREG_ROM_ENABLE;
590 pci_conf_write(spc->sc_pc, spc->sc_tag, PCI_MAPREG_ROM, address);
591 }
592
593 void
594 gftfb_enable_rom(struct sti_softc *sc)
595 {
596 struct gftfb_softc *spc = device_private(sc->sc_dev);
597
598 if (!ISSET(sc->sc_flags, STI_ROM_ENABLED)) {
599 gftfb_enable_rom_internal(spc);
600 }
601 SET(sc->sc_flags, STI_ROM_ENABLED);
602 }
603
604 /*
605 * Disable PCI ROM.
606 */
607 void
608 gftfb_disable_rom_internal(struct gftfb_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 gftfb_disable_rom(struct sti_softc *sc)
621 {
622 struct gftfb_softc *spc = device_private(sc->sc_dev);
623
624 if (ISSET(sc->sc_flags, STI_ROM_ENABLED)) {
625 gftfb_disable_rom_internal(spc);
626 }
627 CLR(sc->sc_flags, STI_ROM_ENABLED);
628 }
629
630 static inline void
631 gftfb_wait(struct gftfb_softc *sc)
632 {
633 struct sti_rom *rom = sc->sc_base.sc_rom;
634 bus_space_tag_t memt = rom->memt;
635 bus_space_handle_t memh = rom->regh[2];
636 uint8_t stat;
637
638 do {
639 stat = bus_space_read_1(memt, memh, NGLE_REG_15b0);
640 if (stat == 0)
641 stat = bus_space_read_1(memt, memh, NGLE_REG_15b0);
642 } while (stat != 0);
643 }
644
645 static inline void
646 gftfb_setup_fb(struct gftfb_softc *sc)
647 {
648 struct sti_rom *rom = sc->sc_base.sc_rom;
649 bus_space_tag_t memt = rom->memt;
650 bus_space_handle_t memh = rom->regh[2];
651
652 gftfb_wait(sc);
653 bus_space_write_stream_4(memt, memh, NGLE_REG_10, 0x13601000);
654 bus_space_write_stream_4(memt, memh, NGLE_REG_14, 0x83000300);
655 gftfb_wait(sc);
656 bus_space_write_1(memt, memh, NGLE_REG_16b1, 1);
657 sc->sc_hwmode = HW_FB;
658 }
659
660 void
661 gftfb_setup(struct gftfb_softc *sc)
662 {
663 struct sti_rom *rom = sc->sc_base.sc_rom;
664 bus_space_tag_t memt = rom->memt;
665 bus_space_handle_t memh = rom->regh[2];
666
667 sc->sc_hwmode = HW_FB;
668
669 /* set Bt458 read mask register to all planes */
670 gftfb_wait(sc);
671 ngle_bt458_write(memt, memh, 0x08, 0x04);
672 ngle_bt458_write(memt, memh, 0x0a, 0xff);
673
674 gftfb_setup_fb(sc);
675
676 /* attr. planes */
677 gftfb_wait(sc);
678 bus_space_write_stream_4(memt, memh, NGLE_REG_11, 0x2ea0d000);
679 bus_space_write_stream_4(memt, memh, NGLE_REG_14, 0x23000302);
680 bus_space_write_stream_4(memt, memh, NGLE_REG_12, NGLE_ARTIST_CMAP0);
681 bus_space_write_stream_4(memt, memh, NGLE_REG_8, 0xffffffff);
682
683 gftfb_wait(sc);
684 bus_space_write_stream_4(memt, memh, NGLE_REG_6, 0x00000000);
685 bus_space_write_stream_4(memt, memh, NGLE_REG_9,
686 (sc->sc_scr.scr_cfg.scr_width << 16) | sc->sc_scr.scr_cfg.scr_height);
687 /*
688 * blit into offscreen memory to force flush previous - apparently
689 * some chips have a bug this works around
690 */
691 bus_space_write_stream_4(memt, memh, NGLE_REG_6, 0x05000000);
692 bus_space_write_stream_4(memt, memh, NGLE_REG_9, 0x00040001);
693
694 gftfb_wait(sc);
695 bus_space_write_stream_4(memt, memh, NGLE_REG_12, 0x00000000);
696
697 gftfb_setup_fb(sc);
698
699 /* make sure video output is enabled */
700 gftfb_wait(sc);
701 bus_space_write_stream_4(memt, memh, NGLE_REG_21,
702 bus_space_read_stream_4(memt, memh, NGLE_REG_21) | 0x0a000000);
703 bus_space_write_stream_4(memt, memh, NGLE_REG_27,
704 bus_space_read_stream_4(memt, memh, NGLE_REG_27) | 0x00800000);
705 }
706
707 static int
708 gftfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
709 struct lwp *l)
710 {
711 struct vcons_data *vd = v;
712 struct gftfb_softc *sc = vd->cookie;
713 struct wsdisplay_fbinfo *wdf;
714 struct vcons_screen *ms = vd->active;
715
716 switch (cmd) {
717 case WSDISPLAYIO_GTYPE:
718 *(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
719 return 0;
720
721 /* PCI config read/write passthrough. */
722 case PCI_IOC_CFGREAD:
723 case PCI_IOC_CFGWRITE:
724 return pci_devioctl(sc->sc_pc, sc->sc_tag,
725 cmd, data, flag, l);
726
727 case WSDISPLAYIO_GET_BUSID:
728 return wsdisplayio_busid_pci(sc->sc_dev, sc->sc_pc,
729 sc->sc_tag, data);
730
731 case WSDISPLAYIO_GINFO:
732 if (ms == NULL)
733 return ENODEV;
734 wdf = (void *)data;
735 wdf->height = ms->scr_ri.ri_height;
736 wdf->width = ms->scr_ri.ri_width;
737 wdf->depth = ms->scr_ri.ri_depth;
738 wdf->cmsize = 256;
739 return 0;
740
741 case WSDISPLAYIO_GETCMAP:
742 return gftfb_getcmap(sc,
743 (struct wsdisplay_cmap *)data);
744
745 case WSDISPLAYIO_PUTCMAP:
746 return gftfb_putcmap(sc,
747 (struct wsdisplay_cmap *)data);
748
749 case WSDISPLAYIO_LINEBYTES:
750 *(u_int *)data = 2048;
751 return 0;
752
753 case WSDISPLAYIO_SMODE: {
754 int new_mode = *(int*)data;
755 if (new_mode != sc->sc_mode) {
756 sc->sc_mode = new_mode;
757 if(new_mode == WSDISPLAYIO_MODE_EMUL) {
758 gftfb_setup(sc);
759 gftfb_restore_palette(sc);
760 glyphcache_wipe(&sc->sc_gc);
761 gftfb_rectfill(sc, 0, 0, sc->sc_width,
762 sc->sc_height, ms->scr_ri.ri_devcmap[
763 (ms->scr_defattr >> 16) & 0xff]);
764 vcons_redraw_screen(ms);
765 }
766 }
767 }
768 return 0;
769
770 case WSDISPLAYIO_GET_FBINFO:
771 {
772 struct wsdisplayio_fbinfo *fbi = data;
773
774 return wsdisplayio_get_fbinfo(&ms->scr_ri, fbi);
775 }
776 }
777 return EPASSTHROUGH;
778 }
779
780 static paddr_t
781 gftfb_mmap(void *v, void *vs, off_t offset, int prot)
782 {
783 struct vcons_data *vd = v;
784 struct gftfb_softc *sc = vd->cookie;
785 struct sti_rom *rom = sc->sc_base.sc_rom;
786 paddr_t pa;
787
788 if (offset < 0 || offset >= sc->sc_scr.fblen)
789 return -1;
790
791 if (sc->sc_mode != WSDISPLAYIO_MODE_DUMBFB)
792 return -1;
793
794 pa = bus_space_mmap(rom->memt, sc->sc_scr.fbaddr, offset, prot,
795 BUS_SPACE_MAP_LINEAR);
796 return pa;
797 }
798
799 static void
800 gftfb_init_screen(void *cookie, struct vcons_screen *scr,
801 int existing, long *defattr)
802 {
803 struct gftfb_softc *sc = cookie;
804 struct rasops_info *ri = &scr->scr_ri;
805
806 ri->ri_depth = 8;
807 ri->ri_width = sc->sc_width;
808 ri->ri_height = sc->sc_height;
809 ri->ri_stride = 2048;
810 ri->ri_flg = RI_CENTER | RI_8BIT_IS_RGB |
811 RI_ENABLE_ALPHA | RI_PREFER_ALPHA;
812
813 ri->ri_bits = (void *)sc->sc_scr.fbaddr;
814 rasops_init(ri, 0, 0);
815 ri->ri_caps = WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_UNDERLINE |
816 WSSCREEN_RESIZE;
817 scr->scr_flags |= VCONS_LOADFONT;
818
819 rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
820 sc->sc_width / ri->ri_font->fontwidth);
821
822 ri->ri_hw = scr;
823 sc->sc_putchar = ri->ri_ops.putchar;
824 ri->ri_ops.copyrows = gftfb_copyrows;
825 ri->ri_ops.copycols = gftfb_copycols;
826 ri->ri_ops.eraserows = gftfb_eraserows;
827 ri->ri_ops.erasecols = gftfb_erasecols;
828 ri->ri_ops.cursor = gftfb_cursor;
829 ri->ri_ops.putchar = gftfb_putchar;
830 }
831
832 static int
833 gftfb_putcmap(struct gftfb_softc *sc, struct wsdisplay_cmap *cm)
834 {
835 u_char *r, *g, *b;
836 u_int index = cm->index;
837 u_int count = cm->count;
838 int i, error;
839 u_char rbuf[256], gbuf[256], bbuf[256];
840
841 if (cm->index >= 256 || cm->count > 256 ||
842 (cm->index + cm->count) > 256)
843 return EINVAL;
844 error = copyin(cm->red, &rbuf[index], count);
845 if (error)
846 return error;
847 error = copyin(cm->green, &gbuf[index], count);
848 if (error)
849 return error;
850 error = copyin(cm->blue, &bbuf[index], count);
851 if (error)
852 return error;
853
854 memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
855 memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
856 memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
857
858 r = &sc->sc_cmap_red[index];
859 g = &sc->sc_cmap_green[index];
860 b = &sc->sc_cmap_blue[index];
861
862 for (i = 0; i < count; i++) {
863 gftfb_putpalreg(sc, index, *r, *g, *b);
864 index++;
865 r++, g++, b++;
866 }
867 return 0;
868 }
869
870 static int
871 gftfb_getcmap(struct gftfb_softc *sc, struct wsdisplay_cmap *cm)
872 {
873 u_int index = cm->index;
874 u_int count = cm->count;
875 int error;
876
877 if (index >= 255 || count > 256 || index + count > 256)
878 return EINVAL;
879
880 error = copyout(&sc->sc_cmap_red[index], cm->red, count);
881 if (error)
882 return error;
883 error = copyout(&sc->sc_cmap_green[index], cm->green, count);
884 if (error)
885 return error;
886 error = copyout(&sc->sc_cmap_blue[index], cm->blue, count);
887 if (error)
888 return error;
889
890 return 0;
891 }
892
893 static void
894 gftfb_restore_palette(struct gftfb_softc *sc)
895 {
896 int i;
897
898 for (i = 0; i < 256; i++) {
899 gftfb_putpalreg(sc, i, sc->sc_cmap_red[i],
900 sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
901 }
902 }
903
904 static int
905 gftfb_putpalreg(struct gftfb_softc *sc, uint8_t idx, uint8_t r, uint8_t g,
906 uint8_t b)
907 {
908 struct sti_rom *rom = sc->sc_base.sc_rom;
909 bus_space_tag_t memt = rom->memt;
910 bus_space_handle_t memh = rom->regh[2];
911
912 gftfb_wait(sc);
913 bus_space_write_stream_4(memt, memh, NGLE_REG_10, 0xbbe0f000);
914 bus_space_write_stream_4(memt, memh, NGLE_REG_14, 0x03000300);
915 bus_space_write_stream_4(memt, memh, NGLE_REG_13, 0xffffffff);
916
917 gftfb_wait(sc);
918 bus_space_write_stream_4(memt, memh, NGLE_REG_3,
919 0x400 | (idx << 2));
920 bus_space_write_stream_4(memt, memh, NGLE_REG_4,
921 (r << 16) | (g << 8) | b);
922
923 bus_space_write_stream_4(memt, memh, NGLE_REG_2, 0x400);
924 bus_space_write_stream_4(memt, memh, NGLE_REG_26, 0x80000100);
925 gftfb_setup_fb(sc);
926 return 0;
927 }
928
929 static inline void
930 gftfb_wait_fifo(struct gftfb_softc *sc, uint32_t slots)
931 {
932 struct sti_rom *rom = sc->sc_base.sc_rom;
933 bus_space_tag_t memt = rom->memt;
934 bus_space_handle_t memh = rom->regh[2];
935 uint32_t reg;
936
937 do {
938 reg = bus_space_read_stream_4(memt, memh, NGLE_REG_34);
939 } while (reg < slots);
940 }
941
942 static void
943 gftfb_rectfill(struct gftfb_softc *sc, int x, int y, int wi, int he,
944 uint32_t bg)
945 {
946 struct sti_rom *rom = sc->sc_base.sc_rom;
947 bus_space_tag_t memt = rom->memt;
948 bus_space_handle_t memh = rom->regh[2];
949
950 if (sc->sc_hwmode != HW_FILL) {
951 gftfb_wait_fifo(sc, 4);
952 /* transfer data */
953 bus_space_write_stream_4(memt, memh, NGLE_REG_8, 0xffffffff);
954 /* plane mask */
955 bus_space_write_stream_4(memt, memh, NGLE_REG_13, 0xff);
956 /* bitmap op */
957 bus_space_write_stream_4(memt, memh, NGLE_REG_14,
958 IBOvals(RopSrc, 0, BitmapExtent08, 0, DataDynamic, MaskOtc, 0, 0));
959 /* dst bitmap access */
960 bus_space_write_stream_4(memt, memh, NGLE_REG_11,
961 BA(IndexedDcd, Otc32, OtsIndirect, AddrLong, 0, BINapp0I, 0));
962 sc->sc_hwmode = HW_FILL;
963 }
964 gftfb_wait_fifo(sc, 3);
965 bus_space_write_stream_4(memt, memh, NGLE_REG_35, bg);
966 /* dst XY */
967 bus_space_write_stream_4(memt, memh, NGLE_REG_6, (x << 16) | y);
968 /* len XY start */
969 bus_space_write_stream_4(memt, memh, NGLE_REG_9, (wi << 16) | he);
970
971 }
972
973
974 static void
975 gftfb_bitblt(void *cookie, int xs, int ys, int xd, int yd, int wi,
976 int he, int rop)
977 {
978 struct gftfb_softc *sc = cookie;
979 struct sti_rom *rom = sc->sc_base.sc_rom;
980 bus_space_tag_t memt = rom->memt;
981 bus_space_handle_t memh = rom->regh[2];
982
983 if (sc->sc_hwmode != HW_BLIT) {
984 gftfb_wait(sc);
985 bus_space_write_stream_4(memt, memh, NGLE_REG_10, 0x13a01000);
986 sc->sc_hwmode = HW_BLIT;
987 }
988 gftfb_wait_fifo(sc, 5);
989 bus_space_write_stream_4(memt, memh, NGLE_REG_14, ((rop << 8) & 0xf00) | 0x23000000);
990 bus_space_write_stream_4(memt, memh, NGLE_REG_13, 0xff);
991 bus_space_write_stream_4(memt, memh, NGLE_REG_24, (xs << 16) | ys);
992 bus_space_write_stream_4(memt, memh, NGLE_REG_7, (wi << 16) | he);
993 bus_space_write_stream_4(memt, memh, NGLE_REG_25, (xd << 16) | yd);
994 }
995
996 static void
997 gftfb_nuke_cursor(struct rasops_info *ri)
998 {
999 struct vcons_screen *scr = ri->ri_hw;
1000 struct gftfb_softc *sc = scr->scr_cookie;
1001 int wi, he, x, y;
1002
1003 if (ri->ri_flg & RI_CURSOR) {
1004 wi = ri->ri_font->fontwidth;
1005 he = ri->ri_font->fontheight;
1006 x = ri->ri_ccol * wi + ri->ri_xorigin;
1007 y = ri->ri_crow * he + ri->ri_yorigin;
1008 gftfb_bitblt(sc, x, y, x, y, wi, he, RopInv);
1009 ri->ri_flg &= ~RI_CURSOR;
1010 }
1011 }
1012
1013 static void
1014 gftfb_cursor(void *cookie, int on, int row, int col)
1015 {
1016 struct rasops_info *ri = cookie;
1017 struct vcons_screen *scr = ri->ri_hw;
1018 struct gftfb_softc *sc = scr->scr_cookie;
1019 int x, y, wi, he;
1020
1021 wi = ri->ri_font->fontwidth;
1022 he = ri->ri_font->fontheight;
1023
1024 if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
1025 if (on) {
1026 if (ri->ri_flg & RI_CURSOR) {
1027 gftfb_nuke_cursor(ri);
1028 }
1029 x = col * wi + ri->ri_xorigin;
1030 y = row * he + ri->ri_yorigin;
1031 gftfb_bitblt(sc, x, y, x, y, wi, he, RopInv);
1032 ri->ri_flg |= RI_CURSOR;
1033 }
1034 ri->ri_crow = row;
1035 ri->ri_ccol = col;
1036 } else
1037 {
1038 ri->ri_crow = row;
1039 ri->ri_ccol = col;
1040 ri->ri_flg &= ~RI_CURSOR;
1041 }
1042
1043 }
1044
1045 static void
1046 gftfb_putchar(void *cookie, int row, int col, u_int c, long attr)
1047 {
1048 struct rasops_info *ri = cookie;
1049 struct wsdisplay_font *font = PICK_FONT(ri, c);
1050 struct vcons_screen *scr = ri->ri_hw;
1051 struct gftfb_softc *sc = scr->scr_cookie;
1052 int x, y, wi, he, rv = GC_NOPE;
1053 #if 0
1054 uint32_t bg;
1055 #endif
1056 if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL)
1057 return;
1058
1059 if (!CHAR_IN_FONT(c, font))
1060 return;
1061
1062 if (row == ri->ri_crow && col == ri->ri_ccol) {
1063 ri->ri_flg &= ~RI_CURSOR;
1064 }
1065
1066 wi = font->fontwidth;
1067 he = font->fontheight;
1068
1069 x = ri->ri_xorigin + col * wi;
1070 y = ri->ri_yorigin + row * he;
1071 #if 0
1072 bg = ri->ri_devcmap[(attr >> 16) & 0xf];
1073
1074 /* XXX
1075 * rectfill currently draws rectangles less than 32 pixels wide as
1076 * 32 pixels wide, no idea why. So until I figure that one out we
1077 * draw blanks by software
1078 * bitblt doesn't seem to have this problem
1079 */
1080 if (c == 0x20) {
1081 gftfb_rectfill(sc, x, y, wi, he, bg);
1082 return;
1083 }
1084 #endif
1085 rv = glyphcache_try(&sc->sc_gc, c, x, y, attr);
1086 if (rv == GC_OK)
1087 return;
1088
1089 if (sc->sc_hwmode != HW_FB) gftfb_setup_fb(sc);
1090 sc->sc_putchar(cookie, row, col, c, attr);
1091
1092 if (rv == GC_ADD)
1093 glyphcache_add(&sc->sc_gc, c, x, y);
1094 }
1095
1096 static void
1097 gftfb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
1098 {
1099 struct rasops_info *ri = cookie;
1100 struct vcons_screen *scr = ri->ri_hw;
1101 struct gftfb_softc *sc = scr->scr_cookie;
1102 int32_t xs, xd, y, width, height;
1103
1104 if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
1105 if (ri->ri_crow == row &&
1106 (ri->ri_ccol >= srccol && ri->ri_ccol < (srccol + ncols)) &&
1107 (ri->ri_flg & RI_CURSOR)) {
1108 gftfb_nuke_cursor(ri);
1109 }
1110
1111 xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
1112 xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
1113 y = ri->ri_yorigin + ri->ri_font->fontheight * row;
1114 width = ri->ri_font->fontwidth * ncols;
1115 height = ri->ri_font->fontheight;
1116 gftfb_bitblt(sc, xs, y, xd, y, width, height, RopSrc);
1117 if (ri->ri_crow == row &&
1118 (ri->ri_ccol >= dstcol && ri->ri_ccol < (dstcol + ncols)))
1119 ri->ri_flg &= ~RI_CURSOR;
1120 }
1121 }
1122
1123 static void
1124 gftfb_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
1125 {
1126 struct rasops_info *ri = cookie;
1127 struct vcons_screen *scr = ri->ri_hw;
1128 struct gftfb_softc *sc = scr->scr_cookie;
1129 int32_t x, y, width, height, fg, bg, ul;
1130
1131 if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
1132 x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
1133 y = ri->ri_yorigin + ri->ri_font->fontheight * row;
1134 width = ri->ri_font->fontwidth * ncols;
1135 height = ri->ri_font->fontheight;
1136 rasops_unpack_attr(fillattr, &fg, &bg, &ul);
1137
1138 gftfb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
1139 if (ri->ri_crow == row &&
1140 (ri->ri_ccol >= startcol && ri->ri_ccol < (startcol + ncols)))
1141 ri->ri_flg &= ~RI_CURSOR;
1142 }
1143 }
1144
1145 static void
1146 gftfb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
1147 {
1148 struct rasops_info *ri = cookie;
1149 struct vcons_screen *scr = ri->ri_hw;
1150 struct gftfb_softc *sc = scr->scr_cookie;
1151 int32_t x, ys, yd, width, height;
1152
1153 if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
1154 if ((ri->ri_crow >= srcrow && ri->ri_crow < (srcrow + nrows)) &&
1155 (ri->ri_flg & RI_CURSOR)) {
1156 gftfb_nuke_cursor(ri);
1157 }
1158 x = ri->ri_xorigin;
1159 ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
1160 yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
1161 width = ri->ri_emuwidth;
1162 height = ri->ri_font->fontheight * nrows;
1163 gftfb_bitblt(sc, x, ys, x, yd, width, height, RopSrc);
1164 if (ri->ri_crow >= dstrow && ri->ri_crow < (dstrow + nrows))
1165 ri->ri_flg &= ~RI_CURSOR;
1166 }
1167 }
1168
1169 static void
1170 gftfb_eraserows(void *cookie, int row, int nrows, long fillattr)
1171 {
1172 struct rasops_info *ri = cookie;
1173 struct vcons_screen *scr = ri->ri_hw;
1174 struct gftfb_softc *sc = scr->scr_cookie;
1175 int32_t x, y, width, height, fg, bg, ul;
1176
1177 if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
1178 x = ri->ri_xorigin;
1179 y = ri->ri_yorigin + ri->ri_font->fontheight * row;
1180 width = ri->ri_emuwidth;
1181 height = ri->ri_font->fontheight * nrows;
1182 rasops_unpack_attr(fillattr, &fg, &bg, &ul);
1183
1184 gftfb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
1185
1186 if (ri->ri_crow >= row && ri->ri_crow < (row + nrows))
1187 ri->ri_flg &= ~RI_CURSOR;
1188 }
1189 }
1190