Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * MTD chip driver for pre-CFI Sharp flash chips |
| 3 | * |
| 4 | * Copyright 2000,2001 David A. Schleef <ds@schleef.org> |
| 5 | * 2000,2001 Lineo, Inc. |
| 6 | * |
Richard Purdie | ee2d49d | 2005-11-29 14:28:31 +0000 | [diff] [blame] | 7 | * $Id: sharp.c,v 1.17 2005/11/29 14:28:28 gleixner Exp $ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * |
| 9 | * Devices supported: |
| 10 | * LH28F016SCT Symmetrical block flash memory, 2Mx8 |
| 11 | * LH28F008SCT Symmetrical block flash memory, 1Mx8 |
| 12 | * |
| 13 | * Documentation: |
| 14 | * http://www.sharpmeg.com/datasheets/memic/flashcmp/ |
| 15 | * http://www.sharpmeg.com/datasheets/memic/flashcmp/01symf/16m/016sctl9.pdf |
| 16 | * 016sctl9.pdf |
| 17 | * |
| 18 | * Limitations: |
| 19 | * This driver only supports 4x1 arrangement of chips. |
| 20 | * Not tested on anything but PowerPC. |
| 21 | */ |
| 22 | |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/types.h> |
| 26 | #include <linux/sched.h> |
| 27 | #include <linux/errno.h> |
| 28 | #include <linux/interrupt.h> |
| 29 | #include <linux/mtd/map.h> |
| 30 | #include <linux/mtd/mtd.h> |
| 31 | #include <linux/mtd/cfi.h> |
| 32 | #include <linux/delay.h> |
| 33 | #include <linux/init.h> |
Todd Poynor | 1da2c9a | 2005-08-02 21:36:09 +0100 | [diff] [blame] | 34 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
| 36 | #define CMD_RESET 0xffffffff |
| 37 | #define CMD_READ_ID 0x90909090 |
| 38 | #define CMD_READ_STATUS 0x70707070 |
| 39 | #define CMD_CLEAR_STATUS 0x50505050 |
| 40 | #define CMD_BLOCK_ERASE_1 0x20202020 |
| 41 | #define CMD_BLOCK_ERASE_2 0xd0d0d0d0 |
| 42 | #define CMD_BYTE_WRITE 0x40404040 |
| 43 | #define CMD_SUSPEND 0xb0b0b0b0 |
| 44 | #define CMD_RESUME 0xd0d0d0d0 |
| 45 | #define CMD_SET_BLOCK_LOCK_1 0x60606060 |
| 46 | #define CMD_SET_BLOCK_LOCK_2 0x01010101 |
| 47 | #define CMD_SET_MASTER_LOCK_1 0x60606060 |
| 48 | #define CMD_SET_MASTER_LOCK_2 0xf1f1f1f1 |
| 49 | #define CMD_CLEAR_BLOCK_LOCKS_1 0x60606060 |
| 50 | #define CMD_CLEAR_BLOCK_LOCKS_2 0xd0d0d0d0 |
| 51 | |
| 52 | #define SR_READY 0x80808080 // 1 = ready |
| 53 | #define SR_ERASE_SUSPEND 0x40404040 // 1 = block erase suspended |
| 54 | #define SR_ERROR_ERASE 0x20202020 // 1 = error in block erase or clear lock bits |
| 55 | #define SR_ERROR_WRITE 0x10101010 // 1 = error in byte write or set lock bit |
| 56 | #define SR_VPP 0x08080808 // 1 = Vpp is low |
| 57 | #define SR_WRITE_SUSPEND 0x04040404 // 1 = byte write suspended |
| 58 | #define SR_PROTECT 0x02020202 // 1 = lock bit set |
| 59 | #define SR_RESERVED 0x01010101 |
| 60 | |
| 61 | #define SR_ERRORS (SR_ERROR_ERASE|SR_ERROR_WRITE|SR_VPP|SR_PROTECT) |
| 62 | |
| 63 | /* Configuration options */ |
| 64 | |
| 65 | #undef AUTOUNLOCK /* automatically unlocks blocks before erasing */ |
| 66 | |
Adrian Bunk | 0500abf | 2006-03-31 02:29:42 -0800 | [diff] [blame] | 67 | static struct mtd_info *sharp_probe(struct map_info *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
| 69 | static int sharp_probe_map(struct map_info *map,struct mtd_info *mtd); |
| 70 | |
| 71 | static int sharp_read(struct mtd_info *mtd, loff_t from, size_t len, |
| 72 | size_t *retlen, u_char *buf); |
| 73 | static int sharp_write(struct mtd_info *mtd, loff_t from, size_t len, |
| 74 | size_t *retlen, const u_char *buf); |
| 75 | static int sharp_erase(struct mtd_info *mtd, struct erase_info *instr); |
| 76 | static void sharp_sync(struct mtd_info *mtd); |
| 77 | static int sharp_suspend(struct mtd_info *mtd); |
| 78 | static void sharp_resume(struct mtd_info *mtd); |
| 79 | static void sharp_destroy(struct mtd_info *mtd); |
| 80 | |
| 81 | static int sharp_write_oneword(struct map_info *map, struct flchip *chip, |
| 82 | unsigned long adr, __u32 datum); |
| 83 | static int sharp_erase_oneblock(struct map_info *map, struct flchip *chip, |
| 84 | unsigned long adr); |
| 85 | #ifdef AUTOUNLOCK |
| 86 | static void sharp_unlock_oneblock(struct map_info *map, struct flchip *chip, |
| 87 | unsigned long adr); |
| 88 | #endif |
| 89 | |
| 90 | |
| 91 | struct sharp_info{ |
| 92 | struct flchip *chip; |
| 93 | int bogus; |
| 94 | int chipshift; |
| 95 | int numchips; |
| 96 | struct flchip chips[1]; |
| 97 | }; |
| 98 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | static void sharp_destroy(struct mtd_info *mtd); |
| 100 | |
| 101 | static struct mtd_chip_driver sharp_chipdrv = { |
| 102 | .probe = sharp_probe, |
| 103 | .destroy = sharp_destroy, |
| 104 | .name = "sharp", |
| 105 | .module = THIS_MODULE |
| 106 | }; |
| 107 | |
| 108 | |
Adrian Bunk | 0500abf | 2006-03-31 02:29:42 -0800 | [diff] [blame] | 109 | static struct mtd_info *sharp_probe(struct map_info *map) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | { |
| 111 | struct mtd_info *mtd = NULL; |
| 112 | struct sharp_info *sharp = NULL; |
| 113 | int width; |
| 114 | |
| 115 | mtd = kmalloc(sizeof(*mtd), GFP_KERNEL); |
| 116 | if(!mtd) |
| 117 | return NULL; |
| 118 | |
| 119 | sharp = kmalloc(sizeof(*sharp), GFP_KERNEL); |
| 120 | if(!sharp) { |
| 121 | kfree(mtd); |
| 122 | return NULL; |
| 123 | } |
| 124 | |
| 125 | memset(mtd, 0, sizeof(*mtd)); |
| 126 | |
| 127 | width = sharp_probe_map(map,mtd); |
| 128 | if(!width){ |
| 129 | kfree(mtd); |
| 130 | kfree(sharp); |
| 131 | return NULL; |
| 132 | } |
| 133 | |
| 134 | mtd->priv = map; |
| 135 | mtd->type = MTD_NORFLASH; |
| 136 | mtd->erase = sharp_erase; |
| 137 | mtd->read = sharp_read; |
| 138 | mtd->write = sharp_write; |
| 139 | mtd->sync = sharp_sync; |
| 140 | mtd->suspend = sharp_suspend; |
| 141 | mtd->resume = sharp_resume; |
| 142 | mtd->flags = MTD_CAP_NORFLASH; |
Artem B. Bityutskiy | 783ed81 | 2006-06-14 19:53:44 +0400 | [diff] [blame] | 143 | mtd->writesize = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | mtd->name = map->name; |
| 145 | |
| 146 | memset(sharp, 0, sizeof(*sharp)); |
| 147 | sharp->chipshift = 23; |
| 148 | sharp->numchips = 1; |
| 149 | sharp->chips[0].start = 0; |
| 150 | sharp->chips[0].state = FL_READY; |
| 151 | sharp->chips[0].mutex = &sharp->chips[0]._spinlock; |
| 152 | sharp->chips[0].word_write_time = 0; |
| 153 | init_waitqueue_head(&sharp->chips[0].wq); |
| 154 | spin_lock_init(&sharp->chips[0]._spinlock); |
| 155 | |
| 156 | map->fldrv = &sharp_chipdrv; |
| 157 | map->fldrv_priv = sharp; |
| 158 | |
| 159 | __module_get(THIS_MODULE); |
| 160 | return mtd; |
| 161 | } |
| 162 | |
Richard Purdie | ee2d49d | 2005-11-29 14:28:31 +0000 | [diff] [blame] | 163 | static inline void sharp_send_cmd(struct map_info *map, unsigned long cmd, unsigned long adr) |
| 164 | { |
| 165 | map_word map_cmd; |
| 166 | map_cmd.x[0] = cmd; |
| 167 | map_write(map, map_cmd, adr); |
| 168 | } |
| 169 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | static int sharp_probe_map(struct map_info *map,struct mtd_info *mtd) |
| 171 | { |
Richard Purdie | ee2d49d | 2005-11-29 14:28:31 +0000 | [diff] [blame] | 172 | map_word tmp, read0, read4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | unsigned long base = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | int width = 4; |
| 175 | |
Richard Purdie | ee2d49d | 2005-11-29 14:28:31 +0000 | [diff] [blame] | 176 | tmp = map_read(map, base+0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | |
Richard Purdie | ee2d49d | 2005-11-29 14:28:31 +0000 | [diff] [blame] | 178 | sharp_send_cmd(map, CMD_READ_ID, base+0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | |
Richard Purdie | ee2d49d | 2005-11-29 14:28:31 +0000 | [diff] [blame] | 180 | read0 = map_read(map, base+0); |
| 181 | read4 = map_read(map, base+4); |
| 182 | if(read0.x[0] == 0x89898989){ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | printk("Looks like sharp flash\n"); |
Richard Purdie | ee2d49d | 2005-11-29 14:28:31 +0000 | [diff] [blame] | 184 | switch(read4.x[0]){ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | case 0xaaaaaaaa: |
| 186 | case 0xa0a0a0a0: |
| 187 | /* aa - LH28F016SCT-L95 2Mx8, 32 64k blocks*/ |
| 188 | /* a0 - LH28F016SCT-Z4 2Mx8, 32 64k blocks*/ |
| 189 | mtd->erasesize = 0x10000 * width; |
| 190 | mtd->size = 0x200000 * width; |
| 191 | return width; |
| 192 | case 0xa6a6a6a6: |
| 193 | /* a6 - LH28F008SCT-L12 1Mx8, 16 64k blocks*/ |
| 194 | /* a6 - LH28F008SCR-L85 1Mx8, 16 64k blocks*/ |
| 195 | mtd->erasesize = 0x10000 * width; |
| 196 | mtd->size = 0x100000 * width; |
| 197 | return width; |
| 198 | #if 0 |
| 199 | case 0x00000000: /* unknown */ |
| 200 | /* XX - LH28F004SCT 512kx8, 8 64k blocks*/ |
| 201 | mtd->erasesize = 0x10000 * width; |
| 202 | mtd->size = 0x80000 * width; |
| 203 | return width; |
| 204 | #endif |
| 205 | default: |
Richard Purdie | ee2d49d | 2005-11-29 14:28:31 +0000 | [diff] [blame] | 206 | printk("Sort-of looks like sharp flash, 0x%08lx 0x%08lx\n", |
| 207 | read0.x[0], read4.x[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | } |
Richard Purdie | ee2d49d | 2005-11-29 14:28:31 +0000 | [diff] [blame] | 209 | }else if((map_read(map, base+0).x[0] == CMD_READ_ID)){ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | /* RAM, probably */ |
| 211 | printk("Looks like RAM\n"); |
Richard Purdie | ee2d49d | 2005-11-29 14:28:31 +0000 | [diff] [blame] | 212 | map_write(map, tmp, base+0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | }else{ |
Richard Purdie | ee2d49d | 2005-11-29 14:28:31 +0000 | [diff] [blame] | 214 | printk("Doesn't look like sharp flash, 0x%08lx 0x%08lx\n", |
| 215 | read0.x[0], read4.x[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | /* This function returns with the chip->mutex lock held. */ |
| 222 | static int sharp_wait(struct map_info *map, struct flchip *chip) |
| 223 | { |
Richard Purdie | ee2d49d | 2005-11-29 14:28:31 +0000 | [diff] [blame] | 224 | int i; |
| 225 | map_word status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | unsigned long timeo = jiffies + HZ; |
| 227 | DECLARE_WAITQUEUE(wait, current); |
| 228 | int adr = 0; |
| 229 | |
| 230 | retry: |
| 231 | spin_lock_bh(chip->mutex); |
| 232 | |
| 233 | switch(chip->state){ |
| 234 | case FL_READY: |
Richard Purdie | ee2d49d | 2005-11-29 14:28:31 +0000 | [diff] [blame] | 235 | sharp_send_cmd(map, CMD_READ_STATUS, adr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | chip->state = FL_STATUS; |
| 237 | case FL_STATUS: |
Todd Poynor | 1da2c9a | 2005-08-02 21:36:09 +0100 | [diff] [blame] | 238 | for(i=0;i<100;i++){ |
Richard Purdie | ee2d49d | 2005-11-29 14:28:31 +0000 | [diff] [blame] | 239 | status = map_read(map, adr); |
| 240 | if((status.x[0] & SR_READY)==SR_READY) |
Todd Poynor | 1da2c9a | 2005-08-02 21:36:09 +0100 | [diff] [blame] | 241 | break; |
| 242 | udelay(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | } |
| 244 | break; |
| 245 | default: |
| 246 | printk("Waiting for chip\n"); |
| 247 | |
| 248 | set_current_state(TASK_INTERRUPTIBLE); |
| 249 | add_wait_queue(&chip->wq, &wait); |
| 250 | |
| 251 | spin_unlock_bh(chip->mutex); |
| 252 | |
| 253 | schedule(); |
| 254 | remove_wait_queue(&chip->wq, &wait); |
| 255 | |
| 256 | if(signal_pending(current)) |
| 257 | return -EINTR; |
| 258 | |
| 259 | timeo = jiffies + HZ; |
| 260 | |
| 261 | goto retry; |
| 262 | } |
| 263 | |
Richard Purdie | ee2d49d | 2005-11-29 14:28:31 +0000 | [diff] [blame] | 264 | sharp_send_cmd(map, CMD_RESET, adr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | |
| 266 | chip->state = FL_READY; |
| 267 | |
| 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | static void sharp_release(struct flchip *chip) |
| 272 | { |
| 273 | wake_up(&chip->wq); |
| 274 | spin_unlock_bh(chip->mutex); |
| 275 | } |
| 276 | |
| 277 | static int sharp_read(struct mtd_info *mtd, loff_t from, size_t len, |
| 278 | size_t *retlen, u_char *buf) |
| 279 | { |
| 280 | struct map_info *map = mtd->priv; |
| 281 | struct sharp_info *sharp = map->fldrv_priv; |
| 282 | int chipnum; |
| 283 | int ret = 0; |
| 284 | int ofs = 0; |
| 285 | |
| 286 | chipnum = (from >> sharp->chipshift); |
| 287 | ofs = from & ((1 << sharp->chipshift)-1); |
| 288 | |
| 289 | *retlen = 0; |
| 290 | |
| 291 | while(len){ |
| 292 | unsigned long thislen; |
| 293 | |
| 294 | if(chipnum>=sharp->numchips) |
| 295 | break; |
| 296 | |
| 297 | thislen = len; |
| 298 | if(ofs+thislen >= (1<<sharp->chipshift)) |
| 299 | thislen = (1<<sharp->chipshift) - ofs; |
| 300 | |
| 301 | ret = sharp_wait(map,&sharp->chips[chipnum]); |
| 302 | if(ret<0) |
| 303 | break; |
| 304 | |
| 305 | map_copy_from(map,buf,ofs,thislen); |
| 306 | |
| 307 | sharp_release(&sharp->chips[chipnum]); |
| 308 | |
| 309 | *retlen += thislen; |
| 310 | len -= thislen; |
| 311 | buf += thislen; |
| 312 | |
| 313 | ofs = 0; |
| 314 | chipnum++; |
| 315 | } |
| 316 | return ret; |
| 317 | } |
| 318 | |
| 319 | static int sharp_write(struct mtd_info *mtd, loff_t to, size_t len, |
| 320 | size_t *retlen, const u_char *buf) |
| 321 | { |
| 322 | struct map_info *map = mtd->priv; |
| 323 | struct sharp_info *sharp = map->fldrv_priv; |
| 324 | int ret = 0; |
| 325 | int i,j; |
| 326 | int chipnum; |
| 327 | unsigned long ofs; |
| 328 | union { u32 l; unsigned char uc[4]; } tbuf; |
| 329 | |
| 330 | *retlen = 0; |
| 331 | |
| 332 | while(len){ |
| 333 | tbuf.l = 0xffffffff; |
| 334 | chipnum = to >> sharp->chipshift; |
| 335 | ofs = to & ((1<<sharp->chipshift)-1); |
| 336 | |
| 337 | j=0; |
| 338 | for(i=ofs&3;i<4 && len;i++){ |
| 339 | tbuf.uc[i] = *buf; |
| 340 | buf++; |
| 341 | to++; |
| 342 | len--; |
| 343 | j++; |
| 344 | } |
| 345 | sharp_write_oneword(map, &sharp->chips[chipnum], ofs&~3, tbuf.l); |
| 346 | if(ret<0) |
| 347 | return ret; |
| 348 | (*retlen)+=j; |
| 349 | } |
| 350 | |
| 351 | return 0; |
| 352 | } |
| 353 | |
| 354 | static int sharp_write_oneword(struct map_info *map, struct flchip *chip, |
| 355 | unsigned long adr, __u32 datum) |
| 356 | { |
| 357 | int ret; |
| 358 | int timeo; |
| 359 | int try; |
| 360 | int i; |
Richard Purdie | ee2d49d | 2005-11-29 14:28:31 +0000 | [diff] [blame] | 361 | map_word data, status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | |
Richard Purdie | ee2d49d | 2005-11-29 14:28:31 +0000 | [diff] [blame] | 363 | status.x[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | ret = sharp_wait(map,chip); |
| 365 | |
| 366 | for(try=0;try<10;try++){ |
Richard Purdie | ee2d49d | 2005-11-29 14:28:31 +0000 | [diff] [blame] | 367 | sharp_send_cmd(map, CMD_BYTE_WRITE, adr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | /* cpu_to_le32 -> hack to fix the writel be->le conversion */ |
Richard Purdie | ee2d49d | 2005-11-29 14:28:31 +0000 | [diff] [blame] | 369 | data.x[0] = cpu_to_le32(datum); |
| 370 | map_write(map, data, adr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | |
| 372 | chip->state = FL_WRITING; |
| 373 | |
| 374 | timeo = jiffies + (HZ/2); |
| 375 | |
Richard Purdie | ee2d49d | 2005-11-29 14:28:31 +0000 | [diff] [blame] | 376 | sharp_send_cmd(map, CMD_READ_STATUS, adr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | for(i=0;i<100;i++){ |
Richard Purdie | ee2d49d | 2005-11-29 14:28:31 +0000 | [diff] [blame] | 378 | status = map_read(map, adr); |
| 379 | if((status.x[0] & SR_READY) == SR_READY) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | break; |
| 381 | } |
| 382 | if(i==100){ |
| 383 | printk("sharp: timed out writing\n"); |
| 384 | } |
| 385 | |
Richard Purdie | ee2d49d | 2005-11-29 14:28:31 +0000 | [diff] [blame] | 386 | if(!(status.x[0] & SR_ERRORS)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | break; |
| 388 | |
Richard Purdie | ee2d49d | 2005-11-29 14:28:31 +0000 | [diff] [blame] | 389 | printk("sharp: error writing byte at addr=%08lx status=%08lx\n", adr, status.x[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | |
Richard Purdie | ee2d49d | 2005-11-29 14:28:31 +0000 | [diff] [blame] | 391 | sharp_send_cmd(map, CMD_CLEAR_STATUS, adr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | } |
Richard Purdie | ee2d49d | 2005-11-29 14:28:31 +0000 | [diff] [blame] | 393 | sharp_send_cmd(map, CMD_RESET, adr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | chip->state = FL_READY; |
| 395 | |
| 396 | wake_up(&chip->wq); |
| 397 | spin_unlock_bh(chip->mutex); |
| 398 | |
| 399 | return 0; |
| 400 | } |
| 401 | |
| 402 | static int sharp_erase(struct mtd_info *mtd, struct erase_info *instr) |
| 403 | { |
| 404 | struct map_info *map = mtd->priv; |
| 405 | struct sharp_info *sharp = map->fldrv_priv; |
| 406 | unsigned long adr,len; |
| 407 | int chipnum, ret=0; |
| 408 | |
| 409 | //printk("sharp_erase()\n"); |
| 410 | if(instr->addr & (mtd->erasesize - 1)) |
| 411 | return -EINVAL; |
| 412 | if(instr->len & (mtd->erasesize - 1)) |
| 413 | return -EINVAL; |
| 414 | if(instr->len + instr->addr > mtd->size) |
| 415 | return -EINVAL; |
| 416 | |
| 417 | chipnum = instr->addr >> sharp->chipshift; |
| 418 | adr = instr->addr & ((1<<sharp->chipshift)-1); |
| 419 | len = instr->len; |
| 420 | |
| 421 | while(len){ |
| 422 | ret = sharp_erase_oneblock(map, &sharp->chips[chipnum], adr); |
| 423 | if(ret)return ret; |
| 424 | |
| 425 | adr += mtd->erasesize; |
| 426 | len -= mtd->erasesize; |
| 427 | if(adr >> sharp->chipshift){ |
| 428 | adr = 0; |
| 429 | chipnum++; |
| 430 | if(chipnum>=sharp->numchips) |
| 431 | break; |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | instr->state = MTD_ERASE_DONE; |
| 436 | mtd_erase_callback(instr); |
| 437 | |
| 438 | return 0; |
| 439 | } |
| 440 | |
| 441 | static int sharp_do_wait_for_ready(struct map_info *map, struct flchip *chip, |
| 442 | unsigned long adr) |
| 443 | { |
| 444 | int ret; |
| 445 | unsigned long timeo; |
Richard Purdie | ee2d49d | 2005-11-29 14:28:31 +0000 | [diff] [blame] | 446 | map_word status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | DECLARE_WAITQUEUE(wait, current); |
| 448 | |
Richard Purdie | ee2d49d | 2005-11-29 14:28:31 +0000 | [diff] [blame] | 449 | sharp_send_cmd(map, CMD_READ_STATUS, adr); |
| 450 | status = map_read(map, adr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | |
| 452 | timeo = jiffies + HZ; |
| 453 | |
| 454 | while(time_before(jiffies, timeo)){ |
Richard Purdie | ee2d49d | 2005-11-29 14:28:31 +0000 | [diff] [blame] | 455 | sharp_send_cmd(map, CMD_READ_STATUS, adr); |
| 456 | status = map_read(map, adr); |
| 457 | if((status.x[0] & SR_READY)==SR_READY){ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | ret = 0; |
| 459 | goto out; |
| 460 | } |
| 461 | set_current_state(TASK_INTERRUPTIBLE); |
| 462 | add_wait_queue(&chip->wq, &wait); |
| 463 | |
| 464 | //spin_unlock_bh(chip->mutex); |
| 465 | |
| 466 | schedule_timeout(1); |
| 467 | schedule(); |
| 468 | remove_wait_queue(&chip->wq, &wait); |
| 469 | |
| 470 | //spin_lock_bh(chip->mutex); |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 471 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | if (signal_pending(current)){ |
| 473 | ret = -EINTR; |
| 474 | goto out; |
| 475 | } |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 476 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | } |
| 478 | ret = -ETIME; |
| 479 | out: |
| 480 | return ret; |
| 481 | } |
| 482 | |
| 483 | static int sharp_erase_oneblock(struct map_info *map, struct flchip *chip, |
| 484 | unsigned long adr) |
| 485 | { |
| 486 | int ret; |
| 487 | //int timeo; |
Richard Purdie | ee2d49d | 2005-11-29 14:28:31 +0000 | [diff] [blame] | 488 | map_word status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | //int i; |
| 490 | |
| 491 | //printk("sharp_erase_oneblock()\n"); |
| 492 | |
| 493 | #ifdef AUTOUNLOCK |
| 494 | /* This seems like a good place to do an unlock */ |
| 495 | sharp_unlock_oneblock(map,chip,adr); |
| 496 | #endif |
| 497 | |
Richard Purdie | ee2d49d | 2005-11-29 14:28:31 +0000 | [diff] [blame] | 498 | sharp_send_cmd(map, CMD_BLOCK_ERASE_1, adr); |
| 499 | sharp_send_cmd(map, CMD_BLOCK_ERASE_2, adr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | |
| 501 | chip->state = FL_ERASING; |
| 502 | |
| 503 | ret = sharp_do_wait_for_ready(map,chip,adr); |
| 504 | if(ret<0)return ret; |
| 505 | |
Richard Purdie | ee2d49d | 2005-11-29 14:28:31 +0000 | [diff] [blame] | 506 | sharp_send_cmd(map, CMD_READ_STATUS, adr); |
| 507 | status = map_read(map, adr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | |
Richard Purdie | ee2d49d | 2005-11-29 14:28:31 +0000 | [diff] [blame] | 509 | if(!(status.x[0] & SR_ERRORS)){ |
| 510 | sharp_send_cmd(map, CMD_RESET, adr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | chip->state = FL_READY; |
| 512 | //spin_unlock_bh(chip->mutex); |
| 513 | return 0; |
| 514 | } |
| 515 | |
Richard Purdie | ee2d49d | 2005-11-29 14:28:31 +0000 | [diff] [blame] | 516 | printk("sharp: error erasing block at addr=%08lx status=%08lx\n", adr, status.x[0]); |
| 517 | sharp_send_cmd(map, CMD_CLEAR_STATUS, adr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | |
| 519 | //spin_unlock_bh(chip->mutex); |
| 520 | |
| 521 | return -EIO; |
| 522 | } |
| 523 | |
| 524 | #ifdef AUTOUNLOCK |
| 525 | static void sharp_unlock_oneblock(struct map_info *map, struct flchip *chip, |
| 526 | unsigned long adr) |
| 527 | { |
| 528 | int i; |
Richard Purdie | ee2d49d | 2005-11-29 14:28:31 +0000 | [diff] [blame] | 529 | map_word status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | |
Richard Purdie | ee2d49d | 2005-11-29 14:28:31 +0000 | [diff] [blame] | 531 | sharp_send_cmd(map, CMD_CLEAR_BLOCK_LOCKS_1, adr); |
| 532 | sharp_send_cmd(map, CMD_CLEAR_BLOCK_LOCKS_2, adr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | |
| 534 | udelay(100); |
| 535 | |
Richard Purdie | ee2d49d | 2005-11-29 14:28:31 +0000 | [diff] [blame] | 536 | status = map_read(map, adr); |
| 537 | printk("status=%08lx\n", status.x[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | |
| 539 | for(i=0;i<1000;i++){ |
Richard Purdie | ee2d49d | 2005-11-29 14:28:31 +0000 | [diff] [blame] | 540 | //sharp_send_cmd(map, CMD_READ_STATUS, adr); |
| 541 | status = map_read(map, adr); |
| 542 | if((status.x[0] & SR_READY) == SR_READY) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | break; |
| 544 | udelay(100); |
| 545 | } |
| 546 | if(i==1000){ |
| 547 | printk("sharp: timed out unlocking block\n"); |
| 548 | } |
| 549 | |
Richard Purdie | ee2d49d | 2005-11-29 14:28:31 +0000 | [diff] [blame] | 550 | if(!(status.x[0] & SR_ERRORS)){ |
| 551 | sharp_send_cmd(map, CMD_RESET, adr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | chip->state = FL_READY; |
| 553 | return; |
| 554 | } |
| 555 | |
Richard Purdie | ee2d49d | 2005-11-29 14:28:31 +0000 | [diff] [blame] | 556 | printk("sharp: error unlocking block at addr=%08lx status=%08lx\n", adr, status.x[0]); |
| 557 | sharp_send_cmd(map, CMD_CLEAR_STATUS, adr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | } |
| 559 | #endif |
| 560 | |
| 561 | static void sharp_sync(struct mtd_info *mtd) |
| 562 | { |
| 563 | //printk("sharp_sync()\n"); |
| 564 | } |
| 565 | |
| 566 | static int sharp_suspend(struct mtd_info *mtd) |
| 567 | { |
| 568 | printk("sharp_suspend()\n"); |
| 569 | return -EINVAL; |
| 570 | } |
| 571 | |
| 572 | static void sharp_resume(struct mtd_info *mtd) |
| 573 | { |
| 574 | printk("sharp_resume()\n"); |
Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 575 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | static void sharp_destroy(struct mtd_info *mtd) |
| 579 | { |
| 580 | printk("sharp_destroy()\n"); |
| 581 | |
| 582 | } |
| 583 | |
Adrian Bunk | 0500abf | 2006-03-31 02:29:42 -0800 | [diff] [blame] | 584 | static int __init sharp_probe_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | { |
| 586 | printk("MTD Sharp chip driver <ds@lineo.com>\n"); |
| 587 | |
| 588 | register_mtd_chip_driver(&sharp_chipdrv); |
| 589 | |
| 590 | return 0; |
| 591 | } |
| 592 | |
| 593 | static void __exit sharp_probe_exit(void) |
| 594 | { |
| 595 | unregister_mtd_chip_driver(&sharp_chipdrv); |
| 596 | } |
| 597 | |
| 598 | module_init(sharp_probe_init); |
| 599 | module_exit(sharp_probe_exit); |
| 600 | |
| 601 | |
| 602 | MODULE_LICENSE("GPL"); |
| 603 | MODULE_AUTHOR("David Schleef <ds@schleef.org>"); |
| 604 | MODULE_DESCRIPTION("Old MTD chip driver for pre-CFI Sharp flash chips"); |