summaryrefslogtreecommitdiff
path: root/arch/arm/mach-tegra/nct.c
blob: 5b988f34dc5d155be06ab564b048ad34041dc87d (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
/*
 * arch/arm/mach-tegra/nct.c
 *
 * Copyright (c) 2013, NVIDIA CORPORATION.  All rights reserved.
 *
 * 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/errno.h>
#include <linux/init.h>
#include <linux/io.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>
#include <linux/fcntl.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/crc32.h>
#include <linux/sysfs.h>

#include <mach/nct.h>
#include "board.h"


#define USE_CRC32_IN_NCT	1

static bool tegra_nct_initialized;
static struct nct_part_head_type nct_head;
static void __iomem *nct_ptr;


int tegra_nct_read_item(u32 index, union nct_item_type *buf)
{
	struct nct_entry_type *entry = NULL;
	u8 *nct;
#if USE_CRC32_IN_NCT
	u32 crc = 0;
#endif

	if (!tegra_nct_initialized)
		return -EPERM;

	entry = kmalloc(sizeof(struct nct_entry_type), GFP_KERNEL);
	if (!entry) {
		pr_err("%s: failed to allocate buffer\n", __func__);
		return -ENOMEM;
	}

	nct = (u8 *)(nct_ptr + NCT_ENTRY_OFFSET +
			(index * sizeof(*entry)));
	memcpy((u8 *)entry, nct, sizeof(*entry));

	/* check CRC integrity */
#if USE_CRC32_IN_NCT
	/* last checksum field of entry is not included in CRC calculation */
	crc = crc32_le(~0, (u8 *)entry, sizeof(*entry) -
			sizeof(entry->checksum)) ^ ~0;
	if (crc != entry->checksum) {
		pr_err("%s: checksum err(0x%x/0x%x)\n", __func__,
			crc, entry->checksum);
		kfree(entry);
		return -EINVAL;
	}
#endif
	/* check index integrity */
	if (index != entry->index) {
		pr_err("%s: index err(0x%x/0x%x)\n", __func__,
			index, entry->index);
		kfree(entry);
		return -EINVAL;
	}

	memcpy(buf, &entry->data, sizeof(*buf));
	kfree(entry);

	return 0;
}

int tegra_nct_is_init(void)
{
	return tegra_nct_initialized;
}

static int __init tegra_nct_init(void)
{
	if (tegra_nct_initialized)
		return 0;

	if ((tegra_nck_start == 0) || (tegra_nck_size == 0)) {
		pr_err("tegra_nct: not configured\n");
		return -ENOTSUPP;
	}

	nct_ptr = ioremap_wc(tegra_nck_start,
				tegra_nck_size);
	if (!nct_ptr) {
		pr_err("tegra_nct: failed to ioremap memory at 0x%08lx\n",
			tegra_nck_start);
		return -EIO;
	}

	memcpy(&nct_head, nct_ptr, sizeof(nct_head));
	wmb();

	pr_info("%s: magic(0x%x),vid(0x%x),pid(0x%x),ver(V%x.%x),rev(%d)\n",
		__func__,
		nct_head.magicId,
		nct_head.vendorId,
		nct_head.productId,
		(nct_head.version >> 16) & 0xFFFF,
		(nct_head.version & 0xFFFF),
		nct_head.revision);

	if (nct_head.magicId != NCT_MAGIC_ID) {
		pr_err("%s: magic ID error (0x%x/0x%x)\n", __func__,
			nct_head.magicId, NCT_MAGIC_ID);
		iounmap(nct_ptr);
		return -ENOKEY;
	}

	tegra_nct_initialized = true;

	return 0;
}

early_initcall(tegra_nct_init);