blob: c91ba18c4ab10e387195b3c9387483c3cc5a8b74 [file] [log] [blame]
Bhupesh Sharmafe6b5cc2024-04-03 14:07:37 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2023 Bhupesh Sharma <bhupesh.sharma@linaro.org>
4 *
5 * Based on Linux driver
6 */
7
8#include <dm.h>
9#include <generic-phy.h>
10#include <linux/bitops.h>
11#include <asm/io.h>
12#include <reset.h>
13#include <clk.h>
14#include <linux/delay.h>
15
16#include <dt-bindings/phy/phy-qcom-qusb2.h>
17
18#define QUSB2PHY_PLL 0x0
19#define QUSB2PHY_PLL_TEST 0x04
20#define CLK_REF_SEL BIT(7)
21
22#define QUSB2PHY_PLL_TUNE 0x08
23#define QUSB2PHY_PLL_USER_CTL1 0x0c
24#define QUSB2PHY_PLL_USER_CTL2 0x10
25#define QUSB2PHY_PLL_AUTOPGM_CTL1 0x1c
26#define QUSB2PHY_PLL_PWR_CTRL 0x18
27
28/* QUSB2PHY_PLL_STATUS register bits */
29#define PLL_LOCKED BIT(5)
30
31/* QUSB2PHY_PLL_COMMON_STATUS_ONE register bits */
32#define CORE_READY_STATUS BIT(0)
33
34/* QUSB2PHY_PORT_POWERDOWN register bits */
35#define CLAMP_N_EN BIT(5)
36#define FREEZIO_N BIT(1)
37#define POWER_DOWN BIT(0)
38
39/* QUSB2PHY_PWR_CTRL1 register bits */
40#define PWR_CTRL1_VREF_SUPPLY_TRIM BIT(5)
41#define PWR_CTRL1_CLAMP_N_EN BIT(1)
42
43#define QUSB2PHY_REFCLK_ENABLE BIT(0)
44
45#define PHY_CLK_SCHEME_SEL BIT(0)
46
47/* QUSB2PHY_INTR_CTRL register bits */
48#define DMSE_INTR_HIGH_SEL BIT(4)
49#define DPSE_INTR_HIGH_SEL BIT(3)
50#define CHG_DET_INTR_EN BIT(2)
51#define DMSE_INTR_EN BIT(1)
52#define DPSE_INTR_EN BIT(0)
53
54/* QUSB2PHY_PLL_CORE_INPUT_OVERRIDE register bits */
55#define CORE_PLL_EN_FROM_RESET BIT(4)
56#define CORE_RESET BIT(5)
57#define CORE_RESET_MUX BIT(6)
58
59/* QUSB2PHY_IMP_CTRL1 register bits */
60#define IMP_RES_OFFSET_MASK GENMASK(5, 0)
61#define IMP_RES_OFFSET_SHIFT 0x0
62
63/* QUSB2PHY_PLL_BIAS_CONTROL_2 register bits */
64#define BIAS_CTRL2_RES_OFFSET_MASK GENMASK(5, 0)
65#define BIAS_CTRL2_RES_OFFSET_SHIFT 0x0
66
67/* QUSB2PHY_CHG_CONTROL_2 register bits */
68#define CHG_CTRL2_OFFSET_MASK GENMASK(5, 4)
69#define CHG_CTRL2_OFFSET_SHIFT 0x4
70
71/* QUSB2PHY_PORT_TUNE1 register bits */
72#define HSTX_TRIM_MASK GENMASK(7, 4)
73#define HSTX_TRIM_SHIFT 0x4
74#define PREEMPH_WIDTH_HALF_BIT BIT(2)
75#define PREEMPHASIS_EN_MASK GENMASK(1, 0)
76#define PREEMPHASIS_EN_SHIFT 0x0
77
78/* QUSB2PHY_PORT_TUNE2 register bits */
79#define HSDISC_TRIM_MASK GENMASK(1, 0)
80#define HSDISC_TRIM_SHIFT 0x0
81
82#define QUSB2PHY_PLL_ANALOG_CONTROLS_TWO 0x04
83#define QUSB2PHY_PLL_CLOCK_INVERTERS 0x18c
84#define QUSB2PHY_PLL_CMODE 0x2c
85#define QUSB2PHY_PLL_LOCK_DELAY 0x184
86#define QUSB2PHY_PLL_DIGITAL_TIMERS_TWO 0xb4
87#define QUSB2PHY_PLL_BIAS_CONTROL_1 0x194
88#define QUSB2PHY_PLL_BIAS_CONTROL_2 0x198
89#define QUSB2PHY_PWR_CTRL2 0x214
90#define QUSB2PHY_IMP_CTRL1 0x220
91#define QUSB2PHY_IMP_CTRL2 0x224
92#define QUSB2PHY_CHG_CTRL2 0x23c
93
94struct qusb2_phy_init_tbl {
95 unsigned int offset;
96 unsigned int val;
97 /*
98 * register part of layout ?
99 * if yes, then offset gives index in the reg-layout
100 */
101 int in_layout;
102};
103
104struct qusb2_phy_cfg {
105 const struct qusb2_phy_init_tbl *tbl;
106 /* number of entries in the table */
107 unsigned int tbl_num;
108 /* offset to PHY_CLK_SCHEME register in TCSR map */
109 unsigned int clk_scheme_offset;
110
111 /* array of registers with different offsets */
112 const unsigned int *regs;
113 unsigned int mask_core_ready;
114 unsigned int disable_ctrl;
115 unsigned int autoresume_en;
116
117 /* true if PHY has PLL_TEST register to select clk_scheme */
118 bool has_pll_test;
119
120 /* true if TUNE1 register must be updated by fused value, else TUNE2 */
121 bool update_tune1_with_efuse;
122
123 /* true if PHY has PLL_CORE_INPUT_OVERRIDE register to reset PLL */
124 bool has_pll_override;
125};
126
127/* set of registers with offsets different per-PHY */
128enum qusb2phy_reg_layout {
129 QUSB2PHY_PLL_CORE_INPUT_OVERRIDE,
130 QUSB2PHY_PLL_STATUS,
131 QUSB2PHY_PORT_TUNE1,
132 QUSB2PHY_PORT_TUNE2,
133 QUSB2PHY_PORT_TUNE3,
134 QUSB2PHY_PORT_TUNE4,
135 QUSB2PHY_PORT_TUNE5,
136 QUSB2PHY_PORT_TEST1,
137 QUSB2PHY_PORT_TEST2,
138 QUSB2PHY_PORT_POWERDOWN,
139 QUSB2PHY_INTR_CTRL,
140};
141
142#define QUSB2_PHY_INIT_CFG(o, v) \
143 { \
144 .offset = o, .val = v, \
145 }
146
147#define QUSB2_PHY_INIT_CFG_L(o, v) \
148 { \
149 .offset = o, .val = v, .in_layout = 1, \
150 }
151
152static const struct qusb2_phy_init_tbl sm6115_init_tbl[] = {
153 QUSB2_PHY_INIT_CFG_L(QUSB2PHY_PORT_TUNE1, 0xf8),
154 QUSB2_PHY_INIT_CFG_L(QUSB2PHY_PORT_TUNE2, 0x53),
155 QUSB2_PHY_INIT_CFG_L(QUSB2PHY_PORT_TUNE3, 0x81),
156 QUSB2_PHY_INIT_CFG_L(QUSB2PHY_PORT_TUNE4, 0x17),
157
158 QUSB2_PHY_INIT_CFG(QUSB2PHY_PLL_TUNE, 0x30),
159 QUSB2_PHY_INIT_CFG(QUSB2PHY_PLL_USER_CTL1, 0x79),
160 QUSB2_PHY_INIT_CFG(QUSB2PHY_PLL_USER_CTL2, 0x21),
161
162 QUSB2_PHY_INIT_CFG_L(QUSB2PHY_PORT_TEST2, 0x14),
163
164 QUSB2_PHY_INIT_CFG(QUSB2PHY_PLL_AUTOPGM_CTL1, 0x9f),
165 QUSB2_PHY_INIT_CFG(QUSB2PHY_PLL_PWR_CTRL, 0x00),
166};
167
168static const unsigned int sm6115_regs_layout[] = {
169 [QUSB2PHY_PLL_STATUS] = 0x38, [QUSB2PHY_PORT_TUNE1] = 0x80,
170 [QUSB2PHY_PORT_TUNE2] = 0x84, [QUSB2PHY_PORT_TUNE3] = 0x88,
171 [QUSB2PHY_PORT_TUNE4] = 0x8c, [QUSB2PHY_PORT_TUNE5] = 0x90,
172 [QUSB2PHY_PORT_TEST1] = 0xb8, [QUSB2PHY_PORT_TEST2] = 0x9c,
173 [QUSB2PHY_PORT_POWERDOWN] = 0xb4, [QUSB2PHY_INTR_CTRL] = 0xbc,
174};
175
176static const struct qusb2_phy_init_tbl qusb2_v2_init_tbl[] = {
177 QUSB2_PHY_INIT_CFG(QUSB2PHY_PLL_ANALOG_CONTROLS_TWO, 0x03),
178 QUSB2_PHY_INIT_CFG(QUSB2PHY_PLL_CLOCK_INVERTERS, 0x7c),
179 QUSB2_PHY_INIT_CFG(QUSB2PHY_PLL_CMODE, 0x80),
180 QUSB2_PHY_INIT_CFG(QUSB2PHY_PLL_LOCK_DELAY, 0x0a),
181 QUSB2_PHY_INIT_CFG(QUSB2PHY_PLL_DIGITAL_TIMERS_TWO, 0x19),
182 QUSB2_PHY_INIT_CFG(QUSB2PHY_PLL_BIAS_CONTROL_1, 0x40),
183 QUSB2_PHY_INIT_CFG(QUSB2PHY_PLL_BIAS_CONTROL_2, 0x20),
184 QUSB2_PHY_INIT_CFG(QUSB2PHY_PWR_CTRL2, 0x21),
185 QUSB2_PHY_INIT_CFG(QUSB2PHY_IMP_CTRL1, 0x0),
186 QUSB2_PHY_INIT_CFG(QUSB2PHY_IMP_CTRL2, 0x58),
187
188 QUSB2_PHY_INIT_CFG_L(QUSB2PHY_PORT_TUNE1, 0x30),
189 QUSB2_PHY_INIT_CFG_L(QUSB2PHY_PORT_TUNE2, 0x29),
190 QUSB2_PHY_INIT_CFG_L(QUSB2PHY_PORT_TUNE3, 0xca),
191 QUSB2_PHY_INIT_CFG_L(QUSB2PHY_PORT_TUNE4, 0x04),
192 QUSB2_PHY_INIT_CFG_L(QUSB2PHY_PORT_TUNE5, 0x03),
193
194 QUSB2_PHY_INIT_CFG(QUSB2PHY_CHG_CTRL2, 0x0),
195};
196
197static const unsigned int qusb2_v2_regs_layout[] = {
198 [QUSB2PHY_PLL_CORE_INPUT_OVERRIDE] = 0xa8,
199 [QUSB2PHY_PLL_STATUS] = 0x1a0,
200 [QUSB2PHY_PORT_TUNE1] = 0x240,
201 [QUSB2PHY_PORT_TUNE2] = 0x244,
202 [QUSB2PHY_PORT_TUNE3] = 0x248,
203 [QUSB2PHY_PORT_TUNE4] = 0x24c,
204 [QUSB2PHY_PORT_TUNE5] = 0x250,
205 [QUSB2PHY_PORT_TEST1] = 0x254,
206 [QUSB2PHY_PORT_TEST2] = 0x258,
207 [QUSB2PHY_PORT_POWERDOWN] = 0x210,
208 [QUSB2PHY_INTR_CTRL] = 0x230,
209};
210
211static const struct qusb2_phy_cfg sm6115_phy_cfg = {
212 .tbl = sm6115_init_tbl,
213 .tbl_num = ARRAY_SIZE(sm6115_init_tbl),
214 .regs = sm6115_regs_layout,
215
216 .has_pll_test = true,
217 .disable_ctrl = (CLAMP_N_EN | FREEZIO_N | POWER_DOWN),
218 .mask_core_ready = PLL_LOCKED,
219 .autoresume_en = BIT(3),
220};
221
222static const struct qusb2_phy_cfg qusb2_v2_phy_cfg = {
223 .tbl = qusb2_v2_init_tbl,
224 .tbl_num = ARRAY_SIZE(qusb2_v2_init_tbl),
225 .regs = qusb2_v2_regs_layout,
226
227 .disable_ctrl = (PWR_CTRL1_VREF_SUPPLY_TRIM | PWR_CTRL1_CLAMP_N_EN |
228 POWER_DOWN),
229 .mask_core_ready = CORE_READY_STATUS,
230 .has_pll_override = true,
231 .autoresume_en = BIT(0),
232 .update_tune1_with_efuse = true,
233};
234
235/**
236 * struct qusb2_phy - structure holding qusb2 phy attributes
237 *
238 * @phy: generic phy
239 * @base: iomapped memory space for qubs2 phy
240 *
241 * @cfg_ahb_clk: AHB2PHY interface clock
242 * @phy_rst: phy reset control
243 *
244 * @cfg: phy config data
245 * @has_se_clk_scheme: indicate if PHY has single-ended ref clock scheme
246 */
247struct qusb2_phy {
248 struct phy *phy;
249 void __iomem *base;
250
251 struct clk cfg_ahb_clk;
252 struct reset_ctl phy_rst;
253
254 const struct qusb2_phy_cfg *cfg;
255 bool has_se_clk_scheme;
256};
257
258static inline void qusb2_phy_configure(void __iomem *base,
259 const unsigned int *regs,
260 const struct qusb2_phy_init_tbl tbl[],
261 int num)
262{
263 int i;
264
265 for (i = 0; i < num; i++) {
266 if (tbl[i].in_layout)
267 writel(tbl[i].val, base + regs[tbl[i].offset]);
268 else
269 writel(tbl[i].val, base + tbl[i].offset);
270 }
271}
272
273static int qusb2phy_do_reset(struct qusb2_phy *qphy)
274{
275 int ret;
276
277 ret = reset_assert(&qphy->phy_rst);
278 if (ret)
279 return ret;
280
281 udelay(500);
282
283 ret = reset_deassert(&qphy->phy_rst);
284 if (ret)
285 return ret;
286
287 return 0;
288}
289
290static int qusb2phy_power_on(struct phy *phy)
291{
292 struct qusb2_phy *qphy = dev_get_priv(phy->dev);
293 const struct qusb2_phy_cfg *cfg = qphy->cfg;
294 int ret;
295 u32 val;
296
297 ret = qusb2phy_do_reset(qphy);
298 if (ret)
299 return ret;
300
301 /* Disable the PHY */
302 setbits_le32(qphy->base + cfg->regs[QUSB2PHY_PORT_POWERDOWN],
303 qphy->cfg->disable_ctrl);
304
305 if (cfg->has_pll_test) {
306 /* save reset value to override reference clock scheme later */
307 val = readl(qphy->base + QUSB2PHY_PLL_TEST);
308 }
309
310 qusb2_phy_configure(qphy->base, cfg->regs, cfg->tbl, cfg->tbl_num);
311
312 /* Enable the PHY */
313 clrbits_le32(qphy->base + cfg->regs[QUSB2PHY_PORT_POWERDOWN],
314 POWER_DOWN);
315
316 /* Required to get phy pll lock successfully */
317 udelay(150);
318
319 if (cfg->has_pll_test) {
320 val |= CLK_REF_SEL;
321
322 writel(val, qphy->base + QUSB2PHY_PLL_TEST);
323
324 /* ensure above write is through */
325 readl(qphy->base + QUSB2PHY_PLL_TEST);
326 }
327
328 /* Required to get phy pll lock successfully */
329 udelay(100);
330
331 val = readb(qphy->base + cfg->regs[QUSB2PHY_PLL_STATUS]);
332 if (!(val & cfg->mask_core_ready)) {
333 pr_err("QUSB2PHY pll lock failed: status reg = %x\n", val);
334 ret = -EBUSY;
335 return ret;
336 }
337
338 return 0;
339}
340
341static int qusb2phy_power_off(struct phy *phy)
342{
343 struct qusb2_phy *qphy = dev_get_priv(phy->dev);
344
345 /* Disable the PHY */
346 setbits_le32(qphy->base + qphy->cfg->regs[QUSB2PHY_PORT_POWERDOWN],
347 qphy->cfg->disable_ctrl);
348
349 reset_assert(&qphy->phy_rst);
350
351 clk_disable(&qphy->cfg_ahb_clk);
352
353 return 0;
354}
355
356static int qusb2phy_clk_init(struct udevice *dev, struct qusb2_phy *qphy)
357{
358 int ret;
359
360 /* We ignore the ref clock as we currently lack a driver for rpmcc/rpmhcc where
361 * it usually comes from - we assume it's always on.
362 */
363 ret = clk_get_by_name(dev, "cfg_ahb", &qphy->cfg_ahb_clk);
364 if (ret == -ENOSYS || ret == -ENOENT)
365 return 0;
366 if (ret)
367 return ret;
368
369 ret = clk_enable(&qphy->cfg_ahb_clk);
370 if (ret)
371 return ret;
372
373 return 0;
374}
375
376static int qusb2phy_probe(struct udevice *dev)
377{
378 struct qusb2_phy *qphy = dev_get_priv(dev);
379 int ret;
380
381 qphy->base = (void __iomem *)dev_read_addr(dev);
382 if (IS_ERR(qphy->base))
383 return PTR_ERR(qphy->base);
384
385 ret = qusb2phy_clk_init(dev, qphy);
386 if (ret) {
387 printf("%s: Couldn't get clocks: %d\n", __func__, ret);
388 return ret;
389 }
390
391 ret = reset_get_by_index(dev, 0, &qphy->phy_rst);
392 if (ret) {
393 printf("%s: Couldn't get resets: %d\n", __func__, ret);
394 return ret;
395 }
396
397 qphy->cfg = (const struct qusb2_phy_cfg *)dev_get_driver_data(dev);
398 if (!qphy->cfg) {
399 printf("%s: Couldn't get driver data\n", __func__);
400 return -EINVAL;
401 }
402
403 debug("%s success qusb phy cfg %p\n", __func__, qphy->cfg);
404 return 0;
405}
406
407static struct phy_ops qusb2phy_ops = {
408 .power_on = qusb2phy_power_on,
409 .power_off = qusb2phy_power_off,
410};
411
412static const struct udevice_id qusb2phy_ids[] = {
413 { .compatible = "qcom,qusb2-phy" },
414 { .compatible = "qcom,qcm2290-qusb2-phy",
415 .data = (ulong)&sm6115_phy_cfg },
416 { .compatible = "qcom,sm6115-qusb2-phy",
417 .data = (ulong)&sm6115_phy_cfg },
418 { .compatible = "qcom,qusb2-v2-phy", .data = (ulong)&qusb2_v2_phy_cfg },
419 {}
420};
421
422U_BOOT_DRIVER(qcom_qusb2_phy) = {
423 .name = "qcom-qusb2-phy",
424 .id = UCLASS_PHY,
425 .of_match = qusb2phy_ids,
426 .ops = &qusb2phy_ops,
427 .probe = qusb2phy_probe,
428 .priv_auto = sizeof(struct qusb2_phy),
429};