Simon Glass | 7accb6e | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011 The Chromium OS Authors. |
Simon Glass | 7accb6e | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 3 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 4 | * SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 7accb6e | 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> |
| 14 | #include <os.h> |
Marek Vasut | cef46b7 | 2012-09-14 22:33:21 +0200 | [diff] [blame] | 15 | #include <serial.h> |
| 16 | #include <linux/compiler.h> |
Simon Glass | 7accb6e | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 17 | |
Taylor Hutt | e101550 | 2013-02-24 17:33:13 +0000 | [diff] [blame] | 18 | /* |
| 19 | * |
| 20 | * serial_buf: A buffer that holds keyboard characters for the |
| 21 | * Sandbox U-boot. |
| 22 | * |
| 23 | * invariants: |
| 24 | * serial_buf_write == serial_buf_read -> empty buffer |
| 25 | * (serial_buf_write + 1) % 16 == serial_buf_read -> full buffer |
| 26 | */ |
| 27 | static char serial_buf[16]; |
| 28 | static unsigned int serial_buf_write; |
| 29 | static unsigned int serial_buf_read; |
| 30 | |
Marek Vasut | cef46b7 | 2012-09-14 22:33:21 +0200 | [diff] [blame] | 31 | static int sandbox_serial_init(void) |
Simon Glass | 7accb6e | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 32 | { |
Mike Frysinger | ab06a75 | 2011-10-26 00:21:29 +0000 | [diff] [blame] | 33 | os_tty_raw(0); |
Simon Glass | 7accb6e | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 34 | return 0; |
| 35 | } |
| 36 | |
Marek Vasut | cef46b7 | 2012-09-14 22:33:21 +0200 | [diff] [blame] | 37 | static void sandbox_serial_setbrg(void) |
Simon Glass | 7accb6e | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 38 | { |
| 39 | } |
| 40 | |
Marek Vasut | cef46b7 | 2012-09-14 22:33:21 +0200 | [diff] [blame] | 41 | static void sandbox_serial_putc(const char ch) |
Simon Glass | 7accb6e | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 42 | { |
| 43 | os_write(1, &ch, 1); |
| 44 | } |
| 45 | |
Marek Vasut | cef46b7 | 2012-09-14 22:33:21 +0200 | [diff] [blame] | 46 | static void sandbox_serial_puts(const char *str) |
Simon Glass | 7accb6e | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 47 | { |
Mike Frysinger | 5778d54 | 2011-10-26 00:20:39 +0000 | [diff] [blame] | 48 | os_write(1, str, strlen(str)); |
Simon Glass | 7accb6e | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Taylor Hutt | e101550 | 2013-02-24 17:33:13 +0000 | [diff] [blame] | 51 | static unsigned int increment_buffer_index(unsigned int index) |
Simon Glass | 7accb6e | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 52 | { |
Taylor Hutt | e101550 | 2013-02-24 17:33:13 +0000 | [diff] [blame] | 53 | return (index + 1) % ARRAY_SIZE(serial_buf); |
Simon Glass | 7accb6e | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Marek Vasut | cef46b7 | 2012-09-14 22:33:21 +0200 | [diff] [blame] | 56 | static int sandbox_serial_tstc(void) |
Simon Glass | 7accb6e | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 57 | { |
Taylor Hutt | e101550 | 2013-02-24 17:33:13 +0000 | [diff] [blame] | 58 | const unsigned int next_index = |
| 59 | increment_buffer_index(serial_buf_write); |
| 60 | ssize_t count; |
| 61 | |
| 62 | os_usleep(100); |
| 63 | if (next_index == serial_buf_read) |
| 64 | return 1; /* buffer full */ |
| 65 | |
| 66 | count = os_read_no_block(0, &serial_buf[serial_buf_write], 1); |
| 67 | if (count == 1) |
| 68 | serial_buf_write = next_index; |
| 69 | return serial_buf_write != serial_buf_read; |
| 70 | } |
| 71 | |
| 72 | static int sandbox_serial_getc(void) |
| 73 | { |
| 74 | int result; |
| 75 | |
| 76 | while (!sandbox_serial_tstc()) |
| 77 | ; /* buffer empty */ |
| 78 | |
| 79 | result = serial_buf[serial_buf_read]; |
| 80 | serial_buf_read = increment_buffer_index(serial_buf_read); |
| 81 | return result; |
Simon Glass | 7accb6e | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 82 | } |
Marek Vasut | cef46b7 | 2012-09-14 22:33:21 +0200 | [diff] [blame] | 83 | |
Marek Vasut | cef46b7 | 2012-09-14 22:33:21 +0200 | [diff] [blame] | 84 | static struct serial_device sandbox_serial_drv = { |
| 85 | .name = "sandbox_serial", |
| 86 | .start = sandbox_serial_init, |
| 87 | .stop = NULL, |
| 88 | .setbrg = sandbox_serial_setbrg, |
| 89 | .putc = sandbox_serial_putc, |
| 90 | .puts = sandbox_serial_puts, |
| 91 | .getc = sandbox_serial_getc, |
| 92 | .tstc = sandbox_serial_tstc, |
| 93 | }; |
| 94 | |
| 95 | void sandbox_serial_initialize(void) |
| 96 | { |
| 97 | serial_register(&sandbox_serial_drv); |
| 98 | } |
| 99 | |
| 100 | __weak struct serial_device *default_serial_console(void) |
| 101 | { |
| 102 | return &sandbox_serial_drv; |
| 103 | } |