Bjorn Helgaas | 8cfab3c | 2018-01-26 12:50:27 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Jianguo Sun | bbd11bd | 2017-10-23 19:17:50 +0800 | [diff] [blame] | 2 | /* |
| 3 | * PCIe host controller driver for HiSilicon STB SoCs |
| 4 | * |
| 5 | * Copyright (C) 2016-2017 HiSilicon Co., Ltd. http://www.hisilicon.com |
| 6 | * |
| 7 | * Authors: Ruqiang Ju <juruqiang@hisilicon.com> |
| 8 | * Jianguo Sun <sunjianguo1@huawei.com> |
Jianguo Sun | bbd11bd | 2017-10-23 19:17:50 +0800 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <linux/clk.h> |
| 12 | #include <linux/delay.h> |
| 13 | #include <linux/interrupt.h> |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/of.h> |
| 17 | #include <linux/of_gpio.h> |
| 18 | #include <linux/pci.h> |
| 19 | #include <linux/phy/phy.h> |
| 20 | #include <linux/platform_device.h> |
| 21 | #include <linux/resource.h> |
| 22 | #include <linux/reset.h> |
| 23 | |
| 24 | #include "pcie-designware.h" |
| 25 | |
| 26 | #define to_histb_pcie(x) dev_get_drvdata((x)->dev) |
| 27 | |
| 28 | #define PCIE_SYS_CTRL0 0x0000 |
| 29 | #define PCIE_SYS_CTRL1 0x0004 |
| 30 | #define PCIE_SYS_CTRL7 0x001C |
| 31 | #define PCIE_SYS_CTRL13 0x0034 |
| 32 | #define PCIE_SYS_CTRL15 0x003C |
| 33 | #define PCIE_SYS_CTRL16 0x0040 |
| 34 | #define PCIE_SYS_CTRL17 0x0044 |
| 35 | |
| 36 | #define PCIE_SYS_STAT0 0x0100 |
| 37 | #define PCIE_SYS_STAT4 0x0110 |
| 38 | |
| 39 | #define PCIE_RDLH_LINK_UP BIT(5) |
| 40 | #define PCIE_XMLH_LINK_UP BIT(15) |
| 41 | #define PCIE_ELBI_SLV_DBI_ENABLE BIT(21) |
| 42 | #define PCIE_APP_LTSSM_ENABLE BIT(11) |
| 43 | |
| 44 | #define PCIE_DEVICE_TYPE_MASK GENMASK(31, 28) |
| 45 | #define PCIE_WM_EP 0 |
| 46 | #define PCIE_WM_LEGACY BIT(1) |
| 47 | #define PCIE_WM_RC BIT(30) |
| 48 | |
| 49 | #define PCIE_LTSSM_STATE_MASK GENMASK(5, 0) |
| 50 | #define PCIE_LTSSM_STATE_ACTIVE 0x11 |
| 51 | |
| 52 | struct histb_pcie { |
| 53 | struct dw_pcie *pci; |
| 54 | struct clk *aux_clk; |
| 55 | struct clk *pipe_clk; |
| 56 | struct clk *sys_clk; |
| 57 | struct clk *bus_clk; |
| 58 | struct phy *phy; |
| 59 | struct reset_control *soft_reset; |
| 60 | struct reset_control *sys_reset; |
| 61 | struct reset_control *bus_reset; |
| 62 | void __iomem *ctrl; |
| 63 | int reset_gpio; |
| 64 | }; |
| 65 | |
| 66 | static u32 histb_pcie_readl(struct histb_pcie *histb_pcie, u32 reg) |
| 67 | { |
| 68 | return readl(histb_pcie->ctrl + reg); |
| 69 | } |
| 70 | |
| 71 | static void histb_pcie_writel(struct histb_pcie *histb_pcie, u32 reg, u32 val) |
| 72 | { |
| 73 | writel(val, histb_pcie->ctrl + reg); |
| 74 | } |
| 75 | |
| 76 | static void histb_pcie_dbi_w_mode(struct pcie_port *pp, bool enable) |
| 77 | { |
| 78 | struct dw_pcie *pci = to_dw_pcie_from_pp(pp); |
| 79 | struct histb_pcie *hipcie = to_histb_pcie(pci); |
| 80 | u32 val; |
| 81 | |
| 82 | val = histb_pcie_readl(hipcie, PCIE_SYS_CTRL0); |
| 83 | if (enable) |
| 84 | val |= PCIE_ELBI_SLV_DBI_ENABLE; |
| 85 | else |
| 86 | val &= ~PCIE_ELBI_SLV_DBI_ENABLE; |
| 87 | histb_pcie_writel(hipcie, PCIE_SYS_CTRL0, val); |
| 88 | } |
| 89 | |
| 90 | static void histb_pcie_dbi_r_mode(struct pcie_port *pp, bool enable) |
| 91 | { |
| 92 | struct dw_pcie *pci = to_dw_pcie_from_pp(pp); |
| 93 | struct histb_pcie *hipcie = to_histb_pcie(pci); |
| 94 | u32 val; |
| 95 | |
| 96 | val = histb_pcie_readl(hipcie, PCIE_SYS_CTRL1); |
| 97 | if (enable) |
| 98 | val |= PCIE_ELBI_SLV_DBI_ENABLE; |
| 99 | else |
| 100 | val &= ~PCIE_ELBI_SLV_DBI_ENABLE; |
| 101 | histb_pcie_writel(hipcie, PCIE_SYS_CTRL1, val); |
| 102 | } |
| 103 | |
| 104 | static u32 histb_pcie_read_dbi(struct dw_pcie *pci, void __iomem *base, |
| 105 | u32 reg, size_t size) |
| 106 | { |
| 107 | u32 val; |
| 108 | |
| 109 | histb_pcie_dbi_r_mode(&pci->pp, true); |
| 110 | dw_pcie_read(base + reg, size, &val); |
| 111 | histb_pcie_dbi_r_mode(&pci->pp, false); |
| 112 | |
| 113 | return val; |
| 114 | } |
| 115 | |
| 116 | static void histb_pcie_write_dbi(struct dw_pcie *pci, void __iomem *base, |
| 117 | u32 reg, size_t size, u32 val) |
| 118 | { |
| 119 | histb_pcie_dbi_w_mode(&pci->pp, true); |
| 120 | dw_pcie_write(base + reg, size, val); |
| 121 | histb_pcie_dbi_w_mode(&pci->pp, false); |
| 122 | } |
| 123 | |
| 124 | static int histb_pcie_rd_own_conf(struct pcie_port *pp, int where, |
| 125 | int size, u32 *val) |
| 126 | { |
| 127 | struct dw_pcie *pci = to_dw_pcie_from_pp(pp); |
| 128 | int ret; |
| 129 | |
| 130 | histb_pcie_dbi_r_mode(pp, true); |
| 131 | ret = dw_pcie_read(pci->dbi_base + where, size, val); |
| 132 | histb_pcie_dbi_r_mode(pp, false); |
| 133 | |
| 134 | return ret; |
| 135 | } |
| 136 | |
| 137 | static int histb_pcie_wr_own_conf(struct pcie_port *pp, int where, |
| 138 | int size, u32 val) |
| 139 | { |
| 140 | struct dw_pcie *pci = to_dw_pcie_from_pp(pp); |
| 141 | int ret; |
| 142 | |
| 143 | histb_pcie_dbi_w_mode(pp, true); |
| 144 | ret = dw_pcie_write(pci->dbi_base + where, size, val); |
| 145 | histb_pcie_dbi_w_mode(pp, false); |
| 146 | |
| 147 | return ret; |
| 148 | } |
| 149 | |
| 150 | static int histb_pcie_link_up(struct dw_pcie *pci) |
| 151 | { |
| 152 | struct histb_pcie *hipcie = to_histb_pcie(pci); |
| 153 | u32 regval; |
| 154 | u32 status; |
| 155 | |
| 156 | regval = histb_pcie_readl(hipcie, PCIE_SYS_STAT0); |
| 157 | status = histb_pcie_readl(hipcie, PCIE_SYS_STAT4); |
| 158 | status &= PCIE_LTSSM_STATE_MASK; |
| 159 | if ((regval & PCIE_XMLH_LINK_UP) && (regval & PCIE_RDLH_LINK_UP) && |
| 160 | (status == PCIE_LTSSM_STATE_ACTIVE)) |
| 161 | return 1; |
| 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | static int histb_pcie_establish_link(struct pcie_port *pp) |
| 167 | { |
| 168 | struct dw_pcie *pci = to_dw_pcie_from_pp(pp); |
| 169 | struct histb_pcie *hipcie = to_histb_pcie(pci); |
| 170 | u32 regval; |
| 171 | |
| 172 | if (dw_pcie_link_up(pci)) { |
| 173 | dev_info(pci->dev, "Link already up\n"); |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | /* PCIe RC work mode */ |
| 178 | regval = histb_pcie_readl(hipcie, PCIE_SYS_CTRL0); |
| 179 | regval &= ~PCIE_DEVICE_TYPE_MASK; |
| 180 | regval |= PCIE_WM_RC; |
| 181 | histb_pcie_writel(hipcie, PCIE_SYS_CTRL0, regval); |
| 182 | |
| 183 | /* setup root complex */ |
| 184 | dw_pcie_setup_rc(pp); |
| 185 | |
| 186 | /* assert LTSSM enable */ |
| 187 | regval = histb_pcie_readl(hipcie, PCIE_SYS_CTRL7); |
| 188 | regval |= PCIE_APP_LTSSM_ENABLE; |
| 189 | histb_pcie_writel(hipcie, PCIE_SYS_CTRL7, regval); |
| 190 | |
| 191 | return dw_pcie_wait_for_link(pci); |
| 192 | } |
| 193 | |
| 194 | static int histb_pcie_host_init(struct pcie_port *pp) |
| 195 | { |
| 196 | histb_pcie_establish_link(pp); |
| 197 | |
| 198 | if (IS_ENABLED(CONFIG_PCI_MSI)) |
| 199 | dw_pcie_msi_init(pp); |
| 200 | |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | static struct dw_pcie_host_ops histb_pcie_host_ops = { |
| 205 | .rd_own_conf = histb_pcie_rd_own_conf, |
| 206 | .wr_own_conf = histb_pcie_wr_own_conf, |
| 207 | .host_init = histb_pcie_host_init, |
| 208 | }; |
| 209 | |
| 210 | static irqreturn_t histb_pcie_msi_irq_handler(int irq, void *arg) |
| 211 | { |
| 212 | struct pcie_port *pp = arg; |
| 213 | |
| 214 | return dw_handle_msi_irq(pp); |
| 215 | } |
| 216 | |
| 217 | static void histb_pcie_host_disable(struct histb_pcie *hipcie) |
| 218 | { |
| 219 | reset_control_assert(hipcie->soft_reset); |
| 220 | reset_control_assert(hipcie->sys_reset); |
| 221 | reset_control_assert(hipcie->bus_reset); |
| 222 | |
| 223 | clk_disable_unprepare(hipcie->aux_clk); |
| 224 | clk_disable_unprepare(hipcie->pipe_clk); |
| 225 | clk_disable_unprepare(hipcie->sys_clk); |
| 226 | clk_disable_unprepare(hipcie->bus_clk); |
| 227 | |
| 228 | if (gpio_is_valid(hipcie->reset_gpio)) |
| 229 | gpio_set_value_cansleep(hipcie->reset_gpio, 0); |
| 230 | } |
| 231 | |
| 232 | static int histb_pcie_host_enable(struct pcie_port *pp) |
| 233 | { |
| 234 | struct dw_pcie *pci = to_dw_pcie_from_pp(pp); |
| 235 | struct histb_pcie *hipcie = to_histb_pcie(pci); |
| 236 | struct device *dev = pci->dev; |
| 237 | int ret; |
| 238 | |
| 239 | /* power on PCIe device if have */ |
| 240 | if (gpio_is_valid(hipcie->reset_gpio)) |
| 241 | gpio_set_value_cansleep(hipcie->reset_gpio, 1); |
| 242 | |
| 243 | ret = clk_prepare_enable(hipcie->bus_clk); |
| 244 | if (ret) { |
| 245 | dev_err(dev, "cannot prepare/enable bus clk\n"); |
| 246 | goto err_bus_clk; |
| 247 | } |
| 248 | |
| 249 | ret = clk_prepare_enable(hipcie->sys_clk); |
| 250 | if (ret) { |
| 251 | dev_err(dev, "cannot prepare/enable sys clk\n"); |
| 252 | goto err_sys_clk; |
| 253 | } |
| 254 | |
| 255 | ret = clk_prepare_enable(hipcie->pipe_clk); |
| 256 | if (ret) { |
| 257 | dev_err(dev, "cannot prepare/enable pipe clk\n"); |
| 258 | goto err_pipe_clk; |
| 259 | } |
| 260 | |
| 261 | ret = clk_prepare_enable(hipcie->aux_clk); |
| 262 | if (ret) { |
| 263 | dev_err(dev, "cannot prepare/enable aux clk\n"); |
| 264 | goto err_aux_clk; |
| 265 | } |
| 266 | |
| 267 | reset_control_assert(hipcie->soft_reset); |
| 268 | reset_control_deassert(hipcie->soft_reset); |
| 269 | |
| 270 | reset_control_assert(hipcie->sys_reset); |
| 271 | reset_control_deassert(hipcie->sys_reset); |
| 272 | |
| 273 | reset_control_assert(hipcie->bus_reset); |
| 274 | reset_control_deassert(hipcie->bus_reset); |
| 275 | |
| 276 | return 0; |
| 277 | |
| 278 | err_aux_clk: |
Jianguo Sun | bbd11bd | 2017-10-23 19:17:50 +0800 | [diff] [blame] | 279 | clk_disable_unprepare(hipcie->pipe_clk); |
Shawn Guo | db0c25f | 2018-03-02 09:12:00 +0800 | [diff] [blame^] | 280 | err_pipe_clk: |
Jianguo Sun | bbd11bd | 2017-10-23 19:17:50 +0800 | [diff] [blame] | 281 | clk_disable_unprepare(hipcie->sys_clk); |
Shawn Guo | db0c25f | 2018-03-02 09:12:00 +0800 | [diff] [blame^] | 282 | err_sys_clk: |
Jianguo Sun | bbd11bd | 2017-10-23 19:17:50 +0800 | [diff] [blame] | 283 | clk_disable_unprepare(hipcie->bus_clk); |
Shawn Guo | db0c25f | 2018-03-02 09:12:00 +0800 | [diff] [blame^] | 284 | err_bus_clk: |
Jianguo Sun | bbd11bd | 2017-10-23 19:17:50 +0800 | [diff] [blame] | 285 | |
| 286 | return ret; |
| 287 | } |
| 288 | |
| 289 | static const struct dw_pcie_ops dw_pcie_ops = { |
| 290 | .read_dbi = histb_pcie_read_dbi, |
| 291 | .write_dbi = histb_pcie_write_dbi, |
| 292 | .link_up = histb_pcie_link_up, |
| 293 | }; |
| 294 | |
| 295 | static int histb_pcie_probe(struct platform_device *pdev) |
| 296 | { |
| 297 | struct histb_pcie *hipcie; |
| 298 | struct dw_pcie *pci; |
| 299 | struct pcie_port *pp; |
| 300 | struct resource *res; |
| 301 | struct device_node *np = pdev->dev.of_node; |
| 302 | struct device *dev = &pdev->dev; |
| 303 | enum of_gpio_flags of_flags; |
| 304 | unsigned long flag = GPIOF_DIR_OUT; |
| 305 | int ret; |
| 306 | |
| 307 | hipcie = devm_kzalloc(dev, sizeof(*hipcie), GFP_KERNEL); |
| 308 | if (!hipcie) |
| 309 | return -ENOMEM; |
| 310 | |
| 311 | pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL); |
| 312 | if (!pci) |
| 313 | return -ENOMEM; |
| 314 | |
| 315 | hipcie->pci = pci; |
| 316 | pp = &pci->pp; |
| 317 | pci->dev = dev; |
| 318 | pci->ops = &dw_pcie_ops; |
| 319 | |
| 320 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "control"); |
| 321 | hipcie->ctrl = devm_ioremap_resource(dev, res); |
| 322 | if (IS_ERR(hipcie->ctrl)) { |
| 323 | dev_err(dev, "cannot get control reg base\n"); |
| 324 | return PTR_ERR(hipcie->ctrl); |
| 325 | } |
| 326 | |
| 327 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rc-dbi"); |
| 328 | pci->dbi_base = devm_ioremap_resource(dev, res); |
| 329 | if (IS_ERR(pci->dbi_base)) { |
| 330 | dev_err(dev, "cannot get rc-dbi base\n"); |
| 331 | return PTR_ERR(pci->dbi_base); |
| 332 | } |
| 333 | |
| 334 | hipcie->reset_gpio = of_get_named_gpio_flags(np, |
| 335 | "reset-gpios", 0, &of_flags); |
| 336 | if (of_flags & OF_GPIO_ACTIVE_LOW) |
| 337 | flag |= GPIOF_ACTIVE_LOW; |
| 338 | if (gpio_is_valid(hipcie->reset_gpio)) { |
| 339 | ret = devm_gpio_request_one(dev, hipcie->reset_gpio, |
| 340 | flag, "PCIe device power control"); |
| 341 | if (ret) { |
| 342 | dev_err(dev, "unable to request gpio\n"); |
| 343 | return ret; |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | hipcie->aux_clk = devm_clk_get(dev, "aux"); |
| 348 | if (IS_ERR(hipcie->aux_clk)) { |
| 349 | dev_err(dev, "Failed to get PCIe aux clk\n"); |
| 350 | return PTR_ERR(hipcie->aux_clk); |
| 351 | } |
| 352 | |
| 353 | hipcie->pipe_clk = devm_clk_get(dev, "pipe"); |
| 354 | if (IS_ERR(hipcie->pipe_clk)) { |
| 355 | dev_err(dev, "Failed to get PCIe pipe clk\n"); |
| 356 | return PTR_ERR(hipcie->pipe_clk); |
| 357 | } |
| 358 | |
| 359 | hipcie->sys_clk = devm_clk_get(dev, "sys"); |
| 360 | if (IS_ERR(hipcie->sys_clk)) { |
| 361 | dev_err(dev, "Failed to get PCIEe sys clk\n"); |
| 362 | return PTR_ERR(hipcie->sys_clk); |
| 363 | } |
| 364 | |
| 365 | hipcie->bus_clk = devm_clk_get(dev, "bus"); |
| 366 | if (IS_ERR(hipcie->bus_clk)) { |
| 367 | dev_err(dev, "Failed to get PCIe bus clk\n"); |
| 368 | return PTR_ERR(hipcie->bus_clk); |
| 369 | } |
| 370 | |
| 371 | hipcie->soft_reset = devm_reset_control_get(dev, "soft"); |
| 372 | if (IS_ERR(hipcie->soft_reset)) { |
| 373 | dev_err(dev, "couldn't get soft reset\n"); |
| 374 | return PTR_ERR(hipcie->soft_reset); |
| 375 | } |
| 376 | |
| 377 | hipcie->sys_reset = devm_reset_control_get(dev, "sys"); |
| 378 | if (IS_ERR(hipcie->sys_reset)) { |
| 379 | dev_err(dev, "couldn't get sys reset\n"); |
| 380 | return PTR_ERR(hipcie->sys_reset); |
| 381 | } |
| 382 | |
| 383 | hipcie->bus_reset = devm_reset_control_get(dev, "bus"); |
| 384 | if (IS_ERR(hipcie->bus_reset)) { |
| 385 | dev_err(dev, "couldn't get bus reset\n"); |
| 386 | return PTR_ERR(hipcie->bus_reset); |
| 387 | } |
| 388 | |
| 389 | if (IS_ENABLED(CONFIG_PCI_MSI)) { |
| 390 | pp->msi_irq = platform_get_irq_byname(pdev, "msi"); |
| 391 | if (pp->msi_irq < 0) { |
| 392 | dev_err(dev, "Failed to get MSI IRQ\n"); |
| 393 | return pp->msi_irq; |
| 394 | } |
| 395 | |
| 396 | ret = devm_request_irq(dev, pp->msi_irq, |
| 397 | histb_pcie_msi_irq_handler, |
| 398 | IRQF_SHARED, "histb-pcie-msi", pp); |
| 399 | if (ret) { |
| 400 | dev_err(dev, "cannot request MSI IRQ\n"); |
| 401 | return ret; |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | hipcie->phy = devm_phy_get(dev, "phy"); |
| 406 | if (IS_ERR(hipcie->phy)) { |
| 407 | dev_info(dev, "no pcie-phy found\n"); |
| 408 | hipcie->phy = NULL; |
| 409 | /* fall through here! |
| 410 | * if no pcie-phy found, phy init |
| 411 | * should be done under boot! |
| 412 | */ |
| 413 | } else { |
| 414 | phy_init(hipcie->phy); |
| 415 | } |
| 416 | |
| 417 | pp->root_bus_nr = -1; |
| 418 | pp->ops = &histb_pcie_host_ops; |
| 419 | |
| 420 | platform_set_drvdata(pdev, hipcie); |
| 421 | |
| 422 | ret = histb_pcie_host_enable(pp); |
| 423 | if (ret) { |
| 424 | dev_err(dev, "failed to enable host\n"); |
| 425 | return ret; |
| 426 | } |
| 427 | |
| 428 | ret = dw_pcie_host_init(pp); |
| 429 | if (ret) { |
| 430 | dev_err(dev, "failed to initialize host\n"); |
| 431 | return ret; |
| 432 | } |
| 433 | |
| 434 | return 0; |
| 435 | } |
| 436 | |
| 437 | static int histb_pcie_remove(struct platform_device *pdev) |
| 438 | { |
| 439 | struct histb_pcie *hipcie = platform_get_drvdata(pdev); |
| 440 | |
| 441 | histb_pcie_host_disable(hipcie); |
| 442 | |
| 443 | if (hipcie->phy) |
| 444 | phy_exit(hipcie->phy); |
| 445 | |
| 446 | return 0; |
| 447 | } |
| 448 | |
| 449 | static const struct of_device_id histb_pcie_of_match[] = { |
| 450 | { .compatible = "hisilicon,hi3798cv200-pcie", }, |
| 451 | {}, |
| 452 | }; |
| 453 | MODULE_DEVICE_TABLE(of, histb_pcie_of_match); |
| 454 | |
| 455 | static struct platform_driver histb_pcie_platform_driver = { |
| 456 | .probe = histb_pcie_probe, |
| 457 | .remove = histb_pcie_remove, |
| 458 | .driver = { |
| 459 | .name = "histb-pcie", |
| 460 | .of_match_table = histb_pcie_of_match, |
| 461 | }, |
| 462 | }; |
| 463 | module_platform_driver(histb_pcie_platform_driver); |
| 464 | |
| 465 | MODULE_DESCRIPTION("HiSilicon STB PCIe host controller driver"); |
| 466 | MODULE_LICENSE("GPL v2"); |