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