-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathpsp-dev-flash.c
234 lines (190 loc) · 7.6 KB
/
psp-dev-flash.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/** @file
* PSP Emulator - Flash ROM device attached to SMN.
*/
/*
* 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 <string.h>
#include <stdlib.h>
#include <common/cdefs.h>
#include <os/time.h>
#include <psp-devs.h>
/**
* Flash device instance data.
*/
typedef struct PSPDEVFLASH
{
/** Device instance pointer. */
PPSPDEV pDev;
/** SMN region handle. */
PSPIOMREGIONHANDLE hSmn;
/** SMN region handle to the control interface living at 0x2dc4000. */
PSPIOMREGIONHANDLE hSmnCtrl;
/** SMN region handle to the bank control interface living at 0x2dc405c for Zen2+ systems. */
PSPIOMREGIONHANDLE hSmnBankCtrl;
/** The currently selected flash bank (16MB chunks). */
uint32_t uBank;
/** SPI flash trace file if configured. */
FILE *pSpiFlashTrace;
/** Start timestamp. */
uint64_t tsStart;
/** Last accessed offset. */
SMNADDR offAccLast;
/** Packet ID. */
uint64_t idPacket;
} PSPDEVFLASH;
/** Pointer to the device instance data. */
typedef PSPDEVFLASH *PPSPDEVFLASH;
static uint32_t pspDevFlashGetBankedOffset(PPSPDEVFLASH pThis, SMNADDR offSmn)
{
if (pThis->uBank * 16 * _1M < pThis->pDev->pCfg->cbFlashRom)
return offSmn + pThis->uBank * 16 * _1M;
return offSmn;
}
static void *pspDevFlashGetBankedStart(PPSPDEVFLASH pThis, SMNADDR offSmn)
{
return (uint8_t *)pThis->pDev->pCfg->pvFlashRom + pspDevFlashGetBankedOffset(pThis, offSmn);
}
static void pspDevFlashRead(SMNADDR offSmn, size_t cbRead, void *pvDst, void *pvUser)
{
PPSPDEVFLASH pThis = (PPSPDEVFLASH)pvUser;
const uint8_t *pbFlash = (const uint8_t *)pspDevFlashGetBankedStart(pThis, offSmn);
if (offSmn + cbRead <= 16 * _1M)
memcpy(pvDst, pbFlash, cbRead);
else
printf("%s: ATTEMPTED out of bounds read from offSmn=%#x cbRead=%zu -> IGNORED\n", __FUNCTION__, offSmn, cbRead);
/* Log the access if enabled. */
if (pThis->pSpiFlashTrace)
{
/* Generate a new packet ID and read command if the last access isn't adjacent to this one. */
uint32_t offFlash = pspDevFlashGetBankedOffset(pThis, offSmn);
uint64_t tsCmd = OSTimeTsGetNano() - pThis->tsStart;
uint64_t tsCmdSec = tsCmd / (1000 * 1000 * 1000);
uint64_t tsCmdNs = tsCmd % (1000 * 1000 * 1000);
if (offSmn != pThis->offAccLast)
{
pThis->idPacket++;
int cchWritten = fprintf(pThis->pSpiFlashTrace, "%llu.%llu000000,%llu,0x03,0xFF\n",
tsCmdSec, tsCmdNs++, pThis->idPacket);
cchWritten = fprintf(pThis->pSpiFlashTrace, "%llu.%llu000000,%llu,0x%02x,0xFF\n",
tsCmdSec, tsCmdNs++, pThis->idPacket, (offFlash >> 16) & 0xff);
cchWritten = fprintf(pThis->pSpiFlashTrace, "%llu.%llu000000,%llu,0x%02x,0xFF\n",
tsCmdSec, tsCmdNs++, pThis->idPacket, (offFlash >> 8) & 0xff);
cchWritten = fprintf(pThis->pSpiFlashTrace, "%llu.%llu000000,%llu,0x%02x,0xFF\n",
tsCmdSec, tsCmdNs++, pThis->idPacket, offFlash & 0xff);
}
pThis->offAccLast = offSmn + cbRead;
while (cbRead)
{
int cchWritten = fprintf(pThis->pSpiFlashTrace, "%llu.%llu000000,%llu,0x00,0x%02x\n",
tsCmdSec, tsCmdNs++, pThis->idPacket, *pbFlash);
pbFlash++;
cbRead--;
}
fflush(pThis->pSpiFlashTrace);
}
}
static void pspDevFlashWrite(SMNADDR offSmn, size_t cbWrite, const void *pvVal, void *pvUser)
{
PPSPDEVFLASH pThis = (PPSPDEVFLASH)pvUser;
if (offSmn + cbWrite <= 16 * _1M)
memcpy(pspDevFlashGetBankedStart(pThis, offSmn), pvVal, cbWrite);
else
printf("%s: ATTEMPTED out of bounds write from offSmn=%#x cbWrite=%zu -> IGNORED\n", __FUNCTION__, offSmn, cbWrite);
}
static void pspDevFlashSpiCtrlRead(SMNADDR offSmn, size_t cbRead, void *pvDst, void *pvUser)
{
PPSPDEVFLASH pThis = (PPSPDEVFLASH)pvUser;
printf("%s: ATTEMPTED read from offSmn=%#x cbRead=%zu -> return all 0\n", __FUNCTION__, offSmn, cbRead);
memset(pvDst, 0, cbRead);
}
static void pspDevFlashSpiCtrlWrite(SMNADDR offSmn, size_t cbWrite, const void *pvVal, void *pvUser)
{
PPSPDEVFLASH pThis = (PPSPDEVFLASH)pvUser;
printf("%s: ATTEMPTED write access to offSmn=%#x cbWrite=%zu -> IGNORED\n", __FUNCTION__, offSmn, cbWrite);
}
static void pspDevFlashSpiBankCtrlRead(SMNADDR offSmn, size_t cbRead, void *pvDst, void *pvUser)
{
PPSPDEVFLASH pThis = (PPSPDEVFLASH)pvUser;
*(uint32_t *)pvDst = pThis->uBank;
}
static void pspDevFlashSpiBankCtrlWrite(SMNADDR offSmn, size_t cbWrite, const void *pvVal, void *pvUser)
{
PPSPDEVFLASH pThis = (PPSPDEVFLASH)pvUser;
pThis->uBank = *(uint32_t *)pvVal & 0xff;
}
static int pspDevFlashInit(PPSPDEV pDev)
{
PPSPDEVFLASH pThis = (PPSPDEVFLASH)&pDev->abInstance[0];
pThis->pDev = pDev;
pThis->uBank = 0;
pThis->offAccLast = UINT32_MAX - 1;
pThis->idPacket = 0;
SMNADDR SmnAddrFlash = pDev->pCfg->pPspProfile->SmnAddrFlashStart;
int rc = PSPEmuIoMgrSmnRegister(pDev->hIoMgr, SmnAddrFlash, 16 * _1M,
pspDevFlashRead, pspDevFlashWrite, pThis,
"SPI flash", &pThis->hSmn);
if (!rc)
rc = PSPEmuIoMgrSmnRegister(pDev->hIoMgr, 0x2dc4000, 0x20,
pspDevFlashSpiCtrlRead, pspDevFlashSpiCtrlWrite, pThis,
"SPI Control", &pThis->hSmnCtrl);
if ( !rc
&& pDev->pCfg->pPspProfile->enmMicroArch >= PSPEMUMICROARCH_ZEN2)
rc = PSPEmuIoMgrSmnRegister(pDev->hIoMgr, 0x2dc405c, 4,
pspDevFlashSpiBankCtrlRead, pspDevFlashSpiBankCtrlWrite, pThis,
"SPI Bank Ctrl", &pThis->hSmnBankCtrl);
if ( !rc
&& pDev->pCfg->pszSpiFlashTrace)
{
pThis->pSpiFlashTrace = fopen(pDev->pCfg->pszSpiFlashTrace, "wb");
if (pThis->pSpiFlashTrace)
{
/* Write the header. */
const char szHdr[] = "Time [s],Packet ID,MOSI,MISO\n";
size_t cWritten = fwrite(&szHdr[0], sizeof(szHdr) - 1, 1, pThis->pSpiFlashTrace);
if (cWritten != 1)
rc = -1;
pThis->tsStart = OSTimeTsGetNano();
}
else
rc = -1;
}
return rc;
}
static void pspDevFlashDestruct(PPSPDEV pDev)
{
PPSPDEVFLASH pThis = (PPSPDEVFLASH)&pDev->abInstance[0];
if (pThis->pSpiFlashTrace)
fclose(pThis->pSpiFlashTrace);
}
/**
* Device registration structure.
*/
const PSPDEVREG g_DevRegFlash =
{
/** pszName */
"flash",
/** pszDesc */
"Flash device",
/** cbInstance */
sizeof(PSPDEVFLASH),
/** pfnInit */
pspDevFlashInit,
/** pfnDestruct */
pspDevFlashDestruct,
/** pfnReset */
NULL
};