030-06-MIPS-BCM47XX-Use-mtd-as-an-alternative-way-API-to-ge.patch 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. NVRAM can be read using magic memory offset, but after all it's just a
  10. flash partition. On platforms where NVRAM isn't needed early we can get
  11. it using mtd subsystem.
  12. Signed-off-by: Rafał Miłecki <zajec5@gmail.com>
  13. Acked-by: Hauke Mehrtens <hauke@hauke-m.de>
  14. Cc: linux-mips@linux-mips.org
  15. Patchwork: https://patchwork.linux-mips.org/patch/8266/
  16. Signed-off-by: Ralf Baechle <ralf@linux-mips.org>
  17. ---
  18. arch/mips/bcm47xx/nvram.c | 42 ++++++++++++++++++++++++++++++++++++++----
  19. 1 file changed, 38 insertions(+), 4 deletions(-)
  20. --- a/arch/mips/bcm47xx/nvram.c
  21. +++ b/arch/mips/bcm47xx/nvram.c
  22. @@ -13,12 +13,10 @@
  23. #include <linux/types.h>
  24. #include <linux/module.h>
  25. -#include <linux/ssb/ssb.h>
  26. #include <linux/kernel.h>
  27. #include <linux/string.h>
  28. -#include <asm/addrspace.h>
  29. +#include <linux/mtd/mtd.h>
  30. #include <bcm47xx_nvram.h>
  31. -#include <asm/mach-bcm47xx/bcm47xx.h>
  32. static char nvram_buf[NVRAM_SPACE];
  33. static const u32 nvram_sizes[] = {0x8000, 0xF000, 0x10000};
  34. @@ -123,7 +121,43 @@ int bcm47xx_nvram_init_from_mem(u32 base
  35. static int nvram_init(void)
  36. {
  37. - /* TODO: Look for MTD "nvram" partition */
  38. +#ifdef CONFIG_MTD
  39. + struct mtd_info *mtd;
  40. + struct nvram_header header;
  41. + size_t bytes_read;
  42. + int err, i;
  43. +
  44. + mtd = get_mtd_device_nm("nvram");
  45. + if (IS_ERR(mtd))
  46. + return -ENODEV;
  47. +
  48. + for (i = 0; i < ARRAY_SIZE(nvram_sizes); i++) {
  49. + loff_t from = mtd->size - nvram_sizes[i];
  50. +
  51. + if (from < 0)
  52. + continue;
  53. +
  54. + err = mtd_read(mtd, from, sizeof(header), &bytes_read,
  55. + (uint8_t *)&header);
  56. + if (!err && header.magic == NVRAM_HEADER) {
  57. + u8 *dst = (uint8_t *)nvram_buf;
  58. + size_t len = header.len;
  59. +
  60. + if (header.len > NVRAM_SPACE) {
  61. + pr_err("nvram on flash (%i bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n",
  62. + header.len, NVRAM_SPACE);
  63. + len = NVRAM_SPACE;
  64. + }
  65. +
  66. + err = mtd_read(mtd, from, len, &bytes_read, dst);
  67. + if (err)
  68. + return err;
  69. + memset(dst + bytes_read, 0x0, NVRAM_SPACE - bytes_read);
  70. +
  71. + return 0;
  72. + }
  73. + }
  74. +#endif
  75. return -ENXIO;
  76. }