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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
|
/* SPDX-License-Identifier: GPL-2.0-only */
/*
* This file is created based on MT8186 Functional Specification
* Chapter number: 5.13
*/
#include <delay.h>
#include <soc/rtc.h>
#include <soc/rtc_common.h>
#include <soc/mt6366.h>
#include <soc/pmic_wrap.h>
#include <timer.h>
#define MT8186_RTC_DXCO_CAPID 0xE0
/* Initialize RTC setting of using DCXO clock */
static bool rtc_enable_dcxo(void)
{
u16 bbpu, con, osc32con, sec;
rtc_read(RTC_BBPU, &bbpu);
rtc_write(RTC_BBPU, bbpu | RTC_BBPU_KEY | RTC_BBPU_RELOAD);
if (!rtc_write_trigger()) {
rtc_info("rtc_write_trigger() failed\n");
return false;
}
mdelay(1);
if (!rtc_writeif_unlock()) {
rtc_info("rtc_writeif_unlock() failed\n");
return false;
}
rtc_read(RTC_OSC32CON, &osc32con);
osc32con &= ~(RTC_EMBCK_SRC_SEL | RTC_EMBCK_SEL_MODE_MASK
| RTC_GPS_CKOUT_EN);
osc32con |= RTC_XOSC32_ENB | RTC_REG_XOSC32_ENB
| RTC_EMB_K_EOSC32_MODE | RTC_EMBCK_SEL_OPTION;
if (!rtc_xosc_write(osc32con)) {
rtc_info("rtc_xosc_write() failed\n");
return false;
}
rtc_read(RTC_CON, &con);
rtc_read(RTC_OSC32CON, &osc32con);
rtc_read(RTC_AL_SEC, &sec);
rtc_info("con = %#x, osc32con = %#x, sec = %#x\n", con, osc32con, sec);
return true;
}
/* Initialize RTC related gpio */
bool rtc_gpio_init(void)
{
u16 con;
/* RTC_32K1V8 clock change from 128k div 4 source to RTC 32k source */
pwrap_write_field(PMIC_RG_TOP_CKSEL_CON0_SET, 0x1, 0x1, 3);
/* Export 32K clock RTC_32K1V8_1 */
pwrap_write_field(PMIC_RG_TOP_CKPDN_CON1_CLR, 0x1, 0x1, 1);
/* Export 32K clock RTC_32K2V8 */
rtc_read(RTC_CON, &con);
con &= (RTC_CON_LPSTA_RAW | RTC_CON_LPRST | RTC_CON_EOSC32_LPEN
| RTC_CON_XOSC32_LPEN);
con |= (RTC_CON_GPEN | RTC_CON_GOE);
con &= ~RTC_CON_F32KOB;
rtc_write(RTC_CON, con);
return rtc_write_trigger();
}
u16 rtc_get_frequency_meter(u16 val, u16 measure_src, u16 window_size)
{
u16 bbpu, osc32con;
u16 fqmtr_busy, fqmtr_data, fqmtr_rst, fqmtr_tcksel;
struct stopwatch sw;
rtc_read(RTC_BBPU, &bbpu);
rtc_write(RTC_BBPU, bbpu | RTC_BBPU_KEY | RTC_BBPU_RELOAD);
if (!rtc_write_trigger()) {
rtc_info("rtc_write_trigger() failed\n");
return false;
}
rtc_read(RTC_OSC32CON, &osc32con);
if (!rtc_xosc_write((osc32con & ~RTC_XOSCCALI_MASK) |
(val & RTC_XOSCCALI_MASK))) {
rtc_info("rtc_xosc_write() failed\n");
return false;
}
/* Enable FQMTR clock */
pwrap_write_field(PMIC_RG_TOP_CKPDN_CON0_CLR, 1, 1,
PMIC_RG_FQMTR_32K_CK_PDN_SHIFT);
pwrap_write_field(PMIC_RG_TOP_CKPDN_CON0_CLR, 1, 1,
PMIC_RG_FQMTR_CK_PDN_SHIFT);
/* FQMTR reset */
pwrap_write_field(PMIC_RG_FQMTR_RST, 1, 1, PMIC_FQMTR_RST_SHIFT);
do {
rtc_read(PMIC_RG_FQMTR_DATA, &fqmtr_data);
rtc_read(PMIC_RG_FQMTR_CON0, &fqmtr_busy);
} while (fqmtr_data && (fqmtr_busy & PMIC_FQMTR_CON0_BUSY));
rtc_read(PMIC_RG_FQMTR_RST, &fqmtr_rst);
/* FQMTR normal */
pwrap_write_field(PMIC_RG_FQMTR_RST, 0, 1, PMIC_FQMTR_RST_SHIFT);
/* Set frequency meter window value (0=1X32K(fixed clock)) */
rtc_write(PMIC_RG_FQMTR_WINSET, window_size);
/* Enable 26M and set test clock source */
rtc_write(PMIC_RG_FQMTR_CON0, PMIC_FQMTR_CON0_DCXO26M_EN | measure_src);
/* Enable 26M -> delay 100us -> enable FQMTR */
udelay(100);
rtc_read(PMIC_RG_FQMTR_CON0, &fqmtr_tcksel);
/* Enable FQMTR */
rtc_write(PMIC_RG_FQMTR_CON0, fqmtr_tcksel | PMIC_FQMTR_CON0_FQMTR_EN);
udelay(100);
stopwatch_init_usecs_expire(&sw, FQMTR_TIMEOUT_US);
/* FQMTR read until ready */
if (!wait_us(FQMTR_TIMEOUT_US,
rtc_read(PMIC_RG_FQMTR_CON0, &fqmtr_busy) == 0 &&
!(fqmtr_busy & PMIC_FQMTR_CON0_BUSY))) {
rtc_info("get frequency time out: %#x\n", fqmtr_busy);
return false;
}
/* Read data should be closed to 26M/32k = 794 */
rtc_read(PMIC_RG_FQMTR_DATA, &fqmtr_data);
rtc_read(PMIC_RG_FQMTR_CON0, &fqmtr_tcksel);
/* Disable FQMTR */
rtc_write(PMIC_RG_FQMTR_CON0, fqmtr_tcksel & ~PMIC_FQMTR_CON0_FQMTR_EN);
/* Disable FQMTR -> delay 100us -> disable 26M */
udelay(100);
/* Disable 26M */
rtc_read(PMIC_RG_FQMTR_CON0, &fqmtr_tcksel);
rtc_write(PMIC_RG_FQMTR_CON0,
fqmtr_tcksel & ~PMIC_FQMTR_CON0_DCXO26M_EN);
rtc_info("input = %#x, output = %#x\n", val, fqmtr_data);
/* Disable FQMTR clock */
pwrap_write_field(PMIC_RG_TOP_CKPDN_CON0_SET, 1, 1,
PMIC_RG_FQMTR_32K_CK_PDN_SHIFT);
pwrap_write_field(PMIC_RG_TOP_CKPDN_CON0_SET, 1, 1,
PMIC_RG_FQMTR_CK_PDN_SHIFT);
return fqmtr_data;
}
/* Low power detect setting */
static bool rtc_lpd_init(void)
{
u16 con, sec;
/* Set RTC_LPD_OPT */
rtc_read(RTC_AL_SEC, &sec);
sec |= RTC_LPD_OPT_F32K_CK_ALIVE;
rtc_write(RTC_AL_SEC, sec);
if (!rtc_write_trigger()) {
rtc_info("rtc_write_trigger() failed\n");
return false;
}
/* Initialize XOSC32 to detect 32k clock stop */
rtc_read(RTC_CON, &con);
con |= RTC_CON_XOSC32_LPEN;
if (!rtc_lpen(con))
return false;
/* Initialize EOSC32 to detect RTC low power */
rtc_read(RTC_CON, &con);
con |= RTC_CON_EOSC32_LPEN;
if (!rtc_lpen(con))
return false;
rtc_read(RTC_CON, &con);
con &= ~RTC_CON_XOSC32_LPEN;
rtc_write(RTC_CON, con);
/* Set RTC_LPD_OPT */
rtc_read(RTC_AL_SEC, &sec);
sec &= ~RTC_LPD_OPT_MASK;
sec |= RTC_LPD_OPT_EOSC_LPD;
rtc_write(RTC_AL_SEC, sec);
if (!rtc_write_trigger()) {
rtc_info("rtc_write_trigger() failed\n");
return false;
}
return true;
}
static bool rtc_hw_init(void)
{
u16 bbpu;
rtc_read(RTC_BBPU, &bbpu);
rtc_write(RTC_BBPU, bbpu | RTC_BBPU_KEY | RTC_BBPU_INIT);
if (!rtc_write_trigger()) {
rtc_info("rtc_write_trigger() failed\n");
return false;
}
udelay(500);
rtc_read(RTC_BBPU, &bbpu);
rtc_write(RTC_BBPU, bbpu | RTC_BBPU_KEY | RTC_BBPU_RELOAD);
if (!rtc_write_trigger()) {
rtc_info("rtc_write_trigger() failed\n");
return false;
}
rtc_read(RTC_BBPU, &bbpu);
if (bbpu & RTC_BBPU_INIT) {
rtc_info("timeout\n");
return false;
}
return true;
}
static void mt6366_dcxo_disable_unused(void)
{
/* Disable clock buffer XO_CEL */
rtc_write(PMIC_RG_DCXO_CW00_CLR, 0x0800);
/* Mask bblpm request and switch off bblpm mode */
rtc_write(PMIC_RG_DCXO_CW23, 0x0052);
}
static void rtc_set_capid(u16 capid)
{
u16 read_capid;
rtc_write(PMIC_RG_DCXO_CW03, 0xFF00 | capid);
rtc_read(PMIC_RG_DCXO_CW03, &read_capid);
rtc_info("read back capid: %#x\n", read_capid & 0xFF);
}
/* Check RTC Initialization */
int rtc_init(int recover)
{
int ret;
rtc_info("recovery: %d\n", recover);
/* Write powerkeys to enable RTC functions */
if (!rtc_powerkey_init()) {
ret = -RTC_STATUS_POWERKEY_INIT_FAIL;
goto err;
}
/* Write interface unlock need to be set after powerkey match */
if (!rtc_writeif_unlock()) {
ret = -RTC_STATUS_WRITEIF_UNLOCK_FAIL;
goto err;
}
rtc_osc_init();
/* In recovery mode, we need 20ms delay for register setting. */
if (recover)
mdelay(20);
if (!rtc_gpio_init()) {
ret = -RTC_STATUS_GPIO_INIT_FAIL;
goto err;
}
if (!rtc_hw_init()) {
ret = -RTC_STATUS_HW_INIT_FAIL;
goto err;
}
if (!rtc_reg_init()) {
ret = -RTC_STATUS_REG_INIT_FAIL;
goto err;
}
if (!rtc_lpd_init()) {
ret = -RTC_STATUS_LPD_INIT_FAIL;
goto err;
}
/*
* After lpd init, powerkeys need to be written again to enable
* low power detect function.
*/
if (!rtc_powerkey_init()) {
ret = -RTC_STATUS_POWERKEY_INIT_FAIL;
goto err;
}
return RTC_STATUS_OK;
err:
rtc_info("init failed: ret = %d\n", ret);
return ret;
}
/* Enable RTC bbpu */
void rtc_bbpu_power_on(void)
{
u16 bbpu;
int ret;
/* Pull powerhold high, control by pmic */
mt6366_set_power_hold(true);
/* Pull PWRBB high */
bbpu = RTC_BBPU_KEY | RTC_BBPU_AUTO | RTC_BBPU_RELOAD | RTC_BBPU_PWREN;
rtc_write(RTC_BBPU, bbpu);
ret = rtc_write_trigger();
rtc_info("rtc_write_trigger = %d\n", ret);
rtc_read(RTC_BBPU, &bbpu);
rtc_info("done BBPU = %#x\n", bbpu);
}
static void dcxo_init(void)
{
/* Buffer setting */
rtc_write(PMIC_RG_DCXO_CW15, 0xA2AA);
rtc_write(PMIC_RG_DCXO_CW13, 0x98E9);
rtc_write(PMIC_RG_DCXO_CW16, 0x9855);
/* 26M enable control */
/* Enable clock buffer XO_SOC, XO_CEL */
rtc_write(PMIC_RG_DCXO_CW00, 0x4805);
rtc_write(PMIC_RG_DCXO_CW11, 0x8000);
/* Load thermal coefficient */
rtc_write(PMIC_RG_TOP_TMA_KEY, 0x9CA7);
rtc_write(PMIC_RG_DCXO_CW21, 0x12A7);
rtc_write(PMIC_RG_DCXO_ELR0, 0xD004);
rtc_write(PMIC_RG_TOP_TMA_KEY, 0x0000);
/* Adjust OSC FPM setting */
rtc_write(PMIC_RG_DCXO_CW07, 0x8FFE);
/* Re-calibrate OSC current */
rtc_write(PMIC_RG_DCXO_CW09, 0x008F);
udelay(100);
rtc_write(PMIC_RG_DCXO_CW09, 0x408F);
mdelay(5);
rtc_set_capid(MT8186_RTC_DXCO_CAPID);
mt6366_dcxo_disable_unused();
}
/* Initialize rtc boot flow */
void rtc_boot(void)
{
/* DCXO clock initialized settings */
dcxo_init();
/* DCXO 32k initialized settings */
pwrap_write_field(PMIC_RG_DCXO_CW02, 0xF, 0xF, 0);
pwrap_write_field(PMIC_RG_SCK_TOP_CON0, 0x1, 0x1, 0);
/* Use DCXO 32K clock */
if (!rtc_enable_dcxo())
rtc_info("rtc_enable_dcxo() failed\n");
rtc_boot_common();
rtc_bbpu_power_on();
}
|