blob: 3e3232fd7c050e0bc06028d1d0c087e8b0007ae0 [file] [log] [blame]
John Crispin656e7052016-03-08 11:29:55 +01001/* This program is free software; you can redistribute it and/or modify
2 * it under the terms of the GNU General Public License as published by
3 * the Free Software Foundation; version 2 of the License
4 *
5 * This program is distributed in the hope that it will be useful,
6 * but WITHOUT ANY WARRANTY; without even the implied warranty of
7 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8 * GNU General Public License for more details.
9 *
10 * Copyright (C) 2009-2016 John Crispin <blogic@openwrt.org>
11 * Copyright (C) 2009-2016 Felix Fietkau <nbd@openwrt.org>
12 * Copyright (C) 2013-2016 Michael Lee <igvtee@gmail.com>
13 */
14
15#include <linux/of_device.h>
16#include <linux/of_mdio.h>
17#include <linux/of_net.h>
18#include <linux/mfd/syscon.h>
19#include <linux/regmap.h>
20#include <linux/clk.h>
Sean Wang26a2ad82016-09-14 23:13:18 +080021#include <linux/pm_runtime.h>
John Crispin656e7052016-03-08 11:29:55 +010022#include <linux/if_vlan.h>
23#include <linux/reset.h>
24#include <linux/tcp.h>
25
26#include "mtk_eth_soc.h"
27
28static int mtk_msg_level = -1;
29module_param_named(msg_level, mtk_msg_level, int, 0);
30MODULE_PARM_DESC(msg_level, "Message level (-1=defaults,0=none,...,16=all)");
31
32#define MTK_ETHTOOL_STAT(x) { #x, \
33 offsetof(struct mtk_hw_stats, x) / sizeof(u64) }
34
35/* strings used by ethtool */
36static const struct mtk_ethtool_stats {
37 char str[ETH_GSTRING_LEN];
38 u32 offset;
39} mtk_ethtool_stats[] = {
40 MTK_ETHTOOL_STAT(tx_bytes),
41 MTK_ETHTOOL_STAT(tx_packets),
42 MTK_ETHTOOL_STAT(tx_skip),
43 MTK_ETHTOOL_STAT(tx_collisions),
44 MTK_ETHTOOL_STAT(rx_bytes),
45 MTK_ETHTOOL_STAT(rx_packets),
46 MTK_ETHTOOL_STAT(rx_overflow),
47 MTK_ETHTOOL_STAT(rx_fcs_errors),
48 MTK_ETHTOOL_STAT(rx_short_errors),
49 MTK_ETHTOOL_STAT(rx_long_errors),
50 MTK_ETHTOOL_STAT(rx_checksum_errors),
51 MTK_ETHTOOL_STAT(rx_flow_control_packets),
52};
53
Sean Wang549e5492016-09-01 10:47:28 +080054static const char * const mtk_clks_source_name[] = {
Sean Wangf430dea2016-09-22 10:33:55 +080055 "ethif", "esw", "gp1", "gp2", "trgpll"
Sean Wang549e5492016-09-01 10:47:28 +080056};
57
John Crispin656e7052016-03-08 11:29:55 +010058void mtk_w32(struct mtk_eth *eth, u32 val, unsigned reg)
59{
60 __raw_writel(val, eth->base + reg);
61}
62
63u32 mtk_r32(struct mtk_eth *eth, unsigned reg)
64{
65 return __raw_readl(eth->base + reg);
66}
67
68static int mtk_mdio_busy_wait(struct mtk_eth *eth)
69{
70 unsigned long t_start = jiffies;
71
72 while (1) {
73 if (!(mtk_r32(eth, MTK_PHY_IAC) & PHY_IAC_ACCESS))
74 return 0;
75 if (time_after(jiffies, t_start + PHY_IAC_TIMEOUT))
76 break;
77 usleep_range(10, 20);
78 }
79
80 dev_err(eth->dev, "mdio: MDIO timeout\n");
81 return -1;
82}
83
Wei Yongjun379672d2016-07-12 11:36:44 +000084static u32 _mtk_mdio_write(struct mtk_eth *eth, u32 phy_addr,
85 u32 phy_register, u32 write_data)
John Crispin656e7052016-03-08 11:29:55 +010086{
87 if (mtk_mdio_busy_wait(eth))
88 return -1;
89
90 write_data &= 0xffff;
91
92 mtk_w32(eth, PHY_IAC_ACCESS | PHY_IAC_START | PHY_IAC_WRITE |
93 (phy_register << PHY_IAC_REG_SHIFT) |
94 (phy_addr << PHY_IAC_ADDR_SHIFT) | write_data,
95 MTK_PHY_IAC);
96
97 if (mtk_mdio_busy_wait(eth))
98 return -1;
99
100 return 0;
101}
102
Wei Yongjun379672d2016-07-12 11:36:44 +0000103static u32 _mtk_mdio_read(struct mtk_eth *eth, int phy_addr, int phy_reg)
John Crispin656e7052016-03-08 11:29:55 +0100104{
105 u32 d;
106
107 if (mtk_mdio_busy_wait(eth))
108 return 0xffff;
109
110 mtk_w32(eth, PHY_IAC_ACCESS | PHY_IAC_START | PHY_IAC_READ |
111 (phy_reg << PHY_IAC_REG_SHIFT) |
112 (phy_addr << PHY_IAC_ADDR_SHIFT),
113 MTK_PHY_IAC);
114
115 if (mtk_mdio_busy_wait(eth))
116 return 0xffff;
117
118 d = mtk_r32(eth, MTK_PHY_IAC) & 0xffff;
119
120 return d;
121}
122
123static int mtk_mdio_write(struct mii_bus *bus, int phy_addr,
124 int phy_reg, u16 val)
125{
126 struct mtk_eth *eth = bus->priv;
127
128 return _mtk_mdio_write(eth, phy_addr, phy_reg, val);
129}
130
131static int mtk_mdio_read(struct mii_bus *bus, int phy_addr, int phy_reg)
132{
133 struct mtk_eth *eth = bus->priv;
134
135 return _mtk_mdio_read(eth, phy_addr, phy_reg);
136}
137
Sean Wangf430dea2016-09-22 10:33:55 +0800138static void mtk_gmac0_rgmii_adjust(struct mtk_eth *eth, int speed)
139{
140 u32 val;
141 int ret;
142
143 val = (speed == SPEED_1000) ?
144 INTF_MODE_RGMII_1000 : INTF_MODE_RGMII_10_100;
145 mtk_w32(eth, val, INTF_MODE);
146
147 regmap_update_bits(eth->ethsys, ETHSYS_CLKCFG0,
148 ETHSYS_TRGMII_CLK_SEL362_5,
149 ETHSYS_TRGMII_CLK_SEL362_5);
150
151 val = (speed == SPEED_1000) ? 250000000 : 500000000;
152 ret = clk_set_rate(eth->clks[MTK_CLK_TRGPLL], val);
153 if (ret)
154 dev_err(eth->dev, "Failed to set trgmii pll: %d\n", ret);
155
156 val = (speed == SPEED_1000) ?
157 RCK_CTRL_RGMII_1000 : RCK_CTRL_RGMII_10_100;
158 mtk_w32(eth, val, TRGMII_RCK_CTRL);
159
160 val = (speed == SPEED_1000) ?
161 TCK_CTRL_RGMII_1000 : TCK_CTRL_RGMII_10_100;
162 mtk_w32(eth, val, TRGMII_TCK_CTRL);
163}
164
John Crispin656e7052016-03-08 11:29:55 +0100165static void mtk_phy_link_adjust(struct net_device *dev)
166{
167 struct mtk_mac *mac = netdev_priv(dev);
John Crispin08ef55c2016-06-03 10:17:07 +0200168 u16 lcl_adv = 0, rmt_adv = 0;
169 u8 flowctrl;
John Crispin656e7052016-03-08 11:29:55 +0100170 u32 mcr = MAC_MCR_MAX_RX_1536 | MAC_MCR_IPG_CFG |
171 MAC_MCR_FORCE_MODE | MAC_MCR_TX_EN |
172 MAC_MCR_RX_EN | MAC_MCR_BACKOFF_EN |
173 MAC_MCR_BACKPR_EN;
174
Sean Wangdce6fa42016-09-14 23:13:21 +0800175 if (unlikely(test_bit(MTK_RESETTING, &mac->hw->state)))
176 return;
177
Sean Wang2364c5c2016-09-22 16:33:35 +0800178 switch (dev->phydev->speed) {
John Crispin656e7052016-03-08 11:29:55 +0100179 case SPEED_1000:
180 mcr |= MAC_MCR_SPEED_1000;
181 break;
182 case SPEED_100:
183 mcr |= MAC_MCR_SPEED_100;
184 break;
185 };
186
Sean Wangf430dea2016-09-22 10:33:55 +0800187 if (mac->id == 0 && !mac->trgmii)
Sean Wang2364c5c2016-09-22 16:33:35 +0800188 mtk_gmac0_rgmii_adjust(mac->hw, dev->phydev->speed);
Sean Wangf430dea2016-09-22 10:33:55 +0800189
Sean Wang2364c5c2016-09-22 16:33:35 +0800190 if (dev->phydev->link)
John Crispin656e7052016-03-08 11:29:55 +0100191 mcr |= MAC_MCR_FORCE_LINK;
192
Sean Wang2364c5c2016-09-22 16:33:35 +0800193 if (dev->phydev->duplex) {
John Crispin656e7052016-03-08 11:29:55 +0100194 mcr |= MAC_MCR_FORCE_DPX;
195
Sean Wang2364c5c2016-09-22 16:33:35 +0800196 if (dev->phydev->pause)
John Crispin08ef55c2016-06-03 10:17:07 +0200197 rmt_adv = LPA_PAUSE_CAP;
Sean Wang2364c5c2016-09-22 16:33:35 +0800198 if (dev->phydev->asym_pause)
John Crispin08ef55c2016-06-03 10:17:07 +0200199 rmt_adv |= LPA_PAUSE_ASYM;
200
Sean Wang2364c5c2016-09-22 16:33:35 +0800201 if (dev->phydev->advertising & ADVERTISED_Pause)
John Crispin08ef55c2016-06-03 10:17:07 +0200202 lcl_adv |= ADVERTISE_PAUSE_CAP;
Sean Wang2364c5c2016-09-22 16:33:35 +0800203 if (dev->phydev->advertising & ADVERTISED_Asym_Pause)
John Crispin08ef55c2016-06-03 10:17:07 +0200204 lcl_adv |= ADVERTISE_PAUSE_ASYM;
205
206 flowctrl = mii_resolve_flowctrl_fdx(lcl_adv, rmt_adv);
207
208 if (flowctrl & FLOW_CTRL_TX)
209 mcr |= MAC_MCR_FORCE_TX_FC;
210 if (flowctrl & FLOW_CTRL_RX)
211 mcr |= MAC_MCR_FORCE_RX_FC;
212
213 netif_dbg(mac->hw, link, dev, "rx pause %s, tx pause %s\n",
214 flowctrl & FLOW_CTRL_RX ? "enabled" : "disabled",
215 flowctrl & FLOW_CTRL_TX ? "enabled" : "disabled");
216 }
John Crispin656e7052016-03-08 11:29:55 +0100217
218 mtk_w32(mac->hw, mcr, MTK_MAC_MCR(mac->id));
219
Sean Wang2364c5c2016-09-22 16:33:35 +0800220 if (dev->phydev->link)
John Crispin656e7052016-03-08 11:29:55 +0100221 netif_carrier_on(dev);
222 else
223 netif_carrier_off(dev);
John Crispin5969c422017-06-19 15:37:03 +0200224
225 if (!of_phy_is_fixed_link(mac->of_node))
226 phy_print_status(dev->phydev);
John Crispin656e7052016-03-08 11:29:55 +0100227}
228
229static int mtk_phy_connect_node(struct mtk_eth *eth, struct mtk_mac *mac,
230 struct device_node *phy_node)
231{
John Crispin656e7052016-03-08 11:29:55 +0100232 struct phy_device *phydev;
Sean Wanga2b2a192016-09-22 16:36:15 +0800233 int phy_mode;
John Crispin656e7052016-03-08 11:29:55 +0100234
John Crispin656e7052016-03-08 11:29:55 +0100235 phy_mode = of_get_phy_mode(phy_node);
236 if (phy_mode < 0) {
237 dev_err(eth->dev, "incorrect phy-mode %d\n", phy_mode);
238 return -EINVAL;
239 }
240
241 phydev = of_phy_connect(eth->netdev[mac->id], phy_node,
242 mtk_phy_link_adjust, 0, phy_mode);
Dan Carpenter977bc202016-03-15 10:18:49 +0300243 if (!phydev) {
John Crispin656e7052016-03-08 11:29:55 +0100244 dev_err(eth->dev, "could not connect to PHY\n");
Dan Carpenter977bc202016-03-15 10:18:49 +0300245 return -ENODEV;
John Crispin656e7052016-03-08 11:29:55 +0100246 }
247
248 dev_info(eth->dev,
249 "connected mac %d to PHY at %s [uid=%08x, driver=%s]\n",
250 mac->id, phydev_name(phydev), phydev->phy_id,
251 phydev->drv->name);
252
John Crispin656e7052016-03-08 11:29:55 +0100253 return 0;
254}
255
Sean Wang2364c5c2016-09-22 16:33:35 +0800256static int mtk_phy_connect(struct net_device *dev)
John Crispin656e7052016-03-08 11:29:55 +0100257{
Sean Wang2364c5c2016-09-22 16:33:35 +0800258 struct mtk_mac *mac = netdev_priv(dev);
259 struct mtk_eth *eth;
John Crispin656e7052016-03-08 11:29:55 +0100260 struct device_node *np;
Sean Wang9ea4d312016-09-14 23:13:19 +0800261 u32 val;
John Crispin656e7052016-03-08 11:29:55 +0100262
Sean Wang2364c5c2016-09-22 16:33:35 +0800263 eth = mac->hw;
John Crispin656e7052016-03-08 11:29:55 +0100264 np = of_parse_phandle(mac->of_node, "phy-handle", 0);
John Crispin0c72c502016-06-03 10:17:08 +0200265 if (!np && of_phy_is_fixed_link(mac->of_node))
266 if (!of_phy_register_fixed_link(mac->of_node))
267 np = of_node_get(mac->of_node);
John Crispin656e7052016-03-08 11:29:55 +0100268 if (!np)
269 return -ENODEV;
270
271 switch (of_get_phy_mode(np)) {
Sean Wang572de602016-09-22 10:33:54 +0800272 case PHY_INTERFACE_MODE_TRGMII:
273 mac->trgmii = true;
John Crispin37920fc2016-06-03 10:17:09 +0200274 case PHY_INTERFACE_MODE_RGMII_TXID:
275 case PHY_INTERFACE_MODE_RGMII_RXID:
276 case PHY_INTERFACE_MODE_RGMII_ID:
John Crispin656e7052016-03-08 11:29:55 +0100277 case PHY_INTERFACE_MODE_RGMII:
Sean Wang9ea4d312016-09-14 23:13:19 +0800278 mac->ge_mode = 0;
John Crispin656e7052016-03-08 11:29:55 +0100279 break;
280 case PHY_INTERFACE_MODE_MII:
Sean Wang9ea4d312016-09-14 23:13:19 +0800281 mac->ge_mode = 1;
John Crispin656e7052016-03-08 11:29:55 +0100282 break;
sean.wang@mediatek.com8ca7f4f2016-08-16 13:55:13 +0800283 case PHY_INTERFACE_MODE_REVMII:
Sean Wang9ea4d312016-09-14 23:13:19 +0800284 mac->ge_mode = 2;
John Crispin656e7052016-03-08 11:29:55 +0100285 break;
sean.wang@mediatek.com8ca7f4f2016-08-16 13:55:13 +0800286 case PHY_INTERFACE_MODE_RMII:
287 if (!mac->id)
288 goto err_phy;
Sean Wang9ea4d312016-09-14 23:13:19 +0800289 mac->ge_mode = 3;
sean.wang@mediatek.com8ca7f4f2016-08-16 13:55:13 +0800290 break;
John Crispin656e7052016-03-08 11:29:55 +0100291 default:
sean.wang@mediatek.com8ca7f4f2016-08-16 13:55:13 +0800292 goto err_phy;
John Crispin656e7052016-03-08 11:29:55 +0100293 }
294
295 /* put the gmac into the right mode */
296 regmap_read(eth->ethsys, ETHSYS_SYSCFG0, &val);
297 val &= ~SYSCFG0_GE_MODE(SYSCFG0_GE_MASK, mac->id);
Sean Wang9ea4d312016-09-14 23:13:19 +0800298 val |= SYSCFG0_GE_MODE(mac->ge_mode, mac->id);
John Crispin656e7052016-03-08 11:29:55 +0100299 regmap_write(eth->ethsys, ETHSYS_SYSCFG0, val);
300
Sean Wang2364c5c2016-09-22 16:33:35 +0800301 /* couple phydev to net_device */
Sean Wangf6f7d9c2016-09-22 16:44:16 +0800302 if (mtk_phy_connect_node(eth, mac, np))
303 goto err_phy;
304
Sean Wang2364c5c2016-09-22 16:33:35 +0800305 dev->phydev->autoneg = AUTONEG_ENABLE;
306 dev->phydev->speed = 0;
307 dev->phydev->duplex = 0;
sean.wang@mediatek.comb2025c72016-08-16 13:55:14 +0800308
309 if (of_phy_is_fixed_link(mac->of_node))
Sean Wang2364c5c2016-09-22 16:33:35 +0800310 dev->phydev->supported |=
sean.wang@mediatek.comb2025c72016-08-16 13:55:14 +0800311 SUPPORTED_Pause | SUPPORTED_Asym_Pause;
312
Sean Wang2364c5c2016-09-22 16:33:35 +0800313 dev->phydev->supported &= PHY_GBIT_FEATURES | SUPPORTED_Pause |
John Crispin08ef55c2016-06-03 10:17:07 +0200314 SUPPORTED_Asym_Pause;
Sean Wang2364c5c2016-09-22 16:33:35 +0800315 dev->phydev->advertising = dev->phydev->supported |
John Crispin656e7052016-03-08 11:29:55 +0100316 ADVERTISED_Autoneg;
Sean Wang2364c5c2016-09-22 16:33:35 +0800317 phy_start_aneg(dev->phydev);
John Crispin656e7052016-03-08 11:29:55 +0100318
sean.wang@mediatek.come8c29932016-08-13 19:16:19 +0800319 of_node_put(np);
320
John Crispin656e7052016-03-08 11:29:55 +0100321 return 0;
sean.wang@mediatek.com8ca7f4f2016-08-16 13:55:13 +0800322
323err_phy:
Johan Hovold16a67eb2016-11-28 19:25:05 +0100324 if (of_phy_is_fixed_link(mac->of_node))
325 of_phy_deregister_fixed_link(mac->of_node);
sean.wang@mediatek.com8ca7f4f2016-08-16 13:55:13 +0800326 of_node_put(np);
Sean Wangf6f7d9c2016-09-22 16:44:16 +0800327 dev_err(eth->dev, "%s: invalid phy\n", __func__);
sean.wang@mediatek.com8ca7f4f2016-08-16 13:55:13 +0800328 return -EINVAL;
John Crispin656e7052016-03-08 11:29:55 +0100329}
330
331static int mtk_mdio_init(struct mtk_eth *eth)
332{
333 struct device_node *mii_np;
Sean Wang1e515b72016-09-01 10:47:34 +0800334 int ret;
John Crispin656e7052016-03-08 11:29:55 +0100335
336 mii_np = of_get_child_by_name(eth->dev->of_node, "mdio-bus");
337 if (!mii_np) {
338 dev_err(eth->dev, "no %s child node found", "mdio-bus");
339 return -ENODEV;
340 }
341
342 if (!of_device_is_available(mii_np)) {
Sean Wangaa6e8a52016-09-01 10:47:35 +0800343 ret = -ENODEV;
John Crispin656e7052016-03-08 11:29:55 +0100344 goto err_put_node;
345 }
346
Sean Wang1e515b72016-09-01 10:47:34 +0800347 eth->mii_bus = devm_mdiobus_alloc(eth->dev);
John Crispin656e7052016-03-08 11:29:55 +0100348 if (!eth->mii_bus) {
Sean Wang1e515b72016-09-01 10:47:34 +0800349 ret = -ENOMEM;
John Crispin656e7052016-03-08 11:29:55 +0100350 goto err_put_node;
351 }
352
353 eth->mii_bus->name = "mdio";
354 eth->mii_bus->read = mtk_mdio_read;
355 eth->mii_bus->write = mtk_mdio_write;
356 eth->mii_bus->priv = eth;
357 eth->mii_bus->parent = eth->dev;
358
359 snprintf(eth->mii_bus->id, MII_BUS_ID_SIZE, "%s", mii_np->name);
Sean Wang1e515b72016-09-01 10:47:34 +0800360 ret = of_mdiobus_register(eth->mii_bus, mii_np);
John Crispin656e7052016-03-08 11:29:55 +0100361
362err_put_node:
363 of_node_put(mii_np);
Sean Wang1e515b72016-09-01 10:47:34 +0800364 return ret;
John Crispin656e7052016-03-08 11:29:55 +0100365}
366
367static void mtk_mdio_cleanup(struct mtk_eth *eth)
368{
369 if (!eth->mii_bus)
370 return;
371
372 mdiobus_unregister(eth->mii_bus);
John Crispin656e7052016-03-08 11:29:55 +0100373}
374
John Crispin5cce0322017-06-19 15:37:05 +0200375static inline void mtk_tx_irq_disable(struct mtk_eth *eth, u32 mask)
John Crispin656e7052016-03-08 11:29:55 +0100376{
John Crispin7bc9cce2016-06-29 13:38:10 +0200377 unsigned long flags;
John Crispin656e7052016-03-08 11:29:55 +0100378 u32 val;
379
John Crispin5cce0322017-06-19 15:37:05 +0200380 spin_lock_irqsave(&eth->tx_irq_lock, flags);
381 val = mtk_r32(eth, MTK_QDMA_INT_MASK);
382 mtk_w32(eth, val & ~mask, MTK_QDMA_INT_MASK);
383 spin_unlock_irqrestore(&eth->tx_irq_lock, flags);
John Crispin656e7052016-03-08 11:29:55 +0100384}
385
John Crispin5cce0322017-06-19 15:37:05 +0200386static inline void mtk_tx_irq_enable(struct mtk_eth *eth, u32 mask)
John Crispin656e7052016-03-08 11:29:55 +0100387{
John Crispin7bc9cce2016-06-29 13:38:10 +0200388 unsigned long flags;
John Crispin656e7052016-03-08 11:29:55 +0100389 u32 val;
390
John Crispin5cce0322017-06-19 15:37:05 +0200391 spin_lock_irqsave(&eth->tx_irq_lock, flags);
392 val = mtk_r32(eth, MTK_QDMA_INT_MASK);
393 mtk_w32(eth, val | mask, MTK_QDMA_INT_MASK);
394 spin_unlock_irqrestore(&eth->tx_irq_lock, flags);
395}
396
397static inline void mtk_rx_irq_disable(struct mtk_eth *eth, u32 mask)
398{
399 unsigned long flags;
400 u32 val;
401
402 spin_lock_irqsave(&eth->rx_irq_lock, flags);
403 val = mtk_r32(eth, MTK_PDMA_INT_MASK);
404 mtk_w32(eth, val & ~mask, MTK_PDMA_INT_MASK);
405 spin_unlock_irqrestore(&eth->rx_irq_lock, flags);
406}
407
408static inline void mtk_rx_irq_enable(struct mtk_eth *eth, u32 mask)
409{
410 unsigned long flags;
411 u32 val;
412
413 spin_lock_irqsave(&eth->rx_irq_lock, flags);
414 val = mtk_r32(eth, MTK_PDMA_INT_MASK);
415 mtk_w32(eth, val | mask, MTK_PDMA_INT_MASK);
416 spin_unlock_irqrestore(&eth->rx_irq_lock, flags);
John Crispin656e7052016-03-08 11:29:55 +0100417}
418
419static int mtk_set_mac_address(struct net_device *dev, void *p)
420{
421 int ret = eth_mac_addr(dev, p);
422 struct mtk_mac *mac = netdev_priv(dev);
423 const char *macaddr = dev->dev_addr;
John Crispin656e7052016-03-08 11:29:55 +0100424
425 if (ret)
426 return ret;
427
Sean Wangdce6fa42016-09-14 23:13:21 +0800428 if (unlikely(test_bit(MTK_RESETTING, &mac->hw->state)))
429 return -EBUSY;
430
Sean Wange3e96522016-08-11 17:51:00 +0800431 spin_lock_bh(&mac->hw->page_lock);
John Crispin656e7052016-03-08 11:29:55 +0100432 mtk_w32(mac->hw, (macaddr[0] << 8) | macaddr[1],
433 MTK_GDMA_MAC_ADRH(mac->id));
434 mtk_w32(mac->hw, (macaddr[2] << 24) | (macaddr[3] << 16) |
435 (macaddr[4] << 8) | macaddr[5],
436 MTK_GDMA_MAC_ADRL(mac->id));
Sean Wange3e96522016-08-11 17:51:00 +0800437 spin_unlock_bh(&mac->hw->page_lock);
John Crispin656e7052016-03-08 11:29:55 +0100438
439 return 0;
440}
441
442void mtk_stats_update_mac(struct mtk_mac *mac)
443{
444 struct mtk_hw_stats *hw_stats = mac->hw_stats;
445 unsigned int base = MTK_GDM1_TX_GBCNT;
446 u64 stats;
447
448 base += hw_stats->reg_offset;
449
450 u64_stats_update_begin(&hw_stats->syncp);
451
452 hw_stats->rx_bytes += mtk_r32(mac->hw, base);
453 stats = mtk_r32(mac->hw, base + 0x04);
454 if (stats)
455 hw_stats->rx_bytes += (stats << 32);
456 hw_stats->rx_packets += mtk_r32(mac->hw, base + 0x08);
457 hw_stats->rx_overflow += mtk_r32(mac->hw, base + 0x10);
458 hw_stats->rx_fcs_errors += mtk_r32(mac->hw, base + 0x14);
459 hw_stats->rx_short_errors += mtk_r32(mac->hw, base + 0x18);
460 hw_stats->rx_long_errors += mtk_r32(mac->hw, base + 0x1c);
461 hw_stats->rx_checksum_errors += mtk_r32(mac->hw, base + 0x20);
462 hw_stats->rx_flow_control_packets +=
463 mtk_r32(mac->hw, base + 0x24);
464 hw_stats->tx_skip += mtk_r32(mac->hw, base + 0x28);
465 hw_stats->tx_collisions += mtk_r32(mac->hw, base + 0x2c);
466 hw_stats->tx_bytes += mtk_r32(mac->hw, base + 0x30);
467 stats = mtk_r32(mac->hw, base + 0x34);
468 if (stats)
469 hw_stats->tx_bytes += (stats << 32);
470 hw_stats->tx_packets += mtk_r32(mac->hw, base + 0x38);
471 u64_stats_update_end(&hw_stats->syncp);
472}
473
474static void mtk_stats_update(struct mtk_eth *eth)
475{
476 int i;
477
478 for (i = 0; i < MTK_MAC_COUNT; i++) {
479 if (!eth->mac[i] || !eth->mac[i]->hw_stats)
480 continue;
481 if (spin_trylock(&eth->mac[i]->hw_stats->stats_lock)) {
482 mtk_stats_update_mac(eth->mac[i]);
483 spin_unlock(&eth->mac[i]->hw_stats->stats_lock);
484 }
485 }
486}
487
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800488static void mtk_get_stats64(struct net_device *dev,
489 struct rtnl_link_stats64 *storage)
John Crispin656e7052016-03-08 11:29:55 +0100490{
491 struct mtk_mac *mac = netdev_priv(dev);
492 struct mtk_hw_stats *hw_stats = mac->hw_stats;
493 unsigned int start;
494
495 if (netif_running(dev) && netif_device_present(dev)) {
Sean Wang8d32e062017-07-04 11:17:36 +0800496 if (spin_trylock_bh(&hw_stats->stats_lock)) {
John Crispin656e7052016-03-08 11:29:55 +0100497 mtk_stats_update_mac(mac);
Sean Wang8d32e062017-07-04 11:17:36 +0800498 spin_unlock_bh(&hw_stats->stats_lock);
John Crispin656e7052016-03-08 11:29:55 +0100499 }
500 }
501
502 do {
503 start = u64_stats_fetch_begin_irq(&hw_stats->syncp);
504 storage->rx_packets = hw_stats->rx_packets;
505 storage->tx_packets = hw_stats->tx_packets;
506 storage->rx_bytes = hw_stats->rx_bytes;
507 storage->tx_bytes = hw_stats->tx_bytes;
508 storage->collisions = hw_stats->tx_collisions;
509 storage->rx_length_errors = hw_stats->rx_short_errors +
510 hw_stats->rx_long_errors;
511 storage->rx_over_errors = hw_stats->rx_overflow;
512 storage->rx_crc_errors = hw_stats->rx_fcs_errors;
513 storage->rx_errors = hw_stats->rx_checksum_errors;
514 storage->tx_aborted_errors = hw_stats->tx_skip;
515 } while (u64_stats_fetch_retry_irq(&hw_stats->syncp, start));
516
517 storage->tx_errors = dev->stats.tx_errors;
518 storage->rx_dropped = dev->stats.rx_dropped;
519 storage->tx_dropped = dev->stats.tx_dropped;
John Crispin656e7052016-03-08 11:29:55 +0100520}
521
522static inline int mtk_max_frag_size(int mtu)
523{
524 /* make sure buf_size will be at least MTK_MAX_RX_LENGTH */
525 if (mtu + MTK_RX_ETH_HLEN < MTK_MAX_RX_LENGTH)
526 mtu = MTK_MAX_RX_LENGTH - MTK_RX_ETH_HLEN;
527
528 return SKB_DATA_ALIGN(MTK_RX_HLEN + mtu) +
529 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
530}
531
532static inline int mtk_max_buf_size(int frag_size)
533{
534 int buf_size = frag_size - NET_SKB_PAD - NET_IP_ALIGN -
535 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
536
537 WARN_ON(buf_size < MTK_MAX_RX_LENGTH);
538
539 return buf_size;
540}
541
542static inline void mtk_rx_get_desc(struct mtk_rx_dma *rxd,
543 struct mtk_rx_dma *dma_rxd)
544{
545 rxd->rxd1 = READ_ONCE(dma_rxd->rxd1);
546 rxd->rxd2 = READ_ONCE(dma_rxd->rxd2);
547 rxd->rxd3 = READ_ONCE(dma_rxd->rxd3);
548 rxd->rxd4 = READ_ONCE(dma_rxd->rxd4);
549}
550
551/* the qdma core needs scratch memory to be setup */
552static int mtk_init_fq_dma(struct mtk_eth *eth)
553{
John Crispin605e4fe2016-06-10 13:27:59 +0200554 dma_addr_t phy_ring_tail;
John Crispin656e7052016-03-08 11:29:55 +0100555 int cnt = MTK_DMA_SIZE;
556 dma_addr_t dma_addr;
557 int i;
558
559 eth->scratch_ring = dma_alloc_coherent(eth->dev,
560 cnt * sizeof(struct mtk_tx_dma),
John Crispin605e4fe2016-06-10 13:27:59 +0200561 &eth->phy_scratch_ring,
John Crispin656e7052016-03-08 11:29:55 +0100562 GFP_ATOMIC | __GFP_ZERO);
563 if (unlikely(!eth->scratch_ring))
564 return -ENOMEM;
565
566 eth->scratch_head = kcalloc(cnt, MTK_QDMA_PAGE_SIZE,
567 GFP_KERNEL);
John Crispin562c5a72016-06-10 13:27:58 +0200568 if (unlikely(!eth->scratch_head))
569 return -ENOMEM;
570
John Crispin656e7052016-03-08 11:29:55 +0100571 dma_addr = dma_map_single(eth->dev,
572 eth->scratch_head, cnt * MTK_QDMA_PAGE_SIZE,
573 DMA_FROM_DEVICE);
574 if (unlikely(dma_mapping_error(eth->dev, dma_addr)))
575 return -ENOMEM;
576
577 memset(eth->scratch_ring, 0x0, sizeof(struct mtk_tx_dma) * cnt);
John Crispin605e4fe2016-06-10 13:27:59 +0200578 phy_ring_tail = eth->phy_scratch_ring +
John Crispin656e7052016-03-08 11:29:55 +0100579 (sizeof(struct mtk_tx_dma) * (cnt - 1));
580
581 for (i = 0; i < cnt; i++) {
582 eth->scratch_ring[i].txd1 =
583 (dma_addr + (i * MTK_QDMA_PAGE_SIZE));
584 if (i < cnt - 1)
John Crispin605e4fe2016-06-10 13:27:59 +0200585 eth->scratch_ring[i].txd2 = (eth->phy_scratch_ring +
John Crispin656e7052016-03-08 11:29:55 +0100586 ((i + 1) * sizeof(struct mtk_tx_dma)));
587 eth->scratch_ring[i].txd3 = TX_DMA_SDL(MTK_QDMA_PAGE_SIZE);
588 }
589
John Crispin605e4fe2016-06-10 13:27:59 +0200590 mtk_w32(eth, eth->phy_scratch_ring, MTK_QDMA_FQ_HEAD);
John Crispin656e7052016-03-08 11:29:55 +0100591 mtk_w32(eth, phy_ring_tail, MTK_QDMA_FQ_TAIL);
592 mtk_w32(eth, (cnt << 16) | cnt, MTK_QDMA_FQ_CNT);
593 mtk_w32(eth, MTK_QDMA_PAGE_SIZE << 16, MTK_QDMA_FQ_BLEN);
594
595 return 0;
596}
597
598static inline void *mtk_qdma_phys_to_virt(struct mtk_tx_ring *ring, u32 desc)
599{
600 void *ret = ring->dma;
601
602 return ret + (desc - ring->phys);
603}
604
605static inline struct mtk_tx_buf *mtk_desc_to_tx_buf(struct mtk_tx_ring *ring,
606 struct mtk_tx_dma *txd)
607{
608 int idx = txd - ring->dma;
609
610 return &ring->buf[idx];
611}
612
sean.wang@mediatek.com55a4e772016-08-16 13:55:15 +0800613static void mtk_tx_unmap(struct mtk_eth *eth, struct mtk_tx_buf *tx_buf)
John Crispin656e7052016-03-08 11:29:55 +0100614{
615 if (tx_buf->flags & MTK_TX_FLAGS_SINGLE0) {
sean.wang@mediatek.com55a4e772016-08-16 13:55:15 +0800616 dma_unmap_single(eth->dev,
John Crispin656e7052016-03-08 11:29:55 +0100617 dma_unmap_addr(tx_buf, dma_addr0),
618 dma_unmap_len(tx_buf, dma_len0),
619 DMA_TO_DEVICE);
620 } else if (tx_buf->flags & MTK_TX_FLAGS_PAGE0) {
sean.wang@mediatek.com55a4e772016-08-16 13:55:15 +0800621 dma_unmap_page(eth->dev,
John Crispin656e7052016-03-08 11:29:55 +0100622 dma_unmap_addr(tx_buf, dma_addr0),
623 dma_unmap_len(tx_buf, dma_len0),
624 DMA_TO_DEVICE);
625 }
626 tx_buf->flags = 0;
627 if (tx_buf->skb &&
628 (tx_buf->skb != (struct sk_buff *)MTK_DMA_DUMMY_DESC))
629 dev_kfree_skb_any(tx_buf->skb);
630 tx_buf->skb = NULL;
631}
632
633static int mtk_tx_map(struct sk_buff *skb, struct net_device *dev,
634 int tx_num, struct mtk_tx_ring *ring, bool gso)
635{
636 struct mtk_mac *mac = netdev_priv(dev);
637 struct mtk_eth *eth = mac->hw;
638 struct mtk_tx_dma *itxd, *txd;
Sean Wang81d2dd02017-04-14 11:19:11 +0800639 struct mtk_tx_buf *itx_buf, *tx_buf;
John Crispin656e7052016-03-08 11:29:55 +0100640 dma_addr_t mapped_addr;
641 unsigned int nr_frags;
642 int i, n_desc = 1;
Sean Wangc6f1dc42016-09-01 10:47:27 +0800643 u32 txd4 = 0, fport;
John Crispin656e7052016-03-08 11:29:55 +0100644
645 itxd = ring->next_free;
646 if (itxd == ring->last_free)
647 return -ENOMEM;
648
649 /* set the forward port */
Sean Wangc6f1dc42016-09-01 10:47:27 +0800650 fport = (mac->id + 1) << TX_DMA_FPORT_SHIFT;
651 txd4 |= fport;
John Crispin656e7052016-03-08 11:29:55 +0100652
Sean Wang81d2dd02017-04-14 11:19:11 +0800653 itx_buf = mtk_desc_to_tx_buf(ring, itxd);
654 memset(itx_buf, 0, sizeof(*itx_buf));
John Crispin656e7052016-03-08 11:29:55 +0100655
656 if (gso)
657 txd4 |= TX_DMA_TSO;
658
659 /* TX Checksum offload */
660 if (skb->ip_summed == CHECKSUM_PARTIAL)
661 txd4 |= TX_DMA_CHKSUM;
662
663 /* VLAN header offload */
664 if (skb_vlan_tag_present(skb))
665 txd4 |= TX_DMA_INS_VLAN | skb_vlan_tag_get(skb);
666
sean.wang@mediatek.com55a4e772016-08-16 13:55:15 +0800667 mapped_addr = dma_map_single(eth->dev, skb->data,
John Crispin656e7052016-03-08 11:29:55 +0100668 skb_headlen(skb), DMA_TO_DEVICE);
sean.wang@mediatek.com55a4e772016-08-16 13:55:15 +0800669 if (unlikely(dma_mapping_error(eth->dev, mapped_addr)))
John Crispin656e7052016-03-08 11:29:55 +0100670 return -ENOMEM;
671
John Crispin656e7052016-03-08 11:29:55 +0100672 WRITE_ONCE(itxd->txd1, mapped_addr);
Sean Wang81d2dd02017-04-14 11:19:11 +0800673 itx_buf->flags |= MTK_TX_FLAGS_SINGLE0;
Sean Wang134d2152017-04-14 11:19:12 +0800674 itx_buf->flags |= (!mac->id) ? MTK_TX_FLAGS_FPORT0 :
675 MTK_TX_FLAGS_FPORT1;
Sean Wang81d2dd02017-04-14 11:19:11 +0800676 dma_unmap_addr_set(itx_buf, dma_addr0, mapped_addr);
677 dma_unmap_len_set(itx_buf, dma_len0, skb_headlen(skb));
John Crispin656e7052016-03-08 11:29:55 +0100678
679 /* TX SG offload */
680 txd = itxd;
681 nr_frags = skb_shinfo(skb)->nr_frags;
682 for (i = 0; i < nr_frags; i++) {
683 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i];
684 unsigned int offset = 0;
685 int frag_size = skb_frag_size(frag);
686
687 while (frag_size) {
688 bool last_frag = false;
689 unsigned int frag_map_size;
690
691 txd = mtk_qdma_phys_to_virt(ring, txd->txd2);
692 if (txd == ring->last_free)
693 goto err_dma;
694
695 n_desc++;
696 frag_map_size = min(frag_size, MTK_TX_DMA_BUF_LEN);
sean.wang@mediatek.com55a4e772016-08-16 13:55:15 +0800697 mapped_addr = skb_frag_dma_map(eth->dev, frag, offset,
John Crispin656e7052016-03-08 11:29:55 +0100698 frag_map_size,
699 DMA_TO_DEVICE);
sean.wang@mediatek.com55a4e772016-08-16 13:55:15 +0800700 if (unlikely(dma_mapping_error(eth->dev, mapped_addr)))
John Crispin656e7052016-03-08 11:29:55 +0100701 goto err_dma;
702
703 if (i == nr_frags - 1 &&
704 (frag_size - frag_map_size) == 0)
705 last_frag = true;
706
707 WRITE_ONCE(txd->txd1, mapped_addr);
708 WRITE_ONCE(txd->txd3, (TX_DMA_SWC |
709 TX_DMA_PLEN0(frag_map_size) |
John Crispin369f0452016-04-08 00:54:11 +0200710 last_frag * TX_DMA_LS0));
Sean Wangc6f1dc42016-09-01 10:47:27 +0800711 WRITE_ONCE(txd->txd4, fport);
John Crispin656e7052016-03-08 11:29:55 +0100712
John Crispin656e7052016-03-08 11:29:55 +0100713 tx_buf = mtk_desc_to_tx_buf(ring, txd);
714 memset(tx_buf, 0, sizeof(*tx_buf));
Sean Wang81d2dd02017-04-14 11:19:11 +0800715 tx_buf->skb = (struct sk_buff *)MTK_DMA_DUMMY_DESC;
John Crispin656e7052016-03-08 11:29:55 +0100716 tx_buf->flags |= MTK_TX_FLAGS_PAGE0;
Sean Wang134d2152017-04-14 11:19:12 +0800717 tx_buf->flags |= (!mac->id) ? MTK_TX_FLAGS_FPORT0 :
718 MTK_TX_FLAGS_FPORT1;
719
John Crispin656e7052016-03-08 11:29:55 +0100720 dma_unmap_addr_set(tx_buf, dma_addr0, mapped_addr);
721 dma_unmap_len_set(tx_buf, dma_len0, frag_map_size);
722 frag_size -= frag_map_size;
723 offset += frag_map_size;
724 }
725 }
726
727 /* store skb to cleanup */
Sean Wang81d2dd02017-04-14 11:19:11 +0800728 itx_buf->skb = skb;
John Crispin656e7052016-03-08 11:29:55 +0100729
730 WRITE_ONCE(itxd->txd4, txd4);
731 WRITE_ONCE(itxd->txd3, (TX_DMA_SWC | TX_DMA_PLEN0(skb_headlen(skb)) |
732 (!nr_frags * TX_DMA_LS0)));
733
John Crispin656e7052016-03-08 11:29:55 +0100734 netdev_sent_queue(dev, skb->len);
735 skb_tx_timestamp(skb);
736
737 ring->next_free = mtk_qdma_phys_to_virt(ring, txd->txd2);
738 atomic_sub(n_desc, &ring->free_count);
739
740 /* make sure that all changes to the dma ring are flushed before we
741 * continue
742 */
743 wmb();
744
745 if (netif_xmit_stopped(netdev_get_tx_queue(dev, 0)) || !skb->xmit_more)
746 mtk_w32(eth, txd->txd2, MTK_QTX_CTX_PTR);
747
748 return 0;
749
750err_dma:
751 do {
John Crispin2fae7232016-06-10 13:28:00 +0200752 tx_buf = mtk_desc_to_tx_buf(ring, itxd);
John Crispin656e7052016-03-08 11:29:55 +0100753
754 /* unmap dma */
sean.wang@mediatek.com55a4e772016-08-16 13:55:15 +0800755 mtk_tx_unmap(eth, tx_buf);
John Crispin656e7052016-03-08 11:29:55 +0100756
757 itxd->txd3 = TX_DMA_LS0 | TX_DMA_OWNER_CPU;
758 itxd = mtk_qdma_phys_to_virt(ring, itxd->txd2);
759 } while (itxd != txd);
760
761 return -ENOMEM;
762}
763
764static inline int mtk_cal_txd_req(struct sk_buff *skb)
765{
766 int i, nfrags;
767 struct skb_frag_struct *frag;
768
769 nfrags = 1;
770 if (skb_is_gso(skb)) {
771 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
772 frag = &skb_shinfo(skb)->frags[i];
773 nfrags += DIV_ROUND_UP(frag->size, MTK_TX_DMA_BUF_LEN);
774 }
775 } else {
776 nfrags += skb_shinfo(skb)->nr_frags;
777 }
778
John Crispinbeeb4ca2016-04-08 00:54:05 +0200779 return nfrags;
John Crispin656e7052016-03-08 11:29:55 +0100780}
781
John Crispinad3cba92016-06-10 13:28:07 +0200782static int mtk_queue_stopped(struct mtk_eth *eth)
783{
784 int i;
785
786 for (i = 0; i < MTK_MAC_COUNT; i++) {
787 if (!eth->netdev[i])
788 continue;
789 if (netif_queue_stopped(eth->netdev[i]))
790 return 1;
791 }
792
793 return 0;
794}
795
John Crispin13c822f2016-04-08 00:54:07 +0200796static void mtk_wake_queue(struct mtk_eth *eth)
797{
798 int i;
799
800 for (i = 0; i < MTK_MAC_COUNT; i++) {
801 if (!eth->netdev[i])
802 continue;
803 netif_wake_queue(eth->netdev[i]);
804 }
805}
806
807static void mtk_stop_queue(struct mtk_eth *eth)
808{
809 int i;
810
811 for (i = 0; i < MTK_MAC_COUNT; i++) {
812 if (!eth->netdev[i])
813 continue;
814 netif_stop_queue(eth->netdev[i]);
815 }
816}
817
John Crispin656e7052016-03-08 11:29:55 +0100818static int mtk_start_xmit(struct sk_buff *skb, struct net_device *dev)
819{
820 struct mtk_mac *mac = netdev_priv(dev);
821 struct mtk_eth *eth = mac->hw;
822 struct mtk_tx_ring *ring = &eth->tx_ring;
823 struct net_device_stats *stats = &dev->stats;
824 bool gso = false;
825 int tx_num;
826
John Crispin34c2e4c2016-04-08 00:54:08 +0200827 /* normally we can rely on the stack not calling this more than once,
828 * however we have 2 queues running on the same ring so we need to lock
829 * the ring access
830 */
Sean Wange3e96522016-08-11 17:51:00 +0800831 spin_lock(&eth->page_lock);
John Crispin34c2e4c2016-04-08 00:54:08 +0200832
Sean Wangdce6fa42016-09-14 23:13:21 +0800833 if (unlikely(test_bit(MTK_RESETTING, &eth->state)))
834 goto drop;
835
John Crispin656e7052016-03-08 11:29:55 +0100836 tx_num = mtk_cal_txd_req(skb);
837 if (unlikely(atomic_read(&ring->free_count) <= tx_num)) {
John Crispin13c822f2016-04-08 00:54:07 +0200838 mtk_stop_queue(eth);
John Crispin656e7052016-03-08 11:29:55 +0100839 netif_err(eth, tx_queued, dev,
840 "Tx Ring full when queue awake!\n");
Sean Wange3e96522016-08-11 17:51:00 +0800841 spin_unlock(&eth->page_lock);
John Crispin656e7052016-03-08 11:29:55 +0100842 return NETDEV_TX_BUSY;
843 }
844
845 /* TSO: fill MSS info in tcp checksum field */
846 if (skb_is_gso(skb)) {
847 if (skb_cow_head(skb, 0)) {
848 netif_warn(eth, tx_err, dev,
849 "GSO expand head fail.\n");
850 goto drop;
851 }
852
853 if (skb_shinfo(skb)->gso_type &
854 (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)) {
855 gso = true;
856 tcp_hdr(skb)->check = htons(skb_shinfo(skb)->gso_size);
857 }
858 }
859
860 if (mtk_tx_map(skb, dev, tx_num, ring, gso) < 0)
861 goto drop;
862
John Crispin82c65442016-06-10 13:28:08 +0200863 if (unlikely(atomic_read(&ring->free_count) <= ring->thresh))
John Crispin13c822f2016-04-08 00:54:07 +0200864 mtk_stop_queue(eth);
John Crispin82c65442016-06-10 13:28:08 +0200865
Sean Wange3e96522016-08-11 17:51:00 +0800866 spin_unlock(&eth->page_lock);
John Crispin656e7052016-03-08 11:29:55 +0100867
868 return NETDEV_TX_OK;
869
870drop:
Sean Wange3e96522016-08-11 17:51:00 +0800871 spin_unlock(&eth->page_lock);
John Crispin656e7052016-03-08 11:29:55 +0100872 stats->tx_dropped++;
Wei Yongjun81ad2b72016-10-20 17:00:32 +0000873 dev_kfree_skb_any(skb);
John Crispin656e7052016-03-08 11:29:55 +0100874 return NETDEV_TX_OK;
875}
876
Nelson Changee406812016-09-17 23:50:55 +0800877static struct mtk_rx_ring *mtk_get_rx_ring(struct mtk_eth *eth)
878{
879 int i;
880 struct mtk_rx_ring *ring;
881 int idx;
882
883 if (!eth->hwlro)
884 return &eth->rx_ring[0];
885
886 for (i = 0; i < MTK_MAX_RX_RING_NUM; i++) {
887 ring = &eth->rx_ring[i];
888 idx = NEXT_RX_DESP_IDX(ring->calc_idx, ring->dma_size);
889 if (ring->dma[idx].rxd2 & RX_DMA_DONE) {
890 ring->calc_idx_update = true;
891 return ring;
892 }
893 }
894
895 return NULL;
896}
897
898static void mtk_update_rx_cpu_idx(struct mtk_eth *eth)
899{
900 struct mtk_rx_ring *ring;
901 int i;
902
903 if (!eth->hwlro) {
904 ring = &eth->rx_ring[0];
905 mtk_w32(eth, ring->calc_idx, ring->crx_idx_reg);
906 } else {
907 for (i = 0; i < MTK_MAX_RX_RING_NUM; i++) {
908 ring = &eth->rx_ring[i];
909 if (ring->calc_idx_update) {
910 ring->calc_idx_update = false;
911 mtk_w32(eth, ring->calc_idx, ring->crx_idx_reg);
912 }
913 }
914 }
915}
916
John Crispin656e7052016-03-08 11:29:55 +0100917static int mtk_poll_rx(struct napi_struct *napi, int budget,
John Crispineece71e2016-06-29 13:38:09 +0200918 struct mtk_eth *eth)
John Crispin656e7052016-03-08 11:29:55 +0100919{
Nelson Changee406812016-09-17 23:50:55 +0800920 struct mtk_rx_ring *ring;
921 int idx;
John Crispin656e7052016-03-08 11:29:55 +0100922 struct sk_buff *skb;
923 u8 *data, *new_data;
924 struct mtk_rx_dma *rxd, trxd;
925 int done = 0;
926
927 while (done < budget) {
928 struct net_device *netdev;
929 unsigned int pktlen;
930 dma_addr_t dma_addr;
931 int mac = 0;
932
Nelson Changee406812016-09-17 23:50:55 +0800933 ring = mtk_get_rx_ring(eth);
934 if (unlikely(!ring))
935 goto rx_done;
936
937 idx = NEXT_RX_DESP_IDX(ring->calc_idx, ring->dma_size);
John Crispin656e7052016-03-08 11:29:55 +0100938 rxd = &ring->dma[idx];
939 data = ring->data[idx];
940
941 mtk_rx_get_desc(&trxd, rxd);
942 if (!(trxd.rxd2 & RX_DMA_DONE))
943 break;
944
945 /* find out which mac the packet come from. values start at 1 */
946 mac = (trxd.rxd4 >> RX_DMA_FPORT_SHIFT) &
947 RX_DMA_FPORT_MASK;
948 mac--;
949
Sean Wang6c7fce62017-07-22 20:45:55 +0800950 if (unlikely(mac < 0 || mac >= MTK_MAC_COUNT ||
951 !eth->netdev[mac]))
952 goto release_desc;
953
John Crispin656e7052016-03-08 11:29:55 +0100954 netdev = eth->netdev[mac];
955
Sean Wangdce6fa42016-09-14 23:13:21 +0800956 if (unlikely(test_bit(MTK_RESETTING, &eth->state)))
957 goto release_desc;
958
John Crispin656e7052016-03-08 11:29:55 +0100959 /* alloc new buffer */
960 new_data = napi_alloc_frag(ring->frag_size);
961 if (unlikely(!new_data)) {
962 netdev->stats.rx_dropped++;
963 goto release_desc;
964 }
sean.wang@mediatek.com55a4e772016-08-16 13:55:15 +0800965 dma_addr = dma_map_single(eth->dev,
John Crispin656e7052016-03-08 11:29:55 +0100966 new_data + NET_SKB_PAD,
967 ring->buf_size,
968 DMA_FROM_DEVICE);
sean.wang@mediatek.com55a4e772016-08-16 13:55:15 +0800969 if (unlikely(dma_mapping_error(eth->dev, dma_addr))) {
John Crispin656e7052016-03-08 11:29:55 +0100970 skb_free_frag(new_data);
John Crispin94321a92016-06-10 13:28:01 +0200971 netdev->stats.rx_dropped++;
John Crispin656e7052016-03-08 11:29:55 +0100972 goto release_desc;
973 }
974
975 /* receive data */
976 skb = build_skb(data, ring->frag_size);
977 if (unlikely(!skb)) {
Sean Wang1b430792016-09-01 10:47:29 +0800978 skb_free_frag(new_data);
John Crispin94321a92016-06-10 13:28:01 +0200979 netdev->stats.rx_dropped++;
John Crispin656e7052016-03-08 11:29:55 +0100980 goto release_desc;
981 }
982 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
983
sean.wang@mediatek.com55a4e772016-08-16 13:55:15 +0800984 dma_unmap_single(eth->dev, trxd.rxd1,
John Crispin656e7052016-03-08 11:29:55 +0100985 ring->buf_size, DMA_FROM_DEVICE);
986 pktlen = RX_DMA_GET_PLEN0(trxd.rxd2);
987 skb->dev = netdev;
988 skb_put(skb, pktlen);
989 if (trxd.rxd4 & RX_DMA_L4_VALID)
990 skb->ip_summed = CHECKSUM_UNNECESSARY;
991 else
992 skb_checksum_none_assert(skb);
993 skb->protocol = eth_type_trans(skb, netdev);
994
995 if (netdev->features & NETIF_F_HW_VLAN_CTAG_RX &&
996 RX_DMA_VID(trxd.rxd3))
997 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
998 RX_DMA_VID(trxd.rxd3));
John Crispina2d5e7b2017-06-19 15:37:06 +0200999 skb_record_rx_queue(skb, 0);
John Crispin656e7052016-03-08 11:29:55 +01001000 napi_gro_receive(napi, skb);
1001
1002 ring->data[idx] = new_data;
1003 rxd->rxd1 = (unsigned int)dma_addr;
1004
1005release_desc:
1006 rxd->rxd2 = RX_DMA_PLEN0(ring->buf_size);
1007
1008 ring->calc_idx = idx;
Sean Wang635372a2016-09-03 17:59:26 +08001009
John Crispin656e7052016-03-08 11:29:55 +01001010 done++;
1011 }
1012
Nelson Changee406812016-09-17 23:50:55 +08001013rx_done:
Sean Wang41156ce2016-09-03 17:59:27 +08001014 if (done) {
1015 /* make sure that all changes to the dma ring are flushed before
1016 * we continue
1017 */
1018 wmb();
Nelson Changee406812016-09-17 23:50:55 +08001019 mtk_update_rx_cpu_idx(eth);
Sean Wang41156ce2016-09-03 17:59:27 +08001020 }
John Crispin656e7052016-03-08 11:29:55 +01001021
1022 return done;
1023}
1024
John Crispin80673022016-06-29 13:38:11 +02001025static int mtk_poll_tx(struct mtk_eth *eth, int budget)
John Crispin656e7052016-03-08 11:29:55 +01001026{
1027 struct mtk_tx_ring *ring = &eth->tx_ring;
1028 struct mtk_tx_dma *desc;
1029 struct sk_buff *skb;
1030 struct mtk_tx_buf *tx_buf;
John Crispin80673022016-06-29 13:38:11 +02001031 unsigned int done[MTK_MAX_DEVS];
John Crispin656e7052016-03-08 11:29:55 +01001032 unsigned int bytes[MTK_MAX_DEVS];
1033 u32 cpu, dma;
1034 static int condition;
John Crispin80673022016-06-29 13:38:11 +02001035 int total = 0, i;
John Crispin656e7052016-03-08 11:29:55 +01001036
1037 memset(done, 0, sizeof(done));
1038 memset(bytes, 0, sizeof(bytes));
1039
1040 cpu = mtk_r32(eth, MTK_QTX_CRX_PTR);
1041 dma = mtk_r32(eth, MTK_QTX_DRX_PTR);
1042
1043 desc = mtk_qdma_phys_to_virt(ring, cpu);
1044
1045 while ((cpu != dma) && budget) {
1046 u32 next_cpu = desc->txd2;
Sean Wang134d2152017-04-14 11:19:12 +08001047 int mac = 0;
John Crispin656e7052016-03-08 11:29:55 +01001048
1049 desc = mtk_qdma_phys_to_virt(ring, desc->txd2);
1050 if ((desc->txd3 & TX_DMA_OWNER_CPU) == 0)
1051 break;
1052
John Crispin656e7052016-03-08 11:29:55 +01001053 tx_buf = mtk_desc_to_tx_buf(ring, desc);
Sean Wang134d2152017-04-14 11:19:12 +08001054 if (tx_buf->flags & MTK_TX_FLAGS_FPORT1)
1055 mac = 1;
1056
John Crispin656e7052016-03-08 11:29:55 +01001057 skb = tx_buf->skb;
1058 if (!skb) {
1059 condition = 1;
1060 break;
1061 }
1062
1063 if (skb != (struct sk_buff *)MTK_DMA_DUMMY_DESC) {
1064 bytes[mac] += skb->len;
1065 done[mac]++;
1066 budget--;
1067 }
sean.wang@mediatek.com55a4e772016-08-16 13:55:15 +08001068 mtk_tx_unmap(eth, tx_buf);
John Crispin656e7052016-03-08 11:29:55 +01001069
John Crispin656e7052016-03-08 11:29:55 +01001070 ring->last_free = desc;
1071 atomic_inc(&ring->free_count);
1072
1073 cpu = next_cpu;
1074 }
1075
1076 mtk_w32(eth, cpu, MTK_QTX_CRX_PTR);
1077
1078 for (i = 0; i < MTK_MAC_COUNT; i++) {
1079 if (!eth->netdev[i] || !done[i])
1080 continue;
1081 netdev_completed_queue(eth->netdev[i], done[i], bytes[i]);
1082 total += done[i];
1083 }
1084
John Crispinad3cba92016-06-10 13:28:07 +02001085 if (mtk_queue_stopped(eth) &&
1086 (atomic_read(&ring->free_count) > ring->thresh))
John Crispin13c822f2016-04-08 00:54:07 +02001087 mtk_wake_queue(eth);
John Crispin656e7052016-03-08 11:29:55 +01001088
1089 return total;
1090}
1091
John Crispin80673022016-06-29 13:38:11 +02001092static void mtk_handle_status_irq(struct mtk_eth *eth)
John Crispin656e7052016-03-08 11:29:55 +01001093{
John Crispin80673022016-06-29 13:38:11 +02001094 u32 status2 = mtk_r32(eth, MTK_INT_STATUS2);
John Crispin656e7052016-03-08 11:29:55 +01001095
John Crispineece71e2016-06-29 13:38:09 +02001096 if (unlikely(status2 & (MTK_GDM1_AF | MTK_GDM2_AF))) {
John Crispin656e7052016-03-08 11:29:55 +01001097 mtk_stats_update(eth);
John Crispineece71e2016-06-29 13:38:09 +02001098 mtk_w32(eth, (MTK_GDM1_AF | MTK_GDM2_AF),
1099 MTK_INT_STATUS2);
John Crispin656e7052016-03-08 11:29:55 +01001100 }
John Crispin80673022016-06-29 13:38:11 +02001101}
1102
1103static int mtk_napi_tx(struct napi_struct *napi, int budget)
1104{
1105 struct mtk_eth *eth = container_of(napi, struct mtk_eth, tx_napi);
1106 u32 status, mask;
1107 int tx_done = 0;
1108
1109 mtk_handle_status_irq(eth);
1110 mtk_w32(eth, MTK_TX_DONE_INT, MTK_QMTK_INT_STATUS);
1111 tx_done = mtk_poll_tx(eth, budget);
John Crispin656e7052016-03-08 11:29:55 +01001112
1113 if (unlikely(netif_msg_intr(eth))) {
John Crispin80673022016-06-29 13:38:11 +02001114 status = mtk_r32(eth, MTK_QMTK_INT_STATUS);
John Crispin656e7052016-03-08 11:29:55 +01001115 mask = mtk_r32(eth, MTK_QDMA_INT_MASK);
John Crispin80673022016-06-29 13:38:11 +02001116 dev_info(eth->dev,
1117 "done tx %d, intr 0x%08x/0x%x\n",
1118 tx_done, status, mask);
John Crispin656e7052016-03-08 11:29:55 +01001119 }
1120
John Crispin80673022016-06-29 13:38:11 +02001121 if (tx_done == budget)
John Crispin656e7052016-03-08 11:29:55 +01001122 return budget;
1123
1124 status = mtk_r32(eth, MTK_QMTK_INT_STATUS);
John Crispin80673022016-06-29 13:38:11 +02001125 if (status & MTK_TX_DONE_INT)
John Crispin656e7052016-03-08 11:29:55 +01001126 return budget;
1127
1128 napi_complete(napi);
John Crispin5cce0322017-06-19 15:37:05 +02001129 mtk_tx_irq_enable(eth, MTK_TX_DONE_INT);
John Crispin80673022016-06-29 13:38:11 +02001130
1131 return tx_done;
1132}
1133
1134static int mtk_napi_rx(struct napi_struct *napi, int budget)
1135{
1136 struct mtk_eth *eth = container_of(napi, struct mtk_eth, rx_napi);
1137 u32 status, mask;
1138 int rx_done = 0;
Sean Wang41156ce2016-09-03 17:59:27 +08001139 int remain_budget = budget;
John Crispin80673022016-06-29 13:38:11 +02001140
1141 mtk_handle_status_irq(eth);
Sean Wang41156ce2016-09-03 17:59:27 +08001142
1143poll_again:
Nelson Changbacfd112016-08-26 01:09:42 +08001144 mtk_w32(eth, MTK_RX_DONE_INT, MTK_PDMA_INT_STATUS);
Sean Wang41156ce2016-09-03 17:59:27 +08001145 rx_done = mtk_poll_rx(napi, remain_budget, eth);
John Crispin80673022016-06-29 13:38:11 +02001146
1147 if (unlikely(netif_msg_intr(eth))) {
Nelson Changbacfd112016-08-26 01:09:42 +08001148 status = mtk_r32(eth, MTK_PDMA_INT_STATUS);
1149 mask = mtk_r32(eth, MTK_PDMA_INT_MASK);
John Crispin80673022016-06-29 13:38:11 +02001150 dev_info(eth->dev,
1151 "done rx %d, intr 0x%08x/0x%x\n",
1152 rx_done, status, mask);
1153 }
Sean Wang41156ce2016-09-03 17:59:27 +08001154 if (rx_done == remain_budget)
John Crispin80673022016-06-29 13:38:11 +02001155 return budget;
1156
Nelson Changbacfd112016-08-26 01:09:42 +08001157 status = mtk_r32(eth, MTK_PDMA_INT_STATUS);
Sean Wang41156ce2016-09-03 17:59:27 +08001158 if (status & MTK_RX_DONE_INT) {
1159 remain_budget -= rx_done;
1160 goto poll_again;
1161 }
John Crispin80673022016-06-29 13:38:11 +02001162 napi_complete(napi);
John Crispin5cce0322017-06-19 15:37:05 +02001163 mtk_rx_irq_enable(eth, MTK_RX_DONE_INT);
John Crispin656e7052016-03-08 11:29:55 +01001164
Sean Wang41156ce2016-09-03 17:59:27 +08001165 return rx_done + budget - remain_budget;
John Crispin656e7052016-03-08 11:29:55 +01001166}
1167
1168static int mtk_tx_alloc(struct mtk_eth *eth)
1169{
1170 struct mtk_tx_ring *ring = &eth->tx_ring;
1171 int i, sz = sizeof(*ring->dma);
1172
1173 ring->buf = kcalloc(MTK_DMA_SIZE, sizeof(*ring->buf),
1174 GFP_KERNEL);
1175 if (!ring->buf)
1176 goto no_tx_mem;
1177
1178 ring->dma = dma_alloc_coherent(eth->dev,
1179 MTK_DMA_SIZE * sz,
1180 &ring->phys,
1181 GFP_ATOMIC | __GFP_ZERO);
1182 if (!ring->dma)
1183 goto no_tx_mem;
1184
1185 memset(ring->dma, 0, MTK_DMA_SIZE * sz);
1186 for (i = 0; i < MTK_DMA_SIZE; i++) {
1187 int next = (i + 1) % MTK_DMA_SIZE;
1188 u32 next_ptr = ring->phys + next * sz;
1189
1190 ring->dma[i].txd2 = next_ptr;
1191 ring->dma[i].txd3 = TX_DMA_LS0 | TX_DMA_OWNER_CPU;
1192 }
1193
1194 atomic_set(&ring->free_count, MTK_DMA_SIZE - 2);
1195 ring->next_free = &ring->dma[0];
John Crispin12c97c12016-06-10 13:28:06 +02001196 ring->last_free = &ring->dma[MTK_DMA_SIZE - 1];
John Crispin04698cc2016-06-10 13:28:04 +02001197 ring->thresh = MAX_SKB_FRAGS;
John Crispin656e7052016-03-08 11:29:55 +01001198
1199 /* make sure that all changes to the dma ring are flushed before we
1200 * continue
1201 */
1202 wmb();
1203
1204 mtk_w32(eth, ring->phys, MTK_QTX_CTX_PTR);
1205 mtk_w32(eth, ring->phys, MTK_QTX_DTX_PTR);
1206 mtk_w32(eth,
1207 ring->phys + ((MTK_DMA_SIZE - 1) * sz),
1208 MTK_QTX_CRX_PTR);
1209 mtk_w32(eth,
1210 ring->phys + ((MTK_DMA_SIZE - 1) * sz),
1211 MTK_QTX_DRX_PTR);
Nelson Changbacfd112016-08-26 01:09:42 +08001212 mtk_w32(eth, (QDMA_RES_THRES << 8) | QDMA_RES_THRES, MTK_QTX_CFG(0));
John Crispin656e7052016-03-08 11:29:55 +01001213
1214 return 0;
1215
1216no_tx_mem:
1217 return -ENOMEM;
1218}
1219
1220static void mtk_tx_clean(struct mtk_eth *eth)
1221{
1222 struct mtk_tx_ring *ring = &eth->tx_ring;
1223 int i;
1224
1225 if (ring->buf) {
1226 for (i = 0; i < MTK_DMA_SIZE; i++)
sean.wang@mediatek.com55a4e772016-08-16 13:55:15 +08001227 mtk_tx_unmap(eth, &ring->buf[i]);
John Crispin656e7052016-03-08 11:29:55 +01001228 kfree(ring->buf);
1229 ring->buf = NULL;
1230 }
1231
1232 if (ring->dma) {
1233 dma_free_coherent(eth->dev,
1234 MTK_DMA_SIZE * sizeof(*ring->dma),
1235 ring->dma,
1236 ring->phys);
1237 ring->dma = NULL;
1238 }
1239}
1240
Nelson Changee406812016-09-17 23:50:55 +08001241static int mtk_rx_alloc(struct mtk_eth *eth, int ring_no, int rx_flag)
John Crispin656e7052016-03-08 11:29:55 +01001242{
Nelson Changee406812016-09-17 23:50:55 +08001243 struct mtk_rx_ring *ring = &eth->rx_ring[ring_no];
1244 int rx_data_len, rx_dma_size;
John Crispin656e7052016-03-08 11:29:55 +01001245 int i;
1246
Nelson Changee406812016-09-17 23:50:55 +08001247 if (rx_flag == MTK_RX_FLAGS_HWLRO) {
1248 rx_data_len = MTK_MAX_LRO_RX_LENGTH;
1249 rx_dma_size = MTK_HW_LRO_DMA_SIZE;
1250 } else {
1251 rx_data_len = ETH_DATA_LEN;
1252 rx_dma_size = MTK_DMA_SIZE;
1253 }
1254
1255 ring->frag_size = mtk_max_frag_size(rx_data_len);
John Crispin656e7052016-03-08 11:29:55 +01001256 ring->buf_size = mtk_max_buf_size(ring->frag_size);
Nelson Changee406812016-09-17 23:50:55 +08001257 ring->data = kcalloc(rx_dma_size, sizeof(*ring->data),
John Crispin656e7052016-03-08 11:29:55 +01001258 GFP_KERNEL);
1259 if (!ring->data)
1260 return -ENOMEM;
1261
Nelson Changee406812016-09-17 23:50:55 +08001262 for (i = 0; i < rx_dma_size; i++) {
John Crispin656e7052016-03-08 11:29:55 +01001263 ring->data[i] = netdev_alloc_frag(ring->frag_size);
1264 if (!ring->data[i])
1265 return -ENOMEM;
1266 }
1267
1268 ring->dma = dma_alloc_coherent(eth->dev,
Nelson Changee406812016-09-17 23:50:55 +08001269 rx_dma_size * sizeof(*ring->dma),
John Crispin656e7052016-03-08 11:29:55 +01001270 &ring->phys,
1271 GFP_ATOMIC | __GFP_ZERO);
1272 if (!ring->dma)
1273 return -ENOMEM;
1274
Nelson Changee406812016-09-17 23:50:55 +08001275 for (i = 0; i < rx_dma_size; i++) {
John Crispin656e7052016-03-08 11:29:55 +01001276 dma_addr_t dma_addr = dma_map_single(eth->dev,
1277 ring->data[i] + NET_SKB_PAD,
1278 ring->buf_size,
1279 DMA_FROM_DEVICE);
1280 if (unlikely(dma_mapping_error(eth->dev, dma_addr)))
1281 return -ENOMEM;
1282 ring->dma[i].rxd1 = (unsigned int)dma_addr;
1283
1284 ring->dma[i].rxd2 = RX_DMA_PLEN0(ring->buf_size);
1285 }
Nelson Changee406812016-09-17 23:50:55 +08001286 ring->dma_size = rx_dma_size;
1287 ring->calc_idx_update = false;
1288 ring->calc_idx = rx_dma_size - 1;
1289 ring->crx_idx_reg = MTK_PRX_CRX_IDX_CFG(ring_no);
John Crispin656e7052016-03-08 11:29:55 +01001290 /* make sure that all changes to the dma ring are flushed before we
1291 * continue
1292 */
1293 wmb();
1294
Nelson Changee406812016-09-17 23:50:55 +08001295 mtk_w32(eth, ring->phys, MTK_PRX_BASE_PTR_CFG(ring_no));
1296 mtk_w32(eth, rx_dma_size, MTK_PRX_MAX_CNT_CFG(ring_no));
1297 mtk_w32(eth, ring->calc_idx, ring->crx_idx_reg);
1298 mtk_w32(eth, MTK_PST_DRX_IDX_CFG(ring_no), MTK_PDMA_RST_IDX);
John Crispin656e7052016-03-08 11:29:55 +01001299
1300 return 0;
1301}
1302
Nelson Changee406812016-09-17 23:50:55 +08001303static void mtk_rx_clean(struct mtk_eth *eth, int ring_no)
John Crispin656e7052016-03-08 11:29:55 +01001304{
Nelson Changee406812016-09-17 23:50:55 +08001305 struct mtk_rx_ring *ring = &eth->rx_ring[ring_no];
John Crispin656e7052016-03-08 11:29:55 +01001306 int i;
1307
1308 if (ring->data && ring->dma) {
Nelson Changee406812016-09-17 23:50:55 +08001309 for (i = 0; i < ring->dma_size; i++) {
John Crispin656e7052016-03-08 11:29:55 +01001310 if (!ring->data[i])
1311 continue;
1312 if (!ring->dma[i].rxd1)
1313 continue;
1314 dma_unmap_single(eth->dev,
1315 ring->dma[i].rxd1,
1316 ring->buf_size,
1317 DMA_FROM_DEVICE);
1318 skb_free_frag(ring->data[i]);
1319 }
1320 kfree(ring->data);
1321 ring->data = NULL;
1322 }
1323
1324 if (ring->dma) {
1325 dma_free_coherent(eth->dev,
Nelson Changee406812016-09-17 23:50:55 +08001326 ring->dma_size * sizeof(*ring->dma),
John Crispin656e7052016-03-08 11:29:55 +01001327 ring->dma,
1328 ring->phys);
1329 ring->dma = NULL;
1330 }
1331}
1332
Nelson Changee406812016-09-17 23:50:55 +08001333static int mtk_hwlro_rx_init(struct mtk_eth *eth)
1334{
1335 int i;
1336 u32 ring_ctrl_dw1 = 0, ring_ctrl_dw2 = 0, ring_ctrl_dw3 = 0;
1337 u32 lro_ctrl_dw0 = 0, lro_ctrl_dw3 = 0;
1338
1339 /* set LRO rings to auto-learn modes */
1340 ring_ctrl_dw2 |= MTK_RING_AUTO_LERAN_MODE;
1341
1342 /* validate LRO ring */
1343 ring_ctrl_dw2 |= MTK_RING_VLD;
1344
1345 /* set AGE timer (unit: 20us) */
1346 ring_ctrl_dw2 |= MTK_RING_AGE_TIME_H;
1347 ring_ctrl_dw1 |= MTK_RING_AGE_TIME_L;
1348
1349 /* set max AGG timer (unit: 20us) */
1350 ring_ctrl_dw2 |= MTK_RING_MAX_AGG_TIME;
1351
1352 /* set max LRO AGG count */
1353 ring_ctrl_dw2 |= MTK_RING_MAX_AGG_CNT_L;
1354 ring_ctrl_dw3 |= MTK_RING_MAX_AGG_CNT_H;
1355
1356 for (i = 1; i < MTK_MAX_RX_RING_NUM; i++) {
1357 mtk_w32(eth, ring_ctrl_dw1, MTK_LRO_CTRL_DW1_CFG(i));
1358 mtk_w32(eth, ring_ctrl_dw2, MTK_LRO_CTRL_DW2_CFG(i));
1359 mtk_w32(eth, ring_ctrl_dw3, MTK_LRO_CTRL_DW3_CFG(i));
1360 }
1361
1362 /* IPv4 checksum update enable */
1363 lro_ctrl_dw0 |= MTK_L3_CKS_UPD_EN;
1364
1365 /* switch priority comparison to packet count mode */
1366 lro_ctrl_dw0 |= MTK_LRO_ALT_PKT_CNT_MODE;
1367
1368 /* bandwidth threshold setting */
1369 mtk_w32(eth, MTK_HW_LRO_BW_THRE, MTK_PDMA_LRO_CTRL_DW2);
1370
1371 /* auto-learn score delta setting */
1372 mtk_w32(eth, MTK_HW_LRO_REPLACE_DELTA, MTK_PDMA_LRO_ALT_SCORE_DELTA);
1373
1374 /* set refresh timer for altering flows to 1 sec. (unit: 20us) */
1375 mtk_w32(eth, (MTK_HW_LRO_TIMER_UNIT << 16) | MTK_HW_LRO_REFRESH_TIME,
1376 MTK_PDMA_LRO_ALT_REFRESH_TIMER);
1377
1378 /* set HW LRO mode & the max aggregation count for rx packets */
1379 lro_ctrl_dw3 |= MTK_ADMA_MODE | (MTK_HW_LRO_MAX_AGG_CNT & 0xff);
1380
1381 /* the minimal remaining room of SDL0 in RXD for lro aggregation */
1382 lro_ctrl_dw3 |= MTK_LRO_MIN_RXD_SDL;
1383
1384 /* enable HW LRO */
1385 lro_ctrl_dw0 |= MTK_LRO_EN;
1386
1387 mtk_w32(eth, lro_ctrl_dw3, MTK_PDMA_LRO_CTRL_DW3);
1388 mtk_w32(eth, lro_ctrl_dw0, MTK_PDMA_LRO_CTRL_DW0);
1389
1390 return 0;
1391}
1392
1393static void mtk_hwlro_rx_uninit(struct mtk_eth *eth)
1394{
1395 int i;
1396 u32 val;
1397
1398 /* relinquish lro rings, flush aggregated packets */
1399 mtk_w32(eth, MTK_LRO_RING_RELINQUISH_REQ, MTK_PDMA_LRO_CTRL_DW0);
1400
1401 /* wait for relinquishments done */
1402 for (i = 0; i < 10; i++) {
1403 val = mtk_r32(eth, MTK_PDMA_LRO_CTRL_DW0);
1404 if (val & MTK_LRO_RING_RELINQUISH_DONE) {
1405 msleep(20);
1406 continue;
1407 }
Nelson Changca3ba102016-09-26 14:33:50 +08001408 break;
Nelson Changee406812016-09-17 23:50:55 +08001409 }
1410
1411 /* invalidate lro rings */
1412 for (i = 1; i < MTK_MAX_RX_RING_NUM; i++)
1413 mtk_w32(eth, 0, MTK_LRO_CTRL_DW2_CFG(i));
1414
1415 /* disable HW LRO */
1416 mtk_w32(eth, 0, MTK_PDMA_LRO_CTRL_DW0);
1417}
1418
Nelson Chang7aab7472016-09-17 23:50:56 +08001419static void mtk_hwlro_val_ipaddr(struct mtk_eth *eth, int idx, __be32 ip)
1420{
1421 u32 reg_val;
1422
1423 reg_val = mtk_r32(eth, MTK_LRO_CTRL_DW2_CFG(idx));
1424
1425 /* invalidate the IP setting */
1426 mtk_w32(eth, (reg_val & ~MTK_RING_MYIP_VLD), MTK_LRO_CTRL_DW2_CFG(idx));
1427
1428 mtk_w32(eth, ip, MTK_LRO_DIP_DW0_CFG(idx));
1429
1430 /* validate the IP setting */
1431 mtk_w32(eth, (reg_val | MTK_RING_MYIP_VLD), MTK_LRO_CTRL_DW2_CFG(idx));
1432}
1433
1434static void mtk_hwlro_inval_ipaddr(struct mtk_eth *eth, int idx)
1435{
1436 u32 reg_val;
1437
1438 reg_val = mtk_r32(eth, MTK_LRO_CTRL_DW2_CFG(idx));
1439
1440 /* invalidate the IP setting */
1441 mtk_w32(eth, (reg_val & ~MTK_RING_MYIP_VLD), MTK_LRO_CTRL_DW2_CFG(idx));
1442
1443 mtk_w32(eth, 0, MTK_LRO_DIP_DW0_CFG(idx));
1444}
1445
1446static int mtk_hwlro_get_ip_cnt(struct mtk_mac *mac)
1447{
1448 int cnt = 0;
1449 int i;
1450
1451 for (i = 0; i < MTK_MAX_LRO_IP_CNT; i++) {
1452 if (mac->hwlro_ip[i])
1453 cnt++;
1454 }
1455
1456 return cnt;
1457}
1458
1459static int mtk_hwlro_add_ipaddr(struct net_device *dev,
1460 struct ethtool_rxnfc *cmd)
1461{
1462 struct ethtool_rx_flow_spec *fsp =
1463 (struct ethtool_rx_flow_spec *)&cmd->fs;
1464 struct mtk_mac *mac = netdev_priv(dev);
1465 struct mtk_eth *eth = mac->hw;
1466 int hwlro_idx;
1467
1468 if ((fsp->flow_type != TCP_V4_FLOW) ||
1469 (!fsp->h_u.tcp_ip4_spec.ip4dst) ||
1470 (fsp->location > 1))
1471 return -EINVAL;
1472
1473 mac->hwlro_ip[fsp->location] = htonl(fsp->h_u.tcp_ip4_spec.ip4dst);
1474 hwlro_idx = (mac->id * MTK_MAX_LRO_IP_CNT) + fsp->location;
1475
1476 mac->hwlro_ip_cnt = mtk_hwlro_get_ip_cnt(mac);
1477
1478 mtk_hwlro_val_ipaddr(eth, hwlro_idx, mac->hwlro_ip[fsp->location]);
1479
1480 return 0;
1481}
1482
1483static int mtk_hwlro_del_ipaddr(struct net_device *dev,
1484 struct ethtool_rxnfc *cmd)
1485{
1486 struct ethtool_rx_flow_spec *fsp =
1487 (struct ethtool_rx_flow_spec *)&cmd->fs;
1488 struct mtk_mac *mac = netdev_priv(dev);
1489 struct mtk_eth *eth = mac->hw;
1490 int hwlro_idx;
1491
1492 if (fsp->location > 1)
1493 return -EINVAL;
1494
1495 mac->hwlro_ip[fsp->location] = 0;
1496 hwlro_idx = (mac->id * MTK_MAX_LRO_IP_CNT) + fsp->location;
1497
1498 mac->hwlro_ip_cnt = mtk_hwlro_get_ip_cnt(mac);
1499
1500 mtk_hwlro_inval_ipaddr(eth, hwlro_idx);
1501
1502 return 0;
1503}
1504
1505static void mtk_hwlro_netdev_disable(struct net_device *dev)
1506{
1507 struct mtk_mac *mac = netdev_priv(dev);
1508 struct mtk_eth *eth = mac->hw;
1509 int i, hwlro_idx;
1510
1511 for (i = 0; i < MTK_MAX_LRO_IP_CNT; i++) {
1512 mac->hwlro_ip[i] = 0;
1513 hwlro_idx = (mac->id * MTK_MAX_LRO_IP_CNT) + i;
1514
1515 mtk_hwlro_inval_ipaddr(eth, hwlro_idx);
1516 }
1517
1518 mac->hwlro_ip_cnt = 0;
1519}
1520
1521static int mtk_hwlro_get_fdir_entry(struct net_device *dev,
1522 struct ethtool_rxnfc *cmd)
1523{
1524 struct mtk_mac *mac = netdev_priv(dev);
1525 struct ethtool_rx_flow_spec *fsp =
1526 (struct ethtool_rx_flow_spec *)&cmd->fs;
1527
1528 /* only tcp dst ipv4 is meaningful, others are meaningless */
1529 fsp->flow_type = TCP_V4_FLOW;
1530 fsp->h_u.tcp_ip4_spec.ip4dst = ntohl(mac->hwlro_ip[fsp->location]);
1531 fsp->m_u.tcp_ip4_spec.ip4dst = 0;
1532
1533 fsp->h_u.tcp_ip4_spec.ip4src = 0;
1534 fsp->m_u.tcp_ip4_spec.ip4src = 0xffffffff;
1535 fsp->h_u.tcp_ip4_spec.psrc = 0;
1536 fsp->m_u.tcp_ip4_spec.psrc = 0xffff;
1537 fsp->h_u.tcp_ip4_spec.pdst = 0;
1538 fsp->m_u.tcp_ip4_spec.pdst = 0xffff;
1539 fsp->h_u.tcp_ip4_spec.tos = 0;
1540 fsp->m_u.tcp_ip4_spec.tos = 0xff;
1541
1542 return 0;
1543}
1544
1545static int mtk_hwlro_get_fdir_all(struct net_device *dev,
1546 struct ethtool_rxnfc *cmd,
1547 u32 *rule_locs)
1548{
1549 struct mtk_mac *mac = netdev_priv(dev);
1550 int cnt = 0;
1551 int i;
1552
1553 for (i = 0; i < MTK_MAX_LRO_IP_CNT; i++) {
1554 if (mac->hwlro_ip[i]) {
1555 rule_locs[cnt] = i;
1556 cnt++;
1557 }
1558 }
1559
1560 cmd->rule_cnt = cnt;
1561
1562 return 0;
1563}
1564
1565static netdev_features_t mtk_fix_features(struct net_device *dev,
1566 netdev_features_t features)
1567{
1568 if (!(features & NETIF_F_LRO)) {
1569 struct mtk_mac *mac = netdev_priv(dev);
1570 int ip_cnt = mtk_hwlro_get_ip_cnt(mac);
1571
1572 if (ip_cnt) {
1573 netdev_info(dev, "RX flow is programmed, LRO should keep on\n");
1574
1575 features |= NETIF_F_LRO;
1576 }
1577 }
1578
1579 return features;
1580}
1581
1582static int mtk_set_features(struct net_device *dev, netdev_features_t features)
1583{
1584 int err = 0;
1585
1586 if (!((dev->features ^ features) & NETIF_F_LRO))
1587 return 0;
1588
1589 if (!(features & NETIF_F_LRO))
1590 mtk_hwlro_netdev_disable(dev);
1591
1592 return err;
1593}
1594
John Crispin656e7052016-03-08 11:29:55 +01001595/* wait for DMA to finish whatever it is doing before we start using it again */
1596static int mtk_dma_busy_wait(struct mtk_eth *eth)
1597{
1598 unsigned long t_start = jiffies;
1599
1600 while (1) {
1601 if (!(mtk_r32(eth, MTK_QDMA_GLO_CFG) &
1602 (MTK_RX_DMA_BUSY | MTK_TX_DMA_BUSY)))
1603 return 0;
1604 if (time_after(jiffies, t_start + MTK_DMA_BUSY_TIMEOUT))
1605 break;
1606 }
1607
1608 dev_err(eth->dev, "DMA init timeout\n");
1609 return -1;
1610}
1611
1612static int mtk_dma_init(struct mtk_eth *eth)
1613{
1614 int err;
Nelson Changee406812016-09-17 23:50:55 +08001615 u32 i;
John Crispin656e7052016-03-08 11:29:55 +01001616
1617 if (mtk_dma_busy_wait(eth))
1618 return -EBUSY;
1619
1620 /* QDMA needs scratch memory for internal reordering of the
1621 * descriptors
1622 */
1623 err = mtk_init_fq_dma(eth);
1624 if (err)
1625 return err;
1626
1627 err = mtk_tx_alloc(eth);
1628 if (err)
1629 return err;
1630
Nelson Changee406812016-09-17 23:50:55 +08001631 err = mtk_rx_alloc(eth, 0, MTK_RX_FLAGS_NORMAL);
John Crispin656e7052016-03-08 11:29:55 +01001632 if (err)
1633 return err;
1634
Nelson Changee406812016-09-17 23:50:55 +08001635 if (eth->hwlro) {
1636 for (i = 1; i < MTK_MAX_RX_RING_NUM; i++) {
1637 err = mtk_rx_alloc(eth, i, MTK_RX_FLAGS_HWLRO);
1638 if (err)
1639 return err;
1640 }
1641 err = mtk_hwlro_rx_init(eth);
1642 if (err)
1643 return err;
1644 }
1645
John Crispin656e7052016-03-08 11:29:55 +01001646 /* Enable random early drop and set drop threshold automatically */
1647 mtk_w32(eth, FC_THRES_DROP_MODE | FC_THRES_DROP_EN | FC_THRES_MIN,
1648 MTK_QDMA_FC_THRES);
1649 mtk_w32(eth, 0x0, MTK_QDMA_HRED2);
1650
1651 return 0;
1652}
1653
1654static void mtk_dma_free(struct mtk_eth *eth)
1655{
1656 int i;
1657
1658 for (i = 0; i < MTK_MAC_COUNT; i++)
1659 if (eth->netdev[i])
1660 netdev_reset_queue(eth->netdev[i]);
John Crispin605e4fe2016-06-10 13:27:59 +02001661 if (eth->scratch_ring) {
1662 dma_free_coherent(eth->dev,
1663 MTK_DMA_SIZE * sizeof(struct mtk_tx_dma),
1664 eth->scratch_ring,
1665 eth->phy_scratch_ring);
1666 eth->scratch_ring = NULL;
1667 eth->phy_scratch_ring = 0;
1668 }
John Crispin656e7052016-03-08 11:29:55 +01001669 mtk_tx_clean(eth);
Nelson Changee406812016-09-17 23:50:55 +08001670 mtk_rx_clean(eth, 0);
1671
1672 if (eth->hwlro) {
1673 mtk_hwlro_rx_uninit(eth);
1674 for (i = 1; i < MTK_MAX_RX_RING_NUM; i++)
1675 mtk_rx_clean(eth, i);
1676 }
1677
John Crispin656e7052016-03-08 11:29:55 +01001678 kfree(eth->scratch_head);
1679}
1680
1681static void mtk_tx_timeout(struct net_device *dev)
1682{
1683 struct mtk_mac *mac = netdev_priv(dev);
1684 struct mtk_eth *eth = mac->hw;
1685
1686 eth->netdev[mac->id]->stats.tx_errors++;
1687 netif_err(eth, tx_err, dev,
1688 "transmit timed out\n");
John Crispin7c78b4a2016-04-08 00:54:10 +02001689 schedule_work(&eth->pending_work);
John Crispin656e7052016-03-08 11:29:55 +01001690}
1691
John Crispin80673022016-06-29 13:38:11 +02001692static irqreturn_t mtk_handle_irq_rx(int irq, void *_eth)
John Crispin656e7052016-03-08 11:29:55 +01001693{
1694 struct mtk_eth *eth = _eth;
John Crispin656e7052016-03-08 11:29:55 +01001695
John Crispin80673022016-06-29 13:38:11 +02001696 if (likely(napi_schedule_prep(&eth->rx_napi))) {
1697 __napi_schedule(&eth->rx_napi);
John Crispin5cce0322017-06-19 15:37:05 +02001698 mtk_rx_irq_disable(eth, MTK_RX_DONE_INT);
John Crispin656e7052016-03-08 11:29:55 +01001699 }
John Crispin80673022016-06-29 13:38:11 +02001700
1701 return IRQ_HANDLED;
1702}
1703
1704static irqreturn_t mtk_handle_irq_tx(int irq, void *_eth)
1705{
1706 struct mtk_eth *eth = _eth;
1707
1708 if (likely(napi_schedule_prep(&eth->tx_napi))) {
1709 __napi_schedule(&eth->tx_napi);
John Crispin5cce0322017-06-19 15:37:05 +02001710 mtk_tx_irq_disable(eth, MTK_TX_DONE_INT);
John Crispin80673022016-06-29 13:38:11 +02001711 }
John Crispin656e7052016-03-08 11:29:55 +01001712
1713 return IRQ_HANDLED;
1714}
1715
1716#ifdef CONFIG_NET_POLL_CONTROLLER
1717static void mtk_poll_controller(struct net_device *dev)
1718{
1719 struct mtk_mac *mac = netdev_priv(dev);
1720 struct mtk_eth *eth = mac->hw;
John Crispin656e7052016-03-08 11:29:55 +01001721
John Crispin5cce0322017-06-19 15:37:05 +02001722 mtk_tx_irq_disable(eth, MTK_TX_DONE_INT);
1723 mtk_rx_irq_disable(eth, MTK_RX_DONE_INT);
John Crispin8186f6e2016-07-02 08:00:50 +02001724 mtk_handle_irq_rx(eth->irq[2], dev);
John Crispin5cce0322017-06-19 15:37:05 +02001725 mtk_tx_irq_enable(eth, MTK_TX_DONE_INT);
1726 mtk_rx_irq_enable(eth, MTK_RX_DONE_INT);
John Crispin656e7052016-03-08 11:29:55 +01001727}
1728#endif
1729
1730static int mtk_start_dma(struct mtk_eth *eth)
1731{
1732 int err;
1733
1734 err = mtk_dma_init(eth);
1735 if (err) {
1736 mtk_dma_free(eth);
1737 return err;
1738 }
1739
1740 mtk_w32(eth,
Nelson Changbacfd112016-08-26 01:09:42 +08001741 MTK_TX_WB_DDONE | MTK_TX_DMA_EN |
1742 MTK_DMA_SIZE_16DWORDS | MTK_NDP_CO_PRO,
John Crispin656e7052016-03-08 11:29:55 +01001743 MTK_QDMA_GLO_CFG);
1744
Nelson Changbacfd112016-08-26 01:09:42 +08001745 mtk_w32(eth,
1746 MTK_RX_DMA_EN | MTK_RX_2B_OFFSET |
1747 MTK_RX_BT_32DWORDS | MTK_MULTI_EN,
1748 MTK_PDMA_GLO_CFG);
1749
John Crispin656e7052016-03-08 11:29:55 +01001750 return 0;
1751}
1752
1753static int mtk_open(struct net_device *dev)
1754{
1755 struct mtk_mac *mac = netdev_priv(dev);
1756 struct mtk_eth *eth = mac->hw;
1757
1758 /* we run 2 netdevs on the same dma ring so we only bring it up once */
1759 if (!atomic_read(&eth->dma_refcnt)) {
1760 int err = mtk_start_dma(eth);
1761
1762 if (err)
1763 return err;
1764
John Crispin80673022016-06-29 13:38:11 +02001765 napi_enable(&eth->tx_napi);
John Crispin656e7052016-03-08 11:29:55 +01001766 napi_enable(&eth->rx_napi);
John Crispin5cce0322017-06-19 15:37:05 +02001767 mtk_tx_irq_enable(eth, MTK_TX_DONE_INT);
1768 mtk_rx_irq_enable(eth, MTK_RX_DONE_INT);
John Crispin656e7052016-03-08 11:29:55 +01001769 }
1770 atomic_inc(&eth->dma_refcnt);
1771
Sean Wang2364c5c2016-09-22 16:33:35 +08001772 phy_start(dev->phydev);
John Crispin656e7052016-03-08 11:29:55 +01001773 netif_start_queue(dev);
1774
1775 return 0;
1776}
1777
1778static void mtk_stop_dma(struct mtk_eth *eth, u32 glo_cfg)
1779{
John Crispin656e7052016-03-08 11:29:55 +01001780 u32 val;
1781 int i;
1782
1783 /* stop the dma engine */
Sean Wange3e96522016-08-11 17:51:00 +08001784 spin_lock_bh(&eth->page_lock);
John Crispin656e7052016-03-08 11:29:55 +01001785 val = mtk_r32(eth, glo_cfg);
1786 mtk_w32(eth, val & ~(MTK_TX_WB_DDONE | MTK_RX_DMA_EN | MTK_TX_DMA_EN),
1787 glo_cfg);
Sean Wange3e96522016-08-11 17:51:00 +08001788 spin_unlock_bh(&eth->page_lock);
John Crispin656e7052016-03-08 11:29:55 +01001789
1790 /* wait for dma stop */
1791 for (i = 0; i < 10; i++) {
1792 val = mtk_r32(eth, glo_cfg);
1793 if (val & (MTK_TX_DMA_BUSY | MTK_RX_DMA_BUSY)) {
1794 msleep(20);
1795 continue;
1796 }
1797 break;
1798 }
1799}
1800
1801static int mtk_stop(struct net_device *dev)
1802{
1803 struct mtk_mac *mac = netdev_priv(dev);
1804 struct mtk_eth *eth = mac->hw;
1805
1806 netif_tx_disable(dev);
Sean Wang2364c5c2016-09-22 16:33:35 +08001807 phy_stop(dev->phydev);
John Crispin656e7052016-03-08 11:29:55 +01001808
1809 /* only shutdown DMA if this is the last user */
1810 if (!atomic_dec_and_test(&eth->dma_refcnt))
1811 return 0;
1812
John Crispin5cce0322017-06-19 15:37:05 +02001813 mtk_tx_irq_disable(eth, MTK_TX_DONE_INT);
1814 mtk_rx_irq_disable(eth, MTK_RX_DONE_INT);
John Crispin80673022016-06-29 13:38:11 +02001815 napi_disable(&eth->tx_napi);
John Crispin656e7052016-03-08 11:29:55 +01001816 napi_disable(&eth->rx_napi);
1817
1818 mtk_stop_dma(eth, MTK_QDMA_GLO_CFG);
Nelson Chang6bf563d2016-09-26 14:33:49 +08001819 mtk_stop_dma(eth, MTK_PDMA_GLO_CFG);
John Crispin656e7052016-03-08 11:29:55 +01001820
1821 mtk_dma_free(eth);
1822
1823 return 0;
1824}
1825
Sean Wang2a8307a2016-09-14 23:13:20 +08001826static void ethsys_reset(struct mtk_eth *eth, u32 reset_bits)
1827{
1828 regmap_update_bits(eth->ethsys, ETHSYS_RSTCTRL,
1829 reset_bits,
1830 reset_bits);
1831
1832 usleep_range(1000, 1100);
1833 regmap_update_bits(eth->ethsys, ETHSYS_RSTCTRL,
1834 reset_bits,
1835 ~reset_bits);
1836 mdelay(10);
1837}
1838
Sean Wang9ea4d312016-09-14 23:13:19 +08001839static int mtk_hw_init(struct mtk_eth *eth)
John Crispin656e7052016-03-08 11:29:55 +01001840{
Sean Wang9ea4d312016-09-14 23:13:19 +08001841 int i, val;
1842
1843 if (test_and_set_bit(MTK_HW_INIT, &eth->state))
1844 return 0;
Sean Wang85574db2016-09-14 23:13:15 +08001845
Sean Wang26a2ad82016-09-14 23:13:18 +08001846 pm_runtime_enable(eth->dev);
1847 pm_runtime_get_sync(eth->dev);
1848
Sean Wang85574db2016-09-14 23:13:15 +08001849 clk_prepare_enable(eth->clks[MTK_CLK_ETHIF]);
1850 clk_prepare_enable(eth->clks[MTK_CLK_ESW]);
1851 clk_prepare_enable(eth->clks[MTK_CLK_GP1]);
1852 clk_prepare_enable(eth->clks[MTK_CLK_GP2]);
Sean Wang2a8307a2016-09-14 23:13:20 +08001853 ethsys_reset(eth, RSTCTRL_FE);
1854 ethsys_reset(eth, RSTCTRL_PPE);
John Crispin656e7052016-03-08 11:29:55 +01001855
Sean Wang9ea4d312016-09-14 23:13:19 +08001856 regmap_read(eth->ethsys, ETHSYS_SYSCFG0, &val);
1857 for (i = 0; i < MTK_MAC_COUNT; i++) {
1858 if (!eth->mac[i])
1859 continue;
1860 val &= ~SYSCFG0_GE_MODE(SYSCFG0_GE_MASK, eth->mac[i]->id);
1861 val |= SYSCFG0_GE_MODE(eth->mac[i]->ge_mode, eth->mac[i]->id);
1862 }
1863 regmap_write(eth->ethsys, ETHSYS_SYSCFG0, val);
1864
John Crispin656e7052016-03-08 11:29:55 +01001865 /* Set GE2 driving and slew rate */
1866 regmap_write(eth->pctl, GPIO_DRV_SEL10, 0xa00);
1867
1868 /* set GE2 TDSEL */
1869 regmap_write(eth->pctl, GPIO_OD33_CTRL8, 0x5);
1870
1871 /* set GE2 TUNE */
1872 regmap_write(eth->pctl, GPIO_BIAS_CTRL, 0x0);
1873
1874 /* GE1, Force 1000M/FD, FC ON */
1875 mtk_w32(eth, MAC_MCR_FIXED_LINK, MTK_MAC_MCR(0));
1876
1877 /* GE2, Force 1000M/FD, FC ON */
1878 mtk_w32(eth, MAC_MCR_FIXED_LINK, MTK_MAC_MCR(1));
1879
Sean Wang87e3df42017-04-07 16:45:07 +08001880 /* Indicates CDM to parse the MTK special tag from CPU
1881 * which also is working out for untag packets.
1882 */
1883 val = mtk_r32(eth, MTK_CDMQ_IG_CTRL);
1884 mtk_w32(eth, val | MTK_CDMQ_STAG_EN, MTK_CDMQ_IG_CTRL);
1885
John Crispin656e7052016-03-08 11:29:55 +01001886 /* Enable RX VLan Offloading */
1887 mtk_w32(eth, 1, MTK_CDMP_EG_CTRL);
1888
John Crispin671d41e2017-06-19 15:37:04 +02001889 /* enable interrupt delay for RX */
1890 mtk_w32(eth, MTK_PDMA_DELAY_RX_DELAY, MTK_PDMA_DELAY_INT);
1891
John Crispin656e7052016-03-08 11:29:55 +01001892 /* disable delay and normal interrupt */
1893 mtk_w32(eth, 0, MTK_QDMA_DELAY_INT);
John Crispin5cce0322017-06-19 15:37:05 +02001894 mtk_tx_irq_disable(eth, ~0);
1895 mtk_rx_irq_disable(eth, ~0);
John Crispin656e7052016-03-08 11:29:55 +01001896 mtk_w32(eth, RST_GL_PSE, MTK_RST_GL);
1897 mtk_w32(eth, 0, MTK_RST_GL);
1898
1899 /* FE int grouping */
John Crispin80673022016-06-29 13:38:11 +02001900 mtk_w32(eth, MTK_TX_DONE_INT, MTK_PDMA_INT_GRP1);
1901 mtk_w32(eth, MTK_RX_DONE_INT, MTK_PDMA_INT_GRP2);
1902 mtk_w32(eth, MTK_TX_DONE_INT, MTK_QDMA_INT_GRP1);
1903 mtk_w32(eth, MTK_RX_DONE_INT, MTK_QDMA_INT_GRP2);
1904 mtk_w32(eth, 0x21021000, MTK_FE_INT_GRP);
John Crispin656e7052016-03-08 11:29:55 +01001905
1906 for (i = 0; i < 2; i++) {
1907 u32 val = mtk_r32(eth, MTK_GDMA_FWD_CFG(i));
1908
Nelson Chang9c084352016-08-26 01:09:43 +08001909 /* setup the forward port to send frame to PDMA */
John Crispin656e7052016-03-08 11:29:55 +01001910 val &= ~0xffff;
John Crispin656e7052016-03-08 11:29:55 +01001911
1912 /* Enable RX checksum */
1913 val |= MTK_GDMA_ICS_EN | MTK_GDMA_TCS_EN | MTK_GDMA_UCS_EN;
1914
1915 /* setup the mac dma */
1916 mtk_w32(eth, val, MTK_GDMA_FWD_CFG(i));
1917 }
1918
1919 return 0;
1920}
1921
Sean Wangbf253fb2016-09-14 23:13:16 +08001922static int mtk_hw_deinit(struct mtk_eth *eth)
1923{
Sean Wang9ea4d312016-09-14 23:13:19 +08001924 if (!test_and_clear_bit(MTK_HW_INIT, &eth->state))
1925 return 0;
1926
Sean Wangbf253fb2016-09-14 23:13:16 +08001927 clk_disable_unprepare(eth->clks[MTK_CLK_GP2]);
1928 clk_disable_unprepare(eth->clks[MTK_CLK_GP1]);
1929 clk_disable_unprepare(eth->clks[MTK_CLK_ESW]);
1930 clk_disable_unprepare(eth->clks[MTK_CLK_ETHIF]);
1931
Sean Wang26a2ad82016-09-14 23:13:18 +08001932 pm_runtime_put_sync(eth->dev);
1933 pm_runtime_disable(eth->dev);
1934
Sean Wangbf253fb2016-09-14 23:13:16 +08001935 return 0;
1936}
1937
John Crispin656e7052016-03-08 11:29:55 +01001938static int __init mtk_init(struct net_device *dev)
1939{
1940 struct mtk_mac *mac = netdev_priv(dev);
1941 struct mtk_eth *eth = mac->hw;
1942 const char *mac_addr;
1943
1944 mac_addr = of_get_mac_address(mac->of_node);
1945 if (mac_addr)
1946 ether_addr_copy(dev->dev_addr, mac_addr);
1947
1948 /* If the mac address is invalid, use random mac address */
1949 if (!is_valid_ether_addr(dev->dev_addr)) {
Tobias Klausere3c36e42017-03-07 16:27:10 +01001950 eth_hw_addr_random(dev);
John Crispin656e7052016-03-08 11:29:55 +01001951 dev_err(eth->dev, "generated random MAC address %pM\n",
1952 dev->dev_addr);
John Crispin656e7052016-03-08 11:29:55 +01001953 }
1954
Sean Wang2364c5c2016-09-22 16:33:35 +08001955 return mtk_phy_connect(dev);
John Crispin656e7052016-03-08 11:29:55 +01001956}
1957
1958static void mtk_uninit(struct net_device *dev)
1959{
1960 struct mtk_mac *mac = netdev_priv(dev);
1961 struct mtk_eth *eth = mac->hw;
1962
Sean Wang2364c5c2016-09-22 16:33:35 +08001963 phy_disconnect(dev->phydev);
Johan Hovold16a67eb2016-11-28 19:25:05 +01001964 if (of_phy_is_fixed_link(mac->of_node))
1965 of_phy_deregister_fixed_link(mac->of_node);
John Crispin5cce0322017-06-19 15:37:05 +02001966 mtk_tx_irq_disable(eth, ~0);
1967 mtk_rx_irq_disable(eth, ~0);
John Crispin656e7052016-03-08 11:29:55 +01001968}
1969
1970static int mtk_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1971{
John Crispin656e7052016-03-08 11:29:55 +01001972 switch (cmd) {
1973 case SIOCGMIIPHY:
1974 case SIOCGMIIREG:
1975 case SIOCSMIIREG:
Sean Wang2364c5c2016-09-22 16:33:35 +08001976 return phy_mii_ioctl(dev->phydev, ifr, cmd);
John Crispin656e7052016-03-08 11:29:55 +01001977 default:
1978 break;
1979 }
1980
1981 return -EOPNOTSUPP;
1982}
1983
1984static void mtk_pending_work(struct work_struct *work)
1985{
John Crispin7c78b4a2016-04-08 00:54:10 +02001986 struct mtk_eth *eth = container_of(work, struct mtk_eth, pending_work);
John Crispine7d425d2016-04-08 00:54:09 +02001987 int err, i;
1988 unsigned long restart = 0;
John Crispin656e7052016-03-08 11:29:55 +01001989
1990 rtnl_lock();
John Crispin656e7052016-03-08 11:29:55 +01001991
Sean Wangdce6fa42016-09-14 23:13:21 +08001992 dev_dbg(eth->dev, "[%s][%d] reset\n", __func__, __LINE__);
1993
1994 while (test_and_set_bit_lock(MTK_RESETTING, &eth->state))
1995 cpu_relax();
1996
1997 dev_dbg(eth->dev, "[%s][%d] mtk_stop starts\n", __func__, __LINE__);
John Crispine7d425d2016-04-08 00:54:09 +02001998 /* stop all devices to make sure that dma is properly shut down */
1999 for (i = 0; i < MTK_MAC_COUNT; i++) {
John Crispin7c78b4a2016-04-08 00:54:10 +02002000 if (!eth->netdev[i])
John Crispine7d425d2016-04-08 00:54:09 +02002001 continue;
2002 mtk_stop(eth->netdev[i]);
2003 __set_bit(i, &restart);
2004 }
Sean Wangdce6fa42016-09-14 23:13:21 +08002005 dev_dbg(eth->dev, "[%s][%d] mtk_stop ends\n", __func__, __LINE__);
John Crispine7d425d2016-04-08 00:54:09 +02002006
Sean Wang9ea4d312016-09-14 23:13:19 +08002007 /* restart underlying hardware such as power, clock, pin mux
2008 * and the connected phy
2009 */
2010 mtk_hw_deinit(eth);
2011
2012 if (eth->dev->pins)
2013 pinctrl_select_state(eth->dev->pins->p,
2014 eth->dev->pins->default_state);
2015 mtk_hw_init(eth);
2016
2017 for (i = 0; i < MTK_MAC_COUNT; i++) {
2018 if (!eth->mac[i] ||
2019 of_phy_is_fixed_link(eth->mac[i]->of_node))
2020 continue;
Sean Wang2364c5c2016-09-22 16:33:35 +08002021 err = phy_init_hw(eth->netdev[i]->phydev);
Sean Wang9ea4d312016-09-14 23:13:19 +08002022 if (err)
2023 dev_err(eth->dev, "%s: PHY init failed.\n",
2024 eth->netdev[i]->name);
2025 }
2026
John Crispine7d425d2016-04-08 00:54:09 +02002027 /* restart DMA and enable IRQs */
2028 for (i = 0; i < MTK_MAC_COUNT; i++) {
2029 if (!test_bit(i, &restart))
2030 continue;
2031 err = mtk_open(eth->netdev[i]);
2032 if (err) {
2033 netif_alert(eth, ifup, eth->netdev[i],
2034 "Driver up/down cycle failed, closing device.\n");
2035 dev_close(eth->netdev[i]);
2036 }
John Crispin656e7052016-03-08 11:29:55 +01002037 }
Sean Wangdce6fa42016-09-14 23:13:21 +08002038
2039 dev_dbg(eth->dev, "[%s][%d] reset done\n", __func__, __LINE__);
2040
2041 clear_bit_unlock(MTK_RESETTING, &eth->state);
2042
John Crispin656e7052016-03-08 11:29:55 +01002043 rtnl_unlock();
2044}
2045
Sean Wang8a8a9e82016-09-14 23:13:17 +08002046static int mtk_free_dev(struct mtk_eth *eth)
John Crispin656e7052016-03-08 11:29:55 +01002047{
2048 int i;
2049
2050 for (i = 0; i < MTK_MAC_COUNT; i++) {
John Crispin656e7052016-03-08 11:29:55 +01002051 if (!eth->netdev[i])
2052 continue;
John Crispin656e7052016-03-08 11:29:55 +01002053 free_netdev(eth->netdev[i]);
John Crispin656e7052016-03-08 11:29:55 +01002054 }
Sean Wang8a8a9e82016-09-14 23:13:17 +08002055
2056 return 0;
2057}
2058
2059static int mtk_unreg_dev(struct mtk_eth *eth)
2060{
2061 int i;
2062
2063 for (i = 0; i < MTK_MAC_COUNT; i++) {
2064 if (!eth->netdev[i])
2065 continue;
2066 unregister_netdev(eth->netdev[i]);
2067 }
2068
2069 return 0;
2070}
2071
2072static int mtk_cleanup(struct mtk_eth *eth)
2073{
2074 mtk_unreg_dev(eth);
2075 mtk_free_dev(eth);
John Crispin7c78b4a2016-04-08 00:54:10 +02002076 cancel_work_sync(&eth->pending_work);
John Crispin656e7052016-03-08 11:29:55 +01002077
2078 return 0;
2079}
2080
Baoyou Xie3a82e782016-09-30 15:48:50 +08002081static int mtk_get_link_ksettings(struct net_device *ndev,
2082 struct ethtool_link_ksettings *cmd)
John Crispin656e7052016-03-08 11:29:55 +01002083{
Sean Wang3e60b742016-09-22 16:42:03 +08002084 struct mtk_mac *mac = netdev_priv(ndev);
John Crispin656e7052016-03-08 11:29:55 +01002085
Sean Wangdce6fa42016-09-14 23:13:21 +08002086 if (unlikely(test_bit(MTK_RESETTING, &mac->hw->state)))
2087 return -EBUSY;
2088
yuval.shaia@oracle.com55141742017-06-13 10:09:46 +03002089 phy_ethtool_ksettings_get(ndev->phydev, cmd);
2090
2091 return 0;
John Crispin656e7052016-03-08 11:29:55 +01002092}
2093
Baoyou Xie3a82e782016-09-30 15:48:50 +08002094static int mtk_set_link_ksettings(struct net_device *ndev,
2095 const struct ethtool_link_ksettings *cmd)
John Crispin656e7052016-03-08 11:29:55 +01002096{
Sean Wang3e60b742016-09-22 16:42:03 +08002097 struct mtk_mac *mac = netdev_priv(ndev);
John Crispin656e7052016-03-08 11:29:55 +01002098
Sean Wang3e60b742016-09-22 16:42:03 +08002099 if (unlikely(test_bit(MTK_RESETTING, &mac->hw->state)))
2100 return -EBUSY;
John Crispin656e7052016-03-08 11:29:55 +01002101
Sean Wang3e60b742016-09-22 16:42:03 +08002102 return phy_ethtool_ksettings_set(ndev->phydev, cmd);
John Crispin656e7052016-03-08 11:29:55 +01002103}
2104
2105static void mtk_get_drvinfo(struct net_device *dev,
2106 struct ethtool_drvinfo *info)
2107{
2108 struct mtk_mac *mac = netdev_priv(dev);
2109
2110 strlcpy(info->driver, mac->hw->dev->driver->name, sizeof(info->driver));
2111 strlcpy(info->bus_info, dev_name(mac->hw->dev), sizeof(info->bus_info));
2112 info->n_stats = ARRAY_SIZE(mtk_ethtool_stats);
2113}
2114
2115static u32 mtk_get_msglevel(struct net_device *dev)
2116{
2117 struct mtk_mac *mac = netdev_priv(dev);
2118
2119 return mac->hw->msg_enable;
2120}
2121
2122static void mtk_set_msglevel(struct net_device *dev, u32 value)
2123{
2124 struct mtk_mac *mac = netdev_priv(dev);
2125
2126 mac->hw->msg_enable = value;
2127}
2128
2129static int mtk_nway_reset(struct net_device *dev)
2130{
2131 struct mtk_mac *mac = netdev_priv(dev);
2132
Sean Wangdce6fa42016-09-14 23:13:21 +08002133 if (unlikely(test_bit(MTK_RESETTING, &mac->hw->state)))
2134 return -EBUSY;
2135
Sean Wang2364c5c2016-09-22 16:33:35 +08002136 return genphy_restart_aneg(dev->phydev);
John Crispin656e7052016-03-08 11:29:55 +01002137}
2138
2139static u32 mtk_get_link(struct net_device *dev)
2140{
2141 struct mtk_mac *mac = netdev_priv(dev);
2142 int err;
2143
Sean Wangdce6fa42016-09-14 23:13:21 +08002144 if (unlikely(test_bit(MTK_RESETTING, &mac->hw->state)))
2145 return -EBUSY;
2146
Sean Wang2364c5c2016-09-22 16:33:35 +08002147 err = genphy_update_link(dev->phydev);
John Crispin656e7052016-03-08 11:29:55 +01002148 if (err)
2149 return ethtool_op_get_link(dev);
2150
Sean Wang2364c5c2016-09-22 16:33:35 +08002151 return dev->phydev->link;
John Crispin656e7052016-03-08 11:29:55 +01002152}
2153
2154static void mtk_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2155{
2156 int i;
2157
2158 switch (stringset) {
2159 case ETH_SS_STATS:
2160 for (i = 0; i < ARRAY_SIZE(mtk_ethtool_stats); i++) {
2161 memcpy(data, mtk_ethtool_stats[i].str, ETH_GSTRING_LEN);
2162 data += ETH_GSTRING_LEN;
2163 }
2164 break;
2165 }
2166}
2167
2168static int mtk_get_sset_count(struct net_device *dev, int sset)
2169{
2170 switch (sset) {
2171 case ETH_SS_STATS:
2172 return ARRAY_SIZE(mtk_ethtool_stats);
2173 default:
2174 return -EOPNOTSUPP;
2175 }
2176}
2177
2178static void mtk_get_ethtool_stats(struct net_device *dev,
2179 struct ethtool_stats *stats, u64 *data)
2180{
2181 struct mtk_mac *mac = netdev_priv(dev);
2182 struct mtk_hw_stats *hwstats = mac->hw_stats;
2183 u64 *data_src, *data_dst;
2184 unsigned int start;
2185 int i;
2186
Sean Wangdce6fa42016-09-14 23:13:21 +08002187 if (unlikely(test_bit(MTK_RESETTING, &mac->hw->state)))
2188 return;
2189
John Crispin656e7052016-03-08 11:29:55 +01002190 if (netif_running(dev) && netif_device_present(dev)) {
Sean Wang8d32e062017-07-04 11:17:36 +08002191 if (spin_trylock_bh(&hwstats->stats_lock)) {
John Crispin656e7052016-03-08 11:29:55 +01002192 mtk_stats_update_mac(mac);
Sean Wang8d32e062017-07-04 11:17:36 +08002193 spin_unlock_bh(&hwstats->stats_lock);
John Crispin656e7052016-03-08 11:29:55 +01002194 }
2195 }
2196
Sean Wang94d308d2016-09-20 11:26:48 +08002197 data_src = (u64 *)hwstats;
2198
John Crispin656e7052016-03-08 11:29:55 +01002199 do {
John Crispin656e7052016-03-08 11:29:55 +01002200 data_dst = data;
2201 start = u64_stats_fetch_begin_irq(&hwstats->syncp);
2202
2203 for (i = 0; i < ARRAY_SIZE(mtk_ethtool_stats); i++)
2204 *data_dst++ = *(data_src + mtk_ethtool_stats[i].offset);
2205 } while (u64_stats_fetch_retry_irq(&hwstats->syncp, start));
2206}
2207
Nelson Chang7aab7472016-09-17 23:50:56 +08002208static int mtk_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd,
2209 u32 *rule_locs)
2210{
2211 int ret = -EOPNOTSUPP;
2212
2213 switch (cmd->cmd) {
2214 case ETHTOOL_GRXRINGS:
2215 if (dev->features & NETIF_F_LRO) {
2216 cmd->data = MTK_MAX_RX_RING_NUM;
2217 ret = 0;
2218 }
2219 break;
2220 case ETHTOOL_GRXCLSRLCNT:
2221 if (dev->features & NETIF_F_LRO) {
2222 struct mtk_mac *mac = netdev_priv(dev);
2223
2224 cmd->rule_cnt = mac->hwlro_ip_cnt;
2225 ret = 0;
2226 }
2227 break;
2228 case ETHTOOL_GRXCLSRULE:
2229 if (dev->features & NETIF_F_LRO)
2230 ret = mtk_hwlro_get_fdir_entry(dev, cmd);
2231 break;
2232 case ETHTOOL_GRXCLSRLALL:
2233 if (dev->features & NETIF_F_LRO)
2234 ret = mtk_hwlro_get_fdir_all(dev, cmd,
2235 rule_locs);
2236 break;
2237 default:
2238 break;
2239 }
2240
2241 return ret;
2242}
2243
2244static int mtk_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd)
2245{
2246 int ret = -EOPNOTSUPP;
2247
2248 switch (cmd->cmd) {
2249 case ETHTOOL_SRXCLSRLINS:
2250 if (dev->features & NETIF_F_LRO)
2251 ret = mtk_hwlro_add_ipaddr(dev, cmd);
2252 break;
2253 case ETHTOOL_SRXCLSRLDEL:
2254 if (dev->features & NETIF_F_LRO)
2255 ret = mtk_hwlro_del_ipaddr(dev, cmd);
2256 break;
2257 default:
2258 break;
2259 }
2260
2261 return ret;
2262}
2263
Julia Lawall6a38cb12016-09-01 00:21:19 +02002264static const struct ethtool_ops mtk_ethtool_ops = {
Sean Wang3e60b742016-09-22 16:42:03 +08002265 .get_link_ksettings = mtk_get_link_ksettings,
2266 .set_link_ksettings = mtk_set_link_ksettings,
John Crispin656e7052016-03-08 11:29:55 +01002267 .get_drvinfo = mtk_get_drvinfo,
2268 .get_msglevel = mtk_get_msglevel,
2269 .set_msglevel = mtk_set_msglevel,
2270 .nway_reset = mtk_nway_reset,
2271 .get_link = mtk_get_link,
2272 .get_strings = mtk_get_strings,
2273 .get_sset_count = mtk_get_sset_count,
2274 .get_ethtool_stats = mtk_get_ethtool_stats,
Nelson Chang7aab7472016-09-17 23:50:56 +08002275 .get_rxnfc = mtk_get_rxnfc,
2276 .set_rxnfc = mtk_set_rxnfc,
John Crispin656e7052016-03-08 11:29:55 +01002277};
2278
2279static const struct net_device_ops mtk_netdev_ops = {
2280 .ndo_init = mtk_init,
2281 .ndo_uninit = mtk_uninit,
2282 .ndo_open = mtk_open,
2283 .ndo_stop = mtk_stop,
2284 .ndo_start_xmit = mtk_start_xmit,
2285 .ndo_set_mac_address = mtk_set_mac_address,
2286 .ndo_validate_addr = eth_validate_addr,
2287 .ndo_do_ioctl = mtk_do_ioctl,
John Crispin656e7052016-03-08 11:29:55 +01002288 .ndo_tx_timeout = mtk_tx_timeout,
2289 .ndo_get_stats64 = mtk_get_stats64,
Nelson Chang7aab7472016-09-17 23:50:56 +08002290 .ndo_fix_features = mtk_fix_features,
2291 .ndo_set_features = mtk_set_features,
John Crispin656e7052016-03-08 11:29:55 +01002292#ifdef CONFIG_NET_POLL_CONTROLLER
2293 .ndo_poll_controller = mtk_poll_controller,
2294#endif
2295};
2296
2297static int mtk_add_mac(struct mtk_eth *eth, struct device_node *np)
2298{
2299 struct mtk_mac *mac;
2300 const __be32 *_id = of_get_property(np, "reg", NULL);
2301 int id, err;
2302
2303 if (!_id) {
2304 dev_err(eth->dev, "missing mac id\n");
2305 return -EINVAL;
2306 }
2307
2308 id = be32_to_cpup(_id);
2309 if (id >= MTK_MAC_COUNT) {
2310 dev_err(eth->dev, "%d is not a valid mac id\n", id);
2311 return -EINVAL;
2312 }
2313
2314 if (eth->netdev[id]) {
2315 dev_err(eth->dev, "duplicate mac id found: %d\n", id);
2316 return -EINVAL;
2317 }
2318
2319 eth->netdev[id] = alloc_etherdev(sizeof(*mac));
2320 if (!eth->netdev[id]) {
2321 dev_err(eth->dev, "alloc_etherdev failed\n");
2322 return -ENOMEM;
2323 }
2324 mac = netdev_priv(eth->netdev[id]);
2325 eth->mac[id] = mac;
2326 mac->id = id;
2327 mac->hw = eth;
2328 mac->of_node = np;
John Crispin656e7052016-03-08 11:29:55 +01002329
Nelson Changee406812016-09-17 23:50:55 +08002330 memset(mac->hwlro_ip, 0, sizeof(mac->hwlro_ip));
2331 mac->hwlro_ip_cnt = 0;
2332
John Crispin656e7052016-03-08 11:29:55 +01002333 mac->hw_stats = devm_kzalloc(eth->dev,
2334 sizeof(*mac->hw_stats),
2335 GFP_KERNEL);
2336 if (!mac->hw_stats) {
2337 dev_err(eth->dev, "failed to allocate counter memory\n");
2338 err = -ENOMEM;
2339 goto free_netdev;
2340 }
2341 spin_lock_init(&mac->hw_stats->stats_lock);
sean.wang@mediatek.comd70056522016-08-13 19:16:18 +08002342 u64_stats_init(&mac->hw_stats->syncp);
John Crispin656e7052016-03-08 11:29:55 +01002343 mac->hw_stats->reg_offset = id * MTK_STAT_OFFSET;
2344
2345 SET_NETDEV_DEV(eth->netdev[id], eth->dev);
John Crispineaadf9f2016-06-10 13:28:05 +02002346 eth->netdev[id]->watchdog_timeo = 5 * HZ;
John Crispin656e7052016-03-08 11:29:55 +01002347 eth->netdev[id]->netdev_ops = &mtk_netdev_ops;
2348 eth->netdev[id]->base_addr = (unsigned long)eth->base;
Nelson Changee406812016-09-17 23:50:55 +08002349
2350 eth->netdev[id]->hw_features = MTK_HW_FEATURES;
2351 if (eth->hwlro)
2352 eth->netdev[id]->hw_features |= NETIF_F_LRO;
2353
John Crispin656e7052016-03-08 11:29:55 +01002354 eth->netdev[id]->vlan_features = MTK_HW_FEATURES &
2355 ~(NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX);
2356 eth->netdev[id]->features |= MTK_HW_FEATURES;
2357 eth->netdev[id]->ethtool_ops = &mtk_ethtool_ops;
2358
John Crispin80673022016-06-29 13:38:11 +02002359 eth->netdev[id]->irq = eth->irq[0];
Sean Wang3174b3b2017-04-07 16:45:08 +08002360 eth->netdev[id]->dev.of_node = np;
2361
John Crispin656e7052016-03-08 11:29:55 +01002362 return 0;
2363
2364free_netdev:
2365 free_netdev(eth->netdev[id]);
2366 return err;
2367}
2368
Nelson Changb95b6d92016-10-06 19:44:01 +08002369static int mtk_get_chip_id(struct mtk_eth *eth, u32 *chip_id)
2370{
2371 u32 val[2], id[4];
2372
2373 regmap_read(eth->ethsys, ETHSYS_CHIPID0_3, &val[0]);
2374 regmap_read(eth->ethsys, ETHSYS_CHIPID4_7, &val[1]);
2375
2376 id[3] = ((val[0] >> 16) & 0xff) - '0';
2377 id[2] = ((val[0] >> 24) & 0xff) - '0';
2378 id[1] = (val[1] & 0xff) - '0';
2379 id[0] = ((val[1] >> 8) & 0xff) - '0';
2380
2381 *chip_id = (id[3] * 1000) + (id[2] * 100) +
2382 (id[1] * 10) + id[0];
2383
2384 if (!(*chip_id)) {
2385 dev_err(eth->dev, "failed to get chip id\n");
2386 return -ENODEV;
2387 }
2388
2389 dev_info(eth->dev, "chip id = %d\n", *chip_id);
2390
2391 return 0;
2392}
2393
Nelson Chang983e1a62016-10-06 19:44:02 +08002394static bool mtk_is_hwlro_supported(struct mtk_eth *eth)
2395{
2396 switch (eth->chip_id) {
2397 case MT7623_ETH:
2398 return true;
2399 }
2400
2401 return false;
2402}
2403
John Crispin656e7052016-03-08 11:29:55 +01002404static int mtk_probe(struct platform_device *pdev)
2405{
2406 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2407 struct device_node *mac_np;
John Crispin656e7052016-03-08 11:29:55 +01002408 struct mtk_eth *eth;
2409 int err;
John Crispin80673022016-06-29 13:38:11 +02002410 int i;
John Crispin656e7052016-03-08 11:29:55 +01002411
John Crispin656e7052016-03-08 11:29:55 +01002412 eth = devm_kzalloc(&pdev->dev, sizeof(*eth), GFP_KERNEL);
2413 if (!eth)
2414 return -ENOMEM;
2415
Sean Wang549e5492016-09-01 10:47:28 +08002416 eth->dev = &pdev->dev;
John Crispin656e7052016-03-08 11:29:55 +01002417 eth->base = devm_ioremap_resource(&pdev->dev, res);
Vladimir Zapolskiy621e49f2016-03-23 01:06:04 +02002418 if (IS_ERR(eth->base))
2419 return PTR_ERR(eth->base);
John Crispin656e7052016-03-08 11:29:55 +01002420
2421 spin_lock_init(&eth->page_lock);
John Crispin5cce0322017-06-19 15:37:05 +02002422 spin_lock_init(&eth->tx_irq_lock);
2423 spin_lock_init(&eth->rx_irq_lock);
John Crispin656e7052016-03-08 11:29:55 +01002424
2425 eth->ethsys = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
2426 "mediatek,ethsys");
2427 if (IS_ERR(eth->ethsys)) {
2428 dev_err(&pdev->dev, "no ethsys regmap found\n");
2429 return PTR_ERR(eth->ethsys);
2430 }
2431
2432 eth->pctl = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
2433 "mediatek,pctl");
2434 if (IS_ERR(eth->pctl)) {
2435 dev_err(&pdev->dev, "no pctl regmap found\n");
2436 return PTR_ERR(eth->pctl);
2437 }
2438
John Crispin80673022016-06-29 13:38:11 +02002439 for (i = 0; i < 3; i++) {
2440 eth->irq[i] = platform_get_irq(pdev, i);
2441 if (eth->irq[i] < 0) {
2442 dev_err(&pdev->dev, "no IRQ%d resource found\n", i);
2443 return -ENXIO;
2444 }
John Crispin656e7052016-03-08 11:29:55 +01002445 }
Sean Wang549e5492016-09-01 10:47:28 +08002446 for (i = 0; i < ARRAY_SIZE(eth->clks); i++) {
2447 eth->clks[i] = devm_clk_get(eth->dev,
2448 mtk_clks_source_name[i]);
2449 if (IS_ERR(eth->clks[i])) {
2450 if (PTR_ERR(eth->clks[i]) == -EPROBE_DEFER)
2451 return -EPROBE_DEFER;
2452 return -ENODEV;
2453 }
2454 }
John Crispin656e7052016-03-08 11:29:55 +01002455
John Crispin656e7052016-03-08 11:29:55 +01002456 eth->msg_enable = netif_msg_init(mtk_msg_level, MTK_DEFAULT_MSG_ENABLE);
John Crispin7c78b4a2016-04-08 00:54:10 +02002457 INIT_WORK(&eth->pending_work, mtk_pending_work);
John Crispin656e7052016-03-08 11:29:55 +01002458
2459 err = mtk_hw_init(eth);
2460 if (err)
2461 return err;
2462
Nelson Changb95b6d92016-10-06 19:44:01 +08002463 err = mtk_get_chip_id(eth, &eth->chip_id);
2464 if (err)
2465 return err;
2466
Nelson Chang983e1a62016-10-06 19:44:02 +08002467 eth->hwlro = mtk_is_hwlro_supported(eth);
2468
John Crispin656e7052016-03-08 11:29:55 +01002469 for_each_child_of_node(pdev->dev.of_node, mac_np) {
2470 if (!of_device_is_compatible(mac_np,
2471 "mediatek,eth-mac"))
2472 continue;
2473
2474 if (!of_device_is_available(mac_np))
2475 continue;
2476
2477 err = mtk_add_mac(eth, mac_np);
2478 if (err)
Sean Wang8a8a9e82016-09-14 23:13:17 +08002479 goto err_deinit_hw;
John Crispin656e7052016-03-08 11:29:55 +01002480 }
2481
Sean Wang85574db2016-09-14 23:13:15 +08002482 err = devm_request_irq(eth->dev, eth->irq[1], mtk_handle_irq_tx, 0,
2483 dev_name(eth->dev), eth);
2484 if (err)
2485 goto err_free_dev;
2486
2487 err = devm_request_irq(eth->dev, eth->irq[2], mtk_handle_irq_rx, 0,
2488 dev_name(eth->dev), eth);
2489 if (err)
2490 goto err_free_dev;
2491
2492 err = mtk_mdio_init(eth);
2493 if (err)
2494 goto err_free_dev;
2495
2496 for (i = 0; i < MTK_MAX_DEVS; i++) {
2497 if (!eth->netdev[i])
2498 continue;
2499
2500 err = register_netdev(eth->netdev[i]);
2501 if (err) {
2502 dev_err(eth->dev, "error bringing up device\n");
Sean Wang8a8a9e82016-09-14 23:13:17 +08002503 goto err_deinit_mdio;
Sean Wang85574db2016-09-14 23:13:15 +08002504 } else
2505 netif_info(eth, probe, eth->netdev[i],
2506 "mediatek frame engine at 0x%08lx, irq %d\n",
2507 eth->netdev[i]->base_addr, eth->irq[0]);
2508 }
2509
John Crispin656e7052016-03-08 11:29:55 +01002510 /* we run 2 devices on the same DMA ring so we need a dummy device
2511 * for NAPI to work
2512 */
2513 init_dummy_netdev(&eth->dummy_dev);
John Crispin80673022016-06-29 13:38:11 +02002514 netif_napi_add(&eth->dummy_dev, &eth->tx_napi, mtk_napi_tx,
2515 MTK_NAPI_WEIGHT);
2516 netif_napi_add(&eth->dummy_dev, &eth->rx_napi, mtk_napi_rx,
John Crispin656e7052016-03-08 11:29:55 +01002517 MTK_NAPI_WEIGHT);
2518
2519 platform_set_drvdata(pdev, eth);
2520
2521 return 0;
2522
Sean Wang8a8a9e82016-09-14 23:13:17 +08002523err_deinit_mdio:
2524 mtk_mdio_cleanup(eth);
John Crispin656e7052016-03-08 11:29:55 +01002525err_free_dev:
Sean Wang8a8a9e82016-09-14 23:13:17 +08002526 mtk_free_dev(eth);
2527err_deinit_hw:
2528 mtk_hw_deinit(eth);
2529
John Crispin656e7052016-03-08 11:29:55 +01002530 return err;
2531}
2532
2533static int mtk_remove(struct platform_device *pdev)
2534{
2535 struct mtk_eth *eth = platform_get_drvdata(pdev);
Sean Wang79e9a412016-09-01 10:47:32 +08002536 int i;
John Crispin656e7052016-03-08 11:29:55 +01002537
Sean Wang79e9a412016-09-01 10:47:32 +08002538 /* stop all devices to make sure that dma is properly shut down */
2539 for (i = 0; i < MTK_MAC_COUNT; i++) {
2540 if (!eth->netdev[i])
2541 continue;
2542 mtk_stop(eth->netdev[i]);
2543 }
John Crispin656e7052016-03-08 11:29:55 +01002544
Sean Wangbf253fb2016-09-14 23:13:16 +08002545 mtk_hw_deinit(eth);
John Crispin656e7052016-03-08 11:29:55 +01002546
John Crispin80673022016-06-29 13:38:11 +02002547 netif_napi_del(&eth->tx_napi);
John Crispin656e7052016-03-08 11:29:55 +01002548 netif_napi_del(&eth->rx_napi);
2549 mtk_cleanup(eth);
Sean Wange82f7142016-09-20 23:53:24 +08002550 mtk_mdio_cleanup(eth);
John Crispin656e7052016-03-08 11:29:55 +01002551
2552 return 0;
2553}
2554
2555const struct of_device_id of_mtk_match[] = {
John Crispin8b901f62017-01-25 09:20:55 +01002556 { .compatible = "mediatek,mt2701-eth" },
John Crispin656e7052016-03-08 11:29:55 +01002557 {},
2558};
Sean Wang7077dc42016-09-14 21:29:34 +08002559MODULE_DEVICE_TABLE(of, of_mtk_match);
John Crispin656e7052016-03-08 11:29:55 +01002560
2561static struct platform_driver mtk_driver = {
2562 .probe = mtk_probe,
2563 .remove = mtk_remove,
2564 .driver = {
2565 .name = "mtk_soc_eth",
John Crispin656e7052016-03-08 11:29:55 +01002566 .of_match_table = of_mtk_match,
2567 },
2568};
2569
2570module_platform_driver(mtk_driver);
2571
2572MODULE_LICENSE("GPL");
2573MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
2574MODULE_DESCRIPTION("Ethernet driver for MediaTek SoC");