Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Common Flash Interface support: |
| 3 | * AMD & Fujitsu Standard Vendor Command Set (ID 0x0002) |
| 4 | * |
| 5 | * Copyright (C) 2000 Crossnet Co. <info@crossnet.co.jp> |
| 6 | * Copyright (C) 2004 Arcom Control Systems Ltd <linux@arcom.com> |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 7 | * Copyright (C) 2005 MontaVista Software Inc. <source@mvista.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * |
| 9 | * 2_by_8 routines added by Simon Munton |
| 10 | * |
| 11 | * 4_by_16 work by Carolyn J. Smith |
| 12 | * |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 13 | * XIP support hooks by Vitaly Wool (based on code for Intel flash |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 14 | * by Nicolas Pitre) |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 15 | * |
Christopher Moore | 87e92c0 | 2008-10-17 05:32:22 +0200 | [diff] [blame] | 16 | * 25/09/2008 Christopher Moore: TopBottom fixup for many Macronix with CFI V1.0 |
| 17 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | * Occasionally maintained by Thayne Harbaugh tharbaugh at lnxi dot com |
| 19 | * |
| 20 | * This code is GPL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | */ |
| 22 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/module.h> |
| 24 | #include <linux/types.h> |
| 25 | #include <linux/kernel.h> |
| 26 | #include <linux/sched.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <asm/io.h> |
| 28 | #include <asm/byteorder.h> |
| 29 | |
| 30 | #include <linux/errno.h> |
| 31 | #include <linux/slab.h> |
| 32 | #include <linux/delay.h> |
| 33 | #include <linux/interrupt.h> |
Kevin Cernekee | eafe131 | 2010-04-29 10:26:56 -0700 | [diff] [blame] | 34 | #include <linux/reboot.h> |
Stefan Roese | 1648eaa | 2013-01-18 13:10:05 +0100 | [diff] [blame] | 35 | #include <linux/of.h> |
| 36 | #include <linux/of_platform.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <linux/mtd/map.h> |
| 38 | #include <linux/mtd/mtd.h> |
| 39 | #include <linux/mtd/cfi.h> |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 40 | #include <linux/mtd/xip.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
| 42 | #define AMD_BOOTLOC_BUG |
| 43 | #define FORCE_WORD_WRITE 0 |
| 44 | |
| 45 | #define MAX_WORD_RETRIES 3 |
| 46 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | #define SST49LF004B 0x0060 |
Ryan Jackson | 89072ef | 2006-10-20 14:41:03 -0700 | [diff] [blame] | 48 | #define SST49LF040B 0x0050 |
Eric W. Biedermann | fb4a90b | 2005-05-20 04:28:26 +0100 | [diff] [blame] | 49 | #define SST49LF008A 0x005a |
Haavard Skinnemoen | 0165508 | 2006-08-09 11:06:07 +0200 | [diff] [blame] | 50 | #define AT49BV6416 0x00d6 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
| 52 | static int cfi_amdstd_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *); |
| 53 | static int cfi_amdstd_write_words(struct mtd_info *, loff_t, size_t, size_t *, const u_char *); |
| 54 | static int cfi_amdstd_write_buffers(struct mtd_info *, loff_t, size_t, size_t *, const u_char *); |
| 55 | static int cfi_amdstd_erase_chip(struct mtd_info *, struct erase_info *); |
| 56 | static int cfi_amdstd_erase_varsize(struct mtd_info *, struct erase_info *); |
| 57 | static void cfi_amdstd_sync (struct mtd_info *); |
| 58 | static int cfi_amdstd_suspend (struct mtd_info *); |
| 59 | static void cfi_amdstd_resume (struct mtd_info *); |
Kevin Cernekee | eafe131 | 2010-04-29 10:26:56 -0700 | [diff] [blame] | 60 | static int cfi_amdstd_reboot(struct notifier_block *, unsigned long, void *); |
Christian Riesch | dc7e9ec | 2014-03-06 13:18:27 +0100 | [diff] [blame] | 61 | static int cfi_amdstd_get_fact_prot_info(struct mtd_info *, size_t, |
| 62 | size_t *, struct otp_info *); |
| 63 | static int cfi_amdstd_get_user_prot_info(struct mtd_info *, size_t, |
| 64 | size_t *, struct otp_info *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | static int cfi_amdstd_secsi_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *); |
Christian Riesch | dc7e9ec | 2014-03-06 13:18:27 +0100 | [diff] [blame] | 66 | static int cfi_amdstd_read_fact_prot_reg(struct mtd_info *, loff_t, size_t, |
| 67 | size_t *, u_char *); |
| 68 | static int cfi_amdstd_read_user_prot_reg(struct mtd_info *, loff_t, size_t, |
| 69 | size_t *, u_char *); |
Christian Riesch | af74475 | 2014-03-06 13:18:29 +0100 | [diff] [blame] | 70 | static int cfi_amdstd_write_user_prot_reg(struct mtd_info *, loff_t, size_t, |
| 71 | size_t *, u_char *); |
Christian Riesch | 4f5cb24 | 2014-03-06 13:18:30 +0100 | [diff] [blame] | 72 | static int cfi_amdstd_lock_user_prot_reg(struct mtd_info *, loff_t, size_t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
Ira W. Snyder | 30ec5a2 | 2012-01-06 11:29:19 -0800 | [diff] [blame] | 74 | static int cfi_amdstd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, |
| 75 | size_t *retlen, const u_char *buf); |
| 76 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | static void cfi_amdstd_destroy(struct mtd_info *); |
| 78 | |
| 79 | struct mtd_info *cfi_cmdset_0002(struct map_info *, int); |
| 80 | static struct mtd_info *cfi_amdstd_setup (struct mtd_info *); |
| 81 | |
| 82 | static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode); |
| 83 | static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr); |
| 84 | #include "fwh_lock.h" |
| 85 | |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 86 | static int cfi_atmel_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len); |
| 87 | static int cfi_atmel_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len); |
Haavard Skinnemoen | 0165508 | 2006-08-09 11:06:07 +0200 | [diff] [blame] | 88 | |
Stefan Roese | 1648eaa | 2013-01-18 13:10:05 +0100 | [diff] [blame] | 89 | static int cfi_ppb_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len); |
| 90 | static int cfi_ppb_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len); |
| 91 | static int cfi_ppb_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len); |
| 92 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | static struct mtd_chip_driver cfi_amdstd_chipdrv = { |
| 94 | .probe = NULL, /* Not usable directly */ |
| 95 | .destroy = cfi_amdstd_destroy, |
| 96 | .name = "cfi_cmdset_0002", |
| 97 | .module = THIS_MODULE |
| 98 | }; |
| 99 | |
| 100 | |
| 101 | /* #define DEBUG_CFI_FEATURES */ |
| 102 | |
| 103 | |
| 104 | #ifdef DEBUG_CFI_FEATURES |
| 105 | static void cfi_tell_features(struct cfi_pri_amdstd *extp) |
| 106 | { |
| 107 | const char* erase_suspend[3] = { |
| 108 | "Not supported", "Read only", "Read/write" |
| 109 | }; |
| 110 | const char* top_bottom[6] = { |
| 111 | "No WP", "8x8KiB sectors at top & bottom, no WP", |
| 112 | "Bottom boot", "Top boot", |
| 113 | "Uniform, Bottom WP", "Uniform, Top WP" |
| 114 | }; |
| 115 | |
| 116 | printk(" Silicon revision: %d\n", extp->SiliconRevision >> 1); |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 117 | printk(" Address sensitive unlock: %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | (extp->SiliconRevision & 1) ? "Not required" : "Required"); |
| 119 | |
| 120 | if (extp->EraseSuspend < ARRAY_SIZE(erase_suspend)) |
| 121 | printk(" Erase Suspend: %s\n", erase_suspend[extp->EraseSuspend]); |
| 122 | else |
| 123 | printk(" Erase Suspend: Unknown value %d\n", extp->EraseSuspend); |
| 124 | |
| 125 | if (extp->BlkProt == 0) |
| 126 | printk(" Block protection: Not supported\n"); |
| 127 | else |
| 128 | printk(" Block protection: %d sectors per group\n", extp->BlkProt); |
| 129 | |
| 130 | |
| 131 | printk(" Temporary block unprotect: %s\n", |
| 132 | extp->TmpBlkUnprotect ? "Supported" : "Not supported"); |
| 133 | printk(" Block protect/unprotect scheme: %d\n", extp->BlkProtUnprot); |
| 134 | printk(" Number of simultaneous operations: %d\n", extp->SimultaneousOps); |
| 135 | printk(" Burst mode: %s\n", |
| 136 | extp->BurstMode ? "Supported" : "Not supported"); |
| 137 | if (extp->PageMode == 0) |
| 138 | printk(" Page mode: Not supported\n"); |
| 139 | else |
| 140 | printk(" Page mode: %d word page\n", extp->PageMode << 2); |
| 141 | |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 142 | printk(" Vpp Supply Minimum Program/Erase Voltage: %d.%d V\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | extp->VppMin >> 4, extp->VppMin & 0xf); |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 144 | printk(" Vpp Supply Maximum Program/Erase Voltage: %d.%d V\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | extp->VppMax >> 4, extp->VppMax & 0xf); |
| 146 | |
| 147 | if (extp->TopBottom < ARRAY_SIZE(top_bottom)) |
| 148 | printk(" Top/Bottom Boot Block: %s\n", top_bottom[extp->TopBottom]); |
| 149 | else |
| 150 | printk(" Top/Bottom Boot Block: Unknown value %d\n", extp->TopBottom); |
| 151 | } |
| 152 | #endif |
| 153 | |
| 154 | #ifdef AMD_BOOTLOC_BUG |
| 155 | /* Wheee. Bring me the head of someone at AMD. */ |
Guillaume LECERF | cc31822 | 2010-11-17 12:35:50 +0100 | [diff] [blame] | 156 | static void fixup_amd_bootblock(struct mtd_info *mtd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | { |
| 158 | struct map_info *map = mtd->priv; |
| 159 | struct cfi_private *cfi = map->fldrv_priv; |
| 160 | struct cfi_pri_amdstd *extp = cfi->cmdset_priv; |
| 161 | __u8 major = extp->MajorVersion; |
| 162 | __u8 minor = extp->MinorVersion; |
| 163 | |
| 164 | if (((major << 8) | minor) < 0x3131) { |
| 165 | /* CFI version 1.0 => don't trust bootloc */ |
Christopher Moore | 87e92c0 | 2008-10-17 05:32:22 +0200 | [diff] [blame] | 166 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 167 | pr_debug("%s: JEDEC Vendor ID is 0x%02X Device ID is 0x%02X\n", |
Christopher Moore | 87e92c0 | 2008-10-17 05:32:22 +0200 | [diff] [blame] | 168 | map->name, cfi->mfr, cfi->id); |
| 169 | |
| 170 | /* AFAICS all 29LV400 with a bottom boot block have a device ID |
| 171 | * of 0x22BA in 16-bit mode and 0xBA in 8-bit mode. |
| 172 | * These were badly detected as they have the 0x80 bit set |
| 173 | * so treat them as a special case. |
| 174 | */ |
| 175 | if (((cfi->id == 0xBA) || (cfi->id == 0x22BA)) && |
| 176 | |
| 177 | /* Macronix added CFI to their 2nd generation |
| 178 | * MX29LV400C B/T but AFAICS no other 29LV400 (AMD, |
| 179 | * Fujitsu, Spansion, EON, ESI and older Macronix) |
| 180 | * has CFI. |
| 181 | * |
| 182 | * Therefore also check the manufacturer. |
| 183 | * This reduces the risk of false detection due to |
| 184 | * the 8-bit device ID. |
| 185 | */ |
Guillaume LECERF | f3e69c6 | 2009-12-15 23:01:06 +0100 | [diff] [blame] | 186 | (cfi->mfr == CFI_MFR_MACRONIX)) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 187 | pr_debug("%s: Macronix MX29LV400C with bottom boot block" |
Christopher Moore | 87e92c0 | 2008-10-17 05:32:22 +0200 | [diff] [blame] | 188 | " detected\n", map->name); |
| 189 | extp->TopBottom = 2; /* bottom boot */ |
| 190 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | if (cfi->id & 0x80) { |
| 192 | printk(KERN_WARNING "%s: JEDEC Device ID is 0x%02X. Assuming broken CFI table.\n", map->name, cfi->id); |
| 193 | extp->TopBottom = 3; /* top boot */ |
| 194 | } else { |
| 195 | extp->TopBottom = 2; /* bottom boot */ |
| 196 | } |
Christopher Moore | 87e92c0 | 2008-10-17 05:32:22 +0200 | [diff] [blame] | 197 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 198 | pr_debug("%s: AMD CFI PRI V%c.%c has no boot block field;" |
Christopher Moore | 87e92c0 | 2008-10-17 05:32:22 +0200 | [diff] [blame] | 199 | " deduced %s from Device ID\n", map->name, major, minor, |
| 200 | extp->TopBottom == 2 ? "bottom" : "top"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | #endif |
| 204 | |
Guillaume LECERF | cc31822 | 2010-11-17 12:35:50 +0100 | [diff] [blame] | 205 | static void fixup_use_write_buffers(struct mtd_info *mtd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | { |
| 207 | struct map_info *map = mtd->priv; |
| 208 | struct cfi_private *cfi = map->fldrv_priv; |
| 209 | if (cfi->cfiq->BufWriteTimeoutTyp) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 210 | pr_debug("Using buffer write method\n" ); |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 211 | mtd->_write = cfi_amdstd_write_buffers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
Haavard Skinnemoen | 5b0c5c2 | 2006-08-09 10:54:44 +0200 | [diff] [blame] | 215 | /* Atmel chips don't use the same PRI format as AMD chips */ |
Guillaume LECERF | cc31822 | 2010-11-17 12:35:50 +0100 | [diff] [blame] | 216 | static void fixup_convert_atmel_pri(struct mtd_info *mtd) |
Haavard Skinnemoen | 5b0c5c2 | 2006-08-09 10:54:44 +0200 | [diff] [blame] | 217 | { |
| 218 | struct map_info *map = mtd->priv; |
| 219 | struct cfi_private *cfi = map->fldrv_priv; |
| 220 | struct cfi_pri_amdstd *extp = cfi->cmdset_priv; |
| 221 | struct cfi_pri_atmel atmel_pri; |
| 222 | |
| 223 | memcpy(&atmel_pri, extp, sizeof(atmel_pri)); |
HÃ¥vard Skinnemoen | de591da | 2006-09-15 17:19:31 +0200 | [diff] [blame] | 224 | memset((char *)extp + 5, 0, sizeof(*extp) - 5); |
Haavard Skinnemoen | 5b0c5c2 | 2006-08-09 10:54:44 +0200 | [diff] [blame] | 225 | |
| 226 | if (atmel_pri.Features & 0x02) |
| 227 | extp->EraseSuspend = 2; |
| 228 | |
Haavard Skinnemoen | be8f78b | 2008-09-30 13:55:33 +0200 | [diff] [blame] | 229 | /* Some chips got it backwards... */ |
| 230 | if (cfi->id == AT49BV6416) { |
| 231 | if (atmel_pri.BottomBoot) |
| 232 | extp->TopBottom = 3; |
| 233 | else |
| 234 | extp->TopBottom = 2; |
| 235 | } else { |
| 236 | if (atmel_pri.BottomBoot) |
| 237 | extp->TopBottom = 2; |
| 238 | else |
| 239 | extp->TopBottom = 3; |
| 240 | } |
Hans-Christian Egtvedt | d10a39d | 2007-10-30 16:33:07 +0100 | [diff] [blame] | 241 | |
| 242 | /* burst write mode not supported */ |
| 243 | cfi->cfiq->BufWriteTimeoutTyp = 0; |
| 244 | cfi->cfiq->BufWriteTimeoutMax = 0; |
Haavard Skinnemoen | 5b0c5c2 | 2006-08-09 10:54:44 +0200 | [diff] [blame] | 245 | } |
| 246 | |
Guillaume LECERF | cc31822 | 2010-11-17 12:35:50 +0100 | [diff] [blame] | 247 | static void fixup_use_secsi(struct mtd_info *mtd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | { |
| 249 | /* Setup for chips with a secsi area */ |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 250 | mtd->_read_user_prot_reg = cfi_amdstd_secsi_read; |
| 251 | mtd->_read_fact_prot_reg = cfi_amdstd_secsi_read; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Guillaume LECERF | cc31822 | 2010-11-17 12:35:50 +0100 | [diff] [blame] | 254 | static void fixup_use_erase_chip(struct mtd_info *mtd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | { |
| 256 | struct map_info *map = mtd->priv; |
| 257 | struct cfi_private *cfi = map->fldrv_priv; |
| 258 | if ((cfi->cfiq->NumEraseRegions == 1) && |
| 259 | ((cfi->cfiq->EraseRegionInfo[0] & 0xffff) == 0)) { |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 260 | mtd->_erase = cfi_amdstd_erase_chip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | } |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 262 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Haavard Skinnemoen | 0165508 | 2006-08-09 11:06:07 +0200 | [diff] [blame] | 265 | /* |
| 266 | * Some Atmel chips (e.g. the AT49BV6416) power-up with all sectors |
| 267 | * locked by default. |
| 268 | */ |
Guillaume LECERF | cc31822 | 2010-11-17 12:35:50 +0100 | [diff] [blame] | 269 | static void fixup_use_atmel_lock(struct mtd_info *mtd) |
Haavard Skinnemoen | 0165508 | 2006-08-09 11:06:07 +0200 | [diff] [blame] | 270 | { |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 271 | mtd->_lock = cfi_atmel_lock; |
| 272 | mtd->_unlock = cfi_atmel_unlock; |
Justin Treon | e619a75 | 2008-01-30 10:25:49 -0800 | [diff] [blame] | 273 | mtd->flags |= MTD_POWERUP_LOCK; |
Haavard Skinnemoen | 0165508 | 2006-08-09 11:06:07 +0200 | [diff] [blame] | 274 | } |
| 275 | |
Guillaume LECERF | 83dcd3b | 2010-04-24 17:58:22 +0200 | [diff] [blame] | 276 | static void fixup_old_sst_eraseregion(struct mtd_info *mtd) |
| 277 | { |
| 278 | struct map_info *map = mtd->priv; |
| 279 | struct cfi_private *cfi = map->fldrv_priv; |
| 280 | |
| 281 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 282 | * These flashes report two separate eraseblock regions based on the |
Guillaume LECERF | 83dcd3b | 2010-04-24 17:58:22 +0200 | [diff] [blame] | 283 | * sector_erase-size and block_erase-size, although they both operate on the |
| 284 | * same memory. This is not allowed according to CFI, so we just pick the |
| 285 | * sector_erase-size. |
| 286 | */ |
| 287 | cfi->cfiq->NumEraseRegions = 1; |
| 288 | } |
| 289 | |
Guillaume LECERF | cc31822 | 2010-11-17 12:35:50 +0100 | [diff] [blame] | 290 | static void fixup_sst39vf(struct mtd_info *mtd) |
Guillaume LECERF | 83dcd3b | 2010-04-24 17:58:22 +0200 | [diff] [blame] | 291 | { |
| 292 | struct map_info *map = mtd->priv; |
| 293 | struct cfi_private *cfi = map->fldrv_priv; |
| 294 | |
| 295 | fixup_old_sst_eraseregion(mtd); |
| 296 | |
| 297 | cfi->addr_unlock1 = 0x5555; |
| 298 | cfi->addr_unlock2 = 0x2AAA; |
| 299 | } |
| 300 | |
Guillaume LECERF | cc31822 | 2010-11-17 12:35:50 +0100 | [diff] [blame] | 301 | static void fixup_sst39vf_rev_b(struct mtd_info *mtd) |
Guillaume LECERF | 5a0563f | 2010-04-24 17:58:27 +0200 | [diff] [blame] | 302 | { |
| 303 | struct map_info *map = mtd->priv; |
| 304 | struct cfi_private *cfi = map->fldrv_priv; |
| 305 | |
| 306 | fixup_old_sst_eraseregion(mtd); |
| 307 | |
| 308 | cfi->addr_unlock1 = 0x555; |
| 309 | cfi->addr_unlock2 = 0x2AA; |
Guillaume LECERF | 0896804 | 2010-10-26 10:45:23 +0100 | [diff] [blame] | 310 | |
| 311 | cfi->sector_erase_cmd = CMD(0x50); |
Guillaume LECERF | 5a0563f | 2010-04-24 17:58:27 +0200 | [diff] [blame] | 312 | } |
| 313 | |
Guillaume LECERF | cc31822 | 2010-11-17 12:35:50 +0100 | [diff] [blame] | 314 | static void fixup_sst38vf640x_sectorsize(struct mtd_info *mtd) |
Guillaume LECERF | 9fc05fc | 2010-10-26 11:31:55 +0100 | [diff] [blame] | 315 | { |
| 316 | struct map_info *map = mtd->priv; |
| 317 | struct cfi_private *cfi = map->fldrv_priv; |
| 318 | |
Guillaume LECERF | cc31822 | 2010-11-17 12:35:50 +0100 | [diff] [blame] | 319 | fixup_sst39vf_rev_b(mtd); |
Guillaume LECERF | 9fc05fc | 2010-10-26 11:31:55 +0100 | [diff] [blame] | 320 | |
| 321 | /* |
| 322 | * CFI reports 1024 sectors (0x03ff+1) of 64KBytes (0x0100*256) where |
| 323 | * it should report a size of 8KBytes (0x0020*256). |
| 324 | */ |
| 325 | cfi->cfiq->EraseRegionInfo[0] = 0x002003ff; |
| 326 | pr_warning("%s: Bad 38VF640x CFI data; adjusting sector size from 64 to 8KiB\n", mtd->name); |
| 327 | } |
| 328 | |
Guillaume LECERF | cc31822 | 2010-11-17 12:35:50 +0100 | [diff] [blame] | 329 | static void fixup_s29gl064n_sectors(struct mtd_info *mtd) |
Trent Piepho | 70b0725 | 2008-03-30 21:19:30 -0700 | [diff] [blame] | 330 | { |
| 331 | struct map_info *map = mtd->priv; |
| 332 | struct cfi_private *cfi = map->fldrv_priv; |
| 333 | |
| 334 | if ((cfi->cfiq->EraseRegionInfo[0] & 0xffff) == 0x003f) { |
| 335 | cfi->cfiq->EraseRegionInfo[0] |= 0x0040; |
David Woodhouse | 5df41de | 2012-05-13 23:34:24 -0500 | [diff] [blame] | 336 | pr_warning("%s: Bad S29GL064N CFI data; adjust from 64 to 128 sectors\n", mtd->name); |
Trent Piepho | 70b0725 | 2008-03-30 21:19:30 -0700 | [diff] [blame] | 337 | } |
| 338 | } |
| 339 | |
Guillaume LECERF | cc31822 | 2010-11-17 12:35:50 +0100 | [diff] [blame] | 340 | static void fixup_s29gl032n_sectors(struct mtd_info *mtd) |
Trent Piepho | 70b0725 | 2008-03-30 21:19:30 -0700 | [diff] [blame] | 341 | { |
| 342 | struct map_info *map = mtd->priv; |
| 343 | struct cfi_private *cfi = map->fldrv_priv; |
| 344 | |
| 345 | if ((cfi->cfiq->EraseRegionInfo[1] & 0xffff) == 0x007e) { |
| 346 | cfi->cfiq->EraseRegionInfo[1] &= ~0x0040; |
David Woodhouse | 5df41de | 2012-05-13 23:34:24 -0500 | [diff] [blame] | 347 | pr_warning("%s: Bad S29GL032N CFI data; adjust from 127 to 63 sectors\n", mtd->name); |
Trent Piepho | 70b0725 | 2008-03-30 21:19:30 -0700 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | |
Javier Martin | 43dc03c | 2012-05-11 12:15:41 +0200 | [diff] [blame] | 351 | static void fixup_s29ns512p_sectors(struct mtd_info *mtd) |
| 352 | { |
| 353 | struct map_info *map = mtd->priv; |
| 354 | struct cfi_private *cfi = map->fldrv_priv; |
| 355 | |
| 356 | /* |
| 357 | * S29NS512P flash uses more than 8bits to report number of sectors, |
| 358 | * which is not permitted by CFI. |
| 359 | */ |
| 360 | cfi->cfiq->EraseRegionInfo[0] = 0x020001ff; |
David Woodhouse | 5df41de | 2012-05-13 23:34:24 -0500 | [diff] [blame] | 361 | pr_warning("%s: Bad S29NS512P CFI data; adjust to 512 sectors\n", mtd->name); |
Javier Martin | 43dc03c | 2012-05-11 12:15:41 +0200 | [diff] [blame] | 362 | } |
| 363 | |
Guillaume LECERF | 83dcd3b | 2010-04-24 17:58:22 +0200 | [diff] [blame] | 364 | /* Used to fix CFI-Tables of chips without Extended Query Tables */ |
| 365 | static struct cfi_fixup cfi_nopri_fixup_table[] = { |
Guillaume LECERF | cc31822 | 2010-11-17 12:35:50 +0100 | [diff] [blame] | 366 | { CFI_MFR_SST, 0x234a, fixup_sst39vf }, /* SST39VF1602 */ |
| 367 | { CFI_MFR_SST, 0x234b, fixup_sst39vf }, /* SST39VF1601 */ |
| 368 | { CFI_MFR_SST, 0x235a, fixup_sst39vf }, /* SST39VF3202 */ |
| 369 | { CFI_MFR_SST, 0x235b, fixup_sst39vf }, /* SST39VF3201 */ |
| 370 | { CFI_MFR_SST, 0x235c, fixup_sst39vf_rev_b }, /* SST39VF3202B */ |
| 371 | { CFI_MFR_SST, 0x235d, fixup_sst39vf_rev_b }, /* SST39VF3201B */ |
| 372 | { CFI_MFR_SST, 0x236c, fixup_sst39vf_rev_b }, /* SST39VF6402B */ |
| 373 | { CFI_MFR_SST, 0x236d, fixup_sst39vf_rev_b }, /* SST39VF6401B */ |
| 374 | { 0, 0, NULL } |
Guillaume LECERF | 83dcd3b | 2010-04-24 17:58:22 +0200 | [diff] [blame] | 375 | }; |
| 376 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | static struct cfi_fixup cfi_fixup_table[] = { |
Guillaume LECERF | cc31822 | 2010-11-17 12:35:50 +0100 | [diff] [blame] | 378 | { CFI_MFR_ATMEL, CFI_ID_ANY, fixup_convert_atmel_pri }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | #ifdef AMD_BOOTLOC_BUG |
Guillaume LECERF | cc31822 | 2010-11-17 12:35:50 +0100 | [diff] [blame] | 380 | { CFI_MFR_AMD, CFI_ID_ANY, fixup_amd_bootblock }, |
Steffen Sledz | 1065cda | 2011-03-10 09:05:12 +0100 | [diff] [blame] | 381 | { CFI_MFR_AMIC, CFI_ID_ANY, fixup_amd_bootblock }, |
Guillaume LECERF | cc31822 | 2010-11-17 12:35:50 +0100 | [diff] [blame] | 382 | { CFI_MFR_MACRONIX, CFI_ID_ANY, fixup_amd_bootblock }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | #endif |
Guillaume LECERF | cc31822 | 2010-11-17 12:35:50 +0100 | [diff] [blame] | 384 | { CFI_MFR_AMD, 0x0050, fixup_use_secsi }, |
| 385 | { CFI_MFR_AMD, 0x0053, fixup_use_secsi }, |
| 386 | { CFI_MFR_AMD, 0x0055, fixup_use_secsi }, |
| 387 | { CFI_MFR_AMD, 0x0056, fixup_use_secsi }, |
| 388 | { CFI_MFR_AMD, 0x005C, fixup_use_secsi }, |
| 389 | { CFI_MFR_AMD, 0x005F, fixup_use_secsi }, |
| 390 | { CFI_MFR_AMD, 0x0c01, fixup_s29gl064n_sectors }, |
| 391 | { CFI_MFR_AMD, 0x1301, fixup_s29gl064n_sectors }, |
| 392 | { CFI_MFR_AMD, 0x1a00, fixup_s29gl032n_sectors }, |
| 393 | { CFI_MFR_AMD, 0x1a01, fixup_s29gl032n_sectors }, |
Javier Martin | 43dc03c | 2012-05-11 12:15:41 +0200 | [diff] [blame] | 394 | { CFI_MFR_AMD, 0x3f00, fixup_s29ns512p_sectors }, |
Guillaume LECERF | cc31822 | 2010-11-17 12:35:50 +0100 | [diff] [blame] | 395 | { CFI_MFR_SST, 0x536a, fixup_sst38vf640x_sectorsize }, /* SST38VF6402 */ |
| 396 | { CFI_MFR_SST, 0x536b, fixup_sst38vf640x_sectorsize }, /* SST38VF6401 */ |
| 397 | { CFI_MFR_SST, 0x536c, fixup_sst38vf640x_sectorsize }, /* SST38VF6404 */ |
| 398 | { CFI_MFR_SST, 0x536d, fixup_sst38vf640x_sectorsize }, /* SST38VF6403 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | #if !FORCE_WORD_WRITE |
Guillaume LECERF | cc31822 | 2010-11-17 12:35:50 +0100 | [diff] [blame] | 400 | { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_write_buffers }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | #endif |
Guillaume LECERF | cc31822 | 2010-11-17 12:35:50 +0100 | [diff] [blame] | 402 | { 0, 0, NULL } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | }; |
| 404 | static struct cfi_fixup jedec_fixup_table[] = { |
Guillaume LECERF | cc31822 | 2010-11-17 12:35:50 +0100 | [diff] [blame] | 405 | { CFI_MFR_SST, SST49LF004B, fixup_use_fwh_lock }, |
| 406 | { CFI_MFR_SST, SST49LF040B, fixup_use_fwh_lock }, |
| 407 | { CFI_MFR_SST, SST49LF008A, fixup_use_fwh_lock }, |
| 408 | { 0, 0, NULL } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | }; |
| 410 | |
| 411 | static struct cfi_fixup fixup_table[] = { |
| 412 | /* The CFI vendor ids and the JEDEC vendor IDs appear |
| 413 | * to be common. It is like the devices id's are as |
| 414 | * well. This table is to pick all cases where |
| 415 | * we know that is the case. |
| 416 | */ |
Guillaume LECERF | cc31822 | 2010-11-17 12:35:50 +0100 | [diff] [blame] | 417 | { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_erase_chip }, |
| 418 | { CFI_MFR_ATMEL, AT49BV6416, fixup_use_atmel_lock }, |
| 419 | { 0, 0, NULL } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | }; |
| 421 | |
| 422 | |
Wolfgang Grandegger | fefae48 | 2009-01-08 19:21:27 +0100 | [diff] [blame] | 423 | static void cfi_fixup_major_minor(struct cfi_private *cfi, |
| 424 | struct cfi_pri_amdstd *extp) |
| 425 | { |
Guillaume LECERF | e637276 | 2010-12-17 10:59:41 +0100 | [diff] [blame] | 426 | if (cfi->mfr == CFI_MFR_SAMSUNG) { |
Guillaume LECERF | e8953b7 | 2010-12-17 10:59:47 +0100 | [diff] [blame] | 427 | if ((extp->MajorVersion == '0' && extp->MinorVersion == '0') || |
| 428 | (extp->MajorVersion == '3' && extp->MinorVersion == '3')) { |
Guillaume LECERF | e637276 | 2010-12-17 10:59:41 +0100 | [diff] [blame] | 429 | /* |
| 430 | * Samsung K8P2815UQB and K8D6x16UxM chips |
| 431 | * report major=0 / minor=0. |
Guillaume LECERF | e8953b7 | 2010-12-17 10:59:47 +0100 | [diff] [blame] | 432 | * K8D3x16UxC chips report major=3 / minor=3. |
Guillaume LECERF | e637276 | 2010-12-17 10:59:41 +0100 | [diff] [blame] | 433 | */ |
| 434 | printk(KERN_NOTICE " Fixing Samsung's Amd/Fujitsu" |
| 435 | " Extended Query version to 1.%c\n", |
| 436 | extp->MinorVersion); |
| 437 | extp->MajorVersion = '1'; |
| 438 | } |
| 439 | } |
| 440 | |
Guillaume LECERF | 9fc05fc | 2010-10-26 11:31:55 +0100 | [diff] [blame] | 441 | /* |
| 442 | * SST 38VF640x chips report major=0xFF / minor=0xFF. |
| 443 | */ |
| 444 | if (cfi->mfr == CFI_MFR_SST && (cfi->id >> 4) == 0x0536) { |
| 445 | extp->MajorVersion = '1'; |
| 446 | extp->MinorVersion = '0'; |
| 447 | } |
Wolfgang Grandegger | fefae48 | 2009-01-08 19:21:27 +0100 | [diff] [blame] | 448 | } |
| 449 | |
Gerlando Falauto | 4209628 | 2012-07-03 09:09:47 +0200 | [diff] [blame] | 450 | static int is_m29ew(struct cfi_private *cfi) |
| 451 | { |
| 452 | if (cfi->mfr == CFI_MFR_INTEL && |
| 453 | ((cfi->device_type == CFI_DEVICETYPE_X8 && (cfi->id & 0xff) == 0x7e) || |
| 454 | (cfi->device_type == CFI_DEVICETYPE_X16 && cfi->id == 0x227e))) |
| 455 | return 1; |
| 456 | return 0; |
| 457 | } |
| 458 | |
| 459 | /* |
| 460 | * From TN-13-07: Patching the Linux Kernel and U-Boot for M29 Flash, page 20: |
| 461 | * Some revisions of the M29EW suffer from erase suspend hang ups. In |
| 462 | * particular, it can occur when the sequence |
| 463 | * Erase Confirm -> Suspend -> Program -> Resume |
| 464 | * causes a lockup due to internal timing issues. The consequence is that the |
| 465 | * erase cannot be resumed without inserting a dummy command after programming |
| 466 | * and prior to resuming. [...] The work-around is to issue a dummy write cycle |
| 467 | * that writes an F0 command code before the RESUME command. |
| 468 | */ |
| 469 | static void cfi_fixup_m29ew_erase_suspend(struct map_info *map, |
| 470 | unsigned long adr) |
| 471 | { |
| 472 | struct cfi_private *cfi = map->fldrv_priv; |
| 473 | /* before resume, insert a dummy 0xF0 cycle for Micron M29EW devices */ |
| 474 | if (is_m29ew(cfi)) |
| 475 | map_write(map, CMD(0xF0), adr); |
| 476 | } |
| 477 | |
| 478 | /* |
| 479 | * From TN-13-07: Patching the Linux Kernel and U-Boot for M29 Flash, page 22: |
| 480 | * |
| 481 | * Some revisions of the M29EW (for example, A1 and A2 step revisions) |
| 482 | * are affected by a problem that could cause a hang up when an ERASE SUSPEND |
| 483 | * command is issued after an ERASE RESUME operation without waiting for a |
| 484 | * minimum delay. The result is that once the ERASE seems to be completed |
| 485 | * (no bits are toggling), the contents of the Flash memory block on which |
| 486 | * the erase was ongoing could be inconsistent with the expected values |
| 487 | * (typically, the array value is stuck to the 0xC0, 0xC4, 0x80, or 0x84 |
| 488 | * values), causing a consequent failure of the ERASE operation. |
| 489 | * The occurrence of this issue could be high, especially when file system |
| 490 | * operations on the Flash are intensive. As a result, it is recommended |
| 491 | * that a patch be applied. Intensive file system operations can cause many |
| 492 | * calls to the garbage routine to free Flash space (also by erasing physical |
| 493 | * Flash blocks) and as a result, many consecutive SUSPEND and RESUME |
| 494 | * commands can occur. The problem disappears when a delay is inserted after |
| 495 | * the RESUME command by using the udelay() function available in Linux. |
| 496 | * The DELAY value must be tuned based on the customer's platform. |
| 497 | * The maximum value that fixes the problem in all cases is 500us. |
| 498 | * But, in our experience, a delay of 30 µs to 50 µs is sufficient |
| 499 | * in most cases. |
| 500 | * We have chosen 500µs because this latency is acceptable. |
| 501 | */ |
| 502 | static void cfi_fixup_m29ew_delay_after_resume(struct cfi_private *cfi) |
| 503 | { |
| 504 | /* |
| 505 | * Resolving the Delay After Resume Issue see Micron TN-13-07 |
| 506 | * Worst case delay must be 500µs but 30-50µs should be ok as well |
| 507 | */ |
| 508 | if (is_m29ew(cfi)) |
| 509 | cfi_udelay(500); |
| 510 | } |
| 511 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | struct mtd_info *cfi_cmdset_0002(struct map_info *map, int primary) |
| 513 | { |
| 514 | struct cfi_private *cfi = map->fldrv_priv; |
Stefan Roese | 1648eaa | 2013-01-18 13:10:05 +0100 | [diff] [blame] | 515 | struct device_node __maybe_unused *np = map->device_node; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | struct mtd_info *mtd; |
| 517 | int i; |
| 518 | |
Burman Yan | 95b93a0 | 2006-11-15 21:10:29 +0200 | [diff] [blame] | 519 | mtd = kzalloc(sizeof(*mtd), GFP_KERNEL); |
Jingoo Han | 5c8b1fb | 2014-02-06 15:19:35 +0900 | [diff] [blame] | 520 | if (!mtd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | mtd->priv = map; |
| 523 | mtd->type = MTD_NORFLASH; |
| 524 | |
| 525 | /* Fill in the default mtd operations */ |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 526 | mtd->_erase = cfi_amdstd_erase_varsize; |
| 527 | mtd->_write = cfi_amdstd_write_words; |
| 528 | mtd->_read = cfi_amdstd_read; |
| 529 | mtd->_sync = cfi_amdstd_sync; |
| 530 | mtd->_suspend = cfi_amdstd_suspend; |
| 531 | mtd->_resume = cfi_amdstd_resume; |
Christian Riesch | dc7e9ec | 2014-03-06 13:18:27 +0100 | [diff] [blame] | 532 | mtd->_read_user_prot_reg = cfi_amdstd_read_user_prot_reg; |
| 533 | mtd->_read_fact_prot_reg = cfi_amdstd_read_fact_prot_reg; |
| 534 | mtd->_get_fact_prot_info = cfi_amdstd_get_fact_prot_info; |
| 535 | mtd->_get_user_prot_info = cfi_amdstd_get_user_prot_info; |
Christian Riesch | af74475 | 2014-03-06 13:18:29 +0100 | [diff] [blame] | 536 | mtd->_write_user_prot_reg = cfi_amdstd_write_user_prot_reg; |
Christian Riesch | 4f5cb24 | 2014-03-06 13:18:30 +0100 | [diff] [blame] | 537 | mtd->_lock_user_prot_reg = cfi_amdstd_lock_user_prot_reg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | mtd->flags = MTD_CAP_NORFLASH; |
| 539 | mtd->name = map->name; |
Artem B. Bityutskiy | 783ed81 | 2006-06-14 19:53:44 +0400 | [diff] [blame] | 540 | mtd->writesize = 1; |
Anatolij Gustschin | 13ce77f | 2011-02-10 16:01:46 +0100 | [diff] [blame] | 541 | mtd->writebufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize; |
Anatolij Gustschin | d261c72 | 2010-12-16 23:42:15 +0100 | [diff] [blame] | 542 | |
Brian Norris | 0a32a10 | 2011-07-19 10:06:10 -0700 | [diff] [blame] | 543 | pr_debug("MTD %s(): write buffer size %d\n", __func__, |
| 544 | mtd->writebufsize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 546 | mtd->_panic_write = cfi_amdstd_panic_write; |
Kevin Cernekee | eafe131 | 2010-04-29 10:26:56 -0700 | [diff] [blame] | 547 | mtd->reboot_notifier.notifier_call = cfi_amdstd_reboot; |
| 548 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | if (cfi->cfi_mode==CFI_MODE_CFI){ |
| 550 | unsigned char bootloc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | __u16 adr = primary?cfi->cfiq->P_ADR:cfi->cfiq->A_ADR; |
| 552 | struct cfi_pri_amdstd *extp; |
| 553 | |
| 554 | extp = (struct cfi_pri_amdstd*)cfi_read_pri(map, adr, sizeof(*extp), "Amd/Fujitsu"); |
Guillaume LECERF | 564b849 | 2010-04-24 17:58:17 +0200 | [diff] [blame] | 555 | if (extp) { |
| 556 | /* |
| 557 | * It's a real CFI chip, not one for which the probe |
| 558 | * routine faked a CFI structure. |
| 559 | */ |
| 560 | cfi_fixup_major_minor(cfi, extp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | |
Guillaume LECERF | e17f47a | 2010-07-02 14:39:10 +0200 | [diff] [blame] | 562 | /* |
Gernot Hoyler | c9ddab2 | 2011-04-11 15:53:35 +0200 | [diff] [blame] | 563 | * Valid primary extension versions are: 1.0, 1.1, 1.2, 1.3, 1.4, 1.5 |
Justin P. Mattock | 631dd1a | 2010-10-18 11:03:14 +0200 | [diff] [blame] | 564 | * see: http://cs.ozerki.net/zap/pub/axim-x5/docs/cfi_r20.pdf, page 19 |
| 565 | * http://www.spansion.com/Support/AppNotes/cfi_100_20011201.pdf |
Guillaume LECERF | 5da1953 | 2010-08-05 13:55:24 +0200 | [diff] [blame] | 566 | * http://www.spansion.com/Support/Datasheets/s29ws-p_00_a12_e.pdf |
Gernot Hoyler | c9ddab2 | 2011-04-11 15:53:35 +0200 | [diff] [blame] | 567 | * http://www.spansion.com/Support/Datasheets/S29GL_128S_01GS_00_02_e.pdf |
Guillaume LECERF | e17f47a | 2010-07-02 14:39:10 +0200 | [diff] [blame] | 568 | */ |
Guillaume LECERF | 564b849 | 2010-04-24 17:58:17 +0200 | [diff] [blame] | 569 | if (extp->MajorVersion != '1' || |
Gernot Hoyler | c9ddab2 | 2011-04-11 15:53:35 +0200 | [diff] [blame] | 570 | (extp->MajorVersion == '1' && (extp->MinorVersion < '0' || extp->MinorVersion > '5'))) { |
Guillaume LECERF | 564b849 | 2010-04-24 17:58:17 +0200 | [diff] [blame] | 571 | printk(KERN_ERR " Unknown Amd/Fujitsu Extended Query " |
Guillaume LECERF | e17f47a | 2010-07-02 14:39:10 +0200 | [diff] [blame] | 572 | "version %c.%c (%#02x/%#02x).\n", |
| 573 | extp->MajorVersion, extp->MinorVersion, |
| 574 | extp->MajorVersion, extp->MinorVersion); |
Guillaume LECERF | 564b849 | 2010-04-24 17:58:17 +0200 | [diff] [blame] | 575 | kfree(extp); |
| 576 | kfree(mtd); |
| 577 | return NULL; |
| 578 | } |
Wolfgang Grandegger | fefae48 | 2009-01-08 19:21:27 +0100 | [diff] [blame] | 579 | |
Guillaume LECERF | e17f47a | 2010-07-02 14:39:10 +0200 | [diff] [blame] | 580 | printk(KERN_INFO " Amd/Fujitsu Extended Query version %c.%c.\n", |
| 581 | extp->MajorVersion, extp->MinorVersion); |
| 582 | |
Guillaume LECERF | 564b849 | 2010-04-24 17:58:17 +0200 | [diff] [blame] | 583 | /* Install our own private info structure */ |
| 584 | cfi->cmdset_priv = extp; |
Todd Poynor | d88f977 | 2005-07-20 22:01:17 +0100 | [diff] [blame] | 585 | |
Guillaume LECERF | 564b849 | 2010-04-24 17:58:17 +0200 | [diff] [blame] | 586 | /* Apply cfi device specific fixups */ |
| 587 | cfi_fixup(mtd, cfi_fixup_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | |
| 589 | #ifdef DEBUG_CFI_FEATURES |
Guillaume LECERF | 564b849 | 2010-04-24 17:58:17 +0200 | [diff] [blame] | 590 | /* Tell the user about it in lots of lovely detail */ |
| 591 | cfi_tell_features(extp); |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 592 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | |
Stefan Roese | 1648eaa | 2013-01-18 13:10:05 +0100 | [diff] [blame] | 594 | #ifdef CONFIG_OF |
| 595 | if (np && of_property_read_bool( |
| 596 | np, "use-advanced-sector-protection") |
| 597 | && extp->BlkProtUnprot == 8) { |
| 598 | printk(KERN_INFO " Advanced Sector Protection (PPB Locking) supported\n"); |
| 599 | mtd->_lock = cfi_ppb_lock; |
| 600 | mtd->_unlock = cfi_ppb_unlock; |
| 601 | mtd->_is_locked = cfi_ppb_is_locked; |
| 602 | } |
| 603 | #endif |
| 604 | |
Guillaume LECERF | 564b849 | 2010-04-24 17:58:17 +0200 | [diff] [blame] | 605 | bootloc = extp->TopBottom; |
David Woodhouse | 412da2f | 2010-05-14 01:35:54 +0100 | [diff] [blame] | 606 | if ((bootloc < 2) || (bootloc > 5)) { |
| 607 | printk(KERN_WARNING "%s: CFI contains unrecognised boot " |
| 608 | "bank location (%d). Assuming bottom.\n", |
David Woodhouse | abab7eb | 2010-05-14 09:14:24 +0100 | [diff] [blame] | 609 | map->name, bootloc); |
Guillaume LECERF | 564b849 | 2010-04-24 17:58:17 +0200 | [diff] [blame] | 610 | bootloc = 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | } |
Guillaume LECERF | 564b849 | 2010-04-24 17:58:17 +0200 | [diff] [blame] | 612 | |
| 613 | if (bootloc == 3 && cfi->cfiq->NumEraseRegions > 1) { |
David Woodhouse | 412da2f | 2010-05-14 01:35:54 +0100 | [diff] [blame] | 614 | printk(KERN_WARNING "%s: Swapping erase regions for top-boot CFI table.\n", map->name); |
Guillaume LECERF | 564b849 | 2010-04-24 17:58:17 +0200 | [diff] [blame] | 615 | |
| 616 | for (i=0; i<cfi->cfiq->NumEraseRegions / 2; i++) { |
| 617 | int j = (cfi->cfiq->NumEraseRegions-1)-i; |
Guillaume LECERF | 564b849 | 2010-04-24 17:58:17 +0200 | [diff] [blame] | 618 | |
Fabian Frederick | fdd9d27 | 2015-06-10 18:31:32 +0200 | [diff] [blame] | 619 | swap(cfi->cfiq->EraseRegionInfo[i], |
| 620 | cfi->cfiq->EraseRegionInfo[j]); |
Guillaume LECERF | 564b849 | 2010-04-24 17:58:17 +0200 | [diff] [blame] | 621 | } |
| 622 | } |
| 623 | /* Set the default CFI lock/unlock addresses */ |
| 624 | cfi->addr_unlock1 = 0x555; |
| 625 | cfi->addr_unlock2 = 0x2aa; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | } |
Guillaume LECERF | 83dcd3b | 2010-04-24 17:58:22 +0200 | [diff] [blame] | 627 | cfi_fixup(mtd, cfi_nopri_fixup_table); |
Guillaume LECERF | 564b849 | 2010-04-24 17:58:17 +0200 | [diff] [blame] | 628 | |
| 629 | if (!cfi->addr_unlock1 || !cfi->addr_unlock2) { |
| 630 | kfree(mtd); |
| 631 | return NULL; |
| 632 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | |
| 634 | } /* CFI mode */ |
| 635 | else if (cfi->cfi_mode == CFI_MODE_JEDEC) { |
| 636 | /* Apply jedec specific fixups */ |
| 637 | cfi_fixup(mtd, jedec_fixup_table); |
| 638 | } |
| 639 | /* Apply generic fixups */ |
| 640 | cfi_fixup(mtd, fixup_table); |
| 641 | |
| 642 | for (i=0; i< cfi->numchips; i++) { |
| 643 | cfi->chips[i].word_write_time = 1<<cfi->cfiq->WordWriteTimeoutTyp; |
| 644 | cfi->chips[i].buffer_write_time = 1<<cfi->cfiq->BufWriteTimeoutTyp; |
| 645 | cfi->chips[i].erase_time = 1<<cfi->cfiq->BlockEraseTimeoutTyp; |
Bean Huo | 6534e68 | 2014-07-16 09:05:26 -0700 | [diff] [blame] | 646 | /* |
| 647 | * First calculate the timeout max according to timeout field |
| 648 | * of struct cfi_ident that probed from chip's CFI aera, if |
| 649 | * available. Specify a minimum of 2000us, in case the CFI data |
| 650 | * is wrong. |
| 651 | */ |
| 652 | if (cfi->cfiq->BufWriteTimeoutTyp && |
| 653 | cfi->cfiq->BufWriteTimeoutMax) |
| 654 | cfi->chips[i].buffer_write_time_max = |
| 655 | 1 << (cfi->cfiq->BufWriteTimeoutTyp + |
| 656 | cfi->cfiq->BufWriteTimeoutMax); |
| 657 | else |
| 658 | cfi->chips[i].buffer_write_time_max = 0; |
| 659 | |
| 660 | cfi->chips[i].buffer_write_time_max = |
| 661 | max(cfi->chips[i].buffer_write_time_max, 2000); |
| 662 | |
Vijay Sampath | 83d4809 | 2007-03-06 02:39:44 -0800 | [diff] [blame] | 663 | cfi->chips[i].ref_point_counter = 0; |
| 664 | init_waitqueue_head(&(cfi->chips[i].wq)); |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 665 | } |
| 666 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | map->fldrv = &cfi_amdstd_chipdrv; |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 668 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | return cfi_amdstd_setup(mtd); |
| 670 | } |
Guillaume LECERF | 8046112 | 2010-05-20 16:54:10 +0200 | [diff] [blame] | 671 | struct mtd_info *cfi_cmdset_0006(struct map_info *map, int primary) __attribute__((alias("cfi_cmdset_0002"))); |
David Woodhouse | 1e804ce | 2010-05-20 16:54:05 +0200 | [diff] [blame] | 672 | struct mtd_info *cfi_cmdset_0701(struct map_info *map, int primary) __attribute__((alias("cfi_cmdset_0002"))); |
David Woodhouse | 83ea4ef | 2006-05-08 22:58:25 +0100 | [diff] [blame] | 673 | EXPORT_SYMBOL_GPL(cfi_cmdset_0002); |
Guillaume LECERF | 8046112 | 2010-05-20 16:54:10 +0200 | [diff] [blame] | 674 | EXPORT_SYMBOL_GPL(cfi_cmdset_0006); |
David Woodhouse | 1e804ce | 2010-05-20 16:54:05 +0200 | [diff] [blame] | 675 | EXPORT_SYMBOL_GPL(cfi_cmdset_0701); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | |
| 677 | static struct mtd_info *cfi_amdstd_setup(struct mtd_info *mtd) |
| 678 | { |
| 679 | struct map_info *map = mtd->priv; |
| 680 | struct cfi_private *cfi = map->fldrv_priv; |
| 681 | unsigned long devsize = (1<<cfi->cfiq->DevSize) * cfi->interleave; |
| 682 | unsigned long offset = 0; |
| 683 | int i,j; |
| 684 | |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 685 | printk(KERN_NOTICE "number of %s chips: %d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | (cfi->cfi_mode == CFI_MODE_CFI)?"CFI":"JEDEC",cfi->numchips); |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 687 | /* Select the correct geometry setup */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | mtd->size = devsize * cfi->numchips; |
| 689 | |
| 690 | mtd->numeraseregions = cfi->cfiq->NumEraseRegions * cfi->numchips; |
| 691 | mtd->eraseregions = kmalloc(sizeof(struct mtd_erase_region_info) |
| 692 | * mtd->numeraseregions, GFP_KERNEL); |
Jingoo Han | 5c8b1fb | 2014-02-06 15:19:35 +0900 | [diff] [blame] | 693 | if (!mtd->eraseregions) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | goto setup_err; |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 695 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | for (i=0; i<cfi->cfiq->NumEraseRegions; i++) { |
| 697 | unsigned long ernum, ersize; |
| 698 | ersize = ((cfi->cfiq->EraseRegionInfo[i] >> 8) & ~0xff) * cfi->interleave; |
| 699 | ernum = (cfi->cfiq->EraseRegionInfo[i] & 0xffff) + 1; |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 700 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | if (mtd->erasesize < ersize) { |
| 702 | mtd->erasesize = ersize; |
| 703 | } |
| 704 | for (j=0; j<cfi->numchips; j++) { |
| 705 | mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].offset = (j*devsize)+offset; |
| 706 | mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].erasesize = ersize; |
| 707 | mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].numblocks = ernum; |
| 708 | } |
| 709 | offset += (ersize * ernum); |
| 710 | } |
| 711 | if (offset != devsize) { |
| 712 | /* Argh */ |
| 713 | printk(KERN_WARNING "Sum of regions (%lx) != total size of set of interleaved chips (%lx)\n", offset, devsize); |
| 714 | goto setup_err; |
| 715 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | __module_get(THIS_MODULE); |
Kevin Cernekee | eafe131 | 2010-04-29 10:26:56 -0700 | [diff] [blame] | 718 | register_reboot_notifier(&mtd->reboot_notifier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | return mtd; |
| 720 | |
| 721 | setup_err: |
Jiri Slaby | 17fabf1 | 2010-01-10 10:01:19 +0100 | [diff] [blame] | 722 | kfree(mtd->eraseregions); |
| 723 | kfree(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | kfree(cfi->cmdset_priv); |
| 725 | kfree(cfi->cfiq); |
| 726 | return NULL; |
| 727 | } |
| 728 | |
| 729 | /* |
| 730 | * Return true if the chip is ready. |
| 731 | * |
| 732 | * Ready is one of: read mode, query mode, erase-suspend-read mode (in any |
| 733 | * non-suspended sector) and is indicated by no toggle bits toggling. |
| 734 | * |
| 735 | * Note that anything more complicated than checking if no bits are toggling |
| 736 | * (including checking DQ5 for an error status) is tricky to get working |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 737 | * correctly and is therefore not done (particularly with interleaved chips |
| 738 | * as each chip must be checked independently of the others). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | */ |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 740 | static int __xipram chip_ready(struct map_info *map, unsigned long addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | { |
| 742 | map_word d, t; |
| 743 | |
| 744 | d = map_read(map, addr); |
| 745 | t = map_read(map, addr); |
| 746 | |
| 747 | return map_word_equal(map, d, t); |
| 748 | } |
| 749 | |
Eric W. Biedermann | fb4a90b | 2005-05-20 04:28:26 +0100 | [diff] [blame] | 750 | /* |
| 751 | * Return true if the chip is ready and has the correct value. |
| 752 | * |
| 753 | * Ready is one of: read mode, query mode, erase-suspend-read mode (in any |
| 754 | * non-suspended sector) and it is indicated by no bits toggling. |
| 755 | * |
| 756 | * Error are indicated by toggling bits or bits held with the wrong value, |
| 757 | * or with bits toggling. |
| 758 | * |
| 759 | * Note that anything more complicated than checking if no bits are toggling |
| 760 | * (including checking DQ5 for an error status) is tricky to get working |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 761 | * correctly and is therefore not done (particularly with interleaved chips |
| 762 | * as each chip must be checked independently of the others). |
Eric W. Biedermann | fb4a90b | 2005-05-20 04:28:26 +0100 | [diff] [blame] | 763 | * |
| 764 | */ |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 765 | static int __xipram chip_good(struct map_info *map, unsigned long addr, map_word expected) |
Eric W. Biedermann | fb4a90b | 2005-05-20 04:28:26 +0100 | [diff] [blame] | 766 | { |
| 767 | map_word oldd, curd; |
| 768 | |
| 769 | oldd = map_read(map, addr); |
| 770 | curd = map_read(map, addr); |
| 771 | |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 772 | return map_word_equal(map, oldd, curd) && |
Eric W. Biedermann | fb4a90b | 2005-05-20 04:28:26 +0100 | [diff] [blame] | 773 | map_word_equal(map, curd, expected); |
| 774 | } |
| 775 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode) |
| 777 | { |
| 778 | DECLARE_WAITQUEUE(wait, current); |
| 779 | struct cfi_private *cfi = map->fldrv_priv; |
| 780 | unsigned long timeo; |
| 781 | struct cfi_pri_amdstd *cfip = (struct cfi_pri_amdstd *)cfi->cmdset_priv; |
| 782 | |
| 783 | resettime: |
| 784 | timeo = jiffies + HZ; |
| 785 | retry: |
| 786 | switch (chip->state) { |
| 787 | |
| 788 | case FL_STATUS: |
| 789 | for (;;) { |
| 790 | if (chip_ready(map, adr)) |
| 791 | break; |
| 792 | |
| 793 | if (time_after(jiffies, timeo)) { |
| 794 | printk(KERN_ERR "Waiting for chip to be ready timed out.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | return -EIO; |
| 796 | } |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 797 | mutex_unlock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | cfi_udelay(1); |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 799 | mutex_lock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | /* Someone else might have been playing with it. */ |
| 801 | goto retry; |
| 802 | } |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 803 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | case FL_READY: |
| 805 | case FL_CFI_QUERY: |
| 806 | case FL_JEDEC_QUERY: |
| 807 | return 0; |
| 808 | |
| 809 | case FL_ERASING: |
Joakim Tjernlund | 2695eab | 2009-11-19 12:01:58 +0100 | [diff] [blame] | 810 | if (!cfip || !(cfip->EraseSuspend & (0x1|0x2)) || |
| 811 | !(mode == FL_READY || mode == FL_POINT || |
| 812 | (mode == FL_WRITING && (cfip->EraseSuspend & 0x2)))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | goto sleep; |
| 814 | |
Joakim Tjernlund | f48bbba | 2018-03-01 14:39:41 +0100 | [diff] [blame] | 815 | /* Do not allow suspend iff read/write to EB address */ |
| 816 | if ((adr & chip->in_progress_block_mask) == |
| 817 | chip->in_progress_block_addr) |
| 818 | goto sleep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | |
| 820 | /* Erase suspend */ |
| 821 | /* It's harmless to issue the Erase-Suspend and Erase-Resume |
| 822 | * commands when the erase algorithm isn't in progress. */ |
| 823 | map_write(map, CMD(0xB0), chip->in_progress_block_addr); |
| 824 | chip->oldstate = FL_ERASING; |
| 825 | chip->state = FL_ERASE_SUSPENDING; |
| 826 | chip->erase_suspended = 1; |
| 827 | for (;;) { |
| 828 | if (chip_ready(map, adr)) |
| 829 | break; |
| 830 | |
| 831 | if (time_after(jiffies, timeo)) { |
| 832 | /* Should have suspended the erase by now. |
| 833 | * Send an Erase-Resume command as either |
| 834 | * there was an error (so leave the erase |
| 835 | * routine to recover from it) or we trying to |
| 836 | * use the erase-in-progress sector. */ |
Tadashi Abe | 100f234 | 2011-05-19 15:58:15 +0900 | [diff] [blame] | 837 | put_chip(map, chip, adr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | printk(KERN_ERR "MTD %s(): chip not ready after erase suspend\n", __func__); |
| 839 | return -EIO; |
| 840 | } |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 841 | |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 842 | mutex_unlock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | cfi_udelay(1); |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 844 | mutex_lock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | /* Nobody will touch it while it's in state FL_ERASE_SUSPENDING. |
| 846 | So we can just loop here. */ |
| 847 | } |
| 848 | chip->state = FL_READY; |
| 849 | return 0; |
| 850 | |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 851 | case FL_XIP_WHILE_ERASING: |
| 852 | if (mode != FL_READY && mode != FL_POINT && |
| 853 | (!cfip || !(cfip->EraseSuspend&2))) |
| 854 | goto sleep; |
| 855 | chip->oldstate = chip->state; |
| 856 | chip->state = FL_READY; |
| 857 | return 0; |
| 858 | |
Kevin Cernekee | eafe131 | 2010-04-29 10:26:56 -0700 | [diff] [blame] | 859 | case FL_SHUTDOWN: |
| 860 | /* The machine is rebooting */ |
| 861 | return -EIO; |
| 862 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | case FL_POINT: |
| 864 | /* Only if there's no operation suspended... */ |
| 865 | if (mode == FL_READY && chip->oldstate == FL_READY) |
| 866 | return 0; |
| 867 | |
| 868 | default: |
| 869 | sleep: |
| 870 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 871 | add_wait_queue(&chip->wq, &wait); |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 872 | mutex_unlock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | schedule(); |
| 874 | remove_wait_queue(&chip->wq, &wait); |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 875 | mutex_lock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | goto resettime; |
| 877 | } |
| 878 | } |
| 879 | |
| 880 | |
| 881 | static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr) |
| 882 | { |
| 883 | struct cfi_private *cfi = map->fldrv_priv; |
| 884 | |
| 885 | switch(chip->oldstate) { |
| 886 | case FL_ERASING: |
Gerlando Falauto | 4209628 | 2012-07-03 09:09:47 +0200 | [diff] [blame] | 887 | cfi_fixup_m29ew_erase_suspend(map, |
| 888 | chip->in_progress_block_addr); |
Guillaume LECERF | 0896804 | 2010-10-26 10:45:23 +0100 | [diff] [blame] | 889 | map_write(map, cfi->sector_erase_cmd, chip->in_progress_block_addr); |
Gerlando Falauto | 4209628 | 2012-07-03 09:09:47 +0200 | [diff] [blame] | 890 | cfi_fixup_m29ew_delay_after_resume(cfi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | chip->oldstate = FL_READY; |
| 892 | chip->state = FL_ERASING; |
| 893 | break; |
| 894 | |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 895 | case FL_XIP_WHILE_ERASING: |
| 896 | chip->state = chip->oldstate; |
| 897 | chip->oldstate = FL_READY; |
| 898 | break; |
| 899 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | case FL_READY: |
| 901 | case FL_STATUS: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | break; |
| 903 | default: |
| 904 | printk(KERN_ERR "MTD: put_chip() called with oldstate %d!!\n", chip->oldstate); |
| 905 | } |
| 906 | wake_up(&chip->wq); |
| 907 | } |
| 908 | |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 909 | #ifdef CONFIG_MTD_XIP |
| 910 | |
| 911 | /* |
| 912 | * No interrupt what so ever can be serviced while the flash isn't in array |
| 913 | * mode. This is ensured by the xip_disable() and xip_enable() functions |
| 914 | * enclosing any code path where the flash is known not to be in array mode. |
| 915 | * And within a XIP disabled code path, only functions marked with __xipram |
| 916 | * may be called and nothing else (it's a good thing to inspect generated |
| 917 | * assembly to make sure inline functions were actually inlined and that gcc |
| 918 | * didn't emit calls to its own support functions). Also configuring MTD CFI |
| 919 | * support to a single buswidth and a single interleave is also recommended. |
| 920 | */ |
Thomas Gleixner | f8eb321 | 2005-07-05 01:03:06 +0200 | [diff] [blame] | 921 | |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 922 | static void xip_disable(struct map_info *map, struct flchip *chip, |
| 923 | unsigned long adr) |
| 924 | { |
| 925 | /* TODO: chips with no XIP use should ignore and return */ |
| 926 | (void) map_read(map, adr); /* ensure mmu mapping is up to date */ |
| 927 | local_irq_disable(); |
| 928 | } |
| 929 | |
| 930 | static void __xipram xip_enable(struct map_info *map, struct flchip *chip, |
| 931 | unsigned long adr) |
| 932 | { |
| 933 | struct cfi_private *cfi = map->fldrv_priv; |
| 934 | |
| 935 | if (chip->state != FL_POINT && chip->state != FL_READY) { |
| 936 | map_write(map, CMD(0xf0), adr); |
| 937 | chip->state = FL_READY; |
| 938 | } |
| 939 | (void) map_read(map, adr); |
Thomas Gleixner | 97f927a | 2005-07-07 16:50:16 +0200 | [diff] [blame] | 940 | xip_iprefetch(); |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 941 | local_irq_enable(); |
| 942 | } |
| 943 | |
| 944 | /* |
| 945 | * When a delay is required for the flash operation to complete, the |
| 946 | * xip_udelay() function is polling for both the given timeout and pending |
| 947 | * (but still masked) hardware interrupts. Whenever there is an interrupt |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 948 | * pending then the flash erase operation is suspended, array mode restored |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 949 | * and interrupts unmasked. Task scheduling might also happen at that |
| 950 | * point. The CPU eventually returns from the interrupt or the call to |
| 951 | * schedule() and the suspended flash operation is resumed for the remaining |
| 952 | * of the delay period. |
| 953 | * |
| 954 | * Warning: this function _will_ fool interrupt latency tracing tools. |
| 955 | */ |
| 956 | |
| 957 | static void __xipram xip_udelay(struct map_info *map, struct flchip *chip, |
| 958 | unsigned long adr, int usec) |
| 959 | { |
| 960 | struct cfi_private *cfi = map->fldrv_priv; |
| 961 | struct cfi_pri_amdstd *extp = cfi->cmdset_priv; |
| 962 | map_word status, OK = CMD(0x80); |
| 963 | unsigned long suspended, start = xip_currtime(); |
| 964 | flstate_t oldstate; |
| 965 | |
| 966 | do { |
| 967 | cpu_relax(); |
| 968 | if (xip_irqpending() && extp && |
| 969 | ((chip->state == FL_ERASING && (extp->EraseSuspend & 2))) && |
| 970 | (cfi_interleave_is_1(cfi) || chip->oldstate == FL_READY)) { |
| 971 | /* |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 972 | * Let's suspend the erase operation when supported. |
| 973 | * Note that we currently don't try to suspend |
| 974 | * interleaved chips if there is already another |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 975 | * operation suspended (imagine what happens |
| 976 | * when one chip was already done with the current |
| 977 | * operation while another chip suspended it, then |
| 978 | * we resume the whole thing at once). Yes, it |
| 979 | * can happen! |
| 980 | */ |
| 981 | map_write(map, CMD(0xb0), adr); |
| 982 | usec -= xip_elapsed_since(start); |
| 983 | suspended = xip_currtime(); |
| 984 | do { |
| 985 | if (xip_elapsed_since(suspended) > 100000) { |
| 986 | /* |
| 987 | * The chip doesn't want to suspend |
| 988 | * after waiting for 100 msecs. |
| 989 | * This is a critical error but there |
| 990 | * is not much we can do here. |
| 991 | */ |
| 992 | return; |
| 993 | } |
| 994 | status = map_read(map, adr); |
| 995 | } while (!map_word_andequal(map, status, OK, OK)); |
| 996 | |
| 997 | /* Suspend succeeded */ |
| 998 | oldstate = chip->state; |
| 999 | if (!map_word_bitsset(map, status, CMD(0x40))) |
| 1000 | break; |
| 1001 | chip->state = FL_XIP_WHILE_ERASING; |
| 1002 | chip->erase_suspended = 1; |
| 1003 | map_write(map, CMD(0xf0), adr); |
| 1004 | (void) map_read(map, adr); |
Paulius Zaleckas | ca5c23c | 2008-02-27 01:42:39 +0200 | [diff] [blame] | 1005 | xip_iprefetch(); |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 1006 | local_irq_enable(); |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1007 | mutex_unlock(&chip->mutex); |
Paulius Zaleckas | ca5c23c | 2008-02-27 01:42:39 +0200 | [diff] [blame] | 1008 | xip_iprefetch(); |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 1009 | cond_resched(); |
| 1010 | |
| 1011 | /* |
| 1012 | * We're back. However someone else might have |
| 1013 | * decided to go write to the chip if we are in |
| 1014 | * a suspended erase state. If so let's wait |
| 1015 | * until it's done. |
| 1016 | */ |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1017 | mutex_lock(&chip->mutex); |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 1018 | while (chip->state != FL_XIP_WHILE_ERASING) { |
| 1019 | DECLARE_WAITQUEUE(wait, current); |
| 1020 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 1021 | add_wait_queue(&chip->wq, &wait); |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1022 | mutex_unlock(&chip->mutex); |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 1023 | schedule(); |
| 1024 | remove_wait_queue(&chip->wq, &wait); |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1025 | mutex_lock(&chip->mutex); |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 1026 | } |
| 1027 | /* Disallow XIP again */ |
| 1028 | local_irq_disable(); |
| 1029 | |
Gerlando Falauto | 4209628 | 2012-07-03 09:09:47 +0200 | [diff] [blame] | 1030 | /* Correct Erase Suspend Hangups for M29EW */ |
| 1031 | cfi_fixup_m29ew_erase_suspend(map, adr); |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 1032 | /* Resume the write or erase operation */ |
Guillaume LECERF | 0896804 | 2010-10-26 10:45:23 +0100 | [diff] [blame] | 1033 | map_write(map, cfi->sector_erase_cmd, adr); |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 1034 | chip->state = oldstate; |
| 1035 | start = xip_currtime(); |
| 1036 | } else if (usec >= 1000000/HZ) { |
| 1037 | /* |
| 1038 | * Try to save on CPU power when waiting delay |
| 1039 | * is at least a system timer tick period. |
| 1040 | * No need to be extremely accurate here. |
| 1041 | */ |
| 1042 | xip_cpu_idle(); |
| 1043 | } |
| 1044 | status = map_read(map, adr); |
| 1045 | } while (!map_word_andequal(map, status, OK, OK) |
| 1046 | && xip_elapsed_since(start) < usec); |
| 1047 | } |
| 1048 | |
| 1049 | #define UDELAY(map, chip, adr, usec) xip_udelay(map, chip, adr, usec) |
| 1050 | |
| 1051 | /* |
| 1052 | * The INVALIDATE_CACHED_RANGE() macro is normally used in parallel while |
| 1053 | * the flash is actively programming or erasing since we have to poll for |
| 1054 | * the operation to complete anyway. We can't do that in a generic way with |
| 1055 | * a XIP setup so do it before the actual flash operation in this case |
| 1056 | * and stub it out from INVALIDATE_CACHE_UDELAY. |
| 1057 | */ |
| 1058 | #define XIP_INVAL_CACHED_RANGE(map, from, size) \ |
| 1059 | INVALIDATE_CACHED_RANGE(map, from, size) |
| 1060 | |
| 1061 | #define INVALIDATE_CACHE_UDELAY(map, chip, adr, len, usec) \ |
| 1062 | UDELAY(map, chip, adr, usec) |
| 1063 | |
| 1064 | /* |
| 1065 | * Extra notes: |
| 1066 | * |
| 1067 | * Activating this XIP support changes the way the code works a bit. For |
| 1068 | * example the code to suspend the current process when concurrent access |
| 1069 | * happens is never executed because xip_udelay() will always return with the |
| 1070 | * same chip state as it was entered with. This is why there is no care for |
| 1071 | * the presence of add_wait_queue() or schedule() calls from within a couple |
| 1072 | * xip_disable()'d areas of code, like in do_erase_oneblock for example. |
| 1073 | * The queueing and scheduling are always happening within xip_udelay(). |
| 1074 | * |
| 1075 | * Similarly, get_chip() and put_chip() just happen to always be executed |
| 1076 | * with chip->state set to FL_READY (or FL_XIP_WHILE_*) where flash state |
| 1077 | * is in array mode, therefore never executing many cases therein and not |
| 1078 | * causing any problem with XIP. |
| 1079 | */ |
| 1080 | |
| 1081 | #else |
| 1082 | |
| 1083 | #define xip_disable(map, chip, adr) |
| 1084 | #define xip_enable(map, chip, adr) |
| 1085 | #define XIP_INVAL_CACHED_RANGE(x...) |
| 1086 | |
| 1087 | #define UDELAY(map, chip, adr, usec) \ |
| 1088 | do { \ |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1089 | mutex_unlock(&chip->mutex); \ |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 1090 | cfi_udelay(usec); \ |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1091 | mutex_lock(&chip->mutex); \ |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 1092 | } while (0) |
| 1093 | |
| 1094 | #define INVALIDATE_CACHE_UDELAY(map, chip, adr, len, usec) \ |
| 1095 | do { \ |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1096 | mutex_unlock(&chip->mutex); \ |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 1097 | INVALIDATE_CACHED_RANGE(map, adr, len); \ |
| 1098 | cfi_udelay(usec); \ |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1099 | mutex_lock(&chip->mutex); \ |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 1100 | } while (0) |
| 1101 | |
| 1102 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | |
| 1104 | static inline int do_read_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf) |
| 1105 | { |
| 1106 | unsigned long cmd_addr; |
| 1107 | struct cfi_private *cfi = map->fldrv_priv; |
| 1108 | int ret; |
| 1109 | |
| 1110 | adr += chip->start; |
| 1111 | |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 1112 | /* Ensure cmd read/writes are aligned. */ |
| 1113 | cmd_addr = adr & ~(map_bankwidth(map)-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1115 | mutex_lock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | ret = get_chip(map, chip, cmd_addr, FL_READY); |
| 1117 | if (ret) { |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1118 | mutex_unlock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1119 | return ret; |
| 1120 | } |
| 1121 | |
| 1122 | if (chip->state != FL_POINT && chip->state != FL_READY) { |
| 1123 | map_write(map, CMD(0xf0), cmd_addr); |
| 1124 | chip->state = FL_READY; |
| 1125 | } |
| 1126 | |
| 1127 | map_copy_from(map, buf, adr, len); |
| 1128 | |
| 1129 | put_chip(map, chip, cmd_addr); |
| 1130 | |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1131 | mutex_unlock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1132 | return 0; |
| 1133 | } |
| 1134 | |
| 1135 | |
| 1136 | static int cfi_amdstd_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf) |
| 1137 | { |
| 1138 | struct map_info *map = mtd->priv; |
| 1139 | struct cfi_private *cfi = map->fldrv_priv; |
| 1140 | unsigned long ofs; |
| 1141 | int chipnum; |
| 1142 | int ret = 0; |
| 1143 | |
| 1144 | /* ofs: offset within the first chip that the first read should start */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | chipnum = (from >> cfi->chipshift); |
| 1146 | ofs = from - (chipnum << cfi->chipshift); |
| 1147 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1148 | while (len) { |
| 1149 | unsigned long thislen; |
| 1150 | |
| 1151 | if (chipnum >= cfi->numchips) |
| 1152 | break; |
| 1153 | |
| 1154 | if ((len + ofs -1) >> cfi->chipshift) |
| 1155 | thislen = (1<<cfi->chipshift) - ofs; |
| 1156 | else |
| 1157 | thislen = len; |
| 1158 | |
| 1159 | ret = do_read_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf); |
| 1160 | if (ret) |
| 1161 | break; |
| 1162 | |
| 1163 | *retlen += thislen; |
| 1164 | len -= thislen; |
| 1165 | buf += thislen; |
| 1166 | |
| 1167 | ofs = 0; |
| 1168 | chipnum++; |
| 1169 | } |
| 1170 | return ret; |
| 1171 | } |
| 1172 | |
Christian Riesch | dc7e9ec | 2014-03-06 13:18:27 +0100 | [diff] [blame] | 1173 | typedef int (*otp_op_t)(struct map_info *map, struct flchip *chip, |
Christian Riesch | 4f5cb24 | 2014-03-06 13:18:30 +0100 | [diff] [blame] | 1174 | loff_t adr, size_t len, u_char *buf, size_t grouplen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1175 | |
Christian Riesch | feb8677 | 2013-04-26 21:10:28 +0200 | [diff] [blame] | 1176 | static inline void otp_enter(struct map_info *map, struct flchip *chip, |
| 1177 | loff_t adr, size_t len) |
| 1178 | { |
| 1179 | struct cfi_private *cfi = map->fldrv_priv; |
| 1180 | |
| 1181 | cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, |
| 1182 | cfi->device_type, NULL); |
| 1183 | cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, |
| 1184 | cfi->device_type, NULL); |
| 1185 | cfi_send_gen_cmd(0x88, cfi->addr_unlock1, chip->start, map, cfi, |
| 1186 | cfi->device_type, NULL); |
| 1187 | |
| 1188 | INVALIDATE_CACHED_RANGE(map, chip->start + adr, len); |
| 1189 | } |
| 1190 | |
| 1191 | static inline void otp_exit(struct map_info *map, struct flchip *chip, |
| 1192 | loff_t adr, size_t len) |
| 1193 | { |
| 1194 | struct cfi_private *cfi = map->fldrv_priv; |
| 1195 | |
| 1196 | cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, |
| 1197 | cfi->device_type, NULL); |
| 1198 | cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, |
| 1199 | cfi->device_type, NULL); |
| 1200 | cfi_send_gen_cmd(0x90, cfi->addr_unlock1, chip->start, map, cfi, |
| 1201 | cfi->device_type, NULL); |
| 1202 | cfi_send_gen_cmd(0x00, cfi->addr_unlock1, chip->start, map, cfi, |
| 1203 | cfi->device_type, NULL); |
| 1204 | |
| 1205 | INVALIDATE_CACHED_RANGE(map, chip->start + adr, len); |
| 1206 | } |
| 1207 | |
Christian Riesch | 4f5cb24 | 2014-03-06 13:18:30 +0100 | [diff] [blame] | 1208 | static inline int do_read_secsi_onechip(struct map_info *map, |
| 1209 | struct flchip *chip, loff_t adr, |
| 1210 | size_t len, u_char *buf, |
| 1211 | size_t grouplen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | { |
| 1213 | DECLARE_WAITQUEUE(wait, current); |
| 1214 | unsigned long timeo = jiffies + HZ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1215 | |
| 1216 | retry: |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1217 | mutex_lock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1218 | |
| 1219 | if (chip->state != FL_READY){ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1220 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 1221 | add_wait_queue(&chip->wq, &wait); |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 1222 | |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1223 | mutex_unlock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1224 | |
| 1225 | schedule(); |
| 1226 | remove_wait_queue(&chip->wq, &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1227 | timeo = jiffies + HZ; |
| 1228 | |
| 1229 | goto retry; |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 1230 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | |
| 1232 | adr += chip->start; |
| 1233 | |
| 1234 | chip->state = FL_READY; |
| 1235 | |
Christian Riesch | feb8677 | 2013-04-26 21:10:28 +0200 | [diff] [blame] | 1236 | otp_enter(map, chip, adr, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1237 | map_copy_from(map, buf, adr, len); |
Christian Riesch | feb8677 | 2013-04-26 21:10:28 +0200 | [diff] [blame] | 1238 | otp_exit(map, chip, adr, len); |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 1239 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | wake_up(&chip->wq); |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1241 | mutex_unlock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | |
| 1243 | return 0; |
| 1244 | } |
| 1245 | |
| 1246 | static int cfi_amdstd_secsi_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf) |
| 1247 | { |
| 1248 | struct map_info *map = mtd->priv; |
| 1249 | struct cfi_private *cfi = map->fldrv_priv; |
| 1250 | unsigned long ofs; |
| 1251 | int chipnum; |
| 1252 | int ret = 0; |
| 1253 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1254 | /* ofs: offset within the first chip that the first read should start */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1255 | /* 8 secsi bytes per chip */ |
| 1256 | chipnum=from>>3; |
| 1257 | ofs=from & 7; |
| 1258 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1259 | while (len) { |
| 1260 | unsigned long thislen; |
| 1261 | |
| 1262 | if (chipnum >= cfi->numchips) |
| 1263 | break; |
| 1264 | |
| 1265 | if ((len + ofs -1) >> 3) |
| 1266 | thislen = (1<<3) - ofs; |
| 1267 | else |
| 1268 | thislen = len; |
| 1269 | |
Christian Riesch | 4f5cb24 | 2014-03-06 13:18:30 +0100 | [diff] [blame] | 1270 | ret = do_read_secsi_onechip(map, &cfi->chips[chipnum], ofs, |
| 1271 | thislen, buf, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1272 | if (ret) |
| 1273 | break; |
| 1274 | |
| 1275 | *retlen += thislen; |
| 1276 | len -= thislen; |
| 1277 | buf += thislen; |
| 1278 | |
| 1279 | ofs = 0; |
| 1280 | chipnum++; |
| 1281 | } |
| 1282 | return ret; |
| 1283 | } |
| 1284 | |
Christian Riesch | af74475 | 2014-03-06 13:18:29 +0100 | [diff] [blame] | 1285 | static int __xipram do_write_oneword(struct map_info *map, struct flchip *chip, |
| 1286 | unsigned long adr, map_word datum, |
| 1287 | int mode); |
| 1288 | |
| 1289 | static int do_otp_write(struct map_info *map, struct flchip *chip, loff_t adr, |
Christian Riesch | 4f5cb24 | 2014-03-06 13:18:30 +0100 | [diff] [blame] | 1290 | size_t len, u_char *buf, size_t grouplen) |
Christian Riesch | af74475 | 2014-03-06 13:18:29 +0100 | [diff] [blame] | 1291 | { |
| 1292 | int ret; |
| 1293 | while (len) { |
| 1294 | unsigned long bus_ofs = adr & ~(map_bankwidth(map)-1); |
| 1295 | int gap = adr - bus_ofs; |
| 1296 | int n = min_t(int, len, map_bankwidth(map) - gap); |
Christian Riesch | 636fdbf | 2015-03-31 23:29:22 +0200 | [diff] [blame] | 1297 | map_word datum = map_word_ff(map); |
Christian Riesch | af74475 | 2014-03-06 13:18:29 +0100 | [diff] [blame] | 1298 | |
| 1299 | if (n != map_bankwidth(map)) { |
| 1300 | /* partial write of a word, load old contents */ |
| 1301 | otp_enter(map, chip, bus_ofs, map_bankwidth(map)); |
| 1302 | datum = map_read(map, bus_ofs); |
| 1303 | otp_exit(map, chip, bus_ofs, map_bankwidth(map)); |
| 1304 | } |
| 1305 | |
| 1306 | datum = map_word_load_partial(map, datum, buf, gap, n); |
| 1307 | ret = do_write_oneword(map, chip, bus_ofs, datum, FL_OTP_WRITE); |
| 1308 | if (ret) |
| 1309 | return ret; |
| 1310 | |
| 1311 | adr += n; |
| 1312 | buf += n; |
| 1313 | len -= n; |
| 1314 | } |
| 1315 | |
| 1316 | return 0; |
| 1317 | } |
| 1318 | |
Christian Riesch | 4f5cb24 | 2014-03-06 13:18:30 +0100 | [diff] [blame] | 1319 | static int do_otp_lock(struct map_info *map, struct flchip *chip, loff_t adr, |
| 1320 | size_t len, u_char *buf, size_t grouplen) |
| 1321 | { |
| 1322 | struct cfi_private *cfi = map->fldrv_priv; |
| 1323 | uint8_t lockreg; |
| 1324 | unsigned long timeo; |
| 1325 | int ret; |
| 1326 | |
| 1327 | /* make sure area matches group boundaries */ |
| 1328 | if ((adr != 0) || (len != grouplen)) |
| 1329 | return -EINVAL; |
| 1330 | |
| 1331 | mutex_lock(&chip->mutex); |
| 1332 | ret = get_chip(map, chip, chip->start, FL_LOCKING); |
| 1333 | if (ret) { |
| 1334 | mutex_unlock(&chip->mutex); |
| 1335 | return ret; |
| 1336 | } |
| 1337 | chip->state = FL_LOCKING; |
| 1338 | |
| 1339 | /* Enter lock register command */ |
| 1340 | cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, |
| 1341 | cfi->device_type, NULL); |
| 1342 | cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, |
| 1343 | cfi->device_type, NULL); |
| 1344 | cfi_send_gen_cmd(0x40, cfi->addr_unlock1, chip->start, map, cfi, |
| 1345 | cfi->device_type, NULL); |
| 1346 | |
| 1347 | /* read lock register */ |
| 1348 | lockreg = cfi_read_query(map, 0); |
| 1349 | |
| 1350 | /* set bit 0 to protect extended memory block */ |
| 1351 | lockreg &= ~0x01; |
| 1352 | |
| 1353 | /* set bit 0 to protect extended memory block */ |
| 1354 | /* write lock register */ |
| 1355 | map_write(map, CMD(0xA0), chip->start); |
| 1356 | map_write(map, CMD(lockreg), chip->start); |
| 1357 | |
| 1358 | /* wait for chip to become ready */ |
| 1359 | timeo = jiffies + msecs_to_jiffies(2); |
| 1360 | for (;;) { |
| 1361 | if (chip_ready(map, adr)) |
| 1362 | break; |
| 1363 | |
| 1364 | if (time_after(jiffies, timeo)) { |
| 1365 | pr_err("Waiting for chip to be ready timed out.\n"); |
| 1366 | ret = -EIO; |
| 1367 | break; |
| 1368 | } |
| 1369 | UDELAY(map, chip, 0, 1); |
| 1370 | } |
| 1371 | |
| 1372 | /* exit protection commands */ |
| 1373 | map_write(map, CMD(0x90), chip->start); |
| 1374 | map_write(map, CMD(0x00), chip->start); |
| 1375 | |
| 1376 | chip->state = FL_READY; |
| 1377 | put_chip(map, chip, chip->start); |
| 1378 | mutex_unlock(&chip->mutex); |
| 1379 | |
| 1380 | return ret; |
| 1381 | } |
| 1382 | |
Christian Riesch | dc7e9ec | 2014-03-06 13:18:27 +0100 | [diff] [blame] | 1383 | static int cfi_amdstd_otp_walk(struct mtd_info *mtd, loff_t from, size_t len, |
| 1384 | size_t *retlen, u_char *buf, |
| 1385 | otp_op_t action, int user_regs) |
| 1386 | { |
| 1387 | struct map_info *map = mtd->priv; |
| 1388 | struct cfi_private *cfi = map->fldrv_priv; |
| 1389 | int ofs_factor = cfi->interleave * cfi->device_type; |
| 1390 | unsigned long base; |
| 1391 | int chipnum; |
| 1392 | struct flchip *chip; |
| 1393 | uint8_t otp, lockreg; |
| 1394 | int ret; |
| 1395 | |
| 1396 | size_t user_size, factory_size, otpsize; |
| 1397 | loff_t user_offset, factory_offset, otpoffset; |
| 1398 | int user_locked = 0, otplocked; |
| 1399 | |
| 1400 | *retlen = 0; |
| 1401 | |
| 1402 | for (chipnum = 0; chipnum < cfi->numchips; chipnum++) { |
| 1403 | chip = &cfi->chips[chipnum]; |
| 1404 | factory_size = 0; |
| 1405 | user_size = 0; |
| 1406 | |
| 1407 | /* Micron M29EW family */ |
| 1408 | if (is_m29ew(cfi)) { |
| 1409 | base = chip->start; |
| 1410 | |
| 1411 | /* check whether secsi area is factory locked |
| 1412 | or user lockable */ |
| 1413 | mutex_lock(&chip->mutex); |
| 1414 | ret = get_chip(map, chip, base, FL_CFI_QUERY); |
| 1415 | if (ret) { |
| 1416 | mutex_unlock(&chip->mutex); |
| 1417 | return ret; |
| 1418 | } |
| 1419 | cfi_qry_mode_on(base, map, cfi); |
| 1420 | otp = cfi_read_query(map, base + 0x3 * ofs_factor); |
| 1421 | cfi_qry_mode_off(base, map, cfi); |
| 1422 | put_chip(map, chip, base); |
| 1423 | mutex_unlock(&chip->mutex); |
| 1424 | |
| 1425 | if (otp & 0x80) { |
| 1426 | /* factory locked */ |
| 1427 | factory_offset = 0; |
| 1428 | factory_size = 0x100; |
| 1429 | } else { |
| 1430 | /* customer lockable */ |
| 1431 | user_offset = 0; |
| 1432 | user_size = 0x100; |
| 1433 | |
| 1434 | mutex_lock(&chip->mutex); |
| 1435 | ret = get_chip(map, chip, base, FL_LOCKING); |
Brian Norris | 5d20bad | 2014-08-13 23:35:24 -0700 | [diff] [blame] | 1436 | if (ret) { |
| 1437 | mutex_unlock(&chip->mutex); |
| 1438 | return ret; |
| 1439 | } |
Christian Riesch | dc7e9ec | 2014-03-06 13:18:27 +0100 | [diff] [blame] | 1440 | |
| 1441 | /* Enter lock register command */ |
| 1442 | cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, |
| 1443 | chip->start, map, cfi, |
| 1444 | cfi->device_type, NULL); |
| 1445 | cfi_send_gen_cmd(0x55, cfi->addr_unlock2, |
| 1446 | chip->start, map, cfi, |
| 1447 | cfi->device_type, NULL); |
| 1448 | cfi_send_gen_cmd(0x40, cfi->addr_unlock1, |
| 1449 | chip->start, map, cfi, |
| 1450 | cfi->device_type, NULL); |
| 1451 | /* read lock register */ |
| 1452 | lockreg = cfi_read_query(map, 0); |
| 1453 | /* exit protection commands */ |
| 1454 | map_write(map, CMD(0x90), chip->start); |
| 1455 | map_write(map, CMD(0x00), chip->start); |
| 1456 | put_chip(map, chip, chip->start); |
| 1457 | mutex_unlock(&chip->mutex); |
| 1458 | |
| 1459 | user_locked = ((lockreg & 0x01) == 0x00); |
| 1460 | } |
| 1461 | } |
| 1462 | |
| 1463 | otpsize = user_regs ? user_size : factory_size; |
| 1464 | if (!otpsize) |
| 1465 | continue; |
| 1466 | otpoffset = user_regs ? user_offset : factory_offset; |
| 1467 | otplocked = user_regs ? user_locked : 1; |
| 1468 | |
| 1469 | if (!action) { |
| 1470 | /* return otpinfo */ |
| 1471 | struct otp_info *otpinfo; |
| 1472 | len -= sizeof(*otpinfo); |
| 1473 | if (len <= 0) |
| 1474 | return -ENOSPC; |
| 1475 | otpinfo = (struct otp_info *)buf; |
| 1476 | otpinfo->start = from; |
| 1477 | otpinfo->length = otpsize; |
| 1478 | otpinfo->locked = otplocked; |
| 1479 | buf += sizeof(*otpinfo); |
| 1480 | *retlen += sizeof(*otpinfo); |
| 1481 | from += otpsize; |
| 1482 | } else if ((from < otpsize) && (len > 0)) { |
| 1483 | size_t size; |
| 1484 | size = (len < otpsize - from) ? len : otpsize - from; |
Christian Riesch | 4f5cb24 | 2014-03-06 13:18:30 +0100 | [diff] [blame] | 1485 | ret = action(map, chip, otpoffset + from, size, buf, |
| 1486 | otpsize); |
Christian Riesch | dc7e9ec | 2014-03-06 13:18:27 +0100 | [diff] [blame] | 1487 | if (ret < 0) |
| 1488 | return ret; |
| 1489 | |
| 1490 | buf += size; |
| 1491 | len -= size; |
| 1492 | *retlen += size; |
| 1493 | from = 0; |
| 1494 | } else { |
| 1495 | from -= otpsize; |
| 1496 | } |
| 1497 | } |
| 1498 | return 0; |
| 1499 | } |
| 1500 | |
| 1501 | static int cfi_amdstd_get_fact_prot_info(struct mtd_info *mtd, size_t len, |
| 1502 | size_t *retlen, struct otp_info *buf) |
| 1503 | { |
| 1504 | return cfi_amdstd_otp_walk(mtd, 0, len, retlen, (u_char *)buf, |
| 1505 | NULL, 0); |
| 1506 | } |
| 1507 | |
| 1508 | static int cfi_amdstd_get_user_prot_info(struct mtd_info *mtd, size_t len, |
| 1509 | size_t *retlen, struct otp_info *buf) |
| 1510 | { |
| 1511 | return cfi_amdstd_otp_walk(mtd, 0, len, retlen, (u_char *)buf, |
| 1512 | NULL, 1); |
| 1513 | } |
| 1514 | |
| 1515 | static int cfi_amdstd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, |
| 1516 | size_t len, size_t *retlen, |
| 1517 | u_char *buf) |
| 1518 | { |
| 1519 | return cfi_amdstd_otp_walk(mtd, from, len, retlen, |
| 1520 | buf, do_read_secsi_onechip, 0); |
| 1521 | } |
| 1522 | |
| 1523 | static int cfi_amdstd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, |
| 1524 | size_t len, size_t *retlen, |
| 1525 | u_char *buf) |
| 1526 | { |
| 1527 | return cfi_amdstd_otp_walk(mtd, from, len, retlen, |
| 1528 | buf, do_read_secsi_onechip, 1); |
| 1529 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1530 | |
Christian Riesch | af74475 | 2014-03-06 13:18:29 +0100 | [diff] [blame] | 1531 | static int cfi_amdstd_write_user_prot_reg(struct mtd_info *mtd, loff_t from, |
| 1532 | size_t len, size_t *retlen, |
| 1533 | u_char *buf) |
| 1534 | { |
| 1535 | return cfi_amdstd_otp_walk(mtd, from, len, retlen, buf, |
| 1536 | do_otp_write, 1); |
| 1537 | } |
| 1538 | |
Christian Riesch | 4f5cb24 | 2014-03-06 13:18:30 +0100 | [diff] [blame] | 1539 | static int cfi_amdstd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, |
| 1540 | size_t len) |
| 1541 | { |
| 1542 | size_t retlen; |
| 1543 | return cfi_amdstd_otp_walk(mtd, from, len, &retlen, NULL, |
| 1544 | do_otp_lock, 1); |
| 1545 | } |
| 1546 | |
Christian Riesch | af74475 | 2014-03-06 13:18:29 +0100 | [diff] [blame] | 1547 | static int __xipram do_write_oneword(struct map_info *map, struct flchip *chip, |
| 1548 | unsigned long adr, map_word datum, |
| 1549 | int mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1550 | { |
| 1551 | struct cfi_private *cfi = map->fldrv_priv; |
| 1552 | unsigned long timeo = jiffies + HZ; |
| 1553 | /* |
| 1554 | * We use a 1ms + 1 jiffies generic timeout for writes (most devices |
| 1555 | * have a max write time of a few hundreds usec). However, we should |
| 1556 | * use the maximum timeout value given by the chip at probe time |
| 1557 | * instead. Unfortunately, struct flchip does have a field for |
| 1558 | * maximum timeout, only for typical which can be far too short |
| 1559 | * depending of the conditions. The ' + 1' is to avoid having a |
| 1560 | * timeout of 0 jiffies if HZ is smaller than 1000. |
| 1561 | */ |
| 1562 | unsigned long uWriteTimeout = ( HZ / 1000 ) + 1; |
| 1563 | int ret = 0; |
| 1564 | map_word oldd; |
| 1565 | int retry_cnt = 0; |
| 1566 | |
| 1567 | adr += chip->start; |
| 1568 | |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1569 | mutex_lock(&chip->mutex); |
Christian Riesch | af74475 | 2014-03-06 13:18:29 +0100 | [diff] [blame] | 1570 | ret = get_chip(map, chip, adr, mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1571 | if (ret) { |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1572 | mutex_unlock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1573 | return ret; |
| 1574 | } |
| 1575 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 1576 | pr_debug("MTD %s(): WRITE 0x%.8lx(0x%.8lx)\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1577 | __func__, adr, datum.x[0] ); |
| 1578 | |
Christian Riesch | af74475 | 2014-03-06 13:18:29 +0100 | [diff] [blame] | 1579 | if (mode == FL_OTP_WRITE) |
| 1580 | otp_enter(map, chip, adr, map_bankwidth(map)); |
| 1581 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1582 | /* |
| 1583 | * Check for a NOP for the case when the datum to write is already |
| 1584 | * present - it saves time and works around buggy chips that corrupt |
| 1585 | * data at other locations when 0xff is written to a location that |
| 1586 | * already contains 0xff. |
| 1587 | */ |
| 1588 | oldd = map_read(map, adr); |
| 1589 | if (map_word_equal(map, oldd, datum)) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 1590 | pr_debug("MTD %s(): NOP\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1591 | __func__); |
| 1592 | goto op_done; |
| 1593 | } |
| 1594 | |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 1595 | XIP_INVAL_CACHED_RANGE(map, adr, map_bankwidth(map)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1596 | ENABLE_VPP(map); |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 1597 | xip_disable(map, chip, adr); |
Christian Riesch | af74475 | 2014-03-06 13:18:29 +0100 | [diff] [blame] | 1598 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1599 | retry: |
| 1600 | cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL); |
| 1601 | cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL); |
| 1602 | cfi_send_gen_cmd(0xA0, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL); |
| 1603 | map_write(map, datum, adr); |
Christian Riesch | af74475 | 2014-03-06 13:18:29 +0100 | [diff] [blame] | 1604 | chip->state = mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1605 | |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 1606 | INVALIDATE_CACHE_UDELAY(map, chip, |
| 1607 | adr, map_bankwidth(map), |
| 1608 | chip->word_write_time); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1609 | |
| 1610 | /* See comment above for timeout value. */ |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 1611 | timeo = jiffies + uWriteTimeout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1612 | for (;;) { |
Christian Riesch | af74475 | 2014-03-06 13:18:29 +0100 | [diff] [blame] | 1613 | if (chip->state != mode) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1614 | /* Someone's suspended the write. Sleep */ |
| 1615 | DECLARE_WAITQUEUE(wait, current); |
| 1616 | |
| 1617 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 1618 | add_wait_queue(&chip->wq, &wait); |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1619 | mutex_unlock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1620 | schedule(); |
| 1621 | remove_wait_queue(&chip->wq, &wait); |
| 1622 | timeo = jiffies + (HZ / 2); /* FIXME */ |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1623 | mutex_lock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1624 | continue; |
| 1625 | } |
| 1626 | |
Konstantin Baidarov | b95f960 | 2005-11-07 09:00:05 +0000 | [diff] [blame] | 1627 | if (time_after(jiffies, timeo) && !chip_ready(map, adr)){ |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 1628 | xip_enable(map, chip, adr); |
Eric W. Biedermann | fb4a90b | 2005-05-20 04:28:26 +0100 | [diff] [blame] | 1629 | printk(KERN_WARNING "MTD %s(): software timeout\n", __func__); |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 1630 | xip_disable(map, chip, adr); |
Konstantin Baidarov | b95f960 | 2005-11-07 09:00:05 +0000 | [diff] [blame] | 1631 | break; |
Eric W. Biedermann | fb4a90b | 2005-05-20 04:28:26 +0100 | [diff] [blame] | 1632 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1633 | |
Konstantin Baidarov | b95f960 | 2005-11-07 09:00:05 +0000 | [diff] [blame] | 1634 | if (chip_ready(map, adr)) |
| 1635 | break; |
| 1636 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1637 | /* Latency issues. Drop the lock, wait a while and retry */ |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 1638 | UDELAY(map, chip, adr, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1639 | } |
Eric W. Biedermann | fb4a90b | 2005-05-20 04:28:26 +0100 | [diff] [blame] | 1640 | /* Did we succeed? */ |
| 1641 | if (!chip_good(map, adr, datum)) { |
| 1642 | /* reset on all failures. */ |
| 1643 | map_write( map, CMD(0xF0), chip->start ); |
| 1644 | /* FIXME - should have reset delay before continuing */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1645 | |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 1646 | if (++retry_cnt <= MAX_WORD_RETRIES) |
Eric W. Biedermann | fb4a90b | 2005-05-20 04:28:26 +0100 | [diff] [blame] | 1647 | goto retry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1648 | |
Eric W. Biedermann | fb4a90b | 2005-05-20 04:28:26 +0100 | [diff] [blame] | 1649 | ret = -EIO; |
| 1650 | } |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 1651 | xip_enable(map, chip, adr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1652 | op_done: |
Christian Riesch | af74475 | 2014-03-06 13:18:29 +0100 | [diff] [blame] | 1653 | if (mode == FL_OTP_WRITE) |
| 1654 | otp_exit(map, chip, adr, map_bankwidth(map)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1655 | chip->state = FL_READY; |
Paul Parsons | e7d9377 | 2012-03-07 14:11:16 +0000 | [diff] [blame] | 1656 | DISABLE_VPP(map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1657 | put_chip(map, chip, adr); |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1658 | mutex_unlock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1659 | |
| 1660 | return ret; |
| 1661 | } |
| 1662 | |
| 1663 | |
| 1664 | static int cfi_amdstd_write_words(struct mtd_info *mtd, loff_t to, size_t len, |
| 1665 | size_t *retlen, const u_char *buf) |
| 1666 | { |
| 1667 | struct map_info *map = mtd->priv; |
| 1668 | struct cfi_private *cfi = map->fldrv_priv; |
| 1669 | int ret = 0; |
| 1670 | int chipnum; |
| 1671 | unsigned long ofs, chipstart; |
| 1672 | DECLARE_WAITQUEUE(wait, current); |
| 1673 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1674 | chipnum = to >> cfi->chipshift; |
| 1675 | ofs = to - (chipnum << cfi->chipshift); |
| 1676 | chipstart = cfi->chips[chipnum].start; |
| 1677 | |
| 1678 | /* If it's not bus-aligned, do the first byte write */ |
| 1679 | if (ofs & (map_bankwidth(map)-1)) { |
| 1680 | unsigned long bus_ofs = ofs & ~(map_bankwidth(map)-1); |
| 1681 | int i = ofs - bus_ofs; |
| 1682 | int n = 0; |
| 1683 | map_word tmp_buf; |
| 1684 | |
| 1685 | retry: |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1686 | mutex_lock(&cfi->chips[chipnum].mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1687 | |
| 1688 | if (cfi->chips[chipnum].state != FL_READY) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1689 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 1690 | add_wait_queue(&cfi->chips[chipnum].wq, &wait); |
| 1691 | |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1692 | mutex_unlock(&cfi->chips[chipnum].mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1693 | |
| 1694 | schedule(); |
| 1695 | remove_wait_queue(&cfi->chips[chipnum].wq, &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1696 | goto retry; |
| 1697 | } |
| 1698 | |
| 1699 | /* Load 'tmp_buf' with old contents of flash */ |
| 1700 | tmp_buf = map_read(map, bus_ofs+chipstart); |
| 1701 | |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1702 | mutex_unlock(&cfi->chips[chipnum].mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1703 | |
| 1704 | /* Number of bytes to copy from buffer */ |
| 1705 | n = min_t(int, len, map_bankwidth(map)-i); |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 1706 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1707 | tmp_buf = map_word_load_partial(map, tmp_buf, buf, i, n); |
| 1708 | |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 1709 | ret = do_write_oneword(map, &cfi->chips[chipnum], |
Christian Riesch | af74475 | 2014-03-06 13:18:29 +0100 | [diff] [blame] | 1710 | bus_ofs, tmp_buf, FL_WRITING); |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 1711 | if (ret) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1712 | return ret; |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 1713 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1714 | ofs += n; |
| 1715 | buf += n; |
| 1716 | (*retlen) += n; |
| 1717 | len -= n; |
| 1718 | |
| 1719 | if (ofs >> cfi->chipshift) { |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 1720 | chipnum ++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1721 | ofs = 0; |
| 1722 | if (chipnum == cfi->numchips) |
| 1723 | return 0; |
| 1724 | } |
| 1725 | } |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 1726 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1727 | /* We are now aligned, write as much as possible */ |
| 1728 | while(len >= map_bankwidth(map)) { |
| 1729 | map_word datum; |
| 1730 | |
| 1731 | datum = map_word_load(map, buf); |
| 1732 | |
| 1733 | ret = do_write_oneword(map, &cfi->chips[chipnum], |
Christian Riesch | af74475 | 2014-03-06 13:18:29 +0100 | [diff] [blame] | 1734 | ofs, datum, FL_WRITING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1735 | if (ret) |
| 1736 | return ret; |
| 1737 | |
| 1738 | ofs += map_bankwidth(map); |
| 1739 | buf += map_bankwidth(map); |
| 1740 | (*retlen) += map_bankwidth(map); |
| 1741 | len -= map_bankwidth(map); |
| 1742 | |
| 1743 | if (ofs >> cfi->chipshift) { |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 1744 | chipnum ++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1745 | ofs = 0; |
| 1746 | if (chipnum == cfi->numchips) |
| 1747 | return 0; |
| 1748 | chipstart = cfi->chips[chipnum].start; |
| 1749 | } |
| 1750 | } |
| 1751 | |
| 1752 | /* Write the trailing bytes if any */ |
| 1753 | if (len & (map_bankwidth(map)-1)) { |
| 1754 | map_word tmp_buf; |
| 1755 | |
| 1756 | retry1: |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1757 | mutex_lock(&cfi->chips[chipnum].mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1758 | |
| 1759 | if (cfi->chips[chipnum].state != FL_READY) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1760 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 1761 | add_wait_queue(&cfi->chips[chipnum].wq, &wait); |
| 1762 | |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1763 | mutex_unlock(&cfi->chips[chipnum].mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1764 | |
| 1765 | schedule(); |
| 1766 | remove_wait_queue(&cfi->chips[chipnum].wq, &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1767 | goto retry1; |
| 1768 | } |
| 1769 | |
| 1770 | tmp_buf = map_read(map, ofs + chipstart); |
| 1771 | |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1772 | mutex_unlock(&cfi->chips[chipnum].mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1773 | |
| 1774 | tmp_buf = map_word_load_partial(map, tmp_buf, buf, 0, len); |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 1775 | |
| 1776 | ret = do_write_oneword(map, &cfi->chips[chipnum], |
Christian Riesch | af74475 | 2014-03-06 13:18:29 +0100 | [diff] [blame] | 1777 | ofs, tmp_buf, FL_WRITING); |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 1778 | if (ret) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1779 | return ret; |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 1780 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1781 | (*retlen) += len; |
| 1782 | } |
| 1783 | |
| 1784 | return 0; |
| 1785 | } |
| 1786 | |
| 1787 | |
| 1788 | /* |
| 1789 | * FIXME: interleaved mode not tested, and probably not supported! |
| 1790 | */ |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 1791 | static int __xipram do_write_buffer(struct map_info *map, struct flchip *chip, |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 1792 | unsigned long adr, const u_char *buf, |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 1793 | int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1794 | { |
| 1795 | struct cfi_private *cfi = map->fldrv_priv; |
| 1796 | unsigned long timeo = jiffies + HZ; |
Bean Huo | 6534e68 | 2014-07-16 09:05:26 -0700 | [diff] [blame] | 1797 | /* |
| 1798 | * Timeout is calculated according to CFI data, if available. |
| 1799 | * See more comments in cfi_cmdset_0002(). |
| 1800 | */ |
| 1801 | unsigned long uWriteTimeout = |
| 1802 | usecs_to_jiffies(chip->buffer_write_time_max); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1803 | int ret = -EIO; |
| 1804 | unsigned long cmd_adr; |
| 1805 | int z, words; |
| 1806 | map_word datum; |
| 1807 | |
| 1808 | adr += chip->start; |
| 1809 | cmd_adr = adr; |
| 1810 | |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1811 | mutex_lock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1812 | ret = get_chip(map, chip, adr, FL_WRITING); |
| 1813 | if (ret) { |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1814 | mutex_unlock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1815 | return ret; |
| 1816 | } |
| 1817 | |
| 1818 | datum = map_word_load(map, buf); |
| 1819 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 1820 | pr_debug("MTD %s(): WRITE 0x%.8lx(0x%.8lx)\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1821 | __func__, adr, datum.x[0] ); |
| 1822 | |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 1823 | XIP_INVAL_CACHED_RANGE(map, adr, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1824 | ENABLE_VPP(map); |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 1825 | xip_disable(map, chip, cmd_adr); |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 1826 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1827 | cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL); |
| 1828 | cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1829 | |
| 1830 | /* Write Buffer Load */ |
| 1831 | map_write(map, CMD(0x25), cmd_adr); |
| 1832 | |
| 1833 | chip->state = FL_WRITING_TO_BUFFER; |
| 1834 | |
| 1835 | /* Write length of data to come */ |
| 1836 | words = len / map_bankwidth(map); |
| 1837 | map_write(map, CMD(words - 1), cmd_adr); |
| 1838 | /* Write data */ |
| 1839 | z = 0; |
| 1840 | while(z < words * map_bankwidth(map)) { |
| 1841 | datum = map_word_load(map, buf); |
| 1842 | map_write(map, datum, adr + z); |
| 1843 | |
| 1844 | z += map_bankwidth(map); |
| 1845 | buf += map_bankwidth(map); |
| 1846 | } |
| 1847 | z -= map_bankwidth(map); |
| 1848 | |
| 1849 | adr += z; |
| 1850 | |
| 1851 | /* Write Buffer Program Confirm: GO GO GO */ |
| 1852 | map_write(map, CMD(0x29), cmd_adr); |
| 1853 | chip->state = FL_WRITING; |
| 1854 | |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 1855 | INVALIDATE_CACHE_UDELAY(map, chip, |
| 1856 | adr, map_bankwidth(map), |
| 1857 | chip->word_write_time); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1858 | |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 1859 | timeo = jiffies + uWriteTimeout; |
| 1860 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1861 | for (;;) { |
| 1862 | if (chip->state != FL_WRITING) { |
| 1863 | /* Someone's suspended the write. Sleep */ |
| 1864 | DECLARE_WAITQUEUE(wait, current); |
| 1865 | |
| 1866 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 1867 | add_wait_queue(&chip->wq, &wait); |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1868 | mutex_unlock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1869 | schedule(); |
| 1870 | remove_wait_queue(&chip->wq, &wait); |
| 1871 | timeo = jiffies + (HZ / 2); /* FIXME */ |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1872 | mutex_lock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1873 | continue; |
| 1874 | } |
| 1875 | |
Konstantin Baidarov | b95f960 | 2005-11-07 09:00:05 +0000 | [diff] [blame] | 1876 | if (time_after(jiffies, timeo) && !chip_ready(map, adr)) |
| 1877 | break; |
| 1878 | |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 1879 | if (chip_ready(map, adr)) { |
| 1880 | xip_enable(map, chip, adr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1881 | goto op_done; |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 1882 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1883 | |
| 1884 | /* Latency issues. Drop the lock, wait a while and retry */ |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 1885 | UDELAY(map, chip, adr, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1886 | } |
| 1887 | |
Harald Nordgard-Hansen | 070c322 | 2012-11-23 23:11:03 +0100 | [diff] [blame] | 1888 | /* |
| 1889 | * Recovery from write-buffer programming failures requires |
| 1890 | * the write-to-buffer-reset sequence. Since the last part |
| 1891 | * of the sequence also works as a normal reset, we can run |
| 1892 | * the same commands regardless of why we are here. |
| 1893 | * See e.g. |
| 1894 | * http://www.spansion.com/Support/Application%20Notes/MirrorBit_Write_Buffer_Prog_Page_Buffer_Read_AN.pdf |
| 1895 | */ |
| 1896 | cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, |
| 1897 | cfi->device_type, NULL); |
| 1898 | cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, |
| 1899 | cfi->device_type, NULL); |
| 1900 | cfi_send_gen_cmd(0xF0, cfi->addr_unlock1, chip->start, map, cfi, |
| 1901 | cfi->device_type, NULL); |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 1902 | xip_enable(map, chip, adr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1903 | /* FIXME - should have reset delay before continuing */ |
| 1904 | |
Huang Shijie | 25983b1 | 2013-04-01 17:40:18 +0800 | [diff] [blame] | 1905 | printk(KERN_WARNING "MTD %s(): software timeout, address:0x%.8lx.\n", |
| 1906 | __func__, adr); |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 1907 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1908 | ret = -EIO; |
| 1909 | op_done: |
| 1910 | chip->state = FL_READY; |
Paul Parsons | e7d9377 | 2012-03-07 14:11:16 +0000 | [diff] [blame] | 1911 | DISABLE_VPP(map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1912 | put_chip(map, chip, adr); |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1913 | mutex_unlock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1914 | |
| 1915 | return ret; |
| 1916 | } |
| 1917 | |
| 1918 | |
| 1919 | static int cfi_amdstd_write_buffers(struct mtd_info *mtd, loff_t to, size_t len, |
| 1920 | size_t *retlen, const u_char *buf) |
| 1921 | { |
| 1922 | struct map_info *map = mtd->priv; |
| 1923 | struct cfi_private *cfi = map->fldrv_priv; |
| 1924 | int wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize; |
| 1925 | int ret = 0; |
| 1926 | int chipnum; |
| 1927 | unsigned long ofs; |
| 1928 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1929 | chipnum = to >> cfi->chipshift; |
| 1930 | ofs = to - (chipnum << cfi->chipshift); |
| 1931 | |
| 1932 | /* If it's not bus-aligned, do the first word write */ |
| 1933 | if (ofs & (map_bankwidth(map)-1)) { |
| 1934 | size_t local_len = (-ofs)&(map_bankwidth(map)-1); |
| 1935 | if (local_len > len) |
| 1936 | local_len = len; |
| 1937 | ret = cfi_amdstd_write_words(mtd, ofs + (chipnum<<cfi->chipshift), |
| 1938 | local_len, retlen, buf); |
| 1939 | if (ret) |
| 1940 | return ret; |
| 1941 | ofs += local_len; |
| 1942 | buf += local_len; |
| 1943 | len -= local_len; |
| 1944 | |
| 1945 | if (ofs >> cfi->chipshift) { |
| 1946 | chipnum ++; |
| 1947 | ofs = 0; |
| 1948 | if (chipnum == cfi->numchips) |
| 1949 | return 0; |
| 1950 | } |
| 1951 | } |
| 1952 | |
| 1953 | /* Write buffer is worth it only if more than one word to write... */ |
| 1954 | while (len >= map_bankwidth(map) * 2) { |
| 1955 | /* We must not cross write block boundaries */ |
| 1956 | int size = wbufsize - (ofs & (wbufsize-1)); |
| 1957 | |
| 1958 | if (size > len) |
| 1959 | size = len; |
| 1960 | if (size % map_bankwidth(map)) |
| 1961 | size -= size % map_bankwidth(map); |
| 1962 | |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 1963 | ret = do_write_buffer(map, &cfi->chips[chipnum], |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1964 | ofs, buf, size); |
| 1965 | if (ret) |
| 1966 | return ret; |
| 1967 | |
| 1968 | ofs += size; |
| 1969 | buf += size; |
| 1970 | (*retlen) += size; |
| 1971 | len -= size; |
| 1972 | |
| 1973 | if (ofs >> cfi->chipshift) { |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 1974 | chipnum ++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1975 | ofs = 0; |
| 1976 | if (chipnum == cfi->numchips) |
| 1977 | return 0; |
| 1978 | } |
| 1979 | } |
| 1980 | |
| 1981 | if (len) { |
| 1982 | size_t retlen_dregs = 0; |
| 1983 | |
| 1984 | ret = cfi_amdstd_write_words(mtd, ofs + (chipnum<<cfi->chipshift), |
| 1985 | len, &retlen_dregs, buf); |
| 1986 | |
| 1987 | *retlen += retlen_dregs; |
| 1988 | return ret; |
| 1989 | } |
| 1990 | |
| 1991 | return 0; |
| 1992 | } |
| 1993 | |
Ira W. Snyder | 30ec5a2 | 2012-01-06 11:29:19 -0800 | [diff] [blame] | 1994 | /* |
| 1995 | * Wait for the flash chip to become ready to write data |
| 1996 | * |
| 1997 | * This is only called during the panic_write() path. When panic_write() |
| 1998 | * is called, the kernel is in the process of a panic, and will soon be |
| 1999 | * dead. Therefore we don't take any locks, and attempt to get access |
| 2000 | * to the chip as soon as possible. |
| 2001 | */ |
| 2002 | static int cfi_amdstd_panic_wait(struct map_info *map, struct flchip *chip, |
| 2003 | unsigned long adr) |
| 2004 | { |
| 2005 | struct cfi_private *cfi = map->fldrv_priv; |
| 2006 | int retries = 10; |
| 2007 | int i; |
| 2008 | |
| 2009 | /* |
| 2010 | * If the driver thinks the chip is idle, and no toggle bits |
| 2011 | * are changing, then the chip is actually idle for sure. |
| 2012 | */ |
| 2013 | if (chip->state == FL_READY && chip_ready(map, adr)) |
| 2014 | return 0; |
| 2015 | |
| 2016 | /* |
| 2017 | * Try several times to reset the chip and then wait for it |
| 2018 | * to become idle. The upper limit of a few milliseconds of |
| 2019 | * delay isn't a big problem: the kernel is dying anyway. It |
| 2020 | * is more important to save the messages. |
| 2021 | */ |
| 2022 | while (retries > 0) { |
| 2023 | const unsigned long timeo = (HZ / 1000) + 1; |
| 2024 | |
| 2025 | /* send the reset command */ |
| 2026 | map_write(map, CMD(0xF0), chip->start); |
| 2027 | |
| 2028 | /* wait for the chip to become ready */ |
| 2029 | for (i = 0; i < jiffies_to_usecs(timeo); i++) { |
| 2030 | if (chip_ready(map, adr)) |
| 2031 | return 0; |
| 2032 | |
| 2033 | udelay(1); |
| 2034 | } |
Brian Norris | 36c6a7ac | 2014-07-21 19:06:19 -0700 | [diff] [blame] | 2035 | |
| 2036 | retries--; |
Ira W. Snyder | 30ec5a2 | 2012-01-06 11:29:19 -0800 | [diff] [blame] | 2037 | } |
| 2038 | |
| 2039 | /* the chip never became ready */ |
| 2040 | return -EBUSY; |
| 2041 | } |
| 2042 | |
| 2043 | /* |
| 2044 | * Write out one word of data to a single flash chip during a kernel panic |
| 2045 | * |
| 2046 | * This is only called during the panic_write() path. When panic_write() |
| 2047 | * is called, the kernel is in the process of a panic, and will soon be |
| 2048 | * dead. Therefore we don't take any locks, and attempt to get access |
| 2049 | * to the chip as soon as possible. |
| 2050 | * |
| 2051 | * The implementation of this routine is intentionally similar to |
| 2052 | * do_write_oneword(), in order to ease code maintenance. |
| 2053 | */ |
| 2054 | static int do_panic_write_oneword(struct map_info *map, struct flchip *chip, |
| 2055 | unsigned long adr, map_word datum) |
| 2056 | { |
| 2057 | const unsigned long uWriteTimeout = (HZ / 1000) + 1; |
| 2058 | struct cfi_private *cfi = map->fldrv_priv; |
| 2059 | int retry_cnt = 0; |
| 2060 | map_word oldd; |
| 2061 | int ret = 0; |
| 2062 | int i; |
| 2063 | |
| 2064 | adr += chip->start; |
| 2065 | |
| 2066 | ret = cfi_amdstd_panic_wait(map, chip, adr); |
| 2067 | if (ret) |
| 2068 | return ret; |
| 2069 | |
| 2070 | pr_debug("MTD %s(): PANIC WRITE 0x%.8lx(0x%.8lx)\n", |
| 2071 | __func__, adr, datum.x[0]); |
| 2072 | |
| 2073 | /* |
| 2074 | * Check for a NOP for the case when the datum to write is already |
| 2075 | * present - it saves time and works around buggy chips that corrupt |
| 2076 | * data at other locations when 0xff is written to a location that |
| 2077 | * already contains 0xff. |
| 2078 | */ |
| 2079 | oldd = map_read(map, adr); |
| 2080 | if (map_word_equal(map, oldd, datum)) { |
| 2081 | pr_debug("MTD %s(): NOP\n", __func__); |
| 2082 | goto op_done; |
| 2083 | } |
| 2084 | |
| 2085 | ENABLE_VPP(map); |
| 2086 | |
| 2087 | retry: |
| 2088 | cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL); |
| 2089 | cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL); |
| 2090 | cfi_send_gen_cmd(0xA0, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL); |
| 2091 | map_write(map, datum, adr); |
| 2092 | |
| 2093 | for (i = 0; i < jiffies_to_usecs(uWriteTimeout); i++) { |
| 2094 | if (chip_ready(map, adr)) |
| 2095 | break; |
| 2096 | |
| 2097 | udelay(1); |
| 2098 | } |
| 2099 | |
| 2100 | if (!chip_good(map, adr, datum)) { |
| 2101 | /* reset on all failures. */ |
| 2102 | map_write(map, CMD(0xF0), chip->start); |
| 2103 | /* FIXME - should have reset delay before continuing */ |
| 2104 | |
| 2105 | if (++retry_cnt <= MAX_WORD_RETRIES) |
| 2106 | goto retry; |
| 2107 | |
| 2108 | ret = -EIO; |
| 2109 | } |
| 2110 | |
| 2111 | op_done: |
| 2112 | DISABLE_VPP(map); |
| 2113 | return ret; |
| 2114 | } |
| 2115 | |
| 2116 | /* |
| 2117 | * Write out some data during a kernel panic |
| 2118 | * |
| 2119 | * This is used by the mtdoops driver to save the dying messages from a |
| 2120 | * kernel which has panic'd. |
| 2121 | * |
| 2122 | * This routine ignores all of the locking used throughout the rest of the |
| 2123 | * driver, in order to ensure that the data gets written out no matter what |
| 2124 | * state this driver (and the flash chip itself) was in when the kernel crashed. |
| 2125 | * |
| 2126 | * The implementation of this routine is intentionally similar to |
| 2127 | * cfi_amdstd_write_words(), in order to ease code maintenance. |
| 2128 | */ |
| 2129 | static int cfi_amdstd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, |
| 2130 | size_t *retlen, const u_char *buf) |
| 2131 | { |
| 2132 | struct map_info *map = mtd->priv; |
| 2133 | struct cfi_private *cfi = map->fldrv_priv; |
| 2134 | unsigned long ofs, chipstart; |
| 2135 | int ret = 0; |
| 2136 | int chipnum; |
| 2137 | |
Ira W. Snyder | 30ec5a2 | 2012-01-06 11:29:19 -0800 | [diff] [blame] | 2138 | chipnum = to >> cfi->chipshift; |
| 2139 | ofs = to - (chipnum << cfi->chipshift); |
| 2140 | chipstart = cfi->chips[chipnum].start; |
| 2141 | |
| 2142 | /* If it's not bus aligned, do the first byte write */ |
| 2143 | if (ofs & (map_bankwidth(map) - 1)) { |
| 2144 | unsigned long bus_ofs = ofs & ~(map_bankwidth(map) - 1); |
| 2145 | int i = ofs - bus_ofs; |
| 2146 | int n = 0; |
| 2147 | map_word tmp_buf; |
| 2148 | |
| 2149 | ret = cfi_amdstd_panic_wait(map, &cfi->chips[chipnum], bus_ofs); |
| 2150 | if (ret) |
| 2151 | return ret; |
| 2152 | |
| 2153 | /* Load 'tmp_buf' with old contents of flash */ |
| 2154 | tmp_buf = map_read(map, bus_ofs + chipstart); |
| 2155 | |
| 2156 | /* Number of bytes to copy from buffer */ |
| 2157 | n = min_t(int, len, map_bankwidth(map) - i); |
| 2158 | |
| 2159 | tmp_buf = map_word_load_partial(map, tmp_buf, buf, i, n); |
| 2160 | |
| 2161 | ret = do_panic_write_oneword(map, &cfi->chips[chipnum], |
| 2162 | bus_ofs, tmp_buf); |
| 2163 | if (ret) |
| 2164 | return ret; |
| 2165 | |
| 2166 | ofs += n; |
| 2167 | buf += n; |
| 2168 | (*retlen) += n; |
| 2169 | len -= n; |
| 2170 | |
| 2171 | if (ofs >> cfi->chipshift) { |
| 2172 | chipnum++; |
| 2173 | ofs = 0; |
| 2174 | if (chipnum == cfi->numchips) |
| 2175 | return 0; |
| 2176 | } |
| 2177 | } |
| 2178 | |
| 2179 | /* We are now aligned, write as much as possible */ |
| 2180 | while (len >= map_bankwidth(map)) { |
| 2181 | map_word datum; |
| 2182 | |
| 2183 | datum = map_word_load(map, buf); |
| 2184 | |
| 2185 | ret = do_panic_write_oneword(map, &cfi->chips[chipnum], |
| 2186 | ofs, datum); |
| 2187 | if (ret) |
| 2188 | return ret; |
| 2189 | |
| 2190 | ofs += map_bankwidth(map); |
| 2191 | buf += map_bankwidth(map); |
| 2192 | (*retlen) += map_bankwidth(map); |
| 2193 | len -= map_bankwidth(map); |
| 2194 | |
| 2195 | if (ofs >> cfi->chipshift) { |
| 2196 | chipnum++; |
| 2197 | ofs = 0; |
| 2198 | if (chipnum == cfi->numchips) |
| 2199 | return 0; |
| 2200 | |
| 2201 | chipstart = cfi->chips[chipnum].start; |
| 2202 | } |
| 2203 | } |
| 2204 | |
| 2205 | /* Write the trailing bytes if any */ |
| 2206 | if (len & (map_bankwidth(map) - 1)) { |
| 2207 | map_word tmp_buf; |
| 2208 | |
| 2209 | ret = cfi_amdstd_panic_wait(map, &cfi->chips[chipnum], ofs); |
| 2210 | if (ret) |
| 2211 | return ret; |
| 2212 | |
| 2213 | tmp_buf = map_read(map, ofs + chipstart); |
| 2214 | |
| 2215 | tmp_buf = map_word_load_partial(map, tmp_buf, buf, 0, len); |
| 2216 | |
| 2217 | ret = do_panic_write_oneword(map, &cfi->chips[chipnum], |
| 2218 | ofs, tmp_buf); |
| 2219 | if (ret) |
| 2220 | return ret; |
| 2221 | |
| 2222 | (*retlen) += len; |
| 2223 | } |
| 2224 | |
| 2225 | return 0; |
| 2226 | } |
| 2227 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2228 | |
| 2229 | /* |
| 2230 | * Handle devices with one erase region, that only implement |
| 2231 | * the chip erase command. |
| 2232 | */ |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 2233 | static int __xipram do_erase_chip(struct map_info *map, struct flchip *chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2234 | { |
| 2235 | struct cfi_private *cfi = map->fldrv_priv; |
| 2236 | unsigned long timeo = jiffies + HZ; |
| 2237 | unsigned long int adr; |
| 2238 | DECLARE_WAITQUEUE(wait, current); |
| 2239 | int ret = 0; |
| 2240 | |
| 2241 | adr = cfi->addr_unlock1; |
| 2242 | |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 2243 | mutex_lock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2244 | ret = get_chip(map, chip, adr, FL_WRITING); |
| 2245 | if (ret) { |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 2246 | mutex_unlock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2247 | return ret; |
| 2248 | } |
| 2249 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 2250 | pr_debug("MTD %s(): ERASE 0x%.8lx\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2251 | __func__, chip->start ); |
| 2252 | |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 2253 | XIP_INVAL_CACHED_RANGE(map, adr, map->size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2254 | ENABLE_VPP(map); |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 2255 | xip_disable(map, chip, adr); |
| 2256 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2257 | cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL); |
| 2258 | cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL); |
| 2259 | cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL); |
| 2260 | cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL); |
| 2261 | cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL); |
| 2262 | cfi_send_gen_cmd(0x10, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL); |
| 2263 | |
| 2264 | chip->state = FL_ERASING; |
| 2265 | chip->erase_suspended = 0; |
| 2266 | chip->in_progress_block_addr = adr; |
Joakim Tjernlund | f48bbba | 2018-03-01 14:39:41 +0100 | [diff] [blame] | 2267 | chip->in_progress_block_mask = ~(map->size - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2268 | |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 2269 | INVALIDATE_CACHE_UDELAY(map, chip, |
| 2270 | adr, map->size, |
| 2271 | chip->erase_time*500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2272 | |
| 2273 | timeo = jiffies + (HZ*20); |
| 2274 | |
| 2275 | for (;;) { |
| 2276 | if (chip->state != FL_ERASING) { |
| 2277 | /* Someone's suspended the erase. Sleep */ |
| 2278 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 2279 | add_wait_queue(&chip->wq, &wait); |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 2280 | mutex_unlock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2281 | schedule(); |
| 2282 | remove_wait_queue(&chip->wq, &wait); |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 2283 | mutex_lock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2284 | continue; |
| 2285 | } |
| 2286 | if (chip->erase_suspended) { |
| 2287 | /* This erase was suspended and resumed. |
| 2288 | Adjust the timeout */ |
| 2289 | timeo = jiffies + (HZ*20); /* FIXME */ |
| 2290 | chip->erase_suspended = 0; |
| 2291 | } |
| 2292 | |
| 2293 | if (chip_ready(map, adr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2294 | break; |
| 2295 | |
Eric W. Biedermann | fb4a90b | 2005-05-20 04:28:26 +0100 | [diff] [blame] | 2296 | if (time_after(jiffies, timeo)) { |
| 2297 | printk(KERN_WARNING "MTD %s(): software timeout\n", |
| 2298 | __func__ ); |
| 2299 | break; |
| 2300 | } |
| 2301 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2302 | /* Latency issues. Drop the lock, wait a while and retry */ |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 2303 | UDELAY(map, chip, adr, 1000000/HZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2304 | } |
Eric W. Biedermann | fb4a90b | 2005-05-20 04:28:26 +0100 | [diff] [blame] | 2305 | /* Did we succeed? */ |
| 2306 | if (!chip_good(map, adr, map_word_ff(map))) { |
| 2307 | /* reset on all failures. */ |
| 2308 | map_write( map, CMD(0xF0), chip->start ); |
| 2309 | /* FIXME - should have reset delay before continuing */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2310 | |
Eric W. Biedermann | fb4a90b | 2005-05-20 04:28:26 +0100 | [diff] [blame] | 2311 | ret = -EIO; |
| 2312 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2313 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2314 | chip->state = FL_READY; |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 2315 | xip_enable(map, chip, adr); |
Paul Parsons | e7d9377 | 2012-03-07 14:11:16 +0000 | [diff] [blame] | 2316 | DISABLE_VPP(map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2317 | put_chip(map, chip, adr); |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 2318 | mutex_unlock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2319 | |
| 2320 | return ret; |
| 2321 | } |
| 2322 | |
| 2323 | |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 2324 | static int __xipram do_erase_oneblock(struct map_info *map, struct flchip *chip, unsigned long adr, int len, void *thunk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2325 | { |
| 2326 | struct cfi_private *cfi = map->fldrv_priv; |
| 2327 | unsigned long timeo = jiffies + HZ; |
| 2328 | DECLARE_WAITQUEUE(wait, current); |
| 2329 | int ret = 0; |
| 2330 | |
| 2331 | adr += chip->start; |
| 2332 | |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 2333 | mutex_lock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2334 | ret = get_chip(map, chip, adr, FL_ERASING); |
| 2335 | if (ret) { |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 2336 | mutex_unlock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2337 | return ret; |
| 2338 | } |
| 2339 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 2340 | pr_debug("MTD %s(): ERASE 0x%.8lx\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2341 | __func__, adr ); |
| 2342 | |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 2343 | XIP_INVAL_CACHED_RANGE(map, adr, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2344 | ENABLE_VPP(map); |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 2345 | xip_disable(map, chip, adr); |
| 2346 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2347 | cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL); |
| 2348 | cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL); |
| 2349 | cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL); |
| 2350 | cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL); |
| 2351 | cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL); |
Guillaume LECERF | 0896804 | 2010-10-26 10:45:23 +0100 | [diff] [blame] | 2352 | map_write(map, cfi->sector_erase_cmd, adr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2353 | |
| 2354 | chip->state = FL_ERASING; |
| 2355 | chip->erase_suspended = 0; |
| 2356 | chip->in_progress_block_addr = adr; |
Joakim Tjernlund | f48bbba | 2018-03-01 14:39:41 +0100 | [diff] [blame] | 2357 | chip->in_progress_block_mask = ~(len - 1); |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 2358 | |
| 2359 | INVALIDATE_CACHE_UDELAY(map, chip, |
| 2360 | adr, len, |
| 2361 | chip->erase_time*500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2362 | |
| 2363 | timeo = jiffies + (HZ*20); |
| 2364 | |
| 2365 | for (;;) { |
| 2366 | if (chip->state != FL_ERASING) { |
| 2367 | /* Someone's suspended the erase. Sleep */ |
| 2368 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 2369 | add_wait_queue(&chip->wq, &wait); |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 2370 | mutex_unlock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2371 | schedule(); |
| 2372 | remove_wait_queue(&chip->wq, &wait); |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 2373 | mutex_lock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2374 | continue; |
| 2375 | } |
| 2376 | if (chip->erase_suspended) { |
| 2377 | /* This erase was suspended and resumed. |
| 2378 | Adjust the timeout */ |
| 2379 | timeo = jiffies + (HZ*20); /* FIXME */ |
| 2380 | chip->erase_suspended = 0; |
| 2381 | } |
| 2382 | |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 2383 | if (chip_ready(map, adr)) { |
| 2384 | xip_enable(map, chip, adr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2385 | break; |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 2386 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2387 | |
Eric W. Biedermann | fb4a90b | 2005-05-20 04:28:26 +0100 | [diff] [blame] | 2388 | if (time_after(jiffies, timeo)) { |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 2389 | xip_enable(map, chip, adr); |
Eric W. Biedermann | fb4a90b | 2005-05-20 04:28:26 +0100 | [diff] [blame] | 2390 | printk(KERN_WARNING "MTD %s(): software timeout\n", |
| 2391 | __func__ ); |
| 2392 | break; |
| 2393 | } |
| 2394 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2395 | /* Latency issues. Drop the lock, wait a while and retry */ |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 2396 | UDELAY(map, chip, adr, 1000000/HZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2397 | } |
Eric W. Biedermann | fb4a90b | 2005-05-20 04:28:26 +0100 | [diff] [blame] | 2398 | /* Did we succeed? */ |
Thomas Gleixner | 22fd9a8 | 2005-05-24 15:33:49 +0200 | [diff] [blame] | 2399 | if (!chip_good(map, adr, map_word_ff(map))) { |
Eric W. Biedermann | fb4a90b | 2005-05-20 04:28:26 +0100 | [diff] [blame] | 2400 | /* reset on all failures. */ |
| 2401 | map_write( map, CMD(0xF0), chip->start ); |
| 2402 | /* FIXME - should have reset delay before continuing */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2403 | |
Eric W. Biedermann | fb4a90b | 2005-05-20 04:28:26 +0100 | [diff] [blame] | 2404 | ret = -EIO; |
| 2405 | } |
| 2406 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2407 | chip->state = FL_READY; |
Paul Parsons | e7d9377 | 2012-03-07 14:11:16 +0000 | [diff] [blame] | 2408 | DISABLE_VPP(map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2409 | put_chip(map, chip, adr); |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 2410 | mutex_unlock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2411 | return ret; |
| 2412 | } |
| 2413 | |
| 2414 | |
Ben Dooks | ce0f33a | 2007-05-28 19:59:00 +0100 | [diff] [blame] | 2415 | static int cfi_amdstd_erase_varsize(struct mtd_info *mtd, struct erase_info *instr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2416 | { |
| 2417 | unsigned long ofs, len; |
| 2418 | int ret; |
| 2419 | |
| 2420 | ofs = instr->addr; |
| 2421 | len = instr->len; |
| 2422 | |
| 2423 | ret = cfi_varsize_frob(mtd, do_erase_oneblock, ofs, len, NULL); |
| 2424 | if (ret) |
| 2425 | return ret; |
| 2426 | |
| 2427 | instr->state = MTD_ERASE_DONE; |
| 2428 | mtd_erase_callback(instr); |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 2429 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2430 | return 0; |
| 2431 | } |
| 2432 | |
| 2433 | |
| 2434 | static int cfi_amdstd_erase_chip(struct mtd_info *mtd, struct erase_info *instr) |
| 2435 | { |
| 2436 | struct map_info *map = mtd->priv; |
| 2437 | struct cfi_private *cfi = map->fldrv_priv; |
| 2438 | int ret = 0; |
| 2439 | |
| 2440 | if (instr->addr != 0) |
| 2441 | return -EINVAL; |
| 2442 | |
| 2443 | if (instr->len != mtd->size) |
| 2444 | return -EINVAL; |
| 2445 | |
| 2446 | ret = do_erase_chip(map, &cfi->chips[0]); |
| 2447 | if (ret) |
| 2448 | return ret; |
| 2449 | |
| 2450 | instr->state = MTD_ERASE_DONE; |
| 2451 | mtd_erase_callback(instr); |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 2452 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2453 | return 0; |
| 2454 | } |
| 2455 | |
Haavard Skinnemoen | 0165508 | 2006-08-09 11:06:07 +0200 | [diff] [blame] | 2456 | static int do_atmel_lock(struct map_info *map, struct flchip *chip, |
| 2457 | unsigned long adr, int len, void *thunk) |
| 2458 | { |
| 2459 | struct cfi_private *cfi = map->fldrv_priv; |
| 2460 | int ret; |
| 2461 | |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 2462 | mutex_lock(&chip->mutex); |
Haavard Skinnemoen | 0165508 | 2006-08-09 11:06:07 +0200 | [diff] [blame] | 2463 | ret = get_chip(map, chip, adr + chip->start, FL_LOCKING); |
| 2464 | if (ret) |
| 2465 | goto out_unlock; |
| 2466 | chip->state = FL_LOCKING; |
| 2467 | |
Brian Norris | 0a32a10 | 2011-07-19 10:06:10 -0700 | [diff] [blame] | 2468 | pr_debug("MTD %s(): LOCK 0x%08lx len %d\n", __func__, adr, len); |
Haavard Skinnemoen | 0165508 | 2006-08-09 11:06:07 +0200 | [diff] [blame] | 2469 | |
| 2470 | cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, |
| 2471 | cfi->device_type, NULL); |
| 2472 | cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, |
| 2473 | cfi->device_type, NULL); |
| 2474 | cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi, |
| 2475 | cfi->device_type, NULL); |
| 2476 | cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, |
| 2477 | cfi->device_type, NULL); |
| 2478 | cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, |
| 2479 | cfi->device_type, NULL); |
| 2480 | map_write(map, CMD(0x40), chip->start + adr); |
| 2481 | |
| 2482 | chip->state = FL_READY; |
| 2483 | put_chip(map, chip, adr + chip->start); |
| 2484 | ret = 0; |
| 2485 | |
| 2486 | out_unlock: |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 2487 | mutex_unlock(&chip->mutex); |
Haavard Skinnemoen | 0165508 | 2006-08-09 11:06:07 +0200 | [diff] [blame] | 2488 | return ret; |
| 2489 | } |
| 2490 | |
| 2491 | static int do_atmel_unlock(struct map_info *map, struct flchip *chip, |
| 2492 | unsigned long adr, int len, void *thunk) |
| 2493 | { |
| 2494 | struct cfi_private *cfi = map->fldrv_priv; |
| 2495 | int ret; |
| 2496 | |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 2497 | mutex_lock(&chip->mutex); |
Haavard Skinnemoen | 0165508 | 2006-08-09 11:06:07 +0200 | [diff] [blame] | 2498 | ret = get_chip(map, chip, adr + chip->start, FL_UNLOCKING); |
| 2499 | if (ret) |
| 2500 | goto out_unlock; |
| 2501 | chip->state = FL_UNLOCKING; |
| 2502 | |
Brian Norris | 0a32a10 | 2011-07-19 10:06:10 -0700 | [diff] [blame] | 2503 | pr_debug("MTD %s(): LOCK 0x%08lx len %d\n", __func__, adr, len); |
Haavard Skinnemoen | 0165508 | 2006-08-09 11:06:07 +0200 | [diff] [blame] | 2504 | |
| 2505 | cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, |
| 2506 | cfi->device_type, NULL); |
| 2507 | map_write(map, CMD(0x70), adr); |
| 2508 | |
| 2509 | chip->state = FL_READY; |
| 2510 | put_chip(map, chip, adr + chip->start); |
| 2511 | ret = 0; |
| 2512 | |
| 2513 | out_unlock: |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 2514 | mutex_unlock(&chip->mutex); |
Haavard Skinnemoen | 0165508 | 2006-08-09 11:06:07 +0200 | [diff] [blame] | 2515 | return ret; |
| 2516 | } |
| 2517 | |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 2518 | static int cfi_atmel_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len) |
Haavard Skinnemoen | 0165508 | 2006-08-09 11:06:07 +0200 | [diff] [blame] | 2519 | { |
| 2520 | return cfi_varsize_frob(mtd, do_atmel_lock, ofs, len, NULL); |
| 2521 | } |
| 2522 | |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 2523 | static int cfi_atmel_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len) |
Haavard Skinnemoen | 0165508 | 2006-08-09 11:06:07 +0200 | [diff] [blame] | 2524 | { |
| 2525 | return cfi_varsize_frob(mtd, do_atmel_unlock, ofs, len, NULL); |
| 2526 | } |
| 2527 | |
Stefan Roese | 1648eaa | 2013-01-18 13:10:05 +0100 | [diff] [blame] | 2528 | /* |
| 2529 | * Advanced Sector Protection - PPB (Persistent Protection Bit) locking |
| 2530 | */ |
| 2531 | |
| 2532 | struct ppb_lock { |
| 2533 | struct flchip *chip; |
| 2534 | loff_t offset; |
| 2535 | int locked; |
| 2536 | }; |
| 2537 | |
| 2538 | #define MAX_SECTORS 512 |
| 2539 | |
| 2540 | #define DO_XXLOCK_ONEBLOCK_LOCK ((void *)1) |
| 2541 | #define DO_XXLOCK_ONEBLOCK_UNLOCK ((void *)2) |
| 2542 | #define DO_XXLOCK_ONEBLOCK_GETLOCK ((void *)3) |
| 2543 | |
| 2544 | static int __maybe_unused do_ppb_xxlock(struct map_info *map, |
| 2545 | struct flchip *chip, |
| 2546 | unsigned long adr, int len, void *thunk) |
| 2547 | { |
| 2548 | struct cfi_private *cfi = map->fldrv_priv; |
| 2549 | unsigned long timeo; |
| 2550 | int ret; |
| 2551 | |
| 2552 | mutex_lock(&chip->mutex); |
| 2553 | ret = get_chip(map, chip, adr + chip->start, FL_LOCKING); |
| 2554 | if (ret) { |
| 2555 | mutex_unlock(&chip->mutex); |
| 2556 | return ret; |
| 2557 | } |
| 2558 | |
| 2559 | pr_debug("MTD %s(): XXLOCK 0x%08lx len %d\n", __func__, adr, len); |
| 2560 | |
| 2561 | cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, |
| 2562 | cfi->device_type, NULL); |
| 2563 | cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, |
| 2564 | cfi->device_type, NULL); |
| 2565 | /* PPB entry command */ |
| 2566 | cfi_send_gen_cmd(0xC0, cfi->addr_unlock1, chip->start, map, cfi, |
| 2567 | cfi->device_type, NULL); |
| 2568 | |
| 2569 | if (thunk == DO_XXLOCK_ONEBLOCK_LOCK) { |
| 2570 | chip->state = FL_LOCKING; |
| 2571 | map_write(map, CMD(0xA0), chip->start + adr); |
| 2572 | map_write(map, CMD(0x00), chip->start + adr); |
| 2573 | } else if (thunk == DO_XXLOCK_ONEBLOCK_UNLOCK) { |
| 2574 | /* |
| 2575 | * Unlocking of one specific sector is not supported, so we |
| 2576 | * have to unlock all sectors of this device instead |
| 2577 | */ |
| 2578 | chip->state = FL_UNLOCKING; |
| 2579 | map_write(map, CMD(0x80), chip->start); |
| 2580 | map_write(map, CMD(0x30), chip->start); |
| 2581 | } else if (thunk == DO_XXLOCK_ONEBLOCK_GETLOCK) { |
| 2582 | chip->state = FL_JEDEC_QUERY; |
| 2583 | /* Return locked status: 0->locked, 1->unlocked */ |
| 2584 | ret = !cfi_read_query(map, adr); |
| 2585 | } else |
| 2586 | BUG(); |
| 2587 | |
| 2588 | /* |
| 2589 | * Wait for some time as unlocking of all sectors takes quite long |
| 2590 | */ |
| 2591 | timeo = jiffies + msecs_to_jiffies(2000); /* 2s max (un)locking */ |
| 2592 | for (;;) { |
| 2593 | if (chip_ready(map, adr)) |
| 2594 | break; |
| 2595 | |
| 2596 | if (time_after(jiffies, timeo)) { |
| 2597 | printk(KERN_ERR "Waiting for chip to be ready timed out.\n"); |
| 2598 | ret = -EIO; |
| 2599 | break; |
| 2600 | } |
| 2601 | |
| 2602 | UDELAY(map, chip, adr, 1); |
| 2603 | } |
| 2604 | |
| 2605 | /* Exit BC commands */ |
| 2606 | map_write(map, CMD(0x90), chip->start); |
| 2607 | map_write(map, CMD(0x00), chip->start); |
| 2608 | |
| 2609 | chip->state = FL_READY; |
| 2610 | put_chip(map, chip, adr + chip->start); |
| 2611 | mutex_unlock(&chip->mutex); |
| 2612 | |
| 2613 | return ret; |
| 2614 | } |
| 2615 | |
| 2616 | static int __maybe_unused cfi_ppb_lock(struct mtd_info *mtd, loff_t ofs, |
| 2617 | uint64_t len) |
| 2618 | { |
| 2619 | return cfi_varsize_frob(mtd, do_ppb_xxlock, ofs, len, |
| 2620 | DO_XXLOCK_ONEBLOCK_LOCK); |
| 2621 | } |
| 2622 | |
| 2623 | static int __maybe_unused cfi_ppb_unlock(struct mtd_info *mtd, loff_t ofs, |
| 2624 | uint64_t len) |
| 2625 | { |
| 2626 | struct mtd_erase_region_info *regions = mtd->eraseregions; |
| 2627 | struct map_info *map = mtd->priv; |
| 2628 | struct cfi_private *cfi = map->fldrv_priv; |
| 2629 | struct ppb_lock *sect; |
| 2630 | unsigned long adr; |
| 2631 | loff_t offset; |
| 2632 | uint64_t length; |
| 2633 | int chipnum; |
| 2634 | int i; |
| 2635 | int sectors; |
| 2636 | int ret; |
| 2637 | |
| 2638 | /* |
| 2639 | * PPB unlocking always unlocks all sectors of the flash chip. |
| 2640 | * We need to re-lock all previously locked sectors. So lets |
| 2641 | * first check the locking status of all sectors and save |
| 2642 | * it for future use. |
| 2643 | */ |
| 2644 | sect = kzalloc(MAX_SECTORS * sizeof(struct ppb_lock), GFP_KERNEL); |
| 2645 | if (!sect) |
| 2646 | return -ENOMEM; |
| 2647 | |
| 2648 | /* |
| 2649 | * This code to walk all sectors is a slightly modified version |
| 2650 | * of the cfi_varsize_frob() code. |
| 2651 | */ |
| 2652 | i = 0; |
| 2653 | chipnum = 0; |
| 2654 | adr = 0; |
| 2655 | sectors = 0; |
| 2656 | offset = 0; |
| 2657 | length = mtd->size; |
| 2658 | |
| 2659 | while (length) { |
| 2660 | int size = regions[i].erasesize; |
| 2661 | |
| 2662 | /* |
| 2663 | * Only test sectors that shall not be unlocked. The other |
| 2664 | * sectors shall be unlocked, so lets keep their locking |
| 2665 | * status at "unlocked" (locked=0) for the final re-locking. |
| 2666 | */ |
| 2667 | if ((adr < ofs) || (adr >= (ofs + len))) { |
| 2668 | sect[sectors].chip = &cfi->chips[chipnum]; |
| 2669 | sect[sectors].offset = offset; |
| 2670 | sect[sectors].locked = do_ppb_xxlock( |
| 2671 | map, &cfi->chips[chipnum], adr, 0, |
| 2672 | DO_XXLOCK_ONEBLOCK_GETLOCK); |
| 2673 | } |
| 2674 | |
| 2675 | adr += size; |
| 2676 | offset += size; |
| 2677 | length -= size; |
| 2678 | |
| 2679 | if (offset == regions[i].offset + size * regions[i].numblocks) |
| 2680 | i++; |
| 2681 | |
| 2682 | if (adr >> cfi->chipshift) { |
| 2683 | adr = 0; |
| 2684 | chipnum++; |
| 2685 | |
| 2686 | if (chipnum >= cfi->numchips) |
| 2687 | break; |
| 2688 | } |
| 2689 | |
| 2690 | sectors++; |
| 2691 | if (sectors >= MAX_SECTORS) { |
| 2692 | printk(KERN_ERR "Only %d sectors for PPB locking supported!\n", |
| 2693 | MAX_SECTORS); |
| 2694 | kfree(sect); |
| 2695 | return -EINVAL; |
| 2696 | } |
| 2697 | } |
| 2698 | |
| 2699 | /* Now unlock the whole chip */ |
| 2700 | ret = cfi_varsize_frob(mtd, do_ppb_xxlock, ofs, len, |
| 2701 | DO_XXLOCK_ONEBLOCK_UNLOCK); |
| 2702 | if (ret) { |
| 2703 | kfree(sect); |
| 2704 | return ret; |
| 2705 | } |
| 2706 | |
| 2707 | /* |
| 2708 | * PPB unlocking always unlocks all sectors of the flash chip. |
| 2709 | * We need to re-lock all previously locked sectors. |
| 2710 | */ |
| 2711 | for (i = 0; i < sectors; i++) { |
| 2712 | if (sect[i].locked) |
| 2713 | do_ppb_xxlock(map, sect[i].chip, sect[i].offset, 0, |
| 2714 | DO_XXLOCK_ONEBLOCK_LOCK); |
| 2715 | } |
| 2716 | |
| 2717 | kfree(sect); |
| 2718 | return ret; |
| 2719 | } |
| 2720 | |
| 2721 | static int __maybe_unused cfi_ppb_is_locked(struct mtd_info *mtd, loff_t ofs, |
| 2722 | uint64_t len) |
| 2723 | { |
| 2724 | return cfi_varsize_frob(mtd, do_ppb_xxlock, ofs, len, |
| 2725 | DO_XXLOCK_ONEBLOCK_GETLOCK) ? 1 : 0; |
| 2726 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2727 | |
| 2728 | static void cfi_amdstd_sync (struct mtd_info *mtd) |
| 2729 | { |
| 2730 | struct map_info *map = mtd->priv; |
| 2731 | struct cfi_private *cfi = map->fldrv_priv; |
| 2732 | int i; |
| 2733 | struct flchip *chip; |
| 2734 | int ret = 0; |
| 2735 | DECLARE_WAITQUEUE(wait, current); |
| 2736 | |
| 2737 | for (i=0; !ret && i<cfi->numchips; i++) { |
| 2738 | chip = &cfi->chips[i]; |
| 2739 | |
| 2740 | retry: |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 2741 | mutex_lock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2742 | |
| 2743 | switch(chip->state) { |
| 2744 | case FL_READY: |
| 2745 | case FL_STATUS: |
| 2746 | case FL_CFI_QUERY: |
| 2747 | case FL_JEDEC_QUERY: |
| 2748 | chip->oldstate = chip->state; |
| 2749 | chip->state = FL_SYNCING; |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 2750 | /* No need to wake_up() on this state change - |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2751 | * as the whole point is that nobody can do anything |
| 2752 | * with the chip now anyway. |
| 2753 | */ |
| 2754 | case FL_SYNCING: |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 2755 | mutex_unlock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2756 | break; |
| 2757 | |
| 2758 | default: |
| 2759 | /* Not an idle state */ |
Dmitry Adamushko | f8e30e4 | 2008-04-08 17:41:59 -0700 | [diff] [blame] | 2760 | set_current_state(TASK_UNINTERRUPTIBLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2761 | add_wait_queue(&chip->wq, &wait); |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 2762 | |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 2763 | mutex_unlock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2764 | |
| 2765 | schedule(); |
| 2766 | |
| 2767 | remove_wait_queue(&chip->wq, &wait); |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 2768 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2769 | goto retry; |
| 2770 | } |
| 2771 | } |
| 2772 | |
| 2773 | /* Unlock the chips again */ |
| 2774 | |
| 2775 | for (i--; i >=0; i--) { |
| 2776 | chip = &cfi->chips[i]; |
| 2777 | |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 2778 | mutex_lock(&chip->mutex); |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 2779 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2780 | if (chip->state == FL_SYNCING) { |
| 2781 | chip->state = chip->oldstate; |
| 2782 | wake_up(&chip->wq); |
| 2783 | } |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 2784 | mutex_unlock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2785 | } |
| 2786 | } |
| 2787 | |
| 2788 | |
| 2789 | static int cfi_amdstd_suspend(struct mtd_info *mtd) |
| 2790 | { |
| 2791 | struct map_info *map = mtd->priv; |
| 2792 | struct cfi_private *cfi = map->fldrv_priv; |
| 2793 | int i; |
| 2794 | struct flchip *chip; |
| 2795 | int ret = 0; |
| 2796 | |
| 2797 | for (i=0; !ret && i<cfi->numchips; i++) { |
| 2798 | chip = &cfi->chips[i]; |
| 2799 | |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 2800 | mutex_lock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2801 | |
| 2802 | switch(chip->state) { |
| 2803 | case FL_READY: |
| 2804 | case FL_STATUS: |
| 2805 | case FL_CFI_QUERY: |
| 2806 | case FL_JEDEC_QUERY: |
| 2807 | chip->oldstate = chip->state; |
| 2808 | chip->state = FL_PM_SUSPENDED; |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 2809 | /* No need to wake_up() on this state change - |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2810 | * as the whole point is that nobody can do anything |
| 2811 | * with the chip now anyway. |
| 2812 | */ |
| 2813 | case FL_PM_SUSPENDED: |
| 2814 | break; |
| 2815 | |
| 2816 | default: |
| 2817 | ret = -EAGAIN; |
| 2818 | break; |
| 2819 | } |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 2820 | mutex_unlock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2821 | } |
| 2822 | |
| 2823 | /* Unlock the chips again */ |
| 2824 | |
| 2825 | if (ret) { |
| 2826 | for (i--; i >=0; i--) { |
| 2827 | chip = &cfi->chips[i]; |
| 2828 | |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 2829 | mutex_lock(&chip->mutex); |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 2830 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2831 | if (chip->state == FL_PM_SUSPENDED) { |
| 2832 | chip->state = chip->oldstate; |
| 2833 | wake_up(&chip->wq); |
| 2834 | } |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 2835 | mutex_unlock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2836 | } |
| 2837 | } |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 2838 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2839 | return ret; |
| 2840 | } |
| 2841 | |
| 2842 | |
| 2843 | static void cfi_amdstd_resume(struct mtd_info *mtd) |
| 2844 | { |
| 2845 | struct map_info *map = mtd->priv; |
| 2846 | struct cfi_private *cfi = map->fldrv_priv; |
| 2847 | int i; |
| 2848 | struct flchip *chip; |
| 2849 | |
| 2850 | for (i=0; i<cfi->numchips; i++) { |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 2851 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2852 | chip = &cfi->chips[i]; |
| 2853 | |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 2854 | mutex_lock(&chip->mutex); |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 2855 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2856 | if (chip->state == FL_PM_SUSPENDED) { |
| 2857 | chip->state = FL_READY; |
| 2858 | map_write(map, CMD(0xF0), chip->start); |
| 2859 | wake_up(&chip->wq); |
| 2860 | } |
| 2861 | else |
| 2862 | printk(KERN_ERR "Argh. Chip not in PM_SUSPENDED state upon resume()\n"); |
| 2863 | |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 2864 | mutex_unlock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2865 | } |
| 2866 | } |
| 2867 | |
Kevin Cernekee | eafe131 | 2010-04-29 10:26:56 -0700 | [diff] [blame] | 2868 | |
| 2869 | /* |
| 2870 | * Ensure that the flash device is put back into read array mode before |
| 2871 | * unloading the driver or rebooting. On some systems, rebooting while |
| 2872 | * the flash is in query/program/erase mode will prevent the CPU from |
| 2873 | * fetching the bootloader code, requiring a hard reset or power cycle. |
| 2874 | */ |
| 2875 | static int cfi_amdstd_reset(struct mtd_info *mtd) |
| 2876 | { |
| 2877 | struct map_info *map = mtd->priv; |
| 2878 | struct cfi_private *cfi = map->fldrv_priv; |
| 2879 | int i, ret; |
| 2880 | struct flchip *chip; |
| 2881 | |
| 2882 | for (i = 0; i < cfi->numchips; i++) { |
| 2883 | |
| 2884 | chip = &cfi->chips[i]; |
| 2885 | |
| 2886 | mutex_lock(&chip->mutex); |
| 2887 | |
| 2888 | ret = get_chip(map, chip, chip->start, FL_SHUTDOWN); |
| 2889 | if (!ret) { |
| 2890 | map_write(map, CMD(0xF0), chip->start); |
| 2891 | chip->state = FL_SHUTDOWN; |
| 2892 | put_chip(map, chip, chip->start); |
| 2893 | } |
| 2894 | |
| 2895 | mutex_unlock(&chip->mutex); |
| 2896 | } |
| 2897 | |
| 2898 | return 0; |
| 2899 | } |
| 2900 | |
| 2901 | |
| 2902 | static int cfi_amdstd_reboot(struct notifier_block *nb, unsigned long val, |
| 2903 | void *v) |
| 2904 | { |
| 2905 | struct mtd_info *mtd; |
| 2906 | |
| 2907 | mtd = container_of(nb, struct mtd_info, reboot_notifier); |
| 2908 | cfi_amdstd_reset(mtd); |
| 2909 | return NOTIFY_DONE; |
| 2910 | } |
| 2911 | |
| 2912 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2913 | static void cfi_amdstd_destroy(struct mtd_info *mtd) |
| 2914 | { |
| 2915 | struct map_info *map = mtd->priv; |
| 2916 | struct cfi_private *cfi = map->fldrv_priv; |
Jesper Juhl | fa67164 | 2005-11-07 01:01:27 -0800 | [diff] [blame] | 2917 | |
Kevin Cernekee | eafe131 | 2010-04-29 10:26:56 -0700 | [diff] [blame] | 2918 | cfi_amdstd_reset(mtd); |
| 2919 | unregister_reboot_notifier(&mtd->reboot_notifier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2920 | kfree(cfi->cmdset_priv); |
| 2921 | kfree(cfi->cfiq); |
| 2922 | kfree(cfi); |
| 2923 | kfree(mtd->eraseregions); |
| 2924 | } |
| 2925 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2926 | MODULE_LICENSE("GPL"); |
| 2927 | MODULE_AUTHOR("Crossnet Co. <info@crossnet.co.jp> et al."); |
| 2928 | MODULE_DESCRIPTION("MTD chip driver for AMD/Fujitsu flash chips"); |
Guillaume LECERF | 8046112 | 2010-05-20 16:54:10 +0200 | [diff] [blame] | 2929 | MODULE_ALIAS("cfi_cmdset_0006"); |
David Woodhouse | 1e804ce | 2010-05-20 16:54:05 +0200 | [diff] [blame] | 2930 | MODULE_ALIAS("cfi_cmdset_0701"); |