3 * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
6 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
7 * Marius Groeger <mgroeger@sysgo.de>
10 * Robert Schwebel, Pengutronix, <r.schwebel@pengutronix.de>
12 * See file CREDITS for list of people who contributed to this
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License as
17 * published by the Free Software Foundation; either version 2 of
18 * the License, or (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
32 #include <asm/arch/pxa-regs.h>
34 #define FLASH_BANK_SIZE 0x02000000
35 #define MAIN_SECT_SIZE 0x40000 /* 2x16 = 256k per sector */
37 flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
41 * flash_init: - initialize data structures for flash chips
43 * @return: size of the flash
46 ulong flash_init(void)
51 for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) {
53 flash_info[i].flash_id =
54 (INTEL_MANUFACT & FLASH_VENDMASK) |
55 (INTEL_ID_28F128J3 & FLASH_TYPEMASK);
56 flash_info[i].size = FLASH_BANK_SIZE;
57 flash_info[i].sector_count = CFG_MAX_FLASH_SECT;
58 memset(flash_info[i].protect, 0, CFG_MAX_FLASH_SECT);
62 flashbase = PHYS_FLASH_1;
65 panic("configured too many flash banks!\n");
68 for (j = 0; j < flash_info[i].sector_count; j++) {
69 flash_info[i].start[j] = flashbase + j*MAIN_SECT_SIZE;
71 size += flash_info[i].size;
74 /* Protect monitor and environment sectors */
75 flash_protect(FLAG_PROTECT_SET,
77 CFG_FLASH_BASE + monitor_flash_len - 1,
80 flash_protect(FLAG_PROTECT_SET,
82 CFG_ENV_ADDR + CFG_ENV_SIZE - 1,
90 * flash_print_info: - print information about the flash situation
95 void flash_print_info (flash_info_t *info)
99 for (j=0; j<CFG_MAX_FLASH_BANKS; j++) {
101 switch (info->flash_id & FLASH_VENDMASK) {
103 case (INTEL_MANUFACT & FLASH_VENDMASK):
107 printf("Unknown Vendor ");
111 switch (info->flash_id & FLASH_TYPEMASK) {
113 case (INTEL_ID_28F128J3 & FLASH_TYPEMASK):
114 printf("28F128J3 (128Mbit)\n");
117 printf("Unknown Chip Type\n");
121 printf(" Size: %ld MB in %d Sectors\n",
122 info->size >> 20, info->sector_count);
124 printf(" Sector Start Addresses:");
125 for (i = 0; i < info->sector_count; i++) {
126 if ((i % 5) == 0) printf ("\n ");
128 printf (" %08lX%s", info->start[i],
129 info->protect[i] ? " (RO)" : " ");
138 * flash_erase: - erase flash sectors
142 int flash_erase(flash_info_t *info, int s_first, int s_last)
144 int flag, prot, sect;
147 if (info->flash_id == FLASH_UNKNOWN)
148 return ERR_UNKNOWN_FLASH_TYPE;
150 if ((s_first < 0) || (s_first > s_last)) {
154 if ((info->flash_id & FLASH_VENDMASK) != (INTEL_MANUFACT & FLASH_VENDMASK))
155 return ERR_UNKNOWN_FLASH_VENDOR;
158 for (sect=s_first; sect<=s_last; ++sect) {
159 if (info->protect[sect]) prot++;
162 if (prot) return ERR_PROTECTED;
165 * Disable interrupts which might cause a timeout
166 * here. Remember that our exception vectors are
167 * at address 0 in the flash, and we don't want a
168 * (ticker) exception to happen while the flash
169 * chip is in programming mode.
172 flag = disable_interrupts();
174 /* Start erase on unprotected sectors */
175 for (sect = s_first; sect<=s_last && !ctrlc(); sect++) {
177 printf("Erasing sector %2d ... ", sect);
179 /* arm simple, non interrupt dependent timer */
180 reset_timer_masked();
182 if (info->protect[sect] == 0) { /* not protected */
183 u32 * volatile addr = (u32 * volatile)(info->start[sect]);
186 /* The strata flashs are aligned side by side on */
187 /* the data bus, so we have to write the commands */
188 /* to both chips here: */
190 *addr = 0x00200020; /* erase setup */
191 *addr = 0x00D000D0; /* erase confirm */
193 while ((*addr & 0x00800080) != 0x00800080) {
194 if (get_timer_masked() > CFG_FLASH_ERASE_TOUT) {
195 *addr = 0x00B000B0; /* suspend erase*/
196 *addr = 0x00FF00FF; /* read mode */
202 *addr = 0x00500050; /* clear status register cmd. */
203 *addr = 0x00FF00FF; /* resest to read mode */
210 if (ctrlc()) printf("User Interrupt!\n");
214 /* allow flash to settle - wait 10 ms */
215 udelay_masked(10000);
217 if (flag) enable_interrupts();
224 * write_word: - copy memory to flash
232 static int write_word (flash_info_t *info, ulong dest, ushort data)
234 u32 * volatile addr = (u32 * volatile)dest, val;
238 /* Check if Flash is (sufficiently) erased */
239 if ((*addr & data) != data) return ERR_NOT_ERASED;
242 * Disable interrupts which might cause a timeout
243 * here. Remember that our exception vectors are
244 * at address 0 in the flash, and we don't want a
245 * (ticker) exception to happen while the flash
246 * chip is in programming mode.
248 flag = disable_interrupts();
250 /* clear status register command */
253 /* program set-up command */
256 /* latch address/data */
259 /* arm simple, non interrupt dependent timer */
260 reset_timer_masked();
262 /* wait while polling the status register */
263 while(((val = *addr) & 0x80) != 0x80) {
264 if (get_timer_masked() > CFG_FLASH_WRITE_TOUT) {
266 *addr = 0xB0; /* suspend program command */
271 if(val & 0x1A) { /* check for error */
272 printf("\nFlash write error %02x at address %08lx\n",
273 (int)val, (unsigned long)dest);
275 printf("Voltage range error.\n");
280 printf("Device protect error.\n");
285 printf("Programming error.\n");
295 *addr = 0xFF; /* read array command */
296 if (flag) enable_interrupts();
303 * write_buf: - Copy memory to flash.
306 * @param src: source of copy transaction
307 * @param addr: where to copy to
308 * @param cnt: number of bytes to copy
313 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
320 wp = (addr & ~1); /* get lower word aligned address */
323 * handle unaligned start bytes
325 if ((l = addr - wp) != 0) {
327 for (i=0, cp=wp; i<l; ++i, ++cp) {
328 data = (data >> 8) | (*(uchar *)cp << 8);
330 for (; i<2 && cnt>0; ++i) {
331 data = (data >> 8) | (*src++ << 8);
335 for (; cnt==0 && i<2; ++i, ++cp) {
336 data = (data >> 8) | (*(uchar *)cp << 8);
339 if ((rc = write_word(info, wp, data)) != 0) {
346 * handle word aligned part
349 /* data = *((vushort*)src); */
350 data = *((ushort*)src);
351 if ((rc = write_word(info, wp, data)) != 0) {
359 if (cnt == 0) return ERR_OK;
362 * handle unaligned tail bytes
365 for (i=0, cp=wp; i<2 && cnt>0; ++i, ++cp) {
366 data = (data >> 8) | (*src++ << 8);
369 for (; i<2; ++i, ++cp) {
370 data = (data >> 8) | (*(uchar *)cp << 8);
373 return write_word(info, wp, data);