blob: c8b367a3896b0b05c0ca5969b142187229f0e6fa [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * PCIe host controller driver for ASR ASR1901 SoCs
4 *
5 * SR1901 PCIe Glue Layer Source Code
6 *
7 * Copyright (C) 2022 ASR Technology Group Ltd.
8 *
9 */
10
11#include <linux/clk.h>
12#include <linux/delay.h>
13#include <linux/interrupt.h>
14#include <linux/kernel.h>
15#include <linux/init.h>
16#include <linux/of.h>
17#include <linux/iopoll.h>
18#include <linux/pci.h>
19#include <linux/phy/phy.h>
20#include <linux/platform_device.h>
21#include <linux/resource.h>
22#include <linux/of_pci.h>
23#include <linux/of_irq.h>
24#include <linux/of_gpio.h>
25#include <linux/pm_runtime.h>
26#include <linux/reset.h>
27#include <linux/pm_qos.h>
28#include <linux/cputype.h>
29#include <soc/asr/regs-addr.h>
30#include "pcie-designware.h"
31
32#define DEVICE_NAME "ASR1901 PCIe Host"
33
34#define APMU_PCIE_CLK_RES_CTRL 0x3CC
35#define APMU_PCIE_CTRL_LOGIC 0x3D0
36#define APMU_PCIE2_CLK_RES_CTRL 0x3E4
37#define APMU_PCIE2_CTRL_LOGIC 0x3E8
38#define APMU_USB3PHY0_CTRL0 0x3B8
39
40#define HSIO_RC_R_CAL_STATUS (0x15c)
41#define PCIE_CAL_DONE (0x3<<14)
42
43#define LANE1_OFFSET 0x400
44#define LTSSM_EN (0x1 << 6)
45#define APP_HOLD_PHY_RST (0x1 << 30)
46#define DEVICE_TYPE_RC (0x1 << 31) /* BIT31 0: EP, 1: RC*/
47
48/* PCIe Config registers */
49/* PCIe controller wrapper ASR configuration registers */
50#define PCIE_AHB_IRQ 0x0000
51#define IRQ_EN 0x1
52
53#define PCIE_AHB_LINK_STS 0x0004
54#define DLL_LINK_UP (0x1 << 12)
55#define PHY_LINK_UP (0x1 << 1)
56#define LTSSM_L0 (0x11 << 6)
57#define LTSSM_STS (0x3f << 6)
58
59#define PCIE_AHB_LEGACY_INT 0x0008
60#define PLL_READY (0x1)
61
62#define PCIE_AHB_IRQENABLE_SET_INTX 0x000c
63#define INTA (0x1 << 6)
64#define INTB (0x1 << 7)
65#define INTC (0x1 << 8)
66#define INTD (0x1 << 9)
67#define LEG_EP_INTERRUPTS (INTA | INTB | INTC | INTD)
68#define INTX_MASK GENMASK(9, 6)
69#define INTX_SHIFT 6
70
71#define PCIE_AHB_IRQSTATUS 0x0010
72#define PCIE_AHB_IRQENABLE_SET 0x0014
73#define MSI_INT (0x1 << 11)
74#define DLL_LINK_INT (0x1 << 20)
75
76/* PCIe PHY registers */
77#define PUPHY_CLK_CFG 0x8
78#define PUPHY_MODE_CFG 0x0c
79#define PUPHY_ERROR_STATUS 0x10
80#define PUPHY_OVERRIDE 0x18
81#define PUPHY_RC_REG 0x44
82#define PUPHY_PCIE3X2_REG 0x50
83#define PUPHY_PLL_REG1 0x58
84#define PUPHY_PLL_REG2 0x5c
85#define PUPHY_RX_REG1 0x60
86#define PUPHY_TX_REG 0x74
87#define PUPHY_TEST_REG 0x78
88#define PUPHY_TEST_INFO 0x84
89
90#define OVRD_MPU_U3 (0x1 << 17)
91#define CFG_MPU_U3 (0x1 << 16)
92#define OVRD_PU_RX_LFPS (0x1 << 15)
93
94#define LINK_WAIT_MIN 900
95#define LINK_WAIT_MAX 1000
96/* Time for delay */
97#define REF_PERST_MIN 20000
98#define REF_PERST_MAX 25000
99#define PERST_ACCESS_MIN 10000
100#define PERST_ACCESS_MAX 12000
101
102#define to_kst_pcie(x) dev_get_drvdata((x)->dev)
103
104struct kst_pcie {
105 struct device *dev;
106 struct dw_pcie *pci;
107 void __iomem *phy_base;
108 void __iomem *app_base;
109 void __iomem *usb3_base;
110 struct clk *clk;
111 struct phy *phy[2];
112 unsigned int phy_count;
113 unsigned int slot;
114 unsigned int lanes;
115 void __iomem *pcie_pmua_reg;
116 s32 lpm_qos;
117 int irq;
118 int gpio_reset;
119 struct pm_qos_request qos_idle;
120};
121
122extern u32 usb31_rterm_cal_value;
123
124static inline void kst_phy_writel(struct kst_pcie *pcie, u32 val, u32 reg)
125{
126 writel(val, pcie->phy_base + reg);
127}
128static inline u32 kst_phy_readl(struct kst_pcie *pcie, u32 reg)
129{
130 return readl(pcie->phy_base + reg);
131}
132static inline void kst_app_writel(struct kst_pcie *pcie, u32 val, u32 reg)
133{
134 writel(val, pcie->app_base + reg);
135}
136static inline u32 kst_app_readl(struct kst_pcie *pcie, u32 reg)
137{
138 return readl(pcie->app_base + reg);
139}
140
141#ifndef CONFIG_USB_DWC3
142static inline void kst_usb30phy_writel(struct kst_pcie *pcie, u32 val, u32 reg)
143{
144 writel(val, pcie->usb3_base + reg);
145}
146static inline u32 kst_usb30phy_readl(struct kst_pcie *pcie, u32 reg)
147{
148 return readl(pcie->usb3_base + reg);
149}
150int rterm_val, cali_done = 0;
151#define USB_CALI_TIMEOUT 50000
152static void kst_usb_cali_phy(struct kst_pcie *kst_pcie)
153{
154 u32 val, timeout = 0;
155 if (cali_done == 1)
156 return;
157
158 cali_done = 1;
159 writel(0x1e00b, regs_addr_get_va(REGS_ADDR_APMU) + APMU_USB3PHY0_CTRL0);
160
161 val = kst_usb30phy_readl(kst_pcie, PUPHY_PLL_REG2);
162 kst_usb30phy_writel(kst_pcie, val&(~(0x1<<21)), PUPHY_PLL_REG2);
163
164 val = kst_usb30phy_readl(kst_pcie, PUPHY_PLL_REG1);
165 kst_usb30phy_writel(kst_pcie, val&(0xFFFFC0FF), PUPHY_PLL_REG1);
166
167 do {
168 val = kst_usb30phy_readl(kst_pcie, PUPHY_TEST_INFO);
169 udelay(10);
170 timeout++;
171 if (timeout > USB_CALI_TIMEOUT)
172 break;
173 } while (((val>>24)&0x1) == 0);
174
175 val = kst_usb30phy_readl(kst_pcie, PUPHY_TEST_INFO);
176 val = kst_usb30phy_readl(kst_pcie, PUPHY_TEST_INFO);
177 pr_debug("usb rterm = %08x\r\n", (val>>8) & 0x000000FF);
178 writel(0xb, regs_addr_get_va(REGS_ADDR_APMU) + APMU_USB3PHY0_CTRL0);
179
180 rterm_val = val;
181}
182
183static void kst2_usb_cali_phy(void)
184{
185 u32 value;
186 int timeout = 0;
187 void __iomem *apbs_base = regs_addr_get_va(REGS_ADDR_APBS);
188
189 value = readl(apbs_base + HSIO_RC_R_CAL_STATUS);
190 value &= ~0x1;
191 writel(value, apbs_base + HSIO_RC_R_CAL_STATUS);
192
193 do {
194 value = readl(apbs_base + HSIO_RC_R_CAL_STATUS);
195 udelay(10);
196 timeout++;
197 if (timeout > USB_CALI_TIMEOUT) {
198 pr_err("PHY Calibration fail");
199 break;
200 }
201 } while ((value&PCIE_CAL_DONE) != PCIE_CAL_DONE);
202}
203
204#endif
205
206static int kst_pcie_init_phy(struct kst_pcie *kst_pcie)
207{
208 u32 val, data;
209 int count = 0;
210
211 /* release pcie reset and enable pcie axi clk */
212 __raw_writel(0x8000043f, kst_pcie->pcie_pmua_reg);
213 val = __raw_readl(kst_pcie->pcie_pmua_reg);
214 val |= DEVICE_TYPE_RC;
215 val &= ~APP_HOLD_PHY_RST;
216 __raw_writel(val, kst_pcie->pcie_pmua_reg);
217 /* enable port0 dbi aclock for port1 only case */
218 if (kst_pcie->slot == 1) {
219 val = __raw_readl(regs_addr_get_va(REGS_ADDR_APMU)
220 + APMU_PCIE_CLK_RES_CTRL);
221 if (!(val&0x9)) {
222 val |= (0x80000009);
223 __raw_writel(val, regs_addr_get_va(REGS_ADDR_APMU)
224 + APMU_PCIE_CLK_RES_CTRL);
225 }
226 }
227
228 val = kst_phy_readl(kst_pcie, PUPHY_OVERRIDE);
229 val |= (OVRD_MPU_U3 | OVRD_PU_RX_LFPS);
230 kst_phy_writel(kst_pcie, val, PUPHY_OVERRIDE);
231 if (kst_pcie->lanes == 2) {
232 val = kst_phy_readl(kst_pcie, PUPHY_OVERRIDE + LANE1_OFFSET);
233 val |= (OVRD_MPU_U3 | OVRD_PU_RX_LFPS);
234 kst_phy_writel(kst_pcie, val, PUPHY_OVERRIDE + LANE1_OFFSET);
235 }
236
237 val = kst_phy_readl(kst_pcie, PUPHY_RC_REG);
238 val = (val & 0xFFFF0000) | (0x1387);
239 kst_phy_writel(kst_pcie, val, PUPHY_RC_REG);
240
241 if (cpu_is_asr1901_a0_plus()) {
242#ifndef CONFIG_USB_DWC3
243 kst_usb_cali_phy(kst_pcie);
244 val = rterm_val;
245#else
246 val = usb31_rterm_cal_value;
247#endif
248 data = kst_phy_readl(kst_pcie, PUPHY_RC_REG);
249 data = (data & 0xffffff00) | ((val>>8) & 0xFF);
250 kst_phy_writel(kst_pcie, data, PUPHY_RC_REG);
251 pr_debug("pcie rterm = %08x\r\n", kst_phy_readl(kst_pcie, PUPHY_RC_REG));
252 }
253#ifndef CONFIG_USB_DWC3
254 if (cpu_is_asr1906()) {
255 kst2_usb_cali_phy();
256 }
257#endif
258 val = kst_phy_readl(kst_pcie, PUPHY_TEST_REG);
259 val |= (0x1<<1);
260 kst_phy_writel(kst_pcie, val, PUPHY_TEST_REG);
261 if (kst_pcie->lanes == 2) {
262 val = kst_phy_readl(kst_pcie, PUPHY_TEST_REG + LANE1_OFFSET);
263 val |= (0x1<<1);
264 kst_phy_writel(kst_pcie, val, PUPHY_TEST_REG + LANE1_OFFSET);
265 }
266
267 val = kst_phy_readl(kst_pcie, PUPHY_OVERRIDE);
268 val &= ~(OVRD_MPU_U3 | OVRD_PU_RX_LFPS);
269 kst_phy_writel(kst_pcie, val, PUPHY_OVERRIDE);
270 if (kst_pcie->lanes == 2) {
271 val = kst_phy_readl(kst_pcie, PUPHY_OVERRIDE + LANE1_OFFSET);
272 val &= ~(OVRD_MPU_U3 | OVRD_PU_RX_LFPS);
273 kst_phy_writel(kst_pcie, val, PUPHY_OVERRIDE + LANE1_OFFSET);
274 }
275
276 val = kst_phy_readl(kst_pcie, PUPHY_PLL_REG1);
277 kst_phy_writel(kst_pcie, val&0xffff0fff, PUPHY_PLL_REG1);
278 val = kst_phy_readl(kst_pcie, PUPHY_PLL_REG1);
279 kst_phy_writel(kst_pcie, val|0xC000, PUPHY_PLL_REG1);
280 val = kst_phy_readl(kst_pcie, PUPHY_PLL_REG2);
281 kst_phy_writel(kst_pcie, val|(0x1<<20), PUPHY_PLL_REG2);
282 val = kst_phy_readl(kst_pcie, PUPHY_PLL_REG2);
283 kst_phy_writel(kst_pcie, val&(~(0x1<<21)), PUPHY_PLL_REG2);
284 kst_phy_writel(kst_pcie, 0x6505, PUPHY_PCIE3X2_REG);
285
286 val = kst_phy_readl(kst_pcie, PUPHY_PLL_REG1);
287 kst_phy_writel(kst_pcie, val&0xf0ffffff, PUPHY_PLL_REG1);
288 val = kst_phy_readl(kst_pcie, PUPHY_CLK_CFG);
289 kst_phy_writel(kst_pcie, 0xB7c, PUPHY_CLK_CFG);
290
291 if (kst_pcie->lanes == 1) {
292 pr_debug("PCIE only one Lane, disable Lane1...\r\n");
293 val = kst_phy_readl(kst_pcie, PUPHY_MODE_CFG + LANE1_OFFSET);
294 kst_phy_writel(kst_pcie, val|(0x1<<30), PUPHY_MODE_CFG + LANE1_OFFSET);
295 } else {
296 kst_phy_writel(kst_pcie, 0xB7c, PUPHY_CLK_CFG + LANE1_OFFSET);
297 }
298
299 val = kst_phy_readl(kst_pcie, PUPHY_MODE_CFG);
300 val |= (0x1<<2);
301 kst_phy_writel(kst_pcie, val, PUPHY_MODE_CFG);
302 if (kst_pcie->lanes == 2) {
303 val = kst_phy_readl(kst_pcie, PUPHY_MODE_CFG + LANE1_OFFSET);
304 val |= (0x1<<2);
305 kst_phy_writel(kst_pcie, val, PUPHY_MODE_CFG + LANE1_OFFSET);
306 }
307
308 do {
309 val = kst_phy_readl(kst_pcie, PUPHY_CLK_CFG);
310 count++;
311 usleep_range(LINK_WAIT_MIN, LINK_WAIT_MAX);
312 if (count == 100) {
313 pr_info(DEVICE_NAME "PCIe wait pll ready timeout.\n");
314 return -EINVAL;
315 }
316 }while(( val & PLL_READY ) != PLL_READY);
317
318 return 0;
319}
320
321static int kst_pcie_disable_phy(struct kst_pcie *kst_pcie)
322{
323 u32 val;
324
325 val = kst_phy_readl(kst_pcie, PUPHY_OVERRIDE);
326 val |= OVRD_MPU_U3;
327 val &= ~CFG_MPU_U3;
328 kst_phy_writel(kst_pcie, val, PUPHY_OVERRIDE);
329
330 val = kst_phy_readl(kst_pcie, PUPHY_OVERRIDE + LANE1_OFFSET);
331 val |= OVRD_MPU_U3;
332 val &= ~CFG_MPU_U3;
333
334 kst_phy_writel(kst_pcie, val, PUPHY_OVERRIDE + LANE1_OFFSET);
335
336 return 0;
337}
338
339static int kst_pcie_reenable_phy(struct kst_pcie *kst_pcie)
340{
341 u32 val;
342
343 val = kst_phy_readl(kst_pcie, PUPHY_OVERRIDE);
344 val &= ~(OVRD_MPU_U3 | OVRD_PU_RX_LFPS);
345 kst_phy_writel(kst_pcie, val, PUPHY_OVERRIDE);
346 if (kst_pcie->lanes == 2) {
347 val = kst_phy_readl(kst_pcie, PUPHY_OVERRIDE + LANE1_OFFSET);
348 val &= ~(OVRD_MPU_U3 | OVRD_PU_RX_LFPS);
349 kst_phy_writel(kst_pcie, val, PUPHY_OVERRIDE + LANE1_OFFSET);
350 }
351
352 return 0;
353}
354
355static int kst_pcie_link_up(struct dw_pcie *pci)
356{
357 struct kst_pcie *kst_pcie = to_kst_pcie(pci);
358 u32 status = kst_app_readl(kst_pcie, PCIE_AHB_LINK_STS);
359
360 if ((status & DLL_LINK_UP) && (status & PHY_LINK_UP))
361 return 1;
362
363 return 0;
364}
365
366static int kst_pcie_establish_link(struct pcie_port *pp)
367{
368 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
369 struct kst_pcie *kst_pcie = to_kst_pcie(pci);
370 struct device *dev = kst_pcie->pci->dev;
371 unsigned int val, count = 0;
372
373 if (kst_pcie_link_up(pci))
374 return 0;
375
376 val = __raw_readl(kst_pcie->pcie_pmua_reg);
377 __raw_writel(val | DEVICE_TYPE_RC, kst_pcie->pcie_pmua_reg);
378
379 dw_pcie_setup_rc(pp);
380#if 0
381 val = readl(pci->dbi_base + 0x8a8);
382 val &= ~(0xffff<<8);
383 val |= (0x10<<8); //set TX preset P4
384 writel(val, pci->dbi_base + 0x8a8);
385#endif
386 /* Release app_hold_phy_reset and enable ltssm */
387 val = __raw_readl(kst_pcie->pcie_pmua_reg);
388 val |= LTSSM_EN;
389 val &= ~APP_HOLD_PHY_RST;
390 __raw_writel(val, kst_pcie->pcie_pmua_reg);
391
392 udelay(10);
393 val = readl(pci->dbi_base + 0x80c);
394 val |= 1<<17;
395 writel(val, pci->dbi_base + 0x80c);
396 udelay(10);
397
398 do {
399 usleep_range(LINK_WAIT_MIN, LINK_WAIT_MAX);
400 val = kst_app_readl(kst_pcie, PCIE_AHB_LINK_STS);
401 pr_debug("%s, ltssm: 0x%x.\n", __func__, val);
402 count++;
403 if (count == 1000) {
404 pr_info(DEVICE_NAME "PCIe enter L0 failed, ltssm: 0x%x\n", val);
405 return -EINVAL;
406 }
407 }while(( val & LTSSM_STS ) != LTSSM_L0 );
408
409 count = 0;
410 /* check if the link is up or not */
411 while (!kst_pcie_link_up(pci)) {
412 usleep_range(LINK_WAIT_MIN, LINK_WAIT_MAX);
413 count++;
414 if (count == 100) {
415 dev_err(dev, "Link Fail\n");
416 return -EINVAL;
417 }
418 }
419
420 val = readl(pci->dbi_base + 0x80);
421 pr_info(DEVICE_NAME " %dx link negotiated (gen %d)\n",
422 (val>>20)&0xf, (val>>16)&0xf);
423
424 return 0;
425}
426
427static int kst_pcie_host_init(struct pcie_port *pp)
428{
429 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
430 struct kst_pcie *kst_pcie = to_kst_pcie(pci);
431 int ret;
432
433 ret = kst_pcie_establish_link(pp);
434 if(ret) {
435 __raw_writel(0x0, kst_pcie->pcie_pmua_reg);
436 return -1;
437 }
438 return 0;
439}
440static const struct dw_pcie_host_ops kst_pcie_host_ops = {
441 .host_init = kst_pcie_host_init,
442};
443
444#ifndef CONFIG_PCI_MSI
445static irqreturn_t kst_pcie_irq_handler(int irq, void *arg)
446{
447 struct kst_pcie *kst_pcie = arg;
448 u32 status;
449
450 pm_wakeup_event(kst_pcie->dev, 2000);
451 status = kst_app_readl(kst_pcie, PCIE_AHB_LEGACY_INT);
452 kst_app_writel(kst_pcie, status, PCIE_AHB_LEGACY_INT);
453
454 return IRQ_HANDLED;
455}
456#endif
457#ifdef CONFIG_PCI_MSI
458static int kst_pcie_add_msi(struct dw_pcie *pci,
459 struct platform_device *pdev)
460{
461 int irq;
462
463 if (IS_ENABLED(CONFIG_PCI_MSI)) {
464 irq = platform_get_irq(pdev, 0);
465 if (irq < 0) {
466 dev_err(&pdev->dev,
467 "failed to get MSI IRQ (%d)\n", irq);
468 return irq;
469 }
470 pci->pp.msi_irq = irq;
471 }
472
473 return 0;
474}
475#endif
476
477static void kst_pcie_enable_interrupts(struct pcie_port *pp)
478{
479 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
480 struct kst_pcie *kst_pcie = to_kst_pcie(pci);
481 u32 val;
482
483#ifdef CONFIG_PCI_MSI
484 dw_pcie_msi_init(pp);
485
486 val = kst_app_readl(kst_pcie, PCIE_AHB_IRQENABLE_SET);
487 val |= MSI_INT;
488 kst_app_writel(kst_pcie, val, PCIE_AHB_IRQENABLE_SET);
489#else /* legacy interrupt */
490 val = kst_app_readl(kst_pcie, PCIE_AHB_IRQENABLE_SET_INTX);
491 val |= LEG_EP_INTERRUPTS;
492 kst_app_writel(kst_pcie, val, PCIE_AHB_IRQENABLE_SET_INTX);
493#endif
494 val = kst_app_readl(kst_pcie, PCIE_AHB_IRQ);
495 val |= IRQ_EN;
496 kst_app_writel(kst_pcie, val, PCIE_AHB_IRQ);
497
498 return;
499}
500
501static int kst_add_pcie_port(struct kst_pcie *pcie,
502 struct platform_device *pdev)
503{
504 struct dw_pcie *pci = pcie->pci;
505 struct pcie_port *pp = &pci->pp;
506 struct device *dev = &pdev->dev;
507 int ret;
508
509 pp->ops = &kst_pcie_host_ops;
510#ifdef CONFIG_PCI_MSI
511 ret = kst_pcie_add_msi(pci, pdev);
512 if (ret)
513 return ret;
514#else
515 pp->irq = platform_get_irq(pdev, 0);
516 if (pp->irq < 0) {
517 dev_err(dev, "failed to get irq for port\n");
518 return pp->irq;
519 }
520 ret = devm_request_irq(dev, pp->irq, kst_pcie_irq_handler,
521 IRQF_SHARED, "kst-pcie", pcie);
522 if (ret) {
523 dev_err(dev, "failed to request irq %d\n", pp->irq);
524 return ret;
525 }
526#endif
527
528 ret = dw_pcie_host_init(pp);
529 if (ret) {
530 dev_err(dev, "failed to initialize host: %d\n", ret);
531 return ret;
532 }
533 kst_pcie_enable_interrupts(pp);
534
535 return 0;
536}
537
538static const struct dw_pcie_ops dw_pcie_ops = {
539 .link_up = kst_pcie_link_up,
540};
541
542static long kst_pcie_get_resource(struct kst_pcie *kst_pcie,
543 struct platform_device *pdev)
544{
545 struct device *dev = &pdev->dev;
546 struct resource *phy;
547 struct resource *dbi;
548 struct device_node *np = pdev->dev.of_node;
549 const __be32 *prop;
550 unsigned int proplen;
551
552 phy = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pcie-phy");
553 kst_pcie->phy_base = devm_ioremap_resource(dev, phy);
554 if (IS_ERR(kst_pcie->phy_base))
555 return PTR_ERR(kst_pcie->phy_base);
556 kst_pcie->app_base = kst_pcie->phy_base + 0x10000;
557
558 dbi = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pcie-dbi");
559 kst_pcie->pci->dbi_base = devm_ioremap_resource(dev, dbi);
560 if (IS_ERR(kst_pcie->pci->dbi_base))
561 return PTR_ERR(kst_pcie->pci->dbi_base);
562#ifndef CONFIG_USB_DWC3
563 if (cali_done == 0) { //for 2 pcie only remap 1 usb3-phy address
564 dbi = platform_get_resource_byname(pdev, IORESOURCE_MEM, "usb3-phy");
565 kst_pcie->usb3_base = devm_ioremap_resource(dev, dbi);
566 if (IS_ERR(kst_pcie->usb3_base))
567 return PTR_ERR(kst_pcie->usb3_base);
568 }
569#endif
570 if (of_property_read_u32(np, "num-lanes", &(kst_pcie->lanes))) {
571 pr_err("Failed to parse the PCIE0 or PCIE1 lane number\n");
572 return -EINVAL;
573 }
574
575 prop = of_get_property(np, "lpm-qos", &proplen);
576 if (!prop) {
577 pr_err("lpm-qos config in DT for PCIe is not defined\n");
578 return -EINVAL;
579 } else
580 kst_pcie->lpm_qos = be32_to_cpup(prop);
581
582 if (of_property_read_u32(np, "num-slot", &(kst_pcie->slot))) {
583 pr_err("Failed to parse the PCIE0 or PCIE1\n");
584 return -EINVAL;
585 }
586 kst_pcie->gpio_reset = of_get_named_gpio(np, "reset-gpios", 0);
587 if (kst_pcie->gpio_reset < 0)
588 return -ENODEV;
589
590 if (kst_pcie->slot == 0)
591 kst_pcie->pcie_pmua_reg = regs_addr_get_va(REGS_ADDR_APMU)
592 + APMU_PCIE_CLK_RES_CTRL;
593 else if (kst_pcie->slot == 1)
594 kst_pcie->pcie_pmua_reg = regs_addr_get_va(REGS_ADDR_APMU)
595 + APMU_PCIE2_CLK_RES_CTRL;
596
597 return 0;
598}
599
600static int kst_pcie_probe(struct platform_device *pdev)
601{
602 struct dw_pcie *pci;
603 struct kst_pcie *pcie;
604 struct device *dev = &pdev->dev;
605 int ret;
606
607 pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL);
608 if (!pcie)
609 return -ENOMEM;
610
611 pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
612 if (!pci)
613 return -ENOMEM;
614
615 pci->dev = dev;
616 pci->ops = &dw_pcie_ops;
617 pcie->pci = pci;
618
619 kst_pcie_get_resource(pcie, pdev);
620
621 pcie->clk = devm_clk_get(dev, NULL);
622 if (IS_ERR(pcie->clk))
623 return PTR_ERR(pcie->clk);
624
625 ret = clk_prepare_enable(pcie->clk);
626 if (ret)
627 return ret;
628
629 ret = kst_pcie_init_phy(pcie);
630 if (ret)
631 goto fail_clk;
632
633 /* perst assert Endpoint */
634 if (!gpio_request(pcie->gpio_reset, "pcie_perst")) {
635 usleep_range(REF_PERST_MIN, REF_PERST_MAX);
636 ret = gpio_direction_output(pcie->gpio_reset, 1);
637 if (ret) {
638 pr_info(DEVICE_NAME "PCIE reset device failed.\r\n");
639 goto disable_phy;
640 }
641 gpio_free(pcie->gpio_reset);
642 usleep_range(PERST_ACCESS_MIN, PERST_ACCESS_MAX);
643 }
644 platform_set_drvdata(pdev, pcie);
645
646 ret = kst_add_pcie_port(pcie, pdev);
647 if (ret)
648 goto disable_phy;
649
650 device_init_wakeup(&pdev->dev, 1);
651 pm_qos_add_request(&pcie->qos_idle, PM_QOS_CPUIDLE_BLOCK,
652 PM_QOS_CPUIDLE_BLOCK_DEFAULT_VALUE);
653 pcie->qos_idle.name = pdev->name;
654 pm_qos_update_request(&pcie->qos_idle, pcie->lpm_qos);
655
656 return 0;
657
658disable_phy:
659 kst_pcie_disable_phy(pcie);
660fail_clk:
661 clk_disable_unprepare(pcie->clk);
662
663 return ret;
664}
665#ifdef CONFIG_PM_SLEEP
666static int __maybe_unused kst_pcie_suspend_noirq(struct device *dev)
667{
668 struct kst_pcie *pcie = dev_get_drvdata(dev);
669
670 kst_pcie_disable_phy(pcie);
671 pm_qos_update_request(&pcie->qos_idle,
672 PM_QOS_CPUIDLE_BLOCK_DEFAULT_VALUE);
673
674 return 0;
675}
676
677static int __maybe_unused kst_pcie_resume_noirq(struct device *dev)
678{
679 struct kst_pcie *pcie = dev_get_drvdata(dev);
680
681 kst_pcie_reenable_phy(pcie);
682 pm_qos_update_request(&pcie->qos_idle, pcie->lpm_qos);
683
684 return 0;
685}
686
687static const struct dev_pm_ops kst_pcie_pm_ops = {
688 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(kst_pcie_suspend_noirq,
689 kst_pcie_resume_noirq)
690};
691#endif
692
693static const struct of_device_id kst_pcie_of_match[] = {
694 { .compatible = "asr,kst-pcie", },
695 {},
696};
697
698static struct platform_driver kst_pcie_driver = {
699 .probe = kst_pcie_probe,
700 .driver = {
701 .name = "kst-pcie",
702 .of_match_table = of_match_ptr(kst_pcie_of_match),
703 .suppress_bind_attrs = true,
704#ifdef CONFIG_PM_SLEEP
705 .pm = &kst_pcie_pm_ops,
706#endif
707 },
708};
709
710static int __init kst_pcie_init(void)
711{
712 return platform_driver_probe(&kst_pcie_driver, kst_pcie_probe);
713}
714device_initcall_sync(kst_pcie_init);