aboutsummaryrefslogtreecommitdiff
path: root/backends/platform/ds/arm9/source/fat/io_efa2.c
blob: f3aa65cfcbafd8392a65de8fd2eff5c010bd709c (plain)
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
/*
io_efa2.c by CyteX

Based on io_mpfc.c by chishm (Michael Chisholm)

Hardware Routines for reading the NAND flash located on
EFA2 flash carts

This software is completely free. No warranty is provided.
If you use it, please give me credit and email me about your
project at cytex <at> gmx <dot> de and do not forget to also
drop chishm <at> hotmail <dot> com a line

See gba_nds_fat.txt for help and license details.
*/

#include "io_efa2.h"

#ifdef SUPPORT_EFA2

//
// EFA2 register addresses
//

// RTC registers
#define REG_RTC_CLK        *(vu16*)0x080000c4
#define REG_RTC_EN         *(vu16*)0x080000c8

// "Magic" registers used for unlock/lock sequences
#define REG_EFA2_MAGIC_A   *(vu16*)0x09fe0000
#define REG_EFA2_MAGIC_B   *(vu16*)0x08000000
#define REG_EFA2_MAGIC_C   *(vu16*)0x08020000
#define REG_EFA2_MAGIC_D   *(vu16*)0x08040000
#define REG_EFA2_MAGIC_E   *(vu16*)0x09fc0000

// NAND flash lock/unlock register
#define REG_EFA2_NAND_LOCK *(vu16*)0x09c40000
// NAND flash enable register
#define REG_EFA2_NAND_EN   *(vu16*)0x09400000
// NAND flash command write register
#define REG_EFA2_NAND_CMD   *(vu8*)0x09ffffe2
// NAND flash address/data write register
#define REG_EFA2_NAND_WR    *(vu8*)0x09ffffe0
// NAND flash data read register
#define REG_EFA2_NAND_RD    *(vu8*)0x09ffc000

// ID of Samsung K9K1G NAND flash chip
#define EFA2_NAND_ID 0xEC79A5C0

// first sector of udisk 
#define EFA2_UDSK_START 0x40

//
// EFA2 access functions
//

// deactivate RTC ports
inline void efa2_rtc_deactivate(void) {
	REG_RTC_EN = 0;
}

// unlock register access
void efa2_reg_unlock(void) {
	REG_EFA2_MAGIC_A = 0x0d200;
	REG_EFA2_MAGIC_B = 0x01500;
	REG_EFA2_MAGIC_C = 0x0d200;
	REG_EFA2_MAGIC_D = 0x01500;
}

// finish/lock register access
inline void efa2_reg_lock(void) {
	REG_EFA2_MAGIC_E = 0x1500;
}

// global reset/init/enable/unlock ?
void efa2_global_unlock(void) {
	efa2_reg_unlock();
	*(vu16*)0x09880000 = 0x08000;
	efa2_reg_lock();
}

// global lock, stealth mode
void efa2_global_lock(void) {
	// quite sure there is such a sequence, but haven't had
	// a look for it upto now
}

// unlock NAND Flash
void efa2_nand_unlock(void) {
	efa2_reg_unlock();
	REG_EFA2_NAND_LOCK = 0x01500;
	efa2_reg_lock();
}

// lock NAND Flash
void efa2_nand_lock(void) {
	efa2_reg_unlock();
	REG_EFA2_NAND_LOCK = 0x0d200;
	efa2_reg_lock();
}

//
// Set NAND Flash chip enable and write protection bits ?
// 
//   val | ~CE | ~WP |
//  -----+-----+-----+
//     0 |  0  |  0  |
//     1 |  1  |  0  |
//     3 |  1  |  1  |
//  -----+-----+-----+
//
void efa2_nand_enable(u16 val) {
	efa2_reg_unlock();
	REG_EFA2_NAND_EN = val;
	efa2_reg_lock();
}

//
// Perform NAND reset
// NAND has to be unlocked and enabled when called
//
inline void efa2_nand_reset(void) {
	REG_EFA2_NAND_CMD = 0xff; // write reset command
}

//
// Read out NAND ID information, could be used for card detection
// 
//                    | EFA2 1GBit |
//  ------------------+------------+
//         maker code |    0xEC    |
//        device code |    0x79    |
//         don't care |    0xA5    |
//   multi plane code |    0xC0    |
//  ------------------+------------+
//
u32 efa2_nand_id(void) {
	u8 byte;
	u32 id;

	efa2_nand_unlock();
	efa2_nand_enable(1);

	REG_EFA2_NAND_CMD = 0x90;  // write id command
	REG_EFA2_NAND_WR  = 0x00;  // (dummy) address cycle
	byte = REG_EFA2_NAND_RD;   // read maker code
	id   = byte;
	byte = REG_EFA2_NAND_RD;   // read device code
	id   = (id << 8) | byte;
	byte = REG_EFA2_NAND_RD;   // read don't care
	id   = (id << 8) | byte;
	byte = REG_EFA2_NAND_RD;   // read multi plane code
	id   = (id << 8) | byte;

	efa2_nand_enable(0);
	efa2_nand_lock();
	return (id);
}

//
// Start of gba_nds_fat block device description
//

/*-----------------------------------------------------------------
EFA2_ClearStatus
Reads and checks NAND status information
bool return OUT:  true if NAND is idle
-----------------------------------------------------------------*/
bool EFA2_ClearStatus (void) 
{
	// tbd: currently there is no write support, so always return
	// true, there is no possibility for pending operations
	return true;
}

/*-----------------------------------------------------------------
EFA2_IsInserted
Checks to see if the NAND chip used by the EFA2 is present
bool return OUT:  true if the correct NAND chip is found
-----------------------------------------------------------------*/
bool EFA2_IsInserted (void) 
{
	EFA2_ClearStatus();
	return (efa2_nand_id() == EFA2_NAND_ID);
}

/*-----------------------------------------------------------------
EFA2_ReadSectors
Read "numSecs" 512 byte sectors starting from "sector" into "buffer"
No error correction, no use of spare cells, no use of R/~B signal
u32 sector IN: number of first 512 byte sector to be read
u8 numSecs IN: number of 512 byte sectors to read,
1 to 256 sectors can be read, 0 = 256
void* buffer OUT: pointer to 512 byte buffer to store data in
bool return OUT: true if successful
-----------------------------------------------------------------*/
bool EFA2_ReadSectors (u32 sector, u8 numSecs, void* buffer)
{
	int  i;
	int  j = (numSecs > 0 ? numSecs : 256);

#ifndef _CF_ALLOW_UNALIGNED
	u8  byte;
	u16  word;
#endif

	// NAND page 0x40 (EFA2_UDSK_START) contains the MBR of the
	// udisk and thus is sector 0. The original EFA2 firmware
	// does never look at this, it only watches page 0x60, which
	// contains the boot block of the FAT16 partition. That is
	// fixed, so the EFA2 udisk must not be reformated, else
	// the ARK Octopus and also the original Firmware won't be
	// able to access the udisk anymore and I have to write a
	// recovery tool.
	u32 page = EFA2_UDSK_START + sector;

	// future enhancement: wait for possible write operations to
	// be finisched
	if (!EFA2_ClearStatus()) return false;

	efa2_nand_unlock();
	efa2_nand_enable(1);
	efa2_nand_reset();

	// set NAND to READ1 operation mode and transfer page address
	REG_EFA2_NAND_CMD = 0x00;                // write READ1 command  
	REG_EFA2_NAND_WR  = 0x00;                // write address  [7:0]
	REG_EFA2_NAND_WR  = (page      ) & 0xff; // write address [15:8]
	REG_EFA2_NAND_WR  = (page >> 8 ) & 0xff; // write address[23:16]
	REG_EFA2_NAND_WR  = (page >> 16) & 0xff; // write address[26:24]

	// Due to a bug in EFA2 design there is need to waste some cycles
	// "by hand" instead the possibility to check the R/~B port of
	// the NAND flash via a register. The RTC deactivation is only
	// there to make sure the loop won't be optimized by the compiler
	for (i=0 ; i < 3 ; i++) efa2_rtc_deactivate();

	while (j--)
	{
		// read page data
#ifdef _CF_ALLOW_UNALIGNED
		// slow byte access to RAM, but works in principle
		for (i=0 ; i < 512 ; i++)
			((u8*)buffer)[i] = REG_EFA2_NAND_RD;
#else
		// a bit faster, but DMA is not possible
		for (i=0 ; i < 256 ; i++) {
			byte = REG_EFA2_NAND_RD;   // read lo-byte
			word = byte;
			byte = REG_EFA2_NAND_RD;   // read hi-byte
			word = word | (byte << 8);
			((u16*)buffer)[i] = word;
		}
#endif
	}

	efa2_nand_enable(0);
	efa2_nand_lock();
	return true;
}


/*-----------------------------------------------------------------
EFA2_WriteSectors
Write "numSecs" 512 byte sectors starting at "sector" from "buffer"
u32 sector IN: address of 512 byte sector on card to write
u8 numSecs IN: number of 512 byte sectors to write
1 to 256 sectors can be written, 0 = 256
void* buffer IN: pointer to 512 byte buffer to read data from
bool return OUT: true if successful
-----------------------------------------------------------------*/
bool EFA2_WriteSectors (u32 sector, u8 numSecs, void* buffer)
{
	// Upto now I focused on reading NAND, write operations
	// will follow
	return false;
}

/*-----------------------------------------------------------------
EFA2_Shutdown
unload the EFA2 interface
-----------------------------------------------------------------*/
bool EFA2_Shutdown(void) 
{
	return EFA2_ClearStatus();
}

/*-----------------------------------------------------------------
EFA2_StartUp
initializes the EFA2 card, returns true if successful,
otherwise returns false
-----------------------------------------------------------------*/
bool EFA2_StartUp(void)
{
	efa2_global_unlock();
	return (efa2_nand_id() == EFA2_NAND_ID);
}

/*-----------------------------------------------------------------
the actual interface structure
-----------------------------------------------------------------*/
IO_INTERFACE io_efa2 = {
	DEVICE_TYPE_EFA2,
	FEATURE_MEDIUM_CANREAD | FEATURE_SLOT_GBA,
	(FN_MEDIUM_STARTUP)&EFA2_StartUp,
	(FN_MEDIUM_ISINSERTED)&EFA2_IsInserted,
	(FN_MEDIUM_READSECTORS)&EFA2_ReadSectors,
	(FN_MEDIUM_WRITESECTORS)&EFA2_WriteSectors,
	(FN_MEDIUM_CLEARSTATUS)&EFA2_ClearStatus,
	(FN_MEDIUM_SHUTDOWN)&EFA2_Shutdown
};

/*-----------------------------------------------------------------
EFA2_GetInterface
returns the interface structure to host
-----------------------------------------------------------------*/
LPIO_INTERFACE EFA2_GetInterface(void) {
	return &io_efa2;
}

#endif // SUPPORT_EFA2
/*
io_efa2.c by CyteX

Based on io_mpfc.c by chishm (Michael Chisholm)

Hardware Routines for reading the NAND flash located on
EFA2 flash carts

This software is completely free. No warranty is provided.
If you use it, please give me credit and email me about your
project at cytex <at> gmx <dot> de and do not forget to also
drop chishm <at> hotmail <dot> com a line

See gba_nds_fat.txt for help and license details.
*/

#include "io_efa2.h"

#ifdef SUPPORT_EFA2

//
// EFA2 register addresses
//

// RTC registers
#define REG_RTC_CLK        *(vu16*)0x080000c4
#define REG_RTC_EN         *(vu16*)0x080000c8

// "Magic" registers used for unlock/lock sequences
#define REG_EFA2_MAGIC_A   *(vu16*)0x09fe0000
#define REG_EFA2_MAGIC_B   *(vu16*)0x08000000
#define REG_EFA2_MAGIC_C   *(vu16*)0x08020000
#define REG_EFA2_MAGIC_D   *(vu16*)0x08040000
#define REG_EFA2_MAGIC_E   *(vu16*)0x09fc0000

// NAND flash lock/unlock register
#define REG_EFA2_NAND_LOCK *(vu16*)0x09c40000
// NAND flash enable register
#define REG_EFA2_NAND_EN   *(vu16*)0x09400000
// NAND flash command write register
#define REG_EFA2_NAND_CMD   *(vu8*)0x09ffffe2
// NAND flash address/data write register
#define REG_EFA2_NAND_WR    *(vu8*)0x09ffffe0
// NAND flash data read register
#define REG_EFA2_NAND_RD    *(vu8*)0x09ffc000

// ID of Samsung K9K1G NAND flash chip
#define EFA2_NAND_ID 0xEC79A5C0

// first sector of udisk 
#define EFA2_UDSK_START 0x40

//
// EFA2 access functions
//

// deactivate RTC ports
inline void efa2_rtc_deactivate(void) {
	REG_RTC_EN = 0;
}

// unlock register access
void efa2_reg_unlock(void) {
	REG_EFA2_MAGIC_A = 0x0d200;
	REG_EFA2_MAGIC_B = 0x01500;
	REG_EFA2_MAGIC_C = 0x0d200;
	REG_EFA2_MAGIC_D = 0x01500;
}

// finish/lock register access
inline void efa2_reg_lock(void) {
	REG_EFA2_MAGIC_E = 0x1500;
}

// global reset/init/enable/unlock ?
void efa2_global_unlock(void) {
	efa2_reg_unlock();
	*(vu16*)0x09880000 = 0x08000;
	efa2_reg_lock();
}

// global lock, stealth mode
void efa2_global_lock(void) {
	// quite sure there is such a sequence, but haven't had
	// a look for it upto now
}

// unlock NAND Flash
void efa2_nand_unlock(void) {
	efa2_reg_unlock();
	REG_EFA2_NAND_LOCK = 0x01500;
	efa2_reg_lock();
}

// lock NAND Flash
void efa2_nand_lock(void) {
	efa2_reg_unlock();
	REG_EFA2_NAND_LOCK = 0x0d200;
	efa2_reg_lock();
}

//
// Set NAND Flash chip enable and write protection bits ?
// 
//   val | ~CE | ~WP |
//  -----+-----+-----+
//     0 |  0  |  0  |
//     1 |  1  |  0  |
//     3 |  1  |  1  |
//  -----+-----+-----+
//
void efa2_nand_enable(u16 val) {
	efa2_reg_unlock();
	REG_EFA2_NAND_EN = val;
	efa2_reg_lock();
}

//
// Perform NAND reset
// NAND has to be unlocked and enabled when called
//
inline void efa2_nand_reset(void) {
	REG_EFA2_NAND_CMD = 0xff; // write reset command
}

//
// Read out NAND ID information, could be used for card detection
// 
//                    | EFA2 1GBit |
//  ------------------+------------+
//         maker code |    0xEC    |
//        device code |    0x79    |
//         don't care |    0xA5    |
//   multi plane code |    0xC0    |
//  ------------------+------------+
//
u32 efa2_nand_id(void) {
	u8 byte;
	u32 id;

	efa2_nand_unlock();
	efa2_nand_enable(1);

	REG_EFA2_NAND_CMD = 0x90;  // write id command
	REG_EFA2_NAND_WR  = 0x00;  // (dummy) address cycle
	byte = REG_EFA2_NAND_RD;   // read maker code
	id   = byte;
	byte = REG_EFA2_NAND_RD;   // read device code
	id   = (id << 8) | byte;
	byte = REG_EFA2_NAND_RD;   // read don't care
	id   = (id << 8) | byte;
	byte = REG_EFA2_NAND_RD;   // read multi plane code
	id   = (id << 8) | byte;

	efa2_nand_enable(0);
	efa2_nand_lock();
	return (id);
}

//
// Start of gba_nds_fat block device description
//

/*-----------------------------------------------------------------
EFA2_ClearStatus
Reads and checks NAND status information
bool return OUT:  true if NAND is idle
-----------------------------------------------------------------*/
bool EFA2_ClearStatus (void) 
{
	// tbd: currently there is no write support, so always return
	// true, there is no possibility for pending operations
	return true;
}

/*-----------------------------------------------------------------
EFA2_IsInserted
Checks to see if the NAND chip used by the EFA2 is present
bool return OUT:  true if the correct NAND chip is found
-----------------------------------------------------------------*/
bool EFA2_IsInserted (void) 
{
	EFA2_ClearStatus();
	return (efa2_nand_id() == EFA2_NAND_ID);
}

/*-----------------------------------------------------------------
EFA2_ReadSectors
Read "numSecs" 512 byte sectors starting from "sector" into "buffer"
No error correction, no use of spare cells, no use of R/~B signal
u32 sector IN: number of first 512 byte sector to be read
u8 numSecs IN: number of 512 byte sectors to read,
1 to 256 sectors can be read, 0 = 256
void* buffer OUT: pointer to 512 byte buffer to store data in
bool return OUT: true if successful
-----------------------------------------------------------------*/
bool EFA2_ReadSectors (u32 sector, u8 numSecs, void* buffer)
{
	int  i;
	int  j = (numSecs > 0 ? numSecs : 256);

#ifndef _CF_ALLOW_UNALIGNED
	u8  byte;
	u16  word;
#endif

	// NAND page 0x40 (EFA2_UDSK_START) contains the MBR of the
	// udisk and thus is sector 0. The original EFA2 firmware
	// does never look at this, it only watches page 0x60, which
	// contains the boot block of the FAT16 partition. That is
	// fixed, so the EFA2 udisk must not be reformated, else
	// the ARK Octopus and also the original Firmware won't be
	// able to access the udisk anymore and I have to write a
	// recovery tool.
	u32 page = EFA2_UDSK_START + sector;

	// future enhancement: wait for possible write operations to
	// be finisched
	if (!EFA2_ClearStatus()) return false;

	efa2_nand_unlock();
	efa2_nand_enable(1);
	efa2_nand_reset();

	// set NAND to READ1 operation mode and transfer page address
	REG_EFA2_NAND_CMD = 0x00;                // write READ1 command  
	REG_EFA2_NAND_WR  = 0x00;                // write address  [7:0]
	REG_EFA2_NAND_WR  = (page      ) & 0xff; // write address [15:8]
	REG_EFA2_NAND_WR  = (page >> 8 ) & 0xff; // write address[23:16]
	REG_EFA2_NAND_WR  = (page >> 16) & 0xff; // write address[26:24]

	// Due to a bug in EFA2 design there is need to waste some cycles
	// "by hand" instead the possibility to check the R/~B port of
	// the NAND flash via a register. The RTC deactivation is only
	// there to make sure the loop won't be optimized by the compiler
	for (i=0 ; i < 3 ; i++) efa2_rtc_deactivate();

	while (j--)
	{
		// read page data
#ifdef _CF_ALLOW_UNALIGNED
		// slow byte access to RAM, but works in principle
		for (i=0 ; i < 512 ; i++)
			((u8*)buffer)[i] = REG_EFA2_NAND_RD;
#else
		// a bit faster, but DMA is not possible
		for (i=0 ; i < 256 ; i++) {
			byte = REG_EFA2_NAND_RD;   // read lo-byte
			word = byte;
			byte = REG_EFA2_NAND_RD;   // read hi-byte
			word = word | (byte << 8);
			((u16*)buffer)[i] = word;
		}
#endif
	}

	efa2_nand_enable(0);
	efa2_nand_lock();
	return true;
}


/*-----------------------------------------------------------------
EFA2_WriteSectors
Write "numSecs" 512 byte sectors starting at "sector" from "buffer"
u32 sector IN: address of 512 byte sector on card to write
u8 numSecs IN: number of 512 byte sectors to write
1 to 256 sectors can be written, 0 = 256
void* buffer IN: pointer to 512 byte buffer to read data from
bool return OUT: true if successful
-----------------------------------------------------------------*/
bool EFA2_WriteSectors (u32 sector, u8 numSecs, void* buffer)
{
	// Upto now I focused on reading NAND, write operations
	// will follow
	return false;
}

/*-----------------------------------------------------------------
EFA2_Shutdown
unload the EFA2 interface
-----------------------------------------------------------------*/
bool EFA2_Shutdown(void) 
{
	return EFA2_ClearStatus();
}

/*-----------------------------------------------------------------
EFA2_StartUp
initializes the EFA2 card, returns true if successful,
otherwise returns false
-----------------------------------------------------------------*/
bool EFA2_StartUp(void)
{
	efa2_global_unlock();
	return (efa2_nand_id() == EFA2_NAND_ID);
}

/*-----------------------------------------------------------------
the actual interface structure
-----------------------------------------------------------------*/
IO_INTERFACE io_efa2 = {
	DEVICE_TYPE_EFA2,
	FEATURE_MEDIUM_CANREAD | FEATURE_SLOT_GBA,
	(FN_MEDIUM_STARTUP)&EFA2_StartUp,
	(FN_MEDIUM_ISINSERTED)&EFA2_IsInserted,
	(FN_MEDIUM_READSECTORS)&EFA2_ReadSectors,
	(FN_MEDIUM_WRITESECTORS)&EFA2_WriteSectors,
	(FN_MEDIUM_CLEARSTATUS)&EFA2_ClearStatus,
	(FN_MEDIUM_SHUTDOWN)&EFA2_Shutdown
};

/*-----------------------------------------------------------------
EFA2_GetInterface
returns the interface structure to host
-----------------------------------------------------------------*/
LPIO_INTERFACE EFA2_GetInterface(void) {
	return &io_efa2;
}

#endif // SUPPORT_EFA2