2 * Copyright (C) 2008 Atmel Corporation
4 * SPDX-License-Identifier: GPL-2.0+
8 #ifdef CONFIG_FAVR32_EZKIT_EXT_FLASH
9 #include <asm/arch/cacheflush.h>
11 #include <asm/sections.h>
13 DECLARE_GLOBAL_DATA_PTR;
15 flash_info_t flash_info[1];
17 static void flash_identify(uint16_t *flash, flash_info_t *info)
21 flags = disable_interrupts();
23 dcache_flush_unlocked();
25 writew(0xaa, flash + 0x555);
26 writew(0x55, flash + 0xaaa);
27 writew(0x90, flash + 0x555);
28 info->flash_id = readl(flash);
37 unsigned long flash_init(void)
42 flash_info[0].size = CONFIG_SYS_FLASH_SIZE;
43 flash_info[0].sector_count = 135;
45 flash_identify(uncached((void *)CONFIG_SYS_FLASH_BASE), &flash_info[0]);
47 for (i = 0, addr = 0; i < 8; i++, addr += 0x2000)
48 flash_info[0].start[i] = addr;
49 for (; i < flash_info[0].sector_count; i++, addr += 0x10000)
50 flash_info[0].start[i] = addr;
52 return CONFIG_SYS_FLASH_SIZE;
55 void flash_print_info(flash_info_t *info)
57 printf("Flash: Vendor ID: 0x%02lx, Product ID: 0x%02lx\n",
58 info->flash_id >> 16, info->flash_id & 0xffff);
59 printf("Size: %ld MB in %d sectors\n",
60 info->size >> 10, info->sector_count);
63 int flash_erase(flash_info_t *info, int s_first, int s_last)
66 unsigned long start_time;
72 if ((s_first < 0) || (s_first > s_last)
73 || (s_last >= info->sector_count)) {
74 puts("Error: first and/or last sector out of range\n");
78 for (i = s_first; i < s_last; i++)
79 if (info->protect[i]) {
80 printf("Error: sector %d is protected\n", i);
84 fb = (uint16_t *)uncached(info->start[0]);
86 dcache_flush_unlocked();
88 for (i = s_first; (i <= s_last) && !ctrlc(); i++) {
89 printf("Erasing sector %3d...", i);
91 sb = (uint16_t *)uncached(info->start[i]);
93 flags = disable_interrupts();
95 start_time = get_timer(0);
98 writew(0xaa, fb + 0x555);
102 writew(0xaa, fb + 0x555);
103 writew(0x55, fb + 0xaaa);
104 writew(0x80, fb + 0x555);
105 writew(0xaa, fb + 0x555);
106 writew(0x55, fb + 0xaaa);
109 /* Wait for completion */
114 } while ((status != 0xffff) && !(status & 0x28));
119 * Make sure the command actually makes it to the bus
120 * before we re-enable interrupts.
127 if (status != 0xffff) {
128 printf("Flash erase error at address 0x%p: 0x%02x\n",
130 ret = ERR_PROG_ERROR;
136 printf("User interrupt!\n");
141 int write_buff(flash_info_t *info, uchar *src,
142 ulong addr, ulong count)
145 uint16_t *base, *p, *s, *end;
146 uint16_t word, status, status1;
149 if (addr < info->start[0]
150 || (addr + count) > (info->start[0] + info->size)
151 || (addr + count) < addr) {
152 puts("Error: invalid address range\n");
156 if (addr & 1 || count & 1 || (unsigned int)src & 1) {
157 puts("Error: misaligned source, destination or count\n");
161 base = (uint16_t *)uncached(info->start[0]);
162 end = (uint16_t *)uncached(addr + count);
164 flags = disable_interrupts();
166 dcache_flush_unlocked();
169 for (p = (uint16_t *)uncached(addr), s = (uint16_t *)src;
170 p < end && !ctrlc(); p++, s++) {
173 writew(0xaa, base + 0x555);
174 writew(0x55, base + 0xaaa);
175 writew(0xa0, base + 0x555);
180 /* Wait for completion */
186 } while (((status ^ status1) & 0x40) /* toggled */
187 && !(status1 & 0x28)); /* error bits */
190 * We'll need to check once again for toggle bit
191 * because the toggle bit may stop toggling as I/O5
192 * changes to "1" (ref at49bv642.pdf p9)
196 if ((status ^ status1) & 0x40) {
197 printf("Flash write error at address 0x%p: "
198 "0x%02x != 0x%02x\n",
200 ret = ERR_PROG_ERROR;
216 #endif /* CONFIG_FAVR32_EZKIT_EXT_FLASH */