rockchip-efuse.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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/platform_device.h>
  17. #include <linux/nvmem-provider.h>
  18. #include <linux/slab.h>
  19. #include <linux/regmap.h>
  20. #include <linux/device.h>
  21. #include <linux/io.h>
  22. #include <linux/module.h>
  23. #include <linux/delay.h>
  24. #include <linux/of.h>
  25. #include <linux/clk.h>
  26. #define EFUSE_A_SHIFT 6
  27. #define EFUSE_A_MASK 0x3ff
  28. #define EFUSE_PGENB BIT(3)
  29. #define EFUSE_LOAD BIT(2)
  30. #define EFUSE_STROBE BIT(1)
  31. #define EFUSE_CSB BIT(0)
  32. #define REG_EFUSE_CTRL 0x0000
  33. #define REG_EFUSE_DOUT 0x0004
  34. struct rockchip_efuse_context {
  35. struct device *dev;
  36. void __iomem *base;
  37. struct clk *efuse_clk;
  38. };
  39. static int rockchip_efuse_write(void *context, const void *data, size_t count)
  40. {
  41. /* Nothing TBD, Read-Only */
  42. return 0;
  43. }
  44. static int rockchip_efuse_read(void *context,
  45. const void *reg, size_t reg_size,
  46. void *val, size_t val_size)
  47. {
  48. unsigned int offset = *(u32 *)reg;
  49. struct rockchip_efuse_context *_context = context;
  50. void __iomem *base = _context->base;
  51. struct clk *clk = _context->efuse_clk;
  52. u8 *buf = val;
  53. int ret;
  54. ret = clk_prepare_enable(clk);
  55. if (ret < 0) {
  56. dev_err(_context->dev, "failed to prepare/enable efuse clk\n");
  57. return ret;
  58. }
  59. writel(EFUSE_LOAD | EFUSE_PGENB, base + REG_EFUSE_CTRL);
  60. udelay(1);
  61. while (val_size) {
  62. writel(readl(base + REG_EFUSE_CTRL) &
  63. (~(EFUSE_A_MASK << EFUSE_A_SHIFT)),
  64. base + REG_EFUSE_CTRL);
  65. writel(readl(base + REG_EFUSE_CTRL) |
  66. ((offset & EFUSE_A_MASK) << EFUSE_A_SHIFT),
  67. base + REG_EFUSE_CTRL);
  68. udelay(1);
  69. writel(readl(base + REG_EFUSE_CTRL) |
  70. EFUSE_STROBE, base + REG_EFUSE_CTRL);
  71. udelay(1);
  72. *buf++ = readb(base + REG_EFUSE_DOUT);
  73. writel(readl(base + REG_EFUSE_CTRL) &
  74. (~EFUSE_STROBE), base + REG_EFUSE_CTRL);
  75. udelay(1);
  76. val_size -= 1;
  77. offset += 1;
  78. }
  79. /* Switch to standby mode */
  80. writel(EFUSE_PGENB | EFUSE_CSB, base + REG_EFUSE_CTRL);
  81. clk_disable_unprepare(clk);
  82. return 0;
  83. }
  84. static struct regmap_bus rockchip_efuse_bus = {
  85. .read = rockchip_efuse_read,
  86. .write = rockchip_efuse_write,
  87. .reg_format_endian_default = REGMAP_ENDIAN_NATIVE,
  88. .val_format_endian_default = REGMAP_ENDIAN_NATIVE,
  89. };
  90. static struct regmap_config rockchip_efuse_regmap_config = {
  91. .reg_bits = 32,
  92. .reg_stride = 1,
  93. .val_bits = 8,
  94. };
  95. static struct nvmem_config econfig = {
  96. .name = "rockchip-efuse",
  97. .owner = THIS_MODULE,
  98. .read_only = true,
  99. };
  100. static const struct of_device_id rockchip_efuse_match[] = {
  101. { .compatible = "rockchip,rockchip-efuse",},
  102. { /* sentinel */},
  103. };
  104. MODULE_DEVICE_TABLE(of, rockchip_efuse_match);
  105. static int rockchip_efuse_probe(struct platform_device *pdev)
  106. {
  107. struct device *dev = &pdev->dev;
  108. struct resource *res;
  109. struct nvmem_device *nvmem;
  110. struct regmap *regmap;
  111. void __iomem *base;
  112. struct clk *clk;
  113. struct rockchip_efuse_context *context;
  114. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  115. base = devm_ioremap_resource(dev, res);
  116. if (IS_ERR(base))
  117. return PTR_ERR(base);
  118. context = devm_kzalloc(dev, sizeof(struct rockchip_efuse_context),
  119. GFP_KERNEL);
  120. if (IS_ERR(context))
  121. return PTR_ERR(context);
  122. clk = devm_clk_get(dev, "pclk_efuse");
  123. if (IS_ERR(clk))
  124. return PTR_ERR(clk);
  125. context->dev = dev;
  126. context->base = base;
  127. context->efuse_clk = clk;
  128. rockchip_efuse_regmap_config.max_register = resource_size(res) - 1;
  129. regmap = devm_regmap_init(dev, &rockchip_efuse_bus,
  130. context, &rockchip_efuse_regmap_config);
  131. if (IS_ERR(regmap)) {
  132. dev_err(dev, "regmap init failed\n");
  133. return PTR_ERR(regmap);
  134. }
  135. econfig.dev = dev;
  136. nvmem = nvmem_register(&econfig);
  137. if (IS_ERR(nvmem))
  138. return PTR_ERR(nvmem);
  139. platform_set_drvdata(pdev, nvmem);
  140. return 0;
  141. }
  142. static int rockchip_efuse_remove(struct platform_device *pdev)
  143. {
  144. struct nvmem_device *nvmem = platform_get_drvdata(pdev);
  145. return nvmem_unregister(nvmem);
  146. }
  147. static struct platform_driver rockchip_efuse_driver = {
  148. .probe = rockchip_efuse_probe,
  149. .remove = rockchip_efuse_remove,
  150. .driver = {
  151. .name = "rockchip-efuse",
  152. .of_match_table = rockchip_efuse_match,
  153. },
  154. };
  155. module_platform_driver(rockchip_efuse_driver);
  156. MODULE_DESCRIPTION("rockchip_efuse driver");
  157. MODULE_LICENSE("GPL v2");