OpenCores
URL https://opencores.org/ocsvn/usb_nand_reader/usb_nand_reader/trunk

Subversion Repositories usb_nand_reader

[/] [usb_nand_reader/] [trunk/] [pc/] [nand_vendors.c] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 pradd
 
2
#include <stdlib.h>
3
#include <string.h>
4
#include <stdio.h>
5
#include <ctype.h>
6
#include "include/nand_vendors.h"
7
 
8
 
9
int
10
get_nand_configuration(pnand_t nand)
11
{
12
        int result = 0;
13
 
14
        printf("Getting configuration\n");
15
 
16
        if(NULL != nand->onfiParameterPage)
17
        {
18
                nand->manufacturer = strndup((char*)nand->onfiParameterPage + 32, 12);
19
                nand->device = strndup((char*)nand->onfiParameterPage + 44, 20);
20
                nand->bytesPerPage = *(int*)(nand->onfiParameterPage + 80);
21
                nand->oobPerPage = (int)*(short*)(nand->onfiParameterPage + 84);
22
                nand->pagesPerBlock = *(int*)(nand->onfiParameterPage + 92);
23
                nand->planesPerLun = 1;
24
                nand->blocksPerPlane = *(int*)(nand->onfiParameterPage + 96);
25
                nand->numLuns = (int)*(nand->onfiParameterPage  +100);
26
                nand->addressCycles = (*(nand->onfiParameterPage + 101) & 0x0f) + ((*(nand->onfiParameterPage + 101) >> 4) & 0x0f);
27
                nand->busWidth = 8 << (*(nand->onfiParameterPage + 6) & 0x01);
28
                result = 1;
29
        }
30
        else
31
        {
32
                switch(nand->id[0])
33
                {
34
                        case 0x2c:      // Micron
35
                                printf("Micron NAND flash detected\n");
36
                                nand->manufacturer = strdup("Micron");
37
 
38
                                if(nand->id[1] == 0xa1)
39
                                        nand->device = strdup("MT29F1G08ABBDA");
40
                                else if(nand->id[1] == 0xaa)
41
                                        nand->device = strdup("MT29F2G08ABBEA");
42
                                else if(nand->id[1] == 0xb1)
43
                                        nand->device = strdup("MT29F1G16ABBDA");
44
                                else if(nand->id[1] == 0xba)
45
                                        nand->device = strdup("MT29F2G16ABBEA");
46
                                else if(nand->id[1] == 0xca)
47
                                        nand->device = strdup("MT29F2G16ABAEA");
48
                                else if(nand->id[1] == 0xda)
49
                                        nand->device = strdup("MT29F2G08ABAEA");
50
                                else
51
                                {
52
                                        printf("Unknown device\n");
53
                                        break;
54
                                }
55
 
56
                                if((nand->id[3] & 3) == 1)
57
                                        nand->bytesPerPage = 2048;
58
                                else
59
                                {
60
                                        printf("Could not get number of bytes per page\n");
61
                                        break;
62
                                }
63
 
64
                                if((nand->id[3] & 4) == 4)
65
                                        nand->oobPerPage = 64;
66
                                else
67
                                {
68
                                        printf("Could not get number of OOB bytes per page\n");
69
                                        break;
70
                                }
71
 
72
 
73
                                nand->pagesPerBlock = ((64 << ((nand->id[3] >> 4) & 3)) * 1024) / nand->bytesPerPage;
74
                                nand->busWidth = 8 << ((nand->id[3] >> 4) & 1);
75
 
76
                                nand->numLuns = 1;
77
                                nand->planesPerLun = 1 << ((nand->id[4] >> 2) & 3);
78
                                nand->blocksPerPlane = (((1024 * 1024 * 1024) / 8) << ((nand->id[4] >> 4) & 7)) / (nand->pagesPerBlock * nand->bytesPerPage);
79
                                nand->addressCycles = 5;
80
 
81
                                result = 1;
82
                                break;
83
 
84
                        case 0x98:      // Toshiba
85
                                printf("Toshiba NAND flash detected (JEDEC ID: %02X%02X%02X%02X%02X)\n", nand->id[0], nand->id[1], nand->id[2], nand->id[3], nand->id[4]);
86
                                nand->manufacturer = strdup("Toshiba");
87
 
88
                                if(nand->id[1] == 0xda)
89
                                {
90
                                        nand->device = strdup("TC58NVG1S3HTA00");
91
                                        nand->busWidth = 8;
92
                                        nand->bytesPerPage = 2048;
93
                                        nand->oobPerPage = 128;
94
                                        nand->pagesPerBlock = 64;
95
                                        nand->numLuns = 1;
96
                                        nand->planesPerLun = 1;
97
                                        nand->blocksPerPlane = 2048;
98
                                        nand->addressCycles = 5;
99
                                        result = 1;
100
                                }
101
                                else
102
                                        break;
103
                                break;
104
 
105
 
106
                        default:
107
                                printf("Device manufacturer %02x could not be identified.\n", nand->id[0]);
108
                                break;
109
                }
110
        }
111
        if(result)
112
                nand->pageBuffer = (unsigned char*)malloc(sizeof(unsigned char) * (nand->bytesPerPage + nand->oobPerPage));
113
        return result;
114
}
115
 
116
void free_nand_info(pnand_t* nand)
117
{
118
        if(NULL == nand || NULL == *nand)
119
                return;
120
 
121
        if(NULL != (*nand)->manufacturer)
122
                free((*nand)->manufacturer);
123
        if(NULL != (*nand)->device)
124
                free((*nand)->device);
125
        if(NULL != (*nand)->onfiParameterPage)
126
                free((*nand)->onfiParameterPage);
127
        if(NULL != (*nand)->pageBuffer)
128
                free((*nand)->pageBuffer);
129
        free(*nand);
130
        *nand = NULL;
131
}
132
 
133
void print_nand_configuration(pnand_t nand)
134
{
135
        printf("\n***********************************\n");
136
        printf("***** NAND configuration data *****\n");
137
        printf("***********************************\n");
138
        printf("Manufacturer:            %s\n", nand->manufacturer);
139
        printf("Device:                  %s\n", nand->device);
140
        printf("ID:                      %02x %02x %02x %02x %02x\n", nand->id[0], nand->id[1], nand->id[2], nand->id[3], nand->id[4]);
141
        printf("ONFI compliant:          %s\n", (NULL == nand->onfiParameterPage)? "No" : "Yes");
142
        printf("Data bus width:          %d\n", nand->busWidth);
143
        printf("Bytes per page:          %d bytes\n", nand->bytesPerPage);
144
        printf("OOB per page:            %d bytes\n", nand->oobPerPage);
145
        printf("Pages per block:         %d\n", nand->pagesPerBlock);
146
        printf("Blocks per plane:        %d\n", nand->blocksPerPlane);
147
        printf("Planes per LUN:          %d\n", nand->planesPerLun);
148
        printf("Number of LUNs:          %d\n", nand->numLuns);
149
        printf("Address cycles:          %d\n", nand->addressCycles);
150
        printf("ONFI signature bytes:    %02x %02x %02x %02x\n\n", nand->onfiSignature[0], nand->onfiSignature[1], nand->onfiSignature[2], nand->onfiSignature[3]);
151
}
152
 
153
 
154
void dump_page(pnand_t nand)
155
{
156
        int i, j;
157
 
158
        for(i = 0; i < nand->bytesPerPage + nand->oobPerPage; i += 16)
159
        {
160
                for(j = 0; j < 16; j++)
161
                {
162
                        printf("%02x ", nand->pageBuffer[i + j]);
163
                }
164
 
165
                printf("\t\t");
166
 
167
                for(j = 0; j < 16; j++)
168
                {
169
                        if(isprint(nand->pageBuffer[i + j]))
170
                                printf("%c", nand->pageBuffer[i + j]);
171
                        else
172
                                printf(".");
173
                }
174
                printf("\n");
175
        }
176
}
177
 
178
void store_page(FILE* f, pnand_t nand)
179
{
180
        fwrite(nand->pageBuffer, nand->bytesPerPage + nand->oobPerPage, 1, f);
181
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.