ARM: tegra: loki: fix build error due to warning
[linux-3.10.git] / arch / arm / mach-tegra / nct.c
1 /*
2  * arch/arm/mach-tegra/nct.c
3  *
4  * Copyright (c) 2013, NVIDIA CORPORATION.  All rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19  */
20
21 #include <linux/kernel.h>
22 #include <linux/errno.h>
23 #include <linux/init.h>
24 #include <linux/io.h>
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/slab.h>
28 #include <linux/vmalloc.h>
29 #include <linux/fcntl.h>
30 #include <linux/fs.h>
31 #include <linux/uaccess.h>
32 #include <linux/crc32.h>
33 #include <linux/sysfs.h>
34
35 #include <mach/nct.h>
36 #include "board.h"
37
38
39 #define USE_CRC32_IN_NCT        1
40
41 static bool tegra_nct_initialized;
42 static struct nct_part_head_type nct_head;
43 static void __iomem *nct_ptr;
44
45
46 int tegra_nct_read_item(u32 index, union nct_item_type *buf)
47 {
48         struct nct_entry_type *entry = NULL;
49         u8 *nct;
50 #if USE_CRC32_IN_NCT
51         u32 crc = 0;
52 #endif
53
54         if (!tegra_nct_initialized)
55                 return -EPERM;
56
57         entry = kmalloc(sizeof(struct nct_entry_type), GFP_KERNEL);
58         if (!entry) {
59                 pr_err("%s: failed to allocate buffer\n", __func__);
60                 return -ENOMEM;
61         }
62
63         nct = (u8 *)(nct_ptr + NCT_ENTRY_OFFSET +
64                         (index * sizeof(*entry)));
65         memcpy((u8 *)entry, nct, sizeof(*entry));
66
67         /* check CRC integrity */
68 #if USE_CRC32_IN_NCT
69         /* last checksum field of entry is not included in CRC calculation */
70         crc = crc32_le(~0, (u8 *)entry, sizeof(*entry) -
71                         sizeof(entry->checksum)) ^ ~0;
72         if (crc != entry->checksum) {
73                 pr_err("%s: checksum err(0x%x/0x%x)\n", __func__,
74                         crc, entry->checksum);
75                 kfree(entry);
76                 return -EINVAL;
77         }
78 #endif
79         /* check index integrity */
80         if (index != entry->index) {
81                 pr_err("%s: index err(0x%x/0x%x)\n", __func__,
82                         index, entry->index);
83                 kfree(entry);
84                 return -EINVAL;
85         }
86
87         memcpy(buf, &entry->data, sizeof(*buf));
88         kfree(entry);
89
90         return 0;
91 }
92
93 int tegra_nct_is_init(void)
94 {
95         return tegra_nct_initialized;
96 }
97
98 static int __init tegra_nct_init(void)
99 {
100         if (tegra_nct_initialized)
101                 return 0;
102
103         if ((tegra_nck_start == 0) || (tegra_nck_size == 0)) {
104                 pr_err("tegra_nct: not configured\n");
105                 return -ENOTSUPP;
106         }
107
108         nct_ptr = ioremap_nocache(tegra_nck_start,
109                                 tegra_nck_size);
110         if (!nct_ptr) {
111                 pr_err("tegra_nct: failed to ioremap memory at 0x%08lx\n",
112                         tegra_nck_start);
113                 return -EIO;
114         }
115
116         memcpy(&nct_head, nct_ptr, sizeof(nct_head));
117
118         pr_info("%s: magic(0x%x),vid(0x%x),pid(0x%x),ver(V%x.%x),rev(%d)\n",
119                 __func__,
120                 nct_head.magicId,
121                 nct_head.vendorId,
122                 nct_head.productId,
123                 (nct_head.version >> 16) & 0xFFFF,
124                 (nct_head.version & 0xFFFF),
125                 nct_head.revision);
126
127         if (nct_head.magicId != NCT_MAGIC_ID) {
128                 pr_err("%s: magic ID error (0x%x/0x%x)\n", __func__,
129                         nct_head.magicId, NCT_MAGIC_ID);
130                 BUG();
131         }
132
133         tegra_nct_initialized = true;
134
135         return 0;
136 }
137
138 early_initcall(tegra_nct_init);