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 | 479349a | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 18 | |
Taylor Hutt | 868aa3a | 2013-02-24 17:33:13 +0000 | [diff] [blame] | 19 | /* |
| 20 | * |
| 21 | * serial_buf: A buffer that holds keyboard characters for the |
| 22 | * Sandbox U-boot. |
| 23 | * |
| 24 | * invariants: |
| 25 | * serial_buf_write == serial_buf_read -> empty buffer |
| 26 | * (serial_buf_write + 1) % 16 == serial_buf_read -> full buffer |
| 27 | */ |
| 28 | static char serial_buf[16]; |
| 29 | static unsigned int serial_buf_write; |
| 30 | static unsigned int serial_buf_read; |
| 31 | |
Marek Vasut | 289c300 | 2012-09-14 22:33:21 +0200 | [diff] [blame] | 32 | static int sandbox_serial_init(void) |
Simon Glass | 479349a | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 33 | { |
Mike Frysinger | a5baaee | 2011-10-26 00:21:29 +0000 | [diff] [blame] | 34 | os_tty_raw(0); |
Simon Glass | 479349a | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 35 | return 0; |
| 36 | } |
| 37 | |
Marek Vasut | 289c300 | 2012-09-14 22:33:21 +0200 | [diff] [blame] | 38 | static void sandbox_serial_setbrg(void) |
Simon Glass | 479349a | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 39 | { |
| 40 | } |
| 41 | |
Marek Vasut | 289c300 | 2012-09-14 22:33:21 +0200 | [diff] [blame] | 42 | static void sandbox_serial_putc(const char ch) |
Simon Glass | 479349a | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 43 | { |
| 44 | os_write(1, &ch, 1); |
| 45 | } |
| 46 | |
Marek Vasut | 289c300 | 2012-09-14 22:33:21 +0200 | [diff] [blame] | 47 | static void sandbox_serial_puts(const char *str) |
Simon Glass | 479349a | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 48 | { |
Mike Frysinger | d42e066 | 2011-10-26 00:20:39 +0000 | [diff] [blame] | 49 | os_write(1, str, strlen(str)); |
Simon Glass | 479349a | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Taylor Hutt | 868aa3a | 2013-02-24 17:33:13 +0000 | [diff] [blame] | 52 | static unsigned int increment_buffer_index(unsigned int index) |
| 53 | { |
| 54 | return (index + 1) % ARRAY_SIZE(serial_buf); |
| 55 | } |
| 56 | |
| 57 | static int sandbox_serial_tstc(void) |
Simon Glass | 479349a | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 58 | { |
Taylor Hutt | 868aa3a | 2013-02-24 17:33:13 +0000 | [diff] [blame] | 59 | const unsigned int next_index = |
| 60 | increment_buffer_index(serial_buf_write); |
Mike Frysinger | d174fd9 | 2011-10-26 00:21:01 +0000 | [diff] [blame] | 61 | ssize_t count; |
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 | os_usleep(100); |
Simon Glass | b9ddbf4 | 2014-02-27 13:26:19 -0700 | [diff] [blame^] | 64 | #ifdef CONFIG_LCD |
| 65 | lcd_sync(); |
| 66 | #endif |
Taylor Hutt | 868aa3a | 2013-02-24 17:33:13 +0000 | [diff] [blame] | 67 | if (next_index == serial_buf_read) |
| 68 | return 1; /* buffer full */ |
| 69 | |
| 70 | count = os_read_no_block(0, &serial_buf[serial_buf_write], 1); |
| 71 | if (count == 1) |
| 72 | serial_buf_write = next_index; |
| 73 | return serial_buf_write != serial_buf_read; |
Simon Glass | 479349a | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Taylor Hutt | 868aa3a | 2013-02-24 17:33:13 +0000 | [diff] [blame] | 76 | static int sandbox_serial_getc(void) |
Simon Glass | 479349a | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 77 | { |
Taylor Hutt | 868aa3a | 2013-02-24 17:33:13 +0000 | [diff] [blame] | 78 | int result; |
| 79 | |
| 80 | while (!sandbox_serial_tstc()) |
| 81 | ; /* buffer empty */ |
| 82 | |
| 83 | result = serial_buf[serial_buf_read]; |
| 84 | serial_buf_read = increment_buffer_index(serial_buf_read); |
| 85 | return result; |
Simon Glass | 479349a | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 86 | } |
Marek Vasut | 289c300 | 2012-09-14 22:33:21 +0200 | [diff] [blame] | 87 | |
Marek Vasut | 289c300 | 2012-09-14 22:33:21 +0200 | [diff] [blame] | 88 | static struct serial_device sandbox_serial_drv = { |
| 89 | .name = "sandbox_serial", |
| 90 | .start = sandbox_serial_init, |
| 91 | .stop = NULL, |
| 92 | .setbrg = sandbox_serial_setbrg, |
| 93 | .putc = sandbox_serial_putc, |
| 94 | .puts = sandbox_serial_puts, |
| 95 | .getc = sandbox_serial_getc, |
| 96 | .tstc = sandbox_serial_tstc, |
| 97 | }; |
| 98 | |
| 99 | void sandbox_serial_initialize(void) |
| 100 | { |
| 101 | serial_register(&sandbox_serial_drv); |
| 102 | } |
| 103 | |
| 104 | __weak struct serial_device *default_serial_console(void) |
| 105 | { |
| 106 | return &sandbox_serial_drv; |
| 107 | } |