fonera-mp3: fix json-c dependency here as well
[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 <bcmutils.h>
37 #include <sbconfig.h>
38 #include <sbchipc.h>
39 #include <sbutils.h>
40 #include <hndmips.h>
41 #include <sflash.h>
42
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)));
45
46 #ifdef MODULE
47
48 #define early_nvram_get(name) nvram_get(name)
49
50 #else /* !MODULE */
51
52 /* Global SB handle */
53 extern void *bcm947xx_sbh;
54 extern spinlock_t bcm947xx_sbh_lock;
55
56 /* Convenience */
57 #define sbh bcm947xx_sbh
58 #define sbh_lock bcm947xx_sbh_lock
59 #define KB * 1024
60 #define MB * 1024 * 1024
61
62 /* Probe for NVRAM header */
63 static void __init
64 early_nvram_init(void)
65 {
66 struct nvram_header *header;
67 chipcregs_t *cc;
68 struct sflash *info = NULL;
69 int i;
70 uint32 base, off, lim;
71 u32 *src, *dst;
72
73 if ((cc = sb_setcore(sbh, SB_CC, 0)) != NULL) {
74 base = KSEG1ADDR(SB_FLASH2);
75 switch (readl(&cc->capabilities) & CAP_FLASH_MASK) {
76 case PFLASH:
77 lim = SB_FLASH2_SZ;
78 break;
79
80 case SFLASH_ST:
81 case SFLASH_AT:
82 if ((info = sflash_init(cc)) == NULL)
83 return;
84 lim = info->size;
85 break;
86
87 case FLASH_NONE:
88 default:
89 return;
90 }
91 } else {
92 /* extif assumed, Stop at 4 MB */
93 base = KSEG1ADDR(SB_FLASH1);
94 lim = SB_FLASH1_SZ;
95 }
96
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");
102
103 for (i = 0; i < 0x1ff0; i++) {
104 if (*src == 0xFFFFFFFF)
105 break;
106 *dst++ = *src++;
107 }
108 return;
109 }
110
111 off = FLASH_MIN;
112 while (off <= lim) {
113 /* Windowed flash access */
114 header = (struct nvram_header *) KSEG1ADDR(base + off - NVRAM_SPACE);
115 if (header->magic == NVRAM_MAGIC)
116 goto found;
117 off <<= 1;
118 }
119
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)
123 goto found;
124
125 header = (struct nvram_header *) KSEG1ADDR(base + 1 KB);
126 if (header->magic == NVRAM_MAGIC)
127 goto found;
128
129 printk("early_nvram_init: NVRAM not found\n");
130 return;
131
132 found:
133 src = (u32 *) header;
134 dst = (u32 *) nvram_buf;
135 for (i = 0; i < sizeof(struct nvram_header); i += 4)
136 *dst++ = *src++;
137 for (; i < header->len && i < NVRAM_SPACE; i += 4)
138 *dst++ = ltoh32(*src++);
139 }
140
141 /* Early (before mm or mtd) read-only access to NVRAM */
142 static char * __init
143 early_nvram_get(const char *name)
144 {
145 char *var, *value, *end, *eq;
146
147 if (!name)
148 return NULL;
149
150 /* Too early? */
151 if (sbh == NULL)
152 return NULL;
153
154 if (!nvram_buf[0])
155 early_nvram_init();
156
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, '=')))
163 break;
164 value = eq + 1;
165 if ((eq - var) == strlen(name) && strncmp(var, name, (eq - var)) == 0)
166 return value;
167 }
168
169 return NULL;
170 }
171
172 static int __init
173 early_nvram_getall(char *buf, int count)
174 {
175 char *var, *end;
176 int len = 0;
177
178 /* Too early? */
179 if (sbh == NULL)
180 return -1;
181
182 if (!nvram_buf[0])
183 early_nvram_init();
184
185 bzero(buf, count);
186
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))
193 break;
194 len += sprintf(buf + len, "%s", var) + 1;
195 }
196
197 return 0;
198 }
199 #endif /* !MODULE */
200
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);
208
209 /* Globals */
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;
216
217 int
218 _nvram_read(char *buf)
219 {
220 struct nvram_header *header = (struct nvram_header *) buf;
221 size_t len;
222
223 if (!nvram_mtd ||
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);
229 }
230
231 return 0;
232 }
233
234 struct nvram_tuple *
235 _nvram_realloc(struct nvram_tuple *t, const char *name, const char *value)
236 {
237 if ((nvram_offset + strlen(value) + 1) > NVRAM_SPACE)
238 return NULL;
239
240 if (!t) {
241 if (!(t = kmalloc(sizeof(struct nvram_tuple) + strlen(name) + 1, GFP_ATOMIC)))
242 return NULL;
243
244 /* Copy name */
245 t->name = (char *) &t[1];
246 strcpy(t->name, name);
247
248 t->value = NULL;
249 }
250
251 /* Copy value */
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;
256 }
257
258 return t;
259 }
260
261 void
262 _nvram_free(struct nvram_tuple *t)
263 {
264 if (!t)
265 nvram_offset = 0;
266 else
267 kfree(t);
268 }
269
270 int
271 nvram_set(const char *name, const char *value)
272 {
273 unsigned long flags;
274 int ret;
275 struct nvram_header *header;
276
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);
283 kfree(header);
284 }
285 }
286 spin_unlock_irqrestore(&nvram_lock, flags);
287
288 return ret;
289 }
290
291 char *
292 real_nvram_get(const char *name)
293 {
294 unsigned long flags;
295 char *value;
296
297 spin_lock_irqsave(&nvram_lock, flags);
298 value = _nvram_get(name);
299 spin_unlock_irqrestore(&nvram_lock, flags);
300
301 return value;
302 }
303
304 char *
305 nvram_get(const char *name)
306 {
307 if (nvram_major >= 0)
308 return real_nvram_get(name);
309 else
310 return early_nvram_get(name);
311 }
312
313 int
314 nvram_unset(const char *name)
315 {
316 unsigned long flags;
317 int ret;
318
319 spin_lock_irqsave(&nvram_lock, flags);
320 ret = _nvram_unset(name);
321 spin_unlock_irqrestore(&nvram_lock, flags);
322
323 return ret;
324 }
325
326 static void
327 erase_callback(struct erase_info *done)
328 {
329 wait_queue_head_t *wait_q = (wait_queue_head_t *) done->priv;
330 wake_up(wait_q);
331 }
332
333 int
334 nvram_commit(void)
335 {
336 char *buf;
337 size_t erasesize, len, magic_len;
338 unsigned int i;
339 int ret;
340 struct nvram_header *header;
341 unsigned long flags;
342 u_int32_t offset;
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 # */
347
348 if (!nvram_mtd) {
349 printk("nvram_commit: NVRAM not found\n");
350 return -ENODEV;
351 }
352
353 if (in_interrupt()) {
354 printk("nvram_commit: not committing in interrupt\n");
355 return -EINVAL;
356 }
357
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");
362 return -ENOMEM;
363 }
364
365 down(&nvram_sem);
366
367 if ((i = erasesize - NVRAM_SPACE) > 0) {
368 offset = nvram_mtd->size - erasesize;
369 len = 0;
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);
373 ret = -EIO;
374 goto done;
375 }
376 header = (struct nvram_header *)(buf + i);
377 magic_offset = i + ((void *)&header->magic - (void *)header);
378 } else {
379 offset = nvram_mtd->size - NVRAM_SPACE;
380 magic_offset = ((void *)&header->magic - (void *)header);
381 header = (struct nvram_header *)buf;
382 }
383
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 */
387
388 /* Unlock sector blocks (for Intel 28F320C3B flash) , 20060309 */
389 if(nvram_mtd->unlock)
390 nvram_mtd->unlock(nvram_mtd, offset, nvram_mtd->erasesize);
391
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");
396 ret = -EIO;
397 goto done;
398 }
399
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);
406 if (ret)
407 goto done;
408
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;
413 erase.addr = offset;
414 erase.len = nvram_mtd->erasesize;
415 erase.callback = erase_callback;
416 erase.priv = (u_long) &wait_q;
417
418 set_current_state(TASK_INTERRUPTIBLE);
419 add_wait_queue(&wait_q, &wait);
420
421 /* Unlock sector blocks */
422 if (nvram_mtd->unlock)
423 nvram_mtd->unlock(nvram_mtd, offset, nvram_mtd->erasesize);
424
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");
429 goto done;
430 }
431
432 /* Wait for erase to finish */
433 schedule();
434 remove_wait_queue(&wait_q, &wait);
435 }
436
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");
444 ret = -EIO;
445 goto done;
446 }
447
448 /* Now mark the NVRAM in flash as "valid" by setting the correct
449 MAGIC # */
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");
455 ret = -EIO;
456 goto done;
457 }
458
459 /*
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);
464
465 done:
466 up(&nvram_sem);
467 kfree(buf);
468
469 return ret;
470 }
471
472 int
473 nvram_getall(char *buf, int count)
474 {
475 unsigned long flags;
476 int ret;
477
478 spin_lock_irqsave(&nvram_lock, flags);
479 if (nvram_major >= 0)
480 ret = _nvram_getall(buf, count);
481 else
482 ret = early_nvram_getall(buf, count);
483 spin_unlock_irqrestore(&nvram_lock, flags);
484
485 return ret;
486 }
487
488
489
490
491
492
493
494 /* User mode interface below */
495
496 static ssize_t
497 dev_nvram_read(struct file *file, char *buf, size_t count, loff_t *ppos)
498 {
499 char tmp[100], *name = tmp, *value;
500 ssize_t ret;
501 unsigned long off;
502
503 if (count > sizeof(tmp)) {
504 if (!(name = kmalloc(count, GFP_KERNEL)))
505 return -ENOMEM;
506 }
507
508 if (copy_from_user(name, buf, count)) {
509 ret = -EFAULT;
510 goto done;
511 }
512
513 if (*name == '\0') {
514 /* Get all variables */
515 ret = nvram_getall(name, count);
516 if (ret == 0) {
517 if (copy_to_user(buf, name, count)) {
518 ret = -EFAULT;
519 goto done;
520 }
521 ret = count;
522 }
523 } else {
524 if (!(value = nvram_get(name))) {
525 ret = 0;
526 goto done;
527 }
528
529 /* Provide the offset into mmap() space */
530 off = (unsigned long) value - (unsigned long) nvram_buf;
531
532 if (put_user(off, (unsigned long *) buf)) {
533 ret = -EFAULT;
534 goto done;
535 }
536
537 ret = sizeof(unsigned long);
538 }
539
540 flush_cache_all();
541
542 done:
543 if (name != tmp)
544 kfree(name);
545
546 return ret;
547 }
548
549 static ssize_t
550 dev_nvram_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
551 {
552 char tmp[100], *name = tmp, *value;
553 ssize_t ret;
554
555 if (count > sizeof(tmp)) {
556 if (!(name = kmalloc(count, GFP_KERNEL)))
557 return -ENOMEM;
558 }
559
560 if (copy_from_user(name, buf, count)) {
561 ret = -EFAULT;
562 goto done;
563 }
564
565 value = name;
566 name = strsep(&value, "=");
567 if (value)
568 ret = nvram_set(name, value) ? : count;
569 else
570 ret = nvram_unset(name) ? : count;
571
572 done:
573 if (name != tmp)
574 kfree(name);
575
576 return ret;
577 }
578
579 static int
580 dev_nvram_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
581 {
582 if (cmd != NVRAM_MAGIC)
583 return -EINVAL;
584
585 return nvram_commit();
586 }
587
588 static int
589 dev_nvram_mmap(struct file *file, struct vm_area_struct *vma)
590 {
591 unsigned long offset = virt_to_phys(nvram_buf);
592
593 if (remap_page_range(vma->vm_start, offset, vma->vm_end-vma->vm_start,
594 vma->vm_page_prot))
595 return -EAGAIN;
596
597 return 0;
598 }
599
600 static int
601 dev_nvram_open(struct inode *inode, struct file * file)
602 {
603 MOD_INC_USE_COUNT;
604 return 0;
605 }
606
607 static int
608 dev_nvram_release(struct inode *inode, struct file * file)
609 {
610 MOD_DEC_USE_COUNT;
611 return 0;
612 }
613
614 static struct file_operations dev_nvram_fops = {
615 owner: THIS_MODULE,
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,
622 };
623
624 static void
625 dev_nvram_exit(void)
626 {
627 int order = 0;
628 struct page *page, *end;
629
630 if (nvram_handle)
631 devfs_unregister(nvram_handle);
632
633 if (nvram_major >= 0)
634 devfs_unregister_chrdev(nvram_major, "nvram");
635
636 if (nvram_mtd)
637 put_mtd_device(nvram_mtd);
638
639 while ((PAGE_SIZE << order) < NVRAM_SPACE)
640 order++;
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);
644
645 _nvram_exit();
646 }
647
648 static int __init
649 dev_nvram_init(void)
650 {
651 int order = 0, ret = 0;
652 struct page *page, *end;
653 unsigned int i;
654
655 /* Allocate and reserve memory to mmap() */
656 while ((PAGE_SIZE << order) < NVRAM_SPACE)
657 order++;
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);
661
662 #ifdef CONFIG_MTD
663 /* Find associated MTD device */
664 for (i = 0; i < MAX_MTD_DEVICES; i++) {
665 nvram_mtd = get_mtd_device(NULL, i);
666 if (nvram_mtd) {
667 if (!strcmp(nvram_mtd->name, "nvram") &&
668 nvram_mtd->size >= NVRAM_SPACE)
669 break;
670 put_mtd_device(nvram_mtd);
671 }
672 }
673 if (i >= MAX_MTD_DEVICES)
674 nvram_mtd = NULL;
675 #endif
676
677 /* Initialize hash table lock */
678 spin_lock_init(&nvram_lock);
679
680 /* Initialize commit semaphore */
681 init_MUTEX(&nvram_sem);
682
683 /* Register char device */
684 if ((nvram_major = devfs_register_chrdev(0, "nvram", &dev_nvram_fops)) < 0) {
685 ret = nvram_major;
686 goto err;
687 }
688
689 /* Initialize hash table */
690 _nvram_init(sbh);
691
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);
695
696 /* Set the SDRAM NCDL value into NVRAM if not already done */
697 if (getintvar(NULL, "sdram_ncdl") == 0) {
698 unsigned int ncdl;
699 char buf[] = "0x00000000";
700
701 if ((ncdl = sb_memc_get_ncdl(sbh))) {
702 sprintf(buf, "0x%08x", ncdl);
703 nvram_set("sdram_ncdl", buf);
704 nvram_commit();
705 }
706 }
707
708 return 0;
709
710 err:
711 dev_nvram_exit();
712 return ret;
713 }
714
715 module_init(dev_nvram_init);
716 module_exit(dev_nvram_exit);
This page took 0.07464 seconds and 5 git commands to generate.