Mercurial > flash_v2
annotate packages/io/flash/current/src/flash.c @ 1744:f854ed1e81db
Fix reads on legacy devices
| author | bartv |
|---|---|
| date | Sun, 21 Nov 2004 12:09:40 +0000 |
| parents | 0d824ba900cc |
| children | 172edf8a96d8 |
| 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 |
| 1706 | 109 // Has the FLASH IO library been initialise? |
| 110 static bool init = false; | |
| 111 // We have a linked list of flash drivers. This is the head of the | |
| 112 // list | |
| 113 static struct cyg_flash_dev *flash_head = NULL; | |
| 114 | |
| 115 // This array contains entries for all flash devices that are | |
| 116 // installed in the system. | |
| 117 __externC struct cyg_flash_dev cyg_flashdevtab[]; | |
| 118 CYG_HAL_TABLE_BEGIN(cyg_flashdevtab, cyg_flashdev); | |
| 119 | |
| 120 // end of the flashdev table | |
| 121 __externC struct cyg_flash_dev cyg_flashdevtab_end; | |
| 122 CYG_HAL_TABLE_END(cyg_flashdevtab_end, cyg_flashdev); | |
| 1400 | 123 |
| 1706 | 124 // Sort the linked list into ascending order of flash address. Use a |
| 125 // primitive ripple sort, but since we don't expect to have many | |
| 126 // devices this should be OK. | |
| 127 static void flash_sort(void) | |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
128 { |
| 1706 | 129 bool moved; |
| 130 struct cyg_flash_dev *dev, **previous_next; | |
| 131 | |
| 132 // If there is zero or one device, short cut | |
| 133 if (flash_head == NULL || flash_head->next == NULL) | |
| 134 return; | |
| 135 | |
| 136 do { | |
| 137 moved=false; | |
| 138 for (dev=flash_head, previous_next=&flash_head; | |
| 139 dev->next; | |
| 140 previous_next = &dev->next, dev=dev->next ){ | |
| 141 if (dev->start > dev->next->start) { | |
| 142 *previous_next=dev->next; | |
| 143 dev->next = (*previous_next)->next; | |
| 144 (*previous_next)->next = dev; | |
| 145 moved=true; | |
| 146 break; | |
| 147 } | |
| 148 } | |
| 149 } while (moved); | |
| 150 } | |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
151 |
| 1706 | 152 // Walk the linked list and see if there are any overlaps in the |
| 153 // addresses the devices claim to use using. | |
| 154 static bool flash_check_overlap(void) | |
| 155 { | |
| 156 struct cyg_flash_dev *dev; | |
| 157 | |
| 158 // If there is zero or one device, short cut | |
| 159 if (flash_head == NULL || flash_head->next == NULL) | |
| 160 return false; | |
| 161 | |
| 162 for (dev=flash_head; dev->next; dev=dev->next){ | |
| 163 if (dev->end >= dev->next->start) | |
| 164 return true; | |
| 165 } | |
| 166 return false; | |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
167 } |
|
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
168 |
| 1706 | 169 // Initialise all registered device. Any device that fails to |
| 170 // initialise we leave dev->init as false. Then sort the devices into | |
| 171 // ascending order of address and put them into a linked list. Lastly | |
| 172 // check if we have any overlap of the addresses. | |
| 173 __externC int | |
| 174 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
|
175 { |
| 1706 | 176 int err; |
| 177 struct cyg_flash_dev * dev; | |
| 178 | |
| 179 if (init) return CYG_FLASH_ERR_OK; | |
| 180 init = true; | |
| 181 | |
| 182 for (dev = &cyg_flashdevtab[0]; dev != &cyg_flashdevtab_end; dev++) { | |
| 183 dev->pf = pf; | |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
184 LOCK_INIT(dev); |
| 1706 | 185 |
| 186 err = dev->funs->flash_init(dev); | |
| 187 if (err != CYG_FLASH_ERR_OK) { | |
| 188 continue; | |
| 189 } | |
| 190 CYG_ASSERT(dev->funs, "No flash functions"); | |
| 191 CYG_ASSERT(dev->num_block_infos, "No number of block infoss"); | |
| 192 CYG_ASSERT(dev->block_info, "No block infos"); | |
| 193 CYG_ASSERT(!(((cyg_flashaddr_t)dev->block_info >= dev->start) && | |
| 194 ((cyg_flashaddr_t)dev->block_info < dev->end)), | |
| 195 "Block info is in the flash"); | |
| 196 CYG_ASSERT(dev->funs->flash_erase_block, "No erase function"); | |
| 197 CYG_ASSERT(dev->funs->flash_program, "No program function"); | |
| 198 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
|
199 #ifdef CYGDBG_USE_ASSERTS |
|
e290a0372578
* src/flash.c (cyg_flash_init): Add assert checking that the
asl
parents:
1723
diff
changeset
|
200 { |
|
e290a0372578
* src/flash.c (cyg_flash_init): Add assert checking that the
asl
parents:
1723
diff
changeset
|
201 int i; |
|
e290a0372578
* src/flash.c (cyg_flash_init): Add assert checking that the
asl
parents:
1723
diff
changeset
|
202 cyg_flashaddr_t addr = dev->start; |
|
e290a0372578
* src/flash.c (cyg_flash_init): Add assert checking that the
asl
parents:
1723
diff
changeset
|
203 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
|
204 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
|
205 } |
|
e290a0372578
* src/flash.c (cyg_flash_init): Add assert checking that the
asl
parents:
1723
diff
changeset
|
206 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
|
207 } |
|
e290a0372578
* src/flash.c (cyg_flash_init): Add assert checking that the
asl
parents:
1723
diff
changeset
|
208 #endif |
| 1706 | 209 dev->init = true; |
| 210 dev->next = flash_head; | |
| 211 flash_head = dev; | |
| 212 } | |
| 213 | |
| 214 flash_sort(); | |
| 215 | |
| 216 if (flash_check_overlap()) { | |
| 217 return CYG_FLASH_ERR_INVALID; | |
| 218 } | |
| 219 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
|
220 } |
|
e2d866e32dac
Merge from eCos master repository on 2001-02-26-23:18:05-GMT
jlarmour
parents:
151
diff
changeset
|
221 |
| 1706 | 222 // Is the address within one of the flash drivers? |
| 223 __externC int | |
| 224 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
|
225 { |
| 1706 | 226 struct cyg_flash_dev * dev; |
| 227 | |
| 228 if (!init) return CYG_FLASH_ERR_NOT_INIT; | |
| 229 | |
| 230 for (dev = flash_head; dev; dev=dev->next) { | |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
231 if ((dev->start <= address) && (address <= dev->end)) |
| 1706 | 232 return CYG_FLASH_ERR_OK; |
| 233 } | |
| 234 return CYG_FLASH_ERR_INVALID; | |
|
143
6b5f480c6929
Merge from eCos master repository on 2000-12-08-02:09:36-GMT
jlarmour
parents:
141
diff
changeset
|
235 } |
|
6b5f480c6929
Merge from eCos master repository on 2000-12-08-02:09:36-GMT
jlarmour
parents:
141
diff
changeset
|
236 |
| 1706 | 237 // See if a range of FLASH addresses overlaps currently running code |
| 238 __externC bool cyg_flash_code_overlaps(const cyg_flashaddr_t start, | |
| 239 const cyg_flashaddr_t end) | |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
240 { |
| 1706 | 241 extern char _stext[], _etext[]; |
| 242 | |
| 243 return ((((unsigned long)&_stext >= (unsigned long)start) && | |
| 244 ((unsigned long)&_stext < (unsigned long)end)) || | |
| 245 (((unsigned long)&_etext >= (unsigned long)start) && | |
| 246 ((unsigned long)&_etext < (unsigned long)end))); | |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
247 } |
|
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
248 |
| 1706 | 249 // Return information about the Nth driver |
| 250 __externC int | |
| 251 cyg_flash_get_info(cyg_uint32 Nth, cyg_flash_info_t * info) | |
| 252 { | |
| 253 struct cyg_flash_dev * dev; | |
| 254 | |
| 255 if (!init) return CYG_FLASH_ERR_NOT_INIT; | |
| 256 | |
| 257 for (dev = flash_head; dev && Nth; dev=dev->next, Nth--) | |
| 258 ; | |
| 259 if (dev && !Nth) { | |
| 260 info->start = dev->start; | |
| 261 info->end = dev->end; | |
| 262 info->num_block_infos = dev->num_block_infos; | |
| 263 info->block_info = dev->block_info; | |
| 264 return CYG_FLASH_ERR_OK; | |
| 265 } | |
| 266 return CYG_FLASH_ERR_INVALID; | |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
267 } |
|
157
b65ec671dd4c
Merge from eCos master repository on 2001-04-06-18:28:36-BST
jlarmour
parents:
156
diff
changeset
|
268 |
| 1706 | 269 // Return information about the flash at the given address |
| 270 __externC int | |
| 271 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
|
272 { |
| 1706 | 273 struct cyg_flash_dev *dev; |
| 274 | |
| 275 if (!init) return CYG_FLASH_ERR_NOT_INIT; | |
| 276 | |
| 277 for (dev = flash_head; dev ; dev=dev->next) { | |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
278 if ((dev->start <= flash_base) && ( flash_base <= dev->end)) { |
| 1706 | 279 info->start = dev->start; |
| 280 info->end = dev->end; | |
| 281 info->num_block_infos = dev->num_block_infos; | |
| 282 info->block_info = dev->block_info; | |
| 283 return CYG_FLASH_ERR_OK; | |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
284 } |
| 1706 | 285 } |
| 286 return CYG_FLASH_ERR_INVALID; | |
| 287 } | |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
288 |
| 1706 | 289 #ifdef CYGPKG_KERNEL |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
290 // Lock the mutex's for a range of addresses |
| 1706 | 291 __externC int |
| 292 cyg_flash_mutex_lock(const cyg_flashaddr_t from, const size_t len) | |
| 293 { | |
| 294 struct cyg_flash_dev * dev; | |
| 295 int err; | |
| 296 | |
| 297 if (!init) return CYG_FLASH_ERR_NOT_INIT; | |
| 298 | |
| 299 for (dev = flash_head; dev ; dev=dev->next) { | |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
300 if ((dev->start <= from) && ( from <= dev->end)) { |
| 1706 | 301 cyg_mutex_lock(&dev->mutex); |
| 302 if (dev->end > from+len) { | |
| 303 return CYG_FLASH_ERR_OK; | |
| 304 } else { // Check off by one? | |
| 305 //The region is bigger than this driver. Recurse | |
| 306 err = cyg_flash_mutex_lock(dev->end+1, (len - (dev->end - from))); | |
| 307 if (err != CYG_FLASH_ERR_OK) { | |
| 308 // Something went wrong, unlock what we just locked | |
| 309 cyg_mutex_unlock(&dev->mutex); | |
| 310 } | |
| 311 return err; | |
| 312 } | |
|
208
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
195
diff
changeset
|
313 } |
| 1706 | 314 } |
| 315 return CYG_FLASH_ERR_INVALID; | |
| 316 } | |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
317 |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
318 // Unlock the mutex's for a range of addresses |
| 1706 | 319 __externC int |
| 320 cyg_flash_mutex_unlock(const cyg_flashaddr_t from, const size_t len) | |
| 321 { | |
| 322 struct cyg_flash_dev * dev; | |
| 323 int err; | |
| 324 | |
| 325 if (!init) return CYG_FLASH_ERR_NOT_INIT; | |
| 326 | |
| 327 for (dev = flash_head; dev ; dev=dev->next) { | |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
328 if ((dev->start <= from) && ( from <= dev->end)) { |
| 1706 | 329 cyg_mutex_unlock(&dev->mutex); |
| 330 if (dev->end > from+len) { | |
| 331 return CYG_FLASH_ERR_OK; | |
| 332 } else { // Check off by one? | |
| 333 //The region is bigger than this driver. Recurse | |
| 334 err = cyg_flash_mutex_unlock(dev->end+1, (len - (dev->end - from))); | |
| 335 return err; | |
| 336 } | |
| 337 } | |
| 338 } | |
| 339 return CYG_FLASH_ERR_INVALID; | |
| 340 } | |
|
1223
dbcfe4e0dbd2
New platform - TAMS MOAB - and generic changes to support it
gthomas
parents:
210
diff
changeset
|
341 #endif |
|
dbcfe4e0dbd2
New platform - TAMS MOAB - and generic changes to support it
gthomas
parents:
210
diff
changeset
|
342 |
| 1706 | 343 // Return the size of the block which is at the given address |
| 344 static size_t | |
| 345 flash_block_size(struct cyg_flash_dev *dev, const cyg_flashaddr_t addr) | |
| 346 { | |
| 347 int i; | |
| 348 size_t offset; | |
| 349 | |
| 350 | |
| 351 CYG_ASSERT((addr >= dev->start) && (addr <= dev->end), "Not inside device"); | |
| 352 | |
| 353 offset = addr - dev->start; | |
| 354 for (i=0; i < dev->num_block_infos; i++) { | |
| 355 if (offset < (dev->block_info[i].blocks * dev->block_info[i].block_size)) | |
| 356 return dev->block_info[i].block_size; | |
| 357 offset = offset - | |
| 358 (dev->block_info[i].blocks * dev->block_info[i].block_size); | |
| 359 } | |
| 360 CYG_FAIL("Programming error"); | |
| 361 return 0; | |
| 362 } | |
| 363 | |
| 364 // Return the size of the block which is at the given address | |
| 365 __externC size_t | |
| 366 cyg_flash_block_size(const cyg_flashaddr_t flash_base) | |
| 367 { | |
| 368 struct cyg_flash_dev * dev; | |
| 369 | |
| 370 if (!init) return CYG_FLASH_ERR_NOT_INIT; | |
| 371 | |
| 372 for (dev = flash_head; | |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
373 dev && !((dev->start <= flash_base) && ( flash_base <= dev->end)); |
| 1706 | 374 dev=dev->next) |
| 375 ; | |
| 376 if (!dev) return CYG_FLASH_ERR_INVALID; | |
| 377 | |
| 378 return flash_block_size(dev, flash_base); | |
| 379 } | |
| 380 | |
|
1723
0cc13d07a67e
* src/flash.c: Support flash blocks of arbitary size. The
asl
parents:
1722
diff
changeset
|
381 // 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
|
382 // 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
|
383 // offsets. |
|
0cc13d07a67e
* src/flash.c: Support flash blocks of arbitary size. The
asl
parents:
1722
diff
changeset
|
384 static inline cyg_flashaddr_t |
|
0cc13d07a67e
* src/flash.c: Support flash blocks of arbitary size. The
asl
parents:
1722
diff
changeset
|
385 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
|
386 { |
|
0cc13d07a67e
* src/flash.c: Support flash blocks of arbitary size. The
asl
parents:
1722
diff
changeset
|
387 size_t block_size; |
|
0cc13d07a67e
* src/flash.c: Support flash blocks of arbitary size. The
asl
parents:
1722
diff
changeset
|
388 cyg_flashaddr_t offset; |
|
0cc13d07a67e
* src/flash.c: Support flash blocks of arbitary size. The
asl
parents:
1722
diff
changeset
|
389 |
|
0cc13d07a67e
* src/flash.c: Support flash blocks of arbitary size. The
asl
parents:
1722
diff
changeset
|
390 block_size = flash_block_size(dev, addr); |
|
0cc13d07a67e
* src/flash.c: Support flash blocks of arbitary size. The
asl
parents:
1722
diff
changeset
|
391 |
|
0cc13d07a67e
* src/flash.c: Support flash blocks of arbitary size. The
asl
parents:
1722
diff
changeset
|
392 offset = addr - dev->start; |
|
0cc13d07a67e
* src/flash.c: Support flash blocks of arbitary size. The
asl
parents:
1722
diff
changeset
|
393 offset = (offset / block_size) * block_size; |
|
0cc13d07a67e
* src/flash.c: Support flash blocks of arbitary size. The
asl
parents:
1722
diff
changeset
|
394 return offset + dev->start; |
|
0cc13d07a67e
* src/flash.c: Support flash blocks of arbitary size. The
asl
parents:
1722
diff
changeset
|
395 } |
|
0cc13d07a67e
* src/flash.c: Support flash blocks of arbitary size. The
asl
parents:
1722
diff
changeset
|
396 |
|
208
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
195
diff
changeset
|
397 |
| 1706 | 398 __externC int |
| 399 cyg_flash_erase(const cyg_flashaddr_t flash_base, | |
| 400 const size_t len, | |
| 401 cyg_flashaddr_t *err_address) | |
| 402 { | |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
403 cyg_flashaddr_t block, end_addr; |
| 1706 | 404 struct cyg_flash_dev * dev; |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
405 size_t erase_count; |
| 1706 | 406 int stat = CYG_FLASH_ERR_OK; |
| 407 int d_cache, i_cache; | |
| 408 | |
| 409 if (!init) return CYG_FLASH_ERR_NOT_INIT; | |
| 410 | |
| 411 for (dev = flash_head; | |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
412 dev && !((dev->start <= flash_base) && ( flash_base <= dev->end)); |
| 1706 | 413 dev=dev->next) |
| 414 ; | |
| 415 if (!dev) return CYG_FLASH_ERR_INVALID; | |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
416 |
|
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
417 CHECK_SOFT_WRITE_PROTECT(flash_base, len); |
| 1706 | 418 |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
419 LOCK(dev); |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
420 |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
421 // 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
|
422 // 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
|
423 // recursive call later on. |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
424 if (len > (dev->end + 1 - flash_base)) { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
425 end_addr = dev->end; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
426 } else { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
427 end_addr = flash_base + len - 1; |
| 1706 | 428 } |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
429 // 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
|
430 block = flash_block_begin(flash_base, dev); |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
431 erase_count = (end_addr + 1) - block; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
432 |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
433 CHATTER(dev, "... Erase from %p-%p: ", (void*)block, (void*)end_addr); |
| 1706 | 434 |
| 435 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
|
436 FLASH_Enable(flash_base, end_addr); |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
437 while (erase_count > 0) { |
| 1706 | 438 int i; |
| 439 unsigned char *dp; | |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
440 bool erased = false; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
441 size_t block_size = flash_block_size(dev, block); |
| 1706 | 442 |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
443 // Pad to the block boundary, if necessary |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
444 if (erase_count < block_size) { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
445 erase_count = block_size; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
446 } |
|
1723
0cc13d07a67e
* src/flash.c: Support flash blocks of arbitary size. The
asl
parents:
1722
diff
changeset
|
447 |
| 1706 | 448 // If there is a read function it probably means the flash |
| 449 // cannot be read directly. | |
| 450 if (!dev->funs->flash_read) { | |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
451 erased = true; |
| 1706 | 452 dp = (unsigned char *)block; |
| 453 for (i = 0; i < block_size; i++) { | |
| 454 if (*dp++ != (unsigned char)0xFF) { | |
| 455 erased = false; | |
| 456 break; | |
| 457 } | |
| 458 } | |
| 459 } | |
| 460 if (!erased) { | |
| 461 stat = dev->funs->flash_erase_block(dev,block); | |
| 462 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
|
463 } |
| 1706 | 464 if (CYG_FLASH_ERR_OK != stat && err_address) { |
| 465 *err_address = block; | |
| 466 break; | |
| 467 } | |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
468 block += block_size; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
469 erase_count -= block_size; |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
470 CHATTER(dev, "."); |
| 1706 | 471 } |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
472 FLASH_Disable(flash_base, end_addr); |
| 1706 | 473 HAL_FLASH_CACHES_ON(d_cache, i_cache); |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
474 CHATTER(dev, "\n"); |
|
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
475 UNLOCK(dev); |
| 1706 | 476 if (stat != CYG_FLASH_ERR_OK) { |
| 477 return stat; | |
| 478 } | |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
479 |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
480 // 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
|
481 // 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
|
482 // 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
|
483 // devices will be small. |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
484 if (len > (dev->end + 1 - flash_base)) { |
| 1706 | 485 return cyg_flash_erase(dev->end+1, |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
486 len - (dev->end + 1 - flash_base), |
| 1706 | 487 err_address); |
| 488 } | |
| 489 return CYG_FLASH_ERR_OK; | |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
490 } |
|
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
491 |
| 1706 | 492 __externC int |
| 493 cyg_flash_program(const cyg_flashaddr_t flash_base, | |
| 494 void *ram_base, | |
| 495 const size_t len, | |
| 496 cyg_flashaddr_t *err_address) | |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
497 { |
| 1706 | 498 struct cyg_flash_dev * dev; |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
499 cyg_flashaddr_t addr, end_addr, block; |
| 1706 | 500 unsigned char * ram = ram_base; |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
501 size_t write_count, offset; |
| 1706 | 502 int stat = CYG_FLASH_ERR_OK; |
| 503 int d_cache, i_cache; | |
| 504 | |
| 505 if (!init) return CYG_FLASH_ERR_NOT_INIT; | |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
506 |
| 1706 | 507 for (dev = flash_head; |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
508 dev && !((dev->start <= flash_base) && ( flash_base <= dev->end)); |
| 1706 | 509 dev=dev->next) |
| 510 ; | |
| 511 if (!dev) return CYG_FLASH_ERR_INVALID; | |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
512 |
|
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
513 CHECK_SOFT_WRITE_PROTECT(flash_base, len); |
| 1706 | 514 |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
515 LOCK(dev); |
| 1706 | 516 addr = flash_base; |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
517 if (len > (dev->end + 1 - flash_base)) { |
| 1706 | 518 end_addr = dev->end; |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
519 } else { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
520 end_addr = flash_base + len - 1; |
| 1706 | 521 } |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
522 write_count = (end_addr + 1) - flash_base; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
523 |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
524 // 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
|
525 // adjustment here rather than inside the loop. |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
526 block = flash_block_begin(flash_base, dev); |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
527 if (addr == block) { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
528 offset = 0; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
529 } else { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
530 offset = addr - block; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
531 } |
| 1706 | 532 |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
533 CHATTER(dev, "... Program from %p-%p to %p: ", ram_base, ((CYG_ADDRESS)ram_base)+write_count, addr); |
| 1706 | 534 |
| 535 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
|
536 FLASH_Enable(flash_base, end_addr); |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
537 while (write_count > 0) { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
538 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
|
539 size_t this_write; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
540 if (write_count > (block_size - offset)) { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
541 this_write = block_size - offset; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
542 } else { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
543 this_write = write_count; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
544 } |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
545 // Only the first block may need the offset. |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
546 offset = 0; |
| 1706 | 547 |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
548 stat = dev->funs->flash_program(dev, addr, ram, this_write); |
| 1706 | 549 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
|
550 #ifdef CYGSEM_IO_FLASH_VERIFY_PROGRAM |
| 1706 | 551 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
|
552 if (!dev->funs->flash_read && memcmp((void *)addr, ram, this_write) != 0) { |
| 1706 | 553 stat = CYG_FLASH_ERR_DRV_VERIFY; |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
554 CHATTER(dev, "V"); |
| 1706 | 555 } |
|
157
b65ec671dd4c
Merge from eCos master repository on 2001-04-06-18:28:36-BST
jlarmour
parents:
156
diff
changeset
|
556 #endif |
| 1706 | 557 if (CYG_FLASH_ERR_OK != stat && err_address) { |
| 558 *err_address = addr; | |
| 559 break; | |
| 560 } | |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
561 CHATTER(dev, "."); |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
562 write_count -= this_write; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
563 addr += this_write; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
564 ram += this_write; |
| 1706 | 565 } |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
566 FLASH_Disable(flash_base, end_addr); |
| 1706 | 567 HAL_FLASH_CACHES_ON(d_cache, i_cache); |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
568 CHATTER(dev, "\n"); |
|
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
569 UNLOCK(dev); |
| 1706 | 570 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
|
571 return (stat); |
| 1706 | 572 } |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
573 if (len > (dev->end + 1 - flash_base)) { |
| 1706 | 574 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
|
575 len - (dev->end + 1 - flash_base), |
| 1706 | 576 err_address); |
| 577 } | |
| 578 return CYG_FLASH_ERR_OK; | |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
579 } |
|
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
580 |
| 1706 | 581 __externC int |
| 582 cyg_flash_read(cyg_flashaddr_t flash_base, | |
| 583 const void *ram_base, | |
| 584 const size_t len, | |
| 585 cyg_flashaddr_t *err_address) | |
|
1223
dbcfe4e0dbd2
New platform - TAMS MOAB - and generic changes to support it
gthomas
parents:
210
diff
changeset
|
586 { |
| 1706 | 587 struct cyg_flash_dev * dev; |
|
1742
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
588 cyg_flashaddr_t addr, end_addr; |
| 1706 | 589 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
|
590 size_t read_count; |
| 1706 | 591 int stat = CYG_FLASH_ERR_OK; |
| 592 | |
| 593 if (!init) return CYG_FLASH_ERR_NOT_INIT; | |
| 594 | |
| 595 for (dev = flash_head; | |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
596 dev && !((dev->start <= flash_base) && ( flash_base <= dev->end)); |
| 1706 | 597 dev=dev->next) |
| 598 ; | |
| 599 if (!dev) return CYG_FLASH_ERR_INVALID; | |
|
1223
dbcfe4e0dbd2
New platform - TAMS MOAB - and generic changes to support it
gthomas
parents:
210
diff
changeset
|
600 |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
601 LOCK(dev); |
| 1706 | 602 addr = flash_base; |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
603 if (len > (dev->end + 1 - flash_base)) { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
604 end_addr = dev->end; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
605 } else { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
606 end_addr = flash_base + len - 1; |
| 1706 | 607 } |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
608 read_count = (end_addr + 1) - flash_base; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
609 |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
610 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
|
611 |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
612 // 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
|
613 // 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
|
614 // programs/erases. |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
615 if (! dev->funs->flash_read) { |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
616 memcpy(ram, (void*)addr, read_count); |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
617 } else { |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
618 #ifndef CYGHWR_IO_FLASH_INDIRECT_READS |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
619 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
|
620 stat = CYG_FLASH_ERR_PROTOCOL; |
| 1744 | 621 if (err_address) { |
| 622 *err_address = addr; | |
| 623 } | |
|
1742
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
624 #else |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
625 // 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
|
626 // 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
|
627 // 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
|
628 int d_cache, i_cache; |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
629 size_t offset; |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
630 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
|
631 if (addr == block) { |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
632 offset = 0; |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
633 } else { |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
634 offset = addr - block; |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
635 } |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
636 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
|
637 FLASH_Enable(flash_base, end_addr); |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
638 while (read_count > 0) { |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
639 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
|
640 size_t this_read; |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
641 if (read_count > (block_size - offset)) { |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
642 this_read = block_size - offset; |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
643 } else { |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
644 this_read = read_count; |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
645 } |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
646 // 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
|
647 offset = 0; |
| 1706 | 648 |
|
1742
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
649 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
|
650 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
|
651 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
|
652 *err_address = addr; |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
653 break; |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
654 } |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
655 CHATTER(dev, "."); |
|
1742
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
656 read_count -= this_read; |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
657 addr += this_read; |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
658 ram += this_read; |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
659 } |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
660 FLASH_Disable(flash_base, end_addr); |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
661 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
|
662 #endif |
| 1706 | 663 } |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
664 CHATTER(dev, "\n"); |
|
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
665 UNLOCK(dev); |
| 1706 | 666 if (stat != CYG_FLASH_ERR_OK) { |
|
1223
dbcfe4e0dbd2
New platform - TAMS MOAB - and generic changes to support it
gthomas
parents:
210
diff
changeset
|
667 return (stat); |
| 1706 | 668 } |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
669 if (len > (dev->end + 1 - flash_base)) { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
670 return cyg_flash_read(dev->end+1, ram, |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
671 len - (dev->end + 1 - flash_base), |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
672 err_address); |
| 1706 | 673 } |
| 674 return CYG_FLASH_ERR_OK; | |
|
1223
dbcfe4e0dbd2
New platform - TAMS MOAB - and generic changes to support it
gthomas
parents:
210
diff
changeset
|
675 } |
|
dbcfe4e0dbd2
New platform - TAMS MOAB - and generic changes to support it
gthomas
parents:
210
diff
changeset
|
676 |
|
177
4c750ce71ae3
Merge from eCos master repository on 2001-08-10-19:22:57-BST
jlarmour
parents:
174
diff
changeset
|
677 #ifdef CYGHWR_IO_FLASH_BLOCK_LOCKING |
| 1706 | 678 __externC int |
| 679 cyg_flash_lock(const cyg_flashaddr_t flash_base, | |
| 680 const size_t len, | |
| 681 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
|
682 { |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
683 cyg_flashaddr_t block, end_addr; |
| 1706 | 684 struct cyg_flash_dev * dev; |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
685 size_t lock_count; |
| 1706 | 686 int stat = CYG_FLASH_ERR_OK; |
| 687 int d_cache, i_cache; | |
| 688 | |
| 689 if (!init) return CYG_FLASH_ERR_NOT_INIT; | |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
690 |
| 1706 | 691 for (dev = flash_head; |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
692 dev && !((dev->start <= flash_base) && ( flash_base <= dev->end)); |
| 1706 | 693 dev=dev->next) |
| 694 ; | |
| 695 if (!dev) return CYG_FLASH_ERR_INVALID; | |
| 696 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
|
697 |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
698 CHECK_SOFT_WRITE_PROTECT(flash_base, len); |
|
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
699 |
|
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
700 LOCK(dev); |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
701 if (len > (dev->end + 1 - flash_base)) { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
702 end_addr = dev->end; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
703 } else { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
704 end_addr = flash_base + len - 1; |
| 1706 | 705 } |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
706 block = flash_block_begin(flash_base, dev); |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
707 lock_count = (end_addr + 1) - block; |
| 1706 | 708 |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
709 CHATTER(dev, "... Locking from %p-%p: ", (void*)block, (void*)end_addr); |
| 1706 | 710 |
| 711 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
|
712 FLASH_Enable(flash_base, end_addr); |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
713 while (lock_count > 0) { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
714 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
|
715 if (lock_count < block_size) { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
716 lock_count = block_size; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
717 } |
| 1706 | 718 stat = dev->funs->flash_block_lock(dev,block); |
| 719 stat = dev->funs->flash_hwr_map_error(dev,stat); | |
| 720 | |
| 721 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
|
722 *err_address = block; |
| 1706 | 723 break; |
|
119
77cec8369160
Merge from eCos master repository on 2000-09-01-07:47:03-BST
jlarmour
parents:
117
diff
changeset
|
724 } |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
725 block += block_size; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
726 lock_count -= block_size; |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
727 CHATTER(dev, "."); |
| 1706 | 728 } |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
729 FLASH_Disable(flash_base, end_addr); |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
730 HAL_FLASH_CACHES_ON(d_cache, i_cache); |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
731 CHATTER(dev, "\n"); |
|
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
732 UNLOCK(dev); |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
733 if (stat != CYG_FLASH_ERR_OK) { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
734 return stat; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
735 } |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
736 |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
737 // Recurse if necessary for the next device |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
738 if (len > (dev->end + 1 - flash_base)) { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
739 return cyg_flash_lock(dev->end+1, |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
740 len - (dev->end + 1 - flash_base), |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
741 err_address); |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
742 } |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
743 |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
744 return CYG_FLASH_ERR_OK; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
745 } |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
746 |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
747 __externC int |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
748 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
|
749 const size_t len, |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
750 cyg_flashaddr_t *err_address) |
|
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 cyg_flashaddr_t block, end_addr; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
753 struct cyg_flash_dev * dev; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
754 size_t unlock_count; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
755 int stat = CYG_FLASH_ERR_OK; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
756 int d_cache, i_cache; |
|
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 if (!init) return CYG_FLASH_ERR_NOT_INIT; |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
759 |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
760 for (dev = flash_head; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
761 dev && !((dev->start <= flash_base) && ( flash_base <= dev->end)); |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
762 dev=dev->next) |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
763 ; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
764 if (!dev) return CYG_FLASH_ERR_INVALID; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
765 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
|
766 |
|
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
767 CHECK_SOFT_WRITE_PROTECT(flash_base, len); |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
768 |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
769 LOCK(dev); |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
770 if (len > (dev->end + 1 - flash_base)) { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
771 end_addr = dev->end; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
772 } else { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
773 end_addr = flash_base + len - 1; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
774 } |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
775 block = flash_block_begin(flash_base, dev); |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
776 unlock_count = (end_addr + 1) - block; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
777 |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
778 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
|
779 |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
780 HAL_FLASH_CACHES_OFF(d_cache, i_cache); |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
781 FLASH_Enable(flash_base, end_addr); |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
782 while (unlock_count > 0) { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
783 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
|
784 if (unlock_count < block_size) { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
785 unlock_count = block_size; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
786 } |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
787 stat = dev->funs->flash_block_unlock(dev,block); |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
788 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
|
789 |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
790 if (CYG_FLASH_ERR_OK != stat && err_address) { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
791 *err_address = block; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
792 break; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
793 } |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
794 block += 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 |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
797 CHATTER(dev, "."); |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
798 } |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
799 FLASH_Disable(flash_base, end_addr); |
| 1706 | 800 HAL_FLASH_CACHES_ON(d_cache, i_cache); |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
801 CHATTER(dev, "\n"); |
|
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
802 UNLOCK(dev); |
| 1706 | 803 if (stat != CYG_FLASH_ERR_OK) { |
| 804 return stat; | |
| 805 } | |
| 806 | |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
807 // Recurse if necessary for the next device |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
808 if (len > (dev->end + 1 - flash_base)) { |
| 1706 | 809 return cyg_flash_lock(dev->end+1, |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
810 len - (dev->end + 1 - flash_base), |
| 1706 | 811 err_address); |
| 812 } | |
| 813 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
|
814 } |
|
77cec8369160
Merge from eCos master repository on 2000-09-01-07:47:03-BST
jlarmour
parents:
117
diff
changeset
|
815 #endif |
|
77cec8369160
Merge from eCos master repository on 2000-09-01-07:47:03-BST
jlarmour
parents:
117
diff
changeset
|
816 |
| 1706 | 817 const char * |
| 818 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
|
819 { |
|
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
820 switch (err) { |
| 1706 | 821 case CYG_FLASH_ERR_OK: |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
822 return "No error - operation complete"; |
| 1706 | 823 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
|
824 return "Device is in erase suspend state"; |
| 1706 | 825 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
|
826 return "Device is in program suspend state"; |
| 1706 | 827 case CYG_FLASH_ERR_INVALID: |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
828 return "Invalid FLASH address"; |
| 1706 | 829 case CYG_FLASH_ERR_ERASE: |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
830 return "Error trying to erase"; |
| 1706 | 831 case CYG_FLASH_ERR_LOCK: |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
832 return "Error trying to lock/unlock"; |
| 1706 | 833 case CYG_FLASH_ERR_PROGRAM: |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
834 return "Error trying to program"; |
| 1706 | 835 case CYG_FLASH_ERR_PROTOCOL: |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
836 return "Generic error"; |
| 1706 | 837 case CYG_FLASH_ERR_PROTECT: |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
838 return "Device/region is write-protected"; |
| 1706 | 839 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
|
840 return "FLASH sub-system not initialized"; |
| 1706 | 841 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
|
842 return "Data verify failed after operation"; |
| 1706 | 843 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
|
844 return "Driver timed out waiting for device"; |
| 1706 | 845 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
|
846 return "Driver does not support device"; |
| 1706 | 847 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
|
848 return "Device reports low voltage"; |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
849 default: |
|
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
850 return "Unknown error"; |
|
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
851 } |
|
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
852 } |
|
151
25e238959bae
Merge from eCos master repository on 2001-02-12-23:44:24-GMT
jlarmour
parents:
143
diff
changeset
|
853 |
|
25e238959bae
Merge from eCos master repository on 2001-02-12-23:44:24-GMT
jlarmour
parents:
143
diff
changeset
|
854 // EOF io/flash/..../flash.c |
