fuse-tegra.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2013-2014, NVIDIA CORPORATION. All rights reserved.
  4. */
  5. #include <linux/clk.h>
  6. #include <linux/device.h>
  7. #include <linux/kobject.h>
  8. #include <linux/init.h>
  9. #include <linux/io.h>
  10. #include <linux/of.h>
  11. #include <linux/of_address.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/slab.h>
  14. #include <linux/sys_soc.h>
  15. #include <soc/tegra/common.h>
  16. #include <soc/tegra/fuse.h>
  17. #include "fuse.h"
  18. struct tegra_sku_info tegra_sku_info;
  19. EXPORT_SYMBOL(tegra_sku_info);
  20. static const char *tegra_revision_name[TEGRA_REVISION_MAX] = {
  21. [TEGRA_REVISION_UNKNOWN] = "unknown",
  22. [TEGRA_REVISION_A01] = "A01",
  23. [TEGRA_REVISION_A02] = "A02",
  24. [TEGRA_REVISION_A03] = "A03",
  25. [TEGRA_REVISION_A03p] = "A03 prime",
  26. [TEGRA_REVISION_A04] = "A04",
  27. };
  28. static u8 fuse_readb(struct tegra_fuse *fuse, unsigned int offset)
  29. {
  30. u32 val;
  31. val = fuse->read(fuse, round_down(offset, 4));
  32. val >>= (offset % 4) * 8;
  33. val &= 0xff;
  34. return val;
  35. }
  36. static ssize_t fuse_read(struct file *fd, struct kobject *kobj,
  37. struct bin_attribute *attr, char *buf,
  38. loff_t pos, size_t size)
  39. {
  40. struct device *dev = kobj_to_dev(kobj);
  41. struct tegra_fuse *fuse = dev_get_drvdata(dev);
  42. int i;
  43. if (pos < 0 || pos >= attr->size)
  44. return 0;
  45. if (size > attr->size - pos)
  46. size = attr->size - pos;
  47. for (i = 0; i < size; i++)
  48. buf[i] = fuse_readb(fuse, pos + i);
  49. return i;
  50. }
  51. static struct bin_attribute fuse_bin_attr = {
  52. .attr = { .name = "fuse", .mode = S_IRUGO, },
  53. .read = fuse_read,
  54. };
  55. static int tegra_fuse_create_sysfs(struct device *dev, unsigned int size,
  56. const struct tegra_fuse_info *info)
  57. {
  58. fuse_bin_attr.size = size;
  59. return device_create_bin_file(dev, &fuse_bin_attr);
  60. }
  61. static const struct of_device_id car_match[] __initconst = {
  62. { .compatible = "nvidia,tegra20-car", },
  63. { .compatible = "nvidia,tegra30-car", },
  64. { .compatible = "nvidia,tegra114-car", },
  65. { .compatible = "nvidia,tegra124-car", },
  66. { .compatible = "nvidia,tegra132-car", },
  67. { .compatible = "nvidia,tegra210-car", },
  68. {},
  69. };
  70. static struct tegra_fuse *fuse = &(struct tegra_fuse) {
  71. .base = NULL,
  72. .soc = NULL,
  73. };
  74. static const struct of_device_id tegra_fuse_match[] = {
  75. #ifdef CONFIG_ARCH_TEGRA_186_SOC
  76. { .compatible = "nvidia,tegra186-efuse", .data = &tegra186_fuse_soc },
  77. #endif
  78. #ifdef CONFIG_ARCH_TEGRA_210_SOC
  79. { .compatible = "nvidia,tegra210-efuse", .data = &tegra210_fuse_soc },
  80. #endif
  81. #ifdef CONFIG_ARCH_TEGRA_132_SOC
  82. { .compatible = "nvidia,tegra132-efuse", .data = &tegra124_fuse_soc },
  83. #endif
  84. #ifdef CONFIG_ARCH_TEGRA_124_SOC
  85. { .compatible = "nvidia,tegra124-efuse", .data = &tegra124_fuse_soc },
  86. #endif
  87. #ifdef CONFIG_ARCH_TEGRA_114_SOC
  88. { .compatible = "nvidia,tegra114-efuse", .data = &tegra114_fuse_soc },
  89. #endif
  90. #ifdef CONFIG_ARCH_TEGRA_3x_SOC
  91. { .compatible = "nvidia,tegra30-efuse", .data = &tegra30_fuse_soc },
  92. #endif
  93. #ifdef CONFIG_ARCH_TEGRA_2x_SOC
  94. { .compatible = "nvidia,tegra20-efuse", .data = &tegra20_fuse_soc },
  95. #endif
  96. { /* sentinel */ }
  97. };
  98. static int tegra_fuse_probe(struct platform_device *pdev)
  99. {
  100. void __iomem *base = fuse->base;
  101. struct resource *res;
  102. int err;
  103. /* take over the memory region from the early initialization */
  104. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  105. fuse->phys = res->start;
  106. fuse->base = devm_ioremap_resource(&pdev->dev, res);
  107. if (IS_ERR(fuse->base)) {
  108. err = PTR_ERR(fuse->base);
  109. fuse->base = base;
  110. return err;
  111. }
  112. fuse->clk = devm_clk_get(&pdev->dev, "fuse");
  113. if (IS_ERR(fuse->clk)) {
  114. if (PTR_ERR(fuse->clk) != -EPROBE_DEFER)
  115. dev_err(&pdev->dev, "failed to get FUSE clock: %ld",
  116. PTR_ERR(fuse->clk));
  117. fuse->base = base;
  118. return PTR_ERR(fuse->clk);
  119. }
  120. platform_set_drvdata(pdev, fuse);
  121. fuse->dev = &pdev->dev;
  122. if (fuse->soc->probe) {
  123. err = fuse->soc->probe(fuse);
  124. if (err < 0) {
  125. fuse->base = base;
  126. return err;
  127. }
  128. }
  129. if (tegra_fuse_create_sysfs(&pdev->dev, fuse->soc->info->size,
  130. fuse->soc->info))
  131. return -ENODEV;
  132. /* release the early I/O memory mapping */
  133. iounmap(base);
  134. return 0;
  135. }
  136. static struct platform_driver tegra_fuse_driver = {
  137. .driver = {
  138. .name = "tegra-fuse",
  139. .of_match_table = tegra_fuse_match,
  140. .suppress_bind_attrs = true,
  141. },
  142. .probe = tegra_fuse_probe,
  143. };
  144. builtin_platform_driver(tegra_fuse_driver);
  145. bool __init tegra_fuse_read_spare(unsigned int spare)
  146. {
  147. unsigned int offset = fuse->soc->info->spare + spare * 4;
  148. return fuse->read_early(fuse, offset) & 1;
  149. }
  150. u32 __init tegra_fuse_read_early(unsigned int offset)
  151. {
  152. return fuse->read_early(fuse, offset);
  153. }
  154. int tegra_fuse_readl(unsigned long offset, u32 *value)
  155. {
  156. if (!fuse->read)
  157. return -EPROBE_DEFER;
  158. *value = fuse->read(fuse, offset);
  159. return 0;
  160. }
  161. EXPORT_SYMBOL(tegra_fuse_readl);
  162. static void tegra_enable_fuse_clk(void __iomem *base)
  163. {
  164. u32 reg;
  165. reg = readl_relaxed(base + 0x48);
  166. reg |= 1 << 28;
  167. writel(reg, base + 0x48);
  168. /*
  169. * Enable FUSE clock. This needs to be hardcoded because the clock
  170. * subsystem is not active during early boot.
  171. */
  172. reg = readl(base + 0x14);
  173. reg |= 1 << 7;
  174. writel(reg, base + 0x14);
  175. }
  176. struct device * __init tegra_soc_device_register(void)
  177. {
  178. struct soc_device_attribute *attr;
  179. struct soc_device *dev;
  180. attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  181. if (!attr)
  182. return NULL;
  183. attr->family = kasprintf(GFP_KERNEL, "Tegra");
  184. attr->revision = kasprintf(GFP_KERNEL, "%d", tegra_sku_info.revision);
  185. attr->soc_id = kasprintf(GFP_KERNEL, "%u", tegra_get_chip_id());
  186. dev = soc_device_register(attr);
  187. if (IS_ERR(dev)) {
  188. kfree(attr->soc_id);
  189. kfree(attr->revision);
  190. kfree(attr->family);
  191. kfree(attr);
  192. return ERR_CAST(dev);
  193. }
  194. return soc_device_to_device(dev);
  195. }
  196. static int __init tegra_init_fuse(void)
  197. {
  198. const struct of_device_id *match;
  199. struct device_node *np;
  200. struct resource regs;
  201. tegra_init_apbmisc();
  202. np = of_find_matching_node_and_match(NULL, tegra_fuse_match, &match);
  203. if (!np) {
  204. /*
  205. * Fall back to legacy initialization for 32-bit ARM only. All
  206. * 64-bit ARM device tree files for Tegra are required to have
  207. * a FUSE node.
  208. *
  209. * This is for backwards-compatibility with old device trees
  210. * that didn't contain a FUSE node.
  211. */
  212. if (IS_ENABLED(CONFIG_ARM) && soc_is_tegra()) {
  213. u8 chip = tegra_get_chip_id();
  214. regs.start = 0x7000f800;
  215. regs.end = 0x7000fbff;
  216. regs.flags = IORESOURCE_MEM;
  217. switch (chip) {
  218. #ifdef CONFIG_ARCH_TEGRA_2x_SOC
  219. case TEGRA20:
  220. fuse->soc = &tegra20_fuse_soc;
  221. break;
  222. #endif
  223. #ifdef CONFIG_ARCH_TEGRA_3x_SOC
  224. case TEGRA30:
  225. fuse->soc = &tegra30_fuse_soc;
  226. break;
  227. #endif
  228. #ifdef CONFIG_ARCH_TEGRA_114_SOC
  229. case TEGRA114:
  230. fuse->soc = &tegra114_fuse_soc;
  231. break;
  232. #endif
  233. #ifdef CONFIG_ARCH_TEGRA_124_SOC
  234. case TEGRA124:
  235. fuse->soc = &tegra124_fuse_soc;
  236. break;
  237. #endif
  238. default:
  239. pr_warn("Unsupported SoC: %02x\n", chip);
  240. break;
  241. }
  242. } else {
  243. /*
  244. * At this point we're not running on Tegra, so play
  245. * nice with multi-platform kernels.
  246. */
  247. return 0;
  248. }
  249. } else {
  250. /*
  251. * Extract information from the device tree if we've found a
  252. * matching node.
  253. */
  254. if (of_address_to_resource(np, 0, &regs) < 0) {
  255. pr_err("failed to get FUSE register\n");
  256. return -ENXIO;
  257. }
  258. fuse->soc = match->data;
  259. }
  260. np = of_find_matching_node(NULL, car_match);
  261. if (np) {
  262. void __iomem *base = of_iomap(np, 0);
  263. if (base) {
  264. tegra_enable_fuse_clk(base);
  265. iounmap(base);
  266. } else {
  267. pr_err("failed to map clock registers\n");
  268. return -ENXIO;
  269. }
  270. }
  271. fuse->base = ioremap_nocache(regs.start, resource_size(&regs));
  272. if (!fuse->base) {
  273. pr_err("failed to map FUSE registers\n");
  274. return -ENXIO;
  275. }
  276. fuse->soc->init(fuse);
  277. pr_info("Tegra Revision: %s SKU: %d CPU Process: %d SoC Process: %d\n",
  278. tegra_revision_name[tegra_sku_info.revision],
  279. tegra_sku_info.sku_id, tegra_sku_info.cpu_process_id,
  280. tegra_sku_info.soc_process_id);
  281. pr_debug("Tegra CPU Speedo ID %d, SoC Speedo ID %d\n",
  282. tegra_sku_info.cpu_speedo_id, tegra_sku_info.soc_speedo_id);
  283. return 0;
  284. }
  285. early_initcall(tegra_init_fuse);
  286. #ifdef CONFIG_ARM64
  287. static int __init tegra_init_soc(void)
  288. {
  289. struct device_node *np;
  290. struct device *soc;
  291. /* make sure we're running on Tegra */
  292. np = of_find_matching_node(NULL, tegra_fuse_match);
  293. if (!np)
  294. return 0;
  295. of_node_put(np);
  296. soc = tegra_soc_device_register();
  297. if (IS_ERR(soc)) {
  298. pr_err("failed to register SoC device: %ld\n", PTR_ERR(soc));
  299. return PTR_ERR(soc);
  300. }
  301. return 0;
  302. }
  303. device_initcall(tegra_init_soc);
  304. #endif