blob: e10347067c807411c3279ce6f4899d62c1bde389 [file] [log] [blame]
wdenkf9087a32002-11-03 00:30:25 +00001/*
2 * (C) Copyright 2002
3 * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
4 *
5 * (C) Copyright 2002
6 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
7 * Marius Groeger <mgroeger@sysgo.de>
8 *
wdenk384ae022002-11-05 00:17:55 +00009 * (C) Copyright 2002
wdenk8bde7f72003-06-27 21:31:46 +000010 * Robert Schwebel, Pengutronix, <r.schwebel@pengutronix.de>
wdenk384ae022002-11-05 00:17:55 +000011 *
wdenkb98fff12004-02-09 20:51:26 +000012 * (C) Copyright 2003 (2 x 16 bit Flash bank patches)
13 * Rolf Peukert, IMMS gGmbH, <rolf.peukert@imms.de>
14 *
wdenkf9087a32002-11-03 00:30:25 +000015 * See file CREDITS for list of people who contributed to this
16 * project.
17 *
18 * This program is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU General Public License as
20 * published by the Free Software Foundation; either version 2 of
21 * the License, or (at your option) any later version.
22 *
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
wdenkb98fff12004-02-09 20:51:26 +000025 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wdenkf9087a32002-11-03 00:30:25 +000026 * GNU General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
31 * MA 02111-1307 USA
32 */
33
34#include <common.h>
wdenk384ae022002-11-05 00:17:55 +000035#include <asm/arch/pxa-regs.h>
wdenkf9087a32002-11-03 00:30:25 +000036
37#define FLASH_BANK_SIZE 0x02000000
wdenkb98fff12004-02-09 20:51:26 +000038#define MAIN_SECT_SIZE 0x40000 /* 2x16 = 256k per sector */
wdenkf9087a32002-11-03 00:30:25 +000039
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020040flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
wdenkf9087a32002-11-03 00:30:25 +000041
42
wdenk384ae022002-11-05 00:17:55 +000043/**
44 * flash_init: - initialize data structures for flash chips
45 *
46 * @return: size of the flash
wdenkf9087a32002-11-03 00:30:25 +000047 */
48
49ulong flash_init(void)
50{
wdenk47cd00f2003-03-06 13:39:27 +000051 int i, j;
52 ulong size = 0;
wdenkf9087a32002-11-03 00:30:25 +000053
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020054 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
wdenk47cd00f2003-03-06 13:39:27 +000055 ulong flashbase = 0;
56 flash_info[i].flash_id =
wdenkb98fff12004-02-09 20:51:26 +000057 (INTEL_MANUFACT & FLASH_VENDMASK) |
58 (INTEL_ID_28F128J3 & FLASH_TYPEMASK);
wdenk47cd00f2003-03-06 13:39:27 +000059 flash_info[i].size = FLASH_BANK_SIZE;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020060 flash_info[i].sector_count = CONFIG_SYS_MAX_FLASH_SECT;
61 memset(flash_info[i].protect, 0, CONFIG_SYS_MAX_FLASH_SECT);
wdenkf9087a32002-11-03 00:30:25 +000062
wdenk384ae022002-11-05 00:17:55 +000063 switch (i) {
wdenkb98fff12004-02-09 20:51:26 +000064 case 0:
65 flashbase = PHYS_FLASH_1;
66 break;
67 default:
68 panic("configured too many flash banks!\n");
69 break;
wdenk47cd00f2003-03-06 13:39:27 +000070 }
wdenk384ae022002-11-05 00:17:55 +000071 for (j = 0; j < flash_info[i].sector_count; j++) {
wdenk47cd00f2003-03-06 13:39:27 +000072 flash_info[i].start[j] = flashbase + j*MAIN_SECT_SIZE;
73 }
74 size += flash_info[i].size;
wdenkf9087a32002-11-03 00:30:25 +000075 }
wdenkf9087a32002-11-03 00:30:25 +000076
wdenk384ae022002-11-05 00:17:55 +000077 /* Protect monitor and environment sectors */
wdenk47cd00f2003-03-06 13:39:27 +000078 flash_protect(FLAG_PROTECT_SET,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020079 CONFIG_SYS_FLASH_BASE,
80 CONFIG_SYS_FLASH_BASE + monitor_flash_len - 1,
wdenk47cd00f2003-03-06 13:39:27 +000081 &flash_info[0]);
wdenkf9087a32002-11-03 00:30:25 +000082
wdenk47cd00f2003-03-06 13:39:27 +000083 flash_protect(FLAG_PROTECT_SET,
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020084 CONFIG_ENV_ADDR,
85 CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1,
wdenk47cd00f2003-03-06 13:39:27 +000086 &flash_info[0]);
wdenkf9087a32002-11-03 00:30:25 +000087
wdenk47cd00f2003-03-06 13:39:27 +000088 return size;
wdenkf9087a32002-11-03 00:30:25 +000089}
90
wdenk384ae022002-11-05 00:17:55 +000091
92/**
93 * flash_print_info: - print information about the flash situation
wdenkf9087a32002-11-03 00:30:25 +000094 */
wdenk384ae022002-11-05 00:17:55 +000095
wdenkf9087a32002-11-03 00:30:25 +000096void flash_print_info (flash_info_t *info)
97{
wdenk47cd00f2003-03-06 13:39:27 +000098 int i, j;
wdenkf9087a32002-11-03 00:30:25 +000099
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200100 for (j=0; j<CONFIG_SYS_MAX_FLASH_BANKS; j++) {
wdenk384ae022002-11-05 00:17:55 +0000101
102 switch (info->flash_id & FLASH_VENDMASK) {
wdenkb98fff12004-02-09 20:51:26 +0000103 case (INTEL_MANUFACT & FLASH_VENDMASK):
104 printf ("Intel: ");
105 break;
106 default:
107 printf ("Unknown Vendor ");
108 break;
wdenk47cd00f2003-03-06 13:39:27 +0000109 }
wdenkf9087a32002-11-03 00:30:25 +0000110
wdenk384ae022002-11-05 00:17:55 +0000111 switch (info->flash_id & FLASH_TYPEMASK) {
wdenkb98fff12004-02-09 20:51:26 +0000112 case (INTEL_ID_28F128J3 & FLASH_TYPEMASK):
113 printf("28F128J3 (128Mbit)\n");
114 break;
115 default:
116 printf("Unknown Chip Type\n");
117 return;
wdenk47cd00f2003-03-06 13:39:27 +0000118 }
wdenkf9087a32002-11-03 00:30:25 +0000119
wdenk8bde7f72003-06-27 21:31:46 +0000120 printf(" Size: %ld MB in %d Sectors\n",
wdenk47cd00f2003-03-06 13:39:27 +0000121 info->size >> 20, info->sector_count);
wdenkf9087a32002-11-03 00:30:25 +0000122
wdenk47cd00f2003-03-06 13:39:27 +0000123 printf(" Sector Start Addresses:");
wdenk384ae022002-11-05 00:17:55 +0000124 for (i = 0; i < info->sector_count; i++) {
wdenkb98fff12004-02-09 20:51:26 +0000125 if ((i % 5) == 0) printf ("\n ");
wdenk8bde7f72003-06-27 21:31:46 +0000126
wdenk47cd00f2003-03-06 13:39:27 +0000127 printf (" %08lX%s", info->start[i],
wdenkb98fff12004-02-09 20:51:26 +0000128 info->protect[i] ? " (RO)" : " ");
wdenk47cd00f2003-03-06 13:39:27 +0000129 }
130 printf ("\n");
131 info++;
132 }
wdenkf9087a32002-11-03 00:30:25 +0000133}
134
wdenk384ae022002-11-05 00:17:55 +0000135
136/**
137 * flash_erase: - erase flash sectors
wdenkf9087a32002-11-03 00:30:25 +0000138 */
139
wdenk47cd00f2003-03-06 13:39:27 +0000140int flash_erase(flash_info_t *info, int s_first, int s_last)
wdenkf9087a32002-11-03 00:30:25 +0000141{
wdenk47cd00f2003-03-06 13:39:27 +0000142 int flag, prot, sect;
143 int rc = ERR_OK;
Graeme Russa60d1e52011-07-15 23:31:37 +0000144 ulong start;
wdenkf9087a32002-11-03 00:30:25 +0000145
wdenk47cd00f2003-03-06 13:39:27 +0000146 if (info->flash_id == FLASH_UNKNOWN)
147 return ERR_UNKNOWN_FLASH_TYPE;
wdenkf9087a32002-11-03 00:30:25 +0000148
wdenk47cd00f2003-03-06 13:39:27 +0000149 if ((s_first < 0) || (s_first > s_last)) {
150 return ERR_INVAL;
151 }
wdenkf9087a32002-11-03 00:30:25 +0000152
wdenk384ae022002-11-05 00:17:55 +0000153 if ((info->flash_id & FLASH_VENDMASK) != (INTEL_MANUFACT & FLASH_VENDMASK))
wdenk47cd00f2003-03-06 13:39:27 +0000154 return ERR_UNKNOWN_FLASH_VENDOR;
wdenk8bde7f72003-06-27 21:31:46 +0000155
wdenk47cd00f2003-03-06 13:39:27 +0000156 prot = 0;
157 for (sect=s_first; sect<=s_last; ++sect) {
wdenk384ae022002-11-05 00:17:55 +0000158 if (info->protect[sect]) prot++;
wdenkf9087a32002-11-03 00:30:25 +0000159 }
wdenk384ae022002-11-05 00:17:55 +0000160
161 if (prot) return ERR_PROTECTED;
wdenkf9087a32002-11-03 00:30:25 +0000162
wdenk47cd00f2003-03-06 13:39:27 +0000163 /*
164 * Disable interrupts which might cause a timeout
165 * here. Remember that our exception vectors are
166 * at address 0 in the flash, and we don't want a
167 * (ticker) exception to happen while the flash
168 * chip is in programming mode.
169 */
wdenkf9087a32002-11-03 00:30:25 +0000170
wdenk47cd00f2003-03-06 13:39:27 +0000171 flag = disable_interrupts();
wdenkf9087a32002-11-03 00:30:25 +0000172
wdenk47cd00f2003-03-06 13:39:27 +0000173 /* Start erase on unprotected sectors */
174 for (sect = s_first; sect<=s_last && !ctrlc(); sect++) {
wdenkf9087a32002-11-03 00:30:25 +0000175
wdenk47cd00f2003-03-06 13:39:27 +0000176 printf("Erasing sector %2d ... ", sect);
wdenkf9087a32002-11-03 00:30:25 +0000177
wdenk47cd00f2003-03-06 13:39:27 +0000178 /* arm simple, non interrupt dependent timer */
Graeme Russa60d1e52011-07-15 23:31:37 +0000179 start = get_timer(0);
wdenk47cd00f2003-03-06 13:39:27 +0000180
wdenkb98fff12004-02-09 20:51:26 +0000181 if (info->protect[sect] == 0) { /* not protected */
wdenk384ae022002-11-05 00:17:55 +0000182 u32 * volatile addr = (u32 * volatile)(info->start[sect]);
wdenkf9087a32002-11-03 00:30:25 +0000183
wdenkb98fff12004-02-09 20:51:26 +0000184 /* erase sector: */
wdenk384ae022002-11-05 00:17:55 +0000185 /* The strata flashs are aligned side by side on */
186 /* the data bus, so we have to write the commands */
wdenkb98fff12004-02-09 20:51:26 +0000187 /* to both chips here: */
wdenkf9087a32002-11-03 00:30:25 +0000188
wdenk384ae022002-11-05 00:17:55 +0000189 *addr = 0x00200020; /* erase setup */
190 *addr = 0x00D000D0; /* erase confirm */
191
192 while ((*addr & 0x00800080) != 0x00800080) {
Graeme Russa60d1e52011-07-15 23:31:37 +0000193 if (get_timer(start) > CONFIG_SYS_FLASH_ERASE_TOUT) {
wdenk384ae022002-11-05 00:17:55 +0000194 *addr = 0x00B000B0; /* suspend erase*/
195 *addr = 0x00FF00FF; /* read mode */
wdenk47cd00f2003-03-06 13:39:27 +0000196 rc = ERR_TIMOUT;
197 goto outahere;
198 }
199 }
wdenk384ae022002-11-05 00:17:55 +0000200 *addr = 0x00500050; /* clear status register cmd. */
wdenkb98fff12004-02-09 20:51:26 +0000201 *addr = 0x00FF00FF; /* reset to read mode */
wdenk47cd00f2003-03-06 13:39:27 +0000202 }
wdenk47cd00f2003-03-06 13:39:27 +0000203 printf("ok.\n");
204 }
wdenk384ae022002-11-05 00:17:55 +0000205 if (ctrlc()) printf("User Interrupt!\n");
wdenkf9087a32002-11-03 00:30:25 +0000206
wdenkb98fff12004-02-09 20:51:26 +0000207outahere:
wdenk47cd00f2003-03-06 13:39:27 +0000208 /* allow flash to settle - wait 10 ms */
209 udelay_masked(10000);
wdenkf9087a32002-11-03 00:30:25 +0000210
wdenk384ae022002-11-05 00:17:55 +0000211 if (flag) enable_interrupts();
wdenkf9087a32002-11-03 00:30:25 +0000212
wdenk47cd00f2003-03-06 13:39:27 +0000213 return rc;
wdenkf9087a32002-11-03 00:30:25 +0000214}
215
wdenk384ae022002-11-05 00:17:55 +0000216/**
wdenkb98fff12004-02-09 20:51:26 +0000217 * write_long: - copy memory to flash, assume a bank of 2 devices with 16bit each
wdenkf9087a32002-11-03 00:30:25 +0000218 */
219
wdenkb98fff12004-02-09 20:51:26 +0000220static int write_long (flash_info_t *info, ulong dest, ulong data)
wdenkf9087a32002-11-03 00:30:25 +0000221{
wdenk47cd00f2003-03-06 13:39:27 +0000222 u32 * volatile addr = (u32 * volatile)dest, val;
223 int rc = ERR_OK;
224 int flag;
Graeme Russa60d1e52011-07-15 23:31:37 +0000225 ulong start;
wdenkf9087a32002-11-03 00:30:25 +0000226
wdenkb98fff12004-02-09 20:51:26 +0000227 /* read array command - just for the case... */
228 *addr = 0x00FF00FF;
229
wdenk384ae022002-11-05 00:17:55 +0000230 /* Check if Flash is (sufficiently) erased */
231 if ((*addr & data) != data) return ERR_NOT_ERASED;
wdenkf9087a32002-11-03 00:30:25 +0000232
wdenk47cd00f2003-03-06 13:39:27 +0000233 /*
234 * Disable interrupts which might cause a timeout
235 * here. Remember that our exception vectors are
236 * at address 0 in the flash, and we don't want a
237 * (ticker) exception to happen while the flash
238 * chip is in programming mode.
239 */
240 flag = disable_interrupts();
wdenkf9087a32002-11-03 00:30:25 +0000241
wdenk47cd00f2003-03-06 13:39:27 +0000242 /* clear status register command */
wdenkb98fff12004-02-09 20:51:26 +0000243 *addr = 0x00500050;
wdenkf9087a32002-11-03 00:30:25 +0000244
wdenk47cd00f2003-03-06 13:39:27 +0000245 /* program set-up command */
wdenkb98fff12004-02-09 20:51:26 +0000246 *addr = 0x00400040;
wdenkf9087a32002-11-03 00:30:25 +0000247
wdenk47cd00f2003-03-06 13:39:27 +0000248 /* latch address/data */
249 *addr = data;
wdenkf9087a32002-11-03 00:30:25 +0000250
wdenk47cd00f2003-03-06 13:39:27 +0000251 /* arm simple, non interrupt dependent timer */
Graeme Russa60d1e52011-07-15 23:31:37 +0000252 start = get_timer(0);
wdenkf9087a32002-11-03 00:30:25 +0000253
wdenk47cd00f2003-03-06 13:39:27 +0000254 /* wait while polling the status register */
wdenkb98fff12004-02-09 20:51:26 +0000255 while(((val = *addr) & 0x00800080) != 0x00800080) {
Graeme Russa60d1e52011-07-15 23:31:37 +0000256 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
wdenk47cd00f2003-03-06 13:39:27 +0000257 rc = ERR_TIMOUT;
wdenkb98fff12004-02-09 20:51:26 +0000258 /* suspend program command */
259 *addr = 0x00B000B0;
wdenk47cd00f2003-03-06 13:39:27 +0000260 goto outahere;
261 }
wdenkf9087a32002-11-03 00:30:25 +0000262 }
wdenkf9087a32002-11-03 00:30:25 +0000263
wdenkb98fff12004-02-09 20:51:26 +0000264 /* check for errors */
265 if(val & 0x001A001A) {
wdenk47cd00f2003-03-06 13:39:27 +0000266 printf("\nFlash write error %02x at address %08lx\n",
267 (int)val, (unsigned long)dest);
wdenkb98fff12004-02-09 20:51:26 +0000268 if(val & 0x00080008) {
wdenk47cd00f2003-03-06 13:39:27 +0000269 printf("Voltage range error.\n");
270 rc = ERR_PROG_ERROR;
271 goto outahere;
272 }
wdenkb98fff12004-02-09 20:51:26 +0000273 if(val & 0x00020002) {
wdenk47cd00f2003-03-06 13:39:27 +0000274 printf("Device protect error.\n");
275 rc = ERR_PROTECTED;
276 goto outahere;
277 }
wdenkb98fff12004-02-09 20:51:26 +0000278 if(val & 0x00100010) {
wdenk47cd00f2003-03-06 13:39:27 +0000279 printf("Programming error.\n");
280 rc = ERR_PROG_ERROR;
281 goto outahere;
282 }
283 rc = ERR_PROG_ERROR;
284 goto outahere;
285 }
wdenkf9087a32002-11-03 00:30:25 +0000286
wdenkb98fff12004-02-09 20:51:26 +0000287outahere:
288 /* read array command */
289 *addr = 0x00FF00FF;
wdenk384ae022002-11-05 00:17:55 +0000290 if (flag) enable_interrupts();
wdenkf9087a32002-11-03 00:30:25 +0000291
wdenk47cd00f2003-03-06 13:39:27 +0000292 return rc;
wdenkf9087a32002-11-03 00:30:25 +0000293}
294
wdenk384ae022002-11-05 00:17:55 +0000295
296/**
297 * write_buf: - Copy memory to flash.
wdenk8bde7f72003-06-27 21:31:46 +0000298 *
299 * @param info:
wdenk384ae022002-11-05 00:17:55 +0000300 * @param src: source of copy transaction
wdenkb98fff12004-02-09 20:51:26 +0000301 * @param addr: where to copy to
302 * @param cnt: number of bytes to copy
wdenk384ae022002-11-05 00:17:55 +0000303 *
304 * @return error code
wdenkf9087a32002-11-03 00:30:25 +0000305 */
306
wdenkb98fff12004-02-09 20:51:26 +0000307/* "long" version, uses 32bit words */
wdenkf9087a32002-11-03 00:30:25 +0000308int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
309{
wdenk47cd00f2003-03-06 13:39:27 +0000310 ulong cp, wp;
wdenkb98fff12004-02-09 20:51:26 +0000311 ulong data;
wdenk47cd00f2003-03-06 13:39:27 +0000312 int l;
313 int i, rc;
wdenkf9087a32002-11-03 00:30:25 +0000314
wdenkb98fff12004-02-09 20:51:26 +0000315 wp = (addr & ~3); /* get lower word aligned address */
wdenkf9087a32002-11-03 00:30:25 +0000316
wdenk47cd00f2003-03-06 13:39:27 +0000317 /*
318 * handle unaligned start bytes
319 */
320 if ((l = addr - wp) != 0) {
321 data = 0;
322 for (i=0, cp=wp; i<l; ++i, ++cp) {
wdenkb98fff12004-02-09 20:51:26 +0000323 data = (data >> 8) | (*(uchar *)cp << 24);
wdenk47cd00f2003-03-06 13:39:27 +0000324 }
wdenkb98fff12004-02-09 20:51:26 +0000325 for (; i<4 && cnt>0; ++i) {
326 data = (data >> 8) | (*src++ << 24);
wdenk47cd00f2003-03-06 13:39:27 +0000327 --cnt;
328 ++cp;
329 }
wdenkb98fff12004-02-09 20:51:26 +0000330 for (; cnt==0 && i<4; ++i, ++cp) {
331 data = (data >> 8) | (*(uchar *)cp << 24);
wdenk47cd00f2003-03-06 13:39:27 +0000332 }
333
wdenkb98fff12004-02-09 20:51:26 +0000334 if ((rc = write_long(info, wp, data)) != 0) {
wdenk47cd00f2003-03-06 13:39:27 +0000335 return (rc);
336 }
wdenkb98fff12004-02-09 20:51:26 +0000337 wp += 4;
wdenkf9087a32002-11-03 00:30:25 +0000338 }
339
wdenk47cd00f2003-03-06 13:39:27 +0000340 /*
341 * handle word aligned part
342 */
wdenkb98fff12004-02-09 20:51:26 +0000343 while (cnt >= 4) {
344 data = *((ulong*)src);
345 if ((rc = write_long(info, wp, data)) != 0) {
wdenk47cd00f2003-03-06 13:39:27 +0000346 return (rc);
347 }
wdenkb98fff12004-02-09 20:51:26 +0000348 src += 4;
349 wp += 4;
350 cnt -= 4;
wdenkf9087a32002-11-03 00:30:25 +0000351 }
wdenkf9087a32002-11-03 00:30:25 +0000352
wdenk384ae022002-11-05 00:17:55 +0000353 if (cnt == 0) return ERR_OK;
wdenkf9087a32002-11-03 00:30:25 +0000354
wdenk47cd00f2003-03-06 13:39:27 +0000355 /*
356 * handle unaligned tail bytes
357 */
358 data = 0;
wdenkb98fff12004-02-09 20:51:26 +0000359 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
360 data = (data >> 8) | (*src++ << 24);
wdenk47cd00f2003-03-06 13:39:27 +0000361 --cnt;
362 }
wdenkb98fff12004-02-09 20:51:26 +0000363 for (; i<4; ++i, ++cp) {
364 data = (data >> 8) | (*(uchar *)cp << 24);
wdenk47cd00f2003-03-06 13:39:27 +0000365 }
wdenkf9087a32002-11-03 00:30:25 +0000366
wdenkb98fff12004-02-09 20:51:26 +0000367 return write_long(info, wp, data);
wdenkf9087a32002-11-03 00:30:25 +0000368}