imx-ocotp-ele.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * i.MX9 OCOTP fusebox driver
  4. *
  5. * Copyright 2023 NXP
  6. */
  7. #include <linux/device.h>
  8. #include <linux/io.h>
  9. #include <linux/module.h>
  10. #include <linux/nvmem-provider.h>
  11. #include <linux/of.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/slab.h>
  14. #include <linux/if_ether.h> /* ETH_ALEN */
  15. enum fuse_type {
  16. FUSE_FSB = BIT(0),
  17. FUSE_ELE = BIT(1),
  18. FUSE_ECC = BIT(2),
  19. FUSE_INVALID = -1
  20. };
  21. struct ocotp_map_entry {
  22. u32 start; /* start word */
  23. u32 num; /* num words */
  24. enum fuse_type type;
  25. };
  26. struct ocotp_devtype_data {
  27. u32 reg_off;
  28. char *name;
  29. u32 size;
  30. u32 num_entry;
  31. u32 flag;
  32. nvmem_reg_read_t reg_read;
  33. struct ocotp_map_entry entry[];
  34. };
  35. struct imx_ocotp_priv {
  36. struct device *dev;
  37. void __iomem *base;
  38. struct nvmem_config config;
  39. struct mutex lock;
  40. const struct ocotp_devtype_data *data;
  41. };
  42. static enum fuse_type imx_ocotp_fuse_type(void *context, u32 index)
  43. {
  44. struct imx_ocotp_priv *priv = context;
  45. const struct ocotp_devtype_data *data = priv->data;
  46. u32 start, end;
  47. int i;
  48. for (i = 0; i < data->num_entry; i++) {
  49. start = data->entry[i].start;
  50. end = data->entry[i].start + data->entry[i].num;
  51. if (index >= start && index < end)
  52. return data->entry[i].type;
  53. }
  54. return FUSE_INVALID;
  55. }
  56. static int imx_ocotp_reg_read(void *context, unsigned int offset, void *val, size_t bytes)
  57. {
  58. struct imx_ocotp_priv *priv = context;
  59. void __iomem *reg = priv->base + priv->data->reg_off;
  60. u32 count, index, num_bytes;
  61. enum fuse_type type;
  62. u32 *buf;
  63. void *p;
  64. int i;
  65. u8 skipbytes;
  66. if (offset + bytes > priv->data->size)
  67. bytes = priv->data->size - offset;
  68. index = offset >> 2;
  69. skipbytes = offset - (index << 2);
  70. num_bytes = round_up(bytes + skipbytes, 4);
  71. count = num_bytes >> 2;
  72. p = kzalloc(num_bytes, GFP_KERNEL);
  73. if (!p)
  74. return -ENOMEM;
  75. mutex_lock(&priv->lock);
  76. buf = p;
  77. for (i = index; i < (index + count); i++) {
  78. type = imx_ocotp_fuse_type(context, i);
  79. if (type == FUSE_INVALID || type == FUSE_ELE) {
  80. *buf++ = 0;
  81. continue;
  82. }
  83. if (type & FUSE_ECC)
  84. *buf++ = readl_relaxed(reg + (i << 2)) & GENMASK(15, 0);
  85. else
  86. *buf++ = readl_relaxed(reg + (i << 2));
  87. }
  88. memcpy(val, ((u8 *)p) + skipbytes, bytes);
  89. mutex_unlock(&priv->lock);
  90. kfree(p);
  91. return 0;
  92. };
  93. static int imx_ocotp_cell_pp(void *context, const char *id, int index,
  94. unsigned int offset, void *data, size_t bytes)
  95. {
  96. u8 *buf = data;
  97. int i;
  98. /* Deal with some post processing of nvmem cell data */
  99. if (id && !strcmp(id, "mac-address")) {
  100. bytes = min(bytes, ETH_ALEN);
  101. for (i = 0; i < bytes / 2; i++)
  102. swap(buf[i], buf[bytes - i - 1]);
  103. }
  104. return 0;
  105. }
  106. static void imx_ocotp_fixup_dt_cell_info(struct nvmem_device *nvmem,
  107. struct nvmem_cell_info *cell)
  108. {
  109. cell->read_post_process = imx_ocotp_cell_pp;
  110. }
  111. static int imx_ele_ocotp_probe(struct platform_device *pdev)
  112. {
  113. struct device *dev = &pdev->dev;
  114. struct imx_ocotp_priv *priv;
  115. struct nvmem_device *nvmem;
  116. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  117. if (!priv)
  118. return -ENOMEM;
  119. priv->data = of_device_get_match_data(dev);
  120. priv->base = devm_platform_ioremap_resource(pdev, 0);
  121. if (IS_ERR(priv->base))
  122. return PTR_ERR(priv->base);
  123. priv->config.dev = dev;
  124. priv->config.name = "ELE-OCOTP";
  125. priv->config.id = NVMEM_DEVID_AUTO;
  126. priv->config.owner = THIS_MODULE;
  127. priv->config.size = priv->data->size;
  128. priv->config.reg_read = priv->data->reg_read;
  129. priv->config.word_size = 1;
  130. priv->config.stride = 1;
  131. priv->config.priv = priv;
  132. priv->config.read_only = true;
  133. priv->config.add_legacy_fixed_of_cells = true;
  134. priv->config.fixup_dt_cell_info = imx_ocotp_fixup_dt_cell_info;
  135. mutex_init(&priv->lock);
  136. nvmem = devm_nvmem_register(dev, &priv->config);
  137. if (IS_ERR(nvmem))
  138. return PTR_ERR(nvmem);
  139. return 0;
  140. }
  141. static const struct ocotp_devtype_data imx93_ocotp_data = {
  142. .reg_off = 0x8000,
  143. .reg_read = imx_ocotp_reg_read,
  144. .size = 2048,
  145. .num_entry = 6,
  146. .entry = {
  147. { 0, 52, FUSE_FSB },
  148. { 63, 1, FUSE_ELE},
  149. { 128, 16, FUSE_ELE },
  150. { 182, 1, FUSE_ELE },
  151. { 188, 1, FUSE_ELE },
  152. { 312, 200, FUSE_FSB }
  153. },
  154. };
  155. static const struct ocotp_devtype_data imx95_ocotp_data = {
  156. .reg_off = 0x8000,
  157. .reg_read = imx_ocotp_reg_read,
  158. .size = 2048,
  159. .num_entry = 12,
  160. .entry = {
  161. { 0, 1, FUSE_FSB | FUSE_ECC },
  162. { 7, 1, FUSE_FSB | FUSE_ECC },
  163. { 9, 3, FUSE_FSB | FUSE_ECC },
  164. { 12, 24, FUSE_FSB },
  165. { 36, 2, FUSE_FSB | FUSE_ECC },
  166. { 38, 14, FUSE_FSB },
  167. { 63, 1, FUSE_ELE },
  168. { 128, 16, FUSE_ELE },
  169. { 188, 1, FUSE_ELE },
  170. { 317, 2, FUSE_FSB | FUSE_ECC },
  171. { 320, 7, FUSE_FSB },
  172. { 328, 184, FUSE_FSB }
  173. },
  174. };
  175. static const struct of_device_id imx_ele_ocotp_dt_ids[] = {
  176. { .compatible = "fsl,imx93-ocotp", .data = &imx93_ocotp_data, },
  177. { .compatible = "fsl,imx95-ocotp", .data = &imx95_ocotp_data, },
  178. {},
  179. };
  180. MODULE_DEVICE_TABLE(of, imx_ele_ocotp_dt_ids);
  181. static struct platform_driver imx_ele_ocotp_driver = {
  182. .driver = {
  183. .name = "imx_ele_ocotp",
  184. .of_match_table = imx_ele_ocotp_dt_ids,
  185. },
  186. .probe = imx_ele_ocotp_probe,
  187. };
  188. module_platform_driver(imx_ele_ocotp_driver);
  189. MODULE_DESCRIPTION("i.MX OCOTP/ELE driver");
  190. MODULE_AUTHOR("Peng Fan <peng.fan@nxp.com>");
  191. MODULE_LICENSE("GPL");