3 * Cristian Birsan <cristian.birsan@microchip.com>
4 * Purna Chandra Mandal <purna.mandal@microchip.com>
6 * SPDX-License-Identifier: GPL-2.0+
11 #include <fdt_support.h>
13 #include <mach/pic32.h>
16 DECLARE_GLOBAL_DATA_PTR;
18 /* NVM Controller registers */
19 struct pic32_reg_nvm {
20 struct pic32_reg_atomic ctrl;
21 struct pic32_reg_atomic key;
22 struct pic32_reg_atomic addr;
23 struct pic32_reg_atomic data;
28 #define NVMOP_WORD_WRITE 1
29 #define NVMOP_PAGE_ERASE 4
31 /* NVM control bits */
32 #define NVM_WR BIT(15)
33 #define NVM_WREN BIT(14)
34 #define NVM_WRERR BIT(13)
35 #define NVM_LVDERR BIT(12)
37 /* NVM programming unlock register */
39 #define UNLOCK_KEY1 0xaa996655
40 #define UNLOCK_KEY2 0x556699aa
43 * PIC32 flash banks consist of number of pages, each page
44 * into number of rows and rows into number of words.
45 * Here we will maintain page information instead of sector.
47 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
48 static struct pic32_reg_nvm *nvm_regs_p;
50 static inline void flash_initiate_operation(u32 nvmop)
53 writel(nvmop, &nvm_regs_p->ctrl.raw);
55 /* enable flash write */
56 writel(NVM_WREN, &nvm_regs_p->ctrl.set);
59 writel(LOCK_KEY, &nvm_regs_p->key.raw);
60 writel(UNLOCK_KEY1, &nvm_regs_p->key.raw);
61 writel(UNLOCK_KEY2, &nvm_regs_p->key.raw);
63 /* initiate operation */
64 writel(NVM_WR, &nvm_regs_p->ctrl.set);
67 static int flash_wait_till_busy(const char *func, ulong timeout)
69 int ret = wait_for_bit_le32(&nvm_regs_p->ctrl.raw,
70 NVM_WR, false, timeout, false);
72 return ret ? ERR_TIMEOUT : ERR_OK;
75 static inline int flash_complete_operation(void)
79 tmp = readl(&nvm_regs_p->ctrl.raw);
80 if (tmp & NVM_WRERR) {
81 printf("Error in Block Erase - Lock Bit may be set!\n");
82 flash_initiate_operation(NVMOP_NOP);
86 if (tmp & NVM_LVDERR) {
87 printf("Error in Block Erase - low-vol detected!\n");
88 flash_initiate_operation(NVMOP_NOP);
89 return ERR_NOT_ERASED;
92 /* disable flash write or erase operation */
93 writel(NVM_WREN, &nvm_regs_p->ctrl.clr);
99 * Erase flash sectors, returns:
101 * ERR_INVAL - invalid sector arguments
102 * ERR_TIMEOUT - write timeout
103 * ERR_NOT_ERASED - Flash not erased
104 * ERR_UNKNOWN_FLASH_VENDOR - incorrect flash
106 int flash_erase(flash_info_t *info, int s_first, int s_last)
108 ulong sect_start, sect_end, flags;
112 if ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_MCHP) {
113 printf("Can't erase unknown flash type %08lx - aborted\n",
115 return ERR_UNKNOWN_FLASH_VENDOR;
118 if ((s_first < 0) || (s_first > s_last)) {
119 printf("- no sectors to erase\n");
124 for (sect = s_first; sect <= s_last; ++sect) {
125 if (info->protect[sect])
130 printf("- Warning: %d protected sectors will not be erased!\n",
135 /* erase on unprotected sectors */
136 for (sect = s_first; sect <= s_last; sect++) {
137 if (info->protect[sect])
140 /* disable interrupts */
141 flags = disable_interrupts();
143 /* write destination page address (physical) */
144 sect_start = CPHYSADDR(info->start[sect]);
145 writel(sect_start, &nvm_regs_p->addr.raw);
148 flash_initiate_operation(NVMOP_PAGE_ERASE);
151 rc = flash_wait_till_busy(__func__,
152 CONFIG_SYS_FLASH_ERASE_TOUT);
154 /* re-enable interrupts if necessary */
161 rc = flash_complete_operation();
166 * flash content is updated but cache might contain stale
167 * data, so invalidate dcache.
169 sect_end = info->start[sect] + info->size / info->sector_count;
170 invalidate_dcache_range(info->start[sect], sect_end);
177 int page_erase(flash_info_t *info, int sect)
182 /* Write a word to flash */
183 static int write_word(flash_info_t *info, ulong dest, ulong word)
188 /* read flash to check if it is sufficiently erased */
189 if ((readl((void __iomem *)dest) & word) != word) {
190 printf("Error, Flash not erased!\n");
191 return ERR_NOT_ERASED;
194 /* disable interrupts */
195 flags = disable_interrupts();
197 /* update destination page address (physical) */
198 writel(CPHYSADDR(dest), &nvm_regs_p->addr.raw);
199 writel(word, &nvm_regs_p->data.raw);
202 flash_initiate_operation(NVMOP_WORD_WRITE);
204 /* wait for operation to complete */
205 rc = flash_wait_till_busy(__func__, CONFIG_SYS_FLASH_WRITE_TOUT);
207 /* re-enable interrupts if necessary */
214 return flash_complete_operation();
218 * Copy memory to flash, returns:
220 * ERR_TIMEOUT - write timeout
221 * ERR_NOT_ERASED - Flash not erased
223 int write_buff(flash_info_t *info, uchar *src, ulong addr, ulong cnt)
225 ulong dst, tmp_le, len = cnt;
229 /* get lower word aligned address */
232 /* handle unaligned start bytes */
236 for (i = 0, cp = (uchar *)dst; i < l; ++i, ++cp)
237 tmp_le |= *cp << (i * 8);
239 for (; (i < 4) && (cnt > 0); ++i, ++src, --cnt, ++cp)
240 tmp_le |= *src << (i * 8);
242 for (; (cnt == 0) && (i < 4); ++i, ++cp)
243 tmp_le |= *cp << (i * 8);
245 rc = write_word(info, dst, tmp_le);
252 /* handle word aligned part */
254 tmp_le = src[0] | src[1] << 8 | src[2] << 16 | src[3] << 24;
255 rc = write_word(info, dst, tmp_le);
268 /* handle unaligned tail bytes */
270 for (i = 0, cp = (uchar *)dst; (i < 4) && (cnt > 0); ++i, ++cp) {
271 tmp_le |= *src++ << (i * 8);
275 for (; i < 4; ++i, ++cp)
276 tmp_le |= *cp << (i * 8);
278 rc = write_word(info, dst, tmp_le);
281 * flash content updated by nvm controller but CPU cache might
282 * have stale data, so invalidate dcache.
284 invalidate_dcache_range(addr, addr + len);
290 void flash_print_info(flash_info_t *info)
294 if (info->flash_id == FLASH_UNKNOWN) {
295 printf("missing or unknown FLASH type\n");
299 switch (info->flash_id & FLASH_VENDMASK) {
301 printf("Microchip Technology ");
304 printf("Unknown Vendor ");
308 switch (info->flash_id & FLASH_TYPEMASK) {
310 printf("Internal (8 Mbit, 64 x 16k)\n");
313 printf("Unknown Chip Type\n");
317 printf(" Size: %ld MB in %d Sectors\n",
318 info->size >> 20, info->sector_count);
320 printf(" Sector Start Addresses:");
321 for (i = 0; i < info->sector_count; ++i) {
325 printf(" %08lX%s", info->start[i],
326 info->protect[i] ? " (RO)" : " ");
331 unsigned long flash_init(void)
333 unsigned long size = 0;
337 /* probe every MTD device */
338 for (uclass_first_device(UCLASS_MTD, &dev); dev;
339 uclass_next_device(&dev)) {
343 /* calc total flash size */
344 for (bank = 0; bank < CONFIG_SYS_MAX_FLASH_BANKS; ++bank)
345 size += flash_info[bank].size;
350 static void pic32_flash_bank_init(flash_info_t *info,
351 ulong base, ulong size)
356 /* device & manufacturer code */
357 info->flash_id = FLASH_MAN_MCHP | FLASH_MCHP100T;
358 info->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
361 /* update sector (i.e page) info */
362 sect_size = info->size / info->sector_count;
363 for (sect = 0; sect < info->sector_count; sect++) {
364 info->start[sect] = base;
365 /* protect each sector by default */
366 info->protect[sect] = 1;
371 static int pic32_flash_probe(struct udevice *dev)
373 void *blob = (void *)gd->fdt_blob;
374 int node = dev_of_offset(dev);
375 const char *list, *end;
377 unsigned long addr, size;
378 int parent, addrc, sizec;
383 * decode regs. there are multiple reg tuples, and they need to
384 * match with reg-names.
386 parent = fdt_parent_offset(blob, node);
387 fdt_support_default_count_cells(blob, parent, &addrc, &sizec);
388 list = fdt_getprop(blob, node, "reg-names", &len);
393 cell = fdt_getprop(blob, node, "reg", &len);
397 for (idx = 0, info = &flash_info[0]; list < end;) {
398 addr = fdt_translate_address((void *)blob, node, cell + idx);
399 size = fdt_addr_to_cpu(cell[idx + addrc]);
401 if (!strncmp(list, "nvm", len)) {
403 nvm_regs_p = ioremap(addr, size);
404 } else if (!strncmp(list, "bank", 4)) {
405 /* Flash bank: use kseg0 cached address */
406 pic32_flash_bank_init(info, CKSEG0ADDR(addr), size);
409 idx += addrc + sizec;
413 /* disable flash write/erase operations */
414 writel(NVM_WREN, &nvm_regs_p->ctrl.clr);
416 #if (CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE)
417 /* monitor protection ON by default */
418 flash_protect(FLAG_PROTECT_SET,
419 CONFIG_SYS_MONITOR_BASE,
420 CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1,
424 #ifdef CONFIG_ENV_IS_IN_FLASH
425 /* ENV protection ON by default */
426 flash_protect(FLAG_PROTECT_SET,
428 CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1,
434 static const struct udevice_id pic32_flash_ids[] = {
435 { .compatible = "microchip,pic32mzda-flash" },
439 U_BOOT_DRIVER(pic32_flash) = {
440 .name = "pic32_flash",
442 .of_match = pic32_flash_ids,
443 .probe = pic32_flash_probe,