1 // SPDX-License-Identifier: GPL-2.0
3 * PCIe phy driver for Kirin 970
5 * Copyright (C) 2017 HiSilicon Electronics Co., Ltd.
6 * https://www.huawei.com
7 * Copyright (C) 2021 Huawei Technologies Co., Ltd.
8 * https://www.huawei.com
15 * https://lore.kernel.org/lkml/4c9d6581478aa966698758c0420933f5defab4dd.1612335031.git.mchehab+huawei@kernel.org/
18 #include <linux/bitfield.h>
19 #include <linux/bits.h>
20 #include <linux/clk.h>
21 #include <linux/delay.h>
22 #include <linux/device.h>
23 #include <linux/err.h>
25 #include <linux/mfd/syscon.h>
26 #include <linux/mod_devicetable.h>
27 #include <linux/module.h>
29 #include <linux/phy/phy.h>
30 #include <linux/platform_device.h>
31 #include <linux/regmap.h>
32 #include <linux/types.h>
34 #define AXI_CLK_FREQ 207500000
35 #define REF_CLK_FREQ 100000000
37 /* PCIe CTRL registers */
38 #define SOC_PCIECTRL_CTRL7_ADDR 0x01c
39 #define SOC_PCIECTRL_CTRL12_ADDR 0x030
40 #define SOC_PCIECTRL_CTRL20_ADDR 0x050
41 #define SOC_PCIECTRL_CTRL21_ADDR 0x054
43 #define PCIE_OUTPUT_PULL_BITS GENMASK(3, 0)
44 #define SOC_PCIECTRL_CTRL20_2P_MEM_CTRL 0x02605550
45 #define SOC_PCIECTRL_CTRL21_DEFAULT 0x20000070
46 #define PCIE_PULL_UP_SYS_AUX_PWR_DET BIT(10)
47 #define PCIE_OUTPUT_PULL_DOWN BIT(1)
49 /* PCIe PHY registers */
50 #define SOC_PCIEPHY_CTRL0_ADDR 0x000
51 #define SOC_PCIEPHY_CTRL1_ADDR 0x004
52 #define SOC_PCIEPHY_CTRL38_ADDR 0x0098
53 #define SOC_PCIEPHY_STATE0_ADDR 0x400
55 #define RAWLANEN_DIG_PCS_XF_TX_OVRD_IN_1 0xc004
56 #define SUP_DIG_LVL_OVRD_IN 0x003c
57 #define LANEN_DIG_ASIC_TX_OVRD_IN_1 0x4008
58 #define LANEN_DIG_ASIC_TX_OVRD_IN_2 0x400c
60 #define PCIEPHY_RESET_BIT BIT(17)
61 #define PCIEPHY_PIPE_LINE0_RESET_BIT BIT(19)
62 #define PCIE_TXDETECT_RX_FAIL BIT(2)
63 #define PCIE_CLK_SOURCE BIT(8)
64 #define PCIE_IS_CLOCK_STABLE BIT(19)
65 #define PCIE_PULL_DOWN_PHY_TEST_POWERDOWN BIT(22)
66 #define PCIE_DEASSERT_CONTROLLER_PERST BIT(2)
68 #define EYEPARAM_NOCFG 0xffffffff
69 #define EYE_PARM0_MASK GENMASK(8, 6)
70 #define EYE_PARM1_MASK GENMASK(11, 8)
71 #define EYE_PARM2_MASK GENMASK(5, 0)
72 #define EYE_PARM3_MASK GENMASK(12, 7)
73 #define EYE_PARM4_MASK GENMASK(14, 9)
74 #define EYE_PARM0_EN BIT(9)
75 #define EYE_PARM1_EN BIT(12)
76 #define EYE_PARM2_EN BIT(6)
77 #define EYE_PARM3_EN BIT(13)
78 #define EYE_PARM4_EN BIT(15)
80 /* hi3670 pciephy register */
81 #define APB_PHY_START_ADDR 0x40000
82 #define SOC_PCIEPHY_MMC1PLL_CTRL1 0xc04
83 #define SOC_PCIEPHY_MMC1PLL_CTRL16 0xC40
84 #define SOC_PCIEPHY_MMC1PLL_CTRL17 0xC44
85 #define SOC_PCIEPHY_MMC1PLL_CTRL20 0xC50
86 #define SOC_PCIEPHY_MMC1PLL_CTRL21 0xC54
87 #define SOC_PCIEPHY_MMC1PLL_STAT0 0xE00
89 #define CRGPERIPH_PEREN12 0x470
90 #define CRGPERIPH_PERDIS12 0x474
91 #define CRGPERIPH_PCIECTRL0 0x800
93 #define PCIE_FNPLL_FBDIV_MASK GENMASK(27, 16)
94 #define PCIE_FNPLL_FRACDIV_MASK GENMASK(23, 0)
95 #define PCIE_FNPLL_POSTDIV1_MASK GENMASK(10, 8)
96 #define PCIE_FNPLL_POSTDIV2_MASK GENMASK(14, 12)
97 #define PCIE_FNPLL_PLL_MODE_MASK BIT(25)
99 #define PCIE_FNPLL_DLL_EN BIT(27)
100 #define PCIE_FNPLL_FBDIV 0xd0
101 #define PCIE_FNPLL_FRACDIV 0x555555
102 #define PCIE_FNPLL_POSTDIV1 0x5
103 #define PCIE_FNPLL_POSTDIV2 0x4
104 #define PCIE_FNPLL_PLL_MODE 0x0
106 #define PCIE_PHY_MMC1PLL 0x20
107 #define PCIE_PHY_CHOOSE_FNPLL BIT(27)
108 #define PCIE_PHY_MMC1PLL_DISABLE BIT(0)
109 #define PCIE_PHY_PCIEPL_BP BIT(16)
111 /* define ie,oe cfg */
112 #define IO_OE_HARD_GT_MODE BIT(1)
113 #define IO_IE_EN_HARD_BYPASS BIT(27)
114 #define IO_OE_EN_HARD_BYPASS BIT(11)
115 #define IO_HARD_CTRL_DEBOUNCE_BYPASS BIT(10)
116 #define IO_OE_GT_MODE BIT(8)
117 #define DEBOUNCE_WAITCFG_IN GENMASK(23, 20)
118 #define DEBOUNCE_WAITCFG_OUT GENMASK(16, 13)
120 #define IO_HP_DEBOUNCE_GT (BIT(12) | BIT(15))
121 #define IO_PHYREF_SOFT_GT_MODE BIT(14)
122 #define IO_REF_SOFT_GT_MODE BIT(13)
123 #define IO_REF_HARD_GT_MODE BIT(0)
125 /* noc power domain */
126 #define NOC_POWER_IDLEREQ_1 0x38c
127 #define NOC_POWER_IDLE_1 0x394
128 #define NOC_PW_MASK 0x10000
129 #define NOC_PW_SET_BIT 0x1
131 #define NUM_EYEPARAM 5
133 /* info located in sysctrl */
134 #define SCTRL_PCIE_CMOS_OFFSET 0x60
135 #define SCTRL_PCIE_CMOS_BIT 0x10
136 #define SCTRL_PCIE_ISO_OFFSET 0x44
137 #define SCTRL_PCIE_ISO_BIT 0x30
138 #define SCTRL_PCIE_HPCLK_OFFSET 0x190
139 #define SCTRL_PCIE_HPCLK_BIT 0x184000
140 #define SCTRL_PCIE_OE_OFFSET 0x14a
141 #define PCIE_DEBOUNCE_PARAM 0xf0f400
142 #define PCIE_OE_BYPASS GENMASK(29, 28)
145 #define CRGCTRL_PCIE_ASSERT_OFFSET 0x88
146 #define CRGCTRL_PCIE_ASSERT_BIT 0x8c000000
148 #define FNPLL_HAS_LOCKED BIT(4)
151 #define TIME_CMOS_MIN 100
152 #define TIME_CMOS_MAX 105
153 #define PIPE_CLK_STABLE_TIME 100
154 #define PLL_CTRL_WAIT_TIME 200
155 #define NOC_POWER_TIME 100
157 struct hi3670_pcie_phy {
161 struct regmap *crgctrl;
162 struct regmap *sysctrl;
163 struct regmap *pmctrl;
164 struct clk *apb_sys_clk;
165 struct clk *apb_phy_clk;
166 struct clk *phy_ref_clk;
169 u32 eye_param[NUM_EYEPARAM];
172 /* Registers in PCIePHY */
173 static inline void hi3670_apb_phy_writel(struct hi3670_pcie_phy *phy, u32 val,
176 writel(val, phy->base + APB_PHY_START_ADDR + reg);
179 static inline u32 hi3670_apb_phy_readl(struct hi3670_pcie_phy *phy, u32 reg)
181 return readl(phy->base + APB_PHY_START_ADDR + reg);
184 static inline void hi3670_apb_phy_updatel(struct hi3670_pcie_phy *phy,
185 u32 val, u32 mask, u32 reg)
189 regval = hi3670_apb_phy_readl(phy, reg);
192 hi3670_apb_phy_writel(phy, regval, reg);
195 static inline void kirin_apb_natural_phy_writel(struct hi3670_pcie_phy *phy,
198 writel(val, phy->base + reg);
201 static inline u32 kirin_apb_natural_phy_readl(struct hi3670_pcie_phy *phy,
204 return readl(phy->base + reg);
207 static void hi3670_pcie_phy_oe_enable(struct hi3670_pcie_phy *phy, bool enable)
211 regmap_read(phy->sysctrl, SCTRL_PCIE_OE_OFFSET, &val);
212 val |= PCIE_DEBOUNCE_PARAM;
214 val &= ~PCIE_OE_BYPASS;
216 val |= PCIE_OE_BYPASS;
217 regmap_write(phy->sysctrl, SCTRL_PCIE_OE_OFFSET, val);
220 static void hi3670_pcie_get_eyeparam(struct hi3670_pcie_phy *phy)
222 struct device *dev = phy->dev;
223 struct device_node *np;
228 ret = of_property_read_u32_array(np, "hisilicon,eye-diagram-param",
229 phy->eye_param, NUM_EYEPARAM);
233 /* There's no optional eye_param property. Set array to default */
234 for (i = 0; i < NUM_EYEPARAM; i++)
235 phy->eye_param[i] = EYEPARAM_NOCFG;
238 static void hi3670_pcie_set_eyeparam(struct hi3670_pcie_phy *phy)
242 val = kirin_apb_natural_phy_readl(phy, RAWLANEN_DIG_PCS_XF_TX_OVRD_IN_1);
244 if (phy->eye_param[1] != EYEPARAM_NOCFG) {
245 val &= ~EYE_PARM1_MASK;
246 val |= FIELD_PREP(EYE_PARM1_MASK, phy->eye_param[1]);
249 kirin_apb_natural_phy_writel(phy, val,
250 RAWLANEN_DIG_PCS_XF_TX_OVRD_IN_1);
252 val = kirin_apb_natural_phy_readl(phy, LANEN_DIG_ASIC_TX_OVRD_IN_2);
253 val &= ~(EYE_PARM2_MASK | EYE_PARM3_MASK);
254 if (phy->eye_param[2] != EYEPARAM_NOCFG) {
255 val |= FIELD_PREP(EYE_PARM2_MASK, phy->eye_param[2]);
259 if (phy->eye_param[3] != EYEPARAM_NOCFG) {
260 val |= FIELD_PREP(EYE_PARM3_MASK, phy->eye_param[3]);
264 kirin_apb_natural_phy_writel(phy, val, LANEN_DIG_ASIC_TX_OVRD_IN_2);
266 val = kirin_apb_natural_phy_readl(phy, SUP_DIG_LVL_OVRD_IN);
267 if (phy->eye_param[0] != EYEPARAM_NOCFG) {
268 val &= ~EYE_PARM0_MASK;
269 val |= FIELD_PREP(EYE_PARM0_MASK, phy->eye_param[0]);
272 kirin_apb_natural_phy_writel(phy, val, SUP_DIG_LVL_OVRD_IN);
274 val = kirin_apb_natural_phy_readl(phy, LANEN_DIG_ASIC_TX_OVRD_IN_1);
275 if (phy->eye_param[4] != EYEPARAM_NOCFG) {
276 val &= ~EYE_PARM4_MASK;
277 val |= FIELD_PREP(EYE_PARM4_MASK, phy->eye_param[4]);
280 kirin_apb_natural_phy_writel(phy, val, LANEN_DIG_ASIC_TX_OVRD_IN_1);
283 static void hi3670_pcie_natural_cfg(struct hi3670_pcie_phy *phy)
287 /* change 2p mem_ctrl */
288 regmap_write(phy->apb, SOC_PCIECTRL_CTRL20_ADDR,
289 SOC_PCIECTRL_CTRL20_2P_MEM_CTRL);
291 regmap_read(phy->apb, SOC_PCIECTRL_CTRL7_ADDR, &val);
292 val |= PCIE_PULL_UP_SYS_AUX_PWR_DET;
293 regmap_write(phy->apb, SOC_PCIECTRL_CTRL7_ADDR, val);
295 /* output, pull down */
296 regmap_read(phy->apb, SOC_PCIECTRL_CTRL12_ADDR, &val);
297 val &= ~PCIE_OUTPUT_PULL_BITS;
298 val |= PCIE_OUTPUT_PULL_DOWN;
299 regmap_write(phy->apb, SOC_PCIECTRL_CTRL12_ADDR, val);
301 /* Handle phy_reset and lane0_reset to HW */
302 hi3670_apb_phy_updatel(phy, PCIEPHY_RESET_BIT,
303 PCIEPHY_PIPE_LINE0_RESET_BIT | PCIEPHY_RESET_BIT,
304 SOC_PCIEPHY_CTRL1_ADDR);
306 /* fix chip bug: TxDetectRx fail */
307 hi3670_apb_phy_updatel(phy, PCIE_TXDETECT_RX_FAIL, PCIE_TXDETECT_RX_FAIL,
308 SOC_PCIEPHY_CTRL38_ADDR);
311 static void hi3670_pcie_pll_init(struct hi3670_pcie_phy *phy)
313 hi3670_apb_phy_updatel(phy, PCIE_PHY_CHOOSE_FNPLL, PCIE_PHY_CHOOSE_FNPLL,
314 SOC_PCIEPHY_MMC1PLL_CTRL1);
316 hi3670_apb_phy_updatel(phy,
317 FIELD_PREP(PCIE_FNPLL_FBDIV_MASK, PCIE_FNPLL_FBDIV),
318 PCIE_FNPLL_FBDIV_MASK,
319 SOC_PCIEPHY_MMC1PLL_CTRL16);
321 hi3670_apb_phy_updatel(phy,
322 FIELD_PREP(PCIE_FNPLL_FRACDIV_MASK, PCIE_FNPLL_FRACDIV),
323 PCIE_FNPLL_FRACDIV_MASK, SOC_PCIEPHY_MMC1PLL_CTRL17);
325 hi3670_apb_phy_updatel(phy,
327 FIELD_PREP(PCIE_FNPLL_POSTDIV1_MASK, PCIE_FNPLL_POSTDIV1) |
328 FIELD_PREP(PCIE_FNPLL_POSTDIV2_MASK, PCIE_FNPLL_POSTDIV2) |
329 FIELD_PREP(PCIE_FNPLL_PLL_MODE_MASK, PCIE_FNPLL_PLL_MODE),
330 PCIE_FNPLL_POSTDIV1_MASK |
331 PCIE_FNPLL_POSTDIV2_MASK |
332 PCIE_FNPLL_PLL_MODE_MASK | PCIE_FNPLL_DLL_EN,
333 SOC_PCIEPHY_MMC1PLL_CTRL20);
335 hi3670_apb_phy_writel(phy, PCIE_PHY_MMC1PLL,
336 SOC_PCIEPHY_MMC1PLL_CTRL21);
339 static int hi3670_pcie_pll_ctrl(struct hi3670_pcie_phy *phy, bool enable)
341 struct device *dev = phy->dev;
343 int time = PLL_CTRL_WAIT_TIME;
347 hi3670_apb_phy_updatel(phy, 0, PCIE_PHY_MMC1PLL_DISABLE,
348 SOC_PCIEPHY_MMC1PLL_CTRL16);
351 val = hi3670_apb_phy_readl(phy, SOC_PCIEPHY_MMC1PLL_STAT0);
352 while (!(val & FNPLL_HAS_LOCKED)) {
354 dev_err(dev, "wait for pll_lock timeout\n");
359 val = hi3670_apb_phy_readl(phy, SOC_PCIEPHY_MMC1PLL_STAT0);
362 hi3670_apb_phy_updatel(phy, 0, PCIE_PHY_PCIEPL_BP,
363 SOC_PCIEPHY_MMC1PLL_CTRL20);
366 hi3670_apb_phy_updatel(phy,
367 PCIE_PHY_MMC1PLL_DISABLE,
368 PCIE_PHY_MMC1PLL_DISABLE,
369 SOC_PCIEPHY_MMC1PLL_CTRL16);
371 hi3670_apb_phy_updatel(phy, PCIE_PHY_PCIEPL_BP,
373 SOC_PCIEPHY_MMC1PLL_CTRL20);
379 static void hi3670_pcie_hp_debounce_gt(struct hi3670_pcie_phy *phy, bool open)
382 /* gt_clk_pcie_hp/gt_clk_pcie_debounce open */
383 regmap_write(phy->crgctrl, CRGPERIPH_PEREN12,
386 /* gt_clk_pcie_hp/gt_clk_pcie_debounce close */
387 regmap_write(phy->crgctrl, CRGPERIPH_PERDIS12,
391 static void hi3670_pcie_phyref_gt(struct hi3670_pcie_phy *phy, bool open)
395 regmap_read(phy->crgctrl, CRGPERIPH_PCIECTRL0, &val);
398 val &= ~IO_OE_HARD_GT_MODE; /* enable hard gt mode */
400 val |= IO_OE_HARD_GT_MODE; /* disable hard gt mode */
402 regmap_write(phy->crgctrl, CRGPERIPH_PCIECTRL0, val);
404 /* disable soft gt mode */
405 regmap_write(phy->crgctrl, CRGPERIPH_PERDIS12, IO_PHYREF_SOFT_GT_MODE);
408 static void hi3670_pcie_oe_ctrl(struct hi3670_pcie_phy *phy, bool en_flag)
412 regmap_read(phy->crgctrl, CRGPERIPH_PCIECTRL0, &val);
415 val |= IO_IE_EN_HARD_BYPASS;
418 val &= ~IO_HARD_CTRL_DEBOUNCE_BYPASS;
420 /* set phy_debounce in&out time */
421 val |= (DEBOUNCE_WAITCFG_IN | DEBOUNCE_WAITCFG_OUT);
423 /* select oe_gt_mode */
424 val |= IO_OE_GT_MODE;
427 val &= ~IO_OE_EN_HARD_BYPASS;
429 val |= IO_OE_EN_HARD_BYPASS;
431 regmap_write(phy->crgctrl, CRGPERIPH_PCIECTRL0, val);
434 static void hi3670_pcie_ioref_gt(struct hi3670_pcie_phy *phy, bool open)
439 regmap_write(phy->apb, SOC_PCIECTRL_CTRL21_ADDR,
440 SOC_PCIECTRL_CTRL21_DEFAULT);
442 hi3670_pcie_oe_ctrl(phy, true);
444 /* en hard gt mode */
445 regmap_read(phy->crgctrl, CRGPERIPH_PCIECTRL0, &val);
446 val &= ~IO_REF_HARD_GT_MODE;
447 regmap_write(phy->crgctrl, CRGPERIPH_PCIECTRL0, val);
449 /* disable soft gt mode */
450 regmap_write(phy->crgctrl, CRGPERIPH_PERDIS12,
451 IO_REF_SOFT_GT_MODE);
454 /* disable hard gt mode */
455 regmap_read(phy->crgctrl, CRGPERIPH_PCIECTRL0, &val);
456 val |= IO_REF_HARD_GT_MODE;
457 regmap_write(phy->crgctrl, CRGPERIPH_PCIECTRL0, val);
459 /* disable soft gt mode */
460 regmap_write(phy->crgctrl, CRGPERIPH_PERDIS12,
461 IO_REF_SOFT_GT_MODE);
463 hi3670_pcie_oe_ctrl(phy, false);
467 static int hi3670_pcie_allclk_ctrl(struct hi3670_pcie_phy *phy, bool clk_on)
469 struct device *dev = phy->dev;
475 /* choose 100MHz clk src: Bit[8]==1 pad, Bit[8]==0 pll */
476 hi3670_apb_phy_updatel(phy, 0, PCIE_CLK_SOURCE,
477 SOC_PCIEPHY_CTRL1_ADDR);
479 hi3670_pcie_pll_init(phy);
481 ret = hi3670_pcie_pll_ctrl(phy, true);
483 dev_err(dev, "Failed to enable pll\n");
486 hi3670_pcie_hp_debounce_gt(phy, true);
487 hi3670_pcie_phyref_gt(phy, true);
488 hi3670_pcie_ioref_gt(phy, true);
490 ret = clk_set_rate(phy->aclk, AXI_CLK_FREQ);
492 dev_err(dev, "Failed to set rate\n");
499 hi3670_pcie_ioref_gt(phy, false);
500 hi3670_pcie_phyref_gt(phy, false);
501 hi3670_pcie_hp_debounce_gt(phy, false);
503 hi3670_pcie_pll_ctrl(phy, false);
508 static bool is_pipe_clk_stable(struct hi3670_pcie_phy *phy)
510 struct device *dev = phy->dev;
512 u32 time = PIPE_CLK_STABLE_TIME;
513 u32 pipe_clk_stable = PCIE_IS_CLOCK_STABLE;
515 val = hi3670_apb_phy_readl(phy, SOC_PCIEPHY_STATE0_ADDR);
516 while (val & pipe_clk_stable) {
519 dev_err(dev, "PIPE clk is not stable\n");
523 val = hi3670_apb_phy_readl(phy, SOC_PCIEPHY_STATE0_ADDR);
529 static int hi3670_pcie_noc_power(struct hi3670_pcie_phy *phy, bool enable)
531 struct device *dev = phy->dev;
532 u32 time = NOC_POWER_TIME;
533 unsigned int val = NOC_PW_MASK;
537 val = NOC_PW_MASK | NOC_PW_SET_BIT;
540 rst = enable ? 1 : 0;
542 regmap_write(phy->pmctrl, NOC_POWER_IDLEREQ_1, val);
544 time = NOC_POWER_TIME;
545 regmap_read(phy->pmctrl, NOC_POWER_IDLE_1, &val);
546 while ((val & NOC_PW_SET_BIT) != rst) {
549 dev_err(dev, "Failed to reverse noc power-status\n");
553 regmap_read(phy->pmctrl, NOC_POWER_IDLE_1, &val);
559 static int hi3670_pcie_get_resources_from_pcie(struct hi3670_pcie_phy *phy)
561 struct device_node *pcie_port;
562 struct device *dev = phy->dev;
563 struct device *pcie_dev;
565 pcie_port = of_get_child_by_name(dev->parent->of_node, "pcie");
567 dev_err(dev, "no pcie node found in %s\n",
568 dev->parent->of_node->full_name);
572 pcie_dev = bus_find_device_by_of_node(&platform_bus_type, pcie_port);
574 dev_err(dev, "Didn't find pcie device\n");
579 * We might just use NULL instead of the APB name, as the
580 * pcie-kirin currently registers directly just one regmap (although
581 * the DWC driver register other regmaps).
583 * Yet, it sounds safer to warrant that it will be accessing the
584 * right regmap. So, let's use the named version.
586 phy->apb = dev_get_regmap(pcie_dev, "kirin_pcie_apb");
588 dev_err(dev, "Failed to get APB regmap\n");
595 static int kirin_pcie_clk_ctrl(struct hi3670_pcie_phy *phy, bool enable)
602 ret = clk_set_rate(phy->phy_ref_clk, REF_CLK_FREQ);
606 ret = clk_prepare_enable(phy->phy_ref_clk);
610 ret = clk_prepare_enable(phy->apb_sys_clk);
614 ret = clk_prepare_enable(phy->apb_phy_clk);
618 ret = clk_prepare_enable(phy->aclk);
622 ret = clk_prepare_enable(phy->aux_clk);
629 clk_disable_unprepare(phy->aux_clk);
631 clk_disable_unprepare(phy->aclk);
633 clk_disable_unprepare(phy->apb_phy_clk);
635 clk_disable_unprepare(phy->apb_sys_clk);
637 clk_disable_unprepare(phy->phy_ref_clk);
642 static int hi3670_pcie_phy_init(struct phy *generic_phy)
644 struct hi3670_pcie_phy *phy = phy_get_drvdata(generic_phy);
648 * The code under hi3670_pcie_get_resources_from_pcie() need to
649 * access the reset-gpios and the APB registers, both from the
652 * The APB is obtained via the pcie driver's regmap
653 * Such kind of resource can only be obtained during the PCIe
654 * power_on sequence, as the code inside pcie-kirin needs to
655 * be already probed, as it needs to register the APB regmap.
658 ret = hi3670_pcie_get_resources_from_pcie(phy);
665 static int hi3670_pcie_phy_power_on(struct phy *generic_phy)
667 struct hi3670_pcie_phy *phy = phy_get_drvdata(generic_phy);
670 /* Power supply for Host */
671 regmap_write(phy->sysctrl, SCTRL_PCIE_CMOS_OFFSET, SCTRL_PCIE_CMOS_BIT);
672 usleep_range(TIME_CMOS_MIN, TIME_CMOS_MAX);
674 hi3670_pcie_phy_oe_enable(phy, true);
676 ret = kirin_pcie_clk_ctrl(phy, true);
680 /* ISO disable, PCIeCtrl, PHY assert and clk gate clear */
681 regmap_write(phy->sysctrl, SCTRL_PCIE_ISO_OFFSET, SCTRL_PCIE_ISO_BIT);
682 regmap_write(phy->crgctrl, CRGCTRL_PCIE_ASSERT_OFFSET,
683 CRGCTRL_PCIE_ASSERT_BIT);
684 regmap_write(phy->sysctrl, SCTRL_PCIE_HPCLK_OFFSET,
685 SCTRL_PCIE_HPCLK_BIT);
687 hi3670_pcie_natural_cfg(phy);
689 ret = hi3670_pcie_allclk_ctrl(phy, true);
693 /* pull down phy_test_powerdown signal */
694 hi3670_apb_phy_updatel(phy, 0, PCIE_PULL_DOWN_PHY_TEST_POWERDOWN,
695 SOC_PCIEPHY_CTRL0_ADDR);
697 /* deassert controller perst_n */
698 regmap_read(phy->apb, SOC_PCIECTRL_CTRL12_ADDR, &val);
699 val |= PCIE_DEASSERT_CONTROLLER_PERST;
700 regmap_write(phy->apb, SOC_PCIECTRL_CTRL12_ADDR, val);
703 ret = is_pipe_clk_stable(phy);
707 hi3670_pcie_set_eyeparam(phy);
709 ret = hi3670_pcie_noc_power(phy, false);
716 kirin_pcie_clk_ctrl(phy, false);
720 static int hi3670_pcie_phy_power_off(struct phy *generic_phy)
722 struct hi3670_pcie_phy *phy = phy_get_drvdata(generic_phy);
724 hi3670_pcie_phy_oe_enable(phy, false);
726 hi3670_pcie_allclk_ctrl(phy, false);
728 /* Drop power supply for Host */
729 regmap_write(phy->sysctrl, SCTRL_PCIE_CMOS_OFFSET, 0);
732 * FIXME: The enabled clocks should be disabled here by calling
733 * kirin_pcie_clk_ctrl(phy, false);
734 * However, some clocks used at Kirin 970 should be marked as
735 * CLK_IS_CRITICAL at clk-hi3670 driver, as powering such clocks off
736 * cause an Asynchronous SError interrupt, which produces panic().
737 * While clk-hi3670 is not fixed, we cannot risk disabling clocks here.
743 static const struct phy_ops hi3670_phy_ops = {
744 .init = hi3670_pcie_phy_init,
745 .power_on = hi3670_pcie_phy_power_on,
746 .power_off = hi3670_pcie_phy_power_off,
747 .owner = THIS_MODULE,
750 static int hi3670_pcie_phy_get_resources(struct hi3670_pcie_phy *phy,
751 struct platform_device *pdev)
753 struct device *dev = &pdev->dev;
756 phy->crgctrl = syscon_regmap_lookup_by_compatible("hisilicon,hi3670-crgctrl");
757 if (IS_ERR(phy->crgctrl))
758 return PTR_ERR(phy->crgctrl);
760 phy->sysctrl = syscon_regmap_lookup_by_compatible("hisilicon,hi3670-sctrl");
761 if (IS_ERR(phy->sysctrl))
762 return PTR_ERR(phy->sysctrl);
764 phy->pmctrl = syscon_regmap_lookup_by_compatible("hisilicon,hi3670-pmctrl");
765 if (IS_ERR(phy->pmctrl))
766 return PTR_ERR(phy->pmctrl);
769 phy->phy_ref_clk = devm_clk_get(dev, "phy_ref");
770 if (IS_ERR(phy->phy_ref_clk))
771 return PTR_ERR(phy->phy_ref_clk);
773 phy->aux_clk = devm_clk_get(dev, "aux");
774 if (IS_ERR(phy->aux_clk))
775 return PTR_ERR(phy->aux_clk);
777 phy->apb_phy_clk = devm_clk_get(dev, "apb_phy");
778 if (IS_ERR(phy->apb_phy_clk))
779 return PTR_ERR(phy->apb_phy_clk);
781 phy->apb_sys_clk = devm_clk_get(dev, "apb_sys");
782 if (IS_ERR(phy->apb_sys_clk))
783 return PTR_ERR(phy->apb_sys_clk);
785 phy->aclk = devm_clk_get(dev, "aclk");
786 if (IS_ERR(phy->aclk))
787 return PTR_ERR(phy->aclk);
790 phy->base = devm_platform_ioremap_resource(pdev, 0);
791 if (IS_ERR(phy->base))
792 return PTR_ERR(phy->base);
794 hi3670_pcie_get_eyeparam(phy);
799 static int hi3670_pcie_phy_probe(struct platform_device *pdev)
801 struct phy_provider *phy_provider;
802 struct device *dev = &pdev->dev;
803 struct hi3670_pcie_phy *phy;
804 struct phy *generic_phy;
807 phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL);
813 ret = hi3670_pcie_phy_get_resources(phy, pdev);
817 generic_phy = devm_phy_create(dev, dev->of_node, &hi3670_phy_ops);
818 if (IS_ERR(generic_phy)) {
819 dev_err(dev, "failed to create PHY\n");
820 return PTR_ERR(generic_phy);
823 phy_set_drvdata(generic_phy, phy);
824 phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
826 return PTR_ERR_OR_ZERO(phy_provider);
829 static const struct of_device_id hi3670_pcie_phy_match[] = {
831 .compatible = "hisilicon,hi970-pcie-phy",
836 static struct platform_driver hi3670_pcie_phy_driver = {
837 .probe = hi3670_pcie_phy_probe,
839 .of_match_table = hi3670_pcie_phy_match,
840 .name = "hi3670_pcie_phy",
841 .suppress_bind_attrs = true,
844 builtin_platform_driver(hi3670_pcie_phy_driver);
846 MODULE_DEVICE_TABLE(of, hi3670_pcie_phy_match);
847 MODULE_DESCRIPTION("PCIe phy driver for Kirin 970");
850 MODULE_LICENSE("GPL v2");