OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [tags/] [before_ORP/] [uclinux/] [uClinux-2.0.x/] [drivers/] [block/] [flash.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 199 simons
/* $Id: flash.c,v 1.1.1.1 2001-09-10 07:44:10 simons Exp $
2
 *
3
 * Flash-memory char or block device
4
 *
5
 * Copyright (c) 1998, 1999 Bjorn Wesen, Axis Communications AB
6
 *
7
 * This driver implements a read/write/erase interface to the flash chips.
8
 * It probes for chips, and tries to read a bootblock with partition info from
9
 * the bootblock on the first chip. This bootblock is given the device name
10
 * /dev/flash0, and the rest of the partitions are given flash1 and upwards.
11
 * If no bootblock is found, the entire first chip (including bootblock) is
12
 * set as /dev/flash1.
13
 *
14
 * TODO:
15
 *   implement Unlock Bypass programming, to double the write speed
16
 *   fix various loops for partitions than span more than one chip
17
 */
18
 
19
/* define to make kernel bigger, but with verbose Flash messages */
20
 
21
//#define LISAHACK
22
 
23
/* if IRQ_LOCKS is enabled, interrupts are disabled while doing things
24
 * with the flash that cant have other accesses to the flash at the same
25
 * time. this is normally not needed since we have a wait_queue lock on the
26
 * flash chip as a mutex.
27
 */
28
 
29
//#define IRQ_LOCKS
30
 
31
#define FLASH_VERBOSE
32
#define MEM_NON_CACHEABLE 0
33
#define FLASH_16BIT
34
 
35
/* ROM and flash devices have major 31 (Documentation/devices.txt)
36
 * However, KROM is using that major now, so we use an experimental major = 60.
37
 * When this driver works, it will support read-only memory as well and we can
38
 * remove KROM.
39
 * Our minors start at: 16 = /dev/flash0       First flash memory card (rw)
40
 */
41
 
42
/*#define MAJOR_NR 31 */
43
#define MAJOR_NR 60
44
 
45
#define ROM_MINOR   8   /* ro ROM card */
46
#define FLASH_MINOR 16  /* rw Flash card */
47
 
48
/* dont touch the MAX_CHIPS until the hardcoded chip0 and chip1 are removed from the code */
49
 
50
#define MAX_CHIPS /* 2 */ 1
51
#define MAX_PARTITIONS 8
52
 
53
#define DEF_FLASH2_SIZE 0x50000   /* 5 sectors for JFFS partition per default */
54
 
55
#include <linux/config.h>
56
#include <linux/major.h>
57
#include <linux/malloc.h>
58
 
59
/* all this stuff needs to go in blk.h */
60
#define DEVICE_NAME "Flash/ROM device"
61
 
62
#ifdef CONFIG_BLK_DEV_FLASH
63
 
64
#define FLASH_SECTSIZE 512 /* arbitrary, but Linux assumes 512 often it seems */
65
 
66
#define DEVICE_REQUEST do_flash_request
67
#define DEVICE_NR(device) (MINOR(device))
68
#define DEVICE_ON(device)
69
#define DEVICE_OFF(device)
70
 
71
#include <linux/blk.h>
72
 
73
/* the device and block sizes for each minor */
74
 
75
static int flash_sizes[32];
76
static int flash_blk_sizes[32];
77
 
78
#endif
79
 
80
#include <asm/system.h>
81
#include <linux/flash.h>
82
 
83
//#define DEBUG
84
 
85
#ifdef DEBUG
86
#define FDEBUG(x) x
87
#else
88
#define FDEBUG(x)
89
#endif
90
 
91
#if defined(__CRIS__) && !defined(DEBUG)
92
/* breaks badly if we use printk before we flash an image... */
93
extern void console_print_etrax(const char *b, ...);
94
#define safe_printk console_print_etrax
95
#else
96
#define safe_printk printk
97
#endif
98
 
99
int flash_write(unsigned char *ptr, const unsigned char *source, unsigned int size);
100
void flash_init_erase(unsigned char *ptr, unsigned int size);
101
static struct flashchip *getchip(unsigned char *ptr);
102
 
103
#ifdef FLASH_16BIT
104
/* 16-bit wide Flash-ROM */
105
enum {  unlockAddress1          = 0x555,
106
        unlockData1             = 0xAA,
107
        unlockAddress2          = 0x2AA,
108
        unlockData2             = 0x55,
109
        manufacturerUnlockData  = 0x90,
110
        manufacturerAddress     = 0x00,
111
        deviceIdAddress         = 0x01,
112
        programUnlockData       = 0xA0,
113
        resetData               = 0xF0,
114
        sectorEraseUnlockData   = 0x80,
115
        sectorEraseUnlockData2  = 0x30 };
116
 
117
typedef volatile unsigned short *flashptr;
118
 
119
#else
120
/* 32-bit wide Flash-ROM
121
 * Since we have two devices in parallell, we need to duplicate
122
 * the special _data_ below so it reaches both chips.
123
 */
124
enum {  unlockAddress1          = 0x555,
125
        unlockData1             = 0x00AA00AA,
126
        unlockAddress2          = 0x2AA,
127
        unlockData2             = 0x00550055,
128
        manufacturerUnlockData  = 0x00900090,
129
        manufacturerAddress     = 0x00,
130
        deviceIdAddress         = 0x01,
131
        programUnlockData       = 0x00A000A0,
132
        resetData               = 0x00F000F0,
133
        sectorEraseUnlockData   = 0x00800080,
134
        sectorEraseUnlockData2  = 0x00300030 };
135
 
136
typedef volatile unsigned long *flashptr;
137
 
138
#endif
139
 
140
enum {  ManufacturerAMD   = 0x01,
141
        AM29F800BB        = 0x2258,
142
        AM29F800BT        = 0x22D6,
143
        AM29LV800BB       = 0x225B,
144
        AM29LV800BT       = 0x22DA,
145
        AM29LV160BT       = 0x22C4
146
};
147
 
148
enum {  ManufacturerToshiba = 0x0098,
149
        TC58FVT160          = 0x00c2,
150
        TC58FVB160          = 0x0043
151
};
152
 
153
enum {
154
        /* ST - www.st.com*/
155
        ManufacturerST    = 0x20, /* 0x20 */
156
        M29W800T          = 0x00D7 /* Used in 5600, similar to AM29LV800,
157
                                    * but no unlock bypass */
158
};
159
 
160
enum {  D6_MASK           = 0x40};
161
 
162
enum {  maxNumberOfBootSectors = 8 };
163
 
164
/* internal structure for keeping track of flash devices */
165
 
166
struct flashchip {
167
        unsigned char *start;
168
        unsigned char *bootsector;
169
        unsigned int bootsectorsize[maxNumberOfBootSectors];
170
        unsigned short device_id;
171
        unsigned short isValid;
172
        int size, sectorsize;
173
        /* to block accesses during erase and writing etc */
174
        int busy;
175
        struct wait_queue *wqueue;
176
};
177
 
178
/* and partitions */
179
 
180
struct flashpartition {
181
        struct flashchip *chip;   /* the chip we reside in */
182
        unsigned char *start;     /* starting flash mem address */
183
        int size;                 /* size of partition in bytes */
184
        unsigned int flags;       /* protection bits etc */
185
};
186
 
187
 
188
static struct flashchip chips[MAX_CHIPS];
189
static struct flashpartition partitions[MAX_PARTITIONS];
190
 
191
/* check if flash is busy */
192
 
193
static inline int flash_is_busy(flashptr flashStart)
194
{
195
        /* this should probably be protected! */
196
        return((*flashStart & D6_MASK) !=
197
               (*flashStart & D6_MASK));
198
}
199
 
200
/* Open the device. We don't need to do anything really.  */
201
 
202
static int
203
flash_open(struct inode *inode, struct file *filp)
204
{
205
#ifdef CONFIG_CHR_DEV_FLASH
206
 
207
        int minor = MINOR(inode->i_rdev);
208
        struct flashpartition *part;
209
 
210
        if(minor < FLASH_MINOR)
211
                return -ENODEV;
212
 
213
        part = &partitions[minor - FLASH_MINOR];
214
 
215
        if(!part->start)
216
                return -ENODEV;
217
 
218
        filp->private_data = (void *)part;  /* remember for the future */
219
 
220
#endif
221
 
222
        return 0; /* everything went ok */
223
}
224
 
225
static void
226
flash_release(struct inode *inode, struct file *filp)
227
{
228
#ifdef CONFIG_BLK_DEV_FLASH
229
        sync_dev(inode->i_rdev);
230
#endif
231
        return;
232
}
233
 
234
#ifdef CONFIG_BLK_DEV_FLASH
235
 
236
static void
237
do_flash_request()
238
{
239
        while(1) {
240
                int minor, fsize, opsize;
241
                struct flashchip *chip;
242
                struct flashpartition *part;
243
                unsigned char *fptr;
244
                unsigned long flags;
245
 
246
                INIT_REQUEST;
247
 
248
                minor = DEVICE_NR(CURRENT_DEV);
249
 
250
                minor -= FLASH_MINOR;
251
 
252
                /* for now, just handle requests to the flash minors */
253
 
254
                if(minor < 0 || minor >= MAX_PARTITIONS ||
255
                   !partitions[minor].start) {
256
                        printk(KERN_WARNING "flash: bad minor %d.", minor);
257
                        end_request(0);
258
                        continue;
259
                }
260
 
261
                part = partitions + minor;
262
 
263
                /* get the actual memory address of the sectors requested */
264
 
265
                fptr = part->start + CURRENT->sector * FLASH_SECTSIZE;
266
                fsize = CURRENT->current_nr_sectors * FLASH_SECTSIZE;
267
 
268
                /* check so it's not totally out of bounds */
269
 
270
                if(fptr + fsize > part->start + part->size) {
271
                        printk(KERN_WARNING "flash: request past end "
272
                               "of partition\n");
273
                        end_request(0);
274
                        continue;
275
                }
276
 
277
                /* actually do something, but get a lock on the chip first.
278
                 * since the partition might span several chips, we need to
279
                 * loop and lock each chip in turn
280
                 */
281
 
282
                while(fsize > 0) {
283
 
284
                        chip = getchip(fptr);
285
 
286
                        /* how much fits in this chip ? */
287
 
288
                        opsize = (fptr + fsize) > (chip->start + chip->size) ?
289
                                 (chip->start + chip->size - fptr) : fsize;
290
 
291
                        /* lock the chip */
292
 
293
                        save_flags(flags);
294
                        cli();
295
                        while(chip->busy)
296
                                sleep_on(&chip->wqueue);
297
                        chip->busy = 1;
298
                        restore_flags(flags);
299
 
300
                        switch(CURRENT->cmd) {
301
                        case READ:
302
                                memcpy(CURRENT->buffer, fptr, opsize);
303
                                FDEBUG(printk("flash read from %p to %p "
304
                                              "size %d\n", fptr,
305
                                              CURRENT->buffer, opsize));
306
                                break;
307
                        case WRITE:
308
                                FDEBUG(printk("flash write block at 0x%p\n",
309
                                              fptr));
310
                                flash_write(fptr,
311
                                            (unsigned char *)
312
                                            CURRENT->buffer,
313
                                            opsize);
314
                                break;
315
                        default:
316
                                /* Shouldn't happen.  */
317
                                chip->busy = 0;
318
                                wake_up(&chip->wqueue);
319
                                end_request(0);
320
                                continue;
321
                        }
322
 
323
                        /* release the lock */
324
 
325
                        chip->busy = 0;
326
                        wake_up(&chip->wqueue);
327
 
328
                        /* see if there is anything left to write in the next chip */
329
 
330
                        fsize -= opsize;
331
                        fptr += opsize;
332
                }
333
 
334
                /* We have a liftoff! */
335
 
336
                end_request(1);
337
        }
338
}
339
 
340
#endif /* CONFIG_BLK_DEV_FLASH */
341
 
342
void
343
flash_safe_acquire(void *part)
344
{
345
        struct flashchip *chip = ((struct flashpartition *)part)->chip;
346
        while (chip->busy)
347
                sleep_on(&chip->wqueue);
348
        chip->busy = 1;
349
}
350
 
351
void
352
flash_safe_release(void *part)
353
{
354
        struct flashchip *chip = ((struct flashpartition *)part)->chip;
355
        chip->busy = 0;
356
        wake_up(&chip->wqueue);
357
}
358
 
359
/* flash_safe_read and flash_safe_write are used by the JFFS flash filesystem */
360
 
361
int
362
flash_safe_read(void *_part, unsigned char *fptr,
363
                unsigned char *buf, int count)
364
{
365
        struct flashpartition *part = (struct flashpartition *)_part;
366
 
367
        /* Check so it's not totally out of bounds.  */
368
 
369
        if(fptr + count > part->start + part->size) {
370
                printk(KERN_WARNING "flash: read request past "
371
                       "end of device (address: 0x%p, size: %d)\n",
372
                       fptr, count);
373
                return -EINVAL;
374
        }
375
 
376
        FDEBUG(printk("flash_safe_read: %d bytes from 0x%p to 0x%p\n",
377
                      count, fptr, buf));
378
 
379
        /* Actually do something, but get a lock on the chip first.  */
380
 
381
        flash_safe_acquire(part);
382
 
383
        memcpy(buf, fptr, count);
384
 
385
        /* Release the lock.  */
386
 
387
        flash_safe_release(part);
388
 
389
        return count; /* success */
390
}
391
 
392
 
393
int
394
flash_safe_write(void *_part, unsigned char *fptr,
395
                 const unsigned char *buf, int count)
396
{
397
        struct flashpartition *part = (struct flashpartition *)_part;
398
        int err;
399
 
400
        /* Check so it's not totally out of bounds.  */
401
 
402
        if(fptr + count > part->start + part->size) {
403
                printk(KERN_WARNING "flash: write operation past "
404
                       "end of device (address: 0x%p, size: %d)\n",
405
                       fptr, count);
406
                return -EINVAL;
407
        }
408
 
409
        FDEBUG(printk("flash_safe_write: %d bytes from 0x%p to 0x%p\n",
410
                      count, buf, fptr));
411
 
412
        /* Actually do something, but get a lock on the chip first.  */
413
 
414
        flash_safe_acquire(part);
415
 
416
        if ((err = flash_write(fptr, buf, count)) < 0) {
417
                count = err;
418
        }
419
 
420
        /* Release the lock.  */
421
 
422
        flash_safe_release(part);
423
 
424
        return count; /* success */
425
}
426
 
427
 
428
#ifdef CONFIG_CHR_DEV_FLASH
429
 
430
static int
431
flash_char_read(struct inode *inode, struct file *filp,
432
                char *buf, int count)
433
{
434
        int rlen;
435
        struct flashpartition *part = (struct flashpartition *)filp->private_data;
436
 
437
        FDEBUG(printk("flash_char_read\n"));
438
        rlen = flash_safe_read(part,
439
                               (unsigned char *)part->start + filp->f_pos,
440
                               (unsigned char *)buf, count);
441
 
442
        /* advance file position pointer */
443
 
444
        if(rlen >= 0)
445
                filp->f_pos += rlen;
446
 
447
        return rlen;
448
}
449
 
450
static int
451
flash_char_write(struct inode *inode, struct file *filp,
452
                 const char *buf, int count)
453
{
454
        int wlen;
455
        struct flashpartition *part = (struct flashpartition *)filp->private_data;
456
 
457
        FDEBUG(printk("flash_char_write\n"));
458
        wlen = flash_safe_write(part,
459
                                (unsigned char *)part->start + filp->f_pos,
460
                                (unsigned char *)buf, count);
461
 
462
        /* advance file position pointer */
463
 
464
        if(wlen >= 0)
465
                filp->f_pos += wlen;
466
 
467
        return wlen;
468
}
469
 
470
#endif /* CONFIG_CHR_DEV_FLASH */
471
 
472
 
473
static int
474
flash_ioctl(struct inode *inode, struct file *file,
475
            unsigned int cmd, unsigned long arg)
476
{
477
        int minor;
478
        struct flashpartition *part;
479
 
480
        if (!inode || !inode->i_rdev)
481
                return -EINVAL;
482
 
483
        minor = MINOR(inode->i_rdev);
484
 
485
        if(minor < FLASH_MINOR)
486
                return -EINVAL; /* only ioctl's for flash devices */
487
 
488
        part = &partitions[minor - FLASH_MINOR];
489
 
490
        if(!part->start)
491
                return -EINVAL;
492
 
493
        switch(cmd) {
494
        case FLASHIO_ERASEALL:
495
                FDEBUG(printk("flash_ioctl(): Got FLASHIO_ERASEALL request.\n"));
496
 
497
                if(!suser())
498
                        return -EACCES;
499
 
500
                /* Invalidate all pages and buffers */
501
 
502
                invalidate_inodes(inode->i_rdev);
503
                invalidate_buffers(inode->i_rdev);
504
 
505
                /*
506
                 * Start the erasure, then sleep and wake up now and
507
                 * then to see if it's done. We use the waitqueue to
508
                 * make sure we don't start erasing in the middle of
509
                 * a write, or that nobody start using the flash while
510
                 * we're erasing.
511
                 *
512
                 * TODO: break up partition erases that spans more than one
513
                 *       chip.
514
                 */
515
 
516
                flash_safe_acquire(part);
517
 
518
                flash_init_erase(part->start, part->size);
519
 
520
                while(flash_is_busy((flashptr)part->chip->start)) {
521
                        current->state = TASK_INTERRUPTIBLE;
522
                        current->timeout = jiffies + HZ / 2;
523
                        schedule();
524
                }
525
 
526
                flash_safe_release(part);
527
 
528
                return 0;
529
 
530
        default:
531
                return -EPERM;
532
        }
533
 
534
        return -EPERM;
535
}
536
 
537
/* probe for Flash RAM's - this isn't in the init function, because this needs
538
 * to be done really early in the boot, so we can use the device to burn an
539
 * image before the system is running.
540
 */
541
 
542
void
543
flash_probe()
544
{
545
        int i;
546
 
547
        /* start adresses for the Flash chips - these should really
548
         * be settable in some other way.
549
         */
550
#ifdef FLASH_VERBOSE
551
        safe_printk("Probing flash...\n");
552
#endif
553
 
554
#if 0
555
        chips[0].start = (unsigned char *)(MEM_CSE0_START | MEM_NON_CACHEABLE);
556
        chips[1].start = (unsigned char *)(MEM_CSE1_START | MEM_NON_CACHEABLE);
557
#else
558
        chips[0].start = (unsigned char *)(0x10c00000);
559
        chips[1].start = (unsigned char *)(0x0);
560
#endif
561
        for(i = 0; i < MAX_CHIPS; i++) {
562
                struct flashchip *chip = chips + i;
563
                flashptr flashStart = (flashptr)chip->start;
564
                unsigned short manu;
565
 
566
#ifdef CONFIG_SVINTO_SIM
567
                /* in the simulator, dont trash the flash ram by writing unlocks */
568
                chip->isValid = 1;
569
                chip->device_id = AM29LV160BT;
570
#else
571
                /* reset */
572
 
573
                flashStart[unlockAddress1] = unlockData1;
574
                flashStart[unlockAddress2] = unlockData2;
575
                flashStart[unlockAddress1] = resetData;
576
 
577
                /* read manufacturer */
578
 
579
                flashStart[unlockAddress1] = unlockData1;
580
                flashStart[unlockAddress2] = unlockData2;
581
                flashStart[unlockAddress1] = manufacturerUnlockData;
582
                manu = flashStart[manufacturerAddress];
583
                chip->isValid = (manu == ManufacturerAMD ||
584
                                 manu == ManufacturerToshiba ||
585
                                 manu == ManufacturerST);
586
 
587
                if(!chip->isValid) {
588
#ifdef FLASH_VERBOSE
589
                        safe_printk("Flash: No flash or unsupported "
590
                                    "manufacturer.\n", i);
591
#endif
592
                        continue;
593
                }
594
 
595
                /* reset */
596
 
597
                flashStart[unlockAddress1] = unlockData1;
598
                flashStart[unlockAddress2] = unlockData2;
599
                flashStart[unlockAddress1] = resetData;
600
 
601
                /* read device id */
602
 
603
                flashStart[unlockAddress1] = unlockData1;
604
                flashStart[unlockAddress2] = unlockData2;
605
                flashStart[unlockAddress1] = manufacturerUnlockData;
606
                chip->device_id = flashStart[deviceIdAddress];
607
 
608
                /* reset */
609
 
610
                flashStart[unlockAddress1] = unlockData1;
611
                flashStart[unlockAddress2] = unlockData2;
612
                flashStart[unlockAddress1] = resetData;
613
#endif
614
 
615
                /* check device type and fill in correct sizes etc */
616
 
617
                switch(chip->device_id) {
618
                case AM29LV160BT:
619
                case TC58FVT160:
620
#ifdef FLASH_VERBOSE
621
                        safe_printk("Flash: 16Mb TB.\n");
622
#endif
623
                        chip->size = 0x00200000;
624
                        chip->sectorsize = 0x10000;
625
                        chip->bootsector = chip->start + chip->size
626
                                           - chip->sectorsize;
627
                        chip->bootsectorsize[0] = 0x8000;
628
                        chip->bootsectorsize[1] = 0x2000;
629
                        chip->bootsectorsize[2] = 0x2000;
630
                        chip->bootsectorsize[3] = 0x4000;
631
                        break;
632
                //case AM29LV160BB:
633
                case TC58FVB160:
634
#ifdef FLASH_VERBOSE
635
                        safe_printk("Flash: 16Mb BB.\n");
636
#endif
637
                        chip->size = 0x00200000;
638
                        chip->sectorsize = 0x10000;
639
                        chip->bootsector = chip->start;
640
                        chip->bootsectorsize[0] = 0x4000;
641
                        chip->bootsectorsize[1] = 0x2000;
642
                        chip->bootsectorsize[2] = 0x2000;
643
                        chip->bootsectorsize[3] = 0x8000;
644
                        break;
645
                case AM29LV800BB:
646
                case AM29F800BB:
647
#ifdef FLASH_VERBOSE
648
                        safe_printk("Flash: 8Mb BB.\n");
649
#endif
650
                        chip->size = 0x00100000;
651
                        chip->sectorsize = 0x10000;
652
                        chip->bootsector = chip->start;
653
                        chip->bootsectorsize[0] = 0x4000;
654
                        chip->bootsectorsize[1] = 0x2000;
655
                        chip->bootsectorsize[2] = 0x2000;
656
                        chip->bootsectorsize[3] = 0x8000;
657
                        break;
658
                case M29W800T:
659
                case AM29LV800BT:
660
                case AM29F800BT:
661
#ifdef FLASH_VERBOSE
662
                        safe_printk("Flash: 8Mb TB.\n");
663
#endif
664
                        chip->size = 0x00100000;
665
                        chip->sectorsize = 0x10000;
666
                        chip->bootsector = chip->start + chip->size
667
                                           - chip->sectorsize;
668
                        chip->bootsectorsize[0] = 0x8000;
669
                        chip->bootsectorsize[1] = 0x2000;
670
                        chip->bootsectorsize[2] = 0x2000;
671
                        chip->bootsectorsize[3] = 0x4000;
672
                        break;
673
                        //     case AM29LV800BB:
674
                default:
675
#ifdef FLASH_VERBOSE
676
                        safe_printk("Flash: Unknown device.\n");
677
#endif
678
                        chip->isValid = 0;
679
                        break;
680
                }
681
 
682
                chip->busy = 0;
683
                init_waitqueue(&chip->wqueue);
684
        }
685
}
686
 
687
/* locate the flashchip structure associated with the given adress */
688
 
689
static struct flashchip *
690
getchip(unsigned char *ptr)
691
{
692
        int i;
693
        for(i = 0; i < MAX_CHIPS; i++) {
694
                if(ptr >= chips[i].start &&
695
                   ptr < (chips[i].start + chips[i].size))
696
                        return &chips[i];
697
        }
698
        FDEBUG(printk("Illegal adress in Flash: getchip(0x%p)!\n", ptr));
699
        return (void *)0;
700
}
701
 
702
void *
703
flash_getpart(kdev_t dev)
704
{
705
        struct flashpartition *part;
706
 
707
        if (MINOR(dev) < FLASH_MINOR) {
708
                return 0;
709
        }
710
 
711
        part = &partitions[MINOR(dev) - FLASH_MINOR];
712
 
713
        if (!part->start) {
714
                return 0;
715
        }
716
 
717
        return (void *)part;
718
}
719
 
720
 
721
unsigned char *
722
flash_get_direct_pointer(kdev_t dev, __u32 offset)
723
{
724
        struct flashpartition *part;
725
 
726
        if (MINOR(dev) < FLASH_MINOR) {
727
                return 0;
728
        }
729
 
730
        part = &partitions[MINOR(dev) - FLASH_MINOR];
731
 
732
        if (!part->start) {
733
                return 0;
734
        }
735
 
736
        return (unsigned char *) ((__u32) part->start + offset);
737
}
738
 
739
 
740
/* start erasing flash-memory at ptr of a certain size
741
 * this does not wait until the erasing is done
742
 */
743
 
744
void
745
flash_init_erase(unsigned char *ptr, unsigned int size)
746
{
747
        struct flashchip *chip;
748
        int bootSectorCounter = 0;
749
        unsigned int erasedSize = 0;
750
        flashptr flashStart;
751
#ifdef IRQ_LOCKS
752
        unsigned long flags;
753
#endif
754
 
755
        ptr = (unsigned char *)((unsigned long)ptr | MEM_NON_CACHEABLE);
756
        chip = getchip(ptr);
757
        flashStart = (flashptr)chip->start;
758
 
759
        FDEBUG(safe_printk("Flash: erasing memory at 0x%p, size 0x%x.\n", ptr, size));
760
 
761
        /* need to disable interrupts, to avoid possible delays between the
762
         * unlocking and erase-init
763
         */
764
 
765
#ifdef IRQ_LOCKS
766
        save_flags(flags);
767
        cli();
768
#endif
769
 
770
        /* Init erasing of the number of sectors needed */
771
 
772
        flashStart[unlockAddress1] = unlockData1;
773
        flashStart[unlockAddress2] = unlockData2;
774
        flashStart[unlockAddress1] = sectorEraseUnlockData;
775
        flashStart[unlockAddress1] = unlockData1;
776
        flashStart[unlockAddress2] = unlockData2;
777
 
778
        while(erasedSize < size) {
779
                *(flashptr)ptr = sectorEraseUnlockData2;
780
 
781
                /* make sure we erase the individual bootsectors if in that area */
782
                /* TODO this BREAKS if we start erasing in the middle of the bootblock! */
783
 
784
                if(ptr < chip->bootsector || ptr >= (chip->bootsector +
785
                                                     chip->sectorsize)) {
786
                        erasedSize += chip->sectorsize;
787
                        ptr += chip->sectorsize;
788
                } else {
789
                        erasedSize += chip->bootsectorsize[bootSectorCounter];
790
                        ptr += chip->bootsectorsize[bootSectorCounter++];
791
                }
792
        }
793
 
794
#ifdef IRQ_LOCKS
795
        restore_flags(flags);
796
#endif
797
        /* give the busy signal time enough to activate (tBusy, 90 ns) */
798
 
799
        nop(); nop(); nop(); nop(); nop(); nop(); nop(); nop(); nop();
800
}
801
 
802
 
803
int
804
flash_erase_region(kdev_t dev, __u32 offset, __u32 size)
805
{
806
        int minor;
807
        struct flashpartition *part;
808
        unsigned char *erase_start;
809
        short retries = 5;
810
        short success;
811
 
812
        minor = MINOR(dev);
813
        if (minor < FLASH_MINOR) {
814
                return -EINVAL;
815
        }
816
 
817
        part = &partitions[minor - FLASH_MINOR];
818
        if (!part->start) {
819
                return -EINVAL;
820
        }
821
 
822
        /* Start the erasure, then sleep and wake up now and then to see
823
         * if it's done.
824
         */
825
 
826
        erase_start = part->start + offset;
827
 
828
        flash_safe_acquire(part);
829
        do {
830
                flash_init_erase(erase_start, size);
831
 
832
                while (flash_is_busy((flashptr)part->chip->start)) {
833
                        current->state = TASK_INTERRUPTIBLE;
834
                        current->timeout = jiffies + HZ / 2;
835
                        schedule();
836
                }
837
 
838
                success = ((flashptr)erase_start)[0] == 0xffff
839
                          && ((flashptr)erase_start)[1] == 0xffff
840
                          && ((flashptr)erase_start)[2] == 0xffff
841
                          && ((flashptr)erase_start)[3] == 0xffff;
842
 
843
                if (!success) {
844
                        printk(KERN_NOTICE "flash: erase of region "
845
                               "[0x%p, 0x%p] failed once\n",
846
                               erase_start, erase_start + size);
847
                }
848
 
849
        } while (retries-- && !success);
850
 
851
        flash_safe_release(part);
852
 
853
        if (retries == 0 && !success) {
854
                printk(KERN_WARNING "flash: erase of region "
855
                               "[0x%p, 0x%p] totally failed\n",
856
                               erase_start, erase_start + size);
857
                return -1;
858
        }
859
 
860
        return 0;
861
}
862
 
863
 
864
/* wait until the erase operation is finished, in a CPU hogging manner */
865
 
866
void
867
flash_busy_wait_erase(unsigned char *ptr)
868
{
869
        flashptr flashStart;
870
 
871
        ptr = (unsigned char *)((unsigned long)ptr | MEM_NON_CACHEABLE);
872
        flashStart = (flashptr)getchip(ptr)->start;
873
 
874
        /* busy-wait for flash completion - when D6 stops toggling between
875
         * reads.
876
         */
877
        while(flash_is_busy(flashStart))
878
                /* nothing */;
879
}
880
 
881
/* erase all flashchips and wait until operation is completed */
882
 
883
void
884
flash_erase_all()
885
{
886
        /* TODO: we should loop over chips, not just try the first two! */
887
 
888
        if(chips[0].isValid)
889
                flash_init_erase(chips[0].start, chips[0].size);
890
 
891
        if(chips[1].isValid)
892
                flash_init_erase(chips[1].start, chips[0].size);
893
 
894
        if(chips[0].isValid)
895
                flash_busy_wait_erase(chips[0].start);
896
 
897
        if(chips[1].isValid)
898
                flash_busy_wait_erase(chips[1].start);
899
 
900
#ifdef FLASH_VERBOSE
901
        safe_printk("Flash: full erasure completed.\n");
902
#endif
903
}
904
 
905
/* Write a block of Flash. The destination Flash sectors need to be erased
906
 * first. If the size is larger than the Flash chip the block starts in, the
907
 * function will continue flashing in the next chip if it exists.
908
 * Returns 0 on success, -1 on error.
909
 */
910
 
911
int
912
flash_write(unsigned char *ptr, const unsigned char *source, unsigned int size)
913
{
914
#ifndef CONFIG_SVINTO_SIM
915
        struct flashchip *chip;
916
        flashptr theData = (flashptr)source;
917
        flashptr flashStart;
918
        flashptr programAddress;
919
        int i, fsize;
920
        int odd_size;
921
 
922
        ptr = (unsigned char *)((unsigned long)ptr | MEM_NON_CACHEABLE);
923
 
924
        while(size > 0) {
925
                chip = getchip(ptr);
926
 
927
                if(!chip) {
928
                        printk("Flash: illegal ptr 0x%p in flash_write.\n", ptr);
929
                        return -EINVAL;
930
                }
931
 
932
                flashStart = (flashptr)chip->start;
933
                programAddress = (flashptr)ptr;
934
 
935
                /* if the block doesn't fit in this flash chip, clamp the size */
936
 
937
                fsize = (ptr + size) > (chip->start + chip->size) ?
938
                        (chip->start + chip->size - ptr) : size;
939
 
940
                ptr += fsize;
941
                size -= fsize;
942
                odd_size = fsize & 1;
943
 
944
                fsize >>= 1; /* We write one word at a time.  */
945
 
946
                FDEBUG(printk("flash_write (flash start 0x%p) %d words to 0x%p\n",
947
                              flashStart, fsize, programAddress));
948
 
949
                for (i = 0; i < fsize; i++) {
950
                        int retries = 0;
951
 
952
                        do {
953
                                int timeout;
954
                                /* Start programming sequence.
955
                                 */
956
#ifdef IRQ_LOCKS
957
                                unsigned long flags;
958
                                save_flags(flags);
959
                                cli();
960
#endif
961
                                flashStart[unlockAddress1] = unlockData1;
962
                                flashStart[unlockAddress2] = unlockData2;
963
                                flashStart[unlockAddress1] = programUnlockData;
964
                                *programAddress = *theData;
965
                                /* give the busy signal time to activate (tBusy, 90 ns) */
966
                                nop(); nop(); nop(); nop(); nop(); nop(); nop(); nop(); nop();
967
                                /* Wait for programming to finish.  */
968
                                timeout = 500000;
969
                                while(timeout-- &&
970
                                      (*flashStart & D6_MASK)
971
                                      != (*flashStart & D6_MASK))
972
                                /* nothing */;
973
#ifdef IRQ_LOCKS
974
                                restore_flags(flags);
975
#endif
976
                                if(!timeout)
977
                                        printk("flash: write timeout 0x%p\n",
978
                                               programAddress);
979
                                if(*programAddress == *theData)
980
                                        break;
981
 
982
                                printk("Flash: verify error 0x%p. "
983
                                       "(flash_write() 1)\n",
984
                                       programAddress);
985
                                printk("*programAddress = 0x%04x, "
986
                                       "*theData = 0x%04x\n",
987
                                       *programAddress, *theData);
988
                        } while(++retries < 5);
989
 
990
                        if(retries >= 5) {
991
                                printk("FATAL FLASH ERROR (1)\n");
992
                                return -EIO; /* we failed... */
993
                        }
994
 
995
                        programAddress++;
996
                        theData++;
997
                }
998
 
999
                /* We should write one extra byte to the flash.  */
1000
                if (odd_size) {
1001
                        unsigned char last_byte[2];
1002
                        int retries = 0;
1003
 
1004
                        last_byte[0] = *(unsigned char *)theData;
1005
                        last_byte[1] = ((unsigned char *)programAddress)[1];
1006
 
1007
                        do {
1008
                                int timeout = 500000;
1009
#ifdef IRQ_LOCKS
1010
                                unsigned long flags;
1011
                                save_flags(flags);
1012
                                cli();
1013
#endif
1014
                                flashStart[unlockAddress1] = unlockData1;
1015
                                flashStart[unlockAddress2] = unlockData2;
1016
                                flashStart[unlockAddress1] = programUnlockData;
1017
                                *programAddress = *(flashptr) last_byte;
1018
                                /* give the busy signal time enough to activate (tBusy, 90 ns) */
1019
                                nop(); nop(); nop(); nop(); nop(); nop(); nop(); nop(); nop();
1020
                                /* Wait for programming to finish */
1021
                                while (timeout-- &&
1022
                                       (*flashStart & D6_MASK)
1023
                                       != (*flashStart & D6_MASK))
1024
                                /* nothing */;
1025
#ifdef IRQ_LOCKS
1026
                                restore_flags(flags);
1027
#endif
1028
                                if(!timeout)
1029
                                        printk("flash: write timeout 0x%p\n",
1030
                                               programAddress);
1031
                                if(*programAddress
1032
                                   == *(flashptr)last_byte)
1033
                                        break;
1034
 
1035
                                printk("Flash: verify error 0x%p. "
1036
                                       "(flash_write() 2)\n",
1037
                                       programAddress);
1038
                        } while(++retries < 5);
1039
 
1040
                        if(retries >= 5) {
1041
                                printk("FATAL FLASH ERROR (2)\n");
1042
                                return -EIO; /* we failed... */
1043
                        }
1044
                }
1045
        }
1046
 
1047
#else
1048
        /* in the simulator we simulate flash as ram, so we can use a simple memcpy */
1049
        printk("flash write, source %p dest %p size %d\n", source, ptr, size);
1050
        memcpy(ptr, source, size);
1051
#endif
1052
        return 0;
1053
}
1054
 
1055
 
1056
/* "Memset" a chunk of memory on the flash.
1057
 * do this by flash_write()'ing a pattern chunk.
1058
 */
1059
 
1060
int
1061
flash_memset(unsigned char *ptr, const __u8 c, unsigned long size)
1062
{
1063
#ifndef CONFIG_SVINTO_SIM
1064
 
1065
        static unsigned char pattern[16];
1066
        int i;
1067
 
1068
        /* fill up pattern */
1069
 
1070
        for(i = 0; i < 16; i++)
1071
                pattern[i] = c;
1072
 
1073
        /* write as many 16-byte chunks as we can */
1074
 
1075
        while(size >= 16) {
1076
                flash_write(ptr, pattern, 16);
1077
                size -= 16;
1078
                ptr += 16;
1079
        }
1080
 
1081
        /* and the rest */
1082
 
1083
        if(size)
1084
                flash_write(ptr, pattern, size);
1085
#else
1086
 
1087
        /* In the simulator, we simulate flash as ram, so we can use a
1088
           simple memset.  */
1089
        printk("flash memset, byte 0x%x dest %p size %d\n", c, ptr, size);
1090
        memset(ptr, c, size);
1091
 
1092
#endif
1093
 
1094
        return 0;
1095
}
1096
 
1097
 
1098
#ifdef CONFIG_BLK_DEV_FLASH
1099
/* the operations supported by the block device */
1100
 
1101
static struct file_operations flash_block_fops =
1102
{
1103
        NULL,                   /* lseek - default */
1104
        block_read,             /* read - general block-dev read */
1105
        block_write,            /* write - general block-dev write */
1106
        NULL,                   /* readdir - bad */
1107
        NULL,                   /* poll */
1108
        flash_ioctl,            /* ioctl */
1109
        NULL,                   /* mmap */
1110
        flash_open,             /* open */
1111
        flash_release,          /* release */
1112
        block_fsync,            /* fsync */
1113
        NULL,                   /* fasync */
1114
        NULL,                   /* check media change */
1115
        NULL                    /* revalidate */
1116
};
1117
#endif
1118
 
1119
#ifdef CONFIG_CHR_DEV_FLASH
1120
/* the operations supported by the char device */
1121
 
1122
static struct file_operations flash_char_fops =
1123
{
1124
        NULL,                   /* lseek - default */
1125
        flash_char_read,        /* read */
1126
        flash_char_write,       /* write */
1127
        NULL,                   /* readdir - bad */
1128
        NULL,                   /* poll */
1129
        flash_ioctl,            /* ioctl */
1130
        NULL,                   /* mmap */
1131
        flash_open,             /* open */
1132
        flash_release,          /* release */
1133
        NULL,                   /* fsync */
1134
        NULL,                   /* fasync */
1135
        NULL,                   /* check media change */
1136
        NULL                    /* revalidate */
1137
};
1138
#endif
1139
 
1140
/* Initialize the flash_partitions array, by reading the partition information from the
1141
 * partition table (if there is any). Otherwise use a default partition set.
1142
 *
1143
 * The first partition is always sector 0 on the first chip, so start by initializing that.
1144
 * TODO: partitions only reside on chip[0] now. check that.
1145
 */
1146
 
1147
static void
1148
flash_init_partitions()
1149
{
1150
        struct bootblock *bootb;
1151
        struct partitiontable *ptable;
1152
        int pidx = 0;
1153
        const char *pmsg = "  /dev/flash%d at 0x%x, size 0x%x\n";
1154
 
1155
        /* if there is no chip 0, there is no bootblock => no partitions at all */
1156
 
1157
        if(chips[0].isValid) {
1158
 
1159
                printk("Checking flash partitions:\n");
1160
                /* first sector in the flash is partition 0,
1161
                 * regardless of if its a real flash "bootblock" or not
1162
                 */
1163
 
1164
                partitions[0].chip = &chips[0];
1165
                partitions[0].start = chips[0].start;
1166
                partitions[0].size = chips[0].sectorsize;
1167
                partitions[0].flags = 0;  /* FIXME */
1168
                flash_sizes[FLASH_MINOR] =
1169
                        partitions[0].size >> BLOCK_SIZE_BITS;
1170
                printk(pmsg, 0, partitions[0].start, partitions[0].size);
1171
                pidx++;
1172
 
1173
 
1174
                bootb = (struct bootblock *)partitions[0].start;
1175
 
1176
#ifdef CONFIG_SVINTO_SIM
1177
                /* If running in the simulator, do not scan nonexistent
1178
                   memory.  Behave as when the bootblock is "broken".
1179
                   ??? FIXME: Maybe there's something better to do. */
1180
                partitions[pidx].chip = &chips[0];
1181
                partitions[pidx].start = chips[0].start;
1182
                partitions[pidx].size = chips[0].size;
1183
                partitions[pidx].flags = 0; /* FIXME */
1184
                flash_sizes[FLASH_MINOR + pidx] =
1185
                        partitions[pidx].size >> BLOCK_SIZE_BITS;
1186
                printk(pmsg, pidx, partitions[pidx].start, partitions[pidx].size);
1187
                pidx++;
1188
#else  /* ! defined CONFIG_SVINTO_SIM */
1189
                /* TODO: until we've defined a better partition table, always do the
1190
                 * default flash1 and flash2 partitions.
1191
                 */
1192
                if(1 || bootb->magic != FLASH_BOOT_MAGIC) {
1193
 
1194
                        /* the flash is split into flash1, flash2 and bootblock
1195
                         * (flash0)
1196
                         */
1197
 
1198
                        printk("  No partitiontable recognized. Using default "
1199
                               "flash1 and flash2.\n");
1200
 
1201
                        /* flash1 starts after the first sector */
1202
 
1203
                        partitions[pidx].chip = &chips[0];
1204
                        partitions[pidx].start = chips[0].start + chips[0].sectorsize;
1205
                        partitions[pidx].size = chips[0].size - (DEF_FLASH2_SIZE +
1206
                                partitions[0].size);
1207
                        partitions[pidx].flags = 0; /* FIXME */
1208
                        flash_sizes[FLASH_MINOR + pidx] =
1209
                                partitions[pidx].size >> BLOCK_SIZE_BITS;
1210
                        printk(pmsg, pidx, partitions[pidx].start,
1211
                               partitions[pidx].size);
1212
                        pidx++;
1213
 
1214
                        /* flash2 starts after flash1. */
1215
 
1216
                        partitions[pidx].chip = &chips[0];
1217
                        partitions[pidx].start = partitions[pidx - 1].start +
1218
                                partitions[pidx - 1].size;
1219
                        partitions[pidx].size = DEF_FLASH2_SIZE;
1220
                        partitions[pidx].flags = 0; /* FIXME */
1221
                        flash_sizes[FLASH_MINOR + pidx] =
1222
                                partitions[pidx].size >> BLOCK_SIZE_BITS;
1223
                        printk(pmsg, pidx, partitions[pidx].start,
1224
                               partitions[pidx].size);
1225
                        pidx++;
1226
 
1227
                } else {
1228
 
1229
                        /* a working bootblock! read partitiontable */
1230
 
1231
                        ptable = (struct partitiontable *)(partitions[0].start + bootb->ptable_offset);
1232
 
1233
                        /* scan the table. it ends when there is 0xffffffff, that is, empty flash. */
1234
 
1235
                        while(ptable->offset != 0xffffffff) {
1236
                                partitions[pidx].chip = &chips[0];
1237
                                partitions[pidx].start = chips[0].start +
1238
                                        ptable->offset;
1239
                                partitions[pidx].size = ptable->size;
1240
                                partitions[pidx].flags = ptable->flags;
1241
                                flash_sizes[FLASH_MINOR + pidx] =
1242
                                        partitions[pidx].size >> BLOCK_SIZE_BITS;
1243
                                printk(pmsg, pidx, partitions[pidx].start,
1244
                                       partitions[pidx].size);
1245
                                pidx++;
1246
                                ptable++;
1247
                        }
1248
                }
1249
#endif /* ! defined CONFIG_SVINTO_SIM */
1250
        }
1251
 
1252
        /* fill in the rest of the table as well */
1253
 
1254
        while(pidx < MAX_PARTITIONS) {
1255
                partitions[pidx].start = 0;
1256
                partitions[pidx].size = 0;
1257
                partitions[pidx].chip = 0;
1258
                pidx++;
1259
        }
1260
 
1261
        /*flash_blk_sizes[FLASH_MINOR + i] = 1024; TODO this should be 512.. */
1262
}
1263
 
1264
#ifdef LISAHACK
1265
static void
1266
move_around_bootparams()
1267
{
1268
        unsigned long *newp = (unsigned long *)0x8000c000;  /* new bootsector */
1269
        unsigned long *oldp = (unsigned long *)0x801fc000;  /* old bootsector */
1270
        unsigned long *buf;
1271
        unsigned long magic = 0xbeefcace;
1272
 
1273
        printk("Checking if we need to move bootparams...");
1274
 
1275
        /* first check if they are already moved */
1276
 
1277
        if(*newp == magic) {
1278
                printk(" no\n");
1279
                return;
1280
        }
1281
 
1282
        printk(" yes. Moving..");
1283
 
1284
        buf = (unsigned long *)kmalloc(0x4000, GFP_KERNEL);
1285
 
1286
        memcpy(buf, oldp, 0x4000);
1287
 
1288
        flash_write((unsigned char *)newp, (unsigned char *)&magic, 4);
1289
        flash_write((unsigned char *)(newp + 1), (unsigned char *)buf, 0x4000 - 4);
1290
 
1291
        /* erase old boot block, so JFFS can expand into it */
1292
 
1293
        flash_init_erase((unsigned char *)0x801f0000, chips[0].sectorsize);
1294
        flash_busy_wait_erase(chips[0].start);
1295
 
1296
        printk(" done.\n");
1297
 
1298
        kfree(buf);
1299
}
1300
#endif
1301
 
1302
/* register the device into the kernel - called at boot */
1303
 
1304
int
1305
flash_init()
1306
{
1307
#ifdef CONFIG_BLK_DEV_FLASH
1308
        /* register the block device major */
1309
 
1310
        if(register_blkdev(MAJOR_NR, DEVICE_NAME, &flash_block_fops )) {
1311
                printk(KERN_ERR DEVICE_NAME ": Unable to get major %d\n",
1312
                       MAJOR_NR);
1313
                return -EBUSY;
1314
        }
1315
 
1316
        /* register the actual block I/O function - do_flash_request - and the
1317
         * tables containing the device sizes (in 1kb units) and block sizes
1318
         */
1319
 
1320
        blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
1321
        blk_size[MAJOR_NR] = flash_sizes;
1322
        blksize_size[MAJOR_NR] = flash_blk_sizes;
1323
        read_ahead[MAJOR_NR] = 1; /* fast device, so small read ahead */
1324
 
1325
        printk("Flash/ROM block device v2.1, (c) 1999 Axis Communications AB\n");
1326
#endif
1327
 
1328
#ifdef CONFIG_CHR_DEV_FLASH
1329
        /* register the char device major */
1330
 
1331
        if(register_chrdev(MAJOR_NR, DEVICE_NAME, &flash_char_fops )) {
1332
                printk(KERN_ERR DEVICE_NAME ": Unable to get major %d\n",
1333
                       MAJOR_NR);
1334
                return -EBUSY;
1335
        }
1336
 
1337
        printk("Flash/ROM char device v2.1, (c) 1999 Axis Communications AB\n");
1338
#endif
1339
 
1340
        /* initialize partition table */
1341
 
1342
        flash_init_partitions();
1343
 
1344
#ifdef LISAHACK
1345
        /* nasty hack to "upgrade" older beta units of Lisa into newer by
1346
         * moving the boot block parameters. will go away as soon as this
1347
         * build is done.
1348
         */
1349
 
1350
        move_around_bootparams();
1351
#endif
1352
 
1353
        return 0;
1354
}
1355
 
1356
/* check if it's possible to erase the wanted range, and if not, return
1357
 * the range that IS erasable, or a negative error code.
1358
 */
1359
 
1360
long
1361
flash_erasable_size(void *_part, __u32 offset, __u32 size)
1362
{
1363
        struct flashpartition *part = (struct flashpartition *)_part;
1364
        int ssize;
1365
 
1366
        if (!part->start) {
1367
                return -EINVAL;
1368
        }
1369
 
1370
        /* assume that sector size for a partition is constant even
1371
         * if it spans more than one chip (you usually put the same
1372
         * type of chips in a system)
1373
         */
1374
 
1375
        ssize = part->chip->sectorsize;
1376
 
1377
        if (offset % ssize) {
1378
                /* The offset is not sector size aligned.  */
1379
                return -1;
1380
        }
1381
        else if (offset > part->size) {
1382
                return -2;
1383
        }
1384
        else if (offset + size > part->size) {
1385
                return -3;
1386
        }
1387
 
1388
        return (size / ssize) * ssize;
1389
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.