-
Notifications
You must be signed in to change notification settings - Fork 8
/
mmiodev_DirectNAND.c
executable file
·112 lines (84 loc) · 3.08 KB
/
mmiodev_DirectNAND.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
//(c) uARM project https://github.com/uARM-Palm/uARM [email protected]
#include "mmiodev_DirectNAND.h"
#include <string.h>
#include <stdlib.h>
#include "util.h"
#include "nand.h"
#include "mem.h"
#include "CPU.h"
struct DirectNAND {
uint32_t baseCleAddr, baseAleAddr, baseDataAddr, maskBitsAddr;
struct SocGpio *gpio;
struct NAND *nand;
int8_t rdyPinNo;
};
static void directNandPrvReady(void *userData, bool ready)
{
struct DirectNAND *directNand = (struct DirectNAND*)userData;
if (directNand->rdyPinNo >= 0)
socGpioSetState(directNand->gpio, directNand->rdyPinNo, ready);
}
static bool directNandPrvMemAccessF(void* userData, uint32_t pa, uint_fast8_t size, bool write, void* buf){
struct DirectNAND *directNand = (struct DirectNAND*)userData;
bool ret, cle = false, ale = false;
if (size != 1) {
fprintf(stderr, "%s: Unexpected %s of %u bytes to 0x%08lx\n", __func__, write ? "write" : "read", size, (unsigned long)pa);
return false;
}
pa &=~ directNand->maskBitsAddr;
if (pa == directNand->baseCleAddr)
cle = true;
else if (pa == directNand->baseAleAddr)
ale = true;
else if (pa != directNand->baseDataAddr)
return false;
if (write) {
ret = nandWrite(directNand->nand, cle, ale, *(uint8_t*)buf);
// if (pa & 6)
// fprintf(stderr, "NAND w c%u a%u 0x%02x -> %s\n", cle, ale, *(uint8_t*)buf, ret ? "OK" : "FAIL");
}
else {
ret = nandRead(directNand->nand, cle, ale, (uint8_t*)buf);
// if (pa & 6)
// fprintf(stderr, "NAND r c%u a%u 0x%02x -> %s\n", cle, ale, *(uint8_t*)buf, ret ? "OK" : "FAIL");
}
if (!ret)
ERR("NAND ACCESS FAILS\n");
return ret;
}
struct DirectNAND* directNandInit(struct ArmMem *physMem, uint32_t baseCleAddr, uint32_t baseAleAddr, uint32_t baseDataAddr, uint32_t maskBitsAddr, struct SocGpio* gpio, int rdyPin, const struct NandSpecs *specs, FILE *nandFile)
{
struct DirectNAND *directNand = (struct DirectNAND*)malloc(sizeof(*directNand));
uint32_t minAddr = 0xfffffffful, maxAddr = 0, length;
if (!directNand)
ERR("cannot alloc TE2 NAND");
memset(directNand, 0, sizeof (*directNand));
directNand->baseCleAddr = baseCleAddr;
directNand->baseAleAddr = baseAleAddr;
directNand->baseDataAddr = baseDataAddr;
directNand->maskBitsAddr = maskBitsAddr;
directNand->gpio = gpio;
directNand->rdyPinNo = rdyPin;
directNand->nand = nandInit(nandFile, specs, directNandPrvReady, directNand);
if (!directNand->nand)
ERR("Cannot init underlying NAND\n");
if (baseCleAddr > maxAddr)
maxAddr = baseCleAddr;
if (baseCleAddr < minAddr)
minAddr = baseCleAddr;
if (baseAleAddr > maxAddr)
maxAddr = baseAleAddr;
if (baseAleAddr < minAddr)
minAddr = baseAleAddr;
if (baseDataAddr > maxAddr)
maxAddr = baseDataAddr;
if (baseDataAddr < minAddr)
minAddr = baseDataAddr;
if (!memRegionAdd(physMem, minAddr, maxAddr - minAddr + 4, directNandPrvMemAccessF, directNand))
ERR("cannot add NAND to MEM\n");
return directNand;
}
void directNandPeriodic(struct DirectNAND *directNand)
{
nandPeriodic(directNand->nand);
}