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 | static 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 | |
| 129 | void 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 Vasut | 4750eed | 2012-09-13 12:34:49 +0200 | [diff] [blame] | 138 | |
wdenk | f8cac65 | 2002-08-26 22:36:39 +0000 | [diff] [blame] | 139 | int sconsole_get_baudrate (void) |
| 140 | { |
| 141 | sconsole_buffer_t *sb = SCONSOLE_BUFFER; |
| 142 | |
| 143 | return sb->baud; |
| 144 | } |
| 145 | |
| 146 | void 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 | } |