summaryrefslogtreecommitdiff
path: root/sys/arch/hp300/dev/diofb.c
blob: 1c825b51dfadd31ca11976519264f62459d7ef0a (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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/*	$OpenBSD: diofb.c,v 1.5 2005/01/18 21:53:23 miod Exp $	*/

/*
 * Copyright (c) 2005, Miodrag Vallat
 *
 * 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.
 */
/*
 * Copyright (c) 1988 University of Utah.
 * Copyright (c) 1990, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * the Systems Programming Group of the University of Utah Computer
 * Science Department.
 *
 * 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.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``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 REGENTS OR CONTRIBUTORS 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.
 */

#include <sys/param.h>
#include <sys/conf.h>
#include <sys/proc.h>
#include <sys/ioctl.h>
#include <sys/tty.h>
#include <sys/systm.h>
#include <sys/device.h>

#include <machine/autoconf.h>
#include <machine/cpu.h>

#include <hp300/dev/dioreg.h>
#include <hp300/dev/diovar.h>

#include <dev/wscons/wsdisplayvar.h>

#include <hp300/dev/diofbreg.h>
#include <hp300/dev/diofbvar.h>

/*
 * X and Y location of character 'c' in the framebuffer, in pixels.
 */
#define	charX(fb,c)	\
	(((c) % (fb)->cpl) * (fb)->ftscale + (fb)->fontx)
#define	charY(fb,c)	\
	(((c) / (fb)->cpl) * (fb)->ftheight + (fb)->fonty)

void diofb_fontcopy(struct diofb *, char *, char *);

int	diofb_mapchar(void *, int, unsigned int *);
void	diofb_cursor(void *, int, int, int);
void	diofb_putchar(void *, int, int, u_int, long);
void	diofb_copycols(void *, int, int, int, int);
void	diofb_erasecols(void *, int, int, int, long);
void	diofb_copyrows(void *, int, int, int);
void	diofb_eraserows(void *, int, int, long);

const struct wsdisplay_emulops	diofb_emulops = {
	diofb_cursor,
	diofb_mapchar,
	diofb_putchar,
	diofb_copycols,
	diofb_erasecols,
	diofb_copyrows,
	diofb_eraserows,
	diofb_alloc_attr
};

/*
 * Frame buffer geometry initialization
 */

int
diofb_fbinquire(struct diofb *fb, int scode, struct diofbreg *fbr)
{
	int fboff, regsize;

	if (ISIIOVA(fbr))
		fb->regaddr = (caddr_t)IIOP(fbr);
	else
		fb->regaddr = dio_scodetopa(scode);

	if (fb->fbwidth == 0 || fb->fbheight == 0) {
		fb->fbwidth = (fbr->fbwmsb << 8) | fbr->fbwlsb;
		fb->fbheight = (fbr->fbhmsb << 8) | fbr->fbhlsb;
	}
	fb->fbsize = fb->fbwidth * fb->fbheight;

	fboff = (fbr->fbomsb << 8) | fbr->fbolsb;
	fb->fbaddr = (caddr_t) (*((u_char *)fbr + fboff) << 16);

	if (fb->regaddr >= (caddr_t)DIOII_BASE) {
		/*
		 * For DIO II space the fbaddr just computed is
		 * the offset from the select code base (regaddr)
		 * of the framebuffer.  Hence it is also implicitly
		 * the size of the set.
		 */
		regsize = (int)fb->fbaddr;
		fb->fbaddr += (int)fb->regaddr;
		fb->regkva = (caddr_t)fbr;
		fb->fbkva = (caddr_t)fbr + regsize;
	} else {
		/*
		 * For DIO space we need to map the separate
		 * framebuffer.
		 */
		fb->regkva = (caddr_t)fbr;
		fb->fbkva = iomap(fb->fbaddr, fb->fbsize);
		if (fb->fbkva == NULL)
			return (ENOMEM);
	}
	if (fb->dwidth == 0 || fb->dheight == 0) {
		fb->dwidth = (fbr->dwmsb << 8) | fbr->dwlsb;
		fb->dheight = (fbr->dhmsb << 8) | fbr->dhlsb;
	}

	/*
	 * Some displays, such as the DaVinci, appear to return a display
	 * height larger than the frame buffer height.
	 */
	if (fb->dwidth > fb->fbwidth)
		fb->dwidth = fb->fbwidth;
	if (fb->dheight > fb->fbheight)
		fb->dheight = fb->fbheight;

	return (0);
}

/*
 * PROM font setup
 */

void
diofb_fbsetup(struct diofb *fb)
{
	u_long fontaddr = getword(fb, getword(fb, FONTROM) + FONTADDR);

	/*
	 * Get font metrics.
	 */
	fb->ftheight = getbyte(fb, fontaddr + FONTHEIGHT);
	fb->ftwidth = getbyte(fb, fontaddr + FONTWIDTH);
	fb->ftscale = fb->ftwidth;
	fb->rows = fb->dheight / fb->ftheight;
	fb->cols = fb->dwidth / fb->ftwidth;

	/*
	 * Decide where to put the font in off-screen memory.
	 */
	if (fb->fbwidth > fb->dwidth) {
		/* Unpacked font will be to the right of the display */
		fb->fontx = fb->dwidth;
		fb->fonty = 0;
		fb->cpl = (fb->fbwidth - fb->dwidth) / fb->ftwidth;
		fb->cblankx = fb->dwidth;
	} else {
		/* Unpacked font will be below the display */
		fb->fontx = 0;
		fb->fonty = fb->dheight;
		fb->cpl = fb->fbwidth / fb->ftwidth;
		fb->cblankx = 0;
	}
	fb->cblanky = fb->fonty + ((FONTMAXCHAR / fb->cpl) + 1) * fb->ftheight;

	/*
	 * Clear display
	 */
	(*fb->bmv)(fb, 0, 0, 0, 0, fb->fbwidth, fb->fbheight, RR_CLEAR);
	fb->curvisible = 0;

	/*
	 * Setup inverted cursor.
	 */
	(*fb->bmv)(fb, charX(fb, ' '), charY(fb, ' '),
	    fb->cblankx, fb->cblanky, fb->ftwidth, fb->ftheight,
	    RR_COPYINVERTED);

	/*
	 * Default colormap
	 */
	bzero(&fb->cmap, sizeof(fb->cmap));
	fb->cmap.r[1] = 0xff;
	fb->cmap.g[1] = 0xff;
	fb->cmap.b[1] = 0xff;
	fb->cmap.r[(1 << fb->planes) - 1] = 0xff;
	fb->cmap.g[(1 << fb->planes) - 1] = 0xff;
	fb->cmap.b[(1 << fb->planes) - 1] = 0xff;

	strlcpy(fb->wsd.name, "std", sizeof(fb->wsd.name));
	fb->wsd.ncols = fb->cols;
	fb->wsd.nrows = fb->rows;
	fb->wsd.textops = &diofb_emulops;
	fb->wsd.fontwidth = fb->ftwidth;
	fb->wsd.fontheight = fb->ftheight;
	fb->wsd.capabilities = WSSCREEN_REVERSE;
}

void
diofb_fontunpack(struct diofb *fb)
{
	char fontbuf[500];		/* XXX malloc not initialized yet */
	char *dp, *fbmem;
	int bytewidth, glyphsize;
	int c, i, romp;

	/*
	 * Unpack PROM font to the off-screen location.
	 */
	bytewidth = (((fb->ftwidth - 1) / 8) + 1);
	glyphsize = bytewidth * fb->ftheight;
	romp = getword(fb, getword(fb, FONTROM) + FONTADDR) + FONTDATA;
	for (c = 0; c < FONTMAXCHAR; c++) {
		fbmem = (char *)(FBBASE(fb) +
		     (fb->fonty + (c / fb->cpl) * fb->ftheight) * fb->fbwidth +
		     (fb->fontx + (c % fb->cpl) * fb->ftwidth));
		dp = fontbuf;
		for (i = 0; i < glyphsize; i++) {
			*dp++ = getbyte(fb, romp);
			romp += 2;
		}
		diofb_fontcopy(fb, fbmem, fontbuf);
	}
}

void
diofb_fontcopy(struct diofb *fb, char *fbmem, char *glyphp)
{
	int bn;
	int l, b;

	for (l = 0; l < fb->ftheight; l++) {
		bn = 7;
		for (b = 0; b < fb->ftwidth; b++) {
			if ((1 << bn) & *glyphp)
				*fbmem++ = 1;
			else
				*fbmem++ = 0;
			if (--bn < 0) {
				bn = 7;
				glyphp++;
			}
		}
		if (bn < 7)
			glyphp++;
		fbmem -= fb->ftwidth;
		fbmem += fb->fbwidth;
	}
}

/*
 * Attachment helper
 */
void
diofb_end_attach(void *sc, struct wsdisplay_accessops *accessops,
    struct diofb *fb, int console, int planes, const char *descr)
{
	struct wsemuldisplaydev_attach_args waa;
	struct wsscreen_descr *scrlist[1];
	struct wsscreen_list screenlist;

	printf(": %dx%d", fb->dwidth, fb->dheight);

	if (planes == 0)
		planes = fb->planes;
	if (planes == 1)
		printf(" monochrome");
	else
		printf("x%d", planes);

	if (descr != NULL)
		printf(" %s", descr);
	printf(" frame buffer\n");

	scrlist[0] = &fb->wsd;
	screenlist.nscreens = 1;
	screenlist.screens = (const struct wsscreen_descr **)scrlist;

	waa.console = console;
	waa.scrdata = &screenlist;
	waa.accessops = accessops;
	waa.accesscookie = fb;

	config_found((struct device *)sc, &waa, wsemuldisplaydevprint);
}

/*
 * Common wsdisplay emulops for DIO frame buffers
 */

/* the cursor is just an inverted space */
#define flip_cursor(fb)							\
do {									\
	(*fb->bmv)((fb), (fb)->cblankx, (fb)->cblanky,			\
	    (fb)->cursorx * (fb)->ftwidth,				\
	    (fb)->cursory * (fb)->ftheight,				\
	    (fb)->ftwidth, (fb)->ftheight, RR_XOR);			\
} while (0)

int
diofb_alloc_attr(void *cookie, int fg, int bg, int flag, long *attrp)
{
	*attrp = flag & WSATTR_REVERSE;
	return (0);
}

int
diofb_mapchar(void *cookie, int c, unsigned int *cp)
{
	if (c < (int)' ' || c >= FONTMAXCHAR) {
		*cp = ' ';
		return (0);
	}

	*cp = c;
	return (5);
}

void
diofb_cursor(void *cookie, int on, int row, int col)
{
	struct diofb *fb = cookie;

	/* Turn old cursor off if necessary */
	if (fb->curvisible != 0)
		flip_cursor(fb);

	fb->cursorx = col;
	fb->cursory = row;

	if ((fb->curvisible = on) != 0)
		flip_cursor(fb);
}

void
diofb_putchar(void *cookie, int row, int col, u_int uc, long attr)
{
	struct diofb *fb = cookie;
	int wmrr;

	wmrr = (attr & WSATTR_REVERSE) ? RR_COPYINVERTED : RR_COPY;

	(*fb->bmv)(fb, charX(fb, uc), charY(fb, uc),
	    col * fb->ftwidth, row * fb->ftheight,
	    fb->ftwidth, fb->ftheight, wmrr);
}

void
diofb_copycols(void *cookie, int row, int src, int dst, int n)
{
	struct diofb *fb = cookie;

	n *= fb->ftwidth;
	src *= fb->ftwidth;
	dst *= fb->ftwidth;
	row *= fb->ftheight;

	(*fb->bmv)(fb, src, row, dst, row, n, fb->ftheight, RR_COPY);
}

void
diofb_copyrows(void *cookie, int src, int dst, int n)
{
	struct diofb *fb = cookie;

	n *= fb->ftheight;
	src *= fb->ftheight;
	dst *= fb->ftheight;

	(*fb->bmv)(fb, 0, src, 0, dst, fb->dwidth, n, RR_COPY);
}

void
diofb_erasecols(void *cookie, int row, int col, int num, long attr)
{
	struct diofb *fb = cookie;

	num *= fb->ftwidth;
	col *= fb->ftwidth;
	row *= fb->ftheight;
	(*fb->bmv)(fb, col, row, col, row, num, fb->ftheight, RR_CLEAR);
}

void
diofb_eraserows(void *cookie, int row, int num, long attr)
{
	struct diofb *fb = cookie;

	row *= fb->ftheight;
	num *= fb->ftheight;
	(*fb->bmv)(fb, 0, row, 0, row, fb->dwidth, num, RR_CLEAR);
}

/*
 * Common wsdisplay accessops for DIO frame buffers
 */

int
diofb_alloc_screen(void *v, const struct wsscreen_descr *type,
    void **cookiep, int *curxp, int *curyp, long *attrp)
{
	struct diofb *fb = v;

	if (fb->nscreens > 0)
		return (ENOMEM);

	*cookiep = fb;
	*curxp = *curyp = 0;
	diofb_alloc_attr(fb, 0, 0, 0, attrp);
	fb->nscreens++;

	return (0);
}

void
diofb_free_screen(void *v, void *cookie)
{
	struct diofb *fb = v;

	fb->nscreens--;
}

int
diofb_show_screen(void *v, void *cookie, int waitok,
    void (*cb)(void *, int, int), void *cbarg)
{
	return (0);
}

paddr_t
diofb_mmap(void * v, off_t offset, int prot)
{
	struct diofb *fb = v;

	if (offset & PGOFSET)
		return (-1);

	if (offset < 0 || offset >= fb->fbsize)
		return (-1);

	return (((paddr_t)fb->fbaddr + offset) >> PGSHIFT);
}