Sparc64: add upa-portid properties
[openbios.git] / packages / video.c
blobbe4d6a6eb6f7e6f3e33b45e3bbcb4fdfddc97157
1 /*
2 * Creation Date: <2002/10/23 20:26:40 samuel>
3 * Time-stamp: <2004/01/07 19:39:15 samuel>
5 * <video.c>
7 * Mac-on-Linux display node
9 * Copyright (C) 2002, 2003, 2004 Samuel Rydh (samuel@ibrium.se)
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation
17 #include "config.h"
18 #include "libopenbios/bindings.h"
19 #include "libc/diskio.h"
20 #include "libopenbios/ofmem.h"
21 #include "drivers/drivers.h"
22 #include "packages/video.h"
23 #include "libopenbios/console.h"
24 #include "drivers/vga.h"
26 typedef struct osi_fb_info {
27 unsigned long mphys;
28 int rb, w, h, depth;
29 } osi_fb_info_t;
31 static struct {
32 int has_video;
33 osi_fb_info_t fb;
34 ulong *pal; /* 256 elements */
35 } video;
38 int
39 video_get_res( int *w, int *h )
41 if( !video.has_video ) {
42 *w = *h = 0;
43 return -1;
45 *w = video.fb.w;
46 *h = video.fb.h;
47 return 0;
50 static void
51 startup_splash( void )
53 #ifdef CONFIG_MOL
54 int fd, s, i, y, x, dx, dy;
55 int width, height;
56 char *pp, *p;
57 char buf[64];
58 #endif
60 /* only draw logo in 24-bit mode (for now) */
61 if( video.fb.depth < 15 )
62 return;
63 #ifdef CONFIG_MOL
64 for( i=0; i<2; i++ ) {
65 if( !BootHGetStrResInd("bootlogo", buf, sizeof(buf), 0, i) )
66 return;
67 *(!i ? &width : &height) = atol(buf);
70 if( (s=width * height * 3) > 0x20000 )
71 return;
73 if( (fd=open_io("pseudo:,bootlogo")) >= 0 ) {
74 p = malloc( s );
75 if( read_io(fd, p, s) != s )
76 printk("bootlogo size error\n");
77 close_io( fd );
79 dx = (video.fb.w - width)/2;
80 dy = (video.fb.h - height)/3;
82 pp = (char*)video.fb.mphys + dy * video.fb.rb + dx * (video.fb.depth >= 24 ? 4 : 2);
84 for( y=0 ; y<height; y++, pp += video.fb.rb ) {
85 if( video.fb.depth >= 24 ) {
86 ulong *d = (ulong*)pp;
87 for( x=0; x<width; x++, p+=3, d++ )
88 *d = ((int)p[0] << 16) | ((int)p[1] << 8) | p[2];
89 } else if( video.fb.depth == 15 ) {
90 ushort *d = (ushort*)pp;
91 for( x=0; x<width; x++, p+=3, d++ ) {
92 int col = ((int)p[0] << 16) | ((int)p[1] << 8) | p[2];
93 *d = ((col>>9) & 0x7c00) | ((col>>6) & 0x03e0) | ((col>>3) & 0x1f);
97 free( p );
99 #else
100 /* No bootlogo support yet on other platforms */
101 return;
102 #endif
105 static ulong
106 get_color( int col_ind )
108 ulong col;
109 if( !video.has_video || col_ind < 0 || col_ind > 255 )
110 return 0;
111 if( video.fb.depth == 8 )
112 return col_ind;
113 col = video.pal[col_ind];
114 if( video.fb.depth == 24 || video.fb.depth == 32 )
115 return col;
116 if( video.fb.depth == 15 )
117 return ((col>>9) & 0x7c00) | ((col>>6) & 0x03e0) | ((col>>3) & 0x1f);
118 return 0;
121 void
122 draw_pixel( int x, int y, int colind )
124 char *p = (char*)video.fb.mphys + video.fb.rb * y;
125 int color, d = video.fb.depth;
127 if( x < 0 || y < 0 || x >= video.fb.w || y >=video.fb.h )
128 return;
129 color = get_color( colind );
131 if( d >= 24 )
132 *((ulong*)p + x) = color;
133 else if( d >= 15 )
134 *((short*)p + x) = color;
135 else
136 *(p + x) = color;
139 static void
140 fill_rect( int col_ind, int x, int y, int w, int h )
142 char *pp;
143 ulong col = get_color(col_ind);
145 if( !video.has_video || x < 0 || y < 0 || x+w > video.fb.w || y+h > video.fb.h )
146 return;
148 pp = (char*)video.fb.mphys + video.fb.rb * y;
149 for( ; h--; pp += video.fb.rb ) {
150 int ww = w;
151 if( video.fb.depth == 24 || video.fb.depth == 32 ) {
152 ulong *p = (ulong*)pp + x;
153 while( ww-- )
154 *p++ = col;
155 } else if( video.fb.depth == 16 || video.fb.depth == 15 ) {
156 ushort *p = (ushort*)pp + x;
157 while( ww-- )
158 *p++ = col;
159 } else {
160 char *p = (char *)((ushort*)pp + x);
162 while( ww-- )
163 *p++ = col;
168 static void
169 refresh_palette( void )
171 #ifdef CONFIG_MOL
172 if( video.fb.depth == 8 )
173 OSI_RefreshPalette();
174 #endif
177 void
178 set_color( int ind, ulong color )
180 if( !video.has_video || ind < 0 || ind > 255 )
181 return;
182 video.pal[ind] = color;
184 #ifdef CONFIG_MOL
185 if( video.fb.depth == 8 )
186 OSI_SetColor( ind, color );
187 #elif defined(CONFIG_SPARC32)
188 if( video.fb.depth == 8 ) {
189 dac[0] = ind << 24;
190 dac[1] = ((color >> 16) & 0xff) << 24; // Red
191 dac[1] = ((color >> 8) & 0xff) << 24; // Green
192 dac[1] = (color & 0xff) << 24; // Blue
194 #else
195 vga_set_color(ind, ((color >> 16) & 0xff),
196 ((color >> 8) & 0xff),
197 (color & 0xff));
198 #endif
201 void
202 video_scroll( int height )
204 int i, offs, size, *dest, *src;
206 offs = video.fb.rb * height;
207 size = (video.fb.h * video.fb.rb - offs)/16;
208 dest = (int*)video.fb.mphys;
209 src = (int*)(video.fb.mphys + offs);
211 for( i=0; i<size; i++ ) {
212 dest[0] = src[0];
213 dest[1] = src[1];
214 dest[2] = src[2];
215 dest[3] = src[3];
216 dest += 4;
217 src += 4;
221 /************************************************************************/
222 /* OF methods */
223 /************************************************************************/
225 DECLARE_NODE( video, INSTALL_OPEN, 0, "Tdisplay" );
227 /* ( -- width height ) (?) */
228 static void
229 video_dimensions( void )
231 int w, h;
232 (void) video_get_res( &w, &h );
233 PUSH( w );
234 PUSH( h );
237 /* ( table start count -- ) (?) */
238 static void
239 video_set_colors( void )
241 int count = POP();
242 int start = POP();
243 uchar *p = (uchar*)POP();
244 int i;
246 for( i=0; i<count; i++, p+=3 ) {
247 ulong col = (p[0] << 16) | (p[1] << 8) | p[2];
248 set_color( i + start, col );
250 refresh_palette();
253 /* ( r g b index -- ) */
254 static void
255 video_color_bang( void )
257 int index = POP();
258 int b = POP();
259 int g = POP();
260 int r = POP();
261 ulong col = ((r << 16) & 0xff0000) | ((g << 8) & 0x00ff00) | (b & 0xff);
262 /* printk("color!: %08lx %08lx %08lx %08lx\n", r, g, b, index ); */
263 set_color( index, col );
264 refresh_palette();
267 /* ( color_ind x y width height -- ) (?) */
268 static void
269 video_fill_rect( void )
271 int h = POP();
272 int w = POP();
273 int y = POP();
274 int x = POP();
275 int color_ind = POP();
277 fill_rect( color_ind, x, y, w, h );
280 /* ( addr len -- actual ) */
281 static void
282 video_write(void)
284 char *addr;
285 int len;
287 len = GETTOS();
288 addr = pop_fstr_copy();
290 console_draw_str(addr);
291 free(addr);
292 PUSH(len);
295 NODE_METHODS( video ) = {
296 {"dimensions", video_dimensions },
297 {"set-colors", video_set_colors },
298 {"fill-rectangle", video_fill_rect },
299 {"color!", video_color_bang },
300 {"write", video_write },
304 /************************************************************************/
305 /* init */
306 /************************************************************************/
308 void
309 init_video( unsigned long fb, int width, int height, int depth, int rb )
311 int i;
312 #ifdef CONFIG_PPC
313 int s, size;
314 #endif
315 phandle_t ph=0;
317 video.fb.mphys = fb;
318 video.fb.w = width;
319 video.fb.h = height;
320 video.fb.depth = depth;
321 video.fb.rb = rb;
322 while( (ph=dt_iterate_type(ph, "display")) ) {
323 set_property( ph, "width", (char*)&video.fb.w, 4 );
324 set_property( ph, "height", (char*)&video.fb.h, 4 );
325 set_property( ph, "depth", (char*)&video.fb.depth, 4 );
326 set_property( ph, "linebytes", (char*)&video.fb.rb, 4 );
327 set_property( ph, "address", (char*)&video.fb.mphys, 4 );
329 video.has_video = 1;
330 video.pal = malloc( 256 * sizeof(ulong) );
332 #ifdef CONFIG_PPC
333 s = (video.fb.mphys & 0xfff);
334 size = ((video.fb.h * video.fb.rb + s) + 0xfff) & ~0xfff;
336 ofmem_claim_phys( video.fb.mphys, size, 0 );
337 ofmem_claim_virt( video.fb.mphys, size, 0 );
338 ofmem_map( video.fb.mphys, video.fb.mphys, size, -1 );
339 #endif
341 for( i=0; i<256; i++ )
342 set_color( i, i * 0x010101 );
344 set_color( 254, 0xffffcc );
345 fill_rect( 254, 0, 0, video.fb.w, video.fb.h );
347 refresh_palette();
348 startup_splash();
350 REGISTER_NODE( video );