sconsole.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. #include <config.h>
  24. #include <common.h>
  25. #include <serial.h>
  26. #include <linux/compiler.h>
  27. #include "sconsole.h"
  28. DECLARE_GLOBAL_DATA_PTR;
  29. void (*sconsole_putc) (char) = 0;
  30. void (*sconsole_puts) (const char *) = 0;
  31. int (*sconsole_getc) (void) = 0;
  32. int (*sconsole_tstc) (void) = 0;
  33. void (*sconsole_setbrg) (void) = 0;
  34. static int sconsole_serial_init(void)
  35. {
  36. sconsole_buffer_t *sb = SCONSOLE_BUFFER;
  37. sb->pos = 0;
  38. sb->size = 0;
  39. sb->baud = gd->baudrate;
  40. sb->max_size = CONFIG_SYS_SCONSOLE_SIZE - sizeof (sconsole_buffer_t);
  41. return (0);
  42. }
  43. static void sconsole_serial_putc(char c)
  44. {
  45. if (sconsole_putc) {
  46. (*sconsole_putc) (c);
  47. } else {
  48. sconsole_buffer_t *sb = SCONSOLE_BUFFER;
  49. if (c) {
  50. sb->data[sb->pos++] = c;
  51. if (sb->pos == sb->max_size) {
  52. sb->pos = 0;
  53. }
  54. if (sb->size < sb->max_size) {
  55. sb->size++;
  56. }
  57. }
  58. }
  59. }
  60. static void sconsole_serial_puts(const char *s)
  61. {
  62. if (sconsole_puts) {
  63. (*sconsole_puts) (s);
  64. } else {
  65. sconsole_buffer_t *sb = SCONSOLE_BUFFER;
  66. while (*s) {
  67. sb->data[sb->pos++] = *s++;
  68. if (sb->pos == sb->max_size) {
  69. sb->pos = 0;
  70. }
  71. if (sb->size < sb->max_size) {
  72. sb->size++;
  73. }
  74. }
  75. }
  76. }
  77. static int sconsole_serial_getc(void)
  78. {
  79. if (sconsole_getc) {
  80. return (*sconsole_getc) ();
  81. } else {
  82. return 0;
  83. }
  84. }
  85. static int sconsole_serial_tstc(void)
  86. {
  87. if (sconsole_tstc) {
  88. return (*sconsole_tstc) ();
  89. } else {
  90. return 0;
  91. }
  92. }
  93. static void sconsole_serial_setbrg(void)
  94. {
  95. if (sconsole_setbrg) {
  96. (*sconsole_setbrg) ();
  97. } else {
  98. sconsole_buffer_t *sb = SCONSOLE_BUFFER;
  99. sb->baud = gd->baudrate;
  100. }
  101. }
  102. static struct serial_device sconsole_serial_drv = {
  103. .name = "sconsole_serial",
  104. .start = sconsole_serial_init,
  105. .stop = NULL,
  106. .setbrg = sconsole_serial_setbrg,
  107. .putc = sconsole_serial_putc,
  108. .puts = sconsole_serial_puts,
  109. .getc = sconsole_serial_getc,
  110. .tstc = sconsole_serial_tstc,
  111. };
  112. void sconsole_serial_initialize(void)
  113. {
  114. serial_register(&sconsole_serial_drv);
  115. }
  116. __weak struct serial_device *default_serial_console(void)
  117. {
  118. return &sconsole_serial_drv;
  119. }
  120. int sconsole_get_baudrate (void)
  121. {
  122. sconsole_buffer_t *sb = SCONSOLE_BUFFER;
  123. return sb->baud;
  124. }
  125. void sconsole_flush (void)
  126. {
  127. if (sconsole_putc) {
  128. sconsole_buffer_t *sb = SCONSOLE_BUFFER;
  129. unsigned int end = sb->pos < sb->size
  130. ? sb->pos + sb->max_size - sb->size
  131. : sb->pos - sb->size;
  132. while (sb->size) {
  133. (*sconsole_putc) (sb->data[end++]);
  134. if (end == sb->max_size) {
  135. end = 0;
  136. }
  137. sb->size--;
  138. }
  139. }
  140. }