xref: /haiku/src/system/boot/platform/u-boot/serial.cpp (revision b671e9bbdbd10268a042b4f4cc4317ccd03d105e)
1 /*
2  * Copyright 2004-2008, Axel Dörfler, axeld@pinc-software.de.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include "serial.h"
8 
9 #include "uart.h"
10 #include <boot/platform.h>
11 #include <arch/cpu.h>
12 #include <boot/stage2.h>
13 #include <string.h>
14 
15 // serial output should always be enabled on u-boot platforms..
16 #define ENABLE_SERIAL
17 
18 
19 static int32 sSerialEnabled = 0;
20 
21 static char sBuffer[16384];
22 static uint32 sBufferPosition;
23 
24 
25 static void
26 serial_putc(char c)
27 {
28 	uart_putc(uart_debug_port(),c);
29 }
30 
31 
32 extern "C" void
33 serial_puts(const char* string, size_t size)
34 {
35 	if (sSerialEnabled <= 0)
36 		return;
37 
38 	if (sBufferPosition + size < sizeof(sBuffer)) {
39 		memcpy(sBuffer + sBufferPosition, string, size);
40 		sBufferPosition += size;
41 	}
42 
43 	while (size-- != 0) {
44 		char c = string[0];
45 
46 		if (c == '\n') {
47 			serial_putc('\r');
48 			serial_putc('\n');
49 		} else if (c != '\r')
50 			serial_putc(c);
51 
52 		string++;
53 	}
54 }
55 
56 
57 extern "C" void
58 serial_disable(void)
59 {
60 #ifdef ENABLE_SERIAL
61 	sSerialEnabled = 0;
62 #else
63 	sSerialEnabled--;
64 #endif
65 }
66 
67 
68 extern "C" void
69 serial_enable(void)
70 {
71 	/* should already be initialized by U-Boot */
72 	/*
73 	uart_init_early();
74 	uart_init();//todo
75 	uart_init_port(uart_debug_port(),9600);
76 	*/
77 	sSerialEnabled++;
78 }
79 
80 
81 extern "C" void
82 serial_cleanup(void)
83 {
84 	if (sSerialEnabled <= 0)
85 		return;
86 
87 	gKernelArgs.debug_output = kernel_args_malloc(sBufferPosition);
88 	if (gKernelArgs.debug_output != NULL) {
89 		memcpy(gKernelArgs.debug_output, sBuffer, sBufferPosition);
90 		gKernelArgs.debug_size = sBufferPosition;
91 	}
92 }
93 
94 
95 extern "C" void
96 serial_init(void)
97 {
98 #ifdef ENABLE_SERIAL
99 	serial_enable();
100 #endif
101 }
102 
103