fuse-tegra.c 8.6 KB

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