c730fb1eec0a40d8d2745f34f9f7df30b556749f
[openwrt/openwrt.git] / target / linux / mediatek / patches-4.14 / 0119-soc-mediatek-pwrap-refactor-pwrap_init-for-the-vario.patch
1 From 16bebe4ad52083316907fb7149c797cd331f5948 Mon Sep 17 00:00:00 2001
2 From: Sean Wang <sean.wang@mediatek.com>
3 Date: Wed, 18 Oct 2017 16:28:45 +0800
4 Subject: [PATCH 119/224] soc: mediatek: pwrap: refactor pwrap_init for the
5 various PMIC types
6
7 pwrap initialization is highly associated with the base SoC and the
8 target PMICs, so slight refactorization is made here for allowing
9 pwrap_init to run on those PMICs with different capability from the
10 previous MediaTek PMICs and the determination for the enablement of the
11 pwrap capability depending on PMIC type. Apart from this, the patch
12 makes the driver more extensible especially when more PMICs join into
13 the pwrap driver.
14
15 Signed-off-by: Chenglin Xu <chenglin.xu@mediatek.com>
16 Signed-off-by: Sean Wang <sean.wang@mediatek.com>
17 Signed-off-by: Matthias Brugger <matthias.bgg@gmail.com>
18 ---
19 drivers/soc/mediatek/mtk-pmic-wrap.c | 130 ++++++++++++++++++++++++-----------
20 1 file changed, 90 insertions(+), 40 deletions(-)
21
22 diff --git a/drivers/soc/mediatek/mtk-pmic-wrap.c b/drivers/soc/mediatek/mtk-pmic-wrap.c
23 index 2d3a8faae124..e3398e37a7a6 100644
24 --- a/drivers/soc/mediatek/mtk-pmic-wrap.c
25 +++ b/drivers/soc/mediatek/mtk-pmic-wrap.c
26 @@ -70,6 +70,12 @@
27 PWRAP_WDT_SRC_EN_HARB_STAUPD_DLE | \
28 PWRAP_WDT_SRC_EN_HARB_STAUPD_ALE)
29
30 +/* Group of bits used for shown slave capability */
31 +#define PWRAP_SLV_CAP_SPI BIT(0)
32 +#define PWRAP_SLV_CAP_DUALIO BIT(1)
33 +#define PWRAP_SLV_CAP_SECURITY BIT(2)
34 +#define HAS_CAP(_c, _x) (((_c) & (_x)) == (_x))
35 +
36 /* defines for slave device wrapper registers */
37 enum dew_regs {
38 PWRAP_DEW_BASE,
39 @@ -501,6 +507,8 @@ struct pmic_wrapper;
40 struct pwrap_slv_type {
41 const u32 *dew_regs;
42 enum pmic_type type;
43 + /* Flags indicating the capability for the target slave */
44 + u32 caps;
45 /*
46 * pwrap operations are highly associated with the PMIC types,
47 * so the pointers added increases flexibility allowing determination
48 @@ -787,6 +795,37 @@ static int pwrap_init_sidly(struct pmic_wrapper *wrp)
49 return 0;
50 }
51
52 +static int pwrap_init_dual_io(struct pmic_wrapper *wrp)
53 +{
54 + int ret;
55 + u32 rdata;
56 +
57 + /* Enable dual IO mode */
58 + pwrap_write(wrp, wrp->slave->dew_regs[PWRAP_DEW_DIO_EN], 1);
59 +
60 + /* Check IDLE & INIT_DONE in advance */
61 + ret = pwrap_wait_for_state(wrp,
62 + pwrap_is_fsm_idle_and_sync_idle);
63 + if (ret) {
64 + dev_err(wrp->dev, "%s fail, ret=%d\n", __func__, ret);
65 + return ret;
66 + }
67 +
68 + pwrap_writel(wrp, 1, PWRAP_DIO_EN);
69 +
70 + /* Read Test */
71 + pwrap_read(wrp,
72 + wrp->slave->dew_regs[PWRAP_DEW_READ_TEST], &rdata);
73 + if (rdata != PWRAP_DEW_READ_TEST_VAL) {
74 + dev_err(wrp->dev,
75 + "Read failed on DIO mode: 0x%04x!=0x%04x\n",
76 + PWRAP_DEW_READ_TEST_VAL, rdata);
77 + return -EFAULT;
78 + }
79 +
80 + return 0;
81 +}
82 +
83 static int pwrap_mt8135_init_reg_clock(struct pmic_wrapper *wrp)
84 {
85 pwrap_writel(wrp, 0x4, PWRAP_CSHEXT);
86 @@ -935,6 +974,30 @@ static int pwrap_init_cipher(struct pmic_wrapper *wrp)
87 return 0;
88 }
89
90 +static int pwrap_init_security(struct pmic_wrapper *wrp)
91 +{
92 + int ret;
93 +
94 + /* Enable encryption */
95 + ret = pwrap_init_cipher(wrp);
96 + if (ret)
97 + return ret;
98 +
99 + /* Signature checking - using CRC */
100 + if (pwrap_write(wrp,
101 + wrp->slave->dew_regs[PWRAP_DEW_CRC_EN], 0x1))
102 + return -EFAULT;
103 +
104 + pwrap_writel(wrp, 0x1, PWRAP_CRC_EN);
105 + pwrap_writel(wrp, 0x0, PWRAP_SIG_MODE);
106 + pwrap_writel(wrp, wrp->slave->dew_regs[PWRAP_DEW_CRC_VAL],
107 + PWRAP_SIG_ADR);
108 + pwrap_writel(wrp,
109 + wrp->master->arb_en_all, PWRAP_HIPRIO_ARB_EN);
110 +
111 + return 0;
112 +}
113 +
114 static int pwrap_mt8135_init_soc_specific(struct pmic_wrapper *wrp)
115 {
116 /* enable pwrap events and pwrap bridge in AP side */
117 @@ -995,7 +1058,6 @@ static int pwrap_mt2701_init_soc_specific(struct pmic_wrapper *wrp)
118 static int pwrap_init(struct pmic_wrapper *wrp)
119 {
120 int ret;
121 - u32 rdata;
122
123 reset_control_reset(wrp->rstc);
124 if (wrp->rstc_bridge)
125 @@ -1007,10 +1069,12 @@ static int pwrap_init(struct pmic_wrapper *wrp)
126 pwrap_writel(wrp, 0, PWRAP_DCM_DBC_PRD);
127 }
128
129 - /* Reset SPI slave */
130 - ret = pwrap_reset_spislave(wrp);
131 - if (ret)
132 - return ret;
133 + if (HAS_CAP(wrp->slave->caps, PWRAP_SLV_CAP_SPI)) {
134 + /* Reset SPI slave */
135 + ret = pwrap_reset_spislave(wrp);
136 + if (ret)
137 + return ret;
138 + }
139
140 pwrap_writel(wrp, 1, PWRAP_WRAP_EN);
141
142 @@ -1022,45 +1086,26 @@ static int pwrap_init(struct pmic_wrapper *wrp)
143 if (ret)
144 return ret;
145
146 - /* Setup serial input delay */
147 - ret = pwrap_init_sidly(wrp);
148 - if (ret)
149 - return ret;
150 -
151 - /* Enable dual IO mode */
152 - pwrap_write(wrp, wrp->slave->dew_regs[PWRAP_DEW_DIO_EN], 1);
153 -
154 - /* Check IDLE & INIT_DONE in advance */
155 - ret = pwrap_wait_for_state(wrp, pwrap_is_fsm_idle_and_sync_idle);
156 - if (ret) {
157 - dev_err(wrp->dev, "%s fail, ret=%d\n", __func__, ret);
158 - return ret;
159 + if (HAS_CAP(wrp->slave->caps, PWRAP_SLV_CAP_SPI)) {
160 + /* Setup serial input delay */
161 + ret = pwrap_init_sidly(wrp);
162 + if (ret)
163 + return ret;
164 }
165
166 - pwrap_writel(wrp, 1, PWRAP_DIO_EN);
167 -
168 - /* Read Test */
169 - pwrap_read(wrp, wrp->slave->dew_regs[PWRAP_DEW_READ_TEST], &rdata);
170 - if (rdata != PWRAP_DEW_READ_TEST_VAL) {
171 - dev_err(wrp->dev, "Read test failed after switch to DIO mode: 0x%04x != 0x%04x\n",
172 - PWRAP_DEW_READ_TEST_VAL, rdata);
173 - return -EFAULT;
174 + if (HAS_CAP(wrp->slave->caps, PWRAP_SLV_CAP_DUALIO)) {
175 + /* Enable dual I/O mode */
176 + ret = pwrap_init_dual_io(wrp);
177 + if (ret)
178 + return ret;
179 }
180
181 - /* Enable encryption */
182 - ret = pwrap_init_cipher(wrp);
183 - if (ret)
184 - return ret;
185 -
186 - /* Signature checking - using CRC */
187 - if (pwrap_write(wrp, wrp->slave->dew_regs[PWRAP_DEW_CRC_EN], 0x1))
188 - return -EFAULT;
189 -
190 - pwrap_writel(wrp, 0x1, PWRAP_CRC_EN);
191 - pwrap_writel(wrp, 0x0, PWRAP_SIG_MODE);
192 - pwrap_writel(wrp, wrp->slave->dew_regs[PWRAP_DEW_CRC_VAL],
193 - PWRAP_SIG_ADR);
194 - pwrap_writel(wrp, wrp->master->arb_en_all, PWRAP_HIPRIO_ARB_EN);
195 + if (HAS_CAP(wrp->slave->caps, PWRAP_SLV_CAP_SECURITY)) {
196 + /* Enable security on bus */
197 + ret = pwrap_init_security(wrp);
198 + if (ret)
199 + return ret;
200 + }
201
202 if (wrp->master->type == PWRAP_MT8135)
203 pwrap_writel(wrp, 0x7, PWRAP_RRARB_EN);
204 @@ -1116,6 +1161,8 @@ static const struct regmap_config pwrap_regmap_config = {
205 static const struct pwrap_slv_type pmic_mt6323 = {
206 .dew_regs = mt6323_regs,
207 .type = PMIC_MT6323,
208 + .caps = PWRAP_SLV_CAP_SPI | PWRAP_SLV_CAP_DUALIO |
209 + PWRAP_SLV_CAP_SECURITY,
210 .pwrap_read = pwrap_read16,
211 .pwrap_write = pwrap_write16,
212 };
213 @@ -1123,6 +1170,7 @@ static const struct pwrap_slv_type pmic_mt6323 = {
214 static const struct pwrap_slv_type pmic_mt6380 = {
215 .dew_regs = NULL,
216 .type = PMIC_MT6380,
217 + .caps = 0,
218 .pwrap_read = pwrap_read32,
219 .pwrap_write = pwrap_write32,
220 };
221 @@ -1130,6 +1178,8 @@ static const struct pwrap_slv_type pmic_mt6380 = {
222 static const struct pwrap_slv_type pmic_mt6397 = {
223 .dew_regs = mt6397_regs,
224 .type = PMIC_MT6397,
225 + .caps = PWRAP_SLV_CAP_SPI | PWRAP_SLV_CAP_DUALIO |
226 + PWRAP_SLV_CAP_SECURITY,
227 .pwrap_read = pwrap_read16,
228 .pwrap_write = pwrap_write16,
229 };
230 --
231 2.11.0
232