-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdelay.c
59 lines (46 loc) · 1.17 KB
/
delay.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
* delay.c
*
* Created on: 14/12/2021
* Author: Paulo Almeida
*/
#include "kernel/time/delay.h"
#include "kernel/time/jiffies.h"
#include "kernel/lib/printk.h"
unsigned long loops_per_jiffy = (1 << 16);
static void __delay(unsigned long loops_num) {
asm volatile(
".loop: \n"
" test %[loop_idx], %[loop_idx] \n"
" jz .done \n"
" dec %[loop_idx] \n"
" jmp .loop \n"
".done: \n"
:
: [loop_idx] "r" (loops_num)
:
);
}
void calibrate_delay(void) {
unsigned long ticks;
while ((loops_per_jiffy <<= 1) != 0) {
ticks = jiffies;
/* wait for "start of" clock tick */
while (ticks == jiffies)
/* nothing */;
/* Go .. */
ticks = jiffies;
__delay(loops_per_jiffy);
ticks = jiffies - ticks;
if (ticks)
break;
}
}
void ndelay(unsigned long nsecs) {
unsigned long loops = (nsecs * HZ * loops_per_jiffy) / 1000000000;
__delay(loops);
}
void udelay(unsigned long usecs) {
unsigned long loops = (usecs * HZ * loops_per_jiffy) / 1000000;
__delay(loops);
}