3 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
4 * Marius Groeger <mgroeger@sysgo.de>
6 * (C) Copyright 2005 Rowel Atienza <rowel@diwalabs.com>
7 * Flash driver for armadillo board HT1070
9 * See file CREDITS for list of people who contributed to this
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of
15 * the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
30 #define FLASH_BANK_SIZE 0x400000
32 /*value used by hermit is 0x200*/
33 /*document says sector size is either 64k in low mem reg and 8k in high mem reg*/
34 #define MAIN_SECT_SIZE 0x10000
36 #define UNALIGNED_MASK (3)
37 #define FL_WORD(addr) (*(volatile unsigned short*)(addr))
38 #define FLASH_TIMEOUT 20000000
40 flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
42 /*-----------------------------------------------------------------------
45 ulong flash_init (void)
50 for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) {
53 flash_info[i].flash_id = (FUJ_MANUFACT & FLASH_VENDMASK);
54 /*(INTEL_ID_28F128J3 & FLASH_TYPEMASK); */
55 flash_info[i].size = FLASH_BANK_SIZE;
56 flash_info[i].sector_count = CFG_MAX_FLASH_SECT;
57 memset (flash_info[i].protect, 0, CFG_MAX_FLASH_SECT);
59 flashbase = PHYS_FLASH_1;
61 panic ("configured too many flash banks!\n");
62 for (j = 0; j < flash_info[i].sector_count; j++) {
63 flash_info[i].start[j] =
64 flashbase + j * MAIN_SECT_SIZE;
66 size += flash_info[i].size;
69 /* Protect monitor and environment sectors
71 flash_protect (FLAG_PROTECT_SET,
73 CFG_FLASH_BASE + monitor_flash_len - 1,
76 flash_protect (FLAG_PROTECT_SET,
78 CFG_ENV_ADDR + CFG_ENV_SIZE - 1, &flash_info[0]);
83 /*-----------------------------------------------------------------------
85 void flash_print_info (flash_info_t * info)
89 switch (info->flash_id & FLASH_VENDMASK) {
90 case (FUJ_MANUFACT & FLASH_VENDMASK):
94 printf ("Unknown Vendor ");
98 switch (info->flash_id & FLASH_TYPEMASK) {
99 case (INTEL_ID_28F128J3 & FLASH_TYPEMASK):
100 printf ("28F128J3 (128Mbit)\n");
103 printf ("Unknown Chip Type\n");
108 printf (" Size: %ld MB in %d Sectors\n",
109 info->size >> 20, info->sector_count);
111 printf (" Sector Start Addresses:");
112 for (i = 0; i < info->sector_count; i++) {
116 printf (" %08lX%s", info->start[i],
117 info->protect[i] ? " (RO)" : " ");
125 * * Loop until both write state machines complete.
127 static unsigned short flash_status_wait (unsigned long addr,
128 unsigned short value)
130 unsigned short status;
131 long timeout = FLASH_TIMEOUT;
133 while (((status = (FL_WORD (addr))) != value) && timeout > 0) {
140 * Loop until the Write State machine is ready, then do a full error
141 * check. Clear status and leave the flash in Read Array mode; return
142 * 0 for no error, -1 for error.
144 static int flash_status_full_check (unsigned long addr, unsigned short value1,
145 unsigned short value2)
147 unsigned short status1, status2;
149 status1 = flash_status_wait (addr, value1);
150 status2 = flash_status_wait (addr + 2, value2);
151 return (status1 != value1 || status2 != value2) ? -1 : 0;
154 /*-----------------------------------------------------------------------
157 int flash_erase (flash_info_t * info, int s_first, int s_last)
159 int flag, prot, sect;
164 if ((info->flash_id & FLASH_VENDMASK) !=
165 (FUJ_MANUFACT & FLASH_VENDMASK)) {
166 return ERR_UNKNOWN_FLASH_VENDOR;
170 for (sect = s_first; sect <= s_last; ++sect) {
171 if (info->protect[sect]) {
176 return ERR_PROTECTED;
179 * Disable interrupts which might cause a timeout
180 * here. Remember that our exception vectors are
181 * at address 0 in the flash, and we don't want a
182 * (ticker) exception to happen while the flash
183 * chip is in programming mode.
185 flag = disable_interrupts ();
187 printf ("Erasing %d sectors starting at sector %2d.\n"
188 "This make take some time ... ",
189 s_last - s_first, sect);
190 /* Start erase on unprotected sectors */
191 for (sect = s_first; sect <= s_last && !ctrlc (); sect++) {
192 /* ARM simple, non interrupt dependent timer */
193 reset_timer_masked ();
195 if (info->protect[sect] == 0) { /* not protected */
197 addr = sect * MAIN_SECT_SIZE;
198 addr &= ~(unsigned long) UNALIGNED_MASK; /* word align */
199 base = addr & 0xF0000000;
201 FL_WORD (base + (0x555 << 1)) = 0xAA;
202 FL_WORD (base + (0x2AA << 1)) = 0x55;
203 FL_WORD (base + (0x555 << 1)) = 0x80;
204 FL_WORD (base + (0x555 << 1)) = 0xAA;
205 FL_WORD (base + (0x2AA << 1)) = 0x55;
206 FL_WORD (addr) = 0x30;
207 if (flash_status_full_check (addr, 0xFFFF, 0xFFFF))
208 return ERR_PROTECTED;
211 printf ("\nDone.\n");
213 printf ("User Interrupt!\n");
215 /* allow flash to settle - wait 10 ms */
216 udelay_masked (10000);
219 enable_interrupts ();
225 /*-----------------------------------------------------------------------
226 * Copy memory to flash
229 static int write_word (flash_info_t * info, ulong dest, ushort data)
234 /* Check if Flash is (sufficiently) erased
236 if ((FL_WORD (dest) & data) != data)
237 return ERR_NOT_ERASED;
239 /*if(dest & UNALIGNED_MASK) return ERR_ALIGN; */
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 /* arm simple, non interrupt dependent timer */
251 reset_timer_masked ();
253 base = dest & 0xF0000000;
254 FL_WORD (base + (0x555 << 1)) = 0xAA;
255 FL_WORD (base + (0x2AA << 1)) = 0x55;
256 FL_WORD (base + (0x555 << 1)) = 0xA0;
257 FL_WORD (dest) = data;
258 /*printf("writing 0x%p = 0x%x\n",dest,data); */
259 if (flash_status_wait (dest, data) != data)
260 return ERR_PROG_ERROR;
263 enable_interrupts ();
268 /*-----------------------------------------------------------------------
269 * Copy memory to flash.
272 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
279 wp = (addr & ~1); /* get lower word aligned address */
280 printf ("Writing %d short data to 0x%p from 0x%p.\n ", cnt, wp, src);
283 * handle unaligned start bytes
285 if ((l = addr - wp) != 0) {
287 for (i = 0, cp = wp; i < l; ++i, ++cp) {
288 data = (data >> 8) | (*(uchar *) cp << 8);
290 for (; i < 2 && cnt > 0; ++i) {
291 data = (data >> 8) | (*src++ << 8);
295 for (; cnt == 0 && i < 2; ++i, ++cp) {
296 data = (data >> 8) | (*(uchar *) cp << 8);
299 if ((rc = write_word (info, wp, data)) != 0) {
306 * handle word aligned part
309 data = *((vu_short *) src);
310 if ((rc = write_word (info, wp, data)) != 0) {
319 printf ("\nDone.\n");
324 * handle unaligned tail bytes
327 for (i = 0, cp = wp; i < 2 && cnt > 0; ++i, ++cp) {
328 data = (data >> 8) | (*src++ << 8);
331 for (; i < 2; ++i, ++cp) {
332 data = (data >> 8) | (*(uchar *) cp << 8);
335 return write_word (info, wp, data);