fuse-tegra.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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_186_SOC
  88. { .compatible = "nvidia,tegra186-efuse", .data = &tegra186_fuse_soc },
  89. #endif
  90. #ifdef CONFIG_ARCH_TEGRA_210_SOC
  91. { .compatible = "nvidia,tegra210-efuse", .data = &tegra210_fuse_soc },
  92. #endif
  93. #ifdef CONFIG_ARCH_TEGRA_132_SOC
  94. { .compatible = "nvidia,tegra132-efuse", .data = &tegra124_fuse_soc },
  95. #endif
  96. #ifdef CONFIG_ARCH_TEGRA_124_SOC
  97. { .compatible = "nvidia,tegra124-efuse", .data = &tegra124_fuse_soc },
  98. #endif
  99. #ifdef CONFIG_ARCH_TEGRA_114_SOC
  100. { .compatible = "nvidia,tegra114-efuse", .data = &tegra114_fuse_soc },
  101. #endif
  102. #ifdef CONFIG_ARCH_TEGRA_3x_SOC
  103. { .compatible = "nvidia,tegra30-efuse", .data = &tegra30_fuse_soc },
  104. #endif
  105. #ifdef CONFIG_ARCH_TEGRA_2x_SOC
  106. { .compatible = "nvidia,tegra20-efuse", .data = &tegra20_fuse_soc },
  107. #endif
  108. { /* sentinel */ }
  109. };
  110. static int tegra_fuse_probe(struct platform_device *pdev)
  111. {
  112. void __iomem *base = fuse->base;
  113. struct resource *res;
  114. int err;
  115. /* take over the memory region from the early initialization */
  116. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  117. fuse->phys = res->start;
  118. fuse->base = devm_ioremap_resource(&pdev->dev, res);
  119. if (IS_ERR(fuse->base)) {
  120. err = PTR_ERR(fuse->base);
  121. fuse->base = base;
  122. return err;
  123. }
  124. fuse->clk = devm_clk_get(&pdev->dev, "fuse");
  125. if (IS_ERR(fuse->clk)) {
  126. dev_err(&pdev->dev, "failed to get FUSE clock: %ld",
  127. PTR_ERR(fuse->clk));
  128. fuse->base = base;
  129. return PTR_ERR(fuse->clk);
  130. }
  131. platform_set_drvdata(pdev, fuse);
  132. fuse->dev = &pdev->dev;
  133. if (fuse->soc->probe) {
  134. err = fuse->soc->probe(fuse);
  135. if (err < 0) {
  136. fuse->base = base;
  137. return err;
  138. }
  139. }
  140. if (tegra_fuse_create_sysfs(&pdev->dev, fuse->soc->info->size,
  141. fuse->soc->info))
  142. return -ENODEV;
  143. /* release the early I/O memory mapping */
  144. iounmap(base);
  145. return 0;
  146. }
  147. static struct platform_driver tegra_fuse_driver = {
  148. .driver = {
  149. .name = "tegra-fuse",
  150. .of_match_table = tegra_fuse_match,
  151. .suppress_bind_attrs = true,
  152. },
  153. .probe = tegra_fuse_probe,
  154. };
  155. builtin_platform_driver(tegra_fuse_driver);
  156. bool __init tegra_fuse_read_spare(unsigned int spare)
  157. {
  158. unsigned int offset = fuse->soc->info->spare + spare * 4;
  159. return fuse->read_early(fuse, offset) & 1;
  160. }
  161. u32 __init tegra_fuse_read_early(unsigned int offset)
  162. {
  163. return fuse->read_early(fuse, offset);
  164. }
  165. int tegra_fuse_readl(unsigned long offset, u32 *value)
  166. {
  167. if (!fuse->read)
  168. return -EPROBE_DEFER;
  169. *value = fuse->read(fuse, offset);
  170. return 0;
  171. }
  172. EXPORT_SYMBOL(tegra_fuse_readl);
  173. static void tegra_enable_fuse_clk(void __iomem *base)
  174. {
  175. u32 reg;
  176. reg = readl_relaxed(base + 0x48);
  177. reg |= 1 << 28;
  178. writel(reg, base + 0x48);
  179. /*
  180. * Enable FUSE clock. This needs to be hardcoded because the clock
  181. * subsystem is not active during early boot.
  182. */
  183. reg = readl(base + 0x14);
  184. reg |= 1 << 7;
  185. writel(reg, base + 0x14);
  186. }
  187. struct device * __init tegra_soc_device_register(void)
  188. {
  189. struct soc_device_attribute *attr;
  190. struct soc_device *dev;
  191. attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  192. if (!attr)
  193. return NULL;
  194. attr->family = kasprintf(GFP_KERNEL, "Tegra");
  195. attr->revision = kasprintf(GFP_KERNEL, "%d", tegra_sku_info.revision);
  196. attr->soc_id = kasprintf(GFP_KERNEL, "%u", tegra_get_chip_id());
  197. dev = soc_device_register(attr);
  198. if (IS_ERR(dev)) {
  199. kfree(attr->soc_id);
  200. kfree(attr->revision);
  201. kfree(attr->family);
  202. kfree(attr);
  203. return ERR_CAST(dev);
  204. }
  205. return soc_device_to_device(dev);
  206. }
  207. static int __init tegra_init_fuse(void)
  208. {
  209. const struct of_device_id *match;
  210. struct device_node *np;
  211. struct resource regs;
  212. tegra_init_apbmisc();
  213. np = of_find_matching_node_and_match(NULL, tegra_fuse_match, &match);
  214. if (!np) {
  215. /*
  216. * Fall back to legacy initialization for 32-bit ARM only. All
  217. * 64-bit ARM device tree files for Tegra are required to have
  218. * a FUSE node.
  219. *
  220. * This is for backwards-compatibility with old device trees
  221. * that didn't contain a FUSE node.
  222. */
  223. if (IS_ENABLED(CONFIG_ARM) && soc_is_tegra()) {
  224. u8 chip = tegra_get_chip_id();
  225. regs.start = 0x7000f800;
  226. regs.end = 0x7000fbff;
  227. regs.flags = IORESOURCE_MEM;
  228. switch (chip) {
  229. #ifdef CONFIG_ARCH_TEGRA_2x_SOC
  230. case TEGRA20:
  231. fuse->soc = &tegra20_fuse_soc;
  232. break;
  233. #endif
  234. #ifdef CONFIG_ARCH_TEGRA_3x_SOC
  235. case TEGRA30:
  236. fuse->soc = &tegra30_fuse_soc;
  237. break;
  238. #endif
  239. #ifdef CONFIG_ARCH_TEGRA_114_SOC
  240. case TEGRA114:
  241. fuse->soc = &tegra114_fuse_soc;
  242. break;
  243. #endif
  244. #ifdef CONFIG_ARCH_TEGRA_124_SOC
  245. case TEGRA124:
  246. fuse->soc = &tegra124_fuse_soc;
  247. break;
  248. #endif
  249. default:
  250. pr_warn("Unsupported SoC: %02x\n", chip);
  251. break;
  252. }
  253. } else {
  254. /*
  255. * At this point we're not running on Tegra, so play
  256. * nice with multi-platform kernels.
  257. */
  258. return 0;
  259. }
  260. } else {
  261. /*
  262. * Extract information from the device tree if we've found a
  263. * matching node.
  264. */
  265. if (of_address_to_resource(np, 0, &regs) < 0) {
  266. pr_err("failed to get FUSE register\n");
  267. return -ENXIO;
  268. }
  269. fuse->soc = match->data;
  270. }
  271. np = of_find_matching_node(NULL, car_match);
  272. if (np) {
  273. void __iomem *base = of_iomap(np, 0);
  274. if (base) {
  275. tegra_enable_fuse_clk(base);
  276. iounmap(base);
  277. } else {
  278. pr_err("failed to map clock registers\n");
  279. return -ENXIO;
  280. }
  281. }
  282. fuse->base = ioremap_nocache(regs.start, resource_size(&regs));
  283. if (!fuse->base) {
  284. pr_err("failed to map FUSE registers\n");
  285. return -ENXIO;
  286. }
  287. fuse->soc->init(fuse);
  288. pr_info("Tegra Revision: %s SKU: %d CPU Process: %d SoC Process: %d\n",
  289. tegra_revision_name[tegra_sku_info.revision],
  290. tegra_sku_info.sku_id, tegra_sku_info.cpu_process_id,
  291. tegra_sku_info.soc_process_id);
  292. pr_debug("Tegra CPU Speedo ID %d, SoC Speedo ID %d\n",
  293. tegra_sku_info.cpu_speedo_id, tegra_sku_info.soc_speedo_id);
  294. return 0;
  295. }
  296. early_initcall(tegra_init_fuse);
  297. #ifdef CONFIG_ARM64
  298. static int __init tegra_init_soc(void)
  299. {
  300. struct device_node *np;
  301. struct device *soc;
  302. /* make sure we're running on Tegra */
  303. np = of_find_matching_node(NULL, tegra_fuse_match);
  304. if (!np)
  305. return 0;
  306. of_node_put(np);
  307. soc = tegra_soc_device_register();
  308. if (IS_ERR(soc)) {
  309. pr_err("failed to register SoC device: %ld\n", PTR_ERR(soc));
  310. return PTR_ERR(soc);
  311. }
  312. return 0;
  313. }
  314. device_initcall(tegra_init_soc);
  315. #endif