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