summaryrefslogtreecommitdiff
path: root/openpicc/application/env.c
blob: 92c8014c53860db4e46a4f9d5d8e337cd91b70a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/***************************************************************
 *
 * OpenBeacon.org - flash routines for persistent environment
 *
 * Copyright 2007 Milosch Meriac <meriac@openbeacon.de>
 *
 ***************************************************************

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; version 2.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

*/
#include <FreeRTOS.h>
#include <AT91SAM7.h>
#include <lib_AT91SAM7.h>
#include <string.h>
#include <board.h>
#include <task.h>
#include "env.h"

#define EFCS_CMD_WRITE_PAGE		0x1
#define EFCS_CMD_SET_LOCK_BIT		0x2
#define EFCS_CMD_WRITE_PAGE_LOCK	0x3
#define EFCS_CMD_CLEAR_LOCK		0x4
#define EFCS_CMD_ERASE_ALL		0x8
#define EFCS_CMD_SET_NVM_BIT		0xb
#define EFCS_CMD_CLEAR_NVM_BIT		0xd
#define EFCS_CMD_SET_SECURITY_BIT	0xf

#define PAGES_PER_LOCKREGION	(AT91C_IFLASH_LOCK_REGION_SIZE>>AT91C_IFLASH_PAGE_SHIFT)
#define IS_FIRST_PAGE_OF_LOCKREGION(x)	((x % PAGES_PER_LOCKREGION) == 0)
#define LOCKREGION_FROM_PAGE(x)	(x / PAGES_PER_LOCKREGION)
#define ENV_FLASH ((unsigned int *)AT91C_IFLASH + AT91C_IFLASH_SIZE - ENVIRONMENT_SIZE)

TEnvironmentBlock env;

static inline unsigned short RAMFUNC page_from_ramaddr(const void *addr)
{
	unsigned long ramaddr = (unsigned long) addr;
	ramaddr -= (unsigned long) AT91C_IFLASH;
	return ((ramaddr >> AT91C_IFLASH_PAGE_SHIFT));
}

static inline int RAMFUNC is_page_locked(unsigned short page)
{
	unsigned short lockregion = LOCKREGION_FROM_PAGE(page);

	return (AT91C_BASE_MC->MC_FSR & (lockregion << 16));
}

static inline void RAMFUNC flash_cmd_wait(void)
{
	while(( *AT91C_MC_FSR & AT91C_MC_FRDY ) != AT91C_MC_FRDY );
}

static inline void RAMFUNC unlock_page(unsigned short page)
{
	page &= 0x3ff;
	AT91F_MC_EFC_PerformCmd(AT91C_BASE_MC, AT91C_MC_FCMD_UNLOCK |
				AT91C_MC_CORRECT_KEY | (page << 8));
	flash_cmd_wait();
}

static inline void RAMFUNC flash_page(const void *addr)
{
	unsigned short page = page_from_ramaddr(addr) & 0x3ff;

	if (is_page_locked(page))
	    unlock_page(page);

	AT91F_MC_EFC_PerformCmd(AT91C_BASE_MC, AT91C_MC_FCMD_START_PROG |
				AT91C_MC_CORRECT_KEY | (page << 8));
	flash_cmd_wait();
}

unsigned short RAMFUNC env_crc16 (const unsigned char *buffer, int size)
{
    unsigned short crc = 0xFFFF;

    if(buffer && size)
        while (size--)
        {
            crc = (crc >> 8) | (crc << 8);
            crc ^= *buffer++;
            crc ^= ((unsigned char) crc) >> 4;
            crc ^= crc << 12;
            crc ^= (crc & 0xFF) << 5;
        }

    return crc;
}

void RAMFUNC env_store(void)
{
	unsigned int i,*src,*dst;
	
	/* During flashing only RAMFUNC code may be executed. 
	 * For now, this means that no other code whatsoever may
	 * be run until this function returns. */
	vTaskSuspendAll();
	portENTER_CRITICAL();
	
	env.e.magic=TENVIRONMENT_MAGIC;	
	env.e.crc16=0;
	env.e.size=sizeof(env);
	env.e.crc16=env_crc16((unsigned char*)&env,sizeof(env));
	
	src=env.data;
	dst=ENV_FLASH;
        for (i = 0; i < (sizeof(env.data)/sizeof(env.data[0])); i++)
	    *dst++ = *src++;

	flash_page(ENV_FLASH);
	
	portEXIT_CRITICAL();
	xTaskResumeAll();
}

int env_load(void)
{
	unsigned int crc;
    
	memcpy(&env,ENV_FLASH,sizeof(env));
	
	if(env.e.magic!=TENVIRONMENT_MAGIC || env.e.size!=sizeof(env) )
	    return 0;
    
	crc=env.e.crc16;
	env.e.crc16=0;
    
	return env_crc16((unsigned char*)&env,sizeof(env))==crc;
}

void env_init(void)
{
	unsigned int fmcn = AT91F_MC_EFC_ComputeFMCN(MCK);
	
	AT91F_MC_EFC_CfgModeReg(AT91C_BASE_MC, fmcn << 16 | AT91C_MC_FWS_3FWS);
}
personal git repositories of Harald Welte. Your mileage may vary