2 * PCIe host controller driver for Samsung EXYNOS SoCs
4 * Copyright (C) 2013 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com
7 * Author: Jingoo Han <jg1.han@samsung.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
14 #include <linux/clk.h>
15 #include <linux/delay.h>
16 #include <linux/gpio.h>
17 #include <linux/interrupt.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/of_gpio.h>
21 #include <linux/pci.h>
22 #include <linux/platform_device.h>
23 #include <linux/resource.h>
24 #include <linux/signal.h>
25 #include <linux/types.h>
27 #include "pcie-designware.h"
29 #define to_exynos_pcie(x) container_of(x, struct exynos_pcie, pp)
32 void __iomem *elbi_base;
33 void __iomem *phy_base;
34 void __iomem *block_base;
41 /* PCIe ELBI registers */
42 #define PCIE_IRQ_PULSE 0x000
43 #define IRQ_INTA_ASSERT (0x1 << 0)
44 #define IRQ_INTB_ASSERT (0x1 << 2)
45 #define IRQ_INTC_ASSERT (0x1 << 4)
46 #define IRQ_INTD_ASSERT (0x1 << 6)
47 #define PCIE_IRQ_LEVEL 0x004
48 #define PCIE_IRQ_SPECIAL 0x008
49 #define PCIE_IRQ_EN_PULSE 0x00c
50 #define PCIE_IRQ_EN_LEVEL 0x010
51 #define PCIE_IRQ_EN_SPECIAL 0x014
52 #define PCIE_PWR_RESET 0x018
53 #define PCIE_CORE_RESET 0x01c
54 #define PCIE_CORE_RESET_ENABLE (0x1 << 0)
55 #define PCIE_STICKY_RESET 0x020
56 #define PCIE_NONSTICKY_RESET 0x024
57 #define PCIE_APP_INIT_RESET 0x028
58 #define PCIE_APP_LTSSM_ENABLE 0x02c
59 #define PCIE_ELBI_RDLH_LINKUP 0x064
60 #define PCIE_ELBI_LTSSM_ENABLE 0x1
61 #define PCIE_ELBI_SLV_AWMISC 0x11c
62 #define PCIE_ELBI_SLV_ARMISC 0x120
63 #define PCIE_ELBI_SLV_DBI_ENABLE (0x1 << 21)
65 /* PCIe Purple registers */
66 #define PCIE_PHY_GLOBAL_RESET 0x000
67 #define PCIE_PHY_COMMON_RESET 0x004
68 #define PCIE_PHY_CMN_REG 0x008
69 #define PCIE_PHY_MAC_RESET 0x00c
70 #define PCIE_PHY_PLL_LOCKED 0x010
71 #define PCIE_PHY_TRSVREG_RESET 0x020
72 #define PCIE_PHY_TRSV_RESET 0x024
74 /* PCIe PHY registers */
75 #define PCIE_PHY_IMPEDANCE 0x004
76 #define PCIE_PHY_PLL_DIV_0 0x008
77 #define PCIE_PHY_PLL_BIAS 0x00c
78 #define PCIE_PHY_DCC_FEEDBACK 0x014
79 #define PCIE_PHY_PLL_DIV_1 0x05c
80 #define PCIE_PHY_TRSV0_EMP_LVL 0x084
81 #define PCIE_PHY_TRSV0_DRV_LVL 0x088
82 #define PCIE_PHY_TRSV0_RXCDR 0x0ac
83 #define PCIE_PHY_TRSV0_LVCC 0x0dc
84 #define PCIE_PHY_TRSV1_EMP_LVL 0x144
85 #define PCIE_PHY_TRSV1_RXCDR 0x16c
86 #define PCIE_PHY_TRSV1_LVCC 0x19c
87 #define PCIE_PHY_TRSV2_EMP_LVL 0x204
88 #define PCIE_PHY_TRSV2_RXCDR 0x22c
89 #define PCIE_PHY_TRSV2_LVCC 0x25c
90 #define PCIE_PHY_TRSV3_EMP_LVL 0x2c4
91 #define PCIE_PHY_TRSV3_RXCDR 0x2ec
92 #define PCIE_PHY_TRSV3_LVCC 0x31c
94 static inline void exynos_elb_writel(struct exynos_pcie *pcie, u32 val, u32 reg)
96 writel(val, pcie->elbi_base + reg);
99 static inline u32 exynos_elb_readl(struct exynos_pcie *pcie, u32 reg)
101 return readl(pcie->elbi_base + reg);
104 static inline void exynos_phy_writel(struct exynos_pcie *pcie, u32 val, u32 reg)
106 writel(val, pcie->phy_base + reg);
109 static inline u32 exynos_phy_readl(struct exynos_pcie *pcie, u32 reg)
111 return readl(pcie->phy_base + reg);
114 static inline void exynos_blk_writel(struct exynos_pcie *pcie, u32 val, u32 reg)
116 writel(val, pcie->block_base + reg);
119 static inline u32 exynos_blk_readl(struct exynos_pcie *pcie, u32 reg)
121 return readl(pcie->block_base + reg);
124 static void exynos_pcie_sideband_dbi_w_mode(struct pcie_port *pp, bool on)
127 struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
130 val = exynos_elb_readl(exynos_pcie, PCIE_ELBI_SLV_AWMISC);
131 val |= PCIE_ELBI_SLV_DBI_ENABLE;
132 exynos_elb_writel(exynos_pcie, val, PCIE_ELBI_SLV_AWMISC);
134 val = exynos_elb_readl(exynos_pcie, PCIE_ELBI_SLV_AWMISC);
135 val &= ~PCIE_ELBI_SLV_DBI_ENABLE;
136 exynos_elb_writel(exynos_pcie, val, PCIE_ELBI_SLV_AWMISC);
140 static void exynos_pcie_sideband_dbi_r_mode(struct pcie_port *pp, bool on)
143 struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
146 val = exynos_elb_readl(exynos_pcie, PCIE_ELBI_SLV_ARMISC);
147 val |= PCIE_ELBI_SLV_DBI_ENABLE;
148 exynos_elb_writel(exynos_pcie, val, PCIE_ELBI_SLV_ARMISC);
150 val = exynos_elb_readl(exynos_pcie, PCIE_ELBI_SLV_ARMISC);
151 val &= ~PCIE_ELBI_SLV_DBI_ENABLE;
152 exynos_elb_writel(exynos_pcie, val, PCIE_ELBI_SLV_ARMISC);
156 static void exynos_pcie_assert_core_reset(struct pcie_port *pp)
159 struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
161 val = exynos_elb_readl(exynos_pcie, PCIE_CORE_RESET);
162 val &= ~PCIE_CORE_RESET_ENABLE;
163 exynos_elb_writel(exynos_pcie, val, PCIE_CORE_RESET);
164 exynos_elb_writel(exynos_pcie, 0, PCIE_PWR_RESET);
165 exynos_elb_writel(exynos_pcie, 0, PCIE_STICKY_RESET);
166 exynos_elb_writel(exynos_pcie, 0, PCIE_NONSTICKY_RESET);
169 static void exynos_pcie_deassert_core_reset(struct pcie_port *pp)
172 struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
174 val = exynos_elb_readl(exynos_pcie, PCIE_CORE_RESET);
175 val |= PCIE_CORE_RESET_ENABLE;
177 exynos_elb_writel(exynos_pcie, val, PCIE_CORE_RESET);
178 exynos_elb_writel(exynos_pcie, 1, PCIE_STICKY_RESET);
179 exynos_elb_writel(exynos_pcie, 1, PCIE_NONSTICKY_RESET);
180 exynos_elb_writel(exynos_pcie, 1, PCIE_APP_INIT_RESET);
181 exynos_elb_writel(exynos_pcie, 0, PCIE_APP_INIT_RESET);
182 exynos_blk_writel(exynos_pcie, 1, PCIE_PHY_MAC_RESET);
185 static void exynos_pcie_assert_phy_reset(struct pcie_port *pp)
187 struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
189 exynos_blk_writel(exynos_pcie, 0, PCIE_PHY_MAC_RESET);
190 exynos_blk_writel(exynos_pcie, 1, PCIE_PHY_GLOBAL_RESET);
193 static void exynos_pcie_deassert_phy_reset(struct pcie_port *pp)
195 struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
197 exynos_blk_writel(exynos_pcie, 0, PCIE_PHY_GLOBAL_RESET);
198 exynos_elb_writel(exynos_pcie, 1, PCIE_PWR_RESET);
199 exynos_blk_writel(exynos_pcie, 0, PCIE_PHY_COMMON_RESET);
200 exynos_blk_writel(exynos_pcie, 0, PCIE_PHY_CMN_REG);
201 exynos_blk_writel(exynos_pcie, 0, PCIE_PHY_TRSVREG_RESET);
202 exynos_blk_writel(exynos_pcie, 0, PCIE_PHY_TRSV_RESET);
205 static void exynos_pcie_init_phy(struct pcie_port *pp)
207 struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
209 /* DCC feedback control off */
210 exynos_phy_writel(exynos_pcie, 0x29, PCIE_PHY_DCC_FEEDBACK);
212 /* set TX/RX impedance */
213 exynos_phy_writel(exynos_pcie, 0xd5, PCIE_PHY_IMPEDANCE);
215 /* set 50Mhz PHY clock */
216 exynos_phy_writel(exynos_pcie, 0x14, PCIE_PHY_PLL_DIV_0);
217 exynos_phy_writel(exynos_pcie, 0x12, PCIE_PHY_PLL_DIV_1);
219 /* set TX Differential output for lane 0 */
220 exynos_phy_writel(exynos_pcie, 0x7f, PCIE_PHY_TRSV0_DRV_LVL);
222 /* set TX Pre-emphasis Level Control for lane 0 to minimum */
223 exynos_phy_writel(exynos_pcie, 0x0, PCIE_PHY_TRSV0_EMP_LVL);
225 /* set RX clock and data recovery bandwidth */
226 exynos_phy_writel(exynos_pcie, 0xe7, PCIE_PHY_PLL_BIAS);
227 exynos_phy_writel(exynos_pcie, 0x82, PCIE_PHY_TRSV0_RXCDR);
228 exynos_phy_writel(exynos_pcie, 0x82, PCIE_PHY_TRSV1_RXCDR);
229 exynos_phy_writel(exynos_pcie, 0x82, PCIE_PHY_TRSV2_RXCDR);
230 exynos_phy_writel(exynos_pcie, 0x82, PCIE_PHY_TRSV3_RXCDR);
232 /* change TX Pre-emphasis Level Control for lanes */
233 exynos_phy_writel(exynos_pcie, 0x39, PCIE_PHY_TRSV0_EMP_LVL);
234 exynos_phy_writel(exynos_pcie, 0x39, PCIE_PHY_TRSV1_EMP_LVL);
235 exynos_phy_writel(exynos_pcie, 0x39, PCIE_PHY_TRSV2_EMP_LVL);
236 exynos_phy_writel(exynos_pcie, 0x39, PCIE_PHY_TRSV3_EMP_LVL);
239 exynos_phy_writel(exynos_pcie, 0x20, PCIE_PHY_TRSV0_LVCC);
240 exynos_phy_writel(exynos_pcie, 0xa0, PCIE_PHY_TRSV1_LVCC);
241 exynos_phy_writel(exynos_pcie, 0xa0, PCIE_PHY_TRSV2_LVCC);
242 exynos_phy_writel(exynos_pcie, 0xa0, PCIE_PHY_TRSV3_LVCC);
245 static void exynos_pcie_assert_reset(struct pcie_port *pp)
247 struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
249 if (exynos_pcie->reset_gpio >= 0)
250 devm_gpio_request_one(pp->dev, exynos_pcie->reset_gpio,
251 GPIOF_OUT_INIT_HIGH, "RESET");
255 static int exynos_pcie_establish_link(struct pcie_port *pp)
259 struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
261 if (dw_pcie_link_up(pp)) {
262 dev_err(pp->dev, "Link already up\n");
266 /* assert reset signals */
267 exynos_pcie_assert_core_reset(pp);
268 exynos_pcie_assert_phy_reset(pp);
270 /* de-assert phy reset */
271 exynos_pcie_deassert_phy_reset(pp);
274 exynos_pcie_init_phy(pp);
276 /* pulse for common reset */
277 exynos_blk_writel(exynos_pcie, 1, PCIE_PHY_COMMON_RESET);
279 exynos_blk_writel(exynos_pcie, 0, PCIE_PHY_COMMON_RESET);
281 /* de-assert core reset */
282 exynos_pcie_deassert_core_reset(pp);
284 /* setup root complex */
285 dw_pcie_setup_rc(pp);
287 /* assert reset signal */
288 exynos_pcie_assert_reset(pp);
290 /* assert LTSSM enable */
291 exynos_elb_writel(exynos_pcie, PCIE_ELBI_LTSSM_ENABLE,
292 PCIE_APP_LTSSM_ENABLE);
294 /* check if the link is up or not */
295 while (!dw_pcie_link_up(pp)) {
299 while (exynos_phy_readl(exynos_pcie,
300 PCIE_PHY_PLL_LOCKED) == 0) {
301 val = exynos_blk_readl(exynos_pcie,
302 PCIE_PHY_PLL_LOCKED);
303 dev_info(pp->dev, "PLL Locked: 0x%x\n", val);
305 dev_err(pp->dev, "PCIe Link Fail\n");
310 dev_info(pp->dev, "Link up\n");
315 static void exynos_pcie_clear_irq_pulse(struct pcie_port *pp)
318 struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
320 val = exynos_elb_readl(exynos_pcie, PCIE_IRQ_PULSE);
321 exynos_elb_writel(exynos_pcie, val, PCIE_IRQ_PULSE);
325 static void exynos_pcie_enable_irq_pulse(struct pcie_port *pp)
328 struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
330 /* enable INTX interrupt */
331 val = IRQ_INTA_ASSERT | IRQ_INTB_ASSERT |
332 IRQ_INTC_ASSERT | IRQ_INTD_ASSERT,
333 exynos_elb_writel(exynos_pcie, val, PCIE_IRQ_EN_PULSE);
337 static irqreturn_t exynos_pcie_irq_handler(int irq, void *arg)
339 struct pcie_port *pp = arg;
341 exynos_pcie_clear_irq_pulse(pp);
345 static void exynos_pcie_enable_interrupts(struct pcie_port *pp)
347 exynos_pcie_enable_irq_pulse(pp);
351 static inline void exynos_pcie_readl_rc(struct pcie_port *pp,
352 void __iomem *dbi_base, u32 *val)
354 exynos_pcie_sideband_dbi_r_mode(pp, true);
355 *val = readl(dbi_base);
356 exynos_pcie_sideband_dbi_r_mode(pp, false);
360 static inline void exynos_pcie_writel_rc(struct pcie_port *pp,
361 u32 val, void __iomem *dbi_base)
363 exynos_pcie_sideband_dbi_w_mode(pp, true);
364 writel(val, dbi_base);
365 exynos_pcie_sideband_dbi_w_mode(pp, false);
369 static int exynos_pcie_rd_own_conf(struct pcie_port *pp, int where, int size,
374 exynos_pcie_sideband_dbi_r_mode(pp, true);
375 ret = cfg_read(pp->dbi_base + (where & ~0x3), where, size, val);
376 exynos_pcie_sideband_dbi_r_mode(pp, false);
380 static int exynos_pcie_wr_own_conf(struct pcie_port *pp, int where, int size,
385 exynos_pcie_sideband_dbi_w_mode(pp, true);
386 ret = cfg_write(pp->dbi_base + (where & ~0x3), where, size, val);
387 exynos_pcie_sideband_dbi_w_mode(pp, false);
391 static int exynos_pcie_link_up(struct pcie_port *pp)
393 struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
394 u32 val = exynos_elb_readl(exynos_pcie, PCIE_ELBI_RDLH_LINKUP);
396 if (val == PCIE_ELBI_LTSSM_ENABLE)
402 static void exynos_pcie_host_init(struct pcie_port *pp)
404 exynos_pcie_establish_link(pp);
405 exynos_pcie_enable_interrupts(pp);
408 static struct pcie_host_ops exynos_pcie_host_ops = {
409 .readl_rc = exynos_pcie_readl_rc,
410 .writel_rc = exynos_pcie_writel_rc,
411 .rd_own_conf = exynos_pcie_rd_own_conf,
412 .wr_own_conf = exynos_pcie_wr_own_conf,
413 .link_up = exynos_pcie_link_up,
414 .host_init = exynos_pcie_host_init,
417 static int add_pcie_port(struct pcie_port *pp, struct platform_device *pdev)
421 pp->irq = platform_get_irq(pdev, 1);
423 dev_err(&pdev->dev, "failed to get irq\n");
426 ret = devm_request_irq(&pdev->dev, pp->irq, exynos_pcie_irq_handler,
427 IRQF_SHARED, "exynos-pcie", pp);
429 dev_err(&pdev->dev, "failed to request irq\n");
433 pp->root_bus_nr = -1;
434 pp->ops = &exynos_pcie_host_ops;
436 spin_lock_init(&pp->conf_lock);
437 ret = dw_pcie_host_init(pp);
439 dev_err(&pdev->dev, "failed to initialize host\n");
446 static int __init exynos_pcie_probe(struct platform_device *pdev)
448 struct exynos_pcie *exynos_pcie;
449 struct pcie_port *pp;
450 struct device_node *np = pdev->dev.of_node;
451 struct resource *elbi_base;
452 struct resource *phy_base;
453 struct resource *block_base;
456 exynos_pcie = devm_kzalloc(&pdev->dev, sizeof(*exynos_pcie),
459 dev_err(&pdev->dev, "no memory for exynos pcie\n");
463 pp = &exynos_pcie->pp;
465 pp->dev = &pdev->dev;
467 exynos_pcie->reset_gpio = of_get_named_gpio(np, "reset-gpio", 0);
469 exynos_pcie->clk = devm_clk_get(&pdev->dev, "pcie");
470 if (IS_ERR(exynos_pcie->clk)) {
471 dev_err(&pdev->dev, "Failed to get pcie rc clock\n");
472 return PTR_ERR(exynos_pcie->clk);
474 ret = clk_prepare_enable(exynos_pcie->clk);
478 exynos_pcie->bus_clk = devm_clk_get(&pdev->dev, "pcie_bus");
479 if (IS_ERR(exynos_pcie->bus_clk)) {
480 dev_err(&pdev->dev, "Failed to get pcie bus clock\n");
481 ret = PTR_ERR(exynos_pcie->bus_clk);
484 ret = clk_prepare_enable(exynos_pcie->bus_clk);
488 elbi_base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
489 exynos_pcie->elbi_base = devm_ioremap_resource(&pdev->dev, elbi_base);
490 if (IS_ERR(exynos_pcie->elbi_base))
491 return PTR_ERR(exynos_pcie->elbi_base);
493 phy_base = platform_get_resource(pdev, IORESOURCE_MEM, 1);
494 exynos_pcie->phy_base = devm_ioremap_resource(&pdev->dev, phy_base);
495 if (IS_ERR(exynos_pcie->phy_base))
496 return PTR_ERR(exynos_pcie->phy_base);
498 block_base = platform_get_resource(pdev, IORESOURCE_MEM, 2);
499 exynos_pcie->block_base = devm_ioremap_resource(&pdev->dev, block_base);
500 if (IS_ERR(exynos_pcie->block_base))
501 return PTR_ERR(exynos_pcie->block_base);
503 ret = add_pcie_port(pp, pdev);
507 platform_set_drvdata(pdev, exynos_pcie);
511 clk_disable_unprepare(exynos_pcie->bus_clk);
513 clk_disable_unprepare(exynos_pcie->clk);
517 static int __exit exynos_pcie_remove(struct platform_device *pdev)
519 struct exynos_pcie *exynos_pcie = platform_get_drvdata(pdev);
521 clk_disable_unprepare(exynos_pcie->bus_clk);
522 clk_disable_unprepare(exynos_pcie->clk);
527 static const struct of_device_id exynos_pcie_of_match[] = {
528 { .compatible = "samsung,exynos5440-pcie", },
531 MODULE_DEVICE_TABLE(of, exynos_pcie_of_match);
533 static struct platform_driver exynos_pcie_driver = {
534 .remove = __exit_p(exynos_pcie_remove),
536 .name = "exynos-pcie",
537 .owner = THIS_MODULE,
538 .of_match_table = of_match_ptr(exynos_pcie_of_match),
542 /* Exynos PCIe driver does not allow module unload */
544 static int __init pcie_init(void)
546 return platform_driver_probe(&exynos_pcie_driver, exynos_pcie_probe);
548 subsys_initcall(pcie_init);
550 MODULE_AUTHOR("Jingoo Han <jg1.han@samsung.com>");
551 MODULE_DESCRIPTION("Samsung PCIe host controller driver");
552 MODULE_LICENSE("GPL v2");