1e6bc2f20b245547b4e6096882750212df4dce70
[openwrt/staging/robimarko.git] /
1 From 9d1d08646af4491aec41d40341930b9bfd62ffa9 Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <zajec5@gmail.com>
3 Date: Wed, 29 Oct 2014 10:05:06 +0100
4 Subject: [PATCH] MIPS: BCM47XX: Use mtd as an alternative way/API to get NVRAM
5 content
6 MIME-Version: 1.0
7 Content-Type: text/plain; charset=UTF-8
8 Content-Transfer-Encoding: 8bit
9
10 NVRAM can be read using magic memory offset, but after all it's just a
11 flash partition. On platforms where NVRAM isn't needed early we can get
12 it using mtd subsystem.
13
14 Signed-off-by: Rafał Miłecki <zajec5@gmail.com>
15 Acked-by: Hauke Mehrtens <hauke@hauke-m.de>
16 Cc: linux-mips@linux-mips.org
17 Patchwork: https://patchwork.linux-mips.org/patch/8266/
18 Signed-off-by: Ralf Baechle <ralf@linux-mips.org>
19 ---
20 arch/mips/bcm47xx/nvram.c | 42 ++++++++++++++++++++++++++++++++++++++----
21 1 file changed, 38 insertions(+), 4 deletions(-)
22
23 --- a/arch/mips/bcm47xx/nvram.c
24 +++ b/arch/mips/bcm47xx/nvram.c
25 @@ -13,12 +13,10 @@
26
27 #include <linux/types.h>
28 #include <linux/module.h>
29 -#include <linux/ssb/ssb.h>
30 #include <linux/kernel.h>
31 #include <linux/string.h>
32 -#include <asm/addrspace.h>
33 +#include <linux/mtd/mtd.h>
34 #include <bcm47xx_nvram.h>
35 -#include <asm/mach-bcm47xx/bcm47xx.h>
36
37 static char nvram_buf[NVRAM_SPACE];
38 static const u32 nvram_sizes[] = {0x8000, 0xF000, 0x10000};
39 @@ -123,7 +121,43 @@ int bcm47xx_nvram_init_from_mem(u32 base
40
41 static int nvram_init(void)
42 {
43 - /* TODO: Look for MTD "nvram" partition */
44 +#ifdef CONFIG_MTD
45 + struct mtd_info *mtd;
46 + struct nvram_header header;
47 + size_t bytes_read;
48 + int err, i;
49 +
50 + mtd = get_mtd_device_nm("nvram");
51 + if (IS_ERR(mtd))
52 + return -ENODEV;
53 +
54 + for (i = 0; i < ARRAY_SIZE(nvram_sizes); i++) {
55 + loff_t from = mtd->size - nvram_sizes[i];
56 +
57 + if (from < 0)
58 + continue;
59 +
60 + err = mtd_read(mtd, from, sizeof(header), &bytes_read,
61 + (uint8_t *)&header);
62 + if (!err && header.magic == NVRAM_HEADER) {
63 + u8 *dst = (uint8_t *)nvram_buf;
64 + size_t len = header.len;
65 +
66 + if (header.len > NVRAM_SPACE) {
67 + pr_err("nvram on flash (%i bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n",
68 + header.len, NVRAM_SPACE);
69 + len = NVRAM_SPACE;
70 + }
71 +
72 + err = mtd_read(mtd, from, len, &bytes_read, dst);
73 + if (err)
74 + return err;
75 + memset(dst + bytes_read, 0x0, NVRAM_SPACE - bytes_read);
76 +
77 + return 0;
78 + }
79 + }
80 +#endif
81
82 return -ENXIO;
83 }