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 *addr = FLASH_CMD (CFI_INTEL_CMD_READ_STATUS_REGISTER);
80 static int flash_ready (ulong timeout)
86 while ((flash_status_reg () & FLASH_CMD (CFI_INTEL_SR_READY)) !=
87 FLASH_CMD (CFI_INTEL_SR_READY)) {
88 if (get_timer(start) > timeout && timeout != 0) {
96 #if ( CONFIG_SYS_MAX_FLASH_BANKS != 1 )
97 # error "SCB9328 platform has only one flash bank!"
101 ulong flash_init (void)
104 unsigned long address = SCB9328_FLASH_BASE;
106 flash_info[0].size = SCB9328_FLASH_BANK_SIZE;
107 flash_info[0].sector_count = CONFIG_SYS_MAX_FLASH_SECT;
108 flash_info[0].flash_id = INTEL_MANUFACT;
109 memset (flash_info[0].protect, 0, CONFIG_SYS_MAX_FLASH_SECT);
111 for (i = 0; i < CONFIG_SYS_MAX_FLASH_SECT; i++) {
112 flash_info[0].start[i] = address;
113 #ifdef SCB9328_FLASH_UNLOCK
114 /* Some devices are hw locked after start. */
115 *((FLASH_BUS *) address) = FLASH_CMD (CFI_INTEL_CMD_LOCK_SETUP);
116 *((FLASH_BUS *) address) = FLASH_CMD (CFI_INTEL_CMD_UNLOCK_BLOCK);
118 *((FLASH_BUS *) address) = FLASH_CMD (CFI_INTEL_CMD_READ_ARRAY);
120 address += SCB9328_FLASH_SECT_SIZE;
123 flash_protect (FLAG_PROTECT_SET,
124 CONFIG_SYS_FLASH_BASE,
125 CONFIG_SYS_FLASH_BASE + monitor_flash_len - 1,
128 flash_protect (FLAG_PROTECT_SET,
130 CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1, &flash_info[0]);
132 return SCB9328_FLASH_BANK_SIZE;
135 void flash_print_info (flash_info_t * info)
139 printf (" Intel vendor\n");
140 printf (" Size: %ld MB in %d Sectors\n",
141 info->size >> 20, info->sector_count);
143 printf (" Sector Start Addresses:");
144 for (i = 0; i < info->sector_count; i++) {
149 printf (" %08lX%s", info->start[i],
150 info->protect[i] ? " (RO)" : " ");
156 int flash_erase (flash_info_t * info, int s_first, int s_last)
158 int flag, non_protected = 0, sector;
163 for (sector = s_first; sector <= s_last; sector++) {
164 if (!info->protect[sector]) {
169 if (!non_protected) {
170 return ERR_PROTECTED;
174 * Disable interrupts which might cause a timeout
175 * here. Remember that our exception vectors are
176 * at address 0 in the flash, and we don't want a
177 * (ticker) exception to happen while the flash
178 * chip is in programming mode.
180 flag = disable_interrupts ();
183 /* Start erase on unprotected sectors */
184 for (sector = s_first; sector <= s_last && !ctrlc (); sector++) {
185 if (info->protect[sector]) {
186 printf ("Protected sector %2d skipping...\n", sector);
189 printf ("Erasing sector %2d ... ", sector);
192 address = (FLASH_BUS *) (info->start[sector]);
194 *address = FLASH_CMD (CFI_INTEL_CMD_BLOCK_ERASE);
195 *address = FLASH_CMD (CFI_INTEL_CMD_CONFIRM);
196 if (flash_ready (CONFIG_SYS_FLASH_ERASE_TOUT)) {
197 *address = FLASH_CMD (CFI_INTEL_CMD_CLEAR_STATUS_REGISTER);
200 *address = FLASH_CMD (CFI_INTEL_CMD_SUSPEND);
202 printf ("timeout! Aborting...\n");
205 *address = FLASH_CMD (CFI_INTEL_CMD_READ_ARRAY);
208 printf ("User Interrupt!\n");
210 /* allow flash to settle - wait 10 ms */
211 udelay_masked (10000);
213 enable_interrupts ();
219 static int write_data (flash_info_t * info, ulong dest, FLASH_BUS data)
221 FLASH_BUS *address = (FLASH_BUS *) dest;
225 /* Check if Flash is (sufficiently) erased */
226 if ((*address & data) != data) {
227 return ERR_NOT_ERASED;
231 * Disable interrupts which might cause a timeout
232 * here. Remember that our exception vectors are
233 * at address 0 in the flash, and we don't want a
234 * (ticker) exception to happen while the flash
235 * chip is in programming mode.
238 flag = disable_interrupts ();
240 *address = FLASH_CMD (CFI_INTEL_CMD_CLEAR_STATUS_REGISTER);
241 *address = FLASH_CMD (CFI_INTEL_CMD_PROGRAM1);
244 if (!flash_ready (CONFIG_SYS_FLASH_WRITE_TOUT)) {
245 *address = FLASH_CMD (CFI_INTEL_CMD_SUSPEND);
247 printf ("timeout! Aborting...\n");
250 *address = FLASH_CMD (CFI_INTEL_CMD_READ_ARRAY);
252 enable_interrupts ();
258 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
260 ulong read_addr, write_addr;
262 int i, result = ERR_OK;
265 read_addr = addr & ~(sizeof (FLASH_BUS) - 1);
266 write_addr = read_addr;
267 if (read_addr != addr) {
269 for (i = 0; i < sizeof (FLASH_BUS); i++) {
270 if (read_addr < addr || cnt == 0) {
271 data |= *((uchar *) read_addr) << i * 8;
273 data |= (*src++) << i * 8;
278 if ((result = write_data (info, write_addr, data)) != ERR_OK) {
281 write_addr += sizeof (FLASH_BUS);
283 for (; cnt >= sizeof (FLASH_BUS); cnt -= sizeof (FLASH_BUS)) {
284 if ((result = write_data (info, write_addr,
285 *((FLASH_BUS *) src))) != ERR_OK) {
288 write_addr += sizeof (FLASH_BUS);
289 src += sizeof (FLASH_BUS);
292 read_addr = write_addr;
294 for (i = 0; i < sizeof (FLASH_BUS); i++) {
296 data |= (*src++) << i * 8;
299 data |= *((uchar *) read_addr) << i * 8;
303 if ((result = write_data (info, write_addr, data)) != 0) {