blob: 5aec977d4e28484fa0a59197cf759dae916cc625 [file] [log] [blame]
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +02001/*
2 * Copyright (c) 2018, Arm Limited. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <debug.h>
8#include <mmio.h>
9#include <platform_def.h>
10#include <stdint.h>
11#include <stdlib.h>
12
13uintptr_t bound_rand(uintptr_t min, uintptr_t max)
14{
15 /*
16 * This is not ideal as some numbers will never be generated because of
17 * the integer arithmetic rounding.
18 */
19 return ((rand() * (UINT64_MAX/RAND_MAX)) % (max - min)) + min;
20}
21
22/*******************************************************************************
23 * Test framework helpers
24 ******************************************************************************/
25
26void expect(int expr, int expected)
27{
28 if (expr != expected) {
29 ERROR("Expected value %i, got %i\n", expected, expr);
30 while (1)
31 continue;
32 }
33}
34
35void announce_test_section_start(const char *test_sect_desc)
36{
37 INFO("========================================\n");
38 INFO("Starting %s tests\n", test_sect_desc);
39 INFO("========================================\n");
40}
41void announce_test_section_end(const char *test_sect_desc)
42{
43 INFO("========================================\n");
44 INFO("End of %s tests\n", test_sect_desc);
45 INFO("========================================\n");
46}
47
48void announce_test_start(const char *test_desc)
49{
50 INFO("[+] %s\n", test_desc);
51}
52
53void announce_test_end(const char *test_desc)
54{
55 INFO("Test \"%s\" passed.\n", test_desc);
56}
57
Antonio Nino Diaz2ac6f8f2018-07-02 09:04:07 +010058void sp_sleep(uint32_t ms)
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020059{
Antonio Nino Diaz2ac6f8f2018-07-02 09:04:07 +010060 uint64_t timer_freq = mmio_read_32(SYS_CNT_CONTROL_BASE + CNTFID_OFF);
61 VERBOSE("%s: Timer frequency = %llu\n", __func__, timer_freq);
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020062
Antonio Nino Diaz2ac6f8f2018-07-02 09:04:07 +010063 VERBOSE("%s: Sleeping for %u milliseconds...\n", __func__, ms);
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020064 uint64_t time1 = mmio_read_64(SYS_CNT_READ_BASE);
65 volatile uint64_t time2 = time1;
Antonio Nino Diaz2ac6f8f2018-07-02 09:04:07 +010066 while ((time2 - time1) < ((ms * timer_freq) / 1000U)) {
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020067 time2 = mmio_read_64(SYS_CNT_READ_BASE);
68 }
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020069}