blob: 34b783e062e1f3bbe3add5e4166d115edfffd283 [file] [log] [blame]
Patrice Chotardd7c09682018-08-03 15:07:41 +02001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (c) 2018, STMicroelectronics
4 */
5
6#include <common.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -06007#include <log.h>
Patrice Chotardd7c09682018-08-03 15:07:41 +02008#include <serial.h>
9#include <dm.h>
Sean Anderson679190c2022-04-04 14:17:59 -040010#include <asm/serial.h>
Patrice Chotardd7c09682018-08-03 15:07:41 +020011#include <dm/test.h>
Simon Glass0e1fad42020-07-19 10:15:37 -060012#include <test/test.h>
Patrice Chotardd7c09682018-08-03 15:07:41 +020013#include <test/ut.h>
14
Sean Anderson679190c2022-04-04 14:17:59 -040015static const char test_message[] =
16 "This is a test message\n"
17 "consisting of multiple lines\n";
18
Patrice Chotardd7c09682018-08-03 15:07:41 +020019static int dm_test_serial(struct unit_test_state *uts)
20{
Sean Anderson679190c2022-04-04 14:17:59 -040021 int i;
Andy Shevchenkod5bb4f82018-11-20 23:52:33 +020022 struct serial_device_info info_serial = {0};
Patrice Chotardd7c09682018-08-03 15:07:41 +020023 struct udevice *dev_serial;
Sean Anderson679190c2022-04-04 14:17:59 -040024 size_t start, putc_written;
25
Andy Shevchenkoac7f5db2018-11-20 23:52:32 +020026 uint value_serial;
Patrice Chotardd7c09682018-08-03 15:07:41 +020027
28 ut_assertok(uclass_get_device_by_name(UCLASS_SERIAL, "serial",
29 &dev_serial));
30
31 ut_assertok(serial_tstc());
Algapally Santosh Sagarbd9ff682023-09-21 16:50:43 +053032 ut_asserteq(115200, fetch_baud_from_dtb());
Patrice Chotardd7c09682018-08-03 15:07:41 +020033 /*
34 * test with default config which is the only one supported by
35 * sandbox_serial driver
36 */
Simon Glass3de04e72018-12-28 14:23:09 -070037 ut_assertok(serial_setconfig(dev_serial, SERIAL_DEFAULT_CONFIG));
Simon Glass67d1b052018-12-28 14:23:08 -070038 ut_assertok(serial_getconfig(dev_serial, &value_serial));
Andy Shevchenkoac7f5db2018-11-20 23:52:32 +020039 ut_assert(value_serial == SERIAL_DEFAULT_CONFIG);
Simon Glassa61cbad2018-12-28 14:23:10 -070040 ut_assertok(serial_getinfo(dev_serial, &info_serial));
Andy Shevchenkod5bb4f82018-11-20 23:52:33 +020041 ut_assert(info_serial.type == SERIAL_CHIP_UNKNOWN);
42 ut_assert(info_serial.addr == SERIAL_DEFAULT_ADDRESS);
Andy Shevchenkobf4661bc2020-02-27 17:21:54 +020043 ut_assert(info_serial.clock == SERIAL_DEFAULT_CLOCK);
Andy Shevchenkoac7f5db2018-11-20 23:52:32 +020044 /*
45 * test with a parameter which is NULL pointer
46 */
Simon Glass67d1b052018-12-28 14:23:08 -070047 ut_asserteq(-EINVAL, serial_getconfig(dev_serial, NULL));
Simon Glassa61cbad2018-12-28 14:23:10 -070048 ut_asserteq(-EINVAL, serial_getinfo(dev_serial, NULL));
Patrice Chotardd7c09682018-08-03 15:07:41 +020049 /*
50 * test with a serial config which is not supported by
51 * sandbox_serial driver: test with wrong parity
52 */
53 ut_asserteq(-ENOTSUPP,
Simon Glass3de04e72018-12-28 14:23:09 -070054 serial_setconfig(dev_serial,
55 SERIAL_CONFIG(SERIAL_PAR_ODD,
Patrice Chotardd7c09682018-08-03 15:07:41 +020056 SERIAL_8_BITS,
57 SERIAL_ONE_STOP)));
58 /*
59 * test with a serial config which is not supported by
60 * sandbox_serial driver: test with wrong bits number
61 */
62 ut_asserteq(-ENOTSUPP,
Simon Glass3de04e72018-12-28 14:23:09 -070063 serial_setconfig(dev_serial,
64 SERIAL_CONFIG(SERIAL_PAR_NONE,
Patrice Chotardd7c09682018-08-03 15:07:41 +020065 SERIAL_6_BITS,
66 SERIAL_ONE_STOP)));
67
68 /*
69 * test with a serial config which is not supported by
70 * sandbox_serial driver: test with wrong stop bits number
71 */
72 ut_asserteq(-ENOTSUPP,
Simon Glass3de04e72018-12-28 14:23:09 -070073 serial_setconfig(dev_serial,
74 SERIAL_CONFIG(SERIAL_PAR_NONE,
Patrice Chotardd7c09682018-08-03 15:07:41 +020075 SERIAL_8_BITS,
76 SERIAL_TWO_STOP)));
77
Sean Anderson679190c2022-04-04 14:17:59 -040078 /* Verify that putc and puts print the same number of characters */
79 sandbox_serial_endisable(false);
80 start = sandbox_serial_written();
81 for (i = 0; i < sizeof(test_message) - 1; i++)
82 serial_putc(test_message[i]);
83 putc_written = sandbox_serial_written();
84 serial_puts(test_message);
85 sandbox_serial_endisable(true);
86 ut_asserteq(putc_written - start,
87 sandbox_serial_written() - putc_written);
88
Patrice Chotardd7c09682018-08-03 15:07:41 +020089 return 0;
90}
91
Simon Glasse180c2b2020-07-28 19:41:12 -060092DM_TEST(dm_test_serial, UT_TESTF_SCAN_FDT);