32278b37785570a8ad870ea36fbfbe426c72e62e
[openwrt/openwrt.git] / target / linux / brcm-2.4 / files / arch / mips / bcm947xx / nvram_linux.c
1 /*
2 * NVRAM variable manipulation (Linux kernel half)
3 *
4 * Copyright 2006, Broadcom Corporation
5 * All Rights Reserved.
6 *
7 * THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
8 * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
9 * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
10 * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
11 *
12 * $Id: nvram_linux.c,v 1.19 2006/04/08 07:12:42 honor Exp $
13 */
14
15 #include <linux/config.h>
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/string.h>
20 #include <linux/interrupt.h>
21 #include <linux/spinlock.h>
22 #include <linux/slab.h>
23 #include <linux/bootmem.h>
24 #include <linux/wrapper.h>
25 #include <linux/fs.h>
26 #include <linux/miscdevice.h>
27 #include <linux/mtd/mtd.h>
28 #include <asm/addrspace.h>
29 #include <asm/io.h>
30 #include <asm/uaccess.h>
31
32 #include <typedefs.h>
33 #include <osl.h>
34 #include <bcmendian.h>
35 #include <bcmnvram.h>
36 #include <sbconfig.h>
37 #include <sbchipc.h>
38 #include <sbutils.h>
39 #include <hndmips.h>
40 #include <sflash.h>
41
42 /* In BSS to minimize text size and page aligned so it can be mmap()-ed */
43 static char nvram_buf[NVRAM_SPACE] __attribute__((aligned(PAGE_SIZE)));
44
45 #ifdef MODULE
46
47 #define early_nvram_get(name) nvram_get(name)
48
49 #else /* !MODULE */
50
51 /* Global SB handle */
52 extern void *bcm947xx_sbh;
53 extern spinlock_t bcm947xx_sbh_lock;
54
55 static int cfe_env;
56 extern char *cfe_env_get(char *nv_buf, const char *name);
57
58 /* Convenience */
59 #define sbh bcm947xx_sbh
60 #define sbh_lock bcm947xx_sbh_lock
61 #define KB * 1024
62 #define MB * 1024 * 1024
63
64 /* Probe for NVRAM header */
65 static void __init
66 early_nvram_init(void)
67 {
68 struct nvram_header *header;
69 chipcregs_t *cc;
70 struct sflash *info = NULL;
71 int i;
72 uint32 base, off, lim;
73 u32 *src, *dst;
74
75 if ((cc = sb_setcore(sbh, SB_CC, 0)) != NULL) {
76 base = KSEG1ADDR(SB_FLASH2);
77 switch (readl(&cc->capabilities) & CC_CAP_FLASH_MASK) {
78 case PFLASH:
79 lim = SB_FLASH2_SZ;
80 break;
81
82 case SFLASH_ST:
83 case SFLASH_AT:
84 if ((info = sflash_init(sbh,cc)) == NULL)
85 return;
86 lim = info->size;
87 break;
88
89 case FLASH_NONE:
90 default:
91 return;
92 }
93 } else {
94 /* extif assumed, Stop at 4 MB */
95 base = KSEG1ADDR(SB_FLASH1);
96 lim = SB_FLASH1_SZ;
97 }
98
99 /* XXX: hack for supporting the CFE environment stuff on WGT634U */
100 src = (u32 *) KSEG1ADDR(base + 8 * 1024 * 1024 - 0x2000);
101 dst = (u32 *) nvram_buf;
102 if ((lim == 0x02000000) && ((*src & 0xff00ff) == 0x000001)) {
103 printk("early_nvram_init: WGT634U NVRAM found.\n");
104
105 for (i = 0; i < 0x1ff0; i++) {
106 if (*src == 0xFFFFFFFF)
107 break;
108 *dst++ = *src++;
109 }
110 cfe_env = 1;
111 return;
112 }
113
114 off = FLASH_MIN;
115 while (off <= lim) {
116 /* Windowed flash access */
117 header = (struct nvram_header *) KSEG1ADDR(base + off - NVRAM_SPACE);
118 if (header->magic == NVRAM_MAGIC)
119 goto found;
120 off <<= 1;
121 }
122
123 /* Try embedded NVRAM at 4 KB and 1 KB as last resorts */
124 header = (struct nvram_header *) KSEG1ADDR(base + 4 KB);
125 if (header->magic == NVRAM_MAGIC)
126 goto found;
127
128 header = (struct nvram_header *) KSEG1ADDR(base + 1 KB);
129 if (header->magic == NVRAM_MAGIC)
130 goto found;
131
132 printk("early_nvram_init: NVRAM not found\n");
133 return;
134
135 found:
136 src = (u32 *) header;
137 dst = (u32 *) nvram_buf;
138 for (i = 0; i < sizeof(struct nvram_header); i += 4)
139 *dst++ = *src++;
140 for (; i < header->len && i < NVRAM_SPACE; i += 4)
141 *dst++ = ltoh32(*src++);
142 }
143
144 /* Early (before mm or mtd) read-only access to NVRAM */
145 static char * __init
146 early_nvram_get(const char *name)
147 {
148 char *var, *value, *end, *eq;
149
150 if (!name)
151 return NULL;
152
153 /* Too early? */
154 if (sbh == NULL)
155 return NULL;
156
157 if (!nvram_buf[0])
158 early_nvram_init();
159
160 if (cfe_env)
161 return cfe_env_get(nvram_buf, name);
162
163 /* Look for name=value and return value */
164 var = &nvram_buf[sizeof(struct nvram_header)];
165 end = nvram_buf + sizeof(nvram_buf) - 2;
166 end[0] = end[1] = '\0';
167 for (; *var; var = value + strlen(value) + 1) {
168 if (!(eq = strchr(var, '=')))
169 break;
170 value = eq + 1;
171 if ((eq - var) == strlen(name) && strncmp(var, name, (eq - var)) == 0)
172 return value;
173 }
174
175 return NULL;
176 }
177
178 static int __init
179 early_nvram_getall(char *buf, int count)
180 {
181 char *var, *end;
182 int len = 0;
183
184 /* Too early? */
185 if (sbh == NULL)
186 return -1;
187
188 if (!nvram_buf[0])
189 early_nvram_init();
190
191 bzero(buf, count);
192
193 /* Write name=value\0 ... \0\0 */
194 var = &nvram_buf[sizeof(struct nvram_header)];
195 end = nvram_buf + sizeof(nvram_buf) - 2;
196 end[0] = end[1] = '\0';
197 for (; *var; var += strlen(var) + 1) {
198 if ((count - len) <= (strlen(var) + 1))
199 break;
200 len += sprintf(buf + len, "%s", var) + 1;
201 }
202
203 return 0;
204 }
205 #endif /* !MODULE */
206
207 extern char * _nvram_get(const char *name);
208 extern int _nvram_set(const char *name, const char *value);
209 extern int _nvram_unset(const char *name);
210 extern int _nvram_getall(char *buf, int count);
211 extern int _nvram_commit(struct nvram_header *header);
212 extern int _nvram_init(void *sbh);
213 extern void _nvram_exit(void);
214
215 /* Globals */
216 static spinlock_t nvram_lock = SPIN_LOCK_UNLOCKED;
217 static struct semaphore nvram_sem;
218 static unsigned long nvram_offset = 0;
219 static int nvram_major = -1;
220 static devfs_handle_t nvram_handle = NULL;
221 static struct mtd_info *nvram_mtd = NULL;
222
223 int
224 _nvram_read(char *buf)
225 {
226 struct nvram_header *header = (struct nvram_header *) buf;
227 size_t len;
228
229 if (!nvram_mtd ||
230 MTD_READ(nvram_mtd, nvram_mtd->size - NVRAM_SPACE, NVRAM_SPACE, &len, buf) ||
231 len != NVRAM_SPACE ||
232 header->magic != NVRAM_MAGIC) {
233 /* Maybe we can recover some data from early initialization */
234 memcpy(buf, nvram_buf, NVRAM_SPACE);
235 }
236
237 return 0;
238 }
239
240 struct nvram_tuple *
241 _nvram_realloc(struct nvram_tuple *t, const char *name, const char *value)
242 {
243 if ((nvram_offset + strlen(value) + 1) > NVRAM_SPACE)
244 return NULL;
245
246 if (!t) {
247 if (!(t = kmalloc(sizeof(struct nvram_tuple) + strlen(name) + 1, GFP_ATOMIC)))
248 return NULL;
249
250 /* Copy name */
251 t->name = (char *) &t[1];
252 strcpy(t->name, name);
253
254 t->value = NULL;
255 }
256
257 /* Copy value */
258 if (!t->value || strcmp(t->value, value)) {
259 t->value = &nvram_buf[nvram_offset];
260 strcpy(t->value, value);
261 nvram_offset += strlen(value) + 1;
262 }
263
264 return t;
265 }
266
267 void
268 _nvram_free(struct nvram_tuple *t)
269 {
270 if (!t)
271 nvram_offset = 0;
272 else
273 kfree(t);
274 }
275
276 int
277 nvram_set(const char *name, const char *value)
278 {
279 unsigned long flags;
280 int ret;
281 struct nvram_header *header;
282
283 spin_lock_irqsave(&nvram_lock, flags);
284 if ((ret = _nvram_set(name, value))) {
285 /* Consolidate space and try again */
286 if ((header = kmalloc(NVRAM_SPACE, GFP_ATOMIC))) {
287 if (_nvram_commit(header) == 0)
288 ret = _nvram_set(name, value);
289 kfree(header);
290 }
291 }
292 spin_unlock_irqrestore(&nvram_lock, flags);
293
294 return ret;
295 }
296
297 char *
298 real_nvram_get(const char *name)
299 {
300 unsigned long flags;
301 char *value;
302
303 spin_lock_irqsave(&nvram_lock, flags);
304 value = _nvram_get(name);
305 spin_unlock_irqrestore(&nvram_lock, flags);
306
307 return value;
308 }
309
310 char *
311 nvram_get(const char *name)
312 {
313 if (nvram_major >= 0)
314 return real_nvram_get(name);
315 else
316 return early_nvram_get(name);
317 }
318
319 int
320 nvram_unset(const char *name)
321 {
322 unsigned long flags;
323 int ret;
324
325 spin_lock_irqsave(&nvram_lock, flags);
326 ret = _nvram_unset(name);
327 spin_unlock_irqrestore(&nvram_lock, flags);
328
329 return ret;
330 }
331
332 static void
333 erase_callback(struct erase_info *done)
334 {
335 wait_queue_head_t *wait_q = (wait_queue_head_t *) done->priv;
336 wake_up(wait_q);
337 }
338
339 int
340 nvram_commit(void)
341 {
342 char *buf;
343 size_t erasesize, len, magic_len;
344 unsigned int i;
345 int ret;
346 struct nvram_header *header;
347 unsigned long flags;
348 u_int32_t offset;
349 DECLARE_WAITQUEUE(wait, current);
350 wait_queue_head_t wait_q;
351 struct erase_info erase;
352 u_int32_t magic_offset = 0; /* Offset for writing MAGIC # */
353
354 if (!nvram_mtd) {
355 printk("nvram_commit: NVRAM not found\n");
356 return -ENODEV;
357 }
358
359 if (in_interrupt()) {
360 printk("nvram_commit: not committing in interrupt\n");
361 return -EINVAL;
362 }
363
364 /* Backup sector blocks to be erased */
365 erasesize = ROUNDUP(NVRAM_SPACE, nvram_mtd->erasesize);
366 if (!(buf = kmalloc(erasesize, GFP_KERNEL))) {
367 printk("nvram_commit: out of memory\n");
368 return -ENOMEM;
369 }
370
371 down(&nvram_sem);
372
373 if ((i = erasesize - NVRAM_SPACE) > 0) {
374 offset = nvram_mtd->size - erasesize;
375 len = 0;
376 ret = MTD_READ(nvram_mtd, offset, i, &len, buf);
377 if (ret || len != i) {
378 printk("nvram_commit: read error ret = %d, len = %d/%d\n", ret, len, i);
379 ret = -EIO;
380 goto done;
381 }
382 header = (struct nvram_header *)(buf + i);
383 magic_offset = i + ((void *)&header->magic - (void *)header);
384 } else {
385 offset = nvram_mtd->size - NVRAM_SPACE;
386 magic_offset = ((void *)&header->magic - (void *)header);
387 header = (struct nvram_header *)buf;
388 }
389
390 /* clear the existing magic # to mark the NVRAM as unusable
391 we can pull MAGIC bits low without erase */
392 header->magic = NVRAM_CLEAR_MAGIC; /* All zeros magic */
393
394 /* Unlock sector blocks (for Intel 28F320C3B flash) , 20060309 */
395 if(nvram_mtd->unlock)
396 nvram_mtd->unlock(nvram_mtd, offset, nvram_mtd->erasesize);
397
398 ret = MTD_WRITE(nvram_mtd, offset + magic_offset, sizeof(header->magic),
399 &magic_len, (char *)&header->magic);
400 if (ret || magic_len != sizeof(header->magic)) {
401 printk("nvram_commit: clear MAGIC error\n");
402 ret = -EIO;
403 goto done;
404 }
405
406 header->magic = NVRAM_MAGIC; /* reset MAGIC before we regenerate the NVRAM,
407 otherwise we'll have an incorrect CRC */
408 /* Regenerate NVRAM */
409 spin_lock_irqsave(&nvram_lock, flags);
410 ret = _nvram_commit(header);
411 spin_unlock_irqrestore(&nvram_lock, flags);
412 if (ret)
413 goto done;
414
415 /* Erase sector blocks */
416 init_waitqueue_head(&wait_q);
417 for (; offset < nvram_mtd->size - NVRAM_SPACE + header->len; offset += nvram_mtd->erasesize) {
418 erase.mtd = nvram_mtd;
419 erase.addr = offset;
420 erase.len = nvram_mtd->erasesize;
421 erase.callback = erase_callback;
422 erase.priv = (u_long) &wait_q;
423
424 set_current_state(TASK_INTERRUPTIBLE);
425 add_wait_queue(&wait_q, &wait);
426
427 /* Unlock sector blocks */
428 if (nvram_mtd->unlock)
429 nvram_mtd->unlock(nvram_mtd, offset, nvram_mtd->erasesize);
430
431 if ((ret = MTD_ERASE(nvram_mtd, &erase))) {
432 set_current_state(TASK_RUNNING);
433 remove_wait_queue(&wait_q, &wait);
434 printk("nvram_commit: erase error\n");
435 goto done;
436 }
437
438 /* Wait for erase to finish */
439 schedule();
440 remove_wait_queue(&wait_q, &wait);
441 }
442
443 /* Write partition up to end of data area */
444 header->magic = NVRAM_INVALID_MAGIC; /* All ones magic */
445 offset = nvram_mtd->size - erasesize;
446 i = erasesize - NVRAM_SPACE + header->len;
447 ret = MTD_WRITE(nvram_mtd, offset, i, &len, buf);
448 if (ret || len != i) {
449 printk("nvram_commit: write error\n");
450 ret = -EIO;
451 goto done;
452 }
453
454 /* Now mark the NVRAM in flash as "valid" by setting the correct
455 MAGIC # */
456 header->magic = NVRAM_MAGIC;
457 ret = MTD_WRITE(nvram_mtd, offset + magic_offset, sizeof(header->magic),
458 &magic_len, (char *)&header->magic);
459 if (ret || magic_len != sizeof(header->magic)) {
460 printk("nvram_commit: write MAGIC error\n");
461 ret = -EIO;
462 goto done;
463 }
464
465 /*
466 * Reading a few bytes back here will put the device
467 * back to the correct mode on certain flashes */
468 offset = nvram_mtd->size - erasesize;
469 ret = MTD_READ(nvram_mtd, offset, 4, &len, buf);
470
471 done:
472 up(&nvram_sem);
473 kfree(buf);
474
475 return ret;
476 }
477
478 int
479 nvram_getall(char *buf, int count)
480 {
481 unsigned long flags;
482 int ret;
483
484 spin_lock_irqsave(&nvram_lock, flags);
485 if (nvram_major >= 0)
486 ret = _nvram_getall(buf, count);
487 else
488 ret = early_nvram_getall(buf, count);
489 spin_unlock_irqrestore(&nvram_lock, flags);
490
491 return ret;
492 }
493
494
495
496
497
498
499
500 /* User mode interface below */
501
502 static ssize_t
503 dev_nvram_read(struct file *file, char *buf, size_t count, loff_t *ppos)
504 {
505 char tmp[100], *name = tmp, *value;
506 ssize_t ret;
507 unsigned long off;
508
509 if (count > sizeof(tmp)) {
510 if (!(name = kmalloc(count, GFP_KERNEL)))
511 return -ENOMEM;
512 }
513
514 if (copy_from_user(name, buf, count)) {
515 ret = -EFAULT;
516 goto done;
517 }
518
519 if (*name == '\0') {
520 /* Get all variables */
521 ret = nvram_getall(name, count);
522 if (ret == 0) {
523 if (copy_to_user(buf, name, count)) {
524 ret = -EFAULT;
525 goto done;
526 }
527 ret = count;
528 }
529 } else {
530 if (!(value = nvram_get(name))) {
531 ret = 0;
532 goto done;
533 }
534
535 /* Provide the offset into mmap() space */
536 off = (unsigned long) value - (unsigned long) nvram_buf;
537
538 if (put_user(off, (unsigned long *) buf)) {
539 ret = -EFAULT;
540 goto done;
541 }
542
543 ret = sizeof(unsigned long);
544 }
545
546 flush_cache_all();
547
548 done:
549 if (name != tmp)
550 kfree(name);
551
552 return ret;
553 }
554
555 static ssize_t
556 dev_nvram_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
557 {
558 char tmp[100], *name = tmp, *value;
559 ssize_t ret;
560
561 if (count > sizeof(tmp)) {
562 if (!(name = kmalloc(count, GFP_KERNEL)))
563 return -ENOMEM;
564 }
565
566 if (copy_from_user(name, buf, count)) {
567 ret = -EFAULT;
568 goto done;
569 }
570
571 value = name;
572 name = strsep(&value, "=");
573 if (value)
574 ret = nvram_set(name, value) ? : count;
575 else
576 ret = nvram_unset(name) ? : count;
577
578 done:
579 if (name != tmp)
580 kfree(name);
581
582 return ret;
583 }
584
585 static int
586 dev_nvram_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
587 {
588 if (cmd != NVRAM_MAGIC)
589 return -EINVAL;
590
591 return nvram_commit();
592 }
593
594 static int
595 dev_nvram_mmap(struct file *file, struct vm_area_struct *vma)
596 {
597 unsigned long offset = virt_to_phys(nvram_buf);
598
599 if (remap_page_range(vma->vm_start, offset, vma->vm_end-vma->vm_start,
600 vma->vm_page_prot))
601 return -EAGAIN;
602
603 return 0;
604 }
605
606 static int
607 dev_nvram_open(struct inode *inode, struct file * file)
608 {
609 MOD_INC_USE_COUNT;
610 return 0;
611 }
612
613 static int
614 dev_nvram_release(struct inode *inode, struct file * file)
615 {
616 MOD_DEC_USE_COUNT;
617 return 0;
618 }
619
620 static struct file_operations dev_nvram_fops = {
621 owner: THIS_MODULE,
622 open: dev_nvram_open,
623 release: dev_nvram_release,
624 read: dev_nvram_read,
625 write: dev_nvram_write,
626 ioctl: dev_nvram_ioctl,
627 mmap: dev_nvram_mmap,
628 };
629
630 static void
631 dev_nvram_exit(void)
632 {
633 int order = 0;
634 struct page *page, *end;
635
636 if (nvram_handle)
637 devfs_unregister(nvram_handle);
638
639 if (nvram_major >= 0)
640 devfs_unregister_chrdev(nvram_major, "nvram");
641
642 if (nvram_mtd)
643 put_mtd_device(nvram_mtd);
644
645 while ((PAGE_SIZE << order) < NVRAM_SPACE)
646 order++;
647 end = virt_to_page(nvram_buf + (PAGE_SIZE << order) - 1);
648 for (page = virt_to_page(nvram_buf); page <= end; page++)
649 mem_map_unreserve(page);
650
651 _nvram_exit();
652 }
653
654 static int __init
655 dev_nvram_init(void)
656 {
657 int order = 0, ret = 0;
658 struct page *page, *end;
659 unsigned int i;
660
661 /* Allocate and reserve memory to mmap() */
662 while ((PAGE_SIZE << order) < NVRAM_SPACE)
663 order++;
664 end = virt_to_page(nvram_buf + (PAGE_SIZE << order) - 1);
665 for (page = virt_to_page(nvram_buf); page <= end; page++)
666 mem_map_reserve(page);
667
668 #ifdef CONFIG_MTD
669 /* Find associated MTD device */
670 for (i = 0; i < MAX_MTD_DEVICES; i++) {
671 nvram_mtd = get_mtd_device(NULL, i);
672 if (nvram_mtd) {
673 if (!strcmp(nvram_mtd->name, "nvram") &&
674 nvram_mtd->size >= NVRAM_SPACE)
675 break;
676 put_mtd_device(nvram_mtd);
677 }
678 }
679 if (i >= MAX_MTD_DEVICES)
680 nvram_mtd = NULL;
681 #endif
682
683 /* Initialize hash table lock */
684 spin_lock_init(&nvram_lock);
685
686 /* Initialize commit semaphore */
687 init_MUTEX(&nvram_sem);
688
689 /* Register char device */
690 if ((nvram_major = devfs_register_chrdev(0, "nvram", &dev_nvram_fops)) < 0) {
691 ret = nvram_major;
692 goto err;
693 }
694
695 /* Initialize hash table */
696 _nvram_init(sbh);
697
698 /* Create /dev/nvram handle */
699 nvram_handle = devfs_register(NULL, "nvram", DEVFS_FL_NONE, nvram_major, 0,
700 S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP, &dev_nvram_fops, NULL);
701
702 /* Set the SDRAM NCDL value into NVRAM if not already done */
703 if (getintvar(NULL, "sdram_ncdl") == 0) {
704 unsigned int ncdl;
705 char buf[] = "0x00000000";
706
707 if ((ncdl = sb_memc_get_ncdl(sbh))) {
708 sprintf(buf, "0x%08x", ncdl);
709 nvram_set("sdram_ncdl", buf);
710 nvram_commit();
711 }
712 }
713
714 return 0;
715
716 err:
717 dev_nvram_exit();
718 return ret;
719 }
720
721 module_init(dev_nvram_init);
722 module_exit(dev_nvram_exit);