1 // SPDX-License-Identifier: GPL-2.0-only 2 /******************************************************************************* 3 This contains the functions to handle the platform driver. 4 5 Copyright (C) 2007-2011 STMicroelectronics Ltd 6 7 8 Author: Giuseppe Cavallaro <peppe.cavallaro@st.com> 9 *******************************************************************************/ 10 11 #include <linux/device.h> 12 #include <linux/platform_device.h> 13 #include <linux/pm_runtime.h> 14 #include <linux/module.h> 15 #include <linux/io.h> 16 #include <linux/of.h> 17 #include <linux/of_net.h> 18 #include <linux/of_device.h> 19 #include <linux/of_mdio.h> 20 21 #include "stmmac.h" 22 #include "stmmac_platform.h" 23 24 #ifdef CONFIG_OF 25 26 /** 27 * dwmac1000_validate_mcast_bins - validates the number of Multicast filter bins 28 * @dev: struct device of the platform device 29 * @mcast_bins: Multicast filtering bins 30 * Description: 31 * this function validates the number of Multicast filtering bins specified 32 * by the configuration through the device tree. The Synopsys GMAC supports 33 * 64 bins, 128 bins, or 256 bins. "bins" refer to the division of CRC 34 * number space. 64 bins correspond to 6 bits of the CRC, 128 corresponds 35 * to 7 bits, and 256 refers to 8 bits of the CRC. Any other setting is 36 * invalid and will cause the filtering algorithm to use Multicast 37 * promiscuous mode. 38 */ 39 static int dwmac1000_validate_mcast_bins(struct device *dev, int mcast_bins) 40 { 41 int x = mcast_bins; 42 43 switch (x) { 44 case HASH_TABLE_SIZE: 45 case 128: 46 case 256: 47 break; 48 default: 49 x = 0; 50 dev_info(dev, "Hash table entries set to unexpected value %d\n", 51 mcast_bins); 52 break; 53 } 54 return x; 55 } 56 57 /** 58 * dwmac1000_validate_ucast_entries - validate the Unicast address entries 59 * @dev: struct device of the platform device 60 * @ucast_entries: number of Unicast address entries 61 * Description: 62 * This function validates the number of Unicast address entries supported 63 * by a particular Synopsys 10/100/1000 controller. The Synopsys controller 64 * supports 1..32, 64, or 128 Unicast filter entries for it's Unicast filter 65 * logic. This function validates a valid, supported configuration is 66 * selected, and defaults to 1 Unicast address if an unsupported 67 * configuration is selected. 68 */ 69 static int dwmac1000_validate_ucast_entries(struct device *dev, 70 int ucast_entries) 71 { 72 int x = ucast_entries; 73 74 switch (x) { 75 case 1 ... 32: 76 case 64: 77 case 128: 78 break; 79 default: 80 x = 1; 81 dev_info(dev, "Unicast table entries set to unexpected value %d\n", 82 ucast_entries); 83 break; 84 } 85 return x; 86 } 87 88 /** 89 * stmmac_axi_setup - parse DT parameters for programming the AXI register 90 * @pdev: platform device 91 * Description: 92 * if required, from device-tree the AXI internal register can be tuned 93 * by using platform parameters. 94 */ 95 static struct stmmac_axi *stmmac_axi_setup(struct platform_device *pdev) 96 { 97 struct device_node *np; 98 struct stmmac_axi *axi; 99 100 np = of_parse_phandle(pdev->dev.of_node, "snps,axi-config", 0); 101 if (!np) 102 return NULL; 103 104 axi = devm_kzalloc(&pdev->dev, sizeof(*axi), GFP_KERNEL); 105 if (!axi) { 106 of_node_put(np); 107 return ERR_PTR(-ENOMEM); 108 } 109 110 axi->axi_lpi_en = of_property_read_bool(np, "snps,lpi_en"); 111 axi->axi_xit_frm = of_property_read_bool(np, "snps,xit_frm"); 112 axi->axi_kbbe = of_property_read_bool(np, "snps,kbbe"); 113 axi->axi_fb = of_property_read_bool(np, "snps,fb"); 114 axi->axi_mb = of_property_read_bool(np, "snps,mb"); 115 axi->axi_rb = of_property_read_bool(np, "snps,rb"); 116 117 if (of_property_read_u32(np, "snps,wr_osr_lmt", &axi->axi_wr_osr_lmt)) 118 axi->axi_wr_osr_lmt = 1; 119 if (of_property_read_u32(np, "snps,rd_osr_lmt", &axi->axi_rd_osr_lmt)) 120 axi->axi_rd_osr_lmt = 1; 121 of_property_read_u32_array(np, "snps,blen", axi->axi_blen, AXI_BLEN); 122 of_node_put(np); 123 124 return axi; 125 } 126 127 /** 128 * stmmac_mtl_setup - parse DT parameters for multiple queues configuration 129 * @pdev: platform device 130 * @plat: enet data 131 */ 132 static int stmmac_mtl_setup(struct platform_device *pdev, 133 struct plat_stmmacenet_data *plat) 134 { 135 struct device_node *q_node; 136 struct device_node *rx_node; 137 struct device_node *tx_node; 138 u8 queue = 0; 139 int ret = 0; 140 141 /* For backwards-compatibility with device trees that don't have any 142 * snps,mtl-rx-config or snps,mtl-tx-config properties, we fall back 143 * to one RX and TX queues each. 144 */ 145 plat->rx_queues_to_use = 1; 146 plat->tx_queues_to_use = 1; 147 148 /* First Queue must always be in DCB mode. As MTL_QUEUE_DCB = 1 we need 149 * to always set this, otherwise Queue will be classified as AVB 150 * (because MTL_QUEUE_AVB = 0). 151 */ 152 plat->rx_queues_cfg[0].mode_to_use = MTL_QUEUE_DCB; 153 plat->tx_queues_cfg[0].mode_to_use = MTL_QUEUE_DCB; 154 155 rx_node = of_parse_phandle(pdev->dev.of_node, "snps,mtl-rx-config", 0); 156 if (!rx_node) 157 return ret; 158 159 tx_node = of_parse_phandle(pdev->dev.of_node, "snps,mtl-tx-config", 0); 160 if (!tx_node) { 161 of_node_put(rx_node); 162 return ret; 163 } 164 165 /* Processing RX queues common config */ 166 if (of_property_read_u32(rx_node, "snps,rx-queues-to-use", 167 &plat->rx_queues_to_use)) 168 plat->rx_queues_to_use = 1; 169 170 if (of_property_read_bool(rx_node, "snps,rx-sched-sp")) 171 plat->rx_sched_algorithm = MTL_RX_ALGORITHM_SP; 172 else if (of_property_read_bool(rx_node, "snps,rx-sched-wsp")) 173 plat->rx_sched_algorithm = MTL_RX_ALGORITHM_WSP; 174 else 175 plat->rx_sched_algorithm = MTL_RX_ALGORITHM_SP; 176 177 /* Processing individual RX queue config */ 178 for_each_child_of_node(rx_node, q_node) { 179 if (queue >= plat->rx_queues_to_use) 180 break; 181 182 if (of_property_read_bool(q_node, "snps,dcb-algorithm")) 183 plat->rx_queues_cfg[queue].mode_to_use = MTL_QUEUE_DCB; 184 else if (of_property_read_bool(q_node, "snps,avb-algorithm")) 185 plat->rx_queues_cfg[queue].mode_to_use = MTL_QUEUE_AVB; 186 else 187 plat->rx_queues_cfg[queue].mode_to_use = MTL_QUEUE_DCB; 188 189 if (of_property_read_u32(q_node, "snps,map-to-dma-channel", 190 &plat->rx_queues_cfg[queue].chan)) 191 plat->rx_queues_cfg[queue].chan = queue; 192 /* TODO: Dynamic mapping to be included in the future */ 193 194 if (of_property_read_u32(q_node, "snps,priority", 195 &plat->rx_queues_cfg[queue].prio)) { 196 plat->rx_queues_cfg[queue].prio = 0; 197 plat->rx_queues_cfg[queue].use_prio = false; 198 } else { 199 plat->rx_queues_cfg[queue].use_prio = true; 200 } 201 202 /* RX queue specific packet type routing */ 203 if (of_property_read_bool(q_node, "snps,route-avcp")) 204 plat->rx_queues_cfg[queue].pkt_route = PACKET_AVCPQ; 205 else if (of_property_read_bool(q_node, "snps,route-ptp")) 206 plat->rx_queues_cfg[queue].pkt_route = PACKET_PTPQ; 207 else if (of_property_read_bool(q_node, "snps,route-dcbcp")) 208 plat->rx_queues_cfg[queue].pkt_route = PACKET_DCBCPQ; 209 else if (of_property_read_bool(q_node, "snps,route-up")) 210 plat->rx_queues_cfg[queue].pkt_route = PACKET_UPQ; 211 else if (of_property_read_bool(q_node, "snps,route-multi-broad")) 212 plat->rx_queues_cfg[queue].pkt_route = PACKET_MCBCQ; 213 else 214 plat->rx_queues_cfg[queue].pkt_route = 0x0; 215 216 queue++; 217 } 218 if (queue != plat->rx_queues_to_use) { 219 ret = -EINVAL; 220 dev_err(&pdev->dev, "Not all RX queues were configured\n"); 221 goto out; 222 } 223 224 /* Processing TX queues common config */ 225 if (of_property_read_u32(tx_node, "snps,tx-queues-to-use", 226 &plat->tx_queues_to_use)) 227 plat->tx_queues_to_use = 1; 228 229 if (of_property_read_bool(tx_node, "snps,tx-sched-wrr")) 230 plat->tx_sched_algorithm = MTL_TX_ALGORITHM_WRR; 231 else if (of_property_read_bool(tx_node, "snps,tx-sched-wfq")) 232 plat->tx_sched_algorithm = MTL_TX_ALGORITHM_WFQ; 233 else if (of_property_read_bool(tx_node, "snps,tx-sched-dwrr")) 234 plat->tx_sched_algorithm = MTL_TX_ALGORITHM_DWRR; 235 else 236 plat->tx_sched_algorithm = MTL_TX_ALGORITHM_SP; 237 238 queue = 0; 239 240 /* Processing individual TX queue config */ 241 for_each_child_of_node(tx_node, q_node) { 242 if (queue >= plat->tx_queues_to_use) 243 break; 244 245 if (of_property_read_u32(q_node, "snps,weight", 246 &plat->tx_queues_cfg[queue].weight)) 247 plat->tx_queues_cfg[queue].weight = 0x10 + queue; 248 249 if (of_property_read_bool(q_node, "snps,dcb-algorithm")) { 250 plat->tx_queues_cfg[queue].mode_to_use = MTL_QUEUE_DCB; 251 } else if (of_property_read_bool(q_node, 252 "snps,avb-algorithm")) { 253 plat->tx_queues_cfg[queue].mode_to_use = MTL_QUEUE_AVB; 254 255 /* Credit Base Shaper parameters used by AVB */ 256 if (of_property_read_u32(q_node, "snps,send_slope", 257 &plat->tx_queues_cfg[queue].send_slope)) 258 plat->tx_queues_cfg[queue].send_slope = 0x0; 259 if (of_property_read_u32(q_node, "snps,idle_slope", 260 &plat->tx_queues_cfg[queue].idle_slope)) 261 plat->tx_queues_cfg[queue].idle_slope = 0x0; 262 if (of_property_read_u32(q_node, "snps,high_credit", 263 &plat->tx_queues_cfg[queue].high_credit)) 264 plat->tx_queues_cfg[queue].high_credit = 0x0; 265 if (of_property_read_u32(q_node, "snps,low_credit", 266 &plat->tx_queues_cfg[queue].low_credit)) 267 plat->tx_queues_cfg[queue].low_credit = 0x0; 268 } else { 269 plat->tx_queues_cfg[queue].mode_to_use = MTL_QUEUE_DCB; 270 } 271 272 if (of_property_read_u32(q_node, "snps,priority", 273 &plat->tx_queues_cfg[queue].prio)) { 274 plat->tx_queues_cfg[queue].prio = 0; 275 plat->tx_queues_cfg[queue].use_prio = false; 276 } else { 277 plat->tx_queues_cfg[queue].use_prio = true; 278 } 279 280 queue++; 281 } 282 if (queue != plat->tx_queues_to_use) { 283 ret = -EINVAL; 284 dev_err(&pdev->dev, "Not all TX queues were configured\n"); 285 goto out; 286 } 287 288 out: 289 of_node_put(rx_node); 290 of_node_put(tx_node); 291 of_node_put(q_node); 292 293 return ret; 294 } 295 296 /** 297 * stmmac_dt_phy - parse device-tree driver parameters to allocate PHY resources 298 * @plat: driver data platform structure 299 * @np: device tree node 300 * @dev: device pointer 301 * Description: 302 * The mdio bus will be allocated in case of a phy transceiver is on board; 303 * it will be NULL if the fixed-link is configured. 304 * If there is the "snps,dwmac-mdio" sub-node the mdio will be allocated 305 * in any case (for DSA, mdio must be registered even if fixed-link). 306 * The table below sums the supported configurations: 307 * ------------------------------- 308 * snps,phy-addr | Y 309 * ------------------------------- 310 * phy-handle | Y 311 * ------------------------------- 312 * fixed-link | N 313 * ------------------------------- 314 * snps,dwmac-mdio | 315 * even if | Y 316 * fixed-link | 317 * ------------------------------- 318 * 319 * It returns 0 in case of success otherwise -ENODEV. 320 */ 321 static int stmmac_dt_phy(struct plat_stmmacenet_data *plat, 322 struct device_node *np, struct device *dev) 323 { 324 bool mdio = !of_phy_is_fixed_link(np); 325 static const struct of_device_id need_mdio_ids[] = { 326 { .compatible = "snps,dwc-qos-ethernet-4.10" }, 327 {}, 328 }; 329 330 if (of_match_node(need_mdio_ids, np)) { 331 plat->mdio_node = of_get_child_by_name(np, "mdio"); 332 } else { 333 /** 334 * If snps,dwmac-mdio is passed from DT, always register 335 * the MDIO 336 */ 337 for_each_child_of_node(np, plat->mdio_node) { 338 if (of_device_is_compatible(plat->mdio_node, 339 "snps,dwmac-mdio")) 340 break; 341 } 342 } 343 344 if (plat->mdio_node) { 345 dev_dbg(dev, "Found MDIO subnode\n"); 346 mdio = true; 347 } 348 349 if (mdio) { 350 plat->mdio_bus_data = 351 devm_kzalloc(dev, sizeof(struct stmmac_mdio_bus_data), 352 GFP_KERNEL); 353 if (!plat->mdio_bus_data) 354 return -ENOMEM; 355 356 plat->mdio_bus_data->needs_reset = true; 357 } 358 359 return 0; 360 } 361 362 /** 363 * stmmac_of_get_mac_mode - retrieves the interface of the MAC 364 * @np: - device-tree node 365 * Description: 366 * Similar to `of_get_phy_mode()`, this function will retrieve (from 367 * the device-tree) the interface mode on the MAC side. This assumes 368 * that there is mode converter in-between the MAC & PHY 369 * (e.g. GMII-to-RGMII). 370 */ 371 static int stmmac_of_get_mac_mode(struct device_node *np) 372 { 373 const char *pm; 374 int err, i; 375 376 err = of_property_read_string(np, "mac-mode", &pm); 377 if (err < 0) 378 return err; 379 380 for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++) { 381 if (!strcasecmp(pm, phy_modes(i))) 382 return i; 383 } 384 385 return -ENODEV; 386 } 387 388 /** 389 * stmmac_probe_config_dt - parse device-tree driver parameters 390 * @pdev: platform_device structure 391 * @mac: MAC address to use 392 * Description: 393 * this function is to read the driver parameters from device-tree and 394 * set some private fields that will be used by the main at runtime. 395 */ 396 struct plat_stmmacenet_data * 397 stmmac_probe_config_dt(struct platform_device *pdev, u8 *mac) 398 { 399 struct device_node *np = pdev->dev.of_node; 400 struct plat_stmmacenet_data *plat; 401 struct stmmac_dma_cfg *dma_cfg; 402 int phy_mode; 403 void *ret; 404 int rc; 405 406 plat = devm_kzalloc(&pdev->dev, sizeof(*plat), GFP_KERNEL); 407 if (!plat) 408 return ERR_PTR(-ENOMEM); 409 410 rc = of_get_mac_address(np, mac); 411 if (rc) { 412 if (rc == -EPROBE_DEFER) 413 return ERR_PTR(rc); 414 415 eth_zero_addr(mac); 416 } 417 418 phy_mode = device_get_phy_mode(&pdev->dev); 419 if (phy_mode < 0) 420 return ERR_PTR(phy_mode); 421 422 plat->phy_interface = phy_mode; 423 plat->interface = stmmac_of_get_mac_mode(np); 424 if (plat->interface < 0) 425 plat->interface = plat->phy_interface; 426 427 /* Some wrapper drivers still rely on phy_node. Let's save it while 428 * they are not converted to phylink. */ 429 plat->phy_node = of_parse_phandle(np, "phy-handle", 0); 430 431 /* PHYLINK automatically parses the phy-handle property */ 432 plat->phylink_node = np; 433 434 /* Get max speed of operation from device tree */ 435 of_property_read_u32(np, "max-speed", &plat->max_speed); 436 437 plat->bus_id = of_alias_get_id(np, "ethernet"); 438 if (plat->bus_id < 0) 439 plat->bus_id = 0; 440 441 /* Default to phy auto-detection */ 442 plat->phy_addr = -1; 443 444 /* Default to get clk_csr from stmmac_clk_csr_set(), 445 * or get clk_csr from device tree. 446 */ 447 plat->clk_csr = -1; 448 if (of_property_read_u32(np, "snps,clk-csr", &plat->clk_csr)) 449 of_property_read_u32(np, "clk_csr", &plat->clk_csr); 450 451 /* "snps,phy-addr" is not a standard property. Mark it as deprecated 452 * and warn of its use. Remove this when phy node support is added. 453 */ 454 if (of_property_read_u32(np, "snps,phy-addr", &plat->phy_addr) == 0) 455 dev_warn(&pdev->dev, "snps,phy-addr property is deprecated\n"); 456 457 /* To Configure PHY by using all device-tree supported properties */ 458 rc = stmmac_dt_phy(plat, np, &pdev->dev); 459 if (rc) 460 return ERR_PTR(rc); 461 462 of_property_read_u32(np, "tx-fifo-depth", &plat->tx_fifo_size); 463 464 of_property_read_u32(np, "rx-fifo-depth", &plat->rx_fifo_size); 465 466 plat->force_sf_dma_mode = 467 of_property_read_bool(np, "snps,force_sf_dma_mode"); 468 469 plat->en_tx_lpi_clockgating = 470 of_property_read_bool(np, "snps,en-tx-lpi-clockgating"); 471 472 /* Set the maxmtu to a default of JUMBO_LEN in case the 473 * parameter is not present in the device tree. 474 */ 475 plat->maxmtu = JUMBO_LEN; 476 477 /* Set default value for multicast hash bins */ 478 plat->multicast_filter_bins = HASH_TABLE_SIZE; 479 480 /* Set default value for unicast filter entries */ 481 plat->unicast_filter_entries = 1; 482 483 /* 484 * Currently only the properties needed on SPEAr600 485 * are provided. All other properties should be added 486 * once needed on other platforms. 487 */ 488 if (of_device_is_compatible(np, "st,spear600-gmac") || 489 of_device_is_compatible(np, "snps,dwmac-3.50a") || 490 of_device_is_compatible(np, "snps,dwmac-3.70a") || 491 of_device_is_compatible(np, "snps,dwmac")) { 492 /* Note that the max-frame-size parameter as defined in the 493 * ePAPR v1.1 spec is defined as max-frame-size, it's 494 * actually used as the IEEE definition of MAC Client 495 * data, or MTU. The ePAPR specification is confusing as 496 * the definition is max-frame-size, but usage examples 497 * are clearly MTUs 498 */ 499 of_property_read_u32(np, "max-frame-size", &plat->maxmtu); 500 of_property_read_u32(np, "snps,multicast-filter-bins", 501 &plat->multicast_filter_bins); 502 of_property_read_u32(np, "snps,perfect-filter-entries", 503 &plat->unicast_filter_entries); 504 plat->unicast_filter_entries = dwmac1000_validate_ucast_entries( 505 &pdev->dev, plat->unicast_filter_entries); 506 plat->multicast_filter_bins = dwmac1000_validate_mcast_bins( 507 &pdev->dev, plat->multicast_filter_bins); 508 plat->has_gmac = 1; 509 plat->pmt = 1; 510 } 511 512 if (of_device_is_compatible(np, "snps,dwmac-3.40a")) { 513 plat->has_gmac = 1; 514 plat->enh_desc = 1; 515 plat->tx_coe = 1; 516 plat->bugged_jumbo = 1; 517 plat->pmt = 1; 518 } 519 520 if (of_device_is_compatible(np, "snps,dwmac-4.00") || 521 of_device_is_compatible(np, "snps,dwmac-4.10a") || 522 of_device_is_compatible(np, "snps,dwmac-4.20a") || 523 of_device_is_compatible(np, "snps,dwmac-5.10a") || 524 of_device_is_compatible(np, "snps,dwmac-5.20")) { 525 plat->has_gmac4 = 1; 526 plat->has_gmac = 0; 527 plat->pmt = 1; 528 plat->tso_en = of_property_read_bool(np, "snps,tso"); 529 } 530 531 if (of_device_is_compatible(np, "snps,dwmac-3.610") || 532 of_device_is_compatible(np, "snps,dwmac-3.710")) { 533 plat->enh_desc = 1; 534 plat->bugged_jumbo = 1; 535 plat->force_sf_dma_mode = 1; 536 } 537 538 if (of_device_is_compatible(np, "snps,dwxgmac")) { 539 plat->has_xgmac = 1; 540 plat->pmt = 1; 541 plat->tso_en = of_property_read_bool(np, "snps,tso"); 542 } 543 544 dma_cfg = devm_kzalloc(&pdev->dev, sizeof(*dma_cfg), 545 GFP_KERNEL); 546 if (!dma_cfg) { 547 stmmac_remove_config_dt(pdev, plat); 548 return ERR_PTR(-ENOMEM); 549 } 550 plat->dma_cfg = dma_cfg; 551 552 of_property_read_u32(np, "snps,pbl", &dma_cfg->pbl); 553 if (!dma_cfg->pbl) 554 dma_cfg->pbl = DEFAULT_DMA_PBL; 555 of_property_read_u32(np, "snps,txpbl", &dma_cfg->txpbl); 556 of_property_read_u32(np, "snps,rxpbl", &dma_cfg->rxpbl); 557 dma_cfg->pblx8 = !of_property_read_bool(np, "snps,no-pbl-x8"); 558 559 dma_cfg->aal = of_property_read_bool(np, "snps,aal"); 560 dma_cfg->fixed_burst = of_property_read_bool(np, "snps,fixed-burst"); 561 dma_cfg->mixed_burst = of_property_read_bool(np, "snps,mixed-burst"); 562 563 plat->force_thresh_dma_mode = of_property_read_bool(np, "snps,force_thresh_dma_mode"); 564 if (plat->force_thresh_dma_mode && plat->force_sf_dma_mode) { 565 plat->force_sf_dma_mode = 0; 566 dev_warn(&pdev->dev, 567 "force_sf_dma_mode is ignored if force_thresh_dma_mode is set.\n"); 568 } 569 570 of_property_read_u32(np, "snps,ps-speed", &plat->mac_port_sel_speed); 571 572 plat->axi = stmmac_axi_setup(pdev); 573 574 rc = stmmac_mtl_setup(pdev, plat); 575 if (rc) { 576 stmmac_remove_config_dt(pdev, plat); 577 return ERR_PTR(rc); 578 } 579 580 /* clock setup */ 581 if (!of_device_is_compatible(np, "snps,dwc-qos-ethernet-4.10")) { 582 plat->stmmac_clk = devm_clk_get(&pdev->dev, 583 STMMAC_RESOURCE_NAME); 584 if (IS_ERR(plat->stmmac_clk)) { 585 dev_warn(&pdev->dev, "Cannot get CSR clock\n"); 586 plat->stmmac_clk = NULL; 587 } 588 clk_prepare_enable(plat->stmmac_clk); 589 } 590 591 plat->pclk = devm_clk_get_optional(&pdev->dev, "pclk"); 592 if (IS_ERR(plat->pclk)) { 593 ret = plat->pclk; 594 goto error_pclk_get; 595 } 596 clk_prepare_enable(plat->pclk); 597 598 /* Fall-back to main clock in case of no PTP ref is passed */ 599 plat->clk_ptp_ref = devm_clk_get(&pdev->dev, "ptp_ref"); 600 if (IS_ERR(plat->clk_ptp_ref)) { 601 plat->clk_ptp_rate = clk_get_rate(plat->stmmac_clk); 602 plat->clk_ptp_ref = NULL; 603 dev_info(&pdev->dev, "PTP uses main clock\n"); 604 } else { 605 plat->clk_ptp_rate = clk_get_rate(plat->clk_ptp_ref); 606 dev_dbg(&pdev->dev, "PTP rate %d\n", plat->clk_ptp_rate); 607 } 608 609 plat->stmmac_rst = devm_reset_control_get_optional(&pdev->dev, 610 STMMAC_RESOURCE_NAME); 611 if (IS_ERR(plat->stmmac_rst)) { 612 ret = plat->stmmac_rst; 613 goto error_hw_init; 614 } 615 616 plat->stmmac_ahb_rst = devm_reset_control_get_optional_shared( 617 &pdev->dev, "ahb"); 618 if (IS_ERR(plat->stmmac_ahb_rst)) { 619 ret = plat->stmmac_ahb_rst; 620 goto error_hw_init; 621 } 622 623 return plat; 624 625 error_hw_init: 626 clk_disable_unprepare(plat->pclk); 627 error_pclk_get: 628 clk_disable_unprepare(plat->stmmac_clk); 629 630 return ret; 631 } 632 633 static void devm_stmmac_remove_config_dt(void *data) 634 { 635 struct plat_stmmacenet_data *plat = data; 636 637 /* Platform data argument is unused */ 638 stmmac_remove_config_dt(NULL, plat); 639 } 640 641 /** 642 * devm_stmmac_probe_config_dt 643 * @pdev: platform_device structure 644 * @mac: MAC address to use 645 * Description: Devres variant of stmmac_probe_config_dt(). Does not require 646 * the user to call stmmac_remove_config_dt() at driver detach. 647 */ 648 struct plat_stmmacenet_data * 649 devm_stmmac_probe_config_dt(struct platform_device *pdev, u8 *mac) 650 { 651 struct plat_stmmacenet_data *plat; 652 int ret; 653 654 plat = stmmac_probe_config_dt(pdev, mac); 655 if (IS_ERR(plat)) 656 return plat; 657 658 ret = devm_add_action_or_reset(&pdev->dev, 659 devm_stmmac_remove_config_dt, plat); 660 if (ret) 661 return ERR_PTR(ret); 662 663 return plat; 664 } 665 666 /** 667 * stmmac_remove_config_dt - undo the effects of stmmac_probe_config_dt() 668 * @pdev: platform_device structure 669 * @plat: driver data platform structure 670 * 671 * Release resources claimed by stmmac_probe_config_dt(). 672 */ 673 void stmmac_remove_config_dt(struct platform_device *pdev, 674 struct plat_stmmacenet_data *plat) 675 { 676 clk_disable_unprepare(plat->stmmac_clk); 677 clk_disable_unprepare(plat->pclk); 678 of_node_put(plat->phy_node); 679 of_node_put(plat->mdio_node); 680 } 681 #else 682 struct plat_stmmacenet_data * 683 stmmac_probe_config_dt(struct platform_device *pdev, u8 *mac) 684 { 685 return ERR_PTR(-EINVAL); 686 } 687 688 struct plat_stmmacenet_data * 689 devm_stmmac_probe_config_dt(struct platform_device *pdev, u8 *mac) 690 { 691 return ERR_PTR(-EINVAL); 692 } 693 694 void stmmac_remove_config_dt(struct platform_device *pdev, 695 struct plat_stmmacenet_data *plat) 696 { 697 } 698 #endif /* CONFIG_OF */ 699 EXPORT_SYMBOL_GPL(stmmac_probe_config_dt); 700 EXPORT_SYMBOL_GPL(devm_stmmac_probe_config_dt); 701 EXPORT_SYMBOL_GPL(stmmac_remove_config_dt); 702 703 int stmmac_get_platform_resources(struct platform_device *pdev, 704 struct stmmac_resources *stmmac_res) 705 { 706 memset(stmmac_res, 0, sizeof(*stmmac_res)); 707 708 /* Get IRQ information early to have an ability to ask for deferred 709 * probe if needed before we went too far with resource allocation. 710 */ 711 stmmac_res->irq = platform_get_irq_byname(pdev, "macirq"); 712 if (stmmac_res->irq < 0) 713 return stmmac_res->irq; 714 715 /* On some platforms e.g. SPEAr the wake up irq differs from the mac irq 716 * The external wake up irq can be passed through the platform code 717 * named as "eth_wake_irq" 718 * 719 * In case the wake up interrupt is not passed from the platform 720 * so the driver will continue to use the mac irq (ndev->irq) 721 */ 722 stmmac_res->wol_irq = 723 platform_get_irq_byname_optional(pdev, "eth_wake_irq"); 724 if (stmmac_res->wol_irq < 0) { 725 if (stmmac_res->wol_irq == -EPROBE_DEFER) 726 return -EPROBE_DEFER; 727 dev_info(&pdev->dev, "IRQ eth_wake_irq not found\n"); 728 stmmac_res->wol_irq = stmmac_res->irq; 729 } 730 731 stmmac_res->lpi_irq = 732 platform_get_irq_byname_optional(pdev, "eth_lpi"); 733 if (stmmac_res->lpi_irq < 0) { 734 if (stmmac_res->lpi_irq == -EPROBE_DEFER) 735 return -EPROBE_DEFER; 736 dev_info(&pdev->dev, "IRQ eth_lpi not found\n"); 737 } 738 739 stmmac_res->addr = devm_platform_ioremap_resource(pdev, 0); 740 741 return PTR_ERR_OR_ZERO(stmmac_res->addr); 742 } 743 EXPORT_SYMBOL_GPL(stmmac_get_platform_resources); 744 745 /** 746 * stmmac_pltfr_init 747 * @pdev: pointer to the platform device 748 * @plat: driver data platform structure 749 * Description: Call the platform's init callback (if any) and propagate 750 * the return value. 751 */ 752 int stmmac_pltfr_init(struct platform_device *pdev, 753 struct plat_stmmacenet_data *plat) 754 { 755 int ret = 0; 756 757 if (plat->init) 758 ret = plat->init(pdev, plat->bsp_priv); 759 760 return ret; 761 } 762 EXPORT_SYMBOL_GPL(stmmac_pltfr_init); 763 764 /** 765 * stmmac_pltfr_exit 766 * @pdev: pointer to the platform device 767 * @plat: driver data platform structure 768 * Description: Call the platform's exit callback (if any). 769 */ 770 void stmmac_pltfr_exit(struct platform_device *pdev, 771 struct plat_stmmacenet_data *plat) 772 { 773 if (plat->exit) 774 plat->exit(pdev, plat->bsp_priv); 775 } 776 EXPORT_SYMBOL_GPL(stmmac_pltfr_exit); 777 778 /** 779 * stmmac_pltfr_probe 780 * @pdev: platform device pointer 781 * @plat: driver data platform structure 782 * @res: stmmac resources structure 783 * Description: This calls the platform's init() callback and probes the 784 * stmmac driver. 785 */ 786 int stmmac_pltfr_probe(struct platform_device *pdev, 787 struct plat_stmmacenet_data *plat, 788 struct stmmac_resources *res) 789 { 790 int ret; 791 792 ret = stmmac_pltfr_init(pdev, plat); 793 if (ret) 794 return ret; 795 796 ret = stmmac_dvr_probe(&pdev->dev, plat, res); 797 if (ret) { 798 stmmac_pltfr_exit(pdev, plat); 799 return ret; 800 } 801 802 return ret; 803 } 804 EXPORT_SYMBOL_GPL(stmmac_pltfr_probe); 805 806 static void devm_stmmac_pltfr_remove(void *data) 807 { 808 struct platform_device *pdev = data; 809 810 stmmac_pltfr_remove_no_dt(pdev); 811 } 812 813 /** 814 * devm_stmmac_pltfr_probe 815 * @pdev: pointer to the platform device 816 * @plat: driver data platform structure 817 * @res: stmmac resources 818 * Description: Devres variant of stmmac_pltfr_probe(). Allows users to skip 819 * calling stmmac_pltfr_remove() on driver detach. 820 */ 821 int devm_stmmac_pltfr_probe(struct platform_device *pdev, 822 struct plat_stmmacenet_data *plat, 823 struct stmmac_resources *res) 824 { 825 int ret; 826 827 ret = stmmac_pltfr_probe(pdev, plat, res); 828 if (ret) 829 return ret; 830 831 return devm_add_action_or_reset(&pdev->dev, devm_stmmac_pltfr_remove, 832 pdev); 833 } 834 EXPORT_SYMBOL_GPL(devm_stmmac_pltfr_probe); 835 836 /** 837 * stmmac_pltfr_remove_no_dt 838 * @pdev: pointer to the platform device 839 * Description: This undoes the effects of stmmac_pltfr_probe() by removing the 840 * driver and calling the platform's exit() callback. 841 */ 842 void stmmac_pltfr_remove_no_dt(struct platform_device *pdev) 843 { 844 struct net_device *ndev = platform_get_drvdata(pdev); 845 struct stmmac_priv *priv = netdev_priv(ndev); 846 struct plat_stmmacenet_data *plat = priv->plat; 847 848 stmmac_dvr_remove(&pdev->dev); 849 stmmac_pltfr_exit(pdev, plat); 850 } 851 EXPORT_SYMBOL_GPL(stmmac_pltfr_remove_no_dt); 852 853 /** 854 * stmmac_pltfr_remove 855 * @pdev: platform device pointer 856 * Description: this function calls the main to free the net resources 857 * and calls the platforms hook and release the resources (e.g. mem). 858 */ 859 void stmmac_pltfr_remove(struct platform_device *pdev) 860 { 861 struct net_device *ndev = platform_get_drvdata(pdev); 862 struct stmmac_priv *priv = netdev_priv(ndev); 863 struct plat_stmmacenet_data *plat = priv->plat; 864 865 stmmac_pltfr_remove_no_dt(pdev); 866 stmmac_remove_config_dt(pdev, plat); 867 } 868 EXPORT_SYMBOL_GPL(stmmac_pltfr_remove); 869 870 /** 871 * stmmac_pltfr_suspend 872 * @dev: device pointer 873 * Description: this function is invoked when suspend the driver and it direcly 874 * call the main suspend function and then, if required, on some platform, it 875 * can call an exit helper. 876 */ 877 static int __maybe_unused stmmac_pltfr_suspend(struct device *dev) 878 { 879 int ret; 880 struct net_device *ndev = dev_get_drvdata(dev); 881 struct stmmac_priv *priv = netdev_priv(ndev); 882 struct platform_device *pdev = to_platform_device(dev); 883 884 ret = stmmac_suspend(dev); 885 stmmac_pltfr_exit(pdev, priv->plat); 886 887 return ret; 888 } 889 890 /** 891 * stmmac_pltfr_resume 892 * @dev: device pointer 893 * Description: this function is invoked when resume the driver before calling 894 * the main resume function, on some platforms, it can call own init helper 895 * if required. 896 */ 897 static int __maybe_unused stmmac_pltfr_resume(struct device *dev) 898 { 899 struct net_device *ndev = dev_get_drvdata(dev); 900 struct stmmac_priv *priv = netdev_priv(ndev); 901 struct platform_device *pdev = to_platform_device(dev); 902 int ret; 903 904 ret = stmmac_pltfr_init(pdev, priv->plat->bsp_priv); 905 if (ret) 906 return ret; 907 908 return stmmac_resume(dev); 909 } 910 911 static int __maybe_unused stmmac_runtime_suspend(struct device *dev) 912 { 913 struct net_device *ndev = dev_get_drvdata(dev); 914 struct stmmac_priv *priv = netdev_priv(ndev); 915 916 stmmac_bus_clks_config(priv, false); 917 918 return 0; 919 } 920 921 static int __maybe_unused stmmac_runtime_resume(struct device *dev) 922 { 923 struct net_device *ndev = dev_get_drvdata(dev); 924 struct stmmac_priv *priv = netdev_priv(ndev); 925 926 return stmmac_bus_clks_config(priv, true); 927 } 928 929 static int __maybe_unused stmmac_pltfr_noirq_suspend(struct device *dev) 930 { 931 struct net_device *ndev = dev_get_drvdata(dev); 932 struct stmmac_priv *priv = netdev_priv(ndev); 933 int ret; 934 935 if (!netif_running(ndev)) 936 return 0; 937 938 if (!device_may_wakeup(priv->device) || !priv->plat->pmt) { 939 /* Disable clock in case of PWM is off */ 940 clk_disable_unprepare(priv->plat->clk_ptp_ref); 941 942 ret = pm_runtime_force_suspend(dev); 943 if (ret) 944 return ret; 945 } 946 947 return 0; 948 } 949 950 static int __maybe_unused stmmac_pltfr_noirq_resume(struct device *dev) 951 { 952 struct net_device *ndev = dev_get_drvdata(dev); 953 struct stmmac_priv *priv = netdev_priv(ndev); 954 int ret; 955 956 if (!netif_running(ndev)) 957 return 0; 958 959 if (!device_may_wakeup(priv->device) || !priv->plat->pmt) { 960 /* enable the clk previously disabled */ 961 ret = pm_runtime_force_resume(dev); 962 if (ret) 963 return ret; 964 965 ret = clk_prepare_enable(priv->plat->clk_ptp_ref); 966 if (ret < 0) { 967 netdev_warn(priv->dev, 968 "failed to enable PTP reference clock: %pe\n", 969 ERR_PTR(ret)); 970 return ret; 971 } 972 } 973 974 return 0; 975 } 976 977 const struct dev_pm_ops stmmac_pltfr_pm_ops = { 978 SET_SYSTEM_SLEEP_PM_OPS(stmmac_pltfr_suspend, stmmac_pltfr_resume) 979 SET_RUNTIME_PM_OPS(stmmac_runtime_suspend, stmmac_runtime_resume, NULL) 980 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(stmmac_pltfr_noirq_suspend, stmmac_pltfr_noirq_resume) 981 }; 982 EXPORT_SYMBOL_GPL(stmmac_pltfr_pm_ops); 983 984 MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet platform support"); 985 MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>"); 986 MODULE_LICENSE("GPL"); 987