1
|
/***************************************************************
|
2
|
*
|
3
|
* OpenBeacon.org - flash routines for persistent environment
|
4
|
*
|
5
|
* Copyright 2007 Milosch Meriac <meriac@openbeacon.de>
|
6
|
*
|
7
|
***************************************************************
|
8
|
|
9
|
This program is free software; you can redistribute it and/or modify
|
10
|
it under the terms of the GNU General Public License as published by
|
11
|
the Free Software Foundation; version 2.
|
12
|
|
13
|
This program is distributed in the hope that it will be useful,
|
14
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16
|
GNU General Public License for more details.
|
17
|
|
18
|
You should have received a copy of the GNU General Public License along
|
19
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
20
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
21
|
|
22
|
*/
|
23
|
#include <FreeRTOS.h>
|
24
|
#include <AT91SAM7.h>
|
25
|
#include <lib_AT91SAM7.h>
|
26
|
#include <string.h>
|
27
|
#include <board.h>
|
28
|
#include <task.h>
|
29
|
#include "env.h"
|
30
|
|
31
|
#define EFCS_CMD_WRITE_PAGE 0x1
|
32
|
#define EFCS_CMD_SET_LOCK_BIT 0x2
|
33
|
#define EFCS_CMD_WRITE_PAGE_LOCK 0x3
|
34
|
#define EFCS_CMD_CLEAR_LOCK 0x4
|
35
|
#define EFCS_CMD_ERASE_ALL 0x8
|
36
|
#define EFCS_CMD_SET_NVM_BIT 0xb
|
37
|
#define EFCS_CMD_CLEAR_NVM_BIT 0xd
|
38
|
#define EFCS_CMD_SET_SECURITY_BIT 0xf
|
39
|
|
40
|
#define PAGES_PER_LOCKREGION (AT91C_IFLASH_LOCK_REGION_SIZE>>AT91C_IFLASH_PAGE_SHIFT)
|
41
|
#define IS_FIRST_PAGE_OF_LOCKREGION(x) ((x % PAGES_PER_LOCKREGION) == 0)
|
42
|
#define LOCKREGION_FROM_PAGE(x) (x / PAGES_PER_LOCKREGION)
|
43
|
#define ENV_FLASH ((unsigned int *)AT91C_IFLASH + AT91C_IFLASH_SIZE - ENVIRONMENT_SIZE)
|
44
|
|
45
|
TEnvironmentBlock env;
|
46
|
|
47
|
static inline unsigned short RAMFUNC page_from_ramaddr(const void *addr)
|
48
|
{
|
49
|
unsigned long ramaddr = (unsigned long) addr;
|
50
|
ramaddr -= (unsigned long) AT91C_IFLASH;
|
51
|
return ((ramaddr >> AT91C_IFLASH_PAGE_SHIFT));
|
52
|
}
|
53
|
|
54
|
static inline int RAMFUNC is_page_locked(unsigned short page)
|
55
|
{
|
56
|
unsigned short lockregion = LOCKREGION_FROM_PAGE(page);
|
57
|
|
58
|
return (AT91C_BASE_MC->MC_FSR & (lockregion << 16));
|
59
|
}
|
60
|
|
61
|
static inline void RAMFUNC flash_cmd_wait(void)
|
62
|
{
|
63
|
while(( *AT91C_MC_FSR & AT91C_MC_FRDY ) != AT91C_MC_FRDY );
|
64
|
}
|
65
|
|
66
|
static inline void RAMFUNC unlock_page(unsigned short page)
|
67
|
{
|
68
|
page &= 0x3ff;
|
69
|
AT91F_MC_EFC_PerformCmd(AT91C_BASE_MC, AT91C_MC_FCMD_UNLOCK |
|
70
|
AT91C_MC_CORRECT_KEY | (page << 8));
|
71
|
flash_cmd_wait();
|
72
|
}
|
73
|
|
74
|
static inline void RAMFUNC flash_page(const void *addr)
|
75
|
{
|
76
|
unsigned short page = page_from_ramaddr(addr) & 0x3ff;
|
77
|
|
78
|
if (is_page_locked(page))
|
79
|
unlock_page(page);
|
80
|
|
81
|
AT91F_MC_EFC_PerformCmd(AT91C_BASE_MC, AT91C_MC_FCMD_START_PROG |
|
82
|
AT91C_MC_CORRECT_KEY | (page << 8));
|
83
|
flash_cmd_wait();
|
84
|
}
|
85
|
|
86
|
unsigned short RAMFUNC env_crc16 (const unsigned char *buffer, int size)
|
87
|
{
|
88
|
unsigned short crc = 0xFFFF;
|
89
|
|
90
|
if(buffer && size)
|
91
|
while (size--)
|
92
|
{
|
93
|
crc = (crc >> 8) | (crc << 8);
|
94
|
crc ^= *buffer++;
|
95
|
crc ^= ((unsigned char) crc) >> 4;
|
96
|
crc ^= crc << 12;
|
97
|
crc ^= (crc & 0xFF) << 5;
|
98
|
}
|
99
|
|
100
|
return crc;
|
101
|
}
|
102
|
|
103
|
void RAMFUNC env_store(void)
|
104
|
{
|
105
|
unsigned int i,*src,*dst;
|
106
|
|
107
|
/* During flashing only RAMFUNC code may be executed.
|
108
|
* For now, this means that no other code whatsoever may
|
109
|
* be run until this function returns. */
|
110
|
vTaskSuspendAll();
|
111
|
portENTER_CRITICAL();
|
112
|
|
113
|
env.e.magic=TENVIRONMENT_MAGIC;
|
114
|
env.e.crc16=0;
|
115
|
env.e.size=sizeof(env);
|
116
|
env.e.crc16=env_crc16((unsigned char*)&env,sizeof(env));
|
117
|
|
118
|
src=env.data;
|
119
|
dst=ENV_FLASH;
|
120
|
for (i = 0; i < (sizeof(env.data)/sizeof(env.data[0])); i++)
|
121
|
*dst++ = *src++;
|
122
|
|
123
|
flash_page(ENV_FLASH);
|
124
|
|
125
|
portEXIT_CRITICAL();
|
126
|
xTaskResumeAll();
|
127
|
}
|
128
|
|
129
|
int env_load(void)
|
130
|
{
|
131
|
unsigned int crc;
|
132
|
|
133
|
memcpy(&env,ENV_FLASH,sizeof(env));
|
134
|
|
135
|
if(env.e.magic!=TENVIRONMENT_MAGIC || env.e.size!=sizeof(env) )
|
136
|
return 0;
|
137
|
|
138
|
crc=env.e.crc16;
|
139
|
env.e.crc16=0;
|
140
|
|
141
|
return env_crc16((unsigned char*)&env,sizeof(env))==crc;
|
142
|
}
|
143
|
|
144
|
void env_init(void)
|
145
|
{
|
146
|
unsigned int fmcn = AT91F_MC_EFC_ComputeFMCN(MCK);
|
147
|
|
148
|
AT91F_MC_EFC_CfgModeReg(AT91C_BASE_MC, fmcn << 16 | AT91C_MC_FWS_3FWS);
|
149
|
}
|