Software APIs
pwrmgr_usbdev_smoketest.c
1 // Copyright lowRISC contributors (OpenTitan project).
2 // Licensed under the Apache License, Version 2.0, see LICENSE for details.
3 // SPDX-License-Identifier: Apache-2.0
4 //
5 // This is a DV specific pwrmgr usbdev smoketest that fakes usb activity.
6 // Using the phy drive function of usbdev, we present the usbdev is in suspend
7 // and thus trigger low power entry.
8 // Once the system enters low power entry, the incoming USBP/USBN lines are
9 // immediately different from what the wake module expects and thus causes
10 // a wakeup.
11 // This test just smoke checks the pwrmgr's ability to enter / exit low power
12 // and the usbdev's aon wake function that has been de-coupled from the main
13 // usbdev.
14 
20 #include "sw/device/lib/testing/pwrmgr_testutils.h"
21 #include "sw/device/lib/testing/test_framework/check.h"
23 
25 
26 static dif_pwrmgr_t pwrmgr;
27 static dif_usbdev_t usbdev;
28 
29 OTTF_DEFINE_TEST_CONFIG();
30 
31 static bool compare_wakeup_reasons(dif_pwrmgr_wakeup_reason_t lhs,
33  return lhs.types == rhs.types && lhs.request_sources == rhs.request_sources;
34 }
35 
36 bool test_main(void) {
37  CHECK_DIF_OK(dif_pwrmgr_init(
39  CHECK_DIF_OK(dif_usbdev_init(
41 
42  // Assuming the chip hasn't slept yet, wakeup reason should be empty.
43  dif_pwrmgr_wakeup_reason_t wakeup_reason;
44  CHECK_DIF_OK(dif_pwrmgr_wakeup_reason_get(&pwrmgr, &wakeup_reason));
45 
46  const dif_pwrmgr_wakeup_reason_t exp_por_wakeup_reason = {
47  .types = 0,
48  .request_sources = 0,
49  };
50 
51  const dif_pwrmgr_wakeup_reason_t exp_test_wakeup_reason = {
53  .request_sources = kDifPwrmgrWakeupRequestSourceFour,
54  };
55 
56  bool low_power_exit = false;
57  if (compare_wakeup_reasons(wakeup_reason, exp_por_wakeup_reason)) {
58  LOG_INFO("Powered up for the first time, begin test");
59  } else if (compare_wakeup_reasons(wakeup_reason, exp_test_wakeup_reason)) {
60  low_power_exit = true;
61  LOG_INFO("USB wakeup detected");
62  } else {
63  LOG_ERROR("Unexpected wakeup reason (types: %02x, sources: %08x)",
64  wakeup_reason.types, wakeup_reason.request_sources);
65  return false;
66  }
67 
68  // Fake low power entry through usb
69  // Force wake detection module active
70  if (!low_power_exit) {
71  CHECK_DIF_OK(dif_usbdev_set_wake_enable(&usbdev, kDifToggleDisabled));
73  .dp_pullup_en = true,
74  .dn_pullup_en = false,
75  };
76  CHECK_DIF_OK(
78  CHECK_DIF_OK(dif_usbdev_set_wake_enable(&usbdev, kDifToggleEnabled));
79 
80  // give the hardware a chance to recognize the wakeup values are the same
81  busy_spin_micros(20); // 20us
82 
83  // Enable low power on the next WFI with default settings.
84  CHECK_STATUS_OK(pwrmgr_testutils_enable_low_power(
85  &pwrmgr, kDifPwrmgrWakeupRequestSourceFour,
86  kDifPwrmgrDomainOptionUsbClockInActivePower));
87 
88  // Enter low power mode.
90  }
91 
92  return true;
93 }