blob: 157b71da85e80b426b9979723418ea4e1183f41a [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenkc4cbd342005-01-09 18:21:42 +00002/*
3 * (C) Copyright 2000-2003
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
wdenkc4cbd342005-01-09 18:21:42 +00005 */
6
Tom Rinicec639a2024-04-30 20:41:27 -06007#include <config.h>
Simon Glassa73bda42015-11-08 23:47:45 -07008#include <console.h>
Simon Glass1d91ba72019-11-14 12:57:37 -07009#include <cpu_func.h>
Simon Glass8e201882020-05-10 11:39:54 -060010#include <flash.h>
Simon Glass8f3f7612019-11-14 12:57:42 -070011#include <irq_func.h>
Tom Rinicec639a2024-04-30 20:41:27 -060012#include <stdio.h>
13#include <time.h>
Simon Glass8e201882020-05-10 11:39:54 -060014#include <uuid.h>
Tom Rinicec639a2024-04-30 20:41:27 -060015#include <vsprintf.h>
Simon Glassdbd79542020-05-10 11:40:11 -060016#include <linux/delay.h>
Tom Rinicec639a2024-04-30 20:41:27 -060017#include <linux/string.h>
wdenkc4cbd342005-01-09 18:21:42 +000018
Tom Rini6a5dccc2022-11-16 13:10:41 -050019#define PHYS_FLASH_1 CFG_SYS_FLASH_BASE
wdenkc4cbd342005-01-09 18:21:42 +000020#define FLASH_BANK_SIZE 0x200000
21
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020022flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
wdenkc4cbd342005-01-09 18:21:42 +000023
Simon Glassc7443162020-05-10 11:39:53 -060024void flash_print_info(flash_info_t *info)
wdenkc4cbd342005-01-09 18:21:42 +000025{
26 int i;
27
28 switch (info->flash_id & FLASH_VENDMASK) {
29 case (AMD_MANUFACT & FLASH_VENDMASK):
30 printf ("AMD: ");
31 break;
32 default:
33 printf ("Unknown Vendor ");
34 break;
35 }
36
37 switch (info->flash_id & FLASH_TYPEMASK) {
38 case (AMD_ID_PL160CB & FLASH_TYPEMASK):
39 printf ("AM29PL160CB (16Mbit)\n");
40 break;
41 default:
42 printf ("Unknown Chip Type\n");
43 goto Done;
44 break;
45 }
46
47 printf (" Size: %ld MB in %d Sectors\n",
48 info->size >> 20, info->sector_count);
49
50 printf (" Sector Start Addresses:");
51 for (i = 0; i < info->sector_count; i++) {
52 if ((i % 5) == 0) {
53 printf ("\n ");
54 }
55 printf (" %08lX%s", info->start[i],
56 info->protect[i] ? " (RO)" : " ");
57 }
58 printf ("\n");
59
60Done:
Marian Balakowicz3e8b1dc2006-05-09 11:28:36 +020061 return;
wdenkc4cbd342005-01-09 18:21:42 +000062}
63
64
Simon Glassc7443162020-05-10 11:39:53 -060065unsigned long flash_init(void)
wdenkc4cbd342005-01-09 18:21:42 +000066{
67 int i, j;
68 ulong size = 0;
69
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020070 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
wdenkc4cbd342005-01-09 18:21:42 +000071 ulong flashbase = 0;
72
73 flash_info[i].flash_id =
74 (AMD_MANUFACT & FLASH_VENDMASK) |
75 (AMD_ID_PL160CB & FLASH_TYPEMASK);
76 flash_info[i].size = FLASH_BANK_SIZE;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020077 flash_info[i].sector_count = CONFIG_SYS_MAX_FLASH_SECT;
78 memset (flash_info[i].protect, 0, CONFIG_SYS_MAX_FLASH_SECT);
wdenkc4cbd342005-01-09 18:21:42 +000079 if (i == 0)
80 flashbase = PHYS_FLASH_1;
81 else
82 panic ("configured to many flash banks!\n");
83
84 for (j = 0; j < flash_info[i].sector_count; j++) {
85 if (j == 0) {
86 /* 1st is 16 KiB */
87 flash_info[i].start[j] = flashbase;
88 }
89 if ((j >= 1) && (j <= 2)) {
90 /* 2nd and 3rd are 8 KiB */
91 flash_info[i].start[j] =
92 flashbase + 0x4000 + 0x2000 * (j - 1);
93 }
94 if (j == 3) {
95 /* 4th is 224 KiB */
96 flash_info[i].start[j] = flashbase + 0x8000;
97 }
98 if ((j >= 4) && (j <= 10)) {
99 /* rest is 256 KiB */
100 flash_info[i].start[j] =
101 flashbase + 0x40000 + 0x40000 * (j -
102 4);
103 }
104 }
105 size += flash_info[i].size;
106 }
107
Simon Glassc7443162020-05-10 11:39:53 -0600108 flash_protect(FLAG_PROTECT_SET,
Tom Rini6a5dccc2022-11-16 13:10:41 -0500109 CFG_SYS_FLASH_BASE,
110 CFG_SYS_FLASH_BASE + 0x3ffff, &flash_info[0]);
wdenkc4cbd342005-01-09 18:21:42 +0000111
112 return size;
113}
114
115
116#define CMD_READ_ARRAY 0x00F0
117#define CMD_UNLOCK1 0x00AA
118#define CMD_UNLOCK2 0x0055
119#define CMD_ERASE_SETUP 0x0080
120#define CMD_ERASE_CONFIRM 0x0030
121#define CMD_PROGRAM 0x00A0
122#define CMD_UNLOCK_BYPASS 0x0020
123
Tom Rini6a5dccc2022-11-16 13:10:41 -0500124#define MEM_FLASH_ADDR1 (*(volatile u16 *)(CFG_SYS_FLASH_BASE + (0x00000555<<1)))
125#define MEM_FLASH_ADDR2 (*(volatile u16 *)(CFG_SYS_FLASH_BASE + (0x000002AA<<1)))
wdenkc4cbd342005-01-09 18:21:42 +0000126
127#define BIT_ERASE_DONE 0x0080
128#define BIT_RDY_MASK 0x0080
129#define BIT_PROGRAM_ERROR 0x0020
130#define BIT_TIMEOUT 0x80000000 /* our flag */
131
132#define READY 1
133#define ERR 2
134#define TMO 4
135
136
Simon Glassc7443162020-05-10 11:39:53 -0600137int flash_erase(flash_info_t *info, int s_first, int s_last)
wdenkc4cbd342005-01-09 18:21:42 +0000138{
139 ulong result;
140 int iflag, cflag, prot, sect;
141 int rc = ERR_OK;
142 int chip1;
Graeme Russ43f02552011-06-28 01:40:55 +0000143 ulong start;
wdenkc4cbd342005-01-09 18:21:42 +0000144
145 /* first look for protection bits */
146
147 if (info->flash_id == FLASH_UNKNOWN)
148 return ERR_UNKNOWN_FLASH_TYPE;
149
150 if ((s_first < 0) || (s_first > s_last)) {
151 return ERR_INVAL;
152 }
153
154 if ((info->flash_id & FLASH_VENDMASK) !=
155 (AMD_MANUFACT & FLASH_VENDMASK)) {
156 return ERR_UNKNOWN_FLASH_VENDOR;
157 }
158
159 prot = 0;
160 for (sect = s_first; sect <= s_last; ++sect) {
161 if (info->protect[sect]) {
162 prot++;
163 }
164 }
165 if (prot)
166 return ERR_PROTECTED;
167
168 /*
169 * Disable interrupts which might cause a timeout
170 * here. Remember that our exception vectors are
171 * at address 0 in the flash, and we don't want a
172 * (ticker) exception to happen while the flash
173 * chip is in programming mode.
174 */
175
Simon Glassfbf091b2019-11-14 12:57:36 -0700176 cflag = icache_status();
177 icache_disable();
Simon Glassf87959b2019-11-14 12:57:40 -0700178 iflag = disable_interrupts();
wdenkc4cbd342005-01-09 18:21:42 +0000179
180 printf ("\n");
181
182 /* Start erase on unprotected sectors */
183 for (sect = s_first; sect <= s_last && !ctrlc (); sect++) {
184 printf ("Erasing sector %2d ... ", sect);
185
186 /* arm simple, non interrupt dependent timer */
Graeme Russ43f02552011-06-28 01:40:55 +0000187 start = get_timer(0);
wdenkc4cbd342005-01-09 18:21:42 +0000188
189 if (info->protect[sect] == 0) { /* not protected */
190 volatile u16 *addr =
191 (volatile u16 *) (info->start[sect]);
192
193 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
194 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
195 MEM_FLASH_ADDR1 = CMD_ERASE_SETUP;
196
197 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
198 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
199 *addr = CMD_ERASE_CONFIRM;
200
201 /* wait until flash is ready */
202 chip1 = 0;
203
204 do {
205 result = *addr;
206
Tom Rini78f88002022-07-23 13:05:00 -0400207 /* check timeout, 1000ms */
208 if (get_timer(start) > 1000) {
wdenkc4cbd342005-01-09 18:21:42 +0000209 MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
210 chip1 = TMO;
211 break;
212 }
213
214 if (!chip1
215 && (result & 0xFFFF) & BIT_ERASE_DONE)
216 chip1 = READY;
217
218 } while (!chip1);
219
220 MEM_FLASH_ADDR1 = CMD_READ_ARRAY;
221
222 if (chip1 == ERR) {
223 rc = ERR_PROG_ERROR;
224 goto outahere;
225 }
226 if (chip1 == TMO) {
Mario Six324b9402018-01-26 14:43:52 +0100227 rc = ERR_TIMEOUT;
wdenkc4cbd342005-01-09 18:21:42 +0000228 goto outahere;
229 }
230
231 printf ("ok.\n");
232 } else { /* it was protected */
233
234 printf ("protected!\n");
235 }
236 }
237
238 if (ctrlc ())
239 printf ("User Interrupt!\n");
240
241 outahere:
242 /* allow flash to settle - wait 10 ms */
Simon Glassdbd79542020-05-10 11:40:11 -0600243 mdelay(10);
wdenkc4cbd342005-01-09 18:21:42 +0000244
245 if (iflag)
Simon Glassf87959b2019-11-14 12:57:40 -0700246 enable_interrupts();
wdenkc4cbd342005-01-09 18:21:42 +0000247
248 if (cflag)
Simon Glassfbf091b2019-11-14 12:57:36 -0700249 icache_enable();
wdenkc4cbd342005-01-09 18:21:42 +0000250
251 return rc;
252}
253
Simon Glassc7443162020-05-10 11:39:53 -0600254static int write_word(flash_info_t *info, ulong dest, ulong data)
wdenkc4cbd342005-01-09 18:21:42 +0000255{
256 volatile u16 *addr = (volatile u16 *) dest;
257 ulong result;
258 int rc = ERR_OK;
259 int cflag, iflag;
260 int chip1;
Graeme Russ43f02552011-06-28 01:40:55 +0000261 ulong start;
wdenkc4cbd342005-01-09 18:21:42 +0000262
263 /*
264 * Check if Flash is (sufficiently) erased
265 */
266 result = *addr;
267 if ((result & data) != data)
268 return ERR_NOT_ERASED;
269
270
271 /*
272 * Disable interrupts which might cause a timeout
273 * here. Remember that our exception vectors are
274 * at address 0 in the flash, and we don't want a
275 * (ticker) exception to happen while the flash
276 * chip is in programming mode.
277 */
278
Simon Glassfbf091b2019-11-14 12:57:36 -0700279 cflag = icache_status();
280 icache_disable();
Simon Glassf87959b2019-11-14 12:57:40 -0700281 iflag = disable_interrupts();
wdenkc4cbd342005-01-09 18:21:42 +0000282
283 MEM_FLASH_ADDR1 = CMD_UNLOCK1;
284 MEM_FLASH_ADDR2 = CMD_UNLOCK2;
285 MEM_FLASH_ADDR1 = CMD_PROGRAM;
286 *addr = data;
287
288 /* arm simple, non interrupt dependent timer */
Graeme Russ43f02552011-06-28 01:40:55 +0000289 start = get_timer(0);
wdenkc4cbd342005-01-09 18:21:42 +0000290
291 /* wait until flash is ready */
292 chip1 = 0;
293 do {
294 result = *addr;
295
Tom Rini78f88002022-07-23 13:05:00 -0400296 /* check timeout, 1000ms */
297 if (get_timer(start) > 1000) {
wdenkc4cbd342005-01-09 18:21:42 +0000298 chip1 = ERR | TMO;
299 break;
300 }
301 if (!chip1 && ((result & 0x80) == (data & 0x80)))
302 chip1 = READY;
303
304 } while (!chip1);
305
306 *addr = CMD_READ_ARRAY;
307
308 if (chip1 == ERR || *addr != data)
309 rc = ERR_PROG_ERROR;
310
311 if (iflag)
Simon Glassf87959b2019-11-14 12:57:40 -0700312 enable_interrupts();
wdenkc4cbd342005-01-09 18:21:42 +0000313
314 if (cflag)
Simon Glassfbf091b2019-11-14 12:57:36 -0700315 icache_enable();
wdenkc4cbd342005-01-09 18:21:42 +0000316
317 return rc;
318}
319
320
Simon Glassc7443162020-05-10 11:39:53 -0600321int write_buff(flash_info_t *info, uchar *src, ulong addr, ulong cnt)
wdenkc4cbd342005-01-09 18:21:42 +0000322{
323 ulong wp, data;
324 int rc;
325
326 if (addr & 1) {
327 printf ("unaligned destination not supported\n");
328 return ERR_ALIGN;
329 }
330
331#if 0
332 if (cnt & 1) {
333 printf ("odd transfer sizes not supported\n");
334 return ERR_ALIGN;
335 }
336#endif
337
338 wp = addr;
339
340 if (addr & 1) {
341 data = (*((volatile u8 *) addr) << 8) | *((volatile u8 *)
342 src);
343 if ((rc = write_word (info, wp - 1, data)) != 0) {
344 return (rc);
345 }
346 src += 1;
347 wp += 1;
348 cnt -= 1;
349 }
350
351 while (cnt >= 2) {
352 data = *((volatile u16 *) src);
353 if ((rc = write_word (info, wp, data)) != 0) {
354 return (rc);
355 }
356 src += 2;
357 wp += 2;
358 cnt -= 2;
359 }
360
361 if (cnt == 1) {
362 data = (*((volatile u8 *) src) << 8) |
363 *((volatile u8 *) (wp + 1));
364 if ((rc = write_word (info, wp, data)) != 0) {
365 return (rc);
366 }
367 src += 1;
368 wp += 1;
369 cnt -= 1;
370 }
371
372 return ERR_OK;
373}