fuse-tegra.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2013-2023, NVIDIA CORPORATION. All rights reserved.
  4. */
  5. #include <linux/acpi.h>
  6. #include <linux/clk.h>
  7. #include <linux/device.h>
  8. #include <linux/kobject.h>
  9. #include <linux/init.h>
  10. #include <linux/io.h>
  11. #include <linux/mod_devicetable.h>
  12. #include <linux/nvmem-consumer.h>
  13. #include <linux/nvmem-provider.h>
  14. #include <linux/of.h>
  15. #include <linux/of_address.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/pm_runtime.h>
  18. #include <linux/reset.h>
  19. #include <linux/slab.h>
  20. #include <linux/sys_soc.h>
  21. #include <soc/tegra/common.h>
  22. #include <soc/tegra/fuse.h>
  23. #include "fuse.h"
  24. struct tegra_sku_info tegra_sku_info;
  25. EXPORT_SYMBOL(tegra_sku_info);
  26. static const char *tegra_revision_name[TEGRA_REVISION_MAX] = {
  27. [TEGRA_REVISION_UNKNOWN] = "unknown",
  28. [TEGRA_REVISION_A01] = "A01",
  29. [TEGRA_REVISION_A02] = "A02",
  30. [TEGRA_REVISION_A03] = "A03",
  31. [TEGRA_REVISION_A03p] = "A03 prime",
  32. [TEGRA_REVISION_A04] = "A04",
  33. };
  34. static const char *tegra_platform_name[TEGRA_PLATFORM_MAX] = {
  35. [TEGRA_PLATFORM_SILICON] = "Silicon",
  36. [TEGRA_PLATFORM_QT] = "QT",
  37. [TEGRA_PLATFORM_SYSTEM_FPGA] = "System FPGA",
  38. [TEGRA_PLATFORM_UNIT_FPGA] = "Unit FPGA",
  39. [TEGRA_PLATFORM_ASIM_QT] = "Asim QT",
  40. [TEGRA_PLATFORM_ASIM_LINSIM] = "Asim Linsim",
  41. [TEGRA_PLATFORM_DSIM_ASIM_LINSIM] = "Dsim Asim Linsim",
  42. [TEGRA_PLATFORM_VERIFICATION_SIMULATION] = "Verification Simulation",
  43. [TEGRA_PLATFORM_VDK] = "VDK",
  44. [TEGRA_PLATFORM_VSP] = "VSP",
  45. };
  46. static const struct of_device_id car_match[] __initconst = {
  47. { .compatible = "nvidia,tegra20-car", },
  48. { .compatible = "nvidia,tegra30-car", },
  49. { .compatible = "nvidia,tegra114-car", },
  50. { .compatible = "nvidia,tegra124-car", },
  51. { .compatible = "nvidia,tegra132-car", },
  52. { .compatible = "nvidia,tegra210-car", },
  53. {},
  54. };
  55. static struct tegra_fuse *fuse = &(struct tegra_fuse) {
  56. .base = NULL,
  57. .soc = NULL,
  58. };
  59. static const struct of_device_id tegra_fuse_match[] = {
  60. #ifdef CONFIG_ARCH_TEGRA_234_SOC
  61. { .compatible = "nvidia,tegra234-efuse", .data = &tegra234_fuse_soc },
  62. #endif
  63. #ifdef CONFIG_ARCH_TEGRA_194_SOC
  64. { .compatible = "nvidia,tegra194-efuse", .data = &tegra194_fuse_soc },
  65. #endif
  66. #ifdef CONFIG_ARCH_TEGRA_186_SOC
  67. { .compatible = "nvidia,tegra186-efuse", .data = &tegra186_fuse_soc },
  68. #endif
  69. #ifdef CONFIG_ARCH_TEGRA_210_SOC
  70. { .compatible = "nvidia,tegra210-efuse", .data = &tegra210_fuse_soc },
  71. #endif
  72. #ifdef CONFIG_ARCH_TEGRA_132_SOC
  73. { .compatible = "nvidia,tegra132-efuse", .data = &tegra124_fuse_soc },
  74. #endif
  75. #ifdef CONFIG_ARCH_TEGRA_124_SOC
  76. { .compatible = "nvidia,tegra124-efuse", .data = &tegra124_fuse_soc },
  77. #endif
  78. #ifdef CONFIG_ARCH_TEGRA_114_SOC
  79. { .compatible = "nvidia,tegra114-efuse", .data = &tegra114_fuse_soc },
  80. #endif
  81. #ifdef CONFIG_ARCH_TEGRA_3x_SOC
  82. { .compatible = "nvidia,tegra30-efuse", .data = &tegra30_fuse_soc },
  83. #endif
  84. #ifdef CONFIG_ARCH_TEGRA_2x_SOC
  85. { .compatible = "nvidia,tegra20-efuse", .data = &tegra20_fuse_soc },
  86. #endif
  87. { /* sentinel */ }
  88. };
  89. static int tegra_fuse_read(void *priv, unsigned int offset, void *value,
  90. size_t bytes)
  91. {
  92. unsigned int count = bytes / 4, i;
  93. struct tegra_fuse *fuse = priv;
  94. u32 *buffer = value;
  95. for (i = 0; i < count; i++)
  96. buffer[i] = fuse->read(fuse, offset + i * 4);
  97. return 0;
  98. }
  99. static void tegra_fuse_restore(void *base)
  100. {
  101. fuse->base = (void __iomem *)base;
  102. fuse->clk = NULL;
  103. }
  104. static void tegra_fuse_print_sku_info(struct tegra_sku_info *tegra_sku_info)
  105. {
  106. pr_info("Tegra Revision: %s SKU: %d CPU Process: %d SoC Process: %d\n",
  107. tegra_revision_name[tegra_sku_info->revision],
  108. tegra_sku_info->sku_id, tegra_sku_info->cpu_process_id,
  109. tegra_sku_info->soc_process_id);
  110. pr_debug("Tegra CPU Speedo ID %d, SoC Speedo ID %d\n",
  111. tegra_sku_info->cpu_speedo_id, tegra_sku_info->soc_speedo_id);
  112. }
  113. static int tegra_fuse_add_lookups(struct tegra_fuse *fuse)
  114. {
  115. fuse->lookups = kmemdup_array(fuse->soc->lookups, fuse->soc->num_lookups,
  116. sizeof(*fuse->lookups), GFP_KERNEL);
  117. if (!fuse->lookups)
  118. return -ENOMEM;
  119. nvmem_add_cell_lookups(fuse->lookups, fuse->soc->num_lookups);
  120. return 0;
  121. }
  122. static int tegra_fuse_probe(struct platform_device *pdev)
  123. {
  124. void __iomem *base = fuse->base;
  125. struct nvmem_config nvmem;
  126. struct resource *res;
  127. int err;
  128. err = devm_add_action(&pdev->dev, tegra_fuse_restore, (void __force *)base);
  129. if (err)
  130. return err;
  131. /* take over the memory region from the early initialization */
  132. fuse->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
  133. if (IS_ERR(fuse->base))
  134. return PTR_ERR(fuse->base);
  135. fuse->phys = res->start;
  136. /* Initialize the soc data and lookups if using ACPI boot. */
  137. if (is_acpi_node(dev_fwnode(&pdev->dev)) && !fuse->soc) {
  138. u8 chip;
  139. tegra_acpi_init_apbmisc();
  140. chip = tegra_get_chip_id();
  141. switch (chip) {
  142. #if defined(CONFIG_ARCH_TEGRA_194_SOC)
  143. case TEGRA194:
  144. fuse->soc = &tegra194_fuse_soc;
  145. break;
  146. #endif
  147. #if defined(CONFIG_ARCH_TEGRA_234_SOC)
  148. case TEGRA234:
  149. fuse->soc = &tegra234_fuse_soc;
  150. break;
  151. #endif
  152. #if defined(CONFIG_ARCH_TEGRA_241_SOC)
  153. case TEGRA241:
  154. fuse->soc = &tegra241_fuse_soc;
  155. break;
  156. #endif
  157. default:
  158. return dev_err_probe(&pdev->dev, -EINVAL, "Unsupported SoC: %02x\n", chip);
  159. }
  160. fuse->soc->init(fuse);
  161. err = tegra_fuse_add_lookups(fuse);
  162. if (err)
  163. return dev_err_probe(&pdev->dev, err, "failed to add FUSE lookups\n");
  164. }
  165. fuse->clk = devm_clk_get_optional(&pdev->dev, "fuse");
  166. if (IS_ERR(fuse->clk))
  167. return dev_err_probe(&pdev->dev, PTR_ERR(fuse->clk), "failed to get FUSE clock\n");
  168. platform_set_drvdata(pdev, fuse);
  169. fuse->dev = &pdev->dev;
  170. err = devm_pm_runtime_enable(&pdev->dev);
  171. if (err)
  172. return err;
  173. if (fuse->soc->probe) {
  174. err = fuse->soc->probe(fuse);
  175. if (err < 0)
  176. return err;
  177. }
  178. memset(&nvmem, 0, sizeof(nvmem));
  179. nvmem.dev = &pdev->dev;
  180. nvmem.name = "fuse";
  181. nvmem.id = -1;
  182. nvmem.owner = THIS_MODULE;
  183. nvmem.cells = fuse->soc->cells;
  184. nvmem.ncells = fuse->soc->num_cells;
  185. nvmem.keepout = fuse->soc->keepouts;
  186. nvmem.nkeepout = fuse->soc->num_keepouts;
  187. nvmem.type = NVMEM_TYPE_OTP;
  188. nvmem.read_only = true;
  189. nvmem.root_only = false;
  190. nvmem.reg_read = tegra_fuse_read;
  191. nvmem.size = fuse->soc->info->size;
  192. nvmem.word_size = 4;
  193. nvmem.stride = 4;
  194. nvmem.priv = fuse;
  195. fuse->nvmem = devm_nvmem_register(&pdev->dev, &nvmem);
  196. if (IS_ERR(fuse->nvmem)) {
  197. err = PTR_ERR(fuse->nvmem);
  198. dev_err(&pdev->dev, "failed to register NVMEM device: %d\n",
  199. err);
  200. return err;
  201. }
  202. fuse->rst = devm_reset_control_get_optional(&pdev->dev, "fuse");
  203. if (IS_ERR(fuse->rst))
  204. return dev_err_probe(&pdev->dev, PTR_ERR(fuse->rst), "failed to get FUSE reset\n");
  205. /*
  206. * FUSE clock is enabled at a boot time, hence this resume/suspend
  207. * disables the clock besides the h/w resetting.
  208. */
  209. err = pm_runtime_resume_and_get(&pdev->dev);
  210. if (err)
  211. return err;
  212. err = reset_control_reset(fuse->rst);
  213. pm_runtime_put(&pdev->dev);
  214. if (err < 0) {
  215. dev_err(&pdev->dev, "failed to reset FUSE: %d\n", err);
  216. return err;
  217. }
  218. /* release the early I/O memory mapping */
  219. iounmap(base);
  220. return 0;
  221. }
  222. static int __maybe_unused tegra_fuse_runtime_resume(struct device *dev)
  223. {
  224. int err;
  225. err = clk_prepare_enable(fuse->clk);
  226. if (err < 0) {
  227. dev_err(dev, "failed to enable FUSE clock: %d\n", err);
  228. return err;
  229. }
  230. return 0;
  231. }
  232. static int __maybe_unused tegra_fuse_runtime_suspend(struct device *dev)
  233. {
  234. clk_disable_unprepare(fuse->clk);
  235. return 0;
  236. }
  237. static int __maybe_unused tegra_fuse_suspend(struct device *dev)
  238. {
  239. int ret;
  240. /*
  241. * Critical for RAM re-repair operation, which must occur on resume
  242. * from LP1 system suspend and as part of CCPLEX cluster switching.
  243. */
  244. if (fuse->soc->clk_suspend_on)
  245. ret = pm_runtime_resume_and_get(dev);
  246. else
  247. ret = pm_runtime_force_suspend(dev);
  248. return ret;
  249. }
  250. static int __maybe_unused tegra_fuse_resume(struct device *dev)
  251. {
  252. int ret = 0;
  253. if (fuse->soc->clk_suspend_on)
  254. pm_runtime_put(dev);
  255. else
  256. ret = pm_runtime_force_resume(dev);
  257. return ret;
  258. }
  259. static const struct dev_pm_ops tegra_fuse_pm = {
  260. SET_RUNTIME_PM_OPS(tegra_fuse_runtime_suspend, tegra_fuse_runtime_resume,
  261. NULL)
  262. SET_SYSTEM_SLEEP_PM_OPS(tegra_fuse_suspend, tegra_fuse_resume)
  263. };
  264. static const struct acpi_device_id tegra_fuse_acpi_match[] = {
  265. { "NVDA200F" },
  266. { /* sentinel */ }
  267. };
  268. MODULE_DEVICE_TABLE(acpi, tegra_fuse_acpi_match);
  269. static struct platform_driver tegra_fuse_driver = {
  270. .driver = {
  271. .name = "tegra-fuse",
  272. .of_match_table = tegra_fuse_match,
  273. .acpi_match_table = tegra_fuse_acpi_match,
  274. .pm = &tegra_fuse_pm,
  275. .suppress_bind_attrs = true,
  276. },
  277. .probe = tegra_fuse_probe,
  278. };
  279. builtin_platform_driver(tegra_fuse_driver);
  280. u32 __init tegra_fuse_read_spare(unsigned int spare)
  281. {
  282. unsigned int offset = fuse->soc->info->spare + spare * 4;
  283. return fuse->read_early(fuse, offset) & 1;
  284. }
  285. u32 __init tegra_fuse_read_early(unsigned int offset)
  286. {
  287. return fuse->read_early(fuse, offset);
  288. }
  289. int tegra_fuse_readl(unsigned long offset, u32 *value)
  290. {
  291. if (!fuse->dev)
  292. return -EPROBE_DEFER;
  293. /*
  294. * Wait for fuse->clk to be initialized if device-tree boot is used.
  295. */
  296. if (is_of_node(dev_fwnode(fuse->dev)) && !fuse->clk)
  297. return -EPROBE_DEFER;
  298. if (!fuse->read)
  299. return -EPROBE_DEFER;
  300. if (IS_ERR(fuse->clk))
  301. return PTR_ERR(fuse->clk);
  302. *value = fuse->read(fuse, offset);
  303. return 0;
  304. }
  305. EXPORT_SYMBOL(tegra_fuse_readl);
  306. static void tegra_enable_fuse_clk(void __iomem *base)
  307. {
  308. u32 reg;
  309. reg = readl_relaxed(base + 0x48);
  310. reg |= 1 << 28;
  311. writel(reg, base + 0x48);
  312. /*
  313. * Enable FUSE clock. This needs to be hardcoded because the clock
  314. * subsystem is not active during early boot.
  315. */
  316. reg = readl(base + 0x14);
  317. reg |= 1 << 7;
  318. writel(reg, base + 0x14);
  319. }
  320. static ssize_t major_show(struct device *dev, struct device_attribute *attr,
  321. char *buf)
  322. {
  323. return sprintf(buf, "%d\n", tegra_get_major_rev());
  324. }
  325. static DEVICE_ATTR_RO(major);
  326. static ssize_t minor_show(struct device *dev, struct device_attribute *attr,
  327. char *buf)
  328. {
  329. return sprintf(buf, "%d\n", tegra_get_minor_rev());
  330. }
  331. static DEVICE_ATTR_RO(minor);
  332. static struct attribute *tegra_soc_attr[] = {
  333. &dev_attr_major.attr,
  334. &dev_attr_minor.attr,
  335. NULL,
  336. };
  337. const struct attribute_group tegra_soc_attr_group = {
  338. .attrs = tegra_soc_attr,
  339. };
  340. #if IS_ENABLED(CONFIG_ARCH_TEGRA_194_SOC) || \
  341. IS_ENABLED(CONFIG_ARCH_TEGRA_234_SOC) || \
  342. IS_ENABLED(CONFIG_ARCH_TEGRA_241_SOC)
  343. static ssize_t platform_show(struct device *dev, struct device_attribute *attr,
  344. char *buf)
  345. {
  346. /*
  347. * Displays the value in the 'pre_si_platform' field of the HIDREV
  348. * register for Tegra194 devices. A value of 0 indicates that the
  349. * platform type is silicon and all other non-zero values indicate
  350. * the type of simulation platform is being used.
  351. */
  352. return sprintf(buf, "%d\n", tegra_get_platform());
  353. }
  354. static DEVICE_ATTR_RO(platform);
  355. static struct attribute *tegra194_soc_attr[] = {
  356. &dev_attr_major.attr,
  357. &dev_attr_minor.attr,
  358. &dev_attr_platform.attr,
  359. NULL,
  360. };
  361. const struct attribute_group tegra194_soc_attr_group = {
  362. .attrs = tegra194_soc_attr,
  363. };
  364. #endif
  365. struct device *tegra_soc_device_register(void)
  366. {
  367. struct soc_device_attribute *attr;
  368. struct soc_device *dev;
  369. attr = kzalloc_obj(*attr);
  370. if (!attr)
  371. return NULL;
  372. attr->family = kasprintf(GFP_KERNEL, "Tegra");
  373. if (tegra_is_silicon())
  374. attr->revision = kasprintf(GFP_KERNEL, "%s %s",
  375. tegra_platform_name[tegra_sku_info.platform],
  376. tegra_revision_name[tegra_sku_info.revision]);
  377. else
  378. attr->revision = kasprintf(GFP_KERNEL, "%s",
  379. tegra_platform_name[tegra_sku_info.platform]);
  380. attr->soc_id = kasprintf(GFP_KERNEL, "%u", tegra_get_chip_id());
  381. attr->custom_attr_group = fuse->soc->soc_attr_group;
  382. dev = soc_device_register(attr);
  383. if (IS_ERR(dev)) {
  384. kfree(attr->soc_id);
  385. kfree(attr->revision);
  386. kfree(attr->family);
  387. kfree(attr);
  388. return ERR_CAST(dev);
  389. }
  390. return soc_device_to_device(dev);
  391. }
  392. static int __init tegra_init_fuse(void)
  393. {
  394. const struct of_device_id *match;
  395. struct device_node *np;
  396. struct resource regs;
  397. int err;
  398. tegra_init_apbmisc();
  399. np = of_find_matching_node_and_match(NULL, tegra_fuse_match, &match);
  400. if (!np) {
  401. /*
  402. * Fall back to legacy initialization for 32-bit ARM only. All
  403. * 64-bit ARM device tree files for Tegra are required to have
  404. * a FUSE node.
  405. *
  406. * This is for backwards-compatibility with old device trees
  407. * that didn't contain a FUSE node.
  408. */
  409. if (IS_ENABLED(CONFIG_ARM) && soc_is_tegra()) {
  410. u8 chip = tegra_get_chip_id();
  411. regs.start = 0x7000f800;
  412. regs.end = 0x7000fbff;
  413. regs.flags = IORESOURCE_MEM;
  414. switch (chip) {
  415. #ifdef CONFIG_ARCH_TEGRA_2x_SOC
  416. case TEGRA20:
  417. fuse->soc = &tegra20_fuse_soc;
  418. break;
  419. #endif
  420. #ifdef CONFIG_ARCH_TEGRA_3x_SOC
  421. case TEGRA30:
  422. fuse->soc = &tegra30_fuse_soc;
  423. break;
  424. #endif
  425. #ifdef CONFIG_ARCH_TEGRA_114_SOC
  426. case TEGRA114:
  427. fuse->soc = &tegra114_fuse_soc;
  428. break;
  429. #endif
  430. #ifdef CONFIG_ARCH_TEGRA_124_SOC
  431. case TEGRA124:
  432. fuse->soc = &tegra124_fuse_soc;
  433. break;
  434. #endif
  435. default:
  436. pr_warn("Unsupported SoC: %02x\n", chip);
  437. break;
  438. }
  439. } else {
  440. /*
  441. * At this point we're not running on Tegra, so play
  442. * nice with multi-platform kernels.
  443. */
  444. return 0;
  445. }
  446. } else {
  447. /*
  448. * Extract information from the device tree if we've found a
  449. * matching node.
  450. */
  451. if (of_address_to_resource(np, 0, &regs) < 0) {
  452. pr_err("failed to get FUSE register\n");
  453. return -ENXIO;
  454. }
  455. fuse->soc = match->data;
  456. }
  457. np = of_find_matching_node(NULL, car_match);
  458. if (np) {
  459. void __iomem *base = of_iomap(np, 0);
  460. of_node_put(np);
  461. if (base) {
  462. tegra_enable_fuse_clk(base);
  463. iounmap(base);
  464. } else {
  465. pr_err("failed to map clock registers\n");
  466. return -ENXIO;
  467. }
  468. }
  469. fuse->base = ioremap(regs.start, resource_size(&regs));
  470. if (!fuse->base) {
  471. pr_err("failed to map FUSE registers\n");
  472. return -ENXIO;
  473. }
  474. fuse->soc->init(fuse);
  475. tegra_fuse_print_sku_info(&tegra_sku_info);
  476. err = tegra_fuse_add_lookups(fuse);
  477. if (err)
  478. pr_err("failed to add FUSE lookups\n");
  479. return err;
  480. }
  481. early_initcall(tegra_init_fuse);
  482. #ifdef CONFIG_ARM64
  483. static int __init tegra_init_soc(void)
  484. {
  485. struct device_node *np;
  486. struct device *soc;
  487. /* make sure we're running on Tegra */
  488. np = of_find_matching_node(NULL, tegra_fuse_match);
  489. if (!np)
  490. return 0;
  491. of_node_put(np);
  492. soc = tegra_soc_device_register();
  493. if (IS_ERR(soc)) {
  494. pr_err("failed to register SoC device: %ld\n", PTR_ERR(soc));
  495. return PTR_ERR(soc);
  496. }
  497. return 0;
  498. }
  499. device_initcall(tegra_init_soc);
  500. #endif