xref: /openbmc/linux/drivers/net/ethernet/stmicro/stmmac/dwmac-stm32.c (revision f291209eca5eba0b4704fa0832af57b12dbc1a02)
1af873fceSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2c6eec6f3SAlexandre TORGUE /*
3c6eec6f3SAlexandre TORGUE  * dwmac-stm32.c - DWMAC Specific Glue layer for STM32 MCU
4c6eec6f3SAlexandre TORGUE  *
5f6454f80SBenjamin Gaignard  * Copyright (C) STMicroelectronics SA 2017
6f6454f80SBenjamin Gaignard  * Author:  Alexandre Torgue <alexandre.torgue@st.com> for STMicroelectronics.
7c6eec6f3SAlexandre TORGUE  */
8c6eec6f3SAlexandre TORGUE 
9c6eec6f3SAlexandre TORGUE #include <linux/clk.h>
10c6eec6f3SAlexandre TORGUE #include <linux/kernel.h>
11c6eec6f3SAlexandre TORGUE #include <linux/mfd/syscon.h>
12c6eec6f3SAlexandre TORGUE #include <linux/module.h>
13c6eec6f3SAlexandre TORGUE #include <linux/of.h>
14c6eec6f3SAlexandre TORGUE #include <linux/of_net.h>
15c6eec6f3SAlexandre TORGUE #include <linux/phy.h>
16c6eec6f3SAlexandre TORGUE #include <linux/platform_device.h>
176528e02cSChristophe Roullier #include <linux/pm_wakeirq.h>
18c6eec6f3SAlexandre TORGUE #include <linux/regmap.h>
19c6eec6f3SAlexandre TORGUE #include <linux/slab.h>
20c6eec6f3SAlexandre TORGUE #include <linux/stmmac.h>
21c6eec6f3SAlexandre TORGUE 
22c6eec6f3SAlexandre TORGUE #include "stmmac_platform.h"
23c6eec6f3SAlexandre TORGUE 
246528e02cSChristophe Roullier #define SYSCFG_MCU_ETH_MASK		BIT(23)
256528e02cSChristophe Roullier #define SYSCFG_MP1_ETH_MASK		GENMASK(23, 16)
2622947335SChristophe Roullier #define SYSCFG_PMCCLRR_OFFSET		0x40
276528e02cSChristophe Roullier 
286528e02cSChristophe Roullier #define SYSCFG_PMCR_ETH_CLK_SEL		BIT(16)
296528e02cSChristophe Roullier #define SYSCFG_PMCR_ETH_REF_CLK_SEL	BIT(17)
3022947335SChristophe Roullier 
311bb694e2SChristophe Roullier /* CLOCK feed to PHY*/
321bb694e2SChristophe Roullier #define ETH_CK_F_25M	25000000
331bb694e2SChristophe Roullier #define ETH_CK_F_50M	50000000
341bb694e2SChristophe Roullier #define ETH_CK_F_125M	125000000
351bb694e2SChristophe Roullier 
3622947335SChristophe Roullier /*  Ethernet PHY interface selection in register SYSCFG Configuration
3722947335SChristophe Roullier  *------------------------------------------
3822947335SChristophe Roullier  * src	 |BIT(23)| BIT(22)| BIT(21)|BIT(20)|
3922947335SChristophe Roullier  *------------------------------------------
4022947335SChristophe Roullier  * MII   |   0	 |   0	  |   0    |   1   |
4122947335SChristophe Roullier  *------------------------------------------
4222947335SChristophe Roullier  * GMII  |   0	 |   0	  |   0    |   0   |
4322947335SChristophe Roullier  *------------------------------------------
4422947335SChristophe Roullier  * RGMII |   0	 |   0	  |   1	   |  n/a  |
4522947335SChristophe Roullier  *------------------------------------------
4622947335SChristophe Roullier  * RMII  |   1	 |   0	  |   0	   |  n/a  |
4722947335SChristophe Roullier  *------------------------------------------
4822947335SChristophe Roullier  */
496528e02cSChristophe Roullier #define SYSCFG_PMCR_ETH_SEL_MII		BIT(20)
506528e02cSChristophe Roullier #define SYSCFG_PMCR_ETH_SEL_RGMII	BIT(21)
516528e02cSChristophe Roullier #define SYSCFG_PMCR_ETH_SEL_RMII	BIT(23)
526528e02cSChristophe Roullier #define SYSCFG_PMCR_ETH_SEL_GMII	0
536528e02cSChristophe Roullier #define SYSCFG_MCU_ETH_SEL_MII		0
546528e02cSChristophe Roullier #define SYSCFG_MCU_ETH_SEL_RMII		1
55c6eec6f3SAlexandre TORGUE 
5622947335SChristophe Roullier /* STM32MP1 register definitions
5722947335SChristophe Roullier  *
5822947335SChristophe Roullier  * Below table summarizes the clock requirement and clock sources for
5922947335SChristophe Roullier  * supported phy interface modes.
6022947335SChristophe Roullier  * __________________________________________________________________________
6122947335SChristophe Roullier  *|PHY_MODE | Normal | PHY wo crystal|   PHY wo crystal   |No 125Mhz from PHY|
6222947335SChristophe Roullier  *|         |        |      25MHz    |        50MHz       |                  |
6322947335SChristophe Roullier  * ---------------------------------------------------------------------------
6422947335SChristophe Roullier  *|  MII    |	 -   |     eth-ck    |	      n/a	  |	  n/a        |
651bb694e2SChristophe Roullier  *|         |        | st,ext-phyclk |                    |		     |
6622947335SChristophe Roullier  * ---------------------------------------------------------------------------
6722947335SChristophe Roullier  *|  GMII   |	 -   |     eth-ck    |	      n/a	  |	  n/a        |
681bb694e2SChristophe Roullier  *|         |        | st,ext-phyclk |                    |		     |
6922947335SChristophe Roullier  * ---------------------------------------------------------------------------
701bb694e2SChristophe Roullier  *| RGMII   |	 -   |     eth-ck    |	      n/a	  |      eth-ck      |
711bb694e2SChristophe Roullier  *|         |        | st,ext-phyclk |                    | st,eth-clk-sel or|
721bb694e2SChristophe Roullier  *|         |        |               |                    | st,ext-phyclk    |
7322947335SChristophe Roullier  * ---------------------------------------------------------------------------
7422947335SChristophe Roullier  *| RMII    |	 -   |     eth-ck    |	    eth-ck        |	  n/a        |
751bb694e2SChristophe Roullier  *|         |        | st,ext-phyclk | st,eth-ref-clk-sel |		     |
761bb694e2SChristophe Roullier  *|         |        |               | or st,ext-phyclk   |		     |
7722947335SChristophe Roullier  * ---------------------------------------------------------------------------
7822947335SChristophe Roullier  *
7922947335SChristophe Roullier  */
8022947335SChristophe Roullier 
81c6eec6f3SAlexandre TORGUE struct stm32_dwmac {
82c6eec6f3SAlexandre TORGUE 	struct clk *clk_tx;
83c6eec6f3SAlexandre TORGUE 	struct clk *clk_rx;
846528e02cSChristophe Roullier 	struct clk *clk_eth_ck;
856528e02cSChristophe Roullier 	struct clk *clk_ethstp;
866528e02cSChristophe Roullier 	struct clk *syscfg_clk;
871bb694e2SChristophe Roullier 	int ext_phyclk;
881bb694e2SChristophe Roullier 	int enable_eth_ck;
8922947335SChristophe Roullier 	int eth_clk_sel_reg;
9022947335SChristophe Roullier 	int eth_ref_clk_sel_reg;
91634565f8SChristophe Roullier 	int irq_pwr_wakeup;
92c6eec6f3SAlexandre TORGUE 	u32 mode_reg;		 /* MAC glue-logic mode register */
93c6eec6f3SAlexandre TORGUE 	struct regmap *regmap;
94c6eec6f3SAlexandre TORGUE 	u32 speed;
956528e02cSChristophe Roullier 	const struct stm32_ops *ops;
966528e02cSChristophe Roullier 	struct device *dev;
976528e02cSChristophe Roullier };
986528e02cSChristophe Roullier 
996528e02cSChristophe Roullier struct stm32_ops {
1006528e02cSChristophe Roullier 	int (*set_mode)(struct plat_stmmacenet_data *plat_dat);
1016528e02cSChristophe Roullier 	int (*clk_prepare)(struct stm32_dwmac *dwmac, bool prepare);
1026528e02cSChristophe Roullier 	int (*suspend)(struct stm32_dwmac *dwmac);
1036528e02cSChristophe Roullier 	void (*resume)(struct stm32_dwmac *dwmac);
1046528e02cSChristophe Roullier 	int (*parse_data)(struct stm32_dwmac *dwmac,
1056528e02cSChristophe Roullier 			  struct device *dev);
1066528e02cSChristophe Roullier 	u32 syscfg_eth_mask;
107*6f195d6bSBen Wolsieffer 	bool clk_rx_enable_in_suspend;
108c6eec6f3SAlexandre TORGUE };
109c6eec6f3SAlexandre TORGUE 
stm32_dwmac_init(struct plat_stmmacenet_data * plat_dat)110c6eec6f3SAlexandre TORGUE static int stm32_dwmac_init(struct plat_stmmacenet_data *plat_dat)
111c6eec6f3SAlexandre TORGUE {
112c6eec6f3SAlexandre TORGUE 	struct stm32_dwmac *dwmac = plat_dat->bsp_priv;
113c6eec6f3SAlexandre TORGUE 	int ret;
114c6eec6f3SAlexandre TORGUE 
1156528e02cSChristophe Roullier 	if (dwmac->ops->set_mode) {
1166528e02cSChristophe Roullier 		ret = dwmac->ops->set_mode(plat_dat);
117c6eec6f3SAlexandre TORGUE 		if (ret)
118c6eec6f3SAlexandre TORGUE 			return ret;
1196528e02cSChristophe Roullier 	}
120c6eec6f3SAlexandre TORGUE 
121c6eec6f3SAlexandre TORGUE 	ret = clk_prepare_enable(dwmac->clk_tx);
122c6eec6f3SAlexandre TORGUE 	if (ret)
123c6eec6f3SAlexandre TORGUE 		return ret;
124c6eec6f3SAlexandre TORGUE 
125*6f195d6bSBen Wolsieffer 	if (!dwmac->ops->clk_rx_enable_in_suspend ||
126*6f195d6bSBen Wolsieffer 	    !dwmac->dev->power.is_suspended) {
127c6eec6f3SAlexandre TORGUE 		ret = clk_prepare_enable(dwmac->clk_rx);
1286528e02cSChristophe Roullier 		if (ret) {
129c6eec6f3SAlexandre TORGUE 			clk_disable_unprepare(dwmac->clk_tx);
1306528e02cSChristophe Roullier 			return ret;
1316528e02cSChristophe Roullier 		}
1326528e02cSChristophe Roullier 	}
1336528e02cSChristophe Roullier 
1346528e02cSChristophe Roullier 	if (dwmac->ops->clk_prepare) {
1356528e02cSChristophe Roullier 		ret = dwmac->ops->clk_prepare(dwmac, true);
1366528e02cSChristophe Roullier 		if (ret) {
1376528e02cSChristophe Roullier 			clk_disable_unprepare(dwmac->clk_rx);
1386528e02cSChristophe Roullier 			clk_disable_unprepare(dwmac->clk_tx);
1396528e02cSChristophe Roullier 		}
1406528e02cSChristophe Roullier 	}
141c6eec6f3SAlexandre TORGUE 
142c6eec6f3SAlexandre TORGUE 	return ret;
143c6eec6f3SAlexandre TORGUE }
144c6eec6f3SAlexandre TORGUE 
stm32mp1_clk_prepare(struct stm32_dwmac * dwmac,bool prepare)1456528e02cSChristophe Roullier static int stm32mp1_clk_prepare(struct stm32_dwmac *dwmac, bool prepare)
1466528e02cSChristophe Roullier {
1476528e02cSChristophe Roullier 	int ret = 0;
1486528e02cSChristophe Roullier 
1496528e02cSChristophe Roullier 	if (prepare) {
1506528e02cSChristophe Roullier 		ret = clk_prepare_enable(dwmac->syscfg_clk);
1516528e02cSChristophe Roullier 		if (ret)
1526528e02cSChristophe Roullier 			return ret;
1531bb694e2SChristophe Roullier 		if (dwmac->enable_eth_ck) {
1546528e02cSChristophe Roullier 			ret = clk_prepare_enable(dwmac->clk_eth_ck);
1556528e02cSChristophe Roullier 			if (ret) {
1566528e02cSChristophe Roullier 				clk_disable_unprepare(dwmac->syscfg_clk);
1576528e02cSChristophe Roullier 				return ret;
1586528e02cSChristophe Roullier 			}
1591bb694e2SChristophe Roullier 		}
1606528e02cSChristophe Roullier 	} else {
1616528e02cSChristophe Roullier 		clk_disable_unprepare(dwmac->syscfg_clk);
1621bb694e2SChristophe Roullier 		if (dwmac->enable_eth_ck)
1636528e02cSChristophe Roullier 			clk_disable_unprepare(dwmac->clk_eth_ck);
1646528e02cSChristophe Roullier 	}
1656528e02cSChristophe Roullier 	return ret;
1666528e02cSChristophe Roullier }
1676528e02cSChristophe Roullier 
stm32mp1_set_mode(struct plat_stmmacenet_data * plat_dat)1686528e02cSChristophe Roullier static int stm32mp1_set_mode(struct plat_stmmacenet_data *plat_dat)
1696528e02cSChristophe Roullier {
1706528e02cSChristophe Roullier 	struct stm32_dwmac *dwmac = plat_dat->bsp_priv;
1711bb694e2SChristophe Roullier 	u32 reg = dwmac->mode_reg, clk_rate;
17254e0602dSChristophe Roullier 	int val;
1736528e02cSChristophe Roullier 
1741bb694e2SChristophe Roullier 	clk_rate = clk_get_rate(dwmac->clk_eth_ck);
1751bb694e2SChristophe Roullier 	dwmac->enable_eth_ck = false;
176a014c355SRussell King (Oracle) 	switch (plat_dat->mac_interface) {
1776528e02cSChristophe Roullier 	case PHY_INTERFACE_MODE_MII:
1781bb694e2SChristophe Roullier 		if (clk_rate == ETH_CK_F_25M && dwmac->ext_phyclk)
1791bb694e2SChristophe Roullier 			dwmac->enable_eth_ck = true;
1806528e02cSChristophe Roullier 		val = SYSCFG_PMCR_ETH_SEL_MII;
1816528e02cSChristophe Roullier 		pr_debug("SYSCFG init : PHY_INTERFACE_MODE_MII\n");
1826528e02cSChristophe Roullier 		break;
1836528e02cSChristophe Roullier 	case PHY_INTERFACE_MODE_GMII:
1846528e02cSChristophe Roullier 		val = SYSCFG_PMCR_ETH_SEL_GMII;
1851bb694e2SChristophe Roullier 		if (clk_rate == ETH_CK_F_25M &&
1861bb694e2SChristophe Roullier 		    (dwmac->eth_clk_sel_reg || dwmac->ext_phyclk)) {
1871bb694e2SChristophe Roullier 			dwmac->enable_eth_ck = true;
1886528e02cSChristophe Roullier 			val |= SYSCFG_PMCR_ETH_CLK_SEL;
1891bb694e2SChristophe Roullier 		}
1906528e02cSChristophe Roullier 		pr_debug("SYSCFG init : PHY_INTERFACE_MODE_GMII\n");
1916528e02cSChristophe Roullier 		break;
1926528e02cSChristophe Roullier 	case PHY_INTERFACE_MODE_RMII:
1936528e02cSChristophe Roullier 		val = SYSCFG_PMCR_ETH_SEL_RMII;
1941bb694e2SChristophe Roullier 		if ((clk_rate == ETH_CK_F_25M || clk_rate == ETH_CK_F_50M) &&
1951bb694e2SChristophe Roullier 		    (dwmac->eth_ref_clk_sel_reg || dwmac->ext_phyclk)) {
1961bb694e2SChristophe Roullier 			dwmac->enable_eth_ck = true;
1976528e02cSChristophe Roullier 			val |= SYSCFG_PMCR_ETH_REF_CLK_SEL;
1981bb694e2SChristophe Roullier 		}
1996528e02cSChristophe Roullier 		pr_debug("SYSCFG init : PHY_INTERFACE_MODE_RMII\n");
2006528e02cSChristophe Roullier 		break;
2016528e02cSChristophe Roullier 	case PHY_INTERFACE_MODE_RGMII:
20222947335SChristophe Roullier 	case PHY_INTERFACE_MODE_RGMII_ID:
20322947335SChristophe Roullier 	case PHY_INTERFACE_MODE_RGMII_RXID:
20422947335SChristophe Roullier 	case PHY_INTERFACE_MODE_RGMII_TXID:
2056528e02cSChristophe Roullier 		val = SYSCFG_PMCR_ETH_SEL_RGMII;
2061bb694e2SChristophe Roullier 		if ((clk_rate == ETH_CK_F_25M || clk_rate == ETH_CK_F_125M) &&
2071bb694e2SChristophe Roullier 		    (dwmac->eth_clk_sel_reg || dwmac->ext_phyclk)) {
2081bb694e2SChristophe Roullier 			dwmac->enable_eth_ck = true;
2096528e02cSChristophe Roullier 			val |= SYSCFG_PMCR_ETH_CLK_SEL;
2101bb694e2SChristophe Roullier 		}
2116528e02cSChristophe Roullier 		pr_debug("SYSCFG init : PHY_INTERFACE_MODE_RGMII\n");
2126528e02cSChristophe Roullier 		break;
2136528e02cSChristophe Roullier 	default:
2146528e02cSChristophe Roullier 		pr_debug("SYSCFG init :  Do not manage %d interface\n",
215a014c355SRussell King (Oracle) 			 plat_dat->mac_interface);
2166528e02cSChristophe Roullier 		/* Do not manage others interfaces */
2176528e02cSChristophe Roullier 		return -EINVAL;
2186528e02cSChristophe Roullier 	}
2196528e02cSChristophe Roullier 
22022947335SChristophe Roullier 	/* Need to update PMCCLRR (clear register) */
22154e0602dSChristophe Roullier 	regmap_write(dwmac->regmap, reg + SYSCFG_PMCCLRR_OFFSET,
22222947335SChristophe Roullier 		     dwmac->ops->syscfg_eth_mask);
22322947335SChristophe Roullier 
22422947335SChristophe Roullier 	/* Update PMCSETR (set register) */
2256528e02cSChristophe Roullier 	return regmap_update_bits(dwmac->regmap, reg,
2266528e02cSChristophe Roullier 				 dwmac->ops->syscfg_eth_mask, val);
2276528e02cSChristophe Roullier }
2286528e02cSChristophe Roullier 
stm32mcu_set_mode(struct plat_stmmacenet_data * plat_dat)2296528e02cSChristophe Roullier static int stm32mcu_set_mode(struct plat_stmmacenet_data *plat_dat)
2306528e02cSChristophe Roullier {
2316528e02cSChristophe Roullier 	struct stm32_dwmac *dwmac = plat_dat->bsp_priv;
2326528e02cSChristophe Roullier 	u32 reg = dwmac->mode_reg;
2336528e02cSChristophe Roullier 	int val;
2346528e02cSChristophe Roullier 
235a014c355SRussell King (Oracle) 	switch (plat_dat->mac_interface) {
2366528e02cSChristophe Roullier 	case PHY_INTERFACE_MODE_MII:
2376528e02cSChristophe Roullier 		val = SYSCFG_MCU_ETH_SEL_MII;
2386528e02cSChristophe Roullier 		pr_debug("SYSCFG init : PHY_INTERFACE_MODE_MII\n");
2396528e02cSChristophe Roullier 		break;
2406528e02cSChristophe Roullier 	case PHY_INTERFACE_MODE_RMII:
2416528e02cSChristophe Roullier 		val = SYSCFG_MCU_ETH_SEL_RMII;
2426528e02cSChristophe Roullier 		pr_debug("SYSCFG init : PHY_INTERFACE_MODE_RMII\n");
2436528e02cSChristophe Roullier 		break;
2446528e02cSChristophe Roullier 	default:
2456528e02cSChristophe Roullier 		pr_debug("SYSCFG init :  Do not manage %d interface\n",
246a014c355SRussell King (Oracle) 			 plat_dat->mac_interface);
2476528e02cSChristophe Roullier 		/* Do not manage others interfaces */
2486528e02cSChristophe Roullier 		return -EINVAL;
2496528e02cSChristophe Roullier 	}
2506528e02cSChristophe Roullier 
2516528e02cSChristophe Roullier 	return regmap_update_bits(dwmac->regmap, reg,
25222947335SChristophe Roullier 				 dwmac->ops->syscfg_eth_mask, val << 23);
2536528e02cSChristophe Roullier }
2546528e02cSChristophe Roullier 
stm32_dwmac_clk_disable(struct stm32_dwmac * dwmac)255c6eec6f3SAlexandre TORGUE static void stm32_dwmac_clk_disable(struct stm32_dwmac *dwmac)
256c6eec6f3SAlexandre TORGUE {
257c6eec6f3SAlexandre TORGUE 	clk_disable_unprepare(dwmac->clk_tx);
258c6eec6f3SAlexandre TORGUE 	clk_disable_unprepare(dwmac->clk_rx);
2596528e02cSChristophe Roullier 
2606528e02cSChristophe Roullier 	if (dwmac->ops->clk_prepare)
2616528e02cSChristophe Roullier 		dwmac->ops->clk_prepare(dwmac, false);
262c6eec6f3SAlexandre TORGUE }
263c6eec6f3SAlexandre TORGUE 
stm32_dwmac_parse_data(struct stm32_dwmac * dwmac,struct device * dev)264c6eec6f3SAlexandre TORGUE static int stm32_dwmac_parse_data(struct stm32_dwmac *dwmac,
265c6eec6f3SAlexandre TORGUE 				  struct device *dev)
266c6eec6f3SAlexandre TORGUE {
267c6eec6f3SAlexandre TORGUE 	struct device_node *np = dev->of_node;
268c6eec6f3SAlexandre TORGUE 	int err;
269c6eec6f3SAlexandre TORGUE 
270c6eec6f3SAlexandre TORGUE 	/*  Get TX/RX clocks */
271c6eec6f3SAlexandre TORGUE 	dwmac->clk_tx = devm_clk_get(dev, "mac-clk-tx");
272c6eec6f3SAlexandre TORGUE 	if (IS_ERR(dwmac->clk_tx)) {
2736528e02cSChristophe Roullier 		dev_err(dev, "No ETH Tx clock provided...\n");
274c6eec6f3SAlexandre TORGUE 		return PTR_ERR(dwmac->clk_tx);
275c6eec6f3SAlexandre TORGUE 	}
2766528e02cSChristophe Roullier 
277c6eec6f3SAlexandre TORGUE 	dwmac->clk_rx = devm_clk_get(dev, "mac-clk-rx");
278c6eec6f3SAlexandre TORGUE 	if (IS_ERR(dwmac->clk_rx)) {
2796528e02cSChristophe Roullier 		dev_err(dev, "No ETH Rx clock provided...\n");
280c6eec6f3SAlexandre TORGUE 		return PTR_ERR(dwmac->clk_rx);
281c6eec6f3SAlexandre TORGUE 	}
282c6eec6f3SAlexandre TORGUE 
2836528e02cSChristophe Roullier 	if (dwmac->ops->parse_data) {
2846528e02cSChristophe Roullier 		err = dwmac->ops->parse_data(dwmac, dev);
2856528e02cSChristophe Roullier 		if (err)
2866528e02cSChristophe Roullier 			return err;
2876528e02cSChristophe Roullier 	}
2886528e02cSChristophe Roullier 
289c6eec6f3SAlexandre TORGUE 	/* Get mode register */
290c6eec6f3SAlexandre TORGUE 	dwmac->regmap = syscon_regmap_lookup_by_phandle(np, "st,syscon");
291c6eec6f3SAlexandre TORGUE 	if (IS_ERR(dwmac->regmap))
292c6eec6f3SAlexandre TORGUE 		return PTR_ERR(dwmac->regmap);
293c6eec6f3SAlexandre TORGUE 
294c6eec6f3SAlexandre TORGUE 	err = of_property_read_u32_index(np, "st,syscon", 1, &dwmac->mode_reg);
295c6eec6f3SAlexandre TORGUE 	if (err)
296c6eec6f3SAlexandre TORGUE 		dev_err(dev, "Can't get sysconfig mode offset (%d)\n", err);
297c6eec6f3SAlexandre TORGUE 
298c6eec6f3SAlexandre TORGUE 	return err;
299c6eec6f3SAlexandre TORGUE }
300c6eec6f3SAlexandre TORGUE 
stm32mp1_parse_data(struct stm32_dwmac * dwmac,struct device * dev)3016528e02cSChristophe Roullier static int stm32mp1_parse_data(struct stm32_dwmac *dwmac,
3026528e02cSChristophe Roullier 			       struct device *dev)
3036528e02cSChristophe Roullier {
304634565f8SChristophe Roullier 	struct platform_device *pdev = to_platform_device(dev);
3056528e02cSChristophe Roullier 	struct device_node *np = dev->of_node;
306634565f8SChristophe Roullier 	int err = 0;
3076528e02cSChristophe Roullier 
3081bb694e2SChristophe Roullier 	/* Ethernet PHY have no crystal */
3091bb694e2SChristophe Roullier 	dwmac->ext_phyclk = of_property_read_bool(np, "st,ext-phyclk");
3101bb694e2SChristophe Roullier 
31122947335SChristophe Roullier 	/* Gigabit Ethernet 125MHz clock selection. */
31222947335SChristophe Roullier 	dwmac->eth_clk_sel_reg = of_property_read_bool(np, "st,eth-clk-sel");
3136528e02cSChristophe Roullier 
31422947335SChristophe Roullier 	/* Ethernet 50Mhz RMII clock selection */
31522947335SChristophe Roullier 	dwmac->eth_ref_clk_sel_reg =
31622947335SChristophe Roullier 		of_property_read_bool(np, "st,eth-ref-clk-sel");
31722947335SChristophe Roullier 
3186528e02cSChristophe Roullier 	/*  Get ETH_CLK clocks */
3196528e02cSChristophe Roullier 	dwmac->clk_eth_ck = devm_clk_get(dev, "eth-ck");
3206528e02cSChristophe Roullier 	if (IS_ERR(dwmac->clk_eth_ck)) {
32107cc79efSAhmad Fatoum 		dev_info(dev, "No phy clock provided...\n");
32222947335SChristophe Roullier 		dwmac->clk_eth_ck = NULL;
3236528e02cSChristophe Roullier 	}
3246528e02cSChristophe Roullier 
3256528e02cSChristophe Roullier 	/*  Clock used for low power mode */
3266528e02cSChristophe Roullier 	dwmac->clk_ethstp = devm_clk_get(dev, "ethstp");
3276528e02cSChristophe Roullier 	if (IS_ERR(dwmac->clk_ethstp)) {
32822947335SChristophe Roullier 		dev_err(dev,
32922947335SChristophe Roullier 			"No ETH peripheral clock provided for CStop mode ...\n");
3306528e02cSChristophe Roullier 		return PTR_ERR(dwmac->clk_ethstp);
3316528e02cSChristophe Roullier 	}
3326528e02cSChristophe Roullier 
333caee3174SChristophe Roullier 	/*  Optional Clock for sysconfig */
3346528e02cSChristophe Roullier 	dwmac->syscfg_clk = devm_clk_get(dev, "syscfg-clk");
335caee3174SChristophe Roullier 	if (IS_ERR(dwmac->syscfg_clk))
336caee3174SChristophe Roullier 		dwmac->syscfg_clk = NULL;
3376528e02cSChristophe Roullier 
338634565f8SChristophe Roullier 	/* Get IRQ information early to have an ability to ask for deferred
339634565f8SChristophe Roullier 	 * probe if needed before we went too far with resource allocation.
340634565f8SChristophe Roullier 	 */
341d87ab44aSAhmad Fatoum 	dwmac->irq_pwr_wakeup = platform_get_irq_byname_optional(pdev,
342634565f8SChristophe Roullier 							"stm32_pwr_wakeup");
34356c5bc18SFabien Dessenne 	if (dwmac->irq_pwr_wakeup == -EPROBE_DEFER)
34456c5bc18SFabien Dessenne 		return -EPROBE_DEFER;
34556c5bc18SFabien Dessenne 
34622947335SChristophe Roullier 	if (!dwmac->clk_eth_ck && dwmac->irq_pwr_wakeup >= 0) {
347634565f8SChristophe Roullier 		err = device_init_wakeup(&pdev->dev, true);
348634565f8SChristophe Roullier 		if (err) {
349634565f8SChristophe Roullier 			dev_err(&pdev->dev, "Failed to init wake up irq\n");
350634565f8SChristophe Roullier 			return err;
351634565f8SChristophe Roullier 		}
352634565f8SChristophe Roullier 		err = dev_pm_set_dedicated_wake_irq(&pdev->dev,
353634565f8SChristophe Roullier 						    dwmac->irq_pwr_wakeup);
354634565f8SChristophe Roullier 		if (err) {
355634565f8SChristophe Roullier 			dev_err(&pdev->dev, "Failed to set wake up irq\n");
356634565f8SChristophe Roullier 			device_init_wakeup(&pdev->dev, false);
357634565f8SChristophe Roullier 		}
358634565f8SChristophe Roullier 		device_set_wakeup_enable(&pdev->dev, false);
359634565f8SChristophe Roullier 	}
360634565f8SChristophe Roullier 	return err;
3616528e02cSChristophe Roullier }
3626528e02cSChristophe Roullier 
stm32_dwmac_probe(struct platform_device * pdev)363c6eec6f3SAlexandre TORGUE static int stm32_dwmac_probe(struct platform_device *pdev)
364c6eec6f3SAlexandre TORGUE {
365c6eec6f3SAlexandre TORGUE 	struct plat_stmmacenet_data *plat_dat;
366c6eec6f3SAlexandre TORGUE 	struct stmmac_resources stmmac_res;
367c6eec6f3SAlexandre TORGUE 	struct stm32_dwmac *dwmac;
3686528e02cSChristophe Roullier 	const struct stm32_ops *data;
369c6eec6f3SAlexandre TORGUE 	int ret;
370c6eec6f3SAlexandre TORGUE 
371c6eec6f3SAlexandre TORGUE 	ret = stmmac_get_platform_resources(pdev, &stmmac_res);
372c6eec6f3SAlexandre TORGUE 	if (ret)
373c6eec6f3SAlexandre TORGUE 		return ret;
374c6eec6f3SAlexandre TORGUE 
37583216e39SMichael Walle 	plat_dat = stmmac_probe_config_dt(pdev, stmmac_res.mac);
376c6eec6f3SAlexandre TORGUE 	if (IS_ERR(plat_dat))
377c6eec6f3SAlexandre TORGUE 		return PTR_ERR(plat_dat);
378c6eec6f3SAlexandre TORGUE 
379c6eec6f3SAlexandre TORGUE 	dwmac = devm_kzalloc(&pdev->dev, sizeof(*dwmac), GFP_KERNEL);
380d2ed0a77SJohan Hovold 	if (!dwmac) {
381d2ed0a77SJohan Hovold 		ret = -ENOMEM;
382d2ed0a77SJohan Hovold 		goto err_remove_config_dt;
383d2ed0a77SJohan Hovold 	}
384c6eec6f3SAlexandre TORGUE 
3856528e02cSChristophe Roullier 	data = of_device_get_match_data(&pdev->dev);
3866528e02cSChristophe Roullier 	if (!data) {
3876528e02cSChristophe Roullier 		dev_err(&pdev->dev, "no of match data provided\n");
3886528e02cSChristophe Roullier 		ret = -EINVAL;
3896528e02cSChristophe Roullier 		goto err_remove_config_dt;
3906528e02cSChristophe Roullier 	}
3916528e02cSChristophe Roullier 
3926528e02cSChristophe Roullier 	dwmac->ops = data;
3936528e02cSChristophe Roullier 	dwmac->dev = &pdev->dev;
3946528e02cSChristophe Roullier 
395c6eec6f3SAlexandre TORGUE 	ret = stm32_dwmac_parse_data(dwmac, &pdev->dev);
396c6eec6f3SAlexandre TORGUE 	if (ret) {
397c6eec6f3SAlexandre TORGUE 		dev_err(&pdev->dev, "Unable to parse OF data\n");
398d2ed0a77SJohan Hovold 		goto err_remove_config_dt;
399c6eec6f3SAlexandre TORGUE 	}
400c6eec6f3SAlexandre TORGUE 
401c6eec6f3SAlexandre TORGUE 	plat_dat->bsp_priv = dwmac;
402c6eec6f3SAlexandre TORGUE 
403c6eec6f3SAlexandre TORGUE 	ret = stm32_dwmac_init(plat_dat);
404c6eec6f3SAlexandre TORGUE 	if (ret)
405d2ed0a77SJohan Hovold 		goto err_remove_config_dt;
406c6eec6f3SAlexandre TORGUE 
407c6eec6f3SAlexandre TORGUE 	ret = stmmac_dvr_probe(&pdev->dev, plat_dat, &stmmac_res);
408c6eec6f3SAlexandre TORGUE 	if (ret)
409d2ed0a77SJohan Hovold 		goto err_clk_disable;
410d2ed0a77SJohan Hovold 
411d2ed0a77SJohan Hovold 	return 0;
412d2ed0a77SJohan Hovold 
413d2ed0a77SJohan Hovold err_clk_disable:
414c6eec6f3SAlexandre TORGUE 	stm32_dwmac_clk_disable(dwmac);
415d2ed0a77SJohan Hovold err_remove_config_dt:
416d2ed0a77SJohan Hovold 	stmmac_remove_config_dt(pdev, plat_dat);
417c6eec6f3SAlexandre TORGUE 
418c6eec6f3SAlexandre TORGUE 	return ret;
419c6eec6f3SAlexandre TORGUE }
420c6eec6f3SAlexandre TORGUE 
stm32_dwmac_remove(struct platform_device * pdev)421fec3f552SUwe Kleine-König static void stm32_dwmac_remove(struct platform_device *pdev)
422c6eec6f3SAlexandre TORGUE {
423c6eec6f3SAlexandre TORGUE 	struct net_device *ndev = platform_get_drvdata(pdev);
424c6eec6f3SAlexandre TORGUE 	struct stmmac_priv *priv = netdev_priv(ndev);
425634565f8SChristophe Roullier 	struct stm32_dwmac *dwmac = priv->plat->bsp_priv;
426c6eec6f3SAlexandre TORGUE 
427ff0011cfSUwe Kleine-König 	stmmac_dvr_remove(&pdev->dev);
428ff0011cfSUwe Kleine-König 
429c6eec6f3SAlexandre TORGUE 	stm32_dwmac_clk_disable(priv->plat->bsp_priv);
430c6eec6f3SAlexandre TORGUE 
431634565f8SChristophe Roullier 	if (dwmac->irq_pwr_wakeup >= 0) {
432634565f8SChristophe Roullier 		dev_pm_clear_wake_irq(&pdev->dev);
433634565f8SChristophe Roullier 		device_init_wakeup(&pdev->dev, false);
434634565f8SChristophe Roullier 	}
435c6eec6f3SAlexandre TORGUE }
436c6eec6f3SAlexandre TORGUE 
stm32mp1_suspend(struct stm32_dwmac * dwmac)4376528e02cSChristophe Roullier static int stm32mp1_suspend(struct stm32_dwmac *dwmac)
4386528e02cSChristophe Roullier {
4396528e02cSChristophe Roullier 	int ret = 0;
4406528e02cSChristophe Roullier 
4416528e02cSChristophe Roullier 	ret = clk_prepare_enable(dwmac->clk_ethstp);
4426528e02cSChristophe Roullier 	if (ret)
4436528e02cSChristophe Roullier 		return ret;
4446528e02cSChristophe Roullier 
4456528e02cSChristophe Roullier 	clk_disable_unprepare(dwmac->clk_tx);
4466528e02cSChristophe Roullier 	clk_disable_unprepare(dwmac->syscfg_clk);
4471bb694e2SChristophe Roullier 	if (dwmac->enable_eth_ck)
4486528e02cSChristophe Roullier 		clk_disable_unprepare(dwmac->clk_eth_ck);
4496528e02cSChristophe Roullier 
4506528e02cSChristophe Roullier 	return ret;
4516528e02cSChristophe Roullier }
4526528e02cSChristophe Roullier 
stm32mp1_resume(struct stm32_dwmac * dwmac)4536528e02cSChristophe Roullier static void stm32mp1_resume(struct stm32_dwmac *dwmac)
4546528e02cSChristophe Roullier {
4556528e02cSChristophe Roullier 	clk_disable_unprepare(dwmac->clk_ethstp);
4566528e02cSChristophe Roullier }
4576528e02cSChristophe Roullier 
stm32mcu_suspend(struct stm32_dwmac * dwmac)4586528e02cSChristophe Roullier static int stm32mcu_suspend(struct stm32_dwmac *dwmac)
4596528e02cSChristophe Roullier {
4606528e02cSChristophe Roullier 	clk_disable_unprepare(dwmac->clk_tx);
4616528e02cSChristophe Roullier 	clk_disable_unprepare(dwmac->clk_rx);
4626528e02cSChristophe Roullier 
4636528e02cSChristophe Roullier 	return 0;
4646528e02cSChristophe Roullier }
4656528e02cSChristophe Roullier 
466c6eec6f3SAlexandre TORGUE #ifdef CONFIG_PM_SLEEP
stm32_dwmac_suspend(struct device * dev)467c6eec6f3SAlexandre TORGUE static int stm32_dwmac_suspend(struct device *dev)
468c6eec6f3SAlexandre TORGUE {
469c6eec6f3SAlexandre TORGUE 	struct net_device *ndev = dev_get_drvdata(dev);
470c6eec6f3SAlexandre TORGUE 	struct stmmac_priv *priv = netdev_priv(ndev);
4716528e02cSChristophe Roullier 	struct stm32_dwmac *dwmac = priv->plat->bsp_priv;
4726528e02cSChristophe Roullier 
473c6eec6f3SAlexandre TORGUE 	int ret;
474c6eec6f3SAlexandre TORGUE 
475c6eec6f3SAlexandre TORGUE 	ret = stmmac_suspend(dev);
4766528e02cSChristophe Roullier 
4776528e02cSChristophe Roullier 	if (dwmac->ops->suspend)
4786528e02cSChristophe Roullier 		ret = dwmac->ops->suspend(dwmac);
479c6eec6f3SAlexandre TORGUE 
480c6eec6f3SAlexandre TORGUE 	return ret;
481c6eec6f3SAlexandre TORGUE }
482c6eec6f3SAlexandre TORGUE 
stm32_dwmac_resume(struct device * dev)483c6eec6f3SAlexandre TORGUE static int stm32_dwmac_resume(struct device *dev)
484c6eec6f3SAlexandre TORGUE {
485c6eec6f3SAlexandre TORGUE 	struct net_device *ndev = dev_get_drvdata(dev);
486c6eec6f3SAlexandre TORGUE 	struct stmmac_priv *priv = netdev_priv(ndev);
4876528e02cSChristophe Roullier 	struct stm32_dwmac *dwmac = priv->plat->bsp_priv;
488c6eec6f3SAlexandre TORGUE 	int ret;
489c6eec6f3SAlexandre TORGUE 
4906528e02cSChristophe Roullier 	if (dwmac->ops->resume)
4916528e02cSChristophe Roullier 		dwmac->ops->resume(dwmac);
4926528e02cSChristophe Roullier 
493c6eec6f3SAlexandre TORGUE 	ret = stm32_dwmac_init(priv->plat);
494c6eec6f3SAlexandre TORGUE 	if (ret)
495c6eec6f3SAlexandre TORGUE 		return ret;
496c6eec6f3SAlexandre TORGUE 
497c6eec6f3SAlexandre TORGUE 	ret = stmmac_resume(dev);
498c6eec6f3SAlexandre TORGUE 
499c6eec6f3SAlexandre TORGUE 	return ret;
500c6eec6f3SAlexandre TORGUE }
501c6eec6f3SAlexandre TORGUE #endif /* CONFIG_PM_SLEEP */
502c6eec6f3SAlexandre TORGUE 
503a5f54fccSWei Yongjun static SIMPLE_DEV_PM_OPS(stm32_dwmac_pm_ops,
504a5f54fccSWei Yongjun 	stm32_dwmac_suspend, stm32_dwmac_resume);
505c6eec6f3SAlexandre TORGUE 
5066528e02cSChristophe Roullier static struct stm32_ops stm32mcu_dwmac_data = {
5076528e02cSChristophe Roullier 	.set_mode = stm32mcu_set_mode,
5086528e02cSChristophe Roullier 	.suspend = stm32mcu_suspend,
5096528e02cSChristophe Roullier 	.syscfg_eth_mask = SYSCFG_MCU_ETH_MASK
5106528e02cSChristophe Roullier };
5116528e02cSChristophe Roullier 
5126528e02cSChristophe Roullier static struct stm32_ops stm32mp1_dwmac_data = {
5136528e02cSChristophe Roullier 	.set_mode = stm32mp1_set_mode,
5146528e02cSChristophe Roullier 	.clk_prepare = stm32mp1_clk_prepare,
5156528e02cSChristophe Roullier 	.suspend = stm32mp1_suspend,
5166528e02cSChristophe Roullier 	.resume = stm32mp1_resume,
5176528e02cSChristophe Roullier 	.parse_data = stm32mp1_parse_data,
518*6f195d6bSBen Wolsieffer 	.syscfg_eth_mask = SYSCFG_MP1_ETH_MASK,
519*6f195d6bSBen Wolsieffer 	.clk_rx_enable_in_suspend = true
5206528e02cSChristophe Roullier };
5216528e02cSChristophe Roullier 
522c6eec6f3SAlexandre TORGUE static const struct of_device_id stm32_dwmac_match[] = {
5236528e02cSChristophe Roullier 	{ .compatible = "st,stm32-dwmac", .data = &stm32mcu_dwmac_data},
5246528e02cSChristophe Roullier 	{ .compatible = "st,stm32mp1-dwmac", .data = &stm32mp1_dwmac_data},
525c6eec6f3SAlexandre TORGUE 	{ }
526c6eec6f3SAlexandre TORGUE };
527c6eec6f3SAlexandre TORGUE MODULE_DEVICE_TABLE(of, stm32_dwmac_match);
528c6eec6f3SAlexandre TORGUE 
529c6eec6f3SAlexandre TORGUE static struct platform_driver stm32_dwmac_driver = {
530c6eec6f3SAlexandre TORGUE 	.probe  = stm32_dwmac_probe,
531fec3f552SUwe Kleine-König 	.remove_new = stm32_dwmac_remove,
532c6eec6f3SAlexandre TORGUE 	.driver = {
533c6eec6f3SAlexandre TORGUE 		.name           = "stm32-dwmac",
534c6eec6f3SAlexandre TORGUE 		.pm		= &stm32_dwmac_pm_ops,
535c6eec6f3SAlexandre TORGUE 		.of_match_table = stm32_dwmac_match,
536c6eec6f3SAlexandre TORGUE 	},
537c6eec6f3SAlexandre TORGUE };
538c6eec6f3SAlexandre TORGUE module_platform_driver(stm32_dwmac_driver);
539c6eec6f3SAlexandre TORGUE 
540c6eec6f3SAlexandre TORGUE MODULE_AUTHOR("Alexandre Torgue <alexandre.torgue@gmail.com>");
5416528e02cSChristophe Roullier MODULE_AUTHOR("Christophe Roullier <christophe.roullier@st.com>");
5426528e02cSChristophe Roullier MODULE_DESCRIPTION("STMicroelectronics STM32 DWMAC Specific Glue layer");
543c6eec6f3SAlexandre TORGUE MODULE_LICENSE("GPL v2");
544