Mercurial > flash_v2
annotate packages/io/flash/current/src/flash.c @ 1747:a221170fac3d
Add an optimization for the common case of a single flash device
| author | bartv |
|---|---|
| date | Sun, 21 Nov 2004 23:39:01 +0000 |
| parents | 172edf8a96d8 |
| children | 80232dbac7fb |
| rev | line source |
|---|---|
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
1 //========================================================================== |
|
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
2 // |
|
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
3 // flash.c |
|
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
4 // |
|
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
5 // Flash programming |
|
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
6 // |
|
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
7 //========================================================================== |
|
208
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
195
diff
changeset
|
8 //####ECOSGPLCOPYRIGHTBEGIN#### |
|
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
195
diff
changeset
|
9 // ------------------------------------------- |
|
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
195
diff
changeset
|
10 // This file is part of eCos, the Embedded Configurable Operating System. |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
11 // Copyright (C) 2004 Andrew Lunn |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
12 // Copyright (C) 2004 eCosCentric Ltd. |
|
1223
dbcfe4e0dbd2
New platform - TAMS MOAB - and generic changes to support it
gthomas
parents:
210
diff
changeset
|
13 // Copyright (C) 2003 Gary Thomas |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
14 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc. |
|
208
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
195
diff
changeset
|
15 // |
|
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
195
diff
changeset
|
16 // eCos is free software; you can redistribute it and/or modify it under |
|
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
195
diff
changeset
|
17 // the terms of the GNU General Public License as published by the Free |
|
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
195
diff
changeset
|
18 // Software Foundation; either version 2 or (at your option) any later version. |
|
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
195
diff
changeset
|
19 // |
|
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
195
diff
changeset
|
20 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY |
|
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
195
diff
changeset
|
21 // WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
195
diff
changeset
|
22 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
195
diff
changeset
|
23 // for more details. |
|
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
195
diff
changeset
|
24 // |
|
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
195
diff
changeset
|
25 // You should have received a copy of the GNU General Public License along |
|
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
195
diff
changeset
|
26 // with eCos; if not, write to the Free Software Foundation, Inc., |
|
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
195
diff
changeset
|
27 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
|
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
195
diff
changeset
|
28 // |
|
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
195
diff
changeset
|
29 // As a special exception, if other files instantiate templates or use macros |
|
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
195
diff
changeset
|
30 // or inline functions from this file, or you compile this file and link it |
|
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
195
diff
changeset
|
31 // with other works to produce a work based on this file, this file does not |
|
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
195
diff
changeset
|
32 // by itself cause the resulting work to be covered by the GNU General Public |
|
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
195
diff
changeset
|
33 // License. However the source code for this file must still be made available |
|
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
195
diff
changeset
|
34 // in accordance with section (3) of the GNU General Public License. |
|
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
195
diff
changeset
|
35 // |
|
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
195
diff
changeset
|
36 // This exception does not invalidate any other reasons why a work based on |
|
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
195
diff
changeset
|
37 // this file might be covered by the GNU General Public License. |
|
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
195
diff
changeset
|
38 // |
|
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
195
diff
changeset
|
39 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc. |
|
210
d2c90368aeef
Merge from eCos master repository on 2002-05-23-21:39:14-BST
jlarmour
parents:
208
diff
changeset
|
40 // at http://sources.redhat.com/ecos/ecos-license/ |
|
208
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
195
diff
changeset
|
41 // ------------------------------------------- |
|
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
195
diff
changeset
|
42 //####ECOSGPLCOPYRIGHTEND#### |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
43 //========================================================================== |
|
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
44 //#####DESCRIPTIONBEGIN#### |
|
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
45 // |
|
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
46 // Author(s): gthomas |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
47 // Contributors: gthomas, Andrew Lunn, Bart Veer |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
48 // Date: 2000-07-26 |
|
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
49 // Purpose: |
|
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
50 // Description: |
|
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
51 // |
|
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
52 //####DESCRIPTIONEND#### |
|
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
53 // |
|
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
54 //========================================================================== |
|
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
55 |
|
119
77cec8369160
Merge from eCos master repository on 2000-09-01-07:47:03-BST
jlarmour
parents:
117
diff
changeset
|
56 #include <pkgconf/system.h> |
|
77cec8369160
Merge from eCos master repository on 2000-09-01-07:47:03-BST
jlarmour
parents:
117
diff
changeset
|
57 #include <pkgconf/io_flash.h> |
| 1706 | 58 #ifdef CYGPKG_KERNEL |
| 59 #include <cyg/kernel/kapi.h> | |
| 60 #endif | |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
61 #include <cyg/hal/hal_arch.h> |
|
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
62 #include <cyg/hal/hal_intr.h> |
|
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
63 #include <cyg/hal/hal_cache.h> |
| 1706 | 64 #include <cyg/hal/hal_tables.h> |
| 65 #include <cyg/infra/cyg_ass.h> | |
|
191
678094f34118
Merge from eCos master repository on 2001-10-19-06:43:02-BST
jlarmour
parents:
177
diff
changeset
|
66 #include <string.h> |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
67 |
|
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
68 #define _FLASH_PRIVATE_ |
|
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
69 #include <cyg/io/flash.h> |
|
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
70 |
|
143
6b5f480c6929
Merge from eCos master repository on 2000-12-08-02:09:36-GMT
jlarmour
parents:
141
diff
changeset
|
71 // When this flag is set, do not actually jump to the relocated code. |
| 1706 | 72 // This can be used for running the function in place (RAM startup |
| 73 // only), allowing calls to diag_printf() and similar. | |
|
136
a9ac27ec7abc
Merge from eCos master repository on 2000-11-17-18:24:25-GMT
jlarmour
parents:
121
diff
changeset
|
74 #undef RAM_FLASH_DEV_DEBUG |
|
a9ac27ec7abc
Merge from eCos master repository on 2000-11-17-18:24:25-GMT
jlarmour
parents:
121
diff
changeset
|
75 #if !defined(CYG_HAL_STARTUP_RAM) && defined(RAM_FLASH_DEV_DEBUG) |
|
a9ac27ec7abc
Merge from eCos master repository on 2000-11-17-18:24:25-GMT
jlarmour
parents:
121
diff
changeset
|
76 # warning "Can only enable the flash debugging when configured for RAM startup" |
|
a9ac27ec7abc
Merge from eCos master repository on 2000-11-17-18:24:25-GMT
jlarmour
parents:
121
diff
changeset
|
77 #endif |
| 1400 | 78 |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
79 // Optional verbosity. Using a macro here avoids lots of ifdefs in the |
|
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
80 // rest of the code. |
|
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
81 #ifdef CYGSEM_IO_FLASH_CHATTER |
|
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
82 # define CHATTER(_dev_, _fmt_, ...) (*(_dev_)->pf)((_fmt_), ## __VA_ARGS__) |
|
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
83 #else |
|
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
84 # define CHATTER(_dev_, _fmt_, ...) CYG_EMPTY_STATEMENT |
|
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
85 #endif |
|
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
86 |
|
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
87 // Per-thread locking. Again using macros avoids lots of ifdefs |
|
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
88 #ifdef CYGPKG_KERNEL |
|
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
89 # define LOCK_INIT(_dev_) cyg_mutex_init(&((_dev_)->mutex)) |
|
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
90 # define LOCK(_dev_) cyg_mutex_lock(&((_dev_)->mutex)) |
|
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
91 # define UNLOCK(_dev_) cyg_mutex_unlock(&((_dev_)->mutex)) |
|
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
92 #else |
|
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
93 # define LOCK_INIT(_dev_) CYG_EMPTY_STATEMENT |
|
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
94 # define LOCK(_dev_) CYG_EMPTY_STATEMENT |
|
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
95 # define UNLOCK(_dev_) CYG_EMPTY_STATEMENT |
|
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
96 #endif |
|
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
97 |
|
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
98 // Software write-protect. Very rarely used. |
|
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
99 #ifdef CYGSEM_IO_FLASH_SOFT_WRITE_PROTECT |
|
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
100 # define CHECK_SOFT_WRITE_PROTECT(_addr_, _len_) \ |
|
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
101 CYG_MACRO_START \ |
|
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
102 if (plf_flash_query_soft_wp((_addr_), (_len_))) \ |
|
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
103 return CYG_FLASH_ERR_PROTECT; \ |
|
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
104 CYG_MACRO_END |
|
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
105 #else |
|
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
106 #define CHECK_SOFT_WRITE_PROTECT(_addr_, _len_) CYG_EMPTY_STATEMENT |
|
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
107 #endif |
|
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
108 |
|
1747
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
109 // Has the FLASH IO library been initialised? |
| 1706 | 110 static bool init = false; |
| 111 | |
| 112 // This array contains entries for all flash devices that are | |
| 113 // installed in the system. | |
| 114 __externC struct cyg_flash_dev cyg_flashdevtab[]; | |
| 115 CYG_HAL_TABLE_BEGIN(cyg_flashdevtab, cyg_flashdev); | |
| 116 | |
| 117 // end of the flashdev table | |
| 118 __externC struct cyg_flash_dev cyg_flashdevtab_end; | |
| 119 CYG_HAL_TABLE_END(cyg_flashdevtab_end, cyg_flashdev); | |
| 1400 | 120 |
|
1747
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
121 #if (1 == CYGHWR_IO_FLASH_DEVICE) |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
122 |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
123 // Optimize the code for a single flash device, which is the common case. |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
124 // The flash subsystem must have been initialized, the single device must |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
125 // contain the specified address, and the device itself must have |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
126 // initialized successfully. |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
127 static struct cyg_flash_dev* |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
128 find_dev(cyg_flashaddr_t addr, int* stat) |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
129 { |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
130 if (!init) { |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
131 *stat = CYG_FLASH_ERR_NOT_INIT; |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
132 return NULL; |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
133 } |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
134 if (! ((addr >= cyg_flashdevtab[0].start) && (addr <= cyg_flashdevtab[0].end))) { |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
135 *stat = CYG_FLASH_ERR_INVALID; |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
136 return NULL; |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
137 } |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
138 if (! cyg_flashdevtab[0].init) { |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
139 *stat = CYG_FLASH_ERR_NOT_INIT; |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
140 return NULL; |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
141 } |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
142 return &cyg_flashdevtab[0]; |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
143 } |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
144 |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
145 #else |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
146 |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
147 // There are multiple devices. For convenience these are kept in a |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
148 // linked list, sorted by address. This is the head of the list |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
149 static struct cyg_flash_dev *flash_head = NULL; |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
150 |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
151 static bool flash_sort_and_check(void) |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
152 { |
| 1706 | 153 bool moved; |
| 154 struct cyg_flash_dev *dev, **previous_next; | |
|
1747
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
155 |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
156 // Place all devices on the list, unsorted for now. |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
157 for (dev = &cyg_flashdevtab[0]; dev != &cyg_flashdevtab_end; dev++) { |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
158 dev->next = flash_head; |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
159 flash_head = dev; |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
160 } |
| 1706 | 161 |
|
1747
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
162 // If there are no devices, abort. This should not happen because of |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
163 // the constraints on CYGHWR_IO_FLASH_DEVICE. |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
164 if (flash_head == NULL) { |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
165 return false; |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
166 } |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
167 |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
168 // Sort the linked list into ascending order of flash address. Use a |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
169 // primitive ripple sort, but since we don't expect to have many |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
170 // devices this should be OK. |
| 1706 | 171 do { |
| 172 moved=false; | |
| 173 for (dev=flash_head, previous_next=&flash_head; | |
| 174 dev->next; | |
| 175 previous_next = &dev->next, dev=dev->next ){ | |
| 176 if (dev->start > dev->next->start) { | |
| 177 *previous_next=dev->next; | |
| 178 dev->next = (*previous_next)->next; | |
| 179 (*previous_next)->next = dev; | |
| 180 moved=true; | |
| 181 break; | |
| 182 } | |
| 183 } | |
| 184 } while (moved); | |
|
1747
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
185 |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
186 // Now walk the linked list and see if there are any overlaps in the |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
187 // addresses the devices claim to use using. |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
188 for (dev=flash_head; dev->next; dev=dev->next){ |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
189 if (dev->end >= dev->next->start) |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
190 return false; |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
191 } |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
192 return true; |
| 1706 | 193 } |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
194 |
|
1747
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
195 // Find the device at the specified address, if any. |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
196 static struct cyg_flash_dev* |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
197 find_dev(cyg_flashaddr_t addr, int* stat) |
| 1706 | 198 { |
|
1747
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
199 struct cyg_flash_dev* dev; |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
200 if (!init) { |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
201 *stat = CYG_FLASH_ERR_NOT_INIT; |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
202 return NULL; |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
203 } |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
204 for (dev = flash_head; dev; dev = dev->next) { |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
205 if ((dev->start <= addr) && (addr <= dev->end)) { |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
206 if (! dev->init) { |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
207 *stat = CYG_FLASH_ERR_NOT_INIT; |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
208 return NULL; |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
209 } |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
210 return dev; |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
211 } |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
212 } |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
213 *stat = CYG_FLASH_ERR_INVALID; |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
214 return NULL; |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
215 } |
| 1706 | 216 |
|
1747
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
217 #endif |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
218 |
| 1706 | 219 // Initialise all registered device. Any device that fails to |
| 220 // initialise we leave dev->init as false. Then sort the devices into | |
| 221 // ascending order of address and put them into a linked list. Lastly | |
| 222 // check if we have any overlap of the addresses. | |
| 223 __externC int | |
| 224 cyg_flash_init(cyg_flash_printf *pf) | |
|
154
e2d866e32dac
Merge from eCos master repository on 2001-02-26-23:18:05-GMT
jlarmour
parents:
151
diff
changeset
|
225 { |
| 1706 | 226 int err; |
| 227 struct cyg_flash_dev * dev; | |
| 228 | |
| 229 if (init) return CYG_FLASH_ERR_OK; | |
|
1747
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
230 |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
231 CYG_ASSERT(&(cyg_flashdevtab[CYGHWR_IO_FLASH_DEVICE]) == &cyg_flashdevtab_end, "incorrect number of flash devices"); |
| 1706 | 232 |
| 233 for (dev = &cyg_flashdevtab[0]; dev != &cyg_flashdevtab_end; dev++) { | |
| 234 dev->pf = pf; | |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
235 LOCK_INIT(dev); |
| 1706 | 236 |
| 237 err = dev->funs->flash_init(dev); | |
| 238 if (err != CYG_FLASH_ERR_OK) { | |
| 239 continue; | |
| 240 } | |
| 241 CYG_ASSERT(dev->funs, "No flash functions"); | |
|
1747
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
242 CYG_ASSERT(dev->num_block_infos, "No number of block infos"); |
| 1706 | 243 CYG_ASSERT(dev->block_info, "No block infos"); |
| 244 CYG_ASSERT(!(((cyg_flashaddr_t)dev->block_info >= dev->start) && | |
| 245 ((cyg_flashaddr_t)dev->block_info < dev->end)), | |
| 246 "Block info is in the flash"); | |
| 247 CYG_ASSERT(dev->funs->flash_erase_block, "No erase function"); | |
| 248 CYG_ASSERT(dev->funs->flash_program, "No program function"); | |
| 249 CYG_ASSERT(dev->funs->flash_hwr_map_error, "No hwr map error function"); | |
|
1729
e290a0372578
* src/flash.c (cyg_flash_init): Add assert checking that the
asl
parents:
1723
diff
changeset
|
250 #ifdef CYGDBG_USE_ASSERTS |
|
e290a0372578
* src/flash.c (cyg_flash_init): Add assert checking that the
asl
parents:
1723
diff
changeset
|
251 { |
|
e290a0372578
* src/flash.c (cyg_flash_init): Add assert checking that the
asl
parents:
1723
diff
changeset
|
252 int i; |
|
e290a0372578
* src/flash.c (cyg_flash_init): Add assert checking that the
asl
parents:
1723
diff
changeset
|
253 cyg_flashaddr_t addr = dev->start; |
|
e290a0372578
* src/flash.c (cyg_flash_init): Add assert checking that the
asl
parents:
1723
diff
changeset
|
254 for (i = 0; i < dev->num_block_infos; i++) { |
|
e290a0372578
* src/flash.c (cyg_flash_init): Add assert checking that the
asl
parents:
1723
diff
changeset
|
255 addr += dev->block_info[i].block_size * dev->block_info[i].blocks; |
|
e290a0372578
* src/flash.c (cyg_flash_init): Add assert checking that the
asl
parents:
1723
diff
changeset
|
256 } |
|
e290a0372578
* src/flash.c (cyg_flash_init): Add assert checking that the
asl
parents:
1723
diff
changeset
|
257 CYG_ASSERT(dev->end == addr-1, "Invalid end address"); |
|
e290a0372578
* src/flash.c (cyg_flash_init): Add assert checking that the
asl
parents:
1723
diff
changeset
|
258 } |
|
e290a0372578
* src/flash.c (cyg_flash_init): Add assert checking that the
asl
parents:
1723
diff
changeset
|
259 #endif |
| 1706 | 260 dev->init = true; |
| 261 } | |
| 262 | |
|
1747
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
263 #if (1 == CYGHWR_IO_FLASH_DEVICE) |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
264 // Make sure there is one device, otherwise we could end up |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
265 // accessing a non-existent cyg_flash_dev structure. |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
266 if (&(cyg_flashdevtab[0]) == &cyg_flashdevtab_end) { |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
267 return CYG_FLASH_ERR_INVALID; |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
268 } |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
269 #else |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
270 // Place the devices on a sorted linked list and check that there |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
271 // are no overlaps in the address space. |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
272 if (! flash_sort_and_check() ) { |
| 1706 | 273 return CYG_FLASH_ERR_INVALID; |
| 274 } | |
|
1747
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
275 #endif |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
276 |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
277 // Only mark the flash subsystem as initialized if the world is |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
278 // consistent. |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
279 init = true; |
| 1706 | 280 return CYG_FLASH_ERR_OK; |
|
154
e2d866e32dac
Merge from eCos master repository on 2001-02-26-23:18:05-GMT
jlarmour
parents:
151
diff
changeset
|
281 } |
|
e2d866e32dac
Merge from eCos master repository on 2001-02-26-23:18:05-GMT
jlarmour
parents:
151
diff
changeset
|
282 |
| 1706 | 283 // Is the address within one of the flash drivers? |
| 284 __externC int | |
| 285 cyg_flash_verify_addr(const cyg_flashaddr_t address) | |
|
143
6b5f480c6929
Merge from eCos master repository on 2000-12-08-02:09:36-GMT
jlarmour
parents:
141
diff
changeset
|
286 { |
|
1747
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
287 int stat = CYG_FLASH_ERR_OK; |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
288 (void) find_dev(address, &stat); |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
289 return stat; |
|
143
6b5f480c6929
Merge from eCos master repository on 2000-12-08-02:09:36-GMT
jlarmour
parents:
141
diff
changeset
|
290 } |
|
6b5f480c6929
Merge from eCos master repository on 2000-12-08-02:09:36-GMT
jlarmour
parents:
141
diff
changeset
|
291 |
| 1706 | 292 // Return information about the Nth driver |
| 293 __externC int | |
| 294 cyg_flash_get_info(cyg_uint32 Nth, cyg_flash_info_t * info) | |
| 295 { | |
| 296 struct cyg_flash_dev * dev; | |
| 297 | |
| 298 if (!init) return CYG_FLASH_ERR_NOT_INIT; | |
|
1747
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
299 |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
300 #if (1 == CYGHWR_IO_FLASH_DEVICE) |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
301 if (0 == Nth) { |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
302 dev = &(cyg_flashdevtab[0]); |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
303 } else { |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
304 return CYG_FLASH_ERR_INVALID; |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
305 } |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
306 #else |
| 1706 | 307 for (dev = flash_head; dev && Nth; dev=dev->next, Nth--) |
| 308 ; | |
|
1747
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
309 if (!dev || !dev->init) { |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
310 return CYG_FLASH_ERR_INVALID; |
| 1706 | 311 } |
|
1747
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
312 #endif |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
313 info->start = dev->start; |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
314 info->end = dev->end; |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
315 info->num_block_infos = dev->num_block_infos; |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
316 info->block_info = dev->block_info; |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
317 return CYG_FLASH_ERR_OK; |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
318 } |
|
157
b65ec671dd4c
Merge from eCos master repository on 2001-04-06-18:28:36-BST
jlarmour
parents:
156
diff
changeset
|
319 |
| 1706 | 320 // Return information about the flash at the given address |
| 321 __externC int | |
| 322 cyg_flash_get_info_addr(cyg_flashaddr_t flash_base, cyg_flash_info_t * info) | |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
323 { |
| 1706 | 324 struct cyg_flash_dev *dev; |
|
1747
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
325 int stat = CYG_FLASH_ERR_OK; |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
326 |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
327 dev = find_dev(flash_base, &stat); |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
328 if (dev) { |
| 1706 | 329 info->start = dev->start; |
| 330 info->end = dev->end; | |
| 331 info->num_block_infos = dev->num_block_infos; | |
| 332 info->block_info = dev->block_info; | |
| 333 } | |
|
1747
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
334 return stat; |
| 1706 | 335 } |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
336 |
| 1706 | 337 #ifdef CYGPKG_KERNEL |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
338 // Lock the mutex's for a range of addresses |
| 1706 | 339 __externC int |
| 340 cyg_flash_mutex_lock(const cyg_flashaddr_t from, const size_t len) | |
| 341 { | |
|
1747
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
342 struct cyg_flash_dev * dev; |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
343 int stat = CYG_FLASH_ERR_OK; |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
344 |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
345 dev = find_dev(from, &stat); |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
346 if (dev) { |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
347 LOCK(dev); |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
348 if (len > (dev->end + 1 - from)) { |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
349 stat = cyg_flash_mutex_lock(dev->end + 1, len - (dev->end + 1 - from)); |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
350 if (CYG_FLASH_ERR_OK != stat) { |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
351 // Something went wrong, unlock what we just locked |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
352 UNLOCK(dev); |
| 1706 | 353 } |
|
208
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
195
diff
changeset
|
354 } |
| 1706 | 355 } |
|
1747
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
356 return stat; |
| 1706 | 357 } |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
358 |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
359 // Unlock the mutex's for a range of addresses |
| 1706 | 360 __externC int |
| 361 cyg_flash_mutex_unlock(const cyg_flashaddr_t from, const size_t len) | |
| 362 { | |
|
1747
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
363 struct cyg_flash_dev * dev; |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
364 int stat = CYG_FLASH_ERR_OK; |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
365 |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
366 dev = find_dev(from, &stat); |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
367 if (dev) { |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
368 UNLOCK(dev); |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
369 if (len > (dev->end + 1 - from)) { |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
370 stat = cyg_flash_mutex_lock(dev->end + 1, len - (dev->end + 1 - from)); |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
371 if (CYG_FLASH_ERR_OK != stat) { |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
372 // Something went wrong, relock what we just unlocked. This may not |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
373 // be worth it since things must be pretty messed up, and could |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
374 // conceivably end in deadlock if there is a concurrent call to |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
375 // cyg_flash_mutex_lock(); |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
376 LOCK(dev); |
| 1706 | 377 } |
| 378 } | |
| 379 } | |
|
1747
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
380 return stat; |
| 1706 | 381 } |
|
1223
dbcfe4e0dbd2
New platform - TAMS MOAB - and generic changes to support it
gthomas
parents:
210
diff
changeset
|
382 #endif |
|
dbcfe4e0dbd2
New platform - TAMS MOAB - and generic changes to support it
gthomas
parents:
210
diff
changeset
|
383 |
| 1706 | 384 // Return the size of the block which is at the given address |
| 385 static size_t | |
| 386 flash_block_size(struct cyg_flash_dev *dev, const cyg_flashaddr_t addr) | |
| 387 { | |
| 388 int i; | |
| 389 size_t offset; | |
| 390 | |
| 391 CYG_ASSERT((addr >= dev->start) && (addr <= dev->end), "Not inside device"); | |
| 392 | |
| 393 offset = addr - dev->start; | |
| 394 for (i=0; i < dev->num_block_infos; i++) { | |
| 395 if (offset < (dev->block_info[i].blocks * dev->block_info[i].block_size)) | |
| 396 return dev->block_info[i].block_size; | |
| 397 offset = offset - | |
| 398 (dev->block_info[i].blocks * dev->block_info[i].block_size); | |
| 399 } | |
| 400 CYG_FAIL("Programming error"); | |
| 401 return 0; | |
| 402 } | |
| 403 | |
| 404 // Return the size of the block which is at the given address | |
| 405 __externC size_t | |
| 406 cyg_flash_block_size(const cyg_flashaddr_t flash_base) | |
| 407 { | |
|
1747
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
408 struct cyg_flash_dev * dev; |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
409 int stat; |
| 1706 | 410 |
|
1747
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
411 dev = find_dev(flash_base, &stat); |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
412 if (!dev) return stat; |
| 1706 | 413 return flash_block_size(dev, flash_base); |
| 414 } | |
| 415 | |
|
1723
0cc13d07a67e
* src/flash.c: Support flash blocks of arbitary size. The
asl
parents:
1722
diff
changeset
|
416 // Return the first address of a block. The flash might not be aligned |
|
0cc13d07a67e
* src/flash.c: Support flash blocks of arbitary size. The
asl
parents:
1722
diff
changeset
|
417 // in terms of its block size. So we have to be careful and use |
|
0cc13d07a67e
* src/flash.c: Support flash blocks of arbitary size. The
asl
parents:
1722
diff
changeset
|
418 // offsets. |
|
0cc13d07a67e
* src/flash.c: Support flash blocks of arbitary size. The
asl
parents:
1722
diff
changeset
|
419 static inline cyg_flashaddr_t |
|
0cc13d07a67e
* src/flash.c: Support flash blocks of arbitary size. The
asl
parents:
1722
diff
changeset
|
420 flash_block_begin(cyg_flashaddr_t addr, struct cyg_flash_dev *dev) |
|
0cc13d07a67e
* src/flash.c: Support flash blocks of arbitary size. The
asl
parents:
1722
diff
changeset
|
421 { |
|
0cc13d07a67e
* src/flash.c: Support flash blocks of arbitary size. The
asl
parents:
1722
diff
changeset
|
422 size_t block_size; |
|
0cc13d07a67e
* src/flash.c: Support flash blocks of arbitary size. The
asl
parents:
1722
diff
changeset
|
423 cyg_flashaddr_t offset; |
|
0cc13d07a67e
* src/flash.c: Support flash blocks of arbitary size. The
asl
parents:
1722
diff
changeset
|
424 |
|
0cc13d07a67e
* src/flash.c: Support flash blocks of arbitary size. The
asl
parents:
1722
diff
changeset
|
425 block_size = flash_block_size(dev, addr); |
|
0cc13d07a67e
* src/flash.c: Support flash blocks of arbitary size. The
asl
parents:
1722
diff
changeset
|
426 |
|
0cc13d07a67e
* src/flash.c: Support flash blocks of arbitary size. The
asl
parents:
1722
diff
changeset
|
427 offset = addr - dev->start; |
|
0cc13d07a67e
* src/flash.c: Support flash blocks of arbitary size. The
asl
parents:
1722
diff
changeset
|
428 offset = (offset / block_size) * block_size; |
|
0cc13d07a67e
* src/flash.c: Support flash blocks of arbitary size. The
asl
parents:
1722
diff
changeset
|
429 return offset + dev->start; |
|
0cc13d07a67e
* src/flash.c: Support flash blocks of arbitary size. The
asl
parents:
1722
diff
changeset
|
430 } |
|
0cc13d07a67e
* src/flash.c: Support flash blocks of arbitary size. The
asl
parents:
1722
diff
changeset
|
431 |
|
208
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
195
diff
changeset
|
432 |
| 1706 | 433 __externC int |
| 434 cyg_flash_erase(const cyg_flashaddr_t flash_base, | |
| 435 const size_t len, | |
| 436 cyg_flashaddr_t *err_address) | |
| 437 { | |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
438 cyg_flashaddr_t block, end_addr; |
| 1706 | 439 struct cyg_flash_dev * dev; |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
440 size_t erase_count; |
| 1706 | 441 int stat = CYG_FLASH_ERR_OK; |
| 442 int d_cache, i_cache; | |
|
1747
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
443 |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
444 dev = find_dev(flash_base, &stat); |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
445 if (!dev) return stat; |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
446 |
|
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
447 CHECK_SOFT_WRITE_PROTECT(flash_base, len); |
| 1706 | 448 |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
449 LOCK(dev); |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
450 |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
451 // Check whether or not we are going past the end of this device, on |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
452 // to the next one. If so the next device will be handled by a |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
453 // recursive call later on. |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
454 if (len > (dev->end + 1 - flash_base)) { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
455 end_addr = dev->end; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
456 } else { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
457 end_addr = flash_base + len - 1; |
| 1706 | 458 } |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
459 // erase can only happen on a block boundary, so adjust for this |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
460 block = flash_block_begin(flash_base, dev); |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
461 erase_count = (end_addr + 1) - block; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
462 |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
463 CHATTER(dev, "... Erase from %p-%p: ", (void*)block, (void*)end_addr); |
| 1706 | 464 |
| 465 HAL_FLASH_CACHES_OFF(d_cache, i_cache); | |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
466 FLASH_Enable(flash_base, end_addr); |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
467 while (erase_count > 0) { |
| 1706 | 468 int i; |
| 469 unsigned char *dp; | |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
470 bool erased = false; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
471 size_t block_size = flash_block_size(dev, block); |
| 1706 | 472 |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
473 // Pad to the block boundary, if necessary |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
474 if (erase_count < block_size) { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
475 erase_count = block_size; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
476 } |
|
1723
0cc13d07a67e
* src/flash.c: Support flash blocks of arbitary size. The
asl
parents:
1722
diff
changeset
|
477 |
| 1706 | 478 // If there is a read function it probably means the flash |
| 479 // cannot be read directly. | |
| 480 if (!dev->funs->flash_read) { | |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
481 erased = true; |
| 1706 | 482 dp = (unsigned char *)block; |
| 483 for (i = 0; i < block_size; i++) { | |
| 484 if (*dp++ != (unsigned char)0xFF) { | |
| 485 erased = false; | |
| 486 break; | |
| 487 } | |
| 488 } | |
| 489 } | |
| 490 if (!erased) { | |
| 491 stat = dev->funs->flash_erase_block(dev,block); | |
| 492 stat = dev->funs->flash_hwr_map_error(dev,stat); | |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
493 } |
| 1706 | 494 if (CYG_FLASH_ERR_OK != stat && err_address) { |
| 495 *err_address = block; | |
| 496 break; | |
| 497 } | |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
498 block += block_size; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
499 erase_count -= block_size; |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
500 CHATTER(dev, "."); |
| 1706 | 501 } |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
502 FLASH_Disable(flash_base, end_addr); |
| 1706 | 503 HAL_FLASH_CACHES_ON(d_cache, i_cache); |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
504 CHATTER(dev, "\n"); |
|
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
505 UNLOCK(dev); |
| 1706 | 506 if (stat != CYG_FLASH_ERR_OK) { |
| 507 return stat; | |
| 508 } | |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
509 |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
510 // If there are multiple flash devices in series the erase operation |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
511 // may touch successive devices. This can be handled by recursion. |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
512 // The stack overheads should be minimal because the number of |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
513 // devices will be small. |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
514 if (len > (dev->end + 1 - flash_base)) { |
| 1706 | 515 return cyg_flash_erase(dev->end+1, |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
516 len - (dev->end + 1 - flash_base), |
| 1706 | 517 err_address); |
| 518 } | |
| 519 return CYG_FLASH_ERR_OK; | |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
520 } |
|
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
521 |
| 1706 | 522 __externC int |
| 523 cyg_flash_program(const cyg_flashaddr_t flash_base, | |
| 524 void *ram_base, | |
| 525 const size_t len, | |
| 526 cyg_flashaddr_t *err_address) | |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
527 { |
| 1706 | 528 struct cyg_flash_dev * dev; |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
529 cyg_flashaddr_t addr, end_addr, block; |
| 1706 | 530 unsigned char * ram = ram_base; |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
531 size_t write_count, offset; |
| 1706 | 532 int stat = CYG_FLASH_ERR_OK; |
| 533 int d_cache, i_cache; | |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
534 |
|
1747
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
535 dev = find_dev(flash_base, &stat); |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
536 if (!dev) return stat; |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
537 |
|
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
538 CHECK_SOFT_WRITE_PROTECT(flash_base, len); |
| 1706 | 539 |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
540 LOCK(dev); |
| 1706 | 541 addr = flash_base; |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
542 if (len > (dev->end + 1 - flash_base)) { |
| 1706 | 543 end_addr = dev->end; |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
544 } else { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
545 end_addr = flash_base + len - 1; |
| 1706 | 546 } |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
547 write_count = (end_addr + 1) - flash_base; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
548 |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
549 // The first write may be in the middle of a block. Do the necessary |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
550 // adjustment here rather than inside the loop. |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
551 block = flash_block_begin(flash_base, dev); |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
552 if (addr == block) { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
553 offset = 0; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
554 } else { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
555 offset = addr - block; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
556 } |
| 1706 | 557 |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
558 CHATTER(dev, "... Program from %p-%p to %p: ", ram_base, ((CYG_ADDRESS)ram_base)+write_count, addr); |
| 1706 | 559 |
| 560 HAL_FLASH_CACHES_OFF(d_cache, i_cache); | |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
561 FLASH_Enable(flash_base, end_addr); |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
562 while (write_count > 0) { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
563 size_t block_size = flash_block_size(dev, addr); |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
564 size_t this_write; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
565 if (write_count > (block_size - offset)) { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
566 this_write = block_size - offset; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
567 } else { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
568 this_write = write_count; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
569 } |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
570 // Only the first block may need the offset. |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
571 offset = 0; |
| 1706 | 572 |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
573 stat = dev->funs->flash_program(dev, addr, ram, this_write); |
| 1706 | 574 stat = dev->funs->flash_hwr_map_error(dev,stat); |
|
157
b65ec671dd4c
Merge from eCos master repository on 2001-04-06-18:28:36-BST
jlarmour
parents:
156
diff
changeset
|
575 #ifdef CYGSEM_IO_FLASH_VERIFY_PROGRAM |
| 1706 | 576 if (CYG_FLASH_ERR_OK == stat) // Claims to be OK |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
577 if (!dev->funs->flash_read && memcmp((void *)addr, ram, this_write) != 0) { |
| 1706 | 578 stat = CYG_FLASH_ERR_DRV_VERIFY; |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
579 CHATTER(dev, "V"); |
| 1706 | 580 } |
|
157
b65ec671dd4c
Merge from eCos master repository on 2001-04-06-18:28:36-BST
jlarmour
parents:
156
diff
changeset
|
581 #endif |
| 1706 | 582 if (CYG_FLASH_ERR_OK != stat && err_address) { |
| 583 *err_address = addr; | |
| 584 break; | |
| 585 } | |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
586 CHATTER(dev, "."); |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
587 write_count -= this_write; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
588 addr += this_write; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
589 ram += this_write; |
| 1706 | 590 } |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
591 FLASH_Disable(flash_base, end_addr); |
| 1706 | 592 HAL_FLASH_CACHES_ON(d_cache, i_cache); |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
593 CHATTER(dev, "\n"); |
|
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
594 UNLOCK(dev); |
| 1706 | 595 if (stat != CYG_FLASH_ERR_OK) { |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
596 return (stat); |
| 1706 | 597 } |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
598 if (len > (dev->end + 1 - flash_base)) { |
| 1706 | 599 return cyg_flash_program(dev->end+1, ram, |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
600 len - (dev->end + 1 - flash_base), |
| 1706 | 601 err_address); |
| 602 } | |
| 603 return CYG_FLASH_ERR_OK; | |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
604 } |
|
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
605 |
| 1706 | 606 __externC int |
| 607 cyg_flash_read(cyg_flashaddr_t flash_base, | |
| 608 const void *ram_base, | |
| 609 const size_t len, | |
| 610 cyg_flashaddr_t *err_address) | |
|
1223
dbcfe4e0dbd2
New platform - TAMS MOAB - and generic changes to support it
gthomas
parents:
210
diff
changeset
|
611 { |
| 1706 | 612 struct cyg_flash_dev * dev; |
|
1742
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
613 cyg_flashaddr_t addr, end_addr; |
| 1706 | 614 unsigned char * ram = (unsigned char *)ram_base; |
|
1742
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
615 size_t read_count; |
| 1706 | 616 int stat = CYG_FLASH_ERR_OK; |
|
1747
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
617 |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
618 dev = find_dev(flash_base, &stat); |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
619 if (!dev) return stat; |
|
1223
dbcfe4e0dbd2
New platform - TAMS MOAB - and generic changes to support it
gthomas
parents:
210
diff
changeset
|
620 |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
621 LOCK(dev); |
| 1706 | 622 addr = flash_base; |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
623 if (len > (dev->end + 1 - flash_base)) { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
624 end_addr = dev->end; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
625 } else { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
626 end_addr = flash_base + len - 1; |
| 1706 | 627 } |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
628 read_count = (end_addr + 1) - flash_base; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
629 |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
630 CHATTER(dev, "... Read from %p-%p to %p: ", addr, end_addr, ram_base); |
|
1742
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
631 |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
632 // If the flash is directly accessible, just read it in one go. This |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
633 // still happens with the mutex locked to protect against concurrent |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
634 // programs/erases. |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
635 if (! dev->funs->flash_read) { |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
636 memcpy(ram, (void*)addr, read_count); |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
637 } else { |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
638 #ifndef CYGHWR_IO_FLASH_INDIRECT_READS |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
639 CYG_FAIL("read function supplied but indirect reads not enabled"); |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
640 stat = CYG_FLASH_ERR_PROTOCOL; |
| 1744 | 641 if (err_address) { |
| 642 *err_address = addr; | |
| 643 } | |
|
1742
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
644 #else |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
645 // We have to indirect through the device driver. |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
646 // The first read may be in the middle of a block. Do the necessary |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
647 // adjustment here rather than inside the loop. |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
648 int d_cache, i_cache; |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
649 size_t offset; |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
650 cyg_flashaddr_t block = flash_block_begin(flash_base, dev); |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
651 if (addr == block) { |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
652 offset = 0; |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
653 } else { |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
654 offset = addr - block; |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
655 } |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
656 HAL_FLASH_CACHES_OFF(d_cache, i_cache); |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
657 FLASH_Enable(flash_base, end_addr); |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
658 while (read_count > 0) { |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
659 size_t block_size = flash_block_size(dev, addr); |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
660 size_t this_read; |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
661 if (read_count > (block_size - offset)) { |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
662 this_read = block_size - offset; |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
663 } else { |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
664 this_read = read_count; |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
665 } |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
666 // Only the first block may need the offset |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
667 offset = 0; |
| 1706 | 668 |
|
1742
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
669 stat = dev->funs->flash_read(dev, addr, ram, this_read); |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
670 stat = dev->funs->flash_hwr_map_error(dev,stat); |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
671 if (CYG_FLASH_ERR_OK != stat && err_address) { |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
672 *err_address = addr; |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
673 break; |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
674 } |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
675 CHATTER(dev, "."); |
|
1742
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
676 read_count -= this_read; |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
677 addr += this_read; |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
678 ram += this_read; |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
679 } |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
680 FLASH_Disable(flash_base, end_addr); |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
681 HAL_FLASH_CACHES_ON(d_cache, i_cache); |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
682 #endif |
| 1706 | 683 } |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
684 CHATTER(dev, "\n"); |
|
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
685 UNLOCK(dev); |
| 1706 | 686 if (stat != CYG_FLASH_ERR_OK) { |
|
1223
dbcfe4e0dbd2
New platform - TAMS MOAB - and generic changes to support it
gthomas
parents:
210
diff
changeset
|
687 return (stat); |
| 1706 | 688 } |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
689 if (len > (dev->end + 1 - flash_base)) { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
690 return cyg_flash_read(dev->end+1, ram, |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
691 len - (dev->end + 1 - flash_base), |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
692 err_address); |
| 1706 | 693 } |
| 694 return CYG_FLASH_ERR_OK; | |
|
1223
dbcfe4e0dbd2
New platform - TAMS MOAB - and generic changes to support it
gthomas
parents:
210
diff
changeset
|
695 } |
|
dbcfe4e0dbd2
New platform - TAMS MOAB - and generic changes to support it
gthomas
parents:
210
diff
changeset
|
696 |
|
177
4c750ce71ae3
Merge from eCos master repository on 2001-08-10-19:22:57-BST
jlarmour
parents:
174
diff
changeset
|
697 #ifdef CYGHWR_IO_FLASH_BLOCK_LOCKING |
| 1706 | 698 __externC int |
| 699 cyg_flash_lock(const cyg_flashaddr_t flash_base, | |
| 700 const size_t len, | |
| 701 cyg_flashaddr_t *err_address) | |
|
119
77cec8369160
Merge from eCos master repository on 2000-09-01-07:47:03-BST
jlarmour
parents:
117
diff
changeset
|
702 { |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
703 cyg_flashaddr_t block, end_addr; |
| 1706 | 704 struct cyg_flash_dev * dev; |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
705 size_t lock_count; |
| 1706 | 706 int stat = CYG_FLASH_ERR_OK; |
| 707 int d_cache, i_cache; | |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
708 |
|
1747
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
709 dev = find_dev(flash_base, &stat); |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
710 if (!dev) return stat; |
| 1706 | 711 if (!dev->funs->flash_block_lock) return CYG_FLASH_ERR_INVALID; |
|
119
77cec8369160
Merge from eCos master repository on 2000-09-01-07:47:03-BST
jlarmour
parents:
117
diff
changeset
|
712 |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
713 CHECK_SOFT_WRITE_PROTECT(flash_base, len); |
|
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
714 |
|
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
715 LOCK(dev); |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
716 if (len > (dev->end + 1 - flash_base)) { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
717 end_addr = dev->end; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
718 } else { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
719 end_addr = flash_base + len - 1; |
| 1706 | 720 } |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
721 block = flash_block_begin(flash_base, dev); |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
722 lock_count = (end_addr + 1) - block; |
| 1706 | 723 |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
724 CHATTER(dev, "... Locking from %p-%p: ", (void*)block, (void*)end_addr); |
| 1706 | 725 |
| 726 HAL_FLASH_CACHES_OFF(d_cache, i_cache); | |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
727 FLASH_Enable(flash_base, end_addr); |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
728 while (lock_count > 0) { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
729 size_t block_size = flash_block_size(dev, block); |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
730 if (lock_count < block_size) { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
731 lock_count = block_size; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
732 } |
| 1706 | 733 stat = dev->funs->flash_block_lock(dev,block); |
| 734 stat = dev->funs->flash_hwr_map_error(dev,stat); | |
| 735 | |
| 736 if (CYG_FLASH_ERR_OK != stat && err_address) { | |
|
1715
868c3fa9315f
* src/flash.c: Make the flock lock/unlock code compile without
asl
parents:
1706
diff
changeset
|
737 *err_address = block; |
| 1706 | 738 break; |
|
119
77cec8369160
Merge from eCos master repository on 2000-09-01-07:47:03-BST
jlarmour
parents:
117
diff
changeset
|
739 } |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
740 block += block_size; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
741 lock_count -= block_size; |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
742 CHATTER(dev, "."); |
| 1706 | 743 } |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
744 FLASH_Disable(flash_base, end_addr); |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
745 HAL_FLASH_CACHES_ON(d_cache, i_cache); |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
746 CHATTER(dev, "\n"); |
|
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
747 UNLOCK(dev); |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
748 if (stat != CYG_FLASH_ERR_OK) { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
749 return stat; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
750 } |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
751 |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
752 // Recurse if necessary for the next device |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
753 if (len > (dev->end + 1 - flash_base)) { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
754 return cyg_flash_lock(dev->end+1, |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
755 len - (dev->end + 1 - flash_base), |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
756 err_address); |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
757 } |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
758 |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
759 return CYG_FLASH_ERR_OK; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
760 } |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
761 |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
762 __externC int |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
763 cyg_flash_unlock(const cyg_flashaddr_t flash_base, |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
764 const size_t len, |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
765 cyg_flashaddr_t *err_address) |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
766 { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
767 cyg_flashaddr_t block, end_addr; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
768 struct cyg_flash_dev * dev; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
769 size_t unlock_count; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
770 int stat = CYG_FLASH_ERR_OK; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
771 int d_cache, i_cache; |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
772 |
|
1747
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
773 dev = find_dev(flash_base, &stat); |
|
a221170fac3d
Add an optimization for the common case of a single flash device
bartv
parents:
1746
diff
changeset
|
774 if (!dev) return stat; |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
775 if (!dev->funs->flash_block_unlock) return CYG_FLASH_ERR_INVALID; |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
776 |
|
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
777 CHECK_SOFT_WRITE_PROTECT(flash_base, len); |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
778 |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
779 LOCK(dev); |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
780 if (len > (dev->end + 1 - flash_base)) { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
781 end_addr = dev->end; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
782 } else { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
783 end_addr = flash_base + len - 1; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
784 } |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
785 block = flash_block_begin(flash_base, dev); |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
786 unlock_count = (end_addr + 1) - block; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
787 |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
788 CHATTER(dev, "... Unlocking from %p-%p: ", (void*)block, (void*)end_addr); |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
789 |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
790 HAL_FLASH_CACHES_OFF(d_cache, i_cache); |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
791 FLASH_Enable(flash_base, end_addr); |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
792 while (unlock_count > 0) { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
793 size_t block_size = flash_block_size(dev, block); |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
794 if (unlock_count < block_size) { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
795 unlock_count = block_size; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
796 } |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
797 stat = dev->funs->flash_block_unlock(dev,block); |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
798 stat = dev->funs->flash_hwr_map_error(dev,stat); |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
799 |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
800 if (CYG_FLASH_ERR_OK != stat && err_address) { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
801 *err_address = block; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
802 break; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
803 } |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
804 block += block_size; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
805 unlock_count -= block_size; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
806 |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
807 CHATTER(dev, "."); |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
808 } |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
809 FLASH_Disable(flash_base, end_addr); |
| 1706 | 810 HAL_FLASH_CACHES_ON(d_cache, i_cache); |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
811 CHATTER(dev, "\n"); |
|
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
812 UNLOCK(dev); |
| 1706 | 813 if (stat != CYG_FLASH_ERR_OK) { |
| 814 return stat; | |
| 815 } | |
| 816 | |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
817 // Recurse if necessary for the next device |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
818 if (len > (dev->end + 1 - flash_base)) { |
| 1706 | 819 return cyg_flash_lock(dev->end+1, |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
820 len - (dev->end + 1 - flash_base), |
| 1706 | 821 err_address); |
| 822 } | |
| 823 return CYG_FLASH_ERR_OK; | |
|
119
77cec8369160
Merge from eCos master repository on 2000-09-01-07:47:03-BST
jlarmour
parents:
117
diff
changeset
|
824 } |
|
77cec8369160
Merge from eCos master repository on 2000-09-01-07:47:03-BST
jlarmour
parents:
117
diff
changeset
|
825 #endif |
|
77cec8369160
Merge from eCos master repository on 2000-09-01-07:47:03-BST
jlarmour
parents:
117
diff
changeset
|
826 |
| 1706 | 827 const char * |
| 828 cyg_flash_errmsg(const int err) | |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
829 { |
|
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
830 switch (err) { |
| 1706 | 831 case CYG_FLASH_ERR_OK: |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
832 return "No error - operation complete"; |
| 1706 | 833 case CYG_FLASH_ERR_ERASE_SUSPEND: |
|
136
a9ac27ec7abc
Merge from eCos master repository on 2000-11-17-18:24:25-GMT
jlarmour
parents:
121
diff
changeset
|
834 return "Device is in erase suspend state"; |
| 1706 | 835 case CYG_FLASH_ERR_PROGRAM_SUSPEND: |
|
136
a9ac27ec7abc
Merge from eCos master repository on 2000-11-17-18:24:25-GMT
jlarmour
parents:
121
diff
changeset
|
836 return "Device is in program suspend state"; |
| 1706 | 837 case CYG_FLASH_ERR_INVALID: |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
838 return "Invalid FLASH address"; |
| 1706 | 839 case CYG_FLASH_ERR_ERASE: |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
840 return "Error trying to erase"; |
| 1706 | 841 case CYG_FLASH_ERR_LOCK: |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
842 return "Error trying to lock/unlock"; |
| 1706 | 843 case CYG_FLASH_ERR_PROGRAM: |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
844 return "Error trying to program"; |
| 1706 | 845 case CYG_FLASH_ERR_PROTOCOL: |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
846 return "Generic error"; |
| 1706 | 847 case CYG_FLASH_ERR_PROTECT: |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
848 return "Device/region is write-protected"; |
| 1706 | 849 case CYG_FLASH_ERR_NOT_INIT: |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
850 return "FLASH sub-system not initialized"; |
| 1706 | 851 case CYG_FLASH_ERR_DRV_VERIFY: |
|
143
6b5f480c6929
Merge from eCos master repository on 2000-12-08-02:09:36-GMT
jlarmour
parents:
141
diff
changeset
|
852 return "Data verify failed after operation"; |
| 1706 | 853 case CYG_FLASH_ERR_DRV_TIMEOUT: |
|
154
e2d866e32dac
Merge from eCos master repository on 2001-02-26-23:18:05-GMT
jlarmour
parents:
151
diff
changeset
|
854 return "Driver timed out waiting for device"; |
| 1706 | 855 case CYG_FLASH_ERR_DRV_WRONG_PART: |
|
154
e2d866e32dac
Merge from eCos master repository on 2001-02-26-23:18:05-GMT
jlarmour
parents:
151
diff
changeset
|
856 return "Driver does not support device"; |
| 1706 | 857 case CYG_FLASH_ERR_LOW_VOLTAGE: |
|
156
b939e9cada46
Merge from eCos master repository on 2001-03-29-21:44:39-BST
jlarmour
parents:
154
diff
changeset
|
858 return "Device reports low voltage"; |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
859 default: |
|
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
860 return "Unknown error"; |
|
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
861 } |
|
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
862 } |
|
151
25e238959bae
Merge from eCos master repository on 2001-02-12-23:44:24-GMT
jlarmour
parents:
143
diff
changeset
|
863 |
|
25e238959bae
Merge from eCos master repository on 2001-02-12-23:44:24-GMT
jlarmour
parents:
143
diff
changeset
|
864 // EOF io/flash/..../flash.c |
