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>
12 #include <linux/of_address.h>
13 #include <linux/platform_device.h>
14 #include <linux/slab.h>
15 #include <linux/sys_soc.h>
17 #include <soc/tegra/common.h>
18 #include <soc/tegra/fuse.h>
22 struct tegra_sku_info tegra_sku_info
;
23 EXPORT_SYMBOL(tegra_sku_info
);
25 static const char *tegra_revision_name
[TEGRA_REVISION_MAX
] = {
26 [TEGRA_REVISION_UNKNOWN
] = "unknown",
27 [TEGRA_REVISION_A01
] = "A01",
28 [TEGRA_REVISION_A02
] = "A02",
29 [TEGRA_REVISION_A03
] = "A03",
30 [TEGRA_REVISION_A03p
] = "A03 prime",
31 [TEGRA_REVISION_A04
] = "A04",
34 static u8
fuse_readb(struct tegra_fuse
*fuse
, unsigned int offset
)
38 val
= fuse
->read(fuse
, round_down(offset
, 4));
39 val
>>= (offset
% 4) * 8;
45 static ssize_t
fuse_read(struct file
*fd
, struct kobject
*kobj
,
46 struct bin_attribute
*attr
, char *buf
,
47 loff_t pos
, size_t size
)
49 struct device
*dev
= kobj_to_dev(kobj
);
50 struct tegra_fuse
*fuse
= dev_get_drvdata(dev
);
53 if (pos
< 0 || pos
>= attr
->size
)
56 if (size
> attr
->size
- pos
)
57 size
= attr
->size
- pos
;
59 for (i
= 0; i
< size
; i
++)
60 buf
[i
] = fuse_readb(fuse
, pos
+ i
);
65 static struct bin_attribute fuse_bin_attr
= {
66 .attr
= { .name
= "fuse", .mode
= S_IRUGO
, },
70 static int tegra_fuse_create_sysfs(struct device
*dev
, unsigned int size
,
71 const struct tegra_fuse_info
*info
)
73 fuse_bin_attr
.size
= size
;
75 return device_create_bin_file(dev
, &fuse_bin_attr
);
78 static const struct of_device_id car_match
[] __initconst
= {
79 { .compatible
= "nvidia,tegra20-car", },
80 { .compatible
= "nvidia,tegra30-car", },
81 { .compatible
= "nvidia,tegra114-car", },
82 { .compatible
= "nvidia,tegra124-car", },
83 { .compatible
= "nvidia,tegra132-car", },
84 { .compatible
= "nvidia,tegra210-car", },
88 static struct tegra_fuse
*fuse
= &(struct tegra_fuse
) {
93 static const struct of_device_id tegra_fuse_match
[] = {
94 #ifdef CONFIG_ARCH_TEGRA_186_SOC
95 { .compatible
= "nvidia,tegra186-efuse", .data
= &tegra186_fuse_soc
},
97 #ifdef CONFIG_ARCH_TEGRA_210_SOC
98 { .compatible
= "nvidia,tegra210-efuse", .data
= &tegra210_fuse_soc
},
100 #ifdef CONFIG_ARCH_TEGRA_132_SOC
101 { .compatible
= "nvidia,tegra132-efuse", .data
= &tegra124_fuse_soc
},
103 #ifdef CONFIG_ARCH_TEGRA_124_SOC
104 { .compatible
= "nvidia,tegra124-efuse", .data
= &tegra124_fuse_soc
},
106 #ifdef CONFIG_ARCH_TEGRA_114_SOC
107 { .compatible
= "nvidia,tegra114-efuse", .data
= &tegra114_fuse_soc
},
109 #ifdef CONFIG_ARCH_TEGRA_3x_SOC
110 { .compatible
= "nvidia,tegra30-efuse", .data
= &tegra30_fuse_soc
},
112 #ifdef CONFIG_ARCH_TEGRA_2x_SOC
113 { .compatible
= "nvidia,tegra20-efuse", .data
= &tegra20_fuse_soc
},
118 static int tegra_fuse_probe(struct platform_device
*pdev
)
120 void __iomem
*base
= fuse
->base
;
121 struct resource
*res
;
124 /* take over the memory region from the early initialization */
125 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
126 fuse
->phys
= res
->start
;
127 fuse
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
128 if (IS_ERR(fuse
->base
)) {
129 err
= PTR_ERR(fuse
->base
);
134 fuse
->clk
= devm_clk_get(&pdev
->dev
, "fuse");
135 if (IS_ERR(fuse
->clk
)) {
136 if (PTR_ERR(fuse
->clk
) != -EPROBE_DEFER
)
137 dev_err(&pdev
->dev
, "failed to get FUSE clock: %ld",
141 return PTR_ERR(fuse
->clk
);
144 platform_set_drvdata(pdev
, fuse
);
145 fuse
->dev
= &pdev
->dev
;
147 if (fuse
->soc
->probe
) {
148 err
= fuse
->soc
->probe(fuse
);
155 if (tegra_fuse_create_sysfs(&pdev
->dev
, fuse
->soc
->info
->size
,
159 /* release the early I/O memory mapping */
165 static struct platform_driver tegra_fuse_driver
= {
167 .name
= "tegra-fuse",
168 .of_match_table
= tegra_fuse_match
,
169 .suppress_bind_attrs
= true,
171 .probe
= tegra_fuse_probe
,
173 builtin_platform_driver(tegra_fuse_driver
);
175 bool __init
tegra_fuse_read_spare(unsigned int spare
)
177 unsigned int offset
= fuse
->soc
->info
->spare
+ spare
* 4;
179 return fuse
->read_early(fuse
, offset
) & 1;
182 u32 __init
tegra_fuse_read_early(unsigned int offset
)
184 return fuse
->read_early(fuse
, offset
);
187 int tegra_fuse_readl(unsigned long offset
, u32
*value
)
190 return -EPROBE_DEFER
;
192 *value
= fuse
->read(fuse
, offset
);
196 EXPORT_SYMBOL(tegra_fuse_readl
);
198 static void tegra_enable_fuse_clk(void __iomem
*base
)
202 reg
= readl_relaxed(base
+ 0x48);
204 writel(reg
, base
+ 0x48);
207 * Enable FUSE clock. This needs to be hardcoded because the clock
208 * subsystem is not active during early boot.
210 reg
= readl(base
+ 0x14);
212 writel(reg
, base
+ 0x14);
215 struct device
* __init
tegra_soc_device_register(void)
217 struct soc_device_attribute
*attr
;
218 struct soc_device
*dev
;
220 attr
= kzalloc(sizeof(*attr
), GFP_KERNEL
);
224 attr
->family
= kasprintf(GFP_KERNEL
, "Tegra");
225 attr
->revision
= kasprintf(GFP_KERNEL
, "%d", tegra_sku_info
.revision
);
226 attr
->soc_id
= kasprintf(GFP_KERNEL
, "%u", tegra_get_chip_id());
228 dev
= soc_device_register(attr
);
231 kfree(attr
->revision
);
234 return ERR_CAST(dev
);
237 return soc_device_to_device(dev
);
240 static int __init
tegra_init_fuse(void)
242 const struct of_device_id
*match
;
243 struct device_node
*np
;
244 struct resource regs
;
246 tegra_init_apbmisc();
248 np
= of_find_matching_node_and_match(NULL
, tegra_fuse_match
, &match
);
251 * Fall back to legacy initialization for 32-bit ARM only. All
252 * 64-bit ARM device tree files for Tegra are required to have
255 * This is for backwards-compatibility with old device trees
256 * that didn't contain a FUSE node.
258 if (IS_ENABLED(CONFIG_ARM
) && soc_is_tegra()) {
259 u8 chip
= tegra_get_chip_id();
261 regs
.start
= 0x7000f800;
262 regs
.end
= 0x7000fbff;
263 regs
.flags
= IORESOURCE_MEM
;
266 #ifdef CONFIG_ARCH_TEGRA_2x_SOC
268 fuse
->soc
= &tegra20_fuse_soc
;
272 #ifdef CONFIG_ARCH_TEGRA_3x_SOC
274 fuse
->soc
= &tegra30_fuse_soc
;
278 #ifdef CONFIG_ARCH_TEGRA_114_SOC
280 fuse
->soc
= &tegra114_fuse_soc
;
284 #ifdef CONFIG_ARCH_TEGRA_124_SOC
286 fuse
->soc
= &tegra124_fuse_soc
;
291 pr_warn("Unsupported SoC: %02x\n", chip
);
296 * At this point we're not running on Tegra, so play
297 * nice with multi-platform kernels.
303 * Extract information from the device tree if we've found a
306 if (of_address_to_resource(np
, 0, ®s
) < 0) {
307 pr_err("failed to get FUSE register\n");
311 fuse
->soc
= match
->data
;
314 np
= of_find_matching_node(NULL
, car_match
);
316 void __iomem
*base
= of_iomap(np
, 0);
318 tegra_enable_fuse_clk(base
);
321 pr_err("failed to map clock registers\n");
326 fuse
->base
= ioremap_nocache(regs
.start
, resource_size(®s
));
328 pr_err("failed to map FUSE registers\n");
332 fuse
->soc
->init(fuse
);
334 pr_info("Tegra Revision: %s SKU: %d CPU Process: %d SoC Process: %d\n",
335 tegra_revision_name
[tegra_sku_info
.revision
],
336 tegra_sku_info
.sku_id
, tegra_sku_info
.cpu_process_id
,
337 tegra_sku_info
.soc_process_id
);
338 pr_debug("Tegra CPU Speedo ID %d, SoC Speedo ID %d\n",
339 tegra_sku_info
.cpu_speedo_id
, tegra_sku_info
.soc_speedo_id
);
344 early_initcall(tegra_init_fuse
);
347 static int __init
tegra_init_soc(void)
349 struct device_node
*np
;
352 /* make sure we're running on Tegra */
353 np
= of_find_matching_node(NULL
, tegra_fuse_match
);
359 soc
= tegra_soc_device_register();
361 pr_err("failed to register SoC device: %ld\n", PTR_ERR(soc
));
367 device_initcall(tegra_init_soc
);