]>
git.ipfire.org Git - people/ms/u-boot.git/blob - board/nx823/flash.c
3 * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
6 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
8 * See file CREDITS for list of people who contributed to this
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
30 flash_info_t flash_info
[CFG_MAX_FLASH_BANKS
]; /* info for FLASH chips */
31 extern u_long
*my_sernum
; /* from nx823.c */
33 /*-----------------------------------------------------------------------
36 #define FLAG_PROTECT_SET 0x01
37 #define FLAG_PROTECT_CLEAR 0x02
39 /* Board support for 1 or 2 flash devices */
40 #undef FLASH_PORT_WIDTH32
41 #define FLASH_PORT_WIDTH16
43 #ifdef FLASH_PORT_WIDTH16
44 #define FLASH_PORT_WIDTH ushort
45 #define FLASH_PORT_WIDTHV vu_short
47 #define FLASH_PORT_WIDTH ulong
48 #define FLASH_PORT_WIDTHV vu_long
51 #define FPW FLASH_PORT_WIDTH
52 #define FPWV FLASH_PORT_WIDTHV
54 /*-----------------------------------------------------------------------
57 static ulong
flash_get_size (FPW
*addr
, flash_info_t
*info
);
58 static int write_data (flash_info_t
*info
, ulong dest
, FPW data
);
59 static void flash_get_offsets (ulong base
, flash_info_t
*info
);
61 /*-----------------------------------------------------------------------
64 unsigned long flash_init (void)
66 volatile immap_t
*immap
= (immap_t
*)CFG_IMMR
;
67 volatile memctl8xx_t
*memctl
= &immap
->im_memctl
;
68 unsigned long size_b0
;
71 /* Init: no FLASHes known */
72 for (i
=0; i
<CFG_MAX_FLASH_BANKS
; ++i
) {
73 flash_info
[i
].flash_id
= FLASH_UNKNOWN
;
76 /* Static FLASH Bank configuration here - FIXME XXX */
77 size_b0
= flash_get_size((FPW
*)FLASH_BASE0_PRELIM
, &flash_info
[0]);
79 if (flash_info
[0].flash_id
== FLASH_UNKNOWN
) {
80 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
81 size_b0
, size_b0
<<20);
84 /* Remap FLASH according to real size */
85 memctl
->memc_or0
= CFG_OR_TIMING_FLASH
| (-size_b0
& 0xFFFF8000);
86 memctl
->memc_br0
= (CFG_FLASH_BASE
& BR_BA_MSK
) | BR_PS_16
| BR_MS_GPCM
| BR_V
;
88 /* Re-do sizing to get full correct info */
89 size_b0
= flash_get_size((FPW
*)CFG_FLASH_BASE
, &flash_info
[0]);
91 flash_get_offsets (CFG_FLASH_BASE
, &flash_info
[0]);
93 /* monitor protection ON by default */
94 (void)flash_protect(FLAG_PROTECT_SET
,
96 CFG_FLASH_BASE
+CFG_MONITOR_LEN
-1,
99 flash_info
[0].size
= size_b0
;
104 /*-----------------------------------------------------------------------
106 static void flash_get_offsets (ulong base
, flash_info_t
*info
)
110 if (info
->flash_id
== FLASH_UNKNOWN
) {
114 if ((info
->flash_id
& FLASH_VENDMASK
) == FLASH_MAN_INTEL
) {
115 for (i
= 0; i
< info
->sector_count
; i
++) {
116 info
->start
[i
] = base
+ (i
* 0x00020000);
121 /*-----------------------------------------------------------------------
123 void flash_print_info (flash_info_t
*info
)
127 if (info
->flash_id
== FLASH_UNKNOWN
) {
128 printf ("missing or unknown FLASH type\n");
132 switch (info
->flash_id
& FLASH_VENDMASK
) {
133 case FLASH_MAN_INTEL
: printf ("INTEL "); break;
134 default: printf ("Unknown Vendor "); break;
137 switch (info
->flash_id
& FLASH_TYPEMASK
) {
138 case FLASH_28F320J3A
:
139 printf ("28F320J3A\n"); break;
140 case FLASH_28F640J3A
:
141 printf ("28F640J3A\n"); break;
142 case FLASH_28F128J3A
:
143 printf ("28F128J3A\n"); break;
144 default: printf ("Unknown Chip Type\n"); break;
147 printf (" Size: %ld MB in %d Sectors\n",
148 info
->size
>> 20, info
->sector_count
);
150 printf (" Sector Start Addresses:");
151 for (i
=0; i
<info
->sector_count
; ++i
) {
156 info
->protect
[i
] ? " (RO)" : " "
163 /*-----------------------------------------------------------------------
167 /*-----------------------------------------------------------------------
171 * The following code cannot be run from FLASH!
174 static ulong
flash_get_size (FPW
*addr
, flash_info_t
*info
)
178 /* Write auto select command: read Manufacturer ID */
179 addr
[0x5555] = (FPW
)0x00AA00AA;
180 addr
[0x2AAA] = (FPW
)0x00550055;
181 addr
[0x5555] = (FPW
)0x00900090;
186 case (FPW
)INTEL_MANUFACT
:
187 info
->flash_id
= FLASH_MAN_INTEL
;
190 info
->flash_id
= FLASH_UNKNOWN
;
191 info
->sector_count
= 0;
193 addr
[0] = (FPW
)0x00FF00FF; /* restore read mode */
194 return (0); /* no or unknown flash */
197 value
= addr
[1]; /* device ID */
200 case (FPW
)INTEL_ID_28F320J3A
:
201 info
->flash_id
+= FLASH_28F320J3A
;
202 info
->sector_count
= 32;
203 info
->size
= 0x00400000;
206 case (FPW
)INTEL_ID_28F640J3A
:
207 info
->flash_id
+= FLASH_28F640J3A
;
208 info
->sector_count
= 64;
209 info
->size
= 0x00800000;
212 case (FPW
)INTEL_ID_28F128J3A
:
213 info
->flash_id
+= FLASH_28F128J3A
;
214 info
->sector_count
= 128;
215 info
->size
= 0x01000000;
216 break; /* => 16 MB */
219 info
->flash_id
= FLASH_UNKNOWN
;
223 if (info
->sector_count
> CFG_MAX_FLASH_SECT
) {
224 printf ("** ERROR: sector count %d > max (%d) **\n",
225 info
->sector_count
, CFG_MAX_FLASH_SECT
);
226 info
->sector_count
= CFG_MAX_FLASH_SECT
;
229 addr
[0] = (FPW
)0x00FF00FF; /* restore read mode */
235 /*-----------------------------------------------------------------------
238 int flash_erase (flash_info_t
*info
, int s_first
, int s_last
)
240 int flag
, prot
, sect
;
241 ulong type
, start
, now
, last
;
244 if ((s_first
< 0) || (s_first
> s_last
)) {
245 if (info
->flash_id
== FLASH_UNKNOWN
) {
246 printf ("- missing\n");
248 printf ("- no sectors to erase\n");
253 type
= (info
->flash_id
& FLASH_VENDMASK
);
254 if ((type
!= FLASH_MAN_INTEL
)) {
255 printf ("Can't erase unknown flash type %08lx - aborted\n",
261 for (sect
=s_first
; sect
<=s_last
; ++sect
) {
262 if (info
->protect
[sect
]) {
268 printf ("- Warning: %d protected sectors will not be erased!\n",
274 start
= get_timer (0);
276 /* Start erase on unprotected sectors */
277 for (sect
= s_first
; sect
<=s_last
; sect
++) {
278 if (info
->protect
[sect
] == 0) { /* not protected */
279 FPWV
*addr
= (FPWV
*)(info
->start
[sect
]);
282 /* Disable interrupts which might cause a timeout here */
283 flag
= disable_interrupts();
285 *addr
= (FPW
)0x00500050; /* clear status register */
286 *addr
= (FPW
)0x00200020; /* erase setup */
287 *addr
= (FPW
)0x00D000D0; /* erase confirm */
289 /* re-enable interrupts if necessary */
293 /* wait at least 80us - let's wait 1 ms */
296 while (((status
= *addr
) & (FPW
)0x00800080) != (FPW
)0x00800080) {
297 if ((now
=get_timer(start
)) > CFG_FLASH_ERASE_TOUT
) {
298 printf ("Timeout\n");
299 *addr
= (FPW
)0x00B000B0; /* suspend erase */
300 *addr
= (FPW
)0x00FF00FF; /* reset to read mode */
305 /* show that we're waiting */
306 if ((now
- last
) > 1000) { /* every second */
312 *addr
= (FPW
)0x00FF00FF; /* reset to read mode */
319 /*-----------------------------------------------------------------------
320 * Copy memory to flash, returns:
323 * 2 - Flash not erased
324 * 4 - Flash not identified
327 int write_buff (flash_info_t
*info
, uchar
*src
, ulong addr
, ulong cnt
)
331 int count
, i
, l
, rc
, port_width
;
333 if (info
->flash_id
== FLASH_UNKNOWN
) {
336 /* get lower word aligned address */
337 #ifdef FLASH_PORT_WIDTH16
345 /* save sernum if needed */
346 if (addr
>= CFG_FLASH_SN_SECTOR
&& addr
< CFG_FLASH_SN_BASE
)
348 u_long dest
= CFG_FLASH_SN_BASE
;
349 u_short
*sn
= (u_short
*)my_sernum
;
351 printf("(saving sernum)");
354 if ((rc
= write_data(info
, dest
, sn
[i
])) != 0) {
362 * handle unaligned start bytes
364 if ((l
= addr
- wp
) != 0) {
366 for (i
=0, cp
=wp
; i
<l
; ++i
, ++cp
) {
367 data
= (data
<< 8) | (*(uchar
*)cp
);
369 for (; i
<port_width
&& cnt
>0; ++i
) {
370 data
= (data
<< 8) | *src
++;
374 for (; cnt
==0 && i
<port_width
; ++i
, ++cp
) {
375 data
= (data
<< 8) | (*(uchar
*)cp
);
378 if ((rc
= write_data(info
, wp
, data
)) != 0) {
385 * handle word aligned part
388 while (cnt
>= port_width
) {
390 for (i
=0; i
<port_width
; ++i
) {
391 data
= (data
<< 8) | *src
++;
393 if ((rc
= write_data(info
, wp
, data
)) != 0) {
410 * handle unaligned tail bytes
413 for (i
=0, cp
=wp
; i
<port_width
&& cnt
>0; ++i
, ++cp
) {
414 data
= (data
<< 8) | *src
++;
417 for (; i
<port_width
; ++i
, ++cp
) {
418 data
= (data
<< 8) | (*(uchar
*)cp
);
421 return (write_data(info
, wp
, data
));
424 /*-----------------------------------------------------------------------
425 * Write a word or halfword to Flash, returns:
428 * 2 - Flash not erased
430 static int write_data (flash_info_t
*info
, ulong dest
, FPW data
)
432 FPWV
*addr
= (FPWV
*)dest
;
437 /* Check if Flash is (sufficiently) erased */
438 if ((*addr
& data
) != data
) {
439 printf("not erased at %08lx (%x)\n",(ulong
)addr
,*addr
);
442 /* Disable interrupts which might cause a timeout here */
443 flag
= disable_interrupts();
445 *addr
= (FPW
)0x00400040; /* write setup */
448 /* re-enable interrupts if necessary */
452 start
= get_timer (0);
454 while (((status
= *addr
) & (FPW
)0x00800080) != (FPW
)0x00800080) {
455 if (get_timer(start
) > CFG_FLASH_WRITE_TOUT
) {
456 *addr
= (FPW
)0x00FF00FF; /* restore read mode */
461 *addr
= (FPW
)0x00FF00FF; /* restore read mode */