drivers/mipi: Add support for KD_KD110N11_51IE panel
[coreboot2.git] / src / soc / mediatek / common / pmif_spmi.c
blob7263ff2fa3c6176d136020c77a707716eaf9eda1
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <console/console.h>
4 #include <device/mmio.h>
5 #include <soc/addressmap.h>
6 #include <soc/pmif.h>
7 #include <soc/pmif_spmi.h>
8 #include <soc/pmif_sw.h>
9 #include <soc/spmi.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)
27 /* Do nothing. */
30 static int spmi_read_check(struct pmif *pmif_arb, int slvid)
32 u32 rdata = 0;
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);
38 return -E_NODEV;
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);
45 return -E_NODEV;
48 return 0;
51 static int spmi_cali_rd_clock_polarity(struct pmif *pmif_arb, const struct spmi_device *dev)
53 int i;
54 bool success = false;
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) {
67 success = true;
68 break;
72 if (!success)
73 die("ERROR - calibration fail for spmi clk");
75 return 0;
78 static int spmi_mst_init(struct pmif *pmif_arb)
80 size_t i;
82 if (!pmif_arb) {
83 printk(BIOS_ERR, "%s: null pointer for pmif dev.\n", __func__);
84 return -E_INVAL;
87 if (!CONFIG(PMIF_SPMI_IOCFG_DEFAULT_SETTING))
88 pmif_spmi_iocfg();
89 spmi_config_master();
91 for (i = 0; i < spmi_dev_cnt; i++)
92 spmi_cali_rd_clock_polarity(pmif_arb, &spmi_dev[i]);
94 return 0;
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,
101 SPI_MODE_CTRL, 0,
102 SRVOL_EN, 0,
103 SPI_MODE_EXT_CMD, 1,
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))
161 return -E_NODEV;
164 if (spmi_mst_init(arb)) {
165 printk(BIOS_ERR, "[%s] failed to init spmi master\n", __func__);
166 return -E_NODEV;
169 return 0;