Diff markup
1 /* 1 /*
2 * Common Flash Interface support: 2 * Common Flash Interface support:
3 * ST Advanced Architecture Command Set (ID 3 * ST Advanced Architecture Command Set (ID 0x0020)
4 * 4 *
5 * (C) 2000 Red Hat. GPL'd 5 * (C) 2000 Red Hat. GPL'd
6 * 6 *
>> 7 * $Id: cfi_cmdset_0020.c,v 1.17 2004/11/20 12:49:04 dwmw2 Exp $
>> 8 *
7 * 10/10/2000 Nicolas Pitre <nico@cam.org> 9 * 10/10/2000 Nicolas Pitre <nico@cam.org>
8 * - completely revamped method functions 10 * - completely revamped method functions so they are aware and
9 * independent of the flash geometry (b 11 * independent of the flash geometry (buswidth, interleave, etc.)
10 * - scalability vs code size is complete 12 * - scalability vs code size is completely set at compile-time
11 * (see include/linux/mtd/cfi.h for sel 13 * (see include/linux/mtd/cfi.h for selection)
12 * - optimized write buffer method 14 * - optimized write buffer method
13 * 06/21/2002 Joern Engel <joern@wh.fh-wedel 15 * 06/21/2002 Joern Engel <joern@wh.fh-wedel.de> and others
14 * - modified Intel Command Set 0x0001 to 16 * - modified Intel Command Set 0x0001 to support ST Advanced Architecture
15 * (command set 0x0020) 17 * (command set 0x0020)
16 * - added a writev function 18 * - added a writev function
17 * 07/13/2005 Joern Engel <joern@wh.fh-wedel <<
18 * - Plugged memory leak in cfi_staa_writ <<
19 */ 19 */
20 20
>> 21 #include <linux/version.h>
21 #include <linux/module.h> 22 #include <linux/module.h>
22 #include <linux/types.h> 23 #include <linux/types.h>
23 #include <linux/kernel.h> 24 #include <linux/kernel.h>
24 #include <linux/sched.h> 25 #include <linux/sched.h>
25 #include <linux/init.h> 26 #include <linux/init.h>
26 #include <asm/io.h> 27 #include <asm/io.h>
27 #include <asm/byteorder.h> 28 #include <asm/byteorder.h>
28 29
29 #include <linux/errno.h> 30 #include <linux/errno.h>
30 #include <linux/slab.h> 31 #include <linux/slab.h>
31 #include <linux/delay.h> 32 #include <linux/delay.h>
32 #include <linux/interrupt.h> 33 #include <linux/interrupt.h>
33 #include <linux/mtd/map.h> 34 #include <linux/mtd/map.h>
34 #include <linux/mtd/cfi.h> 35 #include <linux/mtd/cfi.h>
35 #include <linux/mtd/mtd.h> 36 #include <linux/mtd/mtd.h>
36 #include <linux/mtd/compatmac.h> 37 #include <linux/mtd/compatmac.h>
37 38
38 39
39 static int cfi_staa_read(struct mtd_info *, lo 40 static int cfi_staa_read(struct mtd_info *, loff_t, size_t, size_t *, u_char *);
40 static int cfi_staa_write_buffers(struct mtd_i 41 static int cfi_staa_write_buffers(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
41 static int cfi_staa_writev(struct mtd_info *mt 42 static int cfi_staa_writev(struct mtd_info *mtd, const struct kvec *vecs,
42 unsigned long count, loff_t to 43 unsigned long count, loff_t to, size_t *retlen);
43 static int cfi_staa_erase_varsize(struct mtd_i 44 static int cfi_staa_erase_varsize(struct mtd_info *, struct erase_info *);
44 static void cfi_staa_sync (struct mtd_info *); 45 static void cfi_staa_sync (struct mtd_info *);
45 static int cfi_staa_lock(struct mtd_info *mtd, !! 46 static int cfi_staa_lock(struct mtd_info *mtd, loff_t ofs, size_t len);
46 static int cfi_staa_unlock(struct mtd_info *mt !! 47 static int cfi_staa_unlock(struct mtd_info *mtd, loff_t ofs, size_t len);
47 static int cfi_staa_suspend (struct mtd_info * 48 static int cfi_staa_suspend (struct mtd_info *);
48 static void cfi_staa_resume (struct mtd_info * 49 static void cfi_staa_resume (struct mtd_info *);
49 50
50 static void cfi_staa_destroy(struct mtd_info * 51 static void cfi_staa_destroy(struct mtd_info *);
51 52
52 struct mtd_info *cfi_cmdset_0020(struct map_in 53 struct mtd_info *cfi_cmdset_0020(struct map_info *, int);
53 54
54 static struct mtd_info *cfi_staa_setup (struct 55 static struct mtd_info *cfi_staa_setup (struct map_info *);
55 56
56 static struct mtd_chip_driver cfi_staa_chipdrv 57 static struct mtd_chip_driver cfi_staa_chipdrv = {
57 .probe = NULL, /* Not usable 58 .probe = NULL, /* Not usable directly */
58 .destroy = cfi_staa_destroy, 59 .destroy = cfi_staa_destroy,
59 .name = "cfi_cmdset_0020", 60 .name = "cfi_cmdset_0020",
60 .module = THIS_MODULE 61 .module = THIS_MODULE
61 }; 62 };
62 63
63 /* #define DEBUG_LOCK_BITS */ 64 /* #define DEBUG_LOCK_BITS */
64 //#define DEBUG_CFI_FEATURES 65 //#define DEBUG_CFI_FEATURES
65 66
66 #ifdef DEBUG_CFI_FEATURES 67 #ifdef DEBUG_CFI_FEATURES
67 static void cfi_tell_features(struct cfi_pri_i 68 static void cfi_tell_features(struct cfi_pri_intelext *extp)
68 { 69 {
69 int i; 70 int i;
70 printk(" Feature/Command Support: %4. 71 printk(" Feature/Command Support: %4.4X\n", extp->FeatureSupport);
71 printk(" - Chip Erase: %s\ 72 printk(" - Chip Erase: %s\n", extp->FeatureSupport&1?"supported":"unsupported");
72 printk(" - Suspend Erase: %s\ 73 printk(" - Suspend Erase: %s\n", extp->FeatureSupport&2?"supported":"unsupported");
73 printk(" - Suspend Program: %s\ 74 printk(" - Suspend Program: %s\n", extp->FeatureSupport&4?"supported":"unsupported");
74 printk(" - Legacy Lock/Unlock: %s\ 75 printk(" - Legacy Lock/Unlock: %s\n", extp->FeatureSupport&8?"supported":"unsupported");
75 printk(" - Queued Erase: %s\ 76 printk(" - Queued Erase: %s\n", extp->FeatureSupport&16?"supported":"unsupported");
76 printk(" - Instant block lock: %s\ 77 printk(" - Instant block lock: %s\n", extp->FeatureSupport&32?"supported":"unsupported");
77 printk(" - Protection Bits: %s\ 78 printk(" - Protection Bits: %s\n", extp->FeatureSupport&64?"supported":"unsupported");
78 printk(" - Page-mode read: %s\ 79 printk(" - Page-mode read: %s\n", extp->FeatureSupport&128?"supported":"unsupported");
79 printk(" - Synchronous read: %s\ 80 printk(" - Synchronous read: %s\n", extp->FeatureSupport&256?"supported":"unsupported");
80 for (i=9; i<32; i++) { 81 for (i=9; i<32; i++) {
81 if (extp->FeatureSupport & (1< !! 82 if (extp->FeatureSupport & (1<<i))
82 printk(" - Unknown 83 printk(" - Unknown Bit %X: supported\n", i);
83 } 84 }
84 !! 85
85 printk(" Supported functions after Su 86 printk(" Supported functions after Suspend: %2.2X\n", extp->SuspendCmdSupport);
86 printk(" - Program after Erase Sus 87 printk(" - Program after Erase Suspend: %s\n", extp->SuspendCmdSupport&1?"supported":"unsupported");
87 for (i=1; i<8; i++) { 88 for (i=1; i<8; i++) {
88 if (extp->SuspendCmdSupport & 89 if (extp->SuspendCmdSupport & (1<<i))
89 printk(" - Unknown 90 printk(" - Unknown Bit %X: supported\n", i);
90 } 91 }
91 !! 92
92 printk(" Block Status Register Mask: 93 printk(" Block Status Register Mask: %4.4X\n", extp->BlkStatusRegMask);
93 printk(" - Lock Bit Active: % 94 printk(" - Lock Bit Active: %s\n", extp->BlkStatusRegMask&1?"yes":"no");
94 printk(" - Valid Bit Active: % 95 printk(" - Valid Bit Active: %s\n", extp->BlkStatusRegMask&2?"yes":"no");
95 for (i=2; i<16; i++) { 96 for (i=2; i<16; i++) {
96 if (extp->BlkStatusRegMask & ( 97 if (extp->BlkStatusRegMask & (1<<i))
97 printk(" - Unknown 98 printk(" - Unknown Bit %X Active: yes\n",i);
98 } 99 }
99 !! 100
100 printk(" Vcc Logic Supply Optimum Pro !! 101 printk(" Vcc Logic Supply Optimum Program/Erase Voltage: %d.%d V\n",
101 extp->VccOptimal >> 8, extp->Vc 102 extp->VccOptimal >> 8, extp->VccOptimal & 0xf);
102 if (extp->VppOptimal) 103 if (extp->VppOptimal)
103 printk(" Vpp Programming Supp !! 104 printk(" Vpp Programming Supply Optimum Program/Erase Voltage: %d.%d V\n",
104 extp->VppOptimal >> 8, 105 extp->VppOptimal >> 8, extp->VppOptimal & 0xf);
105 } 106 }
106 #endif 107 #endif
107 108
108 /* This routine is made available to other mtd 109 /* This routine is made available to other mtd code via
109 * inter_module_register. It must only be acc 110 * inter_module_register. It must only be accessed through
110 * inter_module_get which will bump the use co 111 * inter_module_get which will bump the use count of this module. The
111 * addresses passed back in cfi are valid as l 112 * addresses passed back in cfi are valid as long as the use count of
112 * this module is non-zero, i.e. between inter 113 * this module is non-zero, i.e. between inter_module_get and
113 * inter_module_put. Keith Owens <kaos@ocs.co 114 * inter_module_put. Keith Owens <kaos@ocs.com.au> 29 Oct 2000.
114 */ 115 */
115 struct mtd_info *cfi_cmdset_0020(struct map_in 116 struct mtd_info *cfi_cmdset_0020(struct map_info *map, int primary)
116 { 117 {
117 struct cfi_private *cfi = map->fldrv_p 118 struct cfi_private *cfi = map->fldrv_priv;
118 int i; 119 int i;
119 120
120 if (cfi->cfi_mode) { 121 if (cfi->cfi_mode) {
121 /* !! 122 /*
122 * It's a real CFI chip, not o 123 * It's a real CFI chip, not one for which the probe
123 * routine faked a CFI structu 124 * routine faked a CFI structure. So we read the feature
124 * table from it. 125 * table from it.
125 */ 126 */
126 __u16 adr = primary?cfi->cfiq- 127 __u16 adr = primary?cfi->cfiq->P_ADR:cfi->cfiq->A_ADR;
127 struct cfi_pri_intelext *extp; 128 struct cfi_pri_intelext *extp;
128 129
129 extp = (struct cfi_pri_intelex 130 extp = (struct cfi_pri_intelext*)cfi_read_pri(map, adr, sizeof(*extp), "ST Microelectronics");
130 if (!extp) 131 if (!extp)
131 return NULL; 132 return NULL;
132 133
133 if (extp->MajorVersion != '1' <<
134 (extp->MinorVersion < '' | <<
135 printk(KERN_ERR " Unk <<
136 " Extended Quer <<
137 extp->MajorVers <<
138 kfree(extp); <<
139 return NULL; <<
140 } <<
141 <<
142 /* Do some byteswapping if nec 134 /* Do some byteswapping if necessary */
143 extp->FeatureSupport = cfi32_t 135 extp->FeatureSupport = cfi32_to_cpu(extp->FeatureSupport);
144 extp->BlkStatusRegMask = cfi32 136 extp->BlkStatusRegMask = cfi32_to_cpu(extp->BlkStatusRegMask);
145 !! 137
146 #ifdef DEBUG_CFI_FEATURES 138 #ifdef DEBUG_CFI_FEATURES
147 /* Tell the user about it in l 139 /* Tell the user about it in lots of lovely detail */
148 cfi_tell_features(extp); 140 cfi_tell_features(extp);
149 #endif !! 141 #endif
150 142
151 /* Install our own private inf 143 /* Install our own private info structure */
152 cfi->cmdset_priv = extp; 144 cfi->cmdset_priv = extp;
153 } !! 145 }
154 146
155 for (i=0; i< cfi->numchips; i++) { 147 for (i=0; i< cfi->numchips; i++) {
156 cfi->chips[i].word_write_time 148 cfi->chips[i].word_write_time = 128;
157 cfi->chips[i].buffer_write_tim 149 cfi->chips[i].buffer_write_time = 128;
158 cfi->chips[i].erase_time = 102 150 cfi->chips[i].erase_time = 1024;
159 cfi->chips[i].ref_point_counte !! 151 }
160 init_waitqueue_head(&(cfi->chi <<
161 } <<
162 152
163 return cfi_staa_setup(map); 153 return cfi_staa_setup(map);
164 } 154 }
165 EXPORT_SYMBOL_GPL(cfi_cmdset_0020); <<
166 155
167 static struct mtd_info *cfi_staa_setup(struct 156 static struct mtd_info *cfi_staa_setup(struct map_info *map)
168 { 157 {
169 struct cfi_private *cfi = map->fldrv_p 158 struct cfi_private *cfi = map->fldrv_priv;
170 struct mtd_info *mtd; 159 struct mtd_info *mtd;
171 unsigned long offset = 0; 160 unsigned long offset = 0;
172 int i,j; 161 int i,j;
173 unsigned long devsize = (1<<cfi->cfiq- 162 unsigned long devsize = (1<<cfi->cfiq->DevSize) * cfi->interleave;
174 163
175 mtd = kzalloc(sizeof(*mtd), GFP_KERNEL !! 164 mtd = kmalloc(sizeof(*mtd), GFP_KERNEL);
176 //printk(KERN_DEBUG "number of CFI chi 165 //printk(KERN_DEBUG "number of CFI chips: %d\n", cfi->numchips);
177 166
178 if (!mtd) { 167 if (!mtd) {
179 printk(KERN_ERR "Failed to all 168 printk(KERN_ERR "Failed to allocate memory for MTD device\n");
180 kfree(cfi->cmdset_priv); 169 kfree(cfi->cmdset_priv);
181 return NULL; 170 return NULL;
182 } 171 }
183 172
>> 173 memset(mtd, 0, sizeof(*mtd));
184 mtd->priv = map; 174 mtd->priv = map;
185 mtd->type = MTD_NORFLASH; 175 mtd->type = MTD_NORFLASH;
186 mtd->size = devsize * cfi->numchips; 176 mtd->size = devsize * cfi->numchips;
187 177
188 mtd->numeraseregions = cfi->cfiq->NumE 178 mtd->numeraseregions = cfi->cfiq->NumEraseRegions * cfi->numchips;
189 mtd->eraseregions = kmalloc(sizeof(str !! 179 mtd->eraseregions = kmalloc(sizeof(struct mtd_erase_region_info)
190 * mtd->numeraseregions 180 * mtd->numeraseregions, GFP_KERNEL);
191 if (!mtd->eraseregions) { !! 181 if (!mtd->eraseregions) {
192 printk(KERN_ERR "Failed to all 182 printk(KERN_ERR "Failed to allocate memory for MTD erase region info\n");
193 kfree(cfi->cmdset_priv); 183 kfree(cfi->cmdset_priv);
194 kfree(mtd); 184 kfree(mtd);
195 return NULL; 185 return NULL;
196 } 186 }
197 !! 187
198 for (i=0; i<cfi->cfiq->NumEraseRegions 188 for (i=0; i<cfi->cfiq->NumEraseRegions; i++) {
199 unsigned long ernum, ersize; 189 unsigned long ernum, ersize;
200 ersize = ((cfi->cfiq->EraseReg 190 ersize = ((cfi->cfiq->EraseRegionInfo[i] >> 8) & ~0xff) * cfi->interleave;
201 ernum = (cfi->cfiq->EraseRegio 191 ernum = (cfi->cfiq->EraseRegionInfo[i] & 0xffff) + 1;
202 192
203 if (mtd->erasesize < ersize) { 193 if (mtd->erasesize < ersize) {
204 mtd->erasesize = ersiz 194 mtd->erasesize = ersize;
205 } 195 }
206 for (j=0; j<cfi->numchips; j++ 196 for (j=0; j<cfi->numchips; j++) {
207 mtd->eraseregions[(j*c 197 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].offset = (j*devsize)+offset;
208 mtd->eraseregions[(j*c 198 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].erasesize = ersize;
209 mtd->eraseregions[(j*c 199 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].numblocks = ernum;
210 } 200 }
211 offset += (ersize * ernum); 201 offset += (ersize * ernum);
212 } 202 }
213 203
214 if (offset != devsize) { 204 if (offset != devsize) {
215 /* Argh */ 205 /* Argh */
216 printk(KERN_WARNING "S 206 printk(KERN_WARNING "Sum of regions (%lx) != total size of set of interleaved chips (%lx)\n", offset, devsize);
217 kfree(mtd->eraseregion 207 kfree(mtd->eraseregions);
218 kfree(cfi->cmdset_priv 208 kfree(cfi->cmdset_priv);
219 kfree(mtd); 209 kfree(mtd);
220 return NULL; 210 return NULL;
221 } 211 }
222 212
223 for (i=0; i<mtd->numeraseregio 213 for (i=0; i<mtd->numeraseregions;i++){
224 printk(KERN_DEBUG "%d: !! 214 printk(KERN_DEBUG "%d: offset=0x%x,size=0x%x,blocks=%d\n",
225 i, (unsigned lo !! 215 i,mtd->eraseregions[i].offset,
226 mtd->eraseregio 216 mtd->eraseregions[i].erasesize,
227 mtd->eraseregio 217 mtd->eraseregions[i].numblocks);
228 } 218 }
229 219
230 /* Also select the correct geometry se !! 220 /* Also select the correct geometry setup too */
231 mtd->erase = cfi_staa_erase_varsize; 221 mtd->erase = cfi_staa_erase_varsize;
232 mtd->read = cfi_staa_read; 222 mtd->read = cfi_staa_read;
233 mtd->write = cfi_staa_write_buffers; 223 mtd->write = cfi_staa_write_buffers;
234 mtd->writev = cfi_staa_writev; 224 mtd->writev = cfi_staa_writev;
235 mtd->sync = cfi_staa_sync; 225 mtd->sync = cfi_staa_sync;
236 mtd->lock = cfi_staa_lock; 226 mtd->lock = cfi_staa_lock;
237 mtd->unlock = cfi_staa_unlock; 227 mtd->unlock = cfi_staa_unlock;
238 mtd->suspend = cfi_staa_suspend; 228 mtd->suspend = cfi_staa_suspend;
239 mtd->resume = cfi_staa_resume; 229 mtd->resume = cfi_staa_resume;
240 mtd->flags = MTD_CAP_NORFLASH & ~MTD_B !! 230 mtd->flags = MTD_CAP_NORFLASH;
241 mtd->writesize = 8; /* FIXME: Should b !! 231 mtd->flags |= MTD_ECC; /* FIXME: Not all STMicro flashes have this */
>> 232 mtd->eccsize = 8; /* FIXME: Should be 0 for STMicro flashes w/out ECC */
242 map->fldrv = &cfi_staa_chipdrv; 233 map->fldrv = &cfi_staa_chipdrv;
243 __module_get(THIS_MODULE); 234 __module_get(THIS_MODULE);
244 mtd->name = map->name; 235 mtd->name = map->name;
245 return mtd; 236 return mtd;
246 } 237 }
247 238
248 239
249 static inline int do_read_onechip(struct map_i 240 static inline int do_read_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf)
250 { 241 {
251 map_word status, status_OK; 242 map_word status, status_OK;
252 unsigned long timeo; 243 unsigned long timeo;
253 DECLARE_WAITQUEUE(wait, current); 244 DECLARE_WAITQUEUE(wait, current);
254 int suspended = 0; 245 int suspended = 0;
255 unsigned long cmd_addr; 246 unsigned long cmd_addr;
256 struct cfi_private *cfi = map->fldrv_p 247 struct cfi_private *cfi = map->fldrv_priv;
257 248
258 adr += chip->start; 249 adr += chip->start;
259 250
260 /* Ensure cmd read/writes are aligned. !! 251 /* Ensure cmd read/writes are aligned. */
261 cmd_addr = adr & ~(map_bankwidth(map)- !! 252 cmd_addr = adr & ~(map_bankwidth(map)-1);
262 253
263 /* Let's determine this according to t 254 /* Let's determine this according to the interleave only once */
264 status_OK = CMD(0x80); 255 status_OK = CMD(0x80);
265 256
266 timeo = jiffies + HZ; 257 timeo = jiffies + HZ;
267 retry: 258 retry:
268 spin_lock_bh(chip->mutex); 259 spin_lock_bh(chip->mutex);
269 260
270 /* Check that the chip's ready to talk 261 /* Check that the chip's ready to talk to us.
271 * If it's in FL_ERASING state, suspen 262 * If it's in FL_ERASING state, suspend it and make it talk now.
272 */ 263 */
273 switch (chip->state) { 264 switch (chip->state) {
274 case FL_ERASING: 265 case FL_ERASING:
275 if (!(((struct cfi_pri_intelex 266 if (!(((struct cfi_pri_intelext *)cfi->cmdset_priv)->FeatureSupport & 2))
276 goto sleep; /* We don' 267 goto sleep; /* We don't support erase suspend */
277 !! 268
278 map_write (map, CMD(0xb0), cmd 269 map_write (map, CMD(0xb0), cmd_addr);
279 /* If the flash has finished e 270 /* If the flash has finished erasing, then 'erase suspend'
280 * appears to make some (28F32 271 * appears to make some (28F320) flash devices switch to
281 * 'read' mode. Make sure tha 272 * 'read' mode. Make sure that we switch to 'read status'
282 * mode so we get the right da 273 * mode so we get the right data. --rmk
283 */ 274 */
284 map_write(map, CMD(0x70), cmd_ 275 map_write(map, CMD(0x70), cmd_addr);
285 chip->oldstate = FL_ERASING; 276 chip->oldstate = FL_ERASING;
286 chip->state = FL_ERASE_SUSPEND 277 chip->state = FL_ERASE_SUSPENDING;
287 // printk("Erase 278 // printk("Erase suspending at 0x%lx\n", cmd_addr);
288 for (;;) { 279 for (;;) {
289 status = map_read(map, 280 status = map_read(map, cmd_addr);
290 if (map_word_andequal( 281 if (map_word_andequal(map, status, status_OK, status_OK))
291 break; 282 break;
292 !! 283
293 if (time_after(jiffies 284 if (time_after(jiffies, timeo)) {
294 /* Urgh */ 285 /* Urgh */
295 map_write(map, 286 map_write(map, CMD(0xd0), cmd_addr);
296 /* make sure w 287 /* make sure we're in 'read status' mode */
297 map_write(map, 288 map_write(map, CMD(0x70), cmd_addr);
298 chip->state = 289 chip->state = FL_ERASING;
299 spin_unlock_bh 290 spin_unlock_bh(chip->mutex);
300 printk(KERN_ER 291 printk(KERN_ERR "Chip not ready after erase "
301 "suspen 292 "suspended: status = 0x%lx\n", status.x[0]);
302 return -EIO; 293 return -EIO;
303 } 294 }
304 !! 295
305 spin_unlock_bh(chip->m 296 spin_unlock_bh(chip->mutex);
306 cfi_udelay(1); 297 cfi_udelay(1);
307 spin_lock_bh(chip->mut 298 spin_lock_bh(chip->mutex);
308 } 299 }
309 !! 300
310 suspended = 1; 301 suspended = 1;
311 map_write(map, CMD(0xff), cmd_ 302 map_write(map, CMD(0xff), cmd_addr);
312 chip->state = FL_READY; 303 chip->state = FL_READY;
313 break; 304 break;
314 !! 305
315 #if 0 306 #if 0
316 case FL_WRITING: 307 case FL_WRITING:
317 /* Not quite yet */ 308 /* Not quite yet */
318 #endif 309 #endif
319 310
320 case FL_READY: 311 case FL_READY:
321 break; 312 break;
322 313
323 case FL_CFI_QUERY: 314 case FL_CFI_QUERY:
324 case FL_JEDEC_QUERY: 315 case FL_JEDEC_QUERY:
325 map_write(map, CMD(0x70), cmd_ 316 map_write(map, CMD(0x70), cmd_addr);
326 chip->state = FL_STATUS; 317 chip->state = FL_STATUS;
327 318
328 case FL_STATUS: 319 case FL_STATUS:
329 status = map_read(map, cmd_add 320 status = map_read(map, cmd_addr);
330 if (map_word_andequal(map, sta 321 if (map_word_andequal(map, status, status_OK, status_OK)) {
331 map_write(map, CMD(0xf 322 map_write(map, CMD(0xff), cmd_addr);
332 chip->state = FL_READY 323 chip->state = FL_READY;
333 break; 324 break;
334 } 325 }
335 !! 326
336 /* Urgh. Chip not yet ready to 327 /* Urgh. Chip not yet ready to talk to us. */
337 if (time_after(jiffies, timeo) 328 if (time_after(jiffies, timeo)) {
338 spin_unlock_bh(chip->m 329 spin_unlock_bh(chip->mutex);
339 printk(KERN_ERR "waiti 330 printk(KERN_ERR "waiting for chip to be ready timed out in read. WSM status = %lx\n", status.x[0]);
340 return -EIO; 331 return -EIO;
341 } 332 }
342 333
343 /* Latency issues. Drop the lo 334 /* Latency issues. Drop the lock, wait a while and retry */
344 spin_unlock_bh(chip->mutex); 335 spin_unlock_bh(chip->mutex);
345 cfi_udelay(1); 336 cfi_udelay(1);
346 goto retry; 337 goto retry;
347 338
348 default: 339 default:
349 sleep: 340 sleep:
350 /* Stick ourselves on a wait q 341 /* Stick ourselves on a wait queue to be woken when
351 someone changes the status 342 someone changes the status */
352 set_current_state(TASK_UNINTER 343 set_current_state(TASK_UNINTERRUPTIBLE);
353 add_wait_queue(&chip->wq, &wai 344 add_wait_queue(&chip->wq, &wait);
354 spin_unlock_bh(chip->mutex); 345 spin_unlock_bh(chip->mutex);
355 schedule(); 346 schedule();
356 remove_wait_queue(&chip->wq, & 347 remove_wait_queue(&chip->wq, &wait);
357 timeo = jiffies + HZ; 348 timeo = jiffies + HZ;
358 goto retry; 349 goto retry;
359 } 350 }
360 351
361 map_copy_from(map, buf, adr, len); 352 map_copy_from(map, buf, adr, len);
362 353
363 if (suspended) { 354 if (suspended) {
364 chip->state = chip->oldstate; 355 chip->state = chip->oldstate;
365 /* What if one interleaved chi !! 356 /* What if one interleaved chip has finished and the
366 other hasn't? The old code 357 other hasn't? The old code would leave the finished
367 one in READY mode. That's b !! 358 one in READY mode. That's bad, and caused -EROFS
368 errors to be returned from 359 errors to be returned from do_erase_oneblock because
369 that's the only bit it chec 360 that's the only bit it checked for at the time.
370 As the state machine appear !! 361 As the state machine appears to explicitly allow
371 sending the 0x70 (Read Stat 362 sending the 0x70 (Read Status) command to an erasing
372 chip and expecting it to be !! 363 chip and expecting it to be ignored, that's what we
373 do. */ 364 do. */
374 map_write(map, CMD(0xd0), cmd_ 365 map_write(map, CMD(0xd0), cmd_addr);
375 map_write(map, CMD(0x70), cmd_ !! 366 map_write(map, CMD(0x70), cmd_addr);
376 } 367 }
377 368
378 wake_up(&chip->wq); 369 wake_up(&chip->wq);
379 spin_unlock_bh(chip->mutex); 370 spin_unlock_bh(chip->mutex);
380 return 0; 371 return 0;
381 } 372 }
382 373
383 static int cfi_staa_read (struct mtd_info *mtd 374 static int cfi_staa_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
384 { 375 {
385 struct map_info *map = mtd->priv; 376 struct map_info *map = mtd->priv;
386 struct cfi_private *cfi = map->fldrv_p 377 struct cfi_private *cfi = map->fldrv_priv;
387 unsigned long ofs; 378 unsigned long ofs;
388 int chipnum; 379 int chipnum;
389 int ret = 0; 380 int ret = 0;
390 381
391 /* ofs: offset within the first chip t 382 /* ofs: offset within the first chip that the first read should start */
392 chipnum = (from >> cfi->chipshift); 383 chipnum = (from >> cfi->chipshift);
393 ofs = from - (chipnum << cfi->chipshi 384 ofs = from - (chipnum << cfi->chipshift);
394 385
395 *retlen = 0; 386 *retlen = 0;
396 387
397 while (len) { 388 while (len) {
398 unsigned long thislen; 389 unsigned long thislen;
399 390
400 if (chipnum >= cfi->numchips) 391 if (chipnum >= cfi->numchips)
401 break; 392 break;
402 393
403 if ((len + ofs -1) >> cfi->chi 394 if ((len + ofs -1) >> cfi->chipshift)
404 thislen = (1<<cfi->chi 395 thislen = (1<<cfi->chipshift) - ofs;
405 else 396 else
406 thislen = len; 397 thislen = len;
407 398
408 ret = do_read_onechip(map, &cf 399 ret = do_read_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf);
409 if (ret) 400 if (ret)
410 break; 401 break;
411 402
412 *retlen += thislen; 403 *retlen += thislen;
413 len -= thislen; 404 len -= thislen;
414 buf += thislen; 405 buf += thislen;
415 !! 406
416 ofs = 0; 407 ofs = 0;
417 chipnum++; 408 chipnum++;
418 } 409 }
419 return ret; 410 return ret;
420 } 411 }
421 412
422 static inline int do_write_buffer(struct map_i !! 413 static inline int do_write_buffer(struct map_info *map, struct flchip *chip,
423 unsigned lon 414 unsigned long adr, const u_char *buf, int len)
424 { 415 {
425 struct cfi_private *cfi = map->fldrv_p 416 struct cfi_private *cfi = map->fldrv_priv;
426 map_word status, status_OK; 417 map_word status, status_OK;
427 unsigned long cmd_adr, timeo; 418 unsigned long cmd_adr, timeo;
428 DECLARE_WAITQUEUE(wait, current); 419 DECLARE_WAITQUEUE(wait, current);
429 int wbufsize, z; 420 int wbufsize, z;
430 !! 421
431 /* M58LW064A requires bus alignment fo 422 /* M58LW064A requires bus alignment for buffer wriets -- saw */
432 if (adr & (map_bankwidth(map)-1)) 423 if (adr & (map_bankwidth(map)-1))
433 return -EINVAL; 424 return -EINVAL;
434 425
435 wbufsize = cfi_interleave(cfi) << cfi- 426 wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
436 adr += chip->start; 427 adr += chip->start;
437 cmd_adr = adr & ~(wbufsize-1); 428 cmd_adr = adr & ~(wbufsize-1);
438 !! 429
439 /* Let's determine this according to t 430 /* Let's determine this according to the interleave only once */
440 status_OK = CMD(0x80); 431 status_OK = CMD(0x80);
441 !! 432
442 timeo = jiffies + HZ; 433 timeo = jiffies + HZ;
443 retry: 434 retry:
444 435
445 #ifdef DEBUG_CFI_FEATURES 436 #ifdef DEBUG_CFI_FEATURES
446 printk("%s: chip->state[%d]\n", __func_ !! 437 printk("%s: chip->state[%d]\n", __FUNCTION__, chip->state);
447 #endif 438 #endif
448 spin_lock_bh(chip->mutex); 439 spin_lock_bh(chip->mutex);
449 !! 440
450 /* Check that the chip's ready to talk 441 /* Check that the chip's ready to talk to us.
451 * Later, we can actually think about 442 * Later, we can actually think about interrupting it
452 * if it's in FL_ERASING state. 443 * if it's in FL_ERASING state.
453 * Not just yet, though. 444 * Not just yet, though.
454 */ 445 */
455 switch (chip->state) { 446 switch (chip->state) {
456 case FL_READY: 447 case FL_READY:
457 break; 448 break;
458 !! 449
459 case FL_CFI_QUERY: 450 case FL_CFI_QUERY:
460 case FL_JEDEC_QUERY: 451 case FL_JEDEC_QUERY:
461 map_write(map, CMD(0x70), cmd_ 452 map_write(map, CMD(0x70), cmd_adr);
462 chip->state = FL_STATUS; 453 chip->state = FL_STATUS;
463 #ifdef DEBUG_CFI_FEATURES 454 #ifdef DEBUG_CFI_FEATURES
464 printk("%s: 1 status[%x]\n", __func__, !! 455 printk("%s: 1 status[%x]\n", __FUNCTION__, map_read(map, cmd_adr));
465 #endif 456 #endif
466 457
467 case FL_STATUS: 458 case FL_STATUS:
468 status = map_read(map, cmd_adr 459 status = map_read(map, cmd_adr);
469 if (map_word_andequal(map, sta 460 if (map_word_andequal(map, status, status_OK, status_OK))
470 break; 461 break;
471 /* Urgh. Chip not yet ready to 462 /* Urgh. Chip not yet ready to talk to us. */
472 if (time_after(jiffies, timeo) 463 if (time_after(jiffies, timeo)) {
473 spin_unlock_bh(chip->m 464 spin_unlock_bh(chip->mutex);
474 printk(KERN_ERR "waiti 465 printk(KERN_ERR "waiting for chip to be ready timed out in buffer write Xstatus = %lx, status = %lx\n",
475 status.x[0], ma 466 status.x[0], map_read(map, cmd_adr).x[0]);
476 return -EIO; 467 return -EIO;
477 } 468 }
478 469
479 /* Latency issues. Drop the lo 470 /* Latency issues. Drop the lock, wait a while and retry */
480 spin_unlock_bh(chip->mutex); 471 spin_unlock_bh(chip->mutex);
481 cfi_udelay(1); 472 cfi_udelay(1);
482 goto retry; 473 goto retry;
483 474
484 default: 475 default:
485 /* Stick ourselves on a wait q 476 /* Stick ourselves on a wait queue to be woken when
486 someone changes the status 477 someone changes the status */
487 set_current_state(TASK_UNINTER 478 set_current_state(TASK_UNINTERRUPTIBLE);
488 add_wait_queue(&chip->wq, &wai 479 add_wait_queue(&chip->wq, &wait);
489 spin_unlock_bh(chip->mutex); 480 spin_unlock_bh(chip->mutex);
490 schedule(); 481 schedule();
491 remove_wait_queue(&chip->wq, & 482 remove_wait_queue(&chip->wq, &wait);
492 timeo = jiffies + HZ; 483 timeo = jiffies + HZ;
493 goto retry; 484 goto retry;
494 } 485 }
495 486
496 ENABLE_VPP(map); 487 ENABLE_VPP(map);
497 map_write(map, CMD(0xe8), cmd_adr); 488 map_write(map, CMD(0xe8), cmd_adr);
498 chip->state = FL_WRITING_TO_BUFFER; 489 chip->state = FL_WRITING_TO_BUFFER;
499 490
500 z = 0; 491 z = 0;
501 for (;;) { 492 for (;;) {
502 status = map_read(map, cmd_adr 493 status = map_read(map, cmd_adr);
503 if (map_word_andequal(map, sta 494 if (map_word_andequal(map, status, status_OK, status_OK))
504 break; 495 break;
505 496
506 spin_unlock_bh(chip->mutex); 497 spin_unlock_bh(chip->mutex);
507 cfi_udelay(1); 498 cfi_udelay(1);
508 spin_lock_bh(chip->mutex); 499 spin_lock_bh(chip->mutex);
509 500
510 if (++z > 100) { 501 if (++z > 100) {
511 /* Argh. Not ready for 502 /* Argh. Not ready for write to buffer */
512 DISABLE_VPP(map); 503 DISABLE_VPP(map);
513 map_write(map, CMD(0x7 504 map_write(map, CMD(0x70), cmd_adr);
514 chip->state = FL_STATU 505 chip->state = FL_STATUS;
515 spin_unlock_bh(chip->m 506 spin_unlock_bh(chip->mutex);
516 printk(KERN_ERR "Chip 507 printk(KERN_ERR "Chip not ready for buffer write. Xstatus = %lx\n", status.x[0]);
517 return -EIO; 508 return -EIO;
518 } 509 }
519 } 510 }
520 511
521 /* Write length of data to come */ 512 /* Write length of data to come */
522 map_write(map, CMD(len/map_bankwidth(m 513 map_write(map, CMD(len/map_bankwidth(map)-1), cmd_adr );
523 !! 514
524 /* Write data */ 515 /* Write data */
525 for (z = 0; z < len; 516 for (z = 0; z < len;
526 z += map_bankwidth(map), buf += m 517 z += map_bankwidth(map), buf += map_bankwidth(map)) {
527 map_word d; 518 map_word d;
528 d = map_word_load(map, buf); 519 d = map_word_load(map, buf);
529 map_write(map, d, adr+z); 520 map_write(map, d, adr+z);
530 } 521 }
531 /* GO GO GO */ 522 /* GO GO GO */
532 map_write(map, CMD(0xd0), cmd_adr); 523 map_write(map, CMD(0xd0), cmd_adr);
533 chip->state = FL_WRITING; 524 chip->state = FL_WRITING;
534 525
535 spin_unlock_bh(chip->mutex); 526 spin_unlock_bh(chip->mutex);
536 cfi_udelay(chip->buffer_write_time); 527 cfi_udelay(chip->buffer_write_time);
537 spin_lock_bh(chip->mutex); 528 spin_lock_bh(chip->mutex);
538 529
539 timeo = jiffies + (HZ/2); 530 timeo = jiffies + (HZ/2);
540 z = 0; 531 z = 0;
541 for (;;) { 532 for (;;) {
542 if (chip->state != FL_WRITING) 533 if (chip->state != FL_WRITING) {
543 /* Someone's suspended 534 /* Someone's suspended the write. Sleep */
544 set_current_state(TASK 535 set_current_state(TASK_UNINTERRUPTIBLE);
545 add_wait_queue(&chip-> 536 add_wait_queue(&chip->wq, &wait);
546 spin_unlock_bh(chip->m 537 spin_unlock_bh(chip->mutex);
547 schedule(); 538 schedule();
548 remove_wait_queue(&chi 539 remove_wait_queue(&chip->wq, &wait);
549 timeo = jiffies + (HZ 540 timeo = jiffies + (HZ / 2); /* FIXME */
550 spin_lock_bh(chip->mut 541 spin_lock_bh(chip->mutex);
551 continue; 542 continue;
552 } 543 }
553 544
554 status = map_read(map, cmd_adr 545 status = map_read(map, cmd_adr);
555 if (map_word_andequal(map, sta 546 if (map_word_andequal(map, status, status_OK, status_OK))
556 break; 547 break;
557 548
558 /* OK Still waiting */ 549 /* OK Still waiting */
559 if (time_after(jiffies, timeo) 550 if (time_after(jiffies, timeo)) {
560 /* clear status */ 551 /* clear status */
561 map_write(map, CMD(0x5 552 map_write(map, CMD(0x50), cmd_adr);
562 /* put back into read 553 /* put back into read status register mode */
563 map_write(map, CMD(0x7 554 map_write(map, CMD(0x70), adr);
564 chip->state = FL_STATU 555 chip->state = FL_STATUS;
565 DISABLE_VPP(map); 556 DISABLE_VPP(map);
566 spin_unlock_bh(chip->m 557 spin_unlock_bh(chip->mutex);
567 printk(KERN_ERR "waiti 558 printk(KERN_ERR "waiting for chip to be ready timed out in bufwrite\n");
568 return -EIO; 559 return -EIO;
569 } 560 }
570 !! 561
571 /* Latency issues. Drop the lo 562 /* Latency issues. Drop the lock, wait a while and retry */
572 spin_unlock_bh(chip->mutex); 563 spin_unlock_bh(chip->mutex);
573 cfi_udelay(1); 564 cfi_udelay(1);
574 z++; 565 z++;
575 spin_lock_bh(chip->mutex); 566 spin_lock_bh(chip->mutex);
576 } 567 }
577 if (!z) { 568 if (!z) {
578 chip->buffer_write_time--; 569 chip->buffer_write_time--;
579 if (!chip->buffer_write_time) 570 if (!chip->buffer_write_time)
580 chip->buffer_write_tim 571 chip->buffer_write_time++;
581 } 572 }
582 if (z > 1) !! 573 if (z > 1)
583 chip->buffer_write_time++; 574 chip->buffer_write_time++;
584 !! 575
585 /* Done and happy. */ 576 /* Done and happy. */
586 DISABLE_VPP(map); 577 DISABLE_VPP(map);
587 chip->state = FL_STATUS; 578 chip->state = FL_STATUS;
588 579
589 /* check for errors: 'lock bit', 'VPP' 580 /* check for errors: 'lock bit', 'VPP', 'dead cell'/'unerased cell' or 'incorrect cmd' -- saw */
590 if (map_word_bitsset(map, status, CMD( 581 if (map_word_bitsset(map, status, CMD(0x3a))) {
591 #ifdef DEBUG_CFI_FEATURES 582 #ifdef DEBUG_CFI_FEATURES
592 printk("%s: 2 status[%lx]\n", !! 583 printk("%s: 2 status[%lx]\n", __FUNCTION__, status.x[0]);
593 #endif 584 #endif
594 /* clear status */ 585 /* clear status */
595 map_write(map, CMD(0x50), cmd_ 586 map_write(map, CMD(0x50), cmd_adr);
596 /* put back into read status r 587 /* put back into read status register mode */
597 map_write(map, CMD(0x70), adr) 588 map_write(map, CMD(0x70), adr);
598 wake_up(&chip->wq); 589 wake_up(&chip->wq);
599 spin_unlock_bh(chip->mutex); 590 spin_unlock_bh(chip->mutex);
600 return map_word_bitsset(map, s 591 return map_word_bitsset(map, status, CMD(0x02)) ? -EROFS : -EIO;
601 } 592 }
602 wake_up(&chip->wq); 593 wake_up(&chip->wq);
603 spin_unlock_bh(chip->mutex); 594 spin_unlock_bh(chip->mutex);
604 595
605 return 0; 596 return 0;
606 } 597 }
607 598
608 static int cfi_staa_write_buffers (struct mtd_ !! 599 static int cfi_staa_write_buffers (struct mtd_info *mtd, loff_t to,
609 size_t 600 size_t len, size_t *retlen, const u_char *buf)
610 { 601 {
611 struct map_info *map = mtd->priv; 602 struct map_info *map = mtd->priv;
612 struct cfi_private *cfi = map->fldrv_p 603 struct cfi_private *cfi = map->fldrv_priv;
613 int wbufsize = cfi_interleave(cfi) << 604 int wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
614 int ret = 0; 605 int ret = 0;
615 int chipnum; 606 int chipnum;
616 unsigned long ofs; 607 unsigned long ofs;
617 608
618 *retlen = 0; 609 *retlen = 0;
619 if (!len) 610 if (!len)
620 return 0; 611 return 0;
621 612
622 chipnum = to >> cfi->chipshift; 613 chipnum = to >> cfi->chipshift;
623 ofs = to - (chipnum << cfi->chipshift 614 ofs = to - (chipnum << cfi->chipshift);
624 615
625 #ifdef DEBUG_CFI_FEATURES 616 #ifdef DEBUG_CFI_FEATURES
626 printk("%s: map_bankwidth(map)[%x]\n", !! 617 printk("%s: map_bankwidth(map)[%x]\n", __FUNCTION__, map_bankwidth(map));
627 printk("%s: chipnum[%x] wbufsize[%x]\n !! 618 printk("%s: chipnum[%x] wbufsize[%x]\n", __FUNCTION__, chipnum, wbufsize);
628 printk("%s: ofs[%x] len[%x]\n", __func !! 619 printk("%s: ofs[%x] len[%x]\n", __FUNCTION__, ofs, len);
629 #endif 620 #endif
630 !! 621
631 /* Write buffer is worth it only if mo 622 /* Write buffer is worth it only if more than one word to write... */
632 while (len > 0) { 623 while (len > 0) {
633 /* We must not cross write blo 624 /* We must not cross write block boundaries */
634 int size = wbufsize - (ofs & ( 625 int size = wbufsize - (ofs & (wbufsize-1));
635 626
636 if (size > len) 627 if (size > len)
637 size = len; 628 size = len;
638 629
639 ret = do_write_buffer(map, &cf !! 630 ret = do_write_buffer(map, &cfi->chips[chipnum],
640 ofs, buf 631 ofs, buf, size);
641 if (ret) 632 if (ret)
642 return ret; 633 return ret;
643 634
644 ofs += size; 635 ofs += size;
645 buf += size; 636 buf += size;
646 (*retlen) += size; 637 (*retlen) += size;
647 len -= size; 638 len -= size;
648 639
649 if (ofs >> cfi->chipshift) { 640 if (ofs >> cfi->chipshift) {
650 chipnum ++; !! 641 chipnum ++;
651 ofs = 0; 642 ofs = 0;
652 if (chipnum == cfi->nu 643 if (chipnum == cfi->numchips)
653 return 0; 644 return 0;
654 } 645 }
655 } 646 }
656 !! 647
657 return 0; 648 return 0;
658 } 649 }
659 650
660 /* 651 /*
661 * Writev for ECC-Flashes is a little more com 652 * Writev for ECC-Flashes is a little more complicated. We need to maintain
662 * a small buffer for this. 653 * a small buffer for this.
663 * XXX: If the buffer size is not a multiple o 654 * XXX: If the buffer size is not a multiple of 2, this will break
664 */ 655 */
665 #define ECCBUF_SIZE (mtd->writesize) !! 656 #define ECCBUF_SIZE (mtd->eccsize)
666 #define ECCBUF_DIV(x) ((x) & ~(ECCBUF_SIZE - 1 657 #define ECCBUF_DIV(x) ((x) & ~(ECCBUF_SIZE - 1))
667 #define ECCBUF_MOD(x) ((x) & (ECCBUF_SIZE - 1 658 #define ECCBUF_MOD(x) ((x) & (ECCBUF_SIZE - 1))
668 static int 659 static int
669 cfi_staa_writev(struct mtd_info *mtd, const st 660 cfi_staa_writev(struct mtd_info *mtd, const struct kvec *vecs,
670 unsigned long count, loff_t to 661 unsigned long count, loff_t to, size_t *retlen)
671 { 662 {
672 unsigned long i; 663 unsigned long i;
673 size_t totlen = 0, thislen; 664 size_t totlen = 0, thislen;
674 int ret = 0; 665 int ret = 0;
675 size_t buflen = 0; 666 size_t buflen = 0;
676 static char *buffer; 667 static char *buffer;
677 668
678 if (!ECCBUF_SIZE) { 669 if (!ECCBUF_SIZE) {
679 /* We should fall back to a ge 670 /* We should fall back to a general writev implementation.
680 * Until that is written, just 671 * Until that is written, just break.
681 */ 672 */
682 return -EIO; 673 return -EIO;
683 } 674 }
684 buffer = kmalloc(ECCBUF_SIZE, GFP_KERN 675 buffer = kmalloc(ECCBUF_SIZE, GFP_KERNEL);
685 if (!buffer) 676 if (!buffer)
686 return -ENOMEM; 677 return -ENOMEM;
687 678
688 for (i=0; i<count; i++) { 679 for (i=0; i<count; i++) {
689 size_t elem_len = vecs[i].iov_ 680 size_t elem_len = vecs[i].iov_len;
690 void *elem_base = vecs[i].iov_ 681 void *elem_base = vecs[i].iov_base;
691 if (!elem_len) /* FIXME: Might 682 if (!elem_len) /* FIXME: Might be unnecessary. Check that */
692 continue; 683 continue;
693 if (buflen) { /* cut off head 684 if (buflen) { /* cut off head */
694 if (buflen + elem_len 685 if (buflen + elem_len < ECCBUF_SIZE) { /* just accumulate */
695 memcpy(buffer+ 686 memcpy(buffer+buflen, elem_base, elem_len);
696 buflen += elem 687 buflen += elem_len;
697 continue; 688 continue;
698 } 689 }
699 memcpy(buffer+buflen, 690 memcpy(buffer+buflen, elem_base, ECCBUF_SIZE-buflen);
700 ret = mtd->write(mtd, 691 ret = mtd->write(mtd, to, ECCBUF_SIZE, &thislen, buffer);
701 totlen += thislen; 692 totlen += thislen;
702 if (ret || thislen != 693 if (ret || thislen != ECCBUF_SIZE)
703 goto write_err 694 goto write_error;
704 elem_len -= thislen-bu 695 elem_len -= thislen-buflen;
705 elem_base += thislen-b 696 elem_base += thislen-buflen;
706 to += ECCBUF_SIZE; 697 to += ECCBUF_SIZE;
707 } 698 }
708 if (ECCBUF_DIV(elem_len)) { /* 699 if (ECCBUF_DIV(elem_len)) { /* write clean aligned data */
709 ret = mtd->write(mtd, 700 ret = mtd->write(mtd, to, ECCBUF_DIV(elem_len), &thislen, elem_base);
710 totlen += thislen; 701 totlen += thislen;
711 if (ret || thislen != 702 if (ret || thislen != ECCBUF_DIV(elem_len))
712 goto write_err 703 goto write_error;
713 to += thislen; 704 to += thislen;
714 } 705 }
715 buflen = ECCBUF_MOD(elem_len); 706 buflen = ECCBUF_MOD(elem_len); /* cut off tail */
716 if (buflen) { 707 if (buflen) {
717 memset(buffer, 0xff, E 708 memset(buffer, 0xff, ECCBUF_SIZE);
718 memcpy(buffer, elem_ba 709 memcpy(buffer, elem_base + thislen, buflen);
719 } 710 }
720 } 711 }
721 if (buflen) { /* flush last page, even 712 if (buflen) { /* flush last page, even if not full */
722 /* This is sometimes intended 713 /* This is sometimes intended behaviour, really */
723 ret = mtd->write(mtd, to, bufl 714 ret = mtd->write(mtd, to, buflen, &thislen, buffer);
724 totlen += thislen; 715 totlen += thislen;
725 if (ret || thislen != ECCBUF_S 716 if (ret || thislen != ECCBUF_SIZE)
726 goto write_error; 717 goto write_error;
727 } 718 }
728 write_error: 719 write_error:
729 if (retlen) 720 if (retlen)
730 *retlen = totlen; 721 *retlen = totlen;
731 kfree(buffer); <<
732 return ret; 722 return ret;
733 } 723 }
734 724
735 725
736 static inline int do_erase_oneblock(struct map 726 static inline int do_erase_oneblock(struct map_info *map, struct flchip *chip, unsigned long adr)
737 { 727 {
738 struct cfi_private *cfi = map->fldrv_p 728 struct cfi_private *cfi = map->fldrv_priv;
739 map_word status, status_OK; 729 map_word status, status_OK;
740 unsigned long timeo; 730 unsigned long timeo;
741 int retries = 3; 731 int retries = 3;
742 DECLARE_WAITQUEUE(wait, current); 732 DECLARE_WAITQUEUE(wait, current);
743 int ret = 0; 733 int ret = 0;
744 734
745 adr += chip->start; 735 adr += chip->start;
746 736
747 /* Let's determine this according to t 737 /* Let's determine this according to the interleave only once */
748 status_OK = CMD(0x80); 738 status_OK = CMD(0x80);
749 739
750 timeo = jiffies + HZ; 740 timeo = jiffies + HZ;
751 retry: 741 retry:
752 spin_lock_bh(chip->mutex); 742 spin_lock_bh(chip->mutex);
753 743
754 /* Check that the chip's ready to talk 744 /* Check that the chip's ready to talk to us. */
755 switch (chip->state) { 745 switch (chip->state) {
756 case FL_CFI_QUERY: 746 case FL_CFI_QUERY:
757 case FL_JEDEC_QUERY: 747 case FL_JEDEC_QUERY:
758 case FL_READY: 748 case FL_READY:
759 map_write(map, CMD(0x70), adr) 749 map_write(map, CMD(0x70), adr);
760 chip->state = FL_STATUS; 750 chip->state = FL_STATUS;
761 751
762 case FL_STATUS: 752 case FL_STATUS:
763 status = map_read(map, adr); 753 status = map_read(map, adr);
764 if (map_word_andequal(map, sta 754 if (map_word_andequal(map, status, status_OK, status_OK))
765 break; 755 break;
766 !! 756
767 /* Urgh. Chip not yet ready to 757 /* Urgh. Chip not yet ready to talk to us. */
768 if (time_after(jiffies, timeo) 758 if (time_after(jiffies, timeo)) {
769 spin_unlock_bh(chip->m 759 spin_unlock_bh(chip->mutex);
770 printk(KERN_ERR "waiti 760 printk(KERN_ERR "waiting for chip to be ready timed out in erase\n");
771 return -EIO; 761 return -EIO;
772 } 762 }
773 763
774 /* Latency issues. Drop the lo 764 /* Latency issues. Drop the lock, wait a while and retry */
775 spin_unlock_bh(chip->mutex); 765 spin_unlock_bh(chip->mutex);
776 cfi_udelay(1); 766 cfi_udelay(1);
777 goto retry; 767 goto retry;
778 768
779 default: 769 default:
780 /* Stick ourselves on a wait q 770 /* Stick ourselves on a wait queue to be woken when
781 someone changes the status 771 someone changes the status */
782 set_current_state(TASK_UNINTER 772 set_current_state(TASK_UNINTERRUPTIBLE);
783 add_wait_queue(&chip->wq, &wai 773 add_wait_queue(&chip->wq, &wait);
784 spin_unlock_bh(chip->mutex); 774 spin_unlock_bh(chip->mutex);
785 schedule(); 775 schedule();
786 remove_wait_queue(&chip->wq, & 776 remove_wait_queue(&chip->wq, &wait);
787 timeo = jiffies + HZ; 777 timeo = jiffies + HZ;
788 goto retry; 778 goto retry;
789 } 779 }
790 780
791 ENABLE_VPP(map); 781 ENABLE_VPP(map);
792 /* Clear the status register first */ 782 /* Clear the status register first */
793 map_write(map, CMD(0x50), adr); 783 map_write(map, CMD(0x50), adr);
794 784
795 /* Now erase */ 785 /* Now erase */
796 map_write(map, CMD(0x20), adr); 786 map_write(map, CMD(0x20), adr);
797 map_write(map, CMD(0xD0), adr); 787 map_write(map, CMD(0xD0), adr);
798 chip->state = FL_ERASING; 788 chip->state = FL_ERASING;
799 !! 789
800 spin_unlock_bh(chip->mutex); 790 spin_unlock_bh(chip->mutex);
801 msleep(1000); 791 msleep(1000);
802 spin_lock_bh(chip->mutex); 792 spin_lock_bh(chip->mutex);
803 793
804 /* FIXME. Use a timer to check this, a 794 /* FIXME. Use a timer to check this, and return immediately. */
805 /* Once the state machine's known to b 795 /* Once the state machine's known to be working I'll do that */
806 796
807 timeo = jiffies + (HZ*20); 797 timeo = jiffies + (HZ*20);
808 for (;;) { 798 for (;;) {
809 if (chip->state != FL_ERASING) 799 if (chip->state != FL_ERASING) {
810 /* Someone's suspended 800 /* Someone's suspended the erase. Sleep */
811 set_current_state(TASK 801 set_current_state(TASK_UNINTERRUPTIBLE);
812 add_wait_queue(&chip-> 802 add_wait_queue(&chip->wq, &wait);
813 spin_unlock_bh(chip->m 803 spin_unlock_bh(chip->mutex);
814 schedule(); 804 schedule();
815 remove_wait_queue(&chi 805 remove_wait_queue(&chip->wq, &wait);
816 timeo = jiffies + (HZ* 806 timeo = jiffies + (HZ*20); /* FIXME */
817 spin_lock_bh(chip->mut 807 spin_lock_bh(chip->mutex);
818 continue; 808 continue;
819 } 809 }
820 810
821 status = map_read(map, adr); 811 status = map_read(map, adr);
822 if (map_word_andequal(map, sta 812 if (map_word_andequal(map, status, status_OK, status_OK))
823 break; 813 break;
824 !! 814
825 /* OK Still waiting */ 815 /* OK Still waiting */
826 if (time_after(jiffies, timeo) 816 if (time_after(jiffies, timeo)) {
827 map_write(map, CMD(0x7 817 map_write(map, CMD(0x70), adr);
828 chip->state = FL_STATU 818 chip->state = FL_STATUS;
829 printk(KERN_ERR "waiti 819 printk(KERN_ERR "waiting for erase to complete timed out. Xstatus = %lx, status = %lx.\n", status.x[0], map_read(map, adr).x[0]);
830 DISABLE_VPP(map); 820 DISABLE_VPP(map);
831 spin_unlock_bh(chip->m 821 spin_unlock_bh(chip->mutex);
832 return -EIO; 822 return -EIO;
833 } 823 }
834 !! 824
835 /* Latency issues. Drop the lo 825 /* Latency issues. Drop the lock, wait a while and retry */
836 spin_unlock_bh(chip->mutex); 826 spin_unlock_bh(chip->mutex);
837 cfi_udelay(1); 827 cfi_udelay(1);
838 spin_lock_bh(chip->mutex); 828 spin_lock_bh(chip->mutex);
839 } 829 }
840 !! 830
841 DISABLE_VPP(map); 831 DISABLE_VPP(map);
842 ret = 0; 832 ret = 0;
843 833
844 /* We've broken this before. It doesn' 834 /* We've broken this before. It doesn't hurt to be safe */
845 map_write(map, CMD(0x70), adr); 835 map_write(map, CMD(0x70), adr);
846 chip->state = FL_STATUS; 836 chip->state = FL_STATUS;
847 status = map_read(map, adr); 837 status = map_read(map, adr);
848 838
849 /* check for lock bit */ 839 /* check for lock bit */
850 if (map_word_bitsset(map, status, CMD( 840 if (map_word_bitsset(map, status, CMD(0x3a))) {
851 unsigned char chipstatus = sta 841 unsigned char chipstatus = status.x[0];
852 if (!map_word_equal(map, statu 842 if (!map_word_equal(map, status, CMD(chipstatus))) {
853 int i, w; 843 int i, w;
854 for (w=0; w<map_words( 844 for (w=0; w<map_words(map); w++) {
855 for (i = 0; i< 845 for (i = 0; i<cfi_interleave(cfi); i++) {
856 chipst 846 chipstatus |= status.x[w] >> (cfi->device_type * 8);
857 } 847 }
858 } 848 }
859 printk(KERN_WARNING "S 849 printk(KERN_WARNING "Status is not identical for all chips: 0x%lx. Merging to give 0x%02x\n",
860 status.x[0], ch 850 status.x[0], chipstatus);
861 } 851 }
862 /* Reset the error bits */ 852 /* Reset the error bits */
863 map_write(map, CMD(0x50), adr) 853 map_write(map, CMD(0x50), adr);
864 map_write(map, CMD(0x70), adr) 854 map_write(map, CMD(0x70), adr);
865 !! 855
866 if ((chipstatus & 0x30) == 0x3 856 if ((chipstatus & 0x30) == 0x30) {
867 printk(KERN_NOTICE "Ch 857 printk(KERN_NOTICE "Chip reports improper command sequence: status 0x%x\n", chipstatus);
868 ret = -EIO; 858 ret = -EIO;
869 } else if (chipstatus & 0x02) 859 } else if (chipstatus & 0x02) {
870 /* Protection bit set 860 /* Protection bit set */
871 ret = -EROFS; 861 ret = -EROFS;
872 } else if (chipstatus & 0x8) { 862 } else if (chipstatus & 0x8) {
873 /* Voltage */ 863 /* Voltage */
874 printk(KERN_WARNING "C 864 printk(KERN_WARNING "Chip reports voltage low on erase: status 0x%x\n", chipstatus);
875 ret = -EIO; 865 ret = -EIO;
876 } else if (chipstatus & 0x20) 866 } else if (chipstatus & 0x20) {
877 if (retries--) { 867 if (retries--) {
878 printk(KERN_DE 868 printk(KERN_DEBUG "Chip erase failed at 0x%08lx: status 0x%x. Retrying...\n", adr, chipstatus);
879 timeo = jiffie 869 timeo = jiffies + HZ;
880 chip->state = 870 chip->state = FL_STATUS;
881 spin_unlock_bh 871 spin_unlock_bh(chip->mutex);
882 goto retry; 872 goto retry;
883 } 873 }
884 printk(KERN_DEBUG "Chi 874 printk(KERN_DEBUG "Chip erase failed at 0x%08lx: status 0x%x\n", adr, chipstatus);
885 ret = -EIO; 875 ret = -EIO;
886 } 876 }
887 } 877 }
888 878
889 wake_up(&chip->wq); 879 wake_up(&chip->wq);
890 spin_unlock_bh(chip->mutex); 880 spin_unlock_bh(chip->mutex);
891 return ret; 881 return ret;
892 } 882 }
893 883
894 static int cfi_staa_erase_varsize(struct mtd_i !! 884 int cfi_staa_erase_varsize(struct mtd_info *mtd, struct erase_info *instr)
895 struct erase <<
896 { struct map_info *map = mtd->priv; 885 { struct map_info *map = mtd->priv;
897 struct cfi_private *cfi = map->fldrv_p 886 struct cfi_private *cfi = map->fldrv_priv;
898 unsigned long adr, len; 887 unsigned long adr, len;
899 int chipnum, ret = 0; 888 int chipnum, ret = 0;
900 int i, first; 889 int i, first;
901 struct mtd_erase_region_info *regions 890 struct mtd_erase_region_info *regions = mtd->eraseregions;
902 891
903 if (instr->addr > mtd->size) 892 if (instr->addr > mtd->size)
904 return -EINVAL; 893 return -EINVAL;
905 894
906 if ((instr->len + instr->addr) > mtd-> 895 if ((instr->len + instr->addr) > mtd->size)
907 return -EINVAL; 896 return -EINVAL;
908 897
909 /* Check that both start and end of th 898 /* Check that both start and end of the requested erase are
910 * aligned with the erasesize at the a 899 * aligned with the erasesize at the appropriate addresses.
911 */ 900 */
912 901
913 i = 0; 902 i = 0;
914 903
915 /* Skip all erase regions which are en !! 904 /* Skip all erase regions which are ended before the start of
916 the requested erase. Actually, to s 905 the requested erase. Actually, to save on the calculations,
917 we skip to the first erase region w 906 we skip to the first erase region which starts after the
918 start of the requested erase, and t 907 start of the requested erase, and then go back one.
919 */ 908 */
920 !! 909
921 while (i < mtd->numeraseregions && ins 910 while (i < mtd->numeraseregions && instr->addr >= regions[i].offset)
922 i++; 911 i++;
923 i--; 912 i--;
924 913
925 /* OK, now i is pointing at the erase !! 914 /* OK, now i is pointing at the erase region in which this
926 erase request starts. Check the sta 915 erase request starts. Check the start of the requested
927 erase range is aligned with the era 916 erase range is aligned with the erase size which is in
928 effect here. 917 effect here.
929 */ 918 */
930 919
931 if (instr->addr & (regions[i].erasesiz 920 if (instr->addr & (regions[i].erasesize-1))
932 return -EINVAL; 921 return -EINVAL;
933 922
934 /* Remember the erase region we start 923 /* Remember the erase region we start on */
935 first = i; 924 first = i;
936 925
937 /* Next, check that the end of the req 926 /* Next, check that the end of the requested erase is aligned
938 * with the erase region at that addre 927 * with the erase region at that address.
939 */ 928 */
940 929
941 while (i<mtd->numeraseregions && (inst 930 while (i<mtd->numeraseregions && (instr->addr + instr->len) >= regions[i].offset)
942 i++; 931 i++;
943 932
944 /* As before, drop back one to point a 933 /* As before, drop back one to point at the region in which
945 the address actually falls 934 the address actually falls
946 */ 935 */
947 i--; 936 i--;
948 !! 937
949 if ((instr->addr + instr->len) & (regi 938 if ((instr->addr + instr->len) & (regions[i].erasesize-1))
950 return -EINVAL; 939 return -EINVAL;
951 940
952 chipnum = instr->addr >> cfi->chipshif 941 chipnum = instr->addr >> cfi->chipshift;
953 adr = instr->addr - (chipnum << cfi->c 942 adr = instr->addr - (chipnum << cfi->chipshift);
954 len = instr->len; 943 len = instr->len;
955 944
956 i=first; 945 i=first;
957 946
958 while(len) { 947 while(len) {
959 ret = do_erase_oneblock(map, & 948 ret = do_erase_oneblock(map, &cfi->chips[chipnum], adr);
960 !! 949
961 if (ret) 950 if (ret)
962 return ret; 951 return ret;
963 952
964 adr += regions[i].erasesize; 953 adr += regions[i].erasesize;
965 len -= regions[i].erasesize; 954 len -= regions[i].erasesize;
966 955
967 if (adr % (1<< cfi->chipshift) !! 956 if (adr % (1<< cfi->chipshift) == ((regions[i].offset + (regions[i].erasesize * regions[i].numblocks)) %( 1<< cfi->chipshift)))
968 i++; 957 i++;
969 958
970 if (adr >> cfi->chipshift) { 959 if (adr >> cfi->chipshift) {
971 adr = 0; 960 adr = 0;
972 chipnum++; 961 chipnum++;
973 !! 962
974 if (chipnum >= cfi->nu 963 if (chipnum >= cfi->numchips)
975 break; 964 break;
976 } 965 }
977 } 966 }
978 !! 967
979 instr->state = MTD_ERASE_DONE; 968 instr->state = MTD_ERASE_DONE;
980 mtd_erase_callback(instr); 969 mtd_erase_callback(instr);
981 !! 970
982 return 0; 971 return 0;
983 } 972 }
984 973
985 static void cfi_staa_sync (struct mtd_info *mt 974 static void cfi_staa_sync (struct mtd_info *mtd)
986 { 975 {
987 struct map_info *map = mtd->priv; 976 struct map_info *map = mtd->priv;
988 struct cfi_private *cfi = map->fldrv_p 977 struct cfi_private *cfi = map->fldrv_priv;
989 int i; 978 int i;
990 struct flchip *chip; 979 struct flchip *chip;
991 int ret = 0; 980 int ret = 0;
992 DECLARE_WAITQUEUE(wait, current); 981 DECLARE_WAITQUEUE(wait, current);
993 982
994 for (i=0; !ret && i<cfi->numchips; i++ 983 for (i=0; !ret && i<cfi->numchips; i++) {
995 chip = &cfi->chips[i]; 984 chip = &cfi->chips[i];
996 985
997 retry: 986 retry:
998 spin_lock_bh(chip->mutex); 987 spin_lock_bh(chip->mutex);
999 988
1000 switch(chip->state) { 989 switch(chip->state) {
1001 case FL_READY: 990 case FL_READY:
1002 case FL_STATUS: 991 case FL_STATUS:
1003 case FL_CFI_QUERY: 992 case FL_CFI_QUERY:
1004 case FL_JEDEC_QUERY: 993 case FL_JEDEC_QUERY:
1005 chip->oldstate = chip 994 chip->oldstate = chip->state;
1006 chip->state = FL_SYNC 995 chip->state = FL_SYNCING;
1007 /* No need to wake_up !! 996 /* No need to wake_up() on this state change -
1008 * as the whole point 997 * as the whole point is that nobody can do anything
1009 * with the chip now 998 * with the chip now anyway.
1010 */ 999 */
1011 case FL_SYNCING: 1000 case FL_SYNCING:
1012 spin_unlock_bh(chip-> 1001 spin_unlock_bh(chip->mutex);
1013 break; 1002 break;
1014 1003
1015 default: 1004 default:
1016 /* Not an idle state 1005 /* Not an idle state */
1017 set_current_state(TAS <<
1018 add_wait_queue(&chip- 1006 add_wait_queue(&chip->wq, &wait);
1019 !! 1007
1020 spin_unlock_bh(chip-> 1008 spin_unlock_bh(chip->mutex);
1021 schedule(); 1009 schedule();
1022 remove_wait_queue(&ch 1010 remove_wait_queue(&chip->wq, &wait);
1023 !! 1011
1024 goto retry; 1012 goto retry;
1025 } 1013 }
1026 } 1014 }
1027 1015
1028 /* Unlock the chips again */ 1016 /* Unlock the chips again */
1029 1017
1030 for (i--; i >=0; i--) { 1018 for (i--; i >=0; i--) {
1031 chip = &cfi->chips[i]; 1019 chip = &cfi->chips[i];
1032 1020
1033 spin_lock_bh(chip->mutex); 1021 spin_lock_bh(chip->mutex);
1034 !! 1022
1035 if (chip->state == FL_SYNCING 1023 if (chip->state == FL_SYNCING) {
1036 chip->state = chip->o 1024 chip->state = chip->oldstate;
1037 wake_up(&chip->wq); 1025 wake_up(&chip->wq);
1038 } 1026 }
1039 spin_unlock_bh(chip->mutex); 1027 spin_unlock_bh(chip->mutex);
1040 } 1028 }
1041 } 1029 }
1042 1030
1043 static inline int do_lock_oneblock(struct map 1031 static inline int do_lock_oneblock(struct map_info *map, struct flchip *chip, unsigned long adr)
1044 { 1032 {
1045 struct cfi_private *cfi = map->fldrv_ 1033 struct cfi_private *cfi = map->fldrv_priv;
1046 map_word status, status_OK; 1034 map_word status, status_OK;
1047 unsigned long timeo = jiffies + HZ; 1035 unsigned long timeo = jiffies + HZ;
1048 DECLARE_WAITQUEUE(wait, current); 1036 DECLARE_WAITQUEUE(wait, current);
1049 1037
1050 adr += chip->start; 1038 adr += chip->start;
1051 1039
1052 /* Let's determine this according to 1040 /* Let's determine this according to the interleave only once */
1053 status_OK = CMD(0x80); 1041 status_OK = CMD(0x80);
1054 1042
1055 timeo = jiffies + HZ; 1043 timeo = jiffies + HZ;
1056 retry: 1044 retry:
1057 spin_lock_bh(chip->mutex); 1045 spin_lock_bh(chip->mutex);
1058 1046
1059 /* Check that the chip's ready to tal 1047 /* Check that the chip's ready to talk to us. */
1060 switch (chip->state) { 1048 switch (chip->state) {
1061 case FL_CFI_QUERY: 1049 case FL_CFI_QUERY:
1062 case FL_JEDEC_QUERY: 1050 case FL_JEDEC_QUERY:
1063 case FL_READY: 1051 case FL_READY:
1064 map_write(map, CMD(0x70), adr 1052 map_write(map, CMD(0x70), adr);
1065 chip->state = FL_STATUS; 1053 chip->state = FL_STATUS;
1066 1054
1067 case FL_STATUS: 1055 case FL_STATUS:
1068 status = map_read(map, adr); 1056 status = map_read(map, adr);
1069 if (map_word_andequal(map, st !! 1057 if (map_word_andequal(map, status, status_OK, status_OK))
1070 break; 1058 break;
1071 !! 1059
1072 /* Urgh. Chip not yet ready t 1060 /* Urgh. Chip not yet ready to talk to us. */
1073 if (time_after(jiffies, timeo 1061 if (time_after(jiffies, timeo)) {
1074 spin_unlock_bh(chip-> 1062 spin_unlock_bh(chip->mutex);
1075 printk(KERN_ERR "wait 1063 printk(KERN_ERR "waiting for chip to be ready timed out in lock\n");
1076 return -EIO; 1064 return -EIO;
1077 } 1065 }
1078 1066
1079 /* Latency issues. Drop the l 1067 /* Latency issues. Drop the lock, wait a while and retry */
1080 spin_unlock_bh(chip->mutex); 1068 spin_unlock_bh(chip->mutex);
1081 cfi_udelay(1); 1069 cfi_udelay(1);
1082 goto retry; 1070 goto retry;
1083 1071
1084 default: 1072 default:
1085 /* Stick ourselves on a wait 1073 /* Stick ourselves on a wait queue to be woken when
1086 someone changes the status 1074 someone changes the status */
1087 set_current_state(TASK_UNINTE 1075 set_current_state(TASK_UNINTERRUPTIBLE);
1088 add_wait_queue(&chip->wq, &wa 1076 add_wait_queue(&chip->wq, &wait);
1089 spin_unlock_bh(chip->mutex); 1077 spin_unlock_bh(chip->mutex);
1090 schedule(); 1078 schedule();
1091 remove_wait_queue(&chip->wq, 1079 remove_wait_queue(&chip->wq, &wait);
1092 timeo = jiffies + HZ; 1080 timeo = jiffies + HZ;
1093 goto retry; 1081 goto retry;
1094 } 1082 }
1095 1083
1096 ENABLE_VPP(map); 1084 ENABLE_VPP(map);
1097 map_write(map, CMD(0x60), adr); 1085 map_write(map, CMD(0x60), adr);
1098 map_write(map, CMD(0x01), adr); 1086 map_write(map, CMD(0x01), adr);
1099 chip->state = FL_LOCKING; 1087 chip->state = FL_LOCKING;
1100 !! 1088
1101 spin_unlock_bh(chip->mutex); 1089 spin_unlock_bh(chip->mutex);
1102 msleep(1000); 1090 msleep(1000);
1103 spin_lock_bh(chip->mutex); 1091 spin_lock_bh(chip->mutex);
1104 1092
1105 /* FIXME. Use a timer to check this, 1093 /* FIXME. Use a timer to check this, and return immediately. */
1106 /* Once the state machine's known to 1094 /* Once the state machine's known to be working I'll do that */
1107 1095
1108 timeo = jiffies + (HZ*2); 1096 timeo = jiffies + (HZ*2);
1109 for (;;) { 1097 for (;;) {
1110 1098
1111 status = map_read(map, adr); 1099 status = map_read(map, adr);
1112 if (map_word_andequal(map, st 1100 if (map_word_andequal(map, status, status_OK, status_OK))
1113 break; 1101 break;
1114 !! 1102
1115 /* OK Still waiting */ 1103 /* OK Still waiting */
1116 if (time_after(jiffies, timeo 1104 if (time_after(jiffies, timeo)) {
1117 map_write(map, CMD(0x 1105 map_write(map, CMD(0x70), adr);
1118 chip->state = FL_STAT 1106 chip->state = FL_STATUS;
1119 printk(KERN_ERR "wait 1107 printk(KERN_ERR "waiting for lock to complete timed out. Xstatus = %lx, status = %lx.\n", status.x[0], map_read(map, adr).x[0]);
1120 DISABLE_VPP(map); 1108 DISABLE_VPP(map);
1121 spin_unlock_bh(chip-> 1109 spin_unlock_bh(chip->mutex);
1122 return -EIO; 1110 return -EIO;
1123 } 1111 }
1124 !! 1112
1125 /* Latency issues. Drop the l 1113 /* Latency issues. Drop the lock, wait a while and retry */
1126 spin_unlock_bh(chip->mutex); 1114 spin_unlock_bh(chip->mutex);
1127 cfi_udelay(1); 1115 cfi_udelay(1);
1128 spin_lock_bh(chip->mutex); 1116 spin_lock_bh(chip->mutex);
1129 } 1117 }
1130 !! 1118
1131 /* Done and happy. */ 1119 /* Done and happy. */
1132 chip->state = FL_STATUS; 1120 chip->state = FL_STATUS;
1133 DISABLE_VPP(map); 1121 DISABLE_VPP(map);
1134 wake_up(&chip->wq); 1122 wake_up(&chip->wq);
1135 spin_unlock_bh(chip->mutex); 1123 spin_unlock_bh(chip->mutex);
1136 return 0; 1124 return 0;
1137 } 1125 }
1138 static int cfi_staa_lock(struct mtd_info *mtd !! 1126 static int cfi_staa_lock(struct mtd_info *mtd, loff_t ofs, size_t len)
1139 { 1127 {
1140 struct map_info *map = mtd->priv; 1128 struct map_info *map = mtd->priv;
1141 struct cfi_private *cfi = map->fldrv_ 1129 struct cfi_private *cfi = map->fldrv_priv;
1142 unsigned long adr; 1130 unsigned long adr;
1143 int chipnum, ret = 0; 1131 int chipnum, ret = 0;
1144 #ifdef DEBUG_LOCK_BITS 1132 #ifdef DEBUG_LOCK_BITS
1145 int ofs_factor = cfi->interleave * cf 1133 int ofs_factor = cfi->interleave * cfi->device_type;
1146 #endif 1134 #endif
1147 1135
1148 if (ofs & (mtd->erasesize - 1)) 1136 if (ofs & (mtd->erasesize - 1))
1149 return -EINVAL; 1137 return -EINVAL;
1150 1138
1151 if (len & (mtd->erasesize -1)) 1139 if (len & (mtd->erasesize -1))
1152 return -EINVAL; 1140 return -EINVAL;
1153 1141
1154 if ((len + ofs) > mtd->size) 1142 if ((len + ofs) > mtd->size)
1155 return -EINVAL; 1143 return -EINVAL;
1156 1144
1157 chipnum = ofs >> cfi->chipshift; 1145 chipnum = ofs >> cfi->chipshift;
1158 adr = ofs - (chipnum << cfi->chipshif 1146 adr = ofs - (chipnum << cfi->chipshift);
1159 1147
1160 while(len) { 1148 while(len) {
1161 1149
1162 #ifdef DEBUG_LOCK_BITS 1150 #ifdef DEBUG_LOCK_BITS
1163 cfi_send_gen_cmd(0x90, 0x55, 1151 cfi_send_gen_cmd(0x90, 0x55, 0, map, cfi, cfi->device_type, NULL);
1164 printk("before lock: block st 1152 printk("before lock: block status register is %x\n",cfi_read_query(map, adr+(2*ofs_factor)));
1165 cfi_send_gen_cmd(0xff, 0x55, 1153 cfi_send_gen_cmd(0xff, 0x55, 0, map, cfi, cfi->device_type, NULL);
1166 #endif 1154 #endif
1167 1155
1168 ret = do_lock_oneblock(map, & 1156 ret = do_lock_oneblock(map, &cfi->chips[chipnum], adr);
1169 1157
1170 #ifdef DEBUG_LOCK_BITS 1158 #ifdef DEBUG_LOCK_BITS
1171 cfi_send_gen_cmd(0x90, 0x55, 1159 cfi_send_gen_cmd(0x90, 0x55, 0, map, cfi, cfi->device_type, NULL);
1172 printk("after lock: block sta 1160 printk("after lock: block status register is %x\n",cfi_read_query(map, adr+(2*ofs_factor)));
1173 cfi_send_gen_cmd(0xff, 0x55, 1161 cfi_send_gen_cmd(0xff, 0x55, 0, map, cfi, cfi->device_type, NULL);
1174 #endif !! 1162 #endif
1175 !! 1163
1176 if (ret) 1164 if (ret)
1177 return ret; 1165 return ret;
1178 1166
1179 adr += mtd->erasesize; 1167 adr += mtd->erasesize;
1180 len -= mtd->erasesize; 1168 len -= mtd->erasesize;
1181 1169
1182 if (adr >> cfi->chipshift) { 1170 if (adr >> cfi->chipshift) {
1183 adr = 0; 1171 adr = 0;
1184 chipnum++; 1172 chipnum++;
1185 !! 1173
1186 if (chipnum >= cfi->n 1174 if (chipnum >= cfi->numchips)
1187 break; 1175 break;
1188 } 1176 }
1189 } 1177 }
1190 return 0; 1178 return 0;
1191 } 1179 }
1192 static inline int do_unlock_oneblock(struct m 1180 static inline int do_unlock_oneblock(struct map_info *map, struct flchip *chip, unsigned long adr)
1193 { 1181 {
1194 struct cfi_private *cfi = map->fldrv_ 1182 struct cfi_private *cfi = map->fldrv_priv;
1195 map_word status, status_OK; 1183 map_word status, status_OK;
1196 unsigned long timeo = jiffies + HZ; 1184 unsigned long timeo = jiffies + HZ;
1197 DECLARE_WAITQUEUE(wait, current); 1185 DECLARE_WAITQUEUE(wait, current);
1198 1186
1199 adr += chip->start; 1187 adr += chip->start;
1200 1188
1201 /* Let's determine this according to 1189 /* Let's determine this according to the interleave only once */
1202 status_OK = CMD(0x80); 1190 status_OK = CMD(0x80);
1203 1191
1204 timeo = jiffies + HZ; 1192 timeo = jiffies + HZ;
1205 retry: 1193 retry:
1206 spin_lock_bh(chip->mutex); 1194 spin_lock_bh(chip->mutex);
1207 1195
1208 /* Check that the chip's ready to tal 1196 /* Check that the chip's ready to talk to us. */
1209 switch (chip->state) { 1197 switch (chip->state) {
1210 case FL_CFI_QUERY: 1198 case FL_CFI_QUERY:
1211 case FL_JEDEC_QUERY: 1199 case FL_JEDEC_QUERY:
1212 case FL_READY: 1200 case FL_READY:
1213 map_write(map, CMD(0x70), adr 1201 map_write(map, CMD(0x70), adr);
1214 chip->state = FL_STATUS; 1202 chip->state = FL_STATUS;
1215 1203
1216 case FL_STATUS: 1204 case FL_STATUS:
1217 status = map_read(map, adr); 1205 status = map_read(map, adr);
1218 if (map_word_andequal(map, st 1206 if (map_word_andequal(map, status, status_OK, status_OK))
1219 break; 1207 break;
1220 !! 1208
1221 /* Urgh. Chip not yet ready t 1209 /* Urgh. Chip not yet ready to talk to us. */
1222 if (time_after(jiffies, timeo 1210 if (time_after(jiffies, timeo)) {
1223 spin_unlock_bh(chip-> 1211 spin_unlock_bh(chip->mutex);
1224 printk(KERN_ERR "wait 1212 printk(KERN_ERR "waiting for chip to be ready timed out in unlock\n");
1225 return -EIO; 1213 return -EIO;
1226 } 1214 }
1227 1215
1228 /* Latency issues. Drop the l 1216 /* Latency issues. Drop the lock, wait a while and retry */
1229 spin_unlock_bh(chip->mutex); 1217 spin_unlock_bh(chip->mutex);
1230 cfi_udelay(1); 1218 cfi_udelay(1);
1231 goto retry; 1219 goto retry;
1232 1220
1233 default: 1221 default:
1234 /* Stick ourselves on a wait 1222 /* Stick ourselves on a wait queue to be woken when
1235 someone changes the status 1223 someone changes the status */
1236 set_current_state(TASK_UNINTE 1224 set_current_state(TASK_UNINTERRUPTIBLE);
1237 add_wait_queue(&chip->wq, &wa 1225 add_wait_queue(&chip->wq, &wait);
1238 spin_unlock_bh(chip->mutex); 1226 spin_unlock_bh(chip->mutex);
1239 schedule(); 1227 schedule();
1240 remove_wait_queue(&chip->wq, 1228 remove_wait_queue(&chip->wq, &wait);
1241 timeo = jiffies + HZ; 1229 timeo = jiffies + HZ;
1242 goto retry; 1230 goto retry;
1243 } 1231 }
1244 1232
1245 ENABLE_VPP(map); 1233 ENABLE_VPP(map);
1246 map_write(map, CMD(0x60), adr); 1234 map_write(map, CMD(0x60), adr);
1247 map_write(map, CMD(0xD0), adr); 1235 map_write(map, CMD(0xD0), adr);
1248 chip->state = FL_UNLOCKING; 1236 chip->state = FL_UNLOCKING;
1249 !! 1237
1250 spin_unlock_bh(chip->mutex); 1238 spin_unlock_bh(chip->mutex);
1251 msleep(1000); 1239 msleep(1000);
1252 spin_lock_bh(chip->mutex); 1240 spin_lock_bh(chip->mutex);
1253 1241
1254 /* FIXME. Use a timer to check this, 1242 /* FIXME. Use a timer to check this, and return immediately. */
1255 /* Once the state machine's known to 1243 /* Once the state machine's known to be working I'll do that */
1256 1244
1257 timeo = jiffies + (HZ*2); 1245 timeo = jiffies + (HZ*2);
1258 for (;;) { 1246 for (;;) {
1259 1247
1260 status = map_read(map, adr); 1248 status = map_read(map, adr);
1261 if (map_word_andequal(map, st 1249 if (map_word_andequal(map, status, status_OK, status_OK))
1262 break; 1250 break;
1263 !! 1251
1264 /* OK Still waiting */ 1252 /* OK Still waiting */
1265 if (time_after(jiffies, timeo 1253 if (time_after(jiffies, timeo)) {
1266 map_write(map, CMD(0x 1254 map_write(map, CMD(0x70), adr);
1267 chip->state = FL_STAT 1255 chip->state = FL_STATUS;
1268 printk(KERN_ERR "wait 1256 printk(KERN_ERR "waiting for unlock to complete timed out. Xstatus = %lx, status = %lx.\n", status.x[0], map_read(map, adr).x[0]);
1269 DISABLE_VPP(map); 1257 DISABLE_VPP(map);
1270 spin_unlock_bh(chip-> 1258 spin_unlock_bh(chip->mutex);
1271 return -EIO; 1259 return -EIO;
1272 } 1260 }
1273 !! 1261
1274 /* Latency issues. Drop the u 1262 /* Latency issues. Drop the unlock, wait a while and retry */
1275 spin_unlock_bh(chip->mutex); 1263 spin_unlock_bh(chip->mutex);
1276 cfi_udelay(1); 1264 cfi_udelay(1);
1277 spin_lock_bh(chip->mutex); 1265 spin_lock_bh(chip->mutex);
1278 } 1266 }
1279 !! 1267
1280 /* Done and happy. */ 1268 /* Done and happy. */
1281 chip->state = FL_STATUS; 1269 chip->state = FL_STATUS;
1282 DISABLE_VPP(map); 1270 DISABLE_VPP(map);
1283 wake_up(&chip->wq); 1271 wake_up(&chip->wq);
1284 spin_unlock_bh(chip->mutex); 1272 spin_unlock_bh(chip->mutex);
1285 return 0; 1273 return 0;
1286 } 1274 }
1287 static int cfi_staa_unlock(struct mtd_info *m !! 1275 static int cfi_staa_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
1288 { 1276 {
1289 struct map_info *map = mtd->priv; 1277 struct map_info *map = mtd->priv;
1290 struct cfi_private *cfi = map->fldrv_ 1278 struct cfi_private *cfi = map->fldrv_priv;
1291 unsigned long adr; 1279 unsigned long adr;
1292 int chipnum, ret = 0; 1280 int chipnum, ret = 0;
1293 #ifdef DEBUG_LOCK_BITS 1281 #ifdef DEBUG_LOCK_BITS
1294 int ofs_factor = cfi->interleave * cf 1282 int ofs_factor = cfi->interleave * cfi->device_type;
1295 #endif 1283 #endif
1296 1284
1297 chipnum = ofs >> cfi->chipshift; 1285 chipnum = ofs >> cfi->chipshift;
1298 adr = ofs - (chipnum << cfi->chipshif 1286 adr = ofs - (chipnum << cfi->chipshift);
1299 1287
1300 #ifdef DEBUG_LOCK_BITS 1288 #ifdef DEBUG_LOCK_BITS
1301 { 1289 {
1302 unsigned long temp_adr = adr; 1290 unsigned long temp_adr = adr;
1303 unsigned long temp_len = len; 1291 unsigned long temp_len = len;
1304 !! 1292
1305 cfi_send_gen_cmd(0x90, 0x55, 1293 cfi_send_gen_cmd(0x90, 0x55, 0, map, cfi, cfi->device_type, NULL);
1306 while (temp_len) { 1294 while (temp_len) {
1307 printk("before unlock 1295 printk("before unlock %x: block status register is %x\n",temp_adr,cfi_read_query(map, temp_adr+(2*ofs_factor)));
1308 temp_adr += mtd->eras 1296 temp_adr += mtd->erasesize;
1309 temp_len -= mtd->eras 1297 temp_len -= mtd->erasesize;
1310 } 1298 }
1311 cfi_send_gen_cmd(0xff, 0x55, 1299 cfi_send_gen_cmd(0xff, 0x55, 0, map, cfi, cfi->device_type, NULL);
1312 } 1300 }
1313 #endif 1301 #endif
1314 1302
1315 ret = do_unlock_oneblock(map, &cfi->c 1303 ret = do_unlock_oneblock(map, &cfi->chips[chipnum], adr);
1316 1304
1317 #ifdef DEBUG_LOCK_BITS 1305 #ifdef DEBUG_LOCK_BITS
1318 cfi_send_gen_cmd(0x90, 0x55, 0, map, 1306 cfi_send_gen_cmd(0x90, 0x55, 0, map, cfi, cfi->device_type, NULL);
1319 printk("after unlock: block status re 1307 printk("after unlock: block status register is %x\n",cfi_read_query(map, adr+(2*ofs_factor)));
1320 cfi_send_gen_cmd(0xff, 0x55, 0, map, 1308 cfi_send_gen_cmd(0xff, 0x55, 0, map, cfi, cfi->device_type, NULL);
1321 #endif 1309 #endif
1322 !! 1310
1323 return ret; 1311 return ret;
1324 } 1312 }
1325 1313
1326 static int cfi_staa_suspend(struct mtd_info * 1314 static int cfi_staa_suspend(struct mtd_info *mtd)
1327 { 1315 {
1328 struct map_info *map = mtd->priv; 1316 struct map_info *map = mtd->priv;
1329 struct cfi_private *cfi = map->fldrv_ 1317 struct cfi_private *cfi = map->fldrv_priv;
1330 int i; 1318 int i;
1331 struct flchip *chip; 1319 struct flchip *chip;
1332 int ret = 0; 1320 int ret = 0;
1333 1321
1334 for (i=0; !ret && i<cfi->numchips; i+ 1322 for (i=0; !ret && i<cfi->numchips; i++) {
1335 chip = &cfi->chips[i]; 1323 chip = &cfi->chips[i];
1336 1324
1337 spin_lock_bh(chip->mutex); 1325 spin_lock_bh(chip->mutex);
1338 1326
1339 switch(chip->state) { 1327 switch(chip->state) {
1340 case FL_READY: 1328 case FL_READY:
1341 case FL_STATUS: 1329 case FL_STATUS:
1342 case FL_CFI_QUERY: 1330 case FL_CFI_QUERY:
1343 case FL_JEDEC_QUERY: 1331 case FL_JEDEC_QUERY:
1344 chip->oldstate = chip 1332 chip->oldstate = chip->state;
1345 chip->state = FL_PM_S 1333 chip->state = FL_PM_SUSPENDED;
1346 /* No need to wake_up !! 1334 /* No need to wake_up() on this state change -
1347 * as the whole point 1335 * as the whole point is that nobody can do anything
1348 * with the chip now 1336 * with the chip now anyway.
1349 */ 1337 */
1350 case FL_PM_SUSPENDED: 1338 case FL_PM_SUSPENDED:
1351 break; 1339 break;
1352 1340
1353 default: 1341 default:
1354 ret = -EAGAIN; 1342 ret = -EAGAIN;
1355 break; 1343 break;
1356 } 1344 }
1357 spin_unlock_bh(chip->mutex); 1345 spin_unlock_bh(chip->mutex);
1358 } 1346 }
1359 1347
1360 /* Unlock the chips again */ 1348 /* Unlock the chips again */
1361 1349
1362 if (ret) { 1350 if (ret) {
1363 for (i--; i >=0; i--) { 1351 for (i--; i >=0; i--) {
1364 chip = &cfi->chips[i] 1352 chip = &cfi->chips[i];
1365 !! 1353
1366 spin_lock_bh(chip->mu 1354 spin_lock_bh(chip->mutex);
1367 !! 1355
1368 if (chip->state == FL 1356 if (chip->state == FL_PM_SUSPENDED) {
1369 /* No need to 1357 /* No need to force it into a known state here,
1370 because we 1358 because we're returning failure, and it didn't
1371 get power 1359 get power cycled */
1372 chip->state = 1360 chip->state = chip->oldstate;
1373 wake_up(&chip 1361 wake_up(&chip->wq);
1374 } 1362 }
1375 spin_unlock_bh(chip-> 1363 spin_unlock_bh(chip->mutex);
1376 } 1364 }
1377 } !! 1365 }
1378 !! 1366
1379 return ret; 1367 return ret;
1380 } 1368 }
1381 1369
1382 static void cfi_staa_resume(struct mtd_info * 1370 static void cfi_staa_resume(struct mtd_info *mtd)
1383 { 1371 {
1384 struct map_info *map = mtd->priv; 1372 struct map_info *map = mtd->priv;
1385 struct cfi_private *cfi = map->fldrv_ 1373 struct cfi_private *cfi = map->fldrv_priv;
1386 int i; 1374 int i;
1387 struct flchip *chip; 1375 struct flchip *chip;
1388 1376
1389 for (i=0; i<cfi->numchips; i++) { 1377 for (i=0; i<cfi->numchips; i++) {
1390 !! 1378
1391 chip = &cfi->chips[i]; 1379 chip = &cfi->chips[i];
1392 1380
1393 spin_lock_bh(chip->mutex); 1381 spin_lock_bh(chip->mutex);
1394 !! 1382
1395 /* Go to known state. Chip ma 1383 /* Go to known state. Chip may have been power cycled */
1396 if (chip->state == FL_PM_SUSP 1384 if (chip->state == FL_PM_SUSPENDED) {
1397 map_write(map, CMD(0x 1385 map_write(map, CMD(0xFF), 0);
1398 chip->state = FL_READ 1386 chip->state = FL_READY;
1399 wake_up(&chip->wq); 1387 wake_up(&chip->wq);
1400 } 1388 }
1401 1389
1402 spin_unlock_bh(chip->mutex); 1390 spin_unlock_bh(chip->mutex);
1403 } 1391 }
1404 } 1392 }
1405 1393
1406 static void cfi_staa_destroy(struct mtd_info 1394 static void cfi_staa_destroy(struct mtd_info *mtd)
1407 { 1395 {
1408 struct map_info *map = mtd->priv; 1396 struct map_info *map = mtd->priv;
1409 struct cfi_private *cfi = map->fldrv_ 1397 struct cfi_private *cfi = map->fldrv_priv;
1410 kfree(cfi->cmdset_priv); 1398 kfree(cfi->cmdset_priv);
1411 kfree(cfi); 1399 kfree(cfi);
1412 } 1400 }
1413 1401
>> 1402 static char im_name[]="cfi_cmdset_0020";
>> 1403
>> 1404 static int __init cfi_staa_init(void)
>> 1405 {
>> 1406 inter_module_register(im_name, THIS_MODULE, &cfi_cmdset_0020);
>> 1407 return 0;
>> 1408 }
>> 1409
>> 1410 static void __exit cfi_staa_exit(void)
>> 1411 {
>> 1412 inter_module_unregister(im_name);
>> 1413 }
>> 1414
>> 1415 module_init(cfi_staa_init);
>> 1416 module_exit(cfi_staa_exit);
>> 1417
1414 MODULE_LICENSE("GPL"); 1418 MODULE_LICENSE("GPL");
1415 1419
|
This page was automatically generated by the
LXR engine.
|