30 #include "sw/device/lib/runtime/irq.h"
33 #include "sw/device/lib/testing/i2c_testutils.h"
34 #include "sw/device/lib/testing/json/command.h"
35 #include "sw/device/lib/testing/rv_plic_testutils.h"
36 #include "sw/device/lib/testing/test_framework/check.h"
37 #include "sw/device/lib/testing/test_framework/ottf_console.h"
39 #include "sw/device/lib/testing/test_framework/ujson_ottf.h"
40 #include "sw/device/lib/testing/test_framework/ujson_ottf_commands.h"
41 #include "sw/device/lib/ujson/ujson.h"
46 static_assert(__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__,
47 "This test assumes the target platform is little endian.");
49 OTTF_DEFINE_TEST_CONFIG(.enable_uart_flow_control =
true);
52 kArpCmdPrepareToArp = 0x01,
53 kArpCmdResetDevice = 0x02,
54 kArpCmdGetUdid = 0x03,
55 kArpCmdAssignAddress = 0x04,
58 static const uint8_t pec_lut[256] = {
59 [0x00] = 0x00, [0x01] = 0x07, [0x02] = 0x0e, [0x03] = 0x09, [0x04] = 0x1c,
60 [0x05] = 0x1b, [0x06] = 0x12, [0x07] = 0x15, [0x08] = 0x38, [0x09] = 0x3f,
61 [0x0a] = 0x36, [0x0b] = 0x31, [0x0c] = 0x24, [0x0d] = 0x23, [0x0e] = 0x2a,
62 [0x0f] = 0x2d, [0x10] = 0x70, [0x11] = 0x77, [0x12] = 0x7e, [0x13] = 0x79,
63 [0x14] = 0x6c, [0x15] = 0x6b, [0x16] = 0x62, [0x17] = 0x65, [0x18] = 0x48,
64 [0x19] = 0x4f, [0x1a] = 0x46, [0x1b] = 0x41, [0x1c] = 0x54, [0x1d] = 0x53,
65 [0x1e] = 0x5a, [0x1f] = 0x5d, [0x20] = 0xe0, [0x21] = 0xe7, [0x22] = 0xee,
66 [0x23] = 0xe9, [0x24] = 0xfc, [0x25] = 0xfb, [0x26] = 0xf2, [0x27] = 0xf5,
67 [0x28] = 0xd8, [0x29] = 0xdf, [0x2a] = 0xd6, [0x2b] = 0xd1, [0x2c] = 0xc4,
68 [0x2d] = 0xc3, [0x2e] = 0xca, [0x2f] = 0xcd, [0x30] = 0x90, [0x31] = 0x97,
69 [0x32] = 0x9e, [0x33] = 0x99, [0x34] = 0x8c, [0x35] = 0x8b, [0x36] = 0x82,
70 [0x37] = 0x85, [0x38] = 0xa8, [0x39] = 0xaf, [0x3a] = 0xa6, [0x3b] = 0xa1,
71 [0x3c] = 0xb4, [0x3d] = 0xb3, [0x3e] = 0xba, [0x3f] = 0xbd, [0x40] = 0xc7,
72 [0x41] = 0xc0, [0x42] = 0xc9, [0x43] = 0xce, [0x44] = 0xdb, [0x45] = 0xdc,
73 [0x46] = 0xd5, [0x47] = 0xd2, [0x48] = 0xff, [0x49] = 0xf8, [0x4a] = 0xf1,
74 [0x4b] = 0xf6, [0x4c] = 0xe3, [0x4d] = 0xe4, [0x4e] = 0xed, [0x4f] = 0xea,
75 [0x50] = 0xb7, [0x51] = 0xb0, [0x52] = 0xb9, [0x53] = 0xbe, [0x54] = 0xab,
76 [0x55] = 0xac, [0x56] = 0xa5, [0x57] = 0xa2, [0x58] = 0x8f, [0x59] = 0x88,
77 [0x5a] = 0x81, [0x5b] = 0x86, [0x5c] = 0x93, [0x5d] = 0x94, [0x5e] = 0x9d,
78 [0x5f] = 0x9a, [0x60] = 0x27, [0x61] = 0x20, [0x62] = 0x29, [0x63] = 0x2e,
79 [0x64] = 0x3b, [0x65] = 0x3c, [0x66] = 0x35, [0x67] = 0x32, [0x68] = 0x1f,
80 [0x69] = 0x18, [0x6a] = 0x11, [0x6b] = 0x16, [0x6c] = 0x03, [0x6d] = 0x04,
81 [0x6e] = 0x0d, [0x6f] = 0x0a, [0x70] = 0x57, [0x71] = 0x50, [0x72] = 0x59,
82 [0x73] = 0x5e, [0x74] = 0x4b, [0x75] = 0x4c, [0x76] = 0x45, [0x77] = 0x42,
83 [0x78] = 0x6f, [0x79] = 0x68, [0x7a] = 0x61, [0x7b] = 0x66, [0x7c] = 0x73,
84 [0x7d] = 0x74, [0x7e] = 0x7d, [0x7f] = 0x7a, [0x80] = 0x89, [0x81] = 0x8e,
85 [0x82] = 0x87, [0x83] = 0x80, [0x84] = 0x95, [0x85] = 0x92, [0x86] = 0x9b,
86 [0x87] = 0x9c, [0x88] = 0xb1, [0x89] = 0xb6, [0x8a] = 0xbf, [0x8b] = 0xb8,
87 [0x8c] = 0xad, [0x8d] = 0xaa, [0x8e] = 0xa3, [0x8f] = 0xa4, [0x90] = 0xf9,
88 [0x91] = 0xfe, [0x92] = 0xf7, [0x93] = 0xf0, [0x94] = 0xe5, [0x95] = 0xe2,
89 [0x96] = 0xeb, [0x97] = 0xec, [0x98] = 0xc1, [0x99] = 0xc6, [0x9a] = 0xcf,
90 [0x9b] = 0xc8, [0x9c] = 0xdd, [0x9d] = 0xda, [0x9e] = 0xd3, [0x9f] = 0xd4,
91 [0xa0] = 0x69, [0xa1] = 0x6e, [0xa2] = 0x67, [0xa3] = 0x60, [0xa4] = 0x75,
92 [0xa5] = 0x72, [0xa6] = 0x7b, [0xa7] = 0x7c, [0xa8] = 0x51, [0xa9] = 0x56,
93 [0xaa] = 0x5f, [0xab] = 0x58, [0xac] = 0x4d, [0xad] = 0x4a, [0xae] = 0x43,
94 [0xaf] = 0x44, [0xb0] = 0x19, [0xb1] = 0x1e, [0xb2] = 0x17, [0xb3] = 0x10,
95 [0xb4] = 0x05, [0xb5] = 0x02, [0xb6] = 0x0b, [0xb7] = 0x0c, [0xb8] = 0x21,
96 [0xb9] = 0x26, [0xba] = 0x2f, [0xbb] = 0x28, [0xbc] = 0x3d, [0xbd] = 0x3a,
97 [0xbe] = 0x33, [0xbf] = 0x34, [0xc0] = 0x4e, [0xc1] = 0x49, [0xc2] = 0x40,
98 [0xc3] = 0x47, [0xc4] = 0x52, [0xc5] = 0x55, [0xc6] = 0x5c, [0xc7] = 0x5b,
99 [0xc8] = 0x76, [0xc9] = 0x71, [0xca] = 0x78, [0xcb] = 0x7f, [0xcc] = 0x6a,
100 [0xcd] = 0x6d, [0xce] = 0x64, [0xcf] = 0x63, [0xd0] = 0x3e, [0xd1] = 0x39,
101 [0xd2] = 0x30, [0xd3] = 0x37, [0xd4] = 0x22, [0xd5] = 0x25, [0xd6] = 0x2c,
102 [0xd7] = 0x2b, [0xd8] = 0x06, [0xd9] = 0x01, [0xda] = 0x08, [0xdb] = 0x0f,
103 [0xdc] = 0x1a, [0xdd] = 0x1d, [0xde] = 0x14, [0xdf] = 0x13, [0xe0] = 0xae,
104 [0xe1] = 0xa9, [0xe2] = 0xa0, [0xe3] = 0xa7, [0xe4] = 0xb2, [0xe5] = 0xb5,
105 [0xe6] = 0xbc, [0xe7] = 0xbb, [0xe8] = 0x96, [0xe9] = 0x91, [0xea] = 0x98,
106 [0xeb] = 0x9f, [0xec] = 0x8a, [0xed] = 0x8d, [0xee] = 0x84, [0xef] = 0x83,
107 [0xf0] = 0xde, [0xf1] = 0xd9, [0xf2] = 0xd0, [0xf3] = 0xd7, [0xf4] = 0xc2,
108 [0xf5] = 0xc5, [0xf6] = 0xcc, [0xf7] = 0xcb, [0xf8] = 0xe6, [0xf9] = 0xe1,
109 [0xfa] = 0xe8, [0xfb] = 0xef, [0xfc] = 0xfa, [0xfd] = 0xfd, [0xfe] = 0xf4,
113 static dif_pinmux_t pinmux;
114 static dif_rv_plic_t plic;
115 static dif_i2c_t i2c;
118 volatile uint8_t kSmbusUdid[16] = {0};
130 volatile uint32_t kMessageAddr = 0;
133 volatile uint32_t kTestInitDone = 0;
135 typedef enum i2c_state {
140 kI2cStateArpGetUdidWait,
142 kI2cStateArpGetUdidTransmit,
144 kI2cStateArpCheckUdid,
149 kI2cStateMessageWait,
154 kI2cStateMessageReceive,
156 kI2cStateMessageTransmit,
158 kI2cStateMessageFinish,
160 kI2cStateAssignAddressComplete,
162 kI2cStateTransactionNack,
166 static uint8_t running_pec;
169 static bool arp_av =
false;
170 static bool arp_ar =
false;
173 static uint8_t arp_cmd_pending = 0;
188 static volatile dif_i2c_irq_enable_snapshot_t i2c_irq_enables;
192 static volatile bool i2c_event =
false;
195 static i2c_state_t state;
201 static uint8_t message[32];
207 void i2c_isr(dif_i2c_irq_t irq) {
209 case kDifI2cIrqFmtThreshold:
210 case kDifI2cIrqRxThreshold:
211 case kDifI2cIrqTxThreshold:
212 case kDifI2cIrqRxOverflow:
213 case kDifI2cIrqControllerHalt:
214 case kDifI2cIrqSclInterference:
215 case kDifI2cIrqSdaInterference:
216 case kDifI2cIrqStretchTimeout:
217 case kDifI2cIrqSdaUnstable:
218 case kDifI2cIrqUnexpStop:
219 case kDifI2cIrqHostTimeout:
220 CHECK(
false,
"Unexpected I2C IRQ %d", irq);
222 case kDifI2cIrqAcqThreshold:
223 case kDifI2cIrqCmdComplete:
224 case kDifI2cIrqTxStretch:
225 case kDifI2cIrqAcqStretch:
229 CHECK(
false,
"invalid interrupt %d", irq);
231 dif_i2c_irq_enable_snapshot_t enables;
232 CHECK_DIF_OK(dif_i2c_irq_disable_all(&i2c, &enables));
233 i2c_irq_enables = enables;
239 void ottf_external_isr(uint32_t *exc_info) {
247 switch (peripheral) {
249 if (!ottf_console_flow_control_isr(exc_info)) {
270 CHECK(
false,
"Unexpected external IRQ %d", plic_irq_id);
275 while (!kTestInitDone) {
276 test_command_t command;
277 TRY(UJSON_WITH_CRC(ujson_deserialize_test_command_t, uj, &command));
279 if (status_err(
status) == kUnimplemented) {
281 }
else if (status_err(
status) != kOk) {
291 TRY(dif_pinmux_init(base_addr, &pinmux));
294 TRY(dif_rv_plic_init(base_addr, &plic));
297 TRY(dif_i2c_init(base_addr, &i2c));
299 TRY(i2c_testutils_select_pinmux(&pinmux, 0, I2cPinmuxPlatformIdHyper310));
323 I2C_PARAM_ACQ_FIFO_DEPTH));
330 for (dif_i2c_irq_t irq = kDifI2cIrqFmtThreshold; irq <= kDifI2cIrqHostTimeout;
334 rv_plic_testutils_irq_range_enable(&plic, kPlicTarget,
339 memset(message, 0,
sizeof(message));
342 irq_global_ctrl(
true);
343 irq_external_ctrl(
true);
345 ujson_t uj = ujson_ottf_console();
346 TRY(command_processor(&uj));
353 static uint8_t calc_pec(uint8_t remainder, uint8_t next) {
354 return pec_lut[remainder ^ next];
367 static status_t process_new_command(dif_i2c_irq_state_snapshot_t irq_snapshot,
379 TRY_CHECK((data & 0x1) == 0,
"Expected a write command, but got read");
382 if ((data >> 1) == kSmbusArpId.
address) {
386 if (command == kArpCmdPrepareToArp) {
387 arp_cmd_pending = command;
388 state = kI2cStateArpPec;
390 }
else if (command == kArpCmdResetDevice) {
391 arp_cmd_pending = command;
392 state = kI2cStateArpPec;
394 }
else if (command == kArpCmdGetUdid) {
397 state = kI2cStateTransactionNack;
401 arp_cmd_pending = command;
402 state = kI2cStateArpGetUdidWait;
405 }
else if (command == kArpCmdAssignAddress) {
410 arp_cmd_pending = command;
411 state = kI2cStateArpCheckUdid;
413 }
else if (command == ((i2c_id.
address < 1) | 0)) {
416 arp_cmd_pending = kArpCmdResetDevice;
417 state = kI2cStateArpPec;
421 state = kI2cStateTransactionNack;
424 }
else if (command == ((i2c_id.
address < 1) | 1)) {
428 arp_cmd_pending = kArpCmdGetUdid;
429 state = kI2cStateArpGetUdidWait;
433 state = kI2cStateTransactionNack;
437 state = kI2cStateTransactionNack;
440 }
else if (arp_av && ((data >> 1) == i2c_id.
address)) {
443 if (command == (kMessageAddr & 0xffu)) {
446 state = kI2cStateMessageWait;
450 state = kI2cStateTransactionNack;
454 TRY_CHECK(
false,
"Unexpected 7-bit addr match: 0x%x", (data >> 1));
457 TRY_CHECK(
false,
"Unexpected INTR_STATE=0x%x", irq_snapshot);
481 static status_t check_arp_pec(dif_i2c_irq_state_snapshot_t irq_snapshot,
483 switch (arp_cmd_pending) {
484 case kArpCmdPrepareToArp:
492 TRY_CHECK(acq_fifo_level >= 3);
499 TRY_CHECK(data == arp_cmd_pending);
500 running_pec = calc_pec(running_pec, data);
505 running_pec = calc_pec(running_pec, data);
511 TRY(dif_i2c_irq_acknowledge(&i2c, kDifI2cIrqCmdComplete));
513 state = kI2cStateIdle;
514 TRY_CHECK(running_pec == 0,
"PEC failure");
517 case kArpCmdResetDevice: {
525 TRY_CHECK(acq_fifo_level >= 3);
532 TRY_CHECK(data == arp_cmd_pending);
533 running_pec = calc_pec(running_pec, data);
538 running_pec = calc_pec(running_pec, data);
544 TRY(dif_i2c_irq_acknowledge(&i2c, kDifI2cIrqCmdComplete));
546 state = kI2cStateIdle;
547 TRY_CHECK(running_pec == 0,
"PEC failure");
551 case kArpCmdAssignAddress: {
564 TRY_CHECK(acq_fifo_level >= 2);
571 TRY_CHECK(data == kSmbusUdid[15]);
572 running_pec = calc_pec(running_pec, data);
575 uint8_t new_address = 0;
578 running_pec = calc_pec(running_pec, new_address);
582 TRY_CHECK(running_pec == data,
"PEC failure");
584 i2c_id.
address = new_address >> 1;
588 state = kI2cStateAssignAddressComplete;
593 TRY_CHECK(
false,
"Found unexpected arp_cmd_pending 0x%x",
617 static status_t prepare_udid_reply(dif_i2c_irq_state_snapshot_t irq_snapshot,
619 TRY_CHECK(arp_cmd_pending == kArpCmdGetUdid);
631 TRY_CHECK((data == ((i2c_id.
address << 1) | 1)) ||
632 (data == kArpCmdGetUdid));
633 TRY(dif_i2c_irq_acknowledge(&i2c, kDifI2cIrqCmdComplete));
641 TRY_CHECK(tx_stretch);
656 TRY_CHECK(data == ((kSmbusArpId.
address << 1) | 1));
660 uint8_t pec = calc_pec(0,
sizeof(kSmbusUdid) + 1);
663 for (
size_t i = 0; i <
sizeof(kSmbusUdid); ++i) {
665 pec = calc_pec(pec, kSmbusUdid[i]);
669 uint8_t current_address = 0xff;
671 current_address = (uint8_t)((i2c_id.
address << 1) | 1);
674 pec = calc_pec(pec, current_address);
678 state = kI2cStateArpGetUdidTransmit;
686 static status_t complete_get_udid(dif_i2c_irq_state_snapshot_t irq_snapshot,
688 TRY_CHECK(arp_cmd_pending == kArpCmdGetUdid);
707 TRY(dif_i2c_irq_acknowledge(&i2c, kDifI2cIrqCmdComplete));
709 state = kI2cStateIdle;
725 static status_t check_received_udid(dif_i2c_irq_state_snapshot_t irq_snapshot,
727 TRY_CHECK(arp_cmd_pending == kArpCmdAssignAddress);
735 TRY_CHECK(acq_fifo_level >= 17);
742 TRY_CHECK(data == arp_cmd_pending);
743 running_pec = calc_pec(running_pec, data);
748 TRY_CHECK(data == 17);
749 running_pec = calc_pec(running_pec, data);
752 bool udid_match =
true;
753 for (
size_t i = 0; i < 15; ++i) {
756 if (data != kSmbusUdid[i]) {
759 running_pec = calc_pec(running_pec, data);
764 if (data != kSmbusUdid[15]) {
770 state = kI2cStateArpPec;
774 state = kI2cStateTransactionNack;
776 LOG_INFO(
"AssignAddress: UDID mismatch");
787 static status_t finish_assign_addr(dif_i2c_irq_state_snapshot_t irq_snapshot,
801 TRY(dif_i2c_irq_acknowledge(&i2c, kDifI2cIrqCmdComplete));
803 state = kI2cStateIdle;
814 static status_t finish_nack(dif_i2c_irq_state_snapshot_t irq_snapshot,
816 bool saw_stop =
false;
818 TRY(dif_i2c_irq_acknowledge(&i2c, kDifI2cIrqCmdComplete));
825 for (
size_t i = 0; i < acq_fifo_level; ++i) {
841 I2C_PARAM_ACQ_FIFO_DEPTH));
845 state = kI2cStateIdle;
865 static status_t message_cmd_dispatch(dif_i2c_irq_state_snapshot_t irq_snapshot,
869 const uint8_t message_addr = (uint8_t)kMessageAddr;
878 TRY_CHECK(data == message_addr);
880 if (acq_stretch && !cmd_complete) {
882 state = kI2cStateMessageReceive;
884 }
else if (cmd_complete) {
889 TRY(dif_i2c_irq_acknowledge(&i2c, kDifI2cIrqCmdComplete));
890 state = kI2cStateMessageTransmit;
892 TRY_CHECK(
false,
"Unknown state of message command");
904 static status_t message_receive(dif_i2c_irq_state_snapshot_t irq_snapshot,
914 TRY_CHECK(acq_fifo_level >=
sizeof(message) + 1);
918 for (
size_t i = 0; i <
sizeof(message); ++i) {
927 TRY(dif_i2c_irq_acknowledge(&i2c, kDifI2cIrqCmdComplete));
928 state = kI2cStateIdle;
936 static status_t message_transmit(dif_i2c_irq_state_snapshot_t irq_snapshot,
947 TRY_CHECK(data == ((i2c_id.
address << 1) | 1));
957 for (
size_t i = 0; i <
sizeof(message); ++i) {
961 state = kI2cStateMessageFinish;
969 static status_t message_finish(dif_i2c_irq_state_snapshot_t irq_snapshot,
980 TRY(dif_i2c_irq_acknowledge(&i2c, kDifI2cIrqCmdComplete));
981 state = kI2cStateIdle;
985 static status_t smbus_arp_test(
void) {
986 bool message_received =
false;
987 state = kI2cStateIdle;
988 test_status_set(kTestStatusInWfi);
989 while (!message_received) {
990 ATOMIC_WAIT_FOR_INTERRUPT(i2c_event ==
true);
993 dif_i2c_irq_state_snapshot_t irq_snapshot = {0};
994 CHECK_DIF_OK(dif_i2c_irq_get_state(&i2c, &irq_snapshot));
1001 TRY(process_new_command(irq_snapshot, i2c_status));
1003 case kI2cStateArpPec:
1004 TRY(check_arp_pec(irq_snapshot, i2c_status));
1006 case kI2cStateArpGetUdidWait:
1007 TRY(prepare_udid_reply(irq_snapshot, i2c_status));
1009 case kI2cStateArpGetUdidTransmit:
1010 TRY(complete_get_udid(irq_snapshot, i2c_status));
1012 case kI2cStateArpCheckUdid:
1013 TRY(check_received_udid(irq_snapshot, i2c_status));
1015 case kI2cStateMessageWait:
1016 TRY(message_cmd_dispatch(irq_snapshot, i2c_status));
1018 case kI2cStateMessageReceive:
1019 TRY(message_receive(irq_snapshot, i2c_status));
1021 case kI2cStateMessageTransmit:
1022 TRY(message_transmit(irq_snapshot, i2c_status));
1024 case kI2cStateMessageFinish:
1025 TRY(message_finish(irq_snapshot, i2c_status));
1026 message_received =
true;
1028 case kI2cStateAssignAddressComplete:
1029 TRY(finish_assign_addr(irq_snapshot, i2c_status));
1031 case kI2cStateTransactionNack:
1032 TRY(finish_nack(irq_snapshot, i2c_status));
1035 TRY_CHECK(
false,
"Reached invalid state %d", state);
1038 dif_i2c_irq_enable_snapshot_t enables = i2c_irq_enables;
1039 CHECK_DIF_OK(dif_i2c_irq_restore_all(&i2c, &enables));
1045 CHECK_STATUS_OK(test_init());
1048 return status_ok(result);