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