2 * Copyright (c) 2014-2015, The Linux Foundation. All rights reserved.
3 * Copyright 2015 Linaro Limited.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 and
7 * only version 2 as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #include <linux/clk.h>
16 #include <linux/delay.h>
17 #include <linux/gpio.h>
18 #include <linux/interrupt.h>
20 #include <linux/iopoll.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/of_device.h>
24 #include <linux/of_gpio.h>
25 #include <linux/pci.h>
26 #include <linux/platform_device.h>
27 #include <linux/phy/phy.h>
28 #include <linux/regulator/consumer.h>
29 #include <linux/reset.h>
30 #include <linux/slab.h>
31 #include <linux/types.h>
33 #include "pcie-designware.h"
35 #define PCIE20_PARF_PHY_CTRL 0x40
36 #define PCIE20_PARF_PHY_REFCLK 0x4C
37 #define PCIE20_PARF_DBI_BASE_ADDR 0x168
38 #define PCIE20_PARF_SLV_ADDR_SPACE_SIZE 0x16c
39 #define PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT 0x178
41 #define PCIE20_ELBI_SYS_CTRL 0x04
42 #define PCIE20_ELBI_SYS_CTRL_LT_ENABLE BIT(0)
44 #define PCIE20_CAP 0x70
46 #define PERST_DELAY_US 1000
48 struct qcom_pcie_resources_v0
{
49 struct clk
*iface_clk
;
52 struct reset_control
*pci_reset
;
53 struct reset_control
*axi_reset
;
54 struct reset_control
*ahb_reset
;
55 struct reset_control
*por_reset
;
56 struct reset_control
*phy_reset
;
57 struct regulator
*vdda
;
58 struct regulator
*vdda_phy
;
59 struct regulator
*vdda_refclk
;
62 struct qcom_pcie_resources_v1
{
65 struct clk
*master_bus
;
66 struct clk
*slave_bus
;
67 struct reset_control
*core
;
68 struct regulator
*vdda
;
71 union qcom_pcie_resources
{
72 struct qcom_pcie_resources_v0 v0
;
73 struct qcom_pcie_resources_v1 v1
;
78 struct qcom_pcie_ops
{
79 int (*get_resources
)(struct qcom_pcie
*pcie
);
80 int (*init
)(struct qcom_pcie
*pcie
);
81 void (*deinit
)(struct qcom_pcie
*pcie
);
87 union qcom_pcie_resources res
;
92 struct gpio_desc
*reset
;
93 struct qcom_pcie_ops
*ops
;
96 #define to_qcom_pcie(x) container_of(x, struct qcom_pcie, pp)
98 static void qcom_ep_reset_assert(struct qcom_pcie
*pcie
)
100 gpiod_set_value(pcie
->reset
, 1);
101 usleep_range(PERST_DELAY_US
, PERST_DELAY_US
+ 500);
104 static void qcom_ep_reset_deassert(struct qcom_pcie
*pcie
)
106 gpiod_set_value(pcie
->reset
, 0);
107 usleep_range(PERST_DELAY_US
, PERST_DELAY_US
+ 500);
110 static irqreturn_t
qcom_pcie_msi_irq_handler(int irq
, void *arg
)
112 struct pcie_port
*pp
= arg
;
114 return dw_handle_msi_irq(pp
);
117 static int qcom_pcie_establish_link(struct qcom_pcie
*pcie
)
121 if (dw_pcie_link_up(&pcie
->pp
))
124 /* enable link training */
125 val
= readl(pcie
->elbi
+ PCIE20_ELBI_SYS_CTRL
);
126 val
|= PCIE20_ELBI_SYS_CTRL_LT_ENABLE
;
127 writel(val
, pcie
->elbi
+ PCIE20_ELBI_SYS_CTRL
);
129 return dw_pcie_wait_for_link(&pcie
->pp
);
132 static int qcom_pcie_get_resources_v0(struct qcom_pcie
*pcie
)
134 struct qcom_pcie_resources_v0
*res
= &pcie
->res
.v0
;
135 struct device
*dev
= pcie
->dev
;
137 res
->vdda
= devm_regulator_get(dev
, "vdda");
138 if (IS_ERR(res
->vdda
))
139 return PTR_ERR(res
->vdda
);
141 res
->vdda_phy
= devm_regulator_get(dev
, "vdda_phy");
142 if (IS_ERR(res
->vdda_phy
))
143 return PTR_ERR(res
->vdda_phy
);
145 res
->vdda_refclk
= devm_regulator_get(dev
, "vdda_refclk");
146 if (IS_ERR(res
->vdda_refclk
))
147 return PTR_ERR(res
->vdda_refclk
);
149 res
->iface_clk
= devm_clk_get(dev
, "iface");
150 if (IS_ERR(res
->iface_clk
))
151 return PTR_ERR(res
->iface_clk
);
153 res
->core_clk
= devm_clk_get(dev
, "core");
154 if (IS_ERR(res
->core_clk
))
155 return PTR_ERR(res
->core_clk
);
157 res
->phy_clk
= devm_clk_get(dev
, "phy");
158 if (IS_ERR(res
->phy_clk
))
159 return PTR_ERR(res
->phy_clk
);
161 res
->pci_reset
= devm_reset_control_get(dev
, "pci");
162 if (IS_ERR(res
->pci_reset
))
163 return PTR_ERR(res
->pci_reset
);
165 res
->axi_reset
= devm_reset_control_get(dev
, "axi");
166 if (IS_ERR(res
->axi_reset
))
167 return PTR_ERR(res
->axi_reset
);
169 res
->ahb_reset
= devm_reset_control_get(dev
, "ahb");
170 if (IS_ERR(res
->ahb_reset
))
171 return PTR_ERR(res
->ahb_reset
);
173 res
->por_reset
= devm_reset_control_get(dev
, "por");
174 if (IS_ERR(res
->por_reset
))
175 return PTR_ERR(res
->por_reset
);
177 res
->phy_reset
= devm_reset_control_get(dev
, "phy");
178 if (IS_ERR(res
->phy_reset
))
179 return PTR_ERR(res
->phy_reset
);
184 static int qcom_pcie_get_resources_v1(struct qcom_pcie
*pcie
)
186 struct qcom_pcie_resources_v1
*res
= &pcie
->res
.v1
;
187 struct device
*dev
= pcie
->dev
;
189 res
->vdda
= devm_regulator_get(dev
, "vdda");
190 if (IS_ERR(res
->vdda
))
191 return PTR_ERR(res
->vdda
);
193 res
->iface
= devm_clk_get(dev
, "iface");
194 if (IS_ERR(res
->iface
))
195 return PTR_ERR(res
->iface
);
197 res
->aux
= devm_clk_get(dev
, "aux");
198 if (IS_ERR(res
->aux
))
199 return PTR_ERR(res
->aux
);
201 res
->master_bus
= devm_clk_get(dev
, "master_bus");
202 if (IS_ERR(res
->master_bus
))
203 return PTR_ERR(res
->master_bus
);
205 res
->slave_bus
= devm_clk_get(dev
, "slave_bus");
206 if (IS_ERR(res
->slave_bus
))
207 return PTR_ERR(res
->slave_bus
);
209 res
->core
= devm_reset_control_get(dev
, "core");
210 if (IS_ERR(res
->core
))
211 return PTR_ERR(res
->core
);
216 static void qcom_pcie_deinit_v0(struct qcom_pcie
*pcie
)
218 struct qcom_pcie_resources_v0
*res
= &pcie
->res
.v0
;
220 reset_control_assert(res
->pci_reset
);
221 reset_control_assert(res
->axi_reset
);
222 reset_control_assert(res
->ahb_reset
);
223 reset_control_assert(res
->por_reset
);
224 reset_control_assert(res
->pci_reset
);
225 clk_disable_unprepare(res
->iface_clk
);
226 clk_disable_unprepare(res
->core_clk
);
227 clk_disable_unprepare(res
->phy_clk
);
228 regulator_disable(res
->vdda
);
229 regulator_disable(res
->vdda_phy
);
230 regulator_disable(res
->vdda_refclk
);
233 static int qcom_pcie_init_v0(struct qcom_pcie
*pcie
)
235 struct qcom_pcie_resources_v0
*res
= &pcie
->res
.v0
;
236 struct device
*dev
= pcie
->dev
;
240 ret
= regulator_enable(res
->vdda
);
242 dev_err(dev
, "cannot enable vdda regulator\n");
246 ret
= regulator_enable(res
->vdda_refclk
);
248 dev_err(dev
, "cannot enable vdda_refclk regulator\n");
252 ret
= regulator_enable(res
->vdda_phy
);
254 dev_err(dev
, "cannot enable vdda_phy regulator\n");
258 ret
= reset_control_assert(res
->ahb_reset
);
260 dev_err(dev
, "cannot assert ahb reset\n");
264 ret
= clk_prepare_enable(res
->iface_clk
);
266 dev_err(dev
, "cannot prepare/enable iface clock\n");
270 ret
= clk_prepare_enable(res
->phy_clk
);
272 dev_err(dev
, "cannot prepare/enable phy clock\n");
276 ret
= clk_prepare_enable(res
->core_clk
);
278 dev_err(dev
, "cannot prepare/enable core clock\n");
282 ret
= reset_control_deassert(res
->ahb_reset
);
284 dev_err(dev
, "cannot deassert ahb reset\n");
285 goto err_deassert_ahb
;
288 /* enable PCIe clocks and resets */
289 val
= readl(pcie
->parf
+ PCIE20_PARF_PHY_CTRL
);
291 writel(val
, pcie
->parf
+ PCIE20_PARF_PHY_CTRL
);
293 /* enable external reference clock */
294 val
= readl(pcie
->parf
+ PCIE20_PARF_PHY_REFCLK
);
296 writel(val
, pcie
->parf
+ PCIE20_PARF_PHY_REFCLK
);
298 ret
= reset_control_deassert(res
->phy_reset
);
300 dev_err(dev
, "cannot deassert phy reset\n");
304 ret
= reset_control_deassert(res
->pci_reset
);
306 dev_err(dev
, "cannot deassert pci reset\n");
310 ret
= reset_control_deassert(res
->por_reset
);
312 dev_err(dev
, "cannot deassert por reset\n");
316 ret
= reset_control_deassert(res
->axi_reset
);
318 dev_err(dev
, "cannot deassert axi reset\n");
322 /* wait for clock acquisition */
323 usleep_range(1000, 1500);
328 clk_disable_unprepare(res
->core_clk
);
330 clk_disable_unprepare(res
->phy_clk
);
332 clk_disable_unprepare(res
->iface_clk
);
334 regulator_disable(res
->vdda_phy
);
336 regulator_disable(res
->vdda_refclk
);
338 regulator_disable(res
->vdda
);
343 static void qcom_pcie_deinit_v1(struct qcom_pcie
*pcie
)
345 struct qcom_pcie_resources_v1
*res
= &pcie
->res
.v1
;
347 reset_control_assert(res
->core
);
348 clk_disable_unprepare(res
->slave_bus
);
349 clk_disable_unprepare(res
->master_bus
);
350 clk_disable_unprepare(res
->iface
);
351 clk_disable_unprepare(res
->aux
);
352 regulator_disable(res
->vdda
);
355 static int qcom_pcie_init_v1(struct qcom_pcie
*pcie
)
357 struct qcom_pcie_resources_v1
*res
= &pcie
->res
.v1
;
358 struct device
*dev
= pcie
->dev
;
361 ret
= reset_control_deassert(res
->core
);
363 dev_err(dev
, "cannot deassert core reset\n");
367 ret
= clk_prepare_enable(res
->aux
);
369 dev_err(dev
, "cannot prepare/enable aux clock\n");
373 ret
= clk_prepare_enable(res
->iface
);
375 dev_err(dev
, "cannot prepare/enable iface clock\n");
379 ret
= clk_prepare_enable(res
->master_bus
);
381 dev_err(dev
, "cannot prepare/enable master_bus clock\n");
385 ret
= clk_prepare_enable(res
->slave_bus
);
387 dev_err(dev
, "cannot prepare/enable slave_bus clock\n");
391 ret
= regulator_enable(res
->vdda
);
393 dev_err(dev
, "cannot enable vdda regulator\n");
397 /* change DBI base address */
398 writel(0, pcie
->parf
+ PCIE20_PARF_DBI_BASE_ADDR
);
400 if (IS_ENABLED(CONFIG_PCI_MSI
)) {
401 u32 val
= readl(pcie
->parf
+ PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT
);
404 writel(val
, pcie
->parf
+ PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT
);
409 clk_disable_unprepare(res
->slave_bus
);
411 clk_disable_unprepare(res
->master_bus
);
413 clk_disable_unprepare(res
->iface
);
415 clk_disable_unprepare(res
->aux
);
417 reset_control_assert(res
->core
);
422 static int qcom_pcie_link_up(struct pcie_port
*pp
)
424 struct qcom_pcie
*pcie
= to_qcom_pcie(pp
);
425 u16 val
= readw(pcie
->dbi
+ PCIE20_CAP
+ PCI_EXP_LNKSTA
);
427 return !!(val
& PCI_EXP_LNKSTA_DLLLA
);
430 static void qcom_pcie_host_init(struct pcie_port
*pp
)
432 struct qcom_pcie
*pcie
= to_qcom_pcie(pp
);
435 qcom_ep_reset_assert(pcie
);
437 ret
= pcie
->ops
->init(pcie
);
441 ret
= phy_power_on(pcie
->phy
);
445 dw_pcie_setup_rc(pp
);
447 if (IS_ENABLED(CONFIG_PCI_MSI
))
448 dw_pcie_msi_init(pp
);
450 qcom_ep_reset_deassert(pcie
);
452 ret
= qcom_pcie_establish_link(pcie
);
458 qcom_ep_reset_assert(pcie
);
459 phy_power_off(pcie
->phy
);
461 pcie
->ops
->deinit(pcie
);
464 static int qcom_pcie_rd_own_conf(struct pcie_port
*pp
, int where
, int size
,
467 /* the device class is not reported correctly from the register */
468 if (where
== PCI_CLASS_REVISION
&& size
== 4) {
469 *val
= readl(pp
->dbi_base
+ PCI_CLASS_REVISION
);
470 *val
&= 0xff; /* keep revision id */
471 *val
|= PCI_CLASS_BRIDGE_PCI
<< 16;
472 return PCIBIOS_SUCCESSFUL
;
475 return dw_pcie_cfg_read(pp
->dbi_base
+ where
, size
, val
);
478 static struct pcie_host_ops qcom_pcie_dw_ops
= {
479 .link_up
= qcom_pcie_link_up
,
480 .host_init
= qcom_pcie_host_init
,
481 .rd_own_conf
= qcom_pcie_rd_own_conf
,
484 static const struct qcom_pcie_ops ops_v0
= {
485 .get_resources
= qcom_pcie_get_resources_v0
,
486 .init
= qcom_pcie_init_v0
,
487 .deinit
= qcom_pcie_deinit_v0
,
490 static const struct qcom_pcie_ops ops_v1
= {
491 .get_resources
= qcom_pcie_get_resources_v1
,
492 .init
= qcom_pcie_init_v1
,
493 .deinit
= qcom_pcie_deinit_v1
,
496 static int qcom_pcie_probe(struct platform_device
*pdev
)
498 struct device
*dev
= &pdev
->dev
;
499 struct resource
*res
;
500 struct qcom_pcie
*pcie
;
501 struct pcie_port
*pp
;
504 pcie
= devm_kzalloc(dev
, sizeof(*pcie
), GFP_KERNEL
);
508 pcie
->ops
= (struct qcom_pcie_ops
*)of_device_get_match_data(dev
);
511 pcie
->reset
= devm_gpiod_get_optional(dev
, "perst", GPIOD_OUT_LOW
);
512 if (IS_ERR(pcie
->reset
))
513 return PTR_ERR(pcie
->reset
);
515 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "parf");
516 pcie
->parf
= devm_ioremap_resource(dev
, res
);
517 if (IS_ERR(pcie
->parf
))
518 return PTR_ERR(pcie
->parf
);
520 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "dbi");
521 pcie
->dbi
= devm_ioremap_resource(dev
, res
);
522 if (IS_ERR(pcie
->dbi
))
523 return PTR_ERR(pcie
->dbi
);
525 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "elbi");
526 pcie
->elbi
= devm_ioremap_resource(dev
, res
);
527 if (IS_ERR(pcie
->elbi
))
528 return PTR_ERR(pcie
->elbi
);
530 pcie
->phy
= devm_phy_optional_get(dev
, "pciephy");
531 if (IS_ERR(pcie
->phy
))
532 return PTR_ERR(pcie
->phy
);
534 ret
= pcie
->ops
->get_resources(pcie
);
540 pp
->dbi_base
= pcie
->dbi
;
541 pp
->root_bus_nr
= -1;
542 pp
->ops
= &qcom_pcie_dw_ops
;
544 if (IS_ENABLED(CONFIG_PCI_MSI
)) {
545 pp
->msi_irq
= platform_get_irq_byname(pdev
, "msi");
549 ret
= devm_request_irq(dev
, pp
->msi_irq
,
550 qcom_pcie_msi_irq_handler
,
551 IRQF_SHARED
, "qcom-pcie-msi", pp
);
553 dev_err(dev
, "cannot request msi irq\n");
558 ret
= phy_init(pcie
->phy
);
562 ret
= dw_pcie_host_init(pp
);
564 dev_err(dev
, "cannot initialize host\n");
568 platform_set_drvdata(pdev
, pcie
);
573 static int qcom_pcie_remove(struct platform_device
*pdev
)
575 struct qcom_pcie
*pcie
= platform_get_drvdata(pdev
);
577 qcom_ep_reset_assert(pcie
);
578 phy_power_off(pcie
->phy
);
580 pcie
->ops
->deinit(pcie
);
585 static const struct of_device_id qcom_pcie_match
[] = {
586 { .compatible
= "qcom,pcie-ipq8064", .data
= &ops_v0
},
587 { .compatible
= "qcom,pcie-apq8064", .data
= &ops_v0
},
588 { .compatible
= "qcom,pcie-apq8084", .data
= &ops_v1
},
591 MODULE_DEVICE_TABLE(of
, qcom_pcie_match
);
593 static struct platform_driver qcom_pcie_driver
= {
594 .probe
= qcom_pcie_probe
,
595 .remove
= qcom_pcie_remove
,
598 .of_match_table
= qcom_pcie_match
,
602 module_platform_driver(qcom_pcie_driver
);
604 MODULE_AUTHOR("Stanimir Varbanov <svarbanov@mm-sol.com>");
605 MODULE_DESCRIPTION("Qualcomm PCIe root complex driver");
606 MODULE_LICENSE("GPL v2");