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