| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
/* |
| 3 |
* PCIe host controller driver for Intel Gateway SoCs |
| 4 |
* |
| 5 |
* Copyright (c) 2019 Intel Corporation. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <linux/bitfield.h> |
| 9 |
#include <linux/clk.h> |
| 10 |
#include <linux/gpio/consumer.h> |
| 11 |
#include <linux/iopoll.h> |
| 12 |
#include <linux/mod_devicetable.h> |
| 13 |
#include <linux/pci_regs.h> |
| 14 |
#include <linux/phy/phy.h> |
| 15 |
#include <linux/platform_device.h> |
| 16 |
#include <linux/property.h> |
| 17 |
#include <linux/reset.h> |
| 18 |
|
| 19 |
#include "../../pci.h" |
| 20 |
#include "pcie-designware.h" |
| 21 |
|
| 22 |
#define PORT_AFR_N_FTS_GEN12_DFT (SZ_128 - 1) |
| 23 |
#define PORT_AFR_N_FTS_GEN3 180 |
| 24 |
#define PORT_AFR_N_FTS_GEN4 196 |
| 25 |
|
| 26 |
/* PCIe Application logic Registers */ |
| 27 |
#define PCIE_APP_CCR 0x10 |
| 28 |
#define PCIE_APP_CCR_LTSSM_ENABLE BIT(0) |
| 29 |
|
| 30 |
#define PCIE_APP_MSG_CR 0x30 |
| 31 |
#define PCIE_APP_MSG_XMT_PM_TURNOFF BIT(0) |
| 32 |
|
| 33 |
#define PCIE_APP_PMC 0x44 |
| 34 |
#define PCIE_APP_PMC_IN_L2 BIT(20) |
| 35 |
|
| 36 |
#define PCIE_APP_IRNEN 0xF4 |
| 37 |
#define PCIE_APP_IRNCR 0xF8 |
| 38 |
#define PCIE_APP_IRN_AER_REPORT BIT(0) |
| 39 |
#define PCIE_APP_IRN_PME BIT(2) |
| 40 |
#define PCIE_APP_IRN_RX_VDM_MSG BIT(4) |
| 41 |
#define PCIE_APP_IRN_PM_TO_ACK BIT(9) |
| 42 |
#define PCIE_APP_IRN_LINK_AUTO_BW_STAT BIT(11) |
| 43 |
#define PCIE_APP_IRN_BW_MGT BIT(12) |
| 44 |
#define PCIE_APP_IRN_INTA BIT(13) |
| 45 |
#define PCIE_APP_IRN_INTB BIT(14) |
| 46 |
#define PCIE_APP_IRN_INTC BIT(15) |
| 47 |
#define PCIE_APP_IRN_INTD BIT(16) |
| 48 |
#define PCIE_APP_IRN_MSG_LTR BIT(18) |
| 49 |
#define PCIE_APP_IRN_SYS_ERR_RC BIT(29) |
| 50 |
#define PCIE_APP_INTX_OFST 12 |
| 51 |
|
| 52 |
#define PCIE_APP_IRN_INT \ |
| 53 |
(PCIE_APP_IRN_AER_REPORT | PCIE_APP_IRN_PME | \ |
| 54 |
PCIE_APP_IRN_RX_VDM_MSG | PCIE_APP_IRN_SYS_ERR_RC | \ |
| 55 |
PCIE_APP_IRN_PM_TO_ACK | PCIE_APP_IRN_MSG_LTR | \ |
| 56 |
PCIE_APP_IRN_BW_MGT | PCIE_APP_IRN_LINK_AUTO_BW_STAT | \ |
| 57 |
PCIE_APP_IRN_INTA | PCIE_APP_IRN_INTB | \ |
| 58 |
PCIE_APP_IRN_INTC | PCIE_APP_IRN_INTD) |
| 59 |
|
| 60 |
#define RESET_INTERVAL_MS 100 |
| 61 |
|
| 62 |
struct intel_pcie { |
| 63 |
struct dw_pcie pci; |
| 64 |
void __iomem *app_base; |
| 65 |
struct gpio_desc *reset_gpio; |
| 66 |
u32 rst_intrvl; |
| 67 |
struct clk *core_clk; |
| 68 |
struct reset_control *core_rst; |
| 69 |
struct phy *phy; |
| 70 |
}; |
| 71 |
|
| 72 |
static void pcie_update_bits(void __iomem *base, u32 ofs, u32 mask, u32 val) |
| 73 |
{ |
| 74 |
u32 old; |
| 75 |
|
| 76 |
old = readl(base + ofs); |
| 77 |
val = (old & ~mask) | (val & mask); |
| 78 |
|
| 79 |
if (val != old) |
| 80 |
writel(val, base + ofs); |
| 81 |
} |
| 82 |
|
| 83 |
static inline void pcie_app_wr(struct intel_pcie *pcie, u32 ofs, u32 val) |
| 84 |
{ |
| 85 |
writel(val, pcie->app_base + ofs); |
| 86 |
} |
| 87 |
|
| 88 |
static void pcie_app_wr_mask(struct intel_pcie *pcie, u32 ofs, |
| 89 |
u32 mask, u32 val) |
| 90 |
{ |
| 91 |
pcie_update_bits(pcie->app_base, ofs, mask, val); |
| 92 |
} |
| 93 |
|
| 94 |
static inline u32 pcie_rc_cfg_rd(struct intel_pcie *pcie, u32 ofs) |
| 95 |
{ |
| 96 |
return dw_pcie_readl_dbi(&pcie->pci, ofs); |
| 97 |
} |
| 98 |
|
| 99 |
static inline void pcie_rc_cfg_wr(struct intel_pcie *pcie, u32 ofs, u32 val) |
| 100 |
{ |
| 101 |
dw_pcie_writel_dbi(&pcie->pci, ofs, val); |
| 102 |
} |
| 103 |
|
| 104 |
static void pcie_rc_cfg_wr_mask(struct intel_pcie *pcie, u32 ofs, |
| 105 |
u32 mask, u32 val) |
| 106 |
{ |
| 107 |
pcie_update_bits(pcie->pci.dbi_base, ofs, mask, val); |
| 108 |
} |
| 109 |
|
| 110 |
static void intel_pcie_ltssm_enable(struct intel_pcie *pcie) |
| 111 |
{ |
| 112 |
pcie_app_wr_mask(pcie, PCIE_APP_CCR, PCIE_APP_CCR_LTSSM_ENABLE, |
| 113 |
PCIE_APP_CCR_LTSSM_ENABLE); |
| 114 |
} |
| 115 |
|
| 116 |
static void intel_pcie_ltssm_disable(struct intel_pcie *pcie) |
| 117 |
{ |
| 118 |
pcie_app_wr_mask(pcie, PCIE_APP_CCR, PCIE_APP_CCR_LTSSM_ENABLE, 0); |
| 119 |
} |
| 120 |
|
| 121 |
static void intel_pcie_link_setup(struct intel_pcie *pcie) |
| 122 |
{ |
| 123 |
u32 val; |
| 124 |
u8 offset = dw_pcie_find_capability(&pcie->pci, PCI_CAP_ID_EXP); |
| 125 |
|
| 126 |
val = pcie_rc_cfg_rd(pcie, offset + PCI_EXP_LNKCTL); |
| 127 |
|
| 128 |
val &= ~(PCI_EXP_LNKCTL_LD | PCI_EXP_LNKCTL_ASPMC); |
| 129 |
pcie_rc_cfg_wr(pcie, offset + PCI_EXP_LNKCTL, val); |
| 130 |
} |
| 131 |
|
| 132 |
static void intel_pcie_init_n_fts(struct dw_pcie *pci) |
| 133 |
{ |
| 134 |
switch (pci->max_link_speed) { |
| 135 |
case 3: |
| 136 |
pci->n_fts[1] = PORT_AFR_N_FTS_GEN3; |
| 137 |
break; |
| 138 |
case 4: |
| 139 |
pci->n_fts[1] = PORT_AFR_N_FTS_GEN4; |
| 140 |
break; |
| 141 |
default: |
| 142 |
pci->n_fts[1] = PORT_AFR_N_FTS_GEN12_DFT; |
| 143 |
break; |
| 144 |
} |
| 145 |
pci->n_fts[0] = PORT_AFR_N_FTS_GEN12_DFT; |
| 146 |
} |
| 147 |
|
| 148 |
static int intel_pcie_ep_rst_init(struct intel_pcie *pcie) |
| 149 |
{ |
| 150 |
struct device *dev = pcie->pci.dev; |
| 151 |
int ret; |
| 152 |
|
| 153 |
pcie->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW); |
| 154 |
if (IS_ERR(pcie->reset_gpio)) { |
| 155 |
ret = PTR_ERR(pcie->reset_gpio); |
| 156 |
if (ret != -EPROBE_DEFER) |
| 157 |
dev_err(dev, "Failed to request PCIe GPIO: %d\n", ret); |
| 158 |
return ret; |
| 159 |
} |
| 160 |
|
| 161 |
/* Make initial reset last for 100us */ |
| 162 |
usleep_range(100, 200); |
| 163 |
|
| 164 |
return 0; |
| 165 |
} |
| 166 |
|
| 167 |
static void intel_pcie_core_rst_assert(struct intel_pcie *pcie) |
| 168 |
{ |
| 169 |
reset_control_assert(pcie->core_rst); |
| 170 |
} |
| 171 |
|
| 172 |
static void intel_pcie_core_rst_deassert(struct intel_pcie *pcie) |
| 173 |
{ |
| 174 |
/* |
| 175 |
* One micro-second delay to make sure the reset pulse |
| 176 |
* wide enough so that core reset is clean. |
| 177 |
*/ |
| 178 |
udelay(1); |
| 179 |
reset_control_deassert(pcie->core_rst); |
| 180 |
|
| 181 |
/* |
| 182 |
* Some SoC core reset also reset PHY, more delay needed |
| 183 |
* to make sure the reset process is done. |
| 184 |
*/ |
| 185 |
usleep_range(1000, 2000); |
| 186 |
} |
| 187 |
|
| 188 |
static void intel_pcie_device_rst_assert(struct intel_pcie *pcie) |
| 189 |
{ |
| 190 |
gpiod_set_value_cansleep(pcie->reset_gpio, 1); |
| 191 |
} |
| 192 |
|
| 193 |
static void intel_pcie_device_rst_deassert(struct intel_pcie *pcie) |
| 194 |
{ |
| 195 |
msleep(pcie->rst_intrvl); |
| 196 |
gpiod_set_value_cansleep(pcie->reset_gpio, 0); |
| 197 |
} |
| 198 |
|
| 199 |
static void intel_pcie_core_irq_disable(struct intel_pcie *pcie) |
| 200 |
{ |
| 201 |
pcie_app_wr(pcie, PCIE_APP_IRNEN, 0); |
| 202 |
pcie_app_wr(pcie, PCIE_APP_IRNCR, PCIE_APP_IRN_INT); |
| 203 |
} |
| 204 |
|
| 205 |
static int intel_pcie_get_resources(struct platform_device *pdev) |
| 206 |
{ |
| 207 |
struct intel_pcie *pcie = platform_get_drvdata(pdev); |
| 208 |
struct dw_pcie *pci = &pcie->pci; |
| 209 |
struct device *dev = pci->dev; |
| 210 |
int ret; |
| 211 |
|
| 212 |
pcie->core_clk = devm_clk_get(dev, NULL); |
| 213 |
if (IS_ERR(pcie->core_clk)) { |
| 214 |
ret = PTR_ERR(pcie->core_clk); |
| 215 |
if (ret != -EPROBE_DEFER) |
| 216 |
dev_err(dev, "Failed to get clks: %d\n", ret); |
| 217 |
return ret; |
| 218 |
} |
| 219 |
|
| 220 |
pcie->core_rst = devm_reset_control_get(dev, NULL); |
| 221 |
if (IS_ERR(pcie->core_rst)) { |
| 222 |
ret = PTR_ERR(pcie->core_rst); |
| 223 |
if (ret != -EPROBE_DEFER) |
| 224 |
dev_err(dev, "Failed to get resets: %d\n", ret); |
| 225 |
return ret; |
| 226 |
} |
| 227 |
|
| 228 |
ret = device_property_read_u32(dev, "reset-assert-ms", |
| 229 |
&pcie->rst_intrvl); |
| 230 |
if (ret) |
| 231 |
pcie->rst_intrvl = RESET_INTERVAL_MS; |
| 232 |
|
| 233 |
pcie->app_base = devm_platform_ioremap_resource_byname(pdev, "app"); |
| 234 |
if (IS_ERR(pcie->app_base)) |
| 235 |
return PTR_ERR(pcie->app_base); |
| 236 |
|
| 237 |
pcie->phy = devm_phy_get(dev, "pcie"); |
| 238 |
if (IS_ERR(pcie->phy)) { |
| 239 |
ret = PTR_ERR(pcie->phy); |
| 240 |
if (ret != -EPROBE_DEFER) |
| 241 |
dev_err(dev, "Couldn't get pcie-phy: %d\n", ret); |
| 242 |
return ret; |
| 243 |
} |
| 244 |
|
| 245 |
return 0; |
| 246 |
} |
| 247 |
|
| 248 |
static int intel_pcie_wait_l2(struct intel_pcie *pcie) |
| 249 |
{ |
| 250 |
u32 value; |
| 251 |
int ret; |
| 252 |
struct dw_pcie *pci = &pcie->pci; |
| 253 |
|
| 254 |
if (pci->max_link_speed < 3) |
| 255 |
return 0; |
| 256 |
|
| 257 |
/* Send PME_TURN_OFF message */ |
| 258 |
pcie_app_wr_mask(pcie, PCIE_APP_MSG_CR, PCIE_APP_MSG_XMT_PM_TURNOFF, |
| 259 |
PCIE_APP_MSG_XMT_PM_TURNOFF); |
| 260 |
|
| 261 |
/* Read PMC status and wait for falling into L2 link state */ |
| 262 |
ret = readl_poll_timeout(pcie->app_base + PCIE_APP_PMC, value, |
| 263 |
value & PCIE_APP_PMC_IN_L2, 20, |
| 264 |
jiffies_to_usecs(5 * HZ)); |
| 265 |
if (ret) |
| 266 |
dev_err(pcie->pci.dev, "PCIe link enter L2 timeout!\n"); |
| 267 |
|
| 268 |
return ret; |
| 269 |
} |
| 270 |
|
| 271 |
static void intel_pcie_turn_off(struct intel_pcie *pcie) |
| 272 |
{ |
| 273 |
if (dw_pcie_link_up(&pcie->pci)) |
| 274 |
intel_pcie_wait_l2(pcie); |
| 275 |
|
| 276 |
/* Put endpoint device in reset state */ |
| 277 |
intel_pcie_device_rst_assert(pcie); |
| 278 |
pcie_rc_cfg_wr_mask(pcie, PCI_COMMAND, PCI_COMMAND_MEMORY, 0); |
| 279 |
} |
| 280 |
|
| 281 |
static int intel_pcie_host_setup(struct intel_pcie *pcie) |
| 282 |
{ |
| 283 |
int ret; |
| 284 |
struct dw_pcie *pci = &pcie->pci; |
| 285 |
|
| 286 |
intel_pcie_core_rst_assert(pcie); |
| 287 |
intel_pcie_device_rst_assert(pcie); |
| 288 |
|
| 289 |
ret = phy_init(pcie->phy); |
| 290 |
if (ret) |
| 291 |
return ret; |
| 292 |
|
| 293 |
intel_pcie_core_rst_deassert(pcie); |
| 294 |
|
| 295 |
ret = clk_prepare_enable(pcie->core_clk); |
| 296 |
if (ret) { |
| 297 |
dev_err(pcie->pci.dev, "Core clock enable failed: %d\n", ret); |
| 298 |
goto clk_err; |
| 299 |
} |
| 300 |
|
| 301 |
pci->atu_base = pci->dbi_base + 0xC0000; |
| 302 |
|
| 303 |
intel_pcie_ltssm_disable(pcie); |
| 304 |
intel_pcie_link_setup(pcie); |
| 305 |
intel_pcie_init_n_fts(pci); |
| 306 |
|
| 307 |
ret = dw_pcie_setup_rc(&pci->pp); |
| 308 |
if (ret) |
| 309 |
goto app_init_err; |
| 310 |
|
| 311 |
dw_pcie_upconfig_setup(pci); |
| 312 |
|
| 313 |
intel_pcie_device_rst_deassert(pcie); |
| 314 |
intel_pcie_ltssm_enable(pcie); |
| 315 |
|
| 316 |
ret = dw_pcie_wait_for_link(pci); |
| 317 |
if (ret) |
| 318 |
goto app_init_err; |
| 319 |
|
| 320 |
/* Enable integrated interrupts */ |
| 321 |
pcie_app_wr_mask(pcie, PCIE_APP_IRNEN, PCIE_APP_IRN_INT, |
| 322 |
PCIE_APP_IRN_INT); |
| 323 |
|
| 324 |
return 0; |
| 325 |
|
| 326 |
app_init_err: |
| 327 |
clk_disable_unprepare(pcie->core_clk); |
| 328 |
clk_err: |
| 329 |
intel_pcie_core_rst_assert(pcie); |
| 330 |
phy_exit(pcie->phy); |
| 331 |
|
| 332 |
return ret; |
| 333 |
} |
| 334 |
|
| 335 |
static void __intel_pcie_remove(struct intel_pcie *pcie) |
| 336 |
{ |
| 337 |
intel_pcie_core_irq_disable(pcie); |
| 338 |
intel_pcie_turn_off(pcie); |
| 339 |
clk_disable_unprepare(pcie->core_clk); |
| 340 |
intel_pcie_core_rst_assert(pcie); |
| 341 |
phy_exit(pcie->phy); |
| 342 |
} |
| 343 |
|
| 344 |
static void intel_pcie_remove(struct platform_device *pdev) |
| 345 |
{ |
| 346 |
struct intel_pcie *pcie = platform_get_drvdata(pdev); |
| 347 |
struct dw_pcie_rp *pp = &pcie->pci.pp; |
| 348 |
|
| 349 |
dw_pcie_host_deinit(pp); |
| 350 |
__intel_pcie_remove(pcie); |
| 351 |
} |
| 352 |
|
| 353 |
static int intel_pcie_suspend_noirq(struct device *dev) |
| 354 |
{ |
| 355 |
struct intel_pcie *pcie = dev_get_drvdata(dev); |
| 356 |
int ret; |
| 357 |
|
| 358 |
intel_pcie_core_irq_disable(pcie); |
| 359 |
ret = intel_pcie_wait_l2(pcie); |
| 360 |
if (ret) |
| 361 |
return ret; |
| 362 |
|
| 363 |
phy_exit(pcie->phy); |
| 364 |
clk_disable_unprepare(pcie->core_clk); |
| 365 |
return ret; |
| 366 |
} |
| 367 |
|
| 368 |
static int intel_pcie_resume_noirq(struct device *dev) |
| 369 |
{ |
| 370 |
struct intel_pcie *pcie = dev_get_drvdata(dev); |
| 371 |
|
| 372 |
return intel_pcie_host_setup(pcie); |
| 373 |
} |
| 374 |
|
| 375 |
static int intel_pcie_rc_init(struct dw_pcie_rp *pp) |
| 376 |
{ |
| 377 |
struct dw_pcie *pci = to_dw_pcie_from_pp(pp); |
| 378 |
struct intel_pcie *pcie = dev_get_drvdata(pci->dev); |
| 379 |
|
| 380 |
return intel_pcie_host_setup(pcie); |
| 381 |
} |
| 382 |
|
| 383 |
static const struct dw_pcie_ops intel_pcie_ops = { |
| 384 |
}; |
| 385 |
|
| 386 |
static const struct dw_pcie_host_ops intel_pcie_dw_ops = { |
| 387 |
.init = intel_pcie_rc_init, |
| 388 |
}; |
| 389 |
|
| 390 |
static int intel_pcie_probe(struct platform_device *pdev) |
| 391 |
{ |
| 392 |
struct device *dev = &pdev->dev; |
| 393 |
struct intel_pcie *pcie; |
| 394 |
struct dw_pcie_rp *pp; |
| 395 |
struct dw_pcie *pci; |
| 396 |
int ret; |
| 397 |
|
| 398 |
pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL); |
| 399 |
if (!pcie) |
| 400 |
return -ENOMEM; |
| 401 |
|
| 402 |
platform_set_drvdata(pdev, pcie); |
| 403 |
pci = &pcie->pci; |
| 404 |
pci->dev = dev; |
| 405 |
pci->use_parent_dt_ranges = true; |
| 406 |
pp = &pci->pp; |
| 407 |
|
| 408 |
ret = intel_pcie_get_resources(pdev); |
| 409 |
if (ret) |
| 410 |
return ret; |
| 411 |
|
| 412 |
ret = intel_pcie_ep_rst_init(pcie); |
| 413 |
if (ret) |
| 414 |
return ret; |
| 415 |
|
| 416 |
pci->ops = &intel_pcie_ops; |
| 417 |
pp->ops = &intel_pcie_dw_ops; |
| 418 |
|
| 419 |
ret = dw_pcie_host_init(pp); |
| 420 |
if (ret) { |
| 421 |
dev_err(dev, "Cannot initialize host\n"); |
| 422 |
return ret; |
| 423 |
} |
| 424 |
|
| 425 |
return 0; |
| 426 |
} |
| 427 |
|
| 428 |
static const struct dev_pm_ops intel_pcie_pm_ops = { |
| 429 |
NOIRQ_SYSTEM_SLEEP_PM_OPS(intel_pcie_suspend_noirq, |
| 430 |
intel_pcie_resume_noirq) |
| 431 |
}; |
| 432 |
|
| 433 |
static const struct of_device_id of_intel_pcie_match[] = { |
| 434 |
{ .compatible = "intel,lgm-pcie" }, |
| 435 |
{} |
| 436 |
}; |
| 437 |
|
| 438 |
static struct platform_driver intel_pcie_driver = { |
| 439 |
.probe = intel_pcie_probe, |
| 440 |
.remove = intel_pcie_remove, |
| 441 |
.driver = { |
| 442 |
.name = "intel-gw-pcie", |
| 443 |
.of_match_table = of_intel_pcie_match, |
| 444 |
.pm = &intel_pcie_pm_ops, |
| 445 |
}, |
| 446 |
}; |
| 447 |
builtin_platform_driver(intel_pcie_driver); |