1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * SpacemiT K1 PCIe and PCIe/USB 3 combo PHY driver 4 * 5 * Copyright (C) 2025 by RISCstar Solutions Corporation. All rights reserved. 6 */ 7 8 #include <linux/bitfield.h> 9 #include <linux/clk.h> 10 #include <linux/clk-provider.h> 11 #include <linux/iopoll.h> 12 #include <linux/kernel.h> 13 #include <linux/mfd/syscon.h> 14 #include <linux/module.h> 15 #include <linux/phy/phy.h> 16 #include <linux/platform_device.h> 17 #include <linux/regmap.h> 18 #include <linux/reset.h> 19 20 #include <dt-bindings/phy/phy.h> 21 22 /* 23 * Three PCIe ports are supported in the SpacemiT K1 SoC, and this driver 24 * supports their PHYs. 25 * 26 * The PHY for PCIe port A is different from the PHYs for ports B and C: 27 * - It has one lane, while ports B and C have two 28 * - It is a combo PHY can be used for PCIe or USB 3 29 * - It can automatically calibrate PCIe TX and RX termination settings 30 * 31 * The PHY functionality for PCIe ports B and C is identical: 32 * - They have two PCIe lanes (but can be restricted to 1 via device tree) 33 * - They are used for PCIe only 34 * - They are configured using TX and RX values computed for port A 35 * 36 * A given board is designed to use the combo PHY for either PCIe or USB 3. 37 * Whether the combo PHY is configured for PCIe or USB 3 is specified in 38 * device tree using a phandle plus an argument. The argument indicates 39 * the type (either PHY_TYPE_PCIE or PHY_TYPE_USB3). 40 * 41 * Each PHY has a reset that it gets and deasserts during initialization. 42 * Each depends also on other clocks and resets provided by the controller 43 * hardware (PCIe or USB) it is associated with. The controller drivers 44 * are required to enable any clocks and de-assert any resets that affect 45 * PHY operation. In addition each PHY implements an internal PLL, driven 46 * by an external (24 MHz) oscillator. 47 * 48 * PCIe PHYs must be programmed with RX and TX calibration values. The 49 * combo PHY is the only one that can determine these values. They are 50 * determined by temporarily enabling the combo PHY in PCIe mode at probe 51 * time (if necessary). This calibration only needs to be done once, and 52 * when it has completed the TX and RX values are saved. 53 * 54 * To allow the combo PHY to be enabled for calibration, the resets and 55 * clocks it uses in PCIe mode must be supplied. 56 */ 57 58 struct k1_pcie_phy { 59 struct device *dev; /* PHY provider device */ 60 struct phy *phy; 61 void __iomem *regs; 62 u32 pcie_lanes; /* Max (1 or 2) unless limited by DT */ 63 struct clk *pll; 64 struct clk_hw pll_hw; /* Private PLL clock */ 65 66 /* The remaining fields are only used for the combo PHY */ 67 u32 type; /* PHY_TYPE_PCIE or PHY_TYPE_USB3 */ 68 struct regmap *pmu; /* MMIO regmap (no errors) */ 69 }; 70 71 #define CALIBRATION_TIMEOUT 500000 /* For combo PHY (usec) */ 72 #define PLL_TIMEOUT 500000 /* For PHY PLL lock (usec) */ 73 #define POLL_DELAY 500 /* Time between polls (usec) */ 74 75 /* Selecting the combo PHY operating mode requires APMU regmap access */ 76 #define SYSCON_APMU "spacemit,apmu" 77 78 /* PMU space, for selecting between PCIe and USB 3 mode (combo PHY only) */ 79 80 #define PMUA_USB_PHY_CTRL0 0x0110 81 #define COMBO_PHY_SEL BIT(3) /* 0: PCIe; 1: USB 3 */ 82 83 #define PCIE_CLK_RES_CTRL 0x03cc 84 #define PCIE_APP_HOLD_PHY_RST BIT(30) 85 86 /* PHY register space */ 87 88 /* Offset between lane 0 and lane 1 registers when there are two */ 89 #define PHY_LANE_OFFSET 0x0400 90 91 /* PHY PLL configuration */ 92 #define PCIE_PU_ADDR_CLK_CFG 0x0008 93 #define PLL_READY BIT(0) /* read-only */ 94 #define CFG_INTERNAL_TIMER_ADJ GENMASK(10, 7) 95 #define TIMER_ADJ_USB 0x2 96 #define TIMER_ADJ_PCIE 0x6 97 #define CFG_SW_PHY_INIT_DONE BIT(11) /* We set after PLL config */ 98 99 #define PCIE_RC_DONE_STATUS 0x0018 100 #define CFG_FORCE_RCV_RETRY BIT(10) /* Used for PCIe */ 101 102 /* PCIe PHY lane calibration; assumes 24MHz input clock */ 103 #define PCIE_RC_CAL_REG2 0x0020 104 #define RC_CAL_TOGGLE BIT(22) 105 #define CLKSEL GENMASK(31, 29) 106 #define CLKSEL_24M 0x3 107 108 /* Additional PHY PLL configuration (USB 3 and PCIe) */ 109 #define PCIE_PU_PLL_1 0x0048 110 #define REF_100_WSSC BIT(12) /* 1: input is 100MHz, SSC */ 111 #define FREF_SEL GENMASK(15, 13) 112 #define FREF_24M 0x1 113 #define SSC_DEP_SEL GENMASK(19, 16) 114 #define SSC_DEP_NONE 0x0 115 #define SSC_DEP_5000PPM 0xa 116 117 /* PCIe PHY configuration */ 118 #define PCIE_PU_PLL_2 0x004c 119 #define GEN_REF100 BIT(4) /* 1: generate 100MHz clk */ 120 121 #define PCIE_RX_REG1 0x0050 122 #define EN_RTERM BIT(3) 123 #define AFE_RTERM_REG GENMASK(11, 8) 124 125 #define PCIE_RX_REG2 0x0054 126 #define RX_RTERM_SEL BIT(5) /* 0: use AFE_RTERM_REG value */ 127 128 #define PCIE_LTSSM_DIS_ENTRY 0x005c 129 #define CFG_REFCLK_MODE GENMASK(9, 8) 130 #define RFCLK_MODE_DRIVER 0x1 131 #define OVRD_REFCLK_MODE BIT(10) /* 1: use CFG_RFCLK_MODE */ 132 133 #define PCIE_TX_REG1 0x0064 134 #define TX_RTERM_REG GENMASK(15, 12) 135 #define TX_RTERM_SEL BIT(25) /* 1: use TX_RTERM_REG */ 136 137 /* Zeroed for the combo PHY operating in USB mode */ 138 #define USB3_TEST_CTRL 0x0068 139 140 /* PHY calibration values, determined by the combo PHY at probe time */ 141 #define PCIE_RCAL_RESULT 0x0084 /* Port A PHY only */ 142 #define RTERM_VALUE_RX GENMASK(3, 0) 143 #define RTERM_VALUE_TX GENMASK(7, 4) 144 #define R_TUNE_DONE BIT(10) 145 146 static u32 k1_phy_rterm = ~0; /* Invalid initial value */ 147 148 /* Save the RX and TX receiver termination values */ 149 static void k1_phy_rterm_set(u32 val) 150 { 151 k1_phy_rterm = val & (RTERM_VALUE_RX | RTERM_VALUE_TX); 152 } 153 154 static bool k1_phy_rterm_valid(void) 155 { 156 /* Valid if no bits outside those we care about are set */ 157 return !(k1_phy_rterm & ~(RTERM_VALUE_RX | RTERM_VALUE_TX)); 158 } 159 160 static u32 k1_phy_rterm_rx(void) 161 { 162 return FIELD_GET(RTERM_VALUE_RX, k1_phy_rterm); 163 } 164 165 static u32 k1_phy_rterm_tx(void) 166 { 167 return FIELD_GET(RTERM_VALUE_TX, k1_phy_rterm); 168 } 169 170 /* Only the combo PHY has a PMU pointer defined */ 171 static bool k1_phy_port_a(struct k1_pcie_phy *k1_phy) 172 { 173 return !!k1_phy->pmu; 174 } 175 176 /* The PLL clocks are driven by the external oscillator */ 177 static const struct clk_parent_data k1_pcie_phy_data[] = { 178 { .fw_name = "refclk", }, 179 }; 180 181 static struct k1_pcie_phy *clk_hw_to_k1_phy(struct clk_hw *clk_hw) 182 { 183 return container_of(clk_hw, struct k1_pcie_phy, pll_hw); 184 } 185 186 /* USB mode only works on the combo PHY, which has only one lane */ 187 static void k1_pcie_phy_pll_prepare_usb(struct k1_pcie_phy *k1_phy) 188 { 189 void __iomem *regs = k1_phy->regs; 190 u32 val; 191 192 val = readl(regs + PCIE_PU_ADDR_CLK_CFG); 193 val &= ~CFG_INTERNAL_TIMER_ADJ; 194 val |= FIELD_PREP(CFG_INTERNAL_TIMER_ADJ, TIMER_ADJ_USB); 195 writel(val, regs + PCIE_PU_ADDR_CLK_CFG); 196 197 val = readl(regs + PCIE_PU_PLL_1); 198 val &= ~SSC_DEP_SEL; 199 val |= FIELD_PREP(SSC_DEP_SEL, SSC_DEP_5000PPM); 200 writel(val, regs + PCIE_PU_PLL_1); 201 } 202 203 /* Perform PCIe-specific register updates before starting the PLL clock */ 204 static void k1_pcie_phy_pll_prepare_pcie(struct k1_pcie_phy *k1_phy) 205 { 206 void __iomem *regs = k1_phy->regs; 207 u32 val; 208 u32 i; 209 210 for (i = 0; i < k1_phy->pcie_lanes; i++) { 211 val = readl(regs + PCIE_PU_ADDR_CLK_CFG); 212 val &= ~CFG_INTERNAL_TIMER_ADJ; 213 val |= FIELD_PREP(CFG_INTERNAL_TIMER_ADJ, TIMER_ADJ_PCIE); 214 writel(val, regs + PCIE_PU_ADDR_CLK_CFG); 215 216 regs += PHY_LANE_OFFSET; /* Next lane */ 217 } 218 219 regs = k1_phy->regs; 220 val = readl(regs + PCIE_RC_DONE_STATUS); 221 val |= CFG_FORCE_RCV_RETRY; 222 writel(val, regs + PCIE_RC_DONE_STATUS); 223 224 val = readl(regs + PCIE_PU_PLL_1); 225 val &= ~SSC_DEP_SEL; 226 val |= FIELD_PREP(SSC_DEP_SEL, SSC_DEP_NONE); 227 writel(val, regs + PCIE_PU_PLL_1); 228 229 val = readl(regs + PCIE_PU_PLL_2); 230 val |= GEN_REF100; /* Enable 100 MHz PLL output clock */ 231 writel(val, regs + PCIE_PU_PLL_2); 232 } 233 234 static int k1_pcie_phy_pll_prepare(struct clk_hw *clk_hw) 235 { 236 struct k1_pcie_phy *k1_phy = clk_hw_to_k1_phy(clk_hw); 237 void __iomem *regs = k1_phy->regs; 238 u32 val; 239 u32 i; 240 241 if (k1_phy_port_a(k1_phy) && k1_phy->type == PHY_TYPE_USB3) 242 k1_pcie_phy_pll_prepare_usb(k1_phy); 243 else 244 k1_pcie_phy_pll_prepare_pcie(k1_phy); 245 246 /* 247 * Disable 100 MHz input reference with spread-spectrum 248 * clocking and select the 24 MHz clock input frequency 249 */ 250 val = readl(regs + PCIE_PU_PLL_1); 251 val &= ~REF_100_WSSC; 252 val &= ~FREF_SEL; 253 val |= FIELD_PREP(FREF_SEL, FREF_24M); 254 writel(val, regs + PCIE_PU_PLL_1); 255 256 /* Mark PLL configuration done on all lanes */ 257 for (i = 0; i < k1_phy->pcie_lanes; i++) { 258 val = readl(regs + PCIE_PU_ADDR_CLK_CFG); 259 val |= CFG_SW_PHY_INIT_DONE; 260 writel(val, regs + PCIE_PU_ADDR_CLK_CFG); 261 262 regs += PHY_LANE_OFFSET; /* Next lane */ 263 } 264 265 /* 266 * Wait for indication the PHY PLL is locked. Lanes for ports 267 * B and C share a PLL, so it's enough to sample just lane 0. 268 */ 269 return readl_poll_timeout(k1_phy->regs + PCIE_PU_ADDR_CLK_CFG, 270 val, val & PLL_READY, 271 POLL_DELAY, PLL_TIMEOUT); 272 } 273 274 /* Prepare implies enable, and once enabled, it's always on */ 275 static const struct clk_ops k1_pcie_phy_pll_ops = { 276 .prepare = k1_pcie_phy_pll_prepare, 277 }; 278 279 /* We represent the PHY PLL as a private clock */ 280 static int k1_pcie_phy_pll_setup(struct k1_pcie_phy *k1_phy) 281 { 282 struct clk_hw *hw = &k1_phy->pll_hw; 283 struct device *dev = k1_phy->dev; 284 struct clk_init_data init = { }; 285 char *name; 286 int ret; 287 288 name = kasprintf(GFP_KERNEL, "pcie%u_phy_pll", k1_phy->phy->id); 289 if (!name) 290 return -ENOMEM; 291 292 init.name = name; 293 init.ops = &k1_pcie_phy_pll_ops; 294 init.parent_data = k1_pcie_phy_data; 295 init.num_parents = ARRAY_SIZE(k1_pcie_phy_data); 296 297 hw->init = &init; 298 299 ret = devm_clk_hw_register(dev, hw); 300 301 kfree(name); /* __clk_register() duplicates the name we provide */ 302 303 if (ret) 304 return ret; 305 306 k1_phy->pll = devm_clk_hw_get_clk(dev, hw, "pll"); 307 if (IS_ERR(k1_phy->pll)) 308 return PTR_ERR(k1_phy->pll); 309 310 return 0; 311 } 312 313 /* Select PCIe or USB 3 mode for the combo PHY. */ 314 static void k1_combo_phy_sel(struct k1_pcie_phy *k1_phy, bool usb) 315 { 316 struct regmap *pmu = k1_phy->pmu; 317 318 /* Only change it if it's not already in the desired state */ 319 if (!regmap_test_bits(pmu, PMUA_USB_PHY_CTRL0, COMBO_PHY_SEL) == usb) 320 regmap_assign_bits(pmu, PMUA_USB_PHY_CTRL0, COMBO_PHY_SEL, usb); 321 } 322 323 static void k1_pcie_phy_init_pcie(struct k1_pcie_phy *k1_phy) 324 { 325 u32 rx_rterm = k1_phy_rterm_rx(); 326 u32 tx_rterm = k1_phy_rterm_tx(); 327 void __iomem *regs; 328 u32 val; 329 int i; 330 331 /* For the combo PHY, set PHY to PCIe mode */ 332 if (k1_phy_port_a(k1_phy)) 333 k1_combo_phy_sel(k1_phy, false); 334 335 regs = k1_phy->regs; 336 for (i = 0; i < k1_phy->pcie_lanes; i++) { 337 val = readl(regs + PCIE_RX_REG1); 338 339 /* Set RX analog front-end receiver termination value */ 340 val &= ~AFE_RTERM_REG; 341 val |= FIELD_PREP(AFE_RTERM_REG, rx_rterm); 342 343 /* And enable refclock receiver termination */ 344 val |= EN_RTERM; 345 writel(val, regs + PCIE_RX_REG1); 346 347 val = readl(regs + PCIE_RX_REG2); 348 /* Use PCIE_RX_REG1 AFE_RTERM_REG value */ 349 val &= ~RX_RTERM_SEL; 350 writel(val, regs + PCIE_RX_REG2); 351 352 val = readl(regs + PCIE_TX_REG1); 353 354 /* Set TX driver termination value */ 355 val &= ~TX_RTERM_REG; 356 val |= FIELD_PREP(TX_RTERM_REG, tx_rterm); 357 358 /* Use PCIE_TX_REG1 TX_RTERM_REG value */ 359 val |= TX_RTERM_SEL; 360 writel(val, regs + PCIE_TX_REG1); 361 362 /* Set the input clock to 24 MHz, and clear RC_CAL_TOGGLE */ 363 val = readl(regs + PCIE_RC_CAL_REG2); 364 val &= CLKSEL; 365 val |= FIELD_PREP(CLKSEL, CLKSEL_24M); 366 val &= ~RC_CAL_TOGGLE; 367 writel(val, regs + PCIE_RC_CAL_REG2); 368 369 /* Now trigger recalibration by setting RC_CAL_TOGGLE again */ 370 val |= RC_CAL_TOGGLE; 371 writel(val, regs + PCIE_RC_CAL_REG2); 372 373 val = readl(regs + PCIE_LTSSM_DIS_ENTRY); 374 /* Override the reference clock; set to refclk driver mode */ 375 val |= OVRD_REFCLK_MODE; 376 val &= ~CFG_REFCLK_MODE; 377 val |= FIELD_PREP(CFG_REFCLK_MODE, RFCLK_MODE_DRIVER); 378 writel(val, regs + PCIE_LTSSM_DIS_ENTRY); 379 380 regs += PHY_LANE_OFFSET; /* Next lane */ 381 } 382 } 383 384 /* Only called for combo PHY */ 385 static void k1_pcie_phy_init_usb(struct k1_pcie_phy *k1_phy) 386 { 387 k1_combo_phy_sel(k1_phy, true); 388 389 /* We're not doing any testing */ 390 writel(0, k1_phy->regs + USB3_TEST_CTRL); 391 } 392 393 static int k1_pcie_phy_init(struct phy *phy) 394 { 395 struct k1_pcie_phy *k1_phy = phy_get_drvdata(phy); 396 397 /* Note: port type is only valid for port A (both checks needed) */ 398 if (k1_phy_port_a(k1_phy) && k1_phy->type == PHY_TYPE_USB3) 399 k1_pcie_phy_init_usb(k1_phy); 400 else 401 k1_pcie_phy_init_pcie(k1_phy); 402 403 404 return clk_prepare_enable(k1_phy->pll); 405 } 406 407 static int k1_pcie_phy_exit(struct phy *phy) 408 { 409 struct k1_pcie_phy *k1_phy = phy_get_drvdata(phy); 410 411 clk_disable_unprepare(k1_phy->pll); 412 413 return 0; 414 } 415 416 static const struct phy_ops k1_pcie_phy_ops = { 417 .init = k1_pcie_phy_init, 418 .exit = k1_pcie_phy_exit, 419 .owner = THIS_MODULE, 420 }; 421 422 /* 423 * Get values needed for calibrating PHYs operating in PCIe mode. Only 424 * the combo PHY is able to do this, and its calibration values are used 425 * for configuring all PCIe PHYs. 426 * 427 * We always need to de-assert the "global" reset on the combo PHY, 428 * because the USB driver depends on it. If used for PCIe, that driver 429 * will (also) de-assert this, but by leaving it de-asserted for the 430 * combo PHY, the USB driver doesn't have to do this. Note: although 431 * SpacemiT refers to this as the global reset, we name the "phy" reset. 432 * 433 * In addition, we guarantee the APP_HOLD_PHY_RESET bit is clear for the 434 * combo PHY, so the USB driver doesn't have to manage that either. The 435 * PCIe driver is free to change this bit for normal operation. 436 * 437 * Calibration only needs to be done once. It's possible calibration has 438 * already completed (e.g., it might have happened in the boot loader, or 439 * -EPROBE_DEFER might result in this function being called again). So we 440 * check that early too, to avoid doing it more than once. 441 * 442 * Otherwise we temporarily power up the PHY using the PCIe app clocks 443 * and resets, wait for the hardware to indicate calibration is done, 444 * grab the value, then shut the PHY down again. 445 */ 446 static int k1_pcie_combo_phy_calibrate(struct k1_pcie_phy *k1_phy) 447 { 448 struct reset_control_bulk_data resets[] = { 449 { .id = "dbi", }, 450 { .id = "mstr", }, 451 { .id = "slv", }, 452 }; 453 struct clk_bulk_data clocks[] = { 454 { .id = "dbi", }, 455 { .id = "mstr", }, 456 { .id = "slv", }, 457 }; 458 struct device *dev = k1_phy->dev; 459 int ret = 0; 460 int val; 461 462 /* Nothing to do if we already set the receiver termination value */ 463 if (k1_phy_rterm_valid()) 464 return 0; 465 466 /* 467 * We also guarantee the APP_HOLD_PHY_RESET bit is clear. We can 468 * leave this bit clear even if an error happens below. 469 */ 470 regmap_assign_bits(k1_phy->pmu, PCIE_CLK_RES_CTRL, 471 PCIE_APP_HOLD_PHY_RST, false); 472 473 /* If the calibration already completed (e.g. by U-Boot), we're done */ 474 val = readl(k1_phy->regs + PCIE_RCAL_RESULT); 475 if (val & R_TUNE_DONE) 476 goto out_tune_done; 477 478 /* Put the PHY into PCIe mode */ 479 k1_combo_phy_sel(k1_phy, false); 480 481 /* Get and enable the PCIe app clocks */ 482 ret = clk_bulk_get(dev, ARRAY_SIZE(clocks), clocks); 483 if (ret < 0) 484 goto out_tune_done; 485 ret = clk_bulk_prepare_enable(ARRAY_SIZE(clocks), clocks); 486 if (ret) 487 goto out_put_clocks; 488 489 /* Get the PCIe application resets (not the PHY reset) */ 490 ret = reset_control_bulk_get_shared(dev, ARRAY_SIZE(resets), resets); 491 if (ret) 492 goto out_disable_clocks; 493 494 /* De-assert the PCIe application resets */ 495 ret = reset_control_bulk_deassert(ARRAY_SIZE(resets), resets); 496 if (ret) 497 goto out_put_resets; 498 499 /* 500 * This is the core activity here. Wait for the hardware to 501 * signal that it has completed calibration/tuning. Once it 502 * has, the register value will contain the values we'll 503 * use to configure PCIe PHYs. 504 */ 505 ret = readl_poll_timeout(k1_phy->regs + PCIE_RCAL_RESULT, 506 val, val & R_TUNE_DONE, 507 POLL_DELAY, CALIBRATION_TIMEOUT); 508 509 /* Clean up. We're done with the resets and clocks */ 510 reset_control_bulk_assert(ARRAY_SIZE(resets), resets); 511 out_put_resets: 512 reset_control_bulk_put(ARRAY_SIZE(resets), resets); 513 out_disable_clocks: 514 clk_bulk_disable_unprepare(ARRAY_SIZE(clocks), clocks); 515 out_put_clocks: 516 clk_bulk_put(ARRAY_SIZE(clocks), clocks); 517 out_tune_done: 518 /* If we got the value without timing out, set k1_phy_rterm */ 519 if (!ret) 520 k1_phy_rterm_set(val); 521 522 return ret; 523 } 524 525 static struct phy * 526 k1_pcie_combo_phy_xlate(struct device *dev, const struct of_phandle_args *args) 527 { 528 struct k1_pcie_phy *k1_phy = dev_get_drvdata(dev); 529 u32 type; 530 531 /* The argument specifying the PHY mode is required */ 532 if (args->args_count != 1) 533 return ERR_PTR(-EINVAL); 534 535 /* We only support PCIe and USB 3 mode */ 536 type = args->args[0]; 537 if (type != PHY_TYPE_PCIE && type != PHY_TYPE_USB3) 538 return ERR_PTR(-EINVAL); 539 540 /* This PHY can only be used once */ 541 if (k1_phy->type != PHY_NONE) 542 return ERR_PTR(-EBUSY); 543 544 k1_phy->type = type; 545 546 return k1_phy->phy; 547 } 548 549 /* Use the maximum number of PCIe lanes unless limited by device tree */ 550 static u32 k1_pcie_num_lanes(struct k1_pcie_phy *k1_phy, bool port_a) 551 { 552 struct device *dev = k1_phy->dev; 553 u32 count = 0; 554 u32 max; 555 int ret; 556 557 ret = of_property_read_u32(dev_of_node(dev), "num-lanes", &count); 558 if (count == 1) 559 return 1; 560 561 if (count == 2 && !port_a) 562 return 2; 563 564 max = port_a ? 1 : 2; 565 if (ret != -EINVAL) 566 dev_warn(dev, "bad lane count %u for port; using %u\n", 567 count, max); 568 569 return max; 570 } 571 572 static int k1_pcie_combo_phy_probe(struct k1_pcie_phy *k1_phy) 573 { 574 struct device *dev = k1_phy->dev; 575 struct regmap *regmap; 576 int ret; 577 578 /* Setting the PHY mode requires access to the PMU regmap */ 579 regmap = syscon_regmap_lookup_by_phandle(dev_of_node(dev), SYSCON_APMU); 580 if (IS_ERR(regmap)) 581 return dev_err_probe(dev, PTR_ERR(regmap), "failed to get PMU\n"); 582 k1_phy->pmu = regmap; 583 584 ret = k1_pcie_combo_phy_calibrate(k1_phy); 585 if (ret) 586 return dev_err_probe(dev, ret, "calibration failed\n"); 587 588 /* Needed by k1_pcie_combo_phy_xlate(), which also sets k1_phy->type */ 589 dev_set_drvdata(dev, k1_phy); 590 591 return 0; 592 } 593 594 static int k1_pcie_phy_probe(struct platform_device *pdev) 595 { 596 struct phy *(*xlate)(struct device *dev, 597 const struct of_phandle_args *args); 598 struct device *dev = &pdev->dev; 599 struct reset_control *phy_reset; 600 struct phy_provider *provider; 601 struct k1_pcie_phy *k1_phy; 602 bool probing_port_a; 603 int ret; 604 605 xlate = of_device_get_match_data(dev); 606 probing_port_a = xlate == k1_pcie_combo_phy_xlate; 607 608 /* Only the combo PHY can calibrate, so it must probe first */ 609 if (!k1_phy_rterm_valid() && !probing_port_a) 610 return -EPROBE_DEFER; 611 612 k1_phy = devm_kzalloc(dev, sizeof(*k1_phy), GFP_KERNEL); 613 if (!k1_phy) 614 return -ENOMEM; 615 k1_phy->dev = dev; 616 617 k1_phy->regs = devm_platform_ioremap_resource(pdev, 0); 618 if (IS_ERR(k1_phy->regs)) 619 return dev_err_probe(dev, PTR_ERR(k1_phy->regs), 620 "error mapping registers\n"); 621 622 /* De-assert the PHY (global) reset and leave it that way */ 623 phy_reset = devm_reset_control_get_exclusive_deasserted(dev, "phy"); 624 if (IS_ERR(phy_reset)) 625 return PTR_ERR(phy_reset); 626 627 if (probing_port_a) { 628 ret = k1_pcie_combo_phy_probe(k1_phy); 629 if (ret) 630 return dev_err_probe(dev, ret, 631 "error probing combo phy\n"); 632 } 633 634 k1_phy->pcie_lanes = k1_pcie_num_lanes(k1_phy, probing_port_a); 635 636 k1_phy->phy = devm_phy_create(dev, NULL, &k1_pcie_phy_ops); 637 if (IS_ERR(k1_phy->phy)) 638 return dev_err_probe(dev, PTR_ERR(k1_phy->phy), 639 "error creating phy\n"); 640 phy_set_drvdata(k1_phy->phy, k1_phy); 641 642 ret = k1_pcie_phy_pll_setup(k1_phy); 643 if (ret) 644 return dev_err_probe(dev, ret, "error initializing clock\n"); 645 646 provider = devm_of_phy_provider_register(dev, xlate); 647 if (IS_ERR(provider)) 648 return dev_err_probe(dev, PTR_ERR(provider), 649 "error registering provider\n"); 650 return 0; 651 } 652 653 static const struct of_device_id k1_pcie_phy_of_match[] = { 654 { .compatible = "spacemit,k1-combo-phy", k1_pcie_combo_phy_xlate, }, 655 { .compatible = "spacemit,k1-pcie-phy", of_phy_simple_xlate, }, 656 { }, 657 }; 658 MODULE_DEVICE_TABLE(of, k1_pcie_phy_of_match); 659 660 static struct platform_driver k1_pcie_phy_driver = { 661 .probe = k1_pcie_phy_probe, 662 .driver = { 663 .of_match_table = k1_pcie_phy_of_match, 664 .name = "spacemit-k1-pcie-phy", 665 } 666 }; 667 module_platform_driver(k1_pcie_phy_driver); 668 669 MODULE_DESCRIPTION("SpacemiT K1 PCIe and USB 3 PHY driver"); 670 MODULE_LICENSE("GPL"); 671