summaryrefslogtreecommitdiff
path: root/sys/arch/macppc/dev/valkyriefb.c
blob: 418da91f8df9a21199498c2f9f7c55581eb6aea6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/*	$NetBSD: valkyriefb.c,v 1.8 2022/02/16 23:49:26 riastradh Exp $	*/

/*
 * Copyright (c) 2012 Michael Lorenz
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/* 
 * A console driver for Apple's Valkyrie onboard video controller, found in
 * for example the Performa 6360
 * This should be easy enough to adapt to mac68k but I don't have the hardware
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: valkyriefb.c,v 1.8 2022/02/16 23:49:26 riastradh Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/device.h>

#include <dev/ofw/openfirm.h>

#include <machine/autoconf.h>

#include <uvm/uvm_extern.h>
#include <powerpc/oea/pmap.h>

#include <dev/wscons/wsdisplayvar.h>
#include <dev/wscons/wsconsio.h>
#include <dev/wsfont/wsfont.h>
#include <dev/rasops/rasops.h>
#include <dev/wscons/wsdisplay_vconsvar.h>

#include <dev/videomode/videomode.h>

#include <arch/macppc/dev/valkyriefbreg.h>
#include <arch/macppc/dev/videopllvar.h>

#include "opt_wsemul.h"
#include "opt_valkyriefb.h"

#include "videopll.h"
#if NVIDEOPLL < 1
#error "valkyriefb requires videopll at iic"
#endif

struct valkyriefb_softc {
	device_t sc_dev;
	int sc_node;
	uint8_t *sc_base;
	uint8_t *sc_fbaddr;

	int sc_depth;
	int sc_width, sc_height, sc_linebytes;
	const struct videomode *sc_videomode;
	uint8_t sc_modereg;

	int sc_mode;
	uint32_t sc_bg;
		
	u_char sc_cmap_red[256];
	u_char sc_cmap_green[256];
	u_char sc_cmap_blue[256];	

	struct vcons_data vd;
};

struct valkyriefb_mode {
	int width;
	int height;
	uint8_t mode;
};

/*
 * Translate screen resolutions to values for Valkyrie's mode register.
 * These numbers seem to roughly correspond to MacOS video mode numbers
 * there are many numbers that result in the same resolution which in MacOS
 * only differ by the display refresh rate, which isn't set by Valkyrie at
 * all, instead there's a PLL chip hooked to cuda's i2c bus. It doesn't seem
 * to matter which number we use as long as the resolution is right and we
 * program the PLL for the right pixel clock
 */
static struct valkyriefb_mode modetab[] = {
	{  640, 480, 6 },
	{  800, 600, 12 },
	{  832, 624, 9 },
	{ 1024, 768, 14},
};
	
static struct vcons_screen valkyriefb_console_screen;

static int	valkyriefb_match(device_t, cfdata_t, void *);
static void	valkyriefb_attach(device_t, device_t, void *);
static int	valkyriefb_init(device_t);
static int 	valkyriefb_set_mode(struct valkyriefb_softc *,
		    const struct videomode *, int);

CFATTACH_DECL_NEW(valkyriefb, sizeof(struct valkyriefb_softc),
    valkyriefb_match, valkyriefb_attach, NULL, NULL);

struct wsscreen_descr valkyriefb_defaultscreen = {
	"default",
	0, 0,
	NULL,
	8, 16,
	WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
	NULL,
};

const struct wsscreen_descr *_valkyriefb_scrlist[] = {
	&valkyriefb_defaultscreen,
	/* XXX other formats, graphics screen? */
};

struct wsscreen_list valkyriefb_screenlist = {
	sizeof(_valkyriefb_scrlist) / sizeof(struct wsscreen_descr *),
	_valkyriefb_scrlist
};

static int	valkyriefb_ioctl(void *, void *, u_long, void *, int,
		    struct lwp *);
static paddr_t	valkyriefb_mmap(void *, void *, off_t, int);

static void	valkyriefb_init_screen(void *, struct vcons_screen *, int,
			    long *);


struct wsdisplay_accessops valkyriefb_accessops = {
	valkyriefb_ioctl,
	valkyriefb_mmap,
	NULL,
	NULL,
	NULL,
	NULL,	/* load_font */
	NULL,	/* polls */
	NULL,	/* scroll */
};

static inline void
valkyriefb_write_reg(struct valkyriefb_softc *sc, int reg, uint8_t val)
{
	*(sc->sc_base + VAL_REGS_OFFSET + reg) = val;
	__asm volatile("eieio; sync;" ::: "memory");
}

#ifdef VALKYRIEFB_DEBUG
static inline uint8_t
valkyriefb_read_reg(struct valkyriefb_softc *sc, int reg)
{
	return *(sc->sc_base + VAL_REGS_OFFSET + reg);
}
#endif

static void
valkyriefb_write_cmap(struct valkyriefb_softc *sc,
    int reg, uint8_t r, uint8_t g, uint8_t b)
{
	*(sc->sc_base + VAL_CMAP_OFFSET + VAL_CMAP_ADDR) = reg;
	__asm volatile("eieio; sync;" ::: "memory");
	*(sc->sc_base + VAL_CMAP_OFFSET + VAL_CMAP_LUT) = r;
	__asm volatile("eieio; sync;" ::: "memory");
	*(sc->sc_base + VAL_CMAP_OFFSET + VAL_CMAP_LUT) = g;
	__asm volatile("eieio; sync;" ::: "memory");
	*(sc->sc_base + VAL_CMAP_OFFSET + VAL_CMAP_LUT) = b;
	__asm volatile("eieio; sync;" ::: "memory");
}

static int
valkyriefb_match(device_t parent, cfdata_t cf, void *aux)
{
	struct confargs *ca = aux;

	if (strcmp(ca->ca_name, "valkyrie") != 0)
		return 0;
	return 1;
}

static void
valkyriefb_attach(device_t parent, device_t self, void *aux)
{
	struct valkyriefb_softc *sc = device_private(self);
	struct confargs *ca = aux;

	sc->sc_dev = self;
	sc->sc_node = ca->ca_node;

	aprint_normal(" address 0x%08x\n", ca->ca_reg[0]);
	aprint_verbose_dev(sc->sc_dev, "waiting for videopll...\n");
	sc->sc_base = (uint8_t *)ca->ca_reg[0];
#ifdef VALKYRIEFB_DEBUG
	for (i = 0; i < 0x40; i += 8) {
		aprint_error_dev(sc->sc_dev, "%02x: %02x\n", i,
		    valkyriefb_read_reg(sc, i));
	}
#endif
	config_finalize_register(sc->sc_dev, valkyriefb_init);
	sc->sc_fbaddr = (uint8_t *)(sc->sc_base + 0x1000);
}

static int
valkyriefb_init(device_t self)
{
	struct valkyriefb_softc *sc = device_private(self);
	const struct videomode *mode;
	struct rasops_info *ri;
	prop_dictionary_t dict;
	struct wsemuldisplaydev_attach_args aa;
	bool console = FALSE;
	long defattr;

	mode = pick_mode_by_ref(800, 600, 60);
	if (mode == NULL)
		return 0;

	valkyriefb_set_mode(sc, mode, 8);

	vcons_init(&sc->vd, sc, &valkyriefb_defaultscreen,
	    &valkyriefb_accessops);
	sc->vd.init_screen = valkyriefb_init_screen;

	dict = device_properties(sc->sc_dev);
	prop_dictionary_get_bool(dict, "is_console", &console);

	ri = &valkyriefb_console_screen.scr_ri;
	vcons_init_screen(&sc->vd, &valkyriefb_console_screen, 1, &defattr);
	memset(sc->sc_base + 0x1000, ri->ri_devcmap[(defattr >> 16) & 0xf],
	    sc->sc_width * sc->sc_linebytes);
	valkyriefb_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;

	valkyriefb_defaultscreen.textops = &ri->ri_ops;
	valkyriefb_defaultscreen.capabilities = ri->ri_caps;
	valkyriefb_defaultscreen.nrows = ri->ri_rows;
	valkyriefb_defaultscreen.ncols = ri->ri_cols;
	if (console) {
		wsdisplay_cnattach(&valkyriefb_defaultscreen, ri, 0, 0,
		    defattr);
		vcons_replay_msgbuf(&valkyriefb_console_screen);
	}
	aa.console = console;
	aa.scrdata = &valkyriefb_screenlist;
	aa.accessops = &valkyriefb_accessops;
	aa.accesscookie = &sc->vd;

	config_found(self, &aa, wsemuldisplaydevprint, CFARGS_NONE);

	return 0;
}

static int
valkyriefb_set_mode(struct valkyriefb_softc *sc,
    const struct videomode *mode, int depth)
{
	int i;
	uint8_t modereg, tmp;

	/* first find the parameter for the mode register */
	i = 0;
	while ((i < __arraycount(modetab)) && 
	       (modetab[i].width != mode->hdisplay) &&
	       (modetab[i].height != mode->vdisplay))
		i++;
	if (i >= __arraycount(modetab)) {
		aprint_error_dev(sc->sc_dev,
		    "Can't find a mode register value for %s\n", mode->name);
		return EINVAL;
	} else
		modereg = modetab[i].mode;

	/* check if we have enough video memory */
	if ((mode->hdisplay * mode->vdisplay * (depth >> 3)) > 0x100000) {
		aprint_error_dev(sc->sc_dev, "Not enough video RAM for %s\n",
		    mode->name);
		return EINVAL;
	}

	/* reject depths other than 8 or 16 */
	if ((depth != 8) && (depth != 16)) {
		aprint_error_dev(sc->sc_dev,
		    "Depth [%d] is unsupported for %s\n", depth, mode->name);
		return EINVAL;
	}

	/* now start programming the chip */
	valkyriefb_write_reg(sc, VAL_STATUS, 0);
	delay(100);
	valkyriefb_write_reg(sc, VAL_MODE, modereg | VAL_MODE_STOP);

	if (depth == 8) {
		sc->sc_depth = 8;
		valkyriefb_write_reg(sc, VAL_DEPTH, VAL_DEPTH_8);
		for (i = 0; i < 256; i++) {
			tmp = i & 0xe0;
			/*
			 * replicate bits so 0xe0 maps to a red value of 0xff
			 * in order to make white look actually white
			 */
			tmp |= (tmp >> 3) | (tmp >> 6);
			sc->sc_cmap_red[i] = tmp;

			tmp = (i & 0x1c) << 3;
			tmp |= (tmp >> 3) | (tmp >> 6);
			sc->sc_cmap_green[i] = tmp;

			tmp = (i & 0x03) << 6;
			tmp |= tmp >> 2;
			tmp |= tmp >> 4;
			sc->sc_cmap_blue[i] = tmp;

			valkyriefb_write_cmap(sc, i, sc->sc_cmap_red[i],
			    sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
		}
	} else if (depth == 16) {
		sc->sc_depth = 16;
		valkyriefb_write_reg(sc, VAL_DEPTH, VAL_DEPTH_16);
		/* does the palette have any effect in 16bit? */
	}

	videopll_set_freq(mode->dot_clock);
	delay(100);
	valkyriefb_write_reg(sc, VAL_MODE, modereg);

	sc->sc_modereg = modereg;
	sc->sc_videomode = mode;
	sc->sc_width = mode->hdisplay;
	sc->sc_height = mode->vdisplay;
	sc->sc_linebytes = mode->hdisplay * (sc->sc_depth >> 3);
	aprint_normal_dev(sc->sc_dev, "switched to %d x %d in %d bit colour\n",
	    sc->sc_width, sc->sc_height, sc->sc_depth);
	return 0;
}

static int
valkyriefb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
	struct lwp *l)
{
	struct vcons_data *vd = v;
	struct valkyriefb_softc *sc = vd->cookie;
	struct wsdisplay_fbinfo *wdf;
	struct vcons_screen *ms = vd->active;

	switch (cmd) {
	case WSDISPLAYIO_GTYPE:
		*(u_int *)data = WSDISPLAY_TYPE_VALKYRIE;
		return 0;

	case WSDISPLAYIO_GINFO:
		wdf = (void *)data;
		wdf->height = ms->scr_ri.ri_height;
		wdf->width = ms->scr_ri.ri_width;
		wdf->depth = ms->scr_ri.ri_depth;
		wdf->cmsize = 256;
		return 0;
#if 0	
	case WSDISPLAYIO_GETCMAP:
		return valkyriefb_getcmap(sc,
		    (struct wsdisplay_cmap *)data);

	case WSDISPLAYIO_PUTCMAP:
		return valkyriefb_putcmap(sc,
		    (struct wsdisplay_cmap *)data);
#endif

	case WSDISPLAYIO_SMODE: {
		int new_mode = *(int*)data;
		if (new_mode != sc->sc_mode) {
			sc->sc_mode = new_mode;
			if (new_mode == WSDISPLAYIO_MODE_EMUL) {
				vcons_redraw_screen(ms);
			}
		}
		}
		return 0;
	}
	return EPASSTHROUGH;
}

static paddr_t
valkyriefb_mmap(void *v, void *vs, off_t offset, int prot)
{
	struct vcons_data *vd = v;
	struct valkyriefb_softc *sc = vd->cookie;
	paddr_t pa;
		
	/* 'regular' framebuffer mmap()ing */
	if (offset < 0x100000) {
		pa = (paddr_t)(sc->sc_base + 0x1000 + offset);	
		pa |= POWERPC_MMAP_FLAG_PREFETCHABLE;
		return pa;
	}
	return -1;
}

static void
valkyriefb_init_screen(void *cookie, struct vcons_screen *scr,
    int existing, long *defattr)
{
	struct valkyriefb_softc *sc = cookie;
	struct rasops_info *ri = &scr->scr_ri;
	
	memset(ri, 0, sizeof(struct rasops_info));
	ri->ri_depth = sc->sc_depth;
	ri->ri_width = sc->sc_width;
	ri->ri_height = sc->sc_height;
	ri->ri_stride = sc->sc_linebytes;
	ri->ri_flg = RI_CENTER | RI_8BIT_IS_RGB | RI_ENABLE_ALPHA;
	ri->ri_bits = sc->sc_fbaddr;

	scr->scr_flags |= VCONS_DONT_READ;
	
	rasops_init(ri, 0, 0);
	ri->ri_caps = WSSCREEN_WSCOLORS;

	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
		    sc->sc_width / ri->ri_font->fontwidth);

	ri->ri_hw = scr;
}