summaryrefslogtreecommitdiff
path: root/board/BuS/EB+MCF-EV123/cfm_flash.c
blob: d5a1c30be50e94dc14ffbcea5c03a1022dd2c0c2 (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
 * Basic Flash Driver for Freescale MCF 5281/5282 internal FLASH
 *
 * (C) Copyright 2005 BuS Elektronik GmbH & Co.KG <esw@bus-elektonik.de>
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * 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; either version 2 of
 * the License, or (at your option) any later version.
 *
 * 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., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 */

#include <common.h> 
#include <asm/m5282.h>
#include  "cfm_flash.h"

#if defined(CONFIG_M5281) || defined(CONFIG_M5282)

#if (CFG_CLK>20000000)
	#define CFM_CLK  (((long) CFG_CLK / (400000 * 8) + 1) | 0x40)
#else
	#define CFM_CLK  ((long) CFG_CLK / 400000 + 1)
#endif

#define cmf_backdoor_address(addr)	(((addr) & 0x0007FFFF) | 0x04000000 | \
					 (CFG_MBAR & 0xC0000000))

void cfm_flash_print_info (flash_info_t * info)
{
	printf ("Freescale: ");
	switch (info->flash_id & FLASH_TYPEMASK) {
	case FREESCALE_ID_MCF5281 & FLASH_TYPEMASK:
		printf ("MCF5281 internal FLASH\n");
		break;
	case FREESCALE_ID_MCF5282 & FLASH_TYPEMASK:
		printf ("MCF5282 internal FLASH\n");
		break;
	default:
		printf ("Unknown Chip Type\n");
		break;
	}
}

void cfm_flash_init (flash_info_t * info)
{
	int sector;
	ulong protection;
	MCFCFM_MCR = 0;
	MCFCFM_CLKD = CFM_CLK;
	debug ("CFM Clock divider: %ld (%d Hz @ %ld Hz)\n",CFM_CLK,\
	 	CFG_CLK / (2* ((CFM_CLK & 0x3F)+1) * (1+((CFM_CLK & 0x40)>>6)*7)),\
		CFG_CLK);
	MCFCFM_SACC = 0;
	MCFCFM_DACC = 0;

	if (MCFCFM_SEC & MCFCFM_SEC_KEYEN)
		puts("CFM backdoor access is enabled\n");
	if (MCFCFM_SEC & MCFCFM_SEC_SECSTAT)
		puts("CFM securety is enabled\n");

	#ifdef CONFIG_M5281
		info->flash_id = (FREESCALE_MANUFACT & FLASH_VENDMASK) |
				 (FREESCALE_ID_MCF5281 & FLASH_TYPEMASK);
		info->size = 256*1024;
		info->sector_count = 16;
	#else
		info->flash_id = (FREESCALE_MANUFACT & FLASH_VENDMASK) |
				 (FREESCALE_ID_MCF5282 & FLASH_TYPEMASK);
		info->size = 512*1024;
		info->sector_count = 32;
	#endif
	protection = MCFCFM_PROT;
	for (sector = 0; sector < info->sector_count; sector++)
	{
		if (sector == 0)
		{
			info->start[sector] = CFG_INT_FLASH_BASE;
		}
		else
		{
			info->start[sector] = info->start[sector-1] + 0x04000;
		}
		info->protect[sector] = protection & 1;
		protection >>= 1;
	}
}

int cfm_flash_readycheck(int checkblank)
{
	int	rc;
	unsigned char state;

	rc	= ERR_OK;
	while (!(MCFCFM_USTAT & MCFCFM_USTAT_CCIF));
	state = MCFCFM_USTAT;
	if (state & MCFCFM_USTAT_ACCERR)
	{
		debug ("%s(): CFM access error",__FUNCTION__);
		rc = ERR_PROG_ERROR;
	}
	if (state & MCFCFM_USTAT_PVIOL)
	{
		debug ("%s(): CFM protection violation",__FUNCTION__);
		rc = ERR_PROTECTED;
	}
	if (checkblank)
	{
		if (!(state & MCFCFM_USTAT_BLANK))
		{
			debug ("%s(): CFM erras error",__FUNCTION__);
			rc = ERR_NOT_ERASED;
		}
	}
	MCFCFM_USTAT = state & 0x34; /* reset state */
	return rc;
}

/* Erase 16KiB = 8 2KiB pages */

int cfm_flash_erase_sector (flash_info_t * info, int sector)
{
	ulong address;
	int page;
	int rc;
	rc= ERR_OK;
	address = cmf_backdoor_address(info->start[sector]);
	for (page=0; (page<8) && (rc==ERR_OK); page++)
	{
		*(volatile __u32*) address = 0;
		MCFCFM_CMD = MCFCFM_CMD_PGERS;
		MCFCFM_USTAT = MCFCFM_USTAT_CBEIF;
		rc = cfm_flash_readycheck(0);
		if (rc==ERR_OK)
		{
			*(volatile __u32*) address = 0;
			MCFCFM_CMD = MCFCFM_CMD_PGERSVER;
			MCFCFM_USTAT = MCFCFM_USTAT_CBEIF;
			rc = cfm_flash_readycheck(1);
		}
		address += 0x800;
	}
	return rc;
}

int cfm_flash_write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
{
	int rc;
	ulong dest, data;

	rc = ERR_OK;
	if (addr & 3)
	{
		debug ("Byte and Word alignment not supported\n");
		rc = ERR_ALIGN;
	}
	if (cnt & 3)
	{
		debug ("Byte and Word transfer not supported\n");
		rc = ERR_ALIGN;
	}
	dest = cmf_backdoor_address(addr);
	while ((cnt>=4) && (rc == ERR_OK))
	{
		data =*((volatile u32 *) src);
		*(volatile u32*) dest = data;
		MCFCFM_CMD = MCFCFM_CMD_PGM;
		MCFCFM_USTAT = MCFCFM_USTAT_CBEIF;
		rc = cfm_flash_readycheck(0);
		if (*(volatile u32*) addr != data) rc = ERR_PROG_ERROR;
		src +=4;
		dest +=4;
		addr +=4;
		cnt -=4;
	}
	return rc;
}

#ifdef CFG_FLASH_PROTECTION

int cfm_flash_protect(flash_info_t * info,long sector,int prot)
{
	int rc;

	rc= ERR_OK;
	if (prot)
	{
		MCFCFM_PROT |= (1<<sector);
		info->protect[sector]=1;
	}
	else
	{
		MCFCFM_PROT &= ~(1<<sector);
		info->protect[sector]=0;
	}
	return rc;
}

#endif

#endif