2 * Copyright (c) 2013-2014, NVIDIA CORPORATION. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include <linux/clk.h>
19 #include <linux/device.h>
20 #include <linux/kobject.h>
21 #include <linux/init.h>
24 #include <linux/of_address.h>
25 #include <linux/platform_device.h>
26 #include <linux/slab.h>
27 #include <linux/sys_soc.h>
29 #include <soc/tegra/common.h>
30 #include <soc/tegra/fuse.h>
34 struct tegra_sku_info tegra_sku_info;
35 EXPORT_SYMBOL(tegra_sku_info);
37 static const char *tegra_revision_name[TEGRA_REVISION_MAX] = {
38 [TEGRA_REVISION_UNKNOWN] = "unknown",
39 [TEGRA_REVISION_A01] = "A01",
40 [TEGRA_REVISION_A02] = "A02",
41 [TEGRA_REVISION_A03] = "A03",
42 [TEGRA_REVISION_A03p] = "A03 prime",
43 [TEGRA_REVISION_A04] = "A04",
46 static u8 fuse_readb(struct tegra_fuse *fuse, unsigned int offset)
50 val = fuse->read(fuse, round_down(offset, 4));
51 val >>= (offset % 4) * 8;
57 static ssize_t fuse_read(struct file *fd, struct kobject *kobj,
58 struct bin_attribute *attr, char *buf,
59 loff_t pos, size_t size)
61 struct device *dev = kobj_to_dev(kobj);
62 struct tegra_fuse *fuse = dev_get_drvdata(dev);
65 if (pos < 0 || pos >= attr->size)
68 if (size > attr->size - pos)
69 size = attr->size - pos;
71 for (i = 0; i < size; i++)
72 buf[i] = fuse_readb(fuse, pos + i);
77 static struct bin_attribute fuse_bin_attr = {
78 .attr = { .name = "fuse", .mode = S_IRUGO, },
82 static int tegra_fuse_create_sysfs(struct device *dev, unsigned int size,
83 const struct tegra_fuse_info *info)
85 fuse_bin_attr.size = size;
87 return device_create_bin_file(dev, &fuse_bin_attr);
90 static const struct of_device_id car_match[] __initconst = {
91 { .compatible = "nvidia,tegra20-car", },
92 { .compatible = "nvidia,tegra30-car", },
93 { .compatible = "nvidia,tegra114-car", },
94 { .compatible = "nvidia,tegra124-car", },
95 { .compatible = "nvidia,tegra132-car", },
96 { .compatible = "nvidia,tegra210-car", },
100 static struct tegra_fuse *fuse = &(struct tegra_fuse) {
105 static const struct of_device_id tegra_fuse_match[] = {
106 #ifdef CONFIG_ARCH_TEGRA_210_SOC
107 { .compatible = "nvidia,tegra210-efuse", .data = &tegra210_fuse_soc },
109 #ifdef CONFIG_ARCH_TEGRA_132_SOC
110 { .compatible = "nvidia,tegra132-efuse", .data = &tegra124_fuse_soc },
112 #ifdef CONFIG_ARCH_TEGRA_124_SOC
113 { .compatible = "nvidia,tegra124-efuse", .data = &tegra124_fuse_soc },
115 #ifdef CONFIG_ARCH_TEGRA_114_SOC
116 { .compatible = "nvidia,tegra114-efuse", .data = &tegra114_fuse_soc },
118 #ifdef CONFIG_ARCH_TEGRA_3x_SOC
119 { .compatible = "nvidia,tegra30-efuse", .data = &tegra30_fuse_soc },
121 #ifdef CONFIG_ARCH_TEGRA_2x_SOC
122 { .compatible = "nvidia,tegra20-efuse", .data = &tegra20_fuse_soc },
127 static int tegra_fuse_probe(struct platform_device *pdev)
129 void __iomem *base = fuse->base;
130 struct resource *res;
133 /* take over the memory region from the early initialization */
134 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
135 fuse->base = devm_ioremap_resource(&pdev->dev, res);
136 if (IS_ERR(fuse->base)) {
137 err = PTR_ERR(fuse->base);
142 fuse->clk = devm_clk_get(&pdev->dev, "fuse");
143 if (IS_ERR(fuse->clk)) {
144 dev_err(&pdev->dev, "failed to get FUSE clock: %ld",
147 return PTR_ERR(fuse->clk);
150 platform_set_drvdata(pdev, fuse);
151 fuse->dev = &pdev->dev;
153 if (fuse->soc->probe) {
154 err = fuse->soc->probe(fuse);
161 if (tegra_fuse_create_sysfs(&pdev->dev, fuse->soc->info->size,
165 /* release the early I/O memory mapping */
171 static struct platform_driver tegra_fuse_driver = {
173 .name = "tegra-fuse",
174 .of_match_table = tegra_fuse_match,
175 .suppress_bind_attrs = true,
177 .probe = tegra_fuse_probe,
179 builtin_platform_driver(tegra_fuse_driver);
181 u32 __init tegra_fuse_read_spare(unsigned int spare)
183 unsigned int offset = fuse->soc->info->spare + spare * 4;
185 return fuse->read_early(fuse, offset) & 1;
188 u32 __init tegra_fuse_read_early(unsigned int offset)
190 return fuse->read_early(fuse, offset);
193 int tegra_fuse_readl(unsigned long offset, u32 *value)
196 return -EPROBE_DEFER;
198 *value = fuse->read(fuse, offset);
202 EXPORT_SYMBOL(tegra_fuse_readl);
204 static void tegra_enable_fuse_clk(void __iomem *base)
208 reg = readl_relaxed(base + 0x48);
210 writel(reg, base + 0x48);
213 * Enable FUSE clock. This needs to be hardcoded because the clock
214 * subsystem is not active during early boot.
216 reg = readl(base + 0x14);
218 writel(reg, base + 0x14);
221 struct device * __init tegra_soc_device_register(void)
223 struct soc_device_attribute *attr;
224 struct soc_device *dev;
226 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
230 attr->family = kasprintf(GFP_KERNEL, "Tegra");
231 attr->revision = kasprintf(GFP_KERNEL, "%d", tegra_sku_info.revision);
232 attr->soc_id = kasprintf(GFP_KERNEL, "%u", tegra_get_chip_id());
234 dev = soc_device_register(attr);
237 kfree(attr->revision);
240 return ERR_CAST(dev);
243 return soc_device_to_device(dev);
246 static int __init tegra_init_fuse(void)
248 const struct of_device_id *match;
249 struct device_node *np;
250 struct resource regs;
252 tegra_init_apbmisc();
254 np = of_find_matching_node_and_match(NULL, tegra_fuse_match, &match);
257 * Fall back to legacy initialization for 32-bit ARM only. All
258 * 64-bit ARM device tree files for Tegra are required to have
261 * This is for backwards-compatibility with old device trees
262 * that didn't contain a FUSE node.
264 if (IS_ENABLED(CONFIG_ARM) && soc_is_tegra()) {
265 u8 chip = tegra_get_chip_id();
267 regs.start = 0x7000f800;
268 regs.end = 0x7000fbff;
269 regs.flags = IORESOURCE_MEM;
272 #ifdef CONFIG_ARCH_TEGRA_2x_SOC
274 fuse->soc = &tegra20_fuse_soc;
278 #ifdef CONFIG_ARCH_TEGRA_3x_SOC
280 fuse->soc = &tegra30_fuse_soc;
284 #ifdef CONFIG_ARCH_TEGRA_114_SOC
286 fuse->soc = &tegra114_fuse_soc;
290 #ifdef CONFIG_ARCH_TEGRA_124_SOC
292 fuse->soc = &tegra124_fuse_soc;
297 pr_warn("Unsupported SoC: %02x\n", chip);
302 * At this point we're not running on Tegra, so play
303 * nice with multi-platform kernels.
309 * Extract information from the device tree if we've found a
312 if (of_address_to_resource(np, 0, ®s) < 0) {
313 pr_err("failed to get FUSE register\n");
317 fuse->soc = match->data;
320 np = of_find_matching_node(NULL, car_match);
322 void __iomem *base = of_iomap(np, 0);
324 tegra_enable_fuse_clk(base);
327 pr_err("failed to map clock registers\n");
332 fuse->base = ioremap_nocache(regs.start, resource_size(®s));
334 pr_err("failed to map FUSE registers\n");
338 fuse->soc->init(fuse);
340 pr_info("Tegra Revision: %s SKU: %d CPU Process: %d SoC Process: %d\n",
341 tegra_revision_name[tegra_sku_info.revision],
342 tegra_sku_info.sku_id, tegra_sku_info.cpu_process_id,
343 tegra_sku_info.soc_process_id);
344 pr_debug("Tegra CPU Speedo ID %d, SoC Speedo ID %d\n",
345 tegra_sku_info.cpu_speedo_id, tegra_sku_info.soc_speedo_id);
350 early_initcall(tegra_init_fuse);
353 static int __init tegra_init_soc(void)
355 struct device_node *np;
358 /* make sure we're running on Tegra */
359 np = of_find_matching_node(NULL, tegra_fuse_match);
365 soc = tegra_soc_device_register();
367 pr_err("failed to register SoC device: %ld\n", PTR_ERR(soc));
373 device_initcall(tegra_init_soc);