28
28
#include <linux/if_vlan.h>
29
29
#include <linux/dma-mapping.h>
30
30
#include <linux/slab.h>
31
+ #include <linux/pm_runtime.h>
31
32
#include <linux/prefetch.h>
32
33
#include <linux/pinctrl/consumer.h>
33
34
#ifdef CONFIG_DEBUG_FS
@@ -113,6 +114,28 @@ static void stmmac_exit_fs(struct net_device *dev);
113
114
114
115
#define STMMAC_COAL_TIMER (x ) (ns_to_ktime((x) * NSEC_PER_USEC))
115
116
117
+ int stmmac_bus_clks_config (struct stmmac_priv * priv , bool enabled )
118
+ {
119
+ int ret = 0 ;
120
+
121
+ if (enabled ) {
122
+ ret = clk_prepare_enable (priv -> plat -> stmmac_clk );
123
+ if (ret )
124
+ return ret ;
125
+ ret = clk_prepare_enable (priv -> plat -> pclk );
126
+ if (ret ) {
127
+ clk_disable_unprepare (priv -> plat -> stmmac_clk );
128
+ return ret ;
129
+ }
130
+ } else {
131
+ clk_disable_unprepare (priv -> plat -> stmmac_clk );
132
+ clk_disable_unprepare (priv -> plat -> pclk );
133
+ }
134
+
135
+ return ret ;
136
+ }
137
+ EXPORT_SYMBOL_GPL (stmmac_bus_clks_config );
138
+
116
139
/**
117
140
* stmmac_verify_args - verify the driver parameters.
118
141
* Description: it checks the driver parameters and set a default in case of
@@ -2896,6 +2919,12 @@ static int stmmac_open(struct net_device *dev)
2896
2919
u32 chan ;
2897
2920
int ret ;
2898
2921
2922
+ ret = pm_runtime_get_sync (priv -> device );
2923
+ if (ret < 0 ) {
2924
+ pm_runtime_put_noidle (priv -> device );
2925
+ return ret ;
2926
+ }
2927
+
2899
2928
if (priv -> hw -> pcs != STMMAC_PCS_TBI &&
2900
2929
priv -> hw -> pcs != STMMAC_PCS_RTBI &&
2901
2930
priv -> hw -> xpcs_args .an_mode != DW_AN_C73 ) {
@@ -2904,7 +2933,7 @@ static int stmmac_open(struct net_device *dev)
2904
2933
netdev_err (priv -> dev ,
2905
2934
"%s: Cannot attach to PHY (error: %d)\n" ,
2906
2935
__func__ , ret );
2907
- return ret ;
2936
+ goto init_phy_error ;
2908
2937
}
2909
2938
}
2910
2939
@@ -3020,6 +3049,8 @@ static int stmmac_open(struct net_device *dev)
3020
3049
free_dma_desc_resources (priv );
3021
3050
dma_desc_error :
3022
3051
phylink_disconnect_phy (priv -> phylink );
3052
+ init_phy_error :
3053
+ pm_runtime_put (priv -> device );
3023
3054
return ret ;
3024
3055
}
3025
3056
@@ -3070,6 +3101,8 @@ static int stmmac_release(struct net_device *dev)
3070
3101
3071
3102
stmmac_release_ptp (priv );
3072
3103
3104
+ pm_runtime_put (priv -> device );
3105
+
3073
3106
return 0 ;
3074
3107
}
3075
3108
@@ -4706,6 +4739,12 @@ static int stmmac_vlan_rx_add_vid(struct net_device *ndev, __be16 proto, u16 vid
4706
4739
bool is_double = false;
4707
4740
int ret ;
4708
4741
4742
+ ret = pm_runtime_get_sync (priv -> device );
4743
+ if (ret < 0 ) {
4744
+ pm_runtime_put_noidle (priv -> device );
4745
+ return ret ;
4746
+ }
4747
+
4709
4748
if (be16_to_cpu (proto ) == ETH_P_8021AD )
4710
4749
is_double = true;
4711
4750
@@ -4739,10 +4778,15 @@ static int stmmac_vlan_rx_kill_vid(struct net_device *ndev, __be16 proto, u16 vi
4739
4778
if (priv -> hw -> num_vlan ) {
4740
4779
ret = stmmac_del_hw_vlan_rx_fltr (priv , ndev , priv -> hw , proto , vid );
4741
4780
if (ret )
4742
- return ret ;
4781
+ goto del_vlan_error ;
4743
4782
}
4744
4783
4745
- return stmmac_vlan_update (priv , is_double );
4784
+ ret = stmmac_vlan_update (priv , is_double );
4785
+
4786
+ del_vlan_error :
4787
+ pm_runtime_put (priv -> device );
4788
+
4789
+ return ret ;
4746
4790
}
4747
4791
4748
4792
static const struct net_device_ops stmmac_netdev_ops = {
@@ -5181,6 +5225,10 @@ int stmmac_dvr_probe(struct device *device,
5181
5225
5182
5226
stmmac_check_pcs_mode (priv );
5183
5227
5228
+ pm_runtime_get_noresume (device );
5229
+ pm_runtime_set_active (device );
5230
+ pm_runtime_enable (device );
5231
+
5184
5232
if (priv -> hw -> pcs != STMMAC_PCS_TBI &&
5185
5233
priv -> hw -> pcs != STMMAC_PCS_RTBI ) {
5186
5234
/* MDIO bus Registration */
@@ -5218,6 +5266,11 @@ int stmmac_dvr_probe(struct device *device,
5218
5266
stmmac_init_fs (ndev );
5219
5267
#endif
5220
5268
5269
+ /* Let pm_runtime_put() disable the clocks.
5270
+ * If CONFIG_PM is not enabled, the clocks will stay powered.
5271
+ */
5272
+ pm_runtime_put (device );
5273
+
5221
5274
return ret ;
5222
5275
5223
5276
error_serdes_powerup :
@@ -5232,6 +5285,7 @@ int stmmac_dvr_probe(struct device *device,
5232
5285
stmmac_napi_del (ndev );
5233
5286
error_hw_init :
5234
5287
destroy_workqueue (priv -> wq );
5288
+ stmmac_bus_clks_config (priv , false);
5235
5289
5236
5290
return ret ;
5237
5291
}
@@ -5267,8 +5321,8 @@ int stmmac_dvr_remove(struct device *dev)
5267
5321
phylink_destroy (priv -> phylink );
5268
5322
if (priv -> plat -> stmmac_rst )
5269
5323
reset_control_assert (priv -> plat -> stmmac_rst );
5270
- clk_disable_unprepare ( priv -> plat -> pclk );
5271
- clk_disable_unprepare ( priv -> plat -> stmmac_clk );
5324
+ pm_runtime_put ( dev );
5325
+ pm_runtime_disable ( dev );
5272
5326
if (priv -> hw -> pcs != STMMAC_PCS_TBI &&
5273
5327
priv -> hw -> pcs != STMMAC_PCS_RTBI )
5274
5328
stmmac_mdio_unregister (ndev );
@@ -5291,6 +5345,7 @@ int stmmac_suspend(struct device *dev)
5291
5345
struct net_device * ndev = dev_get_drvdata (dev );
5292
5346
struct stmmac_priv * priv = netdev_priv (ndev );
5293
5347
u32 chan ;
5348
+ int ret ;
5294
5349
5295
5350
if (!ndev || !netif_running (ndev ))
5296
5351
return 0 ;
@@ -5334,8 +5389,9 @@ int stmmac_suspend(struct device *dev)
5334
5389
pinctrl_pm_select_sleep_state (priv -> device );
5335
5390
/* Disable clock in case of PWM is off */
5336
5391
clk_disable_unprepare (priv -> plat -> clk_ptp_ref );
5337
- clk_disable_unprepare (priv -> plat -> pclk );
5338
- clk_disable_unprepare (priv -> plat -> stmmac_clk );
5392
+ ret = pm_runtime_force_suspend (dev );
5393
+ if (ret )
5394
+ return ret ;
5339
5395
}
5340
5396
mutex_unlock (& priv -> lock );
5341
5397
@@ -5401,8 +5457,9 @@ int stmmac_resume(struct device *dev)
5401
5457
} else {
5402
5458
pinctrl_pm_select_default_state (priv -> device );
5403
5459
/* enable the clk previously disabled */
5404
- clk_prepare_enable (priv -> plat -> stmmac_clk );
5405
- clk_prepare_enable (priv -> plat -> pclk );
5460
+ ret = pm_runtime_force_resume (dev );
5461
+ if (ret )
5462
+ return ret ;
5406
5463
if (priv -> plat -> clk_ptp_ref )
5407
5464
clk_prepare_enable (priv -> plat -> clk_ptp_ref );
5408
5465
/* reset the phy so that it's ready */
0 commit comments