blob: 456799a9eb817eac97d191ec850510fd114c28c1 [file] [log] [blame]
Patrice Chotard70ed0ea2018-08-03 15:07:41 +02001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (c) 2018, STMicroelectronics
4 */
5
Simon Glass0f2af882020-05-10 11:40:05 -06006#include <log.h>
Patrice Chotard70ed0ea2018-08-03 15:07:41 +02007#include <serial.h>
8#include <dm.h>
Sean Andersonb2711612022-04-04 14:17:59 -04009#include <asm/serial.h>
Patrice Chotard70ed0ea2018-08-03 15:07:41 +020010#include <dm/test.h>
Simon Glass75c4d412020-07-19 10:15:37 -060011#include <test/test.h>
Patrice Chotard70ed0ea2018-08-03 15:07:41 +020012#include <test/ut.h>
13
Sean Andersonb2711612022-04-04 14:17:59 -040014static const char test_message[] =
15 "This is a test message\n"
16 "consisting of multiple lines\n";
17
Patrice Chotard70ed0ea2018-08-03 15:07:41 +020018static int dm_test_serial(struct unit_test_state *uts)
19{
Sean Andersonb2711612022-04-04 14:17:59 -040020 int i;
Andy Shevchenko44f21da2018-11-20 23:52:33 +020021 struct serial_device_info info_serial = {0};
Patrice Chotard70ed0ea2018-08-03 15:07:41 +020022 struct udevice *dev_serial;
Sean Andersonb2711612022-04-04 14:17:59 -040023 size_t start, putc_written;
24
Andy Shevchenko08e98792018-11-20 23:52:32 +020025 uint value_serial;
Patrice Chotard70ed0ea2018-08-03 15:07:41 +020026
27 ut_assertok(uclass_get_device_by_name(UCLASS_SERIAL, "serial",
28 &dev_serial));
29
30 ut_assertok(serial_tstc());
Algapally Santosh Sagardf178992023-09-21 16:50:43 +053031 ut_asserteq(115200, fetch_baud_from_dtb());
Patrice Chotard70ed0ea2018-08-03 15:07:41 +020032 /*
33 * test with default config which is the only one supported by
34 * sandbox_serial driver
35 */
Simon Glassde62d482018-12-28 14:23:09 -070036 ut_assertok(serial_setconfig(dev_serial, SERIAL_DEFAULT_CONFIG));
Simon Glassdaaff932018-12-28 14:23:08 -070037 ut_assertok(serial_getconfig(dev_serial, &value_serial));
Andy Shevchenko08e98792018-11-20 23:52:32 +020038 ut_assert(value_serial == SERIAL_DEFAULT_CONFIG);
Simon Glass896c1642018-12-28 14:23:10 -070039 ut_assertok(serial_getinfo(dev_serial, &info_serial));
Andy Shevchenko44f21da2018-11-20 23:52:33 +020040 ut_assert(info_serial.type == SERIAL_CHIP_UNKNOWN);
41 ut_assert(info_serial.addr == SERIAL_DEFAULT_ADDRESS);
Andy Shevchenko106930e2020-02-27 17:21:54 +020042 ut_assert(info_serial.clock == SERIAL_DEFAULT_CLOCK);
Andy Shevchenko08e98792018-11-20 23:52:32 +020043 /*
44 * test with a parameter which is NULL pointer
45 */
Simon Glassdaaff932018-12-28 14:23:08 -070046 ut_asserteq(-EINVAL, serial_getconfig(dev_serial, NULL));
Simon Glass896c1642018-12-28 14:23:10 -070047 ut_asserteq(-EINVAL, serial_getinfo(dev_serial, NULL));
Patrice Chotard70ed0ea2018-08-03 15:07:41 +020048 /*
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 Glassde62d482018-12-28 14:23:09 -070053 serial_setconfig(dev_serial,
54 SERIAL_CONFIG(SERIAL_PAR_ODD,
Patrice Chotard70ed0ea2018-08-03 15:07:41 +020055 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 Glassde62d482018-12-28 14:23:09 -070062 serial_setconfig(dev_serial,
63 SERIAL_CONFIG(SERIAL_PAR_NONE,
Patrice Chotard70ed0ea2018-08-03 15:07:41 +020064 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 Glassde62d482018-12-28 14:23:09 -070072 serial_setconfig(dev_serial,
73 SERIAL_CONFIG(SERIAL_PAR_NONE,
Patrice Chotard70ed0ea2018-08-03 15:07:41 +020074 SERIAL_8_BITS,
75 SERIAL_TWO_STOP)));
76
Sean Andersonb2711612022-04-04 14:17:59 -040077 /* 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 Chotard70ed0ea2018-08-03 15:07:41 +020088 return 0;
89}
90
Simon Glass1a92f832024-08-22 07:57:48 -060091DM_TEST(dm_test_serial, UTF_SCAN_FDT);