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/>.
16 * Based on drivers/misc/eeprom/sunxi_sid.c
19 #include <linux/device.h>
20 #include <linux/clk.h>
21 #include <linux/completion.h>
22 #include <linux/dmaengine.h>
23 #include <linux/dma-mapping.h>
24 #include <linux/err.h>
26 #include <linux/kernel.h>
27 #include <linux/kobject.h>
28 #include <linux/of_device.h>
29 #include <linux/platform_device.h>
30 #include <linux/random.h>
32 #include <soc/tegra/fuse.h>
36 #define FUSE_BEGIN 0x100
37 #define FUSE_SIZE 0x1f8
38 #define FUSE_UID_LOW 0x08
39 #define FUSE_UID_HIGH 0x0c
41 static phys_addr_t fuse_phys;
42 static struct clk *fuse_clk;
43 static void __iomem __initdata *fuse_base;
45 static DEFINE_MUTEX(apb_dma_lock);
46 static DECLARE_COMPLETION(apb_dma_wait);
47 static struct dma_chan *apb_dma_chan;
48 static struct dma_slave_config dma_sconfig;
49 static u32 *apb_buffer;
50 static dma_addr_t apb_buffer_phys;
52 static void apb_dma_complete(void *args)
54 complete(&apb_dma_wait);
57 static u32 tegra20_fuse_readl(const unsigned int offset)
61 struct dma_async_tx_descriptor *dma_desc;
63 mutex_lock(&apb_dma_lock);
65 dma_sconfig.src_addr = fuse_phys + FUSE_BEGIN + offset;
66 ret = dmaengine_slave_config(apb_dma_chan, &dma_sconfig);
70 dma_desc = dmaengine_prep_slave_single(apb_dma_chan, apb_buffer_phys,
71 sizeof(u32), DMA_DEV_TO_MEM,
72 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
76 dma_desc->callback = apb_dma_complete;
77 dma_desc->callback_param = NULL;
79 reinit_completion(&apb_dma_wait);
81 clk_prepare_enable(fuse_clk);
83 dmaengine_submit(dma_desc);
84 dma_async_issue_pending(apb_dma_chan);
85 ret = wait_for_completion_timeout(&apb_dma_wait, msecs_to_jiffies(50));
87 if (WARN(ret == 0, "apb read dma timed out"))
88 dmaengine_terminate_all(apb_dma_chan);
92 clk_disable_unprepare(fuse_clk);
94 mutex_unlock(&apb_dma_lock);
99 static const struct of_device_id tegra20_fuse_of_match[] = {
100 { .compatible = "nvidia,tegra20-efuse" },
104 static int apb_dma_init(void)
109 dma_cap_set(DMA_SLAVE, mask);
110 apb_dma_chan = dma_request_channel(mask, NULL, NULL);
112 return -EPROBE_DEFER;
114 apb_buffer = dma_alloc_coherent(NULL, sizeof(u32), &apb_buffer_phys,
117 dma_release_channel(apb_dma_chan);
121 dma_sconfig.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
122 dma_sconfig.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
123 dma_sconfig.src_maxburst = 1;
124 dma_sconfig.dst_maxburst = 1;
129 static int tegra20_fuse_probe(struct platform_device *pdev)
131 struct resource *res;
134 fuse_clk = devm_clk_get(&pdev->dev, NULL);
135 if (IS_ERR(fuse_clk)) {
136 dev_err(&pdev->dev, "missing clock");
137 return PTR_ERR(fuse_clk);
140 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
143 fuse_phys = res->start;
145 err = apb_dma_init();
149 if (tegra_fuse_create_sysfs(&pdev->dev, FUSE_SIZE, tegra20_fuse_readl))
152 dev_dbg(&pdev->dev, "loaded\n");
157 static struct platform_driver tegra20_fuse_driver = {
158 .probe = tegra20_fuse_probe,
160 .name = "tegra20_fuse",
161 .of_match_table = tegra20_fuse_of_match,
165 static int __init tegra20_fuse_init(void)
167 return platform_driver_register(&tegra20_fuse_driver);
169 postcore_initcall(tegra20_fuse_init);
171 /* Early boot code. This code is called before the devices are created */
173 u32 __init tegra20_fuse_early(const unsigned int offset)
175 return readl_relaxed(fuse_base + FUSE_BEGIN + offset);
178 bool __init tegra20_spare_fuse_early(int spare_bit)
180 u32 offset = spare_bit * 4;
183 value = tegra20_fuse_early(offset + 0x100);
188 static void __init tegra20_fuse_add_randomness(void)
192 randomness[0] = tegra_sku_info.sku_id;
193 randomness[1] = tegra_read_straps();
194 randomness[2] = tegra_read_chipid();
195 randomness[3] = tegra_sku_info.cpu_process_id << 16;
196 randomness[3] |= tegra_sku_info.core_process_id;
197 randomness[4] = tegra_sku_info.cpu_speedo_id << 16;
198 randomness[4] |= tegra_sku_info.soc_speedo_id;
199 randomness[5] = tegra20_fuse_early(FUSE_UID_LOW);
200 randomness[6] = tegra20_fuse_early(FUSE_UID_HIGH);
202 add_device_randomness(randomness, sizeof(randomness));
205 void __init tegra20_init_fuse_early(void)
207 fuse_base = ioremap(TEGRA_FUSE_BASE, TEGRA_FUSE_SIZE);
209 tegra_init_revision();
210 tegra20_init_speedo_data(&tegra_sku_info);
211 tegra20_fuse_add_randomness();