1 /* SPDX-License-Identifier: GPL-2.0-only OR MIT */
3 #include <console/console.h>
4 #include <device/mmio.h>
6 #include <soc/addressmap.h>
9 #include <soc/pmif_spmi.h>
12 #define SPMI_RCS_CTRL_BASE 0x140084
14 /* TOPCKGEN, CLK_CFG_17 */
15 DEFINE_BITFIELD(CLK_SPMI_MST_SEL
, 10, 8)
16 DEFINE_BIT(CLK_SPMI_MST_INT
, 12)
17 DEFINE_BIT(PDN_SPMI_MST
, 15)
19 /* TOPCKGEN, CLK_CFG_UPDATE2 */
20 DEFINE_BIT(SPMI_MST_CK_UPDATE
, 5)
22 /* SPMI_MST, SPMI_SAMPL_CTRL */
23 DEFINE_BIT(SAMPL_CK_POL
, 0)
24 DEFINE_BITFIELD(SAMPL_CK_DLY
, 3, 1)
25 DEFINE_BITFIELD(SAMPL_CK_DLY_ARB
, 6, 4)
27 /* PMIF, SPI_MODE_CTRL */
28 DEFINE_BIT(VLD_SRCLK_EN_CTRL
, 5)
29 DEFINE_BIT(SPI_MODE_CTRL_PMIF_RDY
, 9)
30 DEFINE_BIT(SPI_MODE_CTRL_SRCLK_EN
, 10)
31 DEFINE_BIT(SPI_MODE_CTRL_SRVOL_EN
, 11)
33 /* PMIF, SLEEP_PROTECTION_CTRL */
34 DEFINE_BIT(SPM_SLEEP_REQ_SEL
, 0)
35 DEFINE_BIT(SCP_SLEEP_REQ_SEL
, 9)
37 const struct spmi_device spmi_dev
[] = {
39 .slvid
= SPMI_SLAVE_4
, /* MT6363 */
40 .mstid
= SPMI_MASTER_1
,
42 .type_id
= MAIN_PMIC_ID
,
48 .slvid
= SPMI_SLAVE_9
, /* MT6685 */
49 .mstid
= SPMI_MASTER_1
,
51 .type_id
= CLOCK_PMIC_ID
,
57 .slvid
= SPMI_SLAVE_5
, /* MT6373 */
58 .mstid
= SPMI_MASTER_1
,
60 .type_id
= SECOND_PMIC_ID
,
66 .slvid
= SPMI_SLAVE_6
, /* MT6316 */
67 .mstid
= SPMI_MASTER_0
,
69 .type_id
= SUB_PMIC_ID
,
75 .slvid
= SPMI_SLAVE_7
, /* MT6316 */
76 .mstid
= SPMI_MASTER_0
,
78 .type_id
= SUB_PMIC_ID
,
84 .slvid
= SPMI_SLAVE_8
, /* MT6316 */
85 .mstid
= SPMI_MASTER_0
,
87 .type_id
= SUB_PMIC_ID
,
93 .slvid
= SPMI_SLAVE_15
, /* MT6316 */
94 .mstid
= SPMI_MASTER_0
,
96 .type_id
= SUB_PMIC_ID
,
103 static const uint32_t chan_perm
[2][4] = {
104 [SPMI_MASTER_0
] = { 0x8888888A, 0x88888888, 0x88888888, 0x88888888 },
105 [SPMI_MASTER_1
] = { 0x888888AA, 0x88888888, 0x88888888, 0x88888888 },
108 static const uint32_t lat_limit
[2][23] = {
109 [SPMI_MASTER_0
] = { 0x3e6, 0x3e6, 0x9, 0x9, 0x3e6, 0x3e6, 0x3e6, 0x3e6, 0x9, 0x3e6,
110 0x0, 0x3e6, 0x9, 0x0, 0x0, 0x3e6, 0x3e6, 0x3e6, 0x3e6, 0x0,
112 [SPMI_MASTER_1
] = { 0x3e6, 0x0, 0x3e6, 0x3e6, 0x26, 0x37, 0x3e6, 0x10, 0x9, 0x23,
113 0x0, 0x39, 0x9, 0x0, 0x0, 0x12, 0x3e6, 0x3e6, 0x3e6, 0x0,
117 const size_t spmi_dev_cnt
= ARRAY_SIZE(spmi_dev
);
119 static struct mtk_spmi_mst_reg
*get_mst_reg(struct pmif
*arb
)
121 if (arb
->mstid
== SPMI_MASTER_1
)
123 else if (arb
->mstid
== SPMI_MASTER_0
|| arb
->mstid
== SPMI_MASTER_2
)
124 return mtk_spmi_mst_p
;
129 static int pmif_spmi_config_master(struct pmif
*arb
)
131 struct mtk_spmi_mst_reg
*reg
= get_mst_reg(arb
);
133 printk(BIOS_ERR
, "%s: get_mst_reg get null pointer\n", __func__
);
137 write32(®
->mst_req_en
, 1);
138 write32(®
->grp_id_en
, 0xC00);
139 /* enable multi-master and rcs support */
140 write32(®
->rcs_ctrl
, SPMI_RCS_CTRL_BASE
+ arb
->mstid
);
141 printk(BIOS_INFO
, "%s: mstid=%d done\n", __func__
, arb
->mstid
);
145 void pmif_spmi_config(struct pmif
*arb
)
147 _Static_assert(ARRAY_SIZE(chan_perm
[0]) == ARRAY_SIZE(arb
->mtk_pmif
->inf_cmd_per
),
148 "Inconsistent array size for chan_perm and inf_cmd_per");
149 const uint32_t *perm
= chan_perm
[arb
->mstid
% 2];
151 * all cmd permission for per channel
152 * [0]: Command 0 Permitted
153 * [1]: Command 1 Permitted
154 * [2]: Command 2 Permitted
155 * [3]: Command 3 Permitted
157 /* PER_0[8:0]: CH0 or CH1 for MD DVFS HW, backward compatible for swrgo */
158 for (int i
= 0; i
< ARRAY_SIZE(arb
->mtk_pmif
->inf_cmd_per
); i
++)
159 write32(&arb
->mtk_pmif
->inf_cmd_per
[i
], perm
[i
]);
162 void pmif_spmi_iocfg(void)
165 gpio_set_driving(GPIO(SPMI_M_SCL
), GPIO_DRV_10_MA
);
166 gpio_set_driving(GPIO(SPMI_M_SDA
), GPIO_DRV_10_MA
);
168 gpio_set_driving(GPIO(SPMI_P_SCL
), GPIO_DRV_14_MA
);
169 gpio_set_driving(GPIO(SPMI_P_SDA
), GPIO_DRV_14_MA
);
170 printk(BIOS_INFO
, "%s done\n", __func__
);
173 static int spmi_read_check(struct pmif
*arb
, const struct spmi_device
*dev
)
177 arb
->read(arb
, dev
->slvid
, dev
->hwcid_addr
, &rdata
);
178 if ((rdata
& dev
->hwcid_mask
) != (dev
->hwcid_val
& dev
->hwcid_mask
)) {
179 printk(BIOS_WARNING
, "%s next, slvid:%d rdata = 0x%x\n",
180 __func__
, dev
->slvid
, rdata
);
184 printk(BIOS_INFO
, "%s next, slvid:%d rdata = 0x%x.\n", __func__
, dev
->slvid
, rdata
);
188 static int spmi_cali_rd_clock_polarity(struct pmif
*arb
, const struct spmi_device
*dev
)
191 bool success
= false;
192 const struct cali cali_data
[] = {
193 {SPMI_CK_DLY_1T
, SPMI_CK_POL_NEG
},
194 {SPMI_CK_DLY_1T
, SPMI_CK_POL_POS
},
195 {SPMI_CK_NO_DLY
, SPMI_CK_POL_POS
},
196 {SPMI_CK_NO_DLY
, SPMI_CK_POL_NEG
},
197 {SPMI_CK_DLY_2T
, SPMI_CK_POL_POS
},
198 {SPMI_CK_DLY_2T
, SPMI_CK_POL_NEG
},
201 struct mtk_spmi_mst_reg
*reg
= get_mst_reg(arb
);
203 printk(BIOS_ERR
, "%s: get_mst_reg get null pointer\n", __func__
);
207 /* Indicate sampling clock polarity, 1: Positive 0: Negative */
208 for (i
= 0; i
< ARRAY_SIZE(cali_data
); i
++) {
209 SET32_BITFIELDS(®
->mst_sampl
, SAMPL_CK_DLY
, cali_data
[i
].dly
,
210 SAMPL_CK_POL
, cali_data
[i
].pol
);
211 SET32_BITFIELDS(®
->mst_sampl
, SAMPL_CK_DLY_ARB
, cali_data
[i
].dly
+ 1);
212 printk(BIOS_DEBUG
, "dly:%d, pol:%d, sampl:0x%x\n",
213 cali_data
[i
].dly
, cali_data
[i
].pol
, read32(®
->mst_sampl
));
214 if (spmi_read_check(arb
, dev
) == 0) {
221 die("ERROR - calibration fail for SPMI clk");
226 static int spmi_config_slave(struct pmif
*arb
, const struct spmi_device
*dev
)
230 /* set RG_RCS_ADDR=slave id */
232 arb
->write(arb
, dev
->slvid
, 0x419, wdata
); /* rcs_slvid_addr: 0x419 */
234 /* set RG_RCS_ENABLE=1, RG_RCS_ID=Master ID */
235 wdata
= 0x5 | (dev
->mstid
<< 4);
236 arb
->write(arb
, dev
->slvid
, 0x418, wdata
); /* rcs_slvid_en: 0x418 */
241 static int spmi_mst_init(struct pmif
*arb
)
246 printk(BIOS_ERR
, "%s: null pointer for pmif dev\n", __func__
);
250 if (!CONFIG(PMIF_SPMI_IOCFG_DEFAULT_SETTING
))
253 if (pmif_spmi_config_master(arb
))
256 for (i
= 0; i
< spmi_dev_cnt
; i
++) {
257 if ((arb
->mstid
% 2) == spmi_dev
[i
].mstid
) {
258 spmi_cali_rd_clock_polarity(arb
, &spmi_dev
[i
]); /* spmi_cali */
259 spmi_config_slave(arb
, &spmi_dev
[i
]);
266 static void pmif_spmi_force_normal_mode(struct pmif
*arb
)
268 /* listen srclken_0 only for entering normal or sleep mode */
269 SET32_BITFIELDS(&arb
->mtk_pmif
->spi_mode_ctrl
,
270 VLD_SRCLK_EN_CTRL
, 0,
271 SPI_MODE_CTRL_PMIF_RDY
, 1,
272 SPI_MODE_CTRL_SRCLK_EN
, 0,
273 SPI_MODE_CTRL_SRVOL_EN
, 0);
275 /* disable spm/scp sleep request */
276 SET32_BITFIELDS(&arb
->mtk_pmif
->sleep_protection_ctrl
, SPM_SLEEP_REQ_SEL
, 1,
277 SCP_SLEEP_REQ_SEL
, 1);
278 printk(BIOS_INFO
, "%s done\n", __func__
);
281 static void pmif_spmi_enable_swinf(struct pmif
*arb
)
283 write32(&arb
->mtk_pmif
->inf_en
, PMIF_SPMI_SW_CHAN
);
284 write32(&arb
->mtk_pmif
->arb_en
, PMIF_SPMI_SW_CHAN
);
285 printk(BIOS_INFO
, "%s done\n", __func__
);
288 static void pmif_spmi_enable_cmd_issue(struct pmif
*arb
, bool en
)
290 /* Enable cmdIssue */
291 write32(&arb
->mtk_pmif
->cmdissue_en
, en
);
292 printk(BIOS_INFO
, "%s done\n", __func__
);
295 static void pmif_spmi_enable(struct pmif
*arb
)
297 _Static_assert(ARRAY_SIZE(lat_limit
[0]) == ARRAY_SIZE(arb
->mtk_pmif
->lat_limit
),
298 "Inconsistent array size for lat_limit and arb->mtk_pmif->lat_limit");
299 const uint32_t *lat_limit_value
= lat_limit
[arb
->mstid
% 2];
304 pmif_spmi_inf
= PMIF_SPMI_INF
;
306 pmif_spmi_inf
= PMIF_SPMI_INF_P
;
308 pmif_spmi_config(arb
);
310 /* set bytecnt max limitation, 0:1 byte, 1:2 bytes */
311 for (i
= 0; i
< ARRAY_SIZE(arb
->mtk_pmif
->inf_max_bytecnt_per
); i
++)
312 write32(&arb
->mtk_pmif
->inf_max_bytecnt_per
[i
], 0x11111111);
314 /* Add latency limitation for all channels, method 2 */
315 /* Latency unit = (LAT_UNIT_SEL + 1)T of TMR_CK, LAT_UNIT_SEL=25;Lat unit=26T ~=1us */
316 write32(&arb
->mtk_pmif
->lat_cnter_ctrl
, 0x32);
318 write32(&arb
->mtk_pmif
->lat_cnter_en
, pmif_spmi_inf
);
319 for (i
= 0; i
< ARRAY_SIZE(arb
->mtk_pmif
->lat_limit
); i
++)
320 write32(&arb
->mtk_pmif
->lat_limit
[i
], lat_limit_value
[i
]);
321 write32(&arb
->mtk_pmif
->lat_limit_loading
, pmif_spmi_inf
);
323 /* Disable STAUPD_ITEM_EN */
324 write32(&arb
->mtk_pmif
->staupd_ctrl
, 0x1030000);
326 write32(&arb
->mtk_pmif
->inf_en
, pmif_spmi_inf
);
327 write32(&arb
->mtk_pmif
->arb_en
, pmif_spmi_inf
);
328 write32(&arb
->mtk_pmif
->timer_ctrl
, 0x3);
329 write32(&arb
->mtk_pmif
->init_done
, 1);
330 printk(BIOS_INFO
, "%s init done: 0x%x\n", __func__
, read32(&arb
->mtk_pmif
->init_done
));
333 int pmif_spmi_init(struct pmif
*arb
)
335 if (arb
->is_pmif_init_done(arb
) != 0) {
336 pmif_spmi_force_normal_mode(arb
);
337 pmif_spmi_enable_swinf(arb
);
338 pmif_spmi_enable_cmd_issue(arb
, true);
339 pmif_spmi_enable(arb
);
340 if (arb
->is_pmif_init_done(arb
))
344 if (spmi_mst_init(arb
)) {
345 printk(BIOS_ERR
, "[%s] failed to init SPMI master\n", __func__
);