xref: /linux/drivers/soc/tegra/fuse/fuse-tegra.c (revision 59c6fceb2ecc382c3d20508a235b539bf23af1f0)
19952f691SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2783c8f4cSPeter De Schrijver /*
3783c8f4cSPeter De Schrijver  * Copyright (c) 2013-2014, NVIDIA CORPORATION.  All rights reserved.
4783c8f4cSPeter De Schrijver  */
5783c8f4cSPeter De Schrijver 
67e939de1SThierry Reding #include <linux/clk.h>
7783c8f4cSPeter De Schrijver #include <linux/device.h>
8783c8f4cSPeter De Schrijver #include <linux/kobject.h>
91859217bSPaul Gortmaker #include <linux/init.h>
1027a0342aSThierry Reding #include <linux/io.h>
1196ee12b2SThierry Reding #include <linux/nvmem-consumer.h>
1296ee12b2SThierry Reding #include <linux/nvmem-provider.h>
13783c8f4cSPeter De Schrijver #include <linux/of.h>
14783c8f4cSPeter De Schrijver #include <linux/of_address.h>
1527a0342aSThierry Reding #include <linux/platform_device.h>
1624a15252SDmitry Osipenko #include <linux/pm_runtime.h>
1727a0342aSThierry Reding #include <linux/slab.h>
1827a0342aSThierry Reding #include <linux/sys_soc.h>
19783c8f4cSPeter De Schrijver 
2024fa5af8SThierry Reding #include <soc/tegra/common.h>
21783c8f4cSPeter De Schrijver #include <soc/tegra/fuse.h>
22783c8f4cSPeter De Schrijver 
23783c8f4cSPeter De Schrijver #include "fuse.h"
24783c8f4cSPeter De Schrijver 
25783c8f4cSPeter De Schrijver struct tegra_sku_info tegra_sku_info;
26f9fc3661SVince Hsu EXPORT_SYMBOL(tegra_sku_info);
27783c8f4cSPeter De Schrijver 
28783c8f4cSPeter De Schrijver static const char *tegra_revision_name[TEGRA_REVISION_MAX] = {
29783c8f4cSPeter De Schrijver 	[TEGRA_REVISION_UNKNOWN] = "unknown",
30783c8f4cSPeter De Schrijver 	[TEGRA_REVISION_A01]     = "A01",
31783c8f4cSPeter De Schrijver 	[TEGRA_REVISION_A02]     = "A02",
32783c8f4cSPeter De Schrijver 	[TEGRA_REVISION_A03]     = "A03",
33783c8f4cSPeter De Schrijver 	[TEGRA_REVISION_A03p]    = "A03 prime",
34783c8f4cSPeter De Schrijver 	[TEGRA_REVISION_A04]     = "A04",
35783c8f4cSPeter De Schrijver };
36783c8f4cSPeter De Schrijver 
37783c8f4cSPeter De Schrijver static const struct of_device_id car_match[] __initconst = {
38783c8f4cSPeter De Schrijver 	{ .compatible = "nvidia,tegra20-car", },
39783c8f4cSPeter De Schrijver 	{ .compatible = "nvidia,tegra30-car", },
40783c8f4cSPeter De Schrijver 	{ .compatible = "nvidia,tegra114-car", },
41783c8f4cSPeter De Schrijver 	{ .compatible = "nvidia,tegra124-car", },
429b07eb05SThierry Reding 	{ .compatible = "nvidia,tegra132-car", },
430dc5a0d8SThierry Reding 	{ .compatible = "nvidia,tegra210-car", },
44783c8f4cSPeter De Schrijver 	{},
45783c8f4cSPeter De Schrijver };
46783c8f4cSPeter De Schrijver 
477e939de1SThierry Reding static struct tegra_fuse *fuse = &(struct tegra_fuse) {
487e939de1SThierry Reding 	.base = NULL,
497e939de1SThierry Reding 	.soc = NULL,
507e939de1SThierry Reding };
517e939de1SThierry Reding 
527e939de1SThierry Reding static const struct of_device_id tegra_fuse_match[] = {
531f44febfSThierry Reding #ifdef CONFIG_ARCH_TEGRA_234_SOC
541f44febfSThierry Reding 	{ .compatible = "nvidia,tegra234-efuse", .data = &tegra234_fuse_soc },
551f44febfSThierry Reding #endif
563979a4c6SJC Kuo #ifdef CONFIG_ARCH_TEGRA_194_SOC
573979a4c6SJC Kuo 	{ .compatible = "nvidia,tegra194-efuse", .data = &tegra194_fuse_soc },
583979a4c6SJC Kuo #endif
5983468fe2STimo Alho #ifdef CONFIG_ARCH_TEGRA_186_SOC
6083468fe2STimo Alho 	{ .compatible = "nvidia,tegra186-efuse", .data = &tegra186_fuse_soc },
6183468fe2STimo Alho #endif
620dc5a0d8SThierry Reding #ifdef CONFIG_ARCH_TEGRA_210_SOC
630dc5a0d8SThierry Reding 	{ .compatible = "nvidia,tegra210-efuse", .data = &tegra210_fuse_soc },
640dc5a0d8SThierry Reding #endif
657e939de1SThierry Reding #ifdef CONFIG_ARCH_TEGRA_132_SOC
667e939de1SThierry Reding 	{ .compatible = "nvidia,tegra132-efuse", .data = &tegra124_fuse_soc },
677e939de1SThierry Reding #endif
687e939de1SThierry Reding #ifdef CONFIG_ARCH_TEGRA_124_SOC
697e939de1SThierry Reding 	{ .compatible = "nvidia,tegra124-efuse", .data = &tegra124_fuse_soc },
707e939de1SThierry Reding #endif
717e939de1SThierry Reding #ifdef CONFIG_ARCH_TEGRA_114_SOC
727e939de1SThierry Reding 	{ .compatible = "nvidia,tegra114-efuse", .data = &tegra114_fuse_soc },
737e939de1SThierry Reding #endif
747e939de1SThierry Reding #ifdef CONFIG_ARCH_TEGRA_3x_SOC
757e939de1SThierry Reding 	{ .compatible = "nvidia,tegra30-efuse", .data = &tegra30_fuse_soc },
767e939de1SThierry Reding #endif
777e939de1SThierry Reding #ifdef CONFIG_ARCH_TEGRA_2x_SOC
787e939de1SThierry Reding 	{ .compatible = "nvidia,tegra20-efuse", .data = &tegra20_fuse_soc },
797e939de1SThierry Reding #endif
807e939de1SThierry Reding 	{ /* sentinel */ }
817e939de1SThierry Reding };
827e939de1SThierry Reding 
8396ee12b2SThierry Reding static int tegra_fuse_read(void *priv, unsigned int offset, void *value,
8496ee12b2SThierry Reding 			   size_t bytes)
8596ee12b2SThierry Reding {
8696ee12b2SThierry Reding 	unsigned int count = bytes / 4, i;
8796ee12b2SThierry Reding 	struct tegra_fuse *fuse = priv;
8896ee12b2SThierry Reding 	u32 *buffer = value;
8996ee12b2SThierry Reding 
9096ee12b2SThierry Reding 	for (i = 0; i < count; i++)
9196ee12b2SThierry Reding 		buffer[i] = fuse->read(fuse, offset + i * 4);
9296ee12b2SThierry Reding 
9396ee12b2SThierry Reding 	return 0;
9496ee12b2SThierry Reding }
9596ee12b2SThierry Reding 
96f4619c7fSThierry Reding static const struct nvmem_cell_info tegra_fuse_cells[] = {
97f4619c7fSThierry Reding 	{
98f4619c7fSThierry Reding 		.name = "tsensor-cpu1",
99f4619c7fSThierry Reding 		.offset = 0x084,
100f4619c7fSThierry Reding 		.bytes = 4,
101f4619c7fSThierry Reding 		.bit_offset = 0,
102f4619c7fSThierry Reding 		.nbits = 32,
103f4619c7fSThierry Reding 	}, {
104f4619c7fSThierry Reding 		.name = "tsensor-cpu2",
105f4619c7fSThierry Reding 		.offset = 0x088,
106f4619c7fSThierry Reding 		.bytes = 4,
107f4619c7fSThierry Reding 		.bit_offset = 0,
108f4619c7fSThierry Reding 		.nbits = 32,
109f4619c7fSThierry Reding 	}, {
110f4619c7fSThierry Reding 		.name = "tsensor-cpu0",
111f4619c7fSThierry Reding 		.offset = 0x098,
112f4619c7fSThierry Reding 		.bytes = 4,
113f4619c7fSThierry Reding 		.bit_offset = 0,
114f4619c7fSThierry Reding 		.nbits = 32,
115f4619c7fSThierry Reding 	}, {
116f4619c7fSThierry Reding 		.name = "xusb-pad-calibration",
117f4619c7fSThierry Reding 		.offset = 0x0f0,
118f4619c7fSThierry Reding 		.bytes = 4,
119f4619c7fSThierry Reding 		.bit_offset = 0,
120f4619c7fSThierry Reding 		.nbits = 32,
121f4619c7fSThierry Reding 	}, {
122f4619c7fSThierry Reding 		.name = "tsensor-cpu3",
123f4619c7fSThierry Reding 		.offset = 0x12c,
124f4619c7fSThierry Reding 		.bytes = 4,
125f4619c7fSThierry Reding 		.bit_offset = 0,
126f4619c7fSThierry Reding 		.nbits = 32,
127f4619c7fSThierry Reding 	}, {
128f4619c7fSThierry Reding 		.name = "sata-calibration",
129f4619c7fSThierry Reding 		.offset = 0x124,
130f4619c7fSThierry Reding 		.bytes = 1,
131f4619c7fSThierry Reding 		.bit_offset = 0,
132f4619c7fSThierry Reding 		.nbits = 2,
133f4619c7fSThierry Reding 	}, {
134f4619c7fSThierry Reding 		.name = "tsensor-gpu",
135f4619c7fSThierry Reding 		.offset = 0x154,
136f4619c7fSThierry Reding 		.bytes = 4,
137f4619c7fSThierry Reding 		.bit_offset = 0,
138f4619c7fSThierry Reding 		.nbits = 32,
139f4619c7fSThierry Reding 	}, {
140f4619c7fSThierry Reding 		.name = "tsensor-mem0",
141f4619c7fSThierry Reding 		.offset = 0x158,
142f4619c7fSThierry Reding 		.bytes = 4,
143f4619c7fSThierry Reding 		.bit_offset = 0,
144f4619c7fSThierry Reding 		.nbits = 32,
145f4619c7fSThierry Reding 	}, {
146f4619c7fSThierry Reding 		.name = "tsensor-mem1",
147f4619c7fSThierry Reding 		.offset = 0x15c,
148f4619c7fSThierry Reding 		.bytes = 4,
149f4619c7fSThierry Reding 		.bit_offset = 0,
150f4619c7fSThierry Reding 		.nbits = 32,
151f4619c7fSThierry Reding 	}, {
152f4619c7fSThierry Reding 		.name = "tsensor-pllx",
153f4619c7fSThierry Reding 		.offset = 0x160,
154f4619c7fSThierry Reding 		.bytes = 4,
155f4619c7fSThierry Reding 		.bit_offset = 0,
156f4619c7fSThierry Reding 		.nbits = 32,
157f4619c7fSThierry Reding 	}, {
158f4619c7fSThierry Reding 		.name = "tsensor-common",
159f4619c7fSThierry Reding 		.offset = 0x180,
160f4619c7fSThierry Reding 		.bytes = 4,
161f4619c7fSThierry Reding 		.bit_offset = 0,
162f4619c7fSThierry Reding 		.nbits = 32,
163f4619c7fSThierry Reding 	}, {
164f4619c7fSThierry Reding 		.name = "tsensor-realignment",
165f4619c7fSThierry Reding 		.offset = 0x1fc,
166f4619c7fSThierry Reding 		.bytes = 4,
167f4619c7fSThierry Reding 		.bit_offset = 0,
168f4619c7fSThierry Reding 		.nbits = 32,
169f4619c7fSThierry Reding 	}, {
170f4619c7fSThierry Reding 		.name = "gpu-calibration",
171f4619c7fSThierry Reding 		.offset = 0x204,
172f4619c7fSThierry Reding 		.bytes = 4,
173f4619c7fSThierry Reding 		.bit_offset = 0,
174f4619c7fSThierry Reding 		.nbits = 32,
175f4619c7fSThierry Reding 	}, {
176f4619c7fSThierry Reding 		.name = "xusb-pad-calibration-ext",
177f4619c7fSThierry Reding 		.offset = 0x250,
178f4619c7fSThierry Reding 		.bytes = 4,
179f4619c7fSThierry Reding 		.bit_offset = 0,
180f4619c7fSThierry Reding 		.nbits = 32,
181f4619c7fSThierry Reding 	},
182f4619c7fSThierry Reding };
183f4619c7fSThierry Reding 
1847e939de1SThierry Reding static int tegra_fuse_probe(struct platform_device *pdev)
1857e939de1SThierry Reding {
1867e939de1SThierry Reding 	void __iomem *base = fuse->base;
18796ee12b2SThierry Reding 	struct nvmem_config nvmem;
1887e939de1SThierry Reding 	struct resource *res;
1897e939de1SThierry Reding 	int err;
1907e939de1SThierry Reding 
1917e939de1SThierry Reding 	/* take over the memory region from the early initialization */
1927e939de1SThierry Reding 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
19355a042b3SDmitry Osipenko 	fuse->phys = res->start;
1947e939de1SThierry Reding 	fuse->base = devm_ioremap_resource(&pdev->dev, res);
19551294bf6STimo Alho 	if (IS_ERR(fuse->base)) {
19651294bf6STimo Alho 		err = PTR_ERR(fuse->base);
19751294bf6STimo Alho 		fuse->base = base;
19851294bf6STimo Alho 		return err;
19951294bf6STimo Alho 	}
2007e939de1SThierry Reding 
2017e939de1SThierry Reding 	fuse->clk = devm_clk_get(&pdev->dev, "fuse");
2027e939de1SThierry Reding 	if (IS_ERR(fuse->clk)) {
203f0b2835fSThierry Reding 		if (PTR_ERR(fuse->clk) != -EPROBE_DEFER)
2047e939de1SThierry Reding 			dev_err(&pdev->dev, "failed to get FUSE clock: %ld",
2057e939de1SThierry Reding 				PTR_ERR(fuse->clk));
206f0b2835fSThierry Reding 
20751294bf6STimo Alho 		fuse->base = base;
2087e939de1SThierry Reding 		return PTR_ERR(fuse->clk);
2097e939de1SThierry Reding 	}
2107e939de1SThierry Reding 
2117e939de1SThierry Reding 	platform_set_drvdata(pdev, fuse);
2127e939de1SThierry Reding 	fuse->dev = &pdev->dev;
2137e939de1SThierry Reding 
21424a15252SDmitry Osipenko 	pm_runtime_enable(&pdev->dev);
21524a15252SDmitry Osipenko 
2167e939de1SThierry Reding 	if (fuse->soc->probe) {
2177e939de1SThierry Reding 		err = fuse->soc->probe(fuse);
2189f1022b8SThierry Reding 		if (err < 0)
2199f1022b8SThierry Reding 			goto restore;
22051294bf6STimo Alho 	}
2217e939de1SThierry Reding 
22296ee12b2SThierry Reding 	memset(&nvmem, 0, sizeof(nvmem));
22396ee12b2SThierry Reding 	nvmem.dev = &pdev->dev;
22496ee12b2SThierry Reding 	nvmem.name = "fuse";
22596ee12b2SThierry Reding 	nvmem.id = -1;
22696ee12b2SThierry Reding 	nvmem.owner = THIS_MODULE;
227f4619c7fSThierry Reding 	nvmem.cells = tegra_fuse_cells;
228f4619c7fSThierry Reding 	nvmem.ncells = ARRAY_SIZE(tegra_fuse_cells);
22996ee12b2SThierry Reding 	nvmem.type = NVMEM_TYPE_OTP;
23096ee12b2SThierry Reding 	nvmem.read_only = true;
23196ee12b2SThierry Reding 	nvmem.root_only = true;
23296ee12b2SThierry Reding 	nvmem.reg_read = tegra_fuse_read;
23396ee12b2SThierry Reding 	nvmem.size = fuse->soc->info->size;
23496ee12b2SThierry Reding 	nvmem.word_size = 4;
23596ee12b2SThierry Reding 	nvmem.stride = 4;
23696ee12b2SThierry Reding 	nvmem.priv = fuse;
23796ee12b2SThierry Reding 
23896ee12b2SThierry Reding 	fuse->nvmem = devm_nvmem_register(&pdev->dev, &nvmem);
23996ee12b2SThierry Reding 	if (IS_ERR(fuse->nvmem)) {
24096ee12b2SThierry Reding 		err = PTR_ERR(fuse->nvmem);
24196ee12b2SThierry Reding 		dev_err(&pdev->dev, "failed to register NVMEM device: %d\n",
24296ee12b2SThierry Reding 			err);
2439f1022b8SThierry Reding 		goto restore;
2449f1022b8SThierry Reding 	}
2457e939de1SThierry Reding 
2467e939de1SThierry Reding 	/* release the early I/O memory mapping */
2477e939de1SThierry Reding 	iounmap(base);
2487e939de1SThierry Reding 
2497e939de1SThierry Reding 	return 0;
2509f1022b8SThierry Reding 
2519f1022b8SThierry Reding restore:
252a65a4ea1SDmitry Osipenko 	fuse->clk = NULL;
2539f1022b8SThierry Reding 	fuse->base = base;
25424a15252SDmitry Osipenko 	pm_runtime_disable(&pdev->dev);
2559f1022b8SThierry Reding 	return err;
2567e939de1SThierry Reding }
2577e939de1SThierry Reding 
25824a15252SDmitry Osipenko static int __maybe_unused tegra_fuse_runtime_resume(struct device *dev)
25924a15252SDmitry Osipenko {
26024a15252SDmitry Osipenko 	int err;
26124a15252SDmitry Osipenko 
26224a15252SDmitry Osipenko 	err = clk_prepare_enable(fuse->clk);
26324a15252SDmitry Osipenko 	if (err < 0) {
26424a15252SDmitry Osipenko 		dev_err(dev, "failed to enable FUSE clock: %d\n", err);
26524a15252SDmitry Osipenko 		return err;
26624a15252SDmitry Osipenko 	}
26724a15252SDmitry Osipenko 
26824a15252SDmitry Osipenko 	return 0;
26924a15252SDmitry Osipenko }
27024a15252SDmitry Osipenko 
27124a15252SDmitry Osipenko static int __maybe_unused tegra_fuse_runtime_suspend(struct device *dev)
27224a15252SDmitry Osipenko {
27324a15252SDmitry Osipenko 	clk_disable_unprepare(fuse->clk);
27424a15252SDmitry Osipenko 
27524a15252SDmitry Osipenko 	return 0;
27624a15252SDmitry Osipenko }
27724a15252SDmitry Osipenko 
278*59c6fcebSDmitry Osipenko static int __maybe_unused tegra_fuse_suspend(struct device *dev)
279*59c6fcebSDmitry Osipenko {
280*59c6fcebSDmitry Osipenko 	int ret;
281*59c6fcebSDmitry Osipenko 
282*59c6fcebSDmitry Osipenko 	/*
283*59c6fcebSDmitry Osipenko 	 * Critical for RAM re-repair operation, which must occur on resume
284*59c6fcebSDmitry Osipenko 	 * from LP1 system suspend and as part of CCPLEX cluster switching.
285*59c6fcebSDmitry Osipenko 	 */
286*59c6fcebSDmitry Osipenko 	if (fuse->soc->clk_suspend_on)
287*59c6fcebSDmitry Osipenko 		ret = pm_runtime_resume_and_get(dev);
288*59c6fcebSDmitry Osipenko 	else
289*59c6fcebSDmitry Osipenko 		ret = pm_runtime_force_suspend(dev);
290*59c6fcebSDmitry Osipenko 
291*59c6fcebSDmitry Osipenko 	return ret;
292*59c6fcebSDmitry Osipenko }
293*59c6fcebSDmitry Osipenko 
294*59c6fcebSDmitry Osipenko static int __maybe_unused tegra_fuse_resume(struct device *dev)
295*59c6fcebSDmitry Osipenko {
296*59c6fcebSDmitry Osipenko 	int ret = 0;
297*59c6fcebSDmitry Osipenko 
298*59c6fcebSDmitry Osipenko 	if (fuse->soc->clk_suspend_on)
299*59c6fcebSDmitry Osipenko 		pm_runtime_put(dev);
300*59c6fcebSDmitry Osipenko 	else
301*59c6fcebSDmitry Osipenko 		ret = pm_runtime_force_resume(dev);
302*59c6fcebSDmitry Osipenko 
303*59c6fcebSDmitry Osipenko 	return ret;
304*59c6fcebSDmitry Osipenko }
305*59c6fcebSDmitry Osipenko 
30624a15252SDmitry Osipenko static const struct dev_pm_ops tegra_fuse_pm = {
30724a15252SDmitry Osipenko 	SET_RUNTIME_PM_OPS(tegra_fuse_runtime_suspend, tegra_fuse_runtime_resume,
30824a15252SDmitry Osipenko 			   NULL)
309*59c6fcebSDmitry Osipenko 	SET_SYSTEM_SLEEP_PM_OPS(tegra_fuse_suspend, tegra_fuse_resume)
31024a15252SDmitry Osipenko };
31124a15252SDmitry Osipenko 
3127e939de1SThierry Reding static struct platform_driver tegra_fuse_driver = {
3137e939de1SThierry Reding 	.driver = {
3147e939de1SThierry Reding 		.name = "tegra-fuse",
3157e939de1SThierry Reding 		.of_match_table = tegra_fuse_match,
31624a15252SDmitry Osipenko 		.pm = &tegra_fuse_pm,
3177e939de1SThierry Reding 		.suppress_bind_attrs = true,
3187e939de1SThierry Reding 	},
3197e939de1SThierry Reding 	.probe = tegra_fuse_probe,
3207e939de1SThierry Reding };
3211859217bSPaul Gortmaker builtin_platform_driver(tegra_fuse_driver);
3227e939de1SThierry Reding 
3237e939de1SThierry Reding bool __init tegra_fuse_read_spare(unsigned int spare)
3247e939de1SThierry Reding {
3257e939de1SThierry Reding 	unsigned int offset = fuse->soc->info->spare + spare * 4;
3267e939de1SThierry Reding 
3277e939de1SThierry Reding 	return fuse->read_early(fuse, offset) & 1;
3287e939de1SThierry Reding }
3297e939de1SThierry Reding 
3307e939de1SThierry Reding u32 __init tegra_fuse_read_early(unsigned int offset)
3317e939de1SThierry Reding {
3327e939de1SThierry Reding 	return fuse->read_early(fuse, offset);
3337e939de1SThierry Reding }
3347e939de1SThierry Reding 
3357e939de1SThierry Reding int tegra_fuse_readl(unsigned long offset, u32 *value)
3367e939de1SThierry Reding {
3370a728e0bSNagarjuna Kristam 	if (!fuse->read || !fuse->clk)
3387e939de1SThierry Reding 		return -EPROBE_DEFER;
3397e939de1SThierry Reding 
3400a728e0bSNagarjuna Kristam 	if (IS_ERR(fuse->clk))
3410a728e0bSNagarjuna Kristam 		return PTR_ERR(fuse->clk);
3420a728e0bSNagarjuna Kristam 
3437e939de1SThierry Reding 	*value = fuse->read(fuse, offset);
3447e939de1SThierry Reding 
3457e939de1SThierry Reding 	return 0;
3467e939de1SThierry Reding }
3477e939de1SThierry Reding EXPORT_SYMBOL(tegra_fuse_readl);
3487e939de1SThierry Reding 
349783c8f4cSPeter De Schrijver static void tegra_enable_fuse_clk(void __iomem *base)
350783c8f4cSPeter De Schrijver {
351783c8f4cSPeter De Schrijver 	u32 reg;
352783c8f4cSPeter De Schrijver 
353783c8f4cSPeter De Schrijver 	reg = readl_relaxed(base + 0x48);
354783c8f4cSPeter De Schrijver 	reg |= 1 << 28;
355783c8f4cSPeter De Schrijver 	writel(reg, base + 0x48);
356783c8f4cSPeter De Schrijver 
357783c8f4cSPeter De Schrijver 	/*
358783c8f4cSPeter De Schrijver 	 * Enable FUSE clock. This needs to be hardcoded because the clock
359783c8f4cSPeter De Schrijver 	 * subsystem is not active during early boot.
360783c8f4cSPeter De Schrijver 	 */
361783c8f4cSPeter De Schrijver 	reg = readl(base + 0x14);
362783c8f4cSPeter De Schrijver 	reg |= 1 << 7;
363783c8f4cSPeter De Schrijver 	writel(reg, base + 0x14);
364783c8f4cSPeter De Schrijver }
365783c8f4cSPeter De Schrijver 
366379ac9ebSJon Hunter static ssize_t major_show(struct device *dev, struct device_attribute *attr,
367379ac9ebSJon Hunter 			     char *buf)
368379ac9ebSJon Hunter {
369379ac9ebSJon Hunter 	return sprintf(buf, "%d\n", tegra_get_major_rev());
370379ac9ebSJon Hunter }
371379ac9ebSJon Hunter 
372379ac9ebSJon Hunter static DEVICE_ATTR_RO(major);
373379ac9ebSJon Hunter 
374379ac9ebSJon Hunter static ssize_t minor_show(struct device *dev, struct device_attribute *attr,
375379ac9ebSJon Hunter 			     char *buf)
376379ac9ebSJon Hunter {
377379ac9ebSJon Hunter 	return sprintf(buf, "%d\n", tegra_get_minor_rev());
378379ac9ebSJon Hunter }
379379ac9ebSJon Hunter 
380379ac9ebSJon Hunter static DEVICE_ATTR_RO(minor);
381379ac9ebSJon Hunter 
382379ac9ebSJon Hunter static struct attribute *tegra_soc_attr[] = {
383379ac9ebSJon Hunter 	&dev_attr_major.attr,
384379ac9ebSJon Hunter 	&dev_attr_minor.attr,
385379ac9ebSJon Hunter 	NULL,
386379ac9ebSJon Hunter };
387379ac9ebSJon Hunter 
388379ac9ebSJon Hunter const struct attribute_group tegra_soc_attr_group = {
389379ac9ebSJon Hunter 	.attrs = tegra_soc_attr,
390379ac9ebSJon Hunter };
391379ac9ebSJon Hunter 
3921f44febfSThierry Reding #if IS_ENABLED(CONFIG_ARCH_TEGRA_194_SOC) || \
3931f44febfSThierry Reding     IS_ENABLED(CONFIG_ARCH_TEGRA_234_SOC)
394379ac9ebSJon Hunter static ssize_t platform_show(struct device *dev, struct device_attribute *attr,
395379ac9ebSJon Hunter 			     char *buf)
396379ac9ebSJon Hunter {
397379ac9ebSJon Hunter 	/*
398379ac9ebSJon Hunter 	 * Displays the value in the 'pre_si_platform' field of the HIDREV
399379ac9ebSJon Hunter 	 * register for Tegra194 devices. A value of 0 indicates that the
400379ac9ebSJon Hunter 	 * platform type is silicon and all other non-zero values indicate
401379ac9ebSJon Hunter 	 * the type of simulation platform is being used.
402379ac9ebSJon Hunter 	 */
403775edf78SThierry Reding 	return sprintf(buf, "%d\n", tegra_get_platform());
404379ac9ebSJon Hunter }
405379ac9ebSJon Hunter 
406379ac9ebSJon Hunter static DEVICE_ATTR_RO(platform);
407379ac9ebSJon Hunter 
408379ac9ebSJon Hunter static struct attribute *tegra194_soc_attr[] = {
409379ac9ebSJon Hunter 	&dev_attr_major.attr,
410379ac9ebSJon Hunter 	&dev_attr_minor.attr,
411379ac9ebSJon Hunter 	&dev_attr_platform.attr,
412379ac9ebSJon Hunter 	NULL,
413379ac9ebSJon Hunter };
414379ac9ebSJon Hunter 
415379ac9ebSJon Hunter const struct attribute_group tegra194_soc_attr_group = {
416379ac9ebSJon Hunter 	.attrs = tegra194_soc_attr,
417379ac9ebSJon Hunter };
418379ac9ebSJon Hunter #endif
419379ac9ebSJon Hunter 
42027a0342aSThierry Reding struct device * __init tegra_soc_device_register(void)
42127a0342aSThierry Reding {
42227a0342aSThierry Reding 	struct soc_device_attribute *attr;
42327a0342aSThierry Reding 	struct soc_device *dev;
42427a0342aSThierry Reding 
42527a0342aSThierry Reding 	attr = kzalloc(sizeof(*attr), GFP_KERNEL);
42627a0342aSThierry Reding 	if (!attr)
42727a0342aSThierry Reding 		return NULL;
42827a0342aSThierry Reding 
42927a0342aSThierry Reding 	attr->family = kasprintf(GFP_KERNEL, "Tegra");
43037558ac8SJon Hunter 	attr->revision = kasprintf(GFP_KERNEL, "%s",
43137558ac8SJon Hunter 		tegra_revision_name[tegra_sku_info.revision]);
43227a0342aSThierry Reding 	attr->soc_id = kasprintf(GFP_KERNEL, "%u", tegra_get_chip_id());
433379ac9ebSJon Hunter 	attr->custom_attr_group = fuse->soc->soc_attr_group;
43427a0342aSThierry Reding 
43527a0342aSThierry Reding 	dev = soc_device_register(attr);
43627a0342aSThierry Reding 	if (IS_ERR(dev)) {
43727a0342aSThierry Reding 		kfree(attr->soc_id);
43827a0342aSThierry Reding 		kfree(attr->revision);
43927a0342aSThierry Reding 		kfree(attr->family);
44027a0342aSThierry Reding 		kfree(attr);
44127a0342aSThierry Reding 		return ERR_CAST(dev);
44227a0342aSThierry Reding 	}
44327a0342aSThierry Reding 
44427a0342aSThierry Reding 	return soc_device_to_device(dev);
44527a0342aSThierry Reding }
44627a0342aSThierry Reding 
44724fa5af8SThierry Reding static int __init tegra_init_fuse(void)
448783c8f4cSPeter De Schrijver {
4497e939de1SThierry Reding 	const struct of_device_id *match;
450783c8f4cSPeter De Schrijver 	struct device_node *np;
4517e939de1SThierry Reding 	struct resource regs;
45224fa5af8SThierry Reding 
453783c8f4cSPeter De Schrijver 	tegra_init_apbmisc();
454783c8f4cSPeter De Schrijver 
4557e939de1SThierry Reding 	np = of_find_matching_node_and_match(NULL, tegra_fuse_match, &match);
4567e939de1SThierry Reding 	if (!np) {
4577e939de1SThierry Reding 		/*
4587e939de1SThierry Reding 		 * Fall back to legacy initialization for 32-bit ARM only. All
4597e939de1SThierry Reding 		 * 64-bit ARM device tree files for Tegra are required to have
4607e939de1SThierry Reding 		 * a FUSE node.
4617e939de1SThierry Reding 		 *
4627e939de1SThierry Reding 		 * This is for backwards-compatibility with old device trees
4637e939de1SThierry Reding 		 * that didn't contain a FUSE node.
4647e939de1SThierry Reding 		 */
4657e939de1SThierry Reding 		if (IS_ENABLED(CONFIG_ARM) && soc_is_tegra()) {
4667e939de1SThierry Reding 			u8 chip = tegra_get_chip_id();
4677e939de1SThierry Reding 
4687e939de1SThierry Reding 			regs.start = 0x7000f800;
4697e939de1SThierry Reding 			regs.end = 0x7000fbff;
4707e939de1SThierry Reding 			regs.flags = IORESOURCE_MEM;
4717e939de1SThierry Reding 
4727e939de1SThierry Reding 			switch (chip) {
4737e939de1SThierry Reding #ifdef CONFIG_ARCH_TEGRA_2x_SOC
4747e939de1SThierry Reding 			case TEGRA20:
4757e939de1SThierry Reding 				fuse->soc = &tegra20_fuse_soc;
4767e939de1SThierry Reding 				break;
4777e939de1SThierry Reding #endif
4787e939de1SThierry Reding 
4797e939de1SThierry Reding #ifdef CONFIG_ARCH_TEGRA_3x_SOC
4807e939de1SThierry Reding 			case TEGRA30:
4817e939de1SThierry Reding 				fuse->soc = &tegra30_fuse_soc;
4827e939de1SThierry Reding 				break;
4837e939de1SThierry Reding #endif
4847e939de1SThierry Reding 
4857e939de1SThierry Reding #ifdef CONFIG_ARCH_TEGRA_114_SOC
4867e939de1SThierry Reding 			case TEGRA114:
4877e939de1SThierry Reding 				fuse->soc = &tegra114_fuse_soc;
4887e939de1SThierry Reding 				break;
4897e939de1SThierry Reding #endif
4907e939de1SThierry Reding 
4917e939de1SThierry Reding #ifdef CONFIG_ARCH_TEGRA_124_SOC
4927e939de1SThierry Reding 			case TEGRA124:
4937e939de1SThierry Reding 				fuse->soc = &tegra124_fuse_soc;
4947e939de1SThierry Reding 				break;
4957e939de1SThierry Reding #endif
4967e939de1SThierry Reding 
4977e939de1SThierry Reding 			default:
4987e939de1SThierry Reding 				pr_warn("Unsupported SoC: %02x\n", chip);
4997e939de1SThierry Reding 				break;
5007e939de1SThierry Reding 			}
501783c8f4cSPeter De Schrijver 		} else {
5027e939de1SThierry Reding 			/*
5037e939de1SThierry Reding 			 * At this point we're not running on Tegra, so play
5047e939de1SThierry Reding 			 * nice with multi-platform kernels.
5057e939de1SThierry Reding 			 */
5067e939de1SThierry Reding 			return 0;
5077e939de1SThierry Reding 		}
5087e939de1SThierry Reding 	} else {
5097e939de1SThierry Reding 		/*
5107e939de1SThierry Reding 		 * Extract information from the device tree if we've found a
5117e939de1SThierry Reding 		 * matching node.
5127e939de1SThierry Reding 		 */
5137e939de1SThierry Reding 		if (of_address_to_resource(np, 0, &regs) < 0) {
5147e939de1SThierry Reding 			pr_err("failed to get FUSE register\n");
51524fa5af8SThierry Reding 			return -ENXIO;
516783c8f4cSPeter De Schrijver 		}
517783c8f4cSPeter De Schrijver 
5187e939de1SThierry Reding 		fuse->soc = match->data;
5197e939de1SThierry Reding 	}
5207e939de1SThierry Reding 
5217e939de1SThierry Reding 	np = of_find_matching_node(NULL, car_match);
5227e939de1SThierry Reding 	if (np) {
5237e939de1SThierry Reding 		void __iomem *base = of_iomap(np, 0);
5247e939de1SThierry Reding 		if (base) {
5257e939de1SThierry Reding 			tegra_enable_fuse_clk(base);
5267e939de1SThierry Reding 			iounmap(base);
5277e939de1SThierry Reding 		} else {
5287e939de1SThierry Reding 			pr_err("failed to map clock registers\n");
5297e939de1SThierry Reding 			return -ENXIO;
5307e939de1SThierry Reding 		}
5317e939de1SThierry Reding 	}
5327e939de1SThierry Reding 
5334bdc0d67SChristoph Hellwig 	fuse->base = ioremap(regs.start, resource_size(&regs));
5347e939de1SThierry Reding 	if (!fuse->base) {
5357e939de1SThierry Reding 		pr_err("failed to map FUSE registers\n");
5367e939de1SThierry Reding 		return -ENXIO;
5377e939de1SThierry Reding 	}
5387e939de1SThierry Reding 
5397e939de1SThierry Reding 	fuse->soc->init(fuse);
540783c8f4cSPeter De Schrijver 
54103b3f4c8SThierry Reding 	pr_info("Tegra Revision: %s SKU: %d CPU Process: %d SoC Process: %d\n",
542783c8f4cSPeter De Schrijver 		tegra_revision_name[tegra_sku_info.revision],
543783c8f4cSPeter De Schrijver 		tegra_sku_info.sku_id, tegra_sku_info.cpu_process_id,
54403b3f4c8SThierry Reding 		tegra_sku_info.soc_process_id);
54503b3f4c8SThierry Reding 	pr_debug("Tegra CPU Speedo ID %d, SoC Speedo ID %d\n",
546783c8f4cSPeter De Schrijver 		 tegra_sku_info.cpu_speedo_id, tegra_sku_info.soc_speedo_id);
54724fa5af8SThierry Reding 
5489f94faddSThierry Reding 	if (fuse->soc->lookups) {
5499f94faddSThierry Reding 		size_t size = sizeof(*fuse->lookups) * fuse->soc->num_lookups;
5509f94faddSThierry Reding 
5519f94faddSThierry Reding 		fuse->lookups = kmemdup(fuse->soc->lookups, size, GFP_KERNEL);
552854d128bSYang Yingliang 		if (fuse->lookups)
5539f94faddSThierry Reding 			nvmem_add_cell_lookups(fuse->lookups, fuse->soc->num_lookups);
5549f94faddSThierry Reding 	}
55527a0342aSThierry Reding 
55624fa5af8SThierry Reding 	return 0;
557783c8f4cSPeter De Schrijver }
55824fa5af8SThierry Reding early_initcall(tegra_init_fuse);
55927a0342aSThierry Reding 
56027a0342aSThierry Reding #ifdef CONFIG_ARM64
56127a0342aSThierry Reding static int __init tegra_init_soc(void)
56227a0342aSThierry Reding {
563226cff48SThierry Reding 	struct device_node *np;
56427a0342aSThierry Reding 	struct device *soc;
56527a0342aSThierry Reding 
566226cff48SThierry Reding 	/* make sure we're running on Tegra */
567226cff48SThierry Reding 	np = of_find_matching_node(NULL, tegra_fuse_match);
568226cff48SThierry Reding 	if (!np)
569226cff48SThierry Reding 		return 0;
570226cff48SThierry Reding 
571226cff48SThierry Reding 	of_node_put(np);
572226cff48SThierry Reding 
57327a0342aSThierry Reding 	soc = tegra_soc_device_register();
57427a0342aSThierry Reding 	if (IS_ERR(soc)) {
57527a0342aSThierry Reding 		pr_err("failed to register SoC device: %ld\n", PTR_ERR(soc));
57627a0342aSThierry Reding 		return PTR_ERR(soc);
57727a0342aSThierry Reding 	}
57827a0342aSThierry Reding 
57927a0342aSThierry Reding 	return 0;
58027a0342aSThierry Reding }
5819261b43eSThierry Reding device_initcall(tegra_init_soc);
58227a0342aSThierry Reding #endif
583