blob: 70d41783329dd4e9b8b25fcc2487c244ad44cc7a [file] [log] [blame]
Thomas Gleixnercaab2772019-06-03 07:44:50 +02001// SPDX-License-Identifier: GPL-2.0-only
Dinh Nguyen801d2332014-03-26 22:45:10 -05002/* Copyright Altera Corporation (C) 2014. All rights reserved.
3 *
Dinh Nguyen801d2332014-03-26 22:45:10 -05004 * Adopted from dwmac-sti.c
5 */
6
Thor Thayer54a5afb2019-03-11 17:18:07 -05007#include <linux/mfd/altera-sysmgr.h>
Dinh Nguyen801d2332014-03-26 22:45:10 -05008#include <linux/of.h>
Ley Foon Tanb4834c82014-08-20 14:33:33 +08009#include <linux/of_address.h>
Dinh Nguyen801d2332014-03-26 22:45:10 -050010#include <linux/of_net.h>
11#include <linux/phy.h>
12#include <linux/regmap.h>
Vince Bridgers2d871aa2014-07-28 14:07:58 -050013#include <linux/reset.h>
Dinh Nguyen801d2332014-03-26 22:45:10 -050014#include <linux/stmmac.h>
Andy Shevchenkof10f9fb2014-11-07 16:46:42 +020015
Vince Bridgers2d871aa2014-07-28 14:07:58 -050016#include "stmmac.h"
Andy Shevchenkof10f9fb2014-11-07 16:46:42 +020017#include "stmmac_platform.h"
Dinh Nguyen801d2332014-03-26 22:45:10 -050018
Tien Hock Lohfb3bbdb2016-07-07 20:23:30 -070019#include "altr_tse_pcs.h"
20
21#define SGMII_ADAPTER_CTRL_REG 0x00
22#define SGMII_ADAPTER_DISABLE 0x0001
23
Dinh Nguyen801d2332014-03-26 22:45:10 -050024#define SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_GMII_MII 0x0
25#define SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_RGMII 0x1
26#define SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_RMII 0x2
27#define SYSMGR_EMACGRP_CTRL_PHYSEL_WIDTH 2
28#define SYSMGR_EMACGRP_CTRL_PHYSEL_MASK 0x00000003
Phil Reid43569812015-12-14 11:32:02 +080029#define SYSMGR_EMACGRP_CTRL_PTP_REF_CLK_MASK 0x00000010
Dinh Nguyen40ae2552019-06-05 10:05:51 -050030#define SYSMGR_GEN10_EMACGRP_CTRL_PTP_REF_CLK_MASK 0x00000100
Dinh Nguyen801d2332014-03-26 22:45:10 -050031
Phil Reid734e00fa2016-04-07 15:55:35 +080032#define SYSMGR_FPGAGRP_MODULE_REG 0x00000028
33#define SYSMGR_FPGAGRP_MODULE_EMAC 0x00000004
Dinh Nguyen40ae2552019-06-05 10:05:51 -050034#define SYSMGR_FPGAINTF_EMAC_REG 0x00000070
35#define SYSMGR_FPGAINTF_EMAC_BIT 0x1
Phil Reid734e00fa2016-04-07 15:55:35 +080036
Ley Foon Tanb4834c82014-08-20 14:33:33 +080037#define EMAC_SPLITTER_CTRL_REG 0x0
38#define EMAC_SPLITTER_CTRL_SPEED_MASK 0x3
39#define EMAC_SPLITTER_CTRL_SPEED_10 0x2
40#define EMAC_SPLITTER_CTRL_SPEED_100 0x3
41#define EMAC_SPLITTER_CTRL_SPEED_1000 0x0
42
Dinh Nguyen40ae2552019-06-05 10:05:51 -050043struct socfpga_dwmac;
44struct socfpga_dwmac_ops {
45 int (*set_phy_mode)(struct socfpga_dwmac *dwmac_priv);
46};
47
Dinh Nguyen801d2332014-03-26 22:45:10 -050048struct socfpga_dwmac {
Dinh Nguyen801d2332014-03-26 22:45:10 -050049 u32 reg_offset;
50 u32 reg_shift;
51 struct device *dev;
52 struct regmap *sys_mgr_base_addr;
Joachim Eastwood70cb1362016-05-01 22:58:21 +020053 struct reset_control *stmmac_rst;
Dinh Nguyenbc8a2d92018-06-19 10:35:38 -050054 struct reset_control *stmmac_ocp_rst;
Ley Foon Tanb4834c82014-08-20 14:33:33 +080055 void __iomem *splitter_base;
Phil Reid43569812015-12-14 11:32:02 +080056 bool f2h_ptp_ref_clk;
Tien Hock Lohfb3bbdb2016-07-07 20:23:30 -070057 struct tse_pcs pcs;
Dinh Nguyen40ae2552019-06-05 10:05:51 -050058 const struct socfpga_dwmac_ops *ops;
Dinh Nguyen801d2332014-03-26 22:45:10 -050059};
60
Ley Foon Tanb4834c82014-08-20 14:33:33 +080061static void socfpga_dwmac_fix_mac_speed(void *priv, unsigned int speed)
62{
63 struct socfpga_dwmac *dwmac = (struct socfpga_dwmac *)priv;
64 void __iomem *splitter_base = dwmac->splitter_base;
Tien Hock Lohfb3bbdb2016-07-07 20:23:30 -070065 void __iomem *tse_pcs_base = dwmac->pcs.tse_pcs_base;
66 void __iomem *sgmii_adapter_base = dwmac->pcs.sgmii_adapter_base;
67 struct device *dev = dwmac->dev;
68 struct net_device *ndev = dev_get_drvdata(dev);
69 struct phy_device *phy_dev = ndev->phydev;
Ley Foon Tanb4834c82014-08-20 14:33:33 +080070 u32 val;
71
Tien Hock Lohfb3bbdb2016-07-07 20:23:30 -070072 if ((tse_pcs_base) && (sgmii_adapter_base))
73 writew(SGMII_ADAPTER_DISABLE,
74 sgmii_adapter_base + SGMII_ADAPTER_CTRL_REG);
Ley Foon Tanb4834c82014-08-20 14:33:33 +080075
Tien Hock Lohfb3bbdb2016-07-07 20:23:30 -070076 if (splitter_base) {
77 val = readl(splitter_base + EMAC_SPLITTER_CTRL_REG);
78 val &= ~EMAC_SPLITTER_CTRL_SPEED_MASK;
Ley Foon Tanb4834c82014-08-20 14:33:33 +080079
Tien Hock Lohfb3bbdb2016-07-07 20:23:30 -070080 switch (speed) {
81 case 1000:
82 val |= EMAC_SPLITTER_CTRL_SPEED_1000;
83 break;
84 case 100:
85 val |= EMAC_SPLITTER_CTRL_SPEED_100;
86 break;
87 case 10:
88 val |= EMAC_SPLITTER_CTRL_SPEED_10;
89 break;
90 default:
91 return;
92 }
93 writel(val, splitter_base + EMAC_SPLITTER_CTRL_REG);
Ley Foon Tanb4834c82014-08-20 14:33:33 +080094 }
95
Tien Hock Lohfb3bbdb2016-07-07 20:23:30 -070096 if (tse_pcs_base && sgmii_adapter_base)
97 tse_pcs_fix_mac_speed(&dwmac->pcs, phy_dev, speed);
Ley Foon Tanb4834c82014-08-20 14:33:33 +080098}
99
Dinh Nguyen801d2332014-03-26 22:45:10 -0500100static int socfpga_dwmac_parse_data(struct socfpga_dwmac *dwmac, struct device *dev)
101{
102 struct device_node *np = dev->of_node;
103 struct regmap *sys_mgr_base_addr;
104 u32 reg_offset, reg_shift;
Tien Hock Lohfb3bbdb2016-07-07 20:23:30 -0700105 int ret, index;
106 struct device_node *np_splitter = NULL;
107 struct device_node *np_sgmii_adapter = NULL;
Ley Foon Tanb4834c82014-08-20 14:33:33 +0800108 struct resource res_splitter;
Tien Hock Lohfb3bbdb2016-07-07 20:23:30 -0700109 struct resource res_tse_pcs;
110 struct resource res_sgmii_adapter;
Dinh Nguyen801d2332014-03-26 22:45:10 -0500111
Thor Thayer54a5afb2019-03-11 17:18:07 -0500112 sys_mgr_base_addr =
113 altr_sysmgr_regmap_lookup_by_phandle(np, "altr,sysmgr-syscon");
Dinh Nguyen801d2332014-03-26 22:45:10 -0500114 if (IS_ERR(sys_mgr_base_addr)) {
115 dev_info(dev, "No sysmgr-syscon node found\n");
116 return PTR_ERR(sys_mgr_base_addr);
117 }
118
119 ret = of_property_read_u32_index(np, "altr,sysmgr-syscon", 1, &reg_offset);
120 if (ret) {
121 dev_info(dev, "Could not read reg_offset from sysmgr-syscon!\n");
122 return -EINVAL;
123 }
124
125 ret = of_property_read_u32_index(np, "altr,sysmgr-syscon", 2, &reg_shift);
126 if (ret) {
127 dev_info(dev, "Could not read reg_shift from sysmgr-syscon!\n");
128 return -EINVAL;
129 }
130
Phil Reid43569812015-12-14 11:32:02 +0800131 dwmac->f2h_ptp_ref_clk = of_property_read_bool(np, "altr,f2h_ptp_ref_clk");
132
Ley Foon Tanb4834c82014-08-20 14:33:33 +0800133 np_splitter = of_parse_phandle(np, "altr,emac-splitter", 0);
134 if (np_splitter) {
Peter Chenf7113b32016-08-01 15:02:41 +0800135 ret = of_address_to_resource(np_splitter, 0, &res_splitter);
136 of_node_put(np_splitter);
137 if (ret) {
Ley Foon Tanb4834c82014-08-20 14:33:33 +0800138 dev_info(dev, "Missing emac splitter address\n");
139 return -EINVAL;
140 }
141
Ley Foon Tandace1b52014-08-28 12:59:46 +0800142 dwmac->splitter_base = devm_ioremap_resource(dev, &res_splitter);
Wei Yongjunf19f9162014-09-12 07:12:57 +0800143 if (IS_ERR(dwmac->splitter_base)) {
Ley Foon Tanb4834c82014-08-20 14:33:33 +0800144 dev_info(dev, "Failed to mapping emac splitter\n");
Wei Yongjunf19f9162014-09-12 07:12:57 +0800145 return PTR_ERR(dwmac->splitter_base);
Ley Foon Tanb4834c82014-08-20 14:33:33 +0800146 }
147 }
148
Tien Hock Lohfb3bbdb2016-07-07 20:23:30 -0700149 np_sgmii_adapter = of_parse_phandle(np,
150 "altr,gmii-to-sgmii-converter", 0);
151 if (np_sgmii_adapter) {
152 index = of_property_match_string(np_sgmii_adapter, "reg-names",
153 "hps_emac_interface_splitter_avalon_slave");
154
155 if (index >= 0) {
156 if (of_address_to_resource(np_sgmii_adapter, index,
157 &res_splitter)) {
158 dev_err(dev,
159 "%s: ERROR: missing emac splitter address\n",
160 __func__);
Peter Chenf7113b32016-08-01 15:02:41 +0800161 ret = -EINVAL;
162 goto err_node_put;
Tien Hock Lohfb3bbdb2016-07-07 20:23:30 -0700163 }
164
165 dwmac->splitter_base =
166 devm_ioremap_resource(dev, &res_splitter);
167
Peter Chenf7113b32016-08-01 15:02:41 +0800168 if (IS_ERR(dwmac->splitter_base)) {
169 ret = PTR_ERR(dwmac->splitter_base);
170 goto err_node_put;
171 }
Tien Hock Lohfb3bbdb2016-07-07 20:23:30 -0700172 }
173
174 index = of_property_match_string(np_sgmii_adapter, "reg-names",
175 "gmii_to_sgmii_adapter_avalon_slave");
176
177 if (index >= 0) {
178 if (of_address_to_resource(np_sgmii_adapter, index,
179 &res_sgmii_adapter)) {
180 dev_err(dev,
181 "%s: ERROR: failed mapping adapter\n",
182 __func__);
Peter Chenf7113b32016-08-01 15:02:41 +0800183 ret = -EINVAL;
184 goto err_node_put;
Tien Hock Lohfb3bbdb2016-07-07 20:23:30 -0700185 }
186
187 dwmac->pcs.sgmii_adapter_base =
188 devm_ioremap_resource(dev, &res_sgmii_adapter);
189
Peter Chenf7113b32016-08-01 15:02:41 +0800190 if (IS_ERR(dwmac->pcs.sgmii_adapter_base)) {
191 ret = PTR_ERR(dwmac->pcs.sgmii_adapter_base);
192 goto err_node_put;
193 }
Tien Hock Lohfb3bbdb2016-07-07 20:23:30 -0700194 }
195
196 index = of_property_match_string(np_sgmii_adapter, "reg-names",
197 "eth_tse_control_port");
198
199 if (index >= 0) {
200 if (of_address_to_resource(np_sgmii_adapter, index,
201 &res_tse_pcs)) {
202 dev_err(dev,
203 "%s: ERROR: failed mapping tse control port\n",
204 __func__);
Peter Chenf7113b32016-08-01 15:02:41 +0800205 ret = -EINVAL;
206 goto err_node_put;
Tien Hock Lohfb3bbdb2016-07-07 20:23:30 -0700207 }
208
209 dwmac->pcs.tse_pcs_base =
210 devm_ioremap_resource(dev, &res_tse_pcs);
211
Peter Chenf7113b32016-08-01 15:02:41 +0800212 if (IS_ERR(dwmac->pcs.tse_pcs_base)) {
213 ret = PTR_ERR(dwmac->pcs.tse_pcs_base);
214 goto err_node_put;
215 }
Tien Hock Lohfb3bbdb2016-07-07 20:23:30 -0700216 }
217 }
Dinh Nguyen801d2332014-03-26 22:45:10 -0500218 dwmac->reg_offset = reg_offset;
219 dwmac->reg_shift = reg_shift;
220 dwmac->sys_mgr_base_addr = sys_mgr_base_addr;
221 dwmac->dev = dev;
Peter Chenf7113b32016-08-01 15:02:41 +0800222 of_node_put(np_sgmii_adapter);
Dinh Nguyen801d2332014-03-26 22:45:10 -0500223
224 return 0;
Peter Chenf7113b32016-08-01 15:02:41 +0800225
226err_node_put:
227 of_node_put(np_sgmii_adapter);
228 return ret;
Dinh Nguyen801d2332014-03-26 22:45:10 -0500229}
230
Alexandru Ardelean5f109d42019-09-16 10:04:00 +0300231static int socfpga_get_plat_phymode(struct socfpga_dwmac *dwmac)
232{
233 struct net_device *ndev = dev_get_drvdata(dwmac->dev);
234 struct stmmac_priv *priv = netdev_priv(ndev);
235
236 return priv->plat->interface;
237}
238
Dinh Nguyen40ae2552019-06-05 10:05:51 -0500239static int socfpga_set_phy_mode_common(int phymode, u32 *val)
240{
241 switch (phymode) {
242 case PHY_INTERFACE_MODE_RGMII:
243 case PHY_INTERFACE_MODE_RGMII_ID:
Atsushi Nemotoa7a0d622020-04-14 10:12:34 +0900244 case PHY_INTERFACE_MODE_RGMII_RXID:
245 case PHY_INTERFACE_MODE_RGMII_TXID:
Dinh Nguyen40ae2552019-06-05 10:05:51 -0500246 *val = SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_RGMII;
247 break;
248 case PHY_INTERFACE_MODE_MII:
249 case PHY_INTERFACE_MODE_GMII:
250 case PHY_INTERFACE_MODE_SGMII:
251 *val = SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_GMII_MII;
252 break;
253 case PHY_INTERFACE_MODE_RMII:
254 *val = SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_RMII;
255 break;
256 default:
257 return -EINVAL;
258 }
259 return 0;
260}
261
262static int socfpga_gen5_set_phy_mode(struct socfpga_dwmac *dwmac)
Dinh Nguyen801d2332014-03-26 22:45:10 -0500263{
264 struct regmap *sys_mgr_base_addr = dwmac->sys_mgr_base_addr;
Alexandru Ardelean5f109d42019-09-16 10:04:00 +0300265 int phymode = socfpga_get_plat_phymode(dwmac);
Dinh Nguyen801d2332014-03-26 22:45:10 -0500266 u32 reg_offset = dwmac->reg_offset;
267 u32 reg_shift = dwmac->reg_shift;
Phil Reid734e00fa2016-04-07 15:55:35 +0800268 u32 ctrl, val, module;
Dinh Nguyen801d2332014-03-26 22:45:10 -0500269
Dinh Nguyen40ae2552019-06-05 10:05:51 -0500270 if (socfpga_set_phy_mode_common(phymode, &val)) {
Dinh Nguyen801d2332014-03-26 22:45:10 -0500271 dev_err(dwmac->dev, "bad phy mode %d\n", phymode);
272 return -EINVAL;
273 }
274
Ley Foon Tanb4834c82014-08-20 14:33:33 +0800275 /* Overwrite val to GMII if splitter core is enabled. The phymode here
276 * is the actual phy mode on phy hardware, but phy interface from
277 * EMAC core is GMII.
278 */
279 if (dwmac->splitter_base)
280 val = SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_GMII_MII;
281
Joachim Eastwood70cb1362016-05-01 22:58:21 +0200282 /* Assert reset to the enet controller before changing the phy mode */
Dinh Nguyenbc8a2d92018-06-19 10:35:38 -0500283 reset_control_assert(dwmac->stmmac_ocp_rst);
284 reset_control_assert(dwmac->stmmac_rst);
Joachim Eastwood70cb1362016-05-01 22:58:21 +0200285
Dinh Nguyen801d2332014-03-26 22:45:10 -0500286 regmap_read(sys_mgr_base_addr, reg_offset, &ctrl);
287 ctrl &= ~(SYSMGR_EMACGRP_CTRL_PHYSEL_MASK << reg_shift);
288 ctrl |= val << reg_shift;
289
Stephan Gatzka013dae52017-08-22 14:25:07 +0200290 if (dwmac->f2h_ptp_ref_clk ||
291 phymode == PHY_INTERFACE_MODE_MII ||
292 phymode == PHY_INTERFACE_MODE_GMII ||
293 phymode == PHY_INTERFACE_MODE_SGMII) {
Phil Reid734e00fa2016-04-07 15:55:35 +0800294 regmap_read(sys_mgr_base_addr, SYSMGR_FPGAGRP_MODULE_REG,
295 &module);
296 module |= (SYSMGR_FPGAGRP_MODULE_EMAC << (reg_shift / 2));
297 regmap_write(sys_mgr_base_addr, SYSMGR_FPGAGRP_MODULE_REG,
298 module);
Phil Reid734e00fa2016-04-07 15:55:35 +0800299 }
Phil Reid43569812015-12-14 11:32:02 +0800300
Julien Beraud15ce3062020-04-15 14:24:31 +0200301 if (dwmac->f2h_ptp_ref_clk)
302 ctrl |= SYSMGR_EMACGRP_CTRL_PTP_REF_CLK_MASK << (reg_shift / 2);
303 else
304 ctrl &= ~(SYSMGR_EMACGRP_CTRL_PTP_REF_CLK_MASK <<
305 (reg_shift / 2));
306
Dinh Nguyen801d2332014-03-26 22:45:10 -0500307 regmap_write(sys_mgr_base_addr, reg_offset, ctrl);
Phil Reid734e00fa2016-04-07 15:55:35 +0800308
Joachim Eastwood70cb1362016-05-01 22:58:21 +0200309 /* Deassert reset for the phy configuration to be sampled by
310 * the enet controller, and operation to start in requested mode
311 */
Dinh Nguyenbc8a2d92018-06-19 10:35:38 -0500312 reset_control_deassert(dwmac->stmmac_ocp_rst);
313 reset_control_deassert(dwmac->stmmac_rst);
Tien Hock Lohfb3bbdb2016-07-07 20:23:30 -0700314 if (phymode == PHY_INTERFACE_MODE_SGMII) {
315 if (tse_pcs_init(dwmac->pcs.tse_pcs_base, &dwmac->pcs) != 0) {
316 dev_err(dwmac->dev, "Unable to initialize TSE PCS");
317 return -EINVAL;
318 }
319 }
Joachim Eastwood70cb1362016-05-01 22:58:21 +0200320
Dinh Nguyen801d2332014-03-26 22:45:10 -0500321 return 0;
322}
323
Dinh Nguyen40ae2552019-06-05 10:05:51 -0500324static int socfpga_gen10_set_phy_mode(struct socfpga_dwmac *dwmac)
325{
326 struct regmap *sys_mgr_base_addr = dwmac->sys_mgr_base_addr;
Alexandru Ardelean5f109d42019-09-16 10:04:00 +0300327 int phymode = socfpga_get_plat_phymode(dwmac);
Dinh Nguyen40ae2552019-06-05 10:05:51 -0500328 u32 reg_offset = dwmac->reg_offset;
329 u32 reg_shift = dwmac->reg_shift;
330 u32 ctrl, val, module;
331
332 if (socfpga_set_phy_mode_common(phymode, &val))
333 return -EINVAL;
334
335 /* Overwrite val to GMII if splitter core is enabled. The phymode here
336 * is the actual phy mode on phy hardware, but phy interface from
337 * EMAC core is GMII.
338 */
339 if (dwmac->splitter_base)
340 val = SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_GMII_MII;
341
342 /* Assert reset to the enet controller before changing the phy mode */
343 reset_control_assert(dwmac->stmmac_ocp_rst);
344 reset_control_assert(dwmac->stmmac_rst);
345
346 regmap_read(sys_mgr_base_addr, reg_offset, &ctrl);
347 ctrl &= ~(SYSMGR_EMACGRP_CTRL_PHYSEL_MASK);
348 ctrl |= val;
349
350 if (dwmac->f2h_ptp_ref_clk ||
351 phymode == PHY_INTERFACE_MODE_MII ||
352 phymode == PHY_INTERFACE_MODE_GMII ||
353 phymode == PHY_INTERFACE_MODE_SGMII) {
354 ctrl |= SYSMGR_GEN10_EMACGRP_CTRL_PTP_REF_CLK_MASK;
355 regmap_read(sys_mgr_base_addr, SYSMGR_FPGAINTF_EMAC_REG,
356 &module);
357 module |= (SYSMGR_FPGAINTF_EMAC_BIT << reg_shift);
358 regmap_write(sys_mgr_base_addr, SYSMGR_FPGAINTF_EMAC_REG,
359 module);
360 } else {
361 ctrl &= ~SYSMGR_GEN10_EMACGRP_CTRL_PTP_REF_CLK_MASK;
362 }
363
364 regmap_write(sys_mgr_base_addr, reg_offset, ctrl);
365
366 /* Deassert reset for the phy configuration to be sampled by
367 * the enet controller, and operation to start in requested mode
368 */
369 reset_control_deassert(dwmac->stmmac_ocp_rst);
370 reset_control_deassert(dwmac->stmmac_rst);
371 if (phymode == PHY_INTERFACE_MODE_SGMII) {
372 if (tse_pcs_init(dwmac->pcs.tse_pcs_base, &dwmac->pcs) != 0) {
373 dev_err(dwmac->dev, "Unable to initialize TSE PCS");
374 return -EINVAL;
375 }
376 }
377 return 0;
378}
379
Joachim Eastwood8880b6c2015-07-29 00:08:52 +0200380static int socfpga_dwmac_probe(struct platform_device *pdev)
Joachim Eastwood82732782015-07-29 00:08:51 +0200381{
Joachim Eastwood8880b6c2015-07-29 00:08:52 +0200382 struct plat_stmmacenet_data *plat_dat;
383 struct stmmac_resources stmmac_res;
Joachim Eastwood82732782015-07-29 00:08:51 +0200384 struct device *dev = &pdev->dev;
385 int ret;
386 struct socfpga_dwmac *dwmac;
Johan Hovold50ac64c2016-11-30 15:29:49 +0100387 struct net_device *ndev;
388 struct stmmac_priv *stpriv;
Dinh Nguyen40ae2552019-06-05 10:05:51 -0500389 const struct socfpga_dwmac_ops *ops;
390
391 ops = device_get_match_data(&pdev->dev);
392 if (!ops) {
393 dev_err(&pdev->dev, "no of match data provided\n");
394 return -EINVAL;
395 }
Joachim Eastwood82732782015-07-29 00:08:51 +0200396
Joachim Eastwood8880b6c2015-07-29 00:08:52 +0200397 ret = stmmac_get_platform_resources(pdev, &stmmac_res);
398 if (ret)
399 return ret;
400
401 plat_dat = stmmac_probe_config_dt(pdev, &stmmac_res.mac);
402 if (IS_ERR(plat_dat))
403 return PTR_ERR(plat_dat);
404
Joachim Eastwood82732782015-07-29 00:08:51 +0200405 dwmac = devm_kzalloc(dev, sizeof(*dwmac), GFP_KERNEL);
Johan Hovoldd2ed0a72016-11-30 15:29:55 +0100406 if (!dwmac) {
407 ret = -ENOMEM;
408 goto err_remove_config_dt;
409 }
Joachim Eastwood82732782015-07-29 00:08:51 +0200410
Dinh Nguyenbc8a2d92018-06-19 10:35:38 -0500411 dwmac->stmmac_ocp_rst = devm_reset_control_get_optional(dev, "stmmaceth-ocp");
412 if (IS_ERR(dwmac->stmmac_ocp_rst)) {
413 ret = PTR_ERR(dwmac->stmmac_ocp_rst);
414 dev_err(dev, "error getting reset control of ocp %d\n", ret);
415 goto err_remove_config_dt;
416 }
417
418 reset_control_deassert(dwmac->stmmac_ocp_rst);
419
Joachim Eastwood82732782015-07-29 00:08:51 +0200420 ret = socfpga_dwmac_parse_data(dwmac, dev);
421 if (ret) {
422 dev_err(dev, "Unable to parse OF data\n");
Johan Hovoldd2ed0a72016-11-30 15:29:55 +0100423 goto err_remove_config_dt;
Joachim Eastwood82732782015-07-29 00:08:51 +0200424 }
425
Dinh Nguyen40ae2552019-06-05 10:05:51 -0500426 dwmac->ops = ops;
Joachim Eastwood8880b6c2015-07-29 00:08:52 +0200427 plat_dat->bsp_priv = dwmac;
Joachim Eastwood8880b6c2015-07-29 00:08:52 +0200428 plat_dat->fix_mac_speed = socfpga_dwmac_fix_mac_speed;
429
Marek Vasut3c201b52016-04-21 14:11:50 +0200430 ret = stmmac_dvr_probe(&pdev->dev, plat_dat, &stmmac_res);
Johan Hovold50ac64c2016-11-30 15:29:49 +0100431 if (ret)
Johan Hovoldd2ed0a72016-11-30 15:29:55 +0100432 goto err_remove_config_dt;
Tien Hock Lohfb3bbdb2016-07-07 20:23:30 -0700433
Johan Hovold50ac64c2016-11-30 15:29:49 +0100434 ndev = platform_get_drvdata(pdev);
435 stpriv = netdev_priv(ndev);
Joachim Eastwood70cb1362016-05-01 22:58:21 +0200436
Johan Hovold50ac64c2016-11-30 15:29:49 +0100437 /* The socfpga driver needs to control the stmmac reset to set the phy
438 * mode. Create a copy of the core reset handle so it can be used by
439 * the driver later.
440 */
jpintof573c0b2017-01-09 12:35:09 +0000441 dwmac->stmmac_rst = stpriv->plat->stmmac_rst;
Joachim Eastwood70cb1362016-05-01 22:58:21 +0200442
Dinh Nguyen40ae2552019-06-05 10:05:51 -0500443 ret = ops->set_phy_mode(dwmac);
Johan Hovold50ac64c2016-11-30 15:29:49 +0100444 if (ret)
445 goto err_dvr_remove;
446
447 return 0;
448
449err_dvr_remove:
450 stmmac_dvr_remove(&pdev->dev);
Johan Hovoldd2ed0a72016-11-30 15:29:55 +0100451err_remove_config_dt:
452 stmmac_remove_config_dt(pdev, plat_dat);
Joachim Eastwood8880b6c2015-07-29 00:08:52 +0200453
Marek Vasut3c201b52016-04-21 14:11:50 +0200454 return ret;
Joachim Eastwood82732782015-07-29 00:08:51 +0200455}
456
Joachim Eastwood56868de2016-05-01 22:58:20 +0200457#ifdef CONFIG_PM_SLEEP
458static int socfpga_dwmac_resume(struct device *dev)
459{
Joachim Eastwood56868de2016-05-01 22:58:20 +0200460 struct net_device *ndev = dev_get_drvdata(dev);
461 struct stmmac_priv *priv = netdev_priv(ndev);
Dinh Nguyen40ae2552019-06-05 10:05:51 -0500462 struct socfpga_dwmac *dwmac_priv = get_stmmac_bsp_priv(dev);
Joachim Eastwood56868de2016-05-01 22:58:20 +0200463
Dinh Nguyen40ae2552019-06-05 10:05:51 -0500464 dwmac_priv->ops->set_phy_mode(priv->plat->bsp_priv);
Joachim Eastwood56868de2016-05-01 22:58:20 +0200465
Joachim Eastwood53737242016-05-01 22:58:22 +0200466 /* Before the enet controller is suspended, the phy is suspended.
467 * This causes the phy clock to be gated. The enet controller is
468 * resumed before the phy, so the clock is still gated "off" when
469 * the enet controller is resumed. This code makes sure the phy
470 * is "resumed" before reinitializing the enet controller since
471 * the enet controller depends on an active phy clock to complete
472 * a DMA reset. A DMA reset will "time out" if executed
473 * with no phy clock input on the Synopsys enet controller.
474 * Verified through Synopsys Case #8000711656.
475 *
476 * Note that the phy clock is also gated when the phy is isolated.
477 * Phy "suspend" and "isolate" controls are located in phy basic
478 * control register 0, and can be modified by the phy driver
479 * framework.
480 */
Philippe Reynesd6d50c72016-10-03 08:28:19 +0200481 if (ndev->phydev)
482 phy_resume(ndev->phydev);
Joachim Eastwood53737242016-05-01 22:58:22 +0200483
Joachim Eastwood56868de2016-05-01 22:58:20 +0200484 return stmmac_resume(dev);
485}
486#endif /* CONFIG_PM_SLEEP */
487
Joachim Eastwoodbfca2eb2016-05-08 13:47:23 +0200488static SIMPLE_DEV_PM_OPS(socfpga_dwmac_pm_ops, stmmac_suspend,
489 socfpga_dwmac_resume);
Joachim Eastwood56868de2016-05-01 22:58:20 +0200490
Dinh Nguyen40ae2552019-06-05 10:05:51 -0500491static const struct socfpga_dwmac_ops socfpga_gen5_ops = {
492 .set_phy_mode = socfpga_gen5_set_phy_mode,
493};
494
495static const struct socfpga_dwmac_ops socfpga_gen10_ops = {
496 .set_phy_mode = socfpga_gen10_set_phy_mode,
497};
498
Joachim Eastwoodc7c52ae2015-05-14 12:11:03 +0200499static const struct of_device_id socfpga_dwmac_match[] = {
Dinh Nguyen40ae2552019-06-05 10:05:51 -0500500 { .compatible = "altr,socfpga-stmmac", .data = &socfpga_gen5_ops },
501 { .compatible = "altr,socfpga-stmmac-a10-s10", .data = &socfpga_gen10_ops },
Joachim Eastwoodc7c52ae2015-05-14 12:11:03 +0200502 { }
503};
504MODULE_DEVICE_TABLE(of, socfpga_dwmac_match);
505
506static struct platform_driver socfpga_dwmac_driver = {
Joachim Eastwood8880b6c2015-07-29 00:08:52 +0200507 .probe = socfpga_dwmac_probe,
Joachim Eastwoodc7c52ae2015-05-14 12:11:03 +0200508 .remove = stmmac_pltfr_remove,
509 .driver = {
510 .name = "socfpga-dwmac",
Joachim Eastwood56868de2016-05-01 22:58:20 +0200511 .pm = &socfpga_dwmac_pm_ops,
Joachim Eastwoodc7c52ae2015-05-14 12:11:03 +0200512 .of_match_table = socfpga_dwmac_match,
513 },
514};
515module_platform_driver(socfpga_dwmac_driver);
516
517MODULE_LICENSE("GPL v2");