blob: 8fff818e42fc5fbe23f3629b563c5e3708a431c4 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Purna Chandra Mandal5c990452016-03-18 18:36:08 +05302/*
3 * Copyright (C) 2015
4 * Cristian Birsan <cristian.birsan@microchip.com>
5 * Purna Chandra Mandal <purna.mandal@microchip.com>
Purna Chandra Mandal5c990452016-03-18 18:36:08 +05306 */
7
8#include <common.h>
Simon Glass1eb69ae2019-11-14 12:57:39 -07009#include <cpu_func.h>
Purna Chandra Mandal5c990452016-03-18 18:36:08 +053010#include <dm.h>
11#include <fdt_support.h>
12#include <flash.h>
Simon Glass36bf4462019-11-14 12:57:42 -070013#include <irq_func.h>
Purna Chandra Mandal5c990452016-03-18 18:36:08 +053014#include <mach/pic32.h>
15#include <wait_bit.h>
16
17DECLARE_GLOBAL_DATA_PTR;
18
19/* NVM Controller registers */
20struct pic32_reg_nvm {
21 struct pic32_reg_atomic ctrl;
22 struct pic32_reg_atomic key;
23 struct pic32_reg_atomic addr;
24 struct pic32_reg_atomic data;
25};
26
27/* NVM operations */
28#define NVMOP_NOP 0
29#define NVMOP_WORD_WRITE 1
30#define NVMOP_PAGE_ERASE 4
31
32/* NVM control bits */
33#define NVM_WR BIT(15)
34#define NVM_WREN BIT(14)
35#define NVM_WRERR BIT(13)
36#define NVM_LVDERR BIT(12)
37
38/* NVM programming unlock register */
39#define LOCK_KEY 0x0
40#define UNLOCK_KEY1 0xaa996655
41#define UNLOCK_KEY2 0x556699aa
42
43/*
44 * PIC32 flash banks consist of number of pages, each page
45 * into number of rows and rows into number of words.
46 * Here we will maintain page information instead of sector.
47 */
48flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
49static struct pic32_reg_nvm *nvm_regs_p;
50
51static inline void flash_initiate_operation(u32 nvmop)
52{
53 /* set operation */
54 writel(nvmop, &nvm_regs_p->ctrl.raw);
55
56 /* enable flash write */
57 writel(NVM_WREN, &nvm_regs_p->ctrl.set);
58
59 /* unlock sequence */
60 writel(LOCK_KEY, &nvm_regs_p->key.raw);
61 writel(UNLOCK_KEY1, &nvm_regs_p->key.raw);
62 writel(UNLOCK_KEY2, &nvm_regs_p->key.raw);
63
64 /* initiate operation */
65 writel(NVM_WR, &nvm_regs_p->ctrl.set);
66}
67
68static int flash_wait_till_busy(const char *func, ulong timeout)
69{
Álvaro Fernández Rojas48263502018-01-23 17:14:55 +010070 int ret = wait_for_bit_le32(&nvm_regs_p->ctrl.raw,
71 NVM_WR, false, timeout, false);
Purna Chandra Mandal5c990452016-03-18 18:36:08 +053072
Mario Six9dbaebc2018-01-26 14:43:52 +010073 return ret ? ERR_TIMEOUT : ERR_OK;
Purna Chandra Mandal5c990452016-03-18 18:36:08 +053074}
75
76static inline int flash_complete_operation(void)
77{
78 u32 tmp;
79
80 tmp = readl(&nvm_regs_p->ctrl.raw);
81 if (tmp & NVM_WRERR) {
82 printf("Error in Block Erase - Lock Bit may be set!\n");
83 flash_initiate_operation(NVMOP_NOP);
84 return ERR_PROTECTED;
85 }
86
87 if (tmp & NVM_LVDERR) {
88 printf("Error in Block Erase - low-vol detected!\n");
89 flash_initiate_operation(NVMOP_NOP);
90 return ERR_NOT_ERASED;
91 }
92
93 /* disable flash write or erase operation */
94 writel(NVM_WREN, &nvm_regs_p->ctrl.clr);
95
96 return ERR_OK;
97}
98
99/*
100 * Erase flash sectors, returns:
101 * ERR_OK - OK
102 * ERR_INVAL - invalid sector arguments
Mario Six9dbaebc2018-01-26 14:43:52 +0100103 * ERR_TIMEOUT - write timeout
Purna Chandra Mandal5c990452016-03-18 18:36:08 +0530104 * ERR_NOT_ERASED - Flash not erased
105 * ERR_UNKNOWN_FLASH_VENDOR - incorrect flash
106 */
107int flash_erase(flash_info_t *info, int s_first, int s_last)
108{
109 ulong sect_start, sect_end, flags;
110 int prot, sect;
111 int rc;
112
113 if ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_MCHP) {
114 printf("Can't erase unknown flash type %08lx - aborted\n",
115 info->flash_id);
116 return ERR_UNKNOWN_FLASH_VENDOR;
117 }
118
119 if ((s_first < 0) || (s_first > s_last)) {
120 printf("- no sectors to erase\n");
121 return ERR_INVAL;
122 }
123
124 prot = 0;
125 for (sect = s_first; sect <= s_last; ++sect) {
126 if (info->protect[sect])
127 prot++;
128 }
129
130 if (prot)
131 printf("- Warning: %d protected sectors will not be erased!\n",
132 prot);
133 else
134 printf("\n");
135
136 /* erase on unprotected sectors */
137 for (sect = s_first; sect <= s_last; sect++) {
138 if (info->protect[sect])
139 continue;
140
141 /* disable interrupts */
142 flags = disable_interrupts();
143
144 /* write destination page address (physical) */
145 sect_start = CPHYSADDR(info->start[sect]);
146 writel(sect_start, &nvm_regs_p->addr.raw);
147
148 /* page erase */
149 flash_initiate_operation(NVMOP_PAGE_ERASE);
150
151 /* wait */
152 rc = flash_wait_till_busy(__func__,
153 CONFIG_SYS_FLASH_ERASE_TOUT);
154
155 /* re-enable interrupts if necessary */
156 if (flags)
157 enable_interrupts();
158
159 if (rc != ERR_OK)
160 return rc;
161
162 rc = flash_complete_operation();
163 if (rc != ERR_OK)
164 return rc;
165
166 /*
167 * flash content is updated but cache might contain stale
168 * data, so invalidate dcache.
169 */
170 sect_end = info->start[sect] + info->size / info->sector_count;
171 invalidate_dcache_range(info->start[sect], sect_end);
172 }
173
174 printf(" done\n");
175 return ERR_OK;
176}
177
178int page_erase(flash_info_t *info, int sect)
179{
180 return 0;
181}
182
183/* Write a word to flash */
184static int write_word(flash_info_t *info, ulong dest, ulong word)
185{
186 ulong flags;
187 int rc;
188
189 /* read flash to check if it is sufficiently erased */
190 if ((readl((void __iomem *)dest) & word) != word) {
191 printf("Error, Flash not erased!\n");
192 return ERR_NOT_ERASED;
193 }
194
195 /* disable interrupts */
196 flags = disable_interrupts();
197
198 /* update destination page address (physical) */
199 writel(CPHYSADDR(dest), &nvm_regs_p->addr.raw);
200 writel(word, &nvm_regs_p->data.raw);
201
202 /* word write */
203 flash_initiate_operation(NVMOP_WORD_WRITE);
204
205 /* wait for operation to complete */
206 rc = flash_wait_till_busy(__func__, CONFIG_SYS_FLASH_WRITE_TOUT);
207
208 /* re-enable interrupts if necessary */
209 if (flags)
210 enable_interrupts();
211
212 if (rc != ERR_OK)
213 return rc;
214
215 return flash_complete_operation();
216}
217
218/*
219 * Copy memory to flash, returns:
220 * ERR_OK - OK
Mario Six9dbaebc2018-01-26 14:43:52 +0100221 * ERR_TIMEOUT - write timeout
Purna Chandra Mandal5c990452016-03-18 18:36:08 +0530222 * ERR_NOT_ERASED - Flash not erased
223 */
224int write_buff(flash_info_t *info, uchar *src, ulong addr, ulong cnt)
225{
226 ulong dst, tmp_le, len = cnt;
227 int i, l, rc;
228 uchar *cp;
229
230 /* get lower word aligned address */
231 dst = (addr & ~3);
232
233 /* handle unaligned start bytes */
234 l = addr - dst;
235 if (l != 0) {
236 tmp_le = 0;
237 for (i = 0, cp = (uchar *)dst; i < l; ++i, ++cp)
238 tmp_le |= *cp << (i * 8);
239
240 for (; (i < 4) && (cnt > 0); ++i, ++src, --cnt, ++cp)
241 tmp_le |= *src << (i * 8);
242
243 for (; (cnt == 0) && (i < 4); ++i, ++cp)
244 tmp_le |= *cp << (i * 8);
245
246 rc = write_word(info, dst, tmp_le);
247 if (rc)
248 goto out;
249
250 dst += 4;
251 }
252
253 /* handle word aligned part */
254 while (cnt >= 4) {
255 tmp_le = src[0] | src[1] << 8 | src[2] << 16 | src[3] << 24;
256 rc = write_word(info, dst, tmp_le);
257 if (rc)
258 goto out;
259 src += 4;
260 dst += 4;
261 cnt -= 4;
262 }
263
264 if (cnt == 0) {
265 rc = ERR_OK;
266 goto out;
267 }
268
269 /* handle unaligned tail bytes */
270 tmp_le = 0;
271 for (i = 0, cp = (uchar *)dst; (i < 4) && (cnt > 0); ++i, ++cp) {
272 tmp_le |= *src++ << (i * 8);
273 --cnt;
274 }
275
276 for (; i < 4; ++i, ++cp)
277 tmp_le |= *cp << (i * 8);
278
279 rc = write_word(info, dst, tmp_le);
280out:
281 /*
282 * flash content updated by nvm controller but CPU cache might
283 * have stale data, so invalidate dcache.
284 */
285 invalidate_dcache_range(addr, addr + len);
286
287 printf(" done\n");
288 return rc;
289}
290
291void flash_print_info(flash_info_t *info)
292{
293 int i;
294
295 if (info->flash_id == FLASH_UNKNOWN) {
296 printf("missing or unknown FLASH type\n");
297 return;
298 }
299
300 switch (info->flash_id & FLASH_VENDMASK) {
301 case FLASH_MAN_MCHP:
302 printf("Microchip Technology ");
303 break;
304 default:
305 printf("Unknown Vendor ");
306 break;
307 }
308
309 switch (info->flash_id & FLASH_TYPEMASK) {
310 case FLASH_MCHP100T:
311 printf("Internal (8 Mbit, 64 x 16k)\n");
312 break;
313 default:
314 printf("Unknown Chip Type\n");
315 break;
316 }
317
318 printf(" Size: %ld MB in %d Sectors\n",
319 info->size >> 20, info->sector_count);
320
321 printf(" Sector Start Addresses:");
322 for (i = 0; i < info->sector_count; ++i) {
323 if ((i % 5) == 0)
324 printf("\n ");
325
326 printf(" %08lX%s", info->start[i],
327 info->protect[i] ? " (RO)" : " ");
328 }
329 printf("\n");
330}
331
332unsigned long flash_init(void)
333{
334 unsigned long size = 0;
335 struct udevice *dev;
336 int bank;
337
338 /* probe every MTD device */
339 for (uclass_first_device(UCLASS_MTD, &dev); dev;
340 uclass_next_device(&dev)) {
341 /* nop */
342 }
343
344 /* calc total flash size */
345 for (bank = 0; bank < CONFIG_SYS_MAX_FLASH_BANKS; ++bank)
346 size += flash_info[bank].size;
347
348 return size;
349}
350
351static void pic32_flash_bank_init(flash_info_t *info,
352 ulong base, ulong size)
353{
354 ulong sect_size;
355 int sect;
356
357 /* device & manufacturer code */
358 info->flash_id = FLASH_MAN_MCHP | FLASH_MCHP100T;
359 info->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
360 info->size = size;
361
362 /* update sector (i.e page) info */
363 sect_size = info->size / info->sector_count;
364 for (sect = 0; sect < info->sector_count; sect++) {
365 info->start[sect] = base;
366 /* protect each sector by default */
367 info->protect[sect] = 1;
368 base += sect_size;
369 }
370}
371
372static int pic32_flash_probe(struct udevice *dev)
373{
374 void *blob = (void *)gd->fdt_blob;
Simon Glasse160f7d2017-01-17 16:52:55 -0700375 int node = dev_of_offset(dev);
Purna Chandra Mandal5c990452016-03-18 18:36:08 +0530376 const char *list, *end;
377 const fdt32_t *cell;
378 unsigned long addr, size;
379 int parent, addrc, sizec;
380 flash_info_t *info;
381 int len, idx;
382
383 /*
384 * decode regs. there are multiple reg tuples, and they need to
385 * match with reg-names.
386 */
387 parent = fdt_parent_offset(blob, node);
Simon Glasseed36602017-05-18 20:09:26 -0600388 fdt_support_default_count_cells(blob, parent, &addrc, &sizec);
Purna Chandra Mandal5c990452016-03-18 18:36:08 +0530389 list = fdt_getprop(blob, node, "reg-names", &len);
390 if (!list)
391 return -ENOENT;
392
393 end = list + len;
394 cell = fdt_getprop(blob, node, "reg", &len);
395 if (!cell)
396 return -ENOENT;
397
398 for (idx = 0, info = &flash_info[0]; list < end;) {
399 addr = fdt_translate_address((void *)blob, node, cell + idx);
400 size = fdt_addr_to_cpu(cell[idx + addrc]);
401 len = strlen(list);
402 if (!strncmp(list, "nvm", len)) {
403 /* NVM controller */
404 nvm_regs_p = ioremap(addr, size);
405 } else if (!strncmp(list, "bank", 4)) {
406 /* Flash bank: use kseg0 cached address */
407 pic32_flash_bank_init(info, CKSEG0ADDR(addr), size);
408 info++;
409 }
410 idx += addrc + sizec;
411 list += len + 1;
412 }
413
414 /* disable flash write/erase operations */
415 writel(NVM_WREN, &nvm_regs_p->ctrl.clr);
416
417#if (CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE)
418 /* monitor protection ON by default */
419 flash_protect(FLAG_PROTECT_SET,
420 CONFIG_SYS_MONITOR_BASE,
421 CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1,
422 &flash_info[0]);
423#endif
424
425#ifdef CONFIG_ENV_IS_IN_FLASH
426 /* ENV protection ON by default */
427 flash_protect(FLAG_PROTECT_SET,
428 CONFIG_ENV_ADDR,
429 CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1,
430 &flash_info[0]);
431#endif
432 return 0;
433}
434
435static const struct udevice_id pic32_flash_ids[] = {
436 { .compatible = "microchip,pic32mzda-flash" },
437 {}
438};
439
440U_BOOT_DRIVER(pic32_flash) = {
441 .name = "pic32_flash",
442 .id = UCLASS_MTD,
443 .of_match = pic32_flash_ids,
444 .probe = pic32_flash_probe,
445};