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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [io/] [flash/] [current/] [doc/] [flash.sgml] - Blame information for rev 838

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
2
 
3
4
 
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 
30
31
 
32
33
FLASH Library
34
35
The eCos FLASH Library
36
The FLASH library is an optional part of eCos, and is only
37
        applicable to some platforms.
38
The eCos FLASH library provides the following functionality:
39
 
40
41
Identifying installed device of a FLASH family.
42
          
43
44
Read, erasing and writing to FLASH blocks.
45
Validating an address is within the FLASH.
46
Determining the number and size of FLASH blocks.
47
          
48
49
 
50
 There are two APIs with the flash library. The old API is
51
retained for backwards compatibility reasons, but should slowly be
52
replaced with the new API which is much more flexible and does not
53
pollute the name space as much.
54
55
 
56
57
Notes on using the FLASH library
58
 
59
FLASH devices cannot be read from when an erase or write
60
operation is active. This means it is not possible to execute code
61
from flash while an erase or write operation is active. It is possible
62
to use the library when the executable image is resident in FLASH. The
63
low level drivers are written such that the linker places the
64
functions that actually manipulate the flash into RAM.  However the
65
library may not be interrupt safe. An interrupt must not cause
66
execution of code that is resident in FLASH. This may be the image
67
itself, or RedBoot. In some configurations of eCos, ^C on the serial
68
port or debugging via Ethernet may cause an interrupt handler to call
69
RedBoot. If RedBoot is resident in FLASH this will cause a crash.
70
Similarly, if another thread invokes a virtual vector function to
71
access RedBoot, eg to perform a diag_printf() a
72
crash could result.
73
74
 
75
 Thus with a ROM based image or a ROM based Redboot it is
76
recommended to disable interrupts while erasing or programming
77
flash. Using both a ROMRAM or RAM images and a ROMRAM or RAM RedBoot
78
are safe and there is no need to disable interrupts.
79
80
81
 
82
83
Danger, Will Robinson! Danger!
84
 
85
Unlike nearly every other aspect of embedded system programming,
86
getting it wrong with FLASH devices can render your target system
87
useless. Most targets have a boot loader in the FLASH. Without this
88
boot loader the target will obviously not boot. So before starting to
89
play with this library its worth investigating a few things. How do
90
you recover your target if you delete the boot loader? Do you have the
91
necessary JTAG cable? Or is specialist hardware needed? Is it even
92
possible to recover the target boards or must it be thrown into the
93
rubbish bin? How does killing the board affect your project schedule?
94
95
 
96
97
98
 
99
100
The Version 2 eCos FLASH API
101
 
102
 There are two APIs described here. The first is the application
103
API which programs should use. The second API is that between the
104
FLASH IO library and the device drivers. 
105
 
106
107
FLASH user API
108
 
109
All of the functions described below are declared in the header
110
file <cyg/io/flash.h> which all users of
111
the FLASH library should include.
112
 
113
114
Initializing the FLASH library
115
 
116
The FLASH library needs to be initialized before other FLASH
117
operations can be performed. This only needs to be done once. The
118
following function will only do the initialization once so it's safe
119
to call multiple times: 
120
 
121
__externC int cyg_flash_init(cyg_flash_printf *pf); 
122
 
123
124
The parameter pf must always be set to NULL. It
125
exists solely for backward compatibility and other settings are deprecated
126
and obsolete. Past use of this parameter has now been replaced with use of
127
the 
128
linkend="ecos-flash-v2-api-config-diag-output">cyg_flash_set_global_printf
129
function.
130
131
 
132
133
 
134
135
Retrieving information about FLASH devices
136
 
137
138
The following five functions return information about the FLASH.
139
140
 
141
142
__externC int cyg_flash_get_info(cyg_uint32 devno, cyg_flash_info_t * info);
143
__externC int cyg_flash_get_info_addr(cyg_flashaddr_t flash_base, cyg_flash_info_t * info);
144
__externC int cyg_flash_verify_addr(const flashaddr_t address);
145
__extern size_t cyg_flash_block_size(const cyg_flashaddr_t flash_base);
146
 
147
typedef struct cyg_flash_block_info
148
{
149
    size_t                    block_size;
150
    cyg_uint32                blocks;
151
} cyg_flash_block_info_t;
152
 
153
typedef struct {
154
    cyg_flashaddr_t              start;          // First address
155
    cyg_flashaddr_t              end;            // Last address
156
    cyg_uint32                   num_block_infos;// Number of entries
157
    const cyg_flash_block_info_t *block_info;    // Info about one block size
158
} cyg_flash_info_t;
159
160
 
161
cyg_flash_get_info() is the main function
162
to get information about installed flash devices.  Parameter
163
devno is used to iterate over the available
164
flash devices, starting from 0. If the devno'th device exists, the
165
structure pointed to by info is filled in and
166
CYG_FLASH_ERR_OK is returned, otherwise
167
CYG_FLASH_ERR_INVALID.
168
cyg_flash_get_info_addr() is similar, but returns the
169
information about the flash device at the given address.
170
cyg_flash_block_size() returns the size of the
171
block at the given address.  cyg_flash_verify_addr()
172
 tests if the target addresses is within one of the FLASH
173
devices, returning CYG_FLASH_ERR_OK  if so.
174
175
176
 
177
 
178
Reading from FLASH
179
 
180
181
There are two methods for reading from FLASH. The first is to use the
182
following function. 
183
 
184
185
__externC int cyg_flash_read(cyg_flashaddr_t flash_base, void *ram_base, size_t len, cyg_flashaddr_t *err_address);
186
187
 
188
189
flash_base is where in the flash to read
190
from. ram_base indicates where the data read
191
from flash should be placed into RAM. len is
192
the number of bytes to be read from the FLASH and
193
err_address is used to return the location in
194
FLASH that any error occurred while reading.
195
196
 
197
198
The second method is to simply memcpy() directly
199
from the FLASH. This is not recommended since some types of device
200
cannot be read in this way, eg NAND FLASH. Using the FLASH library
201
function to read the FLASH will always work so making it easy to port
202
code from one FLASH device to another.
203
204
 
205
206
207
 
208
Erasing areas of FLASH
209
 
210
211
Blocks of FLASH can be erased using the following function:
212
213
 
214
__externC int cyg_flash_erase(cyg_flashaddr_t flash_base, size_t len, cyg_flashaddr_t *err_address);
215
216
 
217
218
flash_base is where in the flash to erase
219
from. len is the minimum number of bytes to
220
erase in the FLASH and err_address is used to
221
return the location in FLASH that any error occurred while erasing. It
222
should be noted that FLASH devices are block oriented when erasing. It
223
is not possible to erase a few bytes within a block, the whole block
224
will be erased. flash_base may be anywhere
225
within the first block to be erased and flash_base+len
226
 may be anywhere in the last block to be erased.  
227
 
228
229
230
 
231
Programming the FLASH
232
 
233
 Programming of the flash is achieved using the following
234
function.
235
 
236
__externC int cyg_flash_program(cyg_flashaddr_t flash_base, void *ram_base, size_t len, cyg_flashaddr_t *err_address);
237
238
 
239
240
flash_base is where in the flash to program
241
from. ram_base indicates where the data to be
242
programmed into FLASH should be read from in RAM. len
243
 is the number of bytes to be program into the FLASH and
244
err_address is used to return the location in
245
FLASH that any error occurred while programming. 
246
 
247
248
249
 
250
Locking and unlocking blocks
251
 
252
253
Some flash devices have the ability to lock and unlock blocks. A
254
locked block cannot be erased or programmed without it first being
255
unlocked. For devices which support this feature and when 
256
CYGHWR_IO_FLASH_BLOCK_LOCKING is enabled then the following
257
two functions are available:
258
 
259
260
__externC int cyg_flash_lock(const cyg_flashaddr_t flash_base, size_t len, cyg_flashaddr_t *err_address);
261
__externC int cyg_flash_unlock(const cyg_flashaddr_t flash_base, size_t len, cyg_flashaddr_t *err_address);
262
263
 
264
265
 
266
267
Locking FLASH mutexes
268
 
269
When the eCos kernel package is included in the eCos
270
configuration, the FLASH IO library will perform mutex locking on
271
FLASH operations. This makes the API defined here thread safe. However
272
applications may wish to directly access the contents of the FLASH. In
273
order for this to be thread safe it is necessary for the application
274
to use the following two functions to inform the FLASH IO library that
275
the FLASH devices are being used and other API calls should be
276
blocked.
277
 
278
279
__externC int cyg_flash_mutex_lock(const cyg_flashaddr_t from, size_t len);
280
__externC int cyg_flash_mutex_unlock(const cyg_flashaddr_t from, size_t len);
281
282
 
283
284
 
285
286
Configuring diagnostic output
287
 
288
 Each FLASH device can have an associated function which is
289
called to perform diagnostic output. The function to be used can
290
be configured with the following functions: 
291
 
292
__externC int cyg_flash_set_printf(const cyg_flashaddr_t flash_base,
293
                                   cyg_flash_printf *pf);
294
__externC void cyg_flash_set_global_printf(cyg_flash_printf *pf);
295
typedef int cyg_flash_printf(const char *fmt, ...); 
296
 
297
The parameter pf is a pointer to a function
298
which is to be used for diagnostic output. Typically the function
299
diag_printf() will be passed. Normally this
300
function is not used by the higher layer of the library unless
301
CYGSEM_IO_FLASH_CHATTER is enabled.  Passing a
302
NULL causes diagnostic output from lower level
303
drivers to be discarded.
304
 
305
cyg_flash_set_printf is used to set a
306
diagnostic output function which will be used specifically when
307
diagnostic output is attempted from the FLASH device driver associated
308
with the base address of flash_base. An error
309
will be returned if no FLASH device is found for this address, or the
310
FLASH subsystem has not yet been initialised with
311
cyg_flash_init.
312
 
313
cyg_flash_set_global_printf sets a
314
diagnostic output function for all available FLASH devices. Any
315
previous setting of a diagnostic output function (including with
316
cyg_flash_set_printf) will be discarded.
317
This function may be called prior to
318
cyg_flash_init.
319
320
 
321
322
 
323
Return values and errors
324
 
325
All the functions above return one of the following return
326
values.
327
 
328
329
CYG_FLASH_ERR_OK              No error - operation complete
330
CYG_FLASH_ERR_INVALID         Invalid FLASH address
331
CYG_FLASH_ERR_ERASE           Error trying to erase
332
CYG_FLASH_ERR_LOCK            Error trying to lock/unlock
333
CYG_FLASH_ERR_PROGRAM         Error trying to program
334
CYG_FLASH_ERR_PROTOCOL        Generic error
335
CYG_FLASH_ERR_PROTECT         Device/region is write-protected
336
CYG_FLASH_ERR_NOT_INIT        FLASH info not yet initialized
337
CYG_FLASH_ERR_HWR             Hardware (configuration?) problem
338
CYG_FLASH_ERR_ERASE_SUSPEND   Device is in erase suspend mode
339
CYG_FLASH_ERR_PROGRAM_SUSPEND Device is in program suspend mode
340
CYG_FLASH_ERR_DRV_VERIFY      Driver failed to verify data
341
CYG_FLASH_ERR_DRV_TIMEOUT     Driver timed out waiting for device
342
CYG_FLASH_ERR_DRV_WRONG_PART  Driver does not support device
343
CYG_FLASH_ERR_LOW_VOLTAGE     Not enough juice to complete job
344
345
 
346
To turn an error code into a human readable string the following
347
function can be used:
348
 
349
__externC const char *cyg_flash_errmsg(const int err);
350
351
352
 
353
354
 
355
356
FLASH device API This section describes the API
357
between the FLASH IO library the FLASH device drivers.
358
 
359
360
The FLASH device Structure
361
 
362
This structure keeps all the information about a single driver.
363
 
364
struct cyg_flash_dev {
365
  const struct cyg_flash_dev_funs *funs;            // Function pointers
366
  cyg_uint32                      flags;            // Device characteristics
367
  cyg_flashaddr_t                 start;            // First address
368
  cyg_flashaddr_t                 end;              // Last address
369
  cyg_uint32                      num_block_infos;  // Number of entries
370
  const cyg_flash_block_info_t    *block_info;      // Info about one block size
371
 
372
  const void                      *priv;            // Devices private data
373
 
374
  // The following are only written to by the FLASH IO layer.
375
  cyg_flash_printf                *pf;              // Pointer to diagnostic printf
376
  bool                            init;             // Device has been initialised
377
#ifdef CYGPKG_KERNEL
378
  cyg_mutex_t                     mutex;            // Mutex for thread safeness
379
#endif
380
#if (CYGHWR_IO_FLASH_DEVICE > 1)
381
  struct cyg_flash_dev            *next;            // Pointer to next device
382
#endif
383
};
384
 
385
struct cyg_flash_dev_funs {
386
  int     (*flash_init) (struct cyg_flash_dev *dev);
387
  size_t  (*flash_query) (struct cyg_flash_dev *dev, void * data, size_t len);
388
  int     (*flash_erase_block) (struct cyg_flash_dev *dev, cyg_flashaddr_t block_base);
389
  int     (*flash_program) (struct cyg_flash_dev *dev, cyg_flashaddr_t base, const void* data, size_t len);
390
  int     (*flash_read) (struct cyg_flash_dev *dev, const cyg_flashaddr_t base, void* data, size_t len);
391
#ifdef CYGHWR_IO_FLASH_BLOCK_LOCKING
392
  int     (*flash_block_lock) (struct cyg_flash_dev *dev, const cyg_flashaddr_t block_base);
393
  int     (*flash_block_unlock) (struct cyg_flash_dev *dev, const cyg_flashaddr_t block_base);
394
#endif
395
};
396
397
 
398
The FLASH IO layer will only pass requests for operations on a single block.
399
400
401
402
 
403
404
The legacy Version 1 eCos FLASH API
405
406
The library has a number of limitations:
407
 
408
409
Only one family of FLASH device may be supported at once.
410
         
411
412
Multiple devices of one family are supported, but they must
413
                be contiguous in memory.
414
          
415
416
The library is not thread or interrupt safe under
417
                some conditions.
418
          
419
420
The library currently does not use the eCos naming
421
                 convention for its functions. This may change in the
422
                 future but backward compatibility is likely to be kept.
423
          
424
425
426
 
427
 There are two APIs described here. The first is the application
428
API which programs should use. The second API is that between the
429
FLASH io library and the device drivers. 
430
 
431
432
FLASH user API
433
 
434
All of the functions described below are declared in the header
435
file <cyg/io/flash.h> which all users of
436
the FLASH library should include.
437
 
438
 
439
440
Initializing the FLASH library
441
 
442
The FLASH library needs to be initialized before other FLASH
443
operations can be performed. This only needs to be done once. The
444
following function will only do the initialization once so it's safe
445
to call multiple times: 
446
 
447
externC int flash_init( _printf *pf );
448
typedef int _printf(const char *fmt, ...); 
449
 
450
451
The parameter pf is a pointer to a function
452
which is to be used for diagnostic output. Typically the function
453
diag_printf() will be passed. Normally this
454
function is not used by the higher layer of the library unless
455
CYGSEM_IO_FLASH_CHATTER is enabled.  Passing a
456
NULL is not recommended, even when
457
CYGSEM_IO_FLASH_CHATTER is disabled. The lower layers of the library
458
may unconditionally call this function, especially when errors occur,
459
probably resulting in a more serious error/crash!.
460
461
 
462
463
Retrieving information about the FLASH
464
 
465
466
The following four functions return information about the FLASH.
467
468
 
469
externC int flash_get_block_info(int *block_size, int *blocks);
470
externC int flash_get_limits(void *target, void **start, void **end);
471
externC int flash_verify_addr(void *target);
472
externC bool flash_code_overlaps(void *start, void *end);
473
474
 
475
476
The function flash_get_block_info() returns the
477
size and number of blocks. When the device has a mixture of block
478
sizes, the size of the "normal" block will be returned. Please read
479
the source code to determine exactly what this means.
480
flash_get_limits() returns the lower and upper
481
memory address the FLASH occupies. The target
482
parameter is current unused.  flash_verify_addr()
483
 tests if the target addresses is within the flash,
484
returning FLASH_ERR_OK if so. Lastly, 
485
flash_code_overlaps()  checks if the executing code is
486
resident in the section of flash indicated by
487
start and  end. If this
488
function returns true, erase and program operations within this range
489
are very likely to cause the target to crash and burn horribly. Note
490
the FLASH library does allow you to shoot yourself in the foot in this
491
way.
492
 
493
494
495
 
496
Reading from FLASH
497
 
498
499
There are two methods for reading from FLASH. The first is to use the
500
following function. 
501
 
502
503
externC int flash_read(void *flash_base, void *ram_base, int len, void **err_address);
504
505
 
506
507
flash_base is where in the flash to read
508
from. ram_base indicates where the data read
509
from flash should be placed into RAM. len is
510
the number of bytes to be read from the FLASH and
511
err_address is used to return the location in
512
FLASH that any error occurred while reading.
513
514
 
515
516
The second method is to simply memcpy() directly
517
from the FLASH. This is not recommended since some types of device
518
cannot be read in this way, eg NAND FLASH. Using the FLASH library
519
function to read the FLASH will always work so making it easy to port
520
code from one FLASH device to another.
521
522
 
523
524
525
 
526
Erasing areas of FLASH
527
 
528
529
Blocks of FLASH can be erased using the following function:
530
531
 
532
externC int flash_erase(void *flash_base, int len, void **err_address);
533
534
 
535
536
flash_base is where in the flash to erase
537
from. len is the minimum number of bytes to
538
erase in the FLASH and err_address is used to
539
return the location in FLASH that any error occurred while erasing. It
540
should be noted that FLASH devices are block oriented when erasing. It
541
is not possible to erase a few bytes within a block, the whole block
542
will be erased. flash_base may be anywhere
543
within the first block to be erased and flash_base+len
544
 may be anywhere in the last block to be erased.  
545
 
546
547
548
 
549
Programming the FLASH
550
 
551
 Programming of the flash is achieved using the following
552
function.
553
 
554
externC int flash_program(void *flash_base, void *ram_base, int len, void **err_address);
555
556
 
557
558
flash_base is where in the flash to program
559
from. ram_base indicates where the data to be
560
programmed into FLASH should be read from in RAM. len
561
 is the number of bytes to be program into the FLASH and
562
err_address is used to return the location in
563
FLASH that any error occurred while programming. 
564
 
565
566
567
 
568
Locking and unlocking blocks
569
 
570
571
Some flash devices have the ability to lock and unlock blocks. A
572
locked block cannot be erased or programmed without it first being
573
unlocked. For devices which support this feature and when 
574
CYGHWR_IO_FLASH_BLOCK_LOCKING is enabled then the following
575
two functions are available:
576
 
577
578
externC int flash_lock(void *flash_base, int len, void **err_address);
579
externC int flash_unlock(void *flash_base, int len, void **err_address);
580
581
 
582
583
584
 
585
Return values and errors
586
 
587
All the functions above, except flash_code_overlaps()
588
 return one of the following return values.
589
 
590
591
FLASH_ERR_OK              No error - operation complete
592
FLASH_ERR_INVALID         Invalid FLASH address
593
FLASH_ERR_ERASE           Error trying to erase
594
FLASH_ERR_LOCK            Error trying to lock/unlock
595
FLASH_ERR_PROGRAM         Error trying to program
596
FLASH_ERR_PROTOCOL        Generic error
597
FLASH_ERR_PROTECT         Device/region is write-protected
598
FLASH_ERR_NOT_INIT        FLASH info not yet initialized
599
FLASH_ERR_HWR             Hardware (configuration?) problem
600
FLASH_ERR_ERASE_SUSPEND   Device is in erase suspend mode
601
FLASH_ERR_PROGRAM_SUSPEND Device is in program suspend mode
602
FLASH_ERR_DRV_VERIFY      Driver failed to verify data
603
FLASH_ERR_DRV_TIMEOUT     Driver timed out waiting for device
604
FLASH_ERR_DRV_WRONG_PART  Driver does not support device
605
FLASH_ERR_LOW_VOLTAGE     Not enough juice to complete job
606
607
 
608
To turn an error code into a human readable string the following
609
function can be used:
610
 
611
externC char *flash_errmsg(int err);
612
613
614
 
615
616
 
617
 Notes on using the FLASH library
618
 
619
620
The FLASH library evolved from the needs and environment of RedBoot
621
rather than being a general purpose eCos component. This history
622
explains some of the problems with the library.  
623
 
624
The library is not thread safe. Multiple simultaneous calls to
625
its library functions will likely fail and may cause a crash. It is
626
the callers responsibility to use the necessary mutex's if needed.
627
628
 
629
630
631
 
632
633
FLASH device API This section describes the API
634
between the FLASH IO library the FLASH device drivers.
635
 
636
637
The flash_info structure
638
 
639
 The flash_infostructure is used by both
640
the FLASH IO library and the device driver.
641
struct flash_info {
642
    int   block_size;   // Assuming fixed size "blocks"
643
    int   blocks;       // Number of blocks
644
    int   buffer_size;  // Size of write buffer (only defined for some devices)
645
    unsigned long block_mask;
646
    void *start, *end;  // Address range
647
    int   init;         // FLASH API initialised
648
    _printf *pf;        // printf like function for diagnostics
649
};
650
651
 
652
block_mask is used internally in the FLASH IO library. It
653
contains a mask which can be used to turn an arbitrary address in
654
flash to the base address of the block which contains the
655
address.
656
 
657
There exists one global instance of this structure with the name
658
flash_info. All calls into the device driver
659
makes use of this global structure to maintain state.
660
 
661
662
 
663
664
Initializing the device driver
665
 
666
The FLASH IO library will call the following function to
667
initialize the device driver:
668
 
669
externC int  flash_hwr_init(void);
670
671
 
672
The device driver should probe the hardware to see if the FLASH
673
devices exist. If it does it should fill in start, end,
674
blocks and block_size.If the FLASH contains a write buffer
675
the size of this should be placed in buffer_size
676
. On successful probing the function should return
677
FLASH_ERR_OK. When things go wrong it can be
678
assumed that pf points to a printf like
679
function for outputting error messages.
680
681
682
 
683
684
Querying the FLASH
685
 
686
FLASH devices can be queried to return there manufacture ID,
687
size etc. This function allows this information to be returned.
688
 
689
int flash_query(unsigned char *data);
690
 
691
The caller must know the size of data to be returned and provide
692
an appropriately sized buffer pointed to be parameter
693
data. This function is generally used by
694
flash_hwr_init().
695
 
696
697
698
Erasing a block of FLASH
699
 
700
So that the FLASH IO layer can erase a block of FLASH the
701
following function should be provided.
702
 
703
int flash_erase_block(volatile flash_t *block, unsigned int block_size);
704
705
 
706
707
708
Programming a region of FLASH
709
 
710
The following function must be provided so that data can be
711
written into the FLASH.
712
 
713
int flash_program_buf(volatile flash_t *addr, flash_t *data, int len,
714
                  unsigned long block_mask, int buffer_size);
715
 
716
The device will only be asked to program data in one block of
717
the flash. The FLASH IO layer will break longer user requests into a
718
smaller writes.
719
 
720
721
 
722
723
Reading a region from FLASH
724
 
725
Some FLASH devices are not memory mapped so it is not possible
726
to read there contents directly. The following function read a region
727
of FLASH.
728
 
729
int flash_read_buf(volatile flash_t* addr, flash_t* data, int len);
730
731
 
732
As with writing to the flash, the FLASH IO layer will break
733
longer user requests for data into a number of reads which are at
734
maximum one block in size.
735
 
736
A device which cannot be read directy should set
737
CYGSEM_IO_FLASH_READ_INDIRECT so that the IO layer
738
makes use of the flash_read_buf()function.
739
 
740
741
 
742
743
Locking and unlocking FLASH blocks
744
 
745
Some flash devices allow blocks to be locked so that they cannot
746
be written to. The device driver should provide the following
747
functions to manipulate these locks.
748
 
749
int flash_lock_block(volatile flash_t *block);
750
int flash_unlock_block(volatile flash_t *block, int block_size, int blocks);
751
752
 
753
These functions are only used if
754
CYGHWR_IO_FLASH_BLOCK_LOCKING
755
 
756
757
 
758
759
Mapping FLASH error codes to FLASH IO error codes
760
 
761
The functions flash_erase_block(),
762
flash_program_buf(), flash_read_buf(), flash_lock_block() and
763
flash_unlock_block() return an error code which is specific
764
to the flash device. To map this into a FLASH IO error code, the
765
driver should provide the following function: 
766
 
767
int flash_hwr_map_error(int err);
768
 
769
770
 
771
772
Determining if code is in FLASH
773
 
774
Although a general function, the device driver is expected to
775
provide the implementation of the function
776
flash_code_overlaps().
777
 
778
779
 
780
781
Implementation Notes
782
 
783
The FLASH IO layer will manipulate the caches as required. The
784
device drivers do not need to enable/disable caches when performing
785
operations of the FLASH.
786
 
787
Device drivers should keep all chatter to a minimum when
788
CYGSEM_IO_FLASH_CHATTER is not defined. All output
789
should use the print function in the pf in
790
flash_info and not
791
diag_printf()
792
 
793
Device driver functions which manipulate the state of the flash
794
so that it cannot be read from for program execute need to ensure
795
there code is placed into RAM. The linker will do this if the
796
appropriate attribute is added to the function. e.g:
797
 
798
int flash_program_buf(volatile flash_t *addr, flash_t *data, int len,
799
                  unsigned long block_mask, int buffer_size)
800
 __attribute__ ((section (".2ram.flash_program_buf")));
801
 
802
803
804
805
 
806
807
FLASH I/O devices
808
 
809
810
It can be useful to be able to access FLASH devices using the generic
811
I/O infrastructure found in CYGPKG_IO, and the generic
812
FLASH layer provides an optional ability to do so. This allows
813
the use of functions like cyg_io_lookup(),
814
cyg_io_read(),
815
cyg_io_write() etc.
816
817
 Additionally it means that, courtesy of the
818
“devfs” pseudo-filesystem in the file I/O layer
819
(CYGPKG_IO_FILEIO), functions like
820
open(), read(),
821
write() etc. can even be used directly
822
on the FLASH devices.
823
824
 
825
826
Overview and CDL Configuration
827
 
828
829
This package implements support for FLASH as an I/O device by exporting
830
it as if it is a block device. To enable this support, the CDL option
831
titled “Provide /dev block devices”, also known as
832
CYGPKG_IO_FLASH_BLOCK_DEVICE, must be enabled.
833
(There is also a legacy format alternative which is now deprecated).
834
835
836
There are two methods of addressing FLASH as a block device:
837
 
838
839
Using the FLASH Information System (FIS) - this is a
840
method of defining and naming FLASH partitions, usually in RedBoot.
841
This option is only valid if RedBoot is resident and was used to
842
boot the application. To reference FLASH partitions in this way,
843
you would use a device name of the form
844
/dev/flash/fis/partition-name,
845
for example /dev/flash/fis/jffs2 to reference a
846
FIS partition named JFFS2.
847
 
848
The CDL option CYGFUN_IO_FLASH_BLOCK_FROM_FIS
849
must be enabled for this support.
850
851
 
852
Referencing by device number, offset and length - this
853
method extracts addressing information from the name itself. The form
854
of the device would be
855
/dev/flash/device-number/offset[,length]
856
 
857
858
device-number
859
 
860
This is a fixed number allocated to identify each FLASH
861
region in the system. The first region is numbered 0, the second 1,
862
and so on. If you have only one FLASH device, it will be numbered 0.
863
864
 
865
offset
866
This is the index into the FLASH region in bytes to use. It
867
may be specified as decimal, or if prefixed with
868
0x, then hexadecimal.
869
 
870
length
871
This field is optional and defaults to the remainder
872
of the FLASH region.  Again it may be specified in decimal or
873
hexadecimal.
874
875
 
876
877
Some examples:
878
879
/dev/flash/0/0
880
This defines a block device that uses the entirety of
881
FLASH region 0.
882
883
/dev/flash/1/0x20000,65536
884
This defines a block device which points inside FLASH region 1,
885
starting at offset 0x20000 (128Kb) and extending for 64Kb.
886
887
/dev/flash/0/65536
888
This defines a block device which points inside FLASH region 0,
889
starting at offset 64Kb and continuing up to the end of the device.
890
891
892
893
894
Obviously great care is required when constructing the device names as
895
using the wrong specification may subsequently overwrite important areas
896
of FLASH, such as RedBoot. Using the alternative via FIS names is
897
preferable as these are less error-prone to configure, and also allows for
898
the FLASH region to be relocated without requiring program recompilation.
899
900
901
902
903
904
Using FLASH I/O devices
905
906
The FLASH I/O block devices can be accessed, read and written using
907
the standard interface supplied by the generic I/O
908
(CYGPKG_IO) package.  These include the functions:
909
cyg_io_lookup() to access the device and get a
910
handle, cyg_io_read() and
911
cyg_io_write() for sequential read and write
912
operations, cyg_io_bread() and
913
cyg_io_bwrite() for random access read and write
914
operations, and cyg_io_get_config() and
915
cyg_io_setconfig() for run-time configuration
916
inspection and control.
917
918
919
However there are two aspects that differ from some other I/O devices
920
accessed this way:
921
922
923
The first is that the lookup operation uses up
924
resources which must be subsequently freed when the last user of the
925
I/O handle is finished. The number of FLASH I/O devices that may be
926
simultaneously opened is configured with the
927
CYGNUM_IO_FLASH_BLOCK_DEVICES CDL option. After the
928
last user is finished, the device may be closed using
929
cyg_io_setconfig() with the
930
CYG_IO_SET_CONFIG_CLOSE key. Reference counting to
931
ensure that it is only the last user that causes a close, is left to
932
higher layers.
933
934
The second is that write operations assume that the
935
flash is already erased. Attempting to write to Flash that has
936
already been written to may result in errors. Instead FLASH must
937
be erased before it may be written.
938
939
 
940
FLASH block devices can also be read and written using the
941
standard POSIX primitives, open(),
942
close(), read(),
943
write(), lseek(), and so on
944
if the POSIX file I/O package (CYGPKG_FILEIO) is
945
included in the configuration. As with the eCos generic I/O interface
946
you must call close() to ensure resources
947
are freed when the device is no longer used.
948
 
949
Other configuration keys are provided to perform FLASH erase
950
operations, and to retrieve device sizes, and FLASH block sizes at
951
a particular address. These operations are accessed with
952
cyg_io_get_config() (or if using the POSIX
953
file I/O API, cyg_fs_getinfo()) with the
954
following keys:
955
 
956
957
CYG_IO_GET_CONFIG_FLASH_ERASE
958
This erases a region of FLASH.
959
cyg_io_get_config() must be passed a
960
structure defined as per the following, which is also supplied
961
in <cyg/io/flash.h>:
962
963
typedef struct {
964
    CYG_ADDRESS offset;
965
    size_t len;
966
    int flasherr;
967
    cyg_flashaddr_t err_address;
968
} cyg_io_flash_getconfig_erase_t;
969
970
971
 In this structure, offset specifies
972
the offset within the block device to erase, len
973
specifies the amount to address, flasherr is
974
set on return to specify an error with the FLASH erase operation itself,
975
and err_address is used if there was an error
976
to specify at which address the error happened.
977
978
 
979
CYG_IO_GET_CONFIG_FLASH_LOCK
980
This protects a region of FLASH using the locking facilities
981
available on the card, if provided by the underlying driver.
982
cyg_io_get_config() must be passed a
983
structure defined as per the following:
984
985
typedef struct {
986
    CYG_ADDRESS offset;
987
    size_t len;
988
    int flasherr;
989
    cyg_flashaddr_t err_address;
990
} cyg_io_flash_getconfig_lock_t;
991
992
993
 In this structure, offset specifies
994
the offset within the block device to lock, len
995
specifies the amount to address, flasherr is
996
set on return to specify an error with the FLASH lock operation itself,
997
and err_address is used if there was an error
998
to specify at which address the error happened. If locking
999
support is not available -EINVAL will be returned from
1000
cyg_io_get_config().
1001
1002
 
1003
CYG_IO_GET_CONFIG_FLASH_UNLOCK
1004
This disables protection for a region of FLASH using the
1005
unlocking facilities available on the card, if provided by the underlying driver.
1006
cyg_io_get_config() must be passed a
1007
structure defined as per the following:
1008
1009
typedef struct {
1010
    CYG_ADDRESS offset;
1011
    size_t len;
1012
    int flasherr;
1013
    cyg_flashaddr_t err_address;
1014
} cyg_io_flash_getconfig_unlock_t;
1015
1016
1017
 In this structure, offset specifies
1018
the offset within the block device to unlock, len
1019
specifies the amount to address, flasherr is
1020
set on return to specify an error with the FLASH unlock operation itself,
1021
and err_address is used if there was an error
1022
to specify at which address the error happened. If unlocking
1023
support is not available -EINVAL will be returned from
1024
cyg_io_get_config().
1025
1026
 
1027
CYG_IO_GET_CONFIG_FLASH_DEVSIZE
1028
This returns the size of the FLASH block device. The
1029
cyg_io_get_config() function must be passed a
1030
structure defined as per the following, which is also supplied
1031
in <cyg/io/flash.h>:
1032
1033
typedef struct {
1034
    size_t dev_size;
1035
} cyg_io_flash_getconfig_devsize_t;
1036
1037
1038
 In this structure, dev_size is used to
1039
return the size of the FLASH device.
1040
1041
 
1042
CYG_IO_GET_CONFIG_FLASH_DEVADDR
1043
This returns the address in the virtual memory map that the
1044
generic flash layer has been informed that this FLASH device is mapped to. Note
1045
that some flash devices such as dataflash are not truly memory mapped,
1046
and so this function only returns useful information when used with a true
1047
memory mapped FLASH device. The
1048
cyg_io_get_config() function must be passed a
1049
structure defined as per the following, which is also supplied
1050
in <cyg/io/flash.h>:
1051
1052
typedef struct {
1053
    cyg_flashaddr_t dev_addr;
1054
} cyg_io_flash_getconfig_devaddr_t;
1055
1056
1057
 In this structure, dev_addr is used to
1058
return the address corresponding to the base of the FLASH device in the
1059
virtual memory map.
1060
1061
 
1062
CYG_IO_GET_CONFIG_FLASH_BLOCKSIZE
1063
This returns the size of a FLASH block at a
1064
supplied offset in the FLASH block device. The
1065
cyg_io_get_config() function must be passed a
1066
structure defined as per the following, which is also supplied
1067
in <cyg/io/flash.h>:
1068
1069
typedef struct {
1070
    CYG_ADDRESS offset;
1071
    size_t block_size;
1072
} cyg_io_flash_getconfig_blocksize_t;
1073
1074
1075
 In this structure, offset specifies the
1076
address within the block device of which the FLASH block size is
1077
required - a single FLASH device may contain blocks of differing
1078
sizes. The block_size field is used to
1079
return the block size at the specified offset.
1080
1081
1082
1083
1084

powered by: WebSVN 2.1.0

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