summaryrefslogtreecommitdiff
path: root/arch/arm/mach-tegra/fuse.c
blob: c016127cbaeae433728a2707278b1eff2678ecbd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
 * arch/arm/mach-tegra/fuse.c
 *
 * Copyright (C) 2010 Google, Inc.
 *
 * Author:
 *	Colin Cross <ccross@android.com>
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */

#include <linux/kernel.h>
#include <linux/io.h>
#include <linux/dma-mapping.h>
#include <linux/spinlock.h>
#include <linux/completion.h>
#include <linux/sched.h>
#include <linux/mutex.h>

#include <mach/dma.h>
#include <mach/iomap.h>

#include "fuse.h"

#define FUSE_UID_LOW		0x108
#define FUSE_UID_HIGH		0x10c
#define FUSE_SKU_INFO		0x110
#define FUSE_SPARE_BIT		0x200

static DEFINE_MUTEX(tegra_fuse_dma_lock);

#ifdef CONFIG_TEGRA_SYSTEM_DMA
static struct tegra_dma_channel *tegra_fuse_dma;
static u32 *tegra_fuse_bb;
static dma_addr_t tegra_fuse_bb_phys;
static DECLARE_COMPLETION(tegra_fuse_wait);

static void fuse_dma_complete(struct tegra_dma_req *req)
{
	complete(&tegra_fuse_wait);
}

static inline u32 fuse_readl(unsigned long offset)
{
	struct tegra_dma_req req;
	int ret;

	if (!tegra_fuse_dma)
		return readl(IO_TO_VIRT(TEGRA_FUSE_BASE + offset));

	mutex_lock(&tegra_fuse_dma_lock);
	req.complete = fuse_dma_complete;
	req.to_memory = 1;
	req.dest_addr = tegra_fuse_bb_phys;
	req.dest_bus_width = 32;
	req.dest_wrap = 1;
	req.source_addr = TEGRA_FUSE_BASE + offset;
	req.source_bus_width = 32;
	req.source_wrap = 4;
	req.req_sel = 0;
	req.size = 4;

	INIT_COMPLETION(tegra_fuse_wait);

	tegra_dma_enqueue_req(tegra_fuse_dma, &req);

	ret = wait_for_completion_timeout(&tegra_fuse_wait,
		msecs_to_jiffies(50));

	if (WARN(ret == 0, "fuse read dma timed out"))
		*(u32 *)tegra_fuse_bb = 0;

	mutex_unlock(&tegra_fuse_dma_lock);
	return *((u32 *)tegra_fuse_bb);
}

static inline void fuse_writel(u32 value, unsigned long offset)
{
	struct tegra_dma_req req;
	int ret;

	if (!tegra_fuse_dma) {
		writel(value, IO_TO_VIRT(TEGRA_FUSE_BASE + offset));
		return;
	}

	mutex_lock(&tegra_fuse_dma_lock);
	*((u32 *)tegra_fuse_bb) = value;
	req.complete = fuse_dma_complete;
	req.to_memory = 0;
	req.dest_addr = TEGRA_FUSE_BASE + offset;
	req.dest_wrap = 4;
	req.dest_bus_width = 32;
	req.source_addr = tegra_fuse_bb_phys;
	req.source_bus_width = 32;
	req.source_wrap = 1;
	req.req_sel = 0;
	req.size = 4;

	INIT_COMPLETION(tegra_fuse_wait);

	tegra_dma_enqueue_req(tegra_fuse_dma, &req);

	ret = wait_for_completion_timeout(&tegra_fuse_wait,
		msecs_to_jiffies(50));

	mutex_unlock(&tegra_fuse_dma_lock);
}
#else
static inline u32 fuse_readl(unsigned long offset)
{
	return readl(IO_TO_VIRT(TEGRA_FUSE_BASE + offset));
}

static inline void fuse_writel(u32 value, unsigned long offset)
{
	writel(value, IO_TO_VIRT(TEGRA_FUSE_BASE + offset));
}
#endif

u32 tegra_fuse_readl(unsigned long offset)
{
	return fuse_readl(offset);
}

void tegra_fuse_writel(u32 value, unsigned long offset)
{
	fuse_writel(value, offset);
}

void tegra_init_fuse(void)
{
	u32 reg = readl(IO_TO_VIRT(TEGRA_CLK_RESET_BASE + 0x48));
	reg |= 1 << 28;
	writel(reg, IO_TO_VIRT(TEGRA_CLK_RESET_BASE + 0x48));
	tegra_init_speedo_data();
}

void tegra_init_fuse_dma(void)
{
#ifdef CONFIG_TEGRA_SYSTEM_DMA
	tegra_fuse_dma = tegra_dma_allocate_channel(TEGRA_DMA_MODE_ONESHOT |
		TEGRA_DMA_SHARED);
	if (!tegra_fuse_dma) {
		pr_err("%s: can not allocate dma channel\n", __func__);
		return;
	}

	tegra_fuse_bb = dma_alloc_coherent(NULL, sizeof(u32),
		&tegra_fuse_bb_phys, GFP_KERNEL);
	if (!tegra_fuse_bb) {
		pr_err("%s: can not allocate bounce buffer\n", __func__);
		tegra_dma_free_channel(tegra_fuse_dma);
		tegra_fuse_dma = NULL;
		return;
	}
#endif
}

unsigned long long tegra_chip_uid(void)
{
	unsigned long long lo, hi;

	lo = fuse_readl(FUSE_UID_LOW);
	hi = fuse_readl(FUSE_UID_HIGH);
	return (hi << 32ull) | lo;
}

unsigned int tegra_spare_fuse(int bit)
{
	BUG_ON(bit < 0 || bit > 61);
	return fuse_readl(FUSE_SPARE_BIT + bit * 4);
}

int tegra_sku_id(void)
{
	int sku_id;
	u32 reg = fuse_readl(FUSE_SKU_INFO);
	sku_id = reg & 0xFF;
	return sku_id;
}