blob: 64cb97029eeb043fde19fc6f6ca7ae78bc8c14a5 [file] [log] [blame]
wdenk5c952cf2004-10-10 21:27:30 +00001/*
2 * (C) Copyright 2000-2004
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenk5c952cf2004-10-10 21:27:30 +00006 */
7
8
9#include <common.h>
Scott McNutt3d22d0b2006-06-08 12:03:21 -040010#include <asm/io.h>
wdenk5c952cf2004-10-10 21:27:30 +000011
12#define SECTSZ (64 * 1024)
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020013flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
wdenk5c952cf2004-10-10 21:27:30 +000014
15/*----------------------------------------------------------------------*/
16unsigned long flash_init (void)
17{
18 int i;
19 unsigned long addr;
20 flash_info_t *fli = &flash_info[0];
21
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020022 fli->size = CONFIG_SYS_FLASH_SIZE;
23 fli->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
wdenk5c952cf2004-10-10 21:27:30 +000024 fli->flash_id = FLASH_MAN_AMD + FLASH_AMDLV065D;
25
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020026 addr = CONFIG_SYS_FLASH_BASE;
wdenk5c952cf2004-10-10 21:27:30 +000027 for (i = 0; i < fli->sector_count; ++i) {
28 fli->start[i] = addr;
29 addr += SECTSZ;
30 fli->protect[i] = 1;
31 }
32
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020033 return (CONFIG_SYS_FLASH_SIZE);
wdenk5c952cf2004-10-10 21:27:30 +000034}
35/*--------------------------------------------------------------------*/
36void flash_print_info (flash_info_t * info)
37{
38 int i, k;
wdenk5c952cf2004-10-10 21:27:30 +000039 int erased;
Scott McNutt3d22d0b2006-06-08 12:03:21 -040040 unsigned long *addr;
wdenk5c952cf2004-10-10 21:27:30 +000041
42 printf (" Size: %ld KB in %d Sectors\n",
43 info->size >> 10, info->sector_count);
44 printf (" Sector Start Addresses:");
45 for (i = 0; i < info->sector_count; ++i) {
46
47 /* Check if whole sector is erased */
wdenk5c952cf2004-10-10 21:27:30 +000048 erased = 1;
Scott McNutt3d22d0b2006-06-08 12:03:21 -040049 addr = (unsigned long *) info->start[i];
50 for (k = 0; k < SECTSZ/sizeof(unsigned long); k++) {
51 if ( readl(addr++) != (unsigned long)-1) {
wdenk5c952cf2004-10-10 21:27:30 +000052 erased = 0;
53 break;
54 }
55 }
56
57 /* Print the info */
58 if ((i % 5) == 0)
59 printf ("\n ");
60 printf (" %08lX%s%s",
Scott McNutt3d22d0b2006-06-08 12:03:21 -040061 info->start[i],
wdenk5c952cf2004-10-10 21:27:30 +000062 erased ? " E" : " ",
63 info->protect[i] ? "RO " : " ");
64 }
65 printf ("\n");
66}
67
68/*-------------------------------------------------------------------*/
69
70
71int flash_erase (flash_info_t * info, int s_first, int s_last)
72{
Scott McNutt3d22d0b2006-06-08 12:03:21 -040073 unsigned char *addr = (unsigned char *) info->start[0];
74 unsigned char *addr2;
wdenk5c952cf2004-10-10 21:27:30 +000075 int prot, sect;
76 ulong start;
77
78 /* Some sanity checking */
79 if ((s_first < 0) || (s_first > s_last)) {
80 printf ("- no sectors to erase\n");
81 return 1;
82 }
83
84 prot = 0;
85 for (sect = s_first; sect <= s_last; ++sect) {
86 if (info->protect[sect]) {
87 prot++;
88 }
89 }
90 if (prot) {
91 printf ("- Warning: %d protected sectors will not be erased!\n",
92 prot);
93 } else {
94 printf ("\n");
95 }
96
97 /* It's ok to erase multiple sectors provided we don't delay more
98 * than 50 usec between cmds ... at which point the erase time-out
99 * occurs. So don't go and put printf() calls in the loop ... it
100 * won't be very helpful ;-)
101 */
102 for (sect = s_first; sect <= s_last; sect++) {
103 if (info->protect[sect] == 0) { /* not protected */
Scott McNutt3d22d0b2006-06-08 12:03:21 -0400104 addr2 = (unsigned char *) info->start[sect];
Scott McNutt3ea00372010-03-21 21:24:43 -0400105 writeb (0xaa, addr);
106 writeb (0x55, addr);
107 writeb (0x80, addr);
108 writeb (0xaa, addr);
109 writeb (0x55, addr);
110 writeb (0x30, addr2);
wdenk5c952cf2004-10-10 21:27:30 +0000111 /* Now just wait for 0xff & provide some user
112 * feedback while we wait.
113 */
114 start = get_timer (0);
Scott McNutt3d22d0b2006-06-08 12:03:21 -0400115 while ( readb (addr2) != 0xff) {
wdenk5c952cf2004-10-10 21:27:30 +0000116 udelay (1000 * 1000);
117 putc ('.');
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200118 if (get_timer (start) > CONFIG_SYS_FLASH_ERASE_TOUT) {
wdenk5c952cf2004-10-10 21:27:30 +0000119 printf ("timeout\n");
120 return 1;
121 }
122 }
123 }
124 }
125 printf ("\n");
126 return 0;
127}
128
129/*-----------------------------------------------------------------------
130 * Copy memory to flash, returns:
131 * 0 - OK
132 * 1 - write timeout
133 * 2 - Flash not erased
134 */
135
136int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
137{
138
Scott McNutt3d22d0b2006-06-08 12:03:21 -0400139 vu_char *cmd = (vu_char *) info->start[0];
140 vu_char *dst = (vu_char *) addr;
wdenk5c952cf2004-10-10 21:27:30 +0000141 unsigned char b;
142 ulong start;
143
144 while (cnt) {
145 /* Check for sufficient erase */
146 b = *src;
Scott McNutt3d22d0b2006-06-08 12:03:21 -0400147 if ((readb (dst) & b) != b) {
148 printf ("%02x : %02x\n", readb (dst), b);
wdenk5c952cf2004-10-10 21:27:30 +0000149 return (2);
150 }
151
Scott McNutt3ea00372010-03-21 21:24:43 -0400152 writeb (0xaa, cmd);
153 writeb (0x55, cmd);
154 writeb (0xa0, cmd);
Scott McNutt3d22d0b2006-06-08 12:03:21 -0400155 writeb (dst, b);
wdenk5c952cf2004-10-10 21:27:30 +0000156
157 /* Verify write */
158 start = get_timer (0);
Scott McNutt3d22d0b2006-06-08 12:03:21 -0400159 while (readb (dst) != b) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200160 if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
wdenk5c952cf2004-10-10 21:27:30 +0000161 return 1;
162 }
163 }
164 dst++;
165 src++;
166 cnt--;
167 }
168
169 return (0);
170}