1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2013-2014, NVIDIA CORPORATION. All rights reserved.
7 #include <linux/device.h>
8 #include <linux/kobject.h>
9 #include <linux/init.h>
11 #include <linux/nvmem-consumer.h>
12 #include <linux/nvmem-provider.h>
14 #include <linux/of_address.h>
15 #include <linux/platform_device.h>
16 #include <linux/slab.h>
17 #include <linux/sys_soc.h>
19 #include <soc/tegra/common.h>
20 #include <soc/tegra/fuse.h>
24 struct tegra_sku_info tegra_sku_info;
25 EXPORT_SYMBOL(tegra_sku_info);
27 static const char *tegra_revision_name[TEGRA_REVISION_MAX] = {
28 [TEGRA_REVISION_UNKNOWN] = "unknown",
29 [TEGRA_REVISION_A01] = "A01",
30 [TEGRA_REVISION_A02] = "A02",
31 [TEGRA_REVISION_A03] = "A03",
32 [TEGRA_REVISION_A03p] = "A03 prime",
33 [TEGRA_REVISION_A04] = "A04",
36 static const struct of_device_id car_match[] __initconst = {
37 { .compatible = "nvidia,tegra20-car", },
38 { .compatible = "nvidia,tegra30-car", },
39 { .compatible = "nvidia,tegra114-car", },
40 { .compatible = "nvidia,tegra124-car", },
41 { .compatible = "nvidia,tegra132-car", },
42 { .compatible = "nvidia,tegra210-car", },
46 static struct tegra_fuse *fuse = &(struct tegra_fuse) {
51 static const struct of_device_id tegra_fuse_match[] = {
52 #ifdef CONFIG_ARCH_TEGRA_194_SOC
53 { .compatible = "nvidia,tegra194-efuse", .data = &tegra194_fuse_soc },
55 #ifdef CONFIG_ARCH_TEGRA_186_SOC
56 { .compatible = "nvidia,tegra186-efuse", .data = &tegra186_fuse_soc },
58 #ifdef CONFIG_ARCH_TEGRA_210_SOC
59 { .compatible = "nvidia,tegra210-efuse", .data = &tegra210_fuse_soc },
61 #ifdef CONFIG_ARCH_TEGRA_132_SOC
62 { .compatible = "nvidia,tegra132-efuse", .data = &tegra124_fuse_soc },
64 #ifdef CONFIG_ARCH_TEGRA_124_SOC
65 { .compatible = "nvidia,tegra124-efuse", .data = &tegra124_fuse_soc },
67 #ifdef CONFIG_ARCH_TEGRA_114_SOC
68 { .compatible = "nvidia,tegra114-efuse", .data = &tegra114_fuse_soc },
70 #ifdef CONFIG_ARCH_TEGRA_3x_SOC
71 { .compatible = "nvidia,tegra30-efuse", .data = &tegra30_fuse_soc },
73 #ifdef CONFIG_ARCH_TEGRA_2x_SOC
74 { .compatible = "nvidia,tegra20-efuse", .data = &tegra20_fuse_soc },
79 static int tegra_fuse_read(void *priv, unsigned int offset, void *value,
82 unsigned int count = bytes / 4, i;
83 struct tegra_fuse *fuse = priv;
86 for (i = 0; i < count; i++)
87 buffer[i] = fuse->read(fuse, offset + i * 4);
92 static const struct nvmem_cell_info tegra_fuse_cells[] = {
94 .name = "tsensor-cpu1",
100 .name = "tsensor-cpu2",
106 .name = "tsensor-cpu0",
112 .name = "xusb-pad-calibration",
118 .name = "tsensor-cpu3",
124 .name = "sata-calibration",
130 .name = "tsensor-gpu",
136 .name = "tsensor-mem0",
142 .name = "tsensor-mem1",
148 .name = "tsensor-pllx",
154 .name = "tsensor-common",
160 .name = "tsensor-realignment",
166 .name = "gpu-calibration",
172 .name = "xusb-pad-calibration-ext",
180 static int tegra_fuse_probe(struct platform_device *pdev)
182 void __iomem *base = fuse->base;
183 struct nvmem_config nvmem;
184 struct resource *res;
187 /* take over the memory region from the early initialization */
188 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
189 fuse->phys = res->start;
190 fuse->base = devm_ioremap_resource(&pdev->dev, res);
191 if (IS_ERR(fuse->base)) {
192 err = PTR_ERR(fuse->base);
197 fuse->clk = devm_clk_get(&pdev->dev, "fuse");
198 if (IS_ERR(fuse->clk)) {
199 if (PTR_ERR(fuse->clk) != -EPROBE_DEFER)
200 dev_err(&pdev->dev, "failed to get FUSE clock: %ld",
204 return PTR_ERR(fuse->clk);
207 platform_set_drvdata(pdev, fuse);
208 fuse->dev = &pdev->dev;
210 if (fuse->soc->probe) {
211 err = fuse->soc->probe(fuse);
216 memset(&nvmem, 0, sizeof(nvmem));
217 nvmem.dev = &pdev->dev;
220 nvmem.owner = THIS_MODULE;
221 nvmem.cells = tegra_fuse_cells;
222 nvmem.ncells = ARRAY_SIZE(tegra_fuse_cells);
223 nvmem.type = NVMEM_TYPE_OTP;
224 nvmem.read_only = true;
225 nvmem.root_only = true;
226 nvmem.reg_read = tegra_fuse_read;
227 nvmem.size = fuse->soc->info->size;
232 fuse->nvmem = devm_nvmem_register(&pdev->dev, &nvmem);
233 if (IS_ERR(fuse->nvmem)) {
234 err = PTR_ERR(fuse->nvmem);
235 dev_err(&pdev->dev, "failed to register NVMEM device: %d\n",
240 /* release the early I/O memory mapping */
250 static struct platform_driver tegra_fuse_driver = {
252 .name = "tegra-fuse",
253 .of_match_table = tegra_fuse_match,
254 .suppress_bind_attrs = true,
256 .probe = tegra_fuse_probe,
258 builtin_platform_driver(tegra_fuse_driver);
260 bool __init tegra_fuse_read_spare(unsigned int spare)
262 unsigned int offset = fuse->soc->info->spare + spare * 4;
264 return fuse->read_early(fuse, offset) & 1;
267 u32 __init tegra_fuse_read_early(unsigned int offset)
269 return fuse->read_early(fuse, offset);
272 int tegra_fuse_readl(unsigned long offset, u32 *value)
274 if (!fuse->read || !fuse->clk)
275 return -EPROBE_DEFER;
277 if (IS_ERR(fuse->clk))
278 return PTR_ERR(fuse->clk);
280 *value = fuse->read(fuse, offset);
284 EXPORT_SYMBOL(tegra_fuse_readl);
286 static void tegra_enable_fuse_clk(void __iomem *base)
290 reg = readl_relaxed(base + 0x48);
292 writel(reg, base + 0x48);
295 * Enable FUSE clock. This needs to be hardcoded because the clock
296 * subsystem is not active during early boot.
298 reg = readl(base + 0x14);
300 writel(reg, base + 0x14);
303 struct device * __init tegra_soc_device_register(void)
305 struct soc_device_attribute *attr;
306 struct soc_device *dev;
308 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
312 attr->family = kasprintf(GFP_KERNEL, "Tegra");
313 attr->revision = kasprintf(GFP_KERNEL, "%d", tegra_sku_info.revision);
314 attr->soc_id = kasprintf(GFP_KERNEL, "%u", tegra_get_chip_id());
316 dev = soc_device_register(attr);
319 kfree(attr->revision);
322 return ERR_CAST(dev);
325 return soc_device_to_device(dev);
328 static int __init tegra_init_fuse(void)
330 const struct of_device_id *match;
331 struct device_node *np;
332 struct resource regs;
334 tegra_init_apbmisc();
336 np = of_find_matching_node_and_match(NULL, tegra_fuse_match, &match);
339 * Fall back to legacy initialization for 32-bit ARM only. All
340 * 64-bit ARM device tree files for Tegra are required to have
343 * This is for backwards-compatibility with old device trees
344 * that didn't contain a FUSE node.
346 if (IS_ENABLED(CONFIG_ARM) && soc_is_tegra()) {
347 u8 chip = tegra_get_chip_id();
349 regs.start = 0x7000f800;
350 regs.end = 0x7000fbff;
351 regs.flags = IORESOURCE_MEM;
354 #ifdef CONFIG_ARCH_TEGRA_2x_SOC
356 fuse->soc = &tegra20_fuse_soc;
360 #ifdef CONFIG_ARCH_TEGRA_3x_SOC
362 fuse->soc = &tegra30_fuse_soc;
366 #ifdef CONFIG_ARCH_TEGRA_114_SOC
368 fuse->soc = &tegra114_fuse_soc;
372 #ifdef CONFIG_ARCH_TEGRA_124_SOC
374 fuse->soc = &tegra124_fuse_soc;
379 pr_warn("Unsupported SoC: %02x\n", chip);
384 * At this point we're not running on Tegra, so play
385 * nice with multi-platform kernels.
391 * Extract information from the device tree if we've found a
394 if (of_address_to_resource(np, 0, ®s) < 0) {
395 pr_err("failed to get FUSE register\n");
399 fuse->soc = match->data;
402 np = of_find_matching_node(NULL, car_match);
404 void __iomem *base = of_iomap(np, 0);
406 tegra_enable_fuse_clk(base);
409 pr_err("failed to map clock registers\n");
414 fuse->base = ioremap_nocache(regs.start, resource_size(®s));
416 pr_err("failed to map FUSE registers\n");
420 fuse->soc->init(fuse);
422 pr_info("Tegra Revision: %s SKU: %d CPU Process: %d SoC Process: %d\n",
423 tegra_revision_name[tegra_sku_info.revision],
424 tegra_sku_info.sku_id, tegra_sku_info.cpu_process_id,
425 tegra_sku_info.soc_process_id);
426 pr_debug("Tegra CPU Speedo ID %d, SoC Speedo ID %d\n",
427 tegra_sku_info.cpu_speedo_id, tegra_sku_info.soc_speedo_id);
429 if (fuse->soc->lookups) {
430 size_t size = sizeof(*fuse->lookups) * fuse->soc->num_lookups;
432 fuse->lookups = kmemdup(fuse->soc->lookups, size, GFP_KERNEL);
436 nvmem_add_cell_lookups(fuse->lookups, fuse->soc->num_lookups);
441 early_initcall(tegra_init_fuse);
444 static int __init tegra_init_soc(void)
446 struct device_node *np;
449 /* make sure we're running on Tegra */
450 np = of_find_matching_node(NULL, tegra_fuse_match);
456 soc = tegra_soc_device_register();
458 pr_err("failed to register SoC device: %ld\n", PTR_ERR(soc));
464 device_initcall(tegra_init_soc);