xref: /netbsd-src/sys/dev/hpc/video_subr.c (revision 1d7f24ead849f206da1037b3b802e9b5f002b711)
1*1d7f24eaSmatt /*	$NetBSD: video_subr.c,v 1.13 2012/02/12 16:34:11 matt Exp $	*/
2659f65e0Such 
3659f65e0Such /*-
4659f65e0Such  * Copyright (c) 2000 The NetBSD Foundation, Inc.
5659f65e0Such  * All rights reserved.
6659f65e0Such  *
7659f65e0Such  * This code is derived from software contributed to The NetBSD Foundation
8659f65e0Such  * by UCHIYAMA Yasushi.
9659f65e0Such  *
10659f65e0Such  * Redistribution and use in source and binary forms, with or without
11659f65e0Such  * modification, are permitted provided that the following conditions
12659f65e0Such  * are met:
13659f65e0Such  * 1. Redistributions of source code must retain the above copyright
14659f65e0Such  *    notice, this list of conditions and the following disclaimer.
15659f65e0Such  * 2. Redistributions in binary form must reproduce the above copyright
16659f65e0Such  *    notice, this list of conditions and the following disclaimer in the
17659f65e0Such  *    documentation and/or other materials provided with the distribution.
18659f65e0Such  *
19659f65e0Such  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20659f65e0Such  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21659f65e0Such  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22659f65e0Such  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23659f65e0Such  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24659f65e0Such  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25659f65e0Such  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26659f65e0Such  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27659f65e0Such  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28659f65e0Such  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29659f65e0Such  * POSSIBILITY OF SUCH DAMAGE.
30659f65e0Such  */
31659f65e0Such 
32b84f53efSlukem #include <sys/cdefs.h>
33*1d7f24eaSmatt __KERNEL_RCSID(0, "$NetBSD: video_subr.c,v 1.13 2012/02/12 16:34:11 matt Exp $");
34b84f53efSlukem 
35659f65e0Such #include <sys/param.h>
36659f65e0Such #include <sys/systm.h>
37659f65e0Such #include <sys/malloc.h>
38659f65e0Such 
39659f65e0Such #include <machine/bootinfo.h>
40659f65e0Such 
41659f65e0Such #include <dev/hpc/video_subr.h>
42659f65e0Such 
43659f65e0Such #define BPP2 ({								\
44659f65e0Such 	u_int8_t bitmap;						\
45659f65e0Such 	bitmap = *(volatile u_int8_t*)addr;				\
46659f65e0Such 	*(volatile u_int8_t*)addr =					\
47659f65e0Such 		(bitmap & ~(0x3 << ((3 - (x % 4)) * 2)));		\
48659f65e0Such })
49659f65e0Such 
50659f65e0Such #define BPP4 ({								\
51659f65e0Such 	u_int8_t bitmap;						\
52659f65e0Such 	bitmap = *(volatile u_int8_t*)addr;				\
53659f65e0Such 	*(volatile u_int8_t*)addr =					\
54659f65e0Such 		(bitmap & ~(0xf << ((1 - (x % 2)) * 4)));		\
55659f65e0Such })
56659f65e0Such 
57659f65e0Such #define BPP8 ({								\
58659f65e0Such 	*(volatile u_int8_t*)addr = 0xff;				\
59659f65e0Such })
60659f65e0Such 
61659f65e0Such #define BRESENHAM(a, b, c, d, func) ({					\
62659f65e0Such 	u_int32_t fbaddr = vc->vc_fbvaddr;				\
63659f65e0Such 	u_int32_t fbwidth = vc->vc_fbwidth;				\
64659f65e0Such 	u_int32_t fbdepth = vc->vc_fbdepth;				\
65659f65e0Such 	len = a, step = b -1;						\
66659f65e0Such 	if (step == 0)							\
67659f65e0Such 		return;							\
68659f65e0Such 	kstep = len == 0 ? 0 : 1;					\
69659f65e0Such 	for (i = k = 0, j = step / 2; i <= step; i++) {			\
70659f65e0Such 		x = xbase c;						\
71659f65e0Such 		y = ybase d;						\
72659f65e0Such 		addr = fbaddr + (((y * fbwidth + x) * fbdepth) >> 3);	\
73659f65e0Such 		func;							\
74659f65e0Such 		j -= len;						\
75659f65e0Such 		while (j < 0) {						\
76659f65e0Such 			j += step;					\
77659f65e0Such 			k += kstep;					\
78659f65e0Such 		}							\
79659f65e0Such 	}								\
80659f65e0Such })
81659f65e0Such 
82659f65e0Such #define DRAWLINE(func) ({						\
83659f65e0Such 	if (x < 0) {							\
84659f65e0Such 		if (y < 0) {						\
85659f65e0Such 			if (_y < _x) {					\
86659f65e0Such 				BRESENHAM(_y, _x, -i, -k, func);	\
87659f65e0Such 			} else {					\
88659f65e0Such 				BRESENHAM(_x, _y, -k, -i, func);	\
89659f65e0Such 			}						\
90659f65e0Such 		} else {						\
91659f65e0Such 			if (_y < _x) {					\
92659f65e0Such 				BRESENHAM(_y, _x, -i, +k, func);	\
93659f65e0Such 			} else {					\
94659f65e0Such 				BRESENHAM(_x, _y, -k, +i, func);	\
95659f65e0Such 			}						\
96659f65e0Such 		}							\
97659f65e0Such 	} else {							\
98659f65e0Such 		if (y < 0) {						\
99659f65e0Such 			if (_y < _x) {					\
100659f65e0Such 				BRESENHAM(_y, _x, +i, -k, func);	\
101659f65e0Such 			} else {					\
102659f65e0Such 				BRESENHAM(_x, _y, +k, -i, func);	\
103659f65e0Such 			}						\
104659f65e0Such 		} else {						\
105659f65e0Such 			if (_y < _x) {					\
106659f65e0Such 				BRESENHAM(_y, _x, +i, +k, func);	\
107659f65e0Such 			} else {					\
108659f65e0Such 				BRESENHAM(_x, _y, +k, +i, func);	\
109659f65e0Such 			}						\
110659f65e0Such 		}							\
111659f65e0Such 	}								\
112659f65e0Such })
113659f65e0Such 
114659f65e0Such #define LINEFUNC(b)							\
115659f65e0Such static void								\
116*1d7f24eaSmatt linebpp##b(struct video_chip *vc, int x0, int y0, int x1, int y1)	\
117659f65e0Such {									\
118659f65e0Such 	u_int32_t addr;							\
119659f65e0Such 	int i, j, k, len, step, kstep;					\
120659f65e0Such 	int x, _x, y, _y;						\
121659f65e0Such 	int xbase, ybase;						\
122659f65e0Such 	x = x1 - x0;							\
123659f65e0Such 	y = y1 - y0;							\
124659f65e0Such 	_x = abs(x);							\
125659f65e0Such 	_y = abs(y);							\
126659f65e0Such 	xbase = x0;							\
127659f65e0Such 	ybase = y0;							\
1289d1ca6d8Suwe 	DRAWLINE(BPP##b);						\
129659f65e0Such }
130659f65e0Such 
131659f65e0Such #define DOTFUNC(b)							\
132659f65e0Such static void								\
133*1d7f24eaSmatt dotbpp##b(struct video_chip *vc, int x, int y)				\
134659f65e0Such {									\
135659f65e0Such 	u_int32_t addr;							\
136659f65e0Such 	addr = vc->vc_fbvaddr + (((y * vc->vc_fbwidth + x) *		\
137659f65e0Such 				 vc->vc_fbdepth) >> 3);			\
138659f65e0Such 	BPP##b;								\
139659f65e0Such }
140659f65e0Such 
141659f65e0Such LINEFUNC(2)
142659f65e0Such LINEFUNC(4)
143659f65e0Such LINEFUNC(8)
144659f65e0Such DOTFUNC(2)
145659f65e0Such DOTFUNC(4)
146659f65e0Such DOTFUNC(8)
147659f65e0Such static void linebpp_unimpl(struct video_chip *, int, int, int, int);
148659f65e0Such static void dotbpp_unimpl(struct video_chip *, int, int);
149659f65e0Such 
150659f65e0Such int
cmap_work_alloc(u_int8_t ** r,u_int8_t ** g,u_int8_t ** b,u_int32_t ** rgb,int cnt)151659f65e0Such cmap_work_alloc(u_int8_t **r, u_int8_t **g, u_int8_t **b, u_int32_t **rgb,
152659f65e0Such     int cnt)
153659f65e0Such {
154a4221d6bSuch 	KASSERT(LEGAL_CLUT_INDEX(cnt - 1));
155659f65e0Such 
156a4221d6bSuch #define	ALLOC_BUF(x, bit)						\
157a4221d6bSuch 	if (x) {							\
158a4221d6bSuch 		*x = malloc(cnt * sizeof(u_int ## bit ## _t),		\
159a4221d6bSuch 		    M_DEVBUF, M_WAITOK);				\
160a4221d6bSuch 		if (*x == 0)						\
161a4221d6bSuch 			goto errout;					\
162a4221d6bSuch 	}
163a4221d6bSuch 	ALLOC_BUF(r, 8);
164a4221d6bSuch 	ALLOC_BUF(g, 8);
165a4221d6bSuch 	ALLOC_BUF(b, 8);
166a4221d6bSuch 	ALLOC_BUF(rgb, 32);
167a4221d6bSuch #undef	ALLOCBUF
168659f65e0Such 
169a4221d6bSuch 	return (0);
170a4221d6bSuch errout:
171a4221d6bSuch 	cmap_work_free(*r, *g, *b, *rgb);
172a4221d6bSuch 
173e07f0b93Schs 	return (ENOMEM);
174659f65e0Such }
175659f65e0Such 
176659f65e0Such void
cmap_work_free(u_int8_t * r,u_int8_t * g,u_int8_t * b,u_int32_t * rgb)177659f65e0Such cmap_work_free(u_int8_t *r, u_int8_t *g, u_int8_t *b, u_int32_t *rgb)
178659f65e0Such {
179659f65e0Such 	if (r)
180659f65e0Such 		free(r, M_DEVBUF);
181659f65e0Such 	if (g)
182659f65e0Such 		free(g, M_DEVBUF);
183659f65e0Such 	if (b)
184659f65e0Such 		free(b, M_DEVBUF);
185659f65e0Such 	if (rgb)
186659f65e0Such 		free(rgb, M_DEVBUF);
187659f65e0Such }
188659f65e0Such 
189659f65e0Such void
rgb24_compose(u_int32_t * rgb24,u_int8_t * r,u_int8_t * g,u_int8_t * b,int cnt)190659f65e0Such rgb24_compose(u_int32_t *rgb24, u_int8_t *r, u_int8_t *g, u_int8_t *b, int cnt)
191659f65e0Such {
192659f65e0Such 	int i;
193659f65e0Such 	KASSERT(rgb24 && r && g && b && LEGAL_CLUT_INDEX(cnt - 1));
194659f65e0Such 
195659f65e0Such 	for (i = 0; i < cnt; i++) {
196659f65e0Such 		*rgb24++ = RGB24(r[i], g[i], b[i]);
197659f65e0Such 	}
198659f65e0Such }
199659f65e0Such 
200659f65e0Such void
rgb24_decompose(u_int32_t * rgb24,u_int8_t * r,u_int8_t * g,u_int8_t * b,int cnt)201659f65e0Such rgb24_decompose(u_int32_t *rgb24, u_int8_t *r, u_int8_t *g, u_int8_t *b,
202659f65e0Such     int cnt)
203659f65e0Such {
204659f65e0Such 	int i;
205659f65e0Such 	KASSERT(rgb24 && r && g && b && LEGAL_CLUT_INDEX(cnt - 1));
206659f65e0Such 
207659f65e0Such 	for (i = 0; i < cnt; i++) {
208659f65e0Such 		u_int32_t rgb = *rgb24++;
209659f65e0Such 		*r++ = (rgb >> 16) & 0xff;
210659f65e0Such 		*g++ = (rgb >> 8) & 0xff;
211659f65e0Such 		*b++ = rgb & 0xff;
212659f65e0Such 	}
213659f65e0Such }
214659f65e0Such 
215659f65e0Such /*
216659f65e0Such  * Debug routines.
217659f65e0Such  */
218659f65e0Such void
video_calibration_pattern(struct video_chip * vc)219659f65e0Such video_calibration_pattern(struct video_chip *vc)
220659f65e0Such {
221659f65e0Such 	int x, y;
222659f65e0Such 
223659f65e0Such 	x = vc->vc_fbwidth - 40;
224659f65e0Such 	y = vc->vc_fbheight - 40;
225659f65e0Such 	video_line(vc, 40, 40, x , 40);
226659f65e0Such 	video_line(vc, x , 40, x , y );
227659f65e0Such 	video_line(vc, x , y , 40, y );
228659f65e0Such 	video_line(vc, 40, y , 40, 40);
229659f65e0Such 	video_line(vc, 40, 40, x , y );
230659f65e0Such 	video_line(vc, x,  40, 40, y );
231659f65e0Such }
232659f65e0Such 
233659f65e0Such static void
linebpp_unimpl(struct video_chip * vc,int x0,int y0,int x1,int y1)234168cd830Schristos linebpp_unimpl(struct video_chip *vc,
235168cd830Schristos 	       int x0, int y0,
236168cd830Schristos 	       int x1, int y1)
237659f65e0Such {
23844b35078Suwe 
239659f65e0Such 	return;
240659f65e0Such }
241659f65e0Such 
242659f65e0Such static void
dotbpp_unimpl(struct video_chip * vc,int x,int y)243168cd830Schristos dotbpp_unimpl(struct video_chip *vc, int x, int y)
244659f65e0Such {
24544b35078Suwe 
246659f65e0Such 	return;
247659f65e0Such }
248659f65e0Such 
249659f65e0Such void
video_attach_drawfunc(struct video_chip * vc)250659f65e0Such video_attach_drawfunc(struct video_chip *vc)
251659f65e0Such {
252659f65e0Such 	switch (vc->vc_fbdepth) {
253659f65e0Such 	default:
254659f65e0Such 		vc->vc_drawline = linebpp_unimpl;
255659f65e0Such 		vc->vc_drawdot = dotbpp_unimpl;
256659f65e0Such 		break;
257659f65e0Such 	case 8:
258659f65e0Such 		vc->vc_drawline = linebpp8;
259659f65e0Such 		vc->vc_drawdot = dotbpp8;
260659f65e0Such 		break;
261659f65e0Such 	case 4:
262659f65e0Such 		vc->vc_drawline = linebpp4;
263659f65e0Such 		vc->vc_drawdot = dotbpp4;
264659f65e0Such 		break;
265659f65e0Such 	case 2:
266659f65e0Such 		vc->vc_drawline = linebpp2;
267659f65e0Such 		vc->vc_drawdot = dotbpp2;
268659f65e0Such 		break;
269659f65e0Such 	}
270659f65e0Such }
271659f65e0Such 
272659f65e0Such void
video_line(struct video_chip * vc,int x0,int y0,int x1,int y1)273659f65e0Such video_line(struct video_chip *vc, int x0, int y0, int x1, int y1)
274659f65e0Such {
275659f65e0Such 	if (vc->vc_drawline)
276659f65e0Such 		vc->vc_drawline(vc, x0, y0, x1, y1);
277659f65e0Such }
278659f65e0Such 
279659f65e0Such void
video_dot(struct video_chip * vc,int x,int y)280659f65e0Such video_dot(struct video_chip *vc, int x, int y)
281659f65e0Such {
282659f65e0Such 	if (vc->vc_drawdot)
283659f65e0Such 		vc->vc_drawdot(vc, x, y);
284659f65e0Such }
285659f65e0Such 
286659f65e0Such int
video_reverse_color(void)287df7f595eScegger video_reverse_color(void)
288659f65e0Such {
289659f65e0Such 	struct {
290659f65e0Such 		int reverse, normal;
291659f65e0Such 	} ctype[] = {
292659f65e0Such 		{ BIFB_D2_M2L_3,	BIFB_D2_M2L_0	},
293659f65e0Such 		{ BIFB_D2_M2L_3x2,	BIFB_D2_M2L_0x2	},
294659f65e0Such 		{ BIFB_D8_FF,		BIFB_D8_00	},
295659f65e0Such 		{ BIFB_D16_FFFF,	BIFB_D16_0000,	},
296659f65e0Such 		{ -1, -1 } /* terminator */
297659f65e0Such 	}, *ctypep;
298659f65e0Such 	u_int16_t fbtype;
299659f65e0Such 
300659f65e0Such 	/* check reverse color */
301659f65e0Such 	fbtype = bootinfo->fb_type;
302659f65e0Such 	for (ctypep = ctype; ctypep->normal != -1 ;  ctypep++) {
303659f65e0Such 		if (fbtype == ctypep->normal) {
304659f65e0Such 			return (0);
305659f65e0Such 		} else if (fbtype == ctypep->reverse) {
306659f65e0Such 			return (1);
307659f65e0Such 		}
308659f65e0Such 	}
309659f65e0Such 	printf(": WARNING unknown frame buffer type 0x%04x.\n", fbtype);
310659f65e0Such 	return (0);
311659f65e0Such }
312