blob: eaa6b98025e8bf8a552bd40399d93ccf54d5b916 [file] [log] [blame]
Scott McNutt2ca00852006-06-08 13:37:39 -04001/*
2 * (C) Copyright 2000-2004
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Scott McNutt2ca00852006-06-08 13:37:39 -04006 */
7
8
9#include <common.h>
10#if defined(CONFIG_NIOS)
11#include <nios.h>
12#else
13#include <asm/io.h>
14#endif
15
16#define SECTSZ (64 * 1024)
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020017flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
Scott McNutt2ca00852006-06-08 13:37:39 -040018
19/*----------------------------------------------------------------------*/
20unsigned long flash_init (void)
21{
22 int i;
23 unsigned long addr;
24 flash_info_t *fli = &flash_info[0];
25
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020026 fli->size = CONFIG_SYS_FLASH_SIZE;
27 fli->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
Scott McNutt2ca00852006-06-08 13:37:39 -040028 fli->flash_id = FLASH_MAN_AMD + FLASH_AMDLV065D;
29
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020030 addr = CONFIG_SYS_FLASH_BASE;
Scott McNutt2ca00852006-06-08 13:37:39 -040031 for (i = 0; i < fli->sector_count; ++i) {
32 fli->start[i] = addr;
33 addr += SECTSZ;
34 fli->protect[i] = 1;
35 }
36
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020037 return (CONFIG_SYS_FLASH_SIZE);
Scott McNutt2ca00852006-06-08 13:37:39 -040038}
39/*--------------------------------------------------------------------*/
40void flash_print_info (flash_info_t * info)
41{
42 int i, k;
43 int erased;
44 unsigned long *addr;
45
46 printf (" Size: %ld KB in %d Sectors\n",
47 info->size >> 10, info->sector_count);
48 printf (" Sector Start Addresses:");
49 for (i = 0; i < info->sector_count; ++i) {
50
51 /* Check if whole sector is erased */
52 erased = 1;
53 addr = (unsigned long *) info->start[i];
54 for (k = 0; k < SECTSZ/sizeof(unsigned long); k++) {
55 if ( readl(addr++) != (unsigned long)-1) {
56 erased = 0;
57 break;
58 }
59 }
60
61 /* Print the info */
62 if ((i % 5) == 0)
63 printf ("\n ");
64 printf (" %08lX%s%s",
65 info->start[i],
66 erased ? " E" : " ",
67 info->protect[i] ? "RO " : " ");
68 }
69 printf ("\n");
70}
71
72/*-------------------------------------------------------------------*/
73
74
75int flash_erase (flash_info_t * info, int s_first, int s_last)
76{
77 unsigned char *addr = (unsigned char *) info->start[0];
78 unsigned char *addr2;
79 int prot, sect;
80 ulong start;
81
82 /* Some sanity checking */
83 if ((s_first < 0) || (s_first > s_last)) {
84 printf ("- no sectors to erase\n");
85 return 1;
86 }
87
88 prot = 0;
89 for (sect = s_first; sect <= s_last; ++sect) {
90 if (info->protect[sect]) {
91 prot++;
92 }
93 }
94 if (prot) {
95 printf ("- Warning: %d protected sectors will not be erased!\n",
96 prot);
97 } else {
98 printf ("\n");
99 }
100
101 /* It's ok to erase multiple sectors provided we don't delay more
102 * than 50 usec between cmds ... at which point the erase time-out
103 * occurs. So don't go and put printf() calls in the loop ... it
104 * won't be very helpful ;-)
105 */
106 for (sect = s_first; sect <= s_last; sect++) {
107 if (info->protect[sect] == 0) { /* not protected */
108 addr2 = (unsigned char *) info->start[sect];
Scott McNutt0fd72d32010-03-21 21:24:43 -0400109 writeb (0xaa, addr);
110 writeb (0x55, addr);
111 writeb (0x80, addr);
112 writeb (0xaa, addr);
113 writeb (0x55, addr);
114 writeb (0x30, addr2);
Scott McNutt2ca00852006-06-08 13:37:39 -0400115 /* Now just wait for 0xff & provide some user
116 * feedback while we wait.
117 */
118 start = get_timer (0);
119 while ( readb (addr2) != 0xff) {
120 udelay (1000 * 1000);
121 putc ('.');
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200122 if (get_timer (start) > CONFIG_SYS_FLASH_ERASE_TOUT) {
Scott McNutt2ca00852006-06-08 13:37:39 -0400123 printf ("timeout\n");
124 return 1;
125 }
126 }
127 }
128 }
129 printf ("\n");
130 return 0;
131}
132
133/*-----------------------------------------------------------------------
134 * Copy memory to flash, returns:
135 * 0 - OK
136 * 1 - write timeout
137 * 2 - Flash not erased
138 */
139
140int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
141{
142
143 vu_char *cmd = (vu_char *) info->start[0];
144 vu_char *dst = (vu_char *) addr;
145 unsigned char b;
146 ulong start;
147
148 while (cnt) {
149 /* Check for sufficient erase */
150 b = *src;
151 if ((readb (dst) & b) != b) {
152 printf ("%02x : %02x\n", readb (dst), b);
153 return (2);
154 }
155
Scott McNutt0fd72d32010-03-21 21:24:43 -0400156 writeb (0xaa, cmd);
157 writeb (0x55, cmd);
158 writeb (0xa0, cmd);
Scott McNuttbd819482010-03-30 20:26:15 -0400159 writeb (b, dst);
Scott McNutt2ca00852006-06-08 13:37:39 -0400160
161 /* Verify write */
162 start = get_timer (0);
163 while (readb (dst) != b) {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200164 if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
Scott McNutt2ca00852006-06-08 13:37:39 -0400165 return 1;
166 }
167 }
168 dst++;
169 src++;
170 cnt--;
171 }
172
173 return (0);
174}