sc27xx-efuse.c 7.2 KB

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