blob: 0a31963557048ed68639791460987131ce076b4b [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 +0200118#ifdef CONFIG_SERIAL_MULTI
119static struct serial_device sconsole_serial_drv = {
120 .name = "sconsole_serial",
121 .start = sconsole_serial_init,
122 .stop = NULL,
123 .setbrg = sconsole_serial_setbrg,
124 .putc = sconsole_serial_putc,
125 .puts = sconsole_serial_puts,
126 .getc = sconsole_serial_getc,
127 .tstc = sconsole_serial_tstc,
128};
129
130void sconsole_serial_initialize(void)
131{
132 serial_register(&sconsole_serial_drv);
133}
134
135__weak struct serial_device *default_serial_console(void)
136{
137 return &sconsole_serial_drv;
138}
139#else
140int serial_init(void)
141{
142 return sconsole_serial_init();
143}
144
145void serial_setbrg(void)
146{
147 sconsole_serial_setbrg();
148}
149
150void serial_putc(const char c)
151{
152 sconsole_serial_putc(c);
153}
154
155void serial_puts(const char *s)
156{
157 sconsole_serial_puts(s);
158}
159
160int serial_getc(void)
161{
162 return sconsole_serial_getc();
163}
164
165int serial_tstc(void)
166{
167 return sconsole_serial_tstc();
168}
169#endif
wdenkf8cac652002-08-26 22:36:39 +0000170int sconsole_get_baudrate (void)
171{
172 sconsole_buffer_t *sb = SCONSOLE_BUFFER;
173
174 return sb->baud;
175}
176
177void sconsole_flush (void)
178{
179 if (sconsole_putc) {
180 sconsole_buffer_t *sb = SCONSOLE_BUFFER;
181 unsigned int end = sb->pos < sb->size
182 ? sb->pos + sb->max_size - sb->size
183 : sb->pos - sb->size;
184
185 while (sb->size) {
186 (*sconsole_putc) (sb->data[end++]);
187 if (end == sb->max_size) {
188 end = 0;
189 }
190 sb->size--;
191 }
192 }
193}