OSDN Git Service

client 0.6.2 release
[unagi/old-svn-converted.git] / client / tag / 0.6.2 / flashmemory.c
1 /*
2 famicom ROM cartridge utility - unagi
3 flash memory driver
4
5 Copyright (C) 2008-2009 ±·³«È¯¶¨Æ±Áȹç
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
21 flashmemory.c ¤À¤±¤Î·Ù¹ð
22 ¤³¤Î¥½¡¼¥¹¥³¡¼¥É¤ò»²¹Í¡¢Å¾ÍѤ·¤Æ¥·¥§¥¢¥¦¥§¥¢¤Ê¤É¤ÇÍø±×¤òÆÀ¤Ê¤¤¤³¤È¡£
23 ȽÌÀ¤·¤¿¾ì¹ç¤Ï LGPL ¤¬Å¬ÍѤµ¤ì¡¢³ºÅö²Õ½ê¤Î¥½¡¼¥¹¤ò¸ø³«¤¹¤ëɬÍפ¬¤¢¤ë¡£
24 */
25 #include <assert.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <windows.h>
30 #include "type.h"
31 #include "header.h"
32 #include "flashmemory.h"
33
34 /*
35 ---- erase ----
36 */
37 #if 0 //DEBUG==1
38 static void sram_erase(const struct flash_order *d)
39 {
40         //bank ÀÚ¤êÂؤ¨¤¬È¼¤¦¤Î¤Ç¼ÂÁõ¤Ç¤­¤Ê¤¤
41 }
42 #endif
43
44 static void init_nop(const struct flash_order *d, long wait)
45 {
46 }
47
48 static void init_erase(const struct flash_order *d, long wait)
49 {
50         assert(d->pagesize > 0);
51         d->erase(d->command_2aaa, true);
52         Sleep(wait);
53 }
54
55 static void program_dummy(const struct flash_order *d, long address, long length, const struct memory *m)
56 {
57 }
58 static void program_sram(const struct flash_order *d, long address, long length, const struct memory *m)
59 {
60         d->write(address, length, m->data);
61 }
62 static void program_flash(const struct flash_order *d, long address, long length, const struct memory *m)
63 {
64         d->program(address, length, m->data, true, false);
65 }
66 /*
67 ¥Ç¥Ð¥¤¥¹¥ê¥¹¥È
68 */
69 enum{MEGA = 0x20000};
70 const struct flash_driver FLASH_DRIVER_UNDEF = {
71         .name = "undefined",
72         .capacity = 0,
73         .pagesize = 1,
74         .erase_wait = 0,
75         .command_mask = 0,
76         .id_manufacurer = 0,
77         .id_device = 0,
78         .productid_check = NULL,
79         .init = NULL,
80         .program = NULL
81 };
82 static const struct flash_driver DRIVER_SRAM = {
83         .name = "SRAM",
84         .capacity = 4 * MEGA,
85         .pagesize = 1,
86         .erase_wait = 0,
87         .command_mask = 0,
88         .id_manufacurer = FLASH_ID_DEVICE_SRAM,
89         .id_device = FLASH_ID_DEVICE_SRAM,
90 //      .productid_check = productid_sram,
91         .init = init_nop,
92         .program = program_sram
93 };
94
95 static const struct flash_driver DRIVER_DUMMY = {
96         .name = "dummy",
97         .capacity = 16 * MEGA,
98         .pagesize = 1,
99         .erase_wait = 0,
100         .command_mask = 0,
101         .id_manufacurer = FLASH_ID_DEVICE_DUMMY,
102         .id_device = FLASH_ID_DEVICE_DUMMY,
103 //      .productid_check = productid_sram,
104         .init = init_nop,
105         .program = program_dummy
106 };
107
108 static const struct flash_driver DRIVER_W29C020 = {
109         .name = "W29C020",
110         .capacity = 2 * MEGA,
111         .pagesize = 0x80,
112         .erase_wait = 50,
113         .command_mask = 0x7fff,
114         .id_manufacurer = 0xda,
115         .id_device = 0x45,
116 //      .productid_check = productid_check,
117         .init = init_nop,
118         .program = program_flash
119 };
120
121 static const struct flash_driver DRIVER_W29C040 = {
122         .name = "W29C040",
123         .capacity = 4 * MEGA,
124         .pagesize = 0x100,
125         .erase_wait = 50,
126         .command_mask = 0x7fff,
127         .id_manufacurer = 0xda,
128         .id_device = 0x46,
129 //      .productid_check = productid_check,
130         .init = init_nop,
131         .program = program_flash
132 };
133
134 static const struct flash_driver DRIVER_W49F002 = {
135         .name = "W49F002",
136         .capacity = 2 * MEGA,
137         .pagesize = 1,
138         .erase_wait = 100, //typ 0.1, max 0.2 sec
139         .command_mask = 0x7fff,
140         .id_manufacurer = 0xda,
141         .id_device = 0xae,
142 //      .productid_check = productid_check,
143         .init = init_erase,
144         .program = program_flash
145 };
146
147 /*
148 MANUFATUTER ID 0x7f1c
149 EN29F002T DEVICE ID 0x7f92
150 EN29F002B DEVICE ID 0x7f97
151
152 command address ¤¬ 0x00555, 0x00aaa ¤Ë¤Ê¤Ã¤Æ¤ë
153 */
154 static const struct flash_driver DRIVER_EN29F002T = {
155         .name = "EN29F002T",
156         .capacity = 2 * MEGA,
157         .pagesize = 1,
158         .erase_wait = 2000, //typ 2, max 5 sec
159         .command_mask = 0x07ff,
160         .id_manufacurer = 0x1c,
161         .id_device = 0x92,
162 //      .productid_check = productid_check,
163         .init = init_erase,
164         .program = program_flash
165 };
166
167 static const struct flash_driver DRIVER_AM29F040B = {
168         .name = "AM29F040B",
169         .capacity = 4 * MEGA,
170         .pagesize = 1,
171         .erase_wait = 8000, //typ 8, max 64 sec
172         .command_mask = 0x07ff,
173         .id_manufacurer = 0x01,
174         .id_device = 0xa4,
175 //      .productid_check = productid_check,
176         .init = init_erase,
177         .program = program_flash
178 };
179
180 static const struct flash_driver DRIVER_MBM29F080A = {
181         .name = "MBM29F080A",
182         .capacity = 8 * MEGA,
183         .pagesize = 1,
184         .erase_wait = 8000, //chip erase time is not written in datasheet!!
185         .command_mask = 0x07ff,
186         .id_manufacurer = 0x04,
187         .id_device = 0xd5,
188 //      .productid_check = productid_check,
189         .init = init_erase,
190         .program = program_flash
191 };
192
193 static const struct flash_driver *DRIVER_LIST[] = {
194         &DRIVER_W29C020, &DRIVER_W29C040, 
195         &DRIVER_W49F002, &DRIVER_EN29F002T, &DRIVER_AM29F040B, &DRIVER_MBM29F080A,
196         &DRIVER_SRAM, 
197         &DRIVER_DUMMY,
198         NULL
199 };
200
201 const struct flash_driver *flash_driver_get(const char *name)
202 {
203         const struct flash_driver **d;
204         d = DRIVER_LIST;
205         while(*d != NULL){
206                 if(strcmp(name, (*d)->name) == 0){
207                         return *d;
208                 }
209                 d++;
210         }
211         return NULL;
212 }