1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * PCIe host controller driver for Amlogic MESON SoCs 4 * 5 * Copyright (c) 2018 Amlogic, inc. 6 * Author: Yue Wang <yue.wang@amlogic.com> 7 */ 8 9 #include <linux/clk.h> 10 #include <linux/delay.h> 11 #include <linux/gpio/consumer.h> 12 #include <linux/pci.h> 13 #include <linux/platform_device.h> 14 #include <linux/reset.h> 15 #include <linux/resource.h> 16 #include <linux/types.h> 17 #include <linux/phy/phy.h> 18 #include <linux/module.h> 19 20 #include "pcie-designware.h" 21 22 #define to_meson_pcie(x) dev_get_drvdata((x)->dev) 23 24 #define PCIE_CAP_MAX_PAYLOAD_SIZE(x) ((x) << 5) 25 #define PCIE_CAP_MAX_READ_REQ_SIZE(x) ((x) << 12) 26 27 /* PCIe specific config registers */ 28 #define PCIE_CFG0 0x0 29 #define APP_LTSSM_ENABLE BIT(7) 30 31 #define PCIE_CFG_STATUS12 0x30 32 #define IS_SMLH_LINK_UP(x) ((x) & (1 << 6)) 33 #define IS_RDLH_LINK_UP(x) ((x) & (1 << 16)) 34 #define IS_LTSSM_UP(x) ((((x) >> 10) & 0x1f) == 0x11) 35 36 #define PCIE_CFG_STATUS17 0x44 37 #define PM_CURRENT_STATE(x) (((x) >> 7) & 0x1) 38 39 #define PORT_CLK_RATE 100000000UL 40 #define MAX_PAYLOAD_SIZE 256 41 #define MAX_READ_REQ_SIZE 256 42 #define PCIE_RESET_DELAY 500 43 #define PCIE_SHARED_RESET 1 44 #define PCIE_NORMAL_RESET 0 45 46 enum pcie_data_rate { 47 PCIE_GEN1, 48 PCIE_GEN2, 49 PCIE_GEN3, 50 PCIE_GEN4 51 }; 52 53 struct meson_pcie_clk_res { 54 struct clk *clk; 55 struct clk *port_clk; 56 struct clk *general_clk; 57 }; 58 59 struct meson_pcie_rc_reset { 60 struct reset_control *port; 61 struct reset_control *apb; 62 }; 63 64 struct meson_pcie { 65 struct dw_pcie pci; 66 void __iomem *cfg_base; 67 struct meson_pcie_clk_res clk_res; 68 struct meson_pcie_rc_reset mrst; 69 struct gpio_desc *reset_gpio; 70 struct phy *phy; 71 }; 72 73 static struct reset_control *meson_pcie_get_reset(struct meson_pcie *mp, 74 const char *id, 75 u32 reset_type) 76 { 77 struct device *dev = mp->pci.dev; 78 struct reset_control *reset; 79 80 if (reset_type == PCIE_SHARED_RESET) 81 reset = devm_reset_control_get_shared(dev, id); 82 else 83 reset = devm_reset_control_get(dev, id); 84 85 return reset; 86 } 87 88 static int meson_pcie_get_resets(struct meson_pcie *mp) 89 { 90 struct meson_pcie_rc_reset *mrst = &mp->mrst; 91 92 mrst->port = meson_pcie_get_reset(mp, "port", PCIE_NORMAL_RESET); 93 if (IS_ERR(mrst->port)) 94 return PTR_ERR(mrst->port); 95 reset_control_deassert(mrst->port); 96 97 mrst->apb = meson_pcie_get_reset(mp, "apb", PCIE_SHARED_RESET); 98 if (IS_ERR(mrst->apb)) 99 return PTR_ERR(mrst->apb); 100 reset_control_deassert(mrst->apb); 101 102 return 0; 103 } 104 105 static int meson_pcie_get_mems(struct platform_device *pdev, 106 struct meson_pcie *mp) 107 { 108 struct dw_pcie *pci = &mp->pci; 109 struct resource *res; 110 111 /* 112 * For the broken DTs that supply 'dbi' as 'elbi', parse the 'elbi' 113 * region and assign it to both 'pci->elbi_base' and 'pci->dbi_space' so 114 * that the DWC core can skip parsing both regions. 115 */ 116 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "elbi"); 117 if (res) { 118 pci->elbi_base = devm_pci_remap_cfg_resource(pci->dev, res); 119 if (IS_ERR(pci->elbi_base)) 120 return PTR_ERR(pci->elbi_base); 121 122 pci->dbi_base = pci->elbi_base; 123 pci->dbi_phys_addr = res->start; 124 } 125 126 mp->cfg_base = devm_platform_ioremap_resource_byname(pdev, "cfg"); 127 if (IS_ERR(mp->cfg_base)) 128 return PTR_ERR(mp->cfg_base); 129 130 return 0; 131 } 132 133 static int meson_pcie_power_on(struct meson_pcie *mp) 134 { 135 int ret = 0; 136 137 ret = phy_init(mp->phy); 138 if (ret) 139 return ret; 140 141 ret = phy_power_on(mp->phy); 142 if (ret) { 143 phy_exit(mp->phy); 144 return ret; 145 } 146 147 return 0; 148 } 149 150 static void meson_pcie_power_off(struct meson_pcie *mp) 151 { 152 phy_power_off(mp->phy); 153 phy_exit(mp->phy); 154 } 155 156 static int meson_pcie_reset(struct meson_pcie *mp) 157 { 158 struct meson_pcie_rc_reset *mrst = &mp->mrst; 159 int ret = 0; 160 161 ret = phy_reset(mp->phy); 162 if (ret) 163 return ret; 164 165 reset_control_assert(mrst->port); 166 reset_control_assert(mrst->apb); 167 udelay(PCIE_RESET_DELAY); 168 reset_control_deassert(mrst->port); 169 reset_control_deassert(mrst->apb); 170 udelay(PCIE_RESET_DELAY); 171 172 return 0; 173 } 174 175 static inline void meson_pcie_disable_clock(void *data) 176 { 177 struct clk *clk = data; 178 179 clk_disable_unprepare(clk); 180 } 181 182 static inline struct clk *meson_pcie_probe_clock(struct device *dev, 183 const char *id, u64 rate) 184 { 185 struct clk *clk; 186 int ret; 187 188 clk = devm_clk_get(dev, id); 189 if (IS_ERR(clk)) 190 return clk; 191 192 if (rate) { 193 ret = clk_set_rate(clk, rate); 194 if (ret) { 195 dev_err(dev, "set clk rate failed, ret = %d\n", ret); 196 return ERR_PTR(ret); 197 } 198 } 199 200 ret = clk_prepare_enable(clk); 201 if (ret) { 202 dev_err(dev, "couldn't enable clk\n"); 203 return ERR_PTR(ret); 204 } 205 206 ret = devm_add_action_or_reset(dev, meson_pcie_disable_clock, clk); 207 if (ret) 208 return ERR_PTR(ret); 209 210 return clk; 211 } 212 213 static int meson_pcie_probe_clocks(struct meson_pcie *mp) 214 { 215 struct device *dev = mp->pci.dev; 216 struct meson_pcie_clk_res *res = &mp->clk_res; 217 218 res->port_clk = meson_pcie_probe_clock(dev, "port", PORT_CLK_RATE); 219 if (IS_ERR(res->port_clk)) 220 return PTR_ERR(res->port_clk); 221 222 res->general_clk = meson_pcie_probe_clock(dev, "general", 0); 223 if (IS_ERR(res->general_clk)) 224 return PTR_ERR(res->general_clk); 225 226 res->clk = meson_pcie_probe_clock(dev, "pclk", 0); 227 if (IS_ERR(res->clk)) 228 return PTR_ERR(res->clk); 229 230 return 0; 231 } 232 233 static inline u32 meson_cfg_readl(struct meson_pcie *mp, u32 reg) 234 { 235 return readl(mp->cfg_base + reg); 236 } 237 238 static inline void meson_cfg_writel(struct meson_pcie *mp, u32 val, u32 reg) 239 { 240 writel(val, mp->cfg_base + reg); 241 } 242 243 static void meson_pcie_assert_reset(struct meson_pcie *mp) 244 { 245 gpiod_set_value_cansleep(mp->reset_gpio, 1); 246 udelay(500); 247 gpiod_set_value_cansleep(mp->reset_gpio, 0); 248 } 249 250 static void meson_pcie_ltssm_enable(struct meson_pcie *mp) 251 { 252 u32 val; 253 254 val = meson_cfg_readl(mp, PCIE_CFG0); 255 val |= APP_LTSSM_ENABLE; 256 meson_cfg_writel(mp, val, PCIE_CFG0); 257 } 258 259 static int meson_size_to_payload(struct meson_pcie *mp, int size) 260 { 261 struct device *dev = mp->pci.dev; 262 263 /* 264 * dwc supports 2^(val+7) payload size, which val is 0~5 default to 1. 265 * So if input size is not 2^order alignment or less than 2^7 or bigger 266 * than 2^12, just set to default size 2^(1+7). 267 */ 268 if (!is_power_of_2(size) || size < 128 || size > 4096) { 269 dev_warn(dev, "payload size %d, set to default 256\n", size); 270 return 1; 271 } 272 273 return fls(size) - 8; 274 } 275 276 static void meson_set_max_payload(struct meson_pcie *mp, int size) 277 { 278 struct dw_pcie *pci = &mp->pci; 279 u32 val; 280 u16 offset = dw_pcie_find_capability(pci, PCI_CAP_ID_EXP); 281 int max_payload_size = meson_size_to_payload(mp, size); 282 283 val = dw_pcie_readl_dbi(pci, offset + PCI_EXP_DEVCTL); 284 val &= ~PCI_EXP_DEVCTL_PAYLOAD; 285 dw_pcie_writel_dbi(pci, offset + PCI_EXP_DEVCTL, val); 286 287 val = dw_pcie_readl_dbi(pci, offset + PCI_EXP_DEVCTL); 288 val |= PCIE_CAP_MAX_PAYLOAD_SIZE(max_payload_size); 289 dw_pcie_writel_dbi(pci, offset + PCI_EXP_DEVCTL, val); 290 } 291 292 static void meson_set_max_rd_req_size(struct meson_pcie *mp, int size) 293 { 294 struct dw_pcie *pci = &mp->pci; 295 u32 val; 296 u16 offset = dw_pcie_find_capability(pci, PCI_CAP_ID_EXP); 297 int max_rd_req_size = meson_size_to_payload(mp, size); 298 299 val = dw_pcie_readl_dbi(pci, offset + PCI_EXP_DEVCTL); 300 val &= ~PCI_EXP_DEVCTL_READRQ; 301 dw_pcie_writel_dbi(pci, offset + PCI_EXP_DEVCTL, val); 302 303 val = dw_pcie_readl_dbi(pci, offset + PCI_EXP_DEVCTL); 304 val |= PCIE_CAP_MAX_READ_REQ_SIZE(max_rd_req_size); 305 dw_pcie_writel_dbi(pci, offset + PCI_EXP_DEVCTL, val); 306 } 307 308 static int meson_pcie_start_link(struct dw_pcie *pci) 309 { 310 struct meson_pcie *mp = to_meson_pcie(pci); 311 312 meson_pcie_ltssm_enable(mp); 313 meson_pcie_assert_reset(mp); 314 315 return 0; 316 } 317 318 static int meson_pcie_rd_own_conf(struct pci_bus *bus, u32 devfn, 319 int where, int size, u32 *val) 320 { 321 int ret; 322 323 ret = pci_generic_config_read(bus, devfn, where, size, val); 324 if (ret != PCIBIOS_SUCCESSFUL) 325 return ret; 326 327 /* 328 * There is a bug in the MESON AXG PCIe controller whereby software 329 * cannot program the PCI_CLASS_DEVICE register, so we must fabricate 330 * the return value in the config accessors. 331 */ 332 if ((where & ~3) == PCI_CLASS_REVISION) { 333 if (size <= 2) 334 *val = (*val & ((1 << (size * 8)) - 1)) << (8 * (where & 3)); 335 *val &= ~0xffffff00; 336 *val |= PCI_CLASS_BRIDGE_PCI_NORMAL << 8; 337 if (size <= 2) 338 *val = (*val >> (8 * (where & 3))) & ((1 << (size * 8)) - 1); 339 } 340 341 return PCIBIOS_SUCCESSFUL; 342 } 343 344 static struct pci_ops meson_pci_ops = { 345 .map_bus = dw_pcie_own_conf_map_bus, 346 .read = meson_pcie_rd_own_conf, 347 .write = pci_generic_config_write, 348 }; 349 350 static bool meson_pcie_link_up(struct dw_pcie *pci) 351 { 352 struct meson_pcie *mp = to_meson_pcie(pci); 353 u32 state12; 354 355 state12 = meson_cfg_readl(mp, PCIE_CFG_STATUS12); 356 return IS_SMLH_LINK_UP(state12) && IS_RDLH_LINK_UP(state12); 357 } 358 359 static int meson_pcie_host_init(struct dw_pcie_rp *pp) 360 { 361 struct dw_pcie *pci = to_dw_pcie_from_pp(pp); 362 struct meson_pcie *mp = to_meson_pcie(pci); 363 364 pp->bridge->ops = &meson_pci_ops; 365 366 meson_set_max_payload(mp, MAX_PAYLOAD_SIZE); 367 meson_set_max_rd_req_size(mp, MAX_READ_REQ_SIZE); 368 369 return 0; 370 } 371 372 static const struct dw_pcie_host_ops meson_pcie_host_ops = { 373 .init = meson_pcie_host_init, 374 }; 375 376 static const struct dw_pcie_ops dw_pcie_ops = { 377 .link_up = meson_pcie_link_up, 378 .start_link = meson_pcie_start_link, 379 }; 380 381 static int meson_pcie_probe(struct platform_device *pdev) 382 { 383 struct device *dev = &pdev->dev; 384 struct dw_pcie *pci; 385 struct meson_pcie *mp; 386 int ret; 387 388 mp = devm_kzalloc(dev, sizeof(*mp), GFP_KERNEL); 389 if (!mp) 390 return -ENOMEM; 391 392 pci = &mp->pci; 393 pci->dev = dev; 394 pci->ops = &dw_pcie_ops; 395 pci->pp.ops = &meson_pcie_host_ops; 396 pci->num_lanes = 1; 397 398 mp->phy = devm_phy_get(dev, "pcie"); 399 if (IS_ERR(mp->phy)) { 400 dev_err(dev, "get phy failed, %pe\n", mp->phy); 401 return PTR_ERR(mp->phy); 402 } 403 404 mp->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH); 405 if (IS_ERR(mp->reset_gpio)) { 406 dev_err(dev, "get reset gpio failed\n"); 407 return PTR_ERR(mp->reset_gpio); 408 } 409 410 ret = meson_pcie_get_resets(mp); 411 if (ret) { 412 dev_err(dev, "get reset resource failed, %d\n", ret); 413 return ret; 414 } 415 416 ret = meson_pcie_get_mems(pdev, mp); 417 if (ret) { 418 dev_err(dev, "get memory resource failed, %d\n", ret); 419 return ret; 420 } 421 422 ret = meson_pcie_power_on(mp); 423 if (ret) { 424 dev_err(dev, "phy power on failed, %d\n", ret); 425 return ret; 426 } 427 428 ret = meson_pcie_reset(mp); 429 if (ret) { 430 dev_err(dev, "reset failed, %d\n", ret); 431 goto err_phy; 432 } 433 434 ret = meson_pcie_probe_clocks(mp); 435 if (ret) { 436 dev_err(dev, "init clock resources failed, %d\n", ret); 437 goto err_phy; 438 } 439 440 platform_set_drvdata(pdev, mp); 441 442 ret = dw_pcie_host_init(&pci->pp); 443 if (ret < 0) { 444 dev_err(dev, "Add PCIe port failed, %d\n", ret); 445 goto err_phy; 446 } 447 448 return 0; 449 450 err_phy: 451 meson_pcie_power_off(mp); 452 return ret; 453 } 454 455 static void meson_pcie_remove(struct platform_device *pdev) 456 { 457 struct meson_pcie *mp = platform_get_drvdata(pdev); 458 459 dw_pcie_host_deinit(&mp->pci.pp); 460 meson_pcie_power_off(mp); 461 } 462 463 static const struct of_device_id meson_pcie_of_match[] = { 464 { 465 .compatible = "amlogic,axg-pcie", 466 }, 467 { 468 .compatible = "amlogic,g12a-pcie", 469 }, 470 {}, 471 }; 472 MODULE_DEVICE_TABLE(of, meson_pcie_of_match); 473 474 static struct platform_driver meson_pcie_driver = { 475 .probe = meson_pcie_probe, 476 .remove = meson_pcie_remove, 477 .driver = { 478 .name = "meson-pcie", 479 .of_match_table = meson_pcie_of_match, 480 }, 481 }; 482 483 module_platform_driver(meson_pcie_driver); 484 485 MODULE_AUTHOR("Yue Wang <yue.wang@amlogic.com>"); 486 MODULE_DESCRIPTION("Amlogic PCIe Controller driver"); 487 MODULE_LICENSE("GPL v2"); 488