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>
13 * Auerswald GmbH & Co KG, Germany
14 * Kai-Uwe Bloem <kai-uwe.bloem@auerswald.de>
16 * See file CREDITS for list of people who contributed to this
19 * This program is free software; you can redistribute it and/or
20 * modify it under the terms of the GNU General Public License as
21 * published by the Free Software Foundation; either version 2 of
22 * the License, or (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
36 #include <asm/arch/pxa-regs.h>
38 /* Debugging macros ------------------------------------------------------ */
42 /* Some debug macros */
43 #if (FLASH_DEBUG > 2 )
44 #define PRINTK3(args...) printf(args)
46 #define PRINTK3(args...)
50 #define PRINTK2(args...) printf(args)
52 #define PRINTK2(args...)
56 #define PRINTK(args...) printf(args)
58 #define PRINTK(args...)
61 /* ------------------------------------------------------------------------ */
63 /* Development system: we have only 16 MB Flash */
64 #ifdef CONFIG_MTD_INNOKOM_16MB
65 #define FLASH_BANK_SIZE 0x01000000 /* 16 MB (during development) */
66 #define MAIN_SECT_SIZE 0x00020000 /* 128k per sector */
69 /* Production system: we have 64 MB Flash */
70 #ifdef CONFIG_MTD_INNOKOM_64MB
71 #define FLASH_BANK_SIZE 0x04000000 /* 64 MB */
72 #define MAIN_SECT_SIZE 0x00020000 /* 128k per sector */
75 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
78 * flash_init: - initialize data structures for flash chips
80 * @return: size of the flash
83 ulong flash_init(void)
88 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
90 flash_info[i].flash_id =
91 (INTEL_MANUFACT & FLASH_VENDMASK) |
92 (INTEL_ID_28F128J3 & FLASH_TYPEMASK);
93 flash_info[i].size = FLASH_BANK_SIZE;
94 flash_info[i].sector_count = CONFIG_SYS_MAX_FLASH_SECT;
95 memset(flash_info[i].protect, 0, CONFIG_SYS_MAX_FLASH_SECT);
99 flashbase = PHYS_FLASH_1;
102 panic("configured too many flash banks!\n");
105 for (j = 0; j < flash_info[i].sector_count; j++) {
106 flash_info[i].start[j] = flashbase + j*MAIN_SECT_SIZE;
108 size += flash_info[i].size;
111 /* Protect u-boot sectors */
112 flash_protect(FLAG_PROTECT_SET,
113 CONFIG_SYS_FLASH_BASE,
114 CONFIG_SYS_FLASH_BASE + (256*1024) - 1,
117 #ifdef CONFIG_ENV_IS_IN_FLASH
118 flash_protect(FLAG_PROTECT_SET,
120 CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1,
129 * flash_print_info: - print information about the flash situation
134 void flash_print_info (flash_info_t *info)
138 for (j=0; j<CONFIG_SYS_MAX_FLASH_BANKS; j++) {
140 switch (info->flash_id & FLASH_VENDMASK) {
142 case (INTEL_MANUFACT & FLASH_VENDMASK):
146 printf("Unknown Vendor ");
150 switch (info->flash_id & FLASH_TYPEMASK) {
152 case (INTEL_ID_28F128J3 & FLASH_TYPEMASK):
153 printf("28F128J3 (128Mbit)\n");
156 printf("Unknown Chip Type\n");
160 printf(" Size: %ld MB in %d Sectors\n",
161 info->size >> 20, info->sector_count);
163 printf(" Sector Start Addresses:");
164 for (i = 0; i < info->sector_count; i++) {
165 if ((i % 5) == 0) printf ("\n ");
167 printf (" %08lX%s", info->start[i],
168 info->protect[i] ? " (RO)" : " ");
177 * flash_erase: - erase flash sectors
181 int flash_erase(flash_info_t *info, int s_first, int s_last)
183 int flag, prot, sect;
186 if (info->flash_id == FLASH_UNKNOWN)
187 return ERR_UNKNOWN_FLASH_TYPE;
189 if ((s_first < 0) || (s_first > s_last)) {
193 if ((info->flash_id & FLASH_VENDMASK) != (INTEL_MANUFACT & FLASH_VENDMASK))
194 return ERR_UNKNOWN_FLASH_VENDOR;
197 for (sect=s_first; sect<=s_last; ++sect) {
198 if (info->protect[sect]) prot++;
201 if (prot) return ERR_PROTECTED;
204 * Disable interrupts which might cause a timeout
205 * here. Remember that our exception vectors are
206 * at address 0 in the flash, and we don't want a
207 * (ticker) exception to happen while the flash
208 * chip is in programming mode.
211 flag = disable_interrupts();
213 /* Start erase on unprotected sectors */
214 for (sect = s_first; sect<=s_last && !ctrlc(); sect++) {
216 printf("Erasing sector %2d ... ", sect);
220 /* arm simple, non interrupt dependent timer */
221 reset_timer_masked();
223 if (info->protect[sect] == 0) { /* not protected */
224 u16 * volatile addr = (u16 * volatile)(info->start[sect]);
226 PRINTK("unlocking sector\n");
231 PRINTK("erasing sector\n");
233 PRINTK("confirming erase\n");
236 while ((*addr & 0x0080) != 0x0080) {
238 if (get_timer_masked() > CONFIG_SYS_FLASH_ERASE_TOUT) {
239 *addr = 0x00B0; /* suspend erase*/
240 *addr = 0x00FF; /* read mode */
246 PRINTK("clearing status register\n");
248 PRINTK("resetting to read mode");
255 if (ctrlc()) printf("User Interrupt!\n");
259 /* allow flash to settle - wait 10 ms */
260 udelay_masked(10000);
262 if (flag) enable_interrupts();
269 * write_word: - copy memory to flash
277 static int write_word (flash_info_t *info, ulong dest, ushort data)
279 volatile u16 *addr = (u16 *)dest, val;
283 /* Check if Flash is (sufficiently) erased */
284 if ((*addr & data) != data) return ERR_NOT_ERASED;
287 * Disable interrupts which might cause a timeout
288 * here. Remember that our exception vectors are
289 * at address 0 in the flash, and we don't want a
290 * (ticker) exception to happen while the flash
291 * chip is in programming mode.
293 flag = disable_interrupts();
295 /* clear status register command */
298 /* program set-up command */
301 /* latch address/data */
304 /* arm simple, non interrupt dependent timer */
305 reset_timer_masked();
307 /* wait while polling the status register */
308 while(((val = *addr) & 0x80) != 0x80) {
309 if (get_timer_masked() > CONFIG_SYS_FLASH_WRITE_TOUT) {
311 *addr = 0xB0; /* suspend program command */
316 if(val & 0x1A) { /* check for error */
317 printf("\nFlash write error %02x at address %08lx\n",
318 (int)val, (unsigned long)dest);
320 printf("Voltage range error.\n");
325 printf("Device protect error.\n");
330 printf("Programming error.\n");
340 *addr = 0xFF; /* read array command */
341 if (flag) enable_interrupts();
348 * write_buf: - Copy memory to flash.
351 * @param src: source of copy transaction
352 * @param addr: where to copy to
353 * @param cnt: number of bytes to copy
358 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
365 wp = (addr & ~1); /* get lower word aligned address */
368 * handle unaligned start bytes
370 if ((l = addr - wp) != 0) {
372 for (i=0, cp=wp; i<l; ++i, ++cp) {
373 data = (data >> 8) | (*(uchar *)cp << 8);
375 for (; i<2 && cnt>0; ++i) {
376 data = (data >> 8) | (*src++ << 8);
380 for (; cnt==0 && i<2; ++i, ++cp) {
381 data = (data >> 8) | (*(uchar *)cp << 8);
384 if ((rc = write_word(info, wp, data)) != 0) {
391 * handle word aligned part
394 /* data = *((vushort*)src); */
395 data = *((ushort*)src);
396 if ((rc = write_word(info, wp, data)) != 0) {
404 if (cnt == 0) return ERR_OK;
407 * handle unaligned tail bytes
410 for (i=0, cp=wp; i<2 && cnt>0; ++i, ++cp) {
411 data = (data >> 8) | (*src++ << 8);
414 for (; i<2; ++i, ++cp) {
415 data = (data >> 8) | (*(uchar *)cp << 8);
418 return write_word(info, wp, data);