blob: 462d1e83e254213d86c474c9325b342b4aa2134e [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)) {
496 if (spin_trylock(&hw_stats->stats_lock)) {
497 mtk_stats_update_mac(mac);
498 spin_unlock(&hw_stats->stats_lock);
499 }
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
950 netdev = eth->netdev[mac];
951
Sean Wangdce6fa42016-09-14 23:13:21 +0800952 if (unlikely(test_bit(MTK_RESETTING, &eth->state)))
953 goto release_desc;
954
John Crispin656e7052016-03-08 11:29:55 +0100955 /* alloc new buffer */
956 new_data = napi_alloc_frag(ring->frag_size);
957 if (unlikely(!new_data)) {
958 netdev->stats.rx_dropped++;
959 goto release_desc;
960 }
sean.wang@mediatek.com55a4e772016-08-16 13:55:15 +0800961 dma_addr = dma_map_single(eth->dev,
John Crispin656e7052016-03-08 11:29:55 +0100962 new_data + NET_SKB_PAD,
963 ring->buf_size,
964 DMA_FROM_DEVICE);
sean.wang@mediatek.com55a4e772016-08-16 13:55:15 +0800965 if (unlikely(dma_mapping_error(eth->dev, dma_addr))) {
John Crispin656e7052016-03-08 11:29:55 +0100966 skb_free_frag(new_data);
John Crispin94321a92016-06-10 13:28:01 +0200967 netdev->stats.rx_dropped++;
John Crispin656e7052016-03-08 11:29:55 +0100968 goto release_desc;
969 }
970
971 /* receive data */
972 skb = build_skb(data, ring->frag_size);
973 if (unlikely(!skb)) {
Sean Wang1b430792016-09-01 10:47:29 +0800974 skb_free_frag(new_data);
John Crispin94321a92016-06-10 13:28:01 +0200975 netdev->stats.rx_dropped++;
John Crispin656e7052016-03-08 11:29:55 +0100976 goto release_desc;
977 }
978 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
979
sean.wang@mediatek.com55a4e772016-08-16 13:55:15 +0800980 dma_unmap_single(eth->dev, trxd.rxd1,
John Crispin656e7052016-03-08 11:29:55 +0100981 ring->buf_size, DMA_FROM_DEVICE);
982 pktlen = RX_DMA_GET_PLEN0(trxd.rxd2);
983 skb->dev = netdev;
984 skb_put(skb, pktlen);
985 if (trxd.rxd4 & RX_DMA_L4_VALID)
986 skb->ip_summed = CHECKSUM_UNNECESSARY;
987 else
988 skb_checksum_none_assert(skb);
989 skb->protocol = eth_type_trans(skb, netdev);
990
991 if (netdev->features & NETIF_F_HW_VLAN_CTAG_RX &&
992 RX_DMA_VID(trxd.rxd3))
993 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
994 RX_DMA_VID(trxd.rxd3));
995 napi_gro_receive(napi, skb);
996
997 ring->data[idx] = new_data;
998 rxd->rxd1 = (unsigned int)dma_addr;
999
1000release_desc:
1001 rxd->rxd2 = RX_DMA_PLEN0(ring->buf_size);
1002
1003 ring->calc_idx = idx;
Sean Wang635372a2016-09-03 17:59:26 +08001004
John Crispin656e7052016-03-08 11:29:55 +01001005 done++;
1006 }
1007
Nelson Changee406812016-09-17 23:50:55 +08001008rx_done:
Sean Wang41156ce2016-09-03 17:59:27 +08001009 if (done) {
1010 /* make sure that all changes to the dma ring are flushed before
1011 * we continue
1012 */
1013 wmb();
Nelson Changee406812016-09-17 23:50:55 +08001014 mtk_update_rx_cpu_idx(eth);
Sean Wang41156ce2016-09-03 17:59:27 +08001015 }
John Crispin656e7052016-03-08 11:29:55 +01001016
1017 return done;
1018}
1019
John Crispin80673022016-06-29 13:38:11 +02001020static int mtk_poll_tx(struct mtk_eth *eth, int budget)
John Crispin656e7052016-03-08 11:29:55 +01001021{
1022 struct mtk_tx_ring *ring = &eth->tx_ring;
1023 struct mtk_tx_dma *desc;
1024 struct sk_buff *skb;
1025 struct mtk_tx_buf *tx_buf;
John Crispin80673022016-06-29 13:38:11 +02001026 unsigned int done[MTK_MAX_DEVS];
John Crispin656e7052016-03-08 11:29:55 +01001027 unsigned int bytes[MTK_MAX_DEVS];
1028 u32 cpu, dma;
1029 static int condition;
John Crispin80673022016-06-29 13:38:11 +02001030 int total = 0, i;
John Crispin656e7052016-03-08 11:29:55 +01001031
1032 memset(done, 0, sizeof(done));
1033 memset(bytes, 0, sizeof(bytes));
1034
1035 cpu = mtk_r32(eth, MTK_QTX_CRX_PTR);
1036 dma = mtk_r32(eth, MTK_QTX_DRX_PTR);
1037
1038 desc = mtk_qdma_phys_to_virt(ring, cpu);
1039
1040 while ((cpu != dma) && budget) {
1041 u32 next_cpu = desc->txd2;
Sean Wang134d2152017-04-14 11:19:12 +08001042 int mac = 0;
John Crispin656e7052016-03-08 11:29:55 +01001043
1044 desc = mtk_qdma_phys_to_virt(ring, desc->txd2);
1045 if ((desc->txd3 & TX_DMA_OWNER_CPU) == 0)
1046 break;
1047
John Crispin656e7052016-03-08 11:29:55 +01001048 tx_buf = mtk_desc_to_tx_buf(ring, desc);
Sean Wang134d2152017-04-14 11:19:12 +08001049 if (tx_buf->flags & MTK_TX_FLAGS_FPORT1)
1050 mac = 1;
1051
John Crispin656e7052016-03-08 11:29:55 +01001052 skb = tx_buf->skb;
1053 if (!skb) {
1054 condition = 1;
1055 break;
1056 }
1057
1058 if (skb != (struct sk_buff *)MTK_DMA_DUMMY_DESC) {
1059 bytes[mac] += skb->len;
1060 done[mac]++;
1061 budget--;
1062 }
sean.wang@mediatek.com55a4e772016-08-16 13:55:15 +08001063 mtk_tx_unmap(eth, tx_buf);
John Crispin656e7052016-03-08 11:29:55 +01001064
John Crispin656e7052016-03-08 11:29:55 +01001065 ring->last_free = desc;
1066 atomic_inc(&ring->free_count);
1067
1068 cpu = next_cpu;
1069 }
1070
1071 mtk_w32(eth, cpu, MTK_QTX_CRX_PTR);
1072
1073 for (i = 0; i < MTK_MAC_COUNT; i++) {
1074 if (!eth->netdev[i] || !done[i])
1075 continue;
1076 netdev_completed_queue(eth->netdev[i], done[i], bytes[i]);
1077 total += done[i];
1078 }
1079
John Crispinad3cba92016-06-10 13:28:07 +02001080 if (mtk_queue_stopped(eth) &&
1081 (atomic_read(&ring->free_count) > ring->thresh))
John Crispin13c822f2016-04-08 00:54:07 +02001082 mtk_wake_queue(eth);
John Crispin656e7052016-03-08 11:29:55 +01001083
1084 return total;
1085}
1086
John Crispin80673022016-06-29 13:38:11 +02001087static void mtk_handle_status_irq(struct mtk_eth *eth)
John Crispin656e7052016-03-08 11:29:55 +01001088{
John Crispin80673022016-06-29 13:38:11 +02001089 u32 status2 = mtk_r32(eth, MTK_INT_STATUS2);
John Crispin656e7052016-03-08 11:29:55 +01001090
John Crispineece71e2016-06-29 13:38:09 +02001091 if (unlikely(status2 & (MTK_GDM1_AF | MTK_GDM2_AF))) {
John Crispin656e7052016-03-08 11:29:55 +01001092 mtk_stats_update(eth);
John Crispineece71e2016-06-29 13:38:09 +02001093 mtk_w32(eth, (MTK_GDM1_AF | MTK_GDM2_AF),
1094 MTK_INT_STATUS2);
John Crispin656e7052016-03-08 11:29:55 +01001095 }
John Crispin80673022016-06-29 13:38:11 +02001096}
1097
1098static int mtk_napi_tx(struct napi_struct *napi, int budget)
1099{
1100 struct mtk_eth *eth = container_of(napi, struct mtk_eth, tx_napi);
1101 u32 status, mask;
1102 int tx_done = 0;
1103
1104 mtk_handle_status_irq(eth);
1105 mtk_w32(eth, MTK_TX_DONE_INT, MTK_QMTK_INT_STATUS);
1106 tx_done = mtk_poll_tx(eth, budget);
John Crispin656e7052016-03-08 11:29:55 +01001107
1108 if (unlikely(netif_msg_intr(eth))) {
John Crispin80673022016-06-29 13:38:11 +02001109 status = mtk_r32(eth, MTK_QMTK_INT_STATUS);
John Crispin656e7052016-03-08 11:29:55 +01001110 mask = mtk_r32(eth, MTK_QDMA_INT_MASK);
John Crispin80673022016-06-29 13:38:11 +02001111 dev_info(eth->dev,
1112 "done tx %d, intr 0x%08x/0x%x\n",
1113 tx_done, status, mask);
John Crispin656e7052016-03-08 11:29:55 +01001114 }
1115
John Crispin80673022016-06-29 13:38:11 +02001116 if (tx_done == budget)
John Crispin656e7052016-03-08 11:29:55 +01001117 return budget;
1118
1119 status = mtk_r32(eth, MTK_QMTK_INT_STATUS);
John Crispin80673022016-06-29 13:38:11 +02001120 if (status & MTK_TX_DONE_INT)
John Crispin656e7052016-03-08 11:29:55 +01001121 return budget;
1122
1123 napi_complete(napi);
John Crispin5cce0322017-06-19 15:37:05 +02001124 mtk_tx_irq_enable(eth, MTK_TX_DONE_INT);
John Crispin80673022016-06-29 13:38:11 +02001125
1126 return tx_done;
1127}
1128
1129static int mtk_napi_rx(struct napi_struct *napi, int budget)
1130{
1131 struct mtk_eth *eth = container_of(napi, struct mtk_eth, rx_napi);
1132 u32 status, mask;
1133 int rx_done = 0;
Sean Wang41156ce2016-09-03 17:59:27 +08001134 int remain_budget = budget;
John Crispin80673022016-06-29 13:38:11 +02001135
1136 mtk_handle_status_irq(eth);
Sean Wang41156ce2016-09-03 17:59:27 +08001137
1138poll_again:
Nelson Changbacfd112016-08-26 01:09:42 +08001139 mtk_w32(eth, MTK_RX_DONE_INT, MTK_PDMA_INT_STATUS);
Sean Wang41156ce2016-09-03 17:59:27 +08001140 rx_done = mtk_poll_rx(napi, remain_budget, eth);
John Crispin80673022016-06-29 13:38:11 +02001141
1142 if (unlikely(netif_msg_intr(eth))) {
Nelson Changbacfd112016-08-26 01:09:42 +08001143 status = mtk_r32(eth, MTK_PDMA_INT_STATUS);
1144 mask = mtk_r32(eth, MTK_PDMA_INT_MASK);
John Crispin80673022016-06-29 13:38:11 +02001145 dev_info(eth->dev,
1146 "done rx %d, intr 0x%08x/0x%x\n",
1147 rx_done, status, mask);
1148 }
Sean Wang41156ce2016-09-03 17:59:27 +08001149 if (rx_done == remain_budget)
John Crispin80673022016-06-29 13:38:11 +02001150 return budget;
1151
Nelson Changbacfd112016-08-26 01:09:42 +08001152 status = mtk_r32(eth, MTK_PDMA_INT_STATUS);
Sean Wang41156ce2016-09-03 17:59:27 +08001153 if (status & MTK_RX_DONE_INT) {
1154 remain_budget -= rx_done;
1155 goto poll_again;
1156 }
John Crispin80673022016-06-29 13:38:11 +02001157 napi_complete(napi);
John Crispin5cce0322017-06-19 15:37:05 +02001158 mtk_rx_irq_enable(eth, MTK_RX_DONE_INT);
John Crispin656e7052016-03-08 11:29:55 +01001159
Sean Wang41156ce2016-09-03 17:59:27 +08001160 return rx_done + budget - remain_budget;
John Crispin656e7052016-03-08 11:29:55 +01001161}
1162
1163static int mtk_tx_alloc(struct mtk_eth *eth)
1164{
1165 struct mtk_tx_ring *ring = &eth->tx_ring;
1166 int i, sz = sizeof(*ring->dma);
1167
1168 ring->buf = kcalloc(MTK_DMA_SIZE, sizeof(*ring->buf),
1169 GFP_KERNEL);
1170 if (!ring->buf)
1171 goto no_tx_mem;
1172
1173 ring->dma = dma_alloc_coherent(eth->dev,
1174 MTK_DMA_SIZE * sz,
1175 &ring->phys,
1176 GFP_ATOMIC | __GFP_ZERO);
1177 if (!ring->dma)
1178 goto no_tx_mem;
1179
1180 memset(ring->dma, 0, MTK_DMA_SIZE * sz);
1181 for (i = 0; i < MTK_DMA_SIZE; i++) {
1182 int next = (i + 1) % MTK_DMA_SIZE;
1183 u32 next_ptr = ring->phys + next * sz;
1184
1185 ring->dma[i].txd2 = next_ptr;
1186 ring->dma[i].txd3 = TX_DMA_LS0 | TX_DMA_OWNER_CPU;
1187 }
1188
1189 atomic_set(&ring->free_count, MTK_DMA_SIZE - 2);
1190 ring->next_free = &ring->dma[0];
John Crispin12c97c12016-06-10 13:28:06 +02001191 ring->last_free = &ring->dma[MTK_DMA_SIZE - 1];
John Crispin04698cc2016-06-10 13:28:04 +02001192 ring->thresh = MAX_SKB_FRAGS;
John Crispin656e7052016-03-08 11:29:55 +01001193
1194 /* make sure that all changes to the dma ring are flushed before we
1195 * continue
1196 */
1197 wmb();
1198
1199 mtk_w32(eth, ring->phys, MTK_QTX_CTX_PTR);
1200 mtk_w32(eth, ring->phys, MTK_QTX_DTX_PTR);
1201 mtk_w32(eth,
1202 ring->phys + ((MTK_DMA_SIZE - 1) * sz),
1203 MTK_QTX_CRX_PTR);
1204 mtk_w32(eth,
1205 ring->phys + ((MTK_DMA_SIZE - 1) * sz),
1206 MTK_QTX_DRX_PTR);
Nelson Changbacfd112016-08-26 01:09:42 +08001207 mtk_w32(eth, (QDMA_RES_THRES << 8) | QDMA_RES_THRES, MTK_QTX_CFG(0));
John Crispin656e7052016-03-08 11:29:55 +01001208
1209 return 0;
1210
1211no_tx_mem:
1212 return -ENOMEM;
1213}
1214
1215static void mtk_tx_clean(struct mtk_eth *eth)
1216{
1217 struct mtk_tx_ring *ring = &eth->tx_ring;
1218 int i;
1219
1220 if (ring->buf) {
1221 for (i = 0; i < MTK_DMA_SIZE; i++)
sean.wang@mediatek.com55a4e772016-08-16 13:55:15 +08001222 mtk_tx_unmap(eth, &ring->buf[i]);
John Crispin656e7052016-03-08 11:29:55 +01001223 kfree(ring->buf);
1224 ring->buf = NULL;
1225 }
1226
1227 if (ring->dma) {
1228 dma_free_coherent(eth->dev,
1229 MTK_DMA_SIZE * sizeof(*ring->dma),
1230 ring->dma,
1231 ring->phys);
1232 ring->dma = NULL;
1233 }
1234}
1235
Nelson Changee406812016-09-17 23:50:55 +08001236static int mtk_rx_alloc(struct mtk_eth *eth, int ring_no, int rx_flag)
John Crispin656e7052016-03-08 11:29:55 +01001237{
Nelson Changee406812016-09-17 23:50:55 +08001238 struct mtk_rx_ring *ring = &eth->rx_ring[ring_no];
1239 int rx_data_len, rx_dma_size;
John Crispin656e7052016-03-08 11:29:55 +01001240 int i;
1241
Nelson Changee406812016-09-17 23:50:55 +08001242 if (rx_flag == MTK_RX_FLAGS_HWLRO) {
1243 rx_data_len = MTK_MAX_LRO_RX_LENGTH;
1244 rx_dma_size = MTK_HW_LRO_DMA_SIZE;
1245 } else {
1246 rx_data_len = ETH_DATA_LEN;
1247 rx_dma_size = MTK_DMA_SIZE;
1248 }
1249
1250 ring->frag_size = mtk_max_frag_size(rx_data_len);
John Crispin656e7052016-03-08 11:29:55 +01001251 ring->buf_size = mtk_max_buf_size(ring->frag_size);
Nelson Changee406812016-09-17 23:50:55 +08001252 ring->data = kcalloc(rx_dma_size, sizeof(*ring->data),
John Crispin656e7052016-03-08 11:29:55 +01001253 GFP_KERNEL);
1254 if (!ring->data)
1255 return -ENOMEM;
1256
Nelson Changee406812016-09-17 23:50:55 +08001257 for (i = 0; i < rx_dma_size; i++) {
John Crispin656e7052016-03-08 11:29:55 +01001258 ring->data[i] = netdev_alloc_frag(ring->frag_size);
1259 if (!ring->data[i])
1260 return -ENOMEM;
1261 }
1262
1263 ring->dma = dma_alloc_coherent(eth->dev,
Nelson Changee406812016-09-17 23:50:55 +08001264 rx_dma_size * sizeof(*ring->dma),
John Crispin656e7052016-03-08 11:29:55 +01001265 &ring->phys,
1266 GFP_ATOMIC | __GFP_ZERO);
1267 if (!ring->dma)
1268 return -ENOMEM;
1269
Nelson Changee406812016-09-17 23:50:55 +08001270 for (i = 0; i < rx_dma_size; i++) {
John Crispin656e7052016-03-08 11:29:55 +01001271 dma_addr_t dma_addr = dma_map_single(eth->dev,
1272 ring->data[i] + NET_SKB_PAD,
1273 ring->buf_size,
1274 DMA_FROM_DEVICE);
1275 if (unlikely(dma_mapping_error(eth->dev, dma_addr)))
1276 return -ENOMEM;
1277 ring->dma[i].rxd1 = (unsigned int)dma_addr;
1278
1279 ring->dma[i].rxd2 = RX_DMA_PLEN0(ring->buf_size);
1280 }
Nelson Changee406812016-09-17 23:50:55 +08001281 ring->dma_size = rx_dma_size;
1282 ring->calc_idx_update = false;
1283 ring->calc_idx = rx_dma_size - 1;
1284 ring->crx_idx_reg = MTK_PRX_CRX_IDX_CFG(ring_no);
John Crispin656e7052016-03-08 11:29:55 +01001285 /* make sure that all changes to the dma ring are flushed before we
1286 * continue
1287 */
1288 wmb();
1289
Nelson Changee406812016-09-17 23:50:55 +08001290 mtk_w32(eth, ring->phys, MTK_PRX_BASE_PTR_CFG(ring_no));
1291 mtk_w32(eth, rx_dma_size, MTK_PRX_MAX_CNT_CFG(ring_no));
1292 mtk_w32(eth, ring->calc_idx, ring->crx_idx_reg);
1293 mtk_w32(eth, MTK_PST_DRX_IDX_CFG(ring_no), MTK_PDMA_RST_IDX);
John Crispin656e7052016-03-08 11:29:55 +01001294
1295 return 0;
1296}
1297
Nelson Changee406812016-09-17 23:50:55 +08001298static void mtk_rx_clean(struct mtk_eth *eth, int ring_no)
John Crispin656e7052016-03-08 11:29:55 +01001299{
Nelson Changee406812016-09-17 23:50:55 +08001300 struct mtk_rx_ring *ring = &eth->rx_ring[ring_no];
John Crispin656e7052016-03-08 11:29:55 +01001301 int i;
1302
1303 if (ring->data && ring->dma) {
Nelson Changee406812016-09-17 23:50:55 +08001304 for (i = 0; i < ring->dma_size; i++) {
John Crispin656e7052016-03-08 11:29:55 +01001305 if (!ring->data[i])
1306 continue;
1307 if (!ring->dma[i].rxd1)
1308 continue;
1309 dma_unmap_single(eth->dev,
1310 ring->dma[i].rxd1,
1311 ring->buf_size,
1312 DMA_FROM_DEVICE);
1313 skb_free_frag(ring->data[i]);
1314 }
1315 kfree(ring->data);
1316 ring->data = NULL;
1317 }
1318
1319 if (ring->dma) {
1320 dma_free_coherent(eth->dev,
Nelson Changee406812016-09-17 23:50:55 +08001321 ring->dma_size * sizeof(*ring->dma),
John Crispin656e7052016-03-08 11:29:55 +01001322 ring->dma,
1323 ring->phys);
1324 ring->dma = NULL;
1325 }
1326}
1327
Nelson Changee406812016-09-17 23:50:55 +08001328static int mtk_hwlro_rx_init(struct mtk_eth *eth)
1329{
1330 int i;
1331 u32 ring_ctrl_dw1 = 0, ring_ctrl_dw2 = 0, ring_ctrl_dw3 = 0;
1332 u32 lro_ctrl_dw0 = 0, lro_ctrl_dw3 = 0;
1333
1334 /* set LRO rings to auto-learn modes */
1335 ring_ctrl_dw2 |= MTK_RING_AUTO_LERAN_MODE;
1336
1337 /* validate LRO ring */
1338 ring_ctrl_dw2 |= MTK_RING_VLD;
1339
1340 /* set AGE timer (unit: 20us) */
1341 ring_ctrl_dw2 |= MTK_RING_AGE_TIME_H;
1342 ring_ctrl_dw1 |= MTK_RING_AGE_TIME_L;
1343
1344 /* set max AGG timer (unit: 20us) */
1345 ring_ctrl_dw2 |= MTK_RING_MAX_AGG_TIME;
1346
1347 /* set max LRO AGG count */
1348 ring_ctrl_dw2 |= MTK_RING_MAX_AGG_CNT_L;
1349 ring_ctrl_dw3 |= MTK_RING_MAX_AGG_CNT_H;
1350
1351 for (i = 1; i < MTK_MAX_RX_RING_NUM; i++) {
1352 mtk_w32(eth, ring_ctrl_dw1, MTK_LRO_CTRL_DW1_CFG(i));
1353 mtk_w32(eth, ring_ctrl_dw2, MTK_LRO_CTRL_DW2_CFG(i));
1354 mtk_w32(eth, ring_ctrl_dw3, MTK_LRO_CTRL_DW3_CFG(i));
1355 }
1356
1357 /* IPv4 checksum update enable */
1358 lro_ctrl_dw0 |= MTK_L3_CKS_UPD_EN;
1359
1360 /* switch priority comparison to packet count mode */
1361 lro_ctrl_dw0 |= MTK_LRO_ALT_PKT_CNT_MODE;
1362
1363 /* bandwidth threshold setting */
1364 mtk_w32(eth, MTK_HW_LRO_BW_THRE, MTK_PDMA_LRO_CTRL_DW2);
1365
1366 /* auto-learn score delta setting */
1367 mtk_w32(eth, MTK_HW_LRO_REPLACE_DELTA, MTK_PDMA_LRO_ALT_SCORE_DELTA);
1368
1369 /* set refresh timer for altering flows to 1 sec. (unit: 20us) */
1370 mtk_w32(eth, (MTK_HW_LRO_TIMER_UNIT << 16) | MTK_HW_LRO_REFRESH_TIME,
1371 MTK_PDMA_LRO_ALT_REFRESH_TIMER);
1372
1373 /* set HW LRO mode & the max aggregation count for rx packets */
1374 lro_ctrl_dw3 |= MTK_ADMA_MODE | (MTK_HW_LRO_MAX_AGG_CNT & 0xff);
1375
1376 /* the minimal remaining room of SDL0 in RXD for lro aggregation */
1377 lro_ctrl_dw3 |= MTK_LRO_MIN_RXD_SDL;
1378
1379 /* enable HW LRO */
1380 lro_ctrl_dw0 |= MTK_LRO_EN;
1381
1382 mtk_w32(eth, lro_ctrl_dw3, MTK_PDMA_LRO_CTRL_DW3);
1383 mtk_w32(eth, lro_ctrl_dw0, MTK_PDMA_LRO_CTRL_DW0);
1384
1385 return 0;
1386}
1387
1388static void mtk_hwlro_rx_uninit(struct mtk_eth *eth)
1389{
1390 int i;
1391 u32 val;
1392
1393 /* relinquish lro rings, flush aggregated packets */
1394 mtk_w32(eth, MTK_LRO_RING_RELINQUISH_REQ, MTK_PDMA_LRO_CTRL_DW0);
1395
1396 /* wait for relinquishments done */
1397 for (i = 0; i < 10; i++) {
1398 val = mtk_r32(eth, MTK_PDMA_LRO_CTRL_DW0);
1399 if (val & MTK_LRO_RING_RELINQUISH_DONE) {
1400 msleep(20);
1401 continue;
1402 }
Nelson Changca3ba102016-09-26 14:33:50 +08001403 break;
Nelson Changee406812016-09-17 23:50:55 +08001404 }
1405
1406 /* invalidate lro rings */
1407 for (i = 1; i < MTK_MAX_RX_RING_NUM; i++)
1408 mtk_w32(eth, 0, MTK_LRO_CTRL_DW2_CFG(i));
1409
1410 /* disable HW LRO */
1411 mtk_w32(eth, 0, MTK_PDMA_LRO_CTRL_DW0);
1412}
1413
Nelson Chang7aab7472016-09-17 23:50:56 +08001414static void mtk_hwlro_val_ipaddr(struct mtk_eth *eth, int idx, __be32 ip)
1415{
1416 u32 reg_val;
1417
1418 reg_val = mtk_r32(eth, MTK_LRO_CTRL_DW2_CFG(idx));
1419
1420 /* invalidate the IP setting */
1421 mtk_w32(eth, (reg_val & ~MTK_RING_MYIP_VLD), MTK_LRO_CTRL_DW2_CFG(idx));
1422
1423 mtk_w32(eth, ip, MTK_LRO_DIP_DW0_CFG(idx));
1424
1425 /* validate the IP setting */
1426 mtk_w32(eth, (reg_val | MTK_RING_MYIP_VLD), MTK_LRO_CTRL_DW2_CFG(idx));
1427}
1428
1429static void mtk_hwlro_inval_ipaddr(struct mtk_eth *eth, int idx)
1430{
1431 u32 reg_val;
1432
1433 reg_val = mtk_r32(eth, MTK_LRO_CTRL_DW2_CFG(idx));
1434
1435 /* invalidate the IP setting */
1436 mtk_w32(eth, (reg_val & ~MTK_RING_MYIP_VLD), MTK_LRO_CTRL_DW2_CFG(idx));
1437
1438 mtk_w32(eth, 0, MTK_LRO_DIP_DW0_CFG(idx));
1439}
1440
1441static int mtk_hwlro_get_ip_cnt(struct mtk_mac *mac)
1442{
1443 int cnt = 0;
1444 int i;
1445
1446 for (i = 0; i < MTK_MAX_LRO_IP_CNT; i++) {
1447 if (mac->hwlro_ip[i])
1448 cnt++;
1449 }
1450
1451 return cnt;
1452}
1453
1454static int mtk_hwlro_add_ipaddr(struct net_device *dev,
1455 struct ethtool_rxnfc *cmd)
1456{
1457 struct ethtool_rx_flow_spec *fsp =
1458 (struct ethtool_rx_flow_spec *)&cmd->fs;
1459 struct mtk_mac *mac = netdev_priv(dev);
1460 struct mtk_eth *eth = mac->hw;
1461 int hwlro_idx;
1462
1463 if ((fsp->flow_type != TCP_V4_FLOW) ||
1464 (!fsp->h_u.tcp_ip4_spec.ip4dst) ||
1465 (fsp->location > 1))
1466 return -EINVAL;
1467
1468 mac->hwlro_ip[fsp->location] = htonl(fsp->h_u.tcp_ip4_spec.ip4dst);
1469 hwlro_idx = (mac->id * MTK_MAX_LRO_IP_CNT) + fsp->location;
1470
1471 mac->hwlro_ip_cnt = mtk_hwlro_get_ip_cnt(mac);
1472
1473 mtk_hwlro_val_ipaddr(eth, hwlro_idx, mac->hwlro_ip[fsp->location]);
1474
1475 return 0;
1476}
1477
1478static int mtk_hwlro_del_ipaddr(struct net_device *dev,
1479 struct ethtool_rxnfc *cmd)
1480{
1481 struct ethtool_rx_flow_spec *fsp =
1482 (struct ethtool_rx_flow_spec *)&cmd->fs;
1483 struct mtk_mac *mac = netdev_priv(dev);
1484 struct mtk_eth *eth = mac->hw;
1485 int hwlro_idx;
1486
1487 if (fsp->location > 1)
1488 return -EINVAL;
1489
1490 mac->hwlro_ip[fsp->location] = 0;
1491 hwlro_idx = (mac->id * MTK_MAX_LRO_IP_CNT) + fsp->location;
1492
1493 mac->hwlro_ip_cnt = mtk_hwlro_get_ip_cnt(mac);
1494
1495 mtk_hwlro_inval_ipaddr(eth, hwlro_idx);
1496
1497 return 0;
1498}
1499
1500static void mtk_hwlro_netdev_disable(struct net_device *dev)
1501{
1502 struct mtk_mac *mac = netdev_priv(dev);
1503 struct mtk_eth *eth = mac->hw;
1504 int i, hwlro_idx;
1505
1506 for (i = 0; i < MTK_MAX_LRO_IP_CNT; i++) {
1507 mac->hwlro_ip[i] = 0;
1508 hwlro_idx = (mac->id * MTK_MAX_LRO_IP_CNT) + i;
1509
1510 mtk_hwlro_inval_ipaddr(eth, hwlro_idx);
1511 }
1512
1513 mac->hwlro_ip_cnt = 0;
1514}
1515
1516static int mtk_hwlro_get_fdir_entry(struct net_device *dev,
1517 struct ethtool_rxnfc *cmd)
1518{
1519 struct mtk_mac *mac = netdev_priv(dev);
1520 struct ethtool_rx_flow_spec *fsp =
1521 (struct ethtool_rx_flow_spec *)&cmd->fs;
1522
1523 /* only tcp dst ipv4 is meaningful, others are meaningless */
1524 fsp->flow_type = TCP_V4_FLOW;
1525 fsp->h_u.tcp_ip4_spec.ip4dst = ntohl(mac->hwlro_ip[fsp->location]);
1526 fsp->m_u.tcp_ip4_spec.ip4dst = 0;
1527
1528 fsp->h_u.tcp_ip4_spec.ip4src = 0;
1529 fsp->m_u.tcp_ip4_spec.ip4src = 0xffffffff;
1530 fsp->h_u.tcp_ip4_spec.psrc = 0;
1531 fsp->m_u.tcp_ip4_spec.psrc = 0xffff;
1532 fsp->h_u.tcp_ip4_spec.pdst = 0;
1533 fsp->m_u.tcp_ip4_spec.pdst = 0xffff;
1534 fsp->h_u.tcp_ip4_spec.tos = 0;
1535 fsp->m_u.tcp_ip4_spec.tos = 0xff;
1536
1537 return 0;
1538}
1539
1540static int mtk_hwlro_get_fdir_all(struct net_device *dev,
1541 struct ethtool_rxnfc *cmd,
1542 u32 *rule_locs)
1543{
1544 struct mtk_mac *mac = netdev_priv(dev);
1545 int cnt = 0;
1546 int i;
1547
1548 for (i = 0; i < MTK_MAX_LRO_IP_CNT; i++) {
1549 if (mac->hwlro_ip[i]) {
1550 rule_locs[cnt] = i;
1551 cnt++;
1552 }
1553 }
1554
1555 cmd->rule_cnt = cnt;
1556
1557 return 0;
1558}
1559
1560static netdev_features_t mtk_fix_features(struct net_device *dev,
1561 netdev_features_t features)
1562{
1563 if (!(features & NETIF_F_LRO)) {
1564 struct mtk_mac *mac = netdev_priv(dev);
1565 int ip_cnt = mtk_hwlro_get_ip_cnt(mac);
1566
1567 if (ip_cnt) {
1568 netdev_info(dev, "RX flow is programmed, LRO should keep on\n");
1569
1570 features |= NETIF_F_LRO;
1571 }
1572 }
1573
1574 return features;
1575}
1576
1577static int mtk_set_features(struct net_device *dev, netdev_features_t features)
1578{
1579 int err = 0;
1580
1581 if (!((dev->features ^ features) & NETIF_F_LRO))
1582 return 0;
1583
1584 if (!(features & NETIF_F_LRO))
1585 mtk_hwlro_netdev_disable(dev);
1586
1587 return err;
1588}
1589
John Crispin656e7052016-03-08 11:29:55 +01001590/* wait for DMA to finish whatever it is doing before we start using it again */
1591static int mtk_dma_busy_wait(struct mtk_eth *eth)
1592{
1593 unsigned long t_start = jiffies;
1594
1595 while (1) {
1596 if (!(mtk_r32(eth, MTK_QDMA_GLO_CFG) &
1597 (MTK_RX_DMA_BUSY | MTK_TX_DMA_BUSY)))
1598 return 0;
1599 if (time_after(jiffies, t_start + MTK_DMA_BUSY_TIMEOUT))
1600 break;
1601 }
1602
1603 dev_err(eth->dev, "DMA init timeout\n");
1604 return -1;
1605}
1606
1607static int mtk_dma_init(struct mtk_eth *eth)
1608{
1609 int err;
Nelson Changee406812016-09-17 23:50:55 +08001610 u32 i;
John Crispin656e7052016-03-08 11:29:55 +01001611
1612 if (mtk_dma_busy_wait(eth))
1613 return -EBUSY;
1614
1615 /* QDMA needs scratch memory for internal reordering of the
1616 * descriptors
1617 */
1618 err = mtk_init_fq_dma(eth);
1619 if (err)
1620 return err;
1621
1622 err = mtk_tx_alloc(eth);
1623 if (err)
1624 return err;
1625
Nelson Changee406812016-09-17 23:50:55 +08001626 err = mtk_rx_alloc(eth, 0, MTK_RX_FLAGS_NORMAL);
John Crispin656e7052016-03-08 11:29:55 +01001627 if (err)
1628 return err;
1629
Nelson Changee406812016-09-17 23:50:55 +08001630 if (eth->hwlro) {
1631 for (i = 1; i < MTK_MAX_RX_RING_NUM; i++) {
1632 err = mtk_rx_alloc(eth, i, MTK_RX_FLAGS_HWLRO);
1633 if (err)
1634 return err;
1635 }
1636 err = mtk_hwlro_rx_init(eth);
1637 if (err)
1638 return err;
1639 }
1640
John Crispin656e7052016-03-08 11:29:55 +01001641 /* Enable random early drop and set drop threshold automatically */
1642 mtk_w32(eth, FC_THRES_DROP_MODE | FC_THRES_DROP_EN | FC_THRES_MIN,
1643 MTK_QDMA_FC_THRES);
1644 mtk_w32(eth, 0x0, MTK_QDMA_HRED2);
1645
1646 return 0;
1647}
1648
1649static void mtk_dma_free(struct mtk_eth *eth)
1650{
1651 int i;
1652
1653 for (i = 0; i < MTK_MAC_COUNT; i++)
1654 if (eth->netdev[i])
1655 netdev_reset_queue(eth->netdev[i]);
John Crispin605e4fe2016-06-10 13:27:59 +02001656 if (eth->scratch_ring) {
1657 dma_free_coherent(eth->dev,
1658 MTK_DMA_SIZE * sizeof(struct mtk_tx_dma),
1659 eth->scratch_ring,
1660 eth->phy_scratch_ring);
1661 eth->scratch_ring = NULL;
1662 eth->phy_scratch_ring = 0;
1663 }
John Crispin656e7052016-03-08 11:29:55 +01001664 mtk_tx_clean(eth);
Nelson Changee406812016-09-17 23:50:55 +08001665 mtk_rx_clean(eth, 0);
1666
1667 if (eth->hwlro) {
1668 mtk_hwlro_rx_uninit(eth);
1669 for (i = 1; i < MTK_MAX_RX_RING_NUM; i++)
1670 mtk_rx_clean(eth, i);
1671 }
1672
John Crispin656e7052016-03-08 11:29:55 +01001673 kfree(eth->scratch_head);
1674}
1675
1676static void mtk_tx_timeout(struct net_device *dev)
1677{
1678 struct mtk_mac *mac = netdev_priv(dev);
1679 struct mtk_eth *eth = mac->hw;
1680
1681 eth->netdev[mac->id]->stats.tx_errors++;
1682 netif_err(eth, tx_err, dev,
1683 "transmit timed out\n");
John Crispin7c78b4a2016-04-08 00:54:10 +02001684 schedule_work(&eth->pending_work);
John Crispin656e7052016-03-08 11:29:55 +01001685}
1686
John Crispin80673022016-06-29 13:38:11 +02001687static irqreturn_t mtk_handle_irq_rx(int irq, void *_eth)
John Crispin656e7052016-03-08 11:29:55 +01001688{
1689 struct mtk_eth *eth = _eth;
John Crispin656e7052016-03-08 11:29:55 +01001690
John Crispin80673022016-06-29 13:38:11 +02001691 if (likely(napi_schedule_prep(&eth->rx_napi))) {
1692 __napi_schedule(&eth->rx_napi);
John Crispin5cce0322017-06-19 15:37:05 +02001693 mtk_rx_irq_disable(eth, MTK_RX_DONE_INT);
John Crispin656e7052016-03-08 11:29:55 +01001694 }
John Crispin80673022016-06-29 13:38:11 +02001695
1696 return IRQ_HANDLED;
1697}
1698
1699static irqreturn_t mtk_handle_irq_tx(int irq, void *_eth)
1700{
1701 struct mtk_eth *eth = _eth;
1702
1703 if (likely(napi_schedule_prep(&eth->tx_napi))) {
1704 __napi_schedule(&eth->tx_napi);
John Crispin5cce0322017-06-19 15:37:05 +02001705 mtk_tx_irq_disable(eth, MTK_TX_DONE_INT);
John Crispin80673022016-06-29 13:38:11 +02001706 }
John Crispin656e7052016-03-08 11:29:55 +01001707
1708 return IRQ_HANDLED;
1709}
1710
1711#ifdef CONFIG_NET_POLL_CONTROLLER
1712static void mtk_poll_controller(struct net_device *dev)
1713{
1714 struct mtk_mac *mac = netdev_priv(dev);
1715 struct mtk_eth *eth = mac->hw;
John Crispin656e7052016-03-08 11:29:55 +01001716
John Crispin5cce0322017-06-19 15:37:05 +02001717 mtk_tx_irq_disable(eth, MTK_TX_DONE_INT);
1718 mtk_rx_irq_disable(eth, MTK_RX_DONE_INT);
John Crispin8186f6e2016-07-02 08:00:50 +02001719 mtk_handle_irq_rx(eth->irq[2], dev);
John Crispin5cce0322017-06-19 15:37:05 +02001720 mtk_tx_irq_enable(eth, MTK_TX_DONE_INT);
1721 mtk_rx_irq_enable(eth, MTK_RX_DONE_INT);
John Crispin656e7052016-03-08 11:29:55 +01001722}
1723#endif
1724
1725static int mtk_start_dma(struct mtk_eth *eth)
1726{
1727 int err;
1728
1729 err = mtk_dma_init(eth);
1730 if (err) {
1731 mtk_dma_free(eth);
1732 return err;
1733 }
1734
1735 mtk_w32(eth,
Nelson Changbacfd112016-08-26 01:09:42 +08001736 MTK_TX_WB_DDONE | MTK_TX_DMA_EN |
1737 MTK_DMA_SIZE_16DWORDS | MTK_NDP_CO_PRO,
John Crispin656e7052016-03-08 11:29:55 +01001738 MTK_QDMA_GLO_CFG);
1739
Nelson Changbacfd112016-08-26 01:09:42 +08001740 mtk_w32(eth,
1741 MTK_RX_DMA_EN | MTK_RX_2B_OFFSET |
1742 MTK_RX_BT_32DWORDS | MTK_MULTI_EN,
1743 MTK_PDMA_GLO_CFG);
1744
John Crispin656e7052016-03-08 11:29:55 +01001745 return 0;
1746}
1747
1748static int mtk_open(struct net_device *dev)
1749{
1750 struct mtk_mac *mac = netdev_priv(dev);
1751 struct mtk_eth *eth = mac->hw;
1752
1753 /* we run 2 netdevs on the same dma ring so we only bring it up once */
1754 if (!atomic_read(&eth->dma_refcnt)) {
1755 int err = mtk_start_dma(eth);
1756
1757 if (err)
1758 return err;
1759
John Crispin80673022016-06-29 13:38:11 +02001760 napi_enable(&eth->tx_napi);
John Crispin656e7052016-03-08 11:29:55 +01001761 napi_enable(&eth->rx_napi);
John Crispin5cce0322017-06-19 15:37:05 +02001762 mtk_tx_irq_enable(eth, MTK_TX_DONE_INT);
1763 mtk_rx_irq_enable(eth, MTK_RX_DONE_INT);
John Crispin656e7052016-03-08 11:29:55 +01001764 }
1765 atomic_inc(&eth->dma_refcnt);
1766
Sean Wang2364c5c2016-09-22 16:33:35 +08001767 phy_start(dev->phydev);
John Crispin656e7052016-03-08 11:29:55 +01001768 netif_start_queue(dev);
1769
1770 return 0;
1771}
1772
1773static void mtk_stop_dma(struct mtk_eth *eth, u32 glo_cfg)
1774{
John Crispin656e7052016-03-08 11:29:55 +01001775 u32 val;
1776 int i;
1777
1778 /* stop the dma engine */
Sean Wange3e96522016-08-11 17:51:00 +08001779 spin_lock_bh(&eth->page_lock);
John Crispin656e7052016-03-08 11:29:55 +01001780 val = mtk_r32(eth, glo_cfg);
1781 mtk_w32(eth, val & ~(MTK_TX_WB_DDONE | MTK_RX_DMA_EN | MTK_TX_DMA_EN),
1782 glo_cfg);
Sean Wange3e96522016-08-11 17:51:00 +08001783 spin_unlock_bh(&eth->page_lock);
John Crispin656e7052016-03-08 11:29:55 +01001784
1785 /* wait for dma stop */
1786 for (i = 0; i < 10; i++) {
1787 val = mtk_r32(eth, glo_cfg);
1788 if (val & (MTK_TX_DMA_BUSY | MTK_RX_DMA_BUSY)) {
1789 msleep(20);
1790 continue;
1791 }
1792 break;
1793 }
1794}
1795
1796static int mtk_stop(struct net_device *dev)
1797{
1798 struct mtk_mac *mac = netdev_priv(dev);
1799 struct mtk_eth *eth = mac->hw;
1800
1801 netif_tx_disable(dev);
Sean Wang2364c5c2016-09-22 16:33:35 +08001802 phy_stop(dev->phydev);
John Crispin656e7052016-03-08 11:29:55 +01001803
1804 /* only shutdown DMA if this is the last user */
1805 if (!atomic_dec_and_test(&eth->dma_refcnt))
1806 return 0;
1807
John Crispin5cce0322017-06-19 15:37:05 +02001808 mtk_tx_irq_disable(eth, MTK_TX_DONE_INT);
1809 mtk_rx_irq_disable(eth, MTK_RX_DONE_INT);
John Crispin80673022016-06-29 13:38:11 +02001810 napi_disable(&eth->tx_napi);
John Crispin656e7052016-03-08 11:29:55 +01001811 napi_disable(&eth->rx_napi);
1812
1813 mtk_stop_dma(eth, MTK_QDMA_GLO_CFG);
Nelson Chang6bf563d2016-09-26 14:33:49 +08001814 mtk_stop_dma(eth, MTK_PDMA_GLO_CFG);
John Crispin656e7052016-03-08 11:29:55 +01001815
1816 mtk_dma_free(eth);
1817
1818 return 0;
1819}
1820
Sean Wang2a8307a2016-09-14 23:13:20 +08001821static void ethsys_reset(struct mtk_eth *eth, u32 reset_bits)
1822{
1823 regmap_update_bits(eth->ethsys, ETHSYS_RSTCTRL,
1824 reset_bits,
1825 reset_bits);
1826
1827 usleep_range(1000, 1100);
1828 regmap_update_bits(eth->ethsys, ETHSYS_RSTCTRL,
1829 reset_bits,
1830 ~reset_bits);
1831 mdelay(10);
1832}
1833
Sean Wang9ea4d312016-09-14 23:13:19 +08001834static int mtk_hw_init(struct mtk_eth *eth)
John Crispin656e7052016-03-08 11:29:55 +01001835{
Sean Wang9ea4d312016-09-14 23:13:19 +08001836 int i, val;
1837
1838 if (test_and_set_bit(MTK_HW_INIT, &eth->state))
1839 return 0;
Sean Wang85574db2016-09-14 23:13:15 +08001840
Sean Wang26a2ad82016-09-14 23:13:18 +08001841 pm_runtime_enable(eth->dev);
1842 pm_runtime_get_sync(eth->dev);
1843
Sean Wang85574db2016-09-14 23:13:15 +08001844 clk_prepare_enable(eth->clks[MTK_CLK_ETHIF]);
1845 clk_prepare_enable(eth->clks[MTK_CLK_ESW]);
1846 clk_prepare_enable(eth->clks[MTK_CLK_GP1]);
1847 clk_prepare_enable(eth->clks[MTK_CLK_GP2]);
Sean Wang2a8307a2016-09-14 23:13:20 +08001848 ethsys_reset(eth, RSTCTRL_FE);
1849 ethsys_reset(eth, RSTCTRL_PPE);
John Crispin656e7052016-03-08 11:29:55 +01001850
Sean Wang9ea4d312016-09-14 23:13:19 +08001851 regmap_read(eth->ethsys, ETHSYS_SYSCFG0, &val);
1852 for (i = 0; i < MTK_MAC_COUNT; i++) {
1853 if (!eth->mac[i])
1854 continue;
1855 val &= ~SYSCFG0_GE_MODE(SYSCFG0_GE_MASK, eth->mac[i]->id);
1856 val |= SYSCFG0_GE_MODE(eth->mac[i]->ge_mode, eth->mac[i]->id);
1857 }
1858 regmap_write(eth->ethsys, ETHSYS_SYSCFG0, val);
1859
John Crispin656e7052016-03-08 11:29:55 +01001860 /* Set GE2 driving and slew rate */
1861 regmap_write(eth->pctl, GPIO_DRV_SEL10, 0xa00);
1862
1863 /* set GE2 TDSEL */
1864 regmap_write(eth->pctl, GPIO_OD33_CTRL8, 0x5);
1865
1866 /* set GE2 TUNE */
1867 regmap_write(eth->pctl, GPIO_BIAS_CTRL, 0x0);
1868
1869 /* GE1, Force 1000M/FD, FC ON */
1870 mtk_w32(eth, MAC_MCR_FIXED_LINK, MTK_MAC_MCR(0));
1871
1872 /* GE2, Force 1000M/FD, FC ON */
1873 mtk_w32(eth, MAC_MCR_FIXED_LINK, MTK_MAC_MCR(1));
1874
Sean Wang87e3df42017-04-07 16:45:07 +08001875 /* Indicates CDM to parse the MTK special tag from CPU
1876 * which also is working out for untag packets.
1877 */
1878 val = mtk_r32(eth, MTK_CDMQ_IG_CTRL);
1879 mtk_w32(eth, val | MTK_CDMQ_STAG_EN, MTK_CDMQ_IG_CTRL);
1880
John Crispin656e7052016-03-08 11:29:55 +01001881 /* Enable RX VLan Offloading */
1882 mtk_w32(eth, 1, MTK_CDMP_EG_CTRL);
1883
John Crispin671d41e2017-06-19 15:37:04 +02001884 /* enable interrupt delay for RX */
1885 mtk_w32(eth, MTK_PDMA_DELAY_RX_DELAY, MTK_PDMA_DELAY_INT);
1886
John Crispin656e7052016-03-08 11:29:55 +01001887 /* disable delay and normal interrupt */
1888 mtk_w32(eth, 0, MTK_QDMA_DELAY_INT);
John Crispin5cce0322017-06-19 15:37:05 +02001889 mtk_tx_irq_disable(eth, ~0);
1890 mtk_rx_irq_disable(eth, ~0);
John Crispin656e7052016-03-08 11:29:55 +01001891 mtk_w32(eth, RST_GL_PSE, MTK_RST_GL);
1892 mtk_w32(eth, 0, MTK_RST_GL);
1893
1894 /* FE int grouping */
John Crispin80673022016-06-29 13:38:11 +02001895 mtk_w32(eth, MTK_TX_DONE_INT, MTK_PDMA_INT_GRP1);
1896 mtk_w32(eth, MTK_RX_DONE_INT, MTK_PDMA_INT_GRP2);
1897 mtk_w32(eth, MTK_TX_DONE_INT, MTK_QDMA_INT_GRP1);
1898 mtk_w32(eth, MTK_RX_DONE_INT, MTK_QDMA_INT_GRP2);
1899 mtk_w32(eth, 0x21021000, MTK_FE_INT_GRP);
John Crispin656e7052016-03-08 11:29:55 +01001900
1901 for (i = 0; i < 2; i++) {
1902 u32 val = mtk_r32(eth, MTK_GDMA_FWD_CFG(i));
1903
Nelson Chang9c084352016-08-26 01:09:43 +08001904 /* setup the forward port to send frame to PDMA */
John Crispin656e7052016-03-08 11:29:55 +01001905 val &= ~0xffff;
John Crispin656e7052016-03-08 11:29:55 +01001906
1907 /* Enable RX checksum */
1908 val |= MTK_GDMA_ICS_EN | MTK_GDMA_TCS_EN | MTK_GDMA_UCS_EN;
1909
1910 /* setup the mac dma */
1911 mtk_w32(eth, val, MTK_GDMA_FWD_CFG(i));
1912 }
1913
1914 return 0;
1915}
1916
Sean Wangbf253fb2016-09-14 23:13:16 +08001917static int mtk_hw_deinit(struct mtk_eth *eth)
1918{
Sean Wang9ea4d312016-09-14 23:13:19 +08001919 if (!test_and_clear_bit(MTK_HW_INIT, &eth->state))
1920 return 0;
1921
Sean Wangbf253fb2016-09-14 23:13:16 +08001922 clk_disable_unprepare(eth->clks[MTK_CLK_GP2]);
1923 clk_disable_unprepare(eth->clks[MTK_CLK_GP1]);
1924 clk_disable_unprepare(eth->clks[MTK_CLK_ESW]);
1925 clk_disable_unprepare(eth->clks[MTK_CLK_ETHIF]);
1926
Sean Wang26a2ad82016-09-14 23:13:18 +08001927 pm_runtime_put_sync(eth->dev);
1928 pm_runtime_disable(eth->dev);
1929
Sean Wangbf253fb2016-09-14 23:13:16 +08001930 return 0;
1931}
1932
John Crispin656e7052016-03-08 11:29:55 +01001933static int __init mtk_init(struct net_device *dev)
1934{
1935 struct mtk_mac *mac = netdev_priv(dev);
1936 struct mtk_eth *eth = mac->hw;
1937 const char *mac_addr;
1938
1939 mac_addr = of_get_mac_address(mac->of_node);
1940 if (mac_addr)
1941 ether_addr_copy(dev->dev_addr, mac_addr);
1942
1943 /* If the mac address is invalid, use random mac address */
1944 if (!is_valid_ether_addr(dev->dev_addr)) {
Tobias Klausere3c36e42017-03-07 16:27:10 +01001945 eth_hw_addr_random(dev);
John Crispin656e7052016-03-08 11:29:55 +01001946 dev_err(eth->dev, "generated random MAC address %pM\n",
1947 dev->dev_addr);
John Crispin656e7052016-03-08 11:29:55 +01001948 }
1949
Sean Wang2364c5c2016-09-22 16:33:35 +08001950 return mtk_phy_connect(dev);
John Crispin656e7052016-03-08 11:29:55 +01001951}
1952
1953static void mtk_uninit(struct net_device *dev)
1954{
1955 struct mtk_mac *mac = netdev_priv(dev);
1956 struct mtk_eth *eth = mac->hw;
1957
Sean Wang2364c5c2016-09-22 16:33:35 +08001958 phy_disconnect(dev->phydev);
Johan Hovold16a67eb2016-11-28 19:25:05 +01001959 if (of_phy_is_fixed_link(mac->of_node))
1960 of_phy_deregister_fixed_link(mac->of_node);
John Crispin5cce0322017-06-19 15:37:05 +02001961 mtk_tx_irq_disable(eth, ~0);
1962 mtk_rx_irq_disable(eth, ~0);
John Crispin656e7052016-03-08 11:29:55 +01001963}
1964
1965static int mtk_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1966{
John Crispin656e7052016-03-08 11:29:55 +01001967 switch (cmd) {
1968 case SIOCGMIIPHY:
1969 case SIOCGMIIREG:
1970 case SIOCSMIIREG:
Sean Wang2364c5c2016-09-22 16:33:35 +08001971 return phy_mii_ioctl(dev->phydev, ifr, cmd);
John Crispin656e7052016-03-08 11:29:55 +01001972 default:
1973 break;
1974 }
1975
1976 return -EOPNOTSUPP;
1977}
1978
1979static void mtk_pending_work(struct work_struct *work)
1980{
John Crispin7c78b4a2016-04-08 00:54:10 +02001981 struct mtk_eth *eth = container_of(work, struct mtk_eth, pending_work);
John Crispine7d425d2016-04-08 00:54:09 +02001982 int err, i;
1983 unsigned long restart = 0;
John Crispin656e7052016-03-08 11:29:55 +01001984
1985 rtnl_lock();
John Crispin656e7052016-03-08 11:29:55 +01001986
Sean Wangdce6fa42016-09-14 23:13:21 +08001987 dev_dbg(eth->dev, "[%s][%d] reset\n", __func__, __LINE__);
1988
1989 while (test_and_set_bit_lock(MTK_RESETTING, &eth->state))
1990 cpu_relax();
1991
1992 dev_dbg(eth->dev, "[%s][%d] mtk_stop starts\n", __func__, __LINE__);
John Crispine7d425d2016-04-08 00:54:09 +02001993 /* stop all devices to make sure that dma is properly shut down */
1994 for (i = 0; i < MTK_MAC_COUNT; i++) {
John Crispin7c78b4a2016-04-08 00:54:10 +02001995 if (!eth->netdev[i])
John Crispine7d425d2016-04-08 00:54:09 +02001996 continue;
1997 mtk_stop(eth->netdev[i]);
1998 __set_bit(i, &restart);
1999 }
Sean Wangdce6fa42016-09-14 23:13:21 +08002000 dev_dbg(eth->dev, "[%s][%d] mtk_stop ends\n", __func__, __LINE__);
John Crispine7d425d2016-04-08 00:54:09 +02002001
Sean Wang9ea4d312016-09-14 23:13:19 +08002002 /* restart underlying hardware such as power, clock, pin mux
2003 * and the connected phy
2004 */
2005 mtk_hw_deinit(eth);
2006
2007 if (eth->dev->pins)
2008 pinctrl_select_state(eth->dev->pins->p,
2009 eth->dev->pins->default_state);
2010 mtk_hw_init(eth);
2011
2012 for (i = 0; i < MTK_MAC_COUNT; i++) {
2013 if (!eth->mac[i] ||
2014 of_phy_is_fixed_link(eth->mac[i]->of_node))
2015 continue;
Sean Wang2364c5c2016-09-22 16:33:35 +08002016 err = phy_init_hw(eth->netdev[i]->phydev);
Sean Wang9ea4d312016-09-14 23:13:19 +08002017 if (err)
2018 dev_err(eth->dev, "%s: PHY init failed.\n",
2019 eth->netdev[i]->name);
2020 }
2021
John Crispine7d425d2016-04-08 00:54:09 +02002022 /* restart DMA and enable IRQs */
2023 for (i = 0; i < MTK_MAC_COUNT; i++) {
2024 if (!test_bit(i, &restart))
2025 continue;
2026 err = mtk_open(eth->netdev[i]);
2027 if (err) {
2028 netif_alert(eth, ifup, eth->netdev[i],
2029 "Driver up/down cycle failed, closing device.\n");
2030 dev_close(eth->netdev[i]);
2031 }
John Crispin656e7052016-03-08 11:29:55 +01002032 }
Sean Wangdce6fa42016-09-14 23:13:21 +08002033
2034 dev_dbg(eth->dev, "[%s][%d] reset done\n", __func__, __LINE__);
2035
2036 clear_bit_unlock(MTK_RESETTING, &eth->state);
2037
John Crispin656e7052016-03-08 11:29:55 +01002038 rtnl_unlock();
2039}
2040
Sean Wang8a8a9e82016-09-14 23:13:17 +08002041static int mtk_free_dev(struct mtk_eth *eth)
John Crispin656e7052016-03-08 11:29:55 +01002042{
2043 int i;
2044
2045 for (i = 0; i < MTK_MAC_COUNT; i++) {
John Crispin656e7052016-03-08 11:29:55 +01002046 if (!eth->netdev[i])
2047 continue;
John Crispin656e7052016-03-08 11:29:55 +01002048 free_netdev(eth->netdev[i]);
John Crispin656e7052016-03-08 11:29:55 +01002049 }
Sean Wang8a8a9e82016-09-14 23:13:17 +08002050
2051 return 0;
2052}
2053
2054static int mtk_unreg_dev(struct mtk_eth *eth)
2055{
2056 int i;
2057
2058 for (i = 0; i < MTK_MAC_COUNT; i++) {
2059 if (!eth->netdev[i])
2060 continue;
2061 unregister_netdev(eth->netdev[i]);
2062 }
2063
2064 return 0;
2065}
2066
2067static int mtk_cleanup(struct mtk_eth *eth)
2068{
2069 mtk_unreg_dev(eth);
2070 mtk_free_dev(eth);
John Crispin7c78b4a2016-04-08 00:54:10 +02002071 cancel_work_sync(&eth->pending_work);
John Crispin656e7052016-03-08 11:29:55 +01002072
2073 return 0;
2074}
2075
Baoyou Xie3a82e782016-09-30 15:48:50 +08002076static int mtk_get_link_ksettings(struct net_device *ndev,
2077 struct ethtool_link_ksettings *cmd)
John Crispin656e7052016-03-08 11:29:55 +01002078{
Sean Wang3e60b742016-09-22 16:42:03 +08002079 struct mtk_mac *mac = netdev_priv(ndev);
John Crispin656e7052016-03-08 11:29:55 +01002080
Sean Wangdce6fa42016-09-14 23:13:21 +08002081 if (unlikely(test_bit(MTK_RESETTING, &mac->hw->state)))
2082 return -EBUSY;
2083
yuval.shaia@oracle.com55141742017-06-13 10:09:46 +03002084 phy_ethtool_ksettings_get(ndev->phydev, cmd);
2085
2086 return 0;
John Crispin656e7052016-03-08 11:29:55 +01002087}
2088
Baoyou Xie3a82e782016-09-30 15:48:50 +08002089static int mtk_set_link_ksettings(struct net_device *ndev,
2090 const struct ethtool_link_ksettings *cmd)
John Crispin656e7052016-03-08 11:29:55 +01002091{
Sean Wang3e60b742016-09-22 16:42:03 +08002092 struct mtk_mac *mac = netdev_priv(ndev);
John Crispin656e7052016-03-08 11:29:55 +01002093
Sean Wang3e60b742016-09-22 16:42:03 +08002094 if (unlikely(test_bit(MTK_RESETTING, &mac->hw->state)))
2095 return -EBUSY;
John Crispin656e7052016-03-08 11:29:55 +01002096
Sean Wang3e60b742016-09-22 16:42:03 +08002097 return phy_ethtool_ksettings_set(ndev->phydev, cmd);
John Crispin656e7052016-03-08 11:29:55 +01002098}
2099
2100static void mtk_get_drvinfo(struct net_device *dev,
2101 struct ethtool_drvinfo *info)
2102{
2103 struct mtk_mac *mac = netdev_priv(dev);
2104
2105 strlcpy(info->driver, mac->hw->dev->driver->name, sizeof(info->driver));
2106 strlcpy(info->bus_info, dev_name(mac->hw->dev), sizeof(info->bus_info));
2107 info->n_stats = ARRAY_SIZE(mtk_ethtool_stats);
2108}
2109
2110static u32 mtk_get_msglevel(struct net_device *dev)
2111{
2112 struct mtk_mac *mac = netdev_priv(dev);
2113
2114 return mac->hw->msg_enable;
2115}
2116
2117static void mtk_set_msglevel(struct net_device *dev, u32 value)
2118{
2119 struct mtk_mac *mac = netdev_priv(dev);
2120
2121 mac->hw->msg_enable = value;
2122}
2123
2124static int mtk_nway_reset(struct net_device *dev)
2125{
2126 struct mtk_mac *mac = netdev_priv(dev);
2127
Sean Wangdce6fa42016-09-14 23:13:21 +08002128 if (unlikely(test_bit(MTK_RESETTING, &mac->hw->state)))
2129 return -EBUSY;
2130
Sean Wang2364c5c2016-09-22 16:33:35 +08002131 return genphy_restart_aneg(dev->phydev);
John Crispin656e7052016-03-08 11:29:55 +01002132}
2133
2134static u32 mtk_get_link(struct net_device *dev)
2135{
2136 struct mtk_mac *mac = netdev_priv(dev);
2137 int err;
2138
Sean Wangdce6fa42016-09-14 23:13:21 +08002139 if (unlikely(test_bit(MTK_RESETTING, &mac->hw->state)))
2140 return -EBUSY;
2141
Sean Wang2364c5c2016-09-22 16:33:35 +08002142 err = genphy_update_link(dev->phydev);
John Crispin656e7052016-03-08 11:29:55 +01002143 if (err)
2144 return ethtool_op_get_link(dev);
2145
Sean Wang2364c5c2016-09-22 16:33:35 +08002146 return dev->phydev->link;
John Crispin656e7052016-03-08 11:29:55 +01002147}
2148
2149static void mtk_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2150{
2151 int i;
2152
2153 switch (stringset) {
2154 case ETH_SS_STATS:
2155 for (i = 0; i < ARRAY_SIZE(mtk_ethtool_stats); i++) {
2156 memcpy(data, mtk_ethtool_stats[i].str, ETH_GSTRING_LEN);
2157 data += ETH_GSTRING_LEN;
2158 }
2159 break;
2160 }
2161}
2162
2163static int mtk_get_sset_count(struct net_device *dev, int sset)
2164{
2165 switch (sset) {
2166 case ETH_SS_STATS:
2167 return ARRAY_SIZE(mtk_ethtool_stats);
2168 default:
2169 return -EOPNOTSUPP;
2170 }
2171}
2172
2173static void mtk_get_ethtool_stats(struct net_device *dev,
2174 struct ethtool_stats *stats, u64 *data)
2175{
2176 struct mtk_mac *mac = netdev_priv(dev);
2177 struct mtk_hw_stats *hwstats = mac->hw_stats;
2178 u64 *data_src, *data_dst;
2179 unsigned int start;
2180 int i;
2181
Sean Wangdce6fa42016-09-14 23:13:21 +08002182 if (unlikely(test_bit(MTK_RESETTING, &mac->hw->state)))
2183 return;
2184
John Crispin656e7052016-03-08 11:29:55 +01002185 if (netif_running(dev) && netif_device_present(dev)) {
2186 if (spin_trylock(&hwstats->stats_lock)) {
2187 mtk_stats_update_mac(mac);
2188 spin_unlock(&hwstats->stats_lock);
2189 }
2190 }
2191
Sean Wang94d308d2016-09-20 11:26:48 +08002192 data_src = (u64 *)hwstats;
2193
John Crispin656e7052016-03-08 11:29:55 +01002194 do {
John Crispin656e7052016-03-08 11:29:55 +01002195 data_dst = data;
2196 start = u64_stats_fetch_begin_irq(&hwstats->syncp);
2197
2198 for (i = 0; i < ARRAY_SIZE(mtk_ethtool_stats); i++)
2199 *data_dst++ = *(data_src + mtk_ethtool_stats[i].offset);
2200 } while (u64_stats_fetch_retry_irq(&hwstats->syncp, start));
2201}
2202
Nelson Chang7aab7472016-09-17 23:50:56 +08002203static int mtk_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd,
2204 u32 *rule_locs)
2205{
2206 int ret = -EOPNOTSUPP;
2207
2208 switch (cmd->cmd) {
2209 case ETHTOOL_GRXRINGS:
2210 if (dev->features & NETIF_F_LRO) {
2211 cmd->data = MTK_MAX_RX_RING_NUM;
2212 ret = 0;
2213 }
2214 break;
2215 case ETHTOOL_GRXCLSRLCNT:
2216 if (dev->features & NETIF_F_LRO) {
2217 struct mtk_mac *mac = netdev_priv(dev);
2218
2219 cmd->rule_cnt = mac->hwlro_ip_cnt;
2220 ret = 0;
2221 }
2222 break;
2223 case ETHTOOL_GRXCLSRULE:
2224 if (dev->features & NETIF_F_LRO)
2225 ret = mtk_hwlro_get_fdir_entry(dev, cmd);
2226 break;
2227 case ETHTOOL_GRXCLSRLALL:
2228 if (dev->features & NETIF_F_LRO)
2229 ret = mtk_hwlro_get_fdir_all(dev, cmd,
2230 rule_locs);
2231 break;
2232 default:
2233 break;
2234 }
2235
2236 return ret;
2237}
2238
2239static int mtk_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd)
2240{
2241 int ret = -EOPNOTSUPP;
2242
2243 switch (cmd->cmd) {
2244 case ETHTOOL_SRXCLSRLINS:
2245 if (dev->features & NETIF_F_LRO)
2246 ret = mtk_hwlro_add_ipaddr(dev, cmd);
2247 break;
2248 case ETHTOOL_SRXCLSRLDEL:
2249 if (dev->features & NETIF_F_LRO)
2250 ret = mtk_hwlro_del_ipaddr(dev, cmd);
2251 break;
2252 default:
2253 break;
2254 }
2255
2256 return ret;
2257}
2258
Julia Lawall6a38cb12016-09-01 00:21:19 +02002259static const struct ethtool_ops mtk_ethtool_ops = {
Sean Wang3e60b742016-09-22 16:42:03 +08002260 .get_link_ksettings = mtk_get_link_ksettings,
2261 .set_link_ksettings = mtk_set_link_ksettings,
John Crispin656e7052016-03-08 11:29:55 +01002262 .get_drvinfo = mtk_get_drvinfo,
2263 .get_msglevel = mtk_get_msglevel,
2264 .set_msglevel = mtk_set_msglevel,
2265 .nway_reset = mtk_nway_reset,
2266 .get_link = mtk_get_link,
2267 .get_strings = mtk_get_strings,
2268 .get_sset_count = mtk_get_sset_count,
2269 .get_ethtool_stats = mtk_get_ethtool_stats,
Nelson Chang7aab7472016-09-17 23:50:56 +08002270 .get_rxnfc = mtk_get_rxnfc,
2271 .set_rxnfc = mtk_set_rxnfc,
John Crispin656e7052016-03-08 11:29:55 +01002272};
2273
2274static const struct net_device_ops mtk_netdev_ops = {
2275 .ndo_init = mtk_init,
2276 .ndo_uninit = mtk_uninit,
2277 .ndo_open = mtk_open,
2278 .ndo_stop = mtk_stop,
2279 .ndo_start_xmit = mtk_start_xmit,
2280 .ndo_set_mac_address = mtk_set_mac_address,
2281 .ndo_validate_addr = eth_validate_addr,
2282 .ndo_do_ioctl = mtk_do_ioctl,
John Crispin656e7052016-03-08 11:29:55 +01002283 .ndo_tx_timeout = mtk_tx_timeout,
2284 .ndo_get_stats64 = mtk_get_stats64,
Nelson Chang7aab7472016-09-17 23:50:56 +08002285 .ndo_fix_features = mtk_fix_features,
2286 .ndo_set_features = mtk_set_features,
John Crispin656e7052016-03-08 11:29:55 +01002287#ifdef CONFIG_NET_POLL_CONTROLLER
2288 .ndo_poll_controller = mtk_poll_controller,
2289#endif
2290};
2291
2292static int mtk_add_mac(struct mtk_eth *eth, struct device_node *np)
2293{
2294 struct mtk_mac *mac;
2295 const __be32 *_id = of_get_property(np, "reg", NULL);
2296 int id, err;
2297
2298 if (!_id) {
2299 dev_err(eth->dev, "missing mac id\n");
2300 return -EINVAL;
2301 }
2302
2303 id = be32_to_cpup(_id);
2304 if (id >= MTK_MAC_COUNT) {
2305 dev_err(eth->dev, "%d is not a valid mac id\n", id);
2306 return -EINVAL;
2307 }
2308
2309 if (eth->netdev[id]) {
2310 dev_err(eth->dev, "duplicate mac id found: %d\n", id);
2311 return -EINVAL;
2312 }
2313
2314 eth->netdev[id] = alloc_etherdev(sizeof(*mac));
2315 if (!eth->netdev[id]) {
2316 dev_err(eth->dev, "alloc_etherdev failed\n");
2317 return -ENOMEM;
2318 }
2319 mac = netdev_priv(eth->netdev[id]);
2320 eth->mac[id] = mac;
2321 mac->id = id;
2322 mac->hw = eth;
2323 mac->of_node = np;
John Crispin656e7052016-03-08 11:29:55 +01002324
Nelson Changee406812016-09-17 23:50:55 +08002325 memset(mac->hwlro_ip, 0, sizeof(mac->hwlro_ip));
2326 mac->hwlro_ip_cnt = 0;
2327
John Crispin656e7052016-03-08 11:29:55 +01002328 mac->hw_stats = devm_kzalloc(eth->dev,
2329 sizeof(*mac->hw_stats),
2330 GFP_KERNEL);
2331 if (!mac->hw_stats) {
2332 dev_err(eth->dev, "failed to allocate counter memory\n");
2333 err = -ENOMEM;
2334 goto free_netdev;
2335 }
2336 spin_lock_init(&mac->hw_stats->stats_lock);
sean.wang@mediatek.comd70056522016-08-13 19:16:18 +08002337 u64_stats_init(&mac->hw_stats->syncp);
John Crispin656e7052016-03-08 11:29:55 +01002338 mac->hw_stats->reg_offset = id * MTK_STAT_OFFSET;
2339
2340 SET_NETDEV_DEV(eth->netdev[id], eth->dev);
John Crispineaadf9f2016-06-10 13:28:05 +02002341 eth->netdev[id]->watchdog_timeo = 5 * HZ;
John Crispin656e7052016-03-08 11:29:55 +01002342 eth->netdev[id]->netdev_ops = &mtk_netdev_ops;
2343 eth->netdev[id]->base_addr = (unsigned long)eth->base;
Nelson Changee406812016-09-17 23:50:55 +08002344
2345 eth->netdev[id]->hw_features = MTK_HW_FEATURES;
2346 if (eth->hwlro)
2347 eth->netdev[id]->hw_features |= NETIF_F_LRO;
2348
John Crispin656e7052016-03-08 11:29:55 +01002349 eth->netdev[id]->vlan_features = MTK_HW_FEATURES &
2350 ~(NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX);
2351 eth->netdev[id]->features |= MTK_HW_FEATURES;
2352 eth->netdev[id]->ethtool_ops = &mtk_ethtool_ops;
2353
John Crispin80673022016-06-29 13:38:11 +02002354 eth->netdev[id]->irq = eth->irq[0];
Sean Wang3174b3b2017-04-07 16:45:08 +08002355 eth->netdev[id]->dev.of_node = np;
2356
John Crispin656e7052016-03-08 11:29:55 +01002357 return 0;
2358
2359free_netdev:
2360 free_netdev(eth->netdev[id]);
2361 return err;
2362}
2363
Nelson Changb95b6d92016-10-06 19:44:01 +08002364static int mtk_get_chip_id(struct mtk_eth *eth, u32 *chip_id)
2365{
2366 u32 val[2], id[4];
2367
2368 regmap_read(eth->ethsys, ETHSYS_CHIPID0_3, &val[0]);
2369 regmap_read(eth->ethsys, ETHSYS_CHIPID4_7, &val[1]);
2370
2371 id[3] = ((val[0] >> 16) & 0xff) - '0';
2372 id[2] = ((val[0] >> 24) & 0xff) - '0';
2373 id[1] = (val[1] & 0xff) - '0';
2374 id[0] = ((val[1] >> 8) & 0xff) - '0';
2375
2376 *chip_id = (id[3] * 1000) + (id[2] * 100) +
2377 (id[1] * 10) + id[0];
2378
2379 if (!(*chip_id)) {
2380 dev_err(eth->dev, "failed to get chip id\n");
2381 return -ENODEV;
2382 }
2383
2384 dev_info(eth->dev, "chip id = %d\n", *chip_id);
2385
2386 return 0;
2387}
2388
Nelson Chang983e1a62016-10-06 19:44:02 +08002389static bool mtk_is_hwlro_supported(struct mtk_eth *eth)
2390{
2391 switch (eth->chip_id) {
2392 case MT7623_ETH:
2393 return true;
2394 }
2395
2396 return false;
2397}
2398
John Crispin656e7052016-03-08 11:29:55 +01002399static int mtk_probe(struct platform_device *pdev)
2400{
2401 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2402 struct device_node *mac_np;
2403 const struct of_device_id *match;
2404 struct mtk_soc_data *soc;
2405 struct mtk_eth *eth;
2406 int err;
John Crispin80673022016-06-29 13:38:11 +02002407 int i;
John Crispin656e7052016-03-08 11:29:55 +01002408
John Crispin656e7052016-03-08 11:29:55 +01002409 match = of_match_device(of_mtk_match, &pdev->dev);
2410 soc = (struct mtk_soc_data *)match->data;
2411
2412 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");