wdenk | f8cac65 | 2002-08-26 22:36:39 +0000 | [diff] [blame] | 1 | /* |
| 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 Vasut | 4750eed | 2012-09-13 12:34:49 +0200 | [diff] [blame^] | 26 | #include <serial.h> |
| 27 | #include <linux/compiler.h> |
wdenk | f8cac65 | 2002-08-26 22:36:39 +0000 | [diff] [blame] | 28 | |
| 29 | #include "sconsole.h" |
| 30 | |
Wolfgang Denk | 6405a15 | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 31 | DECLARE_GLOBAL_DATA_PTR; |
| 32 | |
wdenk | f8cac65 | 2002-08-26 22:36:39 +0000 | [diff] [blame] | 33 | void (*sconsole_putc) (char) = 0; |
| 34 | void (*sconsole_puts) (const char *) = 0; |
| 35 | int (*sconsole_getc) (void) = 0; |
| 36 | int (*sconsole_tstc) (void) = 0; |
| 37 | void (*sconsole_setbrg) (void) = 0; |
| 38 | |
Marek Vasut | 4750eed | 2012-09-13 12:34:49 +0200 | [diff] [blame^] | 39 | static int sconsole_serial_init(void) |
wdenk | f8cac65 | 2002-08-26 22:36:39 +0000 | [diff] [blame] | 40 | { |
wdenk | f8cac65 | 2002-08-26 22:36:39 +0000 | [diff] [blame] | 41 | sconsole_buffer_t *sb = SCONSOLE_BUFFER; |
| 42 | |
| 43 | sb->pos = 0; |
| 44 | sb->size = 0; |
| 45 | sb->baud = gd->baudrate; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 46 | sb->max_size = CONFIG_SYS_SCONSOLE_SIZE - sizeof (sconsole_buffer_t); |
wdenk | f8cac65 | 2002-08-26 22:36:39 +0000 | [diff] [blame] | 47 | |
| 48 | return (0); |
| 49 | } |
| 50 | |
Marek Vasut | 4750eed | 2012-09-13 12:34:49 +0200 | [diff] [blame^] | 51 | static void sconsole_serial_putc(char c) |
wdenk | f8cac65 | 2002-08-26 22:36:39 +0000 | [diff] [blame] | 52 | { |
| 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 Vasut | 4750eed | 2012-09-13 12:34:49 +0200 | [diff] [blame^] | 70 | static void sconsole_serial_puts(const char *s) |
wdenk | f8cac65 | 2002-08-26 22:36:39 +0000 | [diff] [blame] | 71 | { |
| 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 Vasut | 4750eed | 2012-09-13 12:34:49 +0200 | [diff] [blame^] | 89 | static int sconsole_serial_getc(void) |
wdenk | f8cac65 | 2002-08-26 22:36:39 +0000 | [diff] [blame] | 90 | { |
| 91 | if (sconsole_getc) { |
| 92 | return (*sconsole_getc) (); |
| 93 | } else { |
| 94 | return 0; |
| 95 | } |
| 96 | } |
| 97 | |
Marek Vasut | 4750eed | 2012-09-13 12:34:49 +0200 | [diff] [blame^] | 98 | static int sconsole_serial_tstc(void) |
wdenk | f8cac65 | 2002-08-26 22:36:39 +0000 | [diff] [blame] | 99 | { |
| 100 | if (sconsole_tstc) { |
| 101 | return (*sconsole_tstc) (); |
| 102 | } else { |
| 103 | return 0; |
| 104 | } |
| 105 | } |
| 106 | |
Marek Vasut | 4750eed | 2012-09-13 12:34:49 +0200 | [diff] [blame^] | 107 | static void sconsole_serial_setbrg(void) |
wdenk | f8cac65 | 2002-08-26 22:36:39 +0000 | [diff] [blame] | 108 | { |
wdenk | f8cac65 | 2002-08-26 22:36:39 +0000 | [diff] [blame] | 109 | 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 Vasut | 4750eed | 2012-09-13 12:34:49 +0200 | [diff] [blame^] | 118 | #ifdef CONFIG_SERIAL_MULTI |
| 119 | static 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 | |
| 130 | void 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 |
| 140 | int serial_init(void) |
| 141 | { |
| 142 | return sconsole_serial_init(); |
| 143 | } |
| 144 | |
| 145 | void serial_setbrg(void) |
| 146 | { |
| 147 | sconsole_serial_setbrg(); |
| 148 | } |
| 149 | |
| 150 | void serial_putc(const char c) |
| 151 | { |
| 152 | sconsole_serial_putc(c); |
| 153 | } |
| 154 | |
| 155 | void serial_puts(const char *s) |
| 156 | { |
| 157 | sconsole_serial_puts(s); |
| 158 | } |
| 159 | |
| 160 | int serial_getc(void) |
| 161 | { |
| 162 | return sconsole_serial_getc(); |
| 163 | } |
| 164 | |
| 165 | int serial_tstc(void) |
| 166 | { |
| 167 | return sconsole_serial_tstc(); |
| 168 | } |
| 169 | #endif |
wdenk | f8cac65 | 2002-08-26 22:36:39 +0000 | [diff] [blame] | 170 | int sconsole_get_baudrate (void) |
| 171 | { |
| 172 | sconsole_buffer_t *sb = SCONSOLE_BUFFER; |
| 173 | |
| 174 | return sb->baud; |
| 175 | } |
| 176 | |
| 177 | void 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 | } |