1 // SPDX-License-Identifier: GPL-2.0+
4 * Cristian Birsan <cristian.birsan@microchip.com>
5 * Purna Chandra Mandal <purna.mandal@microchip.com>
10 #include <fdt_support.h>
12 #include <mach/pic32.h>
15 DECLARE_GLOBAL_DATA_PTR
;
17 /* NVM Controller registers */
18 struct pic32_reg_nvm
{
19 struct pic32_reg_atomic ctrl
;
20 struct pic32_reg_atomic key
;
21 struct pic32_reg_atomic addr
;
22 struct pic32_reg_atomic data
;
27 #define NVMOP_WORD_WRITE 1
28 #define NVMOP_PAGE_ERASE 4
30 /* NVM control bits */
31 #define NVM_WR BIT(15)
32 #define NVM_WREN BIT(14)
33 #define NVM_WRERR BIT(13)
34 #define NVM_LVDERR BIT(12)
36 /* NVM programming unlock register */
38 #define UNLOCK_KEY1 0xaa996655
39 #define UNLOCK_KEY2 0x556699aa
42 * PIC32 flash banks consist of number of pages, each page
43 * into number of rows and rows into number of words.
44 * Here we will maintain page information instead of sector.
46 flash_info_t flash_info
[CONFIG_SYS_MAX_FLASH_BANKS
];
47 static struct pic32_reg_nvm
*nvm_regs_p
;
49 static inline void flash_initiate_operation(u32 nvmop
)
52 writel(nvmop
, &nvm_regs_p
->ctrl
.raw
);
54 /* enable flash write */
55 writel(NVM_WREN
, &nvm_regs_p
->ctrl
.set
);
58 writel(LOCK_KEY
, &nvm_regs_p
->key
.raw
);
59 writel(UNLOCK_KEY1
, &nvm_regs_p
->key
.raw
);
60 writel(UNLOCK_KEY2
, &nvm_regs_p
->key
.raw
);
62 /* initiate operation */
63 writel(NVM_WR
, &nvm_regs_p
->ctrl
.set
);
66 static int flash_wait_till_busy(const char *func
, ulong timeout
)
68 int ret
= wait_for_bit_le32(&nvm_regs_p
->ctrl
.raw
,
69 NVM_WR
, false, timeout
, false);
71 return ret
? ERR_TIMEOUT
: ERR_OK
;
74 static inline int flash_complete_operation(void)
78 tmp
= readl(&nvm_regs_p
->ctrl
.raw
);
79 if (tmp
& NVM_WRERR
) {
80 printf("Error in Block Erase - Lock Bit may be set!\n");
81 flash_initiate_operation(NVMOP_NOP
);
85 if (tmp
& NVM_LVDERR
) {
86 printf("Error in Block Erase - low-vol detected!\n");
87 flash_initiate_operation(NVMOP_NOP
);
88 return ERR_NOT_ERASED
;
91 /* disable flash write or erase operation */
92 writel(NVM_WREN
, &nvm_regs_p
->ctrl
.clr
);
98 * Erase flash sectors, returns:
100 * ERR_INVAL - invalid sector arguments
101 * ERR_TIMEOUT - write timeout
102 * ERR_NOT_ERASED - Flash not erased
103 * ERR_UNKNOWN_FLASH_VENDOR - incorrect flash
105 int flash_erase(flash_info_t
*info
, int s_first
, int s_last
)
107 ulong sect_start
, sect_end
, flags
;
111 if ((info
->flash_id
& FLASH_VENDMASK
) != FLASH_MAN_MCHP
) {
112 printf("Can't erase unknown flash type %08lx - aborted\n",
114 return ERR_UNKNOWN_FLASH_VENDOR
;
117 if ((s_first
< 0) || (s_first
> s_last
)) {
118 printf("- no sectors to erase\n");
123 for (sect
= s_first
; sect
<= s_last
; ++sect
) {
124 if (info
->protect
[sect
])
129 printf("- Warning: %d protected sectors will not be erased!\n",
134 /* erase on unprotected sectors */
135 for (sect
= s_first
; sect
<= s_last
; sect
++) {
136 if (info
->protect
[sect
])
139 /* disable interrupts */
140 flags
= disable_interrupts();
142 /* write destination page address (physical) */
143 sect_start
= CPHYSADDR(info
->start
[sect
]);
144 writel(sect_start
, &nvm_regs_p
->addr
.raw
);
147 flash_initiate_operation(NVMOP_PAGE_ERASE
);
150 rc
= flash_wait_till_busy(__func__
,
151 CONFIG_SYS_FLASH_ERASE_TOUT
);
153 /* re-enable interrupts if necessary */
160 rc
= flash_complete_operation();
165 * flash content is updated but cache might contain stale
166 * data, so invalidate dcache.
168 sect_end
= info
->start
[sect
] + info
->size
/ info
->sector_count
;
169 invalidate_dcache_range(info
->start
[sect
], sect_end
);
176 int page_erase(flash_info_t
*info
, int sect
)
181 /* Write a word to flash */
182 static int write_word(flash_info_t
*info
, ulong dest
, ulong word
)
187 /* read flash to check if it is sufficiently erased */
188 if ((readl((void __iomem
*)dest
) & word
) != word
) {
189 printf("Error, Flash not erased!\n");
190 return ERR_NOT_ERASED
;
193 /* disable interrupts */
194 flags
= disable_interrupts();
196 /* update destination page address (physical) */
197 writel(CPHYSADDR(dest
), &nvm_regs_p
->addr
.raw
);
198 writel(word
, &nvm_regs_p
->data
.raw
);
201 flash_initiate_operation(NVMOP_WORD_WRITE
);
203 /* wait for operation to complete */
204 rc
= flash_wait_till_busy(__func__
, CONFIG_SYS_FLASH_WRITE_TOUT
);
206 /* re-enable interrupts if necessary */
213 return flash_complete_operation();
217 * Copy memory to flash, returns:
219 * ERR_TIMEOUT - write timeout
220 * ERR_NOT_ERASED - Flash not erased
222 int write_buff(flash_info_t
*info
, uchar
*src
, ulong addr
, ulong cnt
)
224 ulong dst
, tmp_le
, len
= cnt
;
228 /* get lower word aligned address */
231 /* handle unaligned start bytes */
235 for (i
= 0, cp
= (uchar
*)dst
; i
< l
; ++i
, ++cp
)
236 tmp_le
|= *cp
<< (i
* 8);
238 for (; (i
< 4) && (cnt
> 0); ++i
, ++src
, --cnt
, ++cp
)
239 tmp_le
|= *src
<< (i
* 8);
241 for (; (cnt
== 0) && (i
< 4); ++i
, ++cp
)
242 tmp_le
|= *cp
<< (i
* 8);
244 rc
= write_word(info
, dst
, tmp_le
);
251 /* handle word aligned part */
253 tmp_le
= src
[0] | src
[1] << 8 | src
[2] << 16 | src
[3] << 24;
254 rc
= write_word(info
, dst
, tmp_le
);
267 /* handle unaligned tail bytes */
269 for (i
= 0, cp
= (uchar
*)dst
; (i
< 4) && (cnt
> 0); ++i
, ++cp
) {
270 tmp_le
|= *src
++ << (i
* 8);
274 for (; i
< 4; ++i
, ++cp
)
275 tmp_le
|= *cp
<< (i
* 8);
277 rc
= write_word(info
, dst
, tmp_le
);
280 * flash content updated by nvm controller but CPU cache might
281 * have stale data, so invalidate dcache.
283 invalidate_dcache_range(addr
, addr
+ len
);
289 void flash_print_info(flash_info_t
*info
)
293 if (info
->flash_id
== FLASH_UNKNOWN
) {
294 printf("missing or unknown FLASH type\n");
298 switch (info
->flash_id
& FLASH_VENDMASK
) {
300 printf("Microchip Technology ");
303 printf("Unknown Vendor ");
307 switch (info
->flash_id
& FLASH_TYPEMASK
) {
309 printf("Internal (8 Mbit, 64 x 16k)\n");
312 printf("Unknown Chip Type\n");
316 printf(" Size: %ld MB in %d Sectors\n",
317 info
->size
>> 20, info
->sector_count
);
319 printf(" Sector Start Addresses:");
320 for (i
= 0; i
< info
->sector_count
; ++i
) {
324 printf(" %08lX%s", info
->start
[i
],
325 info
->protect
[i
] ? " (RO)" : " ");
330 unsigned long flash_init(void)
332 unsigned long size
= 0;
336 /* probe every MTD device */
337 for (uclass_first_device(UCLASS_MTD
, &dev
); dev
;
338 uclass_next_device(&dev
)) {
342 /* calc total flash size */
343 for (bank
= 0; bank
< CONFIG_SYS_MAX_FLASH_BANKS
; ++bank
)
344 size
+= flash_info
[bank
].size
;
349 static void pic32_flash_bank_init(flash_info_t
*info
,
350 ulong base
, ulong size
)
355 /* device & manufacturer code */
356 info
->flash_id
= FLASH_MAN_MCHP
| FLASH_MCHP100T
;
357 info
->sector_count
= CONFIG_SYS_MAX_FLASH_SECT
;
360 /* update sector (i.e page) info */
361 sect_size
= info
->size
/ info
->sector_count
;
362 for (sect
= 0; sect
< info
->sector_count
; sect
++) {
363 info
->start
[sect
] = base
;
364 /* protect each sector by default */
365 info
->protect
[sect
] = 1;
370 static int pic32_flash_probe(struct udevice
*dev
)
372 void *blob
= (void *)gd
->fdt_blob
;
373 int node
= dev_of_offset(dev
);
374 const char *list
, *end
;
376 unsigned long addr
, size
;
377 int parent
, addrc
, sizec
;
382 * decode regs. there are multiple reg tuples, and they need to
383 * match with reg-names.
385 parent
= fdt_parent_offset(blob
, node
);
386 fdt_support_default_count_cells(blob
, parent
, &addrc
, &sizec
);
387 list
= fdt_getprop(blob
, node
, "reg-names", &len
);
392 cell
= fdt_getprop(blob
, node
, "reg", &len
);
396 for (idx
= 0, info
= &flash_info
[0]; list
< end
;) {
397 addr
= fdt_translate_address((void *)blob
, node
, cell
+ idx
);
398 size
= fdt_addr_to_cpu(cell
[idx
+ addrc
]);
400 if (!strncmp(list
, "nvm", len
)) {
402 nvm_regs_p
= ioremap(addr
, size
);
403 } else if (!strncmp(list
, "bank", 4)) {
404 /* Flash bank: use kseg0 cached address */
405 pic32_flash_bank_init(info
, CKSEG0ADDR(addr
), size
);
408 idx
+= addrc
+ sizec
;
412 /* disable flash write/erase operations */
413 writel(NVM_WREN
, &nvm_regs_p
->ctrl
.clr
);
415 #if (CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE)
416 /* monitor protection ON by default */
417 flash_protect(FLAG_PROTECT_SET
,
418 CONFIG_SYS_MONITOR_BASE
,
419 CONFIG_SYS_MONITOR_BASE
+ monitor_flash_len
- 1,
423 #ifdef CONFIG_ENV_IS_IN_FLASH
424 /* ENV protection ON by default */
425 flash_protect(FLAG_PROTECT_SET
,
427 CONFIG_ENV_ADDR
+ CONFIG_ENV_SECT_SIZE
- 1,
433 static const struct udevice_id pic32_flash_ids
[] = {
434 { .compatible
= "microchip,pic32mzda-flash" },
438 U_BOOT_DRIVER(pic32_flash
) = {
439 .name
= "pic32_flash",
441 .of_match
= pic32_flash_ids
,
442 .probe
= pic32_flash_probe
,