Debian Codegraph @

src/soc/mediatek/common/pmif_spi.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <console/console.h>
4 #include <device/mmio.h>
5 #include <soc/pll.h>
6 #include <soc/pmif_spi.h>
7 #include <soc/pmif_sw.h>
8 #include <timer.h>
9
10 /* PMIF, SPI_MODE_CTRL */
11 DEFINE_BIT(SPI_MODE_CTRL_VLD_SRCLK_EN_CTRL, 5)
12 DEFINE_BIT(SPI_MODE_CTRL_PMIF_RDY, 9)
13 DEFINE_BIT(SPI_MODE_CTRL_SRCLK_EN, 10)
14 DEFINE_BIT(SPI_MODE_CTRL_SRVOL_EN, 11)
15
16 /* PMIF, SLEEP_PROTECTION_CTRL */
17 DEFINE_BITFIELD(SPM_SLEEP_REQ_SEL, 1, 0)
18 DEFINE_BITFIELD(SCP_SLEEP_REQ_SEL, 10, 9)
19
20 /* PMIF, OTHER_INF_EN */
21 DEFINE_BITFIELD(INTGPSADCINF_EN, 5, 4)
22
23 /* PMIF, STAUPD_CTRL */
24 DEFINE_BITFIELD(STAUPD_CTRL_PRD, 3, 0)
25 DEFINE_BIT(STAUPD_CTRL_PMIC0_SIG_STA, 4)
26 DEFINE_BIT(STAUPD_CTRL_PMIC0_EINT_STA, 6)
27
28 /* SPIMST, Manual_Mode_Access */
29 DEFINE_BITFIELD(MAN_ACC_SPI_OP, 12, 8)
30 DEFINE_BIT(MAN_ACC_SPI_RW, 13)
31
32 static void pmif_spi_config(struct pmif *arb)
33 {
34 /* Set srclk_en always valid regardless of ulposc_sel_for_scp */
35 SET32_BITFIELDS(&arb->mtk_pmif->spi_mode_ctrl, SPI_MODE_CTRL_VLD_SRCLK_EN_CTRL, 0);
36
37 /* Set SPI mode controlled by srclk_en and srvol_en instead of pmif_rdy */
38 SET32_BITFIELDS(&arb->mtk_pmif->spi_mode_ctrl,
39 SPI_MODE_CTRL_SRCLK_EN, 1,
40 SPI_MODE_CTRL_SRVOL_EN, 1,
41 SPI_MODE_CTRL_PMIF_RDY, 0);
42
43 SET32_BITFIELDS(&arb->mtk_pmif->sleep_protection_ctrl, SPM_SLEEP_REQ_SEL, 0,
44 SCP_SLEEP_REQ_SEL, 0);
45
46 /* Enable SWINF for AP */
47 write32(&arb->mtk_pmif->inf_en, PMIF_SPI_AP);
48
49 /* Enable arbitration for SWINF for AP */
50 write32(&arb->mtk_pmif->arb_en, PMIF_SPI_AP);
51
52 /* Enable PMIF_SPI Command Issue */
53 write32(&arb->mtk_pmif->cmdissue_en, 1);
54 }
55
56 static int check_idle(void *addr, u32 expected)
57 {
58 u32 reg_rdata;
59 struct stopwatch sw;
60
61 stopwatch_init_usecs_expire(&sw, PMIF_WAIT_IDLE_US);
62 do {
63 reg_rdata = read32(addr);
64 if (stopwatch_expired(&sw))
65 return E_TIMEOUT;
66 } while ((reg_rdata & expected) != 0);
67
68 return 0;
69 }
70
71 static int reset_spislv(void)
72 {
73 u32 pmicspi_mst_dio_en_backup;
74
75 write32(&mtk_pmicspi_mst->wrap_en, 0);
76 write32(&mtk_pmicspi_mst->mux_sel, 1);
77 write32(&mtk_pmicspi_mst->man_en, 1);
78 pmicspi_mst_dio_en_backup = read32(&mtk_pmicspi_mst->dio_en);
79 write32(&mtk_pmicspi_mst->dio_en, 0);
80
81 SET32_BITFIELDS(&mtk_pmicspi_mst->man_acc, MAN_ACC_SPI_RW, OP_WR,
82 MAN_ACC_SPI_OP, OP_CSL);
83 /* Reset counter */
84 SET32_BITFIELDS(&mtk_pmicspi_mst->man_acc, MAN_ACC_SPI_RW, OP_WR,
85 MAN_ACC_SPI_OP, OP_OUTS);
86 SET32_BITFIELDS(&mtk_pmicspi_mst->man_acc, MAN_ACC_SPI_RW, OP_WR,
87 MAN_ACC_SPI_OP, OP_CSH);
88 /*
89 * In order to pull CSN signal to PMIC,
90 * PMIC will count it then reset spi slave
91 */
92 SET32_BITFIELDS(&mtk_pmicspi_mst->man_acc, MAN_ACC_SPI_RW, OP_WR,
93 MAN_ACC_SPI_OP, OP_OUTS);
94 SET32_BITFIELDS(&mtk_pmicspi_mst->man_acc, MAN_ACC_SPI_RW, OP_WR,
95 MAN_ACC_SPI_OP, OP_OUTS);
96 SET32_BITFIELDS(&mtk_pmicspi_mst->man_acc, MAN_ACC_SPI_RW, OP_WR,
97 MAN_ACC_SPI_OP, OP_OUTS);
98 SET32_BITFIELDS(&mtk_pmicspi_mst->man_acc, MAN_ACC_SPI_RW, OP_WR,
99 MAN_ACC_SPI_OP, OP_OUTS);
100
101 /* Wait for PMIC SPI Master to be idle */
102 if (check_idle(&mtk_pmicspi_mst->other_busy_sta_0, SPIMST_STA)) {
103 printk(BIOS_ERR, "[%s] spi master busy, timeout\n", __func__);
104 return E_TIMEOUT;
105 }
106
107 write32(&mtk_pmicspi_mst->man_en, 0);
108 write32(&mtk_pmicspi_mst->mux_sel, 0);
109 write32(&mtk_pmicspi_mst->wrap_en, 1);
110 write32(&mtk_pmicspi_mst->dio_en, pmicspi_mst_dio_en_backup);
111
112 return 0;
113 }
114
115 static void init_reg_clock(struct pmif *arb)
116 {
117 pmif_spi_iocfg();
118
119 /* Configure SPI protocol */
120 write32(&mtk_pmicspi_mst->ext_ck_write, 1);
121 write32(&mtk_pmicspi_mst->ext_ck_read, 0);
122 write32(&mtk_pmicspi_mst->cshext_write, 0);
123 write32(&mtk_pmicspi_mst->cshext_read, 0);
124 write32(&mtk_pmicspi_mst->cslext_write, 0);
125 write32(&mtk_pmicspi_mst->cslext_read, 0x100);
126
127 /* Set Read Dummy Cycle Number (Slave Clock is 18MHz) */
128 arb->write(arb, DEFAULT_SLVID, PMIC_DEW_RDDMY_NO, DUMMY_READ_CYCLES);
129 write32(&mtk_pmicspi_mst->rddmy, DUMMY_READ_CYCLES);
130
131 /* Enable DIO mode */
132 arb->write(arb, DEFAULT_SLVID, PMIC_DEW_DIO_EN, 0x1);
133
134 /* Wait for completion of sending the commands */
135 if (check_idle(&arb->mtk_pmif->inf_busy_sta, PMIF_SPI_AP)) {
136 printk(BIOS_ERR, "[%s] pmif channel busy, timeout\n", __func__);
137 return;
138 }
139
140 if (check_idle(&arb->mtk_pmif->other_busy_sta_0, PMIF_CMD_STA)) {
141 printk(BIOS_ERR, "[%s] pmif cmd busy, timeout\n", __func__);
142 return;
143 }
144
145 if (check_idle(&mtk_pmicspi_mst->other_busy_sta_0, SPIMST_STA)) {
146 printk(BIOS_ERR, "[%s] spi master busy, timeout\n", __func__);
147 return;
148 }
149
150 write32(&mtk_pmicspi_mst->dio_en, 1);
151 }
152
153 static void init_spislv(struct pmif *arb)
154 {
155 /* Turn on SPI IO filter function */
156 arb->write(arb, DEFAULT_SLVID, PMIC_FILTER_CON0, SPI_FILTER);
157 /* Turn on SPI IO SMT function to improve noise immunity */
158 arb->write(arb, DEFAULT_SLVID, PMIC_SMT_CON1, SPI_SMT);
159 /* Turn off SPI IO pull function for power saving */
160 arb->write(arb, DEFAULT_SLVID, PMIC_GPIO_PULLEN0_CLR, SPI_PULL_DISABLE);
161 /* Enable SPI access in SODI-3.0 and Suspend modes */
162 arb->write(arb, DEFAULT_SLVID, PMIC_RG_SPI_CON0, 0x2);
163 /* Set SPI IO driving strength to 4 mA */
164 arb->write(arb, DEFAULT_SLVID, PMIC_DRV_CON1, SPI_DRIVING);
165 }
166
167 static int init_sistrobe(struct pmif *arb)
168 {
169 u32 rdata = 0;
170 int si_sample_ctrl;
171 /* Random data for testing */
172 const u32 test_data[30] = {
173 0x6996, 0x9669, 0x6996, 0x9669, 0x6996, 0x9669, 0x6996,
174 0x9669, 0x6996, 0x9669, 0x5AA5, 0xA55A, 0x5AA5, 0xA55A,
175 0x5AA5, 0xA55A, 0x5AA5, 0xA55A, 0x5AA5, 0xA55A, 0x1B27,
176 0x1B27, 0x1B27, 0x1B27, 0x1B27, 0x1B27, 0x1B27, 0x1B27,
177 0x1B27, 0x1B27
178 };
179
180 for (si_sample_ctrl = 0; si_sample_ctrl < 16; si_sample_ctrl++) {
181 write32(&mtk_pmicspi_mst->si_sampling_ctrl, si_sample_ctrl << 5);
182
183 arb->read(arb, DEFAULT_SLVID, PMIC_DEW_READ_TEST, &rdata);
184 if (rdata == DEFAULT_VALUE_READ_TEST)
185 break;
186 }
187
188 if (si_sample_ctrl == 16)
189 return E_CLK_EDGE;
190
191 if (si_sample_ctrl == 15)
192 return E_CLK_LAST_SETTING;
193
194 /*
195 * Add the delay time of SPI data from PMIC to align the start boundary
196 * to current sampling clock edge.
197 */
198 for (int si_dly = 0; si_dly < 10; si_dly++) {
199 arb->write(arb, DEFAULT_SLVID, PMIC_RG_SPI_CON2, si_dly);
200
201 int start_boundary_found = 0;
202 for (int i = 0; i < ARRAY_SIZE(test_data); i++) {
203 arb->write(arb, DEFAULT_SLVID, PMIC_DEW_WRITE_TEST, test_data[i]);
204 arb->read(arb, DEFAULT_SLVID, PMIC_DEW_WRITE_TEST, &rdata);
205 if ((rdata & 0x7fff) != (test_data[i] & 0x7fff)) {
206 start_boundary_found = 1;
207 break;
208 }
209 }
210 if (start_boundary_found == 1)
211 break;
212 }
213
214 /*
215 * Change the sampling clock edge to the next one which is the middle
216 * of SPI data window.
217 */
218 write32(&mtk_pmicspi_mst->si_sampling_ctrl, ++si_sample_ctrl << 5);
219
220 /* Read Test */
221 arb->read(arb, DEFAULT_SLVID, PMIC_DEW_READ_TEST, &rdata);
222 if (rdata != DEFAULT_VALUE_READ_TEST) {
223 printk(BIOS_ERR, "[%s] Failed for read test, data = %#x.\n",
224 __func__, rdata);
225 return E_READ_TEST_FAIL;
226 }
227
228 return 0;
229 }
230
231 static void init_staupd(struct pmif *arb)
232 {
233 /* Unlock SPI Slave registers */
234 arb->write(arb, DEFAULT_SLVID, PMIC_SPISLV_KEY, 0xbade);
235
236 /* Enable CRC of PMIC 0 */
237 arb->write(arb, DEFAULT_SLVID, PMIC_DEW_CRC_EN, 0x1);
238
239 /* Wait for completion of sending the commands */
240 if (check_idle(&arb->mtk_pmif->inf_busy_sta, PMIF_SPI_AP)) {
241 printk(BIOS_ERR, "[%s] pmif channel busy, timeout\n", __func__);
242 return;
243 }
244
245 if (check_idle(&arb->mtk_pmif->other_busy_sta_0, PMIF_CMD_STA)) {
246 printk(BIOS_ERR, "[%s] pmif cmd busy, timeout\n", __func__);
247 return;
248 }
249
250 if (check_idle(&mtk_pmicspi_mst->other_busy_sta_0, SPIMST_STA)) {
251 printk(BIOS_ERR, "[%s] spi master busy, timeout\n", __func__);
252 return;
253 }
254
255 /* Configure CRC of PMIC Interface */
256 write32(&arb->mtk_pmif->crc_ctrl, 0x1);
257 write32(&arb->mtk_pmif->sig_mode, 0x0);
258
259 /* Lock SPI Slave registers */
260 arb->write(arb, DEFAULT_SLVID, PMIC_SPISLV_KEY, 0x0);
261
262 /* Set up PMIC Siganature */
263 write32(&arb->mtk_pmif->pmic_sig_addr, PMIC_DEW_CRC_VAL);
264
265 /* Set up PMIC EINT */
266 write32(&arb->mtk_pmif->pmic_eint_sta_addr, PMIC_INT_STA);
267
268 SET32_BITFIELDS(&arb->mtk_pmif->staupd_ctrl,
269 STAUPD_CTRL_PRD, 5,
270 STAUPD_CTRL_PMIC0_SIG_STA, 1,
271 STAUPD_CTRL_PMIC0_EINT_STA, 1);
272 }
273
274 __weak void pmif_spi_setting(struct pmif *arb)
275 {
276 /* do nothing, only for special setting of IC */
277 }
278
279 int pmif_spi_init(struct pmif *arb)
280 {
281 pmif_spi_config(arb);
282
283 /* Reset spislv */
284 if (reset_spislv())
285 return E_SPI_INIT_RESET_SPI;
286
287 /* Enable WRAP */
288 write32(&mtk_pmicspi_mst->wrap_en, 0x1);
289
290 /* SPI Waveform Configuration */
291 init_reg_clock(arb);
292
293 /* SPI Slave Configuration */
294 init_spislv(arb);
295
296 /* Input data calibration flow; */
297 if (init_sistrobe(arb)) {
298 printk(BIOS_ERR, "[%s] data calibration fail\n", __func__);
299 return E_SPI_INIT_SIDLY;
300 }
301
302 /* Lock SPISLV Registers */
303 arb->write(arb, DEFAULT_SLVID, PMIC_SPISLV_KEY, 0x0);
304
305 /*
306 * Status update function initialization
307 * 1. Check signature using CRC (CRC 0 only)
308 * 2. Update EINT
309 * 3. Read back AUXADC thermal data for GPS
310 */
311 init_staupd(arb);
312
313 /* Configure PMIF Timer */
314 write32(&arb->mtk_pmif->timer_ctrl, 0x3);
315
316 /* Enable interfaces and arbitration */
317 write32(&arb->mtk_pmif->inf_en, PMIF_SPI_INF_EN);
318
319 write32(&arb->mtk_pmif->arb_en, PMIF_SPI_ARB_EN);
320
321 /* Enable GPS AUXADC HW 0 and 1 */
322 SET32_BITFIELDS(&arb->mtk_pmif->other_inf_en, INTGPSADCINF_EN, 0x3);
323
324 pmif_spi_setting(arb);
325
326 /* Set INIT_DONE */
327 write32(&arb->mtk_pmif->init_done, 0x1);
328
329 return 0;
330 }