blob: aa3c908c6382ad80618398989fd5c3951e4b9f48 [file] [log] [blame]
wdenkf8cac652002-08-26 22:36:39 +00001/*
2 * (C) Copyright 2002
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
24#include <config.h>
25#include <common.h>
Marek Vasut26d8eaa2012-09-13 12:34:49 +020026#include <serial.h>
27#include <linux/compiler.h>
wdenkf8cac652002-08-26 22:36:39 +000028
29#include "sconsole.h"
30
Wolfgang Denkd87080b2006-03-31 18:32:53 +020031DECLARE_GLOBAL_DATA_PTR;
32
wdenkf8cac652002-08-26 22:36:39 +000033void (*sconsole_putc) (char) = 0;
34void (*sconsole_puts) (const char *) = 0;
35int (*sconsole_getc) (void) = 0;
36int (*sconsole_tstc) (void) = 0;
37void (*sconsole_setbrg) (void) = 0;
38
Marek Vasut26d8eaa2012-09-13 12:34:49 +020039static int sconsole_serial_init(void)
wdenkf8cac652002-08-26 22:36:39 +000040{
wdenkf8cac652002-08-26 22:36:39 +000041 sconsole_buffer_t *sb = SCONSOLE_BUFFER;
42
43 sb->pos = 0;
44 sb->size = 0;
45 sb->baud = gd->baudrate;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020046 sb->max_size = CONFIG_SYS_SCONSOLE_SIZE - sizeof (sconsole_buffer_t);
wdenkf8cac652002-08-26 22:36:39 +000047
48 return (0);
49}
50
Marek Vasut26d8eaa2012-09-13 12:34:49 +020051static void sconsole_serial_putc(char c)
wdenkf8cac652002-08-26 22:36:39 +000052{
53 if (sconsole_putc) {
54 (*sconsole_putc) (c);
55 } else {
56 sconsole_buffer_t *sb = SCONSOLE_BUFFER;
57
58 if (c) {
59 sb->data[sb->pos++] = c;
60 if (sb->pos == sb->max_size) {
61 sb->pos = 0;
62 }
63 if (sb->size < sb->max_size) {
64 sb->size++;
65 }
66 }
67 }
68}
69
Marek Vasut26d8eaa2012-09-13 12:34:49 +020070static void sconsole_serial_puts(const char *s)
wdenkf8cac652002-08-26 22:36:39 +000071{
72 if (sconsole_puts) {
73 (*sconsole_puts) (s);
74 } else {
75 sconsole_buffer_t *sb = SCONSOLE_BUFFER;
76
77 while (*s) {
78 sb->data[sb->pos++] = *s++;
79 if (sb->pos == sb->max_size) {
80 sb->pos = 0;
81 }
82 if (sb->size < sb->max_size) {
83 sb->size++;
84 }
85 }
86 }
87}
88
Marek Vasut26d8eaa2012-09-13 12:34:49 +020089static int sconsole_serial_getc(void)
wdenkf8cac652002-08-26 22:36:39 +000090{
91 if (sconsole_getc) {
92 return (*sconsole_getc) ();
93 } else {
94 return 0;
95 }
96}
97
Marek Vasut26d8eaa2012-09-13 12:34:49 +020098static int sconsole_serial_tstc(void)
wdenkf8cac652002-08-26 22:36:39 +000099{
100 if (sconsole_tstc) {
101 return (*sconsole_tstc) ();
102 } else {
103 return 0;
104 }
105}
106
Marek Vasut26d8eaa2012-09-13 12:34:49 +0200107static void sconsole_serial_setbrg(void)
wdenkf8cac652002-08-26 22:36:39 +0000108{
wdenkf8cac652002-08-26 22:36:39 +0000109 if (sconsole_setbrg) {
110 (*sconsole_setbrg) ();
111 } else {
112 sconsole_buffer_t *sb = SCONSOLE_BUFFER;
113
114 sb->baud = gd->baudrate;
115 }
116}
117
Marek Vasut26d8eaa2012-09-13 12:34:49 +0200118static struct serial_device sconsole_serial_drv = {
119 .name = "sconsole_serial",
120 .start = sconsole_serial_init,
121 .stop = NULL,
122 .setbrg = sconsole_serial_setbrg,
123 .putc = sconsole_serial_putc,
124 .puts = sconsole_serial_puts,
125 .getc = sconsole_serial_getc,
126 .tstc = sconsole_serial_tstc,
127};
128
129void sconsole_serial_initialize(void)
130{
131 serial_register(&sconsole_serial_drv);
132}
133
134__weak struct serial_device *default_serial_console(void)
135{
136 return &sconsole_serial_drv;
137}
Marek Vasut26d8eaa2012-09-13 12:34:49 +0200138
wdenkf8cac652002-08-26 22:36:39 +0000139int sconsole_get_baudrate (void)
140{
141 sconsole_buffer_t *sb = SCONSOLE_BUFFER;
142
143 return sb->baud;
144}
145
146void sconsole_flush (void)
147{
148 if (sconsole_putc) {
149 sconsole_buffer_t *sb = SCONSOLE_BUFFER;
150 unsigned int end = sb->pos < sb->size
151 ? sb->pos + sb->max_size - sb->size
152 : sb->pos - sb->size;
153
154 while (sb->size) {
155 (*sconsole_putc) (sb->data[end++]);
156 if (end == sb->max_size) {
157 end = 0;
158 }
159 sb->size--;
160 }
161 }
162}