fuse-tegra.c 7.5 KB

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