2 * NVRAM variable manipulation (Linux kernel half)
4 * Copyright 2004, 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.
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>
33 #include <bcmendian.h>
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
)));
47 #define early_nvram_get(name) nvram_get(name)
51 /* Global SB handle */
52 extern void *bcm947xx_sbh
;
53 extern spinlock_t bcm947xx_sbh_lock
;
56 #define sbh bcm947xx_sbh
57 #define sbh_lock bcm947xx_sbh_lock
59 #define MB * 1024 * 1024
61 /* Probe for NVRAM header */
63 early_nvram_init(void)
65 struct nvram_header
*header
;
67 struct sflash
*info
= NULL
;
69 uint32 base
, off
, lim
;
71 if ((cc
= sb_setcore(sbh
, SB_CC
, 0)) != NULL
) {
73 switch (readl(&cc
->capabilities
) & CAP_FLASH_MASK
) {
80 if ((info
= sflash_init(cc
)) == NULL
)
90 /* extif assumed, Stop at 4 MB */
97 /* Windowed flash access */
98 header
= (struct nvram_header
*) KSEG1ADDR(base
+ off
- NVRAM_SPACE
);
99 if (header
->magic
== NVRAM_MAGIC
) {
100 u32
*src
= (u32
*) header
;
101 u32
*dst
= (u32
*) nvram_buf
;
102 for (i
= 0; i
< sizeof(struct nvram_header
); i
+= 4)
104 for (; i
< header
->len
&& i
< NVRAM_SPACE
; i
+= 4)
105 *dst
++ = ltoh32(*src
++);
109 /* Try embedded NVRAM at 4 KB and 1 KB as last resorts */
112 else if (off
== 4 KB
)
121 /* Early (before mm or mtd) read-only access to NVRAM */
123 early_nvram_get(const char *name
)
125 char *var
, *value
, *end
, *eq
;
133 /* Look for name=value and return value */
134 var
= &nvram_buf
[sizeof(struct nvram_header
)];
135 end
= nvram_buf
+ sizeof(nvram_buf
) - 2;
136 end
[0] = end
[1] = '\0';
137 for (; *var
; var
= value
+ strlen(value
) + 1) {
138 if (!(eq
= strchr(var
, '=')))
141 if ((eq
- var
) == strlen(name
) && strncmp(var
, name
, (eq
- var
)) == 0)
150 extern char * _nvram_get(const char *name
);
151 extern int _nvram_set(const char *name
, const char *value
);
152 extern int _nvram_unset(const char *name
);
153 extern int _nvram_getall(char *buf
, int count
);
154 extern int _nvram_commit(struct nvram_header
*header
);
155 extern int _nvram_init(void);
156 extern void _nvram_exit(void);
159 static spinlock_t nvram_lock
= SPIN_LOCK_UNLOCKED
;
160 static struct semaphore nvram_sem
;
161 static unsigned long nvram_offset
= 0;
162 static int nvram_major
= -1;
163 static devfs_handle_t nvram_handle
= NULL
;
164 static struct mtd_info
*nvram_mtd
= NULL
;
167 _nvram_read(char *buf
)
169 struct nvram_header
*header
= (struct nvram_header
*) buf
;
173 MTD_READ(nvram_mtd
, nvram_mtd
->size
- NVRAM_SPACE
, NVRAM_SPACE
, &len
, buf
) ||
174 len
!= NVRAM_SPACE
||
175 header
->magic
!= NVRAM_MAGIC
) {
176 /* Maybe we can recover some data from early initialization */
177 memcpy(buf
, nvram_buf
, NVRAM_SPACE
);
184 _nvram_realloc(struct nvram_tuple
*t
, const char *name
, const char *value
)
186 if ((nvram_offset
+ strlen(value
) + 1) > NVRAM_SPACE
)
190 if (!(t
= kmalloc(sizeof(struct nvram_tuple
) + strlen(name
) + 1, GFP_ATOMIC
)))
194 t
->name
= (char *) &t
[1];
195 strcpy(t
->name
, name
);
201 if (!t
->value
|| strcmp(t
->value
, value
)) {
202 t
->value
= &nvram_buf
[nvram_offset
];
203 strcpy(t
->value
, value
);
204 nvram_offset
+= strlen(value
) + 1;
211 _nvram_free(struct nvram_tuple
*t
)
220 nvram_set(const char *name
, const char *value
)
224 struct nvram_header
*header
;
226 spin_lock_irqsave(&nvram_lock
, flags
);
227 if ((ret
= _nvram_set(name
, value
))) {
228 /* Consolidate space and try again */
229 if ((header
= kmalloc(NVRAM_SPACE
, GFP_ATOMIC
))) {
230 if (_nvram_commit(header
) == 0)
231 ret
= _nvram_set(name
, value
);
235 spin_unlock_irqrestore(&nvram_lock
, flags
);
241 real_nvram_get(const char *name
)
246 spin_lock_irqsave(&nvram_lock
, flags
);
247 value
= _nvram_get(name
);
248 spin_unlock_irqrestore(&nvram_lock
, flags
);
254 nvram_get(const char *name
)
256 if (nvram_major
>= 0)
257 return real_nvram_get(name
);
259 return early_nvram_get(name
);
263 nvram_unset(const char *name
)
268 spin_lock_irqsave(&nvram_lock
, flags
);
269 ret
= _nvram_unset(name
);
270 spin_unlock_irqrestore(&nvram_lock
, flags
);
276 erase_callback(struct erase_info
*done
)
278 wait_queue_head_t
*wait_q
= (wait_queue_head_t
*) done
->priv
;
286 size_t erasesize
, len
;
289 struct nvram_header
*header
;
292 DECLARE_WAITQUEUE(wait
, current
);
293 wait_queue_head_t wait_q
;
294 struct erase_info erase
;
296 printk("nvram_commit(): init\n");
299 printk("nvram_commit: NVRAM not found\n");
303 if (in_interrupt()) {
304 printk("nvram_commit: not committing in interrupt\n");
308 /* Backup sector blocks to be erased */
309 erasesize
= ROUNDUP(NVRAM_SPACE
, nvram_mtd
->erasesize
);
310 if (!(buf
= kmalloc(erasesize
, GFP_KERNEL
))) {
311 printk("nvram_commit: out of memory\n");
317 offset
= nvram_mtd
->size
- erasesize
;
318 i
= erasesize
- NVRAM_SPACE
;
319 ret
= MTD_READ(nvram_mtd
, offset
, i
, &len
, buf
);
320 if (ret
|| len
!= i
) {
321 printk("nvram_commit: read error\n");
325 if ((i
= erasesize
- NVRAM_SPACE
) > 0) {
326 offset
= nvram_mtd
->size
- erasesize
;
328 ret
= MTD_READ(nvram_mtd
, offset
, i
, &len
, buf
);
329 if (ret
|| len
!= i
) {
330 printk("nvram_commit: read error ret = %d, len = %d/%d\n", ret
, len
, i
);
334 header
= (struct nvram_header
*)(buf
+ i
);
336 offset
= nvram_mtd
->size
- NVRAM_SPACE
;
337 header
= (struct nvram_header
*)buf
;
340 /* Regenerate NVRAM */
341 spin_lock_irqsave(&nvram_lock
, flags
);
342 ret
= _nvram_commit(header
);
343 spin_unlock_irqrestore(&nvram_lock
, flags
);
347 /* Erase sector blocks */
348 init_waitqueue_head(&wait_q
);
349 for (; offset
< nvram_mtd
->size
- NVRAM_SPACE
+ header
->len
; offset
+= nvram_mtd
->erasesize
) {
350 erase
.mtd
= nvram_mtd
;
352 erase
.len
= nvram_mtd
->erasesize
;
353 erase
.callback
= erase_callback
;
354 erase
.priv
= (u_long
) &wait_q
;
356 set_current_state(TASK_INTERRUPTIBLE
);
357 add_wait_queue(&wait_q
, &wait
);
359 /* Unlock sector blocks */
360 if (nvram_mtd
->unlock
)
361 nvram_mtd
->unlock(nvram_mtd
, offset
, nvram_mtd
->erasesize
);
363 if ((ret
= MTD_ERASE(nvram_mtd
, &erase
))) {
364 set_current_state(TASK_RUNNING
);
365 remove_wait_queue(&wait_q
, &wait
);
366 printk("nvram_commit: erase error\n");
370 /* Wait for erase to finish */
372 remove_wait_queue(&wait_q
, &wait
);
375 /* Write partition up to end of data area */
376 offset
= nvram_mtd
->size
- erasesize
;
377 i
= erasesize
- NVRAM_SPACE
+ header
->len
;
378 ret
= MTD_WRITE(nvram_mtd
, offset
, i
, &len
, buf
);
379 if (ret
|| len
!= i
) {
380 printk("nvram_commit: write error\n");
385 * Reading a few bytes back here will put the device
386 * back to the correct mode on certain flashes */
388 offset
= nvram_mtd
->size
- erasesize
;
389 ret
= MTD_READ(nvram_mtd
, offset
, 4, &len
, buf
);
394 printk("nvram_commit(): end\n");
399 nvram_getall(char *buf
, int count
)
404 spin_lock_irqsave(&nvram_lock
, flags
);
405 ret
= _nvram_getall(buf
, count
);
406 spin_unlock_irqrestore(&nvram_lock
, flags
);
411 EXPORT_SYMBOL(nvram_get
);
412 EXPORT_SYMBOL(nvram_getall
);
413 EXPORT_SYMBOL(nvram_set
);
414 EXPORT_SYMBOL(nvram_unset
);
415 EXPORT_SYMBOL(nvram_commit
);
417 /* User mode interface below */
420 dev_nvram_read(struct file
*file
, char *buf
, size_t count
, loff_t
*ppos
)
422 char tmp
[100], *name
= tmp
, *value
;
426 if (count
> sizeof(tmp
)) {
427 if (!(name
= kmalloc(count
, GFP_KERNEL
)))
431 if (copy_from_user(name
, buf
, count
)) {
437 /* Get all variables */
438 ret
= nvram_getall(name
, count
);
440 if (copy_to_user(buf
, name
, count
)) {
447 if (!(value
= nvram_get(name
))) {
452 /* Provide the offset into mmap() space */
453 off
= (unsigned long) value
- (unsigned long) nvram_buf
;
455 if (put_user(off
, (unsigned long *) buf
)) {
460 ret
= sizeof(unsigned long);
473 dev_nvram_write(struct file
*file
, const char *buf
, size_t count
, loff_t
*ppos
)
475 char tmp
[100], *name
= tmp
, *value
;
478 if (count
> sizeof(tmp
)) {
479 if (!(name
= kmalloc(count
, GFP_KERNEL
)))
483 if (copy_from_user(name
, buf
, count
)) {
489 name
= strsep(&value
, "=");
491 ret
= nvram_set(name
, value
) ? : count
;
493 ret
= nvram_unset(name
) ? : count
;
503 dev_nvram_ioctl(struct inode
*inode
, struct file
*file
, unsigned int cmd
, unsigned long arg
)
505 if (cmd
!= NVRAM_MAGIC
)
507 return nvram_commit();
511 dev_nvram_mmap(struct file
*file
, struct vm_area_struct
*vma
)
513 unsigned long offset
= virt_to_phys(nvram_buf
);
515 if (remap_page_range(vma
->vm_start
, offset
, vma
->vm_end
-vma
->vm_start
,
523 dev_nvram_open(struct inode
*inode
, struct file
* file
)
530 dev_nvram_release(struct inode
*inode
, struct file
* file
)
536 static struct file_operations dev_nvram_fops
= {
538 open
: dev_nvram_open
,
539 release
: dev_nvram_release
,
540 read
: dev_nvram_read
,
541 write
: dev_nvram_write
,
542 ioctl
: dev_nvram_ioctl
,
543 mmap
: dev_nvram_mmap
,
550 struct page
*page
, *end
;
553 devfs_unregister(nvram_handle
);
555 if (nvram_major
>= 0)
556 devfs_unregister_chrdev(nvram_major
, "nvram");
559 put_mtd_device(nvram_mtd
);
561 while ((PAGE_SIZE
<< order
) < NVRAM_SPACE
)
563 end
= virt_to_page(nvram_buf
+ (PAGE_SIZE
<< order
) - 1);
564 for (page
= virt_to_page(nvram_buf
); page
<= end
; page
++)
565 mem_map_unreserve(page
);
573 int order
= 0, ret
= 0;
574 struct page
*page
, *end
;
577 /* Allocate and reserve memory to mmap() */
578 while ((PAGE_SIZE
<< order
) < NVRAM_SPACE
)
580 end
= virt_to_page(nvram_buf
+ (PAGE_SIZE
<< order
) - 1);
581 for (page
= virt_to_page(nvram_buf
); page
<= end
; page
++)
582 mem_map_reserve(page
);
585 /* Find associated MTD device */
586 for (i
= 0; i
< MAX_MTD_DEVICES
; i
++) {
587 nvram_mtd
= get_mtd_device(NULL
, i
);
589 if (!strcmp(nvram_mtd
->name
, "nvram") &&
590 nvram_mtd
->size
>= NVRAM_SPACE
)
592 put_mtd_device(nvram_mtd
);
595 if (i
>= MAX_MTD_DEVICES
)
599 /* Initialize hash table lock */
600 spin_lock_init(&nvram_lock
);
602 /* Initialize commit semaphore */
603 init_MUTEX(&nvram_sem
);
605 /* Register char device */
606 if ((nvram_major
= devfs_register_chrdev(0, "nvram", &dev_nvram_fops
)) < 0) {
611 /* Initialize hash table */
614 /* Create /dev/nvram handle */
615 nvram_handle
= devfs_register(NULL
, "nvram", DEVFS_FL_NONE
, nvram_major
, 0,
616 S_IFCHR
| S_IRUSR
| S_IWUSR
| S_IRGRP
, &dev_nvram_fops
, NULL
);
618 /* Set the SDRAM NCDL value into NVRAM if not already done */
619 if (getintvar(NULL
, "sdram_ncdl") == 0) {
621 char buf
[] = "0x00000000";
623 if ((ncdl
= sb_memc_get_ncdl(sbh
))) {
624 sprintf(buf
, "0x%08x", ncdl
);
625 nvram_set("sdram_ncdl", buf
);
637 module_init(dev_nvram_init
);
638 module_exit(dev_nvram_exit
);