2 * NVRAM variable manipulation (Linux kernel half)
4 * Copyright 2006, Broadcom Corporation
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.
12 * $Id: nvram_linux.c,v 1.19 2006/04/08 07:12:42 honor Exp $
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>
26 #include <linux/miscdevice.h>
27 #include <linux/mtd/mtd.h>
28 #include <asm/addrspace.h>
30 #include <asm/uaccess.h>
34 #include <bcmendian.h>
43 /* In BSS to minimize text size and page aligned so it can be mmap()-ed */
44 static char nvram_buf
[NVRAM_SPACE
] __attribute__((aligned(PAGE_SIZE
)));
48 #define early_nvram_get(name) nvram_get(name)
52 /* Global SB handle */
53 extern void *bcm947xx_sbh
;
54 extern spinlock_t bcm947xx_sbh_lock
;
57 #define sbh bcm947xx_sbh
58 #define sbh_lock bcm947xx_sbh_lock
60 #define MB * 1024 * 1024
62 /* Probe for NVRAM header */
64 early_nvram_init(void)
66 struct nvram_header
*header
;
68 struct sflash
*info
= NULL
;
70 uint32 base
, off
, lim
;
73 if ((cc
= sb_setcore(sbh
, SB_CC
, 0)) != NULL
) {
74 base
= KSEG1ADDR(SB_FLASH2
);
75 switch (readl(&cc
->capabilities
) & CAP_FLASH_MASK
) {
82 if ((info
= sflash_init(cc
)) == NULL
)
92 /* extif assumed, Stop at 4 MB */
93 base
= KSEG1ADDR(SB_FLASH1
);
97 /* XXX: hack for supporting the CFE environment stuff on WGT634U */
98 src
= (u32
*) KSEG1ADDR(base
+ 8 * 1024 * 1024 - 0x2000);
99 dst
= (u32
*) nvram_buf
;
100 if ((lim
== 0x02000000) && ((*src
& 0xff00ff) == 0x000001)) {
101 printk("early_nvram_init: WGT634U NVRAM found.\n");
103 for (i
= 0; i
< 0x1ff0; i
++) {
104 if (*src
== 0xFFFFFFFF)
113 /* Windowed flash access */
114 header
= (struct nvram_header
*) KSEG1ADDR(base
+ off
- NVRAM_SPACE
);
115 if (header
->magic
== NVRAM_MAGIC
)
120 /* Try embedded NVRAM at 4 KB and 1 KB as last resorts */
121 header
= (struct nvram_header
*) KSEG1ADDR(base
+ 4 KB
);
122 if (header
->magic
== NVRAM_MAGIC
)
125 header
= (struct nvram_header
*) KSEG1ADDR(base
+ 1 KB
);
126 if (header
->magic
== NVRAM_MAGIC
)
129 printk("early_nvram_init: NVRAM not found\n");
133 src
= (u32
*) header
;
134 dst
= (u32
*) nvram_buf
;
135 for (i
= 0; i
< sizeof(struct nvram_header
); i
+= 4)
137 for (; i
< header
->len
&& i
< NVRAM_SPACE
; i
+= 4)
138 *dst
++ = ltoh32(*src
++);
141 /* Early (before mm or mtd) read-only access to NVRAM */
143 early_nvram_get(const char *name
)
145 char *var
, *value
, *end
, *eq
;
157 /* Look for name=value and return value */
158 var
= &nvram_buf
[sizeof(struct nvram_header
)];
159 end
= nvram_buf
+ sizeof(nvram_buf
) - 2;
160 end
[0] = end
[1] = '\0';
161 for (; *var
; var
= value
+ strlen(value
) + 1) {
162 if (!(eq
= strchr(var
, '=')))
165 if ((eq
- var
) == strlen(name
) && strncmp(var
, name
, (eq
- var
)) == 0)
173 early_nvram_getall(char *buf
, int count
)
187 /* Write name=value\0 ... \0\0 */
188 var
= &nvram_buf
[sizeof(struct nvram_header
)];
189 end
= nvram_buf
+ sizeof(nvram_buf
) - 2;
190 end
[0] = end
[1] = '\0';
191 for (; *var
; var
+= strlen(var
) + 1) {
192 if ((count
- len
) <= (strlen(var
) + 1))
194 len
+= sprintf(buf
+ len
, "%s", var
) + 1;
201 extern char * _nvram_get(const char *name
);
202 extern int _nvram_set(const char *name
, const char *value
);
203 extern int _nvram_unset(const char *name
);
204 extern int _nvram_getall(char *buf
, int count
);
205 extern int _nvram_commit(struct nvram_header
*header
);
206 extern int _nvram_init(void *sbh
);
207 extern void _nvram_exit(void);
210 static spinlock_t nvram_lock
= SPIN_LOCK_UNLOCKED
;
211 static struct semaphore nvram_sem
;
212 static unsigned long nvram_offset
= 0;
213 static int nvram_major
= -1;
214 static devfs_handle_t nvram_handle
= NULL
;
215 static struct mtd_info
*nvram_mtd
= NULL
;
218 _nvram_read(char *buf
)
220 struct nvram_header
*header
= (struct nvram_header
*) buf
;
224 MTD_READ(nvram_mtd
, nvram_mtd
->size
- NVRAM_SPACE
, NVRAM_SPACE
, &len
, buf
) ||
225 len
!= NVRAM_SPACE
||
226 header
->magic
!= NVRAM_MAGIC
) {
227 /* Maybe we can recover some data from early initialization */
228 memcpy(buf
, nvram_buf
, NVRAM_SPACE
);
235 _nvram_realloc(struct nvram_tuple
*t
, const char *name
, const char *value
)
237 if ((nvram_offset
+ strlen(value
) + 1) > NVRAM_SPACE
)
241 if (!(t
= kmalloc(sizeof(struct nvram_tuple
) + strlen(name
) + 1, GFP_ATOMIC
)))
245 t
->name
= (char *) &t
[1];
246 strcpy(t
->name
, name
);
252 if (!t
->value
|| strcmp(t
->value
, value
)) {
253 t
->value
= &nvram_buf
[nvram_offset
];
254 strcpy(t
->value
, value
);
255 nvram_offset
+= strlen(value
) + 1;
262 _nvram_free(struct nvram_tuple
*t
)
271 nvram_set(const char *name
, const char *value
)
275 struct nvram_header
*header
;
277 spin_lock_irqsave(&nvram_lock
, flags
);
278 if ((ret
= _nvram_set(name
, value
))) {
279 /* Consolidate space and try again */
280 if ((header
= kmalloc(NVRAM_SPACE
, GFP_ATOMIC
))) {
281 if (_nvram_commit(header
) == 0)
282 ret
= _nvram_set(name
, value
);
286 spin_unlock_irqrestore(&nvram_lock
, flags
);
292 real_nvram_get(const char *name
)
297 spin_lock_irqsave(&nvram_lock
, flags
);
298 value
= _nvram_get(name
);
299 spin_unlock_irqrestore(&nvram_lock
, flags
);
305 nvram_get(const char *name
)
307 if (nvram_major
>= 0)
308 return real_nvram_get(name
);
310 return early_nvram_get(name
);
314 nvram_unset(const char *name
)
319 spin_lock_irqsave(&nvram_lock
, flags
);
320 ret
= _nvram_unset(name
);
321 spin_unlock_irqrestore(&nvram_lock
, flags
);
327 erase_callback(struct erase_info
*done
)
329 wait_queue_head_t
*wait_q
= (wait_queue_head_t
*) done
->priv
;
337 size_t erasesize
, len
, magic_len
;
340 struct nvram_header
*header
;
343 DECLARE_WAITQUEUE(wait
, current
);
344 wait_queue_head_t wait_q
;
345 struct erase_info erase
;
346 u_int32_t magic_offset
= 0; /* Offset for writing MAGIC # */
349 printk("nvram_commit: NVRAM not found\n");
353 if (in_interrupt()) {
354 printk("nvram_commit: not committing in interrupt\n");
358 /* Backup sector blocks to be erased */
359 erasesize
= ROUNDUP(NVRAM_SPACE
, nvram_mtd
->erasesize
);
360 if (!(buf
= kmalloc(erasesize
, GFP_KERNEL
))) {
361 printk("nvram_commit: out of memory\n");
367 if ((i
= erasesize
- NVRAM_SPACE
) > 0) {
368 offset
= nvram_mtd
->size
- erasesize
;
370 ret
= MTD_READ(nvram_mtd
, offset
, i
, &len
, buf
);
371 if (ret
|| len
!= i
) {
372 printk("nvram_commit: read error ret = %d, len = %d/%d\n", ret
, len
, i
);
376 header
= (struct nvram_header
*)(buf
+ i
);
377 magic_offset
= i
+ ((void *)&header
->magic
- (void *)header
);
379 offset
= nvram_mtd
->size
- NVRAM_SPACE
;
380 magic_offset
= ((void *)&header
->magic
- (void *)header
);
381 header
= (struct nvram_header
*)buf
;
384 /* clear the existing magic # to mark the NVRAM as unusable
385 we can pull MAGIC bits low without erase */
386 header
->magic
= NVRAM_CLEAR_MAGIC
; /* All zeros magic */
388 /* Unlock sector blocks (for Intel 28F320C3B flash) , 20060309 */
389 if(nvram_mtd
->unlock
)
390 nvram_mtd
->unlock(nvram_mtd
, offset
, nvram_mtd
->erasesize
);
392 ret
= MTD_WRITE(nvram_mtd
, offset
+ magic_offset
, sizeof(header
->magic
),
393 &magic_len
, (char *)&header
->magic
);
394 if (ret
|| magic_len
!= sizeof(header
->magic
)) {
395 printk("nvram_commit: clear MAGIC error\n");
400 header
->magic
= NVRAM_MAGIC
; /* reset MAGIC before we regenerate the NVRAM,
401 otherwise we'll have an incorrect CRC */
402 /* Regenerate NVRAM */
403 spin_lock_irqsave(&nvram_lock
, flags
);
404 ret
= _nvram_commit(header
);
405 spin_unlock_irqrestore(&nvram_lock
, flags
);
409 /* Erase sector blocks */
410 init_waitqueue_head(&wait_q
);
411 for (; offset
< nvram_mtd
->size
- NVRAM_SPACE
+ header
->len
; offset
+= nvram_mtd
->erasesize
) {
412 erase
.mtd
= nvram_mtd
;
414 erase
.len
= nvram_mtd
->erasesize
;
415 erase
.callback
= erase_callback
;
416 erase
.priv
= (u_long
) &wait_q
;
418 set_current_state(TASK_INTERRUPTIBLE
);
419 add_wait_queue(&wait_q
, &wait
);
421 /* Unlock sector blocks */
422 if (nvram_mtd
->unlock
)
423 nvram_mtd
->unlock(nvram_mtd
, offset
, nvram_mtd
->erasesize
);
425 if ((ret
= MTD_ERASE(nvram_mtd
, &erase
))) {
426 set_current_state(TASK_RUNNING
);
427 remove_wait_queue(&wait_q
, &wait
);
428 printk("nvram_commit: erase error\n");
432 /* Wait for erase to finish */
434 remove_wait_queue(&wait_q
, &wait
);
437 /* Write partition up to end of data area */
438 header
->magic
= NVRAM_INVALID_MAGIC
; /* All ones magic */
439 offset
= nvram_mtd
->size
- erasesize
;
440 i
= erasesize
- NVRAM_SPACE
+ header
->len
;
441 ret
= MTD_WRITE(nvram_mtd
, offset
, i
, &len
, buf
);
442 if (ret
|| len
!= i
) {
443 printk("nvram_commit: write error\n");
448 /* Now mark the NVRAM in flash as "valid" by setting the correct
450 header
->magic
= NVRAM_MAGIC
;
451 ret
= MTD_WRITE(nvram_mtd
, offset
+ magic_offset
, sizeof(header
->magic
),
452 &magic_len
, (char *)&header
->magic
);
453 if (ret
|| magic_len
!= sizeof(header
->magic
)) {
454 printk("nvram_commit: write MAGIC error\n");
460 * Reading a few bytes back here will put the device
461 * back to the correct mode on certain flashes */
462 offset
= nvram_mtd
->size
- erasesize
;
463 ret
= MTD_READ(nvram_mtd
, offset
, 4, &len
, buf
);
473 nvram_getall(char *buf
, int count
)
478 spin_lock_irqsave(&nvram_lock
, flags
);
479 if (nvram_major
>= 0)
480 ret
= _nvram_getall(buf
, count
);
482 ret
= early_nvram_getall(buf
, count
);
483 spin_unlock_irqrestore(&nvram_lock
, flags
);
494 /* User mode interface below */
497 dev_nvram_read(struct file
*file
, char *buf
, size_t count
, loff_t
*ppos
)
499 char tmp
[100], *name
= tmp
, *value
;
503 if (count
> sizeof(tmp
)) {
504 if (!(name
= kmalloc(count
, GFP_KERNEL
)))
508 if (copy_from_user(name
, buf
, count
)) {
514 /* Get all variables */
515 ret
= nvram_getall(name
, count
);
517 if (copy_to_user(buf
, name
, count
)) {
524 if (!(value
= nvram_get(name
))) {
529 /* Provide the offset into mmap() space */
530 off
= (unsigned long) value
- (unsigned long) nvram_buf
;
532 if (put_user(off
, (unsigned long *) buf
)) {
537 ret
= sizeof(unsigned long);
550 dev_nvram_write(struct file
*file
, const char *buf
, size_t count
, loff_t
*ppos
)
552 char tmp
[100], *name
= tmp
, *value
;
555 if (count
> sizeof(tmp
)) {
556 if (!(name
= kmalloc(count
, GFP_KERNEL
)))
560 if (copy_from_user(name
, buf
, count
)) {
566 name
= strsep(&value
, "=");
568 ret
= nvram_set(name
, value
) ? : count
;
570 ret
= nvram_unset(name
) ? : count
;
580 dev_nvram_ioctl(struct inode
*inode
, struct file
*file
, unsigned int cmd
, unsigned long arg
)
582 if (cmd
!= NVRAM_MAGIC
)
585 return nvram_commit();
589 dev_nvram_mmap(struct file
*file
, struct vm_area_struct
*vma
)
591 unsigned long offset
= virt_to_phys(nvram_buf
);
593 if (remap_page_range(vma
->vm_start
, offset
, vma
->vm_end
-vma
->vm_start
,
601 dev_nvram_open(struct inode
*inode
, struct file
* file
)
608 dev_nvram_release(struct inode
*inode
, struct file
* file
)
614 static struct file_operations dev_nvram_fops
= {
616 open
: dev_nvram_open
,
617 release
: dev_nvram_release
,
618 read
: dev_nvram_read
,
619 write
: dev_nvram_write
,
620 ioctl
: dev_nvram_ioctl
,
621 mmap
: dev_nvram_mmap
,
628 struct page
*page
, *end
;
631 devfs_unregister(nvram_handle
);
633 if (nvram_major
>= 0)
634 devfs_unregister_chrdev(nvram_major
, "nvram");
637 put_mtd_device(nvram_mtd
);
639 while ((PAGE_SIZE
<< order
) < NVRAM_SPACE
)
641 end
= virt_to_page(nvram_buf
+ (PAGE_SIZE
<< order
) - 1);
642 for (page
= virt_to_page(nvram_buf
); page
<= end
; page
++)
643 mem_map_unreserve(page
);
651 int order
= 0, ret
= 0;
652 struct page
*page
, *end
;
655 /* Allocate and reserve memory to mmap() */
656 while ((PAGE_SIZE
<< order
) < NVRAM_SPACE
)
658 end
= virt_to_page(nvram_buf
+ (PAGE_SIZE
<< order
) - 1);
659 for (page
= virt_to_page(nvram_buf
); page
<= end
; page
++)
660 mem_map_reserve(page
);
663 /* Find associated MTD device */
664 for (i
= 0; i
< MAX_MTD_DEVICES
; i
++) {
665 nvram_mtd
= get_mtd_device(NULL
, i
);
667 if (!strcmp(nvram_mtd
->name
, "nvram") &&
668 nvram_mtd
->size
>= NVRAM_SPACE
)
670 put_mtd_device(nvram_mtd
);
673 if (i
>= MAX_MTD_DEVICES
)
677 /* Initialize hash table lock */
678 spin_lock_init(&nvram_lock
);
680 /* Initialize commit semaphore */
681 init_MUTEX(&nvram_sem
);
683 /* Register char device */
684 if ((nvram_major
= devfs_register_chrdev(0, "nvram", &dev_nvram_fops
)) < 0) {
689 /* Initialize hash table */
692 /* Create /dev/nvram handle */
693 nvram_handle
= devfs_register(NULL
, "nvram", DEVFS_FL_NONE
, nvram_major
, 0,
694 S_IFCHR
| S_IRUSR
| S_IWUSR
| S_IRGRP
, &dev_nvram_fops
, NULL
);
696 /* Set the SDRAM NCDL value into NVRAM if not already done */
697 if (getintvar(NULL
, "sdram_ncdl") == 0) {
699 char buf
[] = "0x00000000";
701 if ((ncdl
= sb_memc_get_ncdl(sbh
))) {
702 sprintf(buf
, "0x%08x", ncdl
);
703 nvram_set("sdram_ncdl", buf
);
715 module_init(dev_nvram_init
);
716 module_exit(dev_nvram_exit
);