Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 1 | /* |
| 2 | * PCIe host controller driver for Samsung EXYNOS SoCs |
| 3 | * |
| 4 | * Copyright (C) 2013 Samsung Electronics Co., Ltd. |
| 5 | * http://www.samsung.com |
| 6 | * |
| 7 | * Author: Jingoo Han <jg1.han@samsung.com> |
| 8 | * |
| 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. |
| 12 | */ |
| 13 | |
| 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> |
Paul Gortmaker | caf5548 | 2016-08-22 17:59:47 -0400 | [diff] [blame] | 19 | #include <linux/init.h> |
Niyas Ahmed S T | 3278478 | 2017-02-01 10:13:06 +0530 | [diff] [blame^] | 20 | #include <linux/of_device.h> |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 21 | #include <linux/of_gpio.h> |
| 22 | #include <linux/pci.h> |
| 23 | #include <linux/platform_device.h> |
| 24 | #include <linux/resource.h> |
| 25 | #include <linux/signal.h> |
| 26 | #include <linux/types.h> |
| 27 | |
| 28 | #include "pcie-designware.h" |
| 29 | |
| 30 | #define to_exynos_pcie(x) container_of(x, struct exynos_pcie, pp) |
| 31 | |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 32 | /* PCIe ELBI registers */ |
| 33 | #define PCIE_IRQ_PULSE 0x000 |
Jaehoon Chung | 2681c0e | 2017-01-16 15:31:37 +0900 | [diff] [blame] | 34 | #define IRQ_INTA_ASSERT BIT(0) |
| 35 | #define IRQ_INTB_ASSERT BIT(2) |
| 36 | #define IRQ_INTC_ASSERT BIT(4) |
| 37 | #define IRQ_INTD_ASSERT BIT(6) |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 38 | #define PCIE_IRQ_LEVEL 0x004 |
| 39 | #define PCIE_IRQ_SPECIAL 0x008 |
| 40 | #define PCIE_IRQ_EN_PULSE 0x00c |
| 41 | #define PCIE_IRQ_EN_LEVEL 0x010 |
Jaehoon Chung | 2681c0e | 2017-01-16 15:31:37 +0900 | [diff] [blame] | 42 | #define IRQ_MSI_ENABLE BIT(2) |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 43 | #define PCIE_IRQ_EN_SPECIAL 0x014 |
| 44 | #define PCIE_PWR_RESET 0x018 |
| 45 | #define PCIE_CORE_RESET 0x01c |
Jaehoon Chung | 2681c0e | 2017-01-16 15:31:37 +0900 | [diff] [blame] | 46 | #define PCIE_CORE_RESET_ENABLE BIT(0) |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 47 | #define PCIE_STICKY_RESET 0x020 |
| 48 | #define PCIE_NONSTICKY_RESET 0x024 |
| 49 | #define PCIE_APP_INIT_RESET 0x028 |
| 50 | #define PCIE_APP_LTSSM_ENABLE 0x02c |
| 51 | #define PCIE_ELBI_RDLH_LINKUP 0x064 |
| 52 | #define PCIE_ELBI_LTSSM_ENABLE 0x1 |
| 53 | #define PCIE_ELBI_SLV_AWMISC 0x11c |
| 54 | #define PCIE_ELBI_SLV_ARMISC 0x120 |
Jaehoon Chung | 2681c0e | 2017-01-16 15:31:37 +0900 | [diff] [blame] | 55 | #define PCIE_ELBI_SLV_DBI_ENABLE BIT(21) |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 56 | |
| 57 | /* PCIe Purple registers */ |
| 58 | #define PCIE_PHY_GLOBAL_RESET 0x000 |
| 59 | #define PCIE_PHY_COMMON_RESET 0x004 |
| 60 | #define PCIE_PHY_CMN_REG 0x008 |
| 61 | #define PCIE_PHY_MAC_RESET 0x00c |
| 62 | #define PCIE_PHY_PLL_LOCKED 0x010 |
| 63 | #define PCIE_PHY_TRSVREG_RESET 0x020 |
| 64 | #define PCIE_PHY_TRSV_RESET 0x024 |
| 65 | |
| 66 | /* PCIe PHY registers */ |
| 67 | #define PCIE_PHY_IMPEDANCE 0x004 |
| 68 | #define PCIE_PHY_PLL_DIV_0 0x008 |
| 69 | #define PCIE_PHY_PLL_BIAS 0x00c |
| 70 | #define PCIE_PHY_DCC_FEEDBACK 0x014 |
| 71 | #define PCIE_PHY_PLL_DIV_1 0x05c |
Jingoo Han | f62b878 | 2013-09-06 17:21:45 +0900 | [diff] [blame] | 72 | #define PCIE_PHY_COMMON_POWER 0x064 |
Jaehoon Chung | 2681c0e | 2017-01-16 15:31:37 +0900 | [diff] [blame] | 73 | #define PCIE_PHY_COMMON_PD_CMN BIT(3) |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 74 | #define PCIE_PHY_TRSV0_EMP_LVL 0x084 |
| 75 | #define PCIE_PHY_TRSV0_DRV_LVL 0x088 |
| 76 | #define PCIE_PHY_TRSV0_RXCDR 0x0ac |
Jingoo Han | f62b878 | 2013-09-06 17:21:45 +0900 | [diff] [blame] | 77 | #define PCIE_PHY_TRSV0_POWER 0x0c4 |
Jaehoon Chung | 2681c0e | 2017-01-16 15:31:37 +0900 | [diff] [blame] | 78 | #define PCIE_PHY_TRSV0_PD_TSV BIT(7) |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 79 | #define PCIE_PHY_TRSV0_LVCC 0x0dc |
| 80 | #define PCIE_PHY_TRSV1_EMP_LVL 0x144 |
| 81 | #define PCIE_PHY_TRSV1_RXCDR 0x16c |
Jingoo Han | f62b878 | 2013-09-06 17:21:45 +0900 | [diff] [blame] | 82 | #define PCIE_PHY_TRSV1_POWER 0x184 |
Jaehoon Chung | 2681c0e | 2017-01-16 15:31:37 +0900 | [diff] [blame] | 83 | #define PCIE_PHY_TRSV1_PD_TSV BIT(7) |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 84 | #define PCIE_PHY_TRSV1_LVCC 0x19c |
| 85 | #define PCIE_PHY_TRSV2_EMP_LVL 0x204 |
| 86 | #define PCIE_PHY_TRSV2_RXCDR 0x22c |
Jingoo Han | f62b878 | 2013-09-06 17:21:45 +0900 | [diff] [blame] | 87 | #define PCIE_PHY_TRSV2_POWER 0x244 |
Jaehoon Chung | 2681c0e | 2017-01-16 15:31:37 +0900 | [diff] [blame] | 88 | #define PCIE_PHY_TRSV2_PD_TSV BIT(7) |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 89 | #define PCIE_PHY_TRSV2_LVCC 0x25c |
| 90 | #define PCIE_PHY_TRSV3_EMP_LVL 0x2c4 |
| 91 | #define PCIE_PHY_TRSV3_RXCDR 0x2ec |
Jingoo Han | f62b878 | 2013-09-06 17:21:45 +0900 | [diff] [blame] | 92 | #define PCIE_PHY_TRSV3_POWER 0x304 |
Jaehoon Chung | 2681c0e | 2017-01-16 15:31:37 +0900 | [diff] [blame] | 93 | #define PCIE_PHY_TRSV3_PD_TSV BIT(7) |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 94 | #define PCIE_PHY_TRSV3_LVCC 0x31c |
| 95 | |
Niyas Ahmed S T | 3278478 | 2017-02-01 10:13:06 +0530 | [diff] [blame^] | 96 | struct exynos_pcie_mem_res { |
| 97 | void __iomem *elbi_base; /* DT 0th resource: PCIe CTRL */ |
| 98 | void __iomem *phy_base; /* DT 1st resource: PHY CTRL */ |
| 99 | void __iomem *block_base; /* DT 2nd resource: PHY ADDITIONAL CTRL */ |
| 100 | }; |
| 101 | |
| 102 | struct exynos_pcie_clk_res { |
| 103 | struct clk *clk; |
| 104 | struct clk *bus_clk; |
| 105 | }; |
| 106 | |
| 107 | struct exynos_pcie { |
| 108 | struct pcie_port pp; |
| 109 | struct exynos_pcie_mem_res *mem_res; |
| 110 | struct exynos_pcie_clk_res *clk_res; |
| 111 | const struct exynos_pcie_ops *ops; |
| 112 | int reset_gpio; |
| 113 | }; |
| 114 | |
| 115 | struct exynos_pcie_ops { |
| 116 | int (*get_mem_resources)(struct platform_device *pdev, |
| 117 | struct exynos_pcie *ep); |
| 118 | int (*get_clk_resources)(struct exynos_pcie *ep); |
| 119 | int (*init_clk_resources)(struct exynos_pcie *ep); |
| 120 | void (*deinit_clk_resources)(struct exynos_pcie *ep); |
| 121 | }; |
| 122 | |
| 123 | static int exynos5440_pcie_get_mem_resources(struct platform_device *pdev, |
| 124 | struct exynos_pcie *ep) |
| 125 | { |
| 126 | struct resource *res; |
| 127 | struct device *dev = ep->pp.dev; |
| 128 | |
| 129 | ep->mem_res = devm_kzalloc(dev, sizeof(*ep->mem_res), GFP_KERNEL); |
| 130 | if (!ep->mem_res) |
| 131 | return -ENOMEM; |
| 132 | |
| 133 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 134 | ep->mem_res->elbi_base = devm_ioremap_resource(dev, res); |
| 135 | if (IS_ERR(ep->mem_res->elbi_base)) |
| 136 | return PTR_ERR(ep->mem_res->elbi_base); |
| 137 | |
| 138 | res = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
| 139 | ep->mem_res->phy_base = devm_ioremap_resource(dev, res); |
| 140 | if (IS_ERR(ep->mem_res->phy_base)) |
| 141 | return PTR_ERR(ep->mem_res->phy_base); |
| 142 | |
| 143 | res = platform_get_resource(pdev, IORESOURCE_MEM, 2); |
| 144 | ep->mem_res->block_base = devm_ioremap_resource(dev, res); |
| 145 | if (IS_ERR(ep->mem_res->block_base)) |
| 146 | return PTR_ERR(ep->mem_res->block_base); |
| 147 | |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | static int exynos5440_pcie_get_clk_resources(struct exynos_pcie *ep) |
| 152 | { |
| 153 | struct device *dev = ep->pp.dev; |
| 154 | |
| 155 | ep->clk_res = devm_kzalloc(dev, sizeof(*ep->clk_res), GFP_KERNEL); |
| 156 | if (!ep->clk_res) |
| 157 | return -ENOMEM; |
| 158 | |
| 159 | ep->clk_res->clk = devm_clk_get(dev, "pcie"); |
| 160 | if (IS_ERR(ep->clk_res->clk)) { |
| 161 | dev_err(dev, "Failed to get pcie rc clock\n"); |
| 162 | return PTR_ERR(ep->clk_res->clk); |
| 163 | } |
| 164 | |
| 165 | ep->clk_res->bus_clk = devm_clk_get(dev, "pcie_bus"); |
| 166 | if (IS_ERR(ep->clk_res->bus_clk)) { |
| 167 | dev_err(dev, "Failed to get pcie bus clock\n"); |
| 168 | return PTR_ERR(ep->clk_res->bus_clk); |
| 169 | } |
| 170 | |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | static int exynos5440_pcie_init_clk_resources(struct exynos_pcie *ep) |
| 175 | { |
| 176 | struct device *dev = ep->pp.dev; |
| 177 | int ret; |
| 178 | |
| 179 | ret = clk_prepare_enable(ep->clk_res->clk); |
| 180 | if (ret) { |
| 181 | dev_err(dev, "cannot enable pcie rc clock"); |
| 182 | return ret; |
| 183 | } |
| 184 | |
| 185 | ret = clk_prepare_enable(ep->clk_res->bus_clk); |
| 186 | if (ret) { |
| 187 | dev_err(dev, "cannot enable pcie bus clock"); |
| 188 | goto err_bus_clk; |
| 189 | } |
| 190 | |
| 191 | return 0; |
| 192 | |
| 193 | err_bus_clk: |
| 194 | clk_disable_unprepare(ep->clk_res->clk); |
| 195 | |
| 196 | return ret; |
| 197 | } |
| 198 | |
| 199 | static void exynos5440_pcie_deinit_clk_resources(struct exynos_pcie *ep) |
| 200 | { |
| 201 | clk_disable_unprepare(ep->clk_res->bus_clk); |
| 202 | clk_disable_unprepare(ep->clk_res->clk); |
| 203 | } |
| 204 | |
| 205 | static const struct exynos_pcie_ops exynos5440_pcie_ops = { |
| 206 | .get_mem_resources = exynos5440_pcie_get_mem_resources, |
| 207 | .get_clk_resources = exynos5440_pcie_get_clk_resources, |
| 208 | .init_clk_resources = exynos5440_pcie_init_clk_resources, |
| 209 | .deinit_clk_resources = exynos5440_pcie_deinit_clk_resources, |
| 210 | }; |
| 211 | |
Jaehoon Chung | d6da7d9 | 2017-01-16 15:31:35 +0900 | [diff] [blame] | 212 | static void exynos_pcie_writel(void __iomem *base, u32 val, u32 reg) |
Seungwon Jeon | 058dd01 | 2013-08-29 21:35:56 +0900 | [diff] [blame] | 213 | { |
Jaehoon Chung | d6da7d9 | 2017-01-16 15:31:35 +0900 | [diff] [blame] | 214 | writel(val, base + reg); |
Seungwon Jeon | 058dd01 | 2013-08-29 21:35:56 +0900 | [diff] [blame] | 215 | } |
| 216 | |
Jaehoon Chung | d6da7d9 | 2017-01-16 15:31:35 +0900 | [diff] [blame] | 217 | static u32 exynos_pcie_readl(void __iomem *base, u32 reg) |
Seungwon Jeon | 058dd01 | 2013-08-29 21:35:56 +0900 | [diff] [blame] | 218 | { |
Jaehoon Chung | d6da7d9 | 2017-01-16 15:31:35 +0900 | [diff] [blame] | 219 | return readl(base + reg); |
Seungwon Jeon | 058dd01 | 2013-08-29 21:35:56 +0900 | [diff] [blame] | 220 | } |
| 221 | |
Jaehoon Chung | 4e0a90b38 | 2017-01-16 15:31:34 +0900 | [diff] [blame] | 222 | static void exynos_pcie_sideband_dbi_w_mode(struct exynos_pcie *ep, bool on) |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 223 | { |
| 224 | u32 val; |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 225 | |
Niyas Ahmed S T | 3278478 | 2017-02-01 10:13:06 +0530 | [diff] [blame^] | 226 | val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_ELBI_SLV_AWMISC); |
Jaehoon Chung | 92004a06 | 2017-01-16 15:31:38 +0900 | [diff] [blame] | 227 | if (on) |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 228 | val |= PCIE_ELBI_SLV_DBI_ENABLE; |
Jaehoon Chung | 92004a06 | 2017-01-16 15:31:38 +0900 | [diff] [blame] | 229 | else |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 230 | val &= ~PCIE_ELBI_SLV_DBI_ENABLE; |
Niyas Ahmed S T | 3278478 | 2017-02-01 10:13:06 +0530 | [diff] [blame^] | 231 | exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_ELBI_SLV_AWMISC); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 232 | } |
| 233 | |
Jaehoon Chung | 4e0a90b38 | 2017-01-16 15:31:34 +0900 | [diff] [blame] | 234 | static void exynos_pcie_sideband_dbi_r_mode(struct exynos_pcie *ep, bool on) |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 235 | { |
| 236 | u32 val; |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 237 | |
Niyas Ahmed S T | 3278478 | 2017-02-01 10:13:06 +0530 | [diff] [blame^] | 238 | val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_ELBI_SLV_ARMISC); |
Jaehoon Chung | 92004a06 | 2017-01-16 15:31:38 +0900 | [diff] [blame] | 239 | if (on) |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 240 | val |= PCIE_ELBI_SLV_DBI_ENABLE; |
Jaehoon Chung | 92004a06 | 2017-01-16 15:31:38 +0900 | [diff] [blame] | 241 | else |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 242 | val &= ~PCIE_ELBI_SLV_DBI_ENABLE; |
Niyas Ahmed S T | 3278478 | 2017-02-01 10:13:06 +0530 | [diff] [blame^] | 243 | exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_ELBI_SLV_ARMISC); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 244 | } |
| 245 | |
Jaehoon Chung | 4e0a90b38 | 2017-01-16 15:31:34 +0900 | [diff] [blame] | 246 | static void exynos_pcie_assert_core_reset(struct exynos_pcie *ep) |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 247 | { |
| 248 | u32 val; |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 249 | |
Niyas Ahmed S T | 3278478 | 2017-02-01 10:13:06 +0530 | [diff] [blame^] | 250 | val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_CORE_RESET); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 251 | val &= ~PCIE_CORE_RESET_ENABLE; |
Niyas Ahmed S T | 3278478 | 2017-02-01 10:13:06 +0530 | [diff] [blame^] | 252 | exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_CORE_RESET); |
| 253 | exynos_pcie_writel(ep->mem_res->elbi_base, 0, PCIE_PWR_RESET); |
| 254 | exynos_pcie_writel(ep->mem_res->elbi_base, 0, PCIE_STICKY_RESET); |
| 255 | exynos_pcie_writel(ep->mem_res->elbi_base, 0, PCIE_NONSTICKY_RESET); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 256 | } |
| 257 | |
Jaehoon Chung | 4e0a90b38 | 2017-01-16 15:31:34 +0900 | [diff] [blame] | 258 | static void exynos_pcie_deassert_core_reset(struct exynos_pcie *ep) |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 259 | { |
| 260 | u32 val; |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 261 | |
Niyas Ahmed S T | 3278478 | 2017-02-01 10:13:06 +0530 | [diff] [blame^] | 262 | val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_CORE_RESET); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 263 | val |= PCIE_CORE_RESET_ENABLE; |
Seungwon Jeon | 058dd01 | 2013-08-29 21:35:56 +0900 | [diff] [blame] | 264 | |
Niyas Ahmed S T | 3278478 | 2017-02-01 10:13:06 +0530 | [diff] [blame^] | 265 | exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_CORE_RESET); |
| 266 | exynos_pcie_writel(ep->mem_res->elbi_base, 1, PCIE_STICKY_RESET); |
| 267 | exynos_pcie_writel(ep->mem_res->elbi_base, 1, PCIE_NONSTICKY_RESET); |
| 268 | exynos_pcie_writel(ep->mem_res->elbi_base, 1, PCIE_APP_INIT_RESET); |
| 269 | exynos_pcie_writel(ep->mem_res->elbi_base, 0, PCIE_APP_INIT_RESET); |
| 270 | exynos_pcie_writel(ep->mem_res->block_base, 1, PCIE_PHY_MAC_RESET); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 271 | } |
| 272 | |
Jaehoon Chung | 4e0a90b38 | 2017-01-16 15:31:34 +0900 | [diff] [blame] | 273 | static void exynos_pcie_assert_phy_reset(struct exynos_pcie *ep) |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 274 | { |
Niyas Ahmed S T | 3278478 | 2017-02-01 10:13:06 +0530 | [diff] [blame^] | 275 | exynos_pcie_writel(ep->mem_res->block_base, 0, PCIE_PHY_MAC_RESET); |
| 276 | exynos_pcie_writel(ep->mem_res->block_base, 1, PCIE_PHY_GLOBAL_RESET); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 277 | } |
| 278 | |
Jaehoon Chung | 4e0a90b38 | 2017-01-16 15:31:34 +0900 | [diff] [blame] | 279 | static void exynos_pcie_deassert_phy_reset(struct exynos_pcie *ep) |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 280 | { |
Niyas Ahmed S T | 3278478 | 2017-02-01 10:13:06 +0530 | [diff] [blame^] | 281 | exynos_pcie_writel(ep->mem_res->block_base, 0, PCIE_PHY_GLOBAL_RESET); |
| 282 | exynos_pcie_writel(ep->mem_res->elbi_base, 1, PCIE_PWR_RESET); |
| 283 | exynos_pcie_writel(ep->mem_res->block_base, 0, PCIE_PHY_COMMON_RESET); |
| 284 | exynos_pcie_writel(ep->mem_res->block_base, 0, PCIE_PHY_CMN_REG); |
| 285 | exynos_pcie_writel(ep->mem_res->block_base, 0, PCIE_PHY_TRSVREG_RESET); |
| 286 | exynos_pcie_writel(ep->mem_res->block_base, 0, PCIE_PHY_TRSV_RESET); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 287 | } |
| 288 | |
Jaehoon Chung | 4e0a90b38 | 2017-01-16 15:31:34 +0900 | [diff] [blame] | 289 | static void exynos_pcie_power_on_phy(struct exynos_pcie *ep) |
Jingoo Han | f62b878 | 2013-09-06 17:21:45 +0900 | [diff] [blame] | 290 | { |
| 291 | u32 val; |
Jingoo Han | f62b878 | 2013-09-06 17:21:45 +0900 | [diff] [blame] | 292 | |
Niyas Ahmed S T | 3278478 | 2017-02-01 10:13:06 +0530 | [diff] [blame^] | 293 | val = exynos_pcie_readl(ep->mem_res->phy_base, PCIE_PHY_COMMON_POWER); |
Jingoo Han | f62b878 | 2013-09-06 17:21:45 +0900 | [diff] [blame] | 294 | val &= ~PCIE_PHY_COMMON_PD_CMN; |
Niyas Ahmed S T | 3278478 | 2017-02-01 10:13:06 +0530 | [diff] [blame^] | 295 | exynos_pcie_writel(ep->mem_res->phy_base, val, PCIE_PHY_COMMON_POWER); |
Jingoo Han | f62b878 | 2013-09-06 17:21:45 +0900 | [diff] [blame] | 296 | |
Niyas Ahmed S T | 3278478 | 2017-02-01 10:13:06 +0530 | [diff] [blame^] | 297 | val = exynos_pcie_readl(ep->mem_res->phy_base, PCIE_PHY_TRSV0_POWER); |
Jingoo Han | f62b878 | 2013-09-06 17:21:45 +0900 | [diff] [blame] | 298 | val &= ~PCIE_PHY_TRSV0_PD_TSV; |
Niyas Ahmed S T | 3278478 | 2017-02-01 10:13:06 +0530 | [diff] [blame^] | 299 | exynos_pcie_writel(ep->mem_res->phy_base, val, PCIE_PHY_TRSV0_POWER); |
Jingoo Han | f62b878 | 2013-09-06 17:21:45 +0900 | [diff] [blame] | 300 | |
Niyas Ahmed S T | 3278478 | 2017-02-01 10:13:06 +0530 | [diff] [blame^] | 301 | val = exynos_pcie_readl(ep->mem_res->phy_base, PCIE_PHY_TRSV1_POWER); |
Jingoo Han | f62b878 | 2013-09-06 17:21:45 +0900 | [diff] [blame] | 302 | val &= ~PCIE_PHY_TRSV1_PD_TSV; |
Niyas Ahmed S T | 3278478 | 2017-02-01 10:13:06 +0530 | [diff] [blame^] | 303 | exynos_pcie_writel(ep->mem_res->phy_base, val, PCIE_PHY_TRSV1_POWER); |
Jingoo Han | f62b878 | 2013-09-06 17:21:45 +0900 | [diff] [blame] | 304 | |
Niyas Ahmed S T | 3278478 | 2017-02-01 10:13:06 +0530 | [diff] [blame^] | 305 | val = exynos_pcie_readl(ep->mem_res->phy_base, PCIE_PHY_TRSV2_POWER); |
Jingoo Han | f62b878 | 2013-09-06 17:21:45 +0900 | [diff] [blame] | 306 | val &= ~PCIE_PHY_TRSV2_PD_TSV; |
Niyas Ahmed S T | 3278478 | 2017-02-01 10:13:06 +0530 | [diff] [blame^] | 307 | exynos_pcie_writel(ep->mem_res->phy_base, val, PCIE_PHY_TRSV2_POWER); |
Jingoo Han | f62b878 | 2013-09-06 17:21:45 +0900 | [diff] [blame] | 308 | |
Niyas Ahmed S T | 3278478 | 2017-02-01 10:13:06 +0530 | [diff] [blame^] | 309 | val = exynos_pcie_readl(ep->mem_res->phy_base, PCIE_PHY_TRSV3_POWER); |
Jingoo Han | f62b878 | 2013-09-06 17:21:45 +0900 | [diff] [blame] | 310 | val &= ~PCIE_PHY_TRSV3_PD_TSV; |
Niyas Ahmed S T | 3278478 | 2017-02-01 10:13:06 +0530 | [diff] [blame^] | 311 | exynos_pcie_writel(ep->mem_res->phy_base, val, PCIE_PHY_TRSV3_POWER); |
Jingoo Han | f62b878 | 2013-09-06 17:21:45 +0900 | [diff] [blame] | 312 | } |
| 313 | |
Jaehoon Chung | 4e0a90b38 | 2017-01-16 15:31:34 +0900 | [diff] [blame] | 314 | static void exynos_pcie_power_off_phy(struct exynos_pcie *ep) |
Jingoo Han | f62b878 | 2013-09-06 17:21:45 +0900 | [diff] [blame] | 315 | { |
| 316 | u32 val; |
Jingoo Han | f62b878 | 2013-09-06 17:21:45 +0900 | [diff] [blame] | 317 | |
Niyas Ahmed S T | 3278478 | 2017-02-01 10:13:06 +0530 | [diff] [blame^] | 318 | val = exynos_pcie_readl(ep->mem_res->phy_base, PCIE_PHY_COMMON_POWER); |
Jingoo Han | f62b878 | 2013-09-06 17:21:45 +0900 | [diff] [blame] | 319 | val |= PCIE_PHY_COMMON_PD_CMN; |
Niyas Ahmed S T | 3278478 | 2017-02-01 10:13:06 +0530 | [diff] [blame^] | 320 | exynos_pcie_writel(ep->mem_res->phy_base, val, PCIE_PHY_COMMON_POWER); |
Jingoo Han | f62b878 | 2013-09-06 17:21:45 +0900 | [diff] [blame] | 321 | |
Niyas Ahmed S T | 3278478 | 2017-02-01 10:13:06 +0530 | [diff] [blame^] | 322 | val = exynos_pcie_readl(ep->mem_res->phy_base, PCIE_PHY_TRSV0_POWER); |
Jingoo Han | f62b878 | 2013-09-06 17:21:45 +0900 | [diff] [blame] | 323 | val |= PCIE_PHY_TRSV0_PD_TSV; |
Niyas Ahmed S T | 3278478 | 2017-02-01 10:13:06 +0530 | [diff] [blame^] | 324 | exynos_pcie_writel(ep->mem_res->phy_base, val, PCIE_PHY_TRSV0_POWER); |
Jingoo Han | f62b878 | 2013-09-06 17:21:45 +0900 | [diff] [blame] | 325 | |
Niyas Ahmed S T | 3278478 | 2017-02-01 10:13:06 +0530 | [diff] [blame^] | 326 | val = exynos_pcie_readl(ep->mem_res->phy_base, PCIE_PHY_TRSV1_POWER); |
Jingoo Han | f62b878 | 2013-09-06 17:21:45 +0900 | [diff] [blame] | 327 | val |= PCIE_PHY_TRSV1_PD_TSV; |
Niyas Ahmed S T | 3278478 | 2017-02-01 10:13:06 +0530 | [diff] [blame^] | 328 | exynos_pcie_writel(ep->mem_res->phy_base, val, PCIE_PHY_TRSV1_POWER); |
Jingoo Han | f62b878 | 2013-09-06 17:21:45 +0900 | [diff] [blame] | 329 | |
Niyas Ahmed S T | 3278478 | 2017-02-01 10:13:06 +0530 | [diff] [blame^] | 330 | val = exynos_pcie_readl(ep->mem_res->phy_base, PCIE_PHY_TRSV2_POWER); |
Jingoo Han | f62b878 | 2013-09-06 17:21:45 +0900 | [diff] [blame] | 331 | val |= PCIE_PHY_TRSV2_PD_TSV; |
Niyas Ahmed S T | 3278478 | 2017-02-01 10:13:06 +0530 | [diff] [blame^] | 332 | exynos_pcie_writel(ep->mem_res->phy_base, val, PCIE_PHY_TRSV2_POWER); |
Jingoo Han | f62b878 | 2013-09-06 17:21:45 +0900 | [diff] [blame] | 333 | |
Niyas Ahmed S T | 3278478 | 2017-02-01 10:13:06 +0530 | [diff] [blame^] | 334 | val = exynos_pcie_readl(ep->mem_res->phy_base, PCIE_PHY_TRSV3_POWER); |
Jingoo Han | f62b878 | 2013-09-06 17:21:45 +0900 | [diff] [blame] | 335 | val |= PCIE_PHY_TRSV3_PD_TSV; |
Niyas Ahmed S T | 3278478 | 2017-02-01 10:13:06 +0530 | [diff] [blame^] | 336 | exynos_pcie_writel(ep->mem_res->phy_base, val, PCIE_PHY_TRSV3_POWER); |
Jingoo Han | f62b878 | 2013-09-06 17:21:45 +0900 | [diff] [blame] | 337 | } |
| 338 | |
Jaehoon Chung | 4e0a90b38 | 2017-01-16 15:31:34 +0900 | [diff] [blame] | 339 | static void exynos_pcie_init_phy(struct exynos_pcie *ep) |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 340 | { |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 341 | /* DCC feedback control off */ |
Niyas Ahmed S T | 3278478 | 2017-02-01 10:13:06 +0530 | [diff] [blame^] | 342 | exynos_pcie_writel(ep->mem_res->phy_base, 0x29, PCIE_PHY_DCC_FEEDBACK); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 343 | |
| 344 | /* set TX/RX impedance */ |
Niyas Ahmed S T | 3278478 | 2017-02-01 10:13:06 +0530 | [diff] [blame^] | 345 | exynos_pcie_writel(ep->mem_res->phy_base, 0xd5, PCIE_PHY_IMPEDANCE); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 346 | |
| 347 | /* set 50Mhz PHY clock */ |
Niyas Ahmed S T | 3278478 | 2017-02-01 10:13:06 +0530 | [diff] [blame^] | 348 | exynos_pcie_writel(ep->mem_res->phy_base, 0x14, PCIE_PHY_PLL_DIV_0); |
| 349 | exynos_pcie_writel(ep->mem_res->phy_base, 0x12, PCIE_PHY_PLL_DIV_1); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 350 | |
| 351 | /* set TX Differential output for lane 0 */ |
Niyas Ahmed S T | 3278478 | 2017-02-01 10:13:06 +0530 | [diff] [blame^] | 352 | exynos_pcie_writel(ep->mem_res->phy_base, 0x7f, PCIE_PHY_TRSV0_DRV_LVL); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 353 | |
| 354 | /* set TX Pre-emphasis Level Control for lane 0 to minimum */ |
Niyas Ahmed S T | 3278478 | 2017-02-01 10:13:06 +0530 | [diff] [blame^] | 355 | exynos_pcie_writel(ep->mem_res->phy_base, 0x0, PCIE_PHY_TRSV0_EMP_LVL); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 356 | |
| 357 | /* set RX clock and data recovery bandwidth */ |
Niyas Ahmed S T | 3278478 | 2017-02-01 10:13:06 +0530 | [diff] [blame^] | 358 | exynos_pcie_writel(ep->mem_res->phy_base, 0xe7, PCIE_PHY_PLL_BIAS); |
| 359 | exynos_pcie_writel(ep->mem_res->phy_base, 0x82, PCIE_PHY_TRSV0_RXCDR); |
| 360 | exynos_pcie_writel(ep->mem_res->phy_base, 0x82, PCIE_PHY_TRSV1_RXCDR); |
| 361 | exynos_pcie_writel(ep->mem_res->phy_base, 0x82, PCIE_PHY_TRSV2_RXCDR); |
| 362 | exynos_pcie_writel(ep->mem_res->phy_base, 0x82, PCIE_PHY_TRSV3_RXCDR); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 363 | |
| 364 | /* change TX Pre-emphasis Level Control for lanes */ |
Niyas Ahmed S T | 3278478 | 2017-02-01 10:13:06 +0530 | [diff] [blame^] | 365 | exynos_pcie_writel(ep->mem_res->phy_base, 0x39, PCIE_PHY_TRSV0_EMP_LVL); |
| 366 | exynos_pcie_writel(ep->mem_res->phy_base, 0x39, PCIE_PHY_TRSV1_EMP_LVL); |
| 367 | exynos_pcie_writel(ep->mem_res->phy_base, 0x39, PCIE_PHY_TRSV2_EMP_LVL); |
| 368 | exynos_pcie_writel(ep->mem_res->phy_base, 0x39, PCIE_PHY_TRSV3_EMP_LVL); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 369 | |
| 370 | /* set LVCC */ |
Niyas Ahmed S T | 3278478 | 2017-02-01 10:13:06 +0530 | [diff] [blame^] | 371 | exynos_pcie_writel(ep->mem_res->phy_base, 0x20, PCIE_PHY_TRSV0_LVCC); |
| 372 | exynos_pcie_writel(ep->mem_res->phy_base, 0xa0, PCIE_PHY_TRSV1_LVCC); |
| 373 | exynos_pcie_writel(ep->mem_res->phy_base, 0xa0, PCIE_PHY_TRSV2_LVCC); |
| 374 | exynos_pcie_writel(ep->mem_res->phy_base, 0xa0, PCIE_PHY_TRSV3_LVCC); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 375 | } |
| 376 | |
Jaehoon Chung | 4e0a90b38 | 2017-01-16 15:31:34 +0900 | [diff] [blame] | 377 | static void exynos_pcie_assert_reset(struct exynos_pcie *ep) |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 378 | { |
Jaehoon Chung | 4e0a90b38 | 2017-01-16 15:31:34 +0900 | [diff] [blame] | 379 | struct pcie_port *pp = &ep->pp; |
Bjorn Helgaas | fae68d6 | 2016-10-06 13:33:41 -0500 | [diff] [blame] | 380 | struct device *dev = pp->dev; |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 381 | |
Jaehoon Chung | 4e0a90b38 | 2017-01-16 15:31:34 +0900 | [diff] [blame] | 382 | if (ep->reset_gpio >= 0) |
| 383 | devm_gpio_request_one(dev, ep->reset_gpio, |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 384 | GPIOF_OUT_INIT_HIGH, "RESET"); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 385 | } |
| 386 | |
Jaehoon Chung | 4e0a90b38 | 2017-01-16 15:31:34 +0900 | [diff] [blame] | 387 | static int exynos_pcie_establish_link(struct exynos_pcie *ep) |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 388 | { |
Jaehoon Chung | 4e0a90b38 | 2017-01-16 15:31:34 +0900 | [diff] [blame] | 389 | struct pcie_port *pp = &ep->pp; |
Bjorn Helgaas | fae68d6 | 2016-10-06 13:33:41 -0500 | [diff] [blame] | 390 | struct device *dev = pp->dev; |
Bjorn Helgaas | 6cbb247 | 2015-06-02 16:47:17 -0500 | [diff] [blame] | 391 | u32 val; |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 392 | |
| 393 | if (dw_pcie_link_up(pp)) { |
Bjorn Helgaas | fae68d6 | 2016-10-06 13:33:41 -0500 | [diff] [blame] | 394 | dev_err(dev, "Link already up\n"); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 395 | return 0; |
| 396 | } |
| 397 | |
Jaehoon Chung | 4e0a90b38 | 2017-01-16 15:31:34 +0900 | [diff] [blame] | 398 | exynos_pcie_assert_core_reset(ep); |
| 399 | exynos_pcie_assert_phy_reset(ep); |
| 400 | exynos_pcie_deassert_phy_reset(ep); |
| 401 | exynos_pcie_power_on_phy(ep); |
| 402 | exynos_pcie_init_phy(ep); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 403 | |
| 404 | /* pulse for common reset */ |
Niyas Ahmed S T | 3278478 | 2017-02-01 10:13:06 +0530 | [diff] [blame^] | 405 | exynos_pcie_writel(ep->mem_res->block_base, 1, PCIE_PHY_COMMON_RESET); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 406 | udelay(500); |
Niyas Ahmed S T | 3278478 | 2017-02-01 10:13:06 +0530 | [diff] [blame^] | 407 | exynos_pcie_writel(ep->mem_res->block_base, 0, PCIE_PHY_COMMON_RESET); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 408 | |
Jaehoon Chung | 4e0a90b38 | 2017-01-16 15:31:34 +0900 | [diff] [blame] | 409 | exynos_pcie_deassert_core_reset(ep); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 410 | dw_pcie_setup_rc(pp); |
Jaehoon Chung | 4e0a90b38 | 2017-01-16 15:31:34 +0900 | [diff] [blame] | 411 | exynos_pcie_assert_reset(ep); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 412 | |
| 413 | /* assert LTSSM enable */ |
Niyas Ahmed S T | 3278478 | 2017-02-01 10:13:06 +0530 | [diff] [blame^] | 414 | exynos_pcie_writel(ep->mem_res->elbi_base, PCIE_ELBI_LTSSM_ENABLE, |
Seungwon Jeon | 058dd01 | 2013-08-29 21:35:56 +0900 | [diff] [blame] | 415 | PCIE_APP_LTSSM_ENABLE); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 416 | |
| 417 | /* check if the link is up or not */ |
Joao Pinto | 886bc5c | 2016-03-10 14:44:35 -0600 | [diff] [blame] | 418 | if (!dw_pcie_wait_for_link(pp)) |
| 419 | return 0; |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 420 | |
Niyas Ahmed S T | 3278478 | 2017-02-01 10:13:06 +0530 | [diff] [blame^] | 421 | while (exynos_pcie_readl(ep->mem_res->phy_base, |
| 422 | PCIE_PHY_PLL_LOCKED) == 0) { |
| 423 | val = exynos_pcie_readl(ep->mem_res->block_base, |
| 424 | PCIE_PHY_PLL_LOCKED); |
Bjorn Helgaas | fae68d6 | 2016-10-06 13:33:41 -0500 | [diff] [blame] | 425 | dev_info(dev, "PLL Locked: 0x%x\n", val); |
Bjorn Helgaas | 6cbb247 | 2015-06-02 16:47:17 -0500 | [diff] [blame] | 426 | } |
Jaehoon Chung | 4e0a90b38 | 2017-01-16 15:31:34 +0900 | [diff] [blame] | 427 | exynos_pcie_power_off_phy(ep); |
Joao Pinto | 886bc5c | 2016-03-10 14:44:35 -0600 | [diff] [blame] | 428 | return -ETIMEDOUT; |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 429 | } |
| 430 | |
Jaehoon Chung | 4e0a90b38 | 2017-01-16 15:31:34 +0900 | [diff] [blame] | 431 | static void exynos_pcie_clear_irq_pulse(struct exynos_pcie *ep) |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 432 | { |
| 433 | u32 val; |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 434 | |
Niyas Ahmed S T | 3278478 | 2017-02-01 10:13:06 +0530 | [diff] [blame^] | 435 | val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_IRQ_PULSE); |
| 436 | exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_IRQ_PULSE); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 437 | } |
| 438 | |
Jaehoon Chung | 4e0a90b38 | 2017-01-16 15:31:34 +0900 | [diff] [blame] | 439 | static void exynos_pcie_enable_irq_pulse(struct exynos_pcie *ep) |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 440 | { |
| 441 | u32 val; |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 442 | |
| 443 | /* enable INTX interrupt */ |
| 444 | val = IRQ_INTA_ASSERT | IRQ_INTB_ASSERT | |
Jaehoon Chung | 01d06a9 | 2015-03-25 14:13:12 +0900 | [diff] [blame] | 445 | IRQ_INTC_ASSERT | IRQ_INTD_ASSERT; |
Niyas Ahmed S T | 3278478 | 2017-02-01 10:13:06 +0530 | [diff] [blame^] | 446 | exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_IRQ_EN_PULSE); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | static irqreturn_t exynos_pcie_irq_handler(int irq, void *arg) |
| 450 | { |
Jaehoon Chung | 4e0a90b38 | 2017-01-16 15:31:34 +0900 | [diff] [blame] | 451 | struct exynos_pcie *ep = arg; |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 452 | |
Jaehoon Chung | 4e0a90b38 | 2017-01-16 15:31:34 +0900 | [diff] [blame] | 453 | exynos_pcie_clear_irq_pulse(ep); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 454 | return IRQ_HANDLED; |
| 455 | } |
| 456 | |
Jingoo Han | f342d94 | 2013-09-06 15:54:59 +0900 | [diff] [blame] | 457 | static irqreturn_t exynos_pcie_msi_irq_handler(int irq, void *arg) |
| 458 | { |
Jaehoon Chung | 4e0a90b38 | 2017-01-16 15:31:34 +0900 | [diff] [blame] | 459 | struct exynos_pcie *ep = arg; |
| 460 | struct pcie_port *pp = &ep->pp; |
Jingoo Han | f342d94 | 2013-09-06 15:54:59 +0900 | [diff] [blame] | 461 | |
Lucas Stach | 7f4f16e | 2014-03-28 17:52:58 +0100 | [diff] [blame] | 462 | return dw_handle_msi_irq(pp); |
Jingoo Han | f342d94 | 2013-09-06 15:54:59 +0900 | [diff] [blame] | 463 | } |
| 464 | |
Jaehoon Chung | 4e0a90b38 | 2017-01-16 15:31:34 +0900 | [diff] [blame] | 465 | static void exynos_pcie_msi_init(struct exynos_pcie *ep) |
Jingoo Han | f342d94 | 2013-09-06 15:54:59 +0900 | [diff] [blame] | 466 | { |
Jaehoon Chung | 4e0a90b38 | 2017-01-16 15:31:34 +0900 | [diff] [blame] | 467 | struct pcie_port *pp = &ep->pp; |
Jingoo Han | f342d94 | 2013-09-06 15:54:59 +0900 | [diff] [blame] | 468 | u32 val; |
Jingoo Han | f342d94 | 2013-09-06 15:54:59 +0900 | [diff] [blame] | 469 | |
| 470 | dw_pcie_msi_init(pp); |
| 471 | |
| 472 | /* enable MSI interrupt */ |
Niyas Ahmed S T | 3278478 | 2017-02-01 10:13:06 +0530 | [diff] [blame^] | 473 | val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_IRQ_EN_LEVEL); |
Jingoo Han | f342d94 | 2013-09-06 15:54:59 +0900 | [diff] [blame] | 474 | val |= IRQ_MSI_ENABLE; |
Niyas Ahmed S T | 3278478 | 2017-02-01 10:13:06 +0530 | [diff] [blame^] | 475 | exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_IRQ_EN_LEVEL); |
Jingoo Han | f342d94 | 2013-09-06 15:54:59 +0900 | [diff] [blame] | 476 | } |
| 477 | |
Jaehoon Chung | 4e0a90b38 | 2017-01-16 15:31:34 +0900 | [diff] [blame] | 478 | static void exynos_pcie_enable_interrupts(struct exynos_pcie *ep) |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 479 | { |
Jaehoon Chung | 4e0a90b38 | 2017-01-16 15:31:34 +0900 | [diff] [blame] | 480 | exynos_pcie_enable_irq_pulse(ep); |
Jingoo Han | f342d94 | 2013-09-06 15:54:59 +0900 | [diff] [blame] | 481 | |
| 482 | if (IS_ENABLED(CONFIG_PCI_MSI)) |
Jaehoon Chung | 4e0a90b38 | 2017-01-16 15:31:34 +0900 | [diff] [blame] | 483 | exynos_pcie_msi_init(ep); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 484 | } |
| 485 | |
Bjorn Helgaas | 53e5bff1 | 2016-10-10 07:50:07 -0500 | [diff] [blame] | 486 | static u32 exynos_pcie_readl_rc(struct pcie_port *pp, u32 reg) |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 487 | { |
Jaehoon Chung | 4e0a90b38 | 2017-01-16 15:31:34 +0900 | [diff] [blame] | 488 | struct exynos_pcie *ep = to_exynos_pcie(pp); |
Bjorn Helgaas | 446fc23 | 2016-08-17 14:17:58 -0500 | [diff] [blame] | 489 | u32 val; |
| 490 | |
Jaehoon Chung | 4e0a90b38 | 2017-01-16 15:31:34 +0900 | [diff] [blame] | 491 | exynos_pcie_sideband_dbi_r_mode(ep, true); |
Bjorn Helgaas | 7e00dfd | 2016-10-06 13:25:46 -0500 | [diff] [blame] | 492 | val = readl(pp->dbi_base + reg); |
Jaehoon Chung | 4e0a90b38 | 2017-01-16 15:31:34 +0900 | [diff] [blame] | 493 | exynos_pcie_sideband_dbi_r_mode(ep, false); |
Bjorn Helgaas | 446fc23 | 2016-08-17 14:17:58 -0500 | [diff] [blame] | 494 | return val; |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 495 | } |
| 496 | |
Bjorn Helgaas | 53e5bff1 | 2016-10-10 07:50:07 -0500 | [diff] [blame] | 497 | static void exynos_pcie_writel_rc(struct pcie_port *pp, u32 reg, u32 val) |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 498 | { |
Jaehoon Chung | 4e0a90b38 | 2017-01-16 15:31:34 +0900 | [diff] [blame] | 499 | struct exynos_pcie *ep = to_exynos_pcie(pp); |
Bjorn Helgaas | cc08e82 | 2016-10-06 13:33:39 -0500 | [diff] [blame] | 500 | |
Jaehoon Chung | 4e0a90b38 | 2017-01-16 15:31:34 +0900 | [diff] [blame] | 501 | exynos_pcie_sideband_dbi_w_mode(ep, true); |
Bjorn Helgaas | 7e00dfd | 2016-10-06 13:25:46 -0500 | [diff] [blame] | 502 | writel(val, pp->dbi_base + reg); |
Jaehoon Chung | 4e0a90b38 | 2017-01-16 15:31:34 +0900 | [diff] [blame] | 503 | exynos_pcie_sideband_dbi_w_mode(ep, false); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | static int exynos_pcie_rd_own_conf(struct pcie_port *pp, int where, int size, |
| 507 | u32 *val) |
| 508 | { |
Jaehoon Chung | 4e0a90b38 | 2017-01-16 15:31:34 +0900 | [diff] [blame] | 509 | struct exynos_pcie *ep = to_exynos_pcie(pp); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 510 | int ret; |
| 511 | |
Jaehoon Chung | 4e0a90b38 | 2017-01-16 15:31:34 +0900 | [diff] [blame] | 512 | exynos_pcie_sideband_dbi_r_mode(ep, true); |
Gabriele Paoloni | 4c45852 | 2015-10-08 14:27:48 -0500 | [diff] [blame] | 513 | ret = dw_pcie_cfg_read(pp->dbi_base + where, size, val); |
Jaehoon Chung | 4e0a90b38 | 2017-01-16 15:31:34 +0900 | [diff] [blame] | 514 | exynos_pcie_sideband_dbi_r_mode(ep, false); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 515 | return ret; |
| 516 | } |
| 517 | |
| 518 | static int exynos_pcie_wr_own_conf(struct pcie_port *pp, int where, int size, |
| 519 | u32 val) |
| 520 | { |
Jaehoon Chung | 4e0a90b38 | 2017-01-16 15:31:34 +0900 | [diff] [blame] | 521 | struct exynos_pcie *ep = to_exynos_pcie(pp); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 522 | int ret; |
| 523 | |
Jaehoon Chung | 4e0a90b38 | 2017-01-16 15:31:34 +0900 | [diff] [blame] | 524 | exynos_pcie_sideband_dbi_w_mode(ep, true); |
Gabriele Paoloni | 4c45852 | 2015-10-08 14:27:48 -0500 | [diff] [blame] | 525 | ret = dw_pcie_cfg_write(pp->dbi_base + where, size, val); |
Jaehoon Chung | 4e0a90b38 | 2017-01-16 15:31:34 +0900 | [diff] [blame] | 526 | exynos_pcie_sideband_dbi_w_mode(ep, false); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 527 | return ret; |
| 528 | } |
| 529 | |
| 530 | static int exynos_pcie_link_up(struct pcie_port *pp) |
| 531 | { |
Jaehoon Chung | 4e0a90b38 | 2017-01-16 15:31:34 +0900 | [diff] [blame] | 532 | struct exynos_pcie *ep = to_exynos_pcie(pp); |
Bjorn Helgaas | cc08e82 | 2016-10-06 13:33:39 -0500 | [diff] [blame] | 533 | u32 val; |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 534 | |
Niyas Ahmed S T | 3278478 | 2017-02-01 10:13:06 +0530 | [diff] [blame^] | 535 | val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_ELBI_RDLH_LINKUP); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 536 | if (val == PCIE_ELBI_LTSSM_ENABLE) |
| 537 | return 1; |
| 538 | |
| 539 | return 0; |
| 540 | } |
| 541 | |
| 542 | static void exynos_pcie_host_init(struct pcie_port *pp) |
| 543 | { |
Jaehoon Chung | 4e0a90b38 | 2017-01-16 15:31:34 +0900 | [diff] [blame] | 544 | struct exynos_pcie *ep = to_exynos_pcie(pp); |
Bjorn Helgaas | cc08e82 | 2016-10-06 13:33:39 -0500 | [diff] [blame] | 545 | |
Jaehoon Chung | 4e0a90b38 | 2017-01-16 15:31:34 +0900 | [diff] [blame] | 546 | exynos_pcie_establish_link(ep); |
| 547 | exynos_pcie_enable_interrupts(ep); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | static struct pcie_host_ops exynos_pcie_host_ops = { |
| 551 | .readl_rc = exynos_pcie_readl_rc, |
| 552 | .writel_rc = exynos_pcie_writel_rc, |
| 553 | .rd_own_conf = exynos_pcie_rd_own_conf, |
| 554 | .wr_own_conf = exynos_pcie_wr_own_conf, |
| 555 | .link_up = exynos_pcie_link_up, |
| 556 | .host_init = exynos_pcie_host_init, |
| 557 | }; |
| 558 | |
Jaehoon Chung | 4e0a90b38 | 2017-01-16 15:31:34 +0900 | [diff] [blame] | 559 | static int __init exynos_add_pcie_port(struct exynos_pcie *ep, |
Jingoo Han | 70b3e89 | 2014-10-22 13:58:49 +0900 | [diff] [blame] | 560 | struct platform_device *pdev) |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 561 | { |
Jaehoon Chung | 4e0a90b38 | 2017-01-16 15:31:34 +0900 | [diff] [blame] | 562 | struct pcie_port *pp = &ep->pp; |
Bjorn Helgaas | fae68d6 | 2016-10-06 13:33:41 -0500 | [diff] [blame] | 563 | struct device *dev = pp->dev; |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 564 | int ret; |
| 565 | |
| 566 | pp->irq = platform_get_irq(pdev, 1); |
| 567 | if (!pp->irq) { |
Bjorn Helgaas | fae68d6 | 2016-10-06 13:33:41 -0500 | [diff] [blame] | 568 | dev_err(dev, "failed to get irq\n"); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 569 | return -ENODEV; |
| 570 | } |
Bjorn Helgaas | fae68d6 | 2016-10-06 13:33:41 -0500 | [diff] [blame] | 571 | ret = devm_request_irq(dev, pp->irq, exynos_pcie_irq_handler, |
Jaehoon Chung | 4e0a90b38 | 2017-01-16 15:31:34 +0900 | [diff] [blame] | 572 | IRQF_SHARED, "exynos-pcie", ep); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 573 | if (ret) { |
Bjorn Helgaas | fae68d6 | 2016-10-06 13:33:41 -0500 | [diff] [blame] | 574 | dev_err(dev, "failed to request irq\n"); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 575 | return ret; |
| 576 | } |
| 577 | |
Jingoo Han | f342d94 | 2013-09-06 15:54:59 +0900 | [diff] [blame] | 578 | if (IS_ENABLED(CONFIG_PCI_MSI)) { |
| 579 | pp->msi_irq = platform_get_irq(pdev, 0); |
| 580 | if (!pp->msi_irq) { |
Bjorn Helgaas | fae68d6 | 2016-10-06 13:33:41 -0500 | [diff] [blame] | 581 | dev_err(dev, "failed to get msi irq\n"); |
Jingoo Han | f342d94 | 2013-09-06 15:54:59 +0900 | [diff] [blame] | 582 | return -ENODEV; |
| 583 | } |
| 584 | |
Bjorn Helgaas | fae68d6 | 2016-10-06 13:33:41 -0500 | [diff] [blame] | 585 | ret = devm_request_irq(dev, pp->msi_irq, |
Jingoo Han | f342d94 | 2013-09-06 15:54:59 +0900 | [diff] [blame] | 586 | exynos_pcie_msi_irq_handler, |
Grygorii Strashko | 8ff0ef9 | 2015-12-10 21:18:20 +0200 | [diff] [blame] | 587 | IRQF_SHARED | IRQF_NO_THREAD, |
Jaehoon Chung | 4e0a90b38 | 2017-01-16 15:31:34 +0900 | [diff] [blame] | 588 | "exynos-pcie", ep); |
Jingoo Han | f342d94 | 2013-09-06 15:54:59 +0900 | [diff] [blame] | 589 | if (ret) { |
Bjorn Helgaas | fae68d6 | 2016-10-06 13:33:41 -0500 | [diff] [blame] | 590 | dev_err(dev, "failed to request msi irq\n"); |
Jingoo Han | f342d94 | 2013-09-06 15:54:59 +0900 | [diff] [blame] | 591 | return ret; |
| 592 | } |
| 593 | } |
| 594 | |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 595 | pp->root_bus_nr = -1; |
| 596 | pp->ops = &exynos_pcie_host_ops; |
| 597 | |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 598 | ret = dw_pcie_host_init(pp); |
| 599 | if (ret) { |
Bjorn Helgaas | fae68d6 | 2016-10-06 13:33:41 -0500 | [diff] [blame] | 600 | dev_err(dev, "failed to initialize host\n"); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 601 | return ret; |
| 602 | } |
| 603 | |
| 604 | return 0; |
| 605 | } |
| 606 | |
| 607 | static int __init exynos_pcie_probe(struct platform_device *pdev) |
| 608 | { |
Bjorn Helgaas | fae68d6 | 2016-10-06 13:33:41 -0500 | [diff] [blame] | 609 | struct device *dev = &pdev->dev; |
Jaehoon Chung | 4e0a90b38 | 2017-01-16 15:31:34 +0900 | [diff] [blame] | 610 | struct exynos_pcie *ep; |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 611 | struct pcie_port *pp; |
Bjorn Helgaas | fae68d6 | 2016-10-06 13:33:41 -0500 | [diff] [blame] | 612 | struct device_node *np = dev->of_node; |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 613 | int ret; |
| 614 | |
Jaehoon Chung | 4e0a90b38 | 2017-01-16 15:31:34 +0900 | [diff] [blame] | 615 | ep = devm_kzalloc(dev, sizeof(*ep), GFP_KERNEL); |
| 616 | if (!ep) |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 617 | return -ENOMEM; |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 618 | |
Jaehoon Chung | 4e0a90b38 | 2017-01-16 15:31:34 +0900 | [diff] [blame] | 619 | pp = &ep->pp; |
Bjorn Helgaas | fae68d6 | 2016-10-06 13:33:41 -0500 | [diff] [blame] | 620 | pp->dev = dev; |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 621 | |
Niyas Ahmed S T | 3278478 | 2017-02-01 10:13:06 +0530 | [diff] [blame^] | 622 | ep->ops = (const struct exynos_pcie_ops *) |
| 623 | of_device_get_match_data(dev); |
| 624 | |
Jaehoon Chung | 4e0a90b38 | 2017-01-16 15:31:34 +0900 | [diff] [blame] | 625 | ep->reset_gpio = of_get_named_gpio(np, "reset-gpio", 0); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 626 | |
Niyas Ahmed S T | 3278478 | 2017-02-01 10:13:06 +0530 | [diff] [blame^] | 627 | if (ep->ops && ep->ops->get_mem_resources) { |
| 628 | ret = ep->ops->get_mem_resources(pdev, ep); |
| 629 | if (ret) |
| 630 | return ret; |
Wei Yongjun | f8db3c9 | 2013-09-29 10:29:11 +0800 | [diff] [blame] | 631 | } |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 632 | |
Niyas Ahmed S T | 3278478 | 2017-02-01 10:13:06 +0530 | [diff] [blame^] | 633 | if (ep->ops && ep->ops->get_clk_resources) { |
| 634 | ret = ep->ops->get_clk_resources(ep); |
| 635 | if (ret) |
| 636 | return ret; |
| 637 | ret = ep->ops->init_clk_resources(ep); |
| 638 | if (ret) |
| 639 | return ret; |
Wei Yongjun | f8db3c9 | 2013-09-29 10:29:11 +0800 | [diff] [blame] | 640 | } |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 641 | |
Jaehoon Chung | 4e0a90b38 | 2017-01-16 15:31:34 +0900 | [diff] [blame] | 642 | ret = exynos_add_pcie_port(ep, pdev); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 643 | if (ret < 0) |
Niyas Ahmed S T | 3278478 | 2017-02-01 10:13:06 +0530 | [diff] [blame^] | 644 | goto fail_probe; |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 645 | |
Jaehoon Chung | 4e0a90b38 | 2017-01-16 15:31:34 +0900 | [diff] [blame] | 646 | platform_set_drvdata(pdev, ep); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 647 | return 0; |
| 648 | |
Niyas Ahmed S T | 3278478 | 2017-02-01 10:13:06 +0530 | [diff] [blame^] | 649 | fail_probe: |
| 650 | if (ep->ops && ep->ops->deinit_clk_resources) |
| 651 | ep->ops->deinit_clk_resources(ep); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 652 | return ret; |
| 653 | } |
| 654 | |
| 655 | static int __exit exynos_pcie_remove(struct platform_device *pdev) |
| 656 | { |
Jaehoon Chung | 4e0a90b38 | 2017-01-16 15:31:34 +0900 | [diff] [blame] | 657 | struct exynos_pcie *ep = platform_get_drvdata(pdev); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 658 | |
Niyas Ahmed S T | 3278478 | 2017-02-01 10:13:06 +0530 | [diff] [blame^] | 659 | if (ep->ops && ep->ops->deinit_clk_resources) |
| 660 | ep->ops->deinit_clk_resources(ep); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 661 | |
| 662 | return 0; |
| 663 | } |
| 664 | |
| 665 | static const struct of_device_id exynos_pcie_of_match[] = { |
Niyas Ahmed S T | 3278478 | 2017-02-01 10:13:06 +0530 | [diff] [blame^] | 666 | { |
| 667 | .compatible = "samsung,exynos5440-pcie", |
| 668 | .data = &exynos5440_pcie_ops |
| 669 | }, |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 670 | {}, |
| 671 | }; |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 672 | |
| 673 | static struct platform_driver exynos_pcie_driver = { |
| 674 | .remove = __exit_p(exynos_pcie_remove), |
| 675 | .driver = { |
| 676 | .name = "exynos-pcie", |
Sachin Kamat | eb36309 | 2013-10-21 14:36:43 +0530 | [diff] [blame] | 677 | .of_match_table = exynos_pcie_of_match, |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 678 | }, |
| 679 | }; |
| 680 | |
| 681 | /* Exynos PCIe driver does not allow module unload */ |
| 682 | |
Jingoo Han | 70b3e89 | 2014-10-22 13:58:49 +0900 | [diff] [blame] | 683 | static int __init exynos_pcie_init(void) |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 684 | { |
| 685 | return platform_driver_probe(&exynos_pcie_driver, exynos_pcie_probe); |
| 686 | } |
Jingoo Han | 70b3e89 | 2014-10-22 13:58:49 +0900 | [diff] [blame] | 687 | subsys_initcall(exynos_pcie_init); |