u-boot-env.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2022 Rafał Miłecki <rafal@milecki.pl>
  4. */
  5. #include <linux/mod_devicetable.h>
  6. #include <linux/module.h>
  7. #include <linux/mtd/mtd.h>
  8. #include <linux/nvmem-provider.h>
  9. #include <linux/of.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/slab.h>
  12. #include "layouts/u-boot-env.h"
  13. struct u_boot_env {
  14. struct device *dev;
  15. struct nvmem_device *nvmem;
  16. enum u_boot_env_format format;
  17. struct mtd_info *mtd;
  18. };
  19. static int u_boot_env_read(void *context, unsigned int offset, void *val,
  20. size_t bytes)
  21. {
  22. struct u_boot_env *priv = context;
  23. struct device *dev = priv->dev;
  24. size_t bytes_read;
  25. int err;
  26. err = mtd_read(priv->mtd, offset, bytes, &bytes_read, val);
  27. if (err && !mtd_is_bitflip(err)) {
  28. dev_err(dev, "Failed to read from mtd: %d\n", err);
  29. return err;
  30. }
  31. if (bytes_read != bytes) {
  32. dev_err(dev, "Failed to read %zu bytes\n", bytes);
  33. return -EIO;
  34. }
  35. return 0;
  36. }
  37. static int u_boot_env_probe(struct platform_device *pdev)
  38. {
  39. struct nvmem_config config = {
  40. .name = "u-boot-env",
  41. .reg_read = u_boot_env_read,
  42. };
  43. struct device *dev = &pdev->dev;
  44. struct device_node *np = dev->of_node;
  45. struct u_boot_env *priv;
  46. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  47. if (!priv)
  48. return -ENOMEM;
  49. priv->dev = dev;
  50. priv->format = (uintptr_t)of_device_get_match_data(dev);
  51. priv->mtd = of_get_mtd_device_by_node(np);
  52. if (IS_ERR(priv->mtd)) {
  53. dev_err_probe(dev, PTR_ERR(priv->mtd), "Failed to get %pOF MTD\n", np);
  54. return PTR_ERR(priv->mtd);
  55. }
  56. config.dev = dev;
  57. config.priv = priv;
  58. config.size = priv->mtd->size;
  59. priv->nvmem = devm_nvmem_register(dev, &config);
  60. if (IS_ERR(priv->nvmem))
  61. return PTR_ERR(priv->nvmem);
  62. return u_boot_env_parse(dev, priv->nvmem, priv->format);
  63. }
  64. static const struct of_device_id u_boot_env_of_match_table[] = {
  65. { .compatible = "u-boot,env", .data = (void *)U_BOOT_FORMAT_SINGLE, },
  66. { .compatible = "u-boot,env-redundant-bool", .data = (void *)U_BOOT_FORMAT_REDUNDANT, },
  67. { .compatible = "u-boot,env-redundant-count", .data = (void *)U_BOOT_FORMAT_REDUNDANT, },
  68. { .compatible = "brcm,env", .data = (void *)U_BOOT_FORMAT_BROADCOM, },
  69. {},
  70. };
  71. static struct platform_driver u_boot_env_driver = {
  72. .probe = u_boot_env_probe,
  73. .driver = {
  74. .name = "u_boot_env",
  75. .of_match_table = u_boot_env_of_match_table,
  76. },
  77. };
  78. module_platform_driver(u_boot_env_driver);
  79. MODULE_AUTHOR("Rafał Miłecki");
  80. MODULE_DESCRIPTION("U-Boot environment variables support module");
  81. MODULE_LICENSE("GPL");
  82. MODULE_DEVICE_TABLE(of, u_boot_env_of_match_table);