xref: /netbsd-src/sys/arch/amiga/dev/zz9k_fb.c (revision 8d9149a73d8c60ae0227e10e709e085238bd5944)
1*8d9149a7Sphx /*	$NetBSD: zz9k_fb.c,v 1.1 2023/05/03 13:49:30 phx Exp $ */
2*8d9149a7Sphx 
3*8d9149a7Sphx /*
4*8d9149a7Sphx  * Copyright (c) 2020 The NetBSD Foundation, Inc.
5*8d9149a7Sphx  * All rights reserved.
6*8d9149a7Sphx  *
7*8d9149a7Sphx  * This code is derived from software contributed to The NetBSD Foundation
8*8d9149a7Sphx  * by Alain Runa.
9*8d9149a7Sphx  *
10*8d9149a7Sphx  * Redistribution and use in source and binary forms, with or without
11*8d9149a7Sphx  * modification, are permitted provided that the following conditions
12*8d9149a7Sphx  * are met:
13*8d9149a7Sphx  * 1. Redistributions of source code must retain the above copyright
14*8d9149a7Sphx  *	notice, this list of conditions and the following disclaimer.
15*8d9149a7Sphx  * 2. Redistributions in binary form must reproduce the above copyright
16*8d9149a7Sphx  *	notice, this list of conditions and the following disclaimer in the
17*8d9149a7Sphx  *	documentation and/or other materials provided with the distribution.
18*8d9149a7Sphx  *
19*8d9149a7Sphx  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20*8d9149a7Sphx  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21*8d9149a7Sphx  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22*8d9149a7Sphx  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23*8d9149a7Sphx  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24*8d9149a7Sphx  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25*8d9149a7Sphx  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26*8d9149a7Sphx  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27*8d9149a7Sphx  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28*8d9149a7Sphx  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29*8d9149a7Sphx  */
30*8d9149a7Sphx 
31*8d9149a7Sphx #include <sys/cdefs.h>
32*8d9149a7Sphx __KERNEL_RCSID(0, "$NetBSD: zz9k_fb.c,v 1.1 2023/05/03 13:49:30 phx Exp $");
33*8d9149a7Sphx 
34*8d9149a7Sphx /* miscellaneous */
35*8d9149a7Sphx #include <sys/errno.h>			/* EPASSTHROUGH */
36*8d9149a7Sphx #include <sys/types.h>			/* size_t */
37*8d9149a7Sphx #include <sys/stdint.h>			/* uintXX_t */
38*8d9149a7Sphx #include <sys/stdbool.h>		/* bool */
39*8d9149a7Sphx 
40*8d9149a7Sphx /* driver(9) */
41*8d9149a7Sphx #include <sys/param.h>      		/* NODEV */
42*8d9149a7Sphx #include <sys/device.h>     		/* CFATTACH_DECL_NEW(), device_priv() */
43*8d9149a7Sphx #include <sys/errno.h>			/* EINVAL, ENODEV, EPASSTHROUGH */
44*8d9149a7Sphx 
45*8d9149a7Sphx /* bus_space(9) and zorro bus */
46*8d9149a7Sphx #include <sys/bus.h>			/* bus_space_xxx(), bus_space_xxx_t */
47*8d9149a7Sphx #include <sys/cpu.h>			/* kvtop() */
48*8d9149a7Sphx #include <sys/systm.h>			/* aprint_xxx() */
49*8d9149a7Sphx 
50*8d9149a7Sphx /* wsdisplay(9) */
51*8d9149a7Sphx #include <dev/wscons/wsconsio.h>	/* WSDISPLAYIO_XXX, wsdisplayio_xxx */
52*8d9149a7Sphx #include <dev/wscons/wsdisplayvar.h>	/* wsscreen_xxx, wsdisplay_xxx */
53*8d9149a7Sphx #include <dev/wscons/wsemulvar.h>	/* ? */
54*8d9149a7Sphx #include <dev/wscons/wsemul_vt100var.h>	/* ? */
55*8d9149a7Sphx 
56*8d9149a7Sphx /* rasops(9) */
57*8d9149a7Sphx /* #include <dev/wscons/wsdisplayvar.h> */
58*8d9149a7Sphx #include <dev/rasops/rasops.h>		/* rasops_unpack_attr(), rasops_info */
59*8d9149a7Sphx 
60*8d9149a7Sphx /* wsfont(9) */
61*8d9149a7Sphx /* #include <dev/wscons/wsconsio.h> */	/* WSDISPLAYIO_XXX, wsdisplayio_xxx */
62*8d9149a7Sphx #include <dev/wsfont/wsfont.h>		/* wsfont_init() */
63*8d9149a7Sphx 
64*8d9149a7Sphx /* vcons(9) */
65*8d9149a7Sphx #include <dev/wscons/wsdisplay_vconsvar.h>	/* vcons_xxx(), vcons_data,
66*8d9149a7Sphx 						vcons_screen */
67*8d9149a7Sphx /* cons(9) */
68*8d9149a7Sphx #include <dev/cons.h>			/* consdev, CN_INTERNAL */
69*8d9149a7Sphx 
70*8d9149a7Sphx /* zz9k and amiga related */
71*8d9149a7Sphx #include <amiga/dev/kbdvar.h>		/* kbd_cnattach() */
72*8d9149a7Sphx #include <amiga/dev/zz9kvar.h>		/* zz9kbus_attach_args */
73*8d9149a7Sphx #include <amiga/dev/zz9kreg.h>		/* ZZ9000 registers */
74*8d9149a7Sphx #include "opt_zz9k_fb.h"		/* ZZFB_CONSOLE */
75*8d9149a7Sphx #include "zz9k_fb.h"			/* NZZ9K_FB */
76*8d9149a7Sphx #include "kbd.h"			/* NKBD */
77*8d9149a7Sphx 
78*8d9149a7Sphx 
79*8d9149a7Sphx /*
80*8d9149a7Sphx  * One can choose different graphics modes and color depths for the different
81*8d9149a7Sphx  * wsdisplay modes emul (console), mapped (raw) and dumbfb (e.g. X11) here.
82*8d9149a7Sphx  * Please consult zz9kreg.h for available graphics modes and color depths
83*8d9149a7Sphx  * supported by the ZZ9000.
84*8d9149a7Sphx  */
85*8d9149a7Sphx #define ZZFB_SET_CON_MODE	ZZ9K_MODE_1280x720	/* Console */
86*8d9149a7Sphx #define ZZFB_SET_CON_BPP	ZZ9K_COLOR_8BIT
87*8d9149a7Sphx #define ZZFB_SET_GFX_MODE	ZZ9K_MODE_1280x720	/* raw FB */
88*8d9149a7Sphx #define ZZFB_SET_GFX_BPP	ZZ9K_COLOR_16BIT
89*8d9149a7Sphx #define ZZFB_SET_DFB_MODE	ZZ9K_MODE_1280x720	/* X11 */
90*8d9149a7Sphx #define ZZFB_SET_DFB_BPP	ZZ9K_COLOR_16BIT
91*8d9149a7Sphx 
92*8d9149a7Sphx /*
93*8d9149a7Sphx  * This defines ZZ9000 scandoubler's capture mode and it is used only in case
94*8d9149a7Sphx  * the ZZ9000 does not have the early console, but also in X11 video off mode.
95*8d9149a7Sphx  * NetBSD defaults to NTSC amiga screen, so the scandoubler default is NTSC too.
96*8d9149a7Sphx  * On a custom configured kernel for a PAL screen, one should consider to change
97*8d9149a7Sphx  * the capture mode to PAL to get the best result. If the attached monitor does
98*8d9149a7Sphx  * not support a scandoubled PAL signal in 50Hz, consider using the VGA 800x600
99*8d9149a7Sphx  * capture mode which is compatible with most monitors and works fine with NTSC
100*8d9149a7Sphx  * and PAL captures at 60Hz.
101*8d9149a7Sphx  * Valid values for ZZFB_CAP_MODE: 0: NTSC, 1: PAL, 2:VGA (PAL60)
102*8d9149a7Sphx  */
103*8d9149a7Sphx #define ZZFB_CAP_MODE 0
104*8d9149a7Sphx 
105*8d9149a7Sphx #if   ZZFB_CAP_MODE == 0
106*8d9149a7Sphx #define ZZFB_CAPTURE_MODE	ZZ9K_MODE_720x480
107*8d9149a7Sphx #define ZZFB_DISPLAY_MODE	ZZ9K_MODE_720x480
108*8d9149a7Sphx #elif ZZFB_CAP_MODE == 1
109*8d9149a7Sphx #define ZZFB_CAPTURE_MODE	ZZ9K_MODE_720x576p50
110*8d9149a7Sphx #define ZZFB_DISPLAY_MODE	ZZ9K_MODE_720x576p50
111*8d9149a7Sphx #elif ZZFB_CAP_MODE == 2
112*8d9149a7Sphx #define ZZFB_CAPTURE_MODE	ZZ9K_MODE_800x600
113*8d9149a7Sphx #define ZZFB_DISPLAY_MODE	ZZ9K_MODE_800x600
114*8d9149a7Sphx #endif
115*8d9149a7Sphx 
116*8d9149a7Sphx /* The allmighty softc structure */
117*8d9149a7Sphx struct zzfb_softc {
118*8d9149a7Sphx 	device_t sc_dev;
119*8d9149a7Sphx 	struct bus_space_tag sc_bst;
120*8d9149a7Sphx 	bus_space_tag_t sc_iot;
121*8d9149a7Sphx 	bus_space_handle_t sc_regh;
122*8d9149a7Sphx 	bus_space_handle_t sc_fbh;
123*8d9149a7Sphx 	size_t sc_fbsize;
124*8d9149a7Sphx 
125*8d9149a7Sphx 	struct vcons_screen sc_console_screen;
126*8d9149a7Sphx 	struct vcons_data sc_vd;
127*8d9149a7Sphx 	struct wsscreen_descr sc_defaultscreen;
128*8d9149a7Sphx 	struct wsscreen_list sc_screenlist;
129*8d9149a7Sphx 	const struct wsscreen_descr *sc_screens[1];
130*8d9149a7Sphx 	u_int sc_wsmode;
131*8d9149a7Sphx 
132*8d9149a7Sphx 	uint16_t sc_displaymode;
133*8d9149a7Sphx 	uint16_t sc_colormode;
134*8d9149a7Sphx 	uint16_t sc_width;
135*8d9149a7Sphx 	uint16_t sc_height;
136*8d9149a7Sphx 	uint16_t sc_bpp;
137*8d9149a7Sphx 	uint16_t sc_stride;
138*8d9149a7Sphx 
139*8d9149a7Sphx 	u_char red[ZZ9K_PALETTE_SIZE];
140*8d9149a7Sphx 	u_char green[ZZ9K_PALETTE_SIZE];
141*8d9149a7Sphx 	u_char blue[ZZ9K_PALETTE_SIZE];
142*8d9149a7Sphx 
143*8d9149a7Sphx 	bool sc_isconsole;
144*8d9149a7Sphx 	bool sc_isrtg;
145*8d9149a7Sphx };
146*8d9149a7Sphx 
147*8d9149a7Sphx static const struct {
148*8d9149a7Sphx 	const char* name;
149*8d9149a7Sphx 	uint16_t width;
150*8d9149a7Sphx 	uint16_t height;
151*8d9149a7Sphx 	uint16_t scale;
152*8d9149a7Sphx } zzfb_modes[] = {				/* Hardcoded in firmware */
153*8d9149a7Sphx 	{       "1280x720p60", 1280,  720, ZZ9K_MODE_SCALE_0},
154*8d9149a7Sphx 	{        "800x600p60",  800,  600, ZZ9K_MODE_SCALE_0},
155*8d9149a7Sphx 	{        "640x480p60",  640,  480, ZZ9K_MODE_SCALE_0},
156*8d9149a7Sphx 	{       "1024x768p60", 1024,  768, ZZ9K_MODE_SCALE_0},
157*8d9149a7Sphx 	{      "1280x1024p60", 1280, 1024, ZZ9K_MODE_SCALE_0},
158*8d9149a7Sphx 	{      "1920x1080p60", 1920, 1080, ZZ9K_MODE_SCALE_0},
159*8d9149a7Sphx 	{        "720x576p50",  720,  576, ZZ9K_MODE_SCALE_0},	/* 50 Hz */
160*8d9149a7Sphx 	{      "1920x1080p50", 1920, 1080, ZZ9K_MODE_SCALE_0},	/* 50 Hz */
161*8d9149a7Sphx 	{        "720x480p60",  720,  480, ZZ9K_MODE_SCALE_0},
162*8d9149a7Sphx 	{        "640x512p60",  640,  512, ZZ9K_MODE_SCALE_0},
163*8d9149a7Sphx 	{      "1600x1200p60", 1600, 1200, ZZ9K_MODE_SCALE_0},
164*8d9149a7Sphx 	{      "2560x1444p30", 2560, 1444, ZZ9K_MODE_SCALE_0},	/* 30 Hz */
165*8d9149a7Sphx 	{ "720x576p50-NS-PAL",  720,  576, ZZ9K_MODE_SCALE_0},	/* 50 Hz */
166*8d9149a7Sphx 	{ "720x480p60-NS-PAL",  720,  480, ZZ9K_MODE_SCALE_0},
167*8d9149a7Sphx 	{"720x576p50-NS-NTSC",  720,  576, ZZ9K_MODE_SCALE_0},	/* 50 Hz */
168*8d9149a7Sphx 	{"720x480p60-NS-NTSC",  720,  480, ZZ9K_MODE_SCALE_0},
169*8d9149a7Sphx 	{        "640x400p60",  640,  400, ZZ9K_MODE_SCALE_0},
170*8d9149a7Sphx 	{       "1920x800p60",  640,  400, ZZ9K_MODE_SCALE_0}
171*8d9149a7Sphx };
172*8d9149a7Sphx 
173*8d9149a7Sphx static const struct {
174*8d9149a7Sphx 	const char* name;
175*8d9149a7Sphx 	uint16_t bpp;
176*8d9149a7Sphx 	uint16_t mode;
177*8d9149a7Sphx 	uint16_t stride;
178*8d9149a7Sphx } zzfb_colors[] = {				/* Hardcoded in firmware */
179*8d9149a7Sphx 	{      "8-bit LUT",  8, ZZ9K_MODE_COLOR_8BIT , 1},
180*8d9149a7Sphx 	{  "16-bit RGB565", 16, ZZ9K_MODE_COLOR_16BIT, 2},
181*8d9149a7Sphx 	{"32-bit BGRA8888", 32, ZZ9K_MODE_COLOR_32BIT, 4},
182*8d9149a7Sphx 	{"16-bit ARGB1555", 15, ZZ9K_MODE_COLOR_15BIT, 2}
183*8d9149a7Sphx };
184*8d9149a7Sphx 
185*8d9149a7Sphx #define ZZFB_MODES_SIZE		(sizeof zzfb_modes / sizeof zzfb_modes[0])
186*8d9149a7Sphx #define ZZFB_COLORS_SIZE	(sizeof zzfb_colors / sizeof zzfb_colors[0])
187*8d9149a7Sphx 
188*8d9149a7Sphx /* functions to set gfx mode, palette and misc stuff to make life easier. */
189*8d9149a7Sphx static void zzfb_set_capture(struct zzfb_softc *sc, uint16_t display_mode,
190*8d9149a7Sphx     uint16_t capture_mode);
191*8d9149a7Sphx static void zzfb_set_mode(struct zzfb_softc *sc, uint16_t display_mode,
192*8d9149a7Sphx     uint16_t color_mode);
193*8d9149a7Sphx static void zzfb_wait_vblank(struct zzfb_softc *sc);
194*8d9149a7Sphx static void zzfb_init_palette(struct zzfb_softc *sc);
195*8d9149a7Sphx static void zzfb_set_palette(struct zzfb_softc *sc);
196*8d9149a7Sphx static void zzfb_clearbg(struct zzfb_softc *sc, uint8_t color_index);
197*8d9149a7Sphx static int zzfb_get_fbinfo(struct zzfb_softc *sc,
198*8d9149a7Sphx     struct wsdisplayio_fbinfo *fbi);
199*8d9149a7Sphx 
200*8d9149a7Sphx /* vcons_data init_screen function */
201*8d9149a7Sphx static void zzfb_init_screen(void *cookie, struct vcons_screen *scr,
202*8d9149a7Sphx     int existing, long *defattr);
203*8d9149a7Sphx 
204*8d9149a7Sphx /* accelerated raster ops functions */
205*8d9149a7Sphx static void zzfb_eraserows(void *cookie, int row, int nrows, long fillattr);
206*8d9149a7Sphx static void zzfb_copyrows(void *cookie, int srcrow, int dstrow, int nrows);
207*8d9149a7Sphx static void zzfb_copycols(void *cookie, int row, int srccol, int dstcol,
208*8d9149a7Sphx     int ncols);
209*8d9149a7Sphx static void zzfb_erasecols(void *cookie, int row, int startcol, int ncols,
210*8d9149a7Sphx     long fillattr);
211*8d9149a7Sphx 
212*8d9149a7Sphx /* blitter support functions*/
213*8d9149a7Sphx static void zzfb_rectfill(struct zzfb_softc *sc, uint16_t x, uint16_t y,
214*8d9149a7Sphx     uint16_t w, uint16_t h, uint32_t color);
215*8d9149a7Sphx static void zzfb_bitblt(struct zzfb_softc *sc, uint16_t x, uint16_t y,
216*8d9149a7Sphx     uint16_t w, uint16_t h, uint16_t xs, uint16_t ys);
217*8d9149a7Sphx 
218*8d9149a7Sphx /* wsdisplay_accessops stuff */
219*8d9149a7Sphx static paddr_t zzfb_mmap(void *v, void *vs, off_t offset, int prot);
220*8d9149a7Sphx static int zzfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
221*8d9149a7Sphx     struct lwp *l);
222*8d9149a7Sphx static int zzfb_getcmap(struct zzfb_softc *sc, struct wsdisplay_cmap *cm);
223*8d9149a7Sphx static int zzfb_putcmap(struct zzfb_softc *sc, struct wsdisplay_cmap *cm);
224*8d9149a7Sphx struct wsdisplay_accessops zzfb_accessops =
225*8d9149a7Sphx     {zzfb_ioctl, zzfb_mmap, NULL, NULL, NULL, NULL, NULL, NULL};
226*8d9149a7Sphx 
227*8d9149a7Sphx /* driver(9) essentials */
228*8d9149a7Sphx static int zzfb_match(device_t parent, cfdata_t match, void *aux);
229*8d9149a7Sphx static void zzfb_attach(device_t parent, device_t self, void *aux);
230*8d9149a7Sphx CFATTACH_DECL_NEW(
231*8d9149a7Sphx     zz9k_fb, sizeof(struct zzfb_softc), zzfb_match, zzfb_attach, NULL, NULL );
232*8d9149a7Sphx 
233*8d9149a7Sphx #ifdef ZZFB_CONSOLE
234*8d9149a7Sphx extern bool zz9k_exists;
235*8d9149a7Sphx #endif /* ZZFB_CONSOLE */
236*8d9149a7Sphx 
237*8d9149a7Sphx 
238*8d9149a7Sphx /* If you build it, they will come... */
239*8d9149a7Sphx 
240*8d9149a7Sphx static int
zzfb_match(device_t parent,cfdata_t match,void * aux)241*8d9149a7Sphx zzfb_match(device_t parent, cfdata_t match, void *aux)
242*8d9149a7Sphx {
243*8d9149a7Sphx 	struct zz9kbus_attach_args *bap = aux;
244*8d9149a7Sphx 
245*8d9149a7Sphx 	if (strcmp(bap->zzaa_name, "zz9k_fb") != 0) {
246*8d9149a7Sphx 		return 0;
247*8d9149a7Sphx 	}
248*8d9149a7Sphx 
249*8d9149a7Sphx 	return 1;
250*8d9149a7Sphx }
251*8d9149a7Sphx 
252*8d9149a7Sphx static void
zzfb_attach(device_t parent,device_t self,void * aux)253*8d9149a7Sphx zzfb_attach(device_t parent, device_t self, void *aux)
254*8d9149a7Sphx {
255*8d9149a7Sphx 	struct zz9kbus_attach_args *bap = aux;
256*8d9149a7Sphx 	struct zzfb_softc *sc = device_private(self);
257*8d9149a7Sphx 	struct zz9k_softc *psc = device_private(parent);
258*8d9149a7Sphx 	struct rasops_info *ri;
259*8d9149a7Sphx 	struct wsemuldisplaydev_attach_args ws_aa;
260*8d9149a7Sphx 	long defattr;
261*8d9149a7Sphx 
262*8d9149a7Sphx 	sc->sc_dev = self;
263*8d9149a7Sphx 	sc->sc_bst.base = bap->zzaa_base;
264*8d9149a7Sphx 	sc->sc_bst.absm = &amiga_bus_stride_1;
265*8d9149a7Sphx 	sc->sc_iot = &sc->sc_bst;
266*8d9149a7Sphx 	sc->sc_regh = psc->sc_regh;
267*8d9149a7Sphx 
268*8d9149a7Sphx 	if (psc->sc_zsize >= (ZZ9K_FB_BASE + ZZ9K_FB_SIZE)) {
269*8d9149a7Sphx 		sc->sc_fbsize = ZZ9K_FB_SIZE;
270*8d9149a7Sphx 	} else {
271*8d9149a7Sphx 		sc->sc_fbsize = psc->sc_zsize - ZZ9K_FB_BASE;
272*8d9149a7Sphx 	}
273*8d9149a7Sphx 
274*8d9149a7Sphx 	if (bus_space_map(sc->sc_iot, ZZ9K_FB_BASE, sc->sc_fbsize,
275*8d9149a7Sphx 	    BUS_SPACE_MAP_LINEAR, &sc->sc_fbh)) {
276*8d9149a7Sphx 		aprint_error(": Failed to map MNT ZZ9000 framebuffer.\n");
277*8d9149a7Sphx 		return;
278*8d9149a7Sphx 	}
279*8d9149a7Sphx 
280*8d9149a7Sphx 	zzfb_set_mode(sc, ZZFB_SET_CON_MODE, ZZFB_SET_CON_BPP);
281*8d9149a7Sphx     	zzfb_init_palette(sc);
282*8d9149a7Sphx 	zzfb_clearbg(sc, WS_DEFAULT_BG);
283*8d9149a7Sphx 
284*8d9149a7Sphx 	aprint_normal(": Framebuffer resolution: %s, "
285*8d9149a7Sphx 	    "depth: %i bpp (%s)\n", zzfb_modes[sc->sc_displaymode].name,
286*8d9149a7Sphx 	    sc->sc_bpp, zzfb_colors[sc->sc_colormode].name);
287*8d9149a7Sphx 
288*8d9149a7Sphx 	aprint_debug_dev(sc->sc_dev, "[DEBUG] registers at %p/%p (pa/va), "
289*8d9149a7Sphx 	    "framebuffer at %p/%p (pa/va) with %i MB\n",
290*8d9149a7Sphx 	    (void *)kvtop((void *)sc->sc_regh),
291*8d9149a7Sphx 	    bus_space_vaddr(sc->sc_iot, sc->sc_regh),
292*8d9149a7Sphx 	    (void *)kvtop((void *)sc->sc_fbh),
293*8d9149a7Sphx 	    bus_space_vaddr(sc->sc_iot, sc->sc_fbh),
294*8d9149a7Sphx 	    sc->sc_fbsize / (1024 * 1024));
295*8d9149a7Sphx 
296*8d9149a7Sphx 	sc->sc_defaultscreen = (struct wsscreen_descr) {"default", 0, 0, NULL,
297*8d9149a7Sphx 	    8, 16, WSSCREEN_WSCOLORS | WSSCREEN_HILIT, NULL};
298*8d9149a7Sphx 	sc->sc_screens[0] = &sc->sc_defaultscreen;
299*8d9149a7Sphx 	sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
300*8d9149a7Sphx 	sc->sc_wsmode = WSDISPLAYIO_MODE_EMUL;
301*8d9149a7Sphx 
302*8d9149a7Sphx 	vcons_init(&sc->sc_vd, sc, &sc->sc_defaultscreen, &zzfb_accessops);
303*8d9149a7Sphx 	sc->sc_vd.init_screen = zzfb_init_screen;
304*8d9149a7Sphx 
305*8d9149a7Sphx 	ri = &sc->sc_console_screen.scr_ri;
306*8d9149a7Sphx 
307*8d9149a7Sphx #ifdef ZZFB_CONSOLE
308*8d9149a7Sphx 	sc->sc_isconsole = true;
309*8d9149a7Sphx 	vcons_init_screen(&sc->sc_vd, &sc->sc_console_screen, 1,
310*8d9149a7Sphx 	    &defattr);
311*8d9149a7Sphx 	sc->sc_console_screen.scr_flags = VCONS_SCREEN_IS_STATIC;
312*8d9149a7Sphx 	vcons_redraw_screen(&sc->sc_console_screen);
313*8d9149a7Sphx 
314*8d9149a7Sphx 	sc->sc_defaultscreen.textops = &ri->ri_ops;
315*8d9149a7Sphx 	sc->sc_defaultscreen.capabilities = ri->ri_caps;
316*8d9149a7Sphx 	sc->sc_defaultscreen.nrows = ri->ri_rows;
317*8d9149a7Sphx 	sc->sc_defaultscreen.ncols = ri->ri_cols;
318*8d9149a7Sphx 
319*8d9149a7Sphx 	wsdisplay_cnattach(&sc->sc_defaultscreen, ri, 0, 0, defattr);
320*8d9149a7Sphx 	vcons_replay_msgbuf(&sc->sc_console_screen);
321*8d9149a7Sphx #else
322*8d9149a7Sphx 	sc->sc_isconsole = false;
323*8d9149a7Sphx 	if (sc->sc_console_screen.scr_ri.ri_rows == 0) {
324*8d9149a7Sphx 		vcons_init_screen(&sc->sc_vd, &sc->sc_console_screen, 1,
325*8d9149a7Sphx 		    &defattr);
326*8d9149a7Sphx 	} else {
327*8d9149a7Sphx 		(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
328*8d9149a7Sphx 	}
329*8d9149a7Sphx 	zzfb_set_capture(sc, ZZFB_DISPLAY_MODE, ZZFB_CAPTURE_MODE);
330*8d9149a7Sphx 	aprint_normal_dev (sc->sc_dev, "Scandoubler capture: %s, "
331*8d9149a7Sphx 	    "display: %s in %s.\n",
332*8d9149a7Sphx 	    zzfb_modes[ZZFB_CAPTURE_MODE].name,
333*8d9149a7Sphx 	    zzfb_modes[ZZFB_DISPLAY_MODE].name,
334*8d9149a7Sphx 	    zzfb_colors[ZZ9K_COLOR_32BIT].name);
335*8d9149a7Sphx #endif /* ZZFB_CONSOLE */
336*8d9149a7Sphx 
337*8d9149a7Sphx 	ws_aa.console = sc->sc_isconsole;
338*8d9149a7Sphx 	ws_aa.scrdata = &sc->sc_screenlist;
339*8d9149a7Sphx 	ws_aa.accessops = &zzfb_accessops;
340*8d9149a7Sphx 	ws_aa.accesscookie = &sc->sc_vd;
341*8d9149a7Sphx 
342*8d9149a7Sphx 	config_found(sc->sc_dev, &ws_aa, wsemuldisplaydevprint, CFARGS_NONE);
343*8d9149a7Sphx }
344*8d9149a7Sphx 
345*8d9149a7Sphx static void
zzfb_init_palette(struct zzfb_softc * sc)346*8d9149a7Sphx zzfb_init_palette(struct zzfb_softc *sc)
347*8d9149a7Sphx {
348*8d9149a7Sphx 	for (int index = 0; index < ZZ9K_PALETTE_SIZE; index++) {
349*8d9149a7Sphx 		sc->red[index] = rasops_cmap[index * 3 + 0];
350*8d9149a7Sphx 		sc->green[index] = rasops_cmap[index * 3 + 1];
351*8d9149a7Sphx 		sc->blue[index] = rasops_cmap[index * 3 + 2];
352*8d9149a7Sphx 	}
353*8d9149a7Sphx 
354*8d9149a7Sphx 	zzfb_set_palette(sc);
355*8d9149a7Sphx }
356*8d9149a7Sphx 
357*8d9149a7Sphx static void
zzfb_set_palette(struct zzfb_softc * sc)358*8d9149a7Sphx zzfb_set_palette(struct zzfb_softc *sc)
359*8d9149a7Sphx {
360*8d9149a7Sphx 	uint32_t palette;
361*8d9149a7Sphx 	uint8_t rVal;
362*8d9149a7Sphx 	uint8_t gVal;
363*8d9149a7Sphx 	uint8_t bVal;
364*8d9149a7Sphx 
365*8d9149a7Sphx 	for (int index = 0; index < ZZ9K_PALETTE_SIZE; index++) {
366*8d9149a7Sphx 		rVal = sc->red[index];
367*8d9149a7Sphx 		gVal = sc->green[index];
368*8d9149a7Sphx 		bVal = sc->blue[index];
369*8d9149a7Sphx 		palette = ((index << 24) | (rVal << 16) | (gVal << 8) | bVal);
370*8d9149a7Sphx 		ZZREG_W(ZZ9K_VIDEO_CTRL_DATA_HI, palette >> 16);
371*8d9149a7Sphx 		ZZREG_W(ZZ9K_VIDEO_CTRL_DATA_LO, palette & 0xFFFF);
372*8d9149a7Sphx 		ZZREG_W(ZZ9K_VIDEO_CTRL_OP, ZZ9K_OP_PALETTE);
373*8d9149a7Sphx 		ZZREG_W(ZZ9K_VIDEO_CTRL_OP, ZZ9K_OP_NOP);
374*8d9149a7Sphx 	}
375*8d9149a7Sphx }
376*8d9149a7Sphx 
377*8d9149a7Sphx static void
zzfb_init_screen(void * cookie,struct vcons_screen * scr,int existing,long * defattr)378*8d9149a7Sphx zzfb_init_screen(void *cookie, struct vcons_screen *scr, int existing,
379*8d9149a7Sphx     long *defattr)
380*8d9149a7Sphx {
381*8d9149a7Sphx 	struct zzfb_softc *sc = cookie;
382*8d9149a7Sphx 	struct rasops_info *ri = &scr->scr_ri;
383*8d9149a7Sphx 
384*8d9149a7Sphx 	scr->scr_flags = VCONS_SCREEN_IS_STATIC;
385*8d9149a7Sphx 
386*8d9149a7Sphx 	wsfont_init();
387*8d9149a7Sphx 
388*8d9149a7Sphx 	ri->ri_bits = (u_char *)bus_space_vaddr(sc->sc_iot, sc->sc_fbh);
389*8d9149a7Sphx 	ri->ri_depth = sc->sc_bpp;
390*8d9149a7Sphx 	ri->ri_width = sc->sc_width;
391*8d9149a7Sphx 	ri->ri_height = sc->sc_height;
392*8d9149a7Sphx 	ri->ri_stride = sc->sc_stride;
393*8d9149a7Sphx 	ri->ri_flg = 0;
394*8d9149a7Sphx 
395*8d9149a7Sphx 	if (ri->ri_depth == 32) {	/* adjust for BGRA8888 */
396*8d9149a7Sphx 		ri->ri_rnum = 8;	/* for other depths default is OK */
397*8d9149a7Sphx 		ri->ri_gnum = 8;
398*8d9149a7Sphx 		ri->ri_bnum = 8;
399*8d9149a7Sphx 		ri->ri_rpos = 8;	/* skip over alpha channel */
400*8d9149a7Sphx 		ri->ri_gpos = 8 + ri->ri_rnum;
401*8d9149a7Sphx 		ri->ri_bpos = 8 + ri->ri_rnum + ri->ri_gnum;
402*8d9149a7Sphx 	}
403*8d9149a7Sphx 
404*8d9149a7Sphx 	rasops_init(ri, 0, 0);
405*8d9149a7Sphx 	ri->ri_caps = WSSCREEN_WSCOLORS;
406*8d9149a7Sphx 	rasops_reconfig(ri, ri->ri_height / ri->ri_font->fontheight,
407*8d9149a7Sphx 	    ri->ri_width / ri->ri_font->fontwidth);
408*8d9149a7Sphx 	ri->ri_hw = scr;
409*8d9149a7Sphx 
410*8d9149a7Sphx 	ri->ri_ops.eraserows = zzfb_eraserows;
411*8d9149a7Sphx 	ri->ri_ops.copyrows = zzfb_copyrows;
412*8d9149a7Sphx 	ri->ri_ops.erasecols = zzfb_erasecols;
413*8d9149a7Sphx 	ri->ri_ops.copycols = zzfb_copycols;
414*8d9149a7Sphx }
415*8d9149a7Sphx 
416*8d9149a7Sphx static void
zzfb_set_capture(struct zzfb_softc * sc,uint16_t display_mode,uint16_t capture_mode)417*8d9149a7Sphx zzfb_set_capture(struct zzfb_softc *sc, uint16_t display_mode,
418*8d9149a7Sphx     uint16_t capture_mode)
419*8d9149a7Sphx {
420*8d9149a7Sphx 	uint16_t panPtrHi;
421*8d9149a7Sphx 	uint16_t panPtrLo;
422*8d9149a7Sphx 	uint16_t new_mode;
423*8d9149a7Sphx 
424*8d9149a7Sphx 	switch (display_mode) {
425*8d9149a7Sphx 	case ZZ9K_MODE_720x480:		/* NTSC */
426*8d9149a7Sphx 		panPtrHi = ZZ9K_CAPTURE_PAN_NTSC >> 16;
427*8d9149a7Sphx 		panPtrLo = ZZ9K_CAPTURE_PAN_NTSC & 0xFFFF;
428*8d9149a7Sphx 		break;
429*8d9149a7Sphx 	case ZZ9K_MODE_720x576p50:	/* PAL */
430*8d9149a7Sphx 		panPtrHi = ZZ9K_CAPTURE_PAN_PAL >> 16;
431*8d9149a7Sphx 		panPtrLo = ZZ9K_CAPTURE_PAN_PAL & 0xFFFF;
432*8d9149a7Sphx 		break;
433*8d9149a7Sphx 	case ZZ9K_MODE_800x600:		/* VGA */
434*8d9149a7Sphx 		panPtrHi = ZZ9K_CAPTURE_PAN_VGA >> 16;
435*8d9149a7Sphx 		panPtrLo = ZZ9K_CAPTURE_PAN_VGA & 0xFFFF;
436*8d9149a7Sphx 		break;
437*8d9149a7Sphx 	default:
438*8d9149a7Sphx 		aprint_error_dev(sc->sc_dev, "Unsupported scandoubler "
439*8d9149a7Sphx 		    "capture and display mode combination.\n");
440*8d9149a7Sphx 		return;
441*8d9149a7Sphx 	}
442*8d9149a7Sphx 	new_mode = ZZ9K_MODE_SCALE_2 | ZZ9K_MODE_COLOR_32BIT | display_mode;
443*8d9149a7Sphx 	zzfb_wait_vblank(sc);
444*8d9149a7Sphx 	ZZREG_W(ZZ9K_VIDEOCAP_VMODE, capture_mode);
445*8d9149a7Sphx 	ZZREG_W(ZZ9K_BLITTER_USER1, ZZ9K_FEATURE_NONSTANDARD_VSYNC);
446*8d9149a7Sphx 	ZZREG_W(ZZ9K_SET_FEATURE, 0x0000);
447*8d9149a7Sphx 	ZZREG_W(ZZ9K_VIDEO_CAPTURE_MODE, ZZ9K_CAPTURE_ON);
448*8d9149a7Sphx 	ZZREG_W(ZZ9K_BLITTER_X1, 0x0000);
449*8d9149a7Sphx 	ZZREG_W(ZZ9K_BLITTER_Y1, 0x0000);
450*8d9149a7Sphx 	ZZREG_W(ZZ9K_BLITTER_X2, 0x0000);
451*8d9149a7Sphx 	ZZREG_W(ZZ9K_BLITTER_COLORMODE, ZZ9K_COLOR_32BIT);
452*8d9149a7Sphx 	ZZREG_W(ZZ9K_PAN_PTR_HI, panPtrHi);
453*8d9149a7Sphx 	ZZREG_W(ZZ9K_PAN_PTR_LO, panPtrLo);
454*8d9149a7Sphx 	ZZREG_W(ZZ9K_MODE, new_mode);
455*8d9149a7Sphx 	sc->sc_isrtg = false;
456*8d9149a7Sphx }
457*8d9149a7Sphx 
458*8d9149a7Sphx void
zzfb_set_mode(struct zzfb_softc * sc,uint16_t display_mode,uint16_t color_mode)459*8d9149a7Sphx zzfb_set_mode(struct zzfb_softc *sc, uint16_t display_mode,
460*8d9149a7Sphx     uint16_t color_mode)
461*8d9149a7Sphx {
462*8d9149a7Sphx 	uint16_t new_mode;
463*8d9149a7Sphx 
464*8d9149a7Sphx 	if ((display_mode < 0) || (display_mode >= ZZFB_MODES_SIZE))
465*8d9149a7Sphx 		display_mode = ZZ9K_MODE_1280x720;
466*8d9149a7Sphx 
467*8d9149a7Sphx 	sc->sc_width	= zzfb_modes[display_mode].width;
468*8d9149a7Sphx 	sc->sc_height	= zzfb_modes[display_mode].height;
469*8d9149a7Sphx 	new_mode	= zzfb_modes[display_mode].scale;
470*8d9149a7Sphx 
471*8d9149a7Sphx 	if ((color_mode < 0) || (color_mode >= ZZFB_COLORS_SIZE))
472*8d9149a7Sphx 		color_mode = ZZ9K_COLOR_8BIT;
473*8d9149a7Sphx 
474*8d9149a7Sphx 	if ((color_mode == ZZ9K_COLOR_32BIT) && (sc->sc_width > 1920))
475*8d9149a7Sphx 		color_mode = ZZ9K_COLOR_16BIT;
476*8d9149a7Sphx 
477*8d9149a7Sphx 	sc->sc_bpp    = zzfb_colors[color_mode].bpp;
478*8d9149a7Sphx 	sc->sc_stride = sc->sc_width * zzfb_colors[color_mode].stride;
479*8d9149a7Sphx 	new_mode      = new_mode | zzfb_colors[color_mode].mode | display_mode;
480*8d9149a7Sphx 
481*8d9149a7Sphx 	sc->sc_displaymode = display_mode;
482*8d9149a7Sphx 	sc->sc_colormode   = color_mode;
483*8d9149a7Sphx 
484*8d9149a7Sphx 	zzfb_wait_vblank(sc);
485*8d9149a7Sphx 	ZZREG_W(ZZ9K_VIDEO_CAPTURE_MODE, ZZ9K_CAPTURE_OFF);
486*8d9149a7Sphx 	ZZREG_W(ZZ9K_BLITTER_X1, 0x0000);
487*8d9149a7Sphx 	ZZREG_W(ZZ9K_BLITTER_Y1, 0x0000);
488*8d9149a7Sphx 	ZZREG_W(ZZ9K_BLITTER_X2, 0x0000);
489*8d9149a7Sphx 	ZZREG_W(ZZ9K_BLITTER_COLORMODE, color_mode);
490*8d9149a7Sphx 	ZZREG_W(ZZ9K_BLITTER_SRC_HI, 0x0000);
491*8d9149a7Sphx 	ZZREG_W(ZZ9K_BLITTER_SRC_LO, 0x0000);
492*8d9149a7Sphx 	ZZREG_W(ZZ9K_BLITTER_DST_HI, 0x0000);
493*8d9149a7Sphx 	ZZREG_W(ZZ9K_BLITTER_DST_LO, 0x0000);
494*8d9149a7Sphx 	ZZREG_W(ZZ9K_BLITTER_SPLIT_POS, 0x0000);
495*8d9149a7Sphx 	ZZREG_W(ZZ9K_PAN_PTR_HI, 0x0000);
496*8d9149a7Sphx 	ZZREG_W(ZZ9K_PAN_PTR_LO, 0x0000);
497*8d9149a7Sphx 	ZZREG_W(ZZ9K_MODE, new_mode);
498*8d9149a7Sphx 	sc->sc_isrtg = true;
499*8d9149a7Sphx }
500*8d9149a7Sphx 
501*8d9149a7Sphx static void
zzfb_wait_vblank(struct zzfb_softc * sc)502*8d9149a7Sphx zzfb_wait_vblank(struct zzfb_softc *sc)
503*8d9149a7Sphx {
504*8d9149a7Sphx 	uint16_t vb_status = ZZREG_R(ZZ9K_VIDEO_BLANK_STATUS);
505*8d9149a7Sphx 	while (vb_status != 0)
506*8d9149a7Sphx 		vb_status = ZZREG_R(ZZ9K_VIDEO_BLANK_STATUS);
507*8d9149a7Sphx 	while (vb_status == 0)
508*8d9149a7Sphx 		vb_status = ZZREG_R(ZZ9K_VIDEO_BLANK_STATUS);
509*8d9149a7Sphx }
510*8d9149a7Sphx 
511*8d9149a7Sphx static void
zzfb_clearbg(struct zzfb_softc * sc,uint8_t color_index)512*8d9149a7Sphx zzfb_clearbg(struct zzfb_softc *sc, uint8_t color_index)
513*8d9149a7Sphx {
514*8d9149a7Sphx 	if (color_index >= ZZ9K_PALETTE_SIZE)
515*8d9149a7Sphx 		color_index = 0;
516*8d9149a7Sphx 
517*8d9149a7Sphx 	uint32_t palette = 0;
518*8d9149a7Sphx 	uint8_t rVal = rasops_cmap[color_index * 3 + 0];
519*8d9149a7Sphx 	uint8_t gVal = rasops_cmap[color_index * 3 + 1];
520*8d9149a7Sphx 	uint8_t bVal = rasops_cmap[color_index * 3 + 2];
521*8d9149a7Sphx 
522*8d9149a7Sphx 	switch (sc->sc_colormode) {
523*8d9149a7Sphx 	case ZZ9K_COLOR_32BIT:	/* BGRA8888 */
524*8d9149a7Sphx 		palette = ((bVal << 24) | (gVal << 16) | (rVal << 8) | 0xFF);
525*8d9149a7Sphx 		break;
526*8d9149a7Sphx 	case ZZ9K_COLOR_16BIT: /* RGB565 at high word, don't ask why. */
527*8d9149a7Sphx 		palette = (((rVal & 0xF8) << 8) |
528*8d9149a7Sphx 			   ((gVal & 0xFC) << 3) |
529*8d9149a7Sphx 			   ((bVal >> 3) & 0x1F) ) << 16;
530*8d9149a7Sphx 		break;
531*8d9149a7Sphx 	case ZZ9K_COLOR_15BIT: /* ARGB1555 at high word, don't ask why. */
532*8d9149a7Sphx 		palette = ((0x8000) |
533*8d9149a7Sphx 			   ((rVal & 0xF8) << 7) |
534*8d9149a7Sphx 			   ((gVal & 0xF8) << 2) |
535*8d9149a7Sphx 			   ((bVal >> 3) & 0x1F) ) << 16;
536*8d9149a7Sphx 		break;
537*8d9149a7Sphx 	case ZZ9K_COLOR_8BIT: /* 256 LUT */
538*8d9149a7Sphx 	default:
539*8d9149a7Sphx 		palette = color_index;
540*8d9149a7Sphx 		break;
541*8d9149a7Sphx 	}
542*8d9149a7Sphx 
543*8d9149a7Sphx 	zzfb_rectfill(sc, 0, 0, sc->sc_width, sc->sc_height, palette);
544*8d9149a7Sphx }
545*8d9149a7Sphx 
546*8d9149a7Sphx static void
zzfb_rectfill(struct zzfb_softc * sc,uint16_t x,uint16_t y,uint16_t w,uint16_t h,uint32_t color)547*8d9149a7Sphx zzfb_rectfill(struct zzfb_softc *sc, uint16_t x, uint16_t y,
548*8d9149a7Sphx     uint16_t w, uint16_t h, uint32_t color)
549*8d9149a7Sphx {
550*8d9149a7Sphx 	ZZREG_W(ZZ9K_BLITTER_X1, x);
551*8d9149a7Sphx 	ZZREG_W(ZZ9K_BLITTER_Y1, y);
552*8d9149a7Sphx 	ZZREG_W(ZZ9K_BLITTER_X2, w);
553*8d9149a7Sphx 	ZZREG_W(ZZ9K_BLITTER_Y2, h);
554*8d9149a7Sphx 	ZZREG_W(ZZ9K_BLITTER_ROW_PITCH, sc->sc_stride >> 2);
555*8d9149a7Sphx 	ZZREG_W(ZZ9K_BLITTER_COLORMODE, sc->sc_colormode);
556*8d9149a7Sphx 	ZZREG_W(ZZ9K_BLITTER_RGB_HI, color >> 16);
557*8d9149a7Sphx 	ZZREG_W(ZZ9K_BLITTER_RGB_LO, color & 0xFFFF);
558*8d9149a7Sphx 	ZZREG_W(ZZ9K_BLITTER_OP_FILLRECT, 0x00FF);
559*8d9149a7Sphx }
560*8d9149a7Sphx 
561*8d9149a7Sphx static void
zzfb_bitblt(struct zzfb_softc * sc,uint16_t x,uint16_t y,uint16_t w,uint16_t h,uint16_t xs,uint16_t ys)562*8d9149a7Sphx zzfb_bitblt(struct zzfb_softc *sc, uint16_t x, uint16_t y, uint16_t w,
563*8d9149a7Sphx     uint16_t h, uint16_t xs, uint16_t ys)
564*8d9149a7Sphx {
565*8d9149a7Sphx 	ZZREG_W(ZZ9K_BLITTER_X1, x);
566*8d9149a7Sphx 	ZZREG_W(ZZ9K_BLITTER_Y1, y);
567*8d9149a7Sphx 	ZZREG_W(ZZ9K_BLITTER_X2, w);
568*8d9149a7Sphx 	ZZREG_W(ZZ9K_BLITTER_Y2, h);
569*8d9149a7Sphx 	ZZREG_W(ZZ9K_BLITTER_X3, xs);
570*8d9149a7Sphx 	ZZREG_W(ZZ9K_BLITTER_Y3, ys);
571*8d9149a7Sphx 	ZZREG_W(ZZ9K_BLITTER_ROW_PITCH, sc->sc_stride >> 2);
572*8d9149a7Sphx 	ZZREG_W(ZZ9K_BLITTER_COLORMODE, (0xFF << 8) | sc->sc_colormode);
573*8d9149a7Sphx 	ZZREG_W(ZZ9K_BLITTER_OP_COPYRECT, ZZ9K_OPT_REGULAR);
574*8d9149a7Sphx }
575*8d9149a7Sphx 
576*8d9149a7Sphx static void
zzfb_copyrows(void * cookie,int srcrow,int dstrow,int nrows)577*8d9149a7Sphx zzfb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
578*8d9149a7Sphx {
579*8d9149a7Sphx 	struct rasops_info *ri = cookie;
580*8d9149a7Sphx 	struct vcons_screen *scr = ri->ri_hw;
581*8d9149a7Sphx 	struct zzfb_softc *sc = scr->scr_cookie;
582*8d9149a7Sphx 	int x, y, w, h, ys;
583*8d9149a7Sphx 
584*8d9149a7Sphx 	if (sc->sc_wsmode == WSDISPLAYIO_MODE_EMUL) {
585*8d9149a7Sphx 		x = ri->ri_xorigin;
586*8d9149a7Sphx 		ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
587*8d9149a7Sphx 		y = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
588*8d9149a7Sphx 		w = ri->ri_emuwidth;
589*8d9149a7Sphx 		h = ri->ri_font->fontheight * nrows;
590*8d9149a7Sphx 		zzfb_bitblt(sc, x, y, w, h, x, ys);
591*8d9149a7Sphx 	}
592*8d9149a7Sphx }
593*8d9149a7Sphx 
594*8d9149a7Sphx static void
zzfb_eraserows(void * cookie,int row,int nrows,long fillattr)595*8d9149a7Sphx zzfb_eraserows(void *cookie, int row, int nrows, long fillattr)
596*8d9149a7Sphx {
597*8d9149a7Sphx 	struct rasops_info *ri = cookie;
598*8d9149a7Sphx 	struct vcons_screen *scr = ri->ri_hw;
599*8d9149a7Sphx 	struct zzfb_softc *sc = scr->scr_cookie;
600*8d9149a7Sphx 	int x, y, w, h, fg, bg, ul;
601*8d9149a7Sphx 
602*8d9149a7Sphx 	if (sc->sc_wsmode == WSDISPLAYIO_MODE_EMUL) {
603*8d9149a7Sphx 		x = ri->ri_xorigin;
604*8d9149a7Sphx 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
605*8d9149a7Sphx 		w = ri->ri_emuwidth;
606*8d9149a7Sphx 		h = ri->ri_font->fontheight * nrows;
607*8d9149a7Sphx 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
608*8d9149a7Sphx 		zzfb_rectfill(sc, x, y, w, h, ri->ri_devcmap[bg]);
609*8d9149a7Sphx 	}
610*8d9149a7Sphx }
611*8d9149a7Sphx 
612*8d9149a7Sphx static void
zzfb_copycols(void * cookie,int row,int srccol,int dstcol,int ncols)613*8d9149a7Sphx zzfb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
614*8d9149a7Sphx {
615*8d9149a7Sphx 	struct rasops_info *ri = cookie;
616*8d9149a7Sphx 	struct vcons_screen *scr = ri->ri_hw;
617*8d9149a7Sphx 	struct zzfb_softc *sc = scr->scr_cookie;
618*8d9149a7Sphx 	int x, y, w, h, xs;
619*8d9149a7Sphx 
620*8d9149a7Sphx 	if (sc->sc_wsmode == WSDISPLAYIO_MODE_EMUL) {
621*8d9149a7Sphx 		xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
622*8d9149a7Sphx 		x = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
623*8d9149a7Sphx 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
624*8d9149a7Sphx 		w = ri->ri_font->fontwidth * ncols;
625*8d9149a7Sphx 		h = ri->ri_font->fontheight;
626*8d9149a7Sphx 		zzfb_bitblt(sc, x, y, w, h, xs, y);
627*8d9149a7Sphx 	}
628*8d9149a7Sphx }
629*8d9149a7Sphx 
630*8d9149a7Sphx static void
zzfb_erasecols(void * cookie,int row,int startcol,int ncols,long fillattr)631*8d9149a7Sphx zzfb_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
632*8d9149a7Sphx {
633*8d9149a7Sphx 	struct rasops_info *ri = cookie;
634*8d9149a7Sphx 	struct vcons_screen *scr = ri->ri_hw;
635*8d9149a7Sphx 	struct zzfb_softc *sc = scr->scr_cookie;
636*8d9149a7Sphx 	int x, y, w, h, fg, bg, ul;
637*8d9149a7Sphx 
638*8d9149a7Sphx 	if (sc->sc_wsmode == WSDISPLAYIO_MODE_EMUL) {
639*8d9149a7Sphx 		x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
640*8d9149a7Sphx 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
641*8d9149a7Sphx 		w = ri->ri_font->fontwidth * ncols;
642*8d9149a7Sphx 		h = ri->ri_font->fontheight;
643*8d9149a7Sphx 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
644*8d9149a7Sphx 		zzfb_rectfill(sc, x, y, w, h, ri->ri_devcmap[bg & 0xf]);
645*8d9149a7Sphx 	}
646*8d9149a7Sphx }
647*8d9149a7Sphx 
648*8d9149a7Sphx static int
zzfb_ioctl(void * v,void * vs,u_long cmd,void * data,int flag,struct lwp * l)649*8d9149a7Sphx zzfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, struct lwp *l)
650*8d9149a7Sphx {
651*8d9149a7Sphx 	int retval = 0;
652*8d9149a7Sphx 	u_int new_wsmode;
653*8d9149a7Sphx 	struct vcons_data *vd = v;
654*8d9149a7Sphx 	struct zzfb_softc *sc = vd->cookie;
655*8d9149a7Sphx 	struct vcons_screen *scr = vd->active;
656*8d9149a7Sphx 	struct wsdisplayio_bus_id *busid;
657*8d9149a7Sphx 
658*8d9149a7Sphx 	switch (cmd) {
659*8d9149a7Sphx 	case WSDISPLAYIO_GTYPE:
660*8d9149a7Sphx 		*(u_int *)data = WSDISPLAY_TYPE_UNKNOWN;
661*8d9149a7Sphx 		break;
662*8d9149a7Sphx 	case WSDISPLAYIO_GET_FBINFO:
663*8d9149a7Sphx 		retval = zzfb_get_fbinfo(sc, (struct wsdisplayio_fbinfo *)data);
664*8d9149a7Sphx 		break;
665*8d9149a7Sphx 	case WSDISPLAYIO_GINFO:
666*8d9149a7Sphx 		((struct wsdisplay_fbinfo *)data)->width  = sc->sc_width;
667*8d9149a7Sphx 		((struct wsdisplay_fbinfo *)data)->height = sc->sc_height;
668*8d9149a7Sphx 		((struct wsdisplay_fbinfo *)data)->depth  = sc->sc_bpp;
669*8d9149a7Sphx 		((struct wsdisplay_fbinfo *)data)->cmsize = ZZ9K_PALETTE_SIZE;
670*8d9149a7Sphx 		break;
671*8d9149a7Sphx 	case WSDISPLAYIO_GETCMAP:
672*8d9149a7Sphx 		retval = zzfb_getcmap(sc, (struct wsdisplay_cmap *)data);
673*8d9149a7Sphx 		break;
674*8d9149a7Sphx 	case WSDISPLAYIO_PUTCMAP:
675*8d9149a7Sphx 		retval = zzfb_putcmap(sc, (struct wsdisplay_cmap *)data);
676*8d9149a7Sphx 		break;
677*8d9149a7Sphx 	case WSDISPLAYIO_GVIDEO:
678*8d9149a7Sphx 		*(int *)data = (sc->sc_isrtg == true) ?
679*8d9149a7Sphx 		    WSDISPLAYIO_VIDEO_ON : WSDISPLAYIO_VIDEO_OFF;
680*8d9149a7Sphx 		break;
681*8d9149a7Sphx 	case WSDISPLAYIO_SVIDEO:
682*8d9149a7Sphx 		if (*(int *)data == WSDISPLAYIO_VIDEO_ON) {
683*8d9149a7Sphx 			zzfb_set_mode(sc, sc->sc_displaymode, sc->sc_colormode);
684*8d9149a7Sphx 		} else {
685*8d9149a7Sphx 			zzfb_set_capture(sc, ZZFB_DISPLAY_MODE,
686*8d9149a7Sphx 			    ZZFB_CAPTURE_MODE);
687*8d9149a7Sphx 		}
688*8d9149a7Sphx 		break;
689*8d9149a7Sphx 	case WSDISPLAYIO_GET_BUSID:
690*8d9149a7Sphx 		busid = data;
691*8d9149a7Sphx 		busid->bus_type = WSDISPLAYIO_BUS_SOC;
692*8d9149a7Sphx 		break;
693*8d9149a7Sphx 	case WSDISPLAYIO_GCURPOS:
694*8d9149a7Sphx 	case WSDISPLAYIO_SCURPOS:
695*8d9149a7Sphx 	case WSDISPLAYIO_GCURMAX:
696*8d9149a7Sphx 	case WSDISPLAYIO_GCURSOR:
697*8d9149a7Sphx 	case WSDISPLAYIO_SCURSOR:
698*8d9149a7Sphx 		retval = EPASSTHROUGH;
699*8d9149a7Sphx 		break;
700*8d9149a7Sphx 	case WSDISPLAYIO_GMODE:
701*8d9149a7Sphx 		*(u_int *)data = sc->sc_wsmode;
702*8d9149a7Sphx 		break;
703*8d9149a7Sphx 	case WSDISPLAYIO_SMODE:
704*8d9149a7Sphx 		new_wsmode = *(u_int *)data;
705*8d9149a7Sphx 		if (new_wsmode != sc->sc_wsmode) {
706*8d9149a7Sphx 			switch (new_wsmode) {
707*8d9149a7Sphx 			case WSDISPLAYIO_MODE_EMUL:
708*8d9149a7Sphx 				zzfb_set_mode(sc,
709*8d9149a7Sphx 				    ZZFB_SET_CON_MODE, ZZFB_SET_CON_BPP);
710*8d9149a7Sphx 				zzfb_init_palette(sc);
711*8d9149a7Sphx 				zzfb_clearbg(sc, WS_DEFAULT_BG);
712*8d9149a7Sphx 				vcons_redraw_screen(scr);
713*8d9149a7Sphx 				sc->sc_wsmode = new_wsmode;
714*8d9149a7Sphx 				break;
715*8d9149a7Sphx 			case WSDISPLAYIO_MODE_MAPPED:
716*8d9149a7Sphx 				zzfb_set_mode(sc,
717*8d9149a7Sphx 				    ZZFB_SET_GFX_MODE, ZZFB_SET_GFX_BPP);
718*8d9149a7Sphx 				    zzfb_clearbg(sc, WSCOL_BLACK);
719*8d9149a7Sphx 				sc->sc_wsmode = new_wsmode;
720*8d9149a7Sphx 				break;
721*8d9149a7Sphx 			case WSDISPLAYIO_MODE_DUMBFB:
722*8d9149a7Sphx 				zzfb_set_mode(sc,
723*8d9149a7Sphx 				    ZZFB_SET_DFB_MODE, ZZFB_SET_DFB_BPP);
724*8d9149a7Sphx 				    zzfb_clearbg(sc, WSCOL_BLACK);
725*8d9149a7Sphx 				sc->sc_wsmode = new_wsmode;
726*8d9149a7Sphx 				break;
727*8d9149a7Sphx 			default:
728*8d9149a7Sphx 				retval = EINVAL;
729*8d9149a7Sphx 				break;
730*8d9149a7Sphx 			}
731*8d9149a7Sphx 		} else {
732*8d9149a7Sphx 			retval = EINVAL;
733*8d9149a7Sphx 		}
734*8d9149a7Sphx 		break;
735*8d9149a7Sphx 	case WSDISPLAYIO_LINEBYTES:
736*8d9149a7Sphx 		*(u_int *)data = sc->sc_stride;
737*8d9149a7Sphx 		break;
738*8d9149a7Sphx 	case WSDISPLAYIO_GMSGATTRS:
739*8d9149a7Sphx 	case WSDISPLAYIO_SMSGATTRS:
740*8d9149a7Sphx 	case WSDISPLAYIO_GBORDER:
741*8d9149a7Sphx 	case WSDISPLAYIO_SBORDER:
742*8d9149a7Sphx 	case WSDISPLAYIO_GETWSCHAR:
743*8d9149a7Sphx 	case WSDISPLAYIO_PUTWSCHAR:
744*8d9149a7Sphx 	case WSDISPLAYIO_SSPLASH:
745*8d9149a7Sphx 	case WSDISPLAYIO_GET_EDID:
746*8d9149a7Sphx 	case WSDISPLAYIO_SETVERSION:
747*8d9149a7Sphx 	default:
748*8d9149a7Sphx 		retval = EPASSTHROUGH;
749*8d9149a7Sphx 		break;
750*8d9149a7Sphx 	}
751*8d9149a7Sphx 
752*8d9149a7Sphx 	return retval;
753*8d9149a7Sphx }
754*8d9149a7Sphx 
755*8d9149a7Sphx static paddr_t
zzfb_mmap(void * v,void * vs,off_t offset,int prot)756*8d9149a7Sphx zzfb_mmap(void *v, void *vs, off_t offset, int prot)
757*8d9149a7Sphx {
758*8d9149a7Sphx 	struct vcons_data *vd = v;
759*8d9149a7Sphx 	struct zzfb_softc *sc = vd->cookie;
760*8d9149a7Sphx 
761*8d9149a7Sphx 	if ( (offset >= 0) && (offset < sc->sc_fbsize) ) {
762*8d9149a7Sphx 		return bus_space_mmap( sc->sc_iot,
763*8d9149a7Sphx 		    (bus_addr_t)kvtop((void *)sc->sc_fbh), offset, prot,
764*8d9149a7Sphx 		    BUS_SPACE_MAP_LINEAR);
765*8d9149a7Sphx 	} else {
766*8d9149a7Sphx 		return -1;
767*8d9149a7Sphx 	}
768*8d9149a7Sphx }
769*8d9149a7Sphx 
770*8d9149a7Sphx static int
zzfb_get_fbinfo(struct zzfb_softc * sc,struct wsdisplayio_fbinfo * fbi)771*8d9149a7Sphx zzfb_get_fbinfo(struct zzfb_softc *sc, struct wsdisplayio_fbinfo *fbi)
772*8d9149a7Sphx {
773*8d9149a7Sphx 	uint32_t bpA, bpR, bpG, bpB;
774*8d9149a7Sphx 
775*8d9149a7Sphx 	switch (sc->sc_bpp) {
776*8d9149a7Sphx 	case 8:
777*8d9149a7Sphx 		bpA = 0; bpR = 0; bpG = 0; bpB = 0;
778*8d9149a7Sphx 		break;
779*8d9149a7Sphx 	case 15:
780*8d9149a7Sphx 		bpA = 1; bpR = 5; bpG = 5; bpB = 5;
781*8d9149a7Sphx 		break;
782*8d9149a7Sphx 	case 16:
783*8d9149a7Sphx 		bpA = 0; bpR = 5; bpG = 6; bpB = 5;
784*8d9149a7Sphx 		break;
785*8d9149a7Sphx 	case 32:
786*8d9149a7Sphx 		bpA = 8; bpR = 8; bpG = 8; bpB = 8;
787*8d9149a7Sphx 		break;
788*8d9149a7Sphx 	default:
789*8d9149a7Sphx 		return EINVAL;
790*8d9149a7Sphx 	}
791*8d9149a7Sphx 
792*8d9149a7Sphx 	fbi->fbi_flags		= 0;
793*8d9149a7Sphx 	fbi->fbi_fboffset	= 0;
794*8d9149a7Sphx 	fbi->fbi_fbsize		= sc->sc_stride * sc->sc_height;
795*8d9149a7Sphx 	fbi->fbi_width 		= sc->sc_width;
796*8d9149a7Sphx 	fbi->fbi_height		= sc->sc_height;
797*8d9149a7Sphx 	fbi->fbi_stride		= sc->sc_stride;
798*8d9149a7Sphx 	fbi->fbi_bitsperpixel	= (sc->sc_bpp == 15) ? 16 : sc->sc_bpp;
799*8d9149a7Sphx 
800*8d9149a7Sphx 	switch (sc->sc_bpp) {
801*8d9149a7Sphx 	case 8:
802*8d9149a7Sphx 		fbi->fbi_pixeltype			    = WSFB_CI;
803*8d9149a7Sphx 		fbi->fbi_subtype.fbi_cmapinfo.cmap_entries  = ZZ9K_PALETTE_SIZE;
804*8d9149a7Sphx 		return 0;
805*8d9149a7Sphx 	case 15: /* ZZ9000 uses ARGB1555 format for 15 bpp */
806*8d9149a7Sphx 	case 16: /* ZZ9000 uses RGB565 format for 16 bpp */
807*8d9149a7Sphx 		fbi->fbi_subtype.fbi_rgbmasks.alpha_offset	= bpB+bpG+bpR;
808*8d9149a7Sphx 		fbi->fbi_subtype.fbi_rgbmasks.red_offset	= bpB+bpG;
809*8d9149a7Sphx 		fbi->fbi_subtype.fbi_rgbmasks.green_offset	= bpB;
810*8d9149a7Sphx 		fbi->fbi_subtype.fbi_rgbmasks.blue_offset	= 0;
811*8d9149a7Sphx 		break;
812*8d9149a7Sphx 	case 32: /* ZZ9000 uses BGRA8888 format for 32 bpp */
813*8d9149a7Sphx 		fbi->fbi_subtype.fbi_rgbmasks.alpha_offset	= 0;
814*8d9149a7Sphx 		fbi->fbi_subtype.fbi_rgbmasks.red_offset	= bpA;
815*8d9149a7Sphx 		fbi->fbi_subtype.fbi_rgbmasks.green_offset	= bpA+bpR;
816*8d9149a7Sphx 		fbi->fbi_subtype.fbi_rgbmasks.blue_offset	= bpA+bpR+bpG;
817*8d9149a7Sphx 		break;
818*8d9149a7Sphx 	default:
819*8d9149a7Sphx 		return EINVAL;
820*8d9149a7Sphx 	}
821*8d9149a7Sphx 
822*8d9149a7Sphx 	fbi->fbi_pixeltype				= WSFB_RGB;
823*8d9149a7Sphx 	fbi->fbi_subtype.fbi_rgbmasks.alpha_size	= bpA;
824*8d9149a7Sphx 	fbi->fbi_subtype.fbi_rgbmasks.red_size		= bpR;
825*8d9149a7Sphx 	fbi->fbi_subtype.fbi_rgbmasks.green_size	= bpG;
826*8d9149a7Sphx 	fbi->fbi_subtype.fbi_rgbmasks.blue_size		= bpB;
827*8d9149a7Sphx 
828*8d9149a7Sphx 	return 0;
829*8d9149a7Sphx }
830*8d9149a7Sphx 
831*8d9149a7Sphx static int
zzfb_getcmap(struct zzfb_softc * sc,struct wsdisplay_cmap * cm)832*8d9149a7Sphx zzfb_getcmap(struct zzfb_softc *sc, struct wsdisplay_cmap *cm)
833*8d9149a7Sphx {
834*8d9149a7Sphx 	int retval = 0;
835*8d9149a7Sphx 	u_int index = cm->index;
836*8d9149a7Sphx 	u_int count = cm->count;
837*8d9149a7Sphx 
838*8d9149a7Sphx 	if (index >= ZZ9K_PALETTE_SIZE || index + count > ZZ9K_PALETTE_SIZE)
839*8d9149a7Sphx 		return EINVAL;
840*8d9149a7Sphx 
841*8d9149a7Sphx 	retval = copyout(&sc->red[index], cm->red, count);
842*8d9149a7Sphx 	if (retval != 0)
843*8d9149a7Sphx 		return retval;
844*8d9149a7Sphx 
845*8d9149a7Sphx 	retval = copyout(&sc->green[index], cm->green, count);
846*8d9149a7Sphx 	if (retval != 0)
847*8d9149a7Sphx 		return retval;
848*8d9149a7Sphx 
849*8d9149a7Sphx 	retval = copyout(&sc->blue[index], cm->blue, count);
850*8d9149a7Sphx 	if (retval != 0)
851*8d9149a7Sphx 		return retval;
852*8d9149a7Sphx 
853*8d9149a7Sphx 	return retval;
854*8d9149a7Sphx }
855*8d9149a7Sphx 
856*8d9149a7Sphx static int
zzfb_putcmap(struct zzfb_softc * sc,struct wsdisplay_cmap * cm)857*8d9149a7Sphx zzfb_putcmap(struct zzfb_softc *sc, struct wsdisplay_cmap *cm)
858*8d9149a7Sphx {
859*8d9149a7Sphx 	int retval = 0;
860*8d9149a7Sphx 	u_int index = cm->index;
861*8d9149a7Sphx 	u_int count = cm->count;
862*8d9149a7Sphx 
863*8d9149a7Sphx 	if (index >= ZZ9K_PALETTE_SIZE || index + count > ZZ9K_PALETTE_SIZE)
864*8d9149a7Sphx 		return EINVAL;
865*8d9149a7Sphx 
866*8d9149a7Sphx 	retval = copyin(cm->red, &sc->red[index], count);
867*8d9149a7Sphx 	if (retval != 0)
868*8d9149a7Sphx 		return retval;
869*8d9149a7Sphx 
870*8d9149a7Sphx 	retval = copyin(cm->green, &sc->green[index], count);
871*8d9149a7Sphx 	if (retval != 0)
872*8d9149a7Sphx 		return retval;
873*8d9149a7Sphx 
874*8d9149a7Sphx 	retval = copyin(cm->blue, &sc->blue[index], count);
875*8d9149a7Sphx 	if (retval != 0)
876*8d9149a7Sphx 		return retval;
877*8d9149a7Sphx 
878*8d9149a7Sphx 	zzfb_set_palette(sc);
879*8d9149a7Sphx 	return retval;
880*8d9149a7Sphx }
881*8d9149a7Sphx 
882*8d9149a7Sphx /*
883*8d9149a7Sphx  * Early console handling, associated with amiga/conf.c file which holds a
884*8d9149a7Sphx  * table of all console devices. The below functions ensures that ZZ9000 becomes
885*8d9149a7Sphx  * wsdisplay0 and wskbd0 gets attached to it.
886*8d9149a7Sphx  */
887*8d9149a7Sphx 
888*8d9149a7Sphx /* early console handling */
889*8d9149a7Sphx void zzfb_cnprobe(struct consdev *cd);
890*8d9149a7Sphx void zzfb_cninit(struct consdev *cd);
891*8d9149a7Sphx void zzfb_cnpollc(dev_t cd, int on);
892*8d9149a7Sphx void zzfb_cnputc(dev_t cd, int ch);
893*8d9149a7Sphx int zzfb_cngetc(dev_t cd);
894*8d9149a7Sphx 
895*8d9149a7Sphx void
zzfb_cnprobe(struct consdev * cd)896*8d9149a7Sphx zzfb_cnprobe(struct consdev *cd)
897*8d9149a7Sphx {
898*8d9149a7Sphx #ifdef ZZFB_CONSOLE
899*8d9149a7Sphx 	if (zz9k_exists == true) {
900*8d9149a7Sphx 		cd->cn_pri = CN_INTERNAL;
901*8d9149a7Sphx 	} else {
902*8d9149a7Sphx 		cd->cn_pri = CN_DEAD;
903*8d9149a7Sphx 	}
904*8d9149a7Sphx 	cd->cn_dev = NODEV;
905*8d9149a7Sphx #endif /* ZZFB_CONSOLE */
906*8d9149a7Sphx }
907*8d9149a7Sphx 
908*8d9149a7Sphx void
zzfb_cninit(struct consdev * cd)909*8d9149a7Sphx zzfb_cninit(struct consdev *cd)
910*8d9149a7Sphx {
911*8d9149a7Sphx #if defined ZZFB_CONSOLE && NKBD > 0
912*8d9149a7Sphx 	/* tell kbd device it is used as console keyboard */
913*8d9149a7Sphx 	if (zz9k_exists == true)
914*8d9149a7Sphx 		kbd_cnattach();
915*8d9149a7Sphx #endif /* ZZFB_CONSOLE && NKBD > 0 */
916*8d9149a7Sphx }
917*8d9149a7Sphx 
918*8d9149a7Sphx void
zzfb_cnpollc(dev_t cd,int on)919*8d9149a7Sphx zzfb_cnpollc(dev_t cd, int on)
920*8d9149a7Sphx {
921*8d9149a7Sphx }
922*8d9149a7Sphx 
923*8d9149a7Sphx void
zzfb_cnputc(dev_t cd,int ch)924*8d9149a7Sphx zzfb_cnputc(dev_t cd, int ch)
925*8d9149a7Sphx {
926*8d9149a7Sphx }
927*8d9149a7Sphx 
928*8d9149a7Sphx int
zzfb_cngetc(dev_t cd)929*8d9149a7Sphx zzfb_cngetc(dev_t cd)
930*8d9149a7Sphx {
931*8d9149a7Sphx 	return 0;
932*8d9149a7Sphx }
933