Yan He | 50222ad | 2017-11-17 18:22:17 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2015-2017, The Linux Foundation. All rights reserved. |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | */ |
| 12 | |
| 13 | /* |
| 14 | * MSM PCIe endpoint core driver. |
| 15 | */ |
| 16 | |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/bitops.h> |
| 19 | #include <linux/clk.h> |
| 20 | #include <linux/debugfs.h> |
| 21 | #include <linux/delay.h> |
| 22 | #include <linux/gpio.h> |
| 23 | #include <linux/iopoll.h> |
| 24 | #include <linux/kernel.h> |
| 25 | #include <linux/platform_device.h> |
| 26 | #include <linux/regulator/consumer.h> |
| 27 | #include <linux/slab.h> |
| 28 | #include <linux/types.h> |
| 29 | #include <linux/of_gpio.h> |
| 30 | #include <linux/clk/qcom.h> |
| 31 | #include <linux/reset.h> |
| 32 | #include <linux/msm-bus.h> |
| 33 | #include <linux/msm-bus-board.h> |
| 34 | #include <linux/interrupt.h> |
| 35 | #include <linux/irq.h> |
| 36 | |
| 37 | #include "ep_pcie_com.h" |
| 38 | |
| 39 | /* debug mask sys interface */ |
| 40 | static int ep_pcie_debug_mask; |
| 41 | static int ep_pcie_debug_keep_resource; |
| 42 | static u32 ep_pcie_bar0_address; |
| 43 | module_param_named(debug_mask, ep_pcie_debug_mask, |
| 44 | int, 0664); |
| 45 | module_param_named(debug_keep_resource, ep_pcie_debug_keep_resource, |
| 46 | int, 0664); |
| 47 | module_param_named(bar0_address, ep_pcie_bar0_address, |
| 48 | int, 0664); |
| 49 | |
| 50 | struct ep_pcie_dev_t ep_pcie_dev = {0}; |
| 51 | |
| 52 | static struct ep_pcie_vreg_info_t ep_pcie_vreg_info[EP_PCIE_MAX_VREG] = { |
| 53 | {NULL, "vreg-1.8", 1800000, 1800000, 14000, true}, |
| 54 | {NULL, "vreg-0.9", 1000000, 1000000, 40000, true} |
| 55 | }; |
| 56 | |
| 57 | static struct ep_pcie_gpio_info_t ep_pcie_gpio_info[EP_PCIE_MAX_GPIO] = { |
| 58 | {"perst-gpio", 0, 0, 0, 1}, |
| 59 | {"wake-gpio", 0, 1, 0, 1}, |
| 60 | {"clkreq-gpio", 0, 1, 0, 0}, |
| 61 | {"mdm2apstatus-gpio", 0, 1, 1, 0} |
| 62 | }; |
| 63 | |
| 64 | static struct ep_pcie_clk_info_t |
| 65 | ep_pcie_clk_info[EP_PCIE_MAX_CLK] = { |
| 66 | {NULL, "pcie_0_cfg_ahb_clk", 0, true}, |
| 67 | {NULL, "pcie_0_mstr_axi_clk", 0, true}, |
| 68 | {NULL, "pcie_0_slv_axi_clk", 0, true}, |
| 69 | {NULL, "pcie_0_aux_clk", 1000000, true}, |
| 70 | {NULL, "pcie_0_ldo", 0, true}, |
| 71 | }; |
| 72 | |
| 73 | static struct ep_pcie_clk_info_t |
| 74 | ep_pcie_pipe_clk_info[EP_PCIE_MAX_PIPE_CLK] = { |
| 75 | {NULL, "pcie_0_pipe_clk", 62500000, true} |
| 76 | }; |
| 77 | |
| 78 | static struct ep_pcie_reset_info_t |
| 79 | ep_pcie_reset_info[EP_PCIE_MAX_RESET] = { |
| 80 | {NULL, "pcie_0_core_reset", false}, |
| 81 | {NULL, "pcie_0_phy_reset", false}, |
| 82 | }; |
| 83 | |
| 84 | static const struct ep_pcie_res_info_t ep_pcie_res_info[EP_PCIE_MAX_RES] = { |
| 85 | {"parf", 0, 0}, |
| 86 | {"phy", 0, 0}, |
| 87 | {"mmio", 0, 0}, |
| 88 | {"msi", 0, 0}, |
| 89 | {"dm_core", 0, 0}, |
| 90 | {"elbi", 0, 0} |
| 91 | }; |
| 92 | |
| 93 | static const struct ep_pcie_irq_info_t ep_pcie_irq_info[EP_PCIE_MAX_IRQ] = { |
| 94 | {"int_pm_turnoff", 0}, |
| 95 | {"int_dstate_change", 0}, |
| 96 | {"int_l1sub_timeout", 0}, |
| 97 | {"int_link_up", 0}, |
| 98 | {"int_link_down", 0}, |
| 99 | {"int_bridge_flush_n", 0}, |
| 100 | {"int_bme", 0}, |
| 101 | {"int_global", 0} |
| 102 | }; |
| 103 | |
| 104 | int ep_pcie_get_debug_mask(void) |
| 105 | { |
| 106 | return ep_pcie_debug_mask; |
| 107 | } |
| 108 | |
| 109 | static bool ep_pcie_confirm_linkup(struct ep_pcie_dev_t *dev, |
| 110 | bool check_sw_stts) |
| 111 | { |
| 112 | u32 val; |
| 113 | |
| 114 | if (check_sw_stts && (dev->link_status != EP_PCIE_LINK_ENABLED)) { |
| 115 | EP_PCIE_DBG(dev, "PCIe V%d: The link is not enabled.\n", |
| 116 | dev->rev); |
| 117 | return false; |
| 118 | } |
| 119 | |
| 120 | val = readl_relaxed(dev->dm_core); |
| 121 | EP_PCIE_DBG(dev, "PCIe V%d: device ID and vender ID are 0x%x.\n", |
| 122 | dev->rev, val); |
| 123 | if (val == EP_PCIE_LINK_DOWN) { |
| 124 | EP_PCIE_ERR(dev, |
| 125 | "PCIe V%d: The link is not really up; device ID and vender ID are 0x%x.\n", |
| 126 | dev->rev, val); |
| 127 | return false; |
| 128 | } |
| 129 | |
| 130 | return true; |
| 131 | } |
| 132 | |
| 133 | static int ep_pcie_gpio_init(struct ep_pcie_dev_t *dev) |
| 134 | { |
| 135 | int i, rc = 0; |
| 136 | struct ep_pcie_gpio_info_t *info; |
| 137 | |
| 138 | EP_PCIE_DBG(dev, "PCIe V%d\n", dev->rev); |
| 139 | |
| 140 | for (i = 0; i < EP_PCIE_MAX_GPIO; i++) { |
| 141 | info = &dev->gpio[i]; |
| 142 | |
| 143 | if (!info->num) { |
| 144 | if (i == EP_PCIE_GPIO_MDM2AP) { |
| 145 | EP_PCIE_DBG(dev, |
| 146 | "PCIe V%d: gpio %s does not exist.\n", |
| 147 | dev->rev, info->name); |
| 148 | continue; |
| 149 | } else { |
| 150 | EP_PCIE_ERR(dev, |
| 151 | "PCIe V%d: the number of gpio %s is invalid\n", |
| 152 | dev->rev, info->name); |
| 153 | rc = -EINVAL; |
| 154 | break; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | rc = gpio_request(info->num, info->name); |
| 159 | if (rc) { |
| 160 | EP_PCIE_ERR(dev, "PCIe V%d: can't get gpio %s; %d\n", |
| 161 | dev->rev, info->name, rc); |
| 162 | break; |
| 163 | } |
| 164 | |
| 165 | if (info->out) |
| 166 | rc = gpio_direction_output(info->num, info->init); |
| 167 | else |
| 168 | rc = gpio_direction_input(info->num); |
| 169 | if (rc) { |
| 170 | EP_PCIE_ERR(dev, |
| 171 | "PCIe V%d: can't set direction for GPIO %s:%d\n", |
| 172 | dev->rev, info->name, rc); |
| 173 | gpio_free(info->num); |
| 174 | break; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | if (rc) |
| 179 | while (i--) |
| 180 | gpio_free(dev->gpio[i].num); |
| 181 | |
| 182 | return rc; |
| 183 | } |
| 184 | |
| 185 | static void ep_pcie_gpio_deinit(struct ep_pcie_dev_t *dev) |
| 186 | { |
| 187 | int i; |
| 188 | |
| 189 | EP_PCIE_DBG(dev, "PCIe V%d\n", dev->rev); |
| 190 | |
| 191 | for (i = 0; i < EP_PCIE_MAX_GPIO; i++) |
| 192 | gpio_free(dev->gpio[i].num); |
| 193 | } |
| 194 | |
| 195 | static int ep_pcie_vreg_init(struct ep_pcie_dev_t *dev) |
| 196 | { |
| 197 | int i, rc = 0; |
| 198 | struct regulator *vreg; |
| 199 | struct ep_pcie_vreg_info_t *info; |
| 200 | |
| 201 | EP_PCIE_DBG(dev, "PCIe V%d\n", dev->rev); |
| 202 | |
| 203 | for (i = 0; i < EP_PCIE_MAX_VREG; i++) { |
| 204 | info = &dev->vreg[i]; |
| 205 | vreg = info->hdl; |
| 206 | |
| 207 | if (!vreg) { |
| 208 | EP_PCIE_ERR(dev, |
| 209 | "PCIe V%d: handle of Vreg %s is NULL\n", |
| 210 | dev->rev, info->name); |
| 211 | rc = -EINVAL; |
| 212 | break; |
| 213 | } |
| 214 | |
| 215 | EP_PCIE_DBG(dev, "PCIe V%d: Vreg %s is being enabled\n", |
| 216 | dev->rev, info->name); |
| 217 | if (info->max_v) { |
| 218 | rc = regulator_set_voltage(vreg, |
| 219 | info->min_v, info->max_v); |
| 220 | if (rc) { |
| 221 | EP_PCIE_ERR(dev, |
| 222 | "PCIe V%d: can't set voltage for %s: %d\n", |
| 223 | dev->rev, info->name, rc); |
| 224 | break; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | if (info->opt_mode) { |
| 229 | rc = regulator_set_load(vreg, info->opt_mode); |
| 230 | if (rc < 0) { |
| 231 | EP_PCIE_ERR(dev, |
| 232 | "PCIe V%d: can't set mode for %s: %d\n", |
| 233 | dev->rev, info->name, rc); |
| 234 | break; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | rc = regulator_enable(vreg); |
| 239 | if (rc) { |
| 240 | EP_PCIE_ERR(dev, |
| 241 | "PCIe V%d: can't enable regulator %s: %d\n", |
| 242 | dev->rev, info->name, rc); |
| 243 | break; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | if (rc) |
| 248 | while (i--) { |
| 249 | struct regulator *hdl = dev->vreg[i].hdl; |
| 250 | |
| 251 | if (hdl) |
| 252 | regulator_disable(hdl); |
| 253 | } |
| 254 | |
| 255 | return rc; |
| 256 | } |
| 257 | |
| 258 | static void ep_pcie_vreg_deinit(struct ep_pcie_dev_t *dev) |
| 259 | { |
| 260 | int i; |
| 261 | |
| 262 | EP_PCIE_DBG(dev, "PCIe V%d\n", dev->rev); |
| 263 | |
| 264 | for (i = EP_PCIE_MAX_VREG - 1; i >= 0; i--) { |
| 265 | if (dev->vreg[i].hdl) { |
| 266 | EP_PCIE_DBG(dev, "Vreg %s is being disabled\n", |
| 267 | dev->vreg[i].name); |
| 268 | regulator_disable(dev->vreg[i].hdl); |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | static int ep_pcie_clk_init(struct ep_pcie_dev_t *dev) |
| 274 | { |
| 275 | int i, rc = 0; |
| 276 | struct ep_pcie_clk_info_t *info; |
| 277 | struct ep_pcie_reset_info_t *reset_info; |
| 278 | |
| 279 | EP_PCIE_DBG(dev, "PCIe V%d\n", dev->rev); |
| 280 | |
| 281 | rc = regulator_enable(dev->gdsc); |
| 282 | |
| 283 | if (rc) { |
| 284 | EP_PCIE_ERR(dev, "PCIe V%d: fail to enable GDSC for %s\n", |
| 285 | dev->rev, dev->pdev->name); |
| 286 | return rc; |
| 287 | } |
| 288 | |
| 289 | if (dev->bus_client) { |
| 290 | rc = msm_bus_scale_client_update_request(dev->bus_client, 1); |
| 291 | if (rc) { |
| 292 | EP_PCIE_ERR(dev, |
| 293 | "PCIe V%d: fail to set bus bandwidth:%d.\n", |
| 294 | dev->rev, rc); |
| 295 | return rc; |
| 296 | } |
| 297 | EP_PCIE_DBG(dev, |
| 298 | "PCIe V%d: set bus bandwidth.\n", |
| 299 | dev->rev); |
| 300 | } |
| 301 | |
| 302 | for (i = 0; i < EP_PCIE_MAX_CLK; i++) { |
| 303 | info = &dev->clk[i]; |
| 304 | |
| 305 | if (!info->hdl) { |
| 306 | EP_PCIE_DBG(dev, |
| 307 | "PCIe V%d: handle of Clock %s is NULL\n", |
| 308 | dev->rev, info->name); |
| 309 | continue; |
| 310 | } |
| 311 | |
| 312 | if (info->freq) { |
| 313 | rc = clk_set_rate(info->hdl, info->freq); |
| 314 | if (rc) { |
| 315 | EP_PCIE_ERR(dev, |
| 316 | "PCIe V%d: can't set rate for clk %s: %d.\n", |
| 317 | dev->rev, info->name, rc); |
| 318 | break; |
| 319 | } |
| 320 | EP_PCIE_DBG(dev, |
| 321 | "PCIe V%d: set rate for clk %s.\n", |
| 322 | dev->rev, info->name); |
| 323 | } |
| 324 | |
| 325 | rc = clk_prepare_enable(info->hdl); |
| 326 | |
| 327 | if (rc) |
| 328 | EP_PCIE_ERR(dev, "PCIe V%d: failed to enable clk %s\n", |
| 329 | dev->rev, info->name); |
| 330 | else |
| 331 | EP_PCIE_DBG(dev, "PCIe V%d: enable clk %s.\n", |
| 332 | dev->rev, info->name); |
| 333 | } |
| 334 | |
| 335 | if (rc) { |
| 336 | EP_PCIE_DBG(dev, |
| 337 | "PCIe V%d: disable clocks for error handling.\n", |
| 338 | dev->rev); |
| 339 | while (i--) { |
| 340 | struct clk *hdl = dev->clk[i].hdl; |
| 341 | |
| 342 | if (hdl) |
| 343 | clk_disable_unprepare(hdl); |
| 344 | } |
| 345 | |
| 346 | regulator_disable(dev->gdsc); |
| 347 | } |
| 348 | |
| 349 | for (i = 0; i < EP_PCIE_MAX_RESET; i++) { |
| 350 | reset_info = &dev->reset[i]; |
| 351 | if (reset_info->hdl) { |
| 352 | rc = reset_control_assert(reset_info->hdl); |
| 353 | if (rc) |
| 354 | EP_PCIE_ERR(dev, |
| 355 | "PCIe V%d: failed to assert reset for %s.\n", |
| 356 | dev->rev, reset_info->name); |
| 357 | else |
| 358 | EP_PCIE_DBG(dev, |
| 359 | "PCIe V%d: successfully asserted reset for %s.\n", |
| 360 | dev->rev, reset_info->name); |
| 361 | |
| 362 | /* add a 1ms delay to ensure the reset is asserted */ |
| 363 | usleep_range(1000, 1005); |
| 364 | |
| 365 | rc = reset_control_deassert(reset_info->hdl); |
| 366 | if (rc) |
| 367 | EP_PCIE_ERR(dev, |
| 368 | "PCIe V%d: failed to deassert reset for %s.\n", |
| 369 | dev->rev, reset_info->name); |
| 370 | else |
| 371 | EP_PCIE_DBG(dev, |
| 372 | "PCIe V%d: successfully deasserted reset for %s.\n", |
| 373 | dev->rev, reset_info->name); |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | return rc; |
| 378 | } |
| 379 | |
| 380 | static void ep_pcie_clk_deinit(struct ep_pcie_dev_t *dev) |
| 381 | { |
| 382 | int i; |
| 383 | int rc; |
| 384 | |
| 385 | EP_PCIE_DBG(dev, "PCIe V%d\n", dev->rev); |
| 386 | |
| 387 | for (i = EP_PCIE_MAX_CLK - 1; i >= 0; i--) |
| 388 | if (dev->clk[i].hdl) |
| 389 | clk_disable_unprepare(dev->clk[i].hdl); |
| 390 | |
| 391 | if (dev->bus_client) { |
| 392 | rc = msm_bus_scale_client_update_request(dev->bus_client, 0); |
| 393 | if (rc) |
| 394 | EP_PCIE_ERR(dev, |
| 395 | "PCIe V%d: fail to relinquish bus bandwidth:%d.\n", |
| 396 | dev->rev, rc); |
| 397 | else |
| 398 | EP_PCIE_DBG(dev, |
| 399 | "PCIe V%d: relinquish bus bandwidth.\n", |
| 400 | dev->rev); |
| 401 | } |
| 402 | |
| 403 | regulator_disable(dev->gdsc); |
| 404 | } |
| 405 | |
| 406 | static int ep_pcie_pipe_clk_init(struct ep_pcie_dev_t *dev) |
| 407 | { |
| 408 | int i, rc = 0; |
| 409 | struct ep_pcie_clk_info_t *info; |
| 410 | |
| 411 | EP_PCIE_DBG(dev, "PCIe V%d\n", dev->rev); |
| 412 | |
| 413 | for (i = 0; i < EP_PCIE_MAX_PIPE_CLK; i++) { |
| 414 | info = &dev->pipeclk[i]; |
| 415 | |
| 416 | if (!info->hdl) { |
| 417 | EP_PCIE_ERR(dev, |
| 418 | "PCIe V%d: handle of Pipe Clock %s is NULL\n", |
| 419 | dev->rev, info->name); |
| 420 | rc = -EINVAL; |
| 421 | break; |
| 422 | } |
| 423 | |
| 424 | if (info->freq) { |
| 425 | rc = clk_set_rate(info->hdl, info->freq); |
| 426 | if (rc) { |
| 427 | EP_PCIE_ERR(dev, |
| 428 | "PCIe V%d: can't set rate for clk %s: %d.\n", |
| 429 | dev->rev, info->name, rc); |
| 430 | break; |
| 431 | } |
| 432 | EP_PCIE_DBG(dev, |
| 433 | "PCIe V%d: set rate for clk %s\n", |
| 434 | dev->rev, info->name); |
| 435 | } |
| 436 | |
| 437 | rc = clk_prepare_enable(info->hdl); |
| 438 | |
| 439 | if (rc) |
| 440 | EP_PCIE_ERR(dev, "PCIe V%d: failed to enable clk %s.\n", |
| 441 | dev->rev, info->name); |
| 442 | else |
| 443 | EP_PCIE_DBG(dev, "PCIe V%d: enabled pipe clk %s.\n", |
| 444 | dev->rev, info->name); |
| 445 | } |
| 446 | |
| 447 | if (rc) { |
| 448 | EP_PCIE_DBG(dev, |
| 449 | "PCIe V%d: disable pipe clocks for error handling.\n", |
| 450 | dev->rev); |
| 451 | while (i--) |
| 452 | if (dev->pipeclk[i].hdl) |
| 453 | clk_disable_unprepare(dev->pipeclk[i].hdl); |
| 454 | } |
| 455 | |
| 456 | return rc; |
| 457 | } |
| 458 | |
| 459 | static void ep_pcie_pipe_clk_deinit(struct ep_pcie_dev_t *dev) |
| 460 | { |
| 461 | int i; |
| 462 | |
| 463 | EP_PCIE_DBG(dev, "PCIe V%d\n", dev->rev); |
| 464 | |
| 465 | for (i = 0; i < EP_PCIE_MAX_PIPE_CLK; i++) |
| 466 | if (dev->pipeclk[i].hdl) |
| 467 | clk_disable_unprepare( |
| 468 | dev->pipeclk[i].hdl); |
| 469 | } |
| 470 | |
| 471 | static void ep_pcie_bar_init(struct ep_pcie_dev_t *dev) |
| 472 | { |
| 473 | struct resource *res = dev->res[EP_PCIE_RES_MMIO].resource; |
| 474 | u32 mask = res->end - res->start; |
| 475 | u32 properties = 0x4; |
| 476 | |
| 477 | EP_PCIE_DBG(dev, "PCIe V%d: BAR mask to program is 0x%x\n", |
| 478 | dev->rev, mask); |
| 479 | |
| 480 | /* Configure BAR mask via CS2 */ |
| 481 | ep_pcie_write_mask(dev->elbi + PCIE20_ELBI_CS2_ENABLE, 0, BIT(0)); |
| 482 | ep_pcie_write_reg(dev->dm_core, PCIE20_BAR0, mask); |
| 483 | ep_pcie_write_reg(dev->dm_core, PCIE20_BAR0 + 0x4, 0); |
| 484 | ep_pcie_write_reg(dev->dm_core, PCIE20_BAR0 + 0x8, mask); |
| 485 | ep_pcie_write_reg(dev->dm_core, PCIE20_BAR0 + 0xc, 0); |
| 486 | ep_pcie_write_reg(dev->dm_core, PCIE20_BAR0 + 0x10, 0); |
| 487 | ep_pcie_write_reg(dev->dm_core, PCIE20_BAR0 + 0x14, 0); |
| 488 | ep_pcie_write_mask(dev->elbi + PCIE20_ELBI_CS2_ENABLE, BIT(0), 0); |
| 489 | |
| 490 | /* Configure BAR properties via CS */ |
| 491 | ep_pcie_write_mask(dev->dm_core + PCIE20_MISC_CONTROL_1, 0, BIT(0)); |
| 492 | ep_pcie_write_reg(dev->dm_core, PCIE20_BAR0, properties); |
| 493 | ep_pcie_write_reg(dev->dm_core, PCIE20_BAR0 + 0x8, properties); |
| 494 | ep_pcie_write_mask(dev->dm_core + PCIE20_MISC_CONTROL_1, BIT(0), 0); |
| 495 | } |
| 496 | |
| 497 | static void ep_pcie_core_init(struct ep_pcie_dev_t *dev, bool configured) |
| 498 | { |
| 499 | EP_PCIE_DBG(dev, "PCIe V%d\n", dev->rev); |
| 500 | |
| 501 | /* enable debug IRQ */ |
| 502 | ep_pcie_write_mask(dev->parf + PCIE20_PARF_DEBUG_INT_EN, |
| 503 | 0, BIT(3) | BIT(2) | BIT(1)); |
| 504 | |
| 505 | if (!configured) { |
| 506 | /* Configure PCIe to endpoint mode */ |
| 507 | ep_pcie_write_reg(dev->parf, PCIE20_PARF_DEVICE_TYPE, 0x0); |
| 508 | |
| 509 | /* adjust DBI base address */ |
| 510 | if (dev->dbi_base_reg) |
| 511 | writel_relaxed(0x3FFFE000, |
| 512 | dev->parf + dev->dbi_base_reg); |
| 513 | else |
| 514 | writel_relaxed(0x3FFFE000, |
| 515 | dev->parf + PCIE20_PARF_DBI_BASE_ADDR); |
| 516 | |
| 517 | /* Configure PCIe core to support 1GB aperture */ |
| 518 | if (dev->slv_space_reg) |
| 519 | ep_pcie_write_reg(dev->parf, dev->slv_space_reg, |
| 520 | 0x40000000); |
| 521 | else |
| 522 | ep_pcie_write_reg(dev->parf, |
| 523 | PCIE20_PARF_SLV_ADDR_SPACE_SIZE, 0x40000000); |
| 524 | |
| 525 | /* Configure link speed */ |
| 526 | ep_pcie_write_mask(dev->dm_core + |
| 527 | PCIE20_LINK_CONTROL2_LINK_STATUS2, |
| 528 | 0xf, dev->link_speed); |
| 529 | } |
| 530 | |
| 531 | /* Read halts write */ |
| 532 | ep_pcie_write_mask(dev->parf + PCIE20_PARF_AXI_MSTR_RD_HALT_NO_WRITES, |
| 533 | 0, BIT(0)); |
| 534 | |
| 535 | /* Write after write halt */ |
| 536 | ep_pcie_write_mask(dev->parf + PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT, |
| 537 | 0, BIT(31)); |
| 538 | |
| 539 | /* Q2A flush disable */ |
| 540 | writel_relaxed(0, dev->parf + PCIE20_PARF_Q2A_FLUSH); |
| 541 | |
| 542 | /* Disable the DBI Wakeup */ |
| 543 | ep_pcie_write_mask(dev->parf + PCIE20_PARF_SYS_CTRL, BIT(11), 0); |
| 544 | |
| 545 | /* Disable the debouncers */ |
| 546 | ep_pcie_write_reg(dev->parf, PCIE20_PARF_DB_CTRL, 0x73); |
| 547 | |
| 548 | /* Disable core clock CGC */ |
| 549 | ep_pcie_write_mask(dev->parf + PCIE20_PARF_SYS_CTRL, 0, BIT(6)); |
| 550 | |
| 551 | /* Set AUX power to be on */ |
| 552 | ep_pcie_write_mask(dev->parf + PCIE20_PARF_SYS_CTRL, 0, BIT(4)); |
| 553 | |
| 554 | /* Request to exit from L1SS for MSI and LTR MSG */ |
| 555 | ep_pcie_write_mask(dev->parf + PCIE20_PARF_CFG_BITS, 0, BIT(1)); |
| 556 | |
| 557 | EP_PCIE_DBG(dev, |
| 558 | "Initial: CLASS_CODE_REVISION_ID:0x%x; HDR_TYPE:0x%x\n", |
| 559 | readl_relaxed(dev->dm_core + PCIE20_CLASS_CODE_REVISION_ID), |
| 560 | readl_relaxed(dev->dm_core + PCIE20_BIST_HDR_TYPE)); |
| 561 | |
| 562 | if (!configured) { |
| 563 | /* Enable CS for RO(CS) register writes */ |
| 564 | ep_pcie_write_mask(dev->dm_core + PCIE20_MISC_CONTROL_1, 0, |
| 565 | BIT(0)); |
| 566 | |
| 567 | /* Set class code and revision ID */ |
| 568 | ep_pcie_write_reg(dev->dm_core, PCIE20_CLASS_CODE_REVISION_ID, |
| 569 | 0xff000000); |
| 570 | |
| 571 | /* Set header type */ |
| 572 | ep_pcie_write_reg(dev->dm_core, PCIE20_BIST_HDR_TYPE, 0x10); |
| 573 | |
| 574 | /* Set Subsystem ID and Subsystem Vendor ID */ |
| 575 | ep_pcie_write_reg(dev->dm_core, PCIE20_SUBSYSTEM, 0xa01f17cb); |
| 576 | |
| 577 | /* Set the PMC Register - to support PME in D0/D3hot/D3cold */ |
| 578 | ep_pcie_write_mask(dev->dm_core + PCIE20_CAP_ID_NXT_PTR, 0, |
| 579 | BIT(31)|BIT(30)|BIT(27)); |
| 580 | |
| 581 | /* Set the Endpoint L0s Acceptable Latency to 1us (max) */ |
| 582 | ep_pcie_write_reg_field(dev->dm_core, |
| 583 | PCIE20_DEVICE_CAPABILITIES, |
| 584 | PCIE20_MASK_EP_L0S_ACCPT_LATENCY, 0x7); |
| 585 | |
| 586 | /* Set the Endpoint L1 Acceptable Latency to 2 us (max) */ |
| 587 | ep_pcie_write_reg_field(dev->dm_core, |
| 588 | PCIE20_DEVICE_CAPABILITIES, |
| 589 | PCIE20_MASK_EP_L1_ACCPT_LATENCY, 0x7); |
| 590 | |
| 591 | /* Set the L0s Exit Latency to 2us-4us = 0x6 */ |
| 592 | ep_pcie_write_reg_field(dev->dm_core, PCIE20_LINK_CAPABILITIES, |
| 593 | PCIE20_MASK_L1_EXIT_LATENCY, 0x6); |
| 594 | |
| 595 | /* Set the L1 Exit Latency to be 32us-64 us = 0x6 */ |
| 596 | ep_pcie_write_reg_field(dev->dm_core, PCIE20_LINK_CAPABILITIES, |
| 597 | PCIE20_MASK_L0S_EXIT_LATENCY, 0x6); |
| 598 | |
| 599 | /* L1ss is supported */ |
| 600 | ep_pcie_write_mask(dev->dm_core + PCIE20_L1SUB_CAPABILITY, 0, |
| 601 | 0x1f); |
| 602 | |
| 603 | /* Enable Clock Power Management */ |
| 604 | ep_pcie_write_reg_field(dev->dm_core, PCIE20_LINK_CAPABILITIES, |
| 605 | PCIE20_MASK_CLOCK_POWER_MAN, 0x1); |
| 606 | |
| 607 | /* Disable CS for RO(CS) register writes */ |
| 608 | ep_pcie_write_mask(dev->dm_core + PCIE20_MISC_CONTROL_1, BIT(0), |
| 609 | 0); |
| 610 | |
| 611 | /* Set FTS value to match the PHY setting */ |
| 612 | ep_pcie_write_reg_field(dev->dm_core, |
| 613 | PCIE20_ACK_F_ASPM_CTRL_REG, |
| 614 | PCIE20_MASK_ACK_N_FTS, 0x80); |
| 615 | |
| 616 | EP_PCIE_DBG(dev, |
| 617 | "After program: CLASS_CODE_REVISION_ID:0x%x; HDR_TYPE:0x%x; L1SUB_CAPABILITY:0x%x; PARF_SYS_CTRL:0x%x\n", |
| 618 | readl_relaxed(dev->dm_core + |
| 619 | PCIE20_CLASS_CODE_REVISION_ID), |
| 620 | readl_relaxed(dev->dm_core + PCIE20_BIST_HDR_TYPE), |
| 621 | readl_relaxed(dev->dm_core + PCIE20_L1SUB_CAPABILITY), |
| 622 | readl_relaxed(dev->parf + PCIE20_PARF_SYS_CTRL)); |
| 623 | |
| 624 | /* Configure BARs */ |
| 625 | ep_pcie_bar_init(dev); |
| 626 | |
| 627 | ep_pcie_write_reg(dev->mmio, PCIE20_MHICFG, 0x02800880); |
| 628 | ep_pcie_write_reg(dev->mmio, PCIE20_BHI_EXECENV, 0x2); |
| 629 | } |
| 630 | |
| 631 | /* Configure IRQ events */ |
| 632 | if (dev->aggregated_irq) { |
| 633 | ep_pcie_write_reg(dev->parf, PCIE20_PARF_INT_ALL_MASK, 0); |
| 634 | ep_pcie_write_mask(dev->parf + PCIE20_PARF_INT_ALL_MASK, 0, |
| 635 | BIT(EP_PCIE_INT_EVT_LINK_DOWN) | |
| 636 | BIT(EP_PCIE_INT_EVT_BME) | |
| 637 | BIT(EP_PCIE_INT_EVT_PM_TURNOFF) | |
| 638 | BIT(EP_PCIE_INT_EVT_DSTATE_CHANGE) | |
| 639 | BIT(EP_PCIE_INT_EVT_LINK_UP)); |
| 640 | if (!dev->mhi_a7_irq) |
| 641 | ep_pcie_write_mask(dev->parf + |
| 642 | PCIE20_PARF_INT_ALL_MASK, 0, |
| 643 | BIT(EP_PCIE_INT_EVT_MHI_A7)); |
| 644 | |
| 645 | EP_PCIE_DBG(dev, "PCIe V%d: PCIE20_PARF_INT_ALL_MASK:0x%x\n", |
| 646 | dev->rev, |
| 647 | readl_relaxed(dev->parf + PCIE20_PARF_INT_ALL_MASK)); |
| 648 | } |
| 649 | |
| 650 | if (dev->active_config) { |
| 651 | ep_pcie_write_reg(dev->dm_core, PCIE20_AUX_CLK_FREQ_REG, 0x14); |
| 652 | |
| 653 | EP_PCIE_DBG2(dev, "PCIe V%d: Enable L1.\n", dev->rev); |
| 654 | ep_pcie_write_mask(dev->parf + PCIE20_PARF_PM_CTRL, BIT(5), 0); |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | static void ep_pcie_config_inbound_iatu(struct ep_pcie_dev_t *dev) |
| 659 | { |
| 660 | struct resource *mmio = dev->res[EP_PCIE_RES_MMIO].resource; |
| 661 | u32 lower, limit, bar; |
| 662 | |
| 663 | lower = mmio->start; |
| 664 | limit = mmio->end; |
| 665 | bar = readl_relaxed(dev->dm_core + PCIE20_BAR0); |
| 666 | |
| 667 | EP_PCIE_DBG(dev, |
| 668 | "PCIe V%d: BAR0 is 0x%x; MMIO[0x%x-0x%x]\n", |
| 669 | dev->rev, bar, lower, limit); |
| 670 | |
| 671 | ep_pcie_write_reg(dev->parf, PCIE20_PARF_MHI_BASE_ADDR_LOWER, lower); |
| 672 | ep_pcie_write_reg(dev->parf, PCIE20_PARF_MHI_BASE_ADDR_UPPER, 0x0); |
| 673 | |
| 674 | /* program inbound address translation using region 0 */ |
| 675 | ep_pcie_write_reg(dev->dm_core, PCIE20_PLR_IATU_VIEWPORT, 0x80000000); |
| 676 | /* set region to mem type */ |
| 677 | ep_pcie_write_reg(dev->dm_core, PCIE20_PLR_IATU_CTRL1, 0x0); |
| 678 | /* setup target address registers */ |
| 679 | ep_pcie_write_reg(dev->dm_core, PCIE20_PLR_IATU_LTAR, lower); |
| 680 | ep_pcie_write_reg(dev->dm_core, PCIE20_PLR_IATU_UTAR, 0x0); |
| 681 | /* use BAR match mode for BAR0 and enable region 0 */ |
| 682 | ep_pcie_write_reg(dev->dm_core, PCIE20_PLR_IATU_CTRL2, 0xc0000000); |
| 683 | |
| 684 | EP_PCIE_DBG(dev, "PCIE20_PLR_IATU_VIEWPORT:0x%x\n", |
| 685 | readl_relaxed(dev->dm_core + PCIE20_PLR_IATU_VIEWPORT)); |
| 686 | EP_PCIE_DBG(dev, "PCIE20_PLR_IATU_CTRL1:0x%x\n", |
| 687 | readl_relaxed(dev->dm_core + PCIE20_PLR_IATU_CTRL1)); |
| 688 | EP_PCIE_DBG(dev, "PCIE20_PLR_IATU_LTAR:0x%x\n", |
| 689 | readl_relaxed(dev->dm_core + PCIE20_PLR_IATU_LTAR)); |
| 690 | EP_PCIE_DBG(dev, "PCIE20_PLR_IATU_UTAR:0x%x\n", |
| 691 | readl_relaxed(dev->dm_core + PCIE20_PLR_IATU_UTAR)); |
| 692 | EP_PCIE_DBG(dev, "PCIE20_PLR_IATU_CTRL2:0x%x\n", |
| 693 | readl_relaxed(dev->dm_core + PCIE20_PLR_IATU_CTRL2)); |
| 694 | } |
| 695 | |
| 696 | static void ep_pcie_config_outbound_iatu_entry(struct ep_pcie_dev_t *dev, |
| 697 | u32 region, u32 lower, u32 upper, |
| 698 | u32 limit, u32 tgt_lower, u32 tgt_upper) |
| 699 | { |
| 700 | EP_PCIE_DBG(dev, |
| 701 | "PCIe V%d: region:%d; lower:0x%x; limit:0x%x; target_lower:0x%x; target_upper:0x%x\n", |
| 702 | dev->rev, region, lower, limit, tgt_lower, tgt_upper); |
| 703 | |
| 704 | /* program outbound address translation using an input region */ |
| 705 | ep_pcie_write_reg(dev->dm_core, PCIE20_PLR_IATU_VIEWPORT, region); |
| 706 | /* set region to mem type */ |
| 707 | ep_pcie_write_reg(dev->dm_core, PCIE20_PLR_IATU_CTRL1, 0x0); |
| 708 | /* setup source address registers */ |
| 709 | ep_pcie_write_reg(dev->dm_core, PCIE20_PLR_IATU_LBAR, lower); |
| 710 | ep_pcie_write_reg(dev->dm_core, PCIE20_PLR_IATU_UBAR, upper); |
| 711 | ep_pcie_write_reg(dev->dm_core, PCIE20_PLR_IATU_LAR, limit); |
| 712 | /* setup target address registers */ |
| 713 | ep_pcie_write_reg(dev->dm_core, PCIE20_PLR_IATU_LTAR, tgt_lower); |
| 714 | ep_pcie_write_reg(dev->dm_core, PCIE20_PLR_IATU_UTAR, tgt_upper); |
| 715 | /* use DMA bypass mode and enable the region */ |
| 716 | ep_pcie_write_mask(dev->dm_core + PCIE20_PLR_IATU_CTRL2, 0, |
| 717 | BIT(31) | BIT(27)); |
| 718 | |
| 719 | EP_PCIE_DBG(dev, "PCIE20_PLR_IATU_VIEWPORT:0x%x\n", |
| 720 | readl_relaxed(dev->dm_core + PCIE20_PLR_IATU_VIEWPORT)); |
| 721 | EP_PCIE_DBG(dev, "PCIE20_PLR_IATU_CTRL1:0x%x\n", |
| 722 | readl_relaxed(dev->dm_core + PCIE20_PLR_IATU_CTRL1)); |
| 723 | EP_PCIE_DBG(dev, "PCIE20_PLR_IATU_LBAR:0x%x\n", |
| 724 | readl_relaxed(dev->dm_core + PCIE20_PLR_IATU_LBAR)); |
| 725 | EP_PCIE_DBG(dev, "PCIE20_PLR_IATU_UBAR:0x%x\n", |
| 726 | readl_relaxed(dev->dm_core + PCIE20_PLR_IATU_UBAR)); |
| 727 | EP_PCIE_DBG(dev, "PCIE20_PLR_IATU_LAR:0x%x\n", |
| 728 | readl_relaxed(dev->dm_core + PCIE20_PLR_IATU_LAR)); |
| 729 | EP_PCIE_DBG(dev, "PCIE20_PLR_IATU_LTAR:0x%x\n", |
| 730 | readl_relaxed(dev->dm_core + PCIE20_PLR_IATU_LTAR)); |
| 731 | EP_PCIE_DBG(dev, "PCIE20_PLR_IATU_UTAR:0x%x\n", |
| 732 | readl_relaxed(dev->dm_core + PCIE20_PLR_IATU_UTAR)); |
| 733 | EP_PCIE_DBG(dev, "PCIE20_PLR_IATU_CTRL2:0x%x\n", |
| 734 | readl_relaxed(dev->dm_core + PCIE20_PLR_IATU_CTRL2)); |
| 735 | } |
| 736 | |
| 737 | static void ep_pcie_notify_event(struct ep_pcie_dev_t *dev, |
| 738 | enum ep_pcie_event event) |
| 739 | { |
| 740 | if (dev->event_reg && dev->event_reg->callback && |
| 741 | (dev->event_reg->events & event)) { |
| 742 | struct ep_pcie_notify *notify = &dev->event_reg->notify; |
| 743 | |
| 744 | notify->event = event; |
| 745 | notify->user = dev->event_reg->user; |
| 746 | EP_PCIE_DBG(&ep_pcie_dev, |
| 747 | "PCIe V%d: Callback client for event %d.\n", |
| 748 | dev->rev, event); |
| 749 | dev->event_reg->callback(notify); |
| 750 | } else { |
| 751 | EP_PCIE_DBG(&ep_pcie_dev, |
| 752 | "PCIe V%d: Client does not register for event %d.\n", |
| 753 | dev->rev, event); |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | static int ep_pcie_get_resources(struct ep_pcie_dev_t *dev, |
| 758 | struct platform_device *pdev) |
| 759 | { |
| 760 | int i, len, cnt, ret = 0, size = 0; |
| 761 | struct ep_pcie_vreg_info_t *vreg_info; |
| 762 | struct ep_pcie_gpio_info_t *gpio_info; |
| 763 | struct ep_pcie_clk_info_t *clk_info; |
| 764 | struct ep_pcie_reset_info_t *reset_info; |
| 765 | struct resource *res; |
| 766 | struct ep_pcie_res_info_t *res_info; |
| 767 | struct ep_pcie_irq_info_t *irq_info; |
| 768 | char prop_name[MAX_PROP_SIZE]; |
| 769 | const __be32 *prop; |
| 770 | u32 *clkfreq = NULL; |
| 771 | |
| 772 | EP_PCIE_DBG(dev, "PCIe V%d\n", dev->rev); |
| 773 | |
| 774 | of_get_property(pdev->dev.of_node, "qcom,phy-init", &size); |
| 775 | if (size) { |
| 776 | dev->phy_init = (struct ep_pcie_phy_info_t *) |
| 777 | devm_kzalloc(&pdev->dev, size, GFP_KERNEL); |
| 778 | |
| 779 | if (dev->phy_init) { |
| 780 | dev->phy_init_len = |
| 781 | size / sizeof(*dev->phy_init); |
| 782 | EP_PCIE_DBG(dev, |
| 783 | "PCIe V%d: phy init length is 0x%x.\n", |
| 784 | dev->rev, dev->phy_init_len); |
| 785 | |
| 786 | of_property_read_u32_array(pdev->dev.of_node, |
| 787 | "qcom,phy-init", |
| 788 | (unsigned int *)dev->phy_init, |
| 789 | size / sizeof(dev->phy_init->offset)); |
| 790 | } else { |
| 791 | EP_PCIE_ERR(dev, |
| 792 | "PCIe V%d: Could not allocate memory for phy init sequence.\n", |
| 793 | dev->rev); |
| 794 | return -ENOMEM; |
| 795 | } |
| 796 | } else { |
| 797 | EP_PCIE_DBG(dev, |
| 798 | "PCIe V%d: PHY V%d: phy init sequence is not present in DT.\n", |
| 799 | dev->rev, dev->phy_rev); |
| 800 | } |
| 801 | |
| 802 | cnt = of_property_count_strings((&pdev->dev)->of_node, |
| 803 | "clock-names"); |
| 804 | if (cnt > 0) { |
| 805 | size_t size = cnt * sizeof(*clkfreq); |
| 806 | |
| 807 | clkfreq = kzalloc(size, GFP_KERNEL); |
| 808 | if (!clkfreq) { |
| 809 | EP_PCIE_ERR(dev, "PCIe V%d: memory alloc failed\n", |
| 810 | dev->rev); |
| 811 | return -ENOMEM; |
| 812 | } |
| 813 | ret = of_property_read_u32_array( |
| 814 | (&pdev->dev)->of_node, |
| 815 | "max-clock-frequency-hz", clkfreq, cnt); |
| 816 | if (ret) { |
| 817 | EP_PCIE_ERR(dev, |
| 818 | "PCIe V%d: invalid max-clock-frequency-hz property:%d\n", |
| 819 | dev->rev, ret); |
| 820 | goto out; |
| 821 | } |
| 822 | } |
| 823 | |
| 824 | for (i = 0; i < EP_PCIE_MAX_VREG; i++) { |
| 825 | vreg_info = &dev->vreg[i]; |
| 826 | vreg_info->hdl = |
| 827 | devm_regulator_get(&pdev->dev, vreg_info->name); |
| 828 | |
| 829 | if (PTR_ERR(vreg_info->hdl) == -EPROBE_DEFER) { |
| 830 | EP_PCIE_DBG(dev, "EPROBE_DEFER for VReg:%s\n", |
| 831 | vreg_info->name); |
| 832 | ret = PTR_ERR(vreg_info->hdl); |
| 833 | goto out; |
| 834 | } |
| 835 | |
| 836 | if (IS_ERR(vreg_info->hdl)) { |
| 837 | if (vreg_info->required) { |
| 838 | EP_PCIE_ERR(dev, "Vreg %s doesn't exist\n", |
| 839 | vreg_info->name); |
| 840 | ret = PTR_ERR(vreg_info->hdl); |
| 841 | goto out; |
| 842 | } else { |
| 843 | EP_PCIE_DBG(dev, |
| 844 | "Optional Vreg %s doesn't exist\n", |
| 845 | vreg_info->name); |
| 846 | vreg_info->hdl = NULL; |
| 847 | } |
| 848 | } else { |
| 849 | snprintf(prop_name, MAX_PROP_SIZE, |
| 850 | "qcom,%s-voltage-level", vreg_info->name); |
| 851 | prop = of_get_property((&pdev->dev)->of_node, |
| 852 | prop_name, &len); |
| 853 | if (!prop || (len != (3 * sizeof(__be32)))) { |
| 854 | EP_PCIE_DBG(dev, "%s %s property\n", |
| 855 | prop ? "invalid format" : |
| 856 | "no", prop_name); |
| 857 | } else { |
| 858 | vreg_info->max_v = be32_to_cpup(&prop[0]); |
| 859 | vreg_info->min_v = be32_to_cpup(&prop[1]); |
| 860 | vreg_info->opt_mode = |
| 861 | be32_to_cpup(&prop[2]); |
| 862 | } |
| 863 | } |
| 864 | } |
| 865 | |
| 866 | dev->gdsc = devm_regulator_get(&pdev->dev, "gdsc-vdd"); |
| 867 | |
| 868 | if (IS_ERR(dev->gdsc)) { |
| 869 | EP_PCIE_ERR(dev, "PCIe V%d: Failed to get %s GDSC:%ld\n", |
| 870 | dev->rev, dev->pdev->name, PTR_ERR(dev->gdsc)); |
| 871 | if (PTR_ERR(dev->gdsc) == -EPROBE_DEFER) |
| 872 | EP_PCIE_DBG(dev, "PCIe V%d: EPROBE_DEFER for %s GDSC\n", |
| 873 | dev->rev, dev->pdev->name); |
| 874 | ret = PTR_ERR(dev->gdsc); |
| 875 | goto out; |
| 876 | } |
| 877 | |
| 878 | for (i = 0; i < EP_PCIE_MAX_GPIO; i++) { |
| 879 | gpio_info = &dev->gpio[i]; |
| 880 | ret = of_get_named_gpio((&pdev->dev)->of_node, |
| 881 | gpio_info->name, 0); |
| 882 | if (ret >= 0) { |
| 883 | gpio_info->num = ret; |
| 884 | ret = 0; |
| 885 | EP_PCIE_DBG(dev, "GPIO num for %s is %d\n", |
| 886 | gpio_info->name, gpio_info->num); |
| 887 | } else { |
| 888 | EP_PCIE_DBG(dev, |
| 889 | "GPIO %s is not supported in this configuration.\n", |
| 890 | gpio_info->name); |
| 891 | ret = 0; |
| 892 | } |
| 893 | } |
| 894 | |
| 895 | for (i = 0; i < EP_PCIE_MAX_CLK; i++) { |
| 896 | clk_info = &dev->clk[i]; |
| 897 | |
| 898 | clk_info->hdl = devm_clk_get(&pdev->dev, clk_info->name); |
| 899 | |
| 900 | if (IS_ERR(clk_info->hdl)) { |
| 901 | if (clk_info->required) { |
| 902 | EP_PCIE_ERR(dev, |
| 903 | "Clock %s isn't available:%ld\n", |
| 904 | clk_info->name, PTR_ERR(clk_info->hdl)); |
| 905 | ret = PTR_ERR(clk_info->hdl); |
| 906 | goto out; |
| 907 | } else { |
| 908 | EP_PCIE_DBG(dev, "Ignoring Clock %s\n", |
| 909 | clk_info->name); |
| 910 | clk_info->hdl = NULL; |
| 911 | } |
| 912 | } else { |
| 913 | if (clkfreq != NULL) { |
| 914 | clk_info->freq = clkfreq[i + |
| 915 | EP_PCIE_MAX_PIPE_CLK]; |
| 916 | EP_PCIE_DBG(dev, "Freq of Clock %s is:%d\n", |
| 917 | clk_info->name, clk_info->freq); |
| 918 | } |
| 919 | } |
| 920 | } |
| 921 | |
| 922 | for (i = 0; i < EP_PCIE_MAX_PIPE_CLK; i++) { |
| 923 | clk_info = &dev->pipeclk[i]; |
| 924 | |
| 925 | clk_info->hdl = devm_clk_get(&pdev->dev, clk_info->name); |
| 926 | |
| 927 | if (IS_ERR(clk_info->hdl)) { |
| 928 | if (clk_info->required) { |
| 929 | EP_PCIE_ERR(dev, |
| 930 | "Clock %s isn't available:%ld\n", |
| 931 | clk_info->name, PTR_ERR(clk_info->hdl)); |
| 932 | ret = PTR_ERR(clk_info->hdl); |
| 933 | goto out; |
| 934 | } else { |
| 935 | EP_PCIE_DBG(dev, "Ignoring Clock %s\n", |
| 936 | clk_info->name); |
| 937 | clk_info->hdl = NULL; |
| 938 | } |
| 939 | } else { |
| 940 | if (clkfreq != NULL) { |
| 941 | clk_info->freq = clkfreq[i]; |
| 942 | EP_PCIE_DBG(dev, "Freq of Clock %s is:%d\n", |
| 943 | clk_info->name, clk_info->freq); |
| 944 | } |
| 945 | } |
| 946 | } |
| 947 | |
| 948 | for (i = 0; i < EP_PCIE_MAX_RESET; i++) { |
| 949 | reset_info = &dev->reset[i]; |
| 950 | |
| 951 | reset_info->hdl = devm_reset_control_get(&pdev->dev, |
| 952 | reset_info->name); |
| 953 | |
| 954 | if (IS_ERR(reset_info->hdl)) { |
| 955 | if (reset_info->required) { |
| 956 | EP_PCIE_ERR(dev, |
| 957 | "Reset %s isn't available:%ld\n", |
| 958 | reset_info->name, |
| 959 | PTR_ERR(reset_info->hdl)); |
| 960 | |
| 961 | ret = PTR_ERR(reset_info->hdl); |
| 962 | reset_info->hdl = NULL; |
| 963 | goto out; |
| 964 | } else { |
| 965 | EP_PCIE_DBG(dev, "Ignoring Reset %s\n", |
| 966 | reset_info->name); |
| 967 | reset_info->hdl = NULL; |
| 968 | } |
| 969 | } |
| 970 | } |
| 971 | |
| 972 | dev->bus_scale_table = msm_bus_cl_get_pdata(pdev); |
| 973 | if (!dev->bus_scale_table) { |
| 974 | EP_PCIE_DBG(dev, "PCIe V%d: No bus scale table for %s\n", |
| 975 | dev->rev, dev->pdev->name); |
| 976 | dev->bus_client = 0; |
| 977 | } else { |
| 978 | dev->bus_client = |
| 979 | msm_bus_scale_register_client(dev->bus_scale_table); |
| 980 | if (!dev->bus_client) { |
| 981 | EP_PCIE_ERR(dev, |
| 982 | "PCIe V%d: Failed to register bus client for %s\n", |
| 983 | dev->rev, dev->pdev->name); |
| 984 | msm_bus_cl_clear_pdata(dev->bus_scale_table); |
| 985 | ret = -ENODEV; |
| 986 | goto out; |
| 987 | } |
| 988 | } |
| 989 | |
| 990 | for (i = 0; i < EP_PCIE_MAX_RES; i++) { |
| 991 | res_info = &dev->res[i]; |
| 992 | |
| 993 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, |
| 994 | res_info->name); |
| 995 | |
| 996 | if (!res) { |
| 997 | EP_PCIE_ERR(dev, |
| 998 | "PCIe V%d: can't get resource for %s.\n", |
| 999 | dev->rev, res_info->name); |
| 1000 | ret = -ENOMEM; |
| 1001 | goto out; |
| 1002 | } else { |
| 1003 | EP_PCIE_DBG(dev, "start addr for %s is %pa.\n", |
| 1004 | res_info->name, &res->start); |
| 1005 | } |
| 1006 | |
| 1007 | res_info->base = devm_ioremap(&pdev->dev, |
| 1008 | res->start, resource_size(res)); |
| 1009 | if (!res_info->base) { |
| 1010 | EP_PCIE_ERR(dev, "PCIe V%d: can't remap %s.\n", |
| 1011 | dev->rev, res_info->name); |
| 1012 | ret = -ENOMEM; |
| 1013 | goto out; |
| 1014 | } |
| 1015 | res_info->resource = res; |
| 1016 | } |
| 1017 | |
| 1018 | for (i = 0; i < EP_PCIE_MAX_IRQ; i++) { |
| 1019 | irq_info = &dev->irq[i]; |
| 1020 | |
| 1021 | res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, |
| 1022 | irq_info->name); |
| 1023 | |
| 1024 | if (!res) { |
| 1025 | EP_PCIE_DBG2(dev, "PCIe V%d: can't find IRQ # for %s\n", |
| 1026 | dev->rev, irq_info->name); |
| 1027 | } else { |
| 1028 | irq_info->num = res->start; |
| 1029 | EP_PCIE_DBG2(dev, "IRQ # for %s is %d.\n", |
| 1030 | irq_info->name, irq_info->num); |
| 1031 | } |
| 1032 | } |
| 1033 | |
| 1034 | dev->parf = dev->res[EP_PCIE_RES_PARF].base; |
| 1035 | dev->phy = dev->res[EP_PCIE_RES_PHY].base; |
| 1036 | dev->mmio = dev->res[EP_PCIE_RES_MMIO].base; |
| 1037 | dev->msi = dev->res[EP_PCIE_RES_MSI].base; |
| 1038 | dev->dm_core = dev->res[EP_PCIE_RES_DM_CORE].base; |
| 1039 | dev->elbi = dev->res[EP_PCIE_RES_ELBI].base; |
| 1040 | |
| 1041 | out: |
| 1042 | kfree(clkfreq); |
| 1043 | return ret; |
| 1044 | } |
| 1045 | |
| 1046 | static void ep_pcie_release_resources(struct ep_pcie_dev_t *dev) |
| 1047 | { |
| 1048 | dev->parf = NULL; |
| 1049 | dev->elbi = NULL; |
| 1050 | dev->dm_core = NULL; |
| 1051 | dev->phy = NULL; |
| 1052 | dev->mmio = NULL; |
| 1053 | dev->msi = NULL; |
| 1054 | |
| 1055 | if (dev->bus_client) { |
| 1056 | msm_bus_scale_unregister_client(dev->bus_client); |
| 1057 | dev->bus_client = 0; |
| 1058 | } |
| 1059 | } |
| 1060 | |
| 1061 | static void ep_pcie_enumeration_complete(struct ep_pcie_dev_t *dev) |
| 1062 | { |
Yan He | 0816989 | 2017-10-27 17:40:24 -0700 | [diff] [blame] | 1063 | unsigned long irqsave_flags; |
| 1064 | |
| 1065 | spin_lock_irqsave(&dev->isr_lock, irqsave_flags); |
| 1066 | |
Yan He | 50222ad | 2017-11-17 18:22:17 -0800 | [diff] [blame] | 1067 | dev->enumerated = true; |
| 1068 | dev->link_status = EP_PCIE_LINK_ENABLED; |
| 1069 | |
| 1070 | if (dev->gpio[EP_PCIE_GPIO_MDM2AP].num) { |
| 1071 | /* assert MDM2AP Status GPIO */ |
| 1072 | EP_PCIE_DBG2(dev, "PCIe V%d: assert MDM2AP Status.\n", |
| 1073 | dev->rev); |
| 1074 | EP_PCIE_DBG(dev, |
| 1075 | "PCIe V%d: MDM2APStatus GPIO initial:%d.\n", |
| 1076 | dev->rev, |
| 1077 | gpio_get_value( |
| 1078 | dev->gpio[EP_PCIE_GPIO_MDM2AP].num)); |
| 1079 | gpio_set_value(dev->gpio[EP_PCIE_GPIO_MDM2AP].num, |
| 1080 | dev->gpio[EP_PCIE_GPIO_MDM2AP].on); |
| 1081 | EP_PCIE_DBG(dev, |
| 1082 | "PCIe V%d: MDM2APStatus GPIO after assertion:%d.\n", |
| 1083 | dev->rev, |
| 1084 | gpio_get_value( |
| 1085 | dev->gpio[EP_PCIE_GPIO_MDM2AP].num)); |
| 1086 | } |
| 1087 | |
| 1088 | hw_drv.device_id = readl_relaxed(dev->dm_core); |
| 1089 | EP_PCIE_DBG(&ep_pcie_dev, |
| 1090 | "PCIe V%d: register driver for device 0x%x.\n", |
| 1091 | ep_pcie_dev.rev, hw_drv.device_id); |
| 1092 | ep_pcie_register_drv(&hw_drv); |
Yan He | 0816989 | 2017-10-27 17:40:24 -0700 | [diff] [blame] | 1093 | if (!dev->no_notify) |
| 1094 | ep_pcie_notify_event(dev, EP_PCIE_EVENT_LINKUP); |
| 1095 | else |
| 1096 | EP_PCIE_DBG(dev, |
| 1097 | "PCIe V%d: do not notify client about linkup.\n", |
| 1098 | dev->rev); |
| 1099 | |
| 1100 | spin_unlock_irqrestore(&dev->isr_lock, irqsave_flags); |
Yan He | 50222ad | 2017-11-17 18:22:17 -0800 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | int ep_pcie_core_enable_endpoint(enum ep_pcie_options opt) |
| 1104 | { |
| 1105 | int ret = 0; |
| 1106 | u32 val = 0; |
| 1107 | u32 retries = 0; |
| 1108 | u32 bme = 0; |
| 1109 | bool ltssm_en = false; |
| 1110 | struct ep_pcie_dev_t *dev = &ep_pcie_dev; |
| 1111 | |
| 1112 | EP_PCIE_DBG(dev, "PCIe V%d: options input are 0x%x.\n", dev->rev, opt); |
| 1113 | |
| 1114 | mutex_lock(&dev->setup_mtx); |
| 1115 | |
| 1116 | if (dev->link_status == EP_PCIE_LINK_ENABLED) { |
| 1117 | EP_PCIE_ERR(dev, |
| 1118 | "PCIe V%d: link is already enabled.\n", |
| 1119 | dev->rev); |
| 1120 | goto out; |
| 1121 | } |
| 1122 | |
| 1123 | if (dev->link_status == EP_PCIE_LINK_UP) |
| 1124 | EP_PCIE_DBG(dev, |
| 1125 | "PCIe V%d: link is already up, let's proceed with the voting for the resources.\n", |
| 1126 | dev->rev); |
| 1127 | |
| 1128 | if (dev->power_on && (opt & EP_PCIE_OPT_POWER_ON)) { |
| 1129 | EP_PCIE_ERR(dev, |
| 1130 | "PCIe V%d: request to turn on the power when link is already powered on.\n", |
| 1131 | dev->rev); |
| 1132 | goto out; |
| 1133 | } |
| 1134 | |
| 1135 | if (opt & EP_PCIE_OPT_POWER_ON) { |
| 1136 | /* enable power */ |
| 1137 | ret = ep_pcie_vreg_init(dev); |
| 1138 | if (ret) { |
| 1139 | EP_PCIE_ERR(dev, "PCIe V%d: failed to enable Vreg\n", |
| 1140 | dev->rev); |
| 1141 | goto out; |
| 1142 | } |
| 1143 | |
| 1144 | /* enable clocks */ |
| 1145 | ret = ep_pcie_clk_init(dev); |
| 1146 | if (ret) { |
| 1147 | EP_PCIE_ERR(dev, "PCIe V%d: failed to enable clocks\n", |
| 1148 | dev->rev); |
| 1149 | goto clk_fail; |
| 1150 | } |
| 1151 | |
| 1152 | /* enable pipe clock */ |
| 1153 | ret = ep_pcie_pipe_clk_init(dev); |
| 1154 | if (ret) { |
| 1155 | EP_PCIE_ERR(dev, |
| 1156 | "PCIe V%d: failed to enable pipe clock\n", |
| 1157 | dev->rev); |
| 1158 | goto pipe_clk_fail; |
| 1159 | } |
| 1160 | |
| 1161 | dev->power_on = true; |
| 1162 | } |
| 1163 | |
| 1164 | if (!(opt & EP_PCIE_OPT_ENUM)) |
| 1165 | goto out; |
| 1166 | |
| 1167 | /* check link status during initial bootup */ |
| 1168 | if (!dev->enumerated) { |
| 1169 | val = readl_relaxed(dev->parf + PCIE20_PARF_PM_STTS); |
| 1170 | val = val & PARF_XMLH_LINK_UP; |
| 1171 | EP_PCIE_DBG(dev, "PCIe V%d: Link status is 0x%x.\n", dev->rev, |
| 1172 | val); |
| 1173 | if (val) { |
| 1174 | EP_PCIE_INFO(dev, |
| 1175 | "PCIe V%d: link initialized by bootloader for LE PCIe endpoint; skip link training in HLOS.\n", |
| 1176 | dev->rev); |
| 1177 | ep_pcie_core_init(dev, true); |
| 1178 | dev->link_status = EP_PCIE_LINK_UP; |
| 1179 | dev->l23_ready = false; |
| 1180 | goto checkbme; |
| 1181 | } else { |
| 1182 | ltssm_en = readl_relaxed(dev->parf |
| 1183 | + PCIE20_PARF_LTSSM) & BIT(8); |
| 1184 | |
| 1185 | if (ltssm_en) { |
| 1186 | EP_PCIE_ERR(dev, |
| 1187 | "PCIe V%d: link is not up when LTSSM has already enabled by bootloader.\n", |
| 1188 | dev->rev); |
| 1189 | ret = EP_PCIE_ERROR; |
| 1190 | goto link_fail; |
| 1191 | } else { |
| 1192 | EP_PCIE_DBG(dev, |
| 1193 | "PCIe V%d: Proceed with regular link training.\n", |
| 1194 | dev->rev); |
| 1195 | } |
| 1196 | } |
| 1197 | } |
| 1198 | |
| 1199 | if (opt & EP_PCIE_OPT_AST_WAKE) { |
| 1200 | /* assert PCIe WAKE# */ |
| 1201 | EP_PCIE_INFO(dev, "PCIe V%d: assert PCIe WAKE#.\n", |
| 1202 | dev->rev); |
| 1203 | EP_PCIE_DBG(dev, "PCIe V%d: WAKE GPIO initial:%d.\n", |
| 1204 | dev->rev, |
| 1205 | gpio_get_value(dev->gpio[EP_PCIE_GPIO_WAKE].num)); |
| 1206 | gpio_set_value(dev->gpio[EP_PCIE_GPIO_WAKE].num, |
| 1207 | 1 - dev->gpio[EP_PCIE_GPIO_WAKE].on); |
| 1208 | EP_PCIE_DBG(dev, |
| 1209 | "PCIe V%d: WAKE GPIO after deassertion:%d.\n", |
| 1210 | dev->rev, |
| 1211 | gpio_get_value(dev->gpio[EP_PCIE_GPIO_WAKE].num)); |
| 1212 | gpio_set_value(dev->gpio[EP_PCIE_GPIO_WAKE].num, |
| 1213 | dev->gpio[EP_PCIE_GPIO_WAKE].on); |
| 1214 | EP_PCIE_DBG(dev, |
| 1215 | "PCIe V%d: WAKE GPIO after assertion:%d.\n", |
| 1216 | dev->rev, |
| 1217 | gpio_get_value(dev->gpio[EP_PCIE_GPIO_WAKE].num)); |
| 1218 | } |
| 1219 | |
| 1220 | /* wait for host side to deassert PERST */ |
| 1221 | retries = 0; |
| 1222 | do { |
| 1223 | if (gpio_get_value(dev->gpio[EP_PCIE_GPIO_PERST].num) == 1) |
| 1224 | break; |
| 1225 | retries++; |
| 1226 | usleep_range(PERST_TIMEOUT_US_MIN, PERST_TIMEOUT_US_MAX); |
| 1227 | } while (retries < PERST_CHECK_MAX_COUNT); |
| 1228 | |
| 1229 | EP_PCIE_DBG(dev, "PCIe V%d: number of PERST retries:%d.\n", |
| 1230 | dev->rev, retries); |
| 1231 | |
| 1232 | if (retries == PERST_CHECK_MAX_COUNT) { |
| 1233 | EP_PCIE_ERR(dev, |
| 1234 | "PCIe V%d: PERST is not de-asserted by host\n", |
| 1235 | dev->rev); |
| 1236 | ret = EP_PCIE_ERROR; |
| 1237 | goto link_fail; |
| 1238 | } else { |
| 1239 | dev->perst_deast = true; |
| 1240 | if (opt & EP_PCIE_OPT_AST_WAKE) { |
| 1241 | /* deassert PCIe WAKE# */ |
| 1242 | EP_PCIE_DBG(dev, |
| 1243 | "PCIe V%d: deassert PCIe WAKE# after PERST# is deasserted.\n", |
| 1244 | dev->rev); |
| 1245 | gpio_set_value(dev->gpio[EP_PCIE_GPIO_WAKE].num, |
| 1246 | 1 - dev->gpio[EP_PCIE_GPIO_WAKE].on); |
| 1247 | } |
| 1248 | } |
| 1249 | |
| 1250 | /* init PCIe PHY */ |
| 1251 | ep_pcie_phy_init(dev); |
| 1252 | |
| 1253 | EP_PCIE_DBG(dev, "PCIe V%d: waiting for phy ready...\n", dev->rev); |
| 1254 | retries = 0; |
| 1255 | do { |
| 1256 | if (ep_pcie_phy_is_ready(dev)) |
| 1257 | break; |
| 1258 | retries++; |
| 1259 | if (retries % 100 == 0) |
| 1260 | EP_PCIE_DBG(dev, |
| 1261 | "PCIe V%d: current number of PHY retries:%d.\n", |
| 1262 | dev->rev, retries); |
| 1263 | usleep_range(REFCLK_STABILIZATION_DELAY_US_MIN, |
| 1264 | REFCLK_STABILIZATION_DELAY_US_MAX); |
| 1265 | } while (retries < PHY_READY_TIMEOUT_COUNT); |
| 1266 | |
| 1267 | EP_PCIE_DBG(dev, "PCIe V%d: number of PHY retries:%d.\n", |
| 1268 | dev->rev, retries); |
| 1269 | |
| 1270 | if (retries == PHY_READY_TIMEOUT_COUNT) { |
| 1271 | EP_PCIE_ERR(dev, "PCIe V%d: PCIe PHY failed to come up!\n", |
| 1272 | dev->rev); |
| 1273 | ret = EP_PCIE_ERROR; |
| 1274 | ep_pcie_reg_dump(dev, BIT(EP_PCIE_RES_PHY), false); |
| 1275 | goto link_fail; |
| 1276 | } else { |
| 1277 | EP_PCIE_INFO(dev, "PCIe V%d: PCIe PHY is ready!\n", dev->rev); |
| 1278 | } |
| 1279 | |
| 1280 | ep_pcie_core_init(dev, false); |
| 1281 | ep_pcie_config_inbound_iatu(dev); |
| 1282 | |
| 1283 | /* enable link training */ |
| 1284 | if (dev->phy_rev >= 3) |
| 1285 | ep_pcie_write_mask(dev->parf + PCIE20_PARF_LTSSM, 0, BIT(8)); |
| 1286 | else |
| 1287 | ep_pcie_write_mask(dev->elbi + PCIE20_ELBI_SYS_CTRL, 0, BIT(0)); |
| 1288 | |
| 1289 | EP_PCIE_DBG(dev, "PCIe V%d: check if link is up\n", dev->rev); |
| 1290 | |
| 1291 | /* Wait for up to 100ms for the link to come up */ |
| 1292 | retries = 0; |
| 1293 | do { |
| 1294 | usleep_range(LINK_UP_TIMEOUT_US_MIN, LINK_UP_TIMEOUT_US_MAX); |
| 1295 | val = readl_relaxed(dev->elbi + PCIE20_ELBI_SYS_STTS); |
| 1296 | retries++; |
| 1297 | if (retries % 100 == 0) |
| 1298 | EP_PCIE_DBG(dev, "PCIe V%d: LTSSM_STATE:0x%x.\n", |
| 1299 | dev->rev, (val >> 0xC) & 0x3f); |
| 1300 | } while ((!(val & XMLH_LINK_UP) || |
| 1301 | !ep_pcie_confirm_linkup(dev, false)) |
| 1302 | && (retries < LINK_UP_CHECK_MAX_COUNT)); |
| 1303 | |
| 1304 | if (retries == LINK_UP_CHECK_MAX_COUNT) { |
| 1305 | EP_PCIE_ERR(dev, "PCIe V%d: link initialization failed\n", |
| 1306 | dev->rev); |
| 1307 | ret = EP_PCIE_ERROR; |
| 1308 | goto link_fail; |
| 1309 | } else { |
| 1310 | dev->link_status = EP_PCIE_LINK_UP; |
| 1311 | dev->l23_ready = false; |
| 1312 | EP_PCIE_DBG(dev, |
| 1313 | "PCIe V%d: link is up after %d checkings (%d ms)\n", |
| 1314 | dev->rev, retries, |
| 1315 | LINK_UP_TIMEOUT_US_MIN * retries / 1000); |
| 1316 | EP_PCIE_INFO(dev, |
| 1317 | "PCIe V%d: link initialized for LE PCIe endpoint\n", |
| 1318 | dev->rev); |
| 1319 | } |
| 1320 | |
| 1321 | checkbme: |
| 1322 | if (dev->active_config) { |
| 1323 | ep_pcie_write_mask(dev->parf + PCIE20_PARF_SLV_ADDR_MSB_CTRL, |
| 1324 | 0, BIT(0)); |
| 1325 | ep_pcie_write_reg(dev->parf, PCIE20_PARF_SLV_ADDR_SPACE_SIZE_HI, |
| 1326 | 0x200); |
| 1327 | ep_pcie_write_reg(dev->parf, PCIE20_PARF_SLV_ADDR_SPACE_SIZE, |
| 1328 | 0x0); |
| 1329 | ep_pcie_write_reg(dev->parf, PCIE20_PARF_DBI_BASE_ADDR_HI, |
| 1330 | 0x100); |
| 1331 | ep_pcie_write_reg(dev->parf, PCIE20_PARF_DBI_BASE_ADDR, |
| 1332 | 0x7FFFE000); |
| 1333 | } |
| 1334 | |
| 1335 | if (!(opt & EP_PCIE_OPT_ENUM_ASYNC)) { |
| 1336 | /* Wait for up to 1000ms for BME to be set */ |
| 1337 | retries = 0; |
| 1338 | |
| 1339 | bme = readl_relaxed(dev->dm_core + |
| 1340 | PCIE20_COMMAND_STATUS) & BIT(2); |
| 1341 | while (!bme && (retries < BME_CHECK_MAX_COUNT)) { |
| 1342 | retries++; |
| 1343 | usleep_range(BME_TIMEOUT_US_MIN, BME_TIMEOUT_US_MAX); |
| 1344 | bme = readl_relaxed(dev->dm_core + |
| 1345 | PCIE20_COMMAND_STATUS) & BIT(2); |
| 1346 | } |
| 1347 | } else { |
| 1348 | EP_PCIE_DBG(dev, |
| 1349 | "PCIe V%d: EP_PCIE_OPT_ENUM_ASYNC is true.\n", |
| 1350 | dev->rev); |
| 1351 | bme = readl_relaxed(dev->dm_core + |
| 1352 | PCIE20_COMMAND_STATUS) & BIT(2); |
| 1353 | } |
| 1354 | |
| 1355 | if (bme) { |
| 1356 | EP_PCIE_DBG(dev, |
| 1357 | "PCIe V%d: PCIe link is up and BME is enabled after %d checkings (%d ms).\n", |
| 1358 | dev->rev, retries, |
| 1359 | BME_TIMEOUT_US_MIN * retries / 1000); |
| 1360 | ep_pcie_enumeration_complete(dev); |
| 1361 | /* expose BAR to user space to identify modem */ |
| 1362 | ep_pcie_bar0_address = |
| 1363 | readl_relaxed(dev->dm_core + PCIE20_BAR0); |
| 1364 | } else { |
| 1365 | if (!(opt & EP_PCIE_OPT_ENUM_ASYNC)) |
| 1366 | EP_PCIE_ERR(dev, |
| 1367 | "PCIe V%d: PCIe link is up but BME is still disabled after max waiting time.\n", |
| 1368 | dev->rev); |
| 1369 | if (!ep_pcie_debug_keep_resource && |
| 1370 | !(opt&EP_PCIE_OPT_ENUM_ASYNC)) { |
| 1371 | ret = EP_PCIE_ERROR; |
| 1372 | dev->link_status = EP_PCIE_LINK_DISABLED; |
| 1373 | goto link_fail; |
| 1374 | } |
| 1375 | } |
| 1376 | |
| 1377 | dev->suspending = false; |
| 1378 | goto out; |
| 1379 | |
| 1380 | link_fail: |
| 1381 | dev->power_on = false; |
| 1382 | if (!ep_pcie_debug_keep_resource) |
| 1383 | ep_pcie_pipe_clk_deinit(dev); |
| 1384 | pipe_clk_fail: |
| 1385 | if (!ep_pcie_debug_keep_resource) |
| 1386 | ep_pcie_clk_deinit(dev); |
| 1387 | clk_fail: |
| 1388 | if (!ep_pcie_debug_keep_resource) |
| 1389 | ep_pcie_vreg_deinit(dev); |
| 1390 | else |
| 1391 | ret = 0; |
| 1392 | out: |
| 1393 | mutex_unlock(&dev->setup_mtx); |
| 1394 | |
| 1395 | return ret; |
| 1396 | } |
| 1397 | |
| 1398 | int ep_pcie_core_disable_endpoint(void) |
| 1399 | { |
| 1400 | int rc = 0; |
| 1401 | struct ep_pcie_dev_t *dev = &ep_pcie_dev; |
| 1402 | |
| 1403 | EP_PCIE_DBG(dev, "PCIe V%d\n", dev->rev); |
| 1404 | |
| 1405 | mutex_lock(&dev->setup_mtx); |
| 1406 | |
| 1407 | if (!dev->power_on) { |
| 1408 | EP_PCIE_DBG(dev, |
| 1409 | "PCIe V%d: the link is already power down.\n", |
| 1410 | dev->rev); |
| 1411 | goto out; |
| 1412 | } |
| 1413 | |
| 1414 | dev->link_status = EP_PCIE_LINK_DISABLED; |
| 1415 | dev->power_on = false; |
| 1416 | |
| 1417 | EP_PCIE_DBG(dev, "PCIe V%d: shut down the link.\n", |
| 1418 | dev->rev); |
| 1419 | |
| 1420 | ep_pcie_pipe_clk_deinit(dev); |
| 1421 | ep_pcie_clk_deinit(dev); |
| 1422 | ep_pcie_vreg_deinit(dev); |
| 1423 | out: |
| 1424 | mutex_unlock(&dev->setup_mtx); |
| 1425 | return rc; |
| 1426 | } |
| 1427 | |
| 1428 | int ep_pcie_core_mask_irq_event(enum ep_pcie_irq_event event, |
| 1429 | bool enable) |
| 1430 | { |
| 1431 | int rc = 0; |
| 1432 | struct ep_pcie_dev_t *dev = &ep_pcie_dev; |
| 1433 | unsigned long irqsave_flags; |
| 1434 | u32 mask = 0; |
| 1435 | |
| 1436 | EP_PCIE_DUMP(dev, |
| 1437 | "PCIe V%d: Client askes to %s IRQ event 0x%x.\n", |
| 1438 | dev->rev, |
| 1439 | enable ? "enable" : "disable", |
| 1440 | event); |
| 1441 | |
| 1442 | spin_lock_irqsave(&dev->ext_lock, irqsave_flags); |
| 1443 | |
| 1444 | if (dev->aggregated_irq) { |
| 1445 | mask = readl_relaxed(dev->parf + PCIE20_PARF_INT_ALL_MASK); |
| 1446 | EP_PCIE_DUMP(dev, |
| 1447 | "PCIe V%d: current PCIE20_PARF_INT_ALL_MASK:0x%x\n", |
| 1448 | dev->rev, mask); |
| 1449 | if (enable) |
| 1450 | ep_pcie_write_mask(dev->parf + PCIE20_PARF_INT_ALL_MASK, |
| 1451 | 0, BIT(event)); |
| 1452 | else |
| 1453 | ep_pcie_write_mask(dev->parf + PCIE20_PARF_INT_ALL_MASK, |
| 1454 | BIT(event), 0); |
| 1455 | EP_PCIE_DUMP(dev, |
| 1456 | "PCIe V%d: new PCIE20_PARF_INT_ALL_MASK:0x%x\n", |
| 1457 | dev->rev, |
| 1458 | readl_relaxed(dev->parf + PCIE20_PARF_INT_ALL_MASK)); |
| 1459 | } else { |
| 1460 | EP_PCIE_ERR(dev, |
| 1461 | "PCIe V%d: Client askes to %s IRQ event 0x%x when aggregated IRQ is not supported.\n", |
| 1462 | dev->rev, |
| 1463 | enable ? "enable" : "disable", |
| 1464 | event); |
| 1465 | rc = EP_PCIE_ERROR; |
| 1466 | } |
| 1467 | |
| 1468 | spin_unlock_irqrestore(&dev->ext_lock, irqsave_flags); |
| 1469 | return rc; |
| 1470 | } |
| 1471 | |
| 1472 | static irqreturn_t ep_pcie_handle_bme_irq(int irq, void *data) |
| 1473 | { |
| 1474 | struct ep_pcie_dev_t *dev = data; |
| 1475 | unsigned long irqsave_flags; |
| 1476 | |
| 1477 | spin_lock_irqsave(&dev->isr_lock, irqsave_flags); |
| 1478 | |
| 1479 | dev->bme_counter++; |
| 1480 | EP_PCIE_DBG(dev, |
| 1481 | "PCIe V%d: No. %ld BME IRQ.\n", dev->rev, dev->bme_counter); |
| 1482 | |
| 1483 | if (readl_relaxed(dev->dm_core + PCIE20_COMMAND_STATUS) & BIT(2)) { |
| 1484 | /* BME has been enabled */ |
| 1485 | if (!dev->enumerated) { |
| 1486 | EP_PCIE_DBG(dev, |
| 1487 | "PCIe V%d:BME is set. Enumeration is complete\n", |
| 1488 | dev->rev); |
| 1489 | schedule_work(&dev->handle_bme_work); |
| 1490 | } else { |
| 1491 | EP_PCIE_DBG(dev, |
| 1492 | "PCIe V%d:BME is set again after the enumeration has completed; callback client for link ready.\n", |
| 1493 | dev->rev); |
| 1494 | ep_pcie_notify_event(dev, EP_PCIE_EVENT_LINKUP); |
| 1495 | } |
| 1496 | } else { |
| 1497 | EP_PCIE_DBG(dev, |
| 1498 | "PCIe V%d:BME is still disabled\n", dev->rev); |
| 1499 | } |
| 1500 | |
| 1501 | spin_unlock_irqrestore(&dev->isr_lock, irqsave_flags); |
| 1502 | return IRQ_HANDLED; |
| 1503 | } |
| 1504 | |
| 1505 | static irqreturn_t ep_pcie_handle_linkdown_irq(int irq, void *data) |
| 1506 | { |
| 1507 | struct ep_pcie_dev_t *dev = data; |
| 1508 | unsigned long irqsave_flags; |
| 1509 | |
| 1510 | spin_lock_irqsave(&dev->isr_lock, irqsave_flags); |
| 1511 | |
| 1512 | dev->linkdown_counter++; |
| 1513 | EP_PCIE_DBG(dev, |
| 1514 | "PCIe V%d: No. %ld linkdown IRQ.\n", |
| 1515 | dev->rev, dev->linkdown_counter); |
| 1516 | |
| 1517 | if (!dev->enumerated || dev->link_status == EP_PCIE_LINK_DISABLED) { |
| 1518 | EP_PCIE_DBG(dev, |
| 1519 | "PCIe V%d:Linkdown IRQ happened when the link is disabled.\n", |
| 1520 | dev->rev); |
| 1521 | } else if (dev->suspending) { |
| 1522 | EP_PCIE_DBG(dev, |
| 1523 | "PCIe V%d:Linkdown IRQ happened when the link is suspending.\n", |
| 1524 | dev->rev); |
| 1525 | } else { |
| 1526 | dev->link_status = EP_PCIE_LINK_DISABLED; |
| 1527 | EP_PCIE_ERR(dev, "PCIe V%d:PCIe link is down for %ld times\n", |
| 1528 | dev->rev, dev->linkdown_counter); |
| 1529 | ep_pcie_reg_dump(dev, BIT(EP_PCIE_RES_PHY) | |
| 1530 | BIT(EP_PCIE_RES_PARF), true); |
| 1531 | ep_pcie_notify_event(dev, EP_PCIE_EVENT_LINKDOWN); |
| 1532 | } |
| 1533 | |
| 1534 | spin_unlock_irqrestore(&dev->isr_lock, irqsave_flags); |
| 1535 | |
| 1536 | return IRQ_HANDLED; |
| 1537 | } |
| 1538 | |
| 1539 | static irqreturn_t ep_pcie_handle_linkup_irq(int irq, void *data) |
| 1540 | { |
| 1541 | struct ep_pcie_dev_t *dev = data; |
| 1542 | unsigned long irqsave_flags; |
| 1543 | |
| 1544 | spin_lock_irqsave(&dev->isr_lock, irqsave_flags); |
| 1545 | |
| 1546 | dev->linkup_counter++; |
| 1547 | EP_PCIE_DBG(dev, |
| 1548 | "PCIe V%d: No. %ld linkup IRQ.\n", |
| 1549 | dev->rev, dev->linkup_counter); |
| 1550 | |
| 1551 | dev->link_status = EP_PCIE_LINK_UP; |
| 1552 | |
| 1553 | spin_unlock_irqrestore(&dev->isr_lock, irqsave_flags); |
| 1554 | |
| 1555 | return IRQ_HANDLED; |
| 1556 | } |
| 1557 | |
| 1558 | static irqreturn_t ep_pcie_handle_pm_turnoff_irq(int irq, void *data) |
| 1559 | { |
| 1560 | struct ep_pcie_dev_t *dev = data; |
| 1561 | unsigned long irqsave_flags; |
| 1562 | |
| 1563 | spin_lock_irqsave(&dev->isr_lock, irqsave_flags); |
| 1564 | |
| 1565 | dev->pm_to_counter++; |
| 1566 | EP_PCIE_DBG2(dev, |
| 1567 | "PCIe V%d: No. %ld PM_TURNOFF is received.\n", |
| 1568 | dev->rev, dev->pm_to_counter); |
| 1569 | EP_PCIE_DBG2(dev, "PCIe V%d: Put the link into L23.\n", dev->rev); |
| 1570 | ep_pcie_write_mask(dev->parf + PCIE20_PARF_PM_CTRL, 0, BIT(2)); |
| 1571 | |
| 1572 | spin_unlock_irqrestore(&dev->isr_lock, irqsave_flags); |
| 1573 | |
| 1574 | return IRQ_HANDLED; |
| 1575 | } |
| 1576 | |
| 1577 | static irqreturn_t ep_pcie_handle_dstate_change_irq(int irq, void *data) |
| 1578 | { |
| 1579 | struct ep_pcie_dev_t *dev = data; |
| 1580 | unsigned long irqsave_flags; |
| 1581 | u32 dstate; |
| 1582 | |
| 1583 | spin_lock_irqsave(&dev->isr_lock, irqsave_flags); |
| 1584 | |
| 1585 | dstate = readl_relaxed(dev->dm_core + |
| 1586 | PCIE20_CON_STATUS) & 0x3; |
| 1587 | |
| 1588 | if (dev->dump_conf) |
| 1589 | ep_pcie_reg_dump(dev, BIT(EP_PCIE_RES_DM_CORE), false); |
| 1590 | |
| 1591 | if (dstate == 3) { |
| 1592 | dev->l23_ready = true; |
| 1593 | dev->d3_counter++; |
| 1594 | EP_PCIE_DBG(dev, |
| 1595 | "PCIe V%d: No. %ld change to D3 state.\n", |
| 1596 | dev->rev, dev->d3_counter); |
| 1597 | ep_pcie_write_mask(dev->parf + PCIE20_PARF_PM_CTRL, 0, BIT(1)); |
Yan He | 0816989 | 2017-10-27 17:40:24 -0700 | [diff] [blame] | 1598 | |
| 1599 | if (dev->enumerated) |
| 1600 | ep_pcie_notify_event(dev, EP_PCIE_EVENT_PM_D3_HOT); |
| 1601 | else |
| 1602 | EP_PCIE_DBG(dev, |
| 1603 | "PCIe V%d: do not notify client about this D3 hot event since enumeration by HLOS is not done yet.\n", |
| 1604 | dev->rev); |
Yan He | 50222ad | 2017-11-17 18:22:17 -0800 | [diff] [blame] | 1605 | } else if (dstate == 0) { |
| 1606 | dev->l23_ready = false; |
| 1607 | dev->d0_counter++; |
| 1608 | EP_PCIE_DBG(dev, |
| 1609 | "PCIe V%d: No. %ld change to D0 state.\n", |
| 1610 | dev->rev, dev->d0_counter); |
| 1611 | ep_pcie_notify_event(dev, EP_PCIE_EVENT_PM_D0); |
| 1612 | } else { |
| 1613 | EP_PCIE_ERR(dev, |
| 1614 | "PCIe V%d:invalid D state change to 0x%x.\n", |
| 1615 | dev->rev, dstate); |
| 1616 | } |
| 1617 | |
| 1618 | spin_unlock_irqrestore(&dev->isr_lock, irqsave_flags); |
| 1619 | |
| 1620 | return IRQ_HANDLED; |
| 1621 | } |
| 1622 | |
| 1623 | static int ep_pcie_enumeration(struct ep_pcie_dev_t *dev) |
| 1624 | { |
| 1625 | int ret = 0; |
| 1626 | |
| 1627 | if (!dev) { |
| 1628 | EP_PCIE_ERR(&ep_pcie_dev, |
| 1629 | "PCIe V%d: the input handler is NULL.\n", |
| 1630 | ep_pcie_dev.rev); |
| 1631 | return EP_PCIE_ERROR; |
| 1632 | } |
| 1633 | |
| 1634 | EP_PCIE_DBG(dev, |
| 1635 | "PCIe V%d: start PCIe link enumeration per host side.\n", |
| 1636 | dev->rev); |
| 1637 | |
| 1638 | ret = ep_pcie_core_enable_endpoint(EP_PCIE_OPT_ALL); |
| 1639 | |
| 1640 | if (ret) { |
| 1641 | EP_PCIE_ERR(&ep_pcie_dev, |
| 1642 | "PCIe V%d: PCIe link enumeration failed.\n", |
| 1643 | ep_pcie_dev.rev); |
| 1644 | } else { |
| 1645 | if (dev->link_status == EP_PCIE_LINK_ENABLED) { |
| 1646 | EP_PCIE_INFO(&ep_pcie_dev, |
| 1647 | "PCIe V%d: PCIe link enumeration is successful with host side.\n", |
| 1648 | ep_pcie_dev.rev); |
| 1649 | } else if (dev->link_status == EP_PCIE_LINK_UP) { |
| 1650 | EP_PCIE_INFO(&ep_pcie_dev, |
| 1651 | "PCIe V%d: PCIe link training is successful with host side. Waiting for enumeration to complete.\n", |
| 1652 | ep_pcie_dev.rev); |
| 1653 | } else { |
| 1654 | EP_PCIE_ERR(&ep_pcie_dev, |
| 1655 | "PCIe V%d: PCIe link is in the unexpected status: %d\n", |
| 1656 | ep_pcie_dev.rev, dev->link_status); |
| 1657 | } |
| 1658 | } |
| 1659 | |
| 1660 | return ret; |
| 1661 | } |
| 1662 | |
| 1663 | static void handle_perst_func(struct work_struct *work) |
| 1664 | { |
| 1665 | struct ep_pcie_dev_t *dev = container_of(work, struct ep_pcie_dev_t, |
| 1666 | handle_perst_work); |
| 1667 | |
Yan He | 0816989 | 2017-10-27 17:40:24 -0700 | [diff] [blame] | 1668 | EP_PCIE_DBG(dev, |
| 1669 | "PCIe V%d: Start enumeration due to PERST deassertion.\n", |
| 1670 | dev->rev); |
| 1671 | |
Yan He | 50222ad | 2017-11-17 18:22:17 -0800 | [diff] [blame] | 1672 | ep_pcie_enumeration(dev); |
| 1673 | } |
| 1674 | |
Yan He | 0816989 | 2017-10-27 17:40:24 -0700 | [diff] [blame] | 1675 | static void handle_d3cold_func(struct work_struct *work) |
| 1676 | { |
| 1677 | struct ep_pcie_dev_t *dev = container_of(work, struct ep_pcie_dev_t, |
| 1678 | handle_d3cold_work); |
| 1679 | |
| 1680 | EP_PCIE_DBG(dev, |
| 1681 | "PCIe V%d: shutdown PCIe link due to PERST assertion before BME is set.\n", |
| 1682 | dev->rev); |
| 1683 | ep_pcie_core_disable_endpoint(); |
| 1684 | dev->no_notify = false; |
| 1685 | } |
| 1686 | |
Yan He | 50222ad | 2017-11-17 18:22:17 -0800 | [diff] [blame] | 1687 | static void handle_bme_func(struct work_struct *work) |
| 1688 | { |
| 1689 | struct ep_pcie_dev_t *dev = container_of(work, |
| 1690 | struct ep_pcie_dev_t, handle_bme_work); |
| 1691 | |
| 1692 | ep_pcie_enumeration_complete(dev); |
| 1693 | } |
| 1694 | |
| 1695 | static irqreturn_t ep_pcie_handle_perst_irq(int irq, void *data) |
| 1696 | { |
| 1697 | struct ep_pcie_dev_t *dev = data; |
| 1698 | unsigned long irqsave_flags; |
| 1699 | u32 perst; |
| 1700 | |
| 1701 | spin_lock_irqsave(&dev->isr_lock, irqsave_flags); |
| 1702 | |
| 1703 | perst = gpio_get_value(dev->gpio[EP_PCIE_GPIO_PERST].num); |
| 1704 | |
| 1705 | if (!dev->enumerated) { |
| 1706 | EP_PCIE_DBG(dev, |
| 1707 | "PCIe V%d: PCIe is not enumerated yet; PERST is %sasserted.\n", |
| 1708 | dev->rev, perst ? "de" : ""); |
Yan He | 0816989 | 2017-10-27 17:40:24 -0700 | [diff] [blame] | 1709 | if (perst) { |
| 1710 | /* start work for link enumeration with the host side */ |
| 1711 | schedule_work(&dev->handle_perst_work); |
| 1712 | } else { |
| 1713 | dev->no_notify = true; |
| 1714 | /* shutdown the link if the link is already on */ |
| 1715 | schedule_work(&dev->handle_d3cold_work); |
| 1716 | } |
Yan He | 50222ad | 2017-11-17 18:22:17 -0800 | [diff] [blame] | 1717 | |
| 1718 | goto out; |
| 1719 | } |
| 1720 | |
| 1721 | if (perst) { |
| 1722 | dev->perst_deast = true; |
| 1723 | dev->perst_deast_counter++; |
| 1724 | EP_PCIE_DBG(dev, |
| 1725 | "PCIe V%d: No. %ld PERST deassertion.\n", |
| 1726 | dev->rev, dev->perst_deast_counter); |
| 1727 | ep_pcie_notify_event(dev, EP_PCIE_EVENT_PM_RST_DEAST); |
| 1728 | } else { |
| 1729 | dev->perst_deast = false; |
| 1730 | dev->perst_ast_counter++; |
| 1731 | EP_PCIE_DBG(dev, |
| 1732 | "PCIe V%d: No. %ld PERST assertion.\n", |
| 1733 | dev->rev, dev->perst_ast_counter); |
Yan He | 0816989 | 2017-10-27 17:40:24 -0700 | [diff] [blame] | 1734 | |
| 1735 | if (dev->client_ready) { |
| 1736 | ep_pcie_notify_event(dev, EP_PCIE_EVENT_PM_D3_COLD); |
| 1737 | } else { |
| 1738 | dev->no_notify = true; |
| 1739 | EP_PCIE_DBG(dev, |
| 1740 | "PCIe V%d: Client driver is not ready when this PERST assertion happens; shutdown link now.\n", |
| 1741 | dev->rev); |
| 1742 | schedule_work(&dev->handle_d3cold_work); |
| 1743 | } |
Yan He | 50222ad | 2017-11-17 18:22:17 -0800 | [diff] [blame] | 1744 | } |
| 1745 | |
| 1746 | out: |
| 1747 | spin_unlock_irqrestore(&dev->isr_lock, irqsave_flags); |
| 1748 | |
| 1749 | return IRQ_HANDLED; |
| 1750 | } |
| 1751 | |
| 1752 | static irqreturn_t ep_pcie_handle_global_irq(int irq, void *data) |
| 1753 | { |
| 1754 | struct ep_pcie_dev_t *dev = data; |
| 1755 | int i; |
| 1756 | u32 status = readl_relaxed(dev->parf + PCIE20_PARF_INT_ALL_STATUS); |
| 1757 | u32 mask = readl_relaxed(dev->parf + PCIE20_PARF_INT_ALL_MASK); |
| 1758 | |
| 1759 | ep_pcie_write_mask(dev->parf + PCIE20_PARF_INT_ALL_CLEAR, 0, status); |
| 1760 | |
| 1761 | dev->global_irq_counter++; |
| 1762 | EP_PCIE_DUMP(dev, |
| 1763 | "PCIe V%d: No. %ld Global IRQ %d received; status:0x%x; mask:0x%x.\n", |
| 1764 | dev->rev, dev->global_irq_counter, irq, status, mask); |
| 1765 | status &= mask; |
| 1766 | |
| 1767 | for (i = 1; i <= EP_PCIE_INT_EVT_MAX; i++) { |
| 1768 | if (status & BIT(i)) { |
| 1769 | switch (i) { |
| 1770 | case EP_PCIE_INT_EVT_LINK_DOWN: |
| 1771 | EP_PCIE_DUMP(dev, |
| 1772 | "PCIe V%d: handle linkdown event.\n", |
| 1773 | dev->rev); |
| 1774 | ep_pcie_handle_linkdown_irq(irq, data); |
| 1775 | break; |
| 1776 | case EP_PCIE_INT_EVT_BME: |
| 1777 | EP_PCIE_DUMP(dev, |
| 1778 | "PCIe V%d: handle BME event.\n", |
| 1779 | dev->rev); |
| 1780 | ep_pcie_handle_bme_irq(irq, data); |
| 1781 | break; |
| 1782 | case EP_PCIE_INT_EVT_PM_TURNOFF: |
| 1783 | EP_PCIE_DUMP(dev, |
| 1784 | "PCIe V%d: handle PM Turn-off event.\n", |
| 1785 | dev->rev); |
| 1786 | ep_pcie_handle_pm_turnoff_irq(irq, data); |
| 1787 | break; |
| 1788 | case EP_PCIE_INT_EVT_MHI_A7: |
| 1789 | EP_PCIE_DUMP(dev, |
| 1790 | "PCIe V%d: handle MHI A7 event.\n", |
| 1791 | dev->rev); |
| 1792 | ep_pcie_notify_event(dev, EP_PCIE_EVENT_MHI_A7); |
| 1793 | break; |
| 1794 | case EP_PCIE_INT_EVT_DSTATE_CHANGE: |
| 1795 | EP_PCIE_DUMP(dev, |
| 1796 | "PCIe V%d: handle D state chagge event.\n", |
| 1797 | dev->rev); |
| 1798 | ep_pcie_handle_dstate_change_irq(irq, data); |
| 1799 | break; |
| 1800 | case EP_PCIE_INT_EVT_LINK_UP: |
| 1801 | EP_PCIE_DUMP(dev, |
| 1802 | "PCIe V%d: handle linkup event.\n", |
| 1803 | dev->rev); |
| 1804 | ep_pcie_handle_linkup_irq(irq, data); |
| 1805 | break; |
| 1806 | default: |
| 1807 | EP_PCIE_ERR(dev, |
| 1808 | "PCIe V%d: Unexpected event %d is caught!\n", |
| 1809 | dev->rev, i); |
| 1810 | } |
| 1811 | } |
| 1812 | } |
| 1813 | |
| 1814 | return IRQ_HANDLED; |
| 1815 | } |
| 1816 | |
| 1817 | int32_t ep_pcie_irq_init(struct ep_pcie_dev_t *dev) |
| 1818 | { |
| 1819 | int ret; |
| 1820 | struct device *pdev = &dev->pdev->dev; |
| 1821 | u32 perst_irq; |
| 1822 | |
| 1823 | EP_PCIE_DBG(dev, "PCIe V%d\n", dev->rev); |
| 1824 | |
| 1825 | /* Initialize all works to be performed before registering for IRQs*/ |
| 1826 | INIT_WORK(&dev->handle_perst_work, handle_perst_func); |
| 1827 | INIT_WORK(&dev->handle_bme_work, handle_bme_func); |
Yan He | 0816989 | 2017-10-27 17:40:24 -0700 | [diff] [blame] | 1828 | INIT_WORK(&dev->handle_d3cold_work, handle_d3cold_func); |
Yan He | 50222ad | 2017-11-17 18:22:17 -0800 | [diff] [blame] | 1829 | |
| 1830 | if (dev->aggregated_irq) { |
| 1831 | ret = devm_request_irq(pdev, |
| 1832 | dev->irq[EP_PCIE_INT_GLOBAL].num, |
| 1833 | ep_pcie_handle_global_irq, |
| 1834 | IRQF_TRIGGER_HIGH, dev->irq[EP_PCIE_INT_GLOBAL].name, |
| 1835 | dev); |
| 1836 | if (ret) { |
| 1837 | EP_PCIE_ERR(dev, |
| 1838 | "PCIe V%d: Unable to request global interrupt %d\n", |
| 1839 | dev->rev, dev->irq[EP_PCIE_INT_GLOBAL].num); |
| 1840 | return ret; |
| 1841 | } |
| 1842 | |
| 1843 | ret = enable_irq_wake(dev->irq[EP_PCIE_INT_GLOBAL].num); |
| 1844 | if (ret) { |
| 1845 | EP_PCIE_ERR(dev, |
| 1846 | "PCIe V%d: Unable to enable wake for Global interrupt\n", |
| 1847 | dev->rev); |
| 1848 | return ret; |
| 1849 | } |
| 1850 | |
| 1851 | EP_PCIE_DBG(dev, |
| 1852 | "PCIe V%d: request global interrupt %d\n", |
| 1853 | dev->rev, dev->irq[EP_PCIE_INT_GLOBAL].num); |
| 1854 | goto perst_irq; |
| 1855 | } |
| 1856 | |
| 1857 | /* register handler for BME interrupt */ |
| 1858 | ret = devm_request_irq(pdev, |
| 1859 | dev->irq[EP_PCIE_INT_BME].num, |
| 1860 | ep_pcie_handle_bme_irq, |
| 1861 | IRQF_TRIGGER_RISING, dev->irq[EP_PCIE_INT_BME].name, |
| 1862 | dev); |
| 1863 | if (ret) { |
| 1864 | EP_PCIE_ERR(dev, |
| 1865 | "PCIe V%d: Unable to request BME interrupt %d\n", |
| 1866 | dev->rev, dev->irq[EP_PCIE_INT_BME].num); |
| 1867 | return ret; |
| 1868 | } |
| 1869 | |
| 1870 | ret = enable_irq_wake(dev->irq[EP_PCIE_INT_BME].num); |
| 1871 | if (ret) { |
| 1872 | EP_PCIE_ERR(dev, |
| 1873 | "PCIe V%d: Unable to enable wake for BME interrupt\n", |
| 1874 | dev->rev); |
| 1875 | return ret; |
| 1876 | } |
| 1877 | |
| 1878 | /* register handler for linkdown interrupt */ |
| 1879 | ret = devm_request_irq(pdev, |
| 1880 | dev->irq[EP_PCIE_INT_LINK_DOWN].num, |
| 1881 | ep_pcie_handle_linkdown_irq, |
| 1882 | IRQF_TRIGGER_RISING, dev->irq[EP_PCIE_INT_LINK_DOWN].name, |
| 1883 | dev); |
| 1884 | if (ret) { |
| 1885 | EP_PCIE_ERR(dev, |
| 1886 | "PCIe V%d: Unable to request linkdown interrupt %d\n", |
| 1887 | dev->rev, dev->irq[EP_PCIE_INT_LINK_DOWN].num); |
| 1888 | return ret; |
| 1889 | } |
| 1890 | |
| 1891 | /* register handler for linkup interrupt */ |
| 1892 | ret = devm_request_irq(pdev, |
| 1893 | dev->irq[EP_PCIE_INT_LINK_UP].num, ep_pcie_handle_linkup_irq, |
| 1894 | IRQF_TRIGGER_RISING, dev->irq[EP_PCIE_INT_LINK_UP].name, |
| 1895 | dev); |
| 1896 | if (ret) { |
| 1897 | EP_PCIE_ERR(dev, |
| 1898 | "PCIe V%d: Unable to request linkup interrupt %d\n", |
| 1899 | dev->rev, dev->irq[EP_PCIE_INT_LINK_UP].num); |
| 1900 | return ret; |
| 1901 | } |
| 1902 | |
| 1903 | /* register handler for PM_TURNOFF interrupt */ |
| 1904 | ret = devm_request_irq(pdev, |
| 1905 | dev->irq[EP_PCIE_INT_PM_TURNOFF].num, |
| 1906 | ep_pcie_handle_pm_turnoff_irq, |
| 1907 | IRQF_TRIGGER_RISING, dev->irq[EP_PCIE_INT_PM_TURNOFF].name, |
| 1908 | dev); |
| 1909 | if (ret) { |
| 1910 | EP_PCIE_ERR(dev, |
| 1911 | "PCIe V%d: Unable to request PM_TURNOFF interrupt %d\n", |
| 1912 | dev->rev, dev->irq[EP_PCIE_INT_PM_TURNOFF].num); |
| 1913 | return ret; |
| 1914 | } |
| 1915 | |
| 1916 | /* register handler for D state change interrupt */ |
| 1917 | ret = devm_request_irq(pdev, |
| 1918 | dev->irq[EP_PCIE_INT_DSTATE_CHANGE].num, |
| 1919 | ep_pcie_handle_dstate_change_irq, |
| 1920 | IRQF_TRIGGER_RISING, dev->irq[EP_PCIE_INT_DSTATE_CHANGE].name, |
| 1921 | dev); |
| 1922 | if (ret) { |
| 1923 | EP_PCIE_ERR(dev, |
| 1924 | "PCIe V%d: Unable to request D state change interrupt %d\n", |
| 1925 | dev->rev, dev->irq[EP_PCIE_INT_DSTATE_CHANGE].num); |
| 1926 | return ret; |
| 1927 | } |
| 1928 | |
| 1929 | perst_irq: |
| 1930 | /* register handler for PERST interrupt */ |
| 1931 | perst_irq = gpio_to_irq(dev->gpio[EP_PCIE_GPIO_PERST].num); |
| 1932 | ret = devm_request_irq(pdev, perst_irq, |
| 1933 | ep_pcie_handle_perst_irq, |
| 1934 | IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING, |
| 1935 | "ep_pcie_perst", dev); |
| 1936 | if (ret) { |
| 1937 | EP_PCIE_ERR(dev, |
| 1938 | "PCIe V%d: Unable to request PERST interrupt %d\n", |
| 1939 | dev->rev, perst_irq); |
| 1940 | return ret; |
| 1941 | } |
| 1942 | |
| 1943 | ret = enable_irq_wake(perst_irq); |
| 1944 | if (ret) { |
| 1945 | EP_PCIE_ERR(dev, |
| 1946 | "PCIe V%d: Unable to enable PERST interrupt %d\n", |
| 1947 | dev->rev, perst_irq); |
| 1948 | return ret; |
| 1949 | } |
| 1950 | |
| 1951 | return 0; |
| 1952 | } |
| 1953 | |
| 1954 | void ep_pcie_irq_deinit(struct ep_pcie_dev_t *dev) |
| 1955 | { |
| 1956 | EP_PCIE_DBG(dev, "PCIe V%d\n", dev->rev); |
| 1957 | |
| 1958 | disable_irq(gpio_to_irq(dev->gpio[EP_PCIE_GPIO_PERST].num)); |
| 1959 | } |
| 1960 | |
| 1961 | int ep_pcie_core_register_event(struct ep_pcie_register_event *reg) |
| 1962 | { |
| 1963 | if (!reg) { |
| 1964 | EP_PCIE_ERR(&ep_pcie_dev, |
| 1965 | "PCIe V%d: Event registration is NULL\n", |
| 1966 | ep_pcie_dev.rev); |
| 1967 | return -ENODEV; |
| 1968 | } |
| 1969 | |
| 1970 | if (!reg->user) { |
| 1971 | EP_PCIE_ERR(&ep_pcie_dev, |
| 1972 | "PCIe V%d: User of event registration is NULL\n", |
| 1973 | ep_pcie_dev.rev); |
| 1974 | return -ENODEV; |
| 1975 | } |
| 1976 | |
| 1977 | ep_pcie_dev.event_reg = reg; |
| 1978 | EP_PCIE_DBG(&ep_pcie_dev, |
| 1979 | "PCIe V%d: Event 0x%x is registered\n", |
| 1980 | ep_pcie_dev.rev, reg->events); |
| 1981 | |
Yan He | 0816989 | 2017-10-27 17:40:24 -0700 | [diff] [blame] | 1982 | ep_pcie_dev.client_ready = true; |
| 1983 | |
Yan He | 50222ad | 2017-11-17 18:22:17 -0800 | [diff] [blame] | 1984 | return 0; |
| 1985 | } |
| 1986 | |
| 1987 | int ep_pcie_core_deregister_event(void) |
| 1988 | { |
| 1989 | if (ep_pcie_dev.event_reg) { |
| 1990 | EP_PCIE_DBG(&ep_pcie_dev, |
| 1991 | "PCIe V%d: current registered events:0x%x; events are deregistered.\n", |
| 1992 | ep_pcie_dev.rev, ep_pcie_dev.event_reg->events); |
| 1993 | ep_pcie_dev.event_reg = NULL; |
| 1994 | } else { |
| 1995 | EP_PCIE_ERR(&ep_pcie_dev, |
| 1996 | "PCIe V%d: Event registration is NULL\n", |
| 1997 | ep_pcie_dev.rev); |
| 1998 | } |
| 1999 | |
| 2000 | return 0; |
| 2001 | } |
| 2002 | |
| 2003 | enum ep_pcie_link_status ep_pcie_core_get_linkstatus(void) |
| 2004 | { |
| 2005 | struct ep_pcie_dev_t *dev = &ep_pcie_dev; |
| 2006 | u32 bme; |
| 2007 | |
| 2008 | if (!dev->power_on || (dev->link_status == EP_PCIE_LINK_DISABLED)) { |
| 2009 | EP_PCIE_DBG(dev, |
| 2010 | "PCIe V%d: PCIe endpoint is not powered on.\n", |
| 2011 | dev->rev); |
| 2012 | return EP_PCIE_LINK_DISABLED; |
| 2013 | } |
| 2014 | |
| 2015 | bme = readl_relaxed(dev->dm_core + |
| 2016 | PCIE20_COMMAND_STATUS) & BIT(2); |
| 2017 | if (bme) { |
| 2018 | EP_PCIE_DBG(dev, |
| 2019 | "PCIe V%d: PCIe link is up and BME is enabled; current SW link status:%d.\n", |
| 2020 | dev->rev, dev->link_status); |
| 2021 | dev->link_status = EP_PCIE_LINK_ENABLED; |
Yan He | 0816989 | 2017-10-27 17:40:24 -0700 | [diff] [blame] | 2022 | if (dev->no_notify) { |
| 2023 | EP_PCIE_DBG(dev, |
| 2024 | "PCIe V%d: BME is set now, but do not tell client about BME enable.\n", |
| 2025 | dev->rev); |
| 2026 | return EP_PCIE_LINK_UP; |
| 2027 | } |
Yan He | 50222ad | 2017-11-17 18:22:17 -0800 | [diff] [blame] | 2028 | } else { |
| 2029 | EP_PCIE_DBG(dev, |
| 2030 | "PCIe V%d: PCIe link is up but BME is disabled; current SW link status:%d.\n", |
| 2031 | dev->rev, dev->link_status); |
| 2032 | dev->link_status = EP_PCIE_LINK_UP; |
| 2033 | } |
| 2034 | return dev->link_status; |
| 2035 | } |
| 2036 | |
| 2037 | int ep_pcie_core_config_outbound_iatu(struct ep_pcie_iatu entries[], |
| 2038 | u32 num_entries) |
| 2039 | { |
| 2040 | u32 data_start = 0; |
| 2041 | u32 data_end = 0; |
| 2042 | u32 data_tgt_lower = 0; |
| 2043 | u32 data_tgt_upper = 0; |
| 2044 | u32 ctrl_start = 0; |
| 2045 | u32 ctrl_end = 0; |
| 2046 | u32 ctrl_tgt_lower = 0; |
| 2047 | u32 ctrl_tgt_upper = 0; |
| 2048 | u32 upper = 0; |
| 2049 | bool once = true; |
| 2050 | |
| 2051 | if (ep_pcie_dev.active_config) { |
| 2052 | upper = EP_PCIE_OATU_UPPER; |
| 2053 | if (once) { |
| 2054 | once = false; |
| 2055 | EP_PCIE_DBG2(&ep_pcie_dev, |
| 2056 | "PCIe V%d: No outbound iATU config is needed since active config is enabled.\n", |
| 2057 | ep_pcie_dev.rev); |
| 2058 | } |
| 2059 | } |
| 2060 | |
| 2061 | if ((num_entries > MAX_IATU_ENTRY_NUM) || !num_entries) { |
| 2062 | EP_PCIE_ERR(&ep_pcie_dev, |
| 2063 | "PCIe V%d: Wrong iATU entry number %d.\n", |
| 2064 | ep_pcie_dev.rev, num_entries); |
| 2065 | return EP_PCIE_ERROR; |
| 2066 | } |
| 2067 | |
| 2068 | data_start = entries[0].start; |
| 2069 | data_end = entries[0].end; |
| 2070 | data_tgt_lower = entries[0].tgt_lower; |
| 2071 | data_tgt_upper = entries[0].tgt_upper; |
| 2072 | |
| 2073 | if (num_entries > 1) { |
| 2074 | ctrl_start = entries[1].start; |
| 2075 | ctrl_end = entries[1].end; |
| 2076 | ctrl_tgt_lower = entries[1].tgt_lower; |
| 2077 | ctrl_tgt_upper = entries[1].tgt_upper; |
| 2078 | } |
| 2079 | |
| 2080 | EP_PCIE_DBG(&ep_pcie_dev, |
| 2081 | "PCIe V%d: data_start:0x%x; data_end:0x%x; data_tgt_lower:0x%x; data_tgt_upper:0x%x; ctrl_start:0x%x; ctrl_end:0x%x; ctrl_tgt_lower:0x%x; ctrl_tgt_upper:0x%x.\n", |
| 2082 | ep_pcie_dev.rev, data_start, data_end, data_tgt_lower, |
| 2083 | data_tgt_upper, ctrl_start, ctrl_end, ctrl_tgt_lower, |
| 2084 | ctrl_tgt_upper); |
| 2085 | |
| 2086 | |
| 2087 | if ((ctrl_end < data_start) || (data_end < ctrl_start)) { |
| 2088 | EP_PCIE_DBG(&ep_pcie_dev, |
| 2089 | "PCIe V%d: iATU configuration case No. 1: detached.\n", |
| 2090 | ep_pcie_dev.rev); |
| 2091 | ep_pcie_config_outbound_iatu_entry(&ep_pcie_dev, |
| 2092 | EP_PCIE_OATU_INDEX_DATA, |
| 2093 | data_start, upper, data_end, |
| 2094 | data_tgt_lower, data_tgt_upper); |
| 2095 | ep_pcie_config_outbound_iatu_entry(&ep_pcie_dev, |
| 2096 | EP_PCIE_OATU_INDEX_CTRL, |
| 2097 | ctrl_start, upper, ctrl_end, |
| 2098 | ctrl_tgt_lower, ctrl_tgt_upper); |
| 2099 | } else if ((data_start <= ctrl_start) && (ctrl_end <= data_end)) { |
| 2100 | EP_PCIE_DBG(&ep_pcie_dev, |
| 2101 | "PCIe V%d: iATU configuration case No. 2: included.\n", |
| 2102 | ep_pcie_dev.rev); |
| 2103 | ep_pcie_config_outbound_iatu_entry(&ep_pcie_dev, |
| 2104 | EP_PCIE_OATU_INDEX_DATA, |
| 2105 | data_start, upper, data_end, |
| 2106 | data_tgt_lower, data_tgt_upper); |
| 2107 | } else { |
| 2108 | EP_PCIE_DBG(&ep_pcie_dev, |
| 2109 | "PCIe V%d: iATU configuration case No. 3: overlap.\n", |
| 2110 | ep_pcie_dev.rev); |
| 2111 | ep_pcie_config_outbound_iatu_entry(&ep_pcie_dev, |
| 2112 | EP_PCIE_OATU_INDEX_CTRL, |
| 2113 | ctrl_start, upper, ctrl_end, |
| 2114 | ctrl_tgt_lower, ctrl_tgt_upper); |
| 2115 | ep_pcie_config_outbound_iatu_entry(&ep_pcie_dev, |
| 2116 | EP_PCIE_OATU_INDEX_DATA, |
| 2117 | data_start, upper, data_end, |
| 2118 | data_tgt_lower, data_tgt_upper); |
| 2119 | } |
| 2120 | |
| 2121 | return 0; |
| 2122 | } |
| 2123 | |
| 2124 | int ep_pcie_core_get_msi_config(struct ep_pcie_msi_config *cfg) |
| 2125 | { |
| 2126 | u32 cap, lower, upper, data, ctrl_reg; |
| 2127 | static u32 changes; |
| 2128 | |
| 2129 | if (ep_pcie_dev.link_status == EP_PCIE_LINK_DISABLED) { |
| 2130 | EP_PCIE_ERR(&ep_pcie_dev, |
| 2131 | "PCIe V%d: PCIe link is currently disabled.\n", |
| 2132 | ep_pcie_dev.rev); |
| 2133 | return EP_PCIE_ERROR; |
| 2134 | } |
| 2135 | |
| 2136 | cap = readl_relaxed(ep_pcie_dev.dm_core + PCIE20_MSI_CAP_ID_NEXT_CTRL); |
| 2137 | EP_PCIE_DBG(&ep_pcie_dev, "PCIe V%d: MSI CAP:0x%x\n", |
| 2138 | ep_pcie_dev.rev, cap); |
| 2139 | |
| 2140 | if (!(cap & BIT(16))) { |
| 2141 | EP_PCIE_ERR(&ep_pcie_dev, |
| 2142 | "PCIe V%d: MSI is not enabled yet.\n", |
| 2143 | ep_pcie_dev.rev); |
| 2144 | return EP_PCIE_ERROR; |
| 2145 | } |
| 2146 | |
| 2147 | lower = readl_relaxed(ep_pcie_dev.dm_core + PCIE20_MSI_LOWER); |
| 2148 | upper = readl_relaxed(ep_pcie_dev.dm_core + PCIE20_MSI_UPPER); |
| 2149 | data = readl_relaxed(ep_pcie_dev.dm_core + PCIE20_MSI_DATA); |
| 2150 | ctrl_reg = readl_relaxed(ep_pcie_dev.dm_core + |
| 2151 | PCIE20_MSI_CAP_ID_NEXT_CTRL); |
| 2152 | |
| 2153 | EP_PCIE_DBG(&ep_pcie_dev, |
| 2154 | "PCIe V%d: MSI info: lower:0x%x; upper:0x%x; data:0x%x.\n", |
| 2155 | ep_pcie_dev.rev, lower, upper, data); |
| 2156 | |
| 2157 | if (ctrl_reg & BIT(16)) { |
| 2158 | struct resource *msi = |
| 2159 | ep_pcie_dev.res[EP_PCIE_RES_MSI].resource; |
| 2160 | if (ep_pcie_dev.active_config) |
| 2161 | ep_pcie_config_outbound_iatu_entry(&ep_pcie_dev, |
| 2162 | EP_PCIE_OATU_INDEX_MSI, |
| 2163 | msi->start, EP_PCIE_OATU_UPPER, |
| 2164 | msi->end, lower, upper); |
| 2165 | else |
| 2166 | ep_pcie_config_outbound_iatu_entry(&ep_pcie_dev, |
| 2167 | EP_PCIE_OATU_INDEX_MSI, |
| 2168 | msi->start, 0, msi->end, |
| 2169 | lower, upper); |
| 2170 | |
| 2171 | if (ep_pcie_dev.active_config) { |
| 2172 | cfg->lower = lower; |
| 2173 | cfg->upper = upper; |
| 2174 | } else { |
| 2175 | cfg->lower = msi->start + (lower & 0xfff); |
| 2176 | cfg->upper = 0; |
| 2177 | } |
| 2178 | cfg->data = data; |
| 2179 | cfg->msg_num = (cap >> 20) & 0x7; |
| 2180 | if ((lower != ep_pcie_dev.msi_cfg.lower) |
| 2181 | || (upper != ep_pcie_dev.msi_cfg.upper) |
| 2182 | || (data != ep_pcie_dev.msi_cfg.data) |
| 2183 | || (cfg->msg_num != ep_pcie_dev.msi_cfg.msg_num)) { |
| 2184 | changes++; |
| 2185 | EP_PCIE_DBG(&ep_pcie_dev, |
| 2186 | "PCIe V%d: MSI config has been changed by host side for %d time(s).\n", |
| 2187 | ep_pcie_dev.rev, changes); |
| 2188 | EP_PCIE_DBG(&ep_pcie_dev, |
| 2189 | "PCIe V%d: old MSI cfg: lower:0x%x; upper:0x%x; data:0x%x; msg_num:0x%x.\n", |
| 2190 | ep_pcie_dev.rev, ep_pcie_dev.msi_cfg.lower, |
| 2191 | ep_pcie_dev.msi_cfg.upper, |
| 2192 | ep_pcie_dev.msi_cfg.data, |
| 2193 | ep_pcie_dev.msi_cfg.msg_num); |
| 2194 | ep_pcie_dev.msi_cfg.lower = lower; |
| 2195 | ep_pcie_dev.msi_cfg.upper = upper; |
| 2196 | ep_pcie_dev.msi_cfg.data = data; |
| 2197 | ep_pcie_dev.msi_cfg.msg_num = cfg->msg_num; |
| 2198 | } |
| 2199 | return 0; |
| 2200 | } |
| 2201 | |
| 2202 | EP_PCIE_ERR(&ep_pcie_dev, |
| 2203 | "PCIe V%d: Wrong MSI info found when MSI is enabled: lower:0x%x; data:0x%x.\n", |
| 2204 | ep_pcie_dev.rev, lower, data); |
| 2205 | return EP_PCIE_ERROR; |
| 2206 | } |
| 2207 | |
| 2208 | int ep_pcie_core_trigger_msi(u32 idx) |
| 2209 | { |
| 2210 | u32 addr, data, ctrl_reg; |
| 2211 | int max_poll = MSI_EXIT_L1SS_WAIT_MAX_COUNT; |
| 2212 | |
| 2213 | if (ep_pcie_dev.link_status == EP_PCIE_LINK_DISABLED) { |
| 2214 | EP_PCIE_ERR(&ep_pcie_dev, |
| 2215 | "PCIe V%d: PCIe link is currently disabled.\n", |
| 2216 | ep_pcie_dev.rev); |
| 2217 | return EP_PCIE_ERROR; |
| 2218 | } |
| 2219 | |
| 2220 | addr = readl_relaxed(ep_pcie_dev.dm_core + PCIE20_MSI_LOWER); |
| 2221 | data = readl_relaxed(ep_pcie_dev.dm_core + PCIE20_MSI_DATA); |
| 2222 | ctrl_reg = readl_relaxed(ep_pcie_dev.dm_core + |
| 2223 | PCIE20_MSI_CAP_ID_NEXT_CTRL); |
| 2224 | |
| 2225 | if (ctrl_reg & BIT(16)) { |
| 2226 | ep_pcie_dev.msi_counter++; |
| 2227 | EP_PCIE_DUMP(&ep_pcie_dev, |
| 2228 | "PCIe V%d: No. %ld MSI fired for IRQ %d; index from client:%d; active-config is %s enabled.\n", |
| 2229 | ep_pcie_dev.rev, ep_pcie_dev.msi_counter, |
| 2230 | data + idx, idx, |
| 2231 | ep_pcie_dev.active_config ? "" : "not"); |
| 2232 | |
| 2233 | if (ep_pcie_dev.active_config) { |
| 2234 | u32 status; |
| 2235 | |
| 2236 | if (ep_pcie_dev.msi_counter % 2) { |
| 2237 | EP_PCIE_DBG2(&ep_pcie_dev, |
| 2238 | "PCIe V%d: try to trigger MSI by PARF_MSI_GEN.\n", |
| 2239 | ep_pcie_dev.rev); |
| 2240 | ep_pcie_write_reg(ep_pcie_dev.parf, |
| 2241 | PCIE20_PARF_MSI_GEN, idx); |
| 2242 | status = readl_relaxed(ep_pcie_dev.parf + |
| 2243 | PCIE20_PARF_LTR_MSI_EXIT_L1SS); |
| 2244 | while ((status & BIT(1)) && (max_poll-- > 0)) { |
| 2245 | udelay(MSI_EXIT_L1SS_WAIT); |
| 2246 | status = readl_relaxed(ep_pcie_dev.parf |
| 2247 | + |
| 2248 | PCIE20_PARF_LTR_MSI_EXIT_L1SS); |
| 2249 | } |
| 2250 | if (max_poll == 0) |
| 2251 | EP_PCIE_DBG2(&ep_pcie_dev, |
| 2252 | "PCIe V%d: MSI_EXIT_L1SS is not cleared yet.\n", |
| 2253 | ep_pcie_dev.rev); |
| 2254 | else |
| 2255 | EP_PCIE_DBG2(&ep_pcie_dev, |
| 2256 | "PCIe V%d: MSI_EXIT_L1SS has been cleared.\n", |
| 2257 | ep_pcie_dev.rev); |
| 2258 | } else { |
| 2259 | EP_PCIE_DBG2(&ep_pcie_dev, |
| 2260 | "PCIe V%d: try to trigger MSI by direct address write as well.\n", |
| 2261 | ep_pcie_dev.rev); |
| 2262 | ep_pcie_write_reg(ep_pcie_dev.msi, addr & 0xfff, |
| 2263 | data + idx); |
| 2264 | } |
| 2265 | } else { |
| 2266 | ep_pcie_write_reg(ep_pcie_dev.msi, addr & 0xfff, data |
| 2267 | + idx); |
| 2268 | } |
| 2269 | return 0; |
| 2270 | } |
| 2271 | |
| 2272 | EP_PCIE_ERR(&ep_pcie_dev, |
| 2273 | "PCIe V%d: MSI is not enabled yet. MSI addr:0x%x; data:0x%x; index from client:%d.\n", |
| 2274 | ep_pcie_dev.rev, addr, data, idx); |
| 2275 | return EP_PCIE_ERROR; |
| 2276 | } |
| 2277 | |
| 2278 | int ep_pcie_core_wakeup_host(void) |
| 2279 | { |
| 2280 | struct ep_pcie_dev_t *dev = &ep_pcie_dev; |
| 2281 | |
| 2282 | if (dev->perst_deast && !dev->l23_ready) { |
| 2283 | EP_PCIE_ERR(dev, |
| 2284 | "PCIe V%d: request to assert WAKE# when PERST is de-asserted and D3hot is not received.\n", |
| 2285 | dev->rev); |
| 2286 | return EP_PCIE_ERROR; |
| 2287 | } |
| 2288 | |
| 2289 | dev->wake_counter++; |
| 2290 | EP_PCIE_DBG(dev, |
| 2291 | "PCIe V%d: No. %ld to assert PCIe WAKE#; perst is %s de-asserted; D3hot is %s received.\n", |
| 2292 | dev->rev, dev->wake_counter, |
| 2293 | dev->perst_deast ? "" : "not", |
| 2294 | dev->l23_ready ? "" : "not"); |
| 2295 | gpio_set_value(dev->gpio[EP_PCIE_GPIO_WAKE].num, |
| 2296 | 1 - dev->gpio[EP_PCIE_GPIO_WAKE].on); |
| 2297 | gpio_set_value(dev->gpio[EP_PCIE_GPIO_WAKE].num, |
| 2298 | dev->gpio[EP_PCIE_GPIO_WAKE].on); |
| 2299 | return 0; |
| 2300 | } |
| 2301 | |
| 2302 | int ep_pcie_core_config_db_routing(struct ep_pcie_db_config chdb_cfg, |
| 2303 | struct ep_pcie_db_config erdb_cfg) |
| 2304 | { |
| 2305 | u32 dbs = (erdb_cfg.end << 24) | (erdb_cfg.base << 16) | |
| 2306 | (chdb_cfg.end << 8) | chdb_cfg.base; |
| 2307 | |
| 2308 | ep_pcie_write_reg(ep_pcie_dev.parf, PCIE20_PARF_MHI_IPA_DBS, dbs); |
| 2309 | ep_pcie_write_reg(ep_pcie_dev.parf, |
| 2310 | PCIE20_PARF_MHI_IPA_CDB_TARGET_LOWER, |
| 2311 | chdb_cfg.tgt_addr); |
| 2312 | ep_pcie_write_reg(ep_pcie_dev.parf, |
| 2313 | PCIE20_PARF_MHI_IPA_EDB_TARGET_LOWER, |
| 2314 | erdb_cfg.tgt_addr); |
| 2315 | |
| 2316 | EP_PCIE_DBG(&ep_pcie_dev, |
| 2317 | "PCIe V%d: DB routing info: chdb_cfg.base:0x%x; chdb_cfg.end:0x%x; erdb_cfg.base:0x%x; erdb_cfg.end:0x%x; chdb_cfg.tgt_addr:0x%x; erdb_cfg.tgt_addr:0x%x.\n", |
| 2318 | ep_pcie_dev.rev, chdb_cfg.base, chdb_cfg.end, erdb_cfg.base, |
| 2319 | erdb_cfg.end, chdb_cfg.tgt_addr, erdb_cfg.tgt_addr); |
| 2320 | |
| 2321 | return 0; |
| 2322 | } |
| 2323 | |
| 2324 | struct ep_pcie_hw hw_drv = { |
| 2325 | .register_event = ep_pcie_core_register_event, |
| 2326 | .deregister_event = ep_pcie_core_deregister_event, |
| 2327 | .get_linkstatus = ep_pcie_core_get_linkstatus, |
| 2328 | .config_outbound_iatu = ep_pcie_core_config_outbound_iatu, |
| 2329 | .get_msi_config = ep_pcie_core_get_msi_config, |
| 2330 | .trigger_msi = ep_pcie_core_trigger_msi, |
| 2331 | .wakeup_host = ep_pcie_core_wakeup_host, |
| 2332 | .config_db_routing = ep_pcie_core_config_db_routing, |
| 2333 | .enable_endpoint = ep_pcie_core_enable_endpoint, |
| 2334 | .disable_endpoint = ep_pcie_core_disable_endpoint, |
| 2335 | .mask_irq_event = ep_pcie_core_mask_irq_event, |
| 2336 | }; |
| 2337 | |
| 2338 | static int ep_pcie_probe(struct platform_device *pdev) |
| 2339 | { |
| 2340 | int ret; |
| 2341 | |
| 2342 | pr_debug("%s\n", __func__); |
| 2343 | |
| 2344 | ep_pcie_dev.link_speed = 1; |
| 2345 | ret = of_property_read_u32((&pdev->dev)->of_node, |
| 2346 | "qcom,pcie-link-speed", |
| 2347 | &ep_pcie_dev.link_speed); |
| 2348 | if (ret) |
| 2349 | EP_PCIE_DBG(&ep_pcie_dev, |
| 2350 | "PCIe V%d: pcie-link-speed does not exist.\n", |
| 2351 | ep_pcie_dev.rev); |
| 2352 | else |
| 2353 | EP_PCIE_DBG(&ep_pcie_dev, "PCIe V%d: pcie-link-speed:%d.\n", |
| 2354 | ep_pcie_dev.rev, ep_pcie_dev.link_speed); |
| 2355 | |
| 2356 | ret = of_property_read_u32((&pdev->dev)->of_node, |
| 2357 | "qcom,dbi-base-reg", |
| 2358 | &ep_pcie_dev.dbi_base_reg); |
| 2359 | if (ret) |
| 2360 | EP_PCIE_DBG(&ep_pcie_dev, |
| 2361 | "PCIe V%d: dbi-base-reg does not exist.\n", |
| 2362 | ep_pcie_dev.rev); |
| 2363 | else |
| 2364 | EP_PCIE_DBG(&ep_pcie_dev, "PCIe V%d: dbi-base-reg:0x%x.\n", |
| 2365 | ep_pcie_dev.rev, ep_pcie_dev.dbi_base_reg); |
| 2366 | |
| 2367 | ret = of_property_read_u32((&pdev->dev)->of_node, |
| 2368 | "qcom,slv-space-reg", |
| 2369 | &ep_pcie_dev.slv_space_reg); |
| 2370 | if (ret) |
| 2371 | EP_PCIE_DBG(&ep_pcie_dev, |
| 2372 | "PCIe V%d: slv-space-reg does not exist.\n", |
| 2373 | ep_pcie_dev.rev); |
| 2374 | else |
| 2375 | EP_PCIE_DBG(&ep_pcie_dev, "PCIe V%d: slv-space-reg:0x%x.\n", |
| 2376 | ep_pcie_dev.rev, ep_pcie_dev.slv_space_reg); |
| 2377 | |
| 2378 | ret = of_property_read_u32((&pdev->dev)->of_node, |
| 2379 | "qcom,phy-status-reg", |
| 2380 | &ep_pcie_dev.phy_status_reg); |
| 2381 | if (ret) |
| 2382 | EP_PCIE_DBG(&ep_pcie_dev, |
| 2383 | "PCIe V%d: phy-status-reg does not exist.\n", |
| 2384 | ep_pcie_dev.rev); |
| 2385 | else |
| 2386 | EP_PCIE_DBG(&ep_pcie_dev, "PCIe V%d: phy-status-reg:0x%x.\n", |
| 2387 | ep_pcie_dev.rev, ep_pcie_dev.phy_status_reg); |
| 2388 | |
| 2389 | ep_pcie_dev.phy_rev = 1; |
| 2390 | ret = of_property_read_u32((&pdev->dev)->of_node, |
| 2391 | "qcom,pcie-phy-ver", |
| 2392 | &ep_pcie_dev.phy_rev); |
| 2393 | if (ret) |
| 2394 | EP_PCIE_DBG(&ep_pcie_dev, |
| 2395 | "PCIe V%d: pcie-phy-ver does not exist.\n", |
| 2396 | ep_pcie_dev.rev); |
| 2397 | else |
| 2398 | EP_PCIE_DBG(&ep_pcie_dev, "PCIe V%d: pcie-phy-ver:%d.\n", |
| 2399 | ep_pcie_dev.rev, ep_pcie_dev.phy_rev); |
| 2400 | |
| 2401 | ep_pcie_dev.active_config = of_property_read_bool((&pdev->dev)->of_node, |
| 2402 | "qcom,pcie-active-config"); |
| 2403 | EP_PCIE_DBG(&ep_pcie_dev, |
| 2404 | "PCIe V%d: active config is %s enabled.\n", |
| 2405 | ep_pcie_dev.rev, ep_pcie_dev.active_config ? "" : "not"); |
| 2406 | |
| 2407 | ep_pcie_dev.aggregated_irq = |
| 2408 | of_property_read_bool((&pdev->dev)->of_node, |
| 2409 | "qcom,pcie-aggregated-irq"); |
| 2410 | EP_PCIE_DBG(&ep_pcie_dev, |
| 2411 | "PCIe V%d: aggregated IRQ is %s enabled.\n", |
| 2412 | ep_pcie_dev.rev, ep_pcie_dev.aggregated_irq ? "" : "not"); |
| 2413 | |
| 2414 | ep_pcie_dev.mhi_a7_irq = |
| 2415 | of_property_read_bool((&pdev->dev)->of_node, |
| 2416 | "qcom,pcie-mhi-a7-irq"); |
| 2417 | EP_PCIE_DBG(&ep_pcie_dev, |
| 2418 | "PCIe V%d: Mhi a7 IRQ is %s enabled.\n", |
| 2419 | ep_pcie_dev.rev, ep_pcie_dev.mhi_a7_irq ? "" : "not"); |
| 2420 | |
| 2421 | ep_pcie_dev.perst_enum = of_property_read_bool((&pdev->dev)->of_node, |
| 2422 | "qcom,pcie-perst-enum"); |
| 2423 | EP_PCIE_DBG(&ep_pcie_dev, |
| 2424 | "PCIe V%d: enum by PERST is %s enabled.\n", |
| 2425 | ep_pcie_dev.rev, ep_pcie_dev.perst_enum ? "" : "not"); |
| 2426 | |
| 2427 | ep_pcie_dev.rev = 1711211; |
| 2428 | ep_pcie_dev.pdev = pdev; |
| 2429 | memcpy(ep_pcie_dev.vreg, ep_pcie_vreg_info, |
| 2430 | sizeof(ep_pcie_vreg_info)); |
| 2431 | memcpy(ep_pcie_dev.gpio, ep_pcie_gpio_info, |
| 2432 | sizeof(ep_pcie_gpio_info)); |
| 2433 | memcpy(ep_pcie_dev.clk, ep_pcie_clk_info, |
| 2434 | sizeof(ep_pcie_clk_info)); |
| 2435 | memcpy(ep_pcie_dev.pipeclk, ep_pcie_pipe_clk_info, |
| 2436 | sizeof(ep_pcie_pipe_clk_info)); |
| 2437 | memcpy(ep_pcie_dev.reset, ep_pcie_reset_info, |
| 2438 | sizeof(ep_pcie_reset_info)); |
| 2439 | memcpy(ep_pcie_dev.res, ep_pcie_res_info, |
| 2440 | sizeof(ep_pcie_res_info)); |
| 2441 | memcpy(ep_pcie_dev.irq, ep_pcie_irq_info, |
| 2442 | sizeof(ep_pcie_irq_info)); |
| 2443 | |
| 2444 | ret = ep_pcie_get_resources(&ep_pcie_dev, |
| 2445 | ep_pcie_dev.pdev); |
| 2446 | if (ret) { |
| 2447 | EP_PCIE_ERR(&ep_pcie_dev, |
| 2448 | "PCIe V%d: failed to get resources.\n", |
| 2449 | ep_pcie_dev.rev); |
| 2450 | goto res_failure; |
| 2451 | } |
| 2452 | |
| 2453 | ret = ep_pcie_gpio_init(&ep_pcie_dev); |
| 2454 | if (ret) { |
| 2455 | EP_PCIE_ERR(&ep_pcie_dev, |
| 2456 | "PCIe V%d: failed to init GPIO.\n", |
| 2457 | ep_pcie_dev.rev); |
| 2458 | ep_pcie_release_resources(&ep_pcie_dev); |
| 2459 | goto gpio_failure; |
| 2460 | } |
| 2461 | |
| 2462 | ret = ep_pcie_irq_init(&ep_pcie_dev); |
| 2463 | if (ret) { |
| 2464 | EP_PCIE_ERR(&ep_pcie_dev, |
| 2465 | "PCIe V%d: failed to init IRQ.\n", |
| 2466 | ep_pcie_dev.rev); |
| 2467 | ep_pcie_release_resources(&ep_pcie_dev); |
| 2468 | ep_pcie_gpio_deinit(&ep_pcie_dev); |
| 2469 | goto irq_failure; |
| 2470 | } |
| 2471 | |
| 2472 | if (ep_pcie_dev.perst_enum && |
| 2473 | !gpio_get_value(ep_pcie_dev.gpio[EP_PCIE_GPIO_PERST].num)) { |
| 2474 | EP_PCIE_DBG2(&ep_pcie_dev, |
| 2475 | "PCIe V%d: %s probe is done; link will be trained when PERST is deasserted.\n", |
| 2476 | ep_pcie_dev.rev, dev_name(&(pdev->dev))); |
| 2477 | return 0; |
| 2478 | } |
| 2479 | |
| 2480 | EP_PCIE_DBG(&ep_pcie_dev, |
| 2481 | "PCIe V%d: %s got resources successfully; start turning on the link.\n", |
| 2482 | ep_pcie_dev.rev, dev_name(&(pdev->dev))); |
| 2483 | |
| 2484 | ret = ep_pcie_enumeration(&ep_pcie_dev); |
| 2485 | |
| 2486 | if (!ret || ep_pcie_debug_keep_resource) |
| 2487 | return 0; |
| 2488 | |
| 2489 | ep_pcie_irq_deinit(&ep_pcie_dev); |
| 2490 | irq_failure: |
| 2491 | ep_pcie_gpio_deinit(&ep_pcie_dev); |
| 2492 | gpio_failure: |
| 2493 | ep_pcie_release_resources(&ep_pcie_dev); |
| 2494 | res_failure: |
| 2495 | EP_PCIE_ERR(&ep_pcie_dev, "PCIe V%d: Driver probe failed:%d\n", |
| 2496 | ep_pcie_dev.rev, ret); |
| 2497 | |
| 2498 | return ret; |
| 2499 | } |
| 2500 | |
| 2501 | static int __exit ep_pcie_remove(struct platform_device *pdev) |
| 2502 | { |
| 2503 | pr_debug("%s\n", __func__); |
| 2504 | |
| 2505 | ep_pcie_irq_deinit(&ep_pcie_dev); |
| 2506 | ep_pcie_vreg_deinit(&ep_pcie_dev); |
| 2507 | ep_pcie_pipe_clk_deinit(&ep_pcie_dev); |
| 2508 | ep_pcie_clk_deinit(&ep_pcie_dev); |
| 2509 | ep_pcie_gpio_deinit(&ep_pcie_dev); |
| 2510 | ep_pcie_release_resources(&ep_pcie_dev); |
| 2511 | ep_pcie_deregister_drv(&hw_drv); |
| 2512 | |
| 2513 | return 0; |
| 2514 | } |
| 2515 | |
| 2516 | static const struct of_device_id ep_pcie_match[] = { |
| 2517 | { .compatible = "qcom,pcie-ep", |
| 2518 | }, |
| 2519 | {} |
| 2520 | }; |
| 2521 | |
| 2522 | static struct platform_driver ep_pcie_driver = { |
| 2523 | .probe = ep_pcie_probe, |
| 2524 | .remove = ep_pcie_remove, |
| 2525 | .driver = { |
| 2526 | .name = "pcie-ep", |
| 2527 | .owner = THIS_MODULE, |
| 2528 | .of_match_table = ep_pcie_match, |
| 2529 | }, |
| 2530 | }; |
| 2531 | |
| 2532 | static int __init ep_pcie_init(void) |
| 2533 | { |
| 2534 | int ret; |
| 2535 | char logname[MAX_NAME_LEN]; |
| 2536 | |
| 2537 | pr_debug("%s\n", __func__); |
| 2538 | |
| 2539 | snprintf(logname, MAX_NAME_LEN, "ep-pcie-long"); |
| 2540 | ep_pcie_dev.ipc_log_sel = |
| 2541 | ipc_log_context_create(EP_PCIE_LOG_PAGES, logname, 0); |
| 2542 | if (ep_pcie_dev.ipc_log_sel == NULL) |
| 2543 | pr_err("%s: unable to create IPC selected log for %s\n", |
| 2544 | __func__, logname); |
| 2545 | else |
| 2546 | EP_PCIE_DBG(&ep_pcie_dev, |
| 2547 | "PCIe V%d: IPC selected logging is enable for %s\n", |
| 2548 | ep_pcie_dev.rev, logname); |
| 2549 | |
| 2550 | snprintf(logname, MAX_NAME_LEN, "ep-pcie-short"); |
| 2551 | ep_pcie_dev.ipc_log_ful = |
| 2552 | ipc_log_context_create(EP_PCIE_LOG_PAGES * 2, logname, 0); |
| 2553 | if (ep_pcie_dev.ipc_log_ful == NULL) |
| 2554 | pr_err("%s: unable to create IPC detailed log for %s\n", |
| 2555 | __func__, logname); |
| 2556 | else |
| 2557 | EP_PCIE_DBG(&ep_pcie_dev, |
| 2558 | "PCIe V%d: IPC detailed logging is enable for %s\n", |
| 2559 | ep_pcie_dev.rev, logname); |
| 2560 | |
| 2561 | snprintf(logname, MAX_NAME_LEN, "ep-pcie-dump"); |
| 2562 | ep_pcie_dev.ipc_log_dump = |
| 2563 | ipc_log_context_create(EP_PCIE_LOG_PAGES, logname, 0); |
| 2564 | if (ep_pcie_dev.ipc_log_dump == NULL) |
| 2565 | pr_err("%s: unable to create IPC dump log for %s\n", |
| 2566 | __func__, logname); |
| 2567 | else |
| 2568 | EP_PCIE_DBG(&ep_pcie_dev, |
| 2569 | "PCIe V%d: IPC dump logging is enable for %s\n", |
| 2570 | ep_pcie_dev.rev, logname); |
| 2571 | |
| 2572 | mutex_init(&ep_pcie_dev.setup_mtx); |
| 2573 | mutex_init(&ep_pcie_dev.ext_mtx); |
| 2574 | spin_lock_init(&ep_pcie_dev.ext_lock); |
| 2575 | spin_lock_init(&ep_pcie_dev.isr_lock); |
| 2576 | |
| 2577 | ep_pcie_debugfs_init(&ep_pcie_dev); |
| 2578 | |
| 2579 | ret = platform_driver_register(&ep_pcie_driver); |
| 2580 | |
| 2581 | if (ret) |
| 2582 | EP_PCIE_ERR(&ep_pcie_dev, |
| 2583 | "PCIe V%d: failed register platform driver:%d\n", |
| 2584 | ep_pcie_dev.rev, ret); |
| 2585 | else |
| 2586 | EP_PCIE_DBG(&ep_pcie_dev, |
| 2587 | "PCIe V%d: platform driver is registered.\n", |
| 2588 | ep_pcie_dev.rev); |
| 2589 | |
| 2590 | return ret; |
| 2591 | } |
| 2592 | |
| 2593 | static void __exit ep_pcie_exit(void) |
| 2594 | { |
| 2595 | pr_debug("%s\n", __func__); |
| 2596 | |
| 2597 | ipc_log_context_destroy(ep_pcie_dev.ipc_log_sel); |
| 2598 | ipc_log_context_destroy(ep_pcie_dev.ipc_log_ful); |
| 2599 | ipc_log_context_destroy(ep_pcie_dev.ipc_log_dump); |
| 2600 | |
| 2601 | ep_pcie_debugfs_exit(); |
| 2602 | |
| 2603 | platform_driver_unregister(&ep_pcie_driver); |
| 2604 | } |
| 2605 | |
| 2606 | module_init(ep_pcie_init); |
| 2607 | module_exit(ep_pcie_exit); |
| 2608 | MODULE_LICENSE("GPL v2"); |
| 2609 | MODULE_DESCRIPTION("MSM PCIe Endpoint Driver"); |