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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
/*
* Copyright (C) 2014 Spreadtrum Communications Inc.
*
* 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.
*
*/
/* IMPORTANT:
*
* TODO:
*/
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/stat.h>
#include <linux/err.h>
#include <linux/delay.h>
#include <linux/of.h>
#include <linux/of_device.h>
/* FIXME: */
#include <soc/sprd/arch_lock.h>
#include <soc/sprd/adi.h>
#include <soc/sprd/sci_glb_regs.h>
#include <soc/sprd/sci.h>
#include "sprd_otp.h"
#include "__regs_ana_efuse.h"
#define ANA_REGS_EFUSE_BASE ( SPRD_ADISLAVE_BASE + 0x200 )
#define EFUSE_BLOCK_MAX ( 32 )
#define EFUSE_BLOCK_WIDTH ( 8 ) /* bit counts */
static DEFINE_MUTEX(adie_efuse_mtx);
void adie_efuse_workaround(void)
{
#define REG_ADI_GSSI_CFG0 (SPRD_ADI_PHYS + 0x1C)
/** BIT_RF_GSSI_SCK_ALL_IN
* 0: sclk auto gate
* 1: sclk always on
*/
#define BIT_RF_GSSI_SCK_ALL_IN BIT(30)
sci_glb_write(REG_ADI_GSSI_CFG0,
BIT_RF_GSSI_SCK_ALL_IN, BIT_RF_GSSI_SCK_ALL_IN);
}
static void adie_efuse_lock(void)
{
mutex_lock(&adie_efuse_mtx);
}
static void adie_efuse_unlock(void)
{
mutex_unlock(&adie_efuse_mtx);
}
static void adie_efuse_reset(void)
{
}
static void __adie_efuse_power_on(void)
{
sci_adi_set(ANA_REG_GLB_ARM_MODULE_EN, BIT_ANA_EFS_EN);
/* FIXME: rtc_efs only for prog
sci_adi_set(ANA_REG_GLB_RTC_CLK_EN, BIT_RTC_EFS_EN);
*/
/* FIXME: sclk always on or not ? */
/* adie_efuse_workaround(); */
}
static void __adie_efuse_power_off(void)
{
/* FIXME: rtc_efs only for prog
sci_adi_clr(ANA_REG_GLB_RTC_CLK_EN, BIT_RTC_EFS_EN);
*/
sci_adi_clr(ANA_REG_GLB_ARM_MODULE_EN, BIT_ANA_EFS_EN);
}
static __inline int __adie_efuse_wait_clear(u32 bits)
{
int ret = 0;
unsigned long timeout;
pr_debug("wait %x\n", sci_adi_read(ANA_REG_EFUSE_STATUS));
/* wait for maximum of 3000 msec */
timeout = jiffies + msecs_to_jiffies(3000);
while (sci_adi_read(ANA_REG_EFUSE_STATUS) & bits) {
if (time_after(jiffies, timeout)) {
WARN_ON(1);
ret = -ETIMEDOUT;
break;
}
cpu_relax();
}
return ret;
}
static u32 adie_efuse_read(int blk_index)
{
u32 val = 0;
pr_debug("adie efuse read %d\n", blk_index);
adie_efuse_lock();
__adie_efuse_power_on();
/* enable adie_efuse module clk and power before */
/* FIXME: set read timing, why 0x20 (default value)
sci_adi_raw_write(ANA_REG_EFUSE_RD_TIMING_CTRL,
BITS_EFUSE_RD_TIMING(0x20));
*/
sci_adi_raw_write(ANA_REG_EFUSE_BLOCK_INDEX,
BITS_READ_WRITE_INDEX(blk_index));
sci_adi_raw_write(ANA_REG_EFUSE_MODE_CTRL, BIT_RD_START);
if (IS_ERR_VALUE(__adie_efuse_wait_clear(BIT_READ_BUSY)))
goto out;
val = sci_adi_read(ANA_REG_EFUSE_DATA_RD);
/* FIXME: reverse the otp value */
val = BITS_EFUSE_DATA_RD(~val);
out:
__adie_efuse_power_off();
adie_efuse_unlock();
return val;
}
static struct sprd_otp_operations adie_efuse_ops = {
.reset = adie_efuse_reset,
.read = adie_efuse_read,
};
u32 __adie_efuse_read(int blk_index)
{
return adie_efuse_ops.read(blk_index);
}
EXPORT_SYMBOL_GPL(__adie_efuse_read);
u32 __adie_efuse_read_bits(int bit_index, int length)
{
int i, blk_index = (int)bit_index / EFUSE_BLOCK_WIDTH;
int blk_max = DIV_ROUND_UP(bit_index + length, EFUSE_BLOCK_WIDTH);
u32 val = 0;
pr_debug("otp read blk %d - %d\n", blk_index, blk_max);
/* FIXME: length should not bigger than 8 */
for (i = blk_index; i < blk_max; i++) {
val |= __adie_efuse_read(i)
<< ((i - blk_index) * EFUSE_BLOCK_WIDTH);
}
//pr_debug("val=%08x\n", val);
val >>= (bit_index & (EFUSE_BLOCK_WIDTH - 1));
val &= BIT(length) - 1;
pr_debug("otp read bits %d ++ %d 0x%08x\n\n", bit_index, length, val);
return val;
}
EXPORT_SYMBOL_GPL(__adie_efuse_read_bits);
static ssize_t adie_efuse_block_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
return sprintf(buf, "adie efuse blocks: %uX%ubits\n",
EFUSE_BLOCK_MAX, EFUSE_BLOCK_WIDTH);
}
static ssize_t adie_efuse_block_dump(struct device *dev,
struct device_attribute *attr, char *buf)
{
int i, idx;
char *p = buf;
p += sprintf(p, "adie efuse blocks dump:\n");
p += sprintf(p, " 7 6 5 4 3 2 1 0");
for (idx = 0; idx < EFUSE_BLOCK_MAX; idx++) {
u32 val = __adie_efuse_read(idx);
p += sprintf(p, "\n%02d ", idx);
for (i = EFUSE_BLOCK_WIDTH - 1; i >= 0; --i)
p += sprintf(p, "%s ", (val & BIT(i)) ? "1" : "0");
}
p += sprintf(p, "\n");
return p - buf;
}
static DEVICE_ATTR(block, S_IRUGO, adie_efuse_block_show, NULL);
static DEVICE_ATTR(dump, S_IRUGO, adie_efuse_block_dump, NULL);
int __init sprd_adie_efuse_init(void)
{
int ret;
void *dev;
dev =
sprd_otp_register("sprd_adie_efuse_otp", &adie_efuse_ops,
EFUSE_BLOCK_MAX, EFUSE_BLOCK_WIDTH / 8);
if (IS_ERR_OR_NULL(dev))
return PTR_ERR(dev);
ret = device_create_file(dev, &dev_attr_block);
ret |= device_create_file(dev, &dev_attr_dump);
if (ret)
return ret;
return 0;
}
|