fuse-tegra.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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. tegra_fuse_print_sku_info(&tegra_sku_info);
  162. tegra_soc_device_register();
  163. err = tegra_fuse_add_lookups(fuse);
  164. if (err)
  165. return dev_err_probe(&pdev->dev, err, "failed to add FUSE lookups\n");
  166. }
  167. fuse->clk = devm_clk_get_optional(&pdev->dev, "fuse");
  168. if (IS_ERR(fuse->clk))
  169. return dev_err_probe(&pdev->dev, PTR_ERR(fuse->clk), "failed to get FUSE clock\n");
  170. platform_set_drvdata(pdev, fuse);
  171. fuse->dev = &pdev->dev;
  172. err = devm_pm_runtime_enable(&pdev->dev);
  173. if (err)
  174. return err;
  175. if (fuse->soc->probe) {
  176. err = fuse->soc->probe(fuse);
  177. if (err < 0)
  178. return err;
  179. }
  180. memset(&nvmem, 0, sizeof(nvmem));
  181. nvmem.dev = &pdev->dev;
  182. nvmem.name = "fuse";
  183. nvmem.id = -1;
  184. nvmem.owner = THIS_MODULE;
  185. nvmem.cells = fuse->soc->cells;
  186. nvmem.ncells = fuse->soc->num_cells;
  187. nvmem.keepout = fuse->soc->keepouts;
  188. nvmem.nkeepout = fuse->soc->num_keepouts;
  189. nvmem.type = NVMEM_TYPE_OTP;
  190. nvmem.read_only = true;
  191. nvmem.root_only = false;
  192. nvmem.reg_read = tegra_fuse_read;
  193. nvmem.size = fuse->soc->info->size;
  194. nvmem.word_size = 4;
  195. nvmem.stride = 4;
  196. nvmem.priv = fuse;
  197. fuse->nvmem = devm_nvmem_register(&pdev->dev, &nvmem);
  198. if (IS_ERR(fuse->nvmem)) {
  199. err = PTR_ERR(fuse->nvmem);
  200. dev_err(&pdev->dev, "failed to register NVMEM device: %d\n",
  201. err);
  202. return err;
  203. }
  204. fuse->rst = devm_reset_control_get_optional(&pdev->dev, "fuse");
  205. if (IS_ERR(fuse->rst))
  206. return dev_err_probe(&pdev->dev, PTR_ERR(fuse->rst), "failed to get FUSE reset\n");
  207. /*
  208. * FUSE clock is enabled at a boot time, hence this resume/suspend
  209. * disables the clock besides the h/w resetting.
  210. */
  211. err = pm_runtime_resume_and_get(&pdev->dev);
  212. if (err)
  213. return err;
  214. err = reset_control_reset(fuse->rst);
  215. pm_runtime_put(&pdev->dev);
  216. if (err < 0) {
  217. dev_err(&pdev->dev, "failed to reset FUSE: %d\n", err);
  218. return err;
  219. }
  220. /* release the early I/O memory mapping */
  221. iounmap(base);
  222. return 0;
  223. }
  224. static int __maybe_unused tegra_fuse_runtime_resume(struct device *dev)
  225. {
  226. int err;
  227. err = clk_prepare_enable(fuse->clk);
  228. if (err < 0) {
  229. dev_err(dev, "failed to enable FUSE clock: %d\n", err);
  230. return err;
  231. }
  232. return 0;
  233. }
  234. static int __maybe_unused tegra_fuse_runtime_suspend(struct device *dev)
  235. {
  236. clk_disable_unprepare(fuse->clk);
  237. return 0;
  238. }
  239. static int __maybe_unused tegra_fuse_suspend(struct device *dev)
  240. {
  241. int ret;
  242. /*
  243. * Critical for RAM re-repair operation, which must occur on resume
  244. * from LP1 system suspend and as part of CCPLEX cluster switching.
  245. */
  246. if (fuse->soc->clk_suspend_on)
  247. ret = pm_runtime_resume_and_get(dev);
  248. else
  249. ret = pm_runtime_force_suspend(dev);
  250. return ret;
  251. }
  252. static int __maybe_unused tegra_fuse_resume(struct device *dev)
  253. {
  254. int ret = 0;
  255. if (fuse->soc->clk_suspend_on)
  256. pm_runtime_put(dev);
  257. else
  258. ret = pm_runtime_force_resume(dev);
  259. return ret;
  260. }
  261. static const struct dev_pm_ops tegra_fuse_pm = {
  262. SET_RUNTIME_PM_OPS(tegra_fuse_runtime_suspend, tegra_fuse_runtime_resume,
  263. NULL)
  264. SET_SYSTEM_SLEEP_PM_OPS(tegra_fuse_suspend, tegra_fuse_resume)
  265. };
  266. static const struct acpi_device_id tegra_fuse_acpi_match[] = {
  267. { "NVDA200F" },
  268. { /* sentinel */ }
  269. };
  270. MODULE_DEVICE_TABLE(acpi, tegra_fuse_acpi_match);
  271. static struct platform_driver tegra_fuse_driver = {
  272. .driver = {
  273. .name = "tegra-fuse",
  274. .of_match_table = tegra_fuse_match,
  275. .acpi_match_table = tegra_fuse_acpi_match,
  276. .pm = &tegra_fuse_pm,
  277. .suppress_bind_attrs = true,
  278. },
  279. .probe = tegra_fuse_probe,
  280. };
  281. builtin_platform_driver(tegra_fuse_driver);
  282. u32 __init tegra_fuse_read_spare(unsigned int spare)
  283. {
  284. unsigned int offset = fuse->soc->info->spare + spare * 4;
  285. return fuse->read_early(fuse, offset) & 1;
  286. }
  287. u32 __init tegra_fuse_read_early(unsigned int offset)
  288. {
  289. return fuse->read_early(fuse, offset);
  290. }
  291. int tegra_fuse_readl(unsigned long offset, u32 *value)
  292. {
  293. if (!fuse->dev)
  294. return -EPROBE_DEFER;
  295. /*
  296. * Wait for fuse->clk to be initialized if device-tree boot is used.
  297. */
  298. if (is_of_node(dev_fwnode(fuse->dev)) && !fuse->clk)
  299. return -EPROBE_DEFER;
  300. if (!fuse->read)
  301. return -EPROBE_DEFER;
  302. if (IS_ERR(fuse->clk))
  303. return PTR_ERR(fuse->clk);
  304. *value = fuse->read(fuse, offset);
  305. return 0;
  306. }
  307. EXPORT_SYMBOL(tegra_fuse_readl);
  308. static void tegra_enable_fuse_clk(void __iomem *base)
  309. {
  310. u32 reg;
  311. reg = readl_relaxed(base + 0x48);
  312. reg |= 1 << 28;
  313. writel(reg, base + 0x48);
  314. /*
  315. * Enable FUSE clock. This needs to be hardcoded because the clock
  316. * subsystem is not active during early boot.
  317. */
  318. reg = readl(base + 0x14);
  319. reg |= 1 << 7;
  320. writel(reg, base + 0x14);
  321. }
  322. static ssize_t major_show(struct device *dev, struct device_attribute *attr,
  323. char *buf)
  324. {
  325. return sprintf(buf, "%d\n", tegra_get_major_rev());
  326. }
  327. static DEVICE_ATTR_RO(major);
  328. static ssize_t minor_show(struct device *dev, struct device_attribute *attr,
  329. char *buf)
  330. {
  331. return sprintf(buf, "%d\n", tegra_get_minor_rev());
  332. }
  333. static DEVICE_ATTR_RO(minor);
  334. static struct attribute *tegra_soc_attr[] = {
  335. &dev_attr_major.attr,
  336. &dev_attr_minor.attr,
  337. NULL,
  338. };
  339. const struct attribute_group tegra_soc_attr_group = {
  340. .attrs = tegra_soc_attr,
  341. };
  342. #if IS_ENABLED(CONFIG_ARCH_TEGRA_194_SOC) || \
  343. IS_ENABLED(CONFIG_ARCH_TEGRA_234_SOC) || \
  344. IS_ENABLED(CONFIG_ARCH_TEGRA_241_SOC)
  345. static ssize_t platform_show(struct device *dev, struct device_attribute *attr,
  346. char *buf)
  347. {
  348. /*
  349. * Displays the value in the 'pre_si_platform' field of the HIDREV
  350. * register for Tegra194 devices. A value of 0 indicates that the
  351. * platform type is silicon and all other non-zero values indicate
  352. * the type of simulation platform is being used.
  353. */
  354. return sprintf(buf, "%d\n", tegra_get_platform());
  355. }
  356. static DEVICE_ATTR_RO(platform);
  357. static struct attribute *tegra194_soc_attr[] = {
  358. &dev_attr_major.attr,
  359. &dev_attr_minor.attr,
  360. &dev_attr_platform.attr,
  361. NULL,
  362. };
  363. const struct attribute_group tegra194_soc_attr_group = {
  364. .attrs = tegra194_soc_attr,
  365. };
  366. #endif
  367. struct device *tegra_soc_device_register(void)
  368. {
  369. struct soc_device_attribute *attr;
  370. struct soc_device *dev;
  371. attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  372. if (!attr)
  373. return NULL;
  374. attr->family = kasprintf(GFP_KERNEL, "Tegra");
  375. if (tegra_is_silicon())
  376. attr->revision = kasprintf(GFP_KERNEL, "%s %s",
  377. tegra_platform_name[tegra_sku_info.platform],
  378. tegra_revision_name[tegra_sku_info.revision]);
  379. else
  380. attr->revision = kasprintf(GFP_KERNEL, "%s",
  381. tegra_platform_name[tegra_sku_info.platform]);
  382. attr->soc_id = kasprintf(GFP_KERNEL, "%u", tegra_get_chip_id());
  383. attr->custom_attr_group = fuse->soc->soc_attr_group;
  384. dev = soc_device_register(attr);
  385. if (IS_ERR(dev)) {
  386. kfree(attr->soc_id);
  387. kfree(attr->revision);
  388. kfree(attr->family);
  389. kfree(attr);
  390. return ERR_CAST(dev);
  391. }
  392. return soc_device_to_device(dev);
  393. }
  394. static int __init tegra_init_fuse(void)
  395. {
  396. const struct of_device_id *match;
  397. struct device_node *np;
  398. struct resource regs;
  399. int err;
  400. tegra_init_apbmisc();
  401. np = of_find_matching_node_and_match(NULL, tegra_fuse_match, &match);
  402. if (!np) {
  403. /*
  404. * Fall back to legacy initialization for 32-bit ARM only. All
  405. * 64-bit ARM device tree files for Tegra are required to have
  406. * a FUSE node.
  407. *
  408. * This is for backwards-compatibility with old device trees
  409. * that didn't contain a FUSE node.
  410. */
  411. if (IS_ENABLED(CONFIG_ARM) && soc_is_tegra()) {
  412. u8 chip = tegra_get_chip_id();
  413. regs.start = 0x7000f800;
  414. regs.end = 0x7000fbff;
  415. regs.flags = IORESOURCE_MEM;
  416. switch (chip) {
  417. #ifdef CONFIG_ARCH_TEGRA_2x_SOC
  418. case TEGRA20:
  419. fuse->soc = &tegra20_fuse_soc;
  420. break;
  421. #endif
  422. #ifdef CONFIG_ARCH_TEGRA_3x_SOC
  423. case TEGRA30:
  424. fuse->soc = &tegra30_fuse_soc;
  425. break;
  426. #endif
  427. #ifdef CONFIG_ARCH_TEGRA_114_SOC
  428. case TEGRA114:
  429. fuse->soc = &tegra114_fuse_soc;
  430. break;
  431. #endif
  432. #ifdef CONFIG_ARCH_TEGRA_124_SOC
  433. case TEGRA124:
  434. fuse->soc = &tegra124_fuse_soc;
  435. break;
  436. #endif
  437. default:
  438. pr_warn("Unsupported SoC: %02x\n", chip);
  439. break;
  440. }
  441. } else {
  442. /*
  443. * At this point we're not running on Tegra, so play
  444. * nice with multi-platform kernels.
  445. */
  446. return 0;
  447. }
  448. } else {
  449. /*
  450. * Extract information from the device tree if we've found a
  451. * matching node.
  452. */
  453. if (of_address_to_resource(np, 0, &regs) < 0) {
  454. pr_err("failed to get FUSE register\n");
  455. return -ENXIO;
  456. }
  457. fuse->soc = match->data;
  458. }
  459. np = of_find_matching_node(NULL, car_match);
  460. if (np) {
  461. void __iomem *base = of_iomap(np, 0);
  462. of_node_put(np);
  463. if (base) {
  464. tegra_enable_fuse_clk(base);
  465. iounmap(base);
  466. } else {
  467. pr_err("failed to map clock registers\n");
  468. return -ENXIO;
  469. }
  470. }
  471. fuse->base = ioremap(regs.start, resource_size(&regs));
  472. if (!fuse->base) {
  473. pr_err("failed to map FUSE registers\n");
  474. return -ENXIO;
  475. }
  476. fuse->soc->init(fuse);
  477. tegra_fuse_print_sku_info(&tegra_sku_info);
  478. err = tegra_fuse_add_lookups(fuse);
  479. if (err)
  480. pr_err("failed to add FUSE lookups\n");
  481. return err;
  482. }
  483. early_initcall(tegra_init_fuse);
  484. #ifdef CONFIG_ARM64
  485. static int __init tegra_init_soc(void)
  486. {
  487. struct device_node *np;
  488. struct device *soc;
  489. /* make sure we're running on Tegra */
  490. np = of_find_matching_node(NULL, tegra_fuse_match);
  491. if (!np)
  492. return 0;
  493. of_node_put(np);
  494. soc = tegra_soc_device_register();
  495. if (IS_ERR(soc)) {
  496. pr_err("failed to register SoC device: %ld\n", PTR_ERR(soc));
  497. return PTR_ERR(soc);
  498. }
  499. return 0;
  500. }
  501. device_initcall(tegra_init_soc);
  502. #endif