sc27xx-efuse.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (C) 2018 Spreadtrum Communications Inc.
  3. #include <linux/hwspinlock.h>
  4. #include <linux/module.h>
  5. #include <linux/of.h>
  6. #include <linux/platform_device.h>
  7. #include <linux/regmap.h>
  8. #include <linux/nvmem-provider.h>
  9. /* PMIC global registers definition */
  10. #define SC27XX_MODULE_EN 0xc08
  11. #define SC27XX_EFUSE_EN BIT(6)
  12. /* Efuse controller registers definition */
  13. #define SC27XX_EFUSE_GLB_CTRL 0x0
  14. #define SC27XX_EFUSE_DATA_RD 0x4
  15. #define SC27XX_EFUSE_DATA_WR 0x8
  16. #define SC27XX_EFUSE_BLOCK_INDEX 0xc
  17. #define SC27XX_EFUSE_MODE_CTRL 0x10
  18. #define SC27XX_EFUSE_STATUS 0x14
  19. #define SC27XX_EFUSE_WR_TIMING_CTRL 0x20
  20. #define SC27XX_EFUSE_RD_TIMING_CTRL 0x24
  21. #define SC27XX_EFUSE_EFUSE_DEB_CTRL 0x28
  22. /* Mask definition for SC27XX_EFUSE_BLOCK_INDEX register */
  23. #define SC27XX_EFUSE_BLOCK_MASK GENMASK(4, 0)
  24. /* Bits definitions for SC27XX_EFUSE_MODE_CTRL register */
  25. #define SC27XX_EFUSE_PG_START BIT(0)
  26. #define SC27XX_EFUSE_RD_START BIT(1)
  27. #define SC27XX_EFUSE_CLR_RDDONE BIT(2)
  28. /* Bits definitions for SC27XX_EFUSE_STATUS register */
  29. #define SC27XX_EFUSE_PGM_BUSY BIT(0)
  30. #define SC27XX_EFUSE_READ_BUSY BIT(1)
  31. #define SC27XX_EFUSE_STANDBY BIT(2)
  32. #define SC27XX_EFUSE_GLOBAL_PROT BIT(3)
  33. #define SC27XX_EFUSE_RD_DONE BIT(4)
  34. /* Block number and block width (bytes) definitions */
  35. #define SC27XX_EFUSE_BLOCK_MAX 32
  36. #define SC27XX_EFUSE_BLOCK_WIDTH 2
  37. /* Timeout (ms) for the trylock of hardware spinlocks */
  38. #define SC27XX_EFUSE_HWLOCK_TIMEOUT 5000
  39. /* Timeout (us) of polling the status */
  40. #define SC27XX_EFUSE_POLL_TIMEOUT 3000000
  41. #define SC27XX_EFUSE_POLL_DELAY_US 10000
  42. struct sc27xx_efuse {
  43. struct device *dev;
  44. struct regmap *regmap;
  45. struct hwspinlock *hwlock;
  46. struct mutex mutex;
  47. u32 base;
  48. };
  49. /*
  50. * On Spreadtrum platform, we have multi-subsystems will access the unique
  51. * efuse controller, so we need one hardware spinlock to synchronize between
  52. * the multiple subsystems.
  53. */
  54. static int sc27xx_efuse_lock(struct sc27xx_efuse *efuse)
  55. {
  56. int ret;
  57. mutex_lock(&efuse->mutex);
  58. ret = hwspin_lock_timeout_raw(efuse->hwlock,
  59. SC27XX_EFUSE_HWLOCK_TIMEOUT);
  60. if (ret) {
  61. dev_err(efuse->dev, "timeout to get the hwspinlock\n");
  62. mutex_unlock(&efuse->mutex);
  63. return ret;
  64. }
  65. return 0;
  66. }
  67. static void sc27xx_efuse_unlock(struct sc27xx_efuse *efuse)
  68. {
  69. hwspin_unlock_raw(efuse->hwlock);
  70. mutex_unlock(&efuse->mutex);
  71. }
  72. static int sc27xx_efuse_poll_status(struct sc27xx_efuse *efuse, u32 bits)
  73. {
  74. int ret;
  75. u32 val;
  76. ret = regmap_read_poll_timeout(efuse->regmap,
  77. efuse->base + SC27XX_EFUSE_STATUS,
  78. val, (val & bits),
  79. SC27XX_EFUSE_POLL_DELAY_US,
  80. SC27XX_EFUSE_POLL_TIMEOUT);
  81. if (ret) {
  82. dev_err(efuse->dev, "timeout to update the efuse status\n");
  83. return ret;
  84. }
  85. return 0;
  86. }
  87. static int sc27xx_efuse_read(void *context, u32 offset, void *val, size_t bytes)
  88. {
  89. struct sc27xx_efuse *efuse = context;
  90. u32 buf;
  91. int ret;
  92. if (offset > SC27XX_EFUSE_BLOCK_MAX || bytes > SC27XX_EFUSE_BLOCK_WIDTH)
  93. return -EINVAL;
  94. ret = sc27xx_efuse_lock(efuse);
  95. if (ret)
  96. return ret;
  97. /* Enable the efuse controller. */
  98. ret = regmap_update_bits(efuse->regmap, SC27XX_MODULE_EN,
  99. SC27XX_EFUSE_EN, SC27XX_EFUSE_EN);
  100. if (ret)
  101. goto unlock_efuse;
  102. /*
  103. * Before reading, we should ensure the efuse controller is in
  104. * standby state.
  105. */
  106. ret = sc27xx_efuse_poll_status(efuse, SC27XX_EFUSE_STANDBY);
  107. if (ret)
  108. goto disable_efuse;
  109. /* Set the block address to be read. */
  110. ret = regmap_write(efuse->regmap,
  111. efuse->base + SC27XX_EFUSE_BLOCK_INDEX,
  112. offset & SC27XX_EFUSE_BLOCK_MASK);
  113. if (ret)
  114. goto disable_efuse;
  115. /* Start reading process from efuse memory. */
  116. ret = regmap_update_bits(efuse->regmap,
  117. efuse->base + SC27XX_EFUSE_MODE_CTRL,
  118. SC27XX_EFUSE_RD_START,
  119. SC27XX_EFUSE_RD_START);
  120. if (ret)
  121. goto disable_efuse;
  122. /*
  123. * Polling the read done status to make sure the reading process
  124. * is completed, that means the data can be read out now.
  125. */
  126. ret = sc27xx_efuse_poll_status(efuse, SC27XX_EFUSE_RD_DONE);
  127. if (ret)
  128. goto disable_efuse;
  129. /* Read data from efuse memory. */
  130. ret = regmap_read(efuse->regmap, efuse->base + SC27XX_EFUSE_DATA_RD,
  131. &buf);
  132. if (ret)
  133. goto disable_efuse;
  134. /* Clear the read done flag. */
  135. ret = regmap_update_bits(efuse->regmap,
  136. efuse->base + SC27XX_EFUSE_MODE_CTRL,
  137. SC27XX_EFUSE_CLR_RDDONE,
  138. SC27XX_EFUSE_CLR_RDDONE);
  139. disable_efuse:
  140. /* Disable the efuse controller after reading. */
  141. regmap_update_bits(efuse->regmap, SC27XX_MODULE_EN, SC27XX_EFUSE_EN, 0);
  142. unlock_efuse:
  143. sc27xx_efuse_unlock(efuse);
  144. if (!ret)
  145. memcpy(val, &buf, bytes);
  146. return ret;
  147. }
  148. static int sc27xx_efuse_probe(struct platform_device *pdev)
  149. {
  150. struct device_node *np = pdev->dev.of_node;
  151. struct nvmem_config econfig = { };
  152. struct nvmem_device *nvmem;
  153. struct sc27xx_efuse *efuse;
  154. int ret;
  155. efuse = devm_kzalloc(&pdev->dev, sizeof(*efuse), GFP_KERNEL);
  156. if (!efuse)
  157. return -ENOMEM;
  158. efuse->regmap = dev_get_regmap(pdev->dev.parent, NULL);
  159. if (!efuse->regmap) {
  160. dev_err(&pdev->dev, "failed to get efuse regmap\n");
  161. return -ENODEV;
  162. }
  163. ret = of_property_read_u32(np, "reg", &efuse->base);
  164. if (ret) {
  165. dev_err(&pdev->dev, "failed to get efuse base address\n");
  166. return ret;
  167. }
  168. ret = of_hwspin_lock_get_id(np, 0);
  169. if (ret < 0) {
  170. dev_err(&pdev->dev, "failed to get hwspinlock id\n");
  171. return ret;
  172. }
  173. efuse->hwlock = hwspin_lock_request_specific(ret);
  174. if (!efuse->hwlock) {
  175. dev_err(&pdev->dev, "failed to request hwspinlock\n");
  176. return -ENXIO;
  177. }
  178. mutex_init(&efuse->mutex);
  179. efuse->dev = &pdev->dev;
  180. platform_set_drvdata(pdev, efuse);
  181. econfig.stride = 1;
  182. econfig.word_size = 1;
  183. econfig.read_only = true;
  184. econfig.name = "sc27xx-efuse";
  185. econfig.size = SC27XX_EFUSE_BLOCK_MAX * SC27XX_EFUSE_BLOCK_WIDTH;
  186. econfig.reg_read = sc27xx_efuse_read;
  187. econfig.priv = efuse;
  188. econfig.dev = &pdev->dev;
  189. nvmem = devm_nvmem_register(&pdev->dev, &econfig);
  190. if (IS_ERR(nvmem)) {
  191. dev_err(&pdev->dev, "failed to register nvmem config\n");
  192. hwspin_lock_free(efuse->hwlock);
  193. return PTR_ERR(nvmem);
  194. }
  195. return 0;
  196. }
  197. static int sc27xx_efuse_remove(struct platform_device *pdev)
  198. {
  199. struct sc27xx_efuse *efuse = platform_get_drvdata(pdev);
  200. hwspin_lock_free(efuse->hwlock);
  201. return 0;
  202. }
  203. static const struct of_device_id sc27xx_efuse_of_match[] = {
  204. { .compatible = "sprd,sc2731-efuse" },
  205. { }
  206. };
  207. static struct platform_driver sc27xx_efuse_driver = {
  208. .probe = sc27xx_efuse_probe,
  209. .remove = sc27xx_efuse_remove,
  210. .driver = {
  211. .name = "sc27xx-efuse",
  212. .of_match_table = sc27xx_efuse_of_match,
  213. },
  214. };
  215. module_platform_driver(sc27xx_efuse_driver);
  216. MODULE_AUTHOR("Freeman Liu <freeman.liu@spreadtrum.com>");
  217. MODULE_DESCRIPTION("Spreadtrum SC27xx efuse driver");
  218. MODULE_LICENSE("GPL v2");