at commit c03c1a14
| 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Common Flash Interface support: |
| 4 | * Intel Extended Vendor Command Set (ID 0x0001) |
| 5 | * |
| 6 | * (C) 2000 Red Hat. |
| 7 | * |
| 8 | * |
| 9 | * 10/10/2000 Nicolas Pitre <nico@fluxnic.net> |
| 10 | * - completely revamped method functions so they are aware and |
| 11 | * independent of the flash geometry (buswidth, interleave, etc.) |
| 12 | * - scalability vs code size is completely set at compile-time |
| 13 | * (see include/linux/mtd/cfi.h for selection) |
| 14 | * - optimized write buffer method |
| 15 | * 02/05/2002 Christopher Hoover <ch@hpl.hp.com>/<ch@murgatroid.com> |
| 16 | * - reworked lock/unlock/erase support for var size flash |
| 17 | * 21/03/2007 Rodolfo Giometti <giometti@linux.it> |
| 18 | * - auto unlock sectors on resume for auto locking flash on power up |
| 19 | */ |
| 20 | |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/types.h> |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/sched.h> |
| 25 | #include <asm/io.h> |
| 26 | #include <asm/byteorder.h> |
| 27 | |
| 28 | #include <linux/errno.h> |
| 29 | #include <linux/slab.h> |
| 30 | #include <linux/delay.h> |
| 31 | #include <linux/interrupt.h> |
| 32 | #include <linux/reboot.h> |
| 33 | #include <linux/bitmap.h> |
| 34 | #include <linux/mtd/xip.h> |
| 35 | #include <linux/mtd/map.h> |
| 36 | #include <linux/mtd/mtd.h> |
| 37 | #include <linux/mtd/cfi.h> |
| 38 | |
| 39 | /* #define CMDSET0001_DISABLE_ERASE_SUSPEND_ON_WRITE */ |
| 40 | /* #define CMDSET0001_DISABLE_WRITE_SUSPEND */ |
| 41 | |
| 42 | // debugging, turns off buffer write mode if set to 1 |
| 43 | #define FORCE_WORD_WRITE 0 |
| 44 | |
| 45 | /* Intel chips */ |
| 46 | #define I82802AB 0x00ad |
| 47 | #define I82802AC 0x00ac |
| 48 | #define PF38F4476 0x881c |
| 49 | #define M28F00AP30 0x8963 |
| 50 | /* STMicroelectronics chips */ |
| 51 | #define M50LPW080 0x002F |
| 52 | #define M50FLW080A 0x0080 |
| 53 | #define M50FLW080B 0x0081 |
| 54 | /* Atmel chips */ |
| 55 | #define AT49BV640D 0x02de |
| 56 | #define AT49BV640DT 0x02db |
| 57 | /* Sharp chips */ |
| 58 | #define LH28F640BFHE_PTTL90 0x00b0 |
| 59 | #define LH28F640BFHE_PBTL90 0x00b1 |
| 60 | #define LH28F640BFHE_PTTL70A 0x00b2 |
| 61 | #define LH28F640BFHE_PBTL70A 0x00b3 |
| 62 | |
| 63 | static int cfi_intelext_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *); |
| 64 | static int cfi_intelext_write_words(struct mtd_info *, loff_t, size_t, size_t *, const u_char *); |
| 65 | static int cfi_intelext_write_buffers(struct mtd_info *, loff_t, size_t, size_t *, const u_char *); |
| 66 | static int cfi_intelext_writev(struct mtd_info *, const struct kvec *, unsigned long, loff_t, size_t *); |
| 67 | static int cfi_intelext_erase_varsize(struct mtd_info *, struct erase_info *); |
| 68 | static void cfi_intelext_sync (struct mtd_info *); |
| 69 | static int cfi_intelext_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len); |
| 70 | static int cfi_intelext_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len); |
| 71 | static int cfi_intelext_is_locked(struct mtd_info *mtd, loff_t ofs, |
| 72 | uint64_t len); |
| 73 | #ifdef CONFIG_MTD_OTP |
| 74 | static int cfi_intelext_read_fact_prot_reg (struct mtd_info *, loff_t, size_t, size_t *, u_char *); |
| 75 | static int cfi_intelext_read_user_prot_reg (struct mtd_info *, loff_t, size_t, size_t *, u_char *); |
| 76 | static int cfi_intelext_write_user_prot_reg(struct mtd_info *, loff_t, size_t, |
| 77 | size_t *, const u_char *); |
| 78 | static int cfi_intelext_lock_user_prot_reg (struct mtd_info *, loff_t, size_t); |
| 79 | static int cfi_intelext_get_fact_prot_info(struct mtd_info *, size_t, |
| 80 | size_t *, struct otp_info *); |
| 81 | static int cfi_intelext_get_user_prot_info(struct mtd_info *, size_t, |
| 82 | size_t *, struct otp_info *); |
| 83 | #endif |
| 84 | static int cfi_intelext_suspend (struct mtd_info *); |
| 85 | static void cfi_intelext_resume (struct mtd_info *); |
| 86 | static int cfi_intelext_reboot (struct notifier_block *, unsigned long, void *); |
| 87 | |
| 88 | static void cfi_intelext_destroy(struct mtd_info *); |
| 89 | |
| 90 | struct mtd_info *cfi_cmdset_0001(struct map_info *, int); |
| 91 | |
| 92 | static struct mtd_info *cfi_intelext_setup (struct mtd_info *); |
| 93 | static int cfi_intelext_partition_fixup(struct mtd_info *, struct cfi_private **); |
| 94 | |
| 95 | static int cfi_intelext_point (struct mtd_info *mtd, loff_t from, size_t len, |
| 96 | size_t *retlen, void **virt, resource_size_t *phys); |
| 97 | static int cfi_intelext_unpoint(struct mtd_info *mtd, loff_t from, size_t len); |
| 98 | |
| 99 | static int chip_ready (struct map_info *map, struct flchip *chip, unsigned long adr, int mode); |
| 100 | static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode); |
| 101 | static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr); |
| 102 | #include "fwh_lock.h" |
| 103 | |
| 104 | |
| 105 | |
| 106 | /* |
| 107 | * *********** SETUP AND PROBE BITS *********** |
| 108 | */ |
| 109 | |
| 110 | static struct mtd_chip_driver cfi_intelext_chipdrv = { |
| 111 | .probe = NULL, /* Not usable directly */ |
| 112 | .destroy = cfi_intelext_destroy, |
| 113 | .name = "cfi_cmdset_0001", |
| 114 | .module = THIS_MODULE |
| 115 | }; |
| 116 | |
| 117 | /* #define DEBUG_LOCK_BITS */ |
| 118 | /* #define DEBUG_CFI_FEATURES */ |
| 119 | |
| 120 | #ifdef DEBUG_CFI_FEATURES |
| 121 | static void cfi_tell_features(struct cfi_pri_intelext *extp) |
| 122 | { |
| 123 | int i; |
| 124 | printk(" Extended Query version %c.%c\n", extp->MajorVersion, extp->MinorVersion); |
| 125 | printk(" Feature/Command Support: %4.4X\n", extp->FeatureSupport); |
| 126 | printk(" - Chip Erase: %s\n", extp->FeatureSupport&1?"supported":"unsupported"); |
| 127 | printk(" - Suspend Erase: %s\n", extp->FeatureSupport&2?"supported":"unsupported"); |
| 128 | printk(" - Suspend Program: %s\n", extp->FeatureSupport&4?"supported":"unsupported"); |
| 129 | printk(" - Legacy Lock/Unlock: %s\n", extp->FeatureSupport&8?"supported":"unsupported"); |
| 130 | printk(" - Queued Erase: %s\n", extp->FeatureSupport&16?"supported":"unsupported"); |
| 131 | printk(" - Instant block lock: %s\n", extp->FeatureSupport&32?"supported":"unsupported"); |
| 132 | printk(" - Protection Bits: %s\n", extp->FeatureSupport&64?"supported":"unsupported"); |
| 133 | printk(" - Page-mode read: %s\n", extp->FeatureSupport&128?"supported":"unsupported"); |
| 134 | printk(" - Synchronous read: %s\n", extp->FeatureSupport&256?"supported":"unsupported"); |
| 135 | printk(" - Simultaneous operations: %s\n", extp->FeatureSupport&512?"supported":"unsupported"); |
| 136 | printk(" - Extended Flash Array: %s\n", extp->FeatureSupport&1024?"supported":"unsupported"); |
| 137 | for (i=11; i<32; i++) { |
| 138 | if (extp->FeatureSupport & (1<<i)) |
| 139 | printk(" - Unknown Bit %X: supported\n", i); |
| 140 | } |
| 141 | |
| 142 | printk(" Supported functions after Suspend: %2.2X\n", extp->SuspendCmdSupport); |
| 143 | printk(" - Program after Erase Suspend: %s\n", extp->SuspendCmdSupport&1?"supported":"unsupported"); |
| 144 | for (i=1; i<8; i++) { |
| 145 | if (extp->SuspendCmdSupport & (1<<i)) |
| 146 | printk(" - Unknown Bit %X: supported\n", i); |
| 147 | } |
| 148 | |
| 149 | printk(" Block Status Register Mask: %4.4X\n", extp->BlkStatusRegMask); |
| 150 | printk(" - Lock Bit Active: %s\n", extp->BlkStatusRegMask&1?"yes":"no"); |
| 151 | printk(" - Lock-Down Bit Active: %s\n", extp->BlkStatusRegMask&2?"yes":"no"); |
| 152 | for (i=2; i<3; i++) { |
| 153 | if (extp->BlkStatusRegMask & (1<<i)) |
| 154 | printk(" - Unknown Bit %X Active: yes\n",i); |
| 155 | } |
| 156 | printk(" - EFA Lock Bit: %s\n", extp->BlkStatusRegMask&16?"yes":"no"); |
| 157 | printk(" - EFA Lock-Down Bit: %s\n", extp->BlkStatusRegMask&32?"yes":"no"); |
| 158 | for (i=6; i<16; i++) { |
| 159 | if (extp->BlkStatusRegMask & (1<<i)) |
| 160 | printk(" - Unknown Bit %X Active: yes\n",i); |
| 161 | } |
| 162 | |
| 163 | printk(" Vcc Logic Supply Optimum Program/Erase Voltage: %d.%d V\n", |
| 164 | extp->VccOptimal >> 4, extp->VccOptimal & 0xf); |
| 165 | if (extp->VppOptimal) |
| 166 | printk(" Vpp Programming Supply Optimum Program/Erase Voltage: %d.%d V\n", |
| 167 | extp->VppOptimal >> 4, extp->VppOptimal & 0xf); |
| 168 | } |
| 169 | #endif |
| 170 | |
| 171 | /* Atmel chips don't use the same PRI format as Intel chips */ |
| 172 | static void fixup_convert_atmel_pri(struct mtd_info *mtd) |
| 173 | { |
| 174 | struct map_info *map = mtd->priv; |
| 175 | struct cfi_private *cfi = map->fldrv_priv; |
| 176 | struct cfi_pri_intelext *extp = cfi->cmdset_priv; |
| 177 | struct cfi_pri_atmel atmel_pri; |
| 178 | uint32_t features = 0; |
| 179 | |
| 180 | /* Reverse byteswapping */ |
| 181 | extp->FeatureSupport = cpu_to_le32(extp->FeatureSupport); |
| 182 | extp->BlkStatusRegMask = cpu_to_le16(extp->BlkStatusRegMask); |
| 183 | extp->ProtRegAddr = cpu_to_le16(extp->ProtRegAddr); |
| 184 | |
| 185 | memcpy(&atmel_pri, extp, sizeof(atmel_pri)); |
| 186 | memset((char *)extp + 5, 0, sizeof(*extp) - 5); |
| 187 | |
| 188 | printk(KERN_ERR "atmel Features: %02x\n", atmel_pri.Features); |
| 189 | |
| 190 | if (atmel_pri.Features & 0x01) /* chip erase supported */ |
| 191 | features |= (1<<0); |
| 192 | if (atmel_pri.Features & 0x02) /* erase suspend supported */ |
| 193 | features |= (1<<1); |
| 194 | if (atmel_pri.Features & 0x04) /* program suspend supported */ |
| 195 | features |= (1<<2); |
| 196 | if (atmel_pri.Features & 0x08) /* simultaneous operations supported */ |
| 197 | features |= (1<<9); |
| 198 | if (atmel_pri.Features & 0x20) /* page mode read supported */ |
| 199 | features |= (1<<7); |
| 200 | if (atmel_pri.Features & 0x40) /* queued erase supported */ |
| 201 | features |= (1<<4); |
| 202 | if (atmel_pri.Features & 0x80) /* Protection bits supported */ |
| 203 | features |= (1<<6); |
| 204 | |
| 205 | extp->FeatureSupport = features; |
| 206 | |
| 207 | /* burst write mode not supported */ |
| 208 | cfi->cfiq->BufWriteTimeoutTyp = 0; |
| 209 | cfi->cfiq->BufWriteTimeoutMax = 0; |
| 210 | } |
| 211 | |
| 212 | static void fixup_at49bv640dx_lock(struct mtd_info *mtd) |
| 213 | { |
| 214 | struct map_info *map = mtd->priv; |
| 215 | struct cfi_private *cfi = map->fldrv_priv; |
| 216 | struct cfi_pri_intelext *cfip = cfi->cmdset_priv; |
| 217 | |
| 218 | cfip->FeatureSupport |= (1 << 5); |
| 219 | mtd->flags |= MTD_POWERUP_LOCK; |
| 220 | } |
| 221 | |
| 222 | #ifdef CMDSET0001_DISABLE_ERASE_SUSPEND_ON_WRITE |
| 223 | /* Some Intel Strata Flash prior to FPO revision C has bugs in this area */ |
| 224 | static void fixup_intel_strataflash(struct mtd_info *mtd) |
| 225 | { |
| 226 | struct map_info *map = mtd->priv; |
| 227 | struct cfi_private *cfi = map->fldrv_priv; |
| 228 | struct cfi_pri_intelext *extp = cfi->cmdset_priv; |
| 229 | |
| 230 | printk(KERN_WARNING "cfi_cmdset_0001: Suspend " |
| 231 | "erase on write disabled.\n"); |
| 232 | extp->SuspendCmdSupport &= ~1; |
| 233 | } |
| 234 | #endif |
| 235 | |
| 236 | #ifdef CMDSET0001_DISABLE_WRITE_SUSPEND |
| 237 | static void fixup_no_write_suspend(struct mtd_info *mtd) |
| 238 | { |
| 239 | struct map_info *map = mtd->priv; |
| 240 | struct cfi_private *cfi = map->fldrv_priv; |
| 241 | struct cfi_pri_intelext *cfip = cfi->cmdset_priv; |
| 242 | |
| 243 | if (cfip && (cfip->FeatureSupport&4)) { |
| 244 | cfip->FeatureSupport &= ~4; |
| 245 | printk(KERN_WARNING "cfi_cmdset_0001: write suspend disabled\n"); |
| 246 | } |
| 247 | } |
| 248 | #endif |
| 249 | |
| 250 | static void fixup_st_m28w320ct(struct mtd_info *mtd) |
| 251 | { |
| 252 | struct map_info *map = mtd->priv; |
| 253 | struct cfi_private *cfi = map->fldrv_priv; |
| 254 | |
| 255 | cfi->cfiq->BufWriteTimeoutTyp = 0; /* Not supported */ |
| 256 | cfi->cfiq->BufWriteTimeoutMax = 0; /* Not supported */ |
| 257 | } |
| 258 | |
| 259 | static void fixup_st_m28w320cb(struct mtd_info *mtd) |
| 260 | { |
| 261 | struct map_info *map = mtd->priv; |
| 262 | struct cfi_private *cfi = map->fldrv_priv; |
| 263 | |
| 264 | /* Note this is done after the region info is endian swapped */ |
| 265 | cfi->cfiq->EraseRegionInfo[1] = |
| 266 | (cfi->cfiq->EraseRegionInfo[1] & 0xffff0000) | 0x3e; |
| 267 | }; |
| 268 | |
| 269 | static int is_LH28F640BF(struct cfi_private *cfi) |
| 270 | { |
| 271 | /* Sharp LH28F640BF Family */ |
| 272 | if (cfi->mfr == CFI_MFR_SHARP && ( |
| 273 | cfi->id == LH28F640BFHE_PTTL90 || cfi->id == LH28F640BFHE_PBTL90 || |
| 274 | cfi->id == LH28F640BFHE_PTTL70A || cfi->id == LH28F640BFHE_PBTL70A)) |
| 275 | return 1; |
| 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | static void fixup_LH28F640BF(struct mtd_info *mtd) |
| 280 | { |
| 281 | struct map_info *map = mtd->priv; |
| 282 | struct cfi_private *cfi = map->fldrv_priv; |
| 283 | struct cfi_pri_intelext *extp = cfi->cmdset_priv; |
| 284 | |
| 285 | /* Reset the Partition Configuration Register on LH28F640BF |
| 286 | * to a single partition (PCR = 0x000): PCR is embedded into A0-A15. */ |
| 287 | if (is_LH28F640BF(cfi)) { |
| 288 | printk(KERN_INFO "Reset Partition Config. Register: 1 Partition of 4 planes\n"); |
| 289 | map_write(map, CMD(0x60), 0); |
| 290 | map_write(map, CMD(0x04), 0); |
| 291 | |
| 292 | /* We have set one single partition thus |
| 293 | * Simultaneous Operations are not allowed */ |
| 294 | printk(KERN_INFO "cfi_cmdset_0001: Simultaneous Operations disabled\n"); |
| 295 | extp->FeatureSupport &= ~512; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | static void fixup_use_point(struct mtd_info *mtd) |
| 300 | { |
| 301 | struct map_info *map = mtd->priv; |
| 302 | if (!mtd->_point && map_is_linear(map)) { |
| 303 | mtd->_point = cfi_intelext_point; |
| 304 | mtd->_unpoint = cfi_intelext_unpoint; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | static void fixup_use_write_buffers(struct mtd_info *mtd) |
| 309 | { |
| 310 | struct map_info *map = mtd->priv; |
| 311 | struct cfi_private *cfi = map->fldrv_priv; |
| 312 | if (cfi->cfiq->BufWriteTimeoutTyp) { |
| 313 | printk(KERN_INFO "Using buffer write method\n" ); |
| 314 | mtd->_write = cfi_intelext_write_buffers; |
| 315 | mtd->_writev = cfi_intelext_writev; |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | /* |
| 320 | * Some chips power-up with all sectors locked by default. |
| 321 | */ |
| 322 | static void fixup_unlock_powerup_lock(struct mtd_info *mtd) |
| 323 | { |
| 324 | struct map_info *map = mtd->priv; |
| 325 | struct cfi_private *cfi = map->fldrv_priv; |
| 326 | struct cfi_pri_intelext *cfip = cfi->cmdset_priv; |
| 327 | |
| 328 | if (cfip->FeatureSupport&32) { |
| 329 | printk(KERN_INFO "Using auto-unlock on power-up/resume\n" ); |
| 330 | mtd->flags |= MTD_POWERUP_LOCK; |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | static struct cfi_fixup cfi_fixup_table[] = { |
| 335 | { CFI_MFR_ATMEL, CFI_ID_ANY, fixup_convert_atmel_pri }, |
| 336 | { CFI_MFR_ATMEL, AT49BV640D, fixup_at49bv640dx_lock }, |
| 337 | { CFI_MFR_ATMEL, AT49BV640DT, fixup_at49bv640dx_lock }, |
| 338 | #ifdef CMDSET0001_DISABLE_ERASE_SUSPEND_ON_WRITE |
| 339 | { CFI_MFR_ANY, CFI_ID_ANY, fixup_intel_strataflash }, |
| 340 | #endif |
| 341 | #ifdef CMDSET0001_DISABLE_WRITE_SUSPEND |
| 342 | { CFI_MFR_ANY, CFI_ID_ANY, fixup_no_write_suspend }, |
| 343 | #endif |
| 344 | #if !FORCE_WORD_WRITE |
| 345 | { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_write_buffers }, |
| 346 | #endif |
| 347 | { CFI_MFR_ST, 0x00ba, /* M28W320CT */ fixup_st_m28w320ct }, |
| 348 | { CFI_MFR_ST, 0x00bb, /* M28W320CB */ fixup_st_m28w320cb }, |
| 349 | { CFI_MFR_INTEL, CFI_ID_ANY, fixup_unlock_powerup_lock }, |
| 350 | { CFI_MFR_SHARP, CFI_ID_ANY, fixup_unlock_powerup_lock }, |
| 351 | { CFI_MFR_SHARP, CFI_ID_ANY, fixup_LH28F640BF }, |
| 352 | { 0, 0, NULL } |
| 353 | }; |
| 354 | |
| 355 | static struct cfi_fixup jedec_fixup_table[] = { |
| 356 | { CFI_MFR_INTEL, I82802AB, fixup_use_fwh_lock }, |
| 357 | { CFI_MFR_INTEL, I82802AC, fixup_use_fwh_lock }, |
| 358 | { CFI_MFR_ST, M50LPW080, fixup_use_fwh_lock }, |
| 359 | { CFI_MFR_ST, M50FLW080A, fixup_use_fwh_lock }, |
| 360 | { CFI_MFR_ST, M50FLW080B, fixup_use_fwh_lock }, |
| 361 | { 0, 0, NULL } |
| 362 | }; |
| 363 | static struct cfi_fixup fixup_table[] = { |
| 364 | /* The CFI vendor ids and the JEDEC vendor IDs appear |
| 365 | * to be common. It is like the devices id's are as |
| 366 | * well. This table is to pick all cases where |
| 367 | * we know that is the case. |
| 368 | */ |
| 369 | { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_point }, |
| 370 | { 0, 0, NULL } |
| 371 | }; |
| 372 | |
| 373 | static void cfi_fixup_major_minor(struct cfi_private *cfi, |
| 374 | struct cfi_pri_intelext *extp) |
| 375 | { |
| 376 | if (cfi->mfr == CFI_MFR_INTEL && |
| 377 | cfi->id == PF38F4476 && extp->MinorVersion == '3') |
| 378 | extp->MinorVersion = '1'; |
| 379 | } |
| 380 | |
| 381 | static int cfi_is_micron_28F00AP30(struct cfi_private *cfi, struct flchip *chip) |
| 382 | { |
| 383 | /* |
| 384 | * Micron(was Numonyx) 1Gbit bottom boot are buggy w.r.t |
| 385 | * Erase Supend for their small Erase Blocks(0x8000) |
| 386 | */ |
| 387 | if (cfi->mfr == CFI_MFR_INTEL && cfi->id == M28F00AP30) |
| 388 | return 1; |
| 389 | return 0; |
| 390 | } |
| 391 | |
| 392 | static inline struct cfi_pri_intelext * |
| 393 | read_pri_intelext(struct map_info *map, __u16 adr) |
| 394 | { |
| 395 | struct cfi_private *cfi = map->fldrv_priv; |
| 396 | struct cfi_pri_intelext *extp; |
| 397 | unsigned int extra_size = 0; |
| 398 | unsigned int extp_size = sizeof(*extp); |
| 399 | |
| 400 | again: |
| 401 | extp = (struct cfi_pri_intelext *)cfi_read_pri(map, adr, extp_size, "Intel/Sharp"); |
| 402 | if (!extp) |
| 403 | return NULL; |
| 404 | |
| 405 | cfi_fixup_major_minor(cfi, extp); |
| 406 | |
| 407 | if (extp->MajorVersion != '1' || |
| 408 | (extp->MinorVersion < '0' || extp->MinorVersion > '5')) { |
| 409 | printk(KERN_ERR " Unknown Intel/Sharp Extended Query " |
| 410 | "version %c.%c.\n", extp->MajorVersion, |
| 411 | extp->MinorVersion); |
| 412 | kfree(extp); |
| 413 | return NULL; |
| 414 | } |
| 415 | |
| 416 | /* Do some byteswapping if necessary */ |
| 417 | extp->FeatureSupport = le32_to_cpu(extp->FeatureSupport); |
| 418 | extp->BlkStatusRegMask = le16_to_cpu(extp->BlkStatusRegMask); |
| 419 | extp->ProtRegAddr = le16_to_cpu(extp->ProtRegAddr); |
| 420 | |
| 421 | if (extp->MinorVersion >= '0') { |
| 422 | extra_size = 0; |
| 423 | |
| 424 | /* Protection Register info */ |
| 425 | if (extp->NumProtectionFields) { |
| 426 | struct cfi_intelext_otpinfo *otp = |
| 427 | (struct cfi_intelext_otpinfo *)&extp->extra[0]; |
| 428 | |
| 429 | extra_size += (extp->NumProtectionFields - 1) * |
| 430 | sizeof(struct cfi_intelext_otpinfo); |
| 431 | |
| 432 | if (extp_size >= sizeof(*extp) + extra_size) { |
| 433 | int i; |
| 434 | |
| 435 | /* Do some byteswapping if necessary */ |
| 436 | for (i = 0; i < extp->NumProtectionFields - 1; i++) { |
| 437 | otp->ProtRegAddr = le32_to_cpu(otp->ProtRegAddr); |
| 438 | otp->FactGroups = le16_to_cpu(otp->FactGroups); |
| 439 | otp->UserGroups = le16_to_cpu(otp->UserGroups); |
| 440 | otp++; |
| 441 | } |
| 442 | } |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | if (extp->MinorVersion >= '1') { |
| 447 | /* Burst Read info */ |
| 448 | extra_size += 2; |
| 449 | if (extp_size < sizeof(*extp) + extra_size) |
| 450 | goto need_more; |
| 451 | extra_size += extp->extra[extra_size - 1]; |
| 452 | } |
| 453 | |
| 454 | if (extp->MinorVersion >= '3') { |
| 455 | int nb_parts, i; |
| 456 | |
| 457 | /* Number of hardware-partitions */ |
| 458 | extra_size += 1; |
| 459 | if (extp_size < sizeof(*extp) + extra_size) |
| 460 | goto need_more; |
| 461 | nb_parts = extp->extra[extra_size - 1]; |
| 462 | |
| 463 | /* skip the sizeof(partregion) field in CFI 1.4 */ |
| 464 | if (extp->MinorVersion >= '4') |
| 465 | extra_size += 2; |
| 466 | |
| 467 | for (i = 0; i < nb_parts; i++) { |
| 468 | struct cfi_intelext_regioninfo *rinfo; |
| 469 | rinfo = (struct cfi_intelext_regioninfo *)&extp->extra[extra_size]; |
| 470 | extra_size += sizeof(*rinfo); |
| 471 | if (extp_size < sizeof(*extp) + extra_size) |
| 472 | goto need_more; |
| 473 | rinfo->NumIdentPartitions=le16_to_cpu(rinfo->NumIdentPartitions); |
| 474 | extra_size += (rinfo->NumBlockTypes - 1) |
| 475 | * sizeof(struct cfi_intelext_blockinfo); |
| 476 | } |
| 477 | |
| 478 | if (extp->MinorVersion >= '4') |
| 479 | extra_size += sizeof(struct cfi_intelext_programming_regioninfo); |
| 480 | |
| 481 | if (extp_size < sizeof(*extp) + extra_size) { |
| 482 | need_more: |
| 483 | extp_size = sizeof(*extp) + extra_size; |
| 484 | kfree(extp); |
| 485 | if (extp_size > 4096) { |
| 486 | printk(KERN_ERR |
| 487 | "%s: cfi_pri_intelext is too fat\n", |
| 488 | __func__); |
| 489 | return NULL; |
| 490 | } |
| 491 | goto again; |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | return extp; |
| 496 | } |
| 497 | |
| 498 | struct mtd_info *cfi_cmdset_0001(struct map_info *map, int primary) |
| 499 | { |
| 500 | struct cfi_private *cfi = map->fldrv_priv; |
| 501 | struct mtd_info *mtd; |
| 502 | int i; |
| 503 | |
| 504 | mtd = kzalloc_obj(*mtd); |
| 505 | if (!mtd) |
| 506 | return NULL; |
| 507 | mtd->priv = map; |
| 508 | mtd->type = MTD_NORFLASH; |
| 509 | |
| 510 | /* Fill in the default mtd operations */ |
| 511 | mtd->_erase = cfi_intelext_erase_varsize; |
| 512 | mtd->_read = cfi_intelext_read; |
| 513 | mtd->_write = cfi_intelext_write_words; |
| 514 | mtd->_sync = cfi_intelext_sync; |
| 515 | mtd->_lock = cfi_intelext_lock; |
| 516 | mtd->_unlock = cfi_intelext_unlock; |
| 517 | mtd->_is_locked = cfi_intelext_is_locked; |
| 518 | mtd->_suspend = cfi_intelext_suspend; |
| 519 | mtd->_resume = cfi_intelext_resume; |
| 520 | mtd->flags = MTD_CAP_NORFLASH; |
| 521 | mtd->name = map->name; |
| 522 | mtd->writesize = 1; |
| 523 | mtd->writebufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize; |
| 524 | |
| 525 | mtd->reboot_notifier.notifier_call = cfi_intelext_reboot; |
| 526 | |
| 527 | if (cfi->cfi_mode == CFI_MODE_CFI) { |
| 528 | /* |
| 529 | * It's a real CFI chip, not one for which the probe |
| 530 | * routine faked a CFI structure. So we read the feature |
| 531 | * table from it. |
| 532 | */ |
| 533 | __u16 adr = primary?cfi->cfiq->P_ADR:cfi->cfiq->A_ADR; |
| 534 | struct cfi_pri_intelext *extp; |
| 535 | |
| 536 | extp = read_pri_intelext(map, adr); |
| 537 | if (!extp) { |
| 538 | kfree(mtd); |
| 539 | return NULL; |
| 540 | } |
| 541 | |
| 542 | /* Install our own private info structure */ |
| 543 | cfi->cmdset_priv = extp; |
| 544 | |
| 545 | cfi_fixup(mtd, cfi_fixup_table); |
| 546 | |
| 547 | #ifdef DEBUG_CFI_FEATURES |
| 548 | /* Tell the user about it in lots of lovely detail */ |
| 549 | cfi_tell_features(extp); |
| 550 | #endif |
| 551 | |
| 552 | if(extp->SuspendCmdSupport & 1) { |
| 553 | printk(KERN_NOTICE "cfi_cmdset_0001: Erase suspend on write enabled\n"); |
| 554 | } |
| 555 | } |
| 556 | else if (cfi->cfi_mode == CFI_MODE_JEDEC) { |
| 557 | /* Apply jedec specific fixups */ |
| 558 | cfi_fixup(mtd, jedec_fixup_table); |
| 559 | } |
| 560 | /* Apply generic fixups */ |
| 561 | cfi_fixup(mtd, fixup_table); |
| 562 | |
| 563 | for (i=0; i< cfi->numchips; i++) { |
| 564 | if (cfi->cfiq->WordWriteTimeoutTyp) |
| 565 | cfi->chips[i].word_write_time = |
| 566 | 1<<cfi->cfiq->WordWriteTimeoutTyp; |
| 567 | else |
| 568 | cfi->chips[i].word_write_time = 50000; |
| 569 | |
| 570 | if (cfi->cfiq->BufWriteTimeoutTyp) |
| 571 | cfi->chips[i].buffer_write_time = |
| 572 | 1<<cfi->cfiq->BufWriteTimeoutTyp; |
| 573 | /* No default; if it isn't specified, we won't use it */ |
| 574 | |
| 575 | if (cfi->cfiq->BlockEraseTimeoutTyp) |
| 576 | cfi->chips[i].erase_time = |
| 577 | 1000<<cfi->cfiq->BlockEraseTimeoutTyp; |
| 578 | else |
| 579 | cfi->chips[i].erase_time = 2000000; |
| 580 | |
| 581 | if (cfi->cfiq->WordWriteTimeoutTyp && |
| 582 | cfi->cfiq->WordWriteTimeoutMax) |
| 583 | cfi->chips[i].word_write_time_max = |
| 584 | 1<<(cfi->cfiq->WordWriteTimeoutTyp + |
| 585 | cfi->cfiq->WordWriteTimeoutMax); |
| 586 | else |
| 587 | cfi->chips[i].word_write_time_max = 50000 * 8; |
| 588 | |
| 589 | if (cfi->cfiq->BufWriteTimeoutTyp && |
| 590 | cfi->cfiq->BufWriteTimeoutMax) |
| 591 | cfi->chips[i].buffer_write_time_max = |
| 592 | 1<<(cfi->cfiq->BufWriteTimeoutTyp + |
| 593 | cfi->cfiq->BufWriteTimeoutMax); |
| 594 | |
| 595 | if (cfi->cfiq->BlockEraseTimeoutTyp && |
| 596 | cfi->cfiq->BlockEraseTimeoutMax) |
| 597 | cfi->chips[i].erase_time_max = |
| 598 | 1000<<(cfi->cfiq->BlockEraseTimeoutTyp + |
| 599 | cfi->cfiq->BlockEraseTimeoutMax); |
| 600 | else |
| 601 | cfi->chips[i].erase_time_max = 2000000 * 8; |
| 602 | |
| 603 | cfi->chips[i].ref_point_counter = 0; |
| 604 | init_waitqueue_head(&(cfi->chips[i].wq)); |
| 605 | } |
| 606 | |
| 607 | map->fldrv = &cfi_intelext_chipdrv; |
| 608 | |
| 609 | return cfi_intelext_setup(mtd); |
| 610 | } |
| 611 | struct mtd_info *cfi_cmdset_0003(struct map_info *map, int primary) __attribute__((alias("cfi_cmdset_0001"))); |
| 612 | struct mtd_info *cfi_cmdset_0200(struct map_info *map, int primary) __attribute__((alias("cfi_cmdset_0001"))); |
| 613 | EXPORT_SYMBOL_GPL(cfi_cmdset_0001); |
| 614 | EXPORT_SYMBOL_GPL(cfi_cmdset_0003); |
| 615 | EXPORT_SYMBOL_GPL(cfi_cmdset_0200); |
| 616 | |
| 617 | static struct mtd_info *cfi_intelext_setup(struct mtd_info *mtd) |
| 618 | { |
| 619 | struct map_info *map = mtd->priv; |
| 620 | struct cfi_private *cfi = map->fldrv_priv; |
| 621 | unsigned long offset = 0; |
| 622 | int i,j; |
| 623 | unsigned long devsize = (1<<cfi->cfiq->DevSize) * cfi->interleave; |
| 624 | |
| 625 | //printk(KERN_DEBUG "number of CFI chips: %d\n", cfi->numchips); |
| 626 | |
| 627 | mtd->size = devsize * cfi->numchips; |
| 628 | |
| 629 | mtd->numeraseregions = cfi->cfiq->NumEraseRegions * cfi->numchips; |
| 630 | mtd->eraseregions = kzalloc_objs(struct mtd_erase_region_info, |
| 631 | mtd->numeraseregions); |
| 632 | if (!mtd->eraseregions) |
| 633 | goto setup_err; |
| 634 | |
| 635 | for (i=0; i<cfi->cfiq->NumEraseRegions; i++) { |
| 636 | unsigned long ernum, ersize; |
| 637 | ersize = ((cfi->cfiq->EraseRegionInfo[i] >> 8) & ~0xff) * cfi->interleave; |
| 638 | ernum = (cfi->cfiq->EraseRegionInfo[i] & 0xffff) + 1; |
| 639 | |
| 640 | if (mtd->erasesize < ersize) { |
| 641 | mtd->erasesize = ersize; |
| 642 | } |
| 643 | for (j=0; j<cfi->numchips; j++) { |
| 644 | mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].offset = (j*devsize)+offset; |
| 645 | mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].erasesize = ersize; |
| 646 | mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].numblocks = ernum; |
| 647 | mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].lockmap = kmalloc(ernum / 8 + 1, GFP_KERNEL); |
| 648 | if (!mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].lockmap) |
| 649 | goto setup_err; |
| 650 | } |
| 651 | offset += (ersize * ernum); |
| 652 | } |
| 653 | |
| 654 | if (offset != devsize) { |
| 655 | /* Argh */ |
| 656 | printk(KERN_WARNING "Sum of regions (%lx) != total size of set of interleaved chips (%lx)\n", offset, devsize); |
| 657 | goto setup_err; |
| 658 | } |
| 659 | |
| 660 | for (i=0; i<mtd->numeraseregions;i++){ |
| 661 | printk(KERN_DEBUG "erase region %d: offset=0x%llx,size=0x%x,blocks=%d\n", |
| 662 | i,(unsigned long long)mtd->eraseregions[i].offset, |
| 663 | mtd->eraseregions[i].erasesize, |
| 664 | mtd->eraseregions[i].numblocks); |
| 665 | } |
| 666 | |
| 667 | #ifdef CONFIG_MTD_OTP |
| 668 | mtd->_read_fact_prot_reg = cfi_intelext_read_fact_prot_reg; |
| 669 | mtd->_read_user_prot_reg = cfi_intelext_read_user_prot_reg; |
| 670 | mtd->_write_user_prot_reg = cfi_intelext_write_user_prot_reg; |
| 671 | mtd->_lock_user_prot_reg = cfi_intelext_lock_user_prot_reg; |
| 672 | mtd->_get_fact_prot_info = cfi_intelext_get_fact_prot_info; |
| 673 | mtd->_get_user_prot_info = cfi_intelext_get_user_prot_info; |
| 674 | #endif |
| 675 | |
| 676 | /* This function has the potential to distort the reality |
| 677 | a bit and therefore should be called last. */ |
| 678 | if (cfi_intelext_partition_fixup(mtd, &cfi) != 0) |
| 679 | goto setup_err; |
| 680 | |
| 681 | __module_get(THIS_MODULE); |
| 682 | register_reboot_notifier(&mtd->reboot_notifier); |
| 683 | return mtd; |
| 684 | |
| 685 | setup_err: |
| 686 | if (mtd->eraseregions) |
| 687 | for (i=0; i<cfi->cfiq->NumEraseRegions; i++) |
| 688 | for (j=0; j<cfi->numchips; j++) |
| 689 | kfree(mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].lockmap); |
| 690 | kfree(mtd->eraseregions); |
| 691 | kfree(mtd); |
| 692 | kfree(cfi->cmdset_priv); |
| 693 | return NULL; |
| 694 | } |
| 695 | |
| 696 | static int cfi_intelext_partition_fixup(struct mtd_info *mtd, |
| 697 | struct cfi_private **pcfi) |
| 698 | { |
| 699 | struct map_info *map = mtd->priv; |
| 700 | struct cfi_private *cfi = *pcfi; |
| 701 | struct cfi_pri_intelext *extp = cfi->cmdset_priv; |
| 702 | |
| 703 | /* |
| 704 | * Probing of multi-partition flash chips. |
| 705 | * |
| 706 | * To support multiple partitions when available, we simply arrange |
| 707 | * for each of them to have their own flchip structure even if they |
| 708 | * are on the same physical chip. This means completely recreating |
| 709 | * a new cfi_private structure right here which is a blatent code |
| 710 | * layering violation, but this is still the least intrusive |
| 711 | * arrangement at this point. This can be rearranged in the future |
| 712 | * if someone feels motivated enough. --nico |
| 713 | */ |
| 714 | if (extp && extp->MajorVersion == '1' && extp->MinorVersion >= '3' |
| 715 | && extp->FeatureSupport & (1 << 9)) { |
| 716 | int offs = 0; |
| 717 | struct cfi_private *newcfi; |
| 718 | struct flchip *chip; |
| 719 | struct flchip_shared *shared; |
| 720 | int numregions, numparts, partshift, numvirtchips, i, j; |
| 721 | |
| 722 | /* Protection Register info */ |
| 723 | if (extp->NumProtectionFields) |
| 724 | offs = (extp->NumProtectionFields - 1) * |
| 725 | sizeof(struct cfi_intelext_otpinfo); |
| 726 | |
| 727 | /* Burst Read info */ |
| 728 | offs += extp->extra[offs+1]+2; |
| 729 | |
| 730 | /* Number of partition regions */ |
| 731 | numregions = extp->extra[offs]; |
| 732 | offs += 1; |
| 733 | |
| 734 | /* skip the sizeof(partregion) field in CFI 1.4 */ |
| 735 | if (extp->MinorVersion >= '4') |
| 736 | offs += 2; |
| 737 | |
| 738 | /* Number of hardware partitions */ |
| 739 | numparts = 0; |
| 740 | for (i = 0; i < numregions; i++) { |
| 741 | struct cfi_intelext_regioninfo *rinfo; |
| 742 | rinfo = (struct cfi_intelext_regioninfo *)&extp->extra[offs]; |
| 743 | numparts += rinfo->NumIdentPartitions; |
| 744 | offs += sizeof(*rinfo) |
| 745 | + (rinfo->NumBlockTypes - 1) * |
| 746 | sizeof(struct cfi_intelext_blockinfo); |
| 747 | } |
| 748 | |
| 749 | if (!numparts) |
| 750 | numparts = 1; |
| 751 | |
| 752 | /* Programming Region info */ |
| 753 | if (extp->MinorVersion >= '4') { |
| 754 | struct cfi_intelext_programming_regioninfo *prinfo; |
| 755 | prinfo = (struct cfi_intelext_programming_regioninfo *)&extp->extra[offs]; |
| 756 | mtd->writesize = cfi->interleave << prinfo->ProgRegShift; |
| 757 | mtd->flags &= ~MTD_BIT_WRITEABLE; |
| 758 | printk(KERN_DEBUG "%s: program region size/ctrl_valid/ctrl_inval = %d/%d/%d\n", |
| 759 | map->name, mtd->writesize, |
| 760 | cfi->interleave * prinfo->ControlValid, |
| 761 | cfi->interleave * prinfo->ControlInvalid); |
| 762 | } |
| 763 | |
| 764 | /* |
| 765 | * All functions below currently rely on all chips having |
| 766 | * the same geometry so we'll just assume that all hardware |
| 767 | * partitions are of the same size too. |
| 768 | */ |
| 769 | partshift = cfi->chipshift - __ffs(numparts); |
| 770 | |
| 771 | if ((1 << partshift) < mtd->erasesize) { |
| 772 | printk( KERN_ERR |
| 773 | "%s: bad number of hw partitions (%d)\n", |
| 774 | __func__, numparts); |
| 775 | return -EINVAL; |
| 776 | } |
| 777 | |
| 778 | numvirtchips = cfi->numchips * numparts; |
| 779 | newcfi = kmalloc_flex(*newcfi, chips, numvirtchips); |
| 780 | if (!newcfi) |
| 781 | return -ENOMEM; |
| 782 | shared = kmalloc_objs(struct flchip_shared, cfi->numchips); |
| 783 | if (!shared) { |
| 784 | kfree(newcfi); |
| 785 | return -ENOMEM; |
| 786 | } |
| 787 | memcpy(newcfi, cfi, sizeof(struct cfi_private)); |
| 788 | newcfi->numchips = numvirtchips; |
| 789 | newcfi->chipshift = partshift; |
| 790 | |
| 791 | chip = &newcfi->chips[0]; |
| 792 | for (i = 0; i < cfi->numchips; i++) { |
| 793 | shared[i].writing = shared[i].erasing = NULL; |
| 794 | mutex_init(&shared[i].lock); |
| 795 | for (j = 0; j < numparts; j++) { |
| 796 | *chip = cfi->chips[i]; |
| 797 | chip->start += j << partshift; |
| 798 | chip->priv = &shared[i]; |
| 799 | /* those should be reset too since |
| 800 | they create memory references. */ |
| 801 | init_waitqueue_head(&chip->wq); |
| 802 | mutex_init(&chip->mutex); |
| 803 | chip++; |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | printk(KERN_DEBUG "%s: %d set(s) of %d interleaved chips " |
| 808 | "--> %d partitions of %d KiB\n", |
| 809 | map->name, cfi->numchips, cfi->interleave, |
| 810 | newcfi->numchips, 1<<(newcfi->chipshift-10)); |
| 811 | |
| 812 | map->fldrv_priv = newcfi; |
| 813 | *pcfi = newcfi; |
| 814 | kfree(cfi); |
| 815 | } |
| 816 | |
| 817 | return 0; |
| 818 | } |
| 819 | |
| 820 | /* |
| 821 | * *********** CHIP ACCESS FUNCTIONS *********** |
| 822 | */ |
| 823 | static int chip_ready (struct map_info *map, struct flchip *chip, unsigned long adr, int mode) |
| 824 | { |
| 825 | DECLARE_WAITQUEUE(wait, current); |
| 826 | struct cfi_private *cfi = map->fldrv_priv; |
| 827 | map_word status, status_OK = CMD(0x80), status_PWS = CMD(0x01); |
| 828 | struct cfi_pri_intelext *cfip = cfi->cmdset_priv; |
| 829 | unsigned long timeo = jiffies + HZ; |
| 830 | |
| 831 | /* Prevent setting state FL_SYNCING for chip in suspended state. */ |
| 832 | if (mode == FL_SYNCING && chip->oldstate != FL_READY) |
| 833 | goto sleep; |
| 834 | |
| 835 | switch (chip->state) { |
| 836 | |
| 837 | case FL_STATUS: |
| 838 | for (;;) { |
| 839 | status = map_read(map, adr); |
| 840 | if (map_word_andequal(map, status, status_OK, status_OK)) |
| 841 | break; |
| 842 | |
| 843 | /* At this point we're fine with write operations |
| 844 | in other partitions as they don't conflict. */ |
| 845 | if (chip->priv && map_word_andequal(map, status, status_PWS, status_PWS)) |
| 846 | break; |
| 847 | |
| 848 | mutex_unlock(&chip->mutex); |
| 849 | cfi_udelay(1); |
| 850 | mutex_lock(&chip->mutex); |
| 851 | /* Someone else might have been playing with it. */ |
| 852 | return -EAGAIN; |
| 853 | } |
| 854 | fallthrough; |
| 855 | case FL_READY: |
| 856 | case FL_CFI_QUERY: |
| 857 | case FL_JEDEC_QUERY: |
| 858 | return 0; |
| 859 | |
| 860 | case FL_ERASING: |
| 861 | if (!cfip || |
| 862 | !(cfip->FeatureSupport & 2) || |
| 863 | !(mode == FL_READY || mode == FL_POINT || |
| 864 | (mode == FL_WRITING && (cfip->SuspendCmdSupport & 1)))) |
| 865 | goto sleep; |
| 866 | |
| 867 | /* Do not allow suspend iff read/write to EB address */ |
| 868 | if ((adr & chip->in_progress_block_mask) == |
| 869 | chip->in_progress_block_addr) |
| 870 | goto sleep; |
| 871 | |
| 872 | /* do not suspend small EBs, buggy Micron Chips */ |
| 873 | if (cfi_is_micron_28F00AP30(cfi, chip) && |
| 874 | (chip->in_progress_block_mask == ~(0x8000-1))) |
| 875 | goto sleep; |
| 876 | |
| 877 | /* Erase suspend */ |
| 878 | map_write(map, CMD(0xB0), chip->in_progress_block_addr); |
| 879 | |
| 880 | /* If the flash has finished erasing, then 'erase suspend' |
| 881 | * appears to make some (28F320) flash devices switch to |
| 882 | * 'read' mode. Make sure that we switch to 'read status' |
| 883 | * mode so we get the right data. --rmk |
| 884 | */ |
| 885 | map_write(map, CMD(0x70), chip->in_progress_block_addr); |
| 886 | chip->oldstate = FL_ERASING; |
| 887 | chip->state = FL_ERASE_SUSPENDING; |
| 888 | chip->erase_suspended = 1; |
| 889 | for (;;) { |
| 890 | status = map_read(map, chip->in_progress_block_addr); |
| 891 | if (map_word_andequal(map, status, status_OK, status_OK)) |
| 892 | break; |
| 893 | |
| 894 | if (time_after(jiffies, timeo)) { |
| 895 | /* Urgh. Resume and pretend we weren't here. |
| 896 | * Make sure we're in 'read status' mode if it had finished */ |
| 897 | put_chip(map, chip, adr); |
| 898 | printk(KERN_ERR "%s: Chip not ready after erase " |
| 899 | "suspended: status = 0x%lx\n", map->name, status.x[0]); |
| 900 | return -EIO; |
| 901 | } |
| 902 | |
| 903 | mutex_unlock(&chip->mutex); |
| 904 | cfi_udelay(1); |
| 905 | mutex_lock(&chip->mutex); |
| 906 | /* Nobody will touch it while it's in state FL_ERASE_SUSPENDING. |
| 907 | So we can just loop here. */ |
| 908 | } |
| 909 | chip->state = FL_STATUS; |
| 910 | return 0; |
| 911 | |
| 912 | case FL_XIP_WHILE_ERASING: |
| 913 | if (mode != FL_READY && mode != FL_POINT && |
| 914 | (mode != FL_WRITING || !cfip || !(cfip->SuspendCmdSupport&1))) |
| 915 | goto sleep; |
| 916 | chip->oldstate = chip->state; |
| 917 | chip->state = FL_READY; |
| 918 | return 0; |
| 919 | |
| 920 | case FL_SHUTDOWN: |
| 921 | /* The machine is rebooting now,so no one can get chip anymore */ |
| 922 | return -EIO; |
| 923 | case FL_POINT: |
| 924 | /* Only if there's no operation suspended... */ |
| 925 | if (mode == FL_READY && chip->oldstate == FL_READY) |
| 926 | return 0; |
| 927 | fallthrough; |
| 928 | default: |
| 929 | sleep: |
| 930 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 931 | add_wait_queue(&chip->wq, &wait); |
| 932 | mutex_unlock(&chip->mutex); |
| 933 | schedule(); |
| 934 | remove_wait_queue(&chip->wq, &wait); |
| 935 | mutex_lock(&chip->mutex); |
| 936 | return -EAGAIN; |
| 937 | } |
| 938 | } |
| 939 | |
| 940 | static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode) |
| 941 | { |
| 942 | int ret; |
| 943 | DECLARE_WAITQUEUE(wait, current); |
| 944 | |
| 945 | retry: |
| 946 | if (chip->priv && |
| 947 | (mode == FL_WRITING || mode == FL_ERASING || mode == FL_OTP_WRITE |
| 948 | || mode == FL_SHUTDOWN) && chip->state != FL_SYNCING) { |
| 949 | /* |
| 950 | * OK. We have possibility for contention on the write/erase |
| 951 | * operations which are global to the real chip and not per |
| 952 | * partition. So let's fight it over in the partition which |
| 953 | * currently has authority on the operation. |
| 954 | * |
| 955 | * The rules are as follows: |
| 956 | * |
| 957 | * - any write operation must own shared->writing. |
| 958 | * |
| 959 | * - any erase operation must own _both_ shared->writing and |
| 960 | * shared->erasing. |
| 961 | * |
| 962 | * - contention arbitration is handled in the owner's context. |
| 963 | * |
| 964 | * The 'shared' struct can be read and/or written only when |
| 965 | * its lock is taken. |
| 966 | */ |
| 967 | struct flchip_shared *shared = chip->priv; |
| 968 | struct flchip *contender; |
| 969 | mutex_lock(&shared->lock); |
| 970 | contender = shared->writing; |
| 971 | if (contender && contender != chip) { |
| 972 | /* |
| 973 | * The engine to perform desired operation on this |
| 974 | * partition is already in use by someone else. |
| 975 | * Let's fight over it in the context of the chip |
| 976 | * currently using it. If it is possible to suspend, |
| 977 | * that other partition will do just that, otherwise |
| 978 | * it'll happily send us to sleep. In any case, when |
| 979 | * get_chip returns success we're clear to go ahead. |
| 980 | */ |
| 981 | ret = mutex_trylock(&contender->mutex); |
| 982 | mutex_unlock(&shared->lock); |
| 983 | if (!ret) |
| 984 | goto retry; |
| 985 | mutex_unlock(&chip->mutex); |
| 986 | ret = chip_ready(map, contender, contender->start, mode); |
| 987 | mutex_lock(&chip->mutex); |
| 988 | |
| 989 | if (ret == -EAGAIN) { |
| 990 | mutex_unlock(&contender->mutex); |
| 991 | goto retry; |
| 992 | } |
| 993 | if (ret) { |
| 994 | mutex_unlock(&contender->mutex); |
| 995 | return ret; |
| 996 | } |
| 997 | mutex_lock(&shared->lock); |
| 998 | |
| 999 | /* We should not own chip if it is already |
| 1000 | * in FL_SYNCING state. Put contender and retry. */ |
| 1001 | if (chip->state == FL_SYNCING) { |
| 1002 | put_chip(map, contender, contender->start); |
| 1003 | mutex_unlock(&contender->mutex); |
| 1004 | goto retry; |
| 1005 | } |
| 1006 | mutex_unlock(&contender->mutex); |
| 1007 | } |
| 1008 | |
| 1009 | /* Check if we already have suspended erase |
| 1010 | * on this chip. Sleep. */ |
| 1011 | if (mode == FL_ERASING && shared->erasing |
| 1012 | && shared->erasing->oldstate == FL_ERASING) { |
| 1013 | mutex_unlock(&shared->lock); |
| 1014 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 1015 | add_wait_queue(&chip->wq, &wait); |
| 1016 | mutex_unlock(&chip->mutex); |
| 1017 | schedule(); |
| 1018 | remove_wait_queue(&chip->wq, &wait); |
| 1019 | mutex_lock(&chip->mutex); |
| 1020 | goto retry; |
| 1021 | } |
| 1022 | |
| 1023 | /* We now own it */ |
| 1024 | shared->writing = chip; |
| 1025 | if (mode == FL_ERASING) |
| 1026 | shared->erasing = chip; |
| 1027 | mutex_unlock(&shared->lock); |
| 1028 | } |
| 1029 | ret = chip_ready(map, chip, adr, mode); |
| 1030 | if (ret == -EAGAIN) |
| 1031 | goto retry; |
| 1032 | |
| 1033 | return ret; |
| 1034 | } |
| 1035 | |
| 1036 | static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr) |
| 1037 | { |
| 1038 | struct cfi_private *cfi = map->fldrv_priv; |
| 1039 | |
| 1040 | if (chip->priv) { |
| 1041 | struct flchip_shared *shared = chip->priv; |
| 1042 | mutex_lock(&shared->lock); |
| 1043 | if (shared->writing == chip && chip->oldstate == FL_READY) { |
| 1044 | /* We own the ability to write, but we're done */ |
| 1045 | shared->writing = shared->erasing; |
| 1046 | if (shared->writing && shared->writing != chip) { |
| 1047 | /* give back ownership to who we loaned it from */ |
| 1048 | struct flchip *loaner = shared->writing; |
| 1049 | mutex_lock(&loaner->mutex); |
| 1050 | mutex_unlock(&shared->lock); |
| 1051 | mutex_unlock(&chip->mutex); |
| 1052 | put_chip(map, loaner, loaner->start); |
| 1053 | mutex_lock(&chip->mutex); |
| 1054 | mutex_unlock(&loaner->mutex); |
| 1055 | wake_up(&chip->wq); |
| 1056 | return; |
| 1057 | } |
| 1058 | shared->erasing = NULL; |
| 1059 | shared->writing = NULL; |
| 1060 | } else if (shared->erasing == chip && shared->writing != chip) { |
| 1061 | /* |
| 1062 | * We own the ability to erase without the ability |
| 1063 | * to write, which means the erase was suspended |
| 1064 | * and some other partition is currently writing. |
| 1065 | * Don't let the switch below mess things up since |
| 1066 | * we don't have ownership to resume anything. |
| 1067 | */ |
| 1068 | mutex_unlock(&shared->lock); |
| 1069 | wake_up(&chip->wq); |
| 1070 | return; |
| 1071 | } |
| 1072 | mutex_unlock(&shared->lock); |
| 1073 | } |
| 1074 | |
| 1075 | switch(chip->oldstate) { |
| 1076 | case FL_ERASING: |
| 1077 | /* What if one interleaved chip has finished and the |
| 1078 | other hasn't? The old code would leave the finished |
| 1079 | one in READY mode. That's bad, and caused -EROFS |
| 1080 | errors to be returned from do_erase_oneblock because |
| 1081 | that's the only bit it checked for at the time. |
| 1082 | As the state machine appears to explicitly allow |
| 1083 | sending the 0x70 (Read Status) command to an erasing |
| 1084 | chip and expecting it to be ignored, that's what we |
| 1085 | do. */ |
| 1086 | map_write(map, CMD(0xd0), chip->in_progress_block_addr); |
| 1087 | map_write(map, CMD(0x70), chip->in_progress_block_addr); |
| 1088 | chip->oldstate = FL_READY; |
| 1089 | chip->state = FL_ERASING; |
| 1090 | break; |
| 1091 | |
| 1092 | case FL_XIP_WHILE_ERASING: |
| 1093 | chip->state = chip->oldstate; |
| 1094 | chip->oldstate = FL_READY; |
| 1095 | break; |
| 1096 | |
| 1097 | case FL_READY: |
| 1098 | case FL_STATUS: |
| 1099 | case FL_JEDEC_QUERY: |
| 1100 | break; |
| 1101 | default: |
| 1102 | printk(KERN_ERR "%s: put_chip() called with oldstate %d!!\n", map->name, chip->oldstate); |
| 1103 | } |
| 1104 | wake_up(&chip->wq); |
| 1105 | } |
| 1106 | |
| 1107 | #ifdef CONFIG_MTD_XIP |
| 1108 | |
| 1109 | /* |
| 1110 | * No interrupt what so ever can be serviced while the flash isn't in array |
| 1111 | * mode. This is ensured by the xip_disable() and xip_enable() functions |
| 1112 | * enclosing any code path where the flash is known not to be in array mode. |
| 1113 | * And within a XIP disabled code path, only functions marked with __xipram |
| 1114 | * may be called and nothing else (it's a good thing to inspect generated |
| 1115 | * assembly to make sure inline functions were actually inlined and that gcc |
| 1116 | * didn't emit calls to its own support functions). Also configuring MTD CFI |
| 1117 | * support to a single buswidth and a single interleave is also recommended. |
| 1118 | */ |
| 1119 | |
| 1120 | static void xip_disable(struct map_info *map, struct flchip *chip, |
| 1121 | unsigned long adr) |
| 1122 | { |
| 1123 | /* TODO: chips with no XIP use should ignore and return */ |
| 1124 | (void) map_read(map, adr); /* ensure mmu mapping is up to date */ |
| 1125 | local_irq_disable(); |
| 1126 | } |
| 1127 | |
| 1128 | static void __xipram xip_enable(struct map_info *map, struct flchip *chip, |
| 1129 | unsigned long adr) |
| 1130 | { |
| 1131 | struct cfi_private *cfi = map->fldrv_priv; |
| 1132 | if (chip->state != FL_POINT && chip->state != FL_READY) { |
| 1133 | map_write(map, CMD(0xff), adr); |
| 1134 | chip->state = FL_READY; |
| 1135 | } |
| 1136 | (void) map_read(map, adr); |
| 1137 | xip_iprefetch(); |
| 1138 | local_irq_enable(); |
| 1139 | } |
| 1140 | |
| 1141 | /* |
| 1142 | * When a delay is required for the flash operation to complete, the |
| 1143 | * xip_wait_for_operation() function is polling for both the given timeout |
| 1144 | * and pending (but still masked) hardware interrupts. Whenever there is an |
| 1145 | * interrupt pending then the flash erase or write operation is suspended, |
| 1146 | * array mode restored and interrupts unmasked. Task scheduling might also |
| 1147 | * happen at that point. The CPU eventually returns from the interrupt or |
| 1148 | * the call to schedule() and the suspended flash operation is resumed for |
| 1149 | * the remaining of the delay period. |
| 1150 | * |
| 1151 | * Warning: this function _will_ fool interrupt latency tracing tools. |
| 1152 | */ |
| 1153 | |
| 1154 | static int __xipram xip_wait_for_operation( |
| 1155 | struct map_info *map, struct flchip *chip, |
| 1156 | unsigned long adr, unsigned int chip_op_time_max) |
| 1157 | { |
| 1158 | struct cfi_private *cfi = map->fldrv_priv; |
| 1159 | struct cfi_pri_intelext *cfip = cfi->cmdset_priv; |
| 1160 | map_word status, OK = CMD(0x80); |
| 1161 | unsigned long usec, suspended, start, done; |
| 1162 | flstate_t oldstate, newstate; |
| 1163 | |
| 1164 | start = xip_currtime(); |
| 1165 | usec = chip_op_time_max; |
| 1166 | if (usec == 0) |
| 1167 | usec = 500000; |
| 1168 | done = 0; |
| 1169 | |
| 1170 | do { |
| 1171 | cpu_relax(); |
| 1172 | if (xip_irqpending() && cfip && |
| 1173 | ((chip->state == FL_ERASING && (cfip->FeatureSupport&2)) || |
| 1174 | (chip->state == FL_WRITING && (cfip->FeatureSupport&4))) && |
| 1175 | (cfi_interleave_is_1(cfi) || chip->oldstate == FL_READY)) { |
| 1176 | /* |
| 1177 | * Let's suspend the erase or write operation when |
| 1178 | * supported. Note that we currently don't try to |
| 1179 | * suspend interleaved chips if there is already |
| 1180 | * another operation suspended (imagine what happens |
| 1181 | * when one chip was already done with the current |
| 1182 | * operation while another chip suspended it, then |
| 1183 | * we resume the whole thing at once). Yes, it |
| 1184 | * can happen! |
| 1185 | */ |
| 1186 | usec -= done; |
| 1187 | map_write(map, CMD(0xb0), adr); |
| 1188 | map_write(map, CMD(0x70), adr); |
| 1189 | suspended = xip_currtime(); |
| 1190 | do { |
| 1191 | if (xip_elapsed_since(suspended) > 100000) { |
| 1192 | /* |
| 1193 | * The chip doesn't want to suspend |
| 1194 | * after waiting for 100 msecs. |
| 1195 | * This is a critical error but there |
| 1196 | * is not much we can do here. |
| 1197 | */ |
| 1198 | return -EIO; |
| 1199 | } |
| 1200 | status = map_read(map, adr); |
| 1201 | } while (!map_word_andequal(map, status, OK, OK)); |
| 1202 | |
| 1203 | /* Suspend succeeded */ |
| 1204 | oldstate = chip->state; |
| 1205 | if (oldstate == FL_ERASING) { |
| 1206 | if (!map_word_bitsset(map, status, CMD(0x40))) |
| 1207 | break; |
| 1208 | newstate = FL_XIP_WHILE_ERASING; |
| 1209 | chip->erase_suspended = 1; |
| 1210 | } else { |
| 1211 | if (!map_word_bitsset(map, status, CMD(0x04))) |
| 1212 | break; |
| 1213 | newstate = FL_XIP_WHILE_WRITING; |
| 1214 | chip->write_suspended = 1; |
| 1215 | } |
| 1216 | chip->state = newstate; |
| 1217 | map_write(map, CMD(0xff), adr); |
| 1218 | (void) map_read(map, adr); |
| 1219 | xip_iprefetch(); |
| 1220 | local_irq_enable(); |
| 1221 | mutex_unlock(&chip->mutex); |
| 1222 | xip_iprefetch(); |
| 1223 | cond_resched(); |
| 1224 | |
| 1225 | /* |
| 1226 | * We're back. However someone else might have |
| 1227 | * decided to go write to the chip if we are in |
| 1228 | * a suspended erase state. If so let's wait |
| 1229 | * until it's done. |
| 1230 | */ |
| 1231 | mutex_lock(&chip->mutex); |
| 1232 | while (chip->state != newstate) { |
| 1233 | DECLARE_WAITQUEUE(wait, current); |
| 1234 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 1235 | add_wait_queue(&chip->wq, &wait); |
| 1236 | mutex_unlock(&chip->mutex); |
| 1237 | schedule(); |
| 1238 | remove_wait_queue(&chip->wq, &wait); |
| 1239 | mutex_lock(&chip->mutex); |
| 1240 | } |
| 1241 | /* Disallow XIP again */ |
| 1242 | local_irq_disable(); |
| 1243 | |
| 1244 | /* Resume the write or erase operation */ |
| 1245 | map_write(map, CMD(0xd0), adr); |
| 1246 | map_write(map, CMD(0x70), adr); |
| 1247 | chip->state = oldstate; |
| 1248 | start = xip_currtime(); |
| 1249 | } else if (usec >= 1000000/HZ) { |
| 1250 | /* |
| 1251 | * Try to save on CPU power when waiting delay |
| 1252 | * is at least a system timer tick period. |
| 1253 | * No need to be extremely accurate here. |
| 1254 | */ |
| 1255 | xip_cpu_idle(); |
| 1256 | } |
| 1257 | status = map_read(map, adr); |
| 1258 | done = xip_elapsed_since(start); |
| 1259 | } while (!map_word_andequal(map, status, OK, OK) |
| 1260 | && done < usec); |
| 1261 | |
| 1262 | return (done >= usec) ? -ETIME : 0; |
| 1263 | } |
| 1264 | |
| 1265 | /* |
| 1266 | * The INVALIDATE_CACHED_RANGE() macro is normally used in parallel while |
| 1267 | * the flash is actively programming or erasing since we have to poll for |
| 1268 | * the operation to complete anyway. We can't do that in a generic way with |
| 1269 | * a XIP setup so do it before the actual flash operation in this case |
| 1270 | * and stub it out from INVAL_CACHE_AND_WAIT. |
| 1271 | */ |
| 1272 | #define XIP_INVAL_CACHED_RANGE(map, from, size) \ |
| 1273 | INVALIDATE_CACHED_RANGE(map, from, size) |
| 1274 | |
| 1275 | #define INVAL_CACHE_AND_WAIT(map, chip, cmd_adr, inval_adr, inval_len, usec, usec_max) \ |
| 1276 | xip_wait_for_operation(map, chip, cmd_adr, usec_max) |
| 1277 | |
| 1278 | #else |
| 1279 | |
| 1280 | #define xip_disable(map, chip, adr) |
| 1281 | #define xip_enable(map, chip, adr) |
| 1282 | #define XIP_INVAL_CACHED_RANGE(x...) |
| 1283 | #define INVAL_CACHE_AND_WAIT inval_cache_and_wait_for_operation |
| 1284 | |
| 1285 | static int inval_cache_and_wait_for_operation( |
| 1286 | struct map_info *map, struct flchip *chip, |
| 1287 | unsigned long cmd_adr, unsigned long inval_adr, int inval_len, |
| 1288 | unsigned int chip_op_time, unsigned int chip_op_time_max) |
| 1289 | { |
| 1290 | struct cfi_private *cfi = map->fldrv_priv; |
| 1291 | map_word status, status_OK = CMD(0x80); |
| 1292 | int chip_state = chip->state; |
| 1293 | unsigned int timeo, sleep_time, reset_timeo; |
| 1294 | |
| 1295 | mutex_unlock(&chip->mutex); |
| 1296 | if (inval_len) |
| 1297 | INVALIDATE_CACHED_RANGE(map, inval_adr, inval_len); |
| 1298 | mutex_lock(&chip->mutex); |
| 1299 | |
| 1300 | timeo = chip_op_time_max; |
| 1301 | if (!timeo) |
| 1302 | timeo = 500000; |
| 1303 | reset_timeo = timeo; |
| 1304 | sleep_time = chip_op_time / 2; |
| 1305 | |
| 1306 | for (;;) { |
| 1307 | if (chip->state != chip_state) { |
| 1308 | /* Someone's suspended the operation: sleep */ |
| 1309 | DECLARE_WAITQUEUE(wait, current); |
| 1310 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 1311 | add_wait_queue(&chip->wq, &wait); |
| 1312 | mutex_unlock(&chip->mutex); |
| 1313 | schedule(); |
| 1314 | remove_wait_queue(&chip->wq, &wait); |
| 1315 | mutex_lock(&chip->mutex); |
| 1316 | continue; |
| 1317 | } |
| 1318 | |
| 1319 | status = map_read(map, cmd_adr); |
| 1320 | if (map_word_andequal(map, status, status_OK, status_OK)) |
| 1321 | break; |
| 1322 | |
| 1323 | if (chip->erase_suspended && chip_state == FL_ERASING) { |
| 1324 | /* Erase suspend occurred while sleep: reset timeout */ |
| 1325 | timeo = reset_timeo; |
| 1326 | chip->erase_suspended = 0; |
| 1327 | } |
| 1328 | if (chip->write_suspended && chip_state == FL_WRITING) { |
| 1329 | /* Write suspend occurred while sleep: reset timeout */ |
| 1330 | timeo = reset_timeo; |
| 1331 | chip->write_suspended = 0; |
| 1332 | } |
| 1333 | if (!timeo) { |
| 1334 | map_write(map, CMD(0x70), cmd_adr); |
| 1335 | chip->state = FL_STATUS; |
| 1336 | return -ETIME; |
| 1337 | } |
| 1338 | |
| 1339 | /* OK Still waiting. Drop the lock, wait a while and retry. */ |
| 1340 | mutex_unlock(&chip->mutex); |
| 1341 | if (sleep_time >= 1000000/HZ) { |
| 1342 | /* |
| 1343 | * Half of the normal delay still remaining |
| 1344 | * can be performed with a sleeping delay instead |
| 1345 | * of busy waiting. |
| 1346 | */ |
| 1347 | msleep(sleep_time/1000); |
| 1348 | timeo -= sleep_time; |
| 1349 | sleep_time = 1000000/HZ; |
| 1350 | } else { |
| 1351 | udelay(1); |
| 1352 | cond_resched(); |
| 1353 | timeo--; |
| 1354 | } |
| 1355 | mutex_lock(&chip->mutex); |
| 1356 | } |
| 1357 | |
| 1358 | /* Done and happy. */ |
| 1359 | chip->state = FL_STATUS; |
| 1360 | return 0; |
| 1361 | } |
| 1362 | |
| 1363 | #endif |
| 1364 | |
| 1365 | #define WAIT_TIMEOUT(map, chip, adr, udelay, udelay_max) \ |
| 1366 | INVAL_CACHE_AND_WAIT(map, chip, adr, 0, 0, udelay, udelay_max); |
| 1367 | |
| 1368 | |
| 1369 | static int do_point_onechip (struct map_info *map, struct flchip *chip, loff_t adr, size_t len) |
| 1370 | { |
| 1371 | unsigned long cmd_addr; |
| 1372 | struct cfi_private *cfi = map->fldrv_priv; |
| 1373 | int ret; |
| 1374 | |
| 1375 | adr += chip->start; |
| 1376 | |
| 1377 | /* Ensure cmd read/writes are aligned. */ |
| 1378 | cmd_addr = adr & ~(map_bankwidth(map)-1); |
| 1379 | |
| 1380 | mutex_lock(&chip->mutex); |
| 1381 | |
| 1382 | ret = get_chip(map, chip, cmd_addr, FL_POINT); |
| 1383 | |
| 1384 | if (!ret) { |
| 1385 | if (chip->state != FL_POINT && chip->state != FL_READY) |
| 1386 | map_write(map, CMD(0xff), cmd_addr); |
| 1387 | |
| 1388 | chip->state = FL_POINT; |
| 1389 | chip->ref_point_counter++; |
| 1390 | } |
| 1391 | mutex_unlock(&chip->mutex); |
| 1392 | |
| 1393 | return ret; |
| 1394 | } |
| 1395 | |
| 1396 | static int cfi_intelext_point(struct mtd_info *mtd, loff_t from, size_t len, |
| 1397 | size_t *retlen, void **virt, resource_size_t *phys) |
| 1398 | { |
| 1399 | struct map_info *map = mtd->priv; |
| 1400 | struct cfi_private *cfi = map->fldrv_priv; |
| 1401 | unsigned long ofs, last_end = 0; |
| 1402 | int chipnum; |
| 1403 | int ret; |
| 1404 | |
| 1405 | if (!map->virt) |
| 1406 | return -EINVAL; |
| 1407 | |
| 1408 | /* Now lock the chip(s) to POINT state */ |
| 1409 | |
| 1410 | /* ofs: offset within the first chip that the first read should start */ |
| 1411 | chipnum = (from >> cfi->chipshift); |
| 1412 | ofs = from - (chipnum << cfi->chipshift); |
| 1413 | |
| 1414 | *virt = map->virt + cfi->chips[chipnum].start + ofs; |
| 1415 | if (phys) |
| 1416 | *phys = map->phys + cfi->chips[chipnum].start + ofs; |
| 1417 | |
| 1418 | while (len) { |
| 1419 | unsigned long thislen; |
| 1420 | |
| 1421 | if (chipnum >= cfi->numchips) |
| 1422 | break; |
| 1423 | |
| 1424 | /* We cannot point across chips that are virtually disjoint */ |
| 1425 | if (!last_end) |
| 1426 | last_end = cfi->chips[chipnum].start; |
| 1427 | else if (cfi->chips[chipnum].start != last_end) |
| 1428 | break; |
| 1429 | |
| 1430 | if ((len + ofs -1) >> cfi->chipshift) |
| 1431 | thislen = (1<<cfi->chipshift) - ofs; |
| 1432 | else |
| 1433 | thislen = len; |
| 1434 | |
| 1435 | ret = do_point_onechip(map, &cfi->chips[chipnum], ofs, thislen); |
| 1436 | if (ret) |
| 1437 | break; |
| 1438 | |
| 1439 | *retlen += thislen; |
| 1440 | len -= thislen; |
| 1441 | |
| 1442 | ofs = 0; |
| 1443 | last_end += 1 << cfi->chipshift; |
| 1444 | chipnum++; |
| 1445 | } |
| 1446 | return 0; |
| 1447 | } |
| 1448 | |
| 1449 | static int cfi_intelext_unpoint(struct mtd_info *mtd, loff_t from, size_t len) |
| 1450 | { |
| 1451 | struct map_info *map = mtd->priv; |
| 1452 | struct cfi_private *cfi = map->fldrv_priv; |
| 1453 | unsigned long ofs; |
| 1454 | int chipnum, err = 0; |
| 1455 | |
| 1456 | /* Now unlock the chip(s) POINT state */ |
| 1457 | |
| 1458 | /* ofs: offset within the first chip that the first read should start */ |
| 1459 | chipnum = (from >> cfi->chipshift); |
| 1460 | ofs = from - (chipnum << cfi->chipshift); |
| 1461 | |
| 1462 | while (len && !err) { |
| 1463 | unsigned long thislen; |
| 1464 | struct flchip *chip; |
| 1465 | |
| 1466 | chip = &cfi->chips[chipnum]; |
| 1467 | if (chipnum >= cfi->numchips) |
| 1468 | break; |
| 1469 | |
| 1470 | if ((len + ofs -1) >> cfi->chipshift) |
| 1471 | thislen = (1<<cfi->chipshift) - ofs; |
| 1472 | else |
| 1473 | thislen = len; |
| 1474 | |
| 1475 | mutex_lock(&chip->mutex); |
| 1476 | if (chip->state == FL_POINT) { |
| 1477 | chip->ref_point_counter--; |
| 1478 | if(chip->ref_point_counter == 0) |
| 1479 | chip->state = FL_READY; |
| 1480 | } else { |
| 1481 | printk(KERN_ERR "%s: Error: unpoint called on non pointed region\n", map->name); |
| 1482 | err = -EINVAL; |
| 1483 | } |
| 1484 | |
| 1485 | put_chip(map, chip, chip->start); |
| 1486 | mutex_unlock(&chip->mutex); |
| 1487 | |
| 1488 | len -= thislen; |
| 1489 | ofs = 0; |
| 1490 | chipnum++; |
| 1491 | } |
| 1492 | |
| 1493 | return err; |
| 1494 | } |
| 1495 | |
| 1496 | static inline int do_read_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf) |
| 1497 | { |
| 1498 | unsigned long cmd_addr; |
| 1499 | struct cfi_private *cfi = map->fldrv_priv; |
| 1500 | int ret; |
| 1501 | |
| 1502 | adr += chip->start; |
| 1503 | |
| 1504 | /* Ensure cmd read/writes are aligned. */ |
| 1505 | cmd_addr = adr & ~(map_bankwidth(map)-1); |
| 1506 | |
| 1507 | mutex_lock(&chip->mutex); |
| 1508 | ret = get_chip(map, chip, cmd_addr, FL_READY); |
| 1509 | if (ret) { |
| 1510 | mutex_unlock(&chip->mutex); |
| 1511 | return ret; |
| 1512 | } |
| 1513 | |
| 1514 | if (chip->state != FL_POINT && chip->state != FL_READY) { |
| 1515 | map_write(map, CMD(0xff), cmd_addr); |
| 1516 | |
| 1517 | chip->state = FL_READY; |
| 1518 | } |
| 1519 | |
| 1520 | map_copy_from(map, buf, adr, len); |
| 1521 | |
| 1522 | put_chip(map, chip, cmd_addr); |
| 1523 | |
| 1524 | mutex_unlock(&chip->mutex); |
| 1525 | return 0; |
| 1526 | } |
| 1527 | |
| 1528 | static int cfi_intelext_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf) |
| 1529 | { |
| 1530 | struct map_info *map = mtd->priv; |
| 1531 | struct cfi_private *cfi = map->fldrv_priv; |
| 1532 | unsigned long ofs; |
| 1533 | int chipnum; |
| 1534 | int ret = 0; |
| 1535 | |
| 1536 | /* ofs: offset within the first chip that the first read should start */ |
| 1537 | chipnum = (from >> cfi->chipshift); |
| 1538 | ofs = from - (chipnum << cfi->chipshift); |
| 1539 | |
| 1540 | while (len) { |
| 1541 | unsigned long thislen; |
| 1542 | |
| 1543 | if (chipnum >= cfi->numchips) |
| 1544 | break; |
| 1545 | |
| 1546 | if ((len + ofs -1) >> cfi->chipshift) |
| 1547 | thislen = (1<<cfi->chipshift) - ofs; |
| 1548 | else |
| 1549 | thislen = len; |
| 1550 | |
| 1551 | ret = do_read_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf); |
| 1552 | if (ret) |
| 1553 | break; |
| 1554 | |
| 1555 | *retlen += thislen; |
| 1556 | len -= thislen; |
| 1557 | buf += thislen; |
| 1558 | |
| 1559 | ofs = 0; |
| 1560 | chipnum++; |
| 1561 | } |
| 1562 | return ret; |
| 1563 | } |
| 1564 | |
| 1565 | static int __xipram do_write_oneword(struct map_info *map, struct flchip *chip, |
| 1566 | unsigned long adr, map_word datum, int mode) |
| 1567 | { |
| 1568 | struct cfi_private *cfi = map->fldrv_priv; |
| 1569 | map_word status, write_cmd; |
| 1570 | int ret; |
| 1571 | |
| 1572 | adr += chip->start; |
| 1573 | |
| 1574 | switch (mode) { |
| 1575 | case FL_WRITING: |
| 1576 | write_cmd = (cfi->cfiq->P_ID != P_ID_INTEL_PERFORMANCE) ? CMD(0x40) : CMD(0x41); |
| 1577 | break; |
| 1578 | case FL_OTP_WRITE: |
| 1579 | write_cmd = CMD(0xc0); |
| 1580 | break; |
| 1581 | default: |
| 1582 | return -EINVAL; |
| 1583 | } |
| 1584 | |
| 1585 | mutex_lock(&chip->mutex); |
| 1586 | ret = get_chip(map, chip, adr, mode); |
| 1587 | if (ret) { |
| 1588 | mutex_unlock(&chip->mutex); |
| 1589 | return ret; |
| 1590 | } |
| 1591 | |
| 1592 | XIP_INVAL_CACHED_RANGE(map, adr, map_bankwidth(map)); |
| 1593 | ENABLE_VPP(map); |
| 1594 | xip_disable(map, chip, adr); |
| 1595 | map_write(map, write_cmd, adr); |
| 1596 | map_write(map, datum, adr); |
| 1597 | chip->state = mode; |
| 1598 | |
| 1599 | ret = INVAL_CACHE_AND_WAIT(map, chip, adr, |
| 1600 | adr, map_bankwidth(map), |
| 1601 | chip->word_write_time, |
| 1602 | chip->word_write_time_max); |
| 1603 | if (ret) { |
| 1604 | xip_enable(map, chip, adr); |
| 1605 | printk(KERN_ERR "%s: word write error (status timeout)\n", map->name); |
| 1606 | goto out; |
| 1607 | } |
| 1608 | |
| 1609 | /* check for errors */ |
| 1610 | status = map_read(map, adr); |
| 1611 | if (map_word_bitsset(map, status, CMD(0x1a))) { |
| 1612 | unsigned long chipstatus = MERGESTATUS(status); |
| 1613 | |
| 1614 | /* reset status */ |
| 1615 | map_write(map, CMD(0x50), adr); |
| 1616 | map_write(map, CMD(0x70), adr); |
| 1617 | xip_enable(map, chip, adr); |
| 1618 | |
| 1619 | if (chipstatus & 0x02) { |
| 1620 | ret = -EROFS; |
| 1621 | } else if (chipstatus & 0x08) { |
| 1622 | printk(KERN_ERR "%s: word write error (bad VPP)\n", map->name); |
| 1623 | ret = -EIO; |
| 1624 | } else { |
| 1625 | printk(KERN_ERR "%s: word write error (status 0x%lx)\n", map->name, chipstatus); |
| 1626 | ret = -EINVAL; |
| 1627 | } |
| 1628 | |
| 1629 | goto out; |
| 1630 | } |
| 1631 | |
| 1632 | xip_enable(map, chip, adr); |
| 1633 | out: DISABLE_VPP(map); |
| 1634 | put_chip(map, chip, adr); |
| 1635 | mutex_unlock(&chip->mutex); |
| 1636 | return ret; |
| 1637 | } |
| 1638 | |
| 1639 | |
| 1640 | static int cfi_intelext_write_words (struct mtd_info *mtd, loff_t to , size_t len, size_t *retlen, const u_char *buf) |
| 1641 | { |
| 1642 | struct map_info *map = mtd->priv; |
| 1643 | struct cfi_private *cfi = map->fldrv_priv; |
| 1644 | int ret; |
| 1645 | int chipnum; |
| 1646 | unsigned long ofs; |
| 1647 | |
| 1648 | chipnum = to >> cfi->chipshift; |
| 1649 | ofs = to - (chipnum << cfi->chipshift); |
| 1650 | |
| 1651 | /* If it's not bus-aligned, do the first byte write */ |
| 1652 | if (ofs & (map_bankwidth(map)-1)) { |
| 1653 | unsigned long bus_ofs = ofs & ~(map_bankwidth(map)-1); |
| 1654 | int gap = ofs - bus_ofs; |
| 1655 | int n; |
| 1656 | map_word datum; |
| 1657 | |
| 1658 | n = min_t(int, len, map_bankwidth(map)-gap); |
| 1659 | datum = map_word_ff(map); |
| 1660 | datum = map_word_load_partial(map, datum, buf, gap, n); |
| 1661 | |
| 1662 | ret = do_write_oneword(map, &cfi->chips[chipnum], |
| 1663 | bus_ofs, datum, FL_WRITING); |
| 1664 | if (ret) |
| 1665 | return ret; |
| 1666 | |
| 1667 | len -= n; |
| 1668 | ofs += n; |
| 1669 | buf += n; |
| 1670 | (*retlen) += n; |
| 1671 | |
| 1672 | if (ofs >> cfi->chipshift) { |
| 1673 | chipnum ++; |
| 1674 | ofs = 0; |
| 1675 | if (chipnum == cfi->numchips) |
| 1676 | return 0; |
| 1677 | } |
| 1678 | } |
| 1679 | |
| 1680 | while(len >= map_bankwidth(map)) { |
| 1681 | map_word datum = map_word_load(map, buf); |
| 1682 | |
| 1683 | ret = do_write_oneword(map, &cfi->chips[chipnum], |
| 1684 | ofs, datum, FL_WRITING); |
| 1685 | if (ret) |
| 1686 | return ret; |
| 1687 | |
| 1688 | ofs += map_bankwidth(map); |
| 1689 | buf += map_bankwidth(map); |
| 1690 | (*retlen) += map_bankwidth(map); |
| 1691 | len -= map_bankwidth(map); |
| 1692 | |
| 1693 | if (ofs >> cfi->chipshift) { |
| 1694 | chipnum ++; |
| 1695 | ofs = 0; |
| 1696 | if (chipnum == cfi->numchips) |
| 1697 | return 0; |
| 1698 | } |
| 1699 | } |
| 1700 | |
| 1701 | if (len & (map_bankwidth(map)-1)) { |
| 1702 | map_word datum; |
| 1703 | |
| 1704 | datum = map_word_ff(map); |
| 1705 | datum = map_word_load_partial(map, datum, buf, 0, len); |
| 1706 | |
| 1707 | ret = do_write_oneword(map, &cfi->chips[chipnum], |
| 1708 | ofs, datum, FL_WRITING); |
| 1709 | if (ret) |
| 1710 | return ret; |
| 1711 | |
| 1712 | (*retlen) += len; |
| 1713 | } |
| 1714 | |
| 1715 | return 0; |
| 1716 | } |
| 1717 | |
| 1718 | |
| 1719 | /* |
| 1720 | * Keep noinline: inlined, the map_word temporaries put do_write_buffer() over |
| 1721 | * the frame-size limit with MTD_MAP_BANK_WIDTH_32 and KASAN_STACK. |
| 1722 | */ |
| 1723 | static noinline void __xipram cfi_write_cmd(struct map_info *map, |
| 1724 | unsigned long cmd, unsigned long adr) |
| 1725 | { |
| 1726 | struct cfi_private *cfi = map->fldrv_priv; |
| 1727 | |
| 1728 | map_write(map, CMD(cmd), adr); |
| 1729 | } |
| 1730 | |
| 1731 | static int __xipram do_write_buffer(struct map_info *map, struct flchip *chip, |
| 1732 | unsigned long adr, const struct kvec **pvec, |
| 1733 | unsigned long *pvec_seek, int len) |
| 1734 | { |
| 1735 | struct cfi_private *cfi = map->fldrv_priv; |
| 1736 | map_word status, datum; |
| 1737 | unsigned long cmd_adr; |
| 1738 | int ret, wbufsize, word_gap, words; |
| 1739 | const struct kvec *vec; |
| 1740 | unsigned long vec_seek; |
| 1741 | unsigned long initial_adr; |
| 1742 | int initial_len = len; |
| 1743 | |
| 1744 | wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize; |
| 1745 | adr += chip->start; |
| 1746 | initial_adr = adr; |
| 1747 | cmd_adr = adr & ~(wbufsize-1); |
| 1748 | |
| 1749 | /* Sharp LH28F640BF chips need the first address for the |
| 1750 | * Page Buffer Program command. See Table 5 of |
| 1751 | * LH28F320BF, LH28F640BF, LH28F128BF Series (Appendix FUM00701) */ |
| 1752 | if (is_LH28F640BF(cfi)) |
| 1753 | cmd_adr = adr; |
| 1754 | |
| 1755 | mutex_lock(&chip->mutex); |
| 1756 | ret = get_chip(map, chip, cmd_adr, FL_WRITING); |
| 1757 | if (ret) { |
| 1758 | mutex_unlock(&chip->mutex); |
| 1759 | return ret; |
| 1760 | } |
| 1761 | |
| 1762 | XIP_INVAL_CACHED_RANGE(map, initial_adr, initial_len); |
| 1763 | ENABLE_VPP(map); |
| 1764 | xip_disable(map, chip, cmd_adr); |
| 1765 | |
| 1766 | /* §4.8 of the 28FxxxJ3A datasheet says "Any time SR.4 and/or SR.5 is set |
| 1767 | [...], the device will not accept any more Write to Buffer commands". |
| 1768 | So we must check here and reset those bits if they're set. Otherwise |
| 1769 | we're just pissing in the wind */ |
| 1770 | if (chip->state != FL_STATUS) { |
| 1771 | cfi_write_cmd(map, 0x70, cmd_adr); |
| 1772 | chip->state = FL_STATUS; |
| 1773 | } |
| 1774 | status = map_read(map, cmd_adr); |
| 1775 | if (map_word_bitsset(map, status, CMD(0x30))) { |
| 1776 | xip_enable(map, chip, cmd_adr); |
| 1777 | printk(KERN_WARNING "SR.4 or SR.5 bits set in buffer write (status %lx). Clearing.\n", status.x[0]); |
| 1778 | xip_disable(map, chip, cmd_adr); |
| 1779 | cfi_write_cmd(map, 0x50, cmd_adr); |
| 1780 | cfi_write_cmd(map, 0x70, cmd_adr); |
| 1781 | } |
| 1782 | |
| 1783 | chip->state = FL_WRITING_TO_BUFFER; |
| 1784 | cfi_write_cmd(map, (cfi->cfiq->P_ID != P_ID_INTEL_PERFORMANCE) ? 0xe8 : 0xe9, |
| 1785 | cmd_adr); |
| 1786 | ret = WAIT_TIMEOUT(map, chip, cmd_adr, 0, 0); |
| 1787 | if (ret) { |
| 1788 | /* Argh. Not ready for write to buffer */ |
| 1789 | map_word Xstatus = map_read(map, cmd_adr); |
| 1790 | cfi_write_cmd(map, 0x70, cmd_adr); |
| 1791 | chip->state = FL_STATUS; |
| 1792 | status = map_read(map, cmd_adr); |
| 1793 | cfi_write_cmd(map, 0x50, cmd_adr); |
| 1794 | cfi_write_cmd(map, 0x70, cmd_adr); |
| 1795 | xip_enable(map, chip, cmd_adr); |
| 1796 | printk(KERN_ERR "%s: Chip not ready for buffer write. Xstatus = %lx, status = %lx\n", |
| 1797 | map->name, Xstatus.x[0], status.x[0]); |
| 1798 | goto out; |
| 1799 | } |
| 1800 | |
| 1801 | /* Figure out the number of words to write */ |
| 1802 | word_gap = (-adr & (map_bankwidth(map)-1)); |
| 1803 | words = DIV_ROUND_UP(len - word_gap, map_bankwidth(map)); |
| 1804 | if (!word_gap) { |
| 1805 | words--; |
| 1806 | } else { |
| 1807 | word_gap = map_bankwidth(map) - word_gap; |
| 1808 | adr -= word_gap; |
| 1809 | datum = map_word_ff(map); |
| 1810 | } |
| 1811 | |
| 1812 | /* Write length of data to come */ |
| 1813 | cfi_write_cmd(map, words, cmd_adr); |
| 1814 | |
| 1815 | /* Write data */ |
| 1816 | vec = *pvec; |
| 1817 | vec_seek = *pvec_seek; |
| 1818 | do { |
| 1819 | int n = map_bankwidth(map) - word_gap; |
| 1820 | if (n > vec->iov_len - vec_seek) |
| 1821 | n = vec->iov_len - vec_seek; |
| 1822 | if (n > len) |
| 1823 | n = len; |
| 1824 | |
| 1825 | if (!word_gap && len < map_bankwidth(map)) |
| 1826 | datum = map_word_ff(map); |
| 1827 | |
| 1828 | datum = map_word_load_partial(map, datum, |
| 1829 | vec->iov_base + vec_seek, |
| 1830 | word_gap, n); |
| 1831 | |
| 1832 | len -= n; |
| 1833 | word_gap += n; |
| 1834 | if (!len || word_gap == map_bankwidth(map)) { |
| 1835 | map_write(map, datum, adr); |
| 1836 | adr += map_bankwidth(map); |
| 1837 | word_gap = 0; |
| 1838 | } |
| 1839 | |
| 1840 | vec_seek += n; |
| 1841 | if (vec_seek == vec->iov_len) { |
| 1842 | vec++; |
| 1843 | vec_seek = 0; |
| 1844 | } |
| 1845 | } while (len); |
| 1846 | *pvec = vec; |
| 1847 | *pvec_seek = vec_seek; |
| 1848 | |
| 1849 | /* GO GO GO */ |
| 1850 | cfi_write_cmd(map, 0xd0, cmd_adr); |
| 1851 | chip->state = FL_WRITING; |
| 1852 | |
| 1853 | ret = INVAL_CACHE_AND_WAIT(map, chip, cmd_adr, |
| 1854 | initial_adr, initial_len, |
| 1855 | chip->buffer_write_time, |
| 1856 | chip->buffer_write_time_max); |
| 1857 | if (ret) { |
| 1858 | cfi_write_cmd(map, 0x70, cmd_adr); |
| 1859 | chip->state = FL_STATUS; |
| 1860 | xip_enable(map, chip, cmd_adr); |
| 1861 | printk(KERN_ERR "%s: buffer write error (status timeout)\n", map->name); |
| 1862 | goto out; |
| 1863 | } |
| 1864 | |
| 1865 | /* check for errors */ |
| 1866 | status = map_read(map, cmd_adr); |
| 1867 | if (map_word_bitsset(map, status, CMD(0x1a))) { |
| 1868 | unsigned long chipstatus = MERGESTATUS(status); |
| 1869 | |
| 1870 | /* reset status */ |
| 1871 | cfi_write_cmd(map, 0x50, cmd_adr); |
| 1872 | cfi_write_cmd(map, 0x70, cmd_adr); |
| 1873 | xip_enable(map, chip, cmd_adr); |
| 1874 | |
| 1875 | if (chipstatus & 0x02) { |
| 1876 | ret = -EROFS; |
| 1877 | } else if (chipstatus & 0x08) { |
| 1878 | printk(KERN_ERR "%s: buffer write error (bad VPP)\n", map->name); |
| 1879 | ret = -EIO; |
| 1880 | } else { |
| 1881 | printk(KERN_ERR "%s: buffer write error (status 0x%lx)\n", map->name, chipstatus); |
| 1882 | ret = -EINVAL; |
| 1883 | } |
| 1884 | |
| 1885 | goto out; |
| 1886 | } |
| 1887 | |
| 1888 | xip_enable(map, chip, cmd_adr); |
| 1889 | out: DISABLE_VPP(map); |
| 1890 | put_chip(map, chip, cmd_adr); |
| 1891 | mutex_unlock(&chip->mutex); |
| 1892 | return ret; |
| 1893 | } |
| 1894 | |
| 1895 | static int cfi_intelext_writev (struct mtd_info *mtd, const struct kvec *vecs, |
| 1896 | unsigned long count, loff_t to, size_t *retlen) |
| 1897 | { |
| 1898 | struct map_info *map = mtd->priv; |
| 1899 | struct cfi_private *cfi = map->fldrv_priv; |
| 1900 | int wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize; |
| 1901 | int ret; |
| 1902 | int chipnum; |
| 1903 | unsigned long ofs, vec_seek, i; |
| 1904 | size_t len = 0; |
| 1905 | |
| 1906 | for (i = 0; i < count; i++) |
| 1907 | len += vecs[i].iov_len; |
| 1908 | |
| 1909 | if (!len) |
| 1910 | return 0; |
| 1911 | |
| 1912 | chipnum = to >> cfi->chipshift; |
| 1913 | ofs = to - (chipnum << cfi->chipshift); |
| 1914 | vec_seek = 0; |
| 1915 | |
| 1916 | do { |
| 1917 | /* We must not cross write block boundaries */ |
| 1918 | int size = wbufsize - (ofs & (wbufsize-1)); |
| 1919 | |
| 1920 | if (size > len) |
| 1921 | size = len; |
| 1922 | ret = do_write_buffer(map, &cfi->chips[chipnum], |
| 1923 | ofs, &vecs, &vec_seek, size); |
| 1924 | if (ret) |
| 1925 | return ret; |
| 1926 | |
| 1927 | ofs += size; |
| 1928 | (*retlen) += size; |
| 1929 | len -= size; |
| 1930 | |
| 1931 | if (ofs >> cfi->chipshift) { |
| 1932 | chipnum ++; |
| 1933 | ofs = 0; |
| 1934 | if (chipnum == cfi->numchips) |
| 1935 | return 0; |
| 1936 | } |
| 1937 | |
| 1938 | /* Be nice and reschedule with the chip in a usable state for other |
| 1939 | processes. */ |
| 1940 | cond_resched(); |
| 1941 | |
| 1942 | } while (len); |
| 1943 | |
| 1944 | return 0; |
| 1945 | } |
| 1946 | |
| 1947 | static int cfi_intelext_write_buffers (struct mtd_info *mtd, loff_t to, |
| 1948 | size_t len, size_t *retlen, const u_char *buf) |
| 1949 | { |
| 1950 | struct kvec vec; |
| 1951 | |
| 1952 | vec.iov_base = (void *) buf; |
| 1953 | vec.iov_len = len; |
| 1954 | |
| 1955 | return cfi_intelext_writev(mtd, &vec, 1, to, retlen); |
| 1956 | } |
| 1957 | |
| 1958 | static int __xipram do_erase_oneblock(struct map_info *map, struct flchip *chip, |
| 1959 | unsigned long adr, int len, void *thunk) |
| 1960 | { |
| 1961 | struct cfi_private *cfi = map->fldrv_priv; |
| 1962 | map_word status; |
| 1963 | int retries = 3; |
| 1964 | int ret; |
| 1965 | |
| 1966 | adr += chip->start; |
| 1967 | |
| 1968 | retry: |
| 1969 | mutex_lock(&chip->mutex); |
| 1970 | ret = get_chip(map, chip, adr, FL_ERASING); |
| 1971 | if (ret) { |
| 1972 | mutex_unlock(&chip->mutex); |
| 1973 | return ret; |
| 1974 | } |
| 1975 | |
| 1976 | XIP_INVAL_CACHED_RANGE(map, adr, len); |
| 1977 | ENABLE_VPP(map); |
| 1978 | xip_disable(map, chip, adr); |
| 1979 | |
| 1980 | /* Clear the status register first */ |
| 1981 | map_write(map, CMD(0x50), adr); |
| 1982 | |
| 1983 | /* Now erase */ |
| 1984 | map_write(map, CMD(0x20), adr); |
| 1985 | map_write(map, CMD(0xD0), adr); |
| 1986 | chip->state = FL_ERASING; |
| 1987 | chip->erase_suspended = 0; |
| 1988 | chip->in_progress_block_addr = adr; |
| 1989 | chip->in_progress_block_mask = ~(len - 1); |
| 1990 | |
| 1991 | ret = INVAL_CACHE_AND_WAIT(map, chip, adr, |
| 1992 | adr, len, |
| 1993 | chip->erase_time, |
| 1994 | chip->erase_time_max); |
| 1995 | if (ret) { |
| 1996 | map_write(map, CMD(0x70), adr); |
| 1997 | chip->state = FL_STATUS; |
| 1998 | xip_enable(map, chip, adr); |
| 1999 | printk(KERN_ERR "%s: block erase error: (status timeout)\n", map->name); |
| 2000 | goto out; |
| 2001 | } |
| 2002 | |
| 2003 | /* We've broken this before. It doesn't hurt to be safe */ |
| 2004 | map_write(map, CMD(0x70), adr); |
| 2005 | chip->state = FL_STATUS; |
| 2006 | status = map_read(map, adr); |
| 2007 | |
| 2008 | /* check for errors */ |
| 2009 | if (map_word_bitsset(map, status, CMD(0x3a))) { |
| 2010 | unsigned long chipstatus = MERGESTATUS(status); |
| 2011 | |
| 2012 | /* Reset the error bits */ |
| 2013 | map_write(map, CMD(0x50), adr); |
| 2014 | map_write(map, CMD(0x70), adr); |
| 2015 | xip_enable(map, chip, adr); |
| 2016 | |
| 2017 | if ((chipstatus & 0x30) == 0x30) { |
| 2018 | printk(KERN_ERR "%s: block erase error: (bad command sequence, status 0x%lx)\n", map->name, chipstatus); |
| 2019 | ret = -EINVAL; |
| 2020 | } else if (chipstatus & 0x02) { |
| 2021 | /* Protection bit set */ |
| 2022 | ret = -EROFS; |
| 2023 | } else if (chipstatus & 0x8) { |
| 2024 | /* Voltage */ |
| 2025 | printk(KERN_ERR "%s: block erase error: (bad VPP)\n", map->name); |
| 2026 | ret = -EIO; |
| 2027 | } else if (chipstatus & 0x20 && retries--) { |
| 2028 | printk(KERN_DEBUG "block erase failed at 0x%08lx: status 0x%lx. Retrying...\n", adr, chipstatus); |
| 2029 | DISABLE_VPP(map); |
| 2030 | put_chip(map, chip, adr); |
| 2031 | mutex_unlock(&chip->mutex); |
| 2032 | goto retry; |
| 2033 | } else { |
| 2034 | printk(KERN_ERR "%s: block erase failed at 0x%08lx (status 0x%lx)\n", map->name, adr, chipstatus); |
| 2035 | ret = -EIO; |
| 2036 | } |
| 2037 | |
| 2038 | goto out; |
| 2039 | } |
| 2040 | |
| 2041 | xip_enable(map, chip, adr); |
| 2042 | out: DISABLE_VPP(map); |
| 2043 | put_chip(map, chip, adr); |
| 2044 | mutex_unlock(&chip->mutex); |
| 2045 | return ret; |
| 2046 | } |
| 2047 | |
| 2048 | static int cfi_intelext_erase_varsize(struct mtd_info *mtd, struct erase_info *instr) |
| 2049 | { |
| 2050 | return cfi_varsize_frob(mtd, do_erase_oneblock, instr->addr, |
| 2051 | instr->len, NULL); |
| 2052 | } |
| 2053 | |
| 2054 | static void cfi_intelext_sync (struct mtd_info *mtd) |
| 2055 | { |
| 2056 | struct map_info *map = mtd->priv; |
| 2057 | struct cfi_private *cfi = map->fldrv_priv; |
| 2058 | int i; |
| 2059 | struct flchip *chip; |
| 2060 | int ret = 0; |
| 2061 | |
| 2062 | for (i=0; !ret && i<cfi->numchips; i++) { |
| 2063 | chip = &cfi->chips[i]; |
| 2064 | |
| 2065 | mutex_lock(&chip->mutex); |
| 2066 | ret = get_chip(map, chip, chip->start, FL_SYNCING); |
| 2067 | |
| 2068 | if (!ret) { |
| 2069 | chip->oldstate = chip->state; |
| 2070 | chip->state = FL_SYNCING; |
| 2071 | /* No need to wake_up() on this state change - |
| 2072 | * as the whole point is that nobody can do anything |
| 2073 | * with the chip now anyway. |
| 2074 | */ |
| 2075 | } |
| 2076 | mutex_unlock(&chip->mutex); |
| 2077 | } |
| 2078 | |
| 2079 | /* Unlock the chips again */ |
| 2080 | |
| 2081 | for (i--; i >=0; i--) { |
| 2082 | chip = &cfi->chips[i]; |
| 2083 | |
| 2084 | mutex_lock(&chip->mutex); |
| 2085 | |
| 2086 | if (chip->state == FL_SYNCING) { |
| 2087 | chip->state = chip->oldstate; |
| 2088 | chip->oldstate = FL_READY; |
| 2089 | wake_up(&chip->wq); |
| 2090 | } |
| 2091 | mutex_unlock(&chip->mutex); |
| 2092 | } |
| 2093 | } |
| 2094 | |
| 2095 | static int __xipram do_getlockstatus_oneblock(struct map_info *map, |
| 2096 | struct flchip *chip, |
| 2097 | unsigned long adr, |
| 2098 | int len, void *thunk) |
| 2099 | { |
| 2100 | struct cfi_private *cfi = map->fldrv_priv; |
| 2101 | int status, ofs_factor = cfi->interleave * cfi->device_type; |
| 2102 | |
| 2103 | adr += chip->start; |
| 2104 | xip_disable(map, chip, adr+(2*ofs_factor)); |
| 2105 | map_write(map, CMD(0x90), adr+(2*ofs_factor)); |
| 2106 | chip->state = FL_JEDEC_QUERY; |
| 2107 | status = cfi_read_query(map, adr+(2*ofs_factor)); |
| 2108 | xip_enable(map, chip, 0); |
| 2109 | return status; |
| 2110 | } |
| 2111 | |
| 2112 | #ifdef DEBUG_LOCK_BITS |
| 2113 | static int __xipram do_printlockstatus_oneblock(struct map_info *map, |
| 2114 | struct flchip *chip, |
| 2115 | unsigned long adr, |
| 2116 | int len, void *thunk) |
| 2117 | { |
| 2118 | printk(KERN_DEBUG "block status register for 0x%08lx is %x\n", |
| 2119 | adr, do_getlockstatus_oneblock(map, chip, adr, len, thunk)); |
| 2120 | return 0; |
| 2121 | } |
| 2122 | #endif |
| 2123 | |
| 2124 | #define DO_XXLOCK_ONEBLOCK_LOCK ((void *) 1) |
| 2125 | #define DO_XXLOCK_ONEBLOCK_UNLOCK ((void *) 2) |
| 2126 | |
| 2127 | static int __xipram do_xxlock_oneblock(struct map_info *map, struct flchip *chip, |
| 2128 | unsigned long adr, int len, void *thunk) |
| 2129 | { |
| 2130 | struct cfi_private *cfi = map->fldrv_priv; |
| 2131 | struct cfi_pri_intelext *extp = cfi->cmdset_priv; |
| 2132 | int mdelay; |
| 2133 | int ret; |
| 2134 | |
| 2135 | adr += chip->start; |
| 2136 | |
| 2137 | mutex_lock(&chip->mutex); |
| 2138 | ret = get_chip(map, chip, adr, FL_LOCKING); |
| 2139 | if (ret) { |
| 2140 | mutex_unlock(&chip->mutex); |
| 2141 | return ret; |
| 2142 | } |
| 2143 | |
| 2144 | ENABLE_VPP(map); |
| 2145 | xip_disable(map, chip, adr); |
| 2146 | |
| 2147 | map_write(map, CMD(0x60), adr); |
| 2148 | if (thunk == DO_XXLOCK_ONEBLOCK_LOCK) { |
| 2149 | map_write(map, CMD(0x01), adr); |
| 2150 | chip->state = FL_LOCKING; |
| 2151 | } else if (thunk == DO_XXLOCK_ONEBLOCK_UNLOCK) { |
| 2152 | map_write(map, CMD(0xD0), adr); |
| 2153 | chip->state = FL_UNLOCKING; |
| 2154 | } else |
| 2155 | BUG(); |
| 2156 | |
| 2157 | /* |
| 2158 | * If Instant Individual Block Locking supported then no need |
| 2159 | * to delay. |
| 2160 | */ |
| 2161 | /* |
| 2162 | * Unlocking may take up to 1.4 seconds on some Intel flashes. So |
| 2163 | * lets use a max of 1.5 seconds (1500ms) as timeout. |
| 2164 | * |
| 2165 | * See "Clear Block Lock-Bits Time" on page 40 in |
| 2166 | * "3 Volt Intel StrataFlash Memory" 28F128J3,28F640J3,28F320J3 manual |
| 2167 | * from February 2003 |
| 2168 | */ |
| 2169 | mdelay = (!extp || !(extp->FeatureSupport & (1 << 5))) ? 1500 : 0; |
| 2170 | |
| 2171 | ret = WAIT_TIMEOUT(map, chip, adr, mdelay, mdelay * 1000); |
| 2172 | if (ret) { |
| 2173 | map_write(map, CMD(0x70), adr); |
| 2174 | chip->state = FL_STATUS; |
| 2175 | xip_enable(map, chip, adr); |
| 2176 | printk(KERN_ERR "%s: block unlock error: (status timeout)\n", map->name); |
| 2177 | goto out; |
| 2178 | } |
| 2179 | |
| 2180 | xip_enable(map, chip, adr); |
| 2181 | out: DISABLE_VPP(map); |
| 2182 | put_chip(map, chip, adr); |
| 2183 | mutex_unlock(&chip->mutex); |
| 2184 | return ret; |
| 2185 | } |
| 2186 | |
| 2187 | static int cfi_intelext_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len) |
| 2188 | { |
| 2189 | int ret; |
| 2190 | |
| 2191 | #ifdef DEBUG_LOCK_BITS |
| 2192 | printk(KERN_DEBUG "%s: lock status before, ofs=0x%08llx, len=0x%08X\n", |
| 2193 | __func__, ofs, len); |
| 2194 | cfi_varsize_frob(mtd, do_printlockstatus_oneblock, |
| 2195 | ofs, len, NULL); |
| 2196 | #endif |
| 2197 | |
| 2198 | ret = cfi_varsize_frob(mtd, do_xxlock_oneblock, |
| 2199 | ofs, len, DO_XXLOCK_ONEBLOCK_LOCK); |
| 2200 | |
| 2201 | #ifdef DEBUG_LOCK_BITS |
| 2202 | printk(KERN_DEBUG "%s: lock status after, ret=%d\n", |
| 2203 | __func__, ret); |
| 2204 | cfi_varsize_frob(mtd, do_printlockstatus_oneblock, |
| 2205 | ofs, len, NULL); |
| 2206 | #endif |
| 2207 | |
| 2208 | return ret; |
| 2209 | } |
| 2210 | |
| 2211 | static int cfi_intelext_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len) |
| 2212 | { |
| 2213 | int ret; |
| 2214 | |
| 2215 | #ifdef DEBUG_LOCK_BITS |
| 2216 | printk(KERN_DEBUG "%s: lock status before, ofs=0x%08llx, len=0x%08X\n", |
| 2217 | __func__, ofs, len); |
| 2218 | cfi_varsize_frob(mtd, do_printlockstatus_oneblock, |
| 2219 | ofs, len, NULL); |
| 2220 | #endif |
| 2221 | |
| 2222 | ret = cfi_varsize_frob(mtd, do_xxlock_oneblock, |
| 2223 | ofs, len, DO_XXLOCK_ONEBLOCK_UNLOCK); |
| 2224 | |
| 2225 | #ifdef DEBUG_LOCK_BITS |
| 2226 | printk(KERN_DEBUG "%s: lock status after, ret=%d\n", |
| 2227 | __func__, ret); |
| 2228 | cfi_varsize_frob(mtd, do_printlockstatus_oneblock, |
| 2229 | ofs, len, NULL); |
| 2230 | #endif |
| 2231 | |
| 2232 | return ret; |
| 2233 | } |
| 2234 | |
| 2235 | static int cfi_intelext_is_locked(struct mtd_info *mtd, loff_t ofs, |
| 2236 | uint64_t len) |
| 2237 | { |
| 2238 | return cfi_varsize_frob(mtd, do_getlockstatus_oneblock, |
| 2239 | ofs, len, NULL) ? 1 : 0; |
| 2240 | } |
| 2241 | |
| 2242 | #ifdef CONFIG_MTD_OTP |
| 2243 | |
| 2244 | typedef int (*otp_op_t)(struct map_info *map, struct flchip *chip, |
| 2245 | u_long data_offset, u_char *buf, u_int size, |
| 2246 | u_long prot_offset, u_int groupno, u_int groupsize); |
| 2247 | |
| 2248 | static int __xipram |
| 2249 | do_otp_read(struct map_info *map, struct flchip *chip, u_long offset, |
| 2250 | u_char *buf, u_int size, u_long prot, u_int grpno, u_int grpsz) |
| 2251 | { |
| 2252 | struct cfi_private *cfi = map->fldrv_priv; |
| 2253 | int ret; |
| 2254 | |
| 2255 | mutex_lock(&chip->mutex); |
| 2256 | ret = get_chip(map, chip, chip->start, FL_JEDEC_QUERY); |
| 2257 | if (ret) { |
| 2258 | mutex_unlock(&chip->mutex); |
| 2259 | return ret; |
| 2260 | } |
| 2261 | |
| 2262 | /* let's ensure we're not reading back cached data from array mode */ |
| 2263 | INVALIDATE_CACHED_RANGE(map, chip->start + offset, size); |
| 2264 | |
| 2265 | xip_disable(map, chip, chip->start); |
| 2266 | if (chip->state != FL_JEDEC_QUERY) { |
| 2267 | map_write(map, CMD(0x90), chip->start); |
| 2268 | chip->state = FL_JEDEC_QUERY; |
| 2269 | } |
| 2270 | map_copy_from(map, buf, chip->start + offset, size); |
| 2271 | xip_enable(map, chip, chip->start); |
| 2272 | |
| 2273 | /* then ensure we don't keep OTP data in the cache */ |
| 2274 | INVALIDATE_CACHED_RANGE(map, chip->start + offset, size); |
| 2275 | |
| 2276 | put_chip(map, chip, chip->start); |
| 2277 | mutex_unlock(&chip->mutex); |
| 2278 | return 0; |
| 2279 | } |
| 2280 | |
| 2281 | static int |
| 2282 | do_otp_write(struct map_info *map, struct flchip *chip, u_long offset, |
| 2283 | u_char *buf, u_int size, u_long prot, u_int grpno, u_int grpsz) |
| 2284 | { |
| 2285 | int ret; |
| 2286 | |
| 2287 | while (size) { |
| 2288 | unsigned long bus_ofs = offset & ~(map_bankwidth(map)-1); |
| 2289 | int gap = offset - bus_ofs; |
| 2290 | int n = min_t(int, size, map_bankwidth(map)-gap); |
| 2291 | map_word datum = map_word_ff(map); |
| 2292 | |
| 2293 | datum = map_word_load_partial(map, datum, buf, gap, n); |
| 2294 | ret = do_write_oneword(map, chip, bus_ofs, datum, FL_OTP_WRITE); |
| 2295 | if (ret) |
| 2296 | return ret; |
| 2297 | |
| 2298 | offset += n; |
| 2299 | buf += n; |
| 2300 | size -= n; |
| 2301 | } |
| 2302 | |
| 2303 | return 0; |
| 2304 | } |
| 2305 | |
| 2306 | static int |
| 2307 | do_otp_lock(struct map_info *map, struct flchip *chip, u_long offset, |
| 2308 | u_char *buf, u_int size, u_long prot, u_int grpno, u_int grpsz) |
| 2309 | { |
| 2310 | struct cfi_private *cfi = map->fldrv_priv; |
| 2311 | map_word datum; |
| 2312 | |
| 2313 | /* make sure area matches group boundaries */ |
| 2314 | if (size != grpsz) |
| 2315 | return -EXDEV; |
| 2316 | |
| 2317 | datum = map_word_ff(map); |
| 2318 | datum = map_word_clr(map, datum, CMD(1 << grpno)); |
| 2319 | return do_write_oneword(map, chip, prot, datum, FL_OTP_WRITE); |
| 2320 | } |
| 2321 | |
| 2322 | static int cfi_intelext_otp_walk(struct mtd_info *mtd, loff_t from, size_t len, |
| 2323 | size_t *retlen, u_char *buf, |
| 2324 | otp_op_t action, int user_regs) |
| 2325 | { |
| 2326 | struct map_info *map = mtd->priv; |
| 2327 | struct cfi_private *cfi = map->fldrv_priv; |
| 2328 | struct cfi_pri_intelext *extp = cfi->cmdset_priv; |
| 2329 | struct flchip *chip; |
| 2330 | struct cfi_intelext_otpinfo *otp; |
| 2331 | u_long devsize, reg_prot_offset, data_offset; |
| 2332 | u_int chip_num, chip_step, field, reg_fact_size, reg_user_size; |
| 2333 | u_int groups, groupno, groupsize, reg_fact_groups, reg_user_groups; |
| 2334 | int ret; |
| 2335 | |
| 2336 | *retlen = 0; |
| 2337 | |
| 2338 | /* Check that we actually have some OTP registers */ |
| 2339 | if (!extp || !(extp->FeatureSupport & 64) || !extp->NumProtectionFields) |
| 2340 | return -ENODATA; |
| 2341 | |
| 2342 | /* we need real chips here not virtual ones */ |
| 2343 | devsize = (1 << cfi->cfiq->DevSize) * cfi->interleave; |
| 2344 | chip_step = devsize >> cfi->chipshift; |
| 2345 | chip_num = 0; |
| 2346 | |
| 2347 | /* Some chips have OTP located in the _top_ partition only. |
| 2348 | For example: Intel 28F256L18T (T means top-parameter device) */ |
| 2349 | if (cfi->mfr == CFI_MFR_INTEL) { |
| 2350 | switch (cfi->id) { |
| 2351 | case 0x880b: |
| 2352 | case 0x880c: |
| 2353 | case 0x880d: |
| 2354 | chip_num = chip_step - 1; |
| 2355 | } |
| 2356 | } |
| 2357 | |
| 2358 | for ( ; chip_num < cfi->numchips; chip_num += chip_step) { |
| 2359 | chip = &cfi->chips[chip_num]; |
| 2360 | otp = (struct cfi_intelext_otpinfo *)&extp->extra[0]; |
| 2361 | |
| 2362 | /* first OTP region */ |
| 2363 | field = 0; |
| 2364 | reg_prot_offset = extp->ProtRegAddr; |
| 2365 | reg_fact_groups = 1; |
| 2366 | reg_fact_size = 1 << extp->FactProtRegSize; |
| 2367 | reg_user_groups = 1; |
| 2368 | reg_user_size = 1 << extp->UserProtRegSize; |
| 2369 | |
| 2370 | while (len > 0) { |
| 2371 | /* flash geometry fixup */ |
| 2372 | data_offset = reg_prot_offset + 1; |
| 2373 | data_offset *= cfi->interleave * cfi->device_type; |
| 2374 | reg_prot_offset *= cfi->interleave * cfi->device_type; |
| 2375 | reg_fact_size *= cfi->interleave; |
| 2376 | reg_user_size *= cfi->interleave; |
| 2377 | |
| 2378 | if (user_regs) { |
| 2379 | groups = reg_user_groups; |
| 2380 | groupsize = reg_user_size; |
| 2381 | /* skip over factory reg area */ |
| 2382 | groupno = reg_fact_groups; |
| 2383 | data_offset += reg_fact_groups * reg_fact_size; |
| 2384 | } else { |
| 2385 | groups = reg_fact_groups; |
| 2386 | groupsize = reg_fact_size; |
| 2387 | groupno = 0; |
| 2388 | } |
| 2389 | |
| 2390 | while (len > 0 && groups > 0) { |
| 2391 | if (!action) { |
| 2392 | /* |
| 2393 | * Special case: if action is NULL |
| 2394 | * we fill buf with otp_info records. |
| 2395 | */ |
| 2396 | struct otp_info *otpinfo; |
| 2397 | map_word lockword; |
| 2398 | len -= sizeof(struct otp_info); |
| 2399 | if (len <= 0) |
| 2400 | return -ENOSPC; |
| 2401 | ret = do_otp_read(map, chip, |
| 2402 | reg_prot_offset, |
| 2403 | (u_char *)&lockword, |
| 2404 | map_bankwidth(map), |
| 2405 | 0, 0, 0); |
| 2406 | if (ret) |
| 2407 | return ret; |
| 2408 | otpinfo = (struct otp_info *)buf; |
| 2409 | otpinfo->start = from; |
| 2410 | otpinfo->length = groupsize; |
| 2411 | otpinfo->locked = |
| 2412 | !map_word_bitsset(map, lockword, |
| 2413 | CMD(1 << groupno)); |
| 2414 | from += groupsize; |
| 2415 | buf += sizeof(*otpinfo); |
| 2416 | *retlen += sizeof(*otpinfo); |
| 2417 | } else if (from >= groupsize) { |
| 2418 | from -= groupsize; |
| 2419 | data_offset += groupsize; |
| 2420 | } else { |
| 2421 | int size = groupsize; |
| 2422 | data_offset += from; |
| 2423 | size -= from; |
| 2424 | from = 0; |
| 2425 | if (size > len) |
| 2426 | size = len; |
| 2427 | ret = action(map, chip, data_offset, |
| 2428 | buf, size, reg_prot_offset, |
| 2429 | groupno, groupsize); |
| 2430 | if (ret < 0) |
| 2431 | return ret; |
| 2432 | buf += size; |
| 2433 | len -= size; |
| 2434 | *retlen += size; |
| 2435 | data_offset += size; |
| 2436 | } |
| 2437 | groupno++; |
| 2438 | groups--; |
| 2439 | } |
| 2440 | |
| 2441 | /* next OTP region */ |
| 2442 | if (++field == extp->NumProtectionFields) |
| 2443 | break; |
| 2444 | reg_prot_offset = otp->ProtRegAddr; |
| 2445 | reg_fact_groups = otp->FactGroups; |
| 2446 | reg_fact_size = 1 << otp->FactProtRegSize; |
| 2447 | reg_user_groups = otp->UserGroups; |
| 2448 | reg_user_size = 1 << otp->UserProtRegSize; |
| 2449 | otp++; |
| 2450 | } |
| 2451 | } |
| 2452 | |
| 2453 | return 0; |
| 2454 | } |
| 2455 | |
| 2456 | static int cfi_intelext_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, |
| 2457 | size_t len, size_t *retlen, |
| 2458 | u_char *buf) |
| 2459 | { |
| 2460 | return cfi_intelext_otp_walk(mtd, from, len, retlen, |
| 2461 | buf, do_otp_read, 0); |
| 2462 | } |
| 2463 | |
| 2464 | static int cfi_intelext_read_user_prot_reg(struct mtd_info *mtd, loff_t from, |
| 2465 | size_t len, size_t *retlen, |
| 2466 | u_char *buf) |
| 2467 | { |
| 2468 | return cfi_intelext_otp_walk(mtd, from, len, retlen, |
| 2469 | buf, do_otp_read, 1); |
| 2470 | } |
| 2471 | |
| 2472 | static int cfi_intelext_write_user_prot_reg(struct mtd_info *mtd, loff_t from, |
| 2473 | size_t len, size_t *retlen, |
| 2474 | const u_char *buf) |
| 2475 | { |
| 2476 | return cfi_intelext_otp_walk(mtd, from, len, retlen, |
| 2477 | (u_char *)buf, do_otp_write, 1); |
| 2478 | } |
| 2479 | |
| 2480 | static int cfi_intelext_lock_user_prot_reg(struct mtd_info *mtd, |
| 2481 | loff_t from, size_t len) |
| 2482 | { |
| 2483 | size_t retlen; |
| 2484 | return cfi_intelext_otp_walk(mtd, from, len, &retlen, |
| 2485 | NULL, do_otp_lock, 1); |
| 2486 | } |
| 2487 | |
| 2488 | static int cfi_intelext_get_fact_prot_info(struct mtd_info *mtd, size_t len, |
| 2489 | size_t *retlen, struct otp_info *buf) |
| 2490 | |
| 2491 | { |
| 2492 | return cfi_intelext_otp_walk(mtd, 0, len, retlen, (u_char *)buf, |
| 2493 | NULL, 0); |
| 2494 | } |
| 2495 | |
| 2496 | static int cfi_intelext_get_user_prot_info(struct mtd_info *mtd, size_t len, |
| 2497 | size_t *retlen, struct otp_info *buf) |
| 2498 | { |
| 2499 | return cfi_intelext_otp_walk(mtd, 0, len, retlen, (u_char *)buf, |
| 2500 | NULL, 1); |
| 2501 | } |
| 2502 | |
| 2503 | #endif |
| 2504 | |
| 2505 | static void cfi_intelext_save_locks(struct mtd_info *mtd) |
| 2506 | { |
| 2507 | struct mtd_erase_region_info *region; |
| 2508 | int block, status, i; |
| 2509 | unsigned long adr; |
| 2510 | size_t len; |
| 2511 | |
| 2512 | for (i = 0; i < mtd->numeraseregions; i++) { |
| 2513 | region = &mtd->eraseregions[i]; |
| 2514 | if (!region->lockmap) |
| 2515 | continue; |
| 2516 | |
| 2517 | for (block = 0; block < region->numblocks; block++){ |
| 2518 | len = region->erasesize; |
| 2519 | adr = region->offset + block * len; |
| 2520 | |
| 2521 | status = cfi_varsize_frob(mtd, |
| 2522 | do_getlockstatus_oneblock, adr, len, NULL); |
| 2523 | if (status) |
| 2524 | set_bit(block, region->lockmap); |
| 2525 | else |
| 2526 | clear_bit(block, region->lockmap); |
| 2527 | } |
| 2528 | } |
| 2529 | } |
| 2530 | |
| 2531 | static int cfi_intelext_suspend(struct mtd_info *mtd) |
| 2532 | { |
| 2533 | struct map_info *map = mtd->priv; |
| 2534 | struct cfi_private *cfi = map->fldrv_priv; |
| 2535 | struct cfi_pri_intelext *extp = cfi->cmdset_priv; |
| 2536 | int i; |
| 2537 | struct flchip *chip; |
| 2538 | int ret = 0; |
| 2539 | |
| 2540 | if ((mtd->flags & MTD_POWERUP_LOCK) |
| 2541 | && extp && (extp->FeatureSupport & (1 << 5))) |
| 2542 | cfi_intelext_save_locks(mtd); |
| 2543 | |
| 2544 | for (i=0; !ret && i<cfi->numchips; i++) { |
| 2545 | chip = &cfi->chips[i]; |
| 2546 | |
| 2547 | mutex_lock(&chip->mutex); |
| 2548 | |
| 2549 | switch (chip->state) { |
| 2550 | case FL_READY: |
| 2551 | case FL_STATUS: |
| 2552 | case FL_CFI_QUERY: |
| 2553 | case FL_JEDEC_QUERY: |
| 2554 | if (chip->oldstate == FL_READY) { |
| 2555 | /* place the chip in a known state before suspend */ |
| 2556 | map_write(map, CMD(0xFF), cfi->chips[i].start); |
| 2557 | chip->oldstate = chip->state; |
| 2558 | chip->state = FL_PM_SUSPENDED; |
| 2559 | /* No need to wake_up() on this state change - |
| 2560 | * as the whole point is that nobody can do anything |
| 2561 | * with the chip now anyway. |
| 2562 | */ |
| 2563 | } else { |
| 2564 | /* There seems to be an operation pending. We must wait for it. */ |
| 2565 | printk(KERN_NOTICE "Flash device refused suspend due to pending operation (oldstate %d)\n", chip->oldstate); |
| 2566 | ret = -EAGAIN; |
| 2567 | } |
| 2568 | break; |
| 2569 | default: |
| 2570 | /* Should we actually wait? Once upon a time these routines weren't |
| 2571 | allowed to. Or should we return -EAGAIN, because the upper layers |
| 2572 | ought to have already shut down anything which was using the device |
| 2573 | anyway? The latter for now. */ |
| 2574 | printk(KERN_NOTICE "Flash device refused suspend due to active operation (state %d)\n", chip->state); |
| 2575 | ret = -EAGAIN; |
| 2576 | break; |
| 2577 | case FL_PM_SUSPENDED: |
| 2578 | case FL_SHUTDOWN: |
| 2579 | /* |
| 2580 | * Already suspended, or put into array mode by the |
| 2581 | * reboot notifier ahead of an imminent power-off. |
| 2582 | * Either way there is nothing to do. |
| 2583 | */ |
| 2584 | break; |
| 2585 | } |
| 2586 | mutex_unlock(&chip->mutex); |
| 2587 | } |
| 2588 | |
| 2589 | /* Unlock the chips again */ |
| 2590 | |
| 2591 | if (ret) { |
| 2592 | for (i--; i >=0; i--) { |
| 2593 | chip = &cfi->chips[i]; |
| 2594 | |
| 2595 | mutex_lock(&chip->mutex); |
| 2596 | |
| 2597 | if (chip->state == FL_PM_SUSPENDED) { |
| 2598 | /* No need to force it into a known state here, |
| 2599 | because we're returning failure, and it didn't |
| 2600 | get power cycled */ |
| 2601 | chip->state = chip->oldstate; |
| 2602 | chip->oldstate = FL_READY; |
| 2603 | wake_up(&chip->wq); |
| 2604 | } |
| 2605 | mutex_unlock(&chip->mutex); |
| 2606 | } |
| 2607 | } |
| 2608 | |
| 2609 | return ret; |
| 2610 | } |
| 2611 | |
| 2612 | static void cfi_intelext_restore_locks(struct mtd_info *mtd) |
| 2613 | { |
| 2614 | struct mtd_erase_region_info *region; |
| 2615 | int block, i; |
| 2616 | unsigned long adr; |
| 2617 | size_t len; |
| 2618 | |
| 2619 | for (i = 0; i < mtd->numeraseregions; i++) { |
| 2620 | region = &mtd->eraseregions[i]; |
| 2621 | if (!region->lockmap) |
| 2622 | continue; |
| 2623 | |
| 2624 | for_each_clear_bit(block, region->lockmap, region->numblocks) { |
| 2625 | len = region->erasesize; |
| 2626 | adr = region->offset + block * len; |
| 2627 | cfi_intelext_unlock(mtd, adr, len); |
| 2628 | } |
| 2629 | } |
| 2630 | } |
| 2631 | |
| 2632 | static void cfi_intelext_resume(struct mtd_info *mtd) |
| 2633 | { |
| 2634 | struct map_info *map = mtd->priv; |
| 2635 | struct cfi_private *cfi = map->fldrv_priv; |
| 2636 | struct cfi_pri_intelext *extp = cfi->cmdset_priv; |
| 2637 | int i; |
| 2638 | struct flchip *chip; |
| 2639 | |
| 2640 | for (i=0; i<cfi->numchips; i++) { |
| 2641 | |
| 2642 | chip = &cfi->chips[i]; |
| 2643 | |
| 2644 | mutex_lock(&chip->mutex); |
| 2645 | |
| 2646 | /* Go to known state. Chip may have been power cycled */ |
| 2647 | if (chip->state == FL_PM_SUSPENDED) { |
| 2648 | /* Refresh LH28F640BF Partition Config. Register */ |
| 2649 | fixup_LH28F640BF(mtd); |
| 2650 | map_write(map, CMD(0xFF), cfi->chips[i].start); |
| 2651 | chip->oldstate = chip->state = FL_READY; |
| 2652 | wake_up(&chip->wq); |
| 2653 | } |
| 2654 | |
| 2655 | mutex_unlock(&chip->mutex); |
| 2656 | } |
| 2657 | |
| 2658 | if ((mtd->flags & MTD_POWERUP_LOCK) |
| 2659 | && extp && (extp->FeatureSupport & (1 << 5))) |
| 2660 | cfi_intelext_restore_locks(mtd); |
| 2661 | } |
| 2662 | |
| 2663 | static int cfi_intelext_reset(struct mtd_info *mtd) |
| 2664 | { |
| 2665 | struct map_info *map = mtd->priv; |
| 2666 | struct cfi_private *cfi = map->fldrv_priv; |
| 2667 | int i, ret; |
| 2668 | |
| 2669 | for (i=0; i < cfi->numchips; i++) { |
| 2670 | struct flchip *chip = &cfi->chips[i]; |
| 2671 | |
| 2672 | /* force the completion of any ongoing operation |
| 2673 | and switch to array mode so any bootloader in |
| 2674 | flash is accessible for soft reboot. */ |
| 2675 | mutex_lock(&chip->mutex); |
| 2676 | ret = get_chip(map, chip, chip->start, FL_SHUTDOWN); |
| 2677 | if (!ret) { |
| 2678 | map_write(map, CMD(0xff), chip->start); |
| 2679 | chip->state = FL_SHUTDOWN; |
| 2680 | put_chip(map, chip, chip->start); |
| 2681 | } |
| 2682 | mutex_unlock(&chip->mutex); |
| 2683 | } |
| 2684 | |
| 2685 | return 0; |
| 2686 | } |
| 2687 | |
| 2688 | static int cfi_intelext_reboot(struct notifier_block *nb, unsigned long val, |
| 2689 | void *v) |
| 2690 | { |
| 2691 | struct mtd_info *mtd; |
| 2692 | |
| 2693 | mtd = container_of(nb, struct mtd_info, reboot_notifier); |
| 2694 | cfi_intelext_reset(mtd); |
| 2695 | return NOTIFY_DONE; |
| 2696 | } |
| 2697 | |
| 2698 | static void cfi_intelext_destroy(struct mtd_info *mtd) |
| 2699 | { |
| 2700 | struct map_info *map = mtd->priv; |
| 2701 | struct cfi_private *cfi = map->fldrv_priv; |
| 2702 | struct mtd_erase_region_info *region; |
| 2703 | int i; |
| 2704 | cfi_intelext_reset(mtd); |
| 2705 | unregister_reboot_notifier(&mtd->reboot_notifier); |
| 2706 | kfree(cfi->cmdset_priv); |
| 2707 | kfree(cfi->cfiq); |
| 2708 | kfree(cfi->chips[0].priv); |
| 2709 | kfree(cfi); |
| 2710 | for (i = 0; i < mtd->numeraseregions; i++) { |
| 2711 | region = &mtd->eraseregions[i]; |
| 2712 | kfree(region->lockmap); |
| 2713 | } |
| 2714 | kfree(mtd->eraseregions); |
| 2715 | } |
| 2716 | |
| 2717 | MODULE_LICENSE("GPL"); |
| 2718 | MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org> et al."); |
| 2719 | MODULE_DESCRIPTION("MTD chip driver for Intel/Sharp flash chips"); |
| 2720 | MODULE_ALIAS("cfi_cmdset_0003"); |
| 2721 | MODULE_ALIAS("cfi_cmdset_0200"); |