Patrice Chotard | d7c0968 | 2018-08-03 15:07:41 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Copyright (c) 2018, STMicroelectronics |
| 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 7 | #include <log.h> |
Patrice Chotard | d7c0968 | 2018-08-03 15:07:41 +0200 | [diff] [blame] | 8 | #include <serial.h> |
| 9 | #include <dm.h> |
Sean Anderson | 679190c | 2022-04-04 14:17:59 -0400 | [diff] [blame] | 10 | #include <asm/serial.h> |
Patrice Chotard | d7c0968 | 2018-08-03 15:07:41 +0200 | [diff] [blame] | 11 | #include <dm/test.h> |
Simon Glass | 0e1fad4 | 2020-07-19 10:15:37 -0600 | [diff] [blame] | 12 | #include <test/test.h> |
Patrice Chotard | d7c0968 | 2018-08-03 15:07:41 +0200 | [diff] [blame] | 13 | #include <test/ut.h> |
| 14 | |
Sean Anderson | 679190c | 2022-04-04 14:17:59 -0400 | [diff] [blame] | 15 | static const char test_message[] = |
| 16 | "This is a test message\n" |
| 17 | "consisting of multiple lines\n"; |
| 18 | |
Patrice Chotard | d7c0968 | 2018-08-03 15:07:41 +0200 | [diff] [blame] | 19 | static int dm_test_serial(struct unit_test_state *uts) |
| 20 | { |
Sean Anderson | 679190c | 2022-04-04 14:17:59 -0400 | [diff] [blame] | 21 | int i; |
Andy Shevchenko | d5bb4f8 | 2018-11-20 23:52:33 +0200 | [diff] [blame] | 22 | struct serial_device_info info_serial = {0}; |
Patrice Chotard | d7c0968 | 2018-08-03 15:07:41 +0200 | [diff] [blame] | 23 | struct udevice *dev_serial; |
Sean Anderson | 679190c | 2022-04-04 14:17:59 -0400 | [diff] [blame] | 24 | size_t start, putc_written; |
| 25 | |
Andy Shevchenko | ac7f5db | 2018-11-20 23:52:32 +0200 | [diff] [blame] | 26 | uint value_serial; |
Patrice Chotard | d7c0968 | 2018-08-03 15:07:41 +0200 | [diff] [blame] | 27 | |
| 28 | ut_assertok(uclass_get_device_by_name(UCLASS_SERIAL, "serial", |
| 29 | &dev_serial)); |
| 30 | |
| 31 | ut_assertok(serial_tstc()); |
| 32 | /* |
| 33 | * test with default config which is the only one supported by |
| 34 | * sandbox_serial driver |
| 35 | */ |
Simon Glass | 3de04e7 | 2018-12-28 14:23:09 -0700 | [diff] [blame] | 36 | ut_assertok(serial_setconfig(dev_serial, SERIAL_DEFAULT_CONFIG)); |
Simon Glass | 67d1b05 | 2018-12-28 14:23:08 -0700 | [diff] [blame] | 37 | ut_assertok(serial_getconfig(dev_serial, &value_serial)); |
Andy Shevchenko | ac7f5db | 2018-11-20 23:52:32 +0200 | [diff] [blame] | 38 | ut_assert(value_serial == SERIAL_DEFAULT_CONFIG); |
Simon Glass | a61cbad | 2018-12-28 14:23:10 -0700 | [diff] [blame] | 39 | ut_assertok(serial_getinfo(dev_serial, &info_serial)); |
Andy Shevchenko | d5bb4f8 | 2018-11-20 23:52:33 +0200 | [diff] [blame] | 40 | ut_assert(info_serial.type == SERIAL_CHIP_UNKNOWN); |
| 41 | ut_assert(info_serial.addr == SERIAL_DEFAULT_ADDRESS); |
Andy Shevchenko | bf4661bc | 2020-02-27 17:21:54 +0200 | [diff] [blame] | 42 | ut_assert(info_serial.clock == SERIAL_DEFAULT_CLOCK); |
Andy Shevchenko | ac7f5db | 2018-11-20 23:52:32 +0200 | [diff] [blame] | 43 | /* |
| 44 | * test with a parameter which is NULL pointer |
| 45 | */ |
Simon Glass | 67d1b05 | 2018-12-28 14:23:08 -0700 | [diff] [blame] | 46 | ut_asserteq(-EINVAL, serial_getconfig(dev_serial, NULL)); |
Simon Glass | a61cbad | 2018-12-28 14:23:10 -0700 | [diff] [blame] | 47 | ut_asserteq(-EINVAL, serial_getinfo(dev_serial, NULL)); |
Patrice Chotard | d7c0968 | 2018-08-03 15:07:41 +0200 | [diff] [blame] | 48 | /* |
| 49 | * test with a serial config which is not supported by |
| 50 | * sandbox_serial driver: test with wrong parity |
| 51 | */ |
| 52 | ut_asserteq(-ENOTSUPP, |
Simon Glass | 3de04e7 | 2018-12-28 14:23:09 -0700 | [diff] [blame] | 53 | serial_setconfig(dev_serial, |
| 54 | SERIAL_CONFIG(SERIAL_PAR_ODD, |
Patrice Chotard | d7c0968 | 2018-08-03 15:07:41 +0200 | [diff] [blame] | 55 | SERIAL_8_BITS, |
| 56 | SERIAL_ONE_STOP))); |
| 57 | /* |
| 58 | * test with a serial config which is not supported by |
| 59 | * sandbox_serial driver: test with wrong bits number |
| 60 | */ |
| 61 | ut_asserteq(-ENOTSUPP, |
Simon Glass | 3de04e7 | 2018-12-28 14:23:09 -0700 | [diff] [blame] | 62 | serial_setconfig(dev_serial, |
| 63 | SERIAL_CONFIG(SERIAL_PAR_NONE, |
Patrice Chotard | d7c0968 | 2018-08-03 15:07:41 +0200 | [diff] [blame] | 64 | SERIAL_6_BITS, |
| 65 | SERIAL_ONE_STOP))); |
| 66 | |
| 67 | /* |
| 68 | * test with a serial config which is not supported by |
| 69 | * sandbox_serial driver: test with wrong stop bits number |
| 70 | */ |
| 71 | ut_asserteq(-ENOTSUPP, |
Simon Glass | 3de04e7 | 2018-12-28 14:23:09 -0700 | [diff] [blame] | 72 | serial_setconfig(dev_serial, |
| 73 | SERIAL_CONFIG(SERIAL_PAR_NONE, |
Patrice Chotard | d7c0968 | 2018-08-03 15:07:41 +0200 | [diff] [blame] | 74 | SERIAL_8_BITS, |
| 75 | SERIAL_TWO_STOP))); |
| 76 | |
Sean Anderson | 679190c | 2022-04-04 14:17:59 -0400 | [diff] [blame] | 77 | /* Verify that putc and puts print the same number of characters */ |
| 78 | sandbox_serial_endisable(false); |
| 79 | start = sandbox_serial_written(); |
| 80 | for (i = 0; i < sizeof(test_message) - 1; i++) |
| 81 | serial_putc(test_message[i]); |
| 82 | putc_written = sandbox_serial_written(); |
| 83 | serial_puts(test_message); |
| 84 | sandbox_serial_endisable(true); |
| 85 | ut_asserteq(putc_written - start, |
| 86 | sandbox_serial_written() - putc_written); |
| 87 | |
Patrice Chotard | d7c0968 | 2018-08-03 15:07:41 +0200 | [diff] [blame] | 88 | return 0; |
| 89 | } |
| 90 | |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 91 | DM_TEST(dm_test_serial, UT_TESTF_SCAN_FDT); |