soc-imx-scu.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright 2019 NXP.
  4. */
  5. #include <dt-bindings/firmware/imx/rsrc.h>
  6. #include <linux/firmware/imx/sci.h>
  7. #include <linux/slab.h>
  8. #include <linux/sys_soc.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/of.h>
  11. #define IMX_SCU_SOC_DRIVER_NAME "imx-scu-soc"
  12. static struct imx_sc_ipc *soc_ipc_handle;
  13. struct imx_sc_msg_misc_get_soc_id {
  14. struct imx_sc_rpc_msg hdr;
  15. union {
  16. struct {
  17. u32 control;
  18. u16 resource;
  19. } __packed req;
  20. struct {
  21. u32 id;
  22. } resp;
  23. } data;
  24. } __packed __aligned(4);
  25. struct imx_sc_msg_misc_get_soc_uid {
  26. struct imx_sc_rpc_msg hdr;
  27. u32 uid_low;
  28. u32 uid_high;
  29. } __packed;
  30. static ssize_t soc_uid_show(struct device *dev,
  31. struct device_attribute *attr, char *buf)
  32. {
  33. struct imx_sc_msg_misc_get_soc_uid msg;
  34. struct imx_sc_rpc_msg *hdr = &msg.hdr;
  35. u64 soc_uid;
  36. int ret;
  37. hdr->ver = IMX_SC_RPC_VERSION;
  38. hdr->svc = IMX_SC_RPC_SVC_MISC;
  39. hdr->func = IMX_SC_MISC_FUNC_UNIQUE_ID;
  40. hdr->size = 1;
  41. ret = imx_scu_call_rpc(soc_ipc_handle, &msg, true);
  42. if (ret) {
  43. pr_err("%s: get soc uid failed, ret %d\n", __func__, ret);
  44. return ret;
  45. }
  46. soc_uid = msg.uid_high;
  47. soc_uid <<= 32;
  48. soc_uid |= msg.uid_low;
  49. return sprintf(buf, "%016llX\n", soc_uid);
  50. }
  51. static DEVICE_ATTR_RO(soc_uid);
  52. static int imx_scu_soc_id(void)
  53. {
  54. struct imx_sc_msg_misc_get_soc_id msg;
  55. struct imx_sc_rpc_msg *hdr = &msg.hdr;
  56. int ret;
  57. hdr->ver = IMX_SC_RPC_VERSION;
  58. hdr->svc = IMX_SC_RPC_SVC_MISC;
  59. hdr->func = IMX_SC_MISC_FUNC_GET_CONTROL;
  60. hdr->size = 3;
  61. msg.data.req.control = IMX_SC_C_ID;
  62. msg.data.req.resource = IMX_SC_R_SYSTEM;
  63. ret = imx_scu_call_rpc(soc_ipc_handle, &msg, true);
  64. if (ret) {
  65. pr_err("%s: get soc info failed, ret %d\n", __func__, ret);
  66. return ret;
  67. }
  68. return msg.data.resp.id;
  69. }
  70. static int imx_scu_soc_probe(struct platform_device *pdev)
  71. {
  72. struct soc_device_attribute *soc_dev_attr;
  73. struct soc_device *soc_dev;
  74. int id, ret;
  75. u32 val;
  76. ret = imx_scu_get_handle(&soc_ipc_handle);
  77. if (ret)
  78. return ret;
  79. soc_dev_attr = devm_kzalloc(&pdev->dev, sizeof(*soc_dev_attr),
  80. GFP_KERNEL);
  81. if (!soc_dev_attr)
  82. return -ENOMEM;
  83. soc_dev_attr->family = "Freescale i.MX";
  84. ret = of_property_read_string(of_root,
  85. "model",
  86. &soc_dev_attr->machine);
  87. if (ret)
  88. return ret;
  89. id = imx_scu_soc_id();
  90. if (id < 0)
  91. return -EINVAL;
  92. /* format soc_id value passed from SCU firmware */
  93. val = id & 0x1f;
  94. soc_dev_attr->soc_id = kasprintf(GFP_KERNEL, "0x%x", val);
  95. if (!soc_dev_attr->soc_id)
  96. return -ENOMEM;
  97. /* format revision value passed from SCU firmware */
  98. val = (id >> 5) & 0xf;
  99. val = (((val >> 2) + 1) << 4) | (val & 0x3);
  100. soc_dev_attr->revision = kasprintf(GFP_KERNEL,
  101. "%d.%d",
  102. (val >> 4) & 0xf,
  103. val & 0xf);
  104. if (!soc_dev_attr->revision) {
  105. ret = -ENOMEM;
  106. goto free_soc_id;
  107. }
  108. soc_dev = soc_device_register(soc_dev_attr);
  109. if (IS_ERR(soc_dev)) {
  110. ret = PTR_ERR(soc_dev);
  111. goto free_revision;
  112. }
  113. ret = device_create_file(soc_device_to_device(soc_dev),
  114. &dev_attr_soc_uid);
  115. if (ret)
  116. goto free_revision;
  117. return 0;
  118. free_revision:
  119. kfree(soc_dev_attr->revision);
  120. free_soc_id:
  121. kfree(soc_dev_attr->soc_id);
  122. return ret;
  123. }
  124. static struct platform_driver imx_scu_soc_driver = {
  125. .driver = {
  126. .name = IMX_SCU_SOC_DRIVER_NAME,
  127. },
  128. .probe = imx_scu_soc_probe,
  129. };
  130. static int __init imx_scu_soc_init(void)
  131. {
  132. struct platform_device *pdev;
  133. struct device_node *np;
  134. int ret;
  135. np = of_find_compatible_node(NULL, NULL, "fsl,imx-scu");
  136. if (!np)
  137. return -ENODEV;
  138. of_node_put(np);
  139. ret = platform_driver_register(&imx_scu_soc_driver);
  140. if (ret)
  141. return ret;
  142. pdev = platform_device_register_simple(IMX_SCU_SOC_DRIVER_NAME,
  143. -1, NULL, 0);
  144. if (IS_ERR(pdev))
  145. platform_driver_unregister(&imx_scu_soc_driver);
  146. return PTR_ERR_OR_ZERO(pdev);
  147. }
  148. device_initcall(imx_scu_soc_init);