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> |
| 27 | #include <linux/init.h> |
| 28 | #include <asm/io.h> |
| 29 | #include <asm/byteorder.h> |
| 30 | |
| 31 | #include <linux/errno.h> |
| 32 | #include <linux/slab.h> |
| 33 | #include <linux/delay.h> |
| 34 | #include <linux/interrupt.h> |
Kevin Cernekee | eafe131 | 2010-04-29 10:26:56 -0700 | [diff] [blame^] | 35 | #include <linux/reboot.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include <linux/mtd/compatmac.h> |
| 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 *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | static int cfi_amdstd_secsi_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *); |
| 62 | |
| 63 | static void cfi_amdstd_destroy(struct mtd_info *); |
| 64 | |
| 65 | struct mtd_info *cfi_cmdset_0002(struct map_info *, int); |
| 66 | static struct mtd_info *cfi_amdstd_setup (struct mtd_info *); |
| 67 | |
| 68 | static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode); |
| 69 | static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr); |
| 70 | #include "fwh_lock.h" |
| 71 | |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 72 | static int cfi_atmel_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len); |
| 73 | 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] | 74 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | static struct mtd_chip_driver cfi_amdstd_chipdrv = { |
| 76 | .probe = NULL, /* Not usable directly */ |
| 77 | .destroy = cfi_amdstd_destroy, |
| 78 | .name = "cfi_cmdset_0002", |
| 79 | .module = THIS_MODULE |
| 80 | }; |
| 81 | |
| 82 | |
| 83 | /* #define DEBUG_CFI_FEATURES */ |
| 84 | |
| 85 | |
| 86 | #ifdef DEBUG_CFI_FEATURES |
| 87 | static void cfi_tell_features(struct cfi_pri_amdstd *extp) |
| 88 | { |
| 89 | const char* erase_suspend[3] = { |
| 90 | "Not supported", "Read only", "Read/write" |
| 91 | }; |
| 92 | const char* top_bottom[6] = { |
| 93 | "No WP", "8x8KiB sectors at top & bottom, no WP", |
| 94 | "Bottom boot", "Top boot", |
| 95 | "Uniform, Bottom WP", "Uniform, Top WP" |
| 96 | }; |
| 97 | |
| 98 | printk(" Silicon revision: %d\n", extp->SiliconRevision >> 1); |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 99 | printk(" Address sensitive unlock: %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | (extp->SiliconRevision & 1) ? "Not required" : "Required"); |
| 101 | |
| 102 | if (extp->EraseSuspend < ARRAY_SIZE(erase_suspend)) |
| 103 | printk(" Erase Suspend: %s\n", erase_suspend[extp->EraseSuspend]); |
| 104 | else |
| 105 | printk(" Erase Suspend: Unknown value %d\n", extp->EraseSuspend); |
| 106 | |
| 107 | if (extp->BlkProt == 0) |
| 108 | printk(" Block protection: Not supported\n"); |
| 109 | else |
| 110 | printk(" Block protection: %d sectors per group\n", extp->BlkProt); |
| 111 | |
| 112 | |
| 113 | printk(" Temporary block unprotect: %s\n", |
| 114 | extp->TmpBlkUnprotect ? "Supported" : "Not supported"); |
| 115 | printk(" Block protect/unprotect scheme: %d\n", extp->BlkProtUnprot); |
| 116 | printk(" Number of simultaneous operations: %d\n", extp->SimultaneousOps); |
| 117 | printk(" Burst mode: %s\n", |
| 118 | extp->BurstMode ? "Supported" : "Not supported"); |
| 119 | if (extp->PageMode == 0) |
| 120 | printk(" Page mode: Not supported\n"); |
| 121 | else |
| 122 | printk(" Page mode: %d word page\n", extp->PageMode << 2); |
| 123 | |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 124 | printk(" Vpp Supply Minimum Program/Erase Voltage: %d.%d V\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | extp->VppMin >> 4, extp->VppMin & 0xf); |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 126 | printk(" Vpp Supply Maximum Program/Erase Voltage: %d.%d V\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | extp->VppMax >> 4, extp->VppMax & 0xf); |
| 128 | |
| 129 | if (extp->TopBottom < ARRAY_SIZE(top_bottom)) |
| 130 | printk(" Top/Bottom Boot Block: %s\n", top_bottom[extp->TopBottom]); |
| 131 | else |
| 132 | printk(" Top/Bottom Boot Block: Unknown value %d\n", extp->TopBottom); |
| 133 | } |
| 134 | #endif |
| 135 | |
| 136 | #ifdef AMD_BOOTLOC_BUG |
| 137 | /* Wheee. Bring me the head of someone at AMD. */ |
| 138 | static void fixup_amd_bootblock(struct mtd_info *mtd, void* param) |
| 139 | { |
| 140 | struct map_info *map = mtd->priv; |
| 141 | struct cfi_private *cfi = map->fldrv_priv; |
| 142 | struct cfi_pri_amdstd *extp = cfi->cmdset_priv; |
| 143 | __u8 major = extp->MajorVersion; |
| 144 | __u8 minor = extp->MinorVersion; |
| 145 | |
| 146 | if (((major << 8) | minor) < 0x3131) { |
| 147 | /* CFI version 1.0 => don't trust bootloc */ |
Christopher Moore | 87e92c0 | 2008-10-17 05:32:22 +0200 | [diff] [blame] | 148 | |
| 149 | DEBUG(MTD_DEBUG_LEVEL1, |
| 150 | "%s: JEDEC Vendor ID is 0x%02X Device ID is 0x%02X\n", |
| 151 | map->name, cfi->mfr, cfi->id); |
| 152 | |
| 153 | /* AFAICS all 29LV400 with a bottom boot block have a device ID |
| 154 | * of 0x22BA in 16-bit mode and 0xBA in 8-bit mode. |
| 155 | * These were badly detected as they have the 0x80 bit set |
| 156 | * so treat them as a special case. |
| 157 | */ |
| 158 | if (((cfi->id == 0xBA) || (cfi->id == 0x22BA)) && |
| 159 | |
| 160 | /* Macronix added CFI to their 2nd generation |
| 161 | * MX29LV400C B/T but AFAICS no other 29LV400 (AMD, |
| 162 | * Fujitsu, Spansion, EON, ESI and older Macronix) |
| 163 | * has CFI. |
| 164 | * |
| 165 | * Therefore also check the manufacturer. |
| 166 | * This reduces the risk of false detection due to |
| 167 | * the 8-bit device ID. |
| 168 | */ |
Guillaume LECERF | f3e69c6 | 2009-12-15 23:01:06 +0100 | [diff] [blame] | 169 | (cfi->mfr == CFI_MFR_MACRONIX)) { |
Christopher Moore | 87e92c0 | 2008-10-17 05:32:22 +0200 | [diff] [blame] | 170 | DEBUG(MTD_DEBUG_LEVEL1, |
| 171 | "%s: Macronix MX29LV400C with bottom boot block" |
| 172 | " detected\n", map->name); |
| 173 | extp->TopBottom = 2; /* bottom boot */ |
| 174 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | if (cfi->id & 0x80) { |
| 176 | printk(KERN_WARNING "%s: JEDEC Device ID is 0x%02X. Assuming broken CFI table.\n", map->name, cfi->id); |
| 177 | extp->TopBottom = 3; /* top boot */ |
| 178 | } else { |
| 179 | extp->TopBottom = 2; /* bottom boot */ |
| 180 | } |
Christopher Moore | 87e92c0 | 2008-10-17 05:32:22 +0200 | [diff] [blame] | 181 | |
| 182 | DEBUG(MTD_DEBUG_LEVEL1, |
| 183 | "%s: AMD CFI PRI V%c.%c has no boot block field;" |
| 184 | " deduced %s from Device ID\n", map->name, major, minor, |
| 185 | extp->TopBottom == 2 ? "bottom" : "top"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | #endif |
| 189 | |
| 190 | static void fixup_use_write_buffers(struct mtd_info *mtd, void *param) |
| 191 | { |
| 192 | struct map_info *map = mtd->priv; |
| 193 | struct cfi_private *cfi = map->fldrv_priv; |
| 194 | if (cfi->cfiq->BufWriteTimeoutTyp) { |
| 195 | DEBUG(MTD_DEBUG_LEVEL1, "Using buffer write method\n" ); |
| 196 | mtd->write = cfi_amdstd_write_buffers; |
| 197 | } |
| 198 | } |
| 199 | |
Haavard Skinnemoen | 5b0c5c2 | 2006-08-09 10:54:44 +0200 | [diff] [blame] | 200 | /* Atmel chips don't use the same PRI format as AMD chips */ |
| 201 | static void fixup_convert_atmel_pri(struct mtd_info *mtd, void *param) |
| 202 | { |
| 203 | struct map_info *map = mtd->priv; |
| 204 | struct cfi_private *cfi = map->fldrv_priv; |
| 205 | struct cfi_pri_amdstd *extp = cfi->cmdset_priv; |
| 206 | struct cfi_pri_atmel atmel_pri; |
| 207 | |
| 208 | memcpy(&atmel_pri, extp, sizeof(atmel_pri)); |
HÃ¥vard Skinnemoen | de591da | 2006-09-15 17:19:31 +0200 | [diff] [blame] | 209 | memset((char *)extp + 5, 0, sizeof(*extp) - 5); |
Haavard Skinnemoen | 5b0c5c2 | 2006-08-09 10:54:44 +0200 | [diff] [blame] | 210 | |
| 211 | if (atmel_pri.Features & 0x02) |
| 212 | extp->EraseSuspend = 2; |
| 213 | |
Haavard Skinnemoen | be8f78b | 2008-09-30 13:55:33 +0200 | [diff] [blame] | 214 | /* Some chips got it backwards... */ |
| 215 | if (cfi->id == AT49BV6416) { |
| 216 | if (atmel_pri.BottomBoot) |
| 217 | extp->TopBottom = 3; |
| 218 | else |
| 219 | extp->TopBottom = 2; |
| 220 | } else { |
| 221 | if (atmel_pri.BottomBoot) |
| 222 | extp->TopBottom = 2; |
| 223 | else |
| 224 | extp->TopBottom = 3; |
| 225 | } |
Hans-Christian Egtvedt | d10a39d | 2007-10-30 16:33:07 +0100 | [diff] [blame] | 226 | |
| 227 | /* burst write mode not supported */ |
| 228 | cfi->cfiq->BufWriteTimeoutTyp = 0; |
| 229 | cfi->cfiq->BufWriteTimeoutMax = 0; |
Haavard Skinnemoen | 5b0c5c2 | 2006-08-09 10:54:44 +0200 | [diff] [blame] | 230 | } |
| 231 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | static void fixup_use_secsi(struct mtd_info *mtd, void *param) |
| 233 | { |
| 234 | /* Setup for chips with a secsi area */ |
| 235 | mtd->read_user_prot_reg = cfi_amdstd_secsi_read; |
| 236 | mtd->read_fact_prot_reg = cfi_amdstd_secsi_read; |
| 237 | } |
| 238 | |
| 239 | static void fixup_use_erase_chip(struct mtd_info *mtd, void *param) |
| 240 | { |
| 241 | struct map_info *map = mtd->priv; |
| 242 | struct cfi_private *cfi = map->fldrv_priv; |
| 243 | if ((cfi->cfiq->NumEraseRegions == 1) && |
| 244 | ((cfi->cfiq->EraseRegionInfo[0] & 0xffff) == 0)) { |
| 245 | mtd->erase = cfi_amdstd_erase_chip; |
| 246 | } |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 247 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Haavard Skinnemoen | 0165508 | 2006-08-09 11:06:07 +0200 | [diff] [blame] | 250 | /* |
| 251 | * Some Atmel chips (e.g. the AT49BV6416) power-up with all sectors |
| 252 | * locked by default. |
| 253 | */ |
| 254 | static void fixup_use_atmel_lock(struct mtd_info *mtd, void *param) |
| 255 | { |
| 256 | mtd->lock = cfi_atmel_lock; |
| 257 | mtd->unlock = cfi_atmel_unlock; |
Justin Treon | e619a75 | 2008-01-30 10:25:49 -0800 | [diff] [blame] | 258 | mtd->flags |= MTD_POWERUP_LOCK; |
Haavard Skinnemoen | 0165508 | 2006-08-09 11:06:07 +0200 | [diff] [blame] | 259 | } |
| 260 | |
Trent Piepho | 70b0725 | 2008-03-30 21:19:30 -0700 | [diff] [blame] | 261 | static void fixup_s29gl064n_sectors(struct mtd_info *mtd, void *param) |
| 262 | { |
| 263 | struct map_info *map = mtd->priv; |
| 264 | struct cfi_private *cfi = map->fldrv_priv; |
| 265 | |
| 266 | if ((cfi->cfiq->EraseRegionInfo[0] & 0xffff) == 0x003f) { |
| 267 | cfi->cfiq->EraseRegionInfo[0] |= 0x0040; |
| 268 | pr_warning("%s: Bad S29GL064N CFI data, adjust from 64 to 128 sectors\n", mtd->name); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | static void fixup_s29gl032n_sectors(struct mtd_info *mtd, void *param) |
| 273 | { |
| 274 | struct map_info *map = mtd->priv; |
| 275 | struct cfi_private *cfi = map->fldrv_priv; |
| 276 | |
| 277 | if ((cfi->cfiq->EraseRegionInfo[1] & 0xffff) == 0x007e) { |
| 278 | cfi->cfiq->EraseRegionInfo[1] &= ~0x0040; |
| 279 | pr_warning("%s: Bad S29GL032N CFI data, adjust from 127 to 63 sectors\n", mtd->name); |
| 280 | } |
| 281 | } |
| 282 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | static struct cfi_fixup cfi_fixup_table[] = { |
Hans-Christian Egtvedt | d10a39d | 2007-10-30 16:33:07 +0100 | [diff] [blame] | 284 | { CFI_MFR_ATMEL, CFI_ID_ANY, fixup_convert_atmel_pri, NULL }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | #ifdef AMD_BOOTLOC_BUG |
| 286 | { CFI_MFR_AMD, CFI_ID_ANY, fixup_amd_bootblock, NULL }, |
Guillaume LECERF | f3e69c6 | 2009-12-15 23:01:06 +0100 | [diff] [blame] | 287 | { CFI_MFR_MACRONIX, CFI_ID_ANY, fixup_amd_bootblock, NULL }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | #endif |
| 289 | { CFI_MFR_AMD, 0x0050, fixup_use_secsi, NULL, }, |
| 290 | { CFI_MFR_AMD, 0x0053, fixup_use_secsi, NULL, }, |
| 291 | { CFI_MFR_AMD, 0x0055, fixup_use_secsi, NULL, }, |
| 292 | { CFI_MFR_AMD, 0x0056, fixup_use_secsi, NULL, }, |
| 293 | { CFI_MFR_AMD, 0x005C, fixup_use_secsi, NULL, }, |
| 294 | { CFI_MFR_AMD, 0x005F, fixup_use_secsi, NULL, }, |
Trent Piepho | 70b0725 | 2008-03-30 21:19:30 -0700 | [diff] [blame] | 295 | { CFI_MFR_AMD, 0x0c01, fixup_s29gl064n_sectors, NULL, }, |
| 296 | { CFI_MFR_AMD, 0x1301, fixup_s29gl064n_sectors, NULL, }, |
| 297 | { CFI_MFR_AMD, 0x1a00, fixup_s29gl032n_sectors, NULL, }, |
| 298 | { CFI_MFR_AMD, 0x1a01, fixup_s29gl032n_sectors, NULL, }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | #if !FORCE_WORD_WRITE |
| 300 | { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_write_buffers, NULL, }, |
| 301 | #endif |
| 302 | { 0, 0, NULL, NULL } |
| 303 | }; |
| 304 | static struct cfi_fixup jedec_fixup_table[] = { |
Guillaume LECERF | f3e69c6 | 2009-12-15 23:01:06 +0100 | [diff] [blame] | 305 | { CFI_MFR_SST, SST49LF004B, fixup_use_fwh_lock, NULL, }, |
| 306 | { CFI_MFR_SST, SST49LF040B, fixup_use_fwh_lock, NULL, }, |
| 307 | { CFI_MFR_SST, SST49LF008A, fixup_use_fwh_lock, NULL, }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | { 0, 0, NULL, NULL } |
| 309 | }; |
| 310 | |
| 311 | static struct cfi_fixup fixup_table[] = { |
| 312 | /* The CFI vendor ids and the JEDEC vendor IDs appear |
| 313 | * to be common. It is like the devices id's are as |
| 314 | * well. This table is to pick all cases where |
| 315 | * we know that is the case. |
| 316 | */ |
| 317 | { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_erase_chip, NULL }, |
Haavard Skinnemoen | 0165508 | 2006-08-09 11:06:07 +0200 | [diff] [blame] | 318 | { CFI_MFR_ATMEL, AT49BV6416, fixup_use_atmel_lock, NULL }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | { 0, 0, NULL, NULL } |
| 320 | }; |
| 321 | |
| 322 | |
Wolfgang Grandegger | fefae48 | 2009-01-08 19:21:27 +0100 | [diff] [blame] | 323 | static void cfi_fixup_major_minor(struct cfi_private *cfi, |
| 324 | struct cfi_pri_amdstd *extp) |
| 325 | { |
| 326 | if (cfi->mfr == CFI_MFR_SAMSUNG && cfi->id == 0x257e && |
| 327 | extp->MajorVersion == '0') |
| 328 | extp->MajorVersion = '1'; |
| 329 | } |
| 330 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | struct mtd_info *cfi_cmdset_0002(struct map_info *map, int primary) |
| 332 | { |
| 333 | struct cfi_private *cfi = map->fldrv_priv; |
| 334 | struct mtd_info *mtd; |
| 335 | int i; |
| 336 | |
Burman Yan | 95b93a0 | 2006-11-15 21:10:29 +0200 | [diff] [blame] | 337 | mtd = kzalloc(sizeof(*mtd), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | if (!mtd) { |
| 339 | printk(KERN_WARNING "Failed to allocate memory for MTD device\n"); |
| 340 | return NULL; |
| 341 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | mtd->priv = map; |
| 343 | mtd->type = MTD_NORFLASH; |
| 344 | |
| 345 | /* Fill in the default mtd operations */ |
| 346 | mtd->erase = cfi_amdstd_erase_varsize; |
| 347 | mtd->write = cfi_amdstd_write_words; |
| 348 | mtd->read = cfi_amdstd_read; |
| 349 | mtd->sync = cfi_amdstd_sync; |
| 350 | mtd->suspend = cfi_amdstd_suspend; |
| 351 | mtd->resume = cfi_amdstd_resume; |
| 352 | mtd->flags = MTD_CAP_NORFLASH; |
| 353 | mtd->name = map->name; |
Artem B. Bityutskiy | 783ed81 | 2006-06-14 19:53:44 +0400 | [diff] [blame] | 354 | mtd->writesize = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | |
Kevin Cernekee | eafe131 | 2010-04-29 10:26:56 -0700 | [diff] [blame^] | 356 | mtd->reboot_notifier.notifier_call = cfi_amdstd_reboot; |
| 357 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | if (cfi->cfi_mode==CFI_MODE_CFI){ |
| 359 | unsigned char bootloc; |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 360 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | * It's a real CFI chip, not one for which the probe |
| 362 | * routine faked a CFI structure. So we read the feature |
| 363 | * table from it. |
| 364 | */ |
| 365 | __u16 adr = primary?cfi->cfiq->P_ADR:cfi->cfiq->A_ADR; |
| 366 | struct cfi_pri_amdstd *extp; |
| 367 | |
| 368 | extp = (struct cfi_pri_amdstd*)cfi_read_pri(map, adr, sizeof(*extp), "Amd/Fujitsu"); |
| 369 | if (!extp) { |
| 370 | kfree(mtd); |
| 371 | return NULL; |
| 372 | } |
| 373 | |
Wolfgang Grandegger | fefae48 | 2009-01-08 19:21:27 +0100 | [diff] [blame] | 374 | cfi_fixup_major_minor(cfi, extp); |
| 375 | |
Todd Poynor | d88f977 | 2005-07-20 22:01:17 +0100 | [diff] [blame] | 376 | if (extp->MajorVersion != '1' || |
| 377 | (extp->MinorVersion < '0' || extp->MinorVersion > '4')) { |
| 378 | printk(KERN_ERR " Unknown Amd/Fujitsu Extended Query " |
| 379 | "version %c.%c.\n", extp->MajorVersion, |
| 380 | extp->MinorVersion); |
| 381 | kfree(extp); |
| 382 | kfree(mtd); |
| 383 | return NULL; |
| 384 | } |
| 385 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | /* Install our own private info structure */ |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 387 | cfi->cmdset_priv = extp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | |
| 389 | /* Apply cfi device specific fixups */ |
| 390 | cfi_fixup(mtd, cfi_fixup_table); |
| 391 | |
| 392 | #ifdef DEBUG_CFI_FEATURES |
| 393 | /* Tell the user about it in lots of lovely detail */ |
| 394 | cfi_tell_features(extp); |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 395 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | |
| 397 | bootloc = extp->TopBottom; |
| 398 | if ((bootloc != 2) && (bootloc != 3)) { |
| 399 | printk(KERN_WARNING "%s: CFI does not contain boot " |
| 400 | "bank location. Assuming top.\n", map->name); |
| 401 | bootloc = 2; |
| 402 | } |
| 403 | |
| 404 | if (bootloc == 3 && cfi->cfiq->NumEraseRegions > 1) { |
| 405 | printk(KERN_WARNING "%s: Swapping erase regions for broken CFI table.\n", map->name); |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 406 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | for (i=0; i<cfi->cfiq->NumEraseRegions / 2; i++) { |
| 408 | int j = (cfi->cfiq->NumEraseRegions-1)-i; |
| 409 | __u32 swap; |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 410 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | swap = cfi->cfiq->EraseRegionInfo[i]; |
| 412 | cfi->cfiq->EraseRegionInfo[i] = cfi->cfiq->EraseRegionInfo[j]; |
| 413 | cfi->cfiq->EraseRegionInfo[j] = swap; |
| 414 | } |
| 415 | } |
| 416 | /* Set the default CFI lock/unlock addresses */ |
| 417 | cfi->addr_unlock1 = 0x555; |
| 418 | cfi->addr_unlock2 = 0x2aa; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | |
| 420 | } /* CFI mode */ |
| 421 | else if (cfi->cfi_mode == CFI_MODE_JEDEC) { |
| 422 | /* Apply jedec specific fixups */ |
| 423 | cfi_fixup(mtd, jedec_fixup_table); |
| 424 | } |
| 425 | /* Apply generic fixups */ |
| 426 | cfi_fixup(mtd, fixup_table); |
| 427 | |
| 428 | for (i=0; i< cfi->numchips; i++) { |
| 429 | cfi->chips[i].word_write_time = 1<<cfi->cfiq->WordWriteTimeoutTyp; |
| 430 | cfi->chips[i].buffer_write_time = 1<<cfi->cfiq->BufWriteTimeoutTyp; |
| 431 | cfi->chips[i].erase_time = 1<<cfi->cfiq->BlockEraseTimeoutTyp; |
Vijay Sampath | 83d4809 | 2007-03-06 02:39:44 -0800 | [diff] [blame] | 432 | cfi->chips[i].ref_point_counter = 0; |
| 433 | init_waitqueue_head(&(cfi->chips[i].wq)); |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 434 | } |
| 435 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | map->fldrv = &cfi_amdstd_chipdrv; |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 437 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | return cfi_amdstd_setup(mtd); |
| 439 | } |
David Woodhouse | 83ea4ef | 2006-05-08 22:58:25 +0100 | [diff] [blame] | 440 | EXPORT_SYMBOL_GPL(cfi_cmdset_0002); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | |
| 442 | static struct mtd_info *cfi_amdstd_setup(struct mtd_info *mtd) |
| 443 | { |
| 444 | struct map_info *map = mtd->priv; |
| 445 | struct cfi_private *cfi = map->fldrv_priv; |
| 446 | unsigned long devsize = (1<<cfi->cfiq->DevSize) * cfi->interleave; |
| 447 | unsigned long offset = 0; |
| 448 | int i,j; |
| 449 | |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 450 | printk(KERN_NOTICE "number of %s chips: %d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | (cfi->cfi_mode == CFI_MODE_CFI)?"CFI":"JEDEC",cfi->numchips); |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 452 | /* Select the correct geometry setup */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | mtd->size = devsize * cfi->numchips; |
| 454 | |
| 455 | mtd->numeraseregions = cfi->cfiq->NumEraseRegions * cfi->numchips; |
| 456 | mtd->eraseregions = kmalloc(sizeof(struct mtd_erase_region_info) |
| 457 | * mtd->numeraseregions, GFP_KERNEL); |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 458 | if (!mtd->eraseregions) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | printk(KERN_WARNING "Failed to allocate memory for MTD erase region info\n"); |
| 460 | goto setup_err; |
| 461 | } |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 462 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | for (i=0; i<cfi->cfiq->NumEraseRegions; i++) { |
| 464 | unsigned long ernum, ersize; |
| 465 | ersize = ((cfi->cfiq->EraseRegionInfo[i] >> 8) & ~0xff) * cfi->interleave; |
| 466 | ernum = (cfi->cfiq->EraseRegionInfo[i] & 0xffff) + 1; |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 467 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | if (mtd->erasesize < ersize) { |
| 469 | mtd->erasesize = ersize; |
| 470 | } |
| 471 | for (j=0; j<cfi->numchips; j++) { |
| 472 | mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].offset = (j*devsize)+offset; |
| 473 | mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].erasesize = ersize; |
| 474 | mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].numblocks = ernum; |
| 475 | } |
| 476 | offset += (ersize * ernum); |
| 477 | } |
| 478 | if (offset != devsize) { |
| 479 | /* Argh */ |
| 480 | printk(KERN_WARNING "Sum of regions (%lx) != total size of set of interleaved chips (%lx)\n", offset, devsize); |
| 481 | goto setup_err; |
| 482 | } |
| 483 | #if 0 |
| 484 | // debug |
| 485 | for (i=0; i<mtd->numeraseregions;i++){ |
| 486 | printk("%d: offset=0x%x,size=0x%x,blocks=%d\n", |
| 487 | i,mtd->eraseregions[i].offset, |
| 488 | mtd->eraseregions[i].erasesize, |
| 489 | mtd->eraseregions[i].numblocks); |
| 490 | } |
| 491 | #endif |
| 492 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | __module_get(THIS_MODULE); |
Kevin Cernekee | eafe131 | 2010-04-29 10:26:56 -0700 | [diff] [blame^] | 494 | register_reboot_notifier(&mtd->reboot_notifier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | return mtd; |
| 496 | |
| 497 | setup_err: |
Jiri Slaby | 17fabf1 | 2010-01-10 10:01:19 +0100 | [diff] [blame] | 498 | kfree(mtd->eraseregions); |
| 499 | kfree(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | kfree(cfi->cmdset_priv); |
| 501 | kfree(cfi->cfiq); |
| 502 | return NULL; |
| 503 | } |
| 504 | |
| 505 | /* |
| 506 | * Return true if the chip is ready. |
| 507 | * |
| 508 | * Ready is one of: read mode, query mode, erase-suspend-read mode (in any |
| 509 | * non-suspended sector) and is indicated by no toggle bits toggling. |
| 510 | * |
| 511 | * Note that anything more complicated than checking if no bits are toggling |
| 512 | * (including checking DQ5 for an error status) is tricky to get working |
| 513 | * correctly and is therefore not done (particulary with interleaved chips |
| 514 | * as each chip must be checked independantly of the others). |
| 515 | */ |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 516 | static int __xipram chip_ready(struct map_info *map, unsigned long addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | { |
| 518 | map_word d, t; |
| 519 | |
| 520 | d = map_read(map, addr); |
| 521 | t = map_read(map, addr); |
| 522 | |
| 523 | return map_word_equal(map, d, t); |
| 524 | } |
| 525 | |
Eric W. Biedermann | fb4a90b | 2005-05-20 04:28:26 +0100 | [diff] [blame] | 526 | /* |
| 527 | * Return true if the chip is ready and has the correct value. |
| 528 | * |
| 529 | * Ready is one of: read mode, query mode, erase-suspend-read mode (in any |
| 530 | * non-suspended sector) and it is indicated by no bits toggling. |
| 531 | * |
| 532 | * Error are indicated by toggling bits or bits held with the wrong value, |
| 533 | * or with bits toggling. |
| 534 | * |
| 535 | * Note that anything more complicated than checking if no bits are toggling |
| 536 | * (including checking DQ5 for an error status) is tricky to get working |
| 537 | * correctly and is therefore not done (particulary with interleaved chips |
| 538 | * as each chip must be checked independantly of the others). |
| 539 | * |
| 540 | */ |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 541 | 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] | 542 | { |
| 543 | map_word oldd, curd; |
| 544 | |
| 545 | oldd = map_read(map, addr); |
| 546 | curd = map_read(map, addr); |
| 547 | |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 548 | return map_word_equal(map, oldd, curd) && |
Eric W. Biedermann | fb4a90b | 2005-05-20 04:28:26 +0100 | [diff] [blame] | 549 | map_word_equal(map, curd, expected); |
| 550 | } |
| 551 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode) |
| 553 | { |
| 554 | DECLARE_WAITQUEUE(wait, current); |
| 555 | struct cfi_private *cfi = map->fldrv_priv; |
| 556 | unsigned long timeo; |
| 557 | struct cfi_pri_amdstd *cfip = (struct cfi_pri_amdstd *)cfi->cmdset_priv; |
| 558 | |
| 559 | resettime: |
| 560 | timeo = jiffies + HZ; |
| 561 | retry: |
| 562 | switch (chip->state) { |
| 563 | |
| 564 | case FL_STATUS: |
| 565 | for (;;) { |
| 566 | if (chip_ready(map, adr)) |
| 567 | break; |
| 568 | |
| 569 | if (time_after(jiffies, timeo)) { |
| 570 | printk(KERN_ERR "Waiting for chip to be ready timed out.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | return -EIO; |
| 572 | } |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 573 | mutex_unlock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | cfi_udelay(1); |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 575 | mutex_lock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | /* Someone else might have been playing with it. */ |
| 577 | goto retry; |
| 578 | } |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 579 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | case FL_READY: |
| 581 | case FL_CFI_QUERY: |
| 582 | case FL_JEDEC_QUERY: |
| 583 | return 0; |
| 584 | |
| 585 | case FL_ERASING: |
Joakim Tjernlund | 2695eab | 2009-11-19 12:01:58 +0100 | [diff] [blame] | 586 | if (!cfip || !(cfip->EraseSuspend & (0x1|0x2)) || |
| 587 | !(mode == FL_READY || mode == FL_POINT || |
| 588 | (mode == FL_WRITING && (cfip->EraseSuspend & 0x2)))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | goto sleep; |
| 590 | |
| 591 | /* We could check to see if we're trying to access the sector |
| 592 | * that is currently being erased. However, no user will try |
| 593 | * anything like that so we just wait for the timeout. */ |
| 594 | |
| 595 | /* Erase suspend */ |
| 596 | /* It's harmless to issue the Erase-Suspend and Erase-Resume |
| 597 | * commands when the erase algorithm isn't in progress. */ |
| 598 | map_write(map, CMD(0xB0), chip->in_progress_block_addr); |
| 599 | chip->oldstate = FL_ERASING; |
| 600 | chip->state = FL_ERASE_SUSPENDING; |
| 601 | chip->erase_suspended = 1; |
| 602 | for (;;) { |
| 603 | if (chip_ready(map, adr)) |
| 604 | break; |
| 605 | |
| 606 | if (time_after(jiffies, timeo)) { |
| 607 | /* Should have suspended the erase by now. |
| 608 | * Send an Erase-Resume command as either |
| 609 | * there was an error (so leave the erase |
| 610 | * routine to recover from it) or we trying to |
| 611 | * use the erase-in-progress sector. */ |
| 612 | map_write(map, CMD(0x30), chip->in_progress_block_addr); |
| 613 | chip->state = FL_ERASING; |
| 614 | chip->oldstate = FL_READY; |
| 615 | printk(KERN_ERR "MTD %s(): chip not ready after erase suspend\n", __func__); |
| 616 | return -EIO; |
| 617 | } |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 618 | |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 619 | mutex_unlock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | cfi_udelay(1); |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 621 | mutex_lock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | /* Nobody will touch it while it's in state FL_ERASE_SUSPENDING. |
| 623 | So we can just loop here. */ |
| 624 | } |
| 625 | chip->state = FL_READY; |
| 626 | return 0; |
| 627 | |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 628 | case FL_XIP_WHILE_ERASING: |
| 629 | if (mode != FL_READY && mode != FL_POINT && |
| 630 | (!cfip || !(cfip->EraseSuspend&2))) |
| 631 | goto sleep; |
| 632 | chip->oldstate = chip->state; |
| 633 | chip->state = FL_READY; |
| 634 | return 0; |
| 635 | |
Kevin Cernekee | eafe131 | 2010-04-29 10:26:56 -0700 | [diff] [blame^] | 636 | case FL_SHUTDOWN: |
| 637 | /* The machine is rebooting */ |
| 638 | return -EIO; |
| 639 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | case FL_POINT: |
| 641 | /* Only if there's no operation suspended... */ |
| 642 | if (mode == FL_READY && chip->oldstate == FL_READY) |
| 643 | return 0; |
| 644 | |
| 645 | default: |
| 646 | sleep: |
| 647 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 648 | add_wait_queue(&chip->wq, &wait); |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 649 | mutex_unlock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | schedule(); |
| 651 | remove_wait_queue(&chip->wq, &wait); |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 652 | mutex_lock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | goto resettime; |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | |
| 658 | static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr) |
| 659 | { |
| 660 | struct cfi_private *cfi = map->fldrv_priv; |
| 661 | |
| 662 | switch(chip->oldstate) { |
| 663 | case FL_ERASING: |
| 664 | chip->state = chip->oldstate; |
| 665 | map_write(map, CMD(0x30), chip->in_progress_block_addr); |
| 666 | chip->oldstate = FL_READY; |
| 667 | chip->state = FL_ERASING; |
| 668 | break; |
| 669 | |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 670 | case FL_XIP_WHILE_ERASING: |
| 671 | chip->state = chip->oldstate; |
| 672 | chip->oldstate = FL_READY; |
| 673 | break; |
| 674 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | case FL_READY: |
| 676 | case FL_STATUS: |
| 677 | /* We should really make set_vpp() count, rather than doing this */ |
| 678 | DISABLE_VPP(map); |
| 679 | break; |
| 680 | default: |
| 681 | printk(KERN_ERR "MTD: put_chip() called with oldstate %d!!\n", chip->oldstate); |
| 682 | } |
| 683 | wake_up(&chip->wq); |
| 684 | } |
| 685 | |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 686 | #ifdef CONFIG_MTD_XIP |
| 687 | |
| 688 | /* |
| 689 | * No interrupt what so ever can be serviced while the flash isn't in array |
| 690 | * mode. This is ensured by the xip_disable() and xip_enable() functions |
| 691 | * enclosing any code path where the flash is known not to be in array mode. |
| 692 | * And within a XIP disabled code path, only functions marked with __xipram |
| 693 | * may be called and nothing else (it's a good thing to inspect generated |
| 694 | * assembly to make sure inline functions were actually inlined and that gcc |
| 695 | * didn't emit calls to its own support functions). Also configuring MTD CFI |
| 696 | * support to a single buswidth and a single interleave is also recommended. |
| 697 | */ |
Thomas Gleixner | f8eb321 | 2005-07-05 01:03:06 +0200 | [diff] [blame] | 698 | |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 699 | static void xip_disable(struct map_info *map, struct flchip *chip, |
| 700 | unsigned long adr) |
| 701 | { |
| 702 | /* TODO: chips with no XIP use should ignore and return */ |
| 703 | (void) map_read(map, adr); /* ensure mmu mapping is up to date */ |
| 704 | local_irq_disable(); |
| 705 | } |
| 706 | |
| 707 | static void __xipram xip_enable(struct map_info *map, struct flchip *chip, |
| 708 | unsigned long adr) |
| 709 | { |
| 710 | struct cfi_private *cfi = map->fldrv_priv; |
| 711 | |
| 712 | if (chip->state != FL_POINT && chip->state != FL_READY) { |
| 713 | map_write(map, CMD(0xf0), adr); |
| 714 | chip->state = FL_READY; |
| 715 | } |
| 716 | (void) map_read(map, adr); |
Thomas Gleixner | 97f927a | 2005-07-07 16:50:16 +0200 | [diff] [blame] | 717 | xip_iprefetch(); |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 718 | local_irq_enable(); |
| 719 | } |
| 720 | |
| 721 | /* |
| 722 | * When a delay is required for the flash operation to complete, the |
| 723 | * xip_udelay() function is polling for both the given timeout and pending |
| 724 | * (but still masked) hardware interrupts. Whenever there is an interrupt |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 725 | * pending then the flash erase operation is suspended, array mode restored |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 726 | * and interrupts unmasked. Task scheduling might also happen at that |
| 727 | * point. The CPU eventually returns from the interrupt or the call to |
| 728 | * schedule() and the suspended flash operation is resumed for the remaining |
| 729 | * of the delay period. |
| 730 | * |
| 731 | * Warning: this function _will_ fool interrupt latency tracing tools. |
| 732 | */ |
| 733 | |
| 734 | static void __xipram xip_udelay(struct map_info *map, struct flchip *chip, |
| 735 | unsigned long adr, int usec) |
| 736 | { |
| 737 | struct cfi_private *cfi = map->fldrv_priv; |
| 738 | struct cfi_pri_amdstd *extp = cfi->cmdset_priv; |
| 739 | map_word status, OK = CMD(0x80); |
| 740 | unsigned long suspended, start = xip_currtime(); |
| 741 | flstate_t oldstate; |
| 742 | |
| 743 | do { |
| 744 | cpu_relax(); |
| 745 | if (xip_irqpending() && extp && |
| 746 | ((chip->state == FL_ERASING && (extp->EraseSuspend & 2))) && |
| 747 | (cfi_interleave_is_1(cfi) || chip->oldstate == FL_READY)) { |
| 748 | /* |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 749 | * Let's suspend the erase operation when supported. |
| 750 | * Note that we currently don't try to suspend |
| 751 | * interleaved chips if there is already another |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 752 | * operation suspended (imagine what happens |
| 753 | * when one chip was already done with the current |
| 754 | * operation while another chip suspended it, then |
| 755 | * we resume the whole thing at once). Yes, it |
| 756 | * can happen! |
| 757 | */ |
| 758 | map_write(map, CMD(0xb0), adr); |
| 759 | usec -= xip_elapsed_since(start); |
| 760 | suspended = xip_currtime(); |
| 761 | do { |
| 762 | if (xip_elapsed_since(suspended) > 100000) { |
| 763 | /* |
| 764 | * The chip doesn't want to suspend |
| 765 | * after waiting for 100 msecs. |
| 766 | * This is a critical error but there |
| 767 | * is not much we can do here. |
| 768 | */ |
| 769 | return; |
| 770 | } |
| 771 | status = map_read(map, adr); |
| 772 | } while (!map_word_andequal(map, status, OK, OK)); |
| 773 | |
| 774 | /* Suspend succeeded */ |
| 775 | oldstate = chip->state; |
| 776 | if (!map_word_bitsset(map, status, CMD(0x40))) |
| 777 | break; |
| 778 | chip->state = FL_XIP_WHILE_ERASING; |
| 779 | chip->erase_suspended = 1; |
| 780 | map_write(map, CMD(0xf0), adr); |
| 781 | (void) map_read(map, adr); |
Paulius Zaleckas | ca5c23c | 2008-02-27 01:42:39 +0200 | [diff] [blame] | 782 | xip_iprefetch(); |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 783 | local_irq_enable(); |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 784 | mutex_unlock(&chip->mutex); |
Paulius Zaleckas | ca5c23c | 2008-02-27 01:42:39 +0200 | [diff] [blame] | 785 | xip_iprefetch(); |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 786 | cond_resched(); |
| 787 | |
| 788 | /* |
| 789 | * We're back. However someone else might have |
| 790 | * decided to go write to the chip if we are in |
| 791 | * a suspended erase state. If so let's wait |
| 792 | * until it's done. |
| 793 | */ |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 794 | mutex_lock(&chip->mutex); |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 795 | while (chip->state != FL_XIP_WHILE_ERASING) { |
| 796 | DECLARE_WAITQUEUE(wait, current); |
| 797 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 798 | add_wait_queue(&chip->wq, &wait); |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 799 | mutex_unlock(&chip->mutex); |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 800 | schedule(); |
| 801 | remove_wait_queue(&chip->wq, &wait); |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 802 | mutex_lock(&chip->mutex); |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 803 | } |
| 804 | /* Disallow XIP again */ |
| 805 | local_irq_disable(); |
| 806 | |
| 807 | /* Resume the write or erase operation */ |
| 808 | map_write(map, CMD(0x30), adr); |
| 809 | chip->state = oldstate; |
| 810 | start = xip_currtime(); |
| 811 | } else if (usec >= 1000000/HZ) { |
| 812 | /* |
| 813 | * Try to save on CPU power when waiting delay |
| 814 | * is at least a system timer tick period. |
| 815 | * No need to be extremely accurate here. |
| 816 | */ |
| 817 | xip_cpu_idle(); |
| 818 | } |
| 819 | status = map_read(map, adr); |
| 820 | } while (!map_word_andequal(map, status, OK, OK) |
| 821 | && xip_elapsed_since(start) < usec); |
| 822 | } |
| 823 | |
| 824 | #define UDELAY(map, chip, adr, usec) xip_udelay(map, chip, adr, usec) |
| 825 | |
| 826 | /* |
| 827 | * The INVALIDATE_CACHED_RANGE() macro is normally used in parallel while |
| 828 | * the flash is actively programming or erasing since we have to poll for |
| 829 | * the operation to complete anyway. We can't do that in a generic way with |
| 830 | * a XIP setup so do it before the actual flash operation in this case |
| 831 | * and stub it out from INVALIDATE_CACHE_UDELAY. |
| 832 | */ |
| 833 | #define XIP_INVAL_CACHED_RANGE(map, from, size) \ |
| 834 | INVALIDATE_CACHED_RANGE(map, from, size) |
| 835 | |
| 836 | #define INVALIDATE_CACHE_UDELAY(map, chip, adr, len, usec) \ |
| 837 | UDELAY(map, chip, adr, usec) |
| 838 | |
| 839 | /* |
| 840 | * Extra notes: |
| 841 | * |
| 842 | * Activating this XIP support changes the way the code works a bit. For |
| 843 | * example the code to suspend the current process when concurrent access |
| 844 | * happens is never executed because xip_udelay() will always return with the |
| 845 | * same chip state as it was entered with. This is why there is no care for |
| 846 | * the presence of add_wait_queue() or schedule() calls from within a couple |
| 847 | * xip_disable()'d areas of code, like in do_erase_oneblock for example. |
| 848 | * The queueing and scheduling are always happening within xip_udelay(). |
| 849 | * |
| 850 | * Similarly, get_chip() and put_chip() just happen to always be executed |
| 851 | * with chip->state set to FL_READY (or FL_XIP_WHILE_*) where flash state |
| 852 | * is in array mode, therefore never executing many cases therein and not |
| 853 | * causing any problem with XIP. |
| 854 | */ |
| 855 | |
| 856 | #else |
| 857 | |
| 858 | #define xip_disable(map, chip, adr) |
| 859 | #define xip_enable(map, chip, adr) |
| 860 | #define XIP_INVAL_CACHED_RANGE(x...) |
| 861 | |
| 862 | #define UDELAY(map, chip, adr, usec) \ |
| 863 | do { \ |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 864 | mutex_unlock(&chip->mutex); \ |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 865 | cfi_udelay(usec); \ |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 866 | mutex_lock(&chip->mutex); \ |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 867 | } while (0) |
| 868 | |
| 869 | #define INVALIDATE_CACHE_UDELAY(map, chip, adr, len, usec) \ |
| 870 | do { \ |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 871 | mutex_unlock(&chip->mutex); \ |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 872 | INVALIDATE_CACHED_RANGE(map, adr, len); \ |
| 873 | cfi_udelay(usec); \ |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 874 | mutex_lock(&chip->mutex); \ |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 875 | } while (0) |
| 876 | |
| 877 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | |
| 879 | static inline int do_read_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf) |
| 880 | { |
| 881 | unsigned long cmd_addr; |
| 882 | struct cfi_private *cfi = map->fldrv_priv; |
| 883 | int ret; |
| 884 | |
| 885 | adr += chip->start; |
| 886 | |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 887 | /* Ensure cmd read/writes are aligned. */ |
| 888 | cmd_addr = adr & ~(map_bankwidth(map)-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 890 | mutex_lock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | ret = get_chip(map, chip, cmd_addr, FL_READY); |
| 892 | if (ret) { |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 893 | mutex_unlock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | return ret; |
| 895 | } |
| 896 | |
| 897 | if (chip->state != FL_POINT && chip->state != FL_READY) { |
| 898 | map_write(map, CMD(0xf0), cmd_addr); |
| 899 | chip->state = FL_READY; |
| 900 | } |
| 901 | |
| 902 | map_copy_from(map, buf, adr, len); |
| 903 | |
| 904 | put_chip(map, chip, cmd_addr); |
| 905 | |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 906 | mutex_unlock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | return 0; |
| 908 | } |
| 909 | |
| 910 | |
| 911 | static int cfi_amdstd_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf) |
| 912 | { |
| 913 | struct map_info *map = mtd->priv; |
| 914 | struct cfi_private *cfi = map->fldrv_priv; |
| 915 | unsigned long ofs; |
| 916 | int chipnum; |
| 917 | int ret = 0; |
| 918 | |
| 919 | /* ofs: offset within the first chip that the first read should start */ |
| 920 | |
| 921 | chipnum = (from >> cfi->chipshift); |
| 922 | ofs = from - (chipnum << cfi->chipshift); |
| 923 | |
| 924 | |
| 925 | *retlen = 0; |
| 926 | |
| 927 | while (len) { |
| 928 | unsigned long thislen; |
| 929 | |
| 930 | if (chipnum >= cfi->numchips) |
| 931 | break; |
| 932 | |
| 933 | if ((len + ofs -1) >> cfi->chipshift) |
| 934 | thislen = (1<<cfi->chipshift) - ofs; |
| 935 | else |
| 936 | thislen = len; |
| 937 | |
| 938 | ret = do_read_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf); |
| 939 | if (ret) |
| 940 | break; |
| 941 | |
| 942 | *retlen += thislen; |
| 943 | len -= thislen; |
| 944 | buf += thislen; |
| 945 | |
| 946 | ofs = 0; |
| 947 | chipnum++; |
| 948 | } |
| 949 | return ret; |
| 950 | } |
| 951 | |
| 952 | |
| 953 | static inline int do_read_secsi_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf) |
| 954 | { |
| 955 | DECLARE_WAITQUEUE(wait, current); |
| 956 | unsigned long timeo = jiffies + HZ; |
| 957 | struct cfi_private *cfi = map->fldrv_priv; |
| 958 | |
| 959 | retry: |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 960 | mutex_lock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | |
| 962 | if (chip->state != FL_READY){ |
| 963 | #if 0 |
| 964 | printk(KERN_DEBUG "Waiting for chip to read, status = %d\n", chip->state); |
| 965 | #endif |
| 966 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 967 | add_wait_queue(&chip->wq, &wait); |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 968 | |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 969 | mutex_unlock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | |
| 971 | schedule(); |
| 972 | remove_wait_queue(&chip->wq, &wait); |
| 973 | #if 0 |
| 974 | if(signal_pending(current)) |
| 975 | return -EINTR; |
| 976 | #endif |
| 977 | timeo = jiffies + HZ; |
| 978 | |
| 979 | goto retry; |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 980 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | |
| 982 | adr += chip->start; |
| 983 | |
| 984 | chip->state = FL_READY; |
| 985 | |
| 986 | cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL); |
| 987 | cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL); |
| 988 | cfi_send_gen_cmd(0x88, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL); |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 989 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | map_copy_from(map, buf, adr, len); |
| 991 | |
| 992 | cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL); |
| 993 | cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL); |
| 994 | cfi_send_gen_cmd(0x90, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL); |
| 995 | cfi_send_gen_cmd(0x00, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL); |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 996 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | wake_up(&chip->wq); |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 998 | mutex_unlock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | |
| 1000 | return 0; |
| 1001 | } |
| 1002 | |
| 1003 | static int cfi_amdstd_secsi_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf) |
| 1004 | { |
| 1005 | struct map_info *map = mtd->priv; |
| 1006 | struct cfi_private *cfi = map->fldrv_priv; |
| 1007 | unsigned long ofs; |
| 1008 | int chipnum; |
| 1009 | int ret = 0; |
| 1010 | |
| 1011 | |
| 1012 | /* ofs: offset within the first chip that the first read should start */ |
| 1013 | |
| 1014 | /* 8 secsi bytes per chip */ |
| 1015 | chipnum=from>>3; |
| 1016 | ofs=from & 7; |
| 1017 | |
| 1018 | |
| 1019 | *retlen = 0; |
| 1020 | |
| 1021 | while (len) { |
| 1022 | unsigned long thislen; |
| 1023 | |
| 1024 | if (chipnum >= cfi->numchips) |
| 1025 | break; |
| 1026 | |
| 1027 | if ((len + ofs -1) >> 3) |
| 1028 | thislen = (1<<3) - ofs; |
| 1029 | else |
| 1030 | thislen = len; |
| 1031 | |
| 1032 | ret = do_read_secsi_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf); |
| 1033 | if (ret) |
| 1034 | break; |
| 1035 | |
| 1036 | *retlen += thislen; |
| 1037 | len -= thislen; |
| 1038 | buf += thislen; |
| 1039 | |
| 1040 | ofs = 0; |
| 1041 | chipnum++; |
| 1042 | } |
| 1043 | return ret; |
| 1044 | } |
| 1045 | |
| 1046 | |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 1047 | static int __xipram do_write_oneword(struct map_info *map, struct flchip *chip, unsigned long adr, map_word datum) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | { |
| 1049 | struct cfi_private *cfi = map->fldrv_priv; |
| 1050 | unsigned long timeo = jiffies + HZ; |
| 1051 | /* |
| 1052 | * We use a 1ms + 1 jiffies generic timeout for writes (most devices |
| 1053 | * have a max write time of a few hundreds usec). However, we should |
| 1054 | * use the maximum timeout value given by the chip at probe time |
| 1055 | * instead. Unfortunately, struct flchip does have a field for |
| 1056 | * maximum timeout, only for typical which can be far too short |
| 1057 | * depending of the conditions. The ' + 1' is to avoid having a |
| 1058 | * timeout of 0 jiffies if HZ is smaller than 1000. |
| 1059 | */ |
| 1060 | unsigned long uWriteTimeout = ( HZ / 1000 ) + 1; |
| 1061 | int ret = 0; |
| 1062 | map_word oldd; |
| 1063 | int retry_cnt = 0; |
| 1064 | |
| 1065 | adr += chip->start; |
| 1066 | |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1067 | mutex_lock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | ret = get_chip(map, chip, adr, FL_WRITING); |
| 1069 | if (ret) { |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1070 | mutex_unlock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1071 | return ret; |
| 1072 | } |
| 1073 | |
| 1074 | DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): WRITE 0x%.8lx(0x%.8lx)\n", |
| 1075 | __func__, adr, datum.x[0] ); |
| 1076 | |
| 1077 | /* |
| 1078 | * Check for a NOP for the case when the datum to write is already |
| 1079 | * present - it saves time and works around buggy chips that corrupt |
| 1080 | * data at other locations when 0xff is written to a location that |
| 1081 | * already contains 0xff. |
| 1082 | */ |
| 1083 | oldd = map_read(map, adr); |
| 1084 | if (map_word_equal(map, oldd, datum)) { |
| 1085 | DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): NOP\n", |
| 1086 | __func__); |
| 1087 | goto op_done; |
| 1088 | } |
| 1089 | |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 1090 | XIP_INVAL_CACHED_RANGE(map, adr, map_bankwidth(map)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | ENABLE_VPP(map); |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 1092 | xip_disable(map, chip, adr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | retry: |
| 1094 | cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL); |
| 1095 | cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL); |
| 1096 | cfi_send_gen_cmd(0xA0, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL); |
| 1097 | map_write(map, datum, adr); |
| 1098 | chip->state = FL_WRITING; |
| 1099 | |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 1100 | INVALIDATE_CACHE_UDELAY(map, chip, |
| 1101 | adr, map_bankwidth(map), |
| 1102 | chip->word_write_time); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | |
| 1104 | /* See comment above for timeout value. */ |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 1105 | timeo = jiffies + uWriteTimeout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | for (;;) { |
| 1107 | if (chip->state != FL_WRITING) { |
| 1108 | /* Someone's suspended the write. Sleep */ |
| 1109 | DECLARE_WAITQUEUE(wait, current); |
| 1110 | |
| 1111 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 1112 | add_wait_queue(&chip->wq, &wait); |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1113 | mutex_unlock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | schedule(); |
| 1115 | remove_wait_queue(&chip->wq, &wait); |
| 1116 | timeo = jiffies + (HZ / 2); /* FIXME */ |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1117 | mutex_lock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1118 | continue; |
| 1119 | } |
| 1120 | |
Konstantin Baidarov | b95f960 | 2005-11-07 09:00:05 +0000 | [diff] [blame] | 1121 | if (time_after(jiffies, timeo) && !chip_ready(map, adr)){ |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 1122 | xip_enable(map, chip, adr); |
Eric W. Biedermann | fb4a90b | 2005-05-20 04:28:26 +0100 | [diff] [blame] | 1123 | printk(KERN_WARNING "MTD %s(): software timeout\n", __func__); |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 1124 | xip_disable(map, chip, adr); |
Konstantin Baidarov | b95f960 | 2005-11-07 09:00:05 +0000 | [diff] [blame] | 1125 | break; |
Eric W. Biedermann | fb4a90b | 2005-05-20 04:28:26 +0100 | [diff] [blame] | 1126 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | |
Konstantin Baidarov | b95f960 | 2005-11-07 09:00:05 +0000 | [diff] [blame] | 1128 | if (chip_ready(map, adr)) |
| 1129 | break; |
| 1130 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1131 | /* Latency issues. Drop the lock, wait a while and retry */ |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 1132 | UDELAY(map, chip, adr, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | } |
Eric W. Biedermann | fb4a90b | 2005-05-20 04:28:26 +0100 | [diff] [blame] | 1134 | /* Did we succeed? */ |
| 1135 | if (!chip_good(map, adr, datum)) { |
| 1136 | /* reset on all failures. */ |
| 1137 | map_write( map, CMD(0xF0), chip->start ); |
| 1138 | /* FIXME - should have reset delay before continuing */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1139 | |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 1140 | if (++retry_cnt <= MAX_WORD_RETRIES) |
Eric W. Biedermann | fb4a90b | 2005-05-20 04:28:26 +0100 | [diff] [blame] | 1141 | goto retry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1142 | |
Eric W. Biedermann | fb4a90b | 2005-05-20 04:28:26 +0100 | [diff] [blame] | 1143 | ret = -EIO; |
| 1144 | } |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 1145 | xip_enable(map, chip, adr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1146 | op_done: |
| 1147 | chip->state = FL_READY; |
| 1148 | put_chip(map, chip, adr); |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1149 | mutex_unlock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | |
| 1151 | return ret; |
| 1152 | } |
| 1153 | |
| 1154 | |
| 1155 | static int cfi_amdstd_write_words(struct mtd_info *mtd, loff_t to, size_t len, |
| 1156 | size_t *retlen, const u_char *buf) |
| 1157 | { |
| 1158 | struct map_info *map = mtd->priv; |
| 1159 | struct cfi_private *cfi = map->fldrv_priv; |
| 1160 | int ret = 0; |
| 1161 | int chipnum; |
| 1162 | unsigned long ofs, chipstart; |
| 1163 | DECLARE_WAITQUEUE(wait, current); |
| 1164 | |
| 1165 | *retlen = 0; |
| 1166 | if (!len) |
| 1167 | return 0; |
| 1168 | |
| 1169 | chipnum = to >> cfi->chipshift; |
| 1170 | ofs = to - (chipnum << cfi->chipshift); |
| 1171 | chipstart = cfi->chips[chipnum].start; |
| 1172 | |
| 1173 | /* If it's not bus-aligned, do the first byte write */ |
| 1174 | if (ofs & (map_bankwidth(map)-1)) { |
| 1175 | unsigned long bus_ofs = ofs & ~(map_bankwidth(map)-1); |
| 1176 | int i = ofs - bus_ofs; |
| 1177 | int n = 0; |
| 1178 | map_word tmp_buf; |
| 1179 | |
| 1180 | retry: |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1181 | mutex_lock(&cfi->chips[chipnum].mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1182 | |
| 1183 | if (cfi->chips[chipnum].state != FL_READY) { |
| 1184 | #if 0 |
| 1185 | printk(KERN_DEBUG "Waiting for chip to write, status = %d\n", cfi->chips[chipnum].state); |
| 1186 | #endif |
| 1187 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 1188 | add_wait_queue(&cfi->chips[chipnum].wq, &wait); |
| 1189 | |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1190 | mutex_unlock(&cfi->chips[chipnum].mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1191 | |
| 1192 | schedule(); |
| 1193 | remove_wait_queue(&cfi->chips[chipnum].wq, &wait); |
| 1194 | #if 0 |
| 1195 | if(signal_pending(current)) |
| 1196 | return -EINTR; |
| 1197 | #endif |
| 1198 | goto retry; |
| 1199 | } |
| 1200 | |
| 1201 | /* Load 'tmp_buf' with old contents of flash */ |
| 1202 | tmp_buf = map_read(map, bus_ofs+chipstart); |
| 1203 | |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1204 | mutex_unlock(&cfi->chips[chipnum].mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | |
| 1206 | /* Number of bytes to copy from buffer */ |
| 1207 | n = min_t(int, len, map_bankwidth(map)-i); |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 1208 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1209 | tmp_buf = map_word_load_partial(map, tmp_buf, buf, i, n); |
| 1210 | |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 1211 | ret = do_write_oneword(map, &cfi->chips[chipnum], |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | bus_ofs, tmp_buf); |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 1213 | if (ret) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1214 | return ret; |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 1215 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1216 | ofs += n; |
| 1217 | buf += n; |
| 1218 | (*retlen) += n; |
| 1219 | len -= n; |
| 1220 | |
| 1221 | if (ofs >> cfi->chipshift) { |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 1222 | chipnum ++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1223 | ofs = 0; |
| 1224 | if (chipnum == cfi->numchips) |
| 1225 | return 0; |
| 1226 | } |
| 1227 | } |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 1228 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1229 | /* We are now aligned, write as much as possible */ |
| 1230 | while(len >= map_bankwidth(map)) { |
| 1231 | map_word datum; |
| 1232 | |
| 1233 | datum = map_word_load(map, buf); |
| 1234 | |
| 1235 | ret = do_write_oneword(map, &cfi->chips[chipnum], |
| 1236 | ofs, datum); |
| 1237 | if (ret) |
| 1238 | return ret; |
| 1239 | |
| 1240 | ofs += map_bankwidth(map); |
| 1241 | buf += map_bankwidth(map); |
| 1242 | (*retlen) += map_bankwidth(map); |
| 1243 | len -= map_bankwidth(map); |
| 1244 | |
| 1245 | if (ofs >> cfi->chipshift) { |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 1246 | chipnum ++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1247 | ofs = 0; |
| 1248 | if (chipnum == cfi->numchips) |
| 1249 | return 0; |
| 1250 | chipstart = cfi->chips[chipnum].start; |
| 1251 | } |
| 1252 | } |
| 1253 | |
| 1254 | /* Write the trailing bytes if any */ |
| 1255 | if (len & (map_bankwidth(map)-1)) { |
| 1256 | map_word tmp_buf; |
| 1257 | |
| 1258 | retry1: |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1259 | mutex_lock(&cfi->chips[chipnum].mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1260 | |
| 1261 | if (cfi->chips[chipnum].state != FL_READY) { |
| 1262 | #if 0 |
| 1263 | printk(KERN_DEBUG "Waiting for chip to write, status = %d\n", cfi->chips[chipnum].state); |
| 1264 | #endif |
| 1265 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 1266 | add_wait_queue(&cfi->chips[chipnum].wq, &wait); |
| 1267 | |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1268 | mutex_unlock(&cfi->chips[chipnum].mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1269 | |
| 1270 | schedule(); |
| 1271 | remove_wait_queue(&cfi->chips[chipnum].wq, &wait); |
| 1272 | #if 0 |
| 1273 | if(signal_pending(current)) |
| 1274 | return -EINTR; |
| 1275 | #endif |
| 1276 | goto retry1; |
| 1277 | } |
| 1278 | |
| 1279 | tmp_buf = map_read(map, ofs + chipstart); |
| 1280 | |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1281 | mutex_unlock(&cfi->chips[chipnum].mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1282 | |
| 1283 | tmp_buf = map_word_load_partial(map, tmp_buf, buf, 0, len); |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 1284 | |
| 1285 | ret = do_write_oneword(map, &cfi->chips[chipnum], |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1286 | ofs, tmp_buf); |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 1287 | if (ret) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1288 | return ret; |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 1289 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1290 | (*retlen) += len; |
| 1291 | } |
| 1292 | |
| 1293 | return 0; |
| 1294 | } |
| 1295 | |
| 1296 | |
| 1297 | /* |
| 1298 | * FIXME: interleaved mode not tested, and probably not supported! |
| 1299 | */ |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 1300 | 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] | 1301 | unsigned long adr, const u_char *buf, |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 1302 | int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1303 | { |
| 1304 | struct cfi_private *cfi = map->fldrv_priv; |
| 1305 | unsigned long timeo = jiffies + HZ; |
| 1306 | /* see comments in do_write_oneword() regarding uWriteTimeo. */ |
| 1307 | unsigned long uWriteTimeout = ( HZ / 1000 ) + 1; |
| 1308 | int ret = -EIO; |
| 1309 | unsigned long cmd_adr; |
| 1310 | int z, words; |
| 1311 | map_word datum; |
| 1312 | |
| 1313 | adr += chip->start; |
| 1314 | cmd_adr = adr; |
| 1315 | |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1316 | mutex_lock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1317 | ret = get_chip(map, chip, adr, FL_WRITING); |
| 1318 | if (ret) { |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1319 | mutex_unlock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1320 | return ret; |
| 1321 | } |
| 1322 | |
| 1323 | datum = map_word_load(map, buf); |
| 1324 | |
| 1325 | DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): WRITE 0x%.8lx(0x%.8lx)\n", |
| 1326 | __func__, adr, datum.x[0] ); |
| 1327 | |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 1328 | XIP_INVAL_CACHED_RANGE(map, adr, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1329 | ENABLE_VPP(map); |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 1330 | xip_disable(map, chip, cmd_adr); |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 1331 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1332 | cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL); |
| 1333 | cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL); |
| 1334 | //cfi_send_gen_cmd(0xA0, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL); |
| 1335 | |
| 1336 | /* Write Buffer Load */ |
| 1337 | map_write(map, CMD(0x25), cmd_adr); |
| 1338 | |
| 1339 | chip->state = FL_WRITING_TO_BUFFER; |
| 1340 | |
| 1341 | /* Write length of data to come */ |
| 1342 | words = len / map_bankwidth(map); |
| 1343 | map_write(map, CMD(words - 1), cmd_adr); |
| 1344 | /* Write data */ |
| 1345 | z = 0; |
| 1346 | while(z < words * map_bankwidth(map)) { |
| 1347 | datum = map_word_load(map, buf); |
| 1348 | map_write(map, datum, adr + z); |
| 1349 | |
| 1350 | z += map_bankwidth(map); |
| 1351 | buf += map_bankwidth(map); |
| 1352 | } |
| 1353 | z -= map_bankwidth(map); |
| 1354 | |
| 1355 | adr += z; |
| 1356 | |
| 1357 | /* Write Buffer Program Confirm: GO GO GO */ |
| 1358 | map_write(map, CMD(0x29), cmd_adr); |
| 1359 | chip->state = FL_WRITING; |
| 1360 | |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 1361 | INVALIDATE_CACHE_UDELAY(map, chip, |
| 1362 | adr, map_bankwidth(map), |
| 1363 | chip->word_write_time); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1364 | |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 1365 | timeo = jiffies + uWriteTimeout; |
| 1366 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1367 | for (;;) { |
| 1368 | if (chip->state != FL_WRITING) { |
| 1369 | /* Someone's suspended the write. Sleep */ |
| 1370 | DECLARE_WAITQUEUE(wait, current); |
| 1371 | |
| 1372 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 1373 | add_wait_queue(&chip->wq, &wait); |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1374 | mutex_unlock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | schedule(); |
| 1376 | remove_wait_queue(&chip->wq, &wait); |
| 1377 | timeo = jiffies + (HZ / 2); /* FIXME */ |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1378 | mutex_lock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1379 | continue; |
| 1380 | } |
| 1381 | |
Konstantin Baidarov | b95f960 | 2005-11-07 09:00:05 +0000 | [diff] [blame] | 1382 | if (time_after(jiffies, timeo) && !chip_ready(map, adr)) |
| 1383 | break; |
| 1384 | |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 1385 | if (chip_ready(map, adr)) { |
| 1386 | xip_enable(map, chip, adr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1387 | goto op_done; |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 1388 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | |
| 1390 | /* Latency issues. Drop the lock, wait a while and retry */ |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 1391 | UDELAY(map, chip, adr, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1392 | } |
| 1393 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1394 | /* reset on all failures. */ |
| 1395 | map_write( map, CMD(0xF0), chip->start ); |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 1396 | xip_enable(map, chip, adr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1397 | /* FIXME - should have reset delay before continuing */ |
| 1398 | |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 1399 | printk(KERN_WARNING "MTD %s(): software timeout\n", |
| 1400 | __func__ ); |
| 1401 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | ret = -EIO; |
| 1403 | op_done: |
| 1404 | chip->state = FL_READY; |
| 1405 | put_chip(map, chip, adr); |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1406 | mutex_unlock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1407 | |
| 1408 | return ret; |
| 1409 | } |
| 1410 | |
| 1411 | |
| 1412 | static int cfi_amdstd_write_buffers(struct mtd_info *mtd, loff_t to, size_t len, |
| 1413 | size_t *retlen, const u_char *buf) |
| 1414 | { |
| 1415 | struct map_info *map = mtd->priv; |
| 1416 | struct cfi_private *cfi = map->fldrv_priv; |
| 1417 | int wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize; |
| 1418 | int ret = 0; |
| 1419 | int chipnum; |
| 1420 | unsigned long ofs; |
| 1421 | |
| 1422 | *retlen = 0; |
| 1423 | if (!len) |
| 1424 | return 0; |
| 1425 | |
| 1426 | chipnum = to >> cfi->chipshift; |
| 1427 | ofs = to - (chipnum << cfi->chipshift); |
| 1428 | |
| 1429 | /* If it's not bus-aligned, do the first word write */ |
| 1430 | if (ofs & (map_bankwidth(map)-1)) { |
| 1431 | size_t local_len = (-ofs)&(map_bankwidth(map)-1); |
| 1432 | if (local_len > len) |
| 1433 | local_len = len; |
| 1434 | ret = cfi_amdstd_write_words(mtd, ofs + (chipnum<<cfi->chipshift), |
| 1435 | local_len, retlen, buf); |
| 1436 | if (ret) |
| 1437 | return ret; |
| 1438 | ofs += local_len; |
| 1439 | buf += local_len; |
| 1440 | len -= local_len; |
| 1441 | |
| 1442 | if (ofs >> cfi->chipshift) { |
| 1443 | chipnum ++; |
| 1444 | ofs = 0; |
| 1445 | if (chipnum == cfi->numchips) |
| 1446 | return 0; |
| 1447 | } |
| 1448 | } |
| 1449 | |
| 1450 | /* Write buffer is worth it only if more than one word to write... */ |
| 1451 | while (len >= map_bankwidth(map) * 2) { |
| 1452 | /* We must not cross write block boundaries */ |
| 1453 | int size = wbufsize - (ofs & (wbufsize-1)); |
| 1454 | |
| 1455 | if (size > len) |
| 1456 | size = len; |
| 1457 | if (size % map_bankwidth(map)) |
| 1458 | size -= size % map_bankwidth(map); |
| 1459 | |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 1460 | ret = do_write_buffer(map, &cfi->chips[chipnum], |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1461 | ofs, buf, size); |
| 1462 | if (ret) |
| 1463 | return ret; |
| 1464 | |
| 1465 | ofs += size; |
| 1466 | buf += size; |
| 1467 | (*retlen) += size; |
| 1468 | len -= size; |
| 1469 | |
| 1470 | if (ofs >> cfi->chipshift) { |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 1471 | chipnum ++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1472 | ofs = 0; |
| 1473 | if (chipnum == cfi->numchips) |
| 1474 | return 0; |
| 1475 | } |
| 1476 | } |
| 1477 | |
| 1478 | if (len) { |
| 1479 | size_t retlen_dregs = 0; |
| 1480 | |
| 1481 | ret = cfi_amdstd_write_words(mtd, ofs + (chipnum<<cfi->chipshift), |
| 1482 | len, &retlen_dregs, buf); |
| 1483 | |
| 1484 | *retlen += retlen_dregs; |
| 1485 | return ret; |
| 1486 | } |
| 1487 | |
| 1488 | return 0; |
| 1489 | } |
| 1490 | |
| 1491 | |
| 1492 | /* |
| 1493 | * Handle devices with one erase region, that only implement |
| 1494 | * the chip erase command. |
| 1495 | */ |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 1496 | 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] | 1497 | { |
| 1498 | struct cfi_private *cfi = map->fldrv_priv; |
| 1499 | unsigned long timeo = jiffies + HZ; |
| 1500 | unsigned long int adr; |
| 1501 | DECLARE_WAITQUEUE(wait, current); |
| 1502 | int ret = 0; |
| 1503 | |
| 1504 | adr = cfi->addr_unlock1; |
| 1505 | |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1506 | mutex_lock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1507 | ret = get_chip(map, chip, adr, FL_WRITING); |
| 1508 | if (ret) { |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1509 | mutex_unlock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1510 | return ret; |
| 1511 | } |
| 1512 | |
| 1513 | DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): ERASE 0x%.8lx\n", |
| 1514 | __func__, chip->start ); |
| 1515 | |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 1516 | XIP_INVAL_CACHED_RANGE(map, adr, map->size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1517 | ENABLE_VPP(map); |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 1518 | xip_disable(map, chip, adr); |
| 1519 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1520 | cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL); |
| 1521 | cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL); |
| 1522 | cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL); |
| 1523 | cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL); |
| 1524 | cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL); |
| 1525 | cfi_send_gen_cmd(0x10, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL); |
| 1526 | |
| 1527 | chip->state = FL_ERASING; |
| 1528 | chip->erase_suspended = 0; |
| 1529 | chip->in_progress_block_addr = adr; |
| 1530 | |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 1531 | INVALIDATE_CACHE_UDELAY(map, chip, |
| 1532 | adr, map->size, |
| 1533 | chip->erase_time*500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1534 | |
| 1535 | timeo = jiffies + (HZ*20); |
| 1536 | |
| 1537 | for (;;) { |
| 1538 | if (chip->state != FL_ERASING) { |
| 1539 | /* Someone's suspended the erase. Sleep */ |
| 1540 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 1541 | add_wait_queue(&chip->wq, &wait); |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1542 | mutex_unlock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1543 | schedule(); |
| 1544 | remove_wait_queue(&chip->wq, &wait); |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1545 | mutex_lock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1546 | continue; |
| 1547 | } |
| 1548 | if (chip->erase_suspended) { |
| 1549 | /* This erase was suspended and resumed. |
| 1550 | Adjust the timeout */ |
| 1551 | timeo = jiffies + (HZ*20); /* FIXME */ |
| 1552 | chip->erase_suspended = 0; |
| 1553 | } |
| 1554 | |
| 1555 | if (chip_ready(map, adr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1556 | break; |
| 1557 | |
Eric W. Biedermann | fb4a90b | 2005-05-20 04:28:26 +0100 | [diff] [blame] | 1558 | if (time_after(jiffies, timeo)) { |
| 1559 | printk(KERN_WARNING "MTD %s(): software timeout\n", |
| 1560 | __func__ ); |
| 1561 | break; |
| 1562 | } |
| 1563 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1564 | /* Latency issues. Drop the lock, wait a while and retry */ |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 1565 | UDELAY(map, chip, adr, 1000000/HZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1566 | } |
Eric W. Biedermann | fb4a90b | 2005-05-20 04:28:26 +0100 | [diff] [blame] | 1567 | /* Did we succeed? */ |
| 1568 | if (!chip_good(map, adr, map_word_ff(map))) { |
| 1569 | /* reset on all failures. */ |
| 1570 | map_write( map, CMD(0xF0), chip->start ); |
| 1571 | /* FIXME - should have reset delay before continuing */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1572 | |
Eric W. Biedermann | fb4a90b | 2005-05-20 04:28:26 +0100 | [diff] [blame] | 1573 | ret = -EIO; |
| 1574 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1575 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1576 | chip->state = FL_READY; |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 1577 | xip_enable(map, chip, adr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1578 | put_chip(map, chip, adr); |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1579 | mutex_unlock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1580 | |
| 1581 | return ret; |
| 1582 | } |
| 1583 | |
| 1584 | |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 1585 | 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] | 1586 | { |
| 1587 | struct cfi_private *cfi = map->fldrv_priv; |
| 1588 | unsigned long timeo = jiffies + HZ; |
| 1589 | DECLARE_WAITQUEUE(wait, current); |
| 1590 | int ret = 0; |
| 1591 | |
| 1592 | adr += chip->start; |
| 1593 | |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1594 | mutex_lock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1595 | ret = get_chip(map, chip, adr, FL_ERASING); |
| 1596 | if (ret) { |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1597 | mutex_unlock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1598 | return ret; |
| 1599 | } |
| 1600 | |
| 1601 | DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): ERASE 0x%.8lx\n", |
| 1602 | __func__, adr ); |
| 1603 | |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 1604 | XIP_INVAL_CACHED_RANGE(map, adr, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1605 | ENABLE_VPP(map); |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 1606 | xip_disable(map, chip, adr); |
| 1607 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1608 | cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL); |
| 1609 | cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL); |
| 1610 | cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL); |
| 1611 | cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL); |
| 1612 | cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL); |
| 1613 | map_write(map, CMD(0x30), adr); |
| 1614 | |
| 1615 | chip->state = FL_ERASING; |
| 1616 | chip->erase_suspended = 0; |
| 1617 | chip->in_progress_block_addr = adr; |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 1618 | |
| 1619 | INVALIDATE_CACHE_UDELAY(map, chip, |
| 1620 | adr, len, |
| 1621 | chip->erase_time*500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1622 | |
| 1623 | timeo = jiffies + (HZ*20); |
| 1624 | |
| 1625 | for (;;) { |
| 1626 | if (chip->state != FL_ERASING) { |
| 1627 | /* Someone's suspended the erase. Sleep */ |
| 1628 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 1629 | add_wait_queue(&chip->wq, &wait); |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1630 | mutex_unlock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1631 | schedule(); |
| 1632 | remove_wait_queue(&chip->wq, &wait); |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1633 | mutex_lock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1634 | continue; |
| 1635 | } |
| 1636 | if (chip->erase_suspended) { |
| 1637 | /* This erase was suspended and resumed. |
| 1638 | Adjust the timeout */ |
| 1639 | timeo = jiffies + (HZ*20); /* FIXME */ |
| 1640 | chip->erase_suspended = 0; |
| 1641 | } |
| 1642 | |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 1643 | if (chip_ready(map, adr)) { |
| 1644 | xip_enable(map, chip, adr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1645 | break; |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 1646 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1647 | |
Eric W. Biedermann | fb4a90b | 2005-05-20 04:28:26 +0100 | [diff] [blame] | 1648 | if (time_after(jiffies, timeo)) { |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 1649 | xip_enable(map, chip, adr); |
Eric W. Biedermann | fb4a90b | 2005-05-20 04:28:26 +0100 | [diff] [blame] | 1650 | printk(KERN_WARNING "MTD %s(): software timeout\n", |
| 1651 | __func__ ); |
| 1652 | break; |
| 1653 | } |
| 1654 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1655 | /* Latency issues. Drop the lock, wait a while and retry */ |
Todd Poynor | 02b15e3 | 2005-06-07 00:04:39 +0100 | [diff] [blame] | 1656 | UDELAY(map, chip, adr, 1000000/HZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1657 | } |
Eric W. Biedermann | fb4a90b | 2005-05-20 04:28:26 +0100 | [diff] [blame] | 1658 | /* Did we succeed? */ |
Thomas Gleixner | 22fd9a8 | 2005-05-24 15:33:49 +0200 | [diff] [blame] | 1659 | if (!chip_good(map, adr, map_word_ff(map))) { |
Eric W. Biedermann | fb4a90b | 2005-05-20 04:28:26 +0100 | [diff] [blame] | 1660 | /* reset on all failures. */ |
| 1661 | map_write( map, CMD(0xF0), chip->start ); |
| 1662 | /* FIXME - should have reset delay before continuing */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1663 | |
Eric W. Biedermann | fb4a90b | 2005-05-20 04:28:26 +0100 | [diff] [blame] | 1664 | ret = -EIO; |
| 1665 | } |
| 1666 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1667 | chip->state = FL_READY; |
| 1668 | put_chip(map, chip, adr); |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1669 | mutex_unlock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1670 | return ret; |
| 1671 | } |
| 1672 | |
| 1673 | |
Ben Dooks | ce0f33a | 2007-05-28 19:59:00 +0100 | [diff] [blame] | 1674 | 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] | 1675 | { |
| 1676 | unsigned long ofs, len; |
| 1677 | int ret; |
| 1678 | |
| 1679 | ofs = instr->addr; |
| 1680 | len = instr->len; |
| 1681 | |
| 1682 | ret = cfi_varsize_frob(mtd, do_erase_oneblock, ofs, len, NULL); |
| 1683 | if (ret) |
| 1684 | return ret; |
| 1685 | |
| 1686 | instr->state = MTD_ERASE_DONE; |
| 1687 | mtd_erase_callback(instr); |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 1688 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1689 | return 0; |
| 1690 | } |
| 1691 | |
| 1692 | |
| 1693 | static int cfi_amdstd_erase_chip(struct mtd_info *mtd, struct erase_info *instr) |
| 1694 | { |
| 1695 | struct map_info *map = mtd->priv; |
| 1696 | struct cfi_private *cfi = map->fldrv_priv; |
| 1697 | int ret = 0; |
| 1698 | |
| 1699 | if (instr->addr != 0) |
| 1700 | return -EINVAL; |
| 1701 | |
| 1702 | if (instr->len != mtd->size) |
| 1703 | return -EINVAL; |
| 1704 | |
| 1705 | ret = do_erase_chip(map, &cfi->chips[0]); |
| 1706 | if (ret) |
| 1707 | return ret; |
| 1708 | |
| 1709 | instr->state = MTD_ERASE_DONE; |
| 1710 | mtd_erase_callback(instr); |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 1711 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1712 | return 0; |
| 1713 | } |
| 1714 | |
Haavard Skinnemoen | 0165508 | 2006-08-09 11:06:07 +0200 | [diff] [blame] | 1715 | static int do_atmel_lock(struct map_info *map, struct flchip *chip, |
| 1716 | unsigned long adr, int len, void *thunk) |
| 1717 | { |
| 1718 | struct cfi_private *cfi = map->fldrv_priv; |
| 1719 | int ret; |
| 1720 | |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1721 | mutex_lock(&chip->mutex); |
Haavard Skinnemoen | 0165508 | 2006-08-09 11:06:07 +0200 | [diff] [blame] | 1722 | ret = get_chip(map, chip, adr + chip->start, FL_LOCKING); |
| 1723 | if (ret) |
| 1724 | goto out_unlock; |
| 1725 | chip->state = FL_LOCKING; |
| 1726 | |
| 1727 | DEBUG(MTD_DEBUG_LEVEL3, "MTD %s(): LOCK 0x%08lx len %d\n", |
| 1728 | __func__, adr, len); |
| 1729 | |
| 1730 | cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, |
| 1731 | cfi->device_type, NULL); |
| 1732 | cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, |
| 1733 | cfi->device_type, NULL); |
| 1734 | cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi, |
| 1735 | cfi->device_type, NULL); |
| 1736 | cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, |
| 1737 | cfi->device_type, NULL); |
| 1738 | cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, |
| 1739 | cfi->device_type, NULL); |
| 1740 | map_write(map, CMD(0x40), chip->start + adr); |
| 1741 | |
| 1742 | chip->state = FL_READY; |
| 1743 | put_chip(map, chip, adr + chip->start); |
| 1744 | ret = 0; |
| 1745 | |
| 1746 | out_unlock: |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1747 | mutex_unlock(&chip->mutex); |
Haavard Skinnemoen | 0165508 | 2006-08-09 11:06:07 +0200 | [diff] [blame] | 1748 | return ret; |
| 1749 | } |
| 1750 | |
| 1751 | static int do_atmel_unlock(struct map_info *map, struct flchip *chip, |
| 1752 | unsigned long adr, int len, void *thunk) |
| 1753 | { |
| 1754 | struct cfi_private *cfi = map->fldrv_priv; |
| 1755 | int ret; |
| 1756 | |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1757 | mutex_lock(&chip->mutex); |
Haavard Skinnemoen | 0165508 | 2006-08-09 11:06:07 +0200 | [diff] [blame] | 1758 | ret = get_chip(map, chip, adr + chip->start, FL_UNLOCKING); |
| 1759 | if (ret) |
| 1760 | goto out_unlock; |
| 1761 | chip->state = FL_UNLOCKING; |
| 1762 | |
| 1763 | DEBUG(MTD_DEBUG_LEVEL3, "MTD %s(): LOCK 0x%08lx len %d\n", |
| 1764 | __func__, adr, len); |
| 1765 | |
| 1766 | cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, |
| 1767 | cfi->device_type, NULL); |
| 1768 | map_write(map, CMD(0x70), adr); |
| 1769 | |
| 1770 | chip->state = FL_READY; |
| 1771 | put_chip(map, chip, adr + chip->start); |
| 1772 | ret = 0; |
| 1773 | |
| 1774 | out_unlock: |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1775 | mutex_unlock(&chip->mutex); |
Haavard Skinnemoen | 0165508 | 2006-08-09 11:06:07 +0200 | [diff] [blame] | 1776 | return ret; |
| 1777 | } |
| 1778 | |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 1779 | 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] | 1780 | { |
| 1781 | return cfi_varsize_frob(mtd, do_atmel_lock, ofs, len, NULL); |
| 1782 | } |
| 1783 | |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 1784 | 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] | 1785 | { |
| 1786 | return cfi_varsize_frob(mtd, do_atmel_unlock, ofs, len, NULL); |
| 1787 | } |
| 1788 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1789 | |
| 1790 | static void cfi_amdstd_sync (struct mtd_info *mtd) |
| 1791 | { |
| 1792 | struct map_info *map = mtd->priv; |
| 1793 | struct cfi_private *cfi = map->fldrv_priv; |
| 1794 | int i; |
| 1795 | struct flchip *chip; |
| 1796 | int ret = 0; |
| 1797 | DECLARE_WAITQUEUE(wait, current); |
| 1798 | |
| 1799 | for (i=0; !ret && i<cfi->numchips; i++) { |
| 1800 | chip = &cfi->chips[i]; |
| 1801 | |
| 1802 | retry: |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1803 | mutex_lock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1804 | |
| 1805 | switch(chip->state) { |
| 1806 | case FL_READY: |
| 1807 | case FL_STATUS: |
| 1808 | case FL_CFI_QUERY: |
| 1809 | case FL_JEDEC_QUERY: |
| 1810 | chip->oldstate = chip->state; |
| 1811 | chip->state = FL_SYNCING; |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 1812 | /* No need to wake_up() on this state change - |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1813 | * as the whole point is that nobody can do anything |
| 1814 | * with the chip now anyway. |
| 1815 | */ |
| 1816 | case FL_SYNCING: |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1817 | mutex_unlock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1818 | break; |
| 1819 | |
| 1820 | default: |
| 1821 | /* Not an idle state */ |
Dmitry Adamushko | f8e30e4 | 2008-04-08 17:41:59 -0700 | [diff] [blame] | 1822 | set_current_state(TASK_UNINTERRUPTIBLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1823 | add_wait_queue(&chip->wq, &wait); |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 1824 | |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1825 | mutex_unlock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1826 | |
| 1827 | schedule(); |
| 1828 | |
| 1829 | remove_wait_queue(&chip->wq, &wait); |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 1830 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1831 | goto retry; |
| 1832 | } |
| 1833 | } |
| 1834 | |
| 1835 | /* Unlock the chips again */ |
| 1836 | |
| 1837 | for (i--; i >=0; i--) { |
| 1838 | chip = &cfi->chips[i]; |
| 1839 | |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1840 | mutex_lock(&chip->mutex); |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 1841 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1842 | if (chip->state == FL_SYNCING) { |
| 1843 | chip->state = chip->oldstate; |
| 1844 | wake_up(&chip->wq); |
| 1845 | } |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1846 | mutex_unlock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1847 | } |
| 1848 | } |
| 1849 | |
| 1850 | |
| 1851 | static int cfi_amdstd_suspend(struct mtd_info *mtd) |
| 1852 | { |
| 1853 | struct map_info *map = mtd->priv; |
| 1854 | struct cfi_private *cfi = map->fldrv_priv; |
| 1855 | int i; |
| 1856 | struct flchip *chip; |
| 1857 | int ret = 0; |
| 1858 | |
| 1859 | for (i=0; !ret && i<cfi->numchips; i++) { |
| 1860 | chip = &cfi->chips[i]; |
| 1861 | |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1862 | mutex_lock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1863 | |
| 1864 | switch(chip->state) { |
| 1865 | case FL_READY: |
| 1866 | case FL_STATUS: |
| 1867 | case FL_CFI_QUERY: |
| 1868 | case FL_JEDEC_QUERY: |
| 1869 | chip->oldstate = chip->state; |
| 1870 | chip->state = FL_PM_SUSPENDED; |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 1871 | /* No need to wake_up() on this state change - |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1872 | * as the whole point is that nobody can do anything |
| 1873 | * with the chip now anyway. |
| 1874 | */ |
| 1875 | case FL_PM_SUSPENDED: |
| 1876 | break; |
| 1877 | |
| 1878 | default: |
| 1879 | ret = -EAGAIN; |
| 1880 | break; |
| 1881 | } |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1882 | mutex_unlock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1883 | } |
| 1884 | |
| 1885 | /* Unlock the chips again */ |
| 1886 | |
| 1887 | if (ret) { |
| 1888 | for (i--; i >=0; i--) { |
| 1889 | chip = &cfi->chips[i]; |
| 1890 | |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1891 | mutex_lock(&chip->mutex); |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 1892 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1893 | if (chip->state == FL_PM_SUSPENDED) { |
| 1894 | chip->state = chip->oldstate; |
| 1895 | wake_up(&chip->wq); |
| 1896 | } |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1897 | mutex_unlock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1898 | } |
| 1899 | } |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 1900 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1901 | return ret; |
| 1902 | } |
| 1903 | |
| 1904 | |
| 1905 | static void cfi_amdstd_resume(struct mtd_info *mtd) |
| 1906 | { |
| 1907 | struct map_info *map = mtd->priv; |
| 1908 | struct cfi_private *cfi = map->fldrv_priv; |
| 1909 | int i; |
| 1910 | struct flchip *chip; |
| 1911 | |
| 1912 | for (i=0; i<cfi->numchips; i++) { |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 1913 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1914 | chip = &cfi->chips[i]; |
| 1915 | |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1916 | mutex_lock(&chip->mutex); |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 1917 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1918 | if (chip->state == FL_PM_SUSPENDED) { |
| 1919 | chip->state = FL_READY; |
| 1920 | map_write(map, CMD(0xF0), chip->start); |
| 1921 | wake_up(&chip->wq); |
| 1922 | } |
| 1923 | else |
| 1924 | printk(KERN_ERR "Argh. Chip not in PM_SUSPENDED state upon resume()\n"); |
| 1925 | |
Stefani Seibold | c4e7737 | 2010-04-18 22:46:44 +0200 | [diff] [blame] | 1926 | mutex_unlock(&chip->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1927 | } |
| 1928 | } |
| 1929 | |
Kevin Cernekee | eafe131 | 2010-04-29 10:26:56 -0700 | [diff] [blame^] | 1930 | |
| 1931 | /* |
| 1932 | * Ensure that the flash device is put back into read array mode before |
| 1933 | * unloading the driver or rebooting. On some systems, rebooting while |
| 1934 | * the flash is in query/program/erase mode will prevent the CPU from |
| 1935 | * fetching the bootloader code, requiring a hard reset or power cycle. |
| 1936 | */ |
| 1937 | static int cfi_amdstd_reset(struct mtd_info *mtd) |
| 1938 | { |
| 1939 | struct map_info *map = mtd->priv; |
| 1940 | struct cfi_private *cfi = map->fldrv_priv; |
| 1941 | int i, ret; |
| 1942 | struct flchip *chip; |
| 1943 | |
| 1944 | for (i = 0; i < cfi->numchips; i++) { |
| 1945 | |
| 1946 | chip = &cfi->chips[i]; |
| 1947 | |
| 1948 | mutex_lock(&chip->mutex); |
| 1949 | |
| 1950 | ret = get_chip(map, chip, chip->start, FL_SHUTDOWN); |
| 1951 | if (!ret) { |
| 1952 | map_write(map, CMD(0xF0), chip->start); |
| 1953 | chip->state = FL_SHUTDOWN; |
| 1954 | put_chip(map, chip, chip->start); |
| 1955 | } |
| 1956 | |
| 1957 | mutex_unlock(&chip->mutex); |
| 1958 | } |
| 1959 | |
| 1960 | return 0; |
| 1961 | } |
| 1962 | |
| 1963 | |
| 1964 | static int cfi_amdstd_reboot(struct notifier_block *nb, unsigned long val, |
| 1965 | void *v) |
| 1966 | { |
| 1967 | struct mtd_info *mtd; |
| 1968 | |
| 1969 | mtd = container_of(nb, struct mtd_info, reboot_notifier); |
| 1970 | cfi_amdstd_reset(mtd); |
| 1971 | return NOTIFY_DONE; |
| 1972 | } |
| 1973 | |
| 1974 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1975 | static void cfi_amdstd_destroy(struct mtd_info *mtd) |
| 1976 | { |
| 1977 | struct map_info *map = mtd->priv; |
| 1978 | struct cfi_private *cfi = map->fldrv_priv; |
Jesper Juhl | fa67164 | 2005-11-07 01:01:27 -0800 | [diff] [blame] | 1979 | |
Kevin Cernekee | eafe131 | 2010-04-29 10:26:56 -0700 | [diff] [blame^] | 1980 | cfi_amdstd_reset(mtd); |
| 1981 | unregister_reboot_notifier(&mtd->reboot_notifier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1982 | kfree(cfi->cmdset_priv); |
| 1983 | kfree(cfi->cfiq); |
| 1984 | kfree(cfi); |
| 1985 | kfree(mtd->eraseregions); |
| 1986 | } |
| 1987 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1988 | MODULE_LICENSE("GPL"); |
| 1989 | MODULE_AUTHOR("Crossnet Co. <info@crossnet.co.jp> et al."); |
| 1990 | MODULE_DESCRIPTION("MTD chip driver for AMD/Fujitsu flash chips"); |