chipsfb.c revision 1.2 1 /* $NetBSD: chipsfb.c,v 1.2 2006/09/27 05:19:23 macallan Exp $ */
2
3 /*
4 * Copyright (c) 2006 Michael Lorenz
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 /*
31 * A console driver for Chips & Technologies 65550 graphics controllers
32 */
33
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: chipsfb.c,v 1.2 2006/09/27 05:19:23 macallan Exp $");
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/device.h>
41 #include <sys/malloc.h>
42 #include <sys/callout.h>
43 #include <sys/lwp.h>
44 #include <sys/kauth.h>
45
46 #include <uvm/uvm_extern.h>
47
48 #if defined(macppc) || defined (sparc64) || defined(ofppc)
49 #define HAVE_OPENFIRMWARE
50 #endif
51
52 #ifdef HAVE_OPENFIRMWARE
53 #include <dev/ofw/openfirm.h>
54 #include <dev/ofw/ofw_pci.h>
55 #endif
56
57 #include <dev/videomode/videomode.h>
58
59 #include <dev/pci/pcivar.h>
60 #include <dev/pci/pcireg.h>
61 #include <dev/pci/pcidevs.h>
62 #include <dev/pci/pciio.h>
63 #include <dev/pci/chipsfbreg.h>
64
65 #include <dev/wscons/wsdisplayvar.h>
66 #include <dev/wscons/wsconsio.h>
67 #include <dev/wsfont/wsfont.h>
68 #include <dev/rasops/rasops.h>
69 #include <dev/wscons/wsdisplay_vconsvar.h>
70
71 #include <dev/i2c/i2cvar.h>
72 #include <dev/i2c/i2c_bitbang.h>
73 #include <dev/i2c/edidvar.h>
74
75 #include "opt_wsemul.h"
76
77 struct chipsfb_softc {
78 struct device sc_dev;
79 pci_chipset_tag_t sc_pc;
80 pcitag_t sc_pcitag;
81
82 bus_space_tag_t sc_memt;
83 bus_space_tag_t sc_iot;
84 bus_space_handle_t sc_memh;
85
86 bus_space_tag_t sc_fbt;
87 bus_space_tag_t sc_ioregt;
88 bus_space_handle_t sc_fbh;
89 bus_space_handle_t sc_ioregh;
90 bus_addr_t sc_fb, sc_ioreg;
91 bus_size_t sc_fbsize, sc_ioregsize;
92
93 void *sc_ih;
94
95 size_t memsize;
96
97 int bits_per_pixel;
98 int width, height, linebytes;
99
100 int sc_mode;
101 uint32_t sc_bg;
102
103 u_char sc_cmap_red[256];
104 u_char sc_cmap_green[256];
105 u_char sc_cmap_blue[256];
106 int sc_dacw;
107
108 /* I2C stuff */
109 struct i2c_controller sc_i2c;
110 uint8_t sc_edid[1024];
111 int sc_edidbytes; /* number of bytes read from the monitor */
112
113 struct vcons_data vd;
114 };
115
116 static struct vcons_screen chipsfb_console_screen;
117
118 extern const u_char rasops_cmap[768];
119
120 static int chipsfb_match(struct device *, struct cfdata *, void *);
121 static void chipsfb_attach(struct device *, struct device *, void *);
122
123 CFATTACH_DECL(chipsfb, sizeof(struct chipsfb_softc), chipsfb_match,
124 chipsfb_attach, NULL, NULL);
125
126 static int chipsfb_is_console(struct pci_attach_args *);
127 static void chipsfb_init(struct chipsfb_softc *);
128
129 static void chipsfb_cursor(void *, int, int, int);
130 static void chipsfb_copycols(void *, int, int, int, int);
131 static void chipsfb_erasecols(void *, int, int, int, long);
132 static void chipsfb_copyrows(void *, int, int, int);
133 static void chipsfb_eraserows(void *, int, int, long);
134
135 #if 0
136 static int chipsfb_allocattr(void *, int, int, int, long *);
137 static void chipsfb_scroll(void *, void *, int);
138 static int chipsfb_load_font(void *, void *, struct wsdisplay_font *);
139 #endif
140
141 static int chipsfb_putcmap(struct chipsfb_softc *,
142 struct wsdisplay_cmap *);
143 static int chipsfb_getcmap(struct chipsfb_softc *,
144 struct wsdisplay_cmap *);
145 static int chipsfb_putpalreg(struct chipsfb_softc *, uint8_t, uint8_t,
146 uint8_t, uint8_t);
147
148 static void chipsfb_bitblt(struct chipsfb_softc *, int, int, int, int,
149 int, int, uint8_t);
150 static void chipsfb_rectfill(struct chipsfb_softc *, int, int, int, int,
151 int);
152 static void chipsfb_putchar(void *, int, int, u_int, long);
153 static void chipsfb_setup_mono(struct chipsfb_softc *, int, int, int,
154 int, uint32_t, uint32_t);
155 static void chipsfb_feed(struct chipsfb_softc *, int, uint8_t *);
156
157 #ifdef chipsfb_DEBUG
158 static void chipsfb_showpal(struct chipsfb_softc *);
159 #endif
160 static void chipsfb_restore_palette(struct chipsfb_softc *);
161
162 static void chipsfb_wait_idle(struct chipsfb_softc *);
163
164 struct wsscreen_descr chipsfb_defaultscreen = {
165 "default",
166 0, 0,
167 NULL,
168 8, 16,
169 WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
170 };
171
172 const struct wsscreen_descr *_chipsfb_scrlist[] = {
173 &chipsfb_defaultscreen,
174 /* XXX other formats, graphics screen? */
175 };
176
177 struct wsscreen_list chipsfb_screenlist = {
178 sizeof(_chipsfb_scrlist) / sizeof(struct wsscreen_descr *), _chipsfb_scrlist
179 };
180
181 static int chipsfb_ioctl(void *, void *, u_long, caddr_t, int,
182 struct lwp *);
183 static paddr_t chipsfb_mmap(void *, void *, off_t, int);
184 static void chipsfb_clearscreen(struct chipsfb_softc *);
185 static void chipsfb_init_screen(void *, struct vcons_screen *, int,
186 long *);
187
188
189 struct wsdisplay_accessops chipsfb_accessops = {
190 chipsfb_ioctl,
191 chipsfb_mmap,
192 NULL, /* load_font */
193 NULL, /* polls */
194 NULL, /* scroll */
195 };
196
197 /*
198 * Inline functions for getting access to register aperture.
199 */
200 static inline void
201 chipsfb_write32(struct chipsfb_softc *sc, uint32_t reg, uint32_t val)
202 {
203 bus_space_write_4(sc->sc_fbt, sc->sc_fbh, reg, val);
204 }
205
206 static inline uint32_t
207 chipsfb_read32(struct chipsfb_softc *sc, uint32_t reg)
208 {
209 return bus_space_read_4(sc->sc_fbt, sc->sc_fbh, reg);
210 }
211
212 static inline void
213 chipsfb_write_vga(struct chipsfb_softc *sc, uint32_t reg, uint8_t val)
214 {
215 bus_space_write_1(sc->sc_iot, sc->sc_ioregh, reg, val);
216 }
217
218 static inline uint8_t
219 chipsfb_read_vga(struct chipsfb_softc *sc, uint32_t reg)
220 {
221 return bus_space_read_1(sc->sc_iot, sc->sc_ioregh, reg);
222 }
223
224 static inline uint8_t
225 chipsfb_read_indexed(struct chipsfb_softc *sc, uint32_t reg, uint8_t index)
226 {
227
228 chipsfb_write_vga(sc, reg & 0xfffe, index);
229 return chipsfb_read_vga(sc, reg | 0x0001);
230 }
231
232 static inline void
233 chipsfb_write_indexed(struct chipsfb_softc *sc, uint32_t reg, uint8_t index,
234 uint8_t val)
235 {
236
237 chipsfb_write_vga(sc, reg & 0xfffe, index);
238 chipsfb_write_vga(sc, reg | 0x0001, val);
239 }
240
241 static void
242 chipsfb_wait_idle(struct chipsfb_softc *sc)
243 {
244
245 /* spin until the blitter is idle */
246 while ((chipsfb_read32(sc, CT_BLT_CONTROL) & BLT_IS_BUSY) != 0) {
247 delay(1);
248 }
249 }
250
251 static int
252 chipsfb_match(struct device *parent, struct cfdata *match, void *aux)
253 {
254 struct pci_attach_args *pa = (struct pci_attach_args *)aux;
255
256 if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY ||
257 PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_DISPLAY_VGA)
258 return 0;
259 if ((PCI_VENDOR(pa->pa_id) == PCI_VENDOR_CHIPS) &&
260 (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_CHIPS_65550))
261 return 100;
262 return 0;
263 }
264
265 static void
266 chipsfb_attach(struct device *parent, struct device *self, void *aux)
267 {
268 struct chipsfb_softc *sc = (void *)self;
269 struct pci_attach_args *pa = aux;
270 char devinfo[256];
271 struct wsemuldisplaydev_attach_args aa;
272 struct rasops_info *ri;
273 pcireg_t screg;
274 ulong defattr;
275 int console, width, height, node, i, j;
276 #ifdef HAVE_OPENFIRMWARE
277 int linebytes, depth;
278 #endif
279 uint32_t bg, fg, ul;
280
281 sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
282 node = pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
283 sc->sc_pc = pa->pa_pc;
284 sc->sc_pcitag = pa->pa_tag;
285 sc->sc_dacw = -1;
286
287 screg = pci_conf_read(sc->sc_pc, sc->sc_pcitag, PCI_COMMAND_STATUS_REG);
288 screg |= PCI_FLAGS_IO_ENABLED | PCI_FLAGS_MEM_ENABLED;
289 pci_conf_write(sc->sc_pc, sc->sc_pcitag,PCI_COMMAND_STATUS_REG,screg);
290
291 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
292 printf(": %s (rev. 0x%02x)\n", devinfo, PCI_REVISION(pa->pa_class));
293
294 sc->sc_memt = pa->pa_memt;
295 sc->sc_iot = pa->pa_iot;
296
297 /* the framebuffer */
298 if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_MEM,
299 BUS_SPACE_MAP_LINEAR,
300 &sc->sc_fbt, &sc->sc_fbh, &sc->sc_fb, &sc->sc_fbsize)) {
301 printf("%s: failed to map the frame buffer.\n",
302 sc->sc_dev.dv_xname);
303 }
304
305 /* IO-mapped registers */
306 if (bus_space_map(sc->sc_iot, 0x0, PAGE_SIZE, 0, &sc->sc_ioregh) != 0) {
307 printf("%s: failed to map IO-mapped registers.\n",
308 sc->sc_dev.dv_xname);
309 }
310
311 chipsfb_init(sc);
312
313 /* we should read these from the chip instead of depending on OF */
314 width = height = -1;
315
316 /* detect panel size */
317 width = chipsfb_read_indexed(sc, CT_FP_INDEX, FP_HSIZE_LSB);
318 width |= (chipsfb_read_indexed(sc, CT_FP_INDEX, FP_HORZ_OVERFLOW_1)
319 & 0x0f) << 8;
320 width = (width + 1) * 8;
321 height = chipsfb_read_indexed(sc, CT_FP_INDEX, FP_VSIZE_LSB);
322 height |= (chipsfb_read_indexed(sc, CT_FP_INDEX, FP_VERT_OVERFLOW_1)
323 & 0x0f) << 8;
324 height++;
325 printf("Panel size: %d x %d\n", width, height);
326
327 #ifdef HAVE_OPENFIRMWARE
328 if (OF_getprop(node, "width", &width, 4) != 4)
329 OF_interpret("screen-width", 1, &width);
330 if (OF_getprop(node, "height", &height, 4) != 4)
331 OF_interpret("screen-height", 1, &height);
332 if (OF_getprop(node, "linebytes", &linebytes, 4) != 4)
333 linebytes = width; /* XXX */
334 if (OF_getprop(node, "depth", &depth, 4) != 4)
335 depth = 8; /* XXX */
336
337 if (width == -1 || height == -1)
338 return;
339
340 sc->width = width;
341 sc->height = height;
342 sc->bits_per_pixel = depth;
343 sc->linebytes = linebytes;
344 printf("%s: initial resolution %dx%d, %d bit\n", sc->sc_dev.dv_xname,
345 sc->width, sc->height, sc->bits_per_pixel);
346 #endif
347
348 #ifdef notyet
349 /* XXX this should at least be configurable via kernel config */
350 chipsfb_set_videomode(sc, &videomode_list[16]);
351 #endif
352
353 vcons_init(&sc->vd, sc, &chipsfb_defaultscreen, &chipsfb_accessops);
354 sc->vd.init_screen = chipsfb_init_screen;
355
356 console = chipsfb_is_console(pa);
357
358 ri = &chipsfb_console_screen.scr_ri;
359 if (console) {
360 vcons_init_screen(&sc->vd, &chipsfb_console_screen, 1,
361 &defattr);
362 chipsfb_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
363
364 chipsfb_defaultscreen.textops = &ri->ri_ops;
365 chipsfb_defaultscreen.capabilities = ri->ri_caps;
366 chipsfb_defaultscreen.nrows = ri->ri_rows;
367 chipsfb_defaultscreen.ncols = ri->ri_cols;
368 wsdisplay_cnattach(&chipsfb_defaultscreen, ri, 0, 0, defattr);
369 } else {
370 /*
371 * since we're not the console we can postpone the rest
372 * until someone actually allocates a screen for us
373 */
374 #ifdef notyet
375 chipsfb_set_videomode(sc, &videomode_list[0]);
376 #endif
377 }
378
379 rasops_unpack_attr(defattr, &fg, &bg, &ul);
380 sc->sc_bg = ri->ri_devcmap[bg];
381 chipsfb_clearscreen(sc);
382
383 printf("%s: %d MB aperture at 0x%08x\n",
384 sc->sc_dev.dv_xname, (u_int)(sc->sc_fbsize >> 20),
385 (u_int)sc->sc_fb);
386 #ifdef chipsfb_DEBUG
387 printf("fb: %08lx\n", (ulong)ri->ri_bits);
388 #endif
389
390 j = 0;
391 for (i = 0; i < 256; i++) {
392 chipsfb_putpalreg(sc, i, rasops_cmap[j], rasops_cmap[j + 1],
393 rasops_cmap[j + 2]);
394 j += 3;
395 }
396
397 aa.console = console;
398 aa.scrdata = &chipsfb_screenlist;
399 aa.accessops = &chipsfb_accessops;
400 aa.accesscookie = &sc->vd;
401
402 config_found(self, &aa, wsemuldisplaydevprint);
403 }
404
405 static int
406 chipsfb_putpalreg(struct chipsfb_softc *sc, uint8_t index, uint8_t r,
407 uint8_t g, uint8_t b)
408 {
409
410 sc->sc_cmap_red[index] = r;
411 sc->sc_cmap_green[index] = g;
412 sc->sc_cmap_blue[index] = b;
413
414 chipsfb_write_vga(sc, CT_DACMASK, 0xff);
415 chipsfb_write_vga(sc, CT_WRITEINDEX, index);
416 chipsfb_write_vga(sc, CT_DACDATA, r);
417 chipsfb_write_vga(sc, CT_DACDATA, g);
418 chipsfb_write_vga(sc, CT_DACDATA, b);
419
420 return 0;
421 }
422
423 static int
424 chipsfb_putcmap(struct chipsfb_softc *sc, struct wsdisplay_cmap *cm)
425 {
426 u_char *r, *g, *b;
427 u_int index = cm->index;
428 u_int count = cm->count;
429 int i, error;
430 u_char rbuf[256], gbuf[256], bbuf[256];
431
432 #ifdef chipsfb_DEBUG
433 printf("putcmap: %d %d\n",index, count);
434 #endif
435 if (cm->index >= 256 || cm->count > 256 ||
436 (cm->index + cm->count) > 256)
437 return EINVAL;
438 error = copyin(cm->red, &rbuf[index], count);
439 if (error)
440 return error;
441 error = copyin(cm->green, &gbuf[index], count);
442 if (error)
443 return error;
444 error = copyin(cm->blue, &bbuf[index], count);
445 if (error)
446 return error;
447
448 memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
449 memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
450 memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
451
452 r = &sc->sc_cmap_red[index];
453 g = &sc->sc_cmap_green[index];
454 b = &sc->sc_cmap_blue[index];
455
456 for (i = 0; i < count; i++) {
457 chipsfb_putpalreg(sc, index, *r, *g, *b);
458 index++;
459 r++, g++, b++;
460 }
461 return 0;
462 }
463
464 static int
465 chipsfb_getcmap(struct chipsfb_softc *sc, struct wsdisplay_cmap *cm)
466 {
467 u_int index = cm->index;
468 u_int count = cm->count;
469 int error;
470
471 if (index >= 255 || count > 256 || index + count > 256)
472 return EINVAL;
473
474 error = copyout(&sc->sc_cmap_red[index], cm->red, count);
475 if (error)
476 return error;
477 error = copyout(&sc->sc_cmap_green[index], cm->green, count);
478 if (error)
479 return error;
480 error = copyout(&sc->sc_cmap_blue[index], cm->blue, count);
481 if (error)
482 return error;
483
484 return 0;
485 }
486
487 static int
488 chipsfb_is_console(struct pci_attach_args *pa)
489 {
490
491 #ifdef HAVE_OPENFIRMWARE
492 /* check if we're the /chosen console device */
493 int chosen, stdout, node, us;
494
495 us = pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
496 chosen = OF_finddevice("/chosen");
497 OF_getprop(chosen, "stdout", &stdout, 4);
498 node = OF_instance_to_package(stdout);
499 return(us == node);
500 #else
501 /* XXX how do we know we're console on i386? */
502 return 1;
503 #endif
504 }
505
506 static void
507 chipsfb_clearscreen(struct chipsfb_softc *sc)
508 {
509 chipsfb_rectfill(sc, 0, 0, sc->width, sc->height, sc->sc_bg);
510 }
511
512 /*
513 * wsdisplay_emulops
514 */
515
516 static void
517 chipsfb_cursor(void *cookie, int on, int row, int col)
518 {
519 struct rasops_info *ri = cookie;
520 struct vcons_screen *scr = ri->ri_hw;
521 struct chipsfb_softc *sc = scr->scr_cookie;
522 int x, y, wi, he;
523
524 wi = ri->ri_font->fontwidth;
525 he = ri->ri_font->fontheight;
526
527 if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
528 x = ri->ri_ccol * wi + ri->ri_xorigin;
529 y = ri->ri_crow * he + ri->ri_yorigin;
530 if (ri->ri_flg & RI_CURSOR) {
531 chipsfb_bitblt(sc, x, y, x, y, wi, he, ROP_NOT_DST);
532 ri->ri_flg &= ~RI_CURSOR;
533 }
534 ri->ri_crow = row;
535 ri->ri_ccol = col;
536 if (on) {
537 x = ri->ri_ccol * wi + ri->ri_xorigin;
538 y = ri->ri_crow * he + ri->ri_yorigin;
539 chipsfb_bitblt(sc, x, y, x, y, wi, he, ROP_NOT_DST);
540 ri->ri_flg |= RI_CURSOR;
541 }
542 } else {
543 ri->ri_flg &= ~RI_CURSOR;
544 ri->ri_crow = row;
545 ri->ri_ccol = col;
546 }
547 }
548
549 #if 0
550 int
551 chipsfb_mapchar(void *cookie, int uni, u_int *index)
552 {
553 return 0;
554 }
555 #endif
556
557 static void
558 chipsfb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
559 {
560 struct rasops_info *ri = cookie;
561 struct vcons_screen *scr = ri->ri_hw;
562 struct chipsfb_softc *sc = scr->scr_cookie;
563 int32_t xs, xd, y, width, height;
564
565 if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
566 xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
567 xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
568 y = ri->ri_yorigin + ri->ri_font->fontheight * row;
569 width = ri->ri_font->fontwidth * ncols;
570 height = ri->ri_font->fontheight;
571 chipsfb_bitblt(sc, xs, y, xd, y, width, height, ROP_COPY);
572 }
573 }
574
575 static void
576 chipsfb_erasecols(void *cookie, int row, int startcol, int ncols,
577 long fillattr)
578 {
579 struct rasops_info *ri = cookie;
580 struct vcons_screen *scr = ri->ri_hw;
581 struct chipsfb_softc *sc = scr->scr_cookie;
582 int32_t x, y, width, height, fg, bg, ul;
583
584 if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
585 x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
586 y = ri->ri_yorigin + ri->ri_font->fontheight * row;
587 width = ri->ri_font->fontwidth * ncols;
588 height = ri->ri_font->fontheight;
589 rasops_unpack_attr(fillattr, &fg, &bg, &ul);
590
591 chipsfb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
592 }
593 }
594
595 static void
596 chipsfb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
597 {
598 struct rasops_info *ri = cookie;
599 struct vcons_screen *scr = ri->ri_hw;
600 struct chipsfb_softc *sc = scr->scr_cookie;
601 int32_t x, ys, yd, width, height;
602
603 if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
604 x = ri->ri_xorigin;
605 ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
606 yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
607 width = ri->ri_emuwidth;
608 height = ri->ri_font->fontheight * nrows;
609 chipsfb_bitblt(sc, x, ys, x, yd, width, height, ROP_COPY);
610 }
611 }
612
613 static void
614 chipsfb_eraserows(void *cookie, int row, int nrows, long fillattr)
615 {
616 struct rasops_info *ri = cookie;
617 struct vcons_screen *scr = ri->ri_hw;
618 struct chipsfb_softc *sc = scr->scr_cookie;
619 int32_t x, y, width, height, fg, bg, ul;
620
621 if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
622 rasops_unpack_attr(fillattr, &fg, &bg, &ul);
623 if ((row == 0) && (nrows == ri->ri_rows)) {
624 /* clear the whole screen */
625 chipsfb_rectfill(sc, 0, 0, ri->ri_width,
626 ri->ri_height, ri->ri_devcmap[bg]);
627 } else {
628 x = ri->ri_xorigin;
629 y = ri->ri_yorigin + ri->ri_font->fontheight * row;
630 width = ri->ri_emuwidth;
631 height = ri->ri_font->fontheight * nrows;
632 chipsfb_rectfill(sc, x, y, width, height,
633 ri->ri_devcmap[bg]);
634 }
635 }
636 }
637
638 static void
639 chipsfb_bitblt(struct chipsfb_softc *sc, int xs, int ys, int xd, int yd,
640 int width, int height, uint8_t rop)
641 {
642 uint32_t src, dst, cmd = rop, stride, size;
643
644 cmd |= BLT_PAT_IS_SOLID;
645
646 /* we assume 8 bit for now */
647 src = xs + ys * sc->linebytes;
648 dst = xd + yd * sc->linebytes;
649
650 if (xs < xd) {
651 /* right-to-left operation */
652 cmd |= BLT_START_RIGHT;
653 src += sc->linebytes - 1;
654 dst += sc->linebytes - 1;
655 }
656
657 if (ys < yd) {
658 /* bottom-to-top operation */
659 cmd |= BLT_START_BOTTOM;
660 src += (height - 1) * sc->linebytes;
661 dst += (height - 1) * sc->linebytes;
662 }
663
664 stride = (sc->linebytes << 16) | sc->linebytes;
665 size = (height << 16) | width;
666
667 chipsfb_wait_idle(sc);
668 chipsfb_write32(sc, CT_BLT_STRIDE, stride);
669 chipsfb_write32(sc, CT_BLT_SRCADDR, src);
670 chipsfb_write32(sc, CT_BLT_DSTADDR, dst);
671 chipsfb_write32(sc, CT_BLT_CONTROL, cmd);
672 chipsfb_write32(sc, CT_BLT_SIZE, size);
673 #ifdef CHIPSFB_WAIT
674 chipsfb_wait_idle(sc);
675 #endif
676 }
677
678 static void
679 chipsfb_rectfill(struct chipsfb_softc *sc, int x, int y, int width,
680 int height, int colour)
681 {
682 uint32_t dst, cmd, stride, size;
683
684 cmd = BLT_PAT_IS_SOLID | BLT_PAT_IS_MONO | ROP_PAT;
685
686 /* we assume 8 bit for now */
687 dst = x + y * sc->linebytes;
688
689 stride = (sc->linebytes << 16) | sc->linebytes;
690 size = (height << 16) | width;
691
692 chipsfb_wait_idle(sc);
693 chipsfb_write32(sc, CT_BLT_STRIDE, stride);
694 chipsfb_write32(sc, CT_BLT_SRCADDR, dst);
695 chipsfb_write32(sc, CT_BLT_DSTADDR, dst);
696 chipsfb_write32(sc, CT_BLT_CONTROL, cmd);
697 chipsfb_write32(sc, CT_BLT_BG, colour);
698 chipsfb_write32(sc, CT_BLT_FG, colour);
699 chipsfb_write32(sc, CT_BLT_SIZE, size);
700 #ifdef CHIPSFB_WAIT
701 chipsfb_wait_idle(sc);
702 #endif
703 }
704
705 static void
706 chipsfb_putchar(void *cookie, int row, int col, u_int c, long attr)
707 {
708 struct rasops_info *ri = cookie;
709 struct vcons_screen *scr = ri->ri_hw;
710 struct chipsfb_softc *sc = scr->scr_cookie;
711
712 if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
713 uint8_t *data;
714 int fg, bg, uc;
715 int x, y, wi, he;
716
717 wi = ri->ri_font->fontwidth;
718 he = ri->ri_font->fontheight;
719
720 if (!CHAR_IN_FONT(c, ri->ri_font))
721 return;
722 bg = (u_char)ri->ri_devcmap[(attr >> 16) & 0xf];
723 fg = (u_char)ri->ri_devcmap[(attr >> 24) & 0xf];
724 x = ri->ri_xorigin + col * wi;
725 y = ri->ri_yorigin + row * he;
726 if (c == 0x20) {
727 chipsfb_rectfill(sc, x, y, wi, he, bg);
728 } else {
729 uc = c-ri->ri_font->firstchar;
730 data = (uint8_t *)ri->ri_font->data + uc *
731 ri->ri_fontscale;
732 chipsfb_setup_mono(sc, x, y, wi, he, fg, bg);
733 chipsfb_feed(sc, ri->ri_font->stride * he, data);
734 }
735 }
736 }
737
738 static void
739 chipsfb_setup_mono(struct chipsfb_softc *sc, int xd, int yd, int width,
740 int height, uint32_t fg, uint32_t bg)
741 {
742 uint32_t dst, cmd, stride, size;
743
744 cmd = BLT_PAT_IS_SOLID | BLT_SRC_IS_CPU | BLT_SRC_IS_MONO | ROP_COPY;
745
746 /* we assume 8 bit for now */
747 dst = xd + yd * sc->linebytes;
748
749 stride = (sc->linebytes << 16);
750 size = (height << 16) | width;
751
752 chipsfb_wait_idle(sc);
753 chipsfb_write32(sc, CT_BLT_STRIDE, stride);
754 chipsfb_write32(sc, CT_BLT_EXPCTL, MONO_SRC_ALIGN_BYTE);
755 chipsfb_write32(sc, CT_BLT_DSTADDR, dst);
756 chipsfb_write32(sc, CT_BLT_SRCADDR, 0);
757 chipsfb_write32(sc, CT_BLT_CONTROL, cmd);
758 chipsfb_write32(sc, CT_BLT_BG, bg);
759 chipsfb_write32(sc, CT_BLT_FG, fg);
760 chipsfb_write32(sc, CT_BLT_SIZE, size);
761
762 }
763
764 static void
765 chipsfb_feed(struct chipsfb_softc *sc, int count, uint8_t *data)
766 {
767 int i;
768 uint32_t latch = 0, bork;
769 int shift = 0;
770
771 for (i = 0; i < count; i++) {
772 bork = data[i];
773 latch |= (bork << shift);
774 if (shift == 24) {
775 chipsfb_write32(sc, CT_OFF_DATA, latch);
776 latch = 0;
777 shift = 0;
778 } else
779 shift += 8;
780 }
781 if (shift != 0) {
782 chipsfb_write32(sc, CT_OFF_DATA, latch);
783 } else {
784 /*
785 * sometimes the chip just sits here waiting for more data -
786 * sending one more word gets it going again
787 */
788 chipsfb_write32(sc, CT_OFF_DATA, 0);
789 }
790 #ifdef CHIPSFB_WAIT
791 chipsfb_wait_idle(sc);
792 #endif
793 }
794
795 #ifdef CHIPSFB_DEBUG
796 static void
797 chipsfb_showpal(struct chipsfb_softc *sc)
798 {
799 int i, x = 0;
800
801 for (i = 0; i < 16; i++) {
802 chipsfb_rectfill(sc, x, 0, 64, 64, i);
803 x += 64;
804 }
805 }
806 #endif
807
808 #if 0
809 static int
810 chipsfb_allocattr(void *cookie, int fg, int bg, int flags, long *attrp)
811 {
812
813 return 0;
814 }
815 #endif
816
817 static void
818 chipsfb_restore_palette(struct chipsfb_softc *sc)
819 {
820 int i;
821
822 for (i = 0; i < 256; i++) {
823 chipsfb_putpalreg(sc,
824 i,
825 sc->sc_cmap_red[i],
826 sc->sc_cmap_green[i],
827 sc->sc_cmap_blue[i]);
828 }
829 }
830
831 /*
832 * wsdisplay_accessops
833 */
834
835 static int
836 chipsfb_ioctl(void *v, void *vs, u_long cmd, caddr_t data, int flag,
837 struct lwp *l)
838 {
839 struct vcons_data *vd = v;
840 struct chipsfb_softc *sc = vd->cookie;
841 struct wsdisplay_fbinfo *wdf;
842 struct vcons_screen *ms = vd->active;
843
844 switch (cmd) {
845 case WSDISPLAYIO_GTYPE:
846 *(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
847 return 0;
848
849 case WSDISPLAYIO_GINFO:
850 wdf = (void *)data;
851 wdf->height = ms->scr_ri.ri_height;
852 wdf->width = ms->scr_ri.ri_width;
853 wdf->depth = ms->scr_ri.ri_depth;
854 wdf->cmsize = 256;
855 return 0;
856
857 case WSDISPLAYIO_GETCMAP:
858 return chipsfb_getcmap(sc,
859 (struct wsdisplay_cmap *)data);
860
861 case WSDISPLAYIO_PUTCMAP:
862 return chipsfb_putcmap(sc,
863 (struct wsdisplay_cmap *)data);
864
865 /* PCI config read/write passthrough. */
866 case PCI_IOC_CFGREAD:
867 case PCI_IOC_CFGWRITE:
868 return (pci_devioctl(sc->sc_pc, sc->sc_pcitag,
869 cmd, data, flag, l));
870
871 case WSDISPLAYIO_SMODE:
872 {
873 int new_mode = *(int*)data;
874 if (new_mode != sc->sc_mode) {
875 sc->sc_mode = new_mode;
876 if(new_mode == WSDISPLAYIO_MODE_EMUL) {
877 chipsfb_restore_palette(sc);
878 vcons_redraw_screen(ms);
879 }
880 }
881 }
882 return 0;
883 }
884 return EPASSTHROUGH;
885 }
886
887 static paddr_t
888 chipsfb_mmap(void *v, void *vs, off_t offset, int prot)
889 {
890 struct vcons_data *vd = v;
891 struct chipsfb_softc *sc = vd->cookie;
892 struct lwp *me;
893 paddr_t pa;
894
895 /* 'regular' framebuffer mmap()ing */
896 if (offset < sc->sc_fbsize) {
897 pa = bus_space_mmap(sc->sc_fbt, offset, 0, prot,
898 BUS_SPACE_MAP_LINEAR);
899 return pa;
900 }
901
902 /*
903 * restrict all other mappings to processes with superuser privileges
904 * or the kernel itself
905 */
906 me = curlwp;
907 if (me != NULL) {
908 if (kauth_authorize_generic(me->l_cred, KAUTH_GENERIC_ISSUSER,
909 NULL) != 0) {
910 printf("%s: mmap() rejected.\n", sc->sc_dev.dv_xname);
911 return -1;
912 }
913 }
914
915 if ((offset >= sc->sc_fb) && (offset < (sc->sc_fb + sc->sc_fbsize))) {
916 pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
917 BUS_SPACE_MAP_LINEAR);
918 return pa;
919 }
920
921 #ifdef macppc
922 /* allow mapping of IO space */
923 if ((offset >= 0xf2000000) && (offset < 0xf2800000)) {
924 pa = bus_space_mmap(sc->sc_iot, offset - 0xf2000000, 0, prot,
925 BUS_SPACE_MAP_LINEAR);
926 return pa;
927 }
928 #endif
929
930 #ifdef OFB_ALLOW_OTHERS
931 if (offset >= 0x80000000) {
932 pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
933 BUS_SPACE_MAP_LINEAR);
934 return pa;
935 }
936 #endif
937 return -1;
938 }
939
940 static void
941 chipsfb_init_screen(void *cookie, struct vcons_screen *scr,
942 int existing, long *defattr)
943 {
944 struct chipsfb_softc *sc = cookie;
945 struct rasops_info *ri = &scr->scr_ri;
946
947 ri->ri_depth = sc->bits_per_pixel;
948 ri->ri_width = sc->width;
949 ri->ri_height = sc->height;
950 ri->ri_stride = sc->width;
951 ri->ri_flg = RI_CENTER | RI_FULLCLEAR;
952
953 ri->ri_bits = bus_space_vaddr(sc->sc_fbt, sc->sc_fbh);
954
955 #ifdef CHIPSFB_DEBUG
956 printf("addr: %08lx\n", (ulong)ri->ri_bits);
957 #endif
958 if (existing) {
959 ri->ri_flg |= RI_CLEAR;
960 }
961
962 rasops_init(ri, sc->height/8, sc->width/8);
963 ri->ri_caps = WSSCREEN_WSCOLORS;
964
965 rasops_reconfig(ri, sc->height / ri->ri_font->fontheight,
966 sc->width / ri->ri_font->fontwidth);
967
968 ri->ri_hw = scr;
969 ri->ri_ops.copyrows = chipsfb_copyrows;
970 ri->ri_ops.copycols = chipsfb_copycols;
971 ri->ri_ops.eraserows = chipsfb_eraserows;
972 ri->ri_ops.erasecols = chipsfb_erasecols;
973 ri->ri_ops.cursor = chipsfb_cursor;
974 ri->ri_ops.putchar = chipsfb_putchar;
975 }
976
977 #if 0
978 int
979 chipsfb_load_font(void *v, void *cookie, struct wsdisplay_font *data)
980 {
981
982 return 0;
983 }
984 #endif
985
986 static void
987 chipsfb_init(struct chipsfb_softc *sc)
988 {
989
990 chipsfb_wait_idle(sc);
991
992 chipsfb_write_indexed(sc, CT_CONF_INDEX, XR_IO_CONTROL,
993 ENABLE_CRTC_EXT | ENABLE_ATTR_EXT);
994 chipsfb_write_indexed(sc, CT_CONF_INDEX, XR_ADDR_MAPPING,
995 ENABLE_LINEAR);
996
997 /* setup the blitter */
998 }
999