summaryrefslogtreecommitdiff
path: root/arch/arm/mach-tegra/fuse-cache.c
blob: 1e994fb48b4065bba03fe8b4e4ecc1699e50e43b (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
/*
 * arch/arm/mach-tegra/fuse-cache.c
 *
 * Interface to kfuses on Tegra 200
 *
 * Copyright (c) 2010, NVIDIA Corporation.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/string.h>
#include <linux/io.h>
#include <linux/mm.h>
#include <mach/fuse.h>
#include <mach/iomap.h>
#include <ap20/arclk_rst.h>
#include <ap20/arfuse.h>

/* register definition */
#define KFUSE_STATE 0x80
#define KFUSE_STATE_DONE 0x10000
#define KFUSE_STATE_CRCPASS 0x20000
#define KFUSE_KEYADDR 0x88
#define KFUSE_KEYADDR_AUTOINC 0x10000
#define KFUSE_KEYADDR_ADDR(addr) (addr)
#define KFUSE_KEYS 0x8c

#define KFUSE_CACHE_SZ (144 * 4)

static u32 *kfuse_cache;
static int kfuse_cache_isvalid;

/* set start address in auto-increment mode */
static inline void kfuse_set_autoinc_addr(u16 addr)
{
	writel(KFUSE_KEYADDR_ADDR(0) | KFUSE_KEYADDR_AUTOINC,
		IO_ADDRESS(TEGRA_KFUSE_BASE) + KFUSE_KEYADDR);
}

static inline void kfuse_clock_enable(int e)
{
	if (e) {
		writel(CLK_RST_CONTROLLER_CLK_ENB_H_SET_0_SET_CLK_ENB_KFUSE_FIELD,
			IO_ADDRESS(TEGRA_CLK_RESET_BASE +
			CLK_RST_CONTROLLER_CLK_ENB_H_SET_0));
	} else {
		writel(CLK_RST_CONTROLLER_CLK_ENB_H_CLR_0_CLR_CLK_ENB_KFUSE_FIELD,
			IO_ADDRESS(TEGRA_CLK_RESET_BASE +
			CLK_RST_CONTROLLER_CLK_ENB_H_CLR_0));
	}
}

static void kfuse_wait_ready(void)
{
	int retries = 1;
	/* wait for hardware to finish loading and verifying key data */
	do {
		u32 val;
		val = readl(IO_ADDRESS(TEGRA_KFUSE_BASE) + KFUSE_STATE);
		if ((val & KFUSE_STATE_DONE) == KFUSE_STATE_DONE) {
			/* hardware does CRC check */
			kfuse_cache_isvalid = (val & KFUSE_STATE_CRCPASS) == KFUSE_STATE_CRCPASS;
			break;
		}
		msleep(10);
	} while( --retries >= 0 );
}

static void kfuse_rewind(void)
{
	/* force HW to decode and check fuses if it has not already done so */
	kfuse_set_autoinc_addr(0);
	kfuse_wait_ready();
	// kfuse_set_autoinc_addr(0);
}

static inline u32 kfuse_read(void)
{
	return readl(IO_ADDRESS(TEGRA_KFUSE_BASE) + KFUSE_KEYS);
}

/* this is called very early in init because there is a bug that can cause
 * corruption if DMA and non-DMA requests overlap on APB bus. */
void __init tegra_init_fuse_cache(void) {
	unsigned i;

	kfuse_cache = kzalloc(KFUSE_CACHE_SZ, GFP_KERNEL);

	kfuse_clock_enable(1);

	kfuse_rewind();

	printk(KERN_DEBUG "kfuse_cache_isvalid=%d\n", kfuse_cache_isvalid);

	if (!kfuse_cache_isvalid) {
		printk(KERN_ERR "kfuse CRC or ECC error\n");
	} else {
		/* load if no CRC or ECC errors */
		for (i = 0; i < KFUSE_CACHE_SZ / sizeof (u32); i ++)
			kfuse_cache[i] = kfuse_read();
	}

	kfuse_clock_enable(0);
}

const u32 *tegra_kfuse_cache_get(size_t *size) {
	if (size) *size = KFUSE_CACHE_SZ;
	return kfuse_cache;
}