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