1/* $NetBSD: macfb.c,v 1.23 2021/08/07 16:18:57 thorpej Exp $ */
2/*
3 * Copyright (c) 1998 Matt DeBergalis
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 *    must display the following acknowledgement:
16 *      This product includes software developed by Matt DeBergalis
17 * 4. The name of the author may not be used to endorse or promote products
18 *    derived from this software without specific prior written permission
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
33__KERNEL_RCSID(0, "$NetBSD: macfb.c,v 1.23 2021/08/07 16:18:57 thorpej Exp $");
34
35#include "opt_wsdisplay_compat.h"
36#include "grf.h"
37
38#include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
39
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/kernel.h>
43#include <sys/device.h>
44#include <sys/kmem.h>
45
46#include <machine/cpu.h>
47#include <machine/bus.h>
48
49#include <machine/video.h>
50#include <machine/grfioctl.h>
51#include <mac68k/nubus/nubus.h>
52#include <mac68k/dev/grfvar.h>
53#include <mac68k/dev/macfbvar.h>
54#include <dev/wscons/wsconsio.h>
55
56#include <dev/rcons/raster.h>
57#include <dev/wscons/wscons_raster.h>
58#include <dev/wscons/wsdisplayvar.h>
59
60int macfb_match(device_t, cfdata_t, void *);
61void macfb_attach(device_t, device_t, void *);
62
63CFATTACH_DECL_NEW(macfb, sizeof(struct macfb_softc),
64    macfb_match, macfb_attach, NULL, NULL);
65
66const struct wsdisplay_emulops macfb_emulops = {
67	rcons_cursor,
68	rcons_mapchar,
69	rcons_putchar,
70	rcons_copycols,
71	rcons_erasecols,
72	rcons_copyrows,
73	rcons_eraserows,
74	rcons_allocattr
75};
76
77struct wsscreen_descr macfb_stdscreen = {
78	"std",
79	0, 0, /* will be filled in -- XXX shouldn't, it's global */
80	&macfb_emulops,
81	0, 0,
82	WSSCREEN_REVERSE
83};
84
85const struct wsscreen_descr *_macfb_scrlist[] = {
86	&macfb_stdscreen,
87};
88
89const struct wsscreen_list macfb_screenlist = {
90	sizeof(_macfb_scrlist) / sizeof(struct wsscreen_descr *),
91	_macfb_scrlist
92};
93
94static int	macfb_ioctl(void *, void *, u_long, void *, int, struct lwp *);
95static paddr_t	macfb_mmap(void *, void *, off_t, int);
96static int	macfb_alloc_screen(void *, const struct wsscreen_descr *,
97		    void **, int *, int *, long *);
98static void	macfb_free_screen(void *, void *);
99static int	macfb_show_screen(void *, void *, int,
100		    void (*)(void *, int, int), void *);
101
102const struct wsdisplay_accessops macfb_accessops = {
103	macfb_ioctl,
104	macfb_mmap,
105	macfb_alloc_screen,
106	macfb_free_screen,
107	macfb_show_screen,
108	0 /* load_font */
109};
110
111void macfb_init(struct macfb_devconfig *);
112
113paddr_t macfb_consaddr;
114static int macfb_is_console(paddr_t);
115#ifdef WSDISPLAY_COMPAT_ITEFONT
116static void	init_itefont(void);
117#endif /* WSDISPLAY_COMPAT_ITEFONT */
118
119static struct macfb_devconfig macfb_console_dc;
120
121static int
122macfb_is_console(paddr_t addr)
123{
124	if (addr != macfb_consaddr &&
125	    (addr >= 0xf9000000 && addr <= 0xfeffffff)) {
126		/*
127		 * This is in the NuBus standard slot space range, so we
128		 * may well have to look at 0xFssxxxxx, too.  Mask off the
129		 * slot number and duplicate it in bits 20-23, per IM:V
130		 * pp 459, 463, and IM:VI ch 30 p 17.
131		 * Note:  this is an ugly hack and I wish I knew what
132		 * to do about it.  -- sr
133		 */
134		addr = (paddr_t)(((u_long)addr & 0xff0fffff) |
135		    (((u_long)addr & 0x0f000000) >> 4));
136	}
137	return ((mac68k_machine.serial_console & 0x03) == 0
138	    && (addr == macfb_consaddr));
139}
140
141void
142macfb_clear(struct macfb_devconfig *dc)
143{
144	int i, rows;
145
146	/* clear the display */
147	rows = dc->dc_ht;
148	for (i = 0; rows-- > 0; i += dc->dc_rowbytes)
149		memset((u_char *)dc->dc_vaddr + dc->dc_offset + i,
150		    0, dc->dc_rowbytes);
151}
152
153void
154macfb_init(struct macfb_devconfig *dc)
155{
156	struct raster *rap;
157	struct rcons *rcp;
158
159	macfb_clear(dc);
160
161#ifdef WSDISPLAY_COMPAT_ITEFONT
162	init_itefont();
163#endif /* WSDISPLAY_COMPAT_ITEFONT */
164
165	rap = &dc->dc_raster;
166	rap->width = dc->dc_wid;
167	rap->height = dc->dc_ht;
168	rap->depth = dc->dc_depth;
169	rap->linelongs = dc->dc_rowbytes / sizeof(u_int32_t);
170	rap->pixels = (u_int32_t *)(dc->dc_vaddr + dc->dc_offset);
171
172	/* initialize the raster console blitter */
173	rcp = &dc->dc_rcons;
174	rcp->rc_sp = rap;
175	rcp->rc_crow = rcp->rc_ccol = -1;
176	rcp->rc_crowp = &rcp->rc_crow;
177	rcp->rc_ccolp = &rcp->rc_ccol;
178	rcons_init(rcp, 128, 192);
179
180	macfb_stdscreen.nrows = dc->dc_rcons.rc_maxrow;
181	macfb_stdscreen.ncols = dc->dc_rcons.rc_maxcol;
182}
183
184int
185macfb_match(device_t parent, cfdata_t match, void *aux)
186{
187	return (1);
188}
189
190void
191macfb_attach(device_t parent, device_t self, void *aux)
192{
193	struct grfbus_attach_args *ga = aux;
194	struct grfmode *gm = ga->ga_grfmode;
195	struct macfb_softc *sc;
196	struct wsemuldisplaydev_attach_args waa;
197	int isconsole;
198
199	sc = device_private(self);
200
201	printf("\n");
202
203	isconsole = macfb_is_console(ga->ga_phys + ga->ga_grfmode->fboff);
204
205	if (isconsole) {
206		sc->sc_dc = &macfb_console_dc;
207		sc->nscreens = 1;
208	} else {
209		sc->sc_dc = kmem_alloc(sizeof(struct macfb_devconfig),
210		    KM_SLEEP);
211		sc->sc_dc->dc_vaddr = (vaddr_t)gm->fbbase;
212		sc->sc_dc->dc_paddr = ga->ga_phys;
213		sc->sc_dc->dc_size = gm->fbsize;
214
215		sc->sc_dc->dc_wid = gm->width;
216		sc->sc_dc->dc_ht = gm->height;
217		sc->sc_dc->dc_depth = gm->psize;
218		sc->sc_dc->dc_rowbytes = gm->rowbytes;
219
220		sc->sc_dc->dc_offset = gm->fboff;
221
222		macfb_clear(sc->sc_dc);
223
224		sc->nscreens = 1;
225	}
226
227	/* initialize the raster */
228	waa.console = isconsole;
229	waa.scrdata = &macfb_screenlist;
230	waa.accessops = &macfb_accessops;
231	waa.accesscookie = sc;
232
233	config_found(self, &waa, wsemuldisplaydevprint, CFARGS_NONE);
234
235#if NGRF > 0
236	grf_attach(sc, device_unit(self));
237#endif
238}
239
240
241int
242macfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
243	struct lwp *l)
244{
245	struct macfb_softc *sc = v;
246	struct macfb_devconfig *dc = sc->sc_dc;
247	struct wsdisplay_fbinfo *wdf;
248
249	switch (cmd) {
250	case WSDISPLAYIO_GTYPE:
251		*(int *)data = dc->dc_type;
252		return 0;
253
254	case WSDISPLAYIO_GINFO:
255		wdf = (struct wsdisplay_fbinfo *)data;
256		wdf->height = dc->dc_raster.height;
257		wdf->width = dc->dc_raster.width;
258		wdf->depth = dc->dc_raster.depth;
259		wdf->cmsize = 256;
260		return 0;
261
262	case WSDISPLAYIO_GCURMAX:
263	case WSDISPLAYIO_GCURPOS:
264	case WSDISPLAYIO_GCURSOR:
265	case WSDISPLAYIO_GETCMAP:
266	case WSDISPLAYIO_GVIDEO:
267	case WSDISPLAYIO_PUTCMAP:
268	case WSDISPLAYIO_SCURPOS:
269	case WSDISPLAYIO_SCURSOR:
270	case WSDISPLAYIO_SVIDEO:
271		/* NONE of these operations are supported. */
272		return EPASSTHROUGH;
273	}
274
275	return EPASSTHROUGH;
276}
277
278static paddr_t
279macfb_mmap(void *v, void *vs, off_t offset, int prot)
280{
281	struct macfb_softc *sc = v;
282	struct macfb_devconfig *dc = sc->sc_dc;
283	paddr_t addr;
284
285	if (offset >= 0 &&
286	    offset < m68k_round_page(dc->dc_rowbytes * dc->dc_ht))
287		addr = m68k_btop(dc->dc_paddr + dc->dc_offset + offset);
288	else
289		addr = (-1);	/* XXX bogus */
290
291	return addr;
292}
293
294int
295macfb_alloc_screen(void *v, const struct wsscreen_descr *type, void **cookiep,
296    int *curxp, int *curyp, long *defattrp)
297{
298	struct macfb_softc *sc = v;
299	long defattr;
300
301	if (sc->nscreens > 0)
302		return (ENOMEM);
303
304	*cookiep = &sc->sc_dc->dc_rcons; /* one and only for now */
305	*curxp = 0;
306	*curyp = 0;
307	rcons_allocattr(&sc->sc_dc->dc_rcons, 0, 0, 0, &defattr);
308	*defattrp = defattr;
309	sc->nscreens++;
310	return (0);
311}
312
313void
314macfb_free_screen(void *v, void *cookie)
315{
316	struct macfb_softc *sc = v;
317
318	if (sc->sc_dc == &macfb_console_dc)
319		panic("cfb_free_screen: console");
320
321	sc->nscreens--;
322}
323
324int
325macfb_show_screen(void *v, void *cookie, int waitok,
326    void (*cb)(void *, int, int), void *cbarg)
327{
328	return 0;
329}
330
331int
332macfb_cnattach(paddr_t addr)
333{
334	struct macfb_devconfig *dc = &macfb_console_dc;
335	long defattr;
336
337	dc->dc_vaddr = m68k_trunc_page(mac68k_video.mv_kvaddr);
338	dc->dc_paddr = m68k_trunc_page(mac68k_video.mv_phys);
339
340	dc->dc_wid = mac68k_video.mv_width;
341	dc->dc_ht = mac68k_video.mv_height;
342	dc->dc_depth = mac68k_video.mv_depth;
343	dc->dc_rowbytes = mac68k_video.mv_stride;
344
345	dc->dc_size = (mac68k_video.mv_len > 0) ?
346	    mac68k_video.mv_len : dc->dc_ht * dc->dc_rowbytes;
347	dc->dc_offset = m68k_page_offset(mac68k_video.mv_phys);
348
349	/* set up the display */
350	macfb_init(&macfb_console_dc);
351
352	rcons_allocattr(&dc->dc_rcons, 0, 0, 0, &defattr);
353
354	wsdisplay_cnattach(&macfb_stdscreen, &dc->dc_rcons,
355			0, 0, defattr);
356
357	macfb_consaddr = addr;
358	dc->isconsole = 1;
359	return (0);
360}
361
362#ifdef WSDISPLAY_COMPAT_ITEFONT
363#include <mac68k/dev/6x10.h>
364
365void
366init_itefont(void)
367{
368	static int itefont_initted;
369	int i, j;
370
371	extern struct raster_font gallant19;		/* XXX */
372
373	if (itefont_initted)
374		return;
375	itefont_initted = 1;
376
377	/* XXX but we cannot use kmem_alloc here... */
378	gallant19.width = 6;
379	gallant19.height = 10;
380	gallant19.ascent = 0;
381
382	for (i = 32; i < 128; i++) {
383		u_int *p;
384
385		if (gallant19.chars[i].r == NULL)
386			continue;
387
388		gallant19.chars[i].r->width = 6;
389		gallant19.chars[i].r->height = 10;
390		p = gallant19.chars[i].r->pixels;
391
392		for (j = 0; j < 10; j++)
393			*p++ = Font6x10[i * 10 + j] << 26;
394	}
395}
396#endif /* WSDISPLAY_COMPAT_ITEFONT */
397