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 | e587069 | 2014-09-04 16:27:27 -0600 | [diff] [blame^] | 14 | #include <dm.h> |
| 15 | #include <fdtdec.h> |
Simon Glass | b9ddbf4 | 2014-02-27 13:26:19 -0700 | [diff] [blame] | 16 | #include <lcd.h> |
Simon Glass | 479349a | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 17 | #include <os.h> |
Marek Vasut | 289c300 | 2012-09-14 22:33:21 +0200 | [diff] [blame] | 18 | #include <serial.h> |
| 19 | #include <linux/compiler.h> |
Simon Glass | 678ef47 | 2014-02-27 13:26:22 -0700 | [diff] [blame] | 20 | #include <asm/state.h> |
Simon Glass | 479349a | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 21 | |
Simon Glass | e587069 | 2014-09-04 16:27:27 -0600 | [diff] [blame^] | 22 | DECLARE_GLOBAL_DATA_PTR; |
| 23 | |
Taylor Hutt | 868aa3a | 2013-02-24 17:33:13 +0000 | [diff] [blame] | 24 | /* |
| 25 | * |
| 26 | * serial_buf: A buffer that holds keyboard characters for the |
| 27 | * Sandbox U-boot. |
| 28 | * |
| 29 | * invariants: |
| 30 | * serial_buf_write == serial_buf_read -> empty buffer |
| 31 | * (serial_buf_write + 1) % 16 == serial_buf_read -> full buffer |
| 32 | */ |
| 33 | static char serial_buf[16]; |
| 34 | static unsigned int serial_buf_write; |
| 35 | static unsigned int serial_buf_read; |
| 36 | |
Simon Glass | e587069 | 2014-09-04 16:27:27 -0600 | [diff] [blame^] | 37 | static int sandbox_serial_probe(struct udevice *dev) |
Simon Glass | 479349a | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 38 | { |
Simon Glass | 678ef47 | 2014-02-27 13:26:22 -0700 | [diff] [blame] | 39 | struct sandbox_state *state = state_get_current(); |
| 40 | |
| 41 | if (state->term_raw != STATE_TERM_COOKED) |
| 42 | os_tty_raw(0, state->term_raw == STATE_TERM_RAW_WITH_SIGS); |
Simon Glass | 479349a | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 43 | |
Simon Glass | e587069 | 2014-09-04 16:27:27 -0600 | [diff] [blame^] | 44 | return 0; |
Simon Glass | 479349a | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Simon Glass | e587069 | 2014-09-04 16:27:27 -0600 | [diff] [blame^] | 47 | static int sandbox_serial_putc(struct udevice *dev, const char ch) |
Simon Glass | 479349a | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 48 | { |
| 49 | os_write(1, &ch, 1); |
Simon Glass | 479349a | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 50 | |
Simon Glass | e587069 | 2014-09-04 16:27:27 -0600 | [diff] [blame^] | 51 | return 0; |
Simon Glass | 479349a | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Taylor Hutt | 868aa3a | 2013-02-24 17:33:13 +0000 | [diff] [blame] | 54 | static unsigned int increment_buffer_index(unsigned int index) |
| 55 | { |
| 56 | return (index + 1) % ARRAY_SIZE(serial_buf); |
| 57 | } |
| 58 | |
Simon Glass | e587069 | 2014-09-04 16:27:27 -0600 | [diff] [blame^] | 59 | static int sandbox_serial_pending(struct udevice *dev, bool input) |
Simon Glass | 479349a | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 60 | { |
Taylor Hutt | 868aa3a | 2013-02-24 17:33:13 +0000 | [diff] [blame] | 61 | const unsigned int next_index = |
| 62 | increment_buffer_index(serial_buf_write); |
Mike Frysinger | d174fd9 | 2011-10-26 00:21:01 +0000 | [diff] [blame] | 63 | ssize_t count; |
Simon Glass | 479349a | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 64 | |
Simon Glass | e587069 | 2014-09-04 16:27:27 -0600 | [diff] [blame^] | 65 | if (!input) |
| 66 | return 0; |
| 67 | |
Taylor Hutt | 868aa3a | 2013-02-24 17:33:13 +0000 | [diff] [blame] | 68 | os_usleep(100); |
Simon Glass | b9ddbf4 | 2014-02-27 13:26:19 -0700 | [diff] [blame] | 69 | #ifdef CONFIG_LCD |
| 70 | lcd_sync(); |
| 71 | #endif |
Taylor Hutt | 868aa3a | 2013-02-24 17:33:13 +0000 | [diff] [blame] | 72 | if (next_index == serial_buf_read) |
| 73 | return 1; /* buffer full */ |
| 74 | |
| 75 | count = os_read_no_block(0, &serial_buf[serial_buf_write], 1); |
| 76 | if (count == 1) |
| 77 | serial_buf_write = next_index; |
Simon Glass | e587069 | 2014-09-04 16:27:27 -0600 | [diff] [blame^] | 78 | |
Taylor Hutt | 868aa3a | 2013-02-24 17:33:13 +0000 | [diff] [blame] | 79 | return serial_buf_write != serial_buf_read; |
Simon Glass | 479349a | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Simon Glass | e587069 | 2014-09-04 16:27:27 -0600 | [diff] [blame^] | 82 | static int sandbox_serial_getc(struct udevice *dev) |
Simon Glass | 479349a | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 83 | { |
Taylor Hutt | 868aa3a | 2013-02-24 17:33:13 +0000 | [diff] [blame] | 84 | int result; |
| 85 | |
Simon Glass | e587069 | 2014-09-04 16:27:27 -0600 | [diff] [blame^] | 86 | if (!sandbox_serial_pending(dev, true)) |
| 87 | return -EAGAIN; /* buffer empty */ |
Taylor Hutt | 868aa3a | 2013-02-24 17:33:13 +0000 | [diff] [blame] | 88 | |
| 89 | result = serial_buf[serial_buf_read]; |
| 90 | serial_buf_read = increment_buffer_index(serial_buf_read); |
| 91 | return result; |
Simon Glass | 479349a | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 92 | } |
Marek Vasut | 289c300 | 2012-09-14 22:33:21 +0200 | [diff] [blame] | 93 | |
Simon Glass | e587069 | 2014-09-04 16:27:27 -0600 | [diff] [blame^] | 94 | static const struct dm_serial_ops sandbox_serial_ops = { |
| 95 | .putc = sandbox_serial_putc, |
| 96 | .pending = sandbox_serial_pending, |
| 97 | .getc = sandbox_serial_getc, |
Marek Vasut | 289c300 | 2012-09-14 22:33:21 +0200 | [diff] [blame] | 98 | }; |
| 99 | |
Simon Glass | e587069 | 2014-09-04 16:27:27 -0600 | [diff] [blame^] | 100 | static const struct udevice_id sandbox_serial_ids[] = { |
| 101 | { .compatible = "sandbox,serial" }, |
| 102 | { } |
| 103 | }; |
Marek Vasut | 289c300 | 2012-09-14 22:33:21 +0200 | [diff] [blame] | 104 | |
Simon Glass | e587069 | 2014-09-04 16:27:27 -0600 | [diff] [blame^] | 105 | U_BOOT_DRIVER(serial_sandbox) = { |
| 106 | .name = "serial_sandbox", |
| 107 | .id = UCLASS_SERIAL, |
| 108 | .of_match = sandbox_serial_ids, |
| 109 | .probe = sandbox_serial_probe, |
| 110 | .ops = &sandbox_serial_ops, |
| 111 | .flags = DM_FLAG_PRE_RELOC, |
| 112 | }; |
| 113 | |
| 114 | U_BOOT_DEVICE(serial_sandbox_non_fdt) = { |
| 115 | .name = "serial_sandbox", |
| 116 | }; |