Simon Glass | 479349a | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011 The Chromium OS Authors. |
Simon Glass | 479349a | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 3 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 4 | * SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 479349a | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | /* |
| 8 | * This provide a test serial port. It provides an emulated serial port where |
| 9 | * a test program and read out the serial output and inject serial input for |
| 10 | * U-Boot. |
| 11 | */ |
| 12 | |
| 13 | #include <common.h> |
Simon Glass | b9ddbf4 | 2014-02-27 13:26:19 -0700 | [diff] [blame] | 14 | #include <lcd.h> |
Simon Glass | 479349a | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 15 | #include <os.h> |
Marek Vasut | 289c300 | 2012-09-14 22:33:21 +0200 | [diff] [blame] | 16 | #include <serial.h> |
| 17 | #include <linux/compiler.h> |
Simon Glass | 678ef47 | 2014-02-27 13:26:22 -0700 | [diff] [blame] | 18 | #include <asm/state.h> |
Simon Glass | 479349a | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 19 | |
Taylor Hutt | 868aa3a | 2013-02-24 17:33:13 +0000 | [diff] [blame] | 20 | /* |
| 21 | * |
| 22 | * serial_buf: A buffer that holds keyboard characters for the |
| 23 | * Sandbox U-boot. |
| 24 | * |
| 25 | * invariants: |
| 26 | * serial_buf_write == serial_buf_read -> empty buffer |
| 27 | * (serial_buf_write + 1) % 16 == serial_buf_read -> full buffer |
| 28 | */ |
| 29 | static char serial_buf[16]; |
| 30 | static unsigned int serial_buf_write; |
| 31 | static unsigned int serial_buf_read; |
| 32 | |
Marek Vasut | 289c300 | 2012-09-14 22:33:21 +0200 | [diff] [blame] | 33 | static int sandbox_serial_init(void) |
Simon Glass | 479349a | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 34 | { |
Simon Glass | 678ef47 | 2014-02-27 13:26:22 -0700 | [diff] [blame] | 35 | struct sandbox_state *state = state_get_current(); |
| 36 | |
| 37 | if (state->term_raw != STATE_TERM_COOKED) |
| 38 | os_tty_raw(0, state->term_raw == STATE_TERM_RAW_WITH_SIGS); |
Simon Glass | 479349a | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 39 | return 0; |
| 40 | } |
| 41 | |
Marek Vasut | 289c300 | 2012-09-14 22:33:21 +0200 | [diff] [blame] | 42 | static void sandbox_serial_setbrg(void) |
Simon Glass | 479349a | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 43 | { |
| 44 | } |
| 45 | |
Marek Vasut | 289c300 | 2012-09-14 22:33:21 +0200 | [diff] [blame] | 46 | static void sandbox_serial_putc(const char ch) |
Simon Glass | 479349a | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 47 | { |
| 48 | os_write(1, &ch, 1); |
| 49 | } |
| 50 | |
Marek Vasut | 289c300 | 2012-09-14 22:33:21 +0200 | [diff] [blame] | 51 | static void sandbox_serial_puts(const char *str) |
Simon Glass | 479349a | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 52 | { |
Mike Frysinger | d42e066 | 2011-10-26 00:20:39 +0000 | [diff] [blame] | 53 | os_write(1, str, strlen(str)); |
Simon Glass | 479349a | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Taylor Hutt | 868aa3a | 2013-02-24 17:33:13 +0000 | [diff] [blame] | 56 | static unsigned int increment_buffer_index(unsigned int index) |
| 57 | { |
| 58 | return (index + 1) % ARRAY_SIZE(serial_buf); |
| 59 | } |
| 60 | |
| 61 | static int sandbox_serial_tstc(void) |
Simon Glass | 479349a | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 62 | { |
Taylor Hutt | 868aa3a | 2013-02-24 17:33:13 +0000 | [diff] [blame] | 63 | const unsigned int next_index = |
| 64 | increment_buffer_index(serial_buf_write); |
Mike Frysinger | d174fd9 | 2011-10-26 00:21:01 +0000 | [diff] [blame] | 65 | ssize_t count; |
Simon Glass | 479349a | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 66 | |
Taylor Hutt | 868aa3a | 2013-02-24 17:33:13 +0000 | [diff] [blame] | 67 | os_usleep(100); |
Simon Glass | b9ddbf4 | 2014-02-27 13:26:19 -0700 | [diff] [blame] | 68 | #ifdef CONFIG_LCD |
| 69 | lcd_sync(); |
| 70 | #endif |
Taylor Hutt | 868aa3a | 2013-02-24 17:33:13 +0000 | [diff] [blame] | 71 | if (next_index == serial_buf_read) |
| 72 | return 1; /* buffer full */ |
| 73 | |
| 74 | count = os_read_no_block(0, &serial_buf[serial_buf_write], 1); |
| 75 | if (count == 1) |
| 76 | serial_buf_write = next_index; |
| 77 | return serial_buf_write != serial_buf_read; |
Simon Glass | 479349a | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Taylor Hutt | 868aa3a | 2013-02-24 17:33:13 +0000 | [diff] [blame] | 80 | static int sandbox_serial_getc(void) |
Simon Glass | 479349a | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 81 | { |
Taylor Hutt | 868aa3a | 2013-02-24 17:33:13 +0000 | [diff] [blame] | 82 | int result; |
| 83 | |
| 84 | while (!sandbox_serial_tstc()) |
| 85 | ; /* buffer empty */ |
| 86 | |
| 87 | result = serial_buf[serial_buf_read]; |
| 88 | serial_buf_read = increment_buffer_index(serial_buf_read); |
| 89 | return result; |
Simon Glass | 479349a | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 90 | } |
Marek Vasut | 289c300 | 2012-09-14 22:33:21 +0200 | [diff] [blame] | 91 | |
Marek Vasut | 289c300 | 2012-09-14 22:33:21 +0200 | [diff] [blame] | 92 | static struct serial_device sandbox_serial_drv = { |
| 93 | .name = "sandbox_serial", |
| 94 | .start = sandbox_serial_init, |
| 95 | .stop = NULL, |
| 96 | .setbrg = sandbox_serial_setbrg, |
| 97 | .putc = sandbox_serial_putc, |
| 98 | .puts = sandbox_serial_puts, |
| 99 | .getc = sandbox_serial_getc, |
| 100 | .tstc = sandbox_serial_tstc, |
| 101 | }; |
| 102 | |
| 103 | void sandbox_serial_initialize(void) |
| 104 | { |
| 105 | serial_register(&sandbox_serial_drv); |
| 106 | } |
| 107 | |
| 108 | __weak struct serial_device *default_serial_console(void) |
| 109 | { |
| 110 | return &sandbox_serial_drv; |
| 111 | } |