Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

phy: miphy365x: Represent each PHY channel as a DT subnode

This has the added advantages of being able to enable/disable each
of the channels as simply as enabling/disabling the DT node.

Suggested-by: Kishon Vijay Abraham I <kishon@ti.com>
Signed-off-by: Lee Jones <lee.jones@linaro.org>
Signed-off-by: Kishon Vijay Abraham I <kishon@ti.com>

authored by

Lee Jones and committed by
Kishon Vijay Abraham I
7ebdb52e 6e877fed

+138 -118
+138 -118
drivers/phy/phy-miphy365x.c
··· 18 18 #include <linux/module.h> 19 19 #include <linux/of.h> 20 20 #include <linux/of_platform.h> 21 + #include <linux/of_address.h> 21 22 #include <linux/clk.h> 22 23 #include <linux/phy/phy.h> 23 24 #include <linux/delay.h> ··· 29 28 30 29 #define HFC_TIMEOUT 100 31 30 32 - #define SYSCFG_2521 0x824 33 - #define SYSCFG_2522 0x828 34 - #define SYSCFG_PCIE_SATA_MASK BIT(1) 35 - #define SYSCFG_PCIE_SATA_POS 1 31 + #define SYSCFG_SELECT_SATA_MASK BIT(1) 32 + #define SYSCFG_SELECT_SATA_POS 1 36 33 37 34 /* MiPHY365x register definitions */ 38 35 #define RESET_REG 0x00 ··· 135 136 #define BIT_LOCK_LEVEL 0x01 136 137 #define BIT_LOCK_CNT_512 (0x03 << 5) 137 138 138 - static u8 ports[] = { MIPHY_PORT_0, MIPHY_PORT_1 }; 139 - 140 139 struct miphy365x_phy { 141 140 struct phy *phy; 142 141 void __iomem *base; 143 - void __iomem *sata; 144 - void __iomem *pcie; 142 + bool pcie_tx_pol_inv; 143 + bool sata_tx_pol_inv; 144 + u32 sata_gen; 145 + u64 ctrlreg; 145 146 u8 type; 146 - u8 port; 147 147 }; 148 148 149 149 struct miphy365x_dev { 150 150 struct device *dev; 151 151 struct regmap *regmap; 152 152 struct mutex miphy_mutex; 153 - struct miphy365x phys[ARRAY_SIZE(ports)]; 154 - bool pcie_tx_pol_inv; 155 - bool sata_tx_pol_inv; 156 - u32 sata_gen; 153 + struct miphy365x_phy **phys; 157 154 }; 158 155 159 156 /* ··· 175 180 static int miphy365x_set_path(struct miphy365x_phy *miphy_phy, 176 181 struct miphy365x_dev *miphy_dev) 177 182 { 178 - u8 config = miphy_phy->type | miphy_phy->port; 179 - u32 mask = SYSCFG_PCIE_SATA_MASK; 180 - u32 reg; 181 - bool sata; 183 + bool sata = (miphy_phy->type == MIPHY_TYPE_SATA); 182 184 183 - switch (config) { 184 - case MIPHY_SATA_PORT0: 185 - reg = SYSCFG_2521; 186 - sata = true; 187 - break; 188 - case MIPHY_PCIE_PORT1: 189 - reg = SYSCFG_2522; 190 - sata = false; 191 - break; 192 - default: 193 - dev_err(miphy_dev->dev, "Configuration not supported\n"); 194 - return -EINVAL; 195 - } 196 - 197 - return regmap_update_bits(miphy_dev->regmap, reg, mask, 198 - sata << SYSCFG_PCIE_SATA_POS); 185 + return regmap_update_bits(miphy_dev->regmap, 186 + (unsigned int)miphy_phy->ctrlreg, 187 + SYSCFG_SELECT_SATA_MASK, 188 + sata << SYSCFG_SELECT_SATA_POS); 199 189 } 200 190 201 191 static int miphy365x_init_pcie_port(struct miphy365x_phy *miphy_phy, ··· 241 261 { 242 262 u8 val, mask; 243 263 244 - if (miphy_dev->sata_gen == SATA_GEN1) 264 + if (miphy_phy->sata_gen == SATA_GEN1) 245 265 writeb_relaxed(COMP_2MHZ_RAT_GEN1, 246 266 miphy_phy->base + COMP_CTRL2_REG); 247 267 else 248 268 writeb_relaxed(COMP_2MHZ_RAT, 249 269 miphy_phy->base + COMP_CTRL2_REG); 250 270 251 - if (miphy_dev->sata_gen != SATA_GEN3) { 271 + if (miphy_phy->sata_gen != SATA_GEN3) { 252 272 writeb_relaxed(COMSR_COMP_REF, 253 273 miphy_phy->base + COMP_CTRL3_REG); 254 274 /* ··· 292 312 miphy_phy->base + PLL_SSC_PER_LSB_REG); 293 313 294 314 /* SSC Settings complete */ 295 - if (miphy_dev->sata_gen == SATA_GEN1) { 315 + if (miphy_phy->sata_gen == SATA_GEN1) { 296 316 val = PLL_START_CAL | BUF_EN | SYNCHRO_TX | CONFIG_PLL; 297 317 writeb_relaxed(val, miphy_phy->base + PLL_CTRL1_REG); 298 318 } else { ··· 314 334 val = RST_PLL | RST_PLL_CAL | RST_RX | RST_MACRO; 315 335 writeb_relaxed(val, miphy_phy->base + RESET_REG); 316 336 317 - if (miphy_dev->sata_tx_pol_inv) 337 + if (miphy_phy->sata_tx_pol_inv) 318 338 writeb_relaxed(TX_POL, miphy_phy->base + CTRL_REG); 319 339 320 340 /* ··· 324 344 */ 325 345 writeb_relaxed(SPDSEL_SEL, miphy_phy->base + BOUNDARY1_REG); 326 346 writeb_relaxed(START_CLK_HF, miphy_phy->base + IDLL_TEST_REG); 327 - val = rx_tx_spd[miphy_dev->sata_gen]; 347 + val = rx_tx_spd[miphy_phy->sata_gen]; 328 348 writeb_relaxed(val, miphy_phy->base + BOUNDARY3_REG); 329 349 330 350 /* Wait for HFC_READY = 0 */ ··· 335 355 /* Compensation Recalibration */ 336 356 miphy365x_set_comp(miphy_phy, miphy_dev); 337 357 338 - switch (miphy_dev->sata_gen) { 358 + switch (miphy_phy->sata_gen) { 339 359 case SATA_GEN3: 340 360 /* 341 361 * TX Swing target 550-600mv peak to peak diff ··· 403 423 writeb_relaxed(0x00, miphy_phy->base + BOUNDARY1_REG); 404 424 writeb_relaxed(0x00, miphy_phy->base + IDLL_TEST_REG); 405 425 writeb_relaxed(RST_RX, miphy_phy->base + RESET_REG); 406 - val = miphy_dev->sata_tx_pol_inv ? 426 + val = miphy_phy->sata_tx_pol_inv ? 407 427 (TX_POL | DES_BIT_LOCK_EN) : DES_BIT_LOCK_EN; 408 428 writeb_relaxed(val, miphy_phy->base + CTRL_REG); 409 429 ··· 439 459 return ret; 440 460 } 441 461 462 + int miphy365x_get_addr(struct device *dev, struct miphy365x_phy *miphy_phy, 463 + int index) 464 + { 465 + struct device_node *phynode = miphy_phy->phy->dev.of_node; 466 + const char *name; 467 + const __be32 *taddr; 468 + int type = miphy_phy->type; 469 + int ret; 470 + 471 + ret = of_property_read_string_index(phynode, "reg-names", index, &name); 472 + if (ret) { 473 + dev_err(dev, "no reg-names property not found\n"); 474 + return ret; 475 + } 476 + 477 + if (!strncmp(name, "syscfg", 6)) { 478 + taddr = of_get_address(phynode, index, NULL, NULL); 479 + if (!taddr) { 480 + dev_err(dev, "failed to fetch syscfg address\n"); 481 + return -EINVAL; 482 + } 483 + 484 + miphy_phy->ctrlreg = of_translate_address(phynode, taddr); 485 + if (miphy_phy->ctrlreg == OF_BAD_ADDR) { 486 + dev_err(dev, "failed to translate syscfg address\n"); 487 + return -EINVAL; 488 + } 489 + 490 + return 0; 491 + } 492 + 493 + if (!((!strncmp(name, "sata", 4) && type == MIPHY_TYPE_SATA) || 494 + (!strncmp(name, "pcie", 4) && type == MIPHY_TYPE_PCIE))) 495 + return 0; 496 + 497 + miphy_phy->base = of_iomap(phynode, index); 498 + if (!miphy_phy->base) { 499 + dev_err(dev, "Failed to map %s\n", phynode->full_name); 500 + return -EINVAL; 501 + } 502 + 503 + return 0; 504 + } 505 + 442 506 static struct phy *miphy365x_xlate(struct device *dev, 443 507 struct of_phandle_args *args) 444 508 { 445 - struct miphy365x_dev *state = dev_get_drvdata(dev); 446 - u8 port, type; 509 + struct miphy365x_dev *miphy_dev = dev_get_drvdata(dev); 510 + struct miphy365x_phy *miphy_phy = NULL; 511 + struct device_node *phynode = args->np; 512 + int ret, index; 447 513 448 - if (args->count != 2) { 514 + if (!of_device_is_available(phynode)) { 515 + dev_warn(dev, "Requested PHY is disabled\n"); 516 + return ERR_PTR(-ENODEV); 517 + } 518 + 519 + if (args->args_count != 1) { 449 520 dev_err(dev, "Invalid number of cells in 'phy' property\n"); 450 521 return ERR_PTR(-EINVAL); 451 522 } 452 523 453 - if (args->args[0] & 0xFFFFFF00 || args->args[1] & 0xFFFFFF00) { 454 - dev_err(dev, "Unsupported flags set in 'phy' property\n"); 524 + for (index = 0; index < of_get_child_count(dev->of_node); index++) 525 + if (phynode == miphy_dev->phys[index]->phy->dev.of_node) { 526 + miphy_phy = miphy_dev->phys[index]; 527 + break; 528 + } 529 + 530 + if (!miphy_phy) { 531 + dev_err(dev, "Failed to find appropriate phy\n"); 455 532 return ERR_PTR(-EINVAL); 456 533 } 457 534 458 - port = args->args[0]; 459 - type = args->args[1]; 535 + miphy_phy->type = args->args[0]; 460 536 461 - if (WARN_ON(port >= ARRAY_SIZE(ports))) 462 - return ERR_PTR(-EINVAL); 463 - 464 - if (type == MIPHY_TYPE_SATA) 465 - state->phys[port].base = state->phys[port].sata; 466 - else if (type == MIPHY_TYPE_PCIE) 467 - state->phys[port].base = state->phys[port].pcie; 468 - else { 469 - WARN(1, "Invalid type specified in DT"); 537 + if (!(miphy_phy->type == MIPHY_TYPE_SATA || 538 + miphy_phy->type == MIPHY_TYPE_PCIE)) { 539 + dev_err(dev, "Unsupported device type: %d\n", miphy_phy->type); 470 540 return ERR_PTR(-EINVAL); 471 541 } 472 542 473 - state->phys[port].type = type; 543 + /* Each port handles SATA and PCIE - third entry is always sysconf. */ 544 + for (index = 0; index < 3; index++) { 545 + ret = miphy365x_get_addr(dev, miphy_phy, index); 546 + if (ret < 0) 547 + return ERR_PTR(ret); 548 + } 474 549 475 - return state->phys[port].phy; 550 + return miphy_phy->phy; 476 551 } 477 552 478 553 static struct phy_ops miphy365x_ops = { ··· 535 500 .owner = THIS_MODULE, 536 501 }; 537 502 538 - static int miphy365x_get_base_addr(struct platform_device *pdev, 539 - struct miphy365x_phy *phy, u8 port) 503 + static int miphy365x_of_probe(struct device_node *phynode, 504 + struct miphy365x_phy *miphy_phy) 540 505 { 541 - struct resource *res; 542 - char type[6]; 506 + of_property_read_u32(phynode, "st,sata-gen", &miphy_phy->sata_gen); 507 + if (!miphy_phy->sata_gen) 508 + miphy_phy->sata_gen = SATA_GEN1; 543 509 544 - sprintf(type, "sata%d", port); 510 + miphy_phy->pcie_tx_pol_inv = 511 + of_property_read_bool(phynode, "st,pcie-tx-pol-inv"); 545 512 546 - res = platform_get_resource_byname(pdev, IORESOURCE_MEM, type); 547 - phy->sata = devm_ioremap_resource(&pdev->dev, res)); 548 - if (!phy->sata) 549 - return -ENOMEM; 550 - 551 - sprintf(type, "pcie%d", port); 552 - 553 - res = platform_get_resource_byname(pdev, IORESOURCE_MEM, type); 554 - phy->pcie = devm_ioremap_resource(&pdev->dev, res)); 555 - if (!phy->pcie) 556 - return -ENOMEM; 557 - 558 - return 0; 559 - } 560 - 561 - static int miphy365x_of_probe(struct device_node *np, 562 - struct miphy365x_dev *phy_dev) 563 - { 564 - phy_dev->regmap = syscon_regmap_lookup_by_phandle(np, "st,syscfg"); 565 - if (IS_ERR(phy_dev->regmap)) { 566 - dev_err(phy_dev->dev, "No syscfg phandle specified\n"); 567 - return PTR_ERR(phy_dev->regmap); 568 - } 569 - 570 - of_property_read_u32(np, "st,sata-gen", &phy_dev->sata_gen); 571 - if (!phy_dev->sata_gen) 572 - phy_dev->sata_gen = SATA_GEN1; 573 - 574 - phy_dev->pcie_tx_pol_inv = 575 - of_property_read_bool(np, "st,pcie-tx-pol-inv"); 576 - 577 - phy_dev->sata_tx_pol_inv = 578 - of_property_read_bool(np, "st,sata-tx-pol-inv"); 513 + miphy_phy->sata_tx_pol_inv = 514 + of_property_read_bool(phynode, "st,sata-tx-pol-inv"); 579 515 580 516 return 0; 581 517 } 582 518 583 519 static int miphy365x_probe(struct platform_device *pdev) 584 520 { 585 - struct device_node *np = pdev->dev.of_node; 586 - struct miphy365x_dev *phy_dev; 587 - struct device *dev = &pdev->dev; 521 + struct device_node *child, *np = pdev->dev.of_node; 522 + struct miphy365x_dev *miphy_dev; 588 523 struct phy_provider *provider; 589 - u8 port; 524 + struct phy *phy; 525 + int chancount, port = 0; 590 526 int ret; 591 527 592 - phy_dev = devm_kzalloc(dev, sizeof(*phy_dev), GFP_KERNEL); 593 - if (!phy_dev) 528 + miphy_dev = devm_kzalloc(&pdev->dev, sizeof(*miphy_dev), GFP_KERNEL); 529 + if (!miphy_dev) 594 530 return -ENOMEM; 595 531 596 - ret = miphy365x_of_probe(np, phy_dev); 597 - if (ret) 598 - return ret; 532 + chancount = of_get_child_count(np); 533 + miphy_dev->phys = devm_kzalloc(&pdev->dev, sizeof(phy) * chancount, 534 + GFP_KERNEL); 535 + if (!miphy_dev->phys) 536 + return -ENOMEM; 599 537 600 - phy_dev->dev = dev; 538 + miphy_dev->regmap = syscon_regmap_lookup_by_phandle(np, "st,syscfg"); 539 + if (IS_ERR(miphy_dev->regmap)) { 540 + dev_err(miphy_dev->dev, "No syscfg phandle specified\n"); 541 + return PTR_ERR(miphy_dev->regmap); 542 + } 601 543 602 - dev_set_drvdata(dev, phy_dev); 544 + miphy_dev->dev = &pdev->dev; 603 545 604 - mutex_init(&phy_dev->miphy_mutex); 546 + dev_set_drvdata(&pdev->dev, miphy_dev); 605 547 606 - for (port = 0; port < ARRAY_SIZE(ports); port++) { 607 - struct phy *phy; 548 + mutex_init(&miphy_dev->miphy_mutex); 608 549 609 - phy = devm_phy_create(dev, &miphy365x_ops, NULL); 550 + for_each_child_of_node(np, child) { 551 + struct miphy365x_phy *miphy_phy; 552 + 553 + miphy_phy = devm_kzalloc(&pdev->dev, sizeof(*miphy_phy), 554 + GFP_KERNEL); 555 + if (!miphy_phy) 556 + return -ENOMEM; 557 + 558 + miphy_dev->phys[port] = miphy_phy; 559 + 560 + phy = devm_phy_create(&pdev->dev, child, &miphy365x_ops, NULL); 610 561 if (IS_ERR(phy)) { 611 - dev_err(dev, "failed to create PHY on port %d\n", port); 562 + dev_err(&pdev->dev, "failed to create PHY\n"); 612 563 return PTR_ERR(phy); 613 564 } 614 565 615 - phy_dev->phys[port].phy = phy; 616 - phy_dev->phys[port].port = port; 566 + miphy_dev->phys[port]->phy = phy; 617 567 618 - ret = miphy365x_get_base_addr(pdev, 619 - &phy_dev->phys[port], port); 568 + ret = miphy365x_of_probe(child, miphy_phy); 620 569 if (ret) 621 570 return ret; 622 571 623 - phy_set_drvdata(phy, &phy_dev->phys[port]); 572 + phy_set_drvdata(phy, miphy_dev->phys[port]); 573 + port++; 624 574 } 625 575 626 - provider = devm_of_phy_provider_register(dev, miphy365x_xlate); 576 + provider = devm_of_phy_provider_register(&pdev->dev, miphy365x_xlate); 627 577 if (IS_ERR(provider)) 628 578 return PTR_ERR(provider); 629 579