-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathpsp-dev-timer.c
204 lines (180 loc) · 5.41 KB
/
psp-dev-timer.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/** @file
* PSP Emulator - Timer device starting at 0x03010400 and 0x03010424.
*/
/*
* Copyright (C) 2020 Alexander Eichner <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <common/cdefs.h>
#include <os/time.h>
#include <psp-devs.h>
/**
* Timer device instance data.
*/
typedef struct PSPDEVTIMER
{
/** Flag whether to run in realtime. */
bool fRealtime;
/** Last nanosecond timestamp. */
uint64_t tsLast;
/** The control register perhaps. */
uint32_t regCtrl;
/** The counter running at 100MHz. */
uint32_t regCnt100MHz;
/** MMIO region handle. */
PSPIOMREGIONHANDLE hMmio;
} PSPDEVTIMER;
/** Pointer to the device instance data. */
typedef PSPDEVTIMER *PPSPDEVTIMER;
static void pspDevTimerMmioRead(PSPADDR offMmio, size_t cbRead, void *pvVal, void *pvUser)
{
PPSPDEVTIMER pThis = (PPSPDEVTIMER)pvUser;
if (cbRead != sizeof(uint32_t))
{
printf("%s: offMmio=%#x cbRead=%zu -> Unsupported access width\n", __FUNCTION__, offMmio, cbRead);
return;
}
uint32_t *pu32Ret = (uint32_t *)pvVal;
switch (offMmio)
{
case 0: /* Control register */
{
*pu32Ret = pThis->regCtrl;
break;
}
case 32: /* 100MHz counter. */
{
if (!pThis->fRealtime)
{
*pu32Ret = pThis->regCnt100MHz;
if (pThis->regCtrl & 0x1)
pThis->regCnt100MHz += 1000 * 1000 * 100; /* 1 second jump per read. */
}
else
{
if (pThis->regCtrl & 0x1)
{
uint64_t tsNow = OSTimeTsGetNano();
uint64_t tsElapsed = tsNow - pThis->tsLast;
pThis->regCnt100MHz += (uint32_t)(tsElapsed / 10); /* 10ns intervals. */
*pu32Ret = pThis->regCnt100MHz;
pThis->tsLast = tsNow;
}
}
break;
}
default:
/* Ignore for now. */
break;
}
}
static void pspDevTimerMmioWrite(PSPADDR offMmio, size_t cbWrite, const void *pvVal, void *pvUser)
{
PPSPDEVTIMER pThis = (PPSPDEVTIMER)pvUser;
if ( cbWrite != sizeof(uint32_t)
&& cbWrite != sizeof(uint8_t))
{
printf("%s: offMmio=%#x cbWrite=%zu -> Unsupported access width\n", __FUNCTION__, offMmio, cbWrite);
return;
}
uint32_t u32Val = 0;
if (cbWrite == sizeof(uint32_t))
u32Val = *(uint32_t *)pvVal;
else
u32Val = *(uint8_t *)pvVal;
switch (offMmio)
{
case 0: /* Control register */
{
/* Read the time once if in realtime mode and the timer got enabled. */
if ( pThis->fRealtime
&& (u32Val & 0x1)
&& !(pThis->regCtrl & 0x1))
pThis->tsLast = OSTimeTsGetNano();
pThis->regCtrl = u32Val;
break;
}
case 1: /* XXX For single byte access by the on chip bl... */
{
pThis->regCtrl |= u32Val << 8;
break;
}
case 32: /* 100MHz counter. */
{
pThis->regCnt100MHz = u32Val;
break;
}
default:
/* Ignore for now. */
break;
}
}
static int pspDevTimerInit(PPSPDEV pDev)
{
PPSPDEVTIMER pThis = (PPSPDEVTIMER)&pDev->abInstance[0];
pThis->fRealtime = pDev->pCfg->fTimerRealtime;
pThis->tsLast = OSTimeTsGetNano();
pThis->regCtrl = 0;
pThis->regCnt100MHz = 0;
/* Register MMIO ranges. */
int rc = PSPEmuIoMgrMmioRegister(pDev->hIoMgr,
pDev->pReg == &g_DevRegTimer1
? 0x03010400
: 0x03010424,
sizeof(uint32_t) * 9,
pspDevTimerMmioRead, pspDevTimerMmioWrite, pThis,
pDev->pReg->pszName, &pThis->hMmio);
return rc;
}
static void pspDevTimerDestruct(PPSPDEV pDev)
{
/* Nothing to do so far. */
}
/**
* Device registration structure.
*/
const PSPDEVREG g_DevRegTimer1 =
{
/** pszName */
"timer1",
/** pszDesc */
"Timer device starting at 0x03010400",
/** cbInstance */
sizeof(PSPDEVTIMER),
/** pfnInit */
pspDevTimerInit,
/** pfnDestruct */
pspDevTimerDestruct,
/** pfnReset */
NULL
};
/**
* Device registration structure.
*/
const PSPDEVREG g_DevRegTimer2 =
{
/** pszName */
"timer2",
/** pszDesc */
"Timer device starting at 0x03010424",
/** cbInstance */
sizeof(PSPDEVTIMER),
/** pfnInit */
pspDevTimerInit,
/** pfnDestruct */
pspDevTimerDestruct,
/** pfnReset */
NULL
};