2 * Copyright (C) 2003 ETC s.r.o.
4 * This code was inspired by Marius Groeger and Kyle Harris code
5 * available in other board ports for U-Boot
7 * SPDX-License-Identifier: GPL-2.0+
9 * Written by Peter Figuli <peposh@etc.sk>, 2003.
17 * This code should handle CFI FLASH memory device. This code is very
18 * minimalistic approach without many essential error handling code as well.
19 * Because U-Boot actually is missing smart handling of FLASH device,
20 * we just set flash_id to anything else to FLASH_UNKNOW, so common code
21 * can call us without any restrictions.
22 * TODO: Add CFI Query, to be able to determine FLASH device.
23 * TODO: Add error handling code
24 * NOTE: This code was tested with BUS_WIDTH 4 and ITERLEAVE 2 only, but
25 * hopefully may work with other configurations.
28 #if ( SCB9328_FLASH_BUS_WIDTH == 1 )
29 # define FLASH_BUS vu_char
30 # define FLASH_BUS_RET u_char
31 # if ( SCB9328_FLASH_INTERLEAVE == 1 )
32 # define FLASH_CMD( x ) x
34 # error "With 8bit bus only one chip is allowed"
38 #elif ( SCB9328_FLASH_BUS_WIDTH == 2 )
39 # define FLASH_BUS vu_short
40 # define FLASH_BUS_RET u_short
41 # if ( SCB9328_FLASH_INTERLEAVE == 1 )
42 # define FLASH_CMD( x ) x
43 # elif ( SCB9328_FLASH_INTERLEAVE == 2 )
44 # define FLASH_CMD( x ) (( x << 8 )| x )
46 # error "With 16bit bus only 1 or 2 chip(s) are allowed"
50 #elif ( SCB9328_FLASH_BUS_WIDTH == 4 )
51 # define FLASH_BUS vu_long
52 # define FLASH_BUS_RET u_long
53 # if ( SCB9328_FLASH_INTERLEAVE == 1 )
54 # define FLASH_CMD( x ) x
55 # elif ( SCB9328_FLASH_INTERLEAVE == 2 )
56 # define FLASH_CMD( x ) (( x << 16 )| x )
57 # elif ( SCB9328_FLASH_INTERLEAVE == 4 )
58 # define FLASH_CMD( x ) (( x << 24 )|( x << 16 ) ( x << 8 )| x )
60 # error "With 32bit bus only 1,2 or 4 chip(s) are allowed"
64 # error "Flash bus width might be 1,2,4 for 8,16,32 bit configuration"
68 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
70 static FLASH_BUS_RET flash_status_reg (void)
73 FLASH_BUS *addr = (FLASH_BUS *) 0;
75 /* cppcheck-suppress nullPointer */
76 *addr = FLASH_CMD (CFI_INTEL_CMD_READ_STATUS_REGISTER);
78 /* cppcheck-suppress nullPointer */
82 static int flash_ready (ulong timeout)
88 while ((flash_status_reg () & FLASH_CMD (CFI_INTEL_SR_READY)) !=
89 FLASH_CMD (CFI_INTEL_SR_READY)) {
90 if (get_timer(start) > timeout && timeout != 0) {
98 #if ( CONFIG_SYS_MAX_FLASH_BANKS != 1 )
99 # error "SCB9328 platform has only one flash bank!"
103 ulong flash_init (void)
106 unsigned long address = SCB9328_FLASH_BASE;
108 flash_info[0].size = SCB9328_FLASH_BANK_SIZE;
109 flash_info[0].sector_count = CONFIG_SYS_MAX_FLASH_SECT;
110 flash_info[0].flash_id = INTEL_MANUFACT;
111 memset (flash_info[0].protect, 0, CONFIG_SYS_MAX_FLASH_SECT);
113 for (i = 0; i < CONFIG_SYS_MAX_FLASH_SECT; i++) {
114 flash_info[0].start[i] = address;
115 #ifdef SCB9328_FLASH_UNLOCK
116 /* Some devices are hw locked after start. */
117 *((FLASH_BUS *) address) = FLASH_CMD (CFI_INTEL_CMD_LOCK_SETUP);
118 *((FLASH_BUS *) address) = FLASH_CMD (CFI_INTEL_CMD_UNLOCK_BLOCK);
120 *((FLASH_BUS *) address) = FLASH_CMD (CFI_INTEL_CMD_READ_ARRAY);
122 address += SCB9328_FLASH_SECT_SIZE;
125 flash_protect (FLAG_PROTECT_SET,
126 CONFIG_SYS_FLASH_BASE,
127 CONFIG_SYS_FLASH_BASE + monitor_flash_len - 1,
130 flash_protect (FLAG_PROTECT_SET,
132 CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1, &flash_info[0]);
134 return SCB9328_FLASH_BANK_SIZE;
137 void flash_print_info (flash_info_t * info)
141 printf (" Intel vendor\n");
142 printf (" Size: %ld MB in %d Sectors\n",
143 info->size >> 20, info->sector_count);
145 printf (" Sector Start Addresses:");
146 for (i = 0; i < info->sector_count; i++) {
151 printf (" %08lX%s", info->start[i],
152 info->protect[i] ? " (RO)" : " ");
158 int flash_erase (flash_info_t * info, int s_first, int s_last)
160 int flag, non_protected = 0, sector;
165 for (sector = s_first; sector <= s_last; sector++) {
166 if (!info->protect[sector]) {
171 if (!non_protected) {
172 return ERR_PROTECTED;
176 * Disable interrupts which might cause a timeout
177 * here. Remember that our exception vectors are
178 * at address 0 in the flash, and we don't want a
179 * (ticker) exception to happen while the flash
180 * chip is in programming mode.
182 flag = disable_interrupts ();
185 /* Start erase on unprotected sectors */
186 for (sector = s_first; sector <= s_last && !ctrlc (); sector++) {
187 if (info->protect[sector]) {
188 printf ("Protected sector %2d skipping...\n", sector);
191 printf ("Erasing sector %2d ... ", sector);
194 address = (FLASH_BUS *) (info->start[sector]);
196 *address = FLASH_CMD (CFI_INTEL_CMD_BLOCK_ERASE);
197 *address = FLASH_CMD (CFI_INTEL_CMD_CONFIRM);
198 if (flash_ready (CONFIG_SYS_FLASH_ERASE_TOUT)) {
199 *address = FLASH_CMD (CFI_INTEL_CMD_CLEAR_STATUS_REGISTER);
202 *address = FLASH_CMD (CFI_INTEL_CMD_SUSPEND);
204 printf ("timeout! Aborting...\n");
207 *address = FLASH_CMD (CFI_INTEL_CMD_READ_ARRAY);
210 printf ("User Interrupt!\n");
212 /* allow flash to settle - wait 10 ms */
213 udelay_masked (10000);
215 enable_interrupts ();
221 static int write_data (flash_info_t * info, ulong dest, FLASH_BUS data)
223 FLASH_BUS *address = (FLASH_BUS *) dest;
227 /* Check if Flash is (sufficiently) erased */
228 if ((*address & data) != data) {
229 return ERR_NOT_ERASED;
233 * Disable interrupts which might cause a timeout
234 * here. Remember that our exception vectors are
235 * at address 0 in the flash, and we don't want a
236 * (ticker) exception to happen while the flash
237 * chip is in programming mode.
240 flag = disable_interrupts ();
242 *address = FLASH_CMD (CFI_INTEL_CMD_CLEAR_STATUS_REGISTER);
243 *address = FLASH_CMD (CFI_INTEL_CMD_PROGRAM1);
246 if (!flash_ready (CONFIG_SYS_FLASH_WRITE_TOUT)) {
247 *address = FLASH_CMD (CFI_INTEL_CMD_SUSPEND);
249 printf ("timeout! Aborting...\n");
252 *address = FLASH_CMD (CFI_INTEL_CMD_READ_ARRAY);
254 enable_interrupts ();
260 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
262 ulong read_addr, write_addr;
264 int i, result = ERR_OK;
267 read_addr = addr & ~(sizeof (FLASH_BUS) - 1);
268 write_addr = read_addr;
269 if (read_addr != addr) {
271 for (i = 0; i < sizeof (FLASH_BUS); i++) {
272 if (read_addr < addr || cnt == 0) {
273 data |= *((uchar *) read_addr) << i * 8;
275 data |= (*src++) << i * 8;
280 if ((result = write_data (info, write_addr, data)) != ERR_OK) {
283 write_addr += sizeof (FLASH_BUS);
285 for (; cnt >= sizeof (FLASH_BUS); cnt -= sizeof (FLASH_BUS)) {
286 if ((result = write_data (info, write_addr,
287 *((FLASH_BUS *) src))) != ERR_OK) {
290 write_addr += sizeof (FLASH_BUS);
291 src += sizeof (FLASH_BUS);
294 read_addr = write_addr;
296 for (i = 0; i < sizeof (FLASH_BUS); i++) {
298 data |= (*src++) << i * 8;
301 data |= *((uchar *) read_addr) << i * 8;
305 if ((result = write_data (info, write_addr, data)) != 0) {