blob: cd613d711108a989374a8ba1473e1aa7e6c75317 [file] [log] [blame]
Dinh Nguyen801d2332014-03-26 22:45:10 -05001/* Copyright Altera Corporation (C) 2014. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License, version 2,
5 * as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 *
15 * Adopted from dwmac-sti.c
16 */
17
18#include <linux/mfd/syscon.h>
19#include <linux/of.h>
Ley Foon Tanb4834c82014-08-20 14:33:33 +080020#include <linux/of_address.h>
Dinh Nguyen801d2332014-03-26 22:45:10 -050021#include <linux/of_net.h>
22#include <linux/phy.h>
23#include <linux/regmap.h>
Vince Bridgers2d871aa2014-07-28 14:07:58 -050024#include <linux/reset.h>
Dinh Nguyen801d2332014-03-26 22:45:10 -050025#include <linux/stmmac.h>
Vince Bridgers2d871aa2014-07-28 14:07:58 -050026#include "stmmac.h"
Dinh Nguyen801d2332014-03-26 22:45:10 -050027
28#define SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_GMII_MII 0x0
29#define SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_RGMII 0x1
30#define SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_RMII 0x2
31#define SYSMGR_EMACGRP_CTRL_PHYSEL_WIDTH 2
32#define SYSMGR_EMACGRP_CTRL_PHYSEL_MASK 0x00000003
33
Ley Foon Tanb4834c82014-08-20 14:33:33 +080034#define EMAC_SPLITTER_CTRL_REG 0x0
35#define EMAC_SPLITTER_CTRL_SPEED_MASK 0x3
36#define EMAC_SPLITTER_CTRL_SPEED_10 0x2
37#define EMAC_SPLITTER_CTRL_SPEED_100 0x3
38#define EMAC_SPLITTER_CTRL_SPEED_1000 0x0
39
Dinh Nguyen801d2332014-03-26 22:45:10 -050040struct socfpga_dwmac {
41 int interface;
42 u32 reg_offset;
43 u32 reg_shift;
44 struct device *dev;
45 struct regmap *sys_mgr_base_addr;
Vince Bridgers2d871aa2014-07-28 14:07:58 -050046 struct reset_control *stmmac_rst;
Ley Foon Tanb4834c82014-08-20 14:33:33 +080047 void __iomem *splitter_base;
Dinh Nguyen801d2332014-03-26 22:45:10 -050048};
49
Ley Foon Tanb4834c82014-08-20 14:33:33 +080050static void socfpga_dwmac_fix_mac_speed(void *priv, unsigned int speed)
51{
52 struct socfpga_dwmac *dwmac = (struct socfpga_dwmac *)priv;
53 void __iomem *splitter_base = dwmac->splitter_base;
54 u32 val;
55
56 if (!splitter_base)
57 return;
58
59 val = readl(splitter_base + EMAC_SPLITTER_CTRL_REG);
60 val &= ~EMAC_SPLITTER_CTRL_SPEED_MASK;
61
62 switch (speed) {
63 case 1000:
64 val |= EMAC_SPLITTER_CTRL_SPEED_1000;
65 break;
66 case 100:
67 val |= EMAC_SPLITTER_CTRL_SPEED_100;
68 break;
69 case 10:
70 val |= EMAC_SPLITTER_CTRL_SPEED_10;
71 break;
72 default:
73 return;
74 }
75
76 writel(val, splitter_base + EMAC_SPLITTER_CTRL_REG);
77}
78
Dinh Nguyen801d2332014-03-26 22:45:10 -050079static int socfpga_dwmac_parse_data(struct socfpga_dwmac *dwmac, struct device *dev)
80{
81 struct device_node *np = dev->of_node;
82 struct regmap *sys_mgr_base_addr;
83 u32 reg_offset, reg_shift;
84 int ret;
Ley Foon Tanb4834c82014-08-20 14:33:33 +080085 struct device_node *np_splitter;
86 struct resource res_splitter;
Dinh Nguyen801d2332014-03-26 22:45:10 -050087
Vince Bridgers2d871aa2014-07-28 14:07:58 -050088 dwmac->stmmac_rst = devm_reset_control_get(dev,
89 STMMAC_RESOURCE_NAME);
90 if (IS_ERR(dwmac->stmmac_rst)) {
91 dev_info(dev, "Could not get reset control!\n");
92 return -EINVAL;
93 }
94
Dinh Nguyen801d2332014-03-26 22:45:10 -050095 dwmac->interface = of_get_phy_mode(np);
96
97 sys_mgr_base_addr = syscon_regmap_lookup_by_phandle(np, "altr,sysmgr-syscon");
98 if (IS_ERR(sys_mgr_base_addr)) {
99 dev_info(dev, "No sysmgr-syscon node found\n");
100 return PTR_ERR(sys_mgr_base_addr);
101 }
102
103 ret = of_property_read_u32_index(np, "altr,sysmgr-syscon", 1, &reg_offset);
104 if (ret) {
105 dev_info(dev, "Could not read reg_offset from sysmgr-syscon!\n");
106 return -EINVAL;
107 }
108
109 ret = of_property_read_u32_index(np, "altr,sysmgr-syscon", 2, &reg_shift);
110 if (ret) {
111 dev_info(dev, "Could not read reg_shift from sysmgr-syscon!\n");
112 return -EINVAL;
113 }
114
Ley Foon Tanb4834c82014-08-20 14:33:33 +0800115 np_splitter = of_parse_phandle(np, "altr,emac-splitter", 0);
116 if (np_splitter) {
117 if (of_address_to_resource(np_splitter, 0, &res_splitter)) {
118 dev_info(dev, "Missing emac splitter address\n");
119 return -EINVAL;
120 }
121
122 dwmac->splitter_base = (void *)devm_ioremap_resource(dev,
123 &res_splitter);
124 if (!dwmac->splitter_base) {
125 dev_info(dev, "Failed to mapping emac splitter\n");
126 return -EINVAL;
127 }
128 }
129
Dinh Nguyen801d2332014-03-26 22:45:10 -0500130 dwmac->reg_offset = reg_offset;
131 dwmac->reg_shift = reg_shift;
132 dwmac->sys_mgr_base_addr = sys_mgr_base_addr;
133 dwmac->dev = dev;
134
135 return 0;
136}
137
138static int socfpga_dwmac_setup(struct socfpga_dwmac *dwmac)
139{
140 struct regmap *sys_mgr_base_addr = dwmac->sys_mgr_base_addr;
141 int phymode = dwmac->interface;
142 u32 reg_offset = dwmac->reg_offset;
143 u32 reg_shift = dwmac->reg_shift;
144 u32 ctrl, val;
145
146 switch (phymode) {
147 case PHY_INTERFACE_MODE_RGMII:
Ley Foon Tanb4834c82014-08-20 14:33:33 +0800148 case PHY_INTERFACE_MODE_RGMII_ID:
Dinh Nguyen801d2332014-03-26 22:45:10 -0500149 val = SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_RGMII;
150 break;
151 case PHY_INTERFACE_MODE_MII:
152 case PHY_INTERFACE_MODE_GMII:
153 val = SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_GMII_MII;
154 break;
155 default:
156 dev_err(dwmac->dev, "bad phy mode %d\n", phymode);
157 return -EINVAL;
158 }
159
Ley Foon Tanb4834c82014-08-20 14:33:33 +0800160 /* Overwrite val to GMII if splitter core is enabled. The phymode here
161 * is the actual phy mode on phy hardware, but phy interface from
162 * EMAC core is GMII.
163 */
164 if (dwmac->splitter_base)
165 val = SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_GMII_MII;
166
Dinh Nguyen801d2332014-03-26 22:45:10 -0500167 regmap_read(sys_mgr_base_addr, reg_offset, &ctrl);
168 ctrl &= ~(SYSMGR_EMACGRP_CTRL_PHYSEL_MASK << reg_shift);
169 ctrl |= val << reg_shift;
170
171 regmap_write(sys_mgr_base_addr, reg_offset, ctrl);
172 return 0;
173}
174
175static void *socfpga_dwmac_probe(struct platform_device *pdev)
176{
177 struct device *dev = &pdev->dev;
178 int ret;
179 struct socfpga_dwmac *dwmac;
180
181 dwmac = devm_kzalloc(dev, sizeof(*dwmac), GFP_KERNEL);
182 if (!dwmac)
183 return ERR_PTR(-ENOMEM);
184
185 ret = socfpga_dwmac_parse_data(dwmac, dev);
186 if (ret) {
187 dev_err(dev, "Unable to parse OF data\n");
188 return ERR_PTR(ret);
189 }
190
191 ret = socfpga_dwmac_setup(dwmac);
192 if (ret) {
193 dev_err(dev, "couldn't setup SoC glue (%d)\n", ret);
194 return ERR_PTR(ret);
195 }
196
197 return dwmac;
198}
199
Vince Bridgers2d871aa2014-07-28 14:07:58 -0500200static void socfpga_dwmac_exit(struct platform_device *pdev, void *priv)
201{
202 struct socfpga_dwmac *dwmac = priv;
203
204 /* On socfpga platform exit, assert and hold reset to the
205 * enet controller - the default state after a hard reset.
206 */
207 if (dwmac->stmmac_rst)
208 reset_control_assert(dwmac->stmmac_rst);
209}
210
211static int socfpga_dwmac_init(struct platform_device *pdev, void *priv)
212{
213 struct socfpga_dwmac *dwmac = priv;
214 struct net_device *ndev = platform_get_drvdata(pdev);
215 struct stmmac_priv *stpriv = NULL;
216 int ret = 0;
217
218 if (ndev)
219 stpriv = netdev_priv(ndev);
220
221 /* Assert reset to the enet controller before changing the phy mode */
222 if (dwmac->stmmac_rst)
223 reset_control_assert(dwmac->stmmac_rst);
224
225 /* Setup the phy mode in the system manager registers according to
226 * devicetree configuration
227 */
228 ret = socfpga_dwmac_setup(dwmac);
229
230 /* Deassert reset for the phy configuration to be sampled by
231 * the enet controller, and operation to start in requested mode
232 */
233 if (dwmac->stmmac_rst)
234 reset_control_deassert(dwmac->stmmac_rst);
235
236 /* Before the enet controller is suspended, the phy is suspended.
237 * This causes the phy clock to be gated. The enet controller is
238 * resumed before the phy, so the clock is still gated "off" when
239 * the enet controller is resumed. This code makes sure the phy
240 * is "resumed" before reinitializing the enet controller since
241 * the enet controller depends on an active phy clock to complete
242 * a DMA reset. A DMA reset will "time out" if executed
243 * with no phy clock input on the Synopsys enet controller.
244 * Verified through Synopsys Case #8000711656.
245 *
246 * Note that the phy clock is also gated when the phy is isolated.
247 * Phy "suspend" and "isolate" controls are located in phy basic
248 * control register 0, and can be modified by the phy driver
249 * framework.
250 */
251 if (stpriv && stpriv->phydev)
252 phy_resume(stpriv->phydev);
253
254 return ret;
255}
256
Dinh Nguyen801d2332014-03-26 22:45:10 -0500257const struct stmmac_of_data socfpga_gmac_data = {
258 .setup = socfpga_dwmac_probe,
Vince Bridgers2d871aa2014-07-28 14:07:58 -0500259 .init = socfpga_dwmac_init,
260 .exit = socfpga_dwmac_exit,
Ley Foon Tanb4834c82014-08-20 14:33:33 +0800261 .fix_mac_speed = socfpga_dwmac_fix_mac_speed,
Dinh Nguyen801d2332014-03-26 22:45:10 -0500262};