Mercurial > flash_v2
annotate packages/io/flash/current/src/flash.c @ 1746:172edf8a96d8
Eliminate cyg_flash_code_overlaps()
| author | bartv |
|---|---|
| date | Sun, 21 Nov 2004 18:39:15 +0000 |
| parents | f854ed1e81db |
| children | a221170fac3d |
| 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 // Return information about the Nth driver |
| 238 __externC int | |
| 239 cyg_flash_get_info(cyg_uint32 Nth, cyg_flash_info_t * info) | |
| 240 { | |
| 241 struct cyg_flash_dev * dev; | |
| 242 | |
| 243 if (!init) return CYG_FLASH_ERR_NOT_INIT; | |
| 244 | |
| 245 for (dev = flash_head; dev && Nth; dev=dev->next, Nth--) | |
| 246 ; | |
| 247 if (dev && !Nth) { | |
| 248 info->start = dev->start; | |
| 249 info->end = dev->end; | |
| 250 info->num_block_infos = dev->num_block_infos; | |
| 251 info->block_info = dev->block_info; | |
| 252 return CYG_FLASH_ERR_OK; | |
| 253 } | |
| 254 return CYG_FLASH_ERR_INVALID; | |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
255 } |
|
157
b65ec671dd4c
Merge from eCos master repository on 2001-04-06-18:28:36-BST
jlarmour
parents:
156
diff
changeset
|
256 |
| 1706 | 257 // Return information about the flash at the given address |
| 258 __externC int | |
| 259 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
|
260 { |
| 1706 | 261 struct cyg_flash_dev *dev; |
| 262 | |
| 263 if (!init) return CYG_FLASH_ERR_NOT_INIT; | |
| 264 | |
| 265 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
|
266 if ((dev->start <= flash_base) && ( flash_base <= dev->end)) { |
| 1706 | 267 info->start = dev->start; |
| 268 info->end = dev->end; | |
| 269 info->num_block_infos = dev->num_block_infos; | |
| 270 info->block_info = dev->block_info; | |
| 271 return CYG_FLASH_ERR_OK; | |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
272 } |
| 1706 | 273 } |
| 274 return CYG_FLASH_ERR_INVALID; | |
| 275 } | |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
276 |
| 1706 | 277 #ifdef CYGPKG_KERNEL |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
278 // Lock the mutex's for a range of addresses |
| 1706 | 279 __externC int |
| 280 cyg_flash_mutex_lock(const cyg_flashaddr_t from, const size_t len) | |
| 281 { | |
| 282 struct cyg_flash_dev * dev; | |
| 283 int err; | |
| 284 | |
| 285 if (!init) return CYG_FLASH_ERR_NOT_INIT; | |
| 286 | |
| 287 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
|
288 if ((dev->start <= from) && ( from <= dev->end)) { |
| 1706 | 289 cyg_mutex_lock(&dev->mutex); |
| 290 if (dev->end > from+len) { | |
| 291 return CYG_FLASH_ERR_OK; | |
| 292 } else { // Check off by one? | |
| 293 //The region is bigger than this driver. Recurse | |
| 294 err = cyg_flash_mutex_lock(dev->end+1, (len - (dev->end - from))); | |
| 295 if (err != CYG_FLASH_ERR_OK) { | |
| 296 // Something went wrong, unlock what we just locked | |
| 297 cyg_mutex_unlock(&dev->mutex); | |
| 298 } | |
| 299 return err; | |
| 300 } | |
|
208
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
195
diff
changeset
|
301 } |
| 1706 | 302 } |
| 303 return CYG_FLASH_ERR_INVALID; | |
| 304 } | |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
305 |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
306 // Unlock the mutex's for a range of addresses |
| 1706 | 307 __externC int |
| 308 cyg_flash_mutex_unlock(const cyg_flashaddr_t from, const size_t len) | |
| 309 { | |
| 310 struct cyg_flash_dev * dev; | |
| 311 int err; | |
| 312 | |
| 313 if (!init) return CYG_FLASH_ERR_NOT_INIT; | |
| 314 | |
| 315 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
|
316 if ((dev->start <= from) && ( from <= dev->end)) { |
| 1706 | 317 cyg_mutex_unlock(&dev->mutex); |
| 318 if (dev->end > from+len) { | |
| 319 return CYG_FLASH_ERR_OK; | |
| 320 } else { // Check off by one? | |
| 321 //The region is bigger than this driver. Recurse | |
| 322 err = cyg_flash_mutex_unlock(dev->end+1, (len - (dev->end - from))); | |
| 323 return err; | |
| 324 } | |
| 325 } | |
| 326 } | |
| 327 return CYG_FLASH_ERR_INVALID; | |
| 328 } | |
|
1223
dbcfe4e0dbd2
New platform - TAMS MOAB - and generic changes to support it
gthomas
parents:
210
diff
changeset
|
329 #endif |
|
dbcfe4e0dbd2
New platform - TAMS MOAB - and generic changes to support it
gthomas
parents:
210
diff
changeset
|
330 |
| 1706 | 331 // Return the size of the block which is at the given address |
| 332 static size_t | |
| 333 flash_block_size(struct cyg_flash_dev *dev, const cyg_flashaddr_t addr) | |
| 334 { | |
| 335 int i; | |
| 336 size_t offset; | |
| 337 | |
| 338 | |
| 339 CYG_ASSERT((addr >= dev->start) && (addr <= dev->end), "Not inside device"); | |
| 340 | |
| 341 offset = addr - dev->start; | |
| 342 for (i=0; i < dev->num_block_infos; i++) { | |
| 343 if (offset < (dev->block_info[i].blocks * dev->block_info[i].block_size)) | |
| 344 return dev->block_info[i].block_size; | |
| 345 offset = offset - | |
| 346 (dev->block_info[i].blocks * dev->block_info[i].block_size); | |
| 347 } | |
| 348 CYG_FAIL("Programming error"); | |
| 349 return 0; | |
| 350 } | |
| 351 | |
| 352 // Return the size of the block which is at the given address | |
| 353 __externC size_t | |
| 354 cyg_flash_block_size(const cyg_flashaddr_t flash_base) | |
| 355 { | |
| 356 struct cyg_flash_dev * dev; | |
| 357 | |
| 358 if (!init) return CYG_FLASH_ERR_NOT_INIT; | |
| 359 | |
| 360 for (dev = flash_head; | |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
361 dev && !((dev->start <= flash_base) && ( flash_base <= dev->end)); |
| 1706 | 362 dev=dev->next) |
| 363 ; | |
| 364 if (!dev) return CYG_FLASH_ERR_INVALID; | |
| 365 | |
| 366 return flash_block_size(dev, flash_base); | |
| 367 } | |
| 368 | |
|
1723
0cc13d07a67e
* src/flash.c: Support flash blocks of arbitary size. The
asl
parents:
1722
diff
changeset
|
369 // 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
|
370 // 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
|
371 // offsets. |
|
0cc13d07a67e
* src/flash.c: Support flash blocks of arbitary size. The
asl
parents:
1722
diff
changeset
|
372 static inline cyg_flashaddr_t |
|
0cc13d07a67e
* src/flash.c: Support flash blocks of arbitary size. The
asl
parents:
1722
diff
changeset
|
373 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
|
374 { |
|
0cc13d07a67e
* src/flash.c: Support flash blocks of arbitary size. The
asl
parents:
1722
diff
changeset
|
375 size_t block_size; |
|
0cc13d07a67e
* src/flash.c: Support flash blocks of arbitary size. The
asl
parents:
1722
diff
changeset
|
376 cyg_flashaddr_t offset; |
|
0cc13d07a67e
* src/flash.c: Support flash blocks of arbitary size. The
asl
parents:
1722
diff
changeset
|
377 |
|
0cc13d07a67e
* src/flash.c: Support flash blocks of arbitary size. The
asl
parents:
1722
diff
changeset
|
378 block_size = flash_block_size(dev, addr); |
|
0cc13d07a67e
* src/flash.c: Support flash blocks of arbitary size. The
asl
parents:
1722
diff
changeset
|
379 |
|
0cc13d07a67e
* src/flash.c: Support flash blocks of arbitary size. The
asl
parents:
1722
diff
changeset
|
380 offset = addr - dev->start; |
|
0cc13d07a67e
* src/flash.c: Support flash blocks of arbitary size. The
asl
parents:
1722
diff
changeset
|
381 offset = (offset / block_size) * block_size; |
|
0cc13d07a67e
* src/flash.c: Support flash blocks of arbitary size. The
asl
parents:
1722
diff
changeset
|
382 return offset + dev->start; |
|
0cc13d07a67e
* src/flash.c: Support flash blocks of arbitary size. The
asl
parents:
1722
diff
changeset
|
383 } |
|
0cc13d07a67e
* src/flash.c: Support flash blocks of arbitary size. The
asl
parents:
1722
diff
changeset
|
384 |
|
208
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
195
diff
changeset
|
385 |
| 1706 | 386 __externC int |
| 387 cyg_flash_erase(const cyg_flashaddr_t flash_base, | |
| 388 const size_t len, | |
| 389 cyg_flashaddr_t *err_address) | |
| 390 { | |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
391 cyg_flashaddr_t block, end_addr; |
| 1706 | 392 struct cyg_flash_dev * dev; |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
393 size_t erase_count; |
| 1706 | 394 int stat = CYG_FLASH_ERR_OK; |
| 395 int d_cache, i_cache; | |
| 396 | |
| 397 if (!init) return CYG_FLASH_ERR_NOT_INIT; | |
| 398 | |
| 399 for (dev = flash_head; | |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
400 dev && !((dev->start <= flash_base) && ( flash_base <= dev->end)); |
| 1706 | 401 dev=dev->next) |
| 402 ; | |
| 403 if (!dev) return CYG_FLASH_ERR_INVALID; | |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
404 |
|
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
405 CHECK_SOFT_WRITE_PROTECT(flash_base, len); |
| 1706 | 406 |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
407 LOCK(dev); |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
408 |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
409 // 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
|
410 // 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
|
411 // recursive call later on. |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
412 if (len > (dev->end + 1 - flash_base)) { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
413 end_addr = dev->end; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
414 } else { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
415 end_addr = flash_base + len - 1; |
| 1706 | 416 } |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
417 // 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
|
418 block = flash_block_begin(flash_base, dev); |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
419 erase_count = (end_addr + 1) - block; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
420 |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
421 CHATTER(dev, "... Erase from %p-%p: ", (void*)block, (void*)end_addr); |
| 1706 | 422 |
| 423 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
|
424 FLASH_Enable(flash_base, end_addr); |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
425 while (erase_count > 0) { |
| 1706 | 426 int i; |
| 427 unsigned char *dp; | |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
428 bool erased = false; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
429 size_t block_size = flash_block_size(dev, block); |
| 1706 | 430 |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
431 // Pad to the block boundary, if necessary |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
432 if (erase_count < block_size) { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
433 erase_count = block_size; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
434 } |
|
1723
0cc13d07a67e
* src/flash.c: Support flash blocks of arbitary size. The
asl
parents:
1722
diff
changeset
|
435 |
| 1706 | 436 // If there is a read function it probably means the flash |
| 437 // cannot be read directly. | |
| 438 if (!dev->funs->flash_read) { | |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
439 erased = true; |
| 1706 | 440 dp = (unsigned char *)block; |
| 441 for (i = 0; i < block_size; i++) { | |
| 442 if (*dp++ != (unsigned char)0xFF) { | |
| 443 erased = false; | |
| 444 break; | |
| 445 } | |
| 446 } | |
| 447 } | |
| 448 if (!erased) { | |
| 449 stat = dev->funs->flash_erase_block(dev,block); | |
| 450 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
|
451 } |
| 1706 | 452 if (CYG_FLASH_ERR_OK != stat && err_address) { |
| 453 *err_address = block; | |
| 454 break; | |
| 455 } | |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
456 block += block_size; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
457 erase_count -= block_size; |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
458 CHATTER(dev, "."); |
| 1706 | 459 } |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
460 FLASH_Disable(flash_base, end_addr); |
| 1706 | 461 HAL_FLASH_CACHES_ON(d_cache, i_cache); |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
462 CHATTER(dev, "\n"); |
|
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
463 UNLOCK(dev); |
| 1706 | 464 if (stat != CYG_FLASH_ERR_OK) { |
| 465 return stat; | |
| 466 } | |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
467 |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
468 // 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
|
469 // 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
|
470 // 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
|
471 // devices will be small. |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
472 if (len > (dev->end + 1 - flash_base)) { |
| 1706 | 473 return cyg_flash_erase(dev->end+1, |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
474 len - (dev->end + 1 - flash_base), |
| 1706 | 475 err_address); |
| 476 } | |
| 477 return CYG_FLASH_ERR_OK; | |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
478 } |
|
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
479 |
| 1706 | 480 __externC int |
| 481 cyg_flash_program(const cyg_flashaddr_t flash_base, | |
| 482 void *ram_base, | |
| 483 const size_t len, | |
| 484 cyg_flashaddr_t *err_address) | |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
485 { |
| 1706 | 486 struct cyg_flash_dev * dev; |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
487 cyg_flashaddr_t addr, end_addr, block; |
| 1706 | 488 unsigned char * ram = ram_base; |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
489 size_t write_count, offset; |
| 1706 | 490 int stat = CYG_FLASH_ERR_OK; |
| 491 int d_cache, i_cache; | |
| 492 | |
| 493 if (!init) return CYG_FLASH_ERR_NOT_INIT; | |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
494 |
| 1706 | 495 for (dev = flash_head; |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
496 dev && !((dev->start <= flash_base) && ( flash_base <= dev->end)); |
| 1706 | 497 dev=dev->next) |
| 498 ; | |
| 499 if (!dev) return CYG_FLASH_ERR_INVALID; | |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
500 |
|
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
501 CHECK_SOFT_WRITE_PROTECT(flash_base, len); |
| 1706 | 502 |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
503 LOCK(dev); |
| 1706 | 504 addr = flash_base; |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
505 if (len > (dev->end + 1 - flash_base)) { |
| 1706 | 506 end_addr = dev->end; |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
507 } else { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
508 end_addr = flash_base + len - 1; |
| 1706 | 509 } |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
510 write_count = (end_addr + 1) - flash_base; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
511 |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
512 // 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
|
513 // adjustment here rather than inside the loop. |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
514 block = flash_block_begin(flash_base, dev); |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
515 if (addr == block) { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
516 offset = 0; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
517 } else { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
518 offset = addr - block; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
519 } |
| 1706 | 520 |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
521 CHATTER(dev, "... Program from %p-%p to %p: ", ram_base, ((CYG_ADDRESS)ram_base)+write_count, addr); |
| 1706 | 522 |
| 523 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
|
524 FLASH_Enable(flash_base, end_addr); |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
525 while (write_count > 0) { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
526 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
|
527 size_t this_write; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
528 if (write_count > (block_size - offset)) { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
529 this_write = block_size - offset; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
530 } else { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
531 this_write = write_count; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
532 } |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
533 // Only the first block may need the offset. |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
534 offset = 0; |
| 1706 | 535 |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
536 stat = dev->funs->flash_program(dev, addr, ram, this_write); |
| 1706 | 537 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
|
538 #ifdef CYGSEM_IO_FLASH_VERIFY_PROGRAM |
| 1706 | 539 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
|
540 if (!dev->funs->flash_read && memcmp((void *)addr, ram, this_write) != 0) { |
| 1706 | 541 stat = CYG_FLASH_ERR_DRV_VERIFY; |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
542 CHATTER(dev, "V"); |
| 1706 | 543 } |
|
157
b65ec671dd4c
Merge from eCos master repository on 2001-04-06-18:28:36-BST
jlarmour
parents:
156
diff
changeset
|
544 #endif |
| 1706 | 545 if (CYG_FLASH_ERR_OK != stat && err_address) { |
| 546 *err_address = addr; | |
| 547 break; | |
| 548 } | |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
549 CHATTER(dev, "."); |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
550 write_count -= this_write; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
551 addr += this_write; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
552 ram += this_write; |
| 1706 | 553 } |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
554 FLASH_Disable(flash_base, end_addr); |
| 1706 | 555 HAL_FLASH_CACHES_ON(d_cache, i_cache); |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
556 CHATTER(dev, "\n"); |
|
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
557 UNLOCK(dev); |
| 1706 | 558 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
|
559 return (stat); |
| 1706 | 560 } |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
561 if (len > (dev->end + 1 - flash_base)) { |
| 1706 | 562 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
|
563 len - (dev->end + 1 - flash_base), |
| 1706 | 564 err_address); |
| 565 } | |
| 566 return CYG_FLASH_ERR_OK; | |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
567 } |
|
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
568 |
| 1706 | 569 __externC int |
| 570 cyg_flash_read(cyg_flashaddr_t flash_base, | |
| 571 const void *ram_base, | |
| 572 const size_t len, | |
| 573 cyg_flashaddr_t *err_address) | |
|
1223
dbcfe4e0dbd2
New platform - TAMS MOAB - and generic changes to support it
gthomas
parents:
210
diff
changeset
|
574 { |
| 1706 | 575 struct cyg_flash_dev * dev; |
|
1742
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
576 cyg_flashaddr_t addr, end_addr; |
| 1706 | 577 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
|
578 size_t read_count; |
| 1706 | 579 int stat = CYG_FLASH_ERR_OK; |
| 580 | |
| 581 if (!init) return CYG_FLASH_ERR_NOT_INIT; | |
| 582 | |
| 583 for (dev = flash_head; | |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
584 dev && !((dev->start <= flash_base) && ( flash_base <= dev->end)); |
| 1706 | 585 dev=dev->next) |
| 586 ; | |
| 587 if (!dev) return CYG_FLASH_ERR_INVALID; | |
|
1223
dbcfe4e0dbd2
New platform - TAMS MOAB - and generic changes to support it
gthomas
parents:
210
diff
changeset
|
588 |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
589 LOCK(dev); |
| 1706 | 590 addr = flash_base; |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
591 if (len > (dev->end + 1 - flash_base)) { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
592 end_addr = dev->end; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
593 } else { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
594 end_addr = flash_base + len - 1; |
| 1706 | 595 } |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
596 read_count = (end_addr + 1) - flash_base; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
597 |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
598 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
|
599 |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
600 // 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
|
601 // 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
|
602 // programs/erases. |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
603 if (! dev->funs->flash_read) { |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
604 memcpy(ram, (void*)addr, read_count); |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
605 } else { |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
606 #ifndef CYGHWR_IO_FLASH_INDIRECT_READS |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
607 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
|
608 stat = CYG_FLASH_ERR_PROTOCOL; |
| 1744 | 609 if (err_address) { |
| 610 *err_address = addr; | |
| 611 } | |
|
1742
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
612 #else |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
613 // 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
|
614 // 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
|
615 // 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
|
616 int d_cache, i_cache; |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
617 size_t offset; |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
618 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
|
619 if (addr == block) { |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
620 offset = 0; |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
621 } else { |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
622 offset = addr - block; |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
623 } |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
624 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
|
625 FLASH_Enable(flash_base, end_addr); |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
626 while (read_count > 0) { |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
627 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
|
628 size_t this_read; |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
629 if (read_count > (block_size - offset)) { |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
630 this_read = block_size - offset; |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
631 } else { |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
632 this_read = read_count; |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
633 } |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
634 // 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
|
635 offset = 0; |
| 1706 | 636 |
|
1742
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
637 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
|
638 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
|
639 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
|
640 *err_address = addr; |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
641 break; |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
642 } |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
643 CHATTER(dev, "."); |
|
1742
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
644 read_count -= this_read; |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
645 addr += this_read; |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
646 ram += this_read; |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
647 } |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
648 FLASH_Disable(flash_base, end_addr); |
|
a668b95e5f2e
Only include the support for indirect flash reads if there are devices
bartv
parents:
1741
diff
changeset
|
649 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
|
650 #endif |
| 1706 | 651 } |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
652 CHATTER(dev, "\n"); |
|
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
653 UNLOCK(dev); |
| 1706 | 654 if (stat != CYG_FLASH_ERR_OK) { |
|
1223
dbcfe4e0dbd2
New platform - TAMS MOAB - and generic changes to support it
gthomas
parents:
210
diff
changeset
|
655 return (stat); |
| 1706 | 656 } |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
657 if (len > (dev->end + 1 - flash_base)) { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
658 return cyg_flash_read(dev->end+1, ram, |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
659 len - (dev->end + 1 - flash_base), |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
660 err_address); |
| 1706 | 661 } |
| 662 return CYG_FLASH_ERR_OK; | |
|
1223
dbcfe4e0dbd2
New platform - TAMS MOAB - and generic changes to support it
gthomas
parents:
210
diff
changeset
|
663 } |
|
dbcfe4e0dbd2
New platform - TAMS MOAB - and generic changes to support it
gthomas
parents:
210
diff
changeset
|
664 |
|
177
4c750ce71ae3
Merge from eCos master repository on 2001-08-10-19:22:57-BST
jlarmour
parents:
174
diff
changeset
|
665 #ifdef CYGHWR_IO_FLASH_BLOCK_LOCKING |
| 1706 | 666 __externC int |
| 667 cyg_flash_lock(const cyg_flashaddr_t flash_base, | |
| 668 const size_t len, | |
| 669 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
|
670 { |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
671 cyg_flashaddr_t block, end_addr; |
| 1706 | 672 struct cyg_flash_dev * dev; |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
673 size_t lock_count; |
| 1706 | 674 int stat = CYG_FLASH_ERR_OK; |
| 675 int d_cache, i_cache; | |
| 676 | |
| 677 if (!init) return CYG_FLASH_ERR_NOT_INIT; | |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
678 |
| 1706 | 679 for (dev = flash_head; |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
680 dev && !((dev->start <= flash_base) && ( flash_base <= dev->end)); |
| 1706 | 681 dev=dev->next) |
| 682 ; | |
| 683 if (!dev) return CYG_FLASH_ERR_INVALID; | |
| 684 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
|
685 |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
686 CHECK_SOFT_WRITE_PROTECT(flash_base, len); |
|
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
687 |
|
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
688 LOCK(dev); |
|
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 end_addr = dev->end; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
691 } else { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
692 end_addr = flash_base + len - 1; |
| 1706 | 693 } |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
694 block = flash_block_begin(flash_base, dev); |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
695 lock_count = (end_addr + 1) - block; |
| 1706 | 696 |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
697 CHATTER(dev, "... Locking from %p-%p: ", (void*)block, (void*)end_addr); |
| 1706 | 698 |
| 699 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
|
700 FLASH_Enable(flash_base, end_addr); |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
701 while (lock_count > 0) { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
702 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
|
703 if (lock_count < block_size) { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
704 lock_count = block_size; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
705 } |
| 1706 | 706 stat = dev->funs->flash_block_lock(dev,block); |
| 707 stat = dev->funs->flash_hwr_map_error(dev,stat); | |
| 708 | |
| 709 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
|
710 *err_address = block; |
| 1706 | 711 break; |
|
119
77cec8369160
Merge from eCos master repository on 2000-09-01-07:47:03-BST
jlarmour
parents:
117
diff
changeset
|
712 } |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
713 block += block_size; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
714 lock_count -= block_size; |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
715 CHATTER(dev, "."); |
| 1706 | 716 } |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
717 FLASH_Disable(flash_base, end_addr); |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
718 HAL_FLASH_CACHES_ON(d_cache, i_cache); |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
719 CHATTER(dev, "\n"); |
|
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
720 UNLOCK(dev); |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
721 if (stat != CYG_FLASH_ERR_OK) { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
722 return stat; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
723 } |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
724 |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
725 // Recurse if necessary for the next device |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
726 if (len > (dev->end + 1 - flash_base)) { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
727 return cyg_flash_lock(dev->end+1, |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
728 len - (dev->end + 1 - flash_base), |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
729 err_address); |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
730 } |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
731 |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
732 return CYG_FLASH_ERR_OK; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
733 } |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
734 |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
735 __externC int |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
736 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
|
737 const size_t len, |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
738 cyg_flashaddr_t *err_address) |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
739 { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
740 cyg_flashaddr_t block, end_addr; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
741 struct cyg_flash_dev * dev; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
742 size_t unlock_count; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
743 int stat = CYG_FLASH_ERR_OK; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
744 int d_cache, i_cache; |
|
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 if (!init) return CYG_FLASH_ERR_NOT_INIT; |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
747 |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
748 for (dev = flash_head; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
749 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
|
750 dev=dev->next) |
|
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 if (!dev) return CYG_FLASH_ERR_INVALID; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
753 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
|
754 |
|
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
755 CHECK_SOFT_WRITE_PROTECT(flash_base, len); |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
756 |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
757 LOCK(dev); |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
758 if (len > (dev->end + 1 - flash_base)) { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
759 end_addr = dev->end; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
760 } else { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
761 end_addr = flash_base + len - 1; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
762 } |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
763 block = flash_block_begin(flash_base, dev); |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
764 unlock_count = (end_addr + 1) - block; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
765 |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
766 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
|
767 |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
768 HAL_FLASH_CACHES_OFF(d_cache, i_cache); |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
769 FLASH_Enable(flash_base, end_addr); |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
770 while (unlock_count > 0) { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
771 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
|
772 if (unlock_count < block_size) { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
773 unlock_count = block_size; |
|
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 stat = dev->funs->flash_block_unlock(dev,block); |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
776 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
|
777 |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
778 if (CYG_FLASH_ERR_OK != stat && err_address) { |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
779 *err_address = block; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
780 break; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
781 } |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
782 block += block_size; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
783 unlock_count -= block_size; |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
784 |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
785 CHATTER(dev, "."); |
|
1741
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 FLASH_Disable(flash_base, end_addr); |
| 1706 | 788 HAL_FLASH_CACHES_ON(d_cache, i_cache); |
|
1743
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
789 CHATTER(dev, "\n"); |
|
0d824ba900cc
Encapsulate various bits of optional functionality inside macros, to
bartv
parents:
1742
diff
changeset
|
790 UNLOCK(dev); |
| 1706 | 791 if (stat != CYG_FLASH_ERR_OK) { |
| 792 return stat; | |
| 793 } | |
| 794 | |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
795 // Recurse if necessary for the next device |
|
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
796 if (len > (dev->end + 1 - flash_base)) { |
| 1706 | 797 return cyg_flash_lock(dev->end+1, |
|
1741
9464c6747e99
Sort out the various loops to cope with boundary conditions,
bartv
parents:
1730
diff
changeset
|
798 len - (dev->end + 1 - flash_base), |
| 1706 | 799 err_address); |
| 800 } | |
| 801 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
|
802 } |
|
77cec8369160
Merge from eCos master repository on 2000-09-01-07:47:03-BST
jlarmour
parents:
117
diff
changeset
|
803 #endif |
|
77cec8369160
Merge from eCos master repository on 2000-09-01-07:47:03-BST
jlarmour
parents:
117
diff
changeset
|
804 |
| 1706 | 805 const char * |
| 806 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
|
807 { |
|
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
808 switch (err) { |
| 1706 | 809 case CYG_FLASH_ERR_OK: |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
810 return "No error - operation complete"; |
| 1706 | 811 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
|
812 return "Device is in erase suspend state"; |
| 1706 | 813 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
|
814 return "Device is in program suspend state"; |
| 1706 | 815 case CYG_FLASH_ERR_INVALID: |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
816 return "Invalid FLASH address"; |
| 1706 | 817 case CYG_FLASH_ERR_ERASE: |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
818 return "Error trying to erase"; |
| 1706 | 819 case CYG_FLASH_ERR_LOCK: |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
820 return "Error trying to lock/unlock"; |
| 1706 | 821 case CYG_FLASH_ERR_PROGRAM: |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
822 return "Error trying to program"; |
| 1706 | 823 case CYG_FLASH_ERR_PROTOCOL: |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
824 return "Generic error"; |
| 1706 | 825 case CYG_FLASH_ERR_PROTECT: |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
826 return "Device/region is write-protected"; |
| 1706 | 827 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
|
828 return "FLASH sub-system not initialized"; |
| 1706 | 829 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
|
830 return "Data verify failed after operation"; |
| 1706 | 831 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
|
832 return "Driver timed out waiting for device"; |
| 1706 | 833 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
|
834 return "Driver does not support device"; |
| 1706 | 835 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
|
836 return "Device reports low voltage"; |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
837 default: |
|
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
838 return "Unknown error"; |
|
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
839 } |
|
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
diff
changeset
|
840 } |
|
151
25e238959bae
Merge from eCos master repository on 2001-02-12-23:44:24-GMT
jlarmour
parents:
143
diff
changeset
|
841 |
|
25e238959bae
Merge from eCos master repository on 2001-02-12-23:44:24-GMT
jlarmour
parents:
143
diff
changeset
|
842 // EOF io/flash/..../flash.c |
