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> |
| 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> |
| 26 | |
| 27 | #include "pcie-designware.h" |
| 28 | |
| 29 | #define to_exynos_pcie(x) container_of(x, struct exynos_pcie, pp) |
| 30 | |
| 31 | struct exynos_pcie { |
| 32 | void __iomem *elbi_base; |
| 33 | void __iomem *phy_base; |
| 34 | void __iomem *block_base; |
| 35 | int reset_gpio; |
| 36 | struct clk *clk; |
| 37 | struct clk *bus_clk; |
| 38 | struct pcie_port pp; |
| 39 | }; |
| 40 | |
| 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) |
| 64 | |
| 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 |
| 73 | |
| 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 |
| 93 | |
Seungwon Jeon | 058dd01 | 2013-08-29 21:35:56 +0900 | [diff] [blame^] | 94 | static inline void exynos_elb_writel(struct exynos_pcie *pcie, u32 val, u32 reg) |
| 95 | { |
| 96 | writel(val, pcie->elbi_base + reg); |
| 97 | } |
| 98 | |
| 99 | static inline u32 exynos_elb_readl(struct exynos_pcie *pcie, u32 reg) |
| 100 | { |
| 101 | return readl(pcie->elbi_base + reg); |
| 102 | } |
| 103 | |
| 104 | static inline void exynos_phy_writel(struct exynos_pcie *pcie, u32 val, u32 reg) |
| 105 | { |
| 106 | writel(val, pcie->phy_base + reg); |
| 107 | } |
| 108 | |
| 109 | static inline u32 exynos_phy_readl(struct exynos_pcie *pcie, u32 reg) |
| 110 | { |
| 111 | return readl(pcie->phy_base + reg); |
| 112 | } |
| 113 | |
| 114 | static inline void exynos_blk_writel(struct exynos_pcie *pcie, u32 val, u32 reg) |
| 115 | { |
| 116 | writel(val, pcie->block_base + reg); |
| 117 | } |
| 118 | |
| 119 | static inline u32 exynos_blk_readl(struct exynos_pcie *pcie, u32 reg) |
| 120 | { |
| 121 | return readl(pcie->block_base + reg); |
| 122 | } |
| 123 | |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 124 | static void exynos_pcie_sideband_dbi_w_mode(struct pcie_port *pp, bool on) |
| 125 | { |
| 126 | u32 val; |
| 127 | struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp); |
| 128 | |
| 129 | if (on) { |
Seungwon Jeon | 058dd01 | 2013-08-29 21:35:56 +0900 | [diff] [blame^] | 130 | val = exynos_elb_readl(exynos_pcie, PCIE_ELBI_SLV_AWMISC); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 131 | val |= PCIE_ELBI_SLV_DBI_ENABLE; |
Seungwon Jeon | 058dd01 | 2013-08-29 21:35:56 +0900 | [diff] [blame^] | 132 | exynos_elb_writel(exynos_pcie, val, PCIE_ELBI_SLV_AWMISC); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 133 | } else { |
Seungwon Jeon | 058dd01 | 2013-08-29 21:35:56 +0900 | [diff] [blame^] | 134 | val = exynos_elb_readl(exynos_pcie, PCIE_ELBI_SLV_AWMISC); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 135 | val &= ~PCIE_ELBI_SLV_DBI_ENABLE; |
Seungwon Jeon | 058dd01 | 2013-08-29 21:35:56 +0900 | [diff] [blame^] | 136 | exynos_elb_writel(exynos_pcie, val, PCIE_ELBI_SLV_AWMISC); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
| 140 | static void exynos_pcie_sideband_dbi_r_mode(struct pcie_port *pp, bool on) |
| 141 | { |
| 142 | u32 val; |
| 143 | struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp); |
| 144 | |
| 145 | if (on) { |
Seungwon Jeon | 058dd01 | 2013-08-29 21:35:56 +0900 | [diff] [blame^] | 146 | val = exynos_elb_readl(exynos_pcie, PCIE_ELBI_SLV_ARMISC); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 147 | val |= PCIE_ELBI_SLV_DBI_ENABLE; |
Seungwon Jeon | 058dd01 | 2013-08-29 21:35:56 +0900 | [diff] [blame^] | 148 | exynos_elb_writel(exynos_pcie, val, PCIE_ELBI_SLV_ARMISC); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 149 | } else { |
Seungwon Jeon | 058dd01 | 2013-08-29 21:35:56 +0900 | [diff] [blame^] | 150 | val = exynos_elb_readl(exynos_pcie, PCIE_ELBI_SLV_ARMISC); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 151 | val &= ~PCIE_ELBI_SLV_DBI_ENABLE; |
Seungwon Jeon | 058dd01 | 2013-08-29 21:35:56 +0900 | [diff] [blame^] | 152 | exynos_elb_writel(exynos_pcie, val, PCIE_ELBI_SLV_ARMISC); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
| 156 | static void exynos_pcie_assert_core_reset(struct pcie_port *pp) |
| 157 | { |
| 158 | u32 val; |
| 159 | struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 160 | |
Seungwon Jeon | 058dd01 | 2013-08-29 21:35:56 +0900 | [diff] [blame^] | 161 | val = exynos_elb_readl(exynos_pcie, PCIE_CORE_RESET); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 162 | val &= ~PCIE_CORE_RESET_ENABLE; |
Seungwon Jeon | 058dd01 | 2013-08-29 21:35:56 +0900 | [diff] [blame^] | 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); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | static void exynos_pcie_deassert_core_reset(struct pcie_port *pp) |
| 170 | { |
| 171 | u32 val; |
| 172 | struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 173 | |
Seungwon Jeon | 058dd01 | 2013-08-29 21:35:56 +0900 | [diff] [blame^] | 174 | val = exynos_elb_readl(exynos_pcie, PCIE_CORE_RESET); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 175 | val |= PCIE_CORE_RESET_ENABLE; |
Seungwon Jeon | 058dd01 | 2013-08-29 21:35:56 +0900 | [diff] [blame^] | 176 | |
| 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); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | static void exynos_pcie_assert_phy_reset(struct pcie_port *pp) |
| 186 | { |
| 187 | struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 188 | |
Seungwon Jeon | 058dd01 | 2013-08-29 21:35:56 +0900 | [diff] [blame^] | 189 | exynos_blk_writel(exynos_pcie, 0, PCIE_PHY_MAC_RESET); |
| 190 | exynos_blk_writel(exynos_pcie, 1, PCIE_PHY_GLOBAL_RESET); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | static void exynos_pcie_deassert_phy_reset(struct pcie_port *pp) |
| 194 | { |
| 195 | struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 196 | |
Seungwon Jeon | 058dd01 | 2013-08-29 21:35:56 +0900 | [diff] [blame^] | 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); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | static void exynos_pcie_init_phy(struct pcie_port *pp) |
| 206 | { |
| 207 | struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 208 | |
| 209 | /* DCC feedback control off */ |
Seungwon Jeon | 058dd01 | 2013-08-29 21:35:56 +0900 | [diff] [blame^] | 210 | exynos_phy_writel(exynos_pcie, 0x29, PCIE_PHY_DCC_FEEDBACK); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 211 | |
| 212 | /* set TX/RX impedance */ |
Seungwon Jeon | 058dd01 | 2013-08-29 21:35:56 +0900 | [diff] [blame^] | 213 | exynos_phy_writel(exynos_pcie, 0xd5, PCIE_PHY_IMPEDANCE); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 214 | |
| 215 | /* set 50Mhz PHY clock */ |
Seungwon Jeon | 058dd01 | 2013-08-29 21:35:56 +0900 | [diff] [blame^] | 216 | exynos_phy_writel(exynos_pcie, 0x14, PCIE_PHY_PLL_DIV_0); |
| 217 | exynos_phy_writel(exynos_pcie, 0x12, PCIE_PHY_PLL_DIV_1); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 218 | |
| 219 | /* set TX Differential output for lane 0 */ |
Seungwon Jeon | 058dd01 | 2013-08-29 21:35:56 +0900 | [diff] [blame^] | 220 | exynos_phy_writel(exynos_pcie, 0x7f, PCIE_PHY_TRSV0_DRV_LVL); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 221 | |
| 222 | /* set TX Pre-emphasis Level Control for lane 0 to minimum */ |
Seungwon Jeon | 058dd01 | 2013-08-29 21:35:56 +0900 | [diff] [blame^] | 223 | exynos_phy_writel(exynos_pcie, 0x0, PCIE_PHY_TRSV0_EMP_LVL); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 224 | |
| 225 | /* set RX clock and data recovery bandwidth */ |
Seungwon Jeon | 058dd01 | 2013-08-29 21:35:56 +0900 | [diff] [blame^] | 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); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 231 | |
| 232 | /* change TX Pre-emphasis Level Control for lanes */ |
Seungwon Jeon | 058dd01 | 2013-08-29 21:35:56 +0900 | [diff] [blame^] | 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); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 237 | |
| 238 | /* set LVCC */ |
Seungwon Jeon | 058dd01 | 2013-08-29 21:35:56 +0900 | [diff] [blame^] | 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); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | static void exynos_pcie_assert_reset(struct pcie_port *pp) |
| 246 | { |
| 247 | struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp); |
| 248 | |
| 249 | if (exynos_pcie->reset_gpio >= 0) |
| 250 | devm_gpio_request_one(pp->dev, exynos_pcie->reset_gpio, |
| 251 | GPIOF_OUT_INIT_HIGH, "RESET"); |
| 252 | return; |
| 253 | } |
| 254 | |
| 255 | static int exynos_pcie_establish_link(struct pcie_port *pp) |
| 256 | { |
| 257 | u32 val; |
| 258 | int count = 0; |
| 259 | struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 260 | |
| 261 | if (dw_pcie_link_up(pp)) { |
| 262 | dev_err(pp->dev, "Link already up\n"); |
| 263 | return 0; |
| 264 | } |
| 265 | |
| 266 | /* assert reset signals */ |
| 267 | exynos_pcie_assert_core_reset(pp); |
| 268 | exynos_pcie_assert_phy_reset(pp); |
| 269 | |
| 270 | /* de-assert phy reset */ |
| 271 | exynos_pcie_deassert_phy_reset(pp); |
| 272 | |
| 273 | /* initialize phy */ |
| 274 | exynos_pcie_init_phy(pp); |
| 275 | |
| 276 | /* pulse for common reset */ |
Seungwon Jeon | 058dd01 | 2013-08-29 21:35:56 +0900 | [diff] [blame^] | 277 | exynos_blk_writel(exynos_pcie, 1, PCIE_PHY_COMMON_RESET); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 278 | udelay(500); |
Seungwon Jeon | 058dd01 | 2013-08-29 21:35:56 +0900 | [diff] [blame^] | 279 | exynos_blk_writel(exynos_pcie, 0, PCIE_PHY_COMMON_RESET); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 280 | |
| 281 | /* de-assert core reset */ |
| 282 | exynos_pcie_deassert_core_reset(pp); |
| 283 | |
| 284 | /* setup root complex */ |
| 285 | dw_pcie_setup_rc(pp); |
| 286 | |
| 287 | /* assert reset signal */ |
| 288 | exynos_pcie_assert_reset(pp); |
| 289 | |
| 290 | /* assert LTSSM enable */ |
Seungwon Jeon | 058dd01 | 2013-08-29 21:35:56 +0900 | [diff] [blame^] | 291 | exynos_elb_writel(exynos_pcie, PCIE_ELBI_LTSSM_ENABLE, |
| 292 | PCIE_APP_LTSSM_ENABLE); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 293 | |
| 294 | /* check if the link is up or not */ |
| 295 | while (!dw_pcie_link_up(pp)) { |
| 296 | mdelay(100); |
| 297 | count++; |
| 298 | if (count == 10) { |
Seungwon Jeon | 058dd01 | 2013-08-29 21:35:56 +0900 | [diff] [blame^] | 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); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 303 | dev_info(pp->dev, "PLL Locked: 0x%x\n", val); |
| 304 | } |
| 305 | dev_err(pp->dev, "PCIe Link Fail\n"); |
| 306 | return -EINVAL; |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | dev_info(pp->dev, "Link up\n"); |
| 311 | |
| 312 | return 0; |
| 313 | } |
| 314 | |
| 315 | static void exynos_pcie_clear_irq_pulse(struct pcie_port *pp) |
| 316 | { |
| 317 | u32 val; |
| 318 | struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 319 | |
Seungwon Jeon | 058dd01 | 2013-08-29 21:35:56 +0900 | [diff] [blame^] | 320 | val = exynos_elb_readl(exynos_pcie, PCIE_IRQ_PULSE); |
| 321 | exynos_elb_writel(exynos_pcie, val, PCIE_IRQ_PULSE); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 322 | return; |
| 323 | } |
| 324 | |
| 325 | static void exynos_pcie_enable_irq_pulse(struct pcie_port *pp) |
| 326 | { |
| 327 | u32 val; |
| 328 | struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 329 | |
| 330 | /* enable INTX interrupt */ |
| 331 | val = IRQ_INTA_ASSERT | IRQ_INTB_ASSERT | |
| 332 | IRQ_INTC_ASSERT | IRQ_INTD_ASSERT, |
Seungwon Jeon | 058dd01 | 2013-08-29 21:35:56 +0900 | [diff] [blame^] | 333 | exynos_elb_writel(exynos_pcie, val, PCIE_IRQ_EN_PULSE); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 334 | return; |
| 335 | } |
| 336 | |
| 337 | static irqreturn_t exynos_pcie_irq_handler(int irq, void *arg) |
| 338 | { |
| 339 | struct pcie_port *pp = arg; |
| 340 | |
| 341 | exynos_pcie_clear_irq_pulse(pp); |
| 342 | return IRQ_HANDLED; |
| 343 | } |
| 344 | |
| 345 | static void exynos_pcie_enable_interrupts(struct pcie_port *pp) |
| 346 | { |
| 347 | exynos_pcie_enable_irq_pulse(pp); |
| 348 | return; |
| 349 | } |
| 350 | |
| 351 | static inline void exynos_pcie_readl_rc(struct pcie_port *pp, |
| 352 | void __iomem *dbi_base, u32 *val) |
| 353 | { |
| 354 | exynos_pcie_sideband_dbi_r_mode(pp, true); |
| 355 | *val = readl(dbi_base); |
| 356 | exynos_pcie_sideband_dbi_r_mode(pp, false); |
| 357 | return; |
| 358 | } |
| 359 | |
| 360 | static inline void exynos_pcie_writel_rc(struct pcie_port *pp, |
| 361 | u32 val, void __iomem *dbi_base) |
| 362 | { |
| 363 | exynos_pcie_sideband_dbi_w_mode(pp, true); |
| 364 | writel(val, dbi_base); |
| 365 | exynos_pcie_sideband_dbi_w_mode(pp, false); |
| 366 | return; |
| 367 | } |
| 368 | |
| 369 | static int exynos_pcie_rd_own_conf(struct pcie_port *pp, int where, int size, |
| 370 | u32 *val) |
| 371 | { |
| 372 | int ret; |
| 373 | |
| 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); |
| 377 | return ret; |
| 378 | } |
| 379 | |
| 380 | static int exynos_pcie_wr_own_conf(struct pcie_port *pp, int where, int size, |
| 381 | u32 val) |
| 382 | { |
| 383 | int ret; |
| 384 | |
| 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); |
| 388 | return ret; |
| 389 | } |
| 390 | |
| 391 | static int exynos_pcie_link_up(struct pcie_port *pp) |
| 392 | { |
| 393 | struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp); |
Seungwon Jeon | 058dd01 | 2013-08-29 21:35:56 +0900 | [diff] [blame^] | 394 | u32 val = exynos_elb_readl(exynos_pcie, PCIE_ELBI_RDLH_LINKUP); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 395 | |
| 396 | if (val == PCIE_ELBI_LTSSM_ENABLE) |
| 397 | return 1; |
| 398 | |
| 399 | return 0; |
| 400 | } |
| 401 | |
| 402 | static void exynos_pcie_host_init(struct pcie_port *pp) |
| 403 | { |
| 404 | exynos_pcie_establish_link(pp); |
| 405 | exynos_pcie_enable_interrupts(pp); |
| 406 | } |
| 407 | |
| 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, |
| 415 | }; |
| 416 | |
| 417 | static int add_pcie_port(struct pcie_port *pp, struct platform_device *pdev) |
| 418 | { |
| 419 | int ret; |
| 420 | |
| 421 | pp->irq = platform_get_irq(pdev, 1); |
| 422 | if (!pp->irq) { |
| 423 | dev_err(&pdev->dev, "failed to get irq\n"); |
| 424 | return -ENODEV; |
| 425 | } |
| 426 | ret = devm_request_irq(&pdev->dev, pp->irq, exynos_pcie_irq_handler, |
| 427 | IRQF_SHARED, "exynos-pcie", pp); |
| 428 | if (ret) { |
| 429 | dev_err(&pdev->dev, "failed to request irq\n"); |
| 430 | return ret; |
| 431 | } |
| 432 | |
| 433 | pp->root_bus_nr = -1; |
| 434 | pp->ops = &exynos_pcie_host_ops; |
| 435 | |
| 436 | spin_lock_init(&pp->conf_lock); |
| 437 | ret = dw_pcie_host_init(pp); |
| 438 | if (ret) { |
| 439 | dev_err(&pdev->dev, "failed to initialize host\n"); |
| 440 | return ret; |
| 441 | } |
| 442 | |
| 443 | return 0; |
| 444 | } |
| 445 | |
| 446 | static int __init exynos_pcie_probe(struct platform_device *pdev) |
| 447 | { |
| 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; |
| 454 | int ret; |
| 455 | |
| 456 | exynos_pcie = devm_kzalloc(&pdev->dev, sizeof(*exynos_pcie), |
| 457 | GFP_KERNEL); |
| 458 | if (!exynos_pcie) { |
| 459 | dev_err(&pdev->dev, "no memory for exynos pcie\n"); |
| 460 | return -ENOMEM; |
| 461 | } |
| 462 | |
| 463 | pp = &exynos_pcie->pp; |
| 464 | |
| 465 | pp->dev = &pdev->dev; |
| 466 | |
| 467 | exynos_pcie->reset_gpio = of_get_named_gpio(np, "reset-gpio", 0); |
| 468 | |
| 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); |
| 473 | } |
| 474 | ret = clk_prepare_enable(exynos_pcie->clk); |
| 475 | if (ret) |
| 476 | return ret; |
| 477 | |
| 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); |
| 482 | goto fail_clk; |
| 483 | } |
| 484 | ret = clk_prepare_enable(exynos_pcie->bus_clk); |
| 485 | if (ret) |
| 486 | goto fail_clk; |
| 487 | |
| 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); |
| 492 | |
| 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); |
| 497 | |
| 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); |
| 502 | |
| 503 | ret = add_pcie_port(pp, pdev); |
| 504 | if (ret < 0) |
| 505 | goto fail_bus_clk; |
| 506 | |
| 507 | platform_set_drvdata(pdev, exynos_pcie); |
| 508 | return 0; |
| 509 | |
| 510 | fail_bus_clk: |
| 511 | clk_disable_unprepare(exynos_pcie->bus_clk); |
| 512 | fail_clk: |
| 513 | clk_disable_unprepare(exynos_pcie->clk); |
| 514 | return ret; |
| 515 | } |
| 516 | |
| 517 | static int __exit exynos_pcie_remove(struct platform_device *pdev) |
| 518 | { |
| 519 | struct exynos_pcie *exynos_pcie = platform_get_drvdata(pdev); |
| 520 | |
| 521 | clk_disable_unprepare(exynos_pcie->bus_clk); |
| 522 | clk_disable_unprepare(exynos_pcie->clk); |
| 523 | |
| 524 | return 0; |
| 525 | } |
| 526 | |
| 527 | static const struct of_device_id exynos_pcie_of_match[] = { |
| 528 | { .compatible = "samsung,exynos5440-pcie", }, |
| 529 | {}, |
| 530 | }; |
| 531 | MODULE_DEVICE_TABLE(of, exynos_pcie_of_match); |
| 532 | |
| 533 | static struct platform_driver exynos_pcie_driver = { |
| 534 | .remove = __exit_p(exynos_pcie_remove), |
| 535 | .driver = { |
| 536 | .name = "exynos-pcie", |
| 537 | .owner = THIS_MODULE, |
| 538 | .of_match_table = of_match_ptr(exynos_pcie_of_match), |
| 539 | }, |
| 540 | }; |
| 541 | |
| 542 | /* Exynos PCIe driver does not allow module unload */ |
| 543 | |
| 544 | static int __init pcie_init(void) |
| 545 | { |
| 546 | return platform_driver_probe(&exynos_pcie_driver, exynos_pcie_probe); |
| 547 | } |
| 548 | subsys_initcall(pcie_init); |
| 549 | |
| 550 | MODULE_AUTHOR("Jingoo Han <jg1.han@samsung.com>"); |
| 551 | MODULE_DESCRIPTION("Samsung PCIe host controller driver"); |
| 552 | MODULE_LICENSE("GPL v2"); |