apple-efuses.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Apple SoC eFuse driver
  4. *
  5. * Copyright (C) The Asahi Linux Contributors
  6. */
  7. #include <linux/io.h>
  8. #include <linux/mod_devicetable.h>
  9. #include <linux/module.h>
  10. #include <linux/nvmem-provider.h>
  11. #include <linux/platform_device.h>
  12. struct apple_efuses_priv {
  13. void __iomem *fuses;
  14. };
  15. static int apple_efuses_read(void *context, unsigned int offset, void *val,
  16. size_t bytes)
  17. {
  18. struct apple_efuses_priv *priv = context;
  19. u32 *dst = val;
  20. while (bytes >= sizeof(u32)) {
  21. *dst++ = readl_relaxed(priv->fuses + offset);
  22. bytes -= sizeof(u32);
  23. offset += sizeof(u32);
  24. }
  25. return 0;
  26. }
  27. static int apple_efuses_probe(struct platform_device *pdev)
  28. {
  29. struct apple_efuses_priv *priv;
  30. struct resource *res;
  31. struct nvmem_config config = {
  32. .dev = &pdev->dev,
  33. .read_only = true,
  34. .reg_read = apple_efuses_read,
  35. .stride = sizeof(u32),
  36. .word_size = sizeof(u32),
  37. .name = "apple_efuses_nvmem",
  38. .id = NVMEM_DEVID_AUTO,
  39. .root_only = true,
  40. };
  41. priv = devm_kzalloc(config.dev, sizeof(*priv), GFP_KERNEL);
  42. if (!priv)
  43. return -ENOMEM;
  44. priv->fuses = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
  45. if (IS_ERR(priv->fuses))
  46. return PTR_ERR(priv->fuses);
  47. config.priv = priv;
  48. config.size = resource_size(res);
  49. return PTR_ERR_OR_ZERO(devm_nvmem_register(config.dev, &config));
  50. }
  51. static const struct of_device_id apple_efuses_of_match[] = {
  52. { .compatible = "apple,efuses", },
  53. {}
  54. };
  55. MODULE_DEVICE_TABLE(of, apple_efuses_of_match);
  56. static struct platform_driver apple_efuses_driver = {
  57. .driver = {
  58. .name = "apple_efuses",
  59. .of_match_table = apple_efuses_of_match,
  60. },
  61. .probe = apple_efuses_probe,
  62. };
  63. module_platform_driver(apple_efuses_driver);
  64. MODULE_AUTHOR("Sven Peter <[email protected]>");
  65. MODULE_LICENSE("GPL");