apple-efuses.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. .add_legacy_fixed_of_cells = true,
  34. .read_only = true,
  35. .reg_read = apple_efuses_read,
  36. .stride = sizeof(u32),
  37. .word_size = sizeof(u32),
  38. .name = "apple_efuses_nvmem",
  39. .id = NVMEM_DEVID_AUTO,
  40. .root_only = true,
  41. };
  42. priv = devm_kzalloc(config.dev, sizeof(*priv), GFP_KERNEL);
  43. if (!priv)
  44. return -ENOMEM;
  45. priv->fuses = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
  46. if (IS_ERR(priv->fuses))
  47. return PTR_ERR(priv->fuses);
  48. config.priv = priv;
  49. config.size = resource_size(res);
  50. return PTR_ERR_OR_ZERO(devm_nvmem_register(config.dev, &config));
  51. }
  52. static const struct of_device_id apple_efuses_of_match[] = {
  53. { .compatible = "apple,efuses", },
  54. {}
  55. };
  56. MODULE_DEVICE_TABLE(of, apple_efuses_of_match);
  57. static struct platform_driver apple_efuses_driver = {
  58. .driver = {
  59. .name = "apple_efuses",
  60. .of_match_table = apple_efuses_of_match,
  61. },
  62. .probe = apple_efuses_probe,
  63. };
  64. module_platform_driver(apple_efuses_driver);
  65. MODULE_AUTHOR("Sven Peter <sven@svenpeter.dev>");
  66. MODULE_DESCRIPTION("Apple SoC eFuse driver");
  67. MODULE_LICENSE("GPL");