1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <console/console.h>
4 #include <device/mmio.h>
5 #include <soc/addressmap.h>
7 #include <soc/pmif_spmi.h>
8 #include <soc/pmif_sw.h>
11 /* SPMI_MST, SPMI_SAMPL_CTRL */
12 DEFINE_BIT(SAMPL_CK_POL
, 0)
13 DEFINE_BITFIELD(SAMPL_CK_DLY
, 3, 1)
15 /* PMIF, SPI_MODE_CTRL */
16 DEFINE_BIT(SPI_MODE_CTRL
, 7)
17 DEFINE_BIT(SRVOL_EN
, 11)
18 DEFINE_BIT(SPI_MODE_EXT_CMD
, 12)
19 DEFINE_BIT(SPI_EINT_MODE_GATING_EN
, 13)
21 /* PMIF, SLEEP_PROTECTION_CTRL */
22 DEFINE_BITFIELD(SPM_SLEEP_REQ_SEL
, 1, 0)
23 DEFINE_BITFIELD(SCP_SLEEP_REQ_SEL
, 10, 9)
25 __weak
void pmif_spmi_config(struct pmif
*arb
)
30 static int spmi_read_check(struct pmif
*pmif_arb
, int slvid
)
34 pmif_arb
->read(pmif_arb
, slvid
, MT6315_READ_TEST
, &rdata
);
35 if (rdata
!= MT6315_DEFAULT_VALUE_READ
) {
36 printk(BIOS_INFO
, "%s next, slvid:%d rdata = 0x%x.\n",
37 __func__
, slvid
, rdata
);
41 pmif_arb
->read(pmif_arb
, slvid
, MT6315_READ_TEST_1
, &rdata
);
42 if (rdata
!= MT6315_DEFAULT_VALUE_READ
) {
43 printk(BIOS_INFO
, "%s next, slvid:%d rdata = 0x%x.\n",
44 __func__
, slvid
, rdata
);
51 static int spmi_cali_rd_clock_polarity(struct pmif
*pmif_arb
, const struct spmi_device
*dev
)
55 const struct cali cali_data
[] = {
56 {SPMI_CK_DLY_1T
, SPMI_CK_POL_POS
},
57 {SPMI_CK_NO_DLY
, SPMI_CK_POL_POS
},
58 {SPMI_CK_NO_DLY
, SPMI_CK_POL_NEG
},
59 {SPMI_CK_DLY_1T
, SPMI_CK_POL_NEG
},
62 /* Indicate sampling clock polarity, 1: Positive 0: Negative */
63 for (i
= 0; i
< ARRAY_SIZE(cali_data
); i
++) {
64 SET32_BITFIELDS(&mtk_spmi_mst
->mst_sampl
, SAMPL_CK_DLY
, cali_data
[i
].dly
,
65 SAMPL_CK_POL
, cali_data
[i
].pol
);
66 if (spmi_read_check(pmif_arb
, dev
->slvid
) == 0) {
73 die("ERROR - calibration fail for spmi clk");
78 static int spmi_mst_init(struct pmif
*pmif_arb
)
83 printk(BIOS_ERR
, "%s: null pointer for pmif dev.\n", __func__
);
87 if (!CONFIG(PMIF_SPMI_IOCFG_DEFAULT_SETTING
))
91 for (i
= 0; i
< spmi_dev_cnt
; i
++)
92 spmi_cali_rd_clock_polarity(pmif_arb
, &spmi_dev
[i
]);
97 static void pmif_spmi_force_normal_mode(struct pmif
*arb
)
99 /* listen srclken_0 only for entering normal or sleep mode */
100 SET32_BITFIELDS(&arb
->mtk_pmif
->spi_mode_ctrl
,
104 SPI_EINT_MODE_GATING_EN
, 1);
106 /* enable spm/scp sleep request */
107 SET32_BITFIELDS(&arb
->mtk_pmif
->sleep_protection_ctrl
, SPM_SLEEP_REQ_SEL
, 0,
108 SCP_SLEEP_REQ_SEL
, 0);
111 static void pmif_spmi_enable_swinf(struct pmif
*arb
)
113 write32(&arb
->mtk_pmif
->inf_en
, PMIF_SPMI_SW_CHAN
);
114 write32(&arb
->mtk_pmif
->arb_en
, PMIF_SPMI_SW_CHAN
);
117 static void pmif_spmi_enable_cmdIssue(struct pmif
*arb
, bool en
)
119 /* Enable cmdIssue */
120 write32(&arb
->mtk_pmif
->cmdissue_en
, en
);
123 static void pmif_spmi_enable(struct pmif
*arb
)
125 pmif_spmi_config(arb
);
128 * set bytecnt max limitation.
129 * hw bytecnt indicate when we set 0, it can send 1 byte;
130 * set 1, it can send 2 byte.
132 write32(&arb
->mtk_pmif
->inf_max_bytecnt_per_0
, 0);
133 write32(&arb
->mtk_pmif
->inf_max_bytecnt_per_1
, 0);
134 write32(&arb
->mtk_pmif
->inf_max_bytecnt_per_2
, 0);
135 write32(&arb
->mtk_pmif
->inf_max_bytecnt_per_3
, 0);
137 /* Add latency limitation */
138 write32(&arb
->mtk_pmif
->lat_cnter_en
, PMIF_SPMI_INF
);
139 write32(&arb
->mtk_pmif
->lat_limit_0
, 0);
140 write32(&arb
->mtk_pmif
->lat_limit_1
, 0x4);
141 write32(&arb
->mtk_pmif
->lat_limit_2
, 0x8);
142 write32(&arb
->mtk_pmif
->lat_limit_4
, 0x8);
143 write32(&arb
->mtk_pmif
->lat_limit_6
, 0x3FF);
144 write32(&arb
->mtk_pmif
->lat_limit_9
, 0x4);
145 write32(&arb
->mtk_pmif
->lat_limit_loading
, PMIF_SPMI_INF
);
147 write32(&arb
->mtk_pmif
->inf_en
, PMIF_SPMI_INF
);
148 write32(&arb
->mtk_pmif
->arb_en
, PMIF_SPMI_INF
);
149 write32(&arb
->mtk_pmif
->timer_ctrl
, 0x3);
150 write32(&arb
->mtk_pmif
->init_done
, 1);
153 int pmif_spmi_init(struct pmif
*arb
)
155 if (arb
->is_pmif_init_done(arb
) != 0) {
156 pmif_spmi_force_normal_mode(arb
);
157 pmif_spmi_enable_swinf(arb
);
158 pmif_spmi_enable_cmdIssue(arb
, true);
159 pmif_spmi_enable(arb
);
160 if (arb
->is_pmif_init_done(arb
))
164 if (spmi_mst_init(arb
)) {
165 printk(BIOS_ERR
, "[%s] failed to init spmi master\n", __func__
);