-
Notifications
You must be signed in to change notification settings - Fork 14
/
hyperflashdrv.c
341 lines (271 loc) · 10 KB
/
hyperflashdrv.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
* Copyright (c) 2015-2016, Renesas Electronics Corporation
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of Renesas nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "common.h"
#include "rpcqspidrv.h"
#include "bit.h"
#include "rpchyperdrv.h"
#include "hyperflashdrv.h"
#include "dgtable.h"
#include "dgmodul4.h"
uint16_t gHyperFlashIdCfi[0x100];
void ResetHyperFlash(void)
{
//Reset / ASO Exit
WriteCommandHyperFlash(0,HYPER_FL_RESET_COM);
// Address=xxxx
}
uint32_t ReadHyperFlashID(uint32_t *readData)
{
uint32_t rdFlashData[2];
uint32_t addr,i;
//1st
WriteCommandHyperFlash(HYPER_FL_UNLOCK1_ADD, HYPER_FL_UNLOCK1_DATA);
//2nd
WriteCommandHyperFlash(HYPER_FL_UNLOCK2_ADD, HYPER_FL_UNLOCK2_DATA);
//3rd
WriteCommandHyperFlash(HYPER_FL_UNLOCK3_ADD, HYPER_FL_ID_ENTRY_COM);
for(addr=0,i=0;addr<0x10;addr+=8){
ReadHyperFlashData(addr,rdFlashData,8);
gHyperFlashIdCfi[i] = (rdFlashData[0] >> 16); i++;
gHyperFlashIdCfi[i] = (rdFlashData[0] & 0x0000FFFF); i++;
gHyperFlashIdCfi[i] = (rdFlashData[1] >> 16); i++;
gHyperFlashIdCfi[i] = (rdFlashData[1] & 0x0000FFFF); i++;
if(addr==0) *readData = ((rdFlashData[0]&0xFF000000)>>8 | ((rdFlashData[0]&0x00FF0000)<<8) | ((rdFlashData[0]&0x0000FF00)>>8) | ((rdFlashData[0]&0x000000FF)<<8)) ;
}
ResetHyperFlash();
return(0);
}
void WordProgramHyperFlash(uint32_t writeAddr, uint32_t writeData)
{
//Word Program
uint32_t hyperFlashStatus;
uint32_t loopf;
uint32_t status=0;
uint32_t readData[2];
//1st
WriteCommandHyperFlash(HYPER_FL_UNLOCK1_ADD, HYPER_FL_UNLOCK1_DATA);
//2nd
WriteCommandHyperFlash(HYPER_FL_UNLOCK2_ADD, HYPER_FL_UNLOCK2_DATA);
//3rd
WriteCommandHyperFlash(HYPER_FL_UNLOCK3_ADD, HYPER_FL_WORD_PROGRAM_COM);
//4th
WriteDataHyperFlash(writeAddr, writeData);
while(1){
status = ReadStatusHyperFlash(readData);
if( status & BIT7 ) break; //BIT7: Device Ready Bit (0=Busy, 1=Ready)
}
}
void SectorEraseHyperFlash(uint32_t sector_addr)
{
uint32_t hyperFlashStatus;
uint32_t loopf;
uint32_t readData[2];
uint32_t status=0;
//1st
WriteCommandHyperFlash(HYPER_FL_UNLOCK1_ADD, HYPER_FL_UNLOCK1_DATA);
//2nd
WriteCommandHyperFlash(HYPER_FL_UNLOCK2_ADD, HYPER_FL_UNLOCK2_DATA);
//3rd
WriteCommandHyperFlash(HYPER_FL_UNLOCK3_ADD, HYPER_FL_ERASE_1ST_COM);
//4th
WriteCommandHyperFlash(HYPER_FL_UNLOCK1_ADD, HYPER_FL_UNLOCK1_DATA);
//5th
WriteCommandHyperFlash(HYPER_FL_UNLOCK2_ADD, HYPER_FL_UNLOCK2_DATA);
//Sixth Command
WriteCommandHyperFlash((sector_addr>>1), HYPER_FL_SECTOR_ERASE_COM);
while(1){
status = ReadStatusHyperFlash(readData);
if( status & BIT7 ) break; //BIT7: Device Ready Bit (0=Busy, 1=Ready)
}
}
int32_t ChipEraseHyperFlash(void)
{
uint32_t hyperFlashStatus;
uint32_t loopf;
uint32_t status=0;
uint32_t readData[2];
int32_t errFlag;
errFlag = NORMAL_END;
char str[10];
//1st
WriteCommandHyperFlash(HYPER_FL_UNLOCK1_ADD, HYPER_FL_UNLOCK1_DATA);
//2nd
WriteCommandHyperFlash(HYPER_FL_UNLOCK2_ADD, HYPER_FL_UNLOCK2_DATA);
//3rd
WriteCommandHyperFlash(HYPER_FL_UNLOCK3_ADD, HYPER_FL_ERASE_1ST_COM);
//4th
WriteCommandHyperFlash(HYPER_FL_UNLOCK1_ADD, HYPER_FL_UNLOCK1_DATA);
//5th
WriteCommandHyperFlash(HYPER_FL_UNLOCK2_ADD, HYPER_FL_UNLOCK2_DATA);
//Sixth Command
WriteCommandHyperFlash(HYPER_FL_UNLOCK3_ADD, HYPER_FL_CHIP_ERASE_COM);
while(1){
status = ReadStatusHyperFlash(readData);
//Data2HexAscii(status,str,4); //********************* DEBUG
//PutStr(" readData(status) : ",0); //********************* DEBUG
//PutStr(str,1); //********************* DEBUG
if( status & BIT5 ){ //BIT5: Erase Status Bit (0=Successful, 1=Fail)
// PutStr("Erase Fail",0);
errFlag=ERROR_END;
break;
}
if( status & BIT7 ) break; //BIT7: Device Ready Bit (0=Busy, 1=Ready)
}
return(errFlag);
}
//////////////////////////////////
// Read (External Address Space Read Mode)
//////////////////////////////////
void RdHyperFlash (uint32_t sourceSpiAdd,uint32_t destinationAdd,uint32_t byteCount)
{
uint32_t sourceAdd;
InitRPC_HyperFlashExtMode();
sourceAdd = (SPI_IOADDRESS_TOP + sourceSpiAdd);
// copy : spi_ioaddress (H'08000000)-> destinationAdd (H'60000000)
mem_copy(destinationAdd, sourceAdd, byteCount);
}
void RdManuHyperFlash(uint32_t sourceSpiAdd,uintptr_t destinationAdd,uint32_t byteCount)
{
char str[10];
uint32_t readAdd;
uint32_t rdData;
rdData=destinationAdd;
for(readAdd=sourceSpiAdd ; readAdd<(sourceSpiAdd+byteCount) ; readAdd+=4){
// ReadHyperFlashData4Byte(readAdd, &rdData);
ReadHyperFlashData8Byte(readAdd, &rdData);
/*
Data2HexAscii(readAdd,str,4);
PutStr(" readAdd = 0x",0);
PutStr(str,1);
*/
destinationAdd +=4;
}
}
void SaveDataWithBuffeHyperFlash(uint32_t srcAdd,uint32_t svFlashAdd,uint32_t svSize)
{
uint32_t flashAdd;
uint32_t writeDataAdd;
char str[64]; //DEBUG
writeDataAdd = srcAdd;
for(flashAdd=svFlashAdd;flashAdd<(svFlashAdd+svSize);flashAdd+=256){ //256byte:RPC Write Buffer size
//Data2HexAscii(flashAdd,str,4); //********************* DEBUG
//PutStr(" flashAdd : ",0); //********************* DEBUG
//PutStr(str,1); //********************* DEBUG
//Data2HexAscii(writeDataAdd,str,4); //********************* DEBUG
//PutStr(" writeDataAdd : ",0); //********************* DEBUG
//PutStr(str,1); //********************* DEBUG
WordProgramWithBuffeHyperFlash(flashAdd, writeDataAdd);
writeDataAdd = writeDataAdd + 256;
}
}
void SaveDataHyperFlash(uintptr_t srcAdd,uint32_t svFlashAdd,uint32_t svSize)
{
uint32_t flashAdd;
uint32_t writeData;
char str[64]; //DEBUG
WriteProtectDisable();
for(flashAdd=svFlashAdd;flashAdd<(svFlashAdd+svSize);flashAdd+=4){
writeData = *(volatile uint32_t*)srcAdd;
//Data2HexAscii(flashAdd,str,4); //********************* DEBUG
//PutStr(" flashAdd : ",0); //********************* DEBUG
//PutStr(str,1); //********************* DEBUG
WordProgramHyperFlash(flashAdd,writeData);
srcAdd = srcAdd + 4;
}
}
void WordProgramWithBuffeHyperFlash(uint32_t addr, uint32_t source_addr)
{
uint32_t loopf;
uint32_t hyperFlashStatus;
uint32_t status=0;
uint32_t readData[2];
WriteProtectDisable();
// PutStr(" WriteProtectDisable",1);
WriteBufferOperationHyperFlash(addr,source_addr);
WaitRpcTxEnd(); //Add 2015.07.21
//SoftDelay(2000); //Add 2015.07.21
#if 0
//Add 2015.07.22
loopf=1;
while(loopf){
PutStr("hyperFlashStatus Check ",1); //DEBUG=================================
ReadStatusHyperFlash(&hyperFlashStatus);
if(hyperFlashStatus & BIT7) loopf=0;
}
#endif
while(1){
status = ReadStatusHyperFlash(readData);
if( status & BIT7 ) break; //BIT7: Device Ready Bit (0=Busy, 1=Ready)
}
}
void SectorEraseHyperFlashRange(uint32_t EraseStatAdd,uint32_t EraseEndAdd)
{
uint32_t sectorAd;
uint32_t SectorStatTopAdd,SectorEndTopAdd;
// char str[64]; //DEBUG
SectorStatTopAdd = EraseStatAdd & 0xFFFC0000;
SectorEndTopAdd = EraseEndAdd & 0xFFFC0000;
//MON Debug
// PutStr(" -------- SectorErase -------- : ",1); //********************* DEBUG
// Data2HexAscii(SectorStatTopAdd,str,4); //********************* DEBUG
// PutStr(" SectorStatTopAdd : H' ",0); //********************* DEBUG
// PutStr(str,1); //********************* DEBUG
// Data2HexAscii(SectorEndTopAdd,str,4); //********************* DEBUG
// PutStr(" SectorEndTopAdd : H' ",0); //********************* DEBUG
// PutStr(str,1); //********************* DEBUG
///MON Debug
for(sectorAd =SectorStatTopAdd;sectorAd<=SectorEndTopAdd;sectorAd=sectorAd+0x40000 ){
SectorEraseHyperFlash(sectorAd);
PutStr(".",0);
}
PutStr("Erase Completed ",1);
}
void SectorRdHyperFlash(uint32_t spiStatAdd,uint32_t distRamAdd)
{
uint32_t SectorStatTopAdd,readSize;
unsigned char *bufPtr;
char str[64]; //DEBUG
SectorStatTopAdd = spiStatAdd & 0xFFFC0000;
readSize = 0x40000;
//MON Debug
// PutStr(" -------- SectorRead -------- : ",1); //********************* DEBUG
// Data2HexAscii(spiStatAdd,str,4); //********************* DEBUG
// PutStr(" spiStatAdd : H'",0); //********************* DEBUG
// PutStr(str,1); //********************* DEBUG
// Data2HexAscii(SectorStatTopAdd,str,4); //********************* DEBUG
// PutStr(" SectorStatTopAdd : H'",0); //********************* DEBUG
// PutStr(str,1); //********************* DEBUG
// Data2HexAscii(distRamAdd,str,4); //********************* DEBUG
// PutStr(" distRamAdd : H'",0); //********************* DEBUG
// PutStr(str,1); //********************* DEBUG
///MON Debug
RdHyperFlash(SectorStatTopAdd,distRamAdd,readSize);
}