1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright 2022 Oxide Computer Company
14  */
15 
16 #include "payload_common.h"
17 #include "payload_utils.h"
18 #include "test_defs.h"
19 
20 
21 void
timer0_reset(void)22 timer0_reset(void)
23 {
24 	/*
25 	 * Configure timer 0 for interrupt-on-terminal-count mode, and prepare
26 	 * it to be loaded with the high and low bytes.
27 	 */
28 	outb(IOP_ATPIT_CMD, 0x30);
29 
30 	/* Load timer with max value (0xffff) */
31 	outb(IOP_ATPIT_C0, 0xff);
32 	outb(IOP_ATPIT_C0, 0xff);
33 }
34 
35 uint16_t
timer0_read(void)36 timer0_read(void)
37 {
38 	uint16_t val;
39 
40 	/* Latch timer0 */
41 	outb(IOP_ATPIT_CMD, 0x00);
42 
43 	/* Read low and high bytes */
44 	val = inb(IOP_ATPIT_C0);
45 	val |= (uint16_t)inb(IOP_ATPIT_C0) << 8;
46 
47 	return (val);
48 }
49 
50 void
start(void)51 start(void)
52 {
53 
54 	/* loop for as long as the host wants */
55 	for (;;) {
56 		uint16_t start, end;
57 
58 		timer0_reset();
59 
60 		start = timer0_read();
61 		outw(IOP_TEST_VALUE, start);
62 
63 		do {
64 			end = timer0_read();
65 			/* wait for enough ticks to pass */
66 		} while (end > (start - ATPIT_TARGET_TICKS));
67 		outw(IOP_TEST_VALUE, end);
68 	}
69 }
70