summaryrefslogtreecommitdiff
path: root/src/console/btext_console.c
blob: ae8dc8ca10ba6d12fe5da288ec9dbac8fd92ee26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*
 * Procedures for drawing on the screen early on in the boot process.
 *
 * Benjamin Herrenschmidt <benh@kernel.crashing.org>
 *
 *   move to LinuxBIOS by LYH yhlu@tyan.com
 */

#if 0

#include <delay.h>
#include <stdlib.h>
#include <string.h>
#include <arch/io.h>

#include <console/console.h>
#include <device/device.h>
#include <device/pci.h>
#include <device/pci_ids.h>
#include <device/pci_ops.h>

#endif

#include <arch/io.h>
#include <string.h>
#include <console/console.h>


#include <arch/byteorder.h>

#include <console/btext.h>

//#define NO_SCROLL

#ifndef NO_SCROLL
static void scrollscreen(void);
#endif

static void draw_byte(unsigned char c, u32 locX, u32 locY);
#if 0
static void draw_byte_32(unsigned char *bits, u32 *base, u32 rb);
static void draw_byte_16(unsigned char *bits, u32 *base, u32 rb);
#endif
static void draw_byte_8(unsigned char *bits, u32 *base, u32 rb);

static u32 g_loc_X;
static u32 g_loc_Y;
static u32 g_max_loc_X;
static u32 g_max_loc_Y;

#define CHAR_256 0

#if CHAR_256==1
#define cmapsz	(16*256)
#else
#define cmapsz        (16*96)
#endif

extern unsigned char vga_font[cmapsz];

u32 boot_text_mapped;

boot_infos_t disp_bi;

#define BTEXT		
#define BTDATA	


/* This function will enable the early boot text when doing OF booting. This
 * way, xmon output should work too
 */
void
btext_setup_display(u32 width, u32 height, u32 depth, u32 pitch,
		    unsigned long address)
{
	boot_infos_t* bi = &disp_bi;

	g_loc_X = 0;
	g_loc_Y = 0;
	g_max_loc_X = width / 8;
	g_max_loc_Y = height / 16;
//	bi->logicalDisplayBase = (unsigned char *)address;
	bi->dispDeviceBase = (unsigned char *)address;
	bi->dispDeviceRowBytes = pitch;
	bi->dispDeviceDepth = depth;
	bi->dispDeviceRect[0] = bi->dispDeviceRect[1] = 0;
	bi->dispDeviceRect[2] = width;
	bi->dispDeviceRect[3] = height;
	boot_text_mapped = 0;
}

/* Here's a small text engine to use during early boot
 * or for debugging purposes
 *
 * todo:
 *
 *  - build some kind of vgacon with it to enable early printk
 *  - move to a separate file
 *  - add a few video driver hooks to keep in sync with display
 *    changes.
 */

void 
map_boot_text(void)
{
#if 0
	unsigned long base, offset, size;
	boot_infos_t *bi = &disp_bi;

	if (bi->dispDeviceBase == 0)
		return;
	base = ((unsigned long) bi->dispDeviceBase) & 0xFFFFF000UL;
	offset = ((unsigned long) bi->dispDeviceBase) - base;
	size = bi->dispDeviceRowBytes * bi->dispDeviceRect[3] + offset 
		+ bi->dispDeviceRect[0];
	bi->logicalDisplayBase = ioremap(base,0x800000 ); 
	if (bi->logicalDisplayBase == 0)
		return;
//	bi->logicalDisplayBase += offset;
#endif
	boot_text_mapped = 1;
}

/* Calc the base address of a given point (x,y) */
static unsigned char * BTEXT
calc_base(boot_infos_t *bi, u32 x, u32 y)
{
	unsigned char *base;
#if 0
	base = bi->logicalDisplayBase;
	if (base == 0)
#endif
		base = bi->dispDeviceBase;
	base += (x + bi->dispDeviceRect[0]) * (bi->dispDeviceDepth >> 3);
	base += (y + bi->dispDeviceRect[1]) * bi->dispDeviceRowBytes;
	return base;
}

/* Adjust the display to a new resolution */
void
btext_update_display(unsigned long phys, u32 width, u32 height,
		     u32 depth, u32 pitch)
{
	boot_infos_t *bi = &disp_bi;
#if 0
	if (bi->dispDeviceBase == 0)
		return;
	/* check it's the same frame buffer (within 256MB) */
	if ((phys ^ (unsigned long)bi->dispDeviceBase) & 0xf0000000)
		return;
#endif

	bi->dispDeviceBase = (u8 *) phys;
	bi->dispDeviceRect[0] = 0;
	bi->dispDeviceRect[1] = 0;
	bi->dispDeviceRect[2] = width;
	bi->dispDeviceRect[3] = height;
	bi->dispDeviceDepth = depth;
	bi->dispDeviceRowBytes = pitch;
	if (boot_text_mapped) {
#if 0
		iounmap(bi->logicalDisplayBase);
#endif
		boot_text_mapped = 0;
	}
	map_boot_text();
	g_loc_X = 0;
	g_loc_Y = 0;
	g_max_loc_X = width / 8;
	g_max_loc_Y = height / 16;
}

void BTEXT btext_clearscreen(void)
{
	boot_infos_t* bi	= &disp_bi;
	u32 *base	= (u32 *)calc_base(bi, 0, 0);
	u32 width 	= ((bi->dispDeviceRect[2] - bi->dispDeviceRect[0]) *
					(bi->dispDeviceDepth >> 3)) >> 2;
	u32 i,j;

	for (i=0; i<(bi->dispDeviceRect[3] - bi->dispDeviceRect[1]); i++)
	{
		u32 *ptr = base;
		for(j=width; j; --j)
			*(ptr++) = 0;
		base += (bi->dispDeviceRowBytes >> 2);
	}
}

#if 0
__inline__ void dcbst(const void* addr)
{
	__asm__ __volatile__ ("dcbst 0,%0" :: "r" (addr));
}

void BTEXT btext_flushscreen(void)
{
	boot_infos_t* bi	= &disp_bi;
	u32  *base	= (unsigned long *)calc_base(bi, 0, 0);
	u32 width 	= ((bi->dispDeviceRect[2] - bi->dispDeviceRect[0]) *
					(bi->dispDeviceDepth >> 3)) >> 2;
	u32 i,j;

	for (i=0; i<(bi->dispDeviceRect[3] - bi->dispDeviceRect[1]); i++)
	{
		u32 *ptr = base;
		for(j=width; j>0; j-=8) {
			dcbst(ptr);
			ptr += 8;
		}
		base += (bi->dispDeviceRowBytes >> 2);
	}
}
#endif


#ifndef NO_SCROLL
static BTEXT void
scrollscreen(void)
{
	boot_infos_t* bi		= &disp_bi;
	u32 *src		= (u32 *)calc_base(bi,0,16);
	u32 *dst		= (u32 *)calc_base(bi,0,0);
	u32 width		= ((bi->dispDeviceRect[2] - bi->dispDeviceRect[0]) *
						(bi->dispDeviceDepth >> 3)) >> 2;
	u32 i,j;

	for (i=0; i<(bi->dispDeviceRect[3] - bi->dispDeviceRect[1] - 16); i++)
	{
		u32 *src_ptr = src;
		u32 *dst_ptr = dst;
		for(j=width; j; --j)
			*(dst_ptr++) = *(src_ptr++);
		src += (bi->dispDeviceRowBytes >> 2);
		dst += (bi->dispDeviceRowBytes >> 2);
	}
	for (i=0; i<16; i++)
	{
		u32 *dst_ptr = dst;
		for(j=width; j; --j)
			*(dst_ptr++) = 0;
		dst += (bi->dispDeviceRowBytes >> 2);
	}
}
#endif /* ndef NO_SCROLL */

void BTEXT btext_drawchar(char c)
{
	u32 cline = 0;

	if (!boot_text_mapped)
		return;

	switch (c) {
	case '\b':
		if (g_loc_X > 0)
			--g_loc_X;
		break;
	case '\t':
		g_loc_X = (g_loc_X & -8) + 8;
		break;
	case '\r':
		g_loc_X = 0;
		break;
	case '\n':
		g_loc_X = 0;
		g_loc_Y++;
		cline = 1;
		break;
	default:
		draw_byte(c, g_loc_X++, g_loc_Y);
	}
	if (g_loc_X >= g_max_loc_X) {
		g_loc_X = 0;
		g_loc_Y++;
		cline = 1;
	}
#ifndef NO_SCROLL
	while (g_loc_Y >= g_max_loc_Y) {
		scrollscreen();
		g_loc_Y--;
	}
#else
	/* wrap around from bottom to top of screen so we don't
	   waste time scrolling each line.  -- paulus. */
	if (g_loc_Y >= g_max_loc_Y)
		g_loc_Y = 0;
	if (cline) {
		for (x = 0; x < g_max_loc_X; ++x)
			draw_byte(' ', x, g_loc_Y);
	}
#endif
}
#if 0
void BTEXT
btext_drawstring(const char *c)
{
	if (!boot_text_mapped)
		return;
	while (*c)
		btext_drawchar(*c++);
}

void BTEXT
btext_drawhex(u32 v)
{
	static char hex_table[] = "0123456789abcdef";

	if (!boot_text_mapped)
		return;
	btext_drawchar(hex_table[(v >> 28) & 0x0000000FUL]);
	btext_drawchar(hex_table[(v >> 24) & 0x0000000FUL]);
	btext_drawchar(hex_table[(v >> 20) & 0x0000000FUL]);
	btext_drawchar(hex_table[(v >> 16) & 0x0000000FUL]);
	btext_drawchar(hex_table[(v >> 12) & 0x0000000FUL]);
	btext_drawchar(hex_table[(v >>  8) & 0x0000000FUL]);
	btext_drawchar(hex_table[(v >>  4) & 0x0000000FUL]);
	btext_drawchar(hex_table[(v >>  0) & 0x0000000FUL]);
	btext_drawchar(' ');
}
#endif
static void BTEXT
draw_byte(unsigned char c, u32 locX, u32 locY)
{
	boot_infos_t* bi	= &disp_bi;
	unsigned char *base	= calc_base(bi, locX << 3, locY << 4);
#if CHAR_256==1
        unsigned char *font     = &vga_font[((u32)c) * 16];
#else
	unsigned char *font	= &vga_font[((u32)c-0x20) * 16]; // skip the first 0x20
#endif
	u32 rb			= bi->dispDeviceRowBytes;

	switch(bi->dispDeviceDepth) {
#if 0
	case 24:
	case 32:
		draw_byte_32(font, (u32 *)base, rb);
		break;
	case 15:
	case 16:
		draw_byte_16(font, (u32 *)base, rb);
		break;
#endif
	case 8:
		draw_byte_8(font, (u32 *)base, rb);
		break;
	}
}
static u32 expand_bits_8[16] BTDATA = {
#if defined(__BIG_ENDIAN)
    0x00000000,0x000000ff,0x0000ff00,0x0000ffff,
    0x00ff0000,0x00ff00ff,0x00ffff00,0x00ffffff,
    0xff000000,0xff0000ff,0xff00ff00,0xff00ffff,
    0xffff0000,0xffff00ff,0xffffff00,0xffffffff
#elif defined(__LITTLE_ENDIAN)
    0x00000000,0xff000000,0x00ff0000,0xffff0000,
    0x0000ff00,0xff00ff00,0x00ffff00,0xffffff00,
    0x000000ff,0xff0000ff,0x00ff00ff,0xffff00ff,
    0x0000ffff,0xff00ffff,0x00ffffff,0xffffffff
#else
#error FIXME: No endianness??
#endif                      
};
#if 0
static const u32 expand_bits_16[4] BTDATA = {
#if defined(__BIG_ENDIAN)
    0x00000000, 0x0000ffff, 0xffff0000, 0xffffffff
#elif defined(__LITTLE_ENDIAN)
    0x00000000, 0xffff0000, 0x0000ffff, 0xffffffff
#else
#error FIXME: No endianness??
#endif
};
#endif
#if 0
static void BTEXT
draw_byte_32(unsigned char *font, u32 *base, u32 rb)
{
	u32 l, bits;
	u32 fg = 0xFFFFFFFF;
	u32 bg = 0x00000000;

	for (l = 0; l < 16; ++l)
	{
		bits = *font++;
		base[0] = (-(bits >> 7) & fg) ^ bg;
		base[1] = (-((bits >> 6) & 1) & fg) ^ bg;
		base[2] = (-((bits >> 5) & 1) & fg) ^ bg;
		base[3] = (-((bits >> 4) & 1) & fg) ^ bg;
		base[4] = (-((bits >> 3) & 1) & fg) ^ bg;
		base[5] = (-((bits >> 2) & 1) & fg) ^ bg;
		base[6] = (-((bits >> 1) & 1) & fg) ^ bg;
		base[7] = (-(bits & 1) & fg) ^ bg;
		base = (u32 *) ((char *)base + rb);
	}
}

static void BTEXT
draw_byte_16(unsigned char *font, u32 *base, u32 rb)
{
	u32 l, bits;
	u32 fg = 0xFFFFFFFF;
	u32 bg = 0x00000000;
	u32 *eb = expand_bits_16;

	for (l = 0; l < 16; ++l)
	{
		bits = *font++;
		base[0] = (eb[bits >> 6] & fg) ^ bg;
		base[1] = (eb[(bits >> 4) & 3] & fg) ^ bg;
		base[2] = (eb[(bits >> 2) & 3] & fg) ^ bg;
		base[3] = (eb[bits & 3] & fg) ^ bg;
		base = (u32 *) ((char *)base + rb);
	}
}
#endif
static void BTEXT
draw_byte_8(unsigned char *font, u32 *base, u32 rb)
{
	u32 l, bits;
	u32 fg = 0x0F0F0F0F;
	u32 bg = 0x00000000;
	u32 *eb = expand_bits_8;

	for (l = 0; l < 16; ++l)
	{
		bits = *font++;
		base[0] = (eb[bits >> 4] & fg) ^ bg;
		base[1] = (eb[bits & 0xf] & fg) ^ bg;
		base = (u32 *) ((char *)base + rb);
	}
}

void btext_init(void)
{
	btext_setup_display(640, 480, 8, 640,0xfc000000);
// Not realy init
// It will be init in xlinit.c because We only can access fb after the device resource is allocated and enabled.

}

void btext_tx_byte(unsigned char data)
{
        btext_drawchar(data);
}

static struct console_driver btext_console __console = {
        .init    = btext_init,
        .tx_byte = btext_tx_byte,
        .rx_byte = 0,
        .tst_byte = 0,
};