3 * Stäubli Faverges - <www.staubli.com>
4 * Pierre AUBERT p.aubert@staubli.com
5 * U-Boot port on RPXClassic LF (CLLF_BW31) board
7 * RPXClassic uses Am29DL323B flash memory with 2 banks
11 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
13 * SPDX-License-Identifier: GPL-2.0+
20 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
22 /*-----------------------------------------------------------------------
25 static ulong flash_get_size (vu_long *addr, flash_info_t *info);
26 static int write_word (flash_info_t *info, ulong dest, ulong data);
27 static void flash_get_offsets (ulong base, flash_info_t *info);
29 /*-----------------------------------------------------------------------
32 unsigned long flash_init (void)
34 unsigned long size_b0 ;
37 /* Init: no FLASHes known */
38 for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
39 flash_info[i].flash_id = FLASH_UNKNOWN;
42 size_b0 = flash_get_size((vu_long *)CONFIG_SYS_FLASH_BASE, &flash_info[0]);
45 flash_get_offsets (CONFIG_SYS_FLASH_BASE, &flash_info[0]);
47 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
48 /* monitor protection ON by default */
49 flash_protect(FLAG_PROTECT_SET,
50 CONFIG_SYS_MONITOR_BASE,
51 CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
55 flash_info[0].size = size_b0;
60 /*-----------------------------------------------------------------------
62 static void flash_get_offsets (ulong base, flash_info_t *info)
66 if (info->flash_id & FLASH_BTYPE) {
67 /* set sector offsets for bottom boot block type */
68 info->start[0] = base + 0x00000000;
69 info->start[1] = base + 0x00008000;
70 info->start[2] = base + 0x00010000;
71 info->start[3] = base + 0x00018000;
72 info->start[4] = base + 0x00020000;
73 info->start[5] = base + 0x00028000;
74 info->start[6] = base + 0x00030000;
75 info->start[7] = base + 0x00038000;
76 for (i = 8; i < info->sector_count; i++) {
77 info->start[i] = base + ((i-7) * 0x00040000) ;
82 /*-----------------------------------------------------------------------
84 void flash_print_info (flash_info_t *info)
88 if (info->flash_id == FLASH_UNKNOWN) {
89 printf ("missing or unknown FLASH type\n");
93 switch (info->flash_id & FLASH_VENDMASK) {
94 case FLASH_MAN_AMD: printf ("AMD "); break;
95 default: printf ("Unknown Vendor "); break;
98 switch (info->flash_id & FLASH_TYPEMASK) {
100 printf ("AMDL323DB (16 Mbytes, bottom boot sect)\n");
103 printf ("Unknown Chip Type\n");
107 printf (" Size: %ld MB in %d Sectors\n",
108 info->size >> 20, info->sector_count);
110 printf (" Sector Start Addresses:");
111 for (i=0; i<info->sector_count; ++i) {
116 info->protect[i] ? " (RO)" : " "
122 /*-----------------------------------------------------------------------
126 /*-----------------------------------------------------------------------
130 * The following code cannot be run from FLASH!
133 static ulong flash_get_size (vu_long *addr, flash_info_t *info)
137 ulong base = (ulong)addr;
139 /* Reset flash componeny */
140 addr [0] = 0xf0f0f0f0;
142 /* Write auto select command: read Manufacturer ID */
143 addr[0xAAA] = 0xAAAAAAAA ;
144 addr[0x555] = 0x55555555 ;
145 addr[0xAAA] = 0x90909090 ;
149 switch (value & 0x00FF00FF) {
151 info->flash_id = FLASH_MAN_AMD;
154 info->flash_id = FLASH_UNKNOWN;
155 info->sector_count = 0;
157 return (0); /* no or unknown flash */
160 value = addr[2] ; /* device ID */
162 switch (value & 0x00FF00FF) {
163 case (AMD_ID_DL323B & 0x00FF00FF):
164 info->flash_id += FLASH_AMDL323B;
165 info->sector_count = 71;
166 info->size = 0x01000000; /* 16 Mb */
170 info->flash_id = FLASH_UNKNOWN;
171 return (0); /* => no or unknown flash */
174 /* set up sector start address table */
175 /* set sector offsets for bottom boot block type */
176 info->start[0] = base + 0x00000000;
177 info->start[1] = base + 0x00008000;
178 info->start[2] = base + 0x00010000;
179 info->start[3] = base + 0x00018000;
180 info->start[4] = base + 0x00020000;
181 info->start[5] = base + 0x00028000;
182 info->start[6] = base + 0x00030000;
183 info->start[7] = base + 0x00038000;
184 for (i = 8; i < info->sector_count; i++) {
185 info->start[i] = base + ((i-7) * 0x00040000) ;
188 /* check for protected sectors */
189 for (i = 0; i < 23; i++) {
190 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
191 /* D0 = 1 if protected */
192 addr = (volatile unsigned long *)(info->start[i]);
193 info->protect[i] = addr[4] & 1 ;
195 /* Check for protected sectors in the 2nd bank */
196 addr[0x100AAA] = 0xAAAAAAAA ;
197 addr[0x100555] = 0x55555555 ;
198 addr[0x100AAA] = 0x90909090 ;
200 for (i = 23; i < info->sector_count; i++) {
201 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
202 /* D0 = 1 if protected */
203 addr = (volatile unsigned long *)(info->start[i]);
204 info->protect[i] = addr[4] & 1 ;
208 * Prevent writes to uninitialized FLASH.
210 if (info->flash_id != FLASH_UNKNOWN) {
211 addr = (volatile unsigned long *)info->start[0];
213 *addr = 0xF0F0F0F0; /* reset bank 1 */
214 addr = (volatile unsigned long *)info->start[23];
216 *addr = 0xF0F0F0F0; /* reset bank 2 */
224 /*-----------------------------------------------------------------------
227 int flash_erase (flash_info_t *info, int s_first, int s_last)
229 vu_long *addr = (vu_long*)(info->start[0]);
230 int flag, prot, sect, l_sect;
231 ulong start, now, last;
233 if ((s_first < 0) || (s_first > s_last)) {
234 if (info->flash_id == FLASH_UNKNOWN) {
235 printf ("- missing\n");
237 printf ("- no sectors to erase\n");
242 if ((info->flash_id == FLASH_UNKNOWN) ||
243 (info->flash_id > FLASH_AMD_COMP)) {
244 printf ("Can't erase unknown flash type %08lx - aborted\n",
250 for (sect=s_first; sect<=s_last; ++sect) {
251 if (info->protect[sect]) {
257 printf ("- Warning: %d protected sectors will not be erased!\n",
265 /* Disable interrupts which might cause a timeout here */
266 flag = disable_interrupts();
268 addr[0xAAA] = 0xAAAAAAAA;
269 addr[0x555] = 0x55555555;
270 addr[0xAAA] = 0x80808080;
271 addr[0xAAA] = 0xAAAAAAAA;
272 addr[0x555] = 0x55555555;
274 /* Start erase on unprotected sectors */
275 for (sect = s_first; sect<=s_last; sect++) {
276 if (info->protect[sect] == 0) { /* not protected */
277 addr = (vu_long *)(info->start[sect]) ;
278 addr[0] = 0x30303030 ;
283 /* re-enable interrupts if necessary */
287 /* wait at least 80us - let's wait 1 ms */
291 * We wait for the last triggered sector
296 start = get_timer (0);
298 addr = (vu_long *)(info->start[l_sect]);
299 while ((addr[0] & 0x80808080) != 0x80808080) {
300 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
301 printf ("Timeout\n");
304 /* show that we're waiting */
305 if ((now - last) > 1000) { /* every second */
312 /* reset to read mode */
313 addr = (vu_long *)info->start[0];
314 addr[0] = 0xF0F0F0F0; /* reset bank */
320 /*-----------------------------------------------------------------------
321 * Copy memory to flash, returns:
324 * 2 - Flash not erased
327 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
332 wp = (addr & ~3); /* get lower word aligned address */
335 * handle unaligned start bytes
337 if ((l = addr - wp) != 0) {
339 for (i=0, cp=wp; i<l; ++i, ++cp) {
340 data = (data << 8) | (*(uchar *)cp);
342 for (; i<4 && cnt>0; ++i) {
343 data = (data << 8) | *src++;
347 for (; cnt==0 && i<4; ++i, ++cp) {
348 data = (data << 8) | (*(uchar *)cp);
351 if ((rc = write_word(info, wp, data)) != 0) {
358 * handle word aligned part
362 for (i=0; i<4; ++i) {
363 data = (data << 8) | *src++;
365 if ((rc = write_word(info, wp, data)) != 0) {
377 * handle unaligned tail bytes
380 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
381 data = (data << 8) | *src++;
384 for (; i<4; ++i, ++cp) {
385 data = (data << 8) | (*(uchar *)cp);
388 return (write_word(info, wp, data));
391 /*-----------------------------------------------------------------------
392 * Write a word to Flash, returns:
395 * 2 - Flash not erased
397 static int write_word (flash_info_t *info, ulong dest, ulong data)
399 vu_long *addr = (vu_long *)(info->start[0]);
403 /* Check if Flash is (sufficiently) erased */
404 if ((*((vu_long *)dest) & data) != data) {
407 /* Disable interrupts which might cause a timeout here */
408 flag = disable_interrupts();
410 addr[0xAAA] = 0xAAAAAAAA;
411 addr[0x555] = 0x55555555;
412 addr[0xAAA] = 0xA0A0A0A0;
414 *((vu_long *)dest) = data;
416 /* re-enable interrupts if necessary */
420 /* data polling for D7 */
421 start = get_timer (0);
422 while ((*((vu_long *)dest) & 0x80808080) != (data & 0x80808080)) {
423 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
430 /*-----------------------------------------------------------------------