-
Notifications
You must be signed in to change notification settings - Fork 8
/
omap_WDT.c
executable file
·144 lines (114 loc) · 2.94 KB
/
omap_WDT.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//(c) uARM project https://github.com/uARM-Palm/uARM [email protected]
#include "omap_WDT.h"
#include "omap_IC.h"
#include <string.h>
#include <stdlib.h>
#include "util.h"
#include "mem.h"
#define OMAP_WDT_BASE 0xFFFEC800UL
#define OMAP_WDT_SIZE 12
struct OmapWdt {
struct SocIc *ic;
uint16_t ctrl, counter, reloadVal;
uint8_t prescaleCtr;
bool wdtMode, hadDisableFirstHalf;
};
static bool omapWdtPrvMemAccessF(void* userData, uint32_t pa, uint_fast8_t size, bool write, void* buf)
{
struct OmapWdt *wdt = (struct OmapWdt*)userData;
uint_fast16_t val = 0;
if (size != 2 || (pa & 3)) {
fprintf(stderr, "%s: Unexpected %s of %u bytes to 0x%08lx\n", __func__, write ? "write" : "read", size, (unsigned long)pa);
return false;
}
pa = (pa - OMAP_WDT_BASE) >> 2;
if (write)
val = *(uint16_t*)buf;
switch (pa) {
case 0x00 / 4:
if (write) {
if (wdt->wdtMode)
val = val | 0xe00;
if (!(wdt->ctrl & 0x80) && (val & 0x80)) //starting
wdt->counter = wdt->reloadVal;
wdt->ctrl = val;
}
else
val = wdt->ctrl;
break;
case 0x04 / 4:
if (write) {
if (wdt->wdtMode)
wdt->counter = val;
else
wdt->reloadVal = val;
}
else
val = wdt->counter;
break;
case 0x08 / 4:
if (write) {
if (val & 0x8000) {
wdt->ctrl |= 0x0e00;
wdt->wdtMode = true;
wdt->counter = 0xffff;
wdt->reloadVal = 0xffff;
}
else if (wdt->hadDisableFirstHalf) {
if ((val & 0xff) == 0xA0)
wdt->wdtMode = false;
else
ERR("improper WDT disablement\n");
wdt->hadDisableFirstHalf = false;
}
else if ((val & 0xff) == 0xf5)
wdt->hadDisableFirstHalf = true;
}
else
val = wdt->wdtMode ? 0x8000 : 0x0000;
break;
default:
return false;
}
if (!write)
*(uint16_t*)buf = val;
return true;
}
struct OmapWdt* omapWdtInit(struct ArmMem *physMem, struct SocIc *ic)
{
struct OmapWdt *wdt = (struct OmapWdt*)malloc(sizeof(*wdt));
if (!wdt)
ERR("cannot alloc WDT");
memset(wdt, 0, sizeof (*wdt));
wdt->ic = ic;
wdt->ctrl = 0x0002;
wdt->counter = 0xffff;
wdt->reloadVal = 0xffff;
wdt->wdtMode = true;
if (!memRegionAdd(physMem, OMAP_WDT_BASE, OMAP_WDT_SIZE, omapWdtPrvMemAccessF, wdt))
ERR("cannot add WDT to MEM\n");
return wdt;
}
void omapWdtPeriodic(struct OmapWdt *wdt)
{
if (!wdt->wdtMode && !(wdt->ctrl & 0x0080)) //non-WDT mode timer can be stopped
return;
if (++wdt->prescaleCtr >= (2 << ((wdt->ctrl >> 9) & 7))) {
wdt->prescaleCtr = 0;
if (wdt->counter)
wdt->counter--;
else {
if (wdt->wdtMode)
ERR("WDT reset\n");
else {
if (wdt->ctrl & 0x0100)
wdt->counter = wdt->reloadVal;
else
wdt->ctrl &=~ 0x0080;
//edge triggered
socIcInt(wdt->ic, OMAP_I_WDT, true);
socIcInt(wdt->ic, OMAP_I_WDT, false);
}
}
}
}