xref: /netbsd-src/sys/dev/hpc/video_subr.c (revision 17dd36da8292193180754d5047c0926dbb56818c)
1 /*	$NetBSD: video_subr.c,v 1.1 2001/02/22 18:37:56 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(r && g && b && rgb && LEGAL_CLUT_INDEX(cnt - 1));
165 
166 	*r = malloc(cnt * sizeof(u_int8_t), M_DEVBUF, M_WAITOK);
167 	*g = malloc(cnt * sizeof(u_int8_t), M_DEVBUF, M_WAITOK);
168 	*b = malloc(cnt * sizeof(u_int8_t), M_DEVBUF, M_WAITOK);
169 	*rgb = malloc(cnt * sizeof(u_int32_t), M_DEVBUF, M_WAITOK);
170 
171 	return (!(*r && *g && *b && *rgb));
172 }
173 
174 void
175 cmap_work_free(u_int8_t *r, u_int8_t *g, u_int8_t *b, u_int32_t *rgb)
176 {
177 	if (r)
178 		free(r, M_DEVBUF);
179 	if (g)
180 		free(g, M_DEVBUF);
181 	if (b)
182 		free(b, M_DEVBUF);
183 	if (rgb)
184 		free(rgb, M_DEVBUF);
185 }
186 
187 void
188 rgb24_compose(u_int32_t *rgb24, u_int8_t *r, u_int8_t *g, u_int8_t *b, int cnt)
189 {
190 	int i;
191 	KASSERT(rgb24 && r && g && b && LEGAL_CLUT_INDEX(cnt - 1));
192 
193 	for (i = 0; i < cnt; i++) {
194 		*rgb24++ = RGB24(r[i], g[i], b[i]);
195 	}
196 }
197 
198 void
199 rgb24_decompose(u_int32_t *rgb24, u_int8_t *r, u_int8_t *g, u_int8_t *b,
200 		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 		u_int32_t rgb = *rgb24++;
207 		*r++ = (rgb >> 16) & 0xff;
208 		*g++ = (rgb >> 8) & 0xff;
209 		*b++ = rgb & 0xff;
210 	}
211 }
212 
213 /*
214  * Debug routines.
215  */
216 void
217 video_calibration_pattern(struct video_chip *vc)
218 {
219 	int x, y;
220 
221 	x = vc->vc_fbwidth - 40;
222 	y = vc->vc_fbheight - 40;
223 	video_line(vc, 40, 40, x , 40);
224 	video_line(vc, x , 40, x , y );
225 	video_line(vc, x , y , 40, y );
226 	video_line(vc, 40, y , 40, 40);
227 	video_line(vc, 40, 40, x , y );
228 	video_line(vc, x,  40, 40, y );
229 }
230 
231 static void
232 linebpp_unimpl(struct video_chip *vc, int x0, int y0, int x1, int y1)
233 {
234 	return;
235 }
236 
237 static void
238 dotbpp_unimpl(struct video_chip *vc, int x, int y)
239 {
240 	return;
241 }
242 
243 void
244 video_attach_drawfunc(struct video_chip *vc)
245 {
246 	switch (vc->vc_fbdepth) {
247 	default:
248 		vc->vc_drawline = linebpp_unimpl;
249 		vc->vc_drawdot = dotbpp_unimpl;
250 		break;
251 	case 8:
252 		vc->vc_drawline = linebpp8;
253 		vc->vc_drawdot = dotbpp8;
254 		break;
255 	case 4:
256 		vc->vc_drawline = linebpp4;
257 		vc->vc_drawdot = dotbpp4;
258 		break;
259 	case 2:
260 		vc->vc_drawline = linebpp2;
261 		vc->vc_drawdot = dotbpp2;
262 		break;
263 	}
264 }
265 
266 void
267 video_line(struct video_chip *vc, int x0, int y0, int x1, int y1)
268 {
269 	if (vc->vc_drawline)
270 		vc->vc_drawline(vc, x0, y0, x1, y1);
271 }
272 
273 void
274 video_dot(struct video_chip *vc, int x, int y)
275 {
276 	if (vc->vc_drawdot)
277 		vc->vc_drawdot(vc, x, y);
278 }
279 
280 int
281 video_reverse_color()
282 {
283 	struct {
284 		int reverse, normal;
285 	} ctype[] = {
286 		{ BIFB_D2_M2L_3,	BIFB_D2_M2L_0	},
287 		{ BIFB_D2_M2L_3x2,	BIFB_D2_M2L_0x2	},
288 		{ BIFB_D8_FF,		BIFB_D8_00	},
289 		{ BIFB_D16_FFFF,	BIFB_D16_0000,	},
290 		{ -1, -1 } /* terminator */
291 	}, *ctypep;
292 	u_int16_t fbtype;
293 
294 	/* check reverse color */
295 	fbtype = bootinfo->fb_type;
296 	for (ctypep = ctype; ctypep->normal != -1 ;  ctypep++) {
297 		if (fbtype == ctypep->normal) {
298 			return (0);
299 		} else if (fbtype == ctypep->reverse) {
300 			return (1);
301 		}
302 	}
303 	printf(": WARNING unknown frame buffer type 0x%04x.\n", fbtype);
304 	return (0);
305 }
306