xref: /netbsd/sys/dev/hpc/video_subr.c (revision 6550d01e)
1 /*	$NetBSD: video_subr.c,v 1.12 2009/03/18 10:22:40 cegger Exp $	*/
2 
3 /*-
4  * Copyright (c) 2000 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by UCHIYAMA Yasushi.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: video_subr.c,v 1.12 2009/03/18 10:22:40 cegger Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/malloc.h>
38 
39 #include <machine/bootinfo.h>
40 
41 #include <dev/hpc/video_subr.h>
42 
43 #define BPP2 ({								\
44 	u_int8_t bitmap;						\
45 	bitmap = *(volatile u_int8_t*)addr;				\
46 	*(volatile u_int8_t*)addr =					\
47 		(bitmap & ~(0x3 << ((3 - (x % 4)) * 2)));		\
48 })
49 
50 #define BPP4 ({								\
51 	u_int8_t bitmap;						\
52 	bitmap = *(volatile u_int8_t*)addr;				\
53 	*(volatile u_int8_t*)addr =					\
54 		(bitmap & ~(0xf << ((1 - (x % 2)) * 4)));		\
55 })
56 
57 #define BPP8 ({								\
58 	*(volatile u_int8_t*)addr = 0xff;				\
59 })
60 
61 #define BRESENHAM(a, b, c, d, func) ({					\
62 	u_int32_t fbaddr = vc->vc_fbvaddr;				\
63 	u_int32_t fbwidth = vc->vc_fbwidth;				\
64 	u_int32_t fbdepth = vc->vc_fbdepth;				\
65 	len = a, step = b -1;						\
66 	if (step == 0)							\
67 		return;							\
68 	kstep = len == 0 ? 0 : 1;					\
69 	for (i = k = 0, j = step / 2; i <= step; i++) {			\
70 		x = xbase c;						\
71 		y = ybase d;						\
72 		addr = fbaddr + (((y * fbwidth + x) * fbdepth) >> 3);	\
73 		func;							\
74 		j -= len;						\
75 		while (j < 0) {						\
76 			j += step;					\
77 			k += kstep;					\
78 		}							\
79 	}								\
80 })
81 
82 #define DRAWLINE(func) ({						\
83 	if (x < 0) {							\
84 		if (y < 0) {						\
85 			if (_y < _x) {					\
86 				BRESENHAM(_y, _x, -i, -k, func);	\
87 			} else {					\
88 				BRESENHAM(_x, _y, -k, -i, func);	\
89 			}						\
90 		} else {						\
91 			if (_y < _x) {					\
92 				BRESENHAM(_y, _x, -i, +k, func);	\
93 			} else {					\
94 				BRESENHAM(_x, _y, -k, +i, func);	\
95 			}						\
96 		}							\
97 	} else {							\
98 		if (y < 0) {						\
99 			if (_y < _x) {					\
100 				BRESENHAM(_y, _x, +i, -k, func);	\
101 			} else {					\
102 				BRESENHAM(_x, _y, +k, -i, func);	\
103 			}						\
104 		} else {						\
105 			if (_y < _x) {					\
106 				BRESENHAM(_y, _x, +i, +k, func);	\
107 			} else {					\
108 				BRESENHAM(_x, _y, +k, +i, func);	\
109 			}						\
110 		}							\
111 	}								\
112 })
113 
114 #define LINEFUNC(b)							\
115 static void linebpp##b (struct video_chip *, int, int, int, int);	\
116 static void								\
117 linebpp##b(vc, x0, y0, x1, y1)						\
118 	struct video_chip *vc;						\
119 	int x0, y0, x1, y1;						\
120 {									\
121 	u_int32_t addr;							\
122 	int i, j, k, len, step, kstep;					\
123 	int x, _x, y, _y;						\
124 	int xbase, ybase;						\
125 	x = x1 - x0;							\
126 	y = y1 - y0;							\
127 	_x = abs(x);							\
128 	_y = abs(y);							\
129 	xbase = x0;							\
130 	ybase = y0;							\
131 	DRAWLINE(BPP##b);						\
132 }
133 
134 #define DOTFUNC(b)							\
135 static void dotbpp##b (struct video_chip *, int, int);			\
136 static void								\
137 dotbpp##b(vc, x, y)							\
138 	struct video_chip *vc;						\
139 	int x, y;							\
140 {									\
141 	u_int32_t addr;							\
142 	addr = vc->vc_fbvaddr + (((y * vc->vc_fbwidth + x) *		\
143 				 vc->vc_fbdepth) >> 3);			\
144 	BPP##b;								\
145 }
146 
147 LINEFUNC(2)
148 LINEFUNC(4)
149 LINEFUNC(8)
150 DOTFUNC(2)
151 DOTFUNC(4)
152 DOTFUNC(8)
153 static void linebpp_unimpl(struct video_chip *, int, int, int, int);
154 static void dotbpp_unimpl(struct video_chip *, int, int);
155 
156 int
157 cmap_work_alloc(u_int8_t **r, u_int8_t **g, u_int8_t **b, u_int32_t **rgb,
158     int cnt)
159 {
160 	KASSERT(LEGAL_CLUT_INDEX(cnt - 1));
161 
162 #define	ALLOC_BUF(x, bit)						\
163 	if (x) {							\
164 		*x = malloc(cnt * sizeof(u_int ## bit ## _t),		\
165 		    M_DEVBUF, M_WAITOK);				\
166 		if (*x == 0)						\
167 			goto errout;					\
168 	}
169 	ALLOC_BUF(r, 8);
170 	ALLOC_BUF(g, 8);
171 	ALLOC_BUF(b, 8);
172 	ALLOC_BUF(rgb, 32);
173 #undef	ALLOCBUF
174 
175 	return (0);
176 errout:
177 	cmap_work_free(*r, *g, *b, *rgb);
178 
179 	return (ENOMEM);
180 }
181 
182 void
183 cmap_work_free(u_int8_t *r, u_int8_t *g, u_int8_t *b, u_int32_t *rgb)
184 {
185 	if (r)
186 		free(r, M_DEVBUF);
187 	if (g)
188 		free(g, M_DEVBUF);
189 	if (b)
190 		free(b, M_DEVBUF);
191 	if (rgb)
192 		free(rgb, M_DEVBUF);
193 }
194 
195 void
196 rgb24_compose(u_int32_t *rgb24, u_int8_t *r, u_int8_t *g, u_int8_t *b, int cnt)
197 {
198 	int i;
199 	KASSERT(rgb24 && r && g && b && LEGAL_CLUT_INDEX(cnt - 1));
200 
201 	for (i = 0; i < cnt; i++) {
202 		*rgb24++ = RGB24(r[i], g[i], b[i]);
203 	}
204 }
205 
206 void
207 rgb24_decompose(u_int32_t *rgb24, u_int8_t *r, u_int8_t *g, u_int8_t *b,
208     int cnt)
209 {
210 	int i;
211 	KASSERT(rgb24 && r && g && b && LEGAL_CLUT_INDEX(cnt - 1));
212 
213 	for (i = 0; i < cnt; i++) {
214 		u_int32_t rgb = *rgb24++;
215 		*r++ = (rgb >> 16) & 0xff;
216 		*g++ = (rgb >> 8) & 0xff;
217 		*b++ = rgb & 0xff;
218 	}
219 }
220 
221 /*
222  * Debug routines.
223  */
224 void
225 video_calibration_pattern(struct video_chip *vc)
226 {
227 	int x, y;
228 
229 	x = vc->vc_fbwidth - 40;
230 	y = vc->vc_fbheight - 40;
231 	video_line(vc, 40, 40, x , 40);
232 	video_line(vc, x , 40, x , y );
233 	video_line(vc, x , y , 40, y );
234 	video_line(vc, 40, y , 40, 40);
235 	video_line(vc, 40, 40, x , y );
236 	video_line(vc, x,  40, 40, y );
237 }
238 
239 static void
240 linebpp_unimpl(struct video_chip *vc,
241 	       int x0, int y0,
242 	       int x1, int y1)
243 {
244 
245 	return;
246 }
247 
248 static void
249 dotbpp_unimpl(struct video_chip *vc, int x, int y)
250 {
251 
252 	return;
253 }
254 
255 void
256 video_attach_drawfunc(struct video_chip *vc)
257 {
258 	switch (vc->vc_fbdepth) {
259 	default:
260 		vc->vc_drawline = linebpp_unimpl;
261 		vc->vc_drawdot = dotbpp_unimpl;
262 		break;
263 	case 8:
264 		vc->vc_drawline = linebpp8;
265 		vc->vc_drawdot = dotbpp8;
266 		break;
267 	case 4:
268 		vc->vc_drawline = linebpp4;
269 		vc->vc_drawdot = dotbpp4;
270 		break;
271 	case 2:
272 		vc->vc_drawline = linebpp2;
273 		vc->vc_drawdot = dotbpp2;
274 		break;
275 	}
276 }
277 
278 void
279 video_line(struct video_chip *vc, int x0, int y0, int x1, int y1)
280 {
281 	if (vc->vc_drawline)
282 		vc->vc_drawline(vc, x0, y0, x1, y1);
283 }
284 
285 void
286 video_dot(struct video_chip *vc, int x, int y)
287 {
288 	if (vc->vc_drawdot)
289 		vc->vc_drawdot(vc, x, y);
290 }
291 
292 int
293 video_reverse_color(void)
294 {
295 	struct {
296 		int reverse, normal;
297 	} ctype[] = {
298 		{ BIFB_D2_M2L_3,	BIFB_D2_M2L_0	},
299 		{ BIFB_D2_M2L_3x2,	BIFB_D2_M2L_0x2	},
300 		{ BIFB_D8_FF,		BIFB_D8_00	},
301 		{ BIFB_D16_FFFF,	BIFB_D16_0000,	},
302 		{ -1, -1 } /* terminator */
303 	}, *ctypep;
304 	u_int16_t fbtype;
305 
306 	/* check reverse color */
307 	fbtype = bootinfo->fb_type;
308 	for (ctypep = ctype; ctypep->normal != -1 ;  ctypep++) {
309 		if (fbtype == ctypep->normal) {
310 			return (0);
311 		} else if (fbtype == ctypep->reverse) {
312 			return (1);
313 		}
314 	}
315 	printf(": WARNING unknown frame buffer type 0x%04x.\n", fbtype);
316 	return (0);
317 }
318