[Blackfin]PATCH-1/2]: Remove obsolete blackfin port and add bf533 platform support
[oweals/u-boot.git] / board / bf533-ezkit / flash.c
1 /*
2  * U-boot - flash.c Flash driver for PSD4256GV
3  *
4  * Copyright (c) 2005 blackfin.uclinux.org
5  * This file is based on BF533EzFlash.c originally written by Analog Devices, Inc.
6  *
7  * (C) Copyright 2000-2004
8  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
9  *
10  * See file CREDITS for list of people who contributed to this
11  * project.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License as
15  * published by the Free Software Foundation; either version 2 of
16  * the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
26  * MA 02111-1307 USA
27  */
28
29 #include "flash-defines.h"
30
31 void flash_reset(void)
32 {
33         reset_flash();
34 }
35
36 unsigned long flash_get_size(ulong baseaddr, flash_info_t * info, int bank_flag)
37 {
38         int id = 0, i = 0;
39         static int FlagDev = 1;
40
41         id = get_codes();
42         if (FlagDev) {
43 #ifdef DEBUG
44                 printf("Device ID of the Flash is %x\n", id);
45 #endif
46                 FlagDev = 0;
47         }
48         info->flash_id = id;
49
50         switch (bank_flag) {
51         case 0:
52                 for (i = PriFlashABegin; i < SecFlashABegin; i++)
53                         info->start[i] = (baseaddr + (i * AFP_SectorSize1));
54                 info->size = 0x200000;
55                 info->sector_count = 32;
56                 break;
57         case 1:
58                 info->start[0] = baseaddr + SecFlashASec1Off;
59                 info->start[1] = baseaddr + SecFlashASec2Off;
60                 info->start[2] = baseaddr + SecFlashASec3Off;
61                 info->start[3] = baseaddr + SecFlashASec4Off;
62                 info->size = 0x10000;
63                 info->sector_count = 4;
64                 break;
65         case 2:
66                 info->start[0] = baseaddr + SecFlashBSec1Off;
67                 info->start[1] = baseaddr + SecFlashBSec2Off;
68                 info->start[2] = baseaddr + SecFlashBSec3Off;
69                 info->start[3] = baseaddr + SecFlashBSec4Off;
70                 info->size = 0x10000;
71                 info->sector_count = 4;
72                 break;
73         }
74         return (info->size);
75 }
76
77 unsigned long flash_init(void)
78 {
79         unsigned long size_b0, size_b1, size_b2;
80         int i;
81
82         size_b0 = size_b1 = size_b2 = 0;
83 #ifdef DEBUG
84         printf("Flash Memory Start 0x%x\n", CFG_FLASH_BASE);
85         printf("Memory Map for the Flash\n");
86         printf("0x20000000 - 0x200FFFFF Flash A Primary (1MB)\n");
87         printf("0x20100000 - 0x201FFFFF Flash B Primary (1MB)\n");
88         printf("0x20200000 - 0x2020FFFF Flash A Secondary (64KB)\n");
89         printf("0x20280000 - 0x2028FFFF Flash B Secondary (64KB)\n");
90         printf("Please type command flinfo for information on Sectors \n");
91 #endif
92         for (i = 0; i < CFG_MAX_FLASH_BANKS; ++i) {
93                 flash_info[i].flash_id = FLASH_UNKNOWN;
94         }
95
96         size_b0 = flash_get_size(CFG_FLASH0_BASE, &flash_info[0], 0);
97         size_b1 = flash_get_size(CFG_FLASH0_BASE, &flash_info[1], 1);
98         size_b2 = flash_get_size(CFG_FLASH0_BASE, &flash_info[2], 2);
99
100         if (flash_info[0].flash_id == FLASH_UNKNOWN || size_b0 == 0) {
101                 printf("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
102                        size_b0, size_b0 >> 20);
103         }
104
105         (void)flash_protect(FLAG_PROTECT_SET, CFG_FLASH0_BASE,
106                             (flash_info[0].start[2] - 1), &flash_info[0]);
107
108         return (size_b0 + size_b1 + size_b2);
109 }
110
111 void flash_print_info(flash_info_t * info)
112 {
113         int i;
114
115         if (info->flash_id == FLASH_UNKNOWN) {
116                 printf("missing or unknown FLASH type\n");
117                 return;
118         }
119
120         switch (info->flash_id) {
121         case FLASH_PSD4256GV:
122                 printf("ST Microelectronics ");
123                 break;
124         default:
125                 printf("Unknown Vendor: (0x%08X) ", info->flash_id);
126                 break;
127         }
128         for (i = 0; i < info->sector_count; ++i) {
129                 if ((i % 5) == 0)
130                         printf("\n   ");
131                 printf(" %08lX%s",
132                        info->start[i], info->protect[i] ? " (RO)" : "     ");
133         }
134         printf("\n");
135         return;
136 }
137
138 int flash_erase(flash_info_t * info, int s_first, int s_last)
139 {
140         int cnt = 0, i;
141         int prot, sect;
142
143         prot = 0;
144         for (sect = s_first; sect <= s_last; ++sect) {
145                 if (info->protect[sect])
146                         prot++;
147         }
148
149         if (prot)
150                 printf("- Warning: %d protected sectors will not be erased!\n",
151                        prot);
152         else
153                 printf("\n");
154
155         cnt = s_last - s_first + 1;
156
157         if (cnt == FLASH_TOT_SECT) {
158                 printf("Erasing flash, Please Wait \n");
159                 if (erase_flash() < 0) {
160                         printf("Erasing flash failed \n");
161                         return FLASH_FAIL;
162                 }
163         } else {
164                 printf("Erasing Flash locations, Please Wait\n");
165                 for (i = s_first; i <= s_last; i++) {
166                         if (info->protect[i] == 0) {    /* not protected */
167                                 if (erase_block_flash(i, info->start[i]) < 0) {
168                                         printf("Error Sector erasing \n");
169                                         return FLASH_FAIL;
170                                 }
171                         }
172                 }
173         }
174         return FLASH_SUCCESS;
175 }
176
177 int write_buff(flash_info_t * info, uchar * src, ulong addr, ulong cnt)
178 {
179         int ret;
180
181         ret = write_data(addr, cnt, 1, (int *)src);
182         if (ret == FLASH_FAIL)
183                 return ERR_NOT_ERASED;
184         return FLASH_SUCCESS;
185 }
186
187 int write_data(long lStart, long lCount, long lStride, int *pnData)
188 {
189         long i = 0;
190         int j = 0;
191         unsigned long ulOffset = lStart - CFG_FLASH_BASE;
192         int d;
193         int iShift = 0;
194         int iNumWords = 2;
195         int nLeftover = lCount % 4;
196         int nSector = 0;
197
198         for (i = 0; (i < lCount / 4) && (i < BUFFER_SIZE); i++) {
199                 for (iShift = 0, j = 0; (j < iNumWords);
200                      j++, ulOffset += (lStride * 2)) {
201                         if ((ulOffset >= INVALIDLOCNSTART)
202                             && (ulOffset < INVALIDLOCNEND)) {
203                                 printf
204                                     ("Invalid locations, Try writing to another location \n");
205                                 return FLASH_FAIL;
206                         }
207                         get_sector_number(ulOffset, &nSector);
208                         read_flash(ulOffset, &d);
209                         if (d != 0xffff) {
210                                 printf
211                                     ("Flash not erased at offset 0x%x Please erase to reprogram \n",
212                                      ulOffset);
213                                 return FLASH_FAIL;
214                         }
215                         unlock_flash(ulOffset);
216                         if (write_flash(ulOffset, (pnData[i] >> iShift)) < 0) {
217                                 printf("Error programming the flash \n");
218                                 return FLASH_FAIL;
219                         }
220                         iShift += 16;
221                 }
222         }
223         if (nLeftover > 0) {
224                 if ((ulOffset >= INVALIDLOCNSTART)
225                     && (ulOffset < INVALIDLOCNEND))
226                         return FLASH_FAIL;
227                 get_sector_number(ulOffset, &nSector);
228                 read_flash(ulOffset, &d);
229                 if (d != 0xffff) {
230                         printf
231                             ("Flash already programmed. Please erase to reprogram \n");
232                         printf("uloffset = 0x%x \t d = 0x%x\n", ulOffset, d);
233                         return FLASH_FAIL;
234                 }
235                 unlock_flash(ulOffset);
236                 if (write_flash(ulOffset, pnData[i]) < 0) {
237                         printf("Error programming the flash \n");
238                         return FLASH_FAIL;
239                 }
240         }
241         return FLASH_SUCCESS;
242 }
243
244 int read_data(long ulStart, long lCount, long lStride, int *pnData)
245 {
246         long i = 0;
247         int j = 0;
248         long ulOffset = ulStart;
249         int iShift = 0;
250         int iNumWords = 2;
251         int nLeftover = lCount % 4;
252         int nHi, nLow;
253         int nSector = 0;
254
255         for (i = 0; (i < lCount / 4) && (i < BUFFER_SIZE); i++) {
256                 for (iShift = 0, j = 0; j < iNumWords; j += 2) {
257                         if ((ulOffset >= INVALIDLOCNSTART)
258                             && (ulOffset < INVALIDLOCNEND))
259                                 return FLASH_FAIL;
260
261                         get_sector_number(ulOffset, &nSector);
262                         read_flash(ulOffset, &nLow);
263                         ulOffset += (lStride * 2);
264                         read_flash(ulOffset, &nHi);
265                         ulOffset += (lStride * 2);
266                         pnData[i] = (nHi << 16) | nLow;
267                 }
268         }
269         if (nLeftover > 0) {
270                 if ((ulOffset >= INVALIDLOCNSTART)
271                     && (ulOffset < INVALIDLOCNEND))
272                         return FLASH_FAIL;
273
274                 get_sector_number(ulOffset, &nSector);
275                 read_flash(ulOffset, &pnData[i]);
276         }
277         return FLASH_SUCCESS;
278 }
279
280 int write_flash(long nOffset, int nValue)
281 {
282         long addr;
283
284         addr = (CFG_FLASH_BASE + nOffset);
285         __builtin_bfin_ssync();
286         *(unsigned volatile short *)addr = nValue;
287         __builtin_bfin_ssync();
288         if (poll_toggle_bit(nOffset) < 0)
289                 return FLASH_FAIL;
290         return FLASH_SUCCESS;
291 }
292
293 int read_flash(long nOffset, int *pnValue)
294 {
295         int nValue = 0x0;
296         long addr = (CFG_FLASH_BASE + nOffset);
297
298         if (nOffset != 0x2)
299                 reset_flash();
300         __builtin_bfin_ssync();
301         nValue = *(volatile unsigned short *)addr;
302         __builtin_bfin_ssync();
303         *pnValue = nValue;
304         return TRUE;
305 }
306
307 int poll_toggle_bit(long lOffset)
308 {
309         unsigned int u1, u2;
310         unsigned long timeout = 0xFFFFFFFF;
311         volatile unsigned long *FB =
312             (volatile unsigned long *)(0x20000000 + lOffset);
313         while (1) {
314                 if (timeout < 0)
315                         break;
316                 u1 = *(volatile unsigned short *)FB;
317                 u2 = *(volatile unsigned short *)FB;
318                 if ((u1 & 0x0040) == (u2 & 0x0040))
319                         return FLASH_SUCCESS;
320                 if ((u2 & 0x0020) == 0x0000)
321                         continue;
322                 u1 = *(volatile unsigned short *)FB;
323                 if ((u2 & 0x0040) == (u1 & 0x0040))
324                         return FLASH_SUCCESS;
325                 else {
326                         reset_flash();
327                         return FLASH_FAIL;
328                 }
329                 timeout--;
330         }
331         printf("Time out occured \n");
332         if (timeout < 0)
333                 return FLASH_FAIL;
334 }
335
336 void reset_flash(void)
337 {
338         write_flash(WRITESEQ1, RESET_VAL);
339         /* Wait for 10 micro seconds */
340         udelay(10);
341 }
342
343 int erase_flash(void)
344 {
345         write_flash(WRITESEQ1, WRITEDATA1);
346         write_flash(WRITESEQ2, WRITEDATA2);
347         write_flash(WRITESEQ3, WRITEDATA3);
348         write_flash(WRITESEQ4, WRITEDATA4);
349         write_flash(WRITESEQ5, WRITEDATA5);
350         write_flash(WRITESEQ6, WRITEDATA6);
351
352         if (poll_toggle_bit(0x0000) < 0)
353                 return FLASH_FAIL;
354
355         write_flash(SecFlashAOff + WRITESEQ1, WRITEDATA1);
356         write_flash(SecFlashAOff + WRITESEQ2, WRITEDATA2);
357         write_flash(SecFlashAOff + WRITESEQ3, WRITEDATA3);
358         write_flash(SecFlashAOff + WRITESEQ4, WRITEDATA4);
359         write_flash(SecFlashAOff + WRITESEQ5, WRITEDATA5);
360         write_flash(SecFlashAOff + WRITESEQ6, WRITEDATA6);
361
362         if (poll_toggle_bit(SecFlashASec1Off) < 0)
363                 return FLASH_FAIL;
364
365         write_flash(PriFlashBOff + WRITESEQ1, WRITEDATA1);
366         write_flash(PriFlashBOff + WRITESEQ2, WRITEDATA2);
367         write_flash(PriFlashBOff + WRITESEQ3, WRITEDATA3);
368         write_flash(PriFlashBOff + WRITESEQ4, WRITEDATA4);
369         write_flash(PriFlashBOff + WRITESEQ5, WRITEDATA5);
370         write_flash(PriFlashBOff + WRITESEQ6, WRITEDATA6);
371
372         if (poll_toggle_bit(PriFlashBOff) < 0)
373                 return FLASH_FAIL;
374
375         write_flash(SecFlashBOff + WRITESEQ1, WRITEDATA1);
376         write_flash(SecFlashBOff + WRITESEQ2, WRITEDATA2);
377         write_flash(SecFlashBOff + WRITESEQ3, WRITEDATA3);
378         write_flash(SecFlashBOff + WRITESEQ4, WRITEDATA4);
379         write_flash(SecFlashBOff + WRITESEQ5, WRITEDATA5);
380         write_flash(SecFlashBOff + WRITESEQ6, WRITEDATA6);
381
382         if (poll_toggle_bit(SecFlashBOff) < 0)
383                 return FLASH_FAIL;
384
385         return FLASH_SUCCESS;
386 }
387
388 int erase_block_flash(int nBlock, unsigned long address)
389 {
390         long ulSectorOff = 0x0;
391
392         if ((nBlock < 0) || (nBlock > AFP_NumSectors))
393                 return FALSE;
394
395         ulSectorOff = (address - CFG_FLASH_BASE);
396
397         write_flash((WRITESEQ1 | ulSectorOff), WRITEDATA1);
398         write_flash((WRITESEQ2 | ulSectorOff), WRITEDATA2);
399         write_flash((WRITESEQ3 | ulSectorOff), WRITEDATA3);
400         write_flash((WRITESEQ4 | ulSectorOff), WRITEDATA4);
401         write_flash((WRITESEQ5 | ulSectorOff), WRITEDATA5);
402
403         write_flash(ulSectorOff, BlockEraseVal);
404
405         if (poll_toggle_bit(ulSectorOff) < 0)
406                 return FLASH_FAIL;
407
408         return FLASH_SUCCESS;
409 }
410
411 void unlock_flash(long ulOffset)
412 {
413         unsigned long ulOffsetAddr = ulOffset;
414         ulOffsetAddr &= 0xFFFF0000;
415
416         write_flash((WRITESEQ1 | ulOffsetAddr), UNLOCKDATA1);
417         write_flash((WRITESEQ2 | ulOffsetAddr), UNLOCKDATA2);
418         write_flash((WRITESEQ3 | ulOffsetAddr), UNLOCKDATA3);
419 }
420
421 int get_codes()
422 {
423         int dev_id = 0;
424
425         write_flash(WRITESEQ1, GETCODEDATA1);
426         write_flash(WRITESEQ2, GETCODEDATA2);
427         write_flash(WRITESEQ3, GETCODEDATA3);
428
429         read_flash(0x0002, &dev_id);
430         dev_id &= 0x00FF;
431
432         reset_flash();
433
434         return dev_id;
435 }
436
437 void get_sector_number(long ulOffset, int *pnSector)
438 {
439         int nSector = 0;
440
441         if (ulOffset >= SecFlashAOff) {
442                 if ((ulOffset < SecFlashASec1Off)
443                     && (ulOffset < SecFlashASec2Off)) {
444                         nSector = SECT32;
445                 } else if ((ulOffset >= SecFlashASec2Off)
446                            && (ulOffset < SecFlashASec3Off)) {
447                         nSector = SECT33;
448                 } else if ((ulOffset >= SecFlashASec3Off)
449                            && (ulOffset < SecFlashASec4Off)) {
450                         nSector = SECT34;
451                 } else if ((ulOffset >= SecFlashASec4Off)
452                            && (ulOffset < SecFlashAEndOff)) {
453                         nSector = SECT35;
454                 }
455         } else if (ulOffset >= SecFlashBOff) {
456                 if ((ulOffset < SecFlashBSec1Off)
457                     && (ulOffset < SecFlashBSec2Off)) {
458                         nSector = SECT36;
459                 }
460                 if ((ulOffset < SecFlashBSec2Off)
461                     && (ulOffset < SecFlashBSec3Off)) {
462                         nSector = SECT37;
463                 }
464                 if ((ulOffset < SecFlashBSec3Off)
465                     && (ulOffset < SecFlashBSec4Off)) {
466                         nSector = SECT38;
467                 }
468                 if ((ulOffset < SecFlashBSec4Off)
469                     && (ulOffset < SecFlashBEndOff)) {
470                         nSector = SECT39;
471                 }
472         } else if ((ulOffset >= PriFlashAOff) && (ulOffset < SecFlashAOff)) {
473                 nSector = ulOffset & 0xffff0000;
474                 nSector = ulOffset >> 16;
475                 nSector = nSector & 0x000ff;
476         }
477
478         if ((nSector >= 0) && (nSector < AFP_NumSectors)) {
479                 *pnSector = nSector;
480         }
481 }