xref: /netbsd-src/sys/dev/hpc/video_subr.c (revision 27578b9aac214cc7796ead81dcc5427e79d5f2a0)
1 /*	$NetBSD: video_subr.c,v 1.3 2001/06/05 15:02:12 uch 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  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/malloc.h>
42 
43 #include <machine/bootinfo.h>
44 
45 #include <dev/hpc/video_subr.h>
46 
47 #define BPP2 ({								\
48 	u_int8_t bitmap;						\
49 	bitmap = *(volatile u_int8_t*)addr;				\
50 	*(volatile u_int8_t*)addr =					\
51 		(bitmap & ~(0x3 << ((3 - (x % 4)) * 2)));		\
52 })
53 
54 #define BPP4 ({								\
55 	u_int8_t bitmap;						\
56 	bitmap = *(volatile u_int8_t*)addr;				\
57 	*(volatile u_int8_t*)addr =					\
58 		(bitmap & ~(0xf << ((1 - (x % 2)) * 4)));		\
59 })
60 
61 #define BPP8 ({								\
62 	*(volatile u_int8_t*)addr = 0xff;				\
63 })
64 
65 #define BRESENHAM(a, b, c, d, func) ({					\
66 	u_int32_t fbaddr = vc->vc_fbvaddr;				\
67 	u_int32_t fbwidth = vc->vc_fbwidth;				\
68 	u_int32_t fbdepth = vc->vc_fbdepth;				\
69 	len = a, step = b -1;						\
70 	if (step == 0)							\
71 		return;							\
72 	kstep = len == 0 ? 0 : 1;					\
73 	for (i = k = 0, j = step / 2; i <= step; i++) {			\
74 		x = xbase c;						\
75 		y = ybase d;						\
76 		addr = fbaddr + (((y * fbwidth + x) * fbdepth) >> 3);	\
77 		func;							\
78 		j -= len;						\
79 		while (j < 0) {						\
80 			j += step;					\
81 			k += kstep;					\
82 		}							\
83 	}								\
84 })
85 
86 #define DRAWLINE(func) ({						\
87 	if (x < 0) {							\
88 		if (y < 0) {						\
89 			if (_y < _x) {					\
90 				BRESENHAM(_y, _x, -i, -k, func);	\
91 			} else {					\
92 				BRESENHAM(_x, _y, -k, -i, func);	\
93 			}						\
94 		} else {						\
95 			if (_y < _x) {					\
96 				BRESENHAM(_y, _x, -i, +k, func);	\
97 			} else {					\
98 				BRESENHAM(_x, _y, -k, +i, func);	\
99 			}						\
100 		}							\
101 	} else {							\
102 		if (y < 0) {						\
103 			if (_y < _x) {					\
104 				BRESENHAM(_y, _x, +i, -k, func);	\
105 			} else {					\
106 				BRESENHAM(_x, _y, +k, -i, func);	\
107 			}						\
108 		} else {						\
109 			if (_y < _x) {					\
110 				BRESENHAM(_y, _x, +i, +k, func);	\
111 			} else {					\
112 				BRESENHAM(_x, _y, +k, +i, func);	\
113 			}						\
114 		}							\
115 	}								\
116 })
117 
118 #define LINEFUNC(b)							\
119 static void linebpp##b (struct video_chip *, int, int, int, int);	\
120 static void								\
121 linebpp##b##(vc, x0, y0, x1, y1)					\
122 	struct video_chip *vc;						\
123 	int x0, y0, x1, y1;						\
124 {									\
125 	u_int32_t addr;							\
126 	int i, j, k, len, step, kstep;					\
127 	int x, _x, y, _y;						\
128 	int xbase, ybase;						\
129 	x = x1 - x0;							\
130 	y = y1 - y0;							\
131 	_x = abs(x);							\
132 	_y = abs(y);							\
133 	xbase = x0;							\
134 	ybase = y0;							\
135 	DRAWLINE(BPP##b##);						\
136 }
137 
138 #define DOTFUNC(b)							\
139 static void dotbpp##b (struct video_chip *, int, int);			\
140 static void								\
141 dotbpp##b##(vc, x, y)							\
142 	struct video_chip *vc;						\
143 	int x, y;							\
144 {									\
145 	u_int32_t addr;							\
146 	addr = vc->vc_fbvaddr + (((y * vc->vc_fbwidth + x) *		\
147 				 vc->vc_fbdepth) >> 3);			\
148 	BPP##b;								\
149 }
150 
151 LINEFUNC(2)
152 LINEFUNC(4)
153 LINEFUNC(8)
154 DOTFUNC(2)
155 DOTFUNC(4)
156 DOTFUNC(8)
157 static void linebpp_unimpl(struct video_chip *, int, int, int, int);
158 static void dotbpp_unimpl(struct video_chip *, int, int);
159 
160 int
161 cmap_work_alloc(u_int8_t **r, u_int8_t **g, u_int8_t **b, u_int32_t **rgb,
162     int cnt)
163 {
164 	KASSERT(LEGAL_CLUT_INDEX(cnt - 1));
165 
166 #define	ALLOC_BUF(x, bit)						\
167 	if (x) {							\
168 		*x = malloc(cnt * sizeof(u_int ## bit ## _t),		\
169 		    M_DEVBUF, M_WAITOK);				\
170 		if (*x == 0)						\
171 			goto errout;					\
172 	}
173 	ALLOC_BUF(r, 8);
174 	ALLOC_BUF(g, 8);
175 	ALLOC_BUF(b, 8);
176 	ALLOC_BUF(rgb, 32);
177 #undef	ALLOCBUF
178 
179 	return (0);
180 errout:
181 	cmap_work_free(*r, *g, *b, *rgb);
182 
183 	return (1);
184 }
185 
186 void
187 cmap_work_free(u_int8_t *r, u_int8_t *g, u_int8_t *b, u_int32_t *rgb)
188 {
189 	if (r)
190 		free(r, M_DEVBUF);
191 	if (g)
192 		free(g, M_DEVBUF);
193 	if (b)
194 		free(b, M_DEVBUF);
195 	if (rgb)
196 		free(rgb, M_DEVBUF);
197 }
198 
199 void
200 rgb24_compose(u_int32_t *rgb24, u_int8_t *r, u_int8_t *g, u_int8_t *b, int cnt)
201 {
202 	int i;
203 	KASSERT(rgb24 && r && g && b && LEGAL_CLUT_INDEX(cnt - 1));
204 
205 	for (i = 0; i < cnt; i++) {
206 		*rgb24++ = RGB24(r[i], g[i], b[i]);
207 	}
208 }
209 
210 void
211 rgb24_decompose(u_int32_t *rgb24, u_int8_t *r, u_int8_t *g, u_int8_t *b,
212     int cnt)
213 {
214 	int i;
215 	KASSERT(rgb24 && r && g && b && LEGAL_CLUT_INDEX(cnt - 1));
216 
217 	for (i = 0; i < cnt; i++) {
218 		u_int32_t rgb = *rgb24++;
219 		*r++ = (rgb >> 16) & 0xff;
220 		*g++ = (rgb >> 8) & 0xff;
221 		*b++ = rgb & 0xff;
222 	}
223 }
224 
225 /*
226  * Debug routines.
227  */
228 void
229 video_calibration_pattern(struct video_chip *vc)
230 {
231 	int x, y;
232 
233 	x = vc->vc_fbwidth - 40;
234 	y = vc->vc_fbheight - 40;
235 	video_line(vc, 40, 40, x , 40);
236 	video_line(vc, x , 40, x , y );
237 	video_line(vc, x , y , 40, y );
238 	video_line(vc, 40, y , 40, 40);
239 	video_line(vc, 40, 40, x , y );
240 	video_line(vc, x,  40, 40, y );
241 }
242 
243 static void
244 linebpp_unimpl(struct video_chip *vc, int x0, int y0, int x1, int y1)
245 {
246 	return;
247 }
248 
249 static void
250 dotbpp_unimpl(struct video_chip *vc, int x, int y)
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()
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