3 * MAZeT GmbH <www.mazet.de>
4 * Stephan Linz <linz@mazet.de>, <linz@li-pro.net>
6 * The most stuff comes from PPCBoot and Linux.
8 * IMMS gGmbH <www.imms.de>
9 * Thomas Elste <info@elste.org>
11 * Modifications for ModNET50 Board
13 * See file CREDITS for list of people who contributed to this
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License as
18 * published by the Free Software Foundation; either version 2 of
19 * the License, or (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
33 #include <asm/arch/netarm_registers.h>
35 #define SCR (*(volatile unsigned int *)(NETARM_GEN_MODULE_BASE + NETARM_GEN_SYSTEM_CONTROL))
37 #define ALIGN_ABORT_OFF SCR = SCR & ~NETARM_GEN_SYS_CFG_ALIGN_ABORT
38 #define ALIGN_ABORT_ON SCR = SCR | NETARM_GEN_SYS_CFG_ALIGN_ABORT
40 #define PROG_ADDR (0x555*2)
41 #define SETUP_ADDR (0x555*2)
42 #define ID_ADDR (0x555*2)
43 #define UNLOCK_ADDR1 (0x555*2)
44 #define UNLOCK_ADDR2 (0x2AA*2)
46 #define UNLOCK_CMD1 (0xAA)
47 #define UNLOCK_CMD2 (0x55)
48 #define ERASE_SUSPEND_CMD (0xB0)
49 #define ERASE_RESUME_CMD (0x30)
50 #define RESET_CMD (0xF0)
52 #define SECERASE_CMD (0x30)
53 #define CHIPERASE_CMD (0x10)
54 #define PROG_CMD (0xa0)
55 #define SETUP_CMD (0x80)
62 #define WRITE_UNLOCK(addr) { \
63 *(volatile __u16*)(addr + UNLOCK_ADDR1) = (__u16)UNLOCK_CMD1; \
64 *(volatile __u16*)(addr + UNLOCK_ADDR2) = (__u16)UNLOCK_CMD2; \
67 #define CONFIG_AM29_RESERVED (0)
71 #define CELL_SIZE (64*K)
72 #define DEVICE_SIZE (MB*K*K)
73 #define CELLS_PER_DEVICE (DEVICE_SIZE/CELL_SIZE)
74 #define RESERVED_CELLS (CONFIG_AM29_RESERVED*K)/CELL_SIZE
75 #define MAX_FLASH_DEVICES (1)
76 #define AVAIL_SIZE (DEVICE_SIZE*MAX_FLASH_DEVICES - RESERVED_CELLS*CELL_SIZE)
79 flash_info_t flash_info
[CFG_MAX_FLASH_BANKS
];
80 static __u16 toggling_bits
;
83 /*-----------------------------------------------------------------------
85 ulong
flash_get_size (ulong baseaddr
, flash_info_t
* info
)
90 /* Write auto select command sequence and test FLASH answer */
91 WRITE_UNLOCK (baseaddr
);
92 *(volatile __u16
*) (baseaddr
+ ID_ADDR
) = (__u16
) ID_CMD
;
93 flashtest
/* manufacturer ID */ = *(volatile __u16
*) (baseaddr
);
94 *(volatile __u16
*) (baseaddr
+ ID_ADDR
) = (__u16
) RESET_CMD
;
96 switch ((__u32
) ((flashtest
<< 16) + flashtest
)) {
98 info
->flash_id
= FLASH_MAN_AMD
& FLASH_VENDMASK
;
101 info
->flash_id
= FLASH_MAN_FUJ
& FLASH_VENDMASK
;
104 info
->flash_id
= FLASH_UNKNOWN
;
105 info
->sector_count
= 0;
107 return (0); /* no or unknown flash */
110 /* Write auto select command sequence and test FLASH answer */
111 WRITE_UNLOCK (baseaddr
);
112 *(volatile __u16
*) (baseaddr
+ ID_ADDR
) = (__u16
) ID_CMD
;
113 flashtest
/* device ID */ = *(volatile __u16
*) (baseaddr
+ 2);
114 *(volatile __u16
*) (baseaddr
+ ID_ADDR
) = (__u16
) RESET_CMD
;
116 /* toggling_bits = (flashtest == TOSHIBA)?(DQ6):(DQ2|DQ6); */
117 toggling_bits
= (DQ2
| DQ6
);
119 switch ((__u32
) ((flashtest
<< 16) + flashtest
)) {
122 (FLASH_AM160LV
| FLASH_AM160B
) & FLASH_TYPEMASK
;
123 info
->sector_count
= CFG_MAX_FLASH_SECT
;
124 info
->size
= CFG_FLASH_SIZE
;
127 1*32K Small Main Block */
128 info
->start
[0] = baseaddr
;
129 info
->start
[1] = baseaddr
+ 0x4000;
130 info
->start
[2] = baseaddr
+ 0x6000;
131 info
->start
[3] = baseaddr
+ 0x8000;
132 for (i
= 1; i
< info
->sector_count
; i
++)
133 info
->start
[3 + i
] = baseaddr
+ i
* CFG_MAIN_SECT_SIZE
;
136 info
->flash_id
= FLASH_UNKNOWN
;
137 return (0); /* no or unknown flash */
140 for (i
= 0; i
< info
->sector_count
; i
++) {
141 /* Write auto select command sequence and test FLASH answer */
142 WRITE_UNLOCK (info
->start
[i
]);
143 *(volatile __u16
*) (info
->start
[i
] + ID_ADDR
) = (__u16
) ID_CMD
;
144 flashtest
/* protected verify */ = *(volatile __u16
*) (info
->start
[i
] + 4);
145 *(volatile __u16
*) (info
->start
[i
] + ID_ADDR
) = (__u16
) RESET_CMD
;
146 if (flashtest
& 0x0001) {
147 info
->protect
[i
] = 1; /* D0 = 1 if protected */
149 info
->protect
[i
] = 0;
155 /*-----------------------------------------------------------------------
157 ulong
flash_init (void)
162 /* Init: no FLASHes known */
163 for (i
= 0; i
< CFG_MAX_FLASH_BANKS
; ++i
) {
164 flash_info
[i
].flash_id
= FLASH_UNKNOWN
;
167 /* Static FLASH Bank configuration here (only one bank) */
168 size
= flash_get_size (CFG_FLASH_BASE
, &flash_info
[0]);
170 if (flash_info
[0].flash_id
== FLASH_UNKNOWN
|| size
== 0) {
171 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
176 * protect monitor and environment sectors
178 flash_protect (FLAG_PROTECT_SET
,
180 CFG_FLASH_BASE
+ monitor_flash_len
- 1,
183 flash_protect (FLAG_PROTECT_SET
,
185 CFG_ENV_ADDR
+ CFG_ENV_SIZE
- 1, &flash_info
[0]);
190 /*-----------------------------------------------------------------------
192 void flash_print_info (flash_info_t
* info
)
196 if (info
->flash_id
== FLASH_UNKNOWN
) {
197 printf ("missing or unknown FLASH type\n");
201 switch (info
->flash_id
& FLASH_VENDMASK
) {
209 printf ("Unknown Vendor ");
213 switch (info
->flash_id
& FLASH_TYPEMASK
) {
215 printf ("29DL323B (32 M, bottom sector)\n");
217 case (FLASH_AM160LV
| FLASH_AM160B
):
218 printf ("29LV160BE (1M x 16, bottom sector)\n");
221 printf ("Unknown Chip Type\n");
225 printf (" Size: %ld MB in %d Sectors\n",
226 info
->size
>> 20, info
->sector_count
);
227 printf (" Sector Start Addresses:");
228 for (i
= 0; i
< info
->sector_count
; i
++) {
231 printf (" S%02d @ 0x%08lX%s", i
,
232 info
->start
[i
], info
->protect
[i
] ? " !" : " ");
238 /*-----------------------------------------------------------------------
240 int flash_check_protection (flash_info_t
* info
, int s_first
, int s_last
)
244 for (sect
= s_first
; sect
<= s_last
; sect
++)
245 if (info
->protect
[sect
])
248 printf ("- can't erase %d protected sectors\n", prot
);
252 /*-----------------------------------------------------------------------
254 int flash_check_erase_amd (ulong start
)
258 v1
= *(volatile __u16
*) (start
);
259 v2
= *(volatile __u16
*) (start
);
261 if (((v1
^ v2
) & toggling_bits
) == toggling_bits
) {
262 if (((v1
| v2
) & DQ5
) == DQ5
) {
264 /* OOPS: exceeded timing limits */
266 v1
= *(volatile __u16
*) (start
);
267 v2
= *(volatile __u16
*) (start
);
269 if (((v1
^ v2
) & toggling_bits
) == toggling_bits
) {
272 ((toggling_bits
& (DQ2
| DQ6
)) ==
273 (DQ2
| DQ6
)) ? "DQ2,DQ6" : "DQ6");
275 /* OOPS: there is an erasure in progress,
276 * try to reset chip */
277 *(volatile __u16
*) (start
) =
280 return 1; /* still busy */
283 return 1; /* still busy */
285 return 0; /* be free */
288 /*-----------------------------------------------------------------------
290 int flash_erase (flash_info_t
* info
, int s_first
, int s_last
)
292 int flag
, sect
, setup_offset
= 0;
295 if (info
->flash_id
== FLASH_UNKNOWN
) {
296 printf ("- missing\n");
297 return ERR_UNKNOWN_FLASH_TYPE
;
300 if ((s_first
< 0) || (s_first
> s_last
)) {
301 printf ("- no sectors to erase\n");
305 if (flash_check_protection (info
, s_first
, s_last
))
306 return ERR_PROTECTED
;
308 switch (info
->flash_id
& FLASH_VENDMASK
) {
311 switch (info
->flash_id
& FLASH_TYPEMASK
) {
312 case (FLASH_AM160LV
| FLASH_AM160B
):
313 setup_offset
= UNLOCK_ADDR1
; /* just the adress for setup_cmd differs */
316 * Disable interrupts which might cause a timeout
317 * here. Remember that our exception vectors are
318 * at address 0 in the flash, and we don't want a
319 * (ticker) exception to happen while the flash
320 * chip is in programming mode.
322 flag
= disable_interrupts ();
323 /* Start erase on unprotected sectors */
324 for (sect
= s_first
; sect
<= s_last
&& !ctrlc ();
326 printf ("Erasing sector %2d ... ", sect
);
328 if (info
->protect
[sect
] == 0) {
330 /* Write sector erase command sequence */
331 WRITE_UNLOCK (info
->start
[0]);
332 *(volatile __u16
*) (info
->start
[0] +
335 WRITE_UNLOCK (info
->start
[0]);
336 *(volatile __u16
*) (info
->
338 (__u16
) SECERASE_CMD
;
341 reset_timer_masked ();
342 while (get_timer_masked () < 1000) {
345 /* arm simple, non interrupt dependent timer */
346 reset_timer_masked ();
347 while (flash_check_erase_amd (info
->start
[sect
])) {
348 if (get_timer_masked () > CFG_FLASH_ERASE_TOUT
) {
349 printf ("timeout!\n");
350 /* OOPS: reach timeout,
353 *(volatile __u16
*) (info
-> start
[sect
]) = (__u16
) RESET_CMD
;
360 printf ("protected!\n");
364 printf ("User Interrupt!\n");
366 /* allow flash to settle - wait 10 ms */
367 udelay_masked (10000);
369 enable_interrupts ();
372 printf ("- unknown chip type\n");
373 return ERR_UNKNOWN_FLASH_TYPE
;
377 printf ("- unknown vendor ");
378 return ERR_UNKNOWN_FLASH_VENDOR
;
382 /*-----------------------------------------------------------------------
384 int flash_check_write_amd (ulong dest
)
388 v1
= *(volatile __u16
*) (dest
);
389 v2
= *(volatile __u16
*) (dest
);
391 /* DQ6 toggles during write */
392 if (((v1
^ v2
) & DQ6
) == DQ6
) {
393 if (((v1
| v2
) & DQ5
) == DQ5
) {
394 printf ("[DQ5] @ %08lX\n", dest
);
396 /* OOPS: exceeded timing limits,
397 * try to reset chip */
398 *(volatile __u16
*) (dest
) = (__u16
) RESET_CMD
;
399 return 0; /* be free */
401 return 1; /* still busy */
404 return 0; /* be free */
407 /*-----------------------------------------------------------------------
408 * Copy memory to flash
410 static int write_word (flash_info_t
* info
, ulong dest
, ushort data
)
415 /* Check if Flash is (sufficiently) erased */
416 if ((*(__u16
*) (dest
) & data
) != data
)
417 return ERR_NOT_ERASED
;
420 * Disable interrupts which might cause a timeout
421 * here. Remember that our exception vectors are
422 * at address 0 in the flash, and we don't want a
423 * (ticker) exception to happen while the flash
424 * chip is in programming mode.
426 flag
= disable_interrupts ();
428 /* Write program command sequence */
429 WRITE_UNLOCK (info
->start
[0]);
431 /* Flash dependend program seqence */
432 switch (info
->flash_id
& FLASH_VENDMASK
) {
435 switch (info
->flash_id
& FLASH_TYPEMASK
) {
436 case (FLASH_AM160LV
| FLASH_AM160B
):
437 *(volatile __u16
*) (info
->start
[0] + UNLOCK_ADDR1
) =
439 *(volatile __u16
*) (dest
) = (__u16
) data
;
442 *(volatile __u16
*) (dest
) = (__u16
) PROG_CMD
;
443 *(volatile __u16
*) (dest
) = (__u16
) data
;
448 /* arm simple, non interrupt dependent timer */
449 reset_timer_masked ();
451 while (flash_check_write_amd (dest
)) {
452 if (get_timer_masked () > CFG_FLASH_WRITE_TOUT
) {
453 printf ("timeout! @ %08lX\n", dest
);
454 /* OOPS: reach timeout,
455 * try to reset chip */
456 *(volatile __u16
*) (dest
) = (__u16
) RESET_CMD
;
463 /* Check if Flash was (accurately) written */
464 if (*(__u16
*) (dest
) != data
)
469 enable_interrupts ();
473 /*-----------------------------------------------------------------------
474 * Copy memory to flash.
476 int write_buff (flash_info_t
* info
, uchar
* src
, ulong addr
, ulong cnt
)
483 wp
= (addr
& ~1); /* get lower word aligned address */
486 * handle unaligned start bytes
488 if ((l
= addr
- wp
) != 0) {
490 for (i
= 0, cp
= wp
; i
< l
; ++i
, ++cp
) {
491 data
= (data
>> 8) | (*(uchar
*) cp
<< 8);
493 for (; i
< 2 && cnt
> 0; ++i
) {
494 data
= (data
>> 8) | (*src
++ << 8);
498 for (; cnt
== 0 && i
< 2; ++i
, ++cp
) {
499 data
= (data
>> 8) | (*(uchar
*) cp
<< 8);
502 if ((rc
= write_word (info
, wp
, data
)) != 0) {
509 * handle word aligned part
512 data
= *((ushort
*) src
);
513 if ((rc
= write_word (info
, wp
, data
)) != 0)
524 * handle unaligned tail bytes
527 for (i
= 0, cp
= wp
; i
< 2 && cnt
> 0; ++i
, ++cp
) {
528 data
= (data
>> 8) | (*src
++ << 8);
531 for (; i
< 2; ++i
, ++cp
) {
532 data
= (data
>> 8) | (*(uchar
*) cp
<< 8);
535 return write_word (info
, wp
, data
);