rockchip-efuse.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * Rockchip eFuse Driver
  3. *
  4. * Copyright (c) 2015 Rockchip Electronics Co. Ltd.
  5. * Author: Caesar Wang <wxt@rock-chips.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of version 2 of the GNU General Public License as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. */
  16. #include <linux/clk.h>
  17. #include <linux/delay.h>
  18. #include <linux/device.h>
  19. #include <linux/io.h>
  20. #include <linux/module.h>
  21. #include <linux/nvmem-provider.h>
  22. #include <linux/slab.h>
  23. #include <linux/of.h>
  24. #include <linux/of_platform.h>
  25. #include <linux/platform_device.h>
  26. #define RK3288_A_SHIFT 6
  27. #define RK3288_A_MASK 0x3ff
  28. #define RK3288_PGENB BIT(3)
  29. #define RK3288_LOAD BIT(2)
  30. #define RK3288_STROBE BIT(1)
  31. #define RK3288_CSB BIT(0)
  32. #define RK3328_SECURE_SIZES 96
  33. #define RK3328_INT_STATUS 0x0018
  34. #define RK3328_DOUT 0x0020
  35. #define RK3328_AUTO_CTRL 0x0024
  36. #define RK3328_INT_FINISH BIT(0)
  37. #define RK3328_AUTO_ENB BIT(0)
  38. #define RK3328_AUTO_RD BIT(1)
  39. #define RK3399_A_SHIFT 16
  40. #define RK3399_A_MASK 0x3ff
  41. #define RK3399_NBYTES 4
  42. #define RK3399_STROBSFTSEL BIT(9)
  43. #define RK3399_RSB BIT(7)
  44. #define RK3399_PD BIT(5)
  45. #define RK3399_PGENB BIT(3)
  46. #define RK3399_LOAD BIT(2)
  47. #define RK3399_STROBE BIT(1)
  48. #define RK3399_CSB BIT(0)
  49. #define REG_EFUSE_CTRL 0x0000
  50. #define REG_EFUSE_DOUT 0x0004
  51. struct rockchip_efuse_chip {
  52. struct device *dev;
  53. void __iomem *base;
  54. struct clk *clk;
  55. };
  56. static int rockchip_rk3288_efuse_read(void *context, unsigned int offset,
  57. void *val, size_t bytes)
  58. {
  59. struct rockchip_efuse_chip *efuse = context;
  60. u8 *buf = val;
  61. int ret;
  62. ret = clk_prepare_enable(efuse->clk);
  63. if (ret < 0) {
  64. dev_err(efuse->dev, "failed to prepare/enable efuse clk\n");
  65. return ret;
  66. }
  67. writel(RK3288_LOAD | RK3288_PGENB, efuse->base + REG_EFUSE_CTRL);
  68. udelay(1);
  69. while (bytes--) {
  70. writel(readl(efuse->base + REG_EFUSE_CTRL) &
  71. (~(RK3288_A_MASK << RK3288_A_SHIFT)),
  72. efuse->base + REG_EFUSE_CTRL);
  73. writel(readl(efuse->base + REG_EFUSE_CTRL) |
  74. ((offset++ & RK3288_A_MASK) << RK3288_A_SHIFT),
  75. efuse->base + REG_EFUSE_CTRL);
  76. udelay(1);
  77. writel(readl(efuse->base + REG_EFUSE_CTRL) |
  78. RK3288_STROBE, efuse->base + REG_EFUSE_CTRL);
  79. udelay(1);
  80. *buf++ = readb(efuse->base + REG_EFUSE_DOUT);
  81. writel(readl(efuse->base + REG_EFUSE_CTRL) &
  82. (~RK3288_STROBE), efuse->base + REG_EFUSE_CTRL);
  83. udelay(1);
  84. }
  85. /* Switch to standby mode */
  86. writel(RK3288_PGENB | RK3288_CSB, efuse->base + REG_EFUSE_CTRL);
  87. clk_disable_unprepare(efuse->clk);
  88. return 0;
  89. }
  90. static int rockchip_rk3328_efuse_read(void *context, unsigned int offset,
  91. void *val, size_t bytes)
  92. {
  93. struct rockchip_efuse_chip *efuse = context;
  94. unsigned int addr_start, addr_end, addr_offset, addr_len;
  95. u32 out_value, status;
  96. u8 *buf;
  97. int ret, i = 0;
  98. ret = clk_prepare_enable(efuse->clk);
  99. if (ret < 0) {
  100. dev_err(efuse->dev, "failed to prepare/enable efuse clk\n");
  101. return ret;
  102. }
  103. /* 128 Byte efuse, 96 Byte for secure, 32 Byte for non-secure */
  104. offset += RK3328_SECURE_SIZES;
  105. addr_start = rounddown(offset, RK3399_NBYTES) / RK3399_NBYTES;
  106. addr_end = roundup(offset + bytes, RK3399_NBYTES) / RK3399_NBYTES;
  107. addr_offset = offset % RK3399_NBYTES;
  108. addr_len = addr_end - addr_start;
  109. buf = kzalloc(array3_size(addr_len, RK3399_NBYTES, sizeof(*buf)),
  110. GFP_KERNEL);
  111. if (!buf) {
  112. ret = -ENOMEM;
  113. goto nomem;
  114. }
  115. while (addr_len--) {
  116. writel(RK3328_AUTO_RD | RK3328_AUTO_ENB |
  117. ((addr_start++ & RK3399_A_MASK) << RK3399_A_SHIFT),
  118. efuse->base + RK3328_AUTO_CTRL);
  119. udelay(4);
  120. status = readl(efuse->base + RK3328_INT_STATUS);
  121. if (!(status & RK3328_INT_FINISH)) {
  122. ret = -EIO;
  123. goto err;
  124. }
  125. out_value = readl(efuse->base + RK3328_DOUT);
  126. writel(RK3328_INT_FINISH, efuse->base + RK3328_INT_STATUS);
  127. memcpy(&buf[i], &out_value, RK3399_NBYTES);
  128. i += RK3399_NBYTES;
  129. }
  130. memcpy(val, buf + addr_offset, bytes);
  131. err:
  132. kfree(buf);
  133. nomem:
  134. clk_disable_unprepare(efuse->clk);
  135. return ret;
  136. }
  137. static int rockchip_rk3399_efuse_read(void *context, unsigned int offset,
  138. void *val, size_t bytes)
  139. {
  140. struct rockchip_efuse_chip *efuse = context;
  141. unsigned int addr_start, addr_end, addr_offset, addr_len;
  142. u32 out_value;
  143. u8 *buf;
  144. int ret, i = 0;
  145. ret = clk_prepare_enable(efuse->clk);
  146. if (ret < 0) {
  147. dev_err(efuse->dev, "failed to prepare/enable efuse clk\n");
  148. return ret;
  149. }
  150. addr_start = rounddown(offset, RK3399_NBYTES) / RK3399_NBYTES;
  151. addr_end = roundup(offset + bytes, RK3399_NBYTES) / RK3399_NBYTES;
  152. addr_offset = offset % RK3399_NBYTES;
  153. addr_len = addr_end - addr_start;
  154. buf = kzalloc(array3_size(addr_len, RK3399_NBYTES, sizeof(*buf)),
  155. GFP_KERNEL);
  156. if (!buf) {
  157. clk_disable_unprepare(efuse->clk);
  158. return -ENOMEM;
  159. }
  160. writel(RK3399_LOAD | RK3399_PGENB | RK3399_STROBSFTSEL | RK3399_RSB,
  161. efuse->base + REG_EFUSE_CTRL);
  162. udelay(1);
  163. while (addr_len--) {
  164. writel(readl(efuse->base + REG_EFUSE_CTRL) | RK3399_STROBE |
  165. ((addr_start++ & RK3399_A_MASK) << RK3399_A_SHIFT),
  166. efuse->base + REG_EFUSE_CTRL);
  167. udelay(1);
  168. out_value = readl(efuse->base + REG_EFUSE_DOUT);
  169. writel(readl(efuse->base + REG_EFUSE_CTRL) & (~RK3399_STROBE),
  170. efuse->base + REG_EFUSE_CTRL);
  171. udelay(1);
  172. memcpy(&buf[i], &out_value, RK3399_NBYTES);
  173. i += RK3399_NBYTES;
  174. }
  175. /* Switch to standby mode */
  176. writel(RK3399_PD | RK3399_CSB, efuse->base + REG_EFUSE_CTRL);
  177. memcpy(val, buf + addr_offset, bytes);
  178. kfree(buf);
  179. clk_disable_unprepare(efuse->clk);
  180. return 0;
  181. }
  182. static struct nvmem_config econfig = {
  183. .name = "rockchip-efuse",
  184. .stride = 1,
  185. .word_size = 1,
  186. .read_only = true,
  187. };
  188. static const struct of_device_id rockchip_efuse_match[] = {
  189. /* deprecated but kept around for dts binding compatibility */
  190. {
  191. .compatible = "rockchip,rockchip-efuse",
  192. .data = (void *)&rockchip_rk3288_efuse_read,
  193. },
  194. {
  195. .compatible = "rockchip,rk3066a-efuse",
  196. .data = (void *)&rockchip_rk3288_efuse_read,
  197. },
  198. {
  199. .compatible = "rockchip,rk3188-efuse",
  200. .data = (void *)&rockchip_rk3288_efuse_read,
  201. },
  202. {
  203. .compatible = "rockchip,rk3228-efuse",
  204. .data = (void *)&rockchip_rk3288_efuse_read,
  205. },
  206. {
  207. .compatible = "rockchip,rk3288-efuse",
  208. .data = (void *)&rockchip_rk3288_efuse_read,
  209. },
  210. {
  211. .compatible = "rockchip,rk3368-efuse",
  212. .data = (void *)&rockchip_rk3288_efuse_read,
  213. },
  214. {
  215. .compatible = "rockchip,rk3328-efuse",
  216. .data = (void *)&rockchip_rk3328_efuse_read,
  217. },
  218. {
  219. .compatible = "rockchip,rk3399-efuse",
  220. .data = (void *)&rockchip_rk3399_efuse_read,
  221. },
  222. { /* sentinel */},
  223. };
  224. MODULE_DEVICE_TABLE(of, rockchip_efuse_match);
  225. static int rockchip_efuse_probe(struct platform_device *pdev)
  226. {
  227. struct resource *res;
  228. struct nvmem_device *nvmem;
  229. struct rockchip_efuse_chip *efuse;
  230. const void *data;
  231. struct device *dev = &pdev->dev;
  232. data = of_device_get_match_data(dev);
  233. if (!data) {
  234. dev_err(dev, "failed to get match data\n");
  235. return -EINVAL;
  236. }
  237. efuse = devm_kzalloc(dev, sizeof(struct rockchip_efuse_chip),
  238. GFP_KERNEL);
  239. if (!efuse)
  240. return -ENOMEM;
  241. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  242. efuse->base = devm_ioremap_resource(dev, res);
  243. if (IS_ERR(efuse->base))
  244. return PTR_ERR(efuse->base);
  245. efuse->clk = devm_clk_get(dev, "pclk_efuse");
  246. if (IS_ERR(efuse->clk))
  247. return PTR_ERR(efuse->clk);
  248. efuse->dev = dev;
  249. if (of_property_read_u32(dev->of_node, "rockchip,efuse-size",
  250. &econfig.size))
  251. econfig.size = resource_size(res);
  252. econfig.reg_read = data;
  253. econfig.priv = efuse;
  254. econfig.dev = efuse->dev;
  255. nvmem = devm_nvmem_register(dev, &econfig);
  256. return PTR_ERR_OR_ZERO(nvmem);
  257. }
  258. static struct platform_driver rockchip_efuse_driver = {
  259. .probe = rockchip_efuse_probe,
  260. .driver = {
  261. .name = "rockchip-efuse",
  262. .of_match_table = rockchip_efuse_match,
  263. },
  264. };
  265. module_platform_driver(rockchip_efuse_driver);
  266. MODULE_DESCRIPTION("rockchip_efuse driver");
  267. MODULE_LICENSE("GPL v2");