1/*	$NetBSD: lynxfb.c,v 1.7 2021/08/07 16:19:14 thorpej Exp $	*/
2/*	$OpenBSD: smfb.c,v 1.13 2011/07/21 20:36:12 miod Exp $	*/
3
4/*
5 * Copyright (c) 2012 NONAKA Kimihiro <nonaka@netbsd.org>
6 * Copyright (c) 2009, 2010 Miodrag Vallat.
7 *
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 */
20
21/*
22 * SiliconMotion SM712 frame buffer driver.
23 *
24 * Assumes its video output is an LCD panel, in 5:6:5 mode, and fixed
25 * 1024x600 resolution, depending on the system model.
26 */
27
28#include <sys/cdefs.h>
29__KERNEL_RCSID(0, "$NetBSD: lynxfb.c,v 1.7 2021/08/07 16:19:14 thorpej Exp $");
30
31#include "opt_wsemul.h"
32
33#include <sys/param.h>
34#include <sys/systm.h>
35#include <sys/device.h>
36#include <sys/endian.h>
37#include <sys/kauth.h>
38
39#include <sys/bus.h>
40
41#include <dev/ic/vgareg.h>
42#include <dev/isa/isareg.h>
43#include <dev/pci/pcireg.h>
44#include <dev/pci/pcivar.h>
45#include <dev/pci/pcidevs.h>
46#include <dev/pci/pciio.h>
47
48#include <dev/wscons/wsconsio.h>
49#include <dev/wscons/wsdisplayvar.h>
50#include <dev/rasops/rasops.h>
51#include <dev/wscons/wsdisplay_vconsvar.h>
52#include <dev/pci/wsdisplay_pci.h>
53
54#include <dev/pci/lynxfbreg.h>
55#include <dev/pci/lynxfbvar.h>
56
57#ifndef	LYNXFB_DEFAULT_WIDTH
58#define	LYNXFB_DEFAULT_WIDTH	1024
59#endif
60#ifndef	LYNXFB_DEFAULT_HEIGHT
61#define	LYNXFB_DEFAULT_HEIGHT	600
62#endif
63#ifndef	LYNXFB_DEFAULT_DEPTH
64#define	LYNXFB_DEFAULT_DEPTH	16
65#endif
66#ifndef	LYNXFB_DEFAULT_STRIDE
67#define	LYNXFB_DEFAULT_STRIDE	0
68#endif
69#ifndef	LYNXFB_DEFAULT_FLAGS
70#ifdef __MIPSEL__
71#define	LYNXFB_DEFAULT_FLAGS	LYNXFB_FLAG_SWAPBR
72#else
73#define	LYNXFB_DEFAULT_FLAGS	0
74#endif
75#endif
76
77struct lynxfb_softc;
78
79/* minimal frame buffer information, suitable for early console */
80struct lynxfb {
81	struct lynxfb_softc	*sc;
82	void			*fbaddr;
83
84	bus_space_tag_t		memt;
85	bus_space_handle_t	memh;
86
87	/* DPR registers */
88	bus_space_tag_t		dprt;
89	bus_space_handle_t	dprh;
90	/* MMIO space */
91	bus_space_tag_t		mmiot;
92	bus_space_handle_t	mmioh;
93
94	struct vcons_screen	vcs;
95	struct wsscreen_descr	wsd;
96
97	int			width, height, depth, stride;
98	int			accel;
99	int			blank;
100
101	/* LYNXFB_FLAG_* */
102	int			flags;
103#define	LYNXFB_FLAG_SWAPBR	(1 << 0)
104};
105
106#define	DPR_READ(fb, reg) \
107	bus_space_read_4((fb)->memt, (fb)->dprh, (reg))
108#define	DPR_WRITE(fb, reg, val) \
109	bus_space_write_4((fb)->memt, (fb)->dprh, (reg), (val))
110
111struct lynxfb_softc {
112	device_t		sc_dev;
113	bus_space_tag_t		sc_memt;
114	bus_space_handle_t	sc_memh;
115
116	pci_chipset_tag_t	sc_pc;
117	pcitag_t		sc_pcitag;
118
119	struct lynxfb		*sc_fb;
120	struct lynxfb		sc_fb_store;
121	bus_addr_t		sc_fbaddr;
122	bus_size_t		sc_fbsize;
123
124	struct vcons_data	sc_vd;
125	struct wsscreen_list	sc_screenlist;
126	const struct wsscreen_descr *sc_screens[1];
127	int			sc_mode;
128};
129
130static int	lynxfb_match_by_id(pcireg_t);
131static int	lynxfb_match(device_t, cfdata_t, void *);
132static void	lynxfb_attach(device_t, device_t, void *);
133
134CFATTACH_DECL_NEW(lynxfb, sizeof(struct lynxfb_softc),
135    lynxfb_match, lynxfb_attach, NULL, NULL);
136
137static int	lynxfb_ioctl(void *, void *, u_long, void *, int, struct lwp *);
138static paddr_t	lynxfb_mmap(void *, void *, off_t, int);
139
140static struct wsdisplay_accessops lynxfb_accessops = {
141	lynxfb_ioctl,
142	lynxfb_mmap,
143	NULL,	/* alloc_screen */
144	NULL,	/* free_screen */
145	NULL,	/* show_screen */
146	NULL,	/* load_font */
147	NULL,	/* pollc */
148	NULL,	/* scroll */
149};
150
151static bool	lynxfb_is_console(struct lynxfb_softc *, pcitag_t);
152static void	lynxfb_init_screen(void *, struct vcons_screen *, int, long *);
153static int	lynxfb_setup(struct lynxfb *);
154
155static int	lynxfb_wait(struct lynxfb *);
156static void	lynxfb_copyrect(struct lynxfb *, int, int, int, int, int, int);
157static void	lynxfb_fillrect(struct lynxfb *, int, int, int, int, int);
158static void	lynxfb_copyrows(void *, int, int, int);
159static void	lynxfb_copycols(void *, int, int, int, int);
160static void	lynxfb_erasecols(void *, int, int, int, long);
161static void	lynxfb_eraserows(void *, int, int, long);
162static void	lynxfb_vcons_copyrows(void *, int, int, int);
163static void	lynxfb_vcons_copycols(void *, int, int, int, int);
164static void	lynxfb_vcons_erasecols(void *, int, int, int, long);
165static void	lynxfb_vcons_eraserows(void *, int, int, long);
166static void	lynxfb_blank(struct lynxfb *, int);
167
168static struct {
169	bool is_console;
170	pcitag_t tag;
171	bus_space_tag_t memt;
172	bus_space_handle_t memh;
173	struct lynxfb fb;
174	bus_addr_t fbaddr;
175	bus_size_t fbsize;
176} lynxfb_console;
177
178int lynxfb_default_width = LYNXFB_DEFAULT_WIDTH;
179int lynxfb_default_height = LYNXFB_DEFAULT_HEIGHT;
180int lynxfb_default_depth = LYNXFB_DEFAULT_DEPTH;
181int lynxfb_default_stride = LYNXFB_DEFAULT_STRIDE;
182int lynxfb_default_flags = LYNXFB_DEFAULT_FLAGS;
183
184static int
185lynxfb_match_by_id(pcireg_t id)
186{
187
188	if (PCI_VENDOR(id) != PCI_VENDOR_SILMOTION)
189		return (0);
190	if (PCI_PRODUCT(id) != PCI_PRODUCT_SILMOTION_SM712)
191		return (0);
192	return (1);
193}
194
195int
196lynxfb_cnattach(bus_space_tag_t memt, bus_space_tag_t iot, pci_chipset_tag_t pc,
197    pcitag_t tag, pcireg_t id)
198{
199	struct rasops_info *ri = &lynxfb_console.fb.vcs.scr_ri;
200	struct lynxfb *fb;
201	bus_space_handle_t memh;
202	bus_addr_t base;
203	bus_size_t size;
204	long defattr;
205	int mapflags;
206	int error;
207
208	if (!lynxfb_match_by_id(id))
209		return (ENODEV);
210
211	if (pci_mapreg_info(pc, tag, PCI_MAPREG_START, PCI_MAPREG_TYPE_MEM,
212	    &base, &size, &mapflags))
213		return (EINVAL);
214	error = bus_space_map(memt, base, size, BUS_SPACE_MAP_LINEAR | mapflags,
215	    &memh);
216	if (error)
217		return (error);
218
219	fb = &lynxfb_console.fb;
220	fb->memt = memt;
221	fb->memh = memh;
222	fb->width = lynxfb_default_width;
223	fb->height = lynxfb_default_height;
224	fb->depth = lynxfb_default_depth;
225	fb->stride = lynxfb_default_stride;
226	if (fb->stride == 0)
227		fb->stride = fb->width * fb->depth / 8;
228	fb->flags = lynxfb_default_flags;
229	error = lynxfb_setup(fb);
230	if (error) {
231		bus_space_unmap(memt, memh, size);
232		return (error);
233	}
234
235	lynxfb_console.is_console = true;
236	lynxfb_console.tag = tag;
237	lynxfb_console.fbaddr = base;
238	lynxfb_console.fbsize = size;
239	(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
240	wsdisplay_preattach(&fb->wsd, ri, 0, 0, defattr);
241
242	return (0);
243}
244
245static bool
246lynxfb_is_console(struct lynxfb_softc *sc, pcitag_t tag)
247{
248
249	return (lynxfb_console.is_console &&
250	    !memcmp(&lynxfb_console.tag, &tag, sizeof(tag)));
251}
252
253static int
254lynxfb_match(device_t parent, cfdata_t 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		return (0);
260	if (!lynxfb_match_by_id(pa->pa_id))
261		return (0);
262	return (1);
263}
264
265static void
266lynxfb_attach(device_t parent, device_t self, void *aux)
267{
268	struct lynxfb_softc *sc = device_private(self);
269	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
270	struct wsemuldisplaydev_attach_args waa;
271	struct lynxfb *fb;
272	struct rasops_info *ri;
273	prop_dictionary_t dict;
274	long defattr;
275	bool is_console;
276
277	sc->sc_dev = self;
278	sc->sc_pc = pa->pa_pc;
279	sc->sc_pcitag = pa->pa_tag;
280
281	pci_aprint_devinfo(pa, NULL);
282
283	is_console = lynxfb_is_console(sc, sc->sc_pcitag);
284
285	fb = is_console ? &lynxfb_console.fb : &sc->sc_fb_store;
286	sc->sc_fb = fb;
287	fb->sc = sc;
288
289	if (is_console) {
290		sc->sc_fbaddr = lynxfb_console.fbaddr;
291		sc->sc_fbsize = lynxfb_console.fbsize;
292	} else {
293		if (pci_mapreg_map(pa, PCI_MAPREG_START, PCI_MAPREG_TYPE_MEM,
294		    BUS_SPACE_MAP_LINEAR, &fb->memt, &fb->memh, &sc->sc_fbaddr,
295		    &sc->sc_fbsize)) {
296			aprint_error_dev(self, "can't map frame buffer\n");
297			return;
298		}
299
300		dict = device_properties(self);
301		if (!prop_dictionary_get_uint32(dict, "width", &fb->width))
302			fb->width = lynxfb_default_width;
303		if (!prop_dictionary_get_uint32(dict, "height", &fb->height))
304			fb->height = lynxfb_default_height;
305		if (!prop_dictionary_get_uint32(dict, "depth", &fb->depth))
306			fb->depth = lynxfb_default_depth;
307		if (!prop_dictionary_get_uint32(dict, "linebytes", &fb->stride)) {
308			if (lynxfb_default_stride == 0)
309				fb->stride = fb->width * fb->depth / 8;
310			else
311				fb->stride = lynxfb_default_stride;
312		}
313		if (!prop_dictionary_get_uint32(dict, "flags", &fb->flags))
314			fb->flags = lynxfb_default_flags;
315
316		if (lynxfb_setup(fb)) {
317			aprint_error_dev(self, "can't setup frame buffer\n");
318			bus_space_unmap(fb->memt, fb->memh, sc->sc_fbsize);
319			return;
320		}
321	}
322	sc->sc_memt = fb->memt;
323	sc->sc_memh = fb->memh;
324
325	aprint_normal_dev(self, "%d x %d, %d bpp, stride %d\n", fb->width,
326	    fb->height, fb->depth, fb->stride);
327
328	fb->wsd = (struct wsscreen_descr) {
329		"default",
330		0, 0,
331		NULL,
332		8, 16,
333		WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
334		NULL,
335	};
336	sc->sc_screens[0] = &fb->wsd;
337	sc->sc_screenlist = (struct wsscreen_list){ 1, sc->sc_screens };
338	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
339
340	vcons_init(&sc->sc_vd, sc, &fb->wsd, &lynxfb_accessops);
341	sc->sc_vd.init_screen = lynxfb_init_screen;
342
343	ri = &fb->vcs.scr_ri;
344	if (is_console) {
345		vcons_init_screen(&sc->sc_vd, &fb->vcs, 1, &defattr);
346		fb->vcs.scr_flags |= VCONS_SCREEN_IS_STATIC;
347
348		fb->wsd.textops = &ri->ri_ops;
349		fb->wsd.capabilities = ri->ri_caps;
350		fb->wsd.nrows = ri->ri_rows;
351		fb->wsd.ncols = ri->ri_cols;
352		wsdisplay_cnattach(&fb->wsd, ri, 0, 0, defattr);
353		vcons_replay_msgbuf(&fb->vcs);
354	} else {
355		/*
356		 * since we're not the console we can postpone the rest
357		 * until someone actually allocates a screen for us
358		 */
359		(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
360	}
361
362	waa.console = is_console;
363	waa.scrdata = &sc->sc_screenlist;
364	waa.accessops = &lynxfb_accessops;
365	waa.accesscookie = &sc->sc_vd;
366
367	config_found(self, &waa, wsemuldisplaydevprint, CFARGS_NONE);
368}
369
370/*
371 * vga sequencer access through MMIO space
372 */
373static __inline uint8_t
374lynxfb_vgats_read(struct lynxfb *fb, uint regno)
375{
376
377	bus_space_write_1(fb->mmiot, fb->mmioh, IO_VGA + VGA_TS_INDEX, regno);
378	return bus_space_read_1(fb->mmiot, fb->mmioh, IO_VGA + VGA_TS_DATA);
379}
380
381static __inline void
382lynxfb_vgats_write(struct lynxfb *fb, uint regno, uint8_t value)
383{
384
385	bus_space_write_1(fb->mmiot, fb->mmioh, IO_VGA + VGA_TS_INDEX, regno);
386	bus_space_write_1(fb->mmiot, fb->mmioh, IO_VGA + VGA_TS_DATA, value);
387}
388
389/*
390 * wsdisplay accesops
391 */
392static int
393lynxfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flags,
394    struct lwp *l)
395{
396	struct vcons_data *vd = v;
397	struct lynxfb_softc *sc = vd->cookie;
398	struct lynxfb *fb = sc->sc_fb;
399	struct vcons_screen *ms = vd->active;
400	struct wsdisplay_fbinfo *wdf;
401	struct wsdisplay_param *param;
402
403	switch (cmd) {
404	case WSDISPLAYIO_GTYPE:
405		*(uint *)data = WSDISPLAY_TYPE_PCIMISC;
406		return (0);
407
408	/* PCI config read/write passthrough. */
409	case PCI_IOC_CFGREAD:
410	case PCI_IOC_CFGWRITE:
411		return pci_devioctl(sc->sc_pc, sc->sc_pcitag,
412		    cmd, data, flags, l);
413
414	case WSDISPLAYIO_GET_BUSID:
415		return wsdisplayio_busid_pci(sc->sc_dev, sc->sc_pc,
416		    sc->sc_pcitag, data);
417
418	case WSDISPLAYIO_GINFO:
419		if (ms == NULL)
420			return (ENODEV);
421		wdf = (struct wsdisplay_fbinfo *)data;
422		wdf->width = ms->scr_ri.ri_width;
423		wdf->height = ms->scr_ri.ri_height;
424		wdf->depth = ms->scr_ri.ri_depth;
425		wdf->cmsize = 0;
426		return (0);
427
428	case WSDISPLAYIO_LINEBYTES:
429		*(uint *)data = fb->stride;
430		return (0);
431
432	case WSDISPLAYIO_GVIDEO:
433		*(int *)data = fb->blank ? WSDISPLAYIO_VIDEO_OFF :
434		    WSDISPLAYIO_VIDEO_ON;
435		return (0);
436
437	case WSDISPLAYIO_SVIDEO:
438		lynxfb_blank(fb, *(int *)data);
439		return (0);
440
441	case WSDISPLAYIO_GETPARAM:
442		param = (struct wsdisplay_param *)data;
443		switch (param->param) {
444		case WSDISPLAYIO_PARAM_BACKLIGHT:
445			param->min = 0;
446			param->max = 1;
447			param->curval = fb->blank;
448			return (0);
449		}
450		break;
451
452	case WSDISPLAYIO_SETPARAM:
453		param = (struct wsdisplay_param *)data;
454		switch (param->param) {
455		case WSDISPLAYIO_PARAM_BACKLIGHT:
456			lynxfb_blank(fb, param->curval);
457			return (0);
458		}
459		break;
460	}
461	return (EPASSTHROUGH);
462}
463
464static paddr_t
465lynxfb_mmap(void *v, void *vs, off_t offset, int prot)
466{
467	struct vcons_data *vd = v;
468	struct lynxfb_softc *sc = vd->cookie;
469	struct rasops_info *ri = &sc->sc_fb->vcs.scr_ri;
470
471	/* 'regular' framebuffer mmap()ing */
472	if (offset < ri->ri_stride * ri->ri_height) {
473		return bus_space_mmap(sc->sc_memt, sc->sc_fbaddr + offset, 0,
474		    prot, BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE);
475	}
476
477	/*
478	 * restrict all other mappings to processes with superuser privileges
479	 * or the kernel itself
480	 */
481	if (kauth_authorize_machdep(kauth_cred_get(),
482	    KAUTH_MACHDEP_UNMANAGEDMEM, NULL, NULL, NULL, NULL) != 0) {
483		aprint_normal_dev(sc->sc_dev, "mmap() rejected.\n");
484		return (-1);
485	}
486
487	/* framebuffer mmap()ing */
488	if (offset >= sc->sc_fbaddr &&
489	    offset < sc->sc_fbaddr + ri->ri_stride * ri->ri_height) {
490		return bus_space_mmap(sc->sc_memt, offset, 0, prot,
491		    BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE);
492	}
493
494	/* register mmap()ing */
495	if (offset >= sc->sc_fbaddr + SM7XX_REG_BASE &&
496	    offset < sc->sc_fbaddr + SM7XX_REG_BASE + SM7XX_REG_SIZE) {
497		return bus_space_mmap(sc->sc_memt, offset, 0, prot, 0);
498	}
499
500	return (-1);
501}
502
503static void
504lynxfb_init_screen(void *cookie, struct vcons_screen *scr,
505    int existing, long *defattr)
506{
507	struct lynxfb_softc *sc = cookie;
508	struct lynxfb *fb = sc->sc_fb;
509	struct rasops_info *ri = &scr->scr_ri;
510
511	ri->ri_width = fb->width;
512	ri->ri_height = fb->height;
513	ri->ri_depth = fb->depth;
514	ri->ri_stride = fb->stride;
515	ri->ri_flg = RI_CENTER;
516	ri->ri_bits = fb->fbaddr;
517
518#ifdef VCONS_DRAW_INTR
519	scr->scr_flags |= VCONS_DONT_READ;
520#endif
521
522	if (existing)
523		ri->ri_flg |= RI_CLEAR;
524
525	rasops_init(ri, 0, 0);
526	ri->ri_caps = WSSCREEN_WSCOLORS;
527	rasops_reconfig(ri, fb->height / ri->ri_font->fontheight,
528	    fb->width / ri->ri_font->fontwidth);
529
530	ri->ri_hw = scr;
531
532	if (fb->accel) {
533		ri->ri_ops.copycols = lynxfb_vcons_copycols;
534		ri->ri_ops.copyrows = lynxfb_vcons_copyrows;
535		ri->ri_ops.erasecols = lynxfb_vcons_erasecols;
536		ri->ri_ops.eraserows = lynxfb_vcons_eraserows;
537	}
538}
539
540/*
541 * Frame buffer initialization.
542 */
543static int
544lynxfb_setup(struct lynxfb *fb)
545{
546	struct rasops_info *ri = &fb->vcs.scr_ri;
547	int error;
548
549	fb->dprt = fb->memt;
550	error = bus_space_subregion(fb->memt, fb->memh, SM7XX_DPR_BASE,
551	    SMXXX_DPR_SIZE, &fb->dprh);
552	if (error != 0)
553		return (error);
554
555	fb->mmiot = fb->memt;
556	error = bus_space_subregion(fb->mmiot, fb->memh, SM7XX_MMIO_BASE,
557	    SM7XX_MMIO_SIZE, &fb->mmioh);
558	if (error != 0)
559		return (error);
560
561	ri->ri_width = fb->width;
562	ri->ri_height = fb->height;
563	ri->ri_depth = fb->depth;
564	ri->ri_stride = fb->stride;
565	ri->ri_flg = RI_CENTER | RI_CLEAR | RI_NO_AUTO;
566	fb->fbaddr = ri->ri_bits = (void *)bus_space_vaddr(fb->memt, fb->memh);
567	ri->ri_hw = fb;
568
569	if (fb->flags & LYNXFB_FLAG_SWAPBR) {
570		switch (fb->depth) {
571		case 16:
572			ri->ri_rnum = 5;
573			ri->ri_rpos = 11;
574			ri->ri_gnum = 6;
575			ri->ri_gpos = 5;
576			ri->ri_bnum = 5;
577			ri->ri_bpos = 0;
578			break;
579		}
580	}
581
582	rasops_init(ri, 0, 0);
583	rasops_reconfig(ri, ri->ri_height / ri->ri_font->fontheight,
584	    ri->ri_width / ri->ri_font->fontwidth);
585
586	fb->wsd.name = "std";
587	fb->wsd.ncols = ri->ri_cols;
588	fb->wsd.nrows = ri->ri_rows;
589	fb->wsd.textops = &ri->ri_ops;
590	fb->wsd.fontwidth = ri->ri_font->fontwidth;
591	fb->wsd.fontheight = ri->ri_font->fontheight;
592	fb->wsd.capabilities = ri->ri_caps;
593
594	/*
595	 * Setup 2D acceleration whenever possible
596	 */
597	if (lynxfb_wait(fb) == 0) {
598		fb->accel = 1;
599
600		DPR_WRITE(fb, DPR_CROP_TOPLEFT_COORDS, DPR_COORDS(0, 0));
601		/* use of width both times is intentional */
602		DPR_WRITE(fb, DPR_PITCH,
603		    DPR_COORDS(ri->ri_width, ri->ri_width));
604		DPR_WRITE(fb, DPR_SRC_WINDOW,
605		    DPR_COORDS(ri->ri_width, ri->ri_width));
606		DPR_WRITE(fb, DPR_BYTE_BIT_MASK, 0xffffffff);
607		DPR_WRITE(fb, DPR_COLOR_COMPARE_MASK, 0);
608		DPR_WRITE(fb, DPR_COLOR_COMPARE, 0);
609		DPR_WRITE(fb, DPR_SRC_BASE, 0);
610		DPR_WRITE(fb, DPR_DST_BASE, 0);
611		DPR_READ(fb, DPR_DST_BASE);
612
613		ri->ri_ops.copycols = lynxfb_copycols;
614		ri->ri_ops.copyrows = lynxfb_copyrows;
615		ri->ri_ops.erasecols = lynxfb_erasecols;
616		ri->ri_ops.eraserows = lynxfb_eraserows;
617	}
618
619	return (0);
620}
621
622static int
623lynxfb_wait(struct lynxfb *fb)
624{
625	uint32_t reg;
626	int i;
627
628	for (i = 10000; i > 0; i--) {
629		reg = lynxfb_vgats_read(fb, 0x16);
630		if ((reg & 0x18) == 0x10)
631			return (0);
632		delay(1);
633	}
634	return (EBUSY);
635}
636
637static void
638lynxfb_copyrect(struct lynxfb *fb, int sx, int sy, int dx, int dy, int w, int h)
639{
640	uint32_t dir;
641
642	/* Compute rop direction */
643	if (sy < dy || (sy == dy && sx <= dx)) {
644		sx += w - 1;
645		dx += w - 1;
646		sy += h - 1;
647		dy += h - 1;
648		dir = DE_CTRL_RTOL;
649	} else
650		dir = 0;
651
652	DPR_WRITE(fb, DPR_SRC_COORDS, DPR_COORDS(sx, sy));
653	DPR_WRITE(fb, DPR_DST_COORDS, DPR_COORDS(dx, dy));
654	DPR_WRITE(fb, DPR_SPAN_COORDS, DPR_COORDS(w, h));
655	DPR_WRITE(fb, DPR_DE_CTRL, DE_CTRL_START | DE_CTRL_ROP_ENABLE | dir |
656	    (DE_CTRL_COMMAND_BITBLT << DE_CTRL_COMMAND_SHIFT) |
657	    (DE_CTRL_ROP_SRC << DE_CTRL_ROP_SHIFT));
658	DPR_READ(fb, DPR_DE_CTRL);
659
660	lynxfb_wait(fb);
661}
662
663static void
664lynxfb_fillrect(struct lynxfb *fb, int x, int y, int w, int h, int bg)
665{
666	struct rasops_info *ri = &fb->vcs.scr_ri;
667
668	DPR_WRITE(fb, DPR_FG_COLOR, ri->ri_devcmap[bg]);
669	DPR_WRITE(fb, DPR_DST_COORDS, DPR_COORDS(x, y));
670	DPR_WRITE(fb, DPR_SPAN_COORDS, DPR_COORDS(w, h));
671	DPR_WRITE(fb, DPR_DE_CTRL, DE_CTRL_START | DE_CTRL_ROP_ENABLE |
672	    (DE_CTRL_COMMAND_SOLIDFILL << DE_CTRL_COMMAND_SHIFT) |
673	    (DE_CTRL_ROP_SRC << DE_CTRL_ROP_SHIFT));
674	DPR_READ(fb, DPR_DE_CTRL);
675
676	lynxfb_wait(fb);
677}
678
679static inline void
680lynxfb_copyrows1(struct rasops_info *ri, int src, int dst, int num,
681    struct lynxfb *fb)
682{
683	struct wsdisplay_font *f = ri->ri_font;
684
685	num *= f->fontheight;
686	src *= f->fontheight;
687	dst *= f->fontheight;
688
689	lynxfb_copyrect(fb, ri->ri_xorigin, ri->ri_yorigin + src,
690	    ri->ri_xorigin, ri->ri_yorigin + dst, ri->ri_emuwidth, num);
691}
692
693static inline void
694lynxfb_copycols1(struct rasops_info *ri, int row, int src, int dst, int num,
695    struct lynxfb *fb)
696{
697	struct wsdisplay_font *f = ri->ri_font;
698
699	num *= f->fontwidth;
700	src *= f->fontwidth;
701	dst *= f->fontwidth;
702	row *= f->fontheight;
703
704	lynxfb_copyrect(fb, ri->ri_xorigin + src, ri->ri_yorigin + row,
705	    ri->ri_xorigin + dst, ri->ri_yorigin + row, num, f->fontheight);
706}
707
708static inline void
709lynxfb_erasecols1(struct rasops_info *ri, int row, int col, int num, long attr,
710    struct lynxfb *fb)
711{
712	struct wsdisplay_font *f = ri->ri_font;
713	int32_t bg, fg, ul;
714
715	row *= f->fontheight;
716	col *= f->fontwidth;
717	num *= f->fontwidth;
718	rasops_unpack_attr(attr, &fg, &bg, &ul);
719
720	lynxfb_fillrect(fb, ri->ri_xorigin + col, ri->ri_yorigin + row,
721	    num, f->fontheight, bg);
722}
723
724static inline void
725lynxfb_eraserows1(struct rasops_info *ri, int row, int num, long attr,
726    struct lynxfb *fb)
727{
728	struct wsdisplay_font *f = ri->ri_font;
729	int32_t bg, fg, ul;
730	int x, y, w;
731
732	if ((num == ri->ri_rows) && ISSET(ri->ri_flg, RI_FULLCLEAR)) {
733		num = ri->ri_height;
734		x = y = 0;
735		w = ri->ri_width;
736	} else {
737		num *= f->fontheight;
738		x = ri->ri_xorigin;
739		y = ri->ri_yorigin + row * f->fontheight;
740		w = ri->ri_emuwidth;
741	}
742	rasops_unpack_attr(attr, &fg, &bg, &ul);
743	lynxfb_fillrect(fb, x, y, w, num, bg);
744}
745
746static void
747lynxfb_copyrows(void *cookie, int src, int dst, int num)
748{
749	struct rasops_info *ri = cookie;
750	struct lynxfb *fb = ri->ri_hw;
751
752	lynxfb_copyrows1(ri, src, dst, num, fb);
753}
754
755static void
756lynxfb_copycols(void *cookie, int row, int src, int dst, int num)
757{
758	struct rasops_info *ri = cookie;
759	struct lynxfb *fb = ri->ri_hw;
760
761	lynxfb_copycols1(ri, row, src, dst, num, fb);
762}
763
764static void
765lynxfb_erasecols(void *cookie, int row, int col, int num, long attr)
766{
767	struct rasops_info *ri = cookie;
768	struct lynxfb *fb = ri->ri_hw;
769
770	lynxfb_erasecols1(ri, row, col, num, attr, fb);
771}
772
773static void
774lynxfb_eraserows(void *cookie, int row, int num, long attr)
775{
776	struct rasops_info *ri = cookie;
777	struct lynxfb *fb = ri->ri_hw;
778
779	lynxfb_eraserows1(ri, row, num, attr, fb);
780}
781
782static void
783lynxfb_vcons_copyrows(void *cookie, int src, int dst, int num)
784{
785	struct rasops_info *ri = cookie;
786	struct vcons_screen *scr = ri->ri_hw;
787	struct lynxfb_softc *sc = scr->scr_cookie;
788	struct lynxfb *fb = sc->sc_fb;
789
790	lynxfb_copyrows1(ri, src, dst, num, fb);
791}
792
793static void
794lynxfb_vcons_copycols(void *cookie, int row, int src, int dst, int num)
795{
796	struct rasops_info *ri = cookie;
797	struct vcons_screen *scr = ri->ri_hw;
798	struct lynxfb_softc *sc = scr->scr_cookie;
799	struct lynxfb *fb = sc->sc_fb;
800
801	lynxfb_copycols1(ri, row, src, dst, num, fb);
802}
803
804static void
805lynxfb_vcons_erasecols(void *cookie, int row, int col, int num, long attr)
806{
807	struct rasops_info *ri = cookie;
808	struct vcons_screen *scr = ri->ri_hw;
809	struct lynxfb_softc *sc = scr->scr_cookie;
810	struct lynxfb *fb = sc->sc_fb;
811
812	lynxfb_erasecols1(ri, row, col, num, attr, fb);
813}
814
815static void
816lynxfb_vcons_eraserows(void *cookie, int row, int num, long attr)
817{
818	struct rasops_info *ri = cookie;
819	struct vcons_screen *scr = ri->ri_hw;
820	struct lynxfb_softc *sc = scr->scr_cookie;
821	struct lynxfb *fb = sc->sc_fb;
822
823	lynxfb_eraserows1(ri, row, num, attr, fb);
824}
825
826static void
827lynxfb_blank(struct lynxfb *fb, int enable)
828{
829
830	fb->blank = !enable;
831	if (enable) {
832		lynxfb_vgats_write(fb, 0x31,
833		    lynxfb_vgats_read(fb, 0x31) | 0x01);
834	} else {
835		lynxfb_vgats_write(fb, 0x21,
836		    lynxfb_vgats_read(fb, 0x21) | 0x30);
837		lynxfb_vgats_write(fb, 0x31,
838		    lynxfb_vgats_read(fb, 0x31) & ~0x01);
839	}
840}
841