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