blob: 92be59a1e4e72aed00d8f7e5dafe46de7319084d [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
Nelson Changbacfd112016-08-26 01:09:42 +0800375static inline void mtk_irq_disable(struct mtk_eth *eth,
376 unsigned reg, u32 mask)
John Crispin656e7052016-03-08 11:29:55 +0100377{
John Crispin7bc9cce2016-06-29 13:38:10 +0200378 unsigned long flags;
John Crispin656e7052016-03-08 11:29:55 +0100379 u32 val;
380
John Crispin7bc9cce2016-06-29 13:38:10 +0200381 spin_lock_irqsave(&eth->irq_lock, flags);
Nelson Changbacfd112016-08-26 01:09:42 +0800382 val = mtk_r32(eth, reg);
383 mtk_w32(eth, val & ~mask, reg);
John Crispin7bc9cce2016-06-29 13:38:10 +0200384 spin_unlock_irqrestore(&eth->irq_lock, flags);
John Crispin656e7052016-03-08 11:29:55 +0100385}
386
Nelson Changbacfd112016-08-26 01:09:42 +0800387static inline void mtk_irq_enable(struct mtk_eth *eth,
388 unsigned reg, u32 mask)
John Crispin656e7052016-03-08 11:29:55 +0100389{
John Crispin7bc9cce2016-06-29 13:38:10 +0200390 unsigned long flags;
John Crispin656e7052016-03-08 11:29:55 +0100391 u32 val;
392
John Crispin7bc9cce2016-06-29 13:38:10 +0200393 spin_lock_irqsave(&eth->irq_lock, flags);
Nelson Changbacfd112016-08-26 01:09:42 +0800394 val = mtk_r32(eth, reg);
395 mtk_w32(eth, val | mask, reg);
John Crispin7bc9cce2016-06-29 13:38:10 +0200396 spin_unlock_irqrestore(&eth->irq_lock, flags);
John Crispin656e7052016-03-08 11:29:55 +0100397}
398
399static int mtk_set_mac_address(struct net_device *dev, void *p)
400{
401 int ret = eth_mac_addr(dev, p);
402 struct mtk_mac *mac = netdev_priv(dev);
403 const char *macaddr = dev->dev_addr;
John Crispin656e7052016-03-08 11:29:55 +0100404
405 if (ret)
406 return ret;
407
Sean Wangdce6fa42016-09-14 23:13:21 +0800408 if (unlikely(test_bit(MTK_RESETTING, &mac->hw->state)))
409 return -EBUSY;
410
Sean Wange3e96522016-08-11 17:51:00 +0800411 spin_lock_bh(&mac->hw->page_lock);
John Crispin656e7052016-03-08 11:29:55 +0100412 mtk_w32(mac->hw, (macaddr[0] << 8) | macaddr[1],
413 MTK_GDMA_MAC_ADRH(mac->id));
414 mtk_w32(mac->hw, (macaddr[2] << 24) | (macaddr[3] << 16) |
415 (macaddr[4] << 8) | macaddr[5],
416 MTK_GDMA_MAC_ADRL(mac->id));
Sean Wange3e96522016-08-11 17:51:00 +0800417 spin_unlock_bh(&mac->hw->page_lock);
John Crispin656e7052016-03-08 11:29:55 +0100418
419 return 0;
420}
421
422void mtk_stats_update_mac(struct mtk_mac *mac)
423{
424 struct mtk_hw_stats *hw_stats = mac->hw_stats;
425 unsigned int base = MTK_GDM1_TX_GBCNT;
426 u64 stats;
427
428 base += hw_stats->reg_offset;
429
430 u64_stats_update_begin(&hw_stats->syncp);
431
432 hw_stats->rx_bytes += mtk_r32(mac->hw, base);
433 stats = mtk_r32(mac->hw, base + 0x04);
434 if (stats)
435 hw_stats->rx_bytes += (stats << 32);
436 hw_stats->rx_packets += mtk_r32(mac->hw, base + 0x08);
437 hw_stats->rx_overflow += mtk_r32(mac->hw, base + 0x10);
438 hw_stats->rx_fcs_errors += mtk_r32(mac->hw, base + 0x14);
439 hw_stats->rx_short_errors += mtk_r32(mac->hw, base + 0x18);
440 hw_stats->rx_long_errors += mtk_r32(mac->hw, base + 0x1c);
441 hw_stats->rx_checksum_errors += mtk_r32(mac->hw, base + 0x20);
442 hw_stats->rx_flow_control_packets +=
443 mtk_r32(mac->hw, base + 0x24);
444 hw_stats->tx_skip += mtk_r32(mac->hw, base + 0x28);
445 hw_stats->tx_collisions += mtk_r32(mac->hw, base + 0x2c);
446 hw_stats->tx_bytes += mtk_r32(mac->hw, base + 0x30);
447 stats = mtk_r32(mac->hw, base + 0x34);
448 if (stats)
449 hw_stats->tx_bytes += (stats << 32);
450 hw_stats->tx_packets += mtk_r32(mac->hw, base + 0x38);
451 u64_stats_update_end(&hw_stats->syncp);
452}
453
454static void mtk_stats_update(struct mtk_eth *eth)
455{
456 int i;
457
458 for (i = 0; i < MTK_MAC_COUNT; i++) {
459 if (!eth->mac[i] || !eth->mac[i]->hw_stats)
460 continue;
461 if (spin_trylock(&eth->mac[i]->hw_stats->stats_lock)) {
462 mtk_stats_update_mac(eth->mac[i]);
463 spin_unlock(&eth->mac[i]->hw_stats->stats_lock);
464 }
465 }
466}
467
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800468static void mtk_get_stats64(struct net_device *dev,
469 struct rtnl_link_stats64 *storage)
John Crispin656e7052016-03-08 11:29:55 +0100470{
471 struct mtk_mac *mac = netdev_priv(dev);
472 struct mtk_hw_stats *hw_stats = mac->hw_stats;
473 unsigned int start;
474
475 if (netif_running(dev) && netif_device_present(dev)) {
476 if (spin_trylock(&hw_stats->stats_lock)) {
477 mtk_stats_update_mac(mac);
478 spin_unlock(&hw_stats->stats_lock);
479 }
480 }
481
482 do {
483 start = u64_stats_fetch_begin_irq(&hw_stats->syncp);
484 storage->rx_packets = hw_stats->rx_packets;
485 storage->tx_packets = hw_stats->tx_packets;
486 storage->rx_bytes = hw_stats->rx_bytes;
487 storage->tx_bytes = hw_stats->tx_bytes;
488 storage->collisions = hw_stats->tx_collisions;
489 storage->rx_length_errors = hw_stats->rx_short_errors +
490 hw_stats->rx_long_errors;
491 storage->rx_over_errors = hw_stats->rx_overflow;
492 storage->rx_crc_errors = hw_stats->rx_fcs_errors;
493 storage->rx_errors = hw_stats->rx_checksum_errors;
494 storage->tx_aborted_errors = hw_stats->tx_skip;
495 } while (u64_stats_fetch_retry_irq(&hw_stats->syncp, start));
496
497 storage->tx_errors = dev->stats.tx_errors;
498 storage->rx_dropped = dev->stats.rx_dropped;
499 storage->tx_dropped = dev->stats.tx_dropped;
John Crispin656e7052016-03-08 11:29:55 +0100500}
501
502static inline int mtk_max_frag_size(int mtu)
503{
504 /* make sure buf_size will be at least MTK_MAX_RX_LENGTH */
505 if (mtu + MTK_RX_ETH_HLEN < MTK_MAX_RX_LENGTH)
506 mtu = MTK_MAX_RX_LENGTH - MTK_RX_ETH_HLEN;
507
508 return SKB_DATA_ALIGN(MTK_RX_HLEN + mtu) +
509 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
510}
511
512static inline int mtk_max_buf_size(int frag_size)
513{
514 int buf_size = frag_size - NET_SKB_PAD - NET_IP_ALIGN -
515 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
516
517 WARN_ON(buf_size < MTK_MAX_RX_LENGTH);
518
519 return buf_size;
520}
521
522static inline void mtk_rx_get_desc(struct mtk_rx_dma *rxd,
523 struct mtk_rx_dma *dma_rxd)
524{
525 rxd->rxd1 = READ_ONCE(dma_rxd->rxd1);
526 rxd->rxd2 = READ_ONCE(dma_rxd->rxd2);
527 rxd->rxd3 = READ_ONCE(dma_rxd->rxd3);
528 rxd->rxd4 = READ_ONCE(dma_rxd->rxd4);
529}
530
531/* the qdma core needs scratch memory to be setup */
532static int mtk_init_fq_dma(struct mtk_eth *eth)
533{
John Crispin605e4fe2016-06-10 13:27:59 +0200534 dma_addr_t phy_ring_tail;
John Crispin656e7052016-03-08 11:29:55 +0100535 int cnt = MTK_DMA_SIZE;
536 dma_addr_t dma_addr;
537 int i;
538
539 eth->scratch_ring = dma_alloc_coherent(eth->dev,
540 cnt * sizeof(struct mtk_tx_dma),
John Crispin605e4fe2016-06-10 13:27:59 +0200541 &eth->phy_scratch_ring,
John Crispin656e7052016-03-08 11:29:55 +0100542 GFP_ATOMIC | __GFP_ZERO);
543 if (unlikely(!eth->scratch_ring))
544 return -ENOMEM;
545
546 eth->scratch_head = kcalloc(cnt, MTK_QDMA_PAGE_SIZE,
547 GFP_KERNEL);
John Crispin562c5a72016-06-10 13:27:58 +0200548 if (unlikely(!eth->scratch_head))
549 return -ENOMEM;
550
John Crispin656e7052016-03-08 11:29:55 +0100551 dma_addr = dma_map_single(eth->dev,
552 eth->scratch_head, cnt * MTK_QDMA_PAGE_SIZE,
553 DMA_FROM_DEVICE);
554 if (unlikely(dma_mapping_error(eth->dev, dma_addr)))
555 return -ENOMEM;
556
557 memset(eth->scratch_ring, 0x0, sizeof(struct mtk_tx_dma) * cnt);
John Crispin605e4fe2016-06-10 13:27:59 +0200558 phy_ring_tail = eth->phy_scratch_ring +
John Crispin656e7052016-03-08 11:29:55 +0100559 (sizeof(struct mtk_tx_dma) * (cnt - 1));
560
561 for (i = 0; i < cnt; i++) {
562 eth->scratch_ring[i].txd1 =
563 (dma_addr + (i * MTK_QDMA_PAGE_SIZE));
564 if (i < cnt - 1)
John Crispin605e4fe2016-06-10 13:27:59 +0200565 eth->scratch_ring[i].txd2 = (eth->phy_scratch_ring +
John Crispin656e7052016-03-08 11:29:55 +0100566 ((i + 1) * sizeof(struct mtk_tx_dma)));
567 eth->scratch_ring[i].txd3 = TX_DMA_SDL(MTK_QDMA_PAGE_SIZE);
568 }
569
John Crispin605e4fe2016-06-10 13:27:59 +0200570 mtk_w32(eth, eth->phy_scratch_ring, MTK_QDMA_FQ_HEAD);
John Crispin656e7052016-03-08 11:29:55 +0100571 mtk_w32(eth, phy_ring_tail, MTK_QDMA_FQ_TAIL);
572 mtk_w32(eth, (cnt << 16) | cnt, MTK_QDMA_FQ_CNT);
573 mtk_w32(eth, MTK_QDMA_PAGE_SIZE << 16, MTK_QDMA_FQ_BLEN);
574
575 return 0;
576}
577
578static inline void *mtk_qdma_phys_to_virt(struct mtk_tx_ring *ring, u32 desc)
579{
580 void *ret = ring->dma;
581
582 return ret + (desc - ring->phys);
583}
584
585static inline struct mtk_tx_buf *mtk_desc_to_tx_buf(struct mtk_tx_ring *ring,
586 struct mtk_tx_dma *txd)
587{
588 int idx = txd - ring->dma;
589
590 return &ring->buf[idx];
591}
592
sean.wang@mediatek.com55a4e772016-08-16 13:55:15 +0800593static void mtk_tx_unmap(struct mtk_eth *eth, struct mtk_tx_buf *tx_buf)
John Crispin656e7052016-03-08 11:29:55 +0100594{
595 if (tx_buf->flags & MTK_TX_FLAGS_SINGLE0) {
sean.wang@mediatek.com55a4e772016-08-16 13:55:15 +0800596 dma_unmap_single(eth->dev,
John Crispin656e7052016-03-08 11:29:55 +0100597 dma_unmap_addr(tx_buf, dma_addr0),
598 dma_unmap_len(tx_buf, dma_len0),
599 DMA_TO_DEVICE);
600 } else if (tx_buf->flags & MTK_TX_FLAGS_PAGE0) {
sean.wang@mediatek.com55a4e772016-08-16 13:55:15 +0800601 dma_unmap_page(eth->dev,
John Crispin656e7052016-03-08 11:29:55 +0100602 dma_unmap_addr(tx_buf, dma_addr0),
603 dma_unmap_len(tx_buf, dma_len0),
604 DMA_TO_DEVICE);
605 }
606 tx_buf->flags = 0;
607 if (tx_buf->skb &&
608 (tx_buf->skb != (struct sk_buff *)MTK_DMA_DUMMY_DESC))
609 dev_kfree_skb_any(tx_buf->skb);
610 tx_buf->skb = NULL;
611}
612
613static int mtk_tx_map(struct sk_buff *skb, struct net_device *dev,
614 int tx_num, struct mtk_tx_ring *ring, bool gso)
615{
616 struct mtk_mac *mac = netdev_priv(dev);
617 struct mtk_eth *eth = mac->hw;
618 struct mtk_tx_dma *itxd, *txd;
Sean Wang81d2dd02017-04-14 11:19:11 +0800619 struct mtk_tx_buf *itx_buf, *tx_buf;
John Crispin656e7052016-03-08 11:29:55 +0100620 dma_addr_t mapped_addr;
621 unsigned int nr_frags;
622 int i, n_desc = 1;
Sean Wangc6f1dc42016-09-01 10:47:27 +0800623 u32 txd4 = 0, fport;
John Crispin656e7052016-03-08 11:29:55 +0100624
625 itxd = ring->next_free;
626 if (itxd == ring->last_free)
627 return -ENOMEM;
628
629 /* set the forward port */
Sean Wangc6f1dc42016-09-01 10:47:27 +0800630 fport = (mac->id + 1) << TX_DMA_FPORT_SHIFT;
631 txd4 |= fport;
John Crispin656e7052016-03-08 11:29:55 +0100632
Sean Wang81d2dd02017-04-14 11:19:11 +0800633 itx_buf = mtk_desc_to_tx_buf(ring, itxd);
634 memset(itx_buf, 0, sizeof(*itx_buf));
John Crispin656e7052016-03-08 11:29:55 +0100635
636 if (gso)
637 txd4 |= TX_DMA_TSO;
638
639 /* TX Checksum offload */
640 if (skb->ip_summed == CHECKSUM_PARTIAL)
641 txd4 |= TX_DMA_CHKSUM;
642
643 /* VLAN header offload */
644 if (skb_vlan_tag_present(skb))
645 txd4 |= TX_DMA_INS_VLAN | skb_vlan_tag_get(skb);
646
sean.wang@mediatek.com55a4e772016-08-16 13:55:15 +0800647 mapped_addr = dma_map_single(eth->dev, skb->data,
John Crispin656e7052016-03-08 11:29:55 +0100648 skb_headlen(skb), DMA_TO_DEVICE);
sean.wang@mediatek.com55a4e772016-08-16 13:55:15 +0800649 if (unlikely(dma_mapping_error(eth->dev, mapped_addr)))
John Crispin656e7052016-03-08 11:29:55 +0100650 return -ENOMEM;
651
John Crispin656e7052016-03-08 11:29:55 +0100652 WRITE_ONCE(itxd->txd1, mapped_addr);
Sean Wang81d2dd02017-04-14 11:19:11 +0800653 itx_buf->flags |= MTK_TX_FLAGS_SINGLE0;
Sean Wang134d2152017-04-14 11:19:12 +0800654 itx_buf->flags |= (!mac->id) ? MTK_TX_FLAGS_FPORT0 :
655 MTK_TX_FLAGS_FPORT1;
Sean Wang81d2dd02017-04-14 11:19:11 +0800656 dma_unmap_addr_set(itx_buf, dma_addr0, mapped_addr);
657 dma_unmap_len_set(itx_buf, dma_len0, skb_headlen(skb));
John Crispin656e7052016-03-08 11:29:55 +0100658
659 /* TX SG offload */
660 txd = itxd;
661 nr_frags = skb_shinfo(skb)->nr_frags;
662 for (i = 0; i < nr_frags; i++) {
663 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i];
664 unsigned int offset = 0;
665 int frag_size = skb_frag_size(frag);
666
667 while (frag_size) {
668 bool last_frag = false;
669 unsigned int frag_map_size;
670
671 txd = mtk_qdma_phys_to_virt(ring, txd->txd2);
672 if (txd == ring->last_free)
673 goto err_dma;
674
675 n_desc++;
676 frag_map_size = min(frag_size, MTK_TX_DMA_BUF_LEN);
sean.wang@mediatek.com55a4e772016-08-16 13:55:15 +0800677 mapped_addr = skb_frag_dma_map(eth->dev, frag, offset,
John Crispin656e7052016-03-08 11:29:55 +0100678 frag_map_size,
679 DMA_TO_DEVICE);
sean.wang@mediatek.com55a4e772016-08-16 13:55:15 +0800680 if (unlikely(dma_mapping_error(eth->dev, mapped_addr)))
John Crispin656e7052016-03-08 11:29:55 +0100681 goto err_dma;
682
683 if (i == nr_frags - 1 &&
684 (frag_size - frag_map_size) == 0)
685 last_frag = true;
686
687 WRITE_ONCE(txd->txd1, mapped_addr);
688 WRITE_ONCE(txd->txd3, (TX_DMA_SWC |
689 TX_DMA_PLEN0(frag_map_size) |
John Crispin369f0452016-04-08 00:54:11 +0200690 last_frag * TX_DMA_LS0));
Sean Wangc6f1dc42016-09-01 10:47:27 +0800691 WRITE_ONCE(txd->txd4, fport);
John Crispin656e7052016-03-08 11:29:55 +0100692
John Crispin656e7052016-03-08 11:29:55 +0100693 tx_buf = mtk_desc_to_tx_buf(ring, txd);
694 memset(tx_buf, 0, sizeof(*tx_buf));
Sean Wang81d2dd02017-04-14 11:19:11 +0800695 tx_buf->skb = (struct sk_buff *)MTK_DMA_DUMMY_DESC;
John Crispin656e7052016-03-08 11:29:55 +0100696 tx_buf->flags |= MTK_TX_FLAGS_PAGE0;
Sean Wang134d2152017-04-14 11:19:12 +0800697 tx_buf->flags |= (!mac->id) ? MTK_TX_FLAGS_FPORT0 :
698 MTK_TX_FLAGS_FPORT1;
699
John Crispin656e7052016-03-08 11:29:55 +0100700 dma_unmap_addr_set(tx_buf, dma_addr0, mapped_addr);
701 dma_unmap_len_set(tx_buf, dma_len0, frag_map_size);
702 frag_size -= frag_map_size;
703 offset += frag_map_size;
704 }
705 }
706
707 /* store skb to cleanup */
Sean Wang81d2dd02017-04-14 11:19:11 +0800708 itx_buf->skb = skb;
John Crispin656e7052016-03-08 11:29:55 +0100709
710 WRITE_ONCE(itxd->txd4, txd4);
711 WRITE_ONCE(itxd->txd3, (TX_DMA_SWC | TX_DMA_PLEN0(skb_headlen(skb)) |
712 (!nr_frags * TX_DMA_LS0)));
713
John Crispin656e7052016-03-08 11:29:55 +0100714 netdev_sent_queue(dev, skb->len);
715 skb_tx_timestamp(skb);
716
717 ring->next_free = mtk_qdma_phys_to_virt(ring, txd->txd2);
718 atomic_sub(n_desc, &ring->free_count);
719
720 /* make sure that all changes to the dma ring are flushed before we
721 * continue
722 */
723 wmb();
724
725 if (netif_xmit_stopped(netdev_get_tx_queue(dev, 0)) || !skb->xmit_more)
726 mtk_w32(eth, txd->txd2, MTK_QTX_CTX_PTR);
727
728 return 0;
729
730err_dma:
731 do {
John Crispin2fae7232016-06-10 13:28:00 +0200732 tx_buf = mtk_desc_to_tx_buf(ring, itxd);
John Crispin656e7052016-03-08 11:29:55 +0100733
734 /* unmap dma */
sean.wang@mediatek.com55a4e772016-08-16 13:55:15 +0800735 mtk_tx_unmap(eth, tx_buf);
John Crispin656e7052016-03-08 11:29:55 +0100736
737 itxd->txd3 = TX_DMA_LS0 | TX_DMA_OWNER_CPU;
738 itxd = mtk_qdma_phys_to_virt(ring, itxd->txd2);
739 } while (itxd != txd);
740
741 return -ENOMEM;
742}
743
744static inline int mtk_cal_txd_req(struct sk_buff *skb)
745{
746 int i, nfrags;
747 struct skb_frag_struct *frag;
748
749 nfrags = 1;
750 if (skb_is_gso(skb)) {
751 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
752 frag = &skb_shinfo(skb)->frags[i];
753 nfrags += DIV_ROUND_UP(frag->size, MTK_TX_DMA_BUF_LEN);
754 }
755 } else {
756 nfrags += skb_shinfo(skb)->nr_frags;
757 }
758
John Crispinbeeb4ca2016-04-08 00:54:05 +0200759 return nfrags;
John Crispin656e7052016-03-08 11:29:55 +0100760}
761
John Crispinad3cba92016-06-10 13:28:07 +0200762static int mtk_queue_stopped(struct mtk_eth *eth)
763{
764 int i;
765
766 for (i = 0; i < MTK_MAC_COUNT; i++) {
767 if (!eth->netdev[i])
768 continue;
769 if (netif_queue_stopped(eth->netdev[i]))
770 return 1;
771 }
772
773 return 0;
774}
775
John Crispin13c822f2016-04-08 00:54:07 +0200776static void mtk_wake_queue(struct mtk_eth *eth)
777{
778 int i;
779
780 for (i = 0; i < MTK_MAC_COUNT; i++) {
781 if (!eth->netdev[i])
782 continue;
783 netif_wake_queue(eth->netdev[i]);
784 }
785}
786
787static void mtk_stop_queue(struct mtk_eth *eth)
788{
789 int i;
790
791 for (i = 0; i < MTK_MAC_COUNT; i++) {
792 if (!eth->netdev[i])
793 continue;
794 netif_stop_queue(eth->netdev[i]);
795 }
796}
797
John Crispin656e7052016-03-08 11:29:55 +0100798static int mtk_start_xmit(struct sk_buff *skb, struct net_device *dev)
799{
800 struct mtk_mac *mac = netdev_priv(dev);
801 struct mtk_eth *eth = mac->hw;
802 struct mtk_tx_ring *ring = &eth->tx_ring;
803 struct net_device_stats *stats = &dev->stats;
804 bool gso = false;
805 int tx_num;
806
John Crispin34c2e4c2016-04-08 00:54:08 +0200807 /* normally we can rely on the stack not calling this more than once,
808 * however we have 2 queues running on the same ring so we need to lock
809 * the ring access
810 */
Sean Wange3e96522016-08-11 17:51:00 +0800811 spin_lock(&eth->page_lock);
John Crispin34c2e4c2016-04-08 00:54:08 +0200812
Sean Wangdce6fa42016-09-14 23:13:21 +0800813 if (unlikely(test_bit(MTK_RESETTING, &eth->state)))
814 goto drop;
815
John Crispin656e7052016-03-08 11:29:55 +0100816 tx_num = mtk_cal_txd_req(skb);
817 if (unlikely(atomic_read(&ring->free_count) <= tx_num)) {
John Crispin13c822f2016-04-08 00:54:07 +0200818 mtk_stop_queue(eth);
John Crispin656e7052016-03-08 11:29:55 +0100819 netif_err(eth, tx_queued, dev,
820 "Tx Ring full when queue awake!\n");
Sean Wange3e96522016-08-11 17:51:00 +0800821 spin_unlock(&eth->page_lock);
John Crispin656e7052016-03-08 11:29:55 +0100822 return NETDEV_TX_BUSY;
823 }
824
825 /* TSO: fill MSS info in tcp checksum field */
826 if (skb_is_gso(skb)) {
827 if (skb_cow_head(skb, 0)) {
828 netif_warn(eth, tx_err, dev,
829 "GSO expand head fail.\n");
830 goto drop;
831 }
832
833 if (skb_shinfo(skb)->gso_type &
834 (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)) {
835 gso = true;
836 tcp_hdr(skb)->check = htons(skb_shinfo(skb)->gso_size);
837 }
838 }
839
840 if (mtk_tx_map(skb, dev, tx_num, ring, gso) < 0)
841 goto drop;
842
John Crispin82c65442016-06-10 13:28:08 +0200843 if (unlikely(atomic_read(&ring->free_count) <= ring->thresh))
John Crispin13c822f2016-04-08 00:54:07 +0200844 mtk_stop_queue(eth);
John Crispin82c65442016-06-10 13:28:08 +0200845
Sean Wange3e96522016-08-11 17:51:00 +0800846 spin_unlock(&eth->page_lock);
John Crispin656e7052016-03-08 11:29:55 +0100847
848 return NETDEV_TX_OK;
849
850drop:
Sean Wange3e96522016-08-11 17:51:00 +0800851 spin_unlock(&eth->page_lock);
John Crispin656e7052016-03-08 11:29:55 +0100852 stats->tx_dropped++;
Wei Yongjun81ad2b72016-10-20 17:00:32 +0000853 dev_kfree_skb_any(skb);
John Crispin656e7052016-03-08 11:29:55 +0100854 return NETDEV_TX_OK;
855}
856
Nelson Changee406812016-09-17 23:50:55 +0800857static struct mtk_rx_ring *mtk_get_rx_ring(struct mtk_eth *eth)
858{
859 int i;
860 struct mtk_rx_ring *ring;
861 int idx;
862
863 if (!eth->hwlro)
864 return &eth->rx_ring[0];
865
866 for (i = 0; i < MTK_MAX_RX_RING_NUM; i++) {
867 ring = &eth->rx_ring[i];
868 idx = NEXT_RX_DESP_IDX(ring->calc_idx, ring->dma_size);
869 if (ring->dma[idx].rxd2 & RX_DMA_DONE) {
870 ring->calc_idx_update = true;
871 return ring;
872 }
873 }
874
875 return NULL;
876}
877
878static void mtk_update_rx_cpu_idx(struct mtk_eth *eth)
879{
880 struct mtk_rx_ring *ring;
881 int i;
882
883 if (!eth->hwlro) {
884 ring = &eth->rx_ring[0];
885 mtk_w32(eth, ring->calc_idx, ring->crx_idx_reg);
886 } else {
887 for (i = 0; i < MTK_MAX_RX_RING_NUM; i++) {
888 ring = &eth->rx_ring[i];
889 if (ring->calc_idx_update) {
890 ring->calc_idx_update = false;
891 mtk_w32(eth, ring->calc_idx, ring->crx_idx_reg);
892 }
893 }
894 }
895}
896
John Crispin656e7052016-03-08 11:29:55 +0100897static int mtk_poll_rx(struct napi_struct *napi, int budget,
John Crispineece71e2016-06-29 13:38:09 +0200898 struct mtk_eth *eth)
John Crispin656e7052016-03-08 11:29:55 +0100899{
Nelson Changee406812016-09-17 23:50:55 +0800900 struct mtk_rx_ring *ring;
901 int idx;
John Crispin656e7052016-03-08 11:29:55 +0100902 struct sk_buff *skb;
903 u8 *data, *new_data;
904 struct mtk_rx_dma *rxd, trxd;
905 int done = 0;
906
907 while (done < budget) {
908 struct net_device *netdev;
909 unsigned int pktlen;
910 dma_addr_t dma_addr;
911 int mac = 0;
912
Nelson Changee406812016-09-17 23:50:55 +0800913 ring = mtk_get_rx_ring(eth);
914 if (unlikely(!ring))
915 goto rx_done;
916
917 idx = NEXT_RX_DESP_IDX(ring->calc_idx, ring->dma_size);
John Crispin656e7052016-03-08 11:29:55 +0100918 rxd = &ring->dma[idx];
919 data = ring->data[idx];
920
921 mtk_rx_get_desc(&trxd, rxd);
922 if (!(trxd.rxd2 & RX_DMA_DONE))
923 break;
924
925 /* find out which mac the packet come from. values start at 1 */
926 mac = (trxd.rxd4 >> RX_DMA_FPORT_SHIFT) &
927 RX_DMA_FPORT_MASK;
928 mac--;
929
930 netdev = eth->netdev[mac];
931
Sean Wangdce6fa42016-09-14 23:13:21 +0800932 if (unlikely(test_bit(MTK_RESETTING, &eth->state)))
933 goto release_desc;
934
John Crispin656e7052016-03-08 11:29:55 +0100935 /* alloc new buffer */
936 new_data = napi_alloc_frag(ring->frag_size);
937 if (unlikely(!new_data)) {
938 netdev->stats.rx_dropped++;
939 goto release_desc;
940 }
sean.wang@mediatek.com55a4e772016-08-16 13:55:15 +0800941 dma_addr = dma_map_single(eth->dev,
John Crispin656e7052016-03-08 11:29:55 +0100942 new_data + NET_SKB_PAD,
943 ring->buf_size,
944 DMA_FROM_DEVICE);
sean.wang@mediatek.com55a4e772016-08-16 13:55:15 +0800945 if (unlikely(dma_mapping_error(eth->dev, dma_addr))) {
John Crispin656e7052016-03-08 11:29:55 +0100946 skb_free_frag(new_data);
John Crispin94321a92016-06-10 13:28:01 +0200947 netdev->stats.rx_dropped++;
John Crispin656e7052016-03-08 11:29:55 +0100948 goto release_desc;
949 }
950
951 /* receive data */
952 skb = build_skb(data, ring->frag_size);
953 if (unlikely(!skb)) {
Sean Wang1b430792016-09-01 10:47:29 +0800954 skb_free_frag(new_data);
John Crispin94321a92016-06-10 13:28:01 +0200955 netdev->stats.rx_dropped++;
John Crispin656e7052016-03-08 11:29:55 +0100956 goto release_desc;
957 }
958 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
959
sean.wang@mediatek.com55a4e772016-08-16 13:55:15 +0800960 dma_unmap_single(eth->dev, trxd.rxd1,
John Crispin656e7052016-03-08 11:29:55 +0100961 ring->buf_size, DMA_FROM_DEVICE);
962 pktlen = RX_DMA_GET_PLEN0(trxd.rxd2);
963 skb->dev = netdev;
964 skb_put(skb, pktlen);
965 if (trxd.rxd4 & RX_DMA_L4_VALID)
966 skb->ip_summed = CHECKSUM_UNNECESSARY;
967 else
968 skb_checksum_none_assert(skb);
969 skb->protocol = eth_type_trans(skb, netdev);
970
971 if (netdev->features & NETIF_F_HW_VLAN_CTAG_RX &&
972 RX_DMA_VID(trxd.rxd3))
973 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
974 RX_DMA_VID(trxd.rxd3));
975 napi_gro_receive(napi, skb);
976
977 ring->data[idx] = new_data;
978 rxd->rxd1 = (unsigned int)dma_addr;
979
980release_desc:
981 rxd->rxd2 = RX_DMA_PLEN0(ring->buf_size);
982
983 ring->calc_idx = idx;
Sean Wang635372a2016-09-03 17:59:26 +0800984
John Crispin656e7052016-03-08 11:29:55 +0100985 done++;
986 }
987
Nelson Changee406812016-09-17 23:50:55 +0800988rx_done:
Sean Wang41156ce2016-09-03 17:59:27 +0800989 if (done) {
990 /* make sure that all changes to the dma ring are flushed before
991 * we continue
992 */
993 wmb();
Nelson Changee406812016-09-17 23:50:55 +0800994 mtk_update_rx_cpu_idx(eth);
Sean Wang41156ce2016-09-03 17:59:27 +0800995 }
John Crispin656e7052016-03-08 11:29:55 +0100996
997 return done;
998}
999
John Crispin80673022016-06-29 13:38:11 +02001000static int mtk_poll_tx(struct mtk_eth *eth, int budget)
John Crispin656e7052016-03-08 11:29:55 +01001001{
1002 struct mtk_tx_ring *ring = &eth->tx_ring;
1003 struct mtk_tx_dma *desc;
1004 struct sk_buff *skb;
1005 struct mtk_tx_buf *tx_buf;
John Crispin80673022016-06-29 13:38:11 +02001006 unsigned int done[MTK_MAX_DEVS];
John Crispin656e7052016-03-08 11:29:55 +01001007 unsigned int bytes[MTK_MAX_DEVS];
1008 u32 cpu, dma;
1009 static int condition;
John Crispin80673022016-06-29 13:38:11 +02001010 int total = 0, i;
John Crispin656e7052016-03-08 11:29:55 +01001011
1012 memset(done, 0, sizeof(done));
1013 memset(bytes, 0, sizeof(bytes));
1014
1015 cpu = mtk_r32(eth, MTK_QTX_CRX_PTR);
1016 dma = mtk_r32(eth, MTK_QTX_DRX_PTR);
1017
1018 desc = mtk_qdma_phys_to_virt(ring, cpu);
1019
1020 while ((cpu != dma) && budget) {
1021 u32 next_cpu = desc->txd2;
Sean Wang134d2152017-04-14 11:19:12 +08001022 int mac = 0;
John Crispin656e7052016-03-08 11:29:55 +01001023
1024 desc = mtk_qdma_phys_to_virt(ring, desc->txd2);
1025 if ((desc->txd3 & TX_DMA_OWNER_CPU) == 0)
1026 break;
1027
John Crispin656e7052016-03-08 11:29:55 +01001028 tx_buf = mtk_desc_to_tx_buf(ring, desc);
Sean Wang134d2152017-04-14 11:19:12 +08001029 if (tx_buf->flags & MTK_TX_FLAGS_FPORT1)
1030 mac = 1;
1031
John Crispin656e7052016-03-08 11:29:55 +01001032 skb = tx_buf->skb;
1033 if (!skb) {
1034 condition = 1;
1035 break;
1036 }
1037
1038 if (skb != (struct sk_buff *)MTK_DMA_DUMMY_DESC) {
1039 bytes[mac] += skb->len;
1040 done[mac]++;
1041 budget--;
1042 }
sean.wang@mediatek.com55a4e772016-08-16 13:55:15 +08001043 mtk_tx_unmap(eth, tx_buf);
John Crispin656e7052016-03-08 11:29:55 +01001044
John Crispin656e7052016-03-08 11:29:55 +01001045 ring->last_free = desc;
1046 atomic_inc(&ring->free_count);
1047
1048 cpu = next_cpu;
1049 }
1050
1051 mtk_w32(eth, cpu, MTK_QTX_CRX_PTR);
1052
1053 for (i = 0; i < MTK_MAC_COUNT; i++) {
1054 if (!eth->netdev[i] || !done[i])
1055 continue;
1056 netdev_completed_queue(eth->netdev[i], done[i], bytes[i]);
1057 total += done[i];
1058 }
1059
John Crispinad3cba92016-06-10 13:28:07 +02001060 if (mtk_queue_stopped(eth) &&
1061 (atomic_read(&ring->free_count) > ring->thresh))
John Crispin13c822f2016-04-08 00:54:07 +02001062 mtk_wake_queue(eth);
John Crispin656e7052016-03-08 11:29:55 +01001063
1064 return total;
1065}
1066
John Crispin80673022016-06-29 13:38:11 +02001067static void mtk_handle_status_irq(struct mtk_eth *eth)
John Crispin656e7052016-03-08 11:29:55 +01001068{
John Crispin80673022016-06-29 13:38:11 +02001069 u32 status2 = mtk_r32(eth, MTK_INT_STATUS2);
John Crispin656e7052016-03-08 11:29:55 +01001070
John Crispineece71e2016-06-29 13:38:09 +02001071 if (unlikely(status2 & (MTK_GDM1_AF | MTK_GDM2_AF))) {
John Crispin656e7052016-03-08 11:29:55 +01001072 mtk_stats_update(eth);
John Crispineece71e2016-06-29 13:38:09 +02001073 mtk_w32(eth, (MTK_GDM1_AF | MTK_GDM2_AF),
1074 MTK_INT_STATUS2);
John Crispin656e7052016-03-08 11:29:55 +01001075 }
John Crispin80673022016-06-29 13:38:11 +02001076}
1077
1078static int mtk_napi_tx(struct napi_struct *napi, int budget)
1079{
1080 struct mtk_eth *eth = container_of(napi, struct mtk_eth, tx_napi);
1081 u32 status, mask;
1082 int tx_done = 0;
1083
1084 mtk_handle_status_irq(eth);
1085 mtk_w32(eth, MTK_TX_DONE_INT, MTK_QMTK_INT_STATUS);
1086 tx_done = mtk_poll_tx(eth, budget);
John Crispin656e7052016-03-08 11:29:55 +01001087
1088 if (unlikely(netif_msg_intr(eth))) {
John Crispin80673022016-06-29 13:38:11 +02001089 status = mtk_r32(eth, MTK_QMTK_INT_STATUS);
John Crispin656e7052016-03-08 11:29:55 +01001090 mask = mtk_r32(eth, MTK_QDMA_INT_MASK);
John Crispin80673022016-06-29 13:38:11 +02001091 dev_info(eth->dev,
1092 "done tx %d, intr 0x%08x/0x%x\n",
1093 tx_done, status, mask);
John Crispin656e7052016-03-08 11:29:55 +01001094 }
1095
John Crispin80673022016-06-29 13:38:11 +02001096 if (tx_done == budget)
John Crispin656e7052016-03-08 11:29:55 +01001097 return budget;
1098
1099 status = mtk_r32(eth, MTK_QMTK_INT_STATUS);
John Crispin80673022016-06-29 13:38:11 +02001100 if (status & MTK_TX_DONE_INT)
John Crispin656e7052016-03-08 11:29:55 +01001101 return budget;
1102
1103 napi_complete(napi);
Nelson Changbacfd112016-08-26 01:09:42 +08001104 mtk_irq_enable(eth, MTK_QDMA_INT_MASK, MTK_TX_DONE_INT);
John Crispin80673022016-06-29 13:38:11 +02001105
1106 return tx_done;
1107}
1108
1109static int mtk_napi_rx(struct napi_struct *napi, int budget)
1110{
1111 struct mtk_eth *eth = container_of(napi, struct mtk_eth, rx_napi);
1112 u32 status, mask;
1113 int rx_done = 0;
Sean Wang41156ce2016-09-03 17:59:27 +08001114 int remain_budget = budget;
John Crispin80673022016-06-29 13:38:11 +02001115
1116 mtk_handle_status_irq(eth);
Sean Wang41156ce2016-09-03 17:59:27 +08001117
1118poll_again:
Nelson Changbacfd112016-08-26 01:09:42 +08001119 mtk_w32(eth, MTK_RX_DONE_INT, MTK_PDMA_INT_STATUS);
Sean Wang41156ce2016-09-03 17:59:27 +08001120 rx_done = mtk_poll_rx(napi, remain_budget, eth);
John Crispin80673022016-06-29 13:38:11 +02001121
1122 if (unlikely(netif_msg_intr(eth))) {
Nelson Changbacfd112016-08-26 01:09:42 +08001123 status = mtk_r32(eth, MTK_PDMA_INT_STATUS);
1124 mask = mtk_r32(eth, MTK_PDMA_INT_MASK);
John Crispin80673022016-06-29 13:38:11 +02001125 dev_info(eth->dev,
1126 "done rx %d, intr 0x%08x/0x%x\n",
1127 rx_done, status, mask);
1128 }
Sean Wang41156ce2016-09-03 17:59:27 +08001129 if (rx_done == remain_budget)
John Crispin80673022016-06-29 13:38:11 +02001130 return budget;
1131
Nelson Changbacfd112016-08-26 01:09:42 +08001132 status = mtk_r32(eth, MTK_PDMA_INT_STATUS);
Sean Wang41156ce2016-09-03 17:59:27 +08001133 if (status & MTK_RX_DONE_INT) {
1134 remain_budget -= rx_done;
1135 goto poll_again;
1136 }
John Crispin80673022016-06-29 13:38:11 +02001137 napi_complete(napi);
Nelson Changbacfd112016-08-26 01:09:42 +08001138 mtk_irq_enable(eth, MTK_PDMA_INT_MASK, MTK_RX_DONE_INT);
John Crispin656e7052016-03-08 11:29:55 +01001139
Sean Wang41156ce2016-09-03 17:59:27 +08001140 return rx_done + budget - remain_budget;
John Crispin656e7052016-03-08 11:29:55 +01001141}
1142
1143static int mtk_tx_alloc(struct mtk_eth *eth)
1144{
1145 struct mtk_tx_ring *ring = &eth->tx_ring;
1146 int i, sz = sizeof(*ring->dma);
1147
1148 ring->buf = kcalloc(MTK_DMA_SIZE, sizeof(*ring->buf),
1149 GFP_KERNEL);
1150 if (!ring->buf)
1151 goto no_tx_mem;
1152
1153 ring->dma = dma_alloc_coherent(eth->dev,
1154 MTK_DMA_SIZE * sz,
1155 &ring->phys,
1156 GFP_ATOMIC | __GFP_ZERO);
1157 if (!ring->dma)
1158 goto no_tx_mem;
1159
1160 memset(ring->dma, 0, MTK_DMA_SIZE * sz);
1161 for (i = 0; i < MTK_DMA_SIZE; i++) {
1162 int next = (i + 1) % MTK_DMA_SIZE;
1163 u32 next_ptr = ring->phys + next * sz;
1164
1165 ring->dma[i].txd2 = next_ptr;
1166 ring->dma[i].txd3 = TX_DMA_LS0 | TX_DMA_OWNER_CPU;
1167 }
1168
1169 atomic_set(&ring->free_count, MTK_DMA_SIZE - 2);
1170 ring->next_free = &ring->dma[0];
John Crispin12c97c12016-06-10 13:28:06 +02001171 ring->last_free = &ring->dma[MTK_DMA_SIZE - 1];
John Crispin04698cc2016-06-10 13:28:04 +02001172 ring->thresh = MAX_SKB_FRAGS;
John Crispin656e7052016-03-08 11:29:55 +01001173
1174 /* make sure that all changes to the dma ring are flushed before we
1175 * continue
1176 */
1177 wmb();
1178
1179 mtk_w32(eth, ring->phys, MTK_QTX_CTX_PTR);
1180 mtk_w32(eth, ring->phys, MTK_QTX_DTX_PTR);
1181 mtk_w32(eth,
1182 ring->phys + ((MTK_DMA_SIZE - 1) * sz),
1183 MTK_QTX_CRX_PTR);
1184 mtk_w32(eth,
1185 ring->phys + ((MTK_DMA_SIZE - 1) * sz),
1186 MTK_QTX_DRX_PTR);
Nelson Changbacfd112016-08-26 01:09:42 +08001187 mtk_w32(eth, (QDMA_RES_THRES << 8) | QDMA_RES_THRES, MTK_QTX_CFG(0));
John Crispin656e7052016-03-08 11:29:55 +01001188
1189 return 0;
1190
1191no_tx_mem:
1192 return -ENOMEM;
1193}
1194
1195static void mtk_tx_clean(struct mtk_eth *eth)
1196{
1197 struct mtk_tx_ring *ring = &eth->tx_ring;
1198 int i;
1199
1200 if (ring->buf) {
1201 for (i = 0; i < MTK_DMA_SIZE; i++)
sean.wang@mediatek.com55a4e772016-08-16 13:55:15 +08001202 mtk_tx_unmap(eth, &ring->buf[i]);
John Crispin656e7052016-03-08 11:29:55 +01001203 kfree(ring->buf);
1204 ring->buf = NULL;
1205 }
1206
1207 if (ring->dma) {
1208 dma_free_coherent(eth->dev,
1209 MTK_DMA_SIZE * sizeof(*ring->dma),
1210 ring->dma,
1211 ring->phys);
1212 ring->dma = NULL;
1213 }
1214}
1215
Nelson Changee406812016-09-17 23:50:55 +08001216static int mtk_rx_alloc(struct mtk_eth *eth, int ring_no, int rx_flag)
John Crispin656e7052016-03-08 11:29:55 +01001217{
Nelson Changee406812016-09-17 23:50:55 +08001218 struct mtk_rx_ring *ring = &eth->rx_ring[ring_no];
1219 int rx_data_len, rx_dma_size;
John Crispin656e7052016-03-08 11:29:55 +01001220 int i;
1221
Nelson Changee406812016-09-17 23:50:55 +08001222 if (rx_flag == MTK_RX_FLAGS_HWLRO) {
1223 rx_data_len = MTK_MAX_LRO_RX_LENGTH;
1224 rx_dma_size = MTK_HW_LRO_DMA_SIZE;
1225 } else {
1226 rx_data_len = ETH_DATA_LEN;
1227 rx_dma_size = MTK_DMA_SIZE;
1228 }
1229
1230 ring->frag_size = mtk_max_frag_size(rx_data_len);
John Crispin656e7052016-03-08 11:29:55 +01001231 ring->buf_size = mtk_max_buf_size(ring->frag_size);
Nelson Changee406812016-09-17 23:50:55 +08001232 ring->data = kcalloc(rx_dma_size, sizeof(*ring->data),
John Crispin656e7052016-03-08 11:29:55 +01001233 GFP_KERNEL);
1234 if (!ring->data)
1235 return -ENOMEM;
1236
Nelson Changee406812016-09-17 23:50:55 +08001237 for (i = 0; i < rx_dma_size; i++) {
John Crispin656e7052016-03-08 11:29:55 +01001238 ring->data[i] = netdev_alloc_frag(ring->frag_size);
1239 if (!ring->data[i])
1240 return -ENOMEM;
1241 }
1242
1243 ring->dma = dma_alloc_coherent(eth->dev,
Nelson Changee406812016-09-17 23:50:55 +08001244 rx_dma_size * sizeof(*ring->dma),
John Crispin656e7052016-03-08 11:29:55 +01001245 &ring->phys,
1246 GFP_ATOMIC | __GFP_ZERO);
1247 if (!ring->dma)
1248 return -ENOMEM;
1249
Nelson Changee406812016-09-17 23:50:55 +08001250 for (i = 0; i < rx_dma_size; i++) {
John Crispin656e7052016-03-08 11:29:55 +01001251 dma_addr_t dma_addr = dma_map_single(eth->dev,
1252 ring->data[i] + NET_SKB_PAD,
1253 ring->buf_size,
1254 DMA_FROM_DEVICE);
1255 if (unlikely(dma_mapping_error(eth->dev, dma_addr)))
1256 return -ENOMEM;
1257 ring->dma[i].rxd1 = (unsigned int)dma_addr;
1258
1259 ring->dma[i].rxd2 = RX_DMA_PLEN0(ring->buf_size);
1260 }
Nelson Changee406812016-09-17 23:50:55 +08001261 ring->dma_size = rx_dma_size;
1262 ring->calc_idx_update = false;
1263 ring->calc_idx = rx_dma_size - 1;
1264 ring->crx_idx_reg = MTK_PRX_CRX_IDX_CFG(ring_no);
John Crispin656e7052016-03-08 11:29:55 +01001265 /* make sure that all changes to the dma ring are flushed before we
1266 * continue
1267 */
1268 wmb();
1269
Nelson Changee406812016-09-17 23:50:55 +08001270 mtk_w32(eth, ring->phys, MTK_PRX_BASE_PTR_CFG(ring_no));
1271 mtk_w32(eth, rx_dma_size, MTK_PRX_MAX_CNT_CFG(ring_no));
1272 mtk_w32(eth, ring->calc_idx, ring->crx_idx_reg);
1273 mtk_w32(eth, MTK_PST_DRX_IDX_CFG(ring_no), MTK_PDMA_RST_IDX);
John Crispin656e7052016-03-08 11:29:55 +01001274
1275 return 0;
1276}
1277
Nelson Changee406812016-09-17 23:50:55 +08001278static void mtk_rx_clean(struct mtk_eth *eth, int ring_no)
John Crispin656e7052016-03-08 11:29:55 +01001279{
Nelson Changee406812016-09-17 23:50:55 +08001280 struct mtk_rx_ring *ring = &eth->rx_ring[ring_no];
John Crispin656e7052016-03-08 11:29:55 +01001281 int i;
1282
1283 if (ring->data && ring->dma) {
Nelson Changee406812016-09-17 23:50:55 +08001284 for (i = 0; i < ring->dma_size; i++) {
John Crispin656e7052016-03-08 11:29:55 +01001285 if (!ring->data[i])
1286 continue;
1287 if (!ring->dma[i].rxd1)
1288 continue;
1289 dma_unmap_single(eth->dev,
1290 ring->dma[i].rxd1,
1291 ring->buf_size,
1292 DMA_FROM_DEVICE);
1293 skb_free_frag(ring->data[i]);
1294 }
1295 kfree(ring->data);
1296 ring->data = NULL;
1297 }
1298
1299 if (ring->dma) {
1300 dma_free_coherent(eth->dev,
Nelson Changee406812016-09-17 23:50:55 +08001301 ring->dma_size * sizeof(*ring->dma),
John Crispin656e7052016-03-08 11:29:55 +01001302 ring->dma,
1303 ring->phys);
1304 ring->dma = NULL;
1305 }
1306}
1307
Nelson Changee406812016-09-17 23:50:55 +08001308static int mtk_hwlro_rx_init(struct mtk_eth *eth)
1309{
1310 int i;
1311 u32 ring_ctrl_dw1 = 0, ring_ctrl_dw2 = 0, ring_ctrl_dw3 = 0;
1312 u32 lro_ctrl_dw0 = 0, lro_ctrl_dw3 = 0;
1313
1314 /* set LRO rings to auto-learn modes */
1315 ring_ctrl_dw2 |= MTK_RING_AUTO_LERAN_MODE;
1316
1317 /* validate LRO ring */
1318 ring_ctrl_dw2 |= MTK_RING_VLD;
1319
1320 /* set AGE timer (unit: 20us) */
1321 ring_ctrl_dw2 |= MTK_RING_AGE_TIME_H;
1322 ring_ctrl_dw1 |= MTK_RING_AGE_TIME_L;
1323
1324 /* set max AGG timer (unit: 20us) */
1325 ring_ctrl_dw2 |= MTK_RING_MAX_AGG_TIME;
1326
1327 /* set max LRO AGG count */
1328 ring_ctrl_dw2 |= MTK_RING_MAX_AGG_CNT_L;
1329 ring_ctrl_dw3 |= MTK_RING_MAX_AGG_CNT_H;
1330
1331 for (i = 1; i < MTK_MAX_RX_RING_NUM; i++) {
1332 mtk_w32(eth, ring_ctrl_dw1, MTK_LRO_CTRL_DW1_CFG(i));
1333 mtk_w32(eth, ring_ctrl_dw2, MTK_LRO_CTRL_DW2_CFG(i));
1334 mtk_w32(eth, ring_ctrl_dw3, MTK_LRO_CTRL_DW3_CFG(i));
1335 }
1336
1337 /* IPv4 checksum update enable */
1338 lro_ctrl_dw0 |= MTK_L3_CKS_UPD_EN;
1339
1340 /* switch priority comparison to packet count mode */
1341 lro_ctrl_dw0 |= MTK_LRO_ALT_PKT_CNT_MODE;
1342
1343 /* bandwidth threshold setting */
1344 mtk_w32(eth, MTK_HW_LRO_BW_THRE, MTK_PDMA_LRO_CTRL_DW2);
1345
1346 /* auto-learn score delta setting */
1347 mtk_w32(eth, MTK_HW_LRO_REPLACE_DELTA, MTK_PDMA_LRO_ALT_SCORE_DELTA);
1348
1349 /* set refresh timer for altering flows to 1 sec. (unit: 20us) */
1350 mtk_w32(eth, (MTK_HW_LRO_TIMER_UNIT << 16) | MTK_HW_LRO_REFRESH_TIME,
1351 MTK_PDMA_LRO_ALT_REFRESH_TIMER);
1352
1353 /* set HW LRO mode & the max aggregation count for rx packets */
1354 lro_ctrl_dw3 |= MTK_ADMA_MODE | (MTK_HW_LRO_MAX_AGG_CNT & 0xff);
1355
1356 /* the minimal remaining room of SDL0 in RXD for lro aggregation */
1357 lro_ctrl_dw3 |= MTK_LRO_MIN_RXD_SDL;
1358
1359 /* enable HW LRO */
1360 lro_ctrl_dw0 |= MTK_LRO_EN;
1361
1362 mtk_w32(eth, lro_ctrl_dw3, MTK_PDMA_LRO_CTRL_DW3);
1363 mtk_w32(eth, lro_ctrl_dw0, MTK_PDMA_LRO_CTRL_DW0);
1364
1365 return 0;
1366}
1367
1368static void mtk_hwlro_rx_uninit(struct mtk_eth *eth)
1369{
1370 int i;
1371 u32 val;
1372
1373 /* relinquish lro rings, flush aggregated packets */
1374 mtk_w32(eth, MTK_LRO_RING_RELINQUISH_REQ, MTK_PDMA_LRO_CTRL_DW0);
1375
1376 /* wait for relinquishments done */
1377 for (i = 0; i < 10; i++) {
1378 val = mtk_r32(eth, MTK_PDMA_LRO_CTRL_DW0);
1379 if (val & MTK_LRO_RING_RELINQUISH_DONE) {
1380 msleep(20);
1381 continue;
1382 }
Nelson Changca3ba102016-09-26 14:33:50 +08001383 break;
Nelson Changee406812016-09-17 23:50:55 +08001384 }
1385
1386 /* invalidate lro rings */
1387 for (i = 1; i < MTK_MAX_RX_RING_NUM; i++)
1388 mtk_w32(eth, 0, MTK_LRO_CTRL_DW2_CFG(i));
1389
1390 /* disable HW LRO */
1391 mtk_w32(eth, 0, MTK_PDMA_LRO_CTRL_DW0);
1392}
1393
Nelson Chang7aab7472016-09-17 23:50:56 +08001394static void mtk_hwlro_val_ipaddr(struct mtk_eth *eth, int idx, __be32 ip)
1395{
1396 u32 reg_val;
1397
1398 reg_val = mtk_r32(eth, MTK_LRO_CTRL_DW2_CFG(idx));
1399
1400 /* invalidate the IP setting */
1401 mtk_w32(eth, (reg_val & ~MTK_RING_MYIP_VLD), MTK_LRO_CTRL_DW2_CFG(idx));
1402
1403 mtk_w32(eth, ip, MTK_LRO_DIP_DW0_CFG(idx));
1404
1405 /* validate the IP setting */
1406 mtk_w32(eth, (reg_val | MTK_RING_MYIP_VLD), MTK_LRO_CTRL_DW2_CFG(idx));
1407}
1408
1409static void mtk_hwlro_inval_ipaddr(struct mtk_eth *eth, int idx)
1410{
1411 u32 reg_val;
1412
1413 reg_val = mtk_r32(eth, MTK_LRO_CTRL_DW2_CFG(idx));
1414
1415 /* invalidate the IP setting */
1416 mtk_w32(eth, (reg_val & ~MTK_RING_MYIP_VLD), MTK_LRO_CTRL_DW2_CFG(idx));
1417
1418 mtk_w32(eth, 0, MTK_LRO_DIP_DW0_CFG(idx));
1419}
1420
1421static int mtk_hwlro_get_ip_cnt(struct mtk_mac *mac)
1422{
1423 int cnt = 0;
1424 int i;
1425
1426 for (i = 0; i < MTK_MAX_LRO_IP_CNT; i++) {
1427 if (mac->hwlro_ip[i])
1428 cnt++;
1429 }
1430
1431 return cnt;
1432}
1433
1434static int mtk_hwlro_add_ipaddr(struct net_device *dev,
1435 struct ethtool_rxnfc *cmd)
1436{
1437 struct ethtool_rx_flow_spec *fsp =
1438 (struct ethtool_rx_flow_spec *)&cmd->fs;
1439 struct mtk_mac *mac = netdev_priv(dev);
1440 struct mtk_eth *eth = mac->hw;
1441 int hwlro_idx;
1442
1443 if ((fsp->flow_type != TCP_V4_FLOW) ||
1444 (!fsp->h_u.tcp_ip4_spec.ip4dst) ||
1445 (fsp->location > 1))
1446 return -EINVAL;
1447
1448 mac->hwlro_ip[fsp->location] = htonl(fsp->h_u.tcp_ip4_spec.ip4dst);
1449 hwlro_idx = (mac->id * MTK_MAX_LRO_IP_CNT) + fsp->location;
1450
1451 mac->hwlro_ip_cnt = mtk_hwlro_get_ip_cnt(mac);
1452
1453 mtk_hwlro_val_ipaddr(eth, hwlro_idx, mac->hwlro_ip[fsp->location]);
1454
1455 return 0;
1456}
1457
1458static int mtk_hwlro_del_ipaddr(struct net_device *dev,
1459 struct ethtool_rxnfc *cmd)
1460{
1461 struct ethtool_rx_flow_spec *fsp =
1462 (struct ethtool_rx_flow_spec *)&cmd->fs;
1463 struct mtk_mac *mac = netdev_priv(dev);
1464 struct mtk_eth *eth = mac->hw;
1465 int hwlro_idx;
1466
1467 if (fsp->location > 1)
1468 return -EINVAL;
1469
1470 mac->hwlro_ip[fsp->location] = 0;
1471 hwlro_idx = (mac->id * MTK_MAX_LRO_IP_CNT) + fsp->location;
1472
1473 mac->hwlro_ip_cnt = mtk_hwlro_get_ip_cnt(mac);
1474
1475 mtk_hwlro_inval_ipaddr(eth, hwlro_idx);
1476
1477 return 0;
1478}
1479
1480static void mtk_hwlro_netdev_disable(struct net_device *dev)
1481{
1482 struct mtk_mac *mac = netdev_priv(dev);
1483 struct mtk_eth *eth = mac->hw;
1484 int i, hwlro_idx;
1485
1486 for (i = 0; i < MTK_MAX_LRO_IP_CNT; i++) {
1487 mac->hwlro_ip[i] = 0;
1488 hwlro_idx = (mac->id * MTK_MAX_LRO_IP_CNT) + i;
1489
1490 mtk_hwlro_inval_ipaddr(eth, hwlro_idx);
1491 }
1492
1493 mac->hwlro_ip_cnt = 0;
1494}
1495
1496static int mtk_hwlro_get_fdir_entry(struct net_device *dev,
1497 struct ethtool_rxnfc *cmd)
1498{
1499 struct mtk_mac *mac = netdev_priv(dev);
1500 struct ethtool_rx_flow_spec *fsp =
1501 (struct ethtool_rx_flow_spec *)&cmd->fs;
1502
1503 /* only tcp dst ipv4 is meaningful, others are meaningless */
1504 fsp->flow_type = TCP_V4_FLOW;
1505 fsp->h_u.tcp_ip4_spec.ip4dst = ntohl(mac->hwlro_ip[fsp->location]);
1506 fsp->m_u.tcp_ip4_spec.ip4dst = 0;
1507
1508 fsp->h_u.tcp_ip4_spec.ip4src = 0;
1509 fsp->m_u.tcp_ip4_spec.ip4src = 0xffffffff;
1510 fsp->h_u.tcp_ip4_spec.psrc = 0;
1511 fsp->m_u.tcp_ip4_spec.psrc = 0xffff;
1512 fsp->h_u.tcp_ip4_spec.pdst = 0;
1513 fsp->m_u.tcp_ip4_spec.pdst = 0xffff;
1514 fsp->h_u.tcp_ip4_spec.tos = 0;
1515 fsp->m_u.tcp_ip4_spec.tos = 0xff;
1516
1517 return 0;
1518}
1519
1520static int mtk_hwlro_get_fdir_all(struct net_device *dev,
1521 struct ethtool_rxnfc *cmd,
1522 u32 *rule_locs)
1523{
1524 struct mtk_mac *mac = netdev_priv(dev);
1525 int cnt = 0;
1526 int i;
1527
1528 for (i = 0; i < MTK_MAX_LRO_IP_CNT; i++) {
1529 if (mac->hwlro_ip[i]) {
1530 rule_locs[cnt] = i;
1531 cnt++;
1532 }
1533 }
1534
1535 cmd->rule_cnt = cnt;
1536
1537 return 0;
1538}
1539
1540static netdev_features_t mtk_fix_features(struct net_device *dev,
1541 netdev_features_t features)
1542{
1543 if (!(features & NETIF_F_LRO)) {
1544 struct mtk_mac *mac = netdev_priv(dev);
1545 int ip_cnt = mtk_hwlro_get_ip_cnt(mac);
1546
1547 if (ip_cnt) {
1548 netdev_info(dev, "RX flow is programmed, LRO should keep on\n");
1549
1550 features |= NETIF_F_LRO;
1551 }
1552 }
1553
1554 return features;
1555}
1556
1557static int mtk_set_features(struct net_device *dev, netdev_features_t features)
1558{
1559 int err = 0;
1560
1561 if (!((dev->features ^ features) & NETIF_F_LRO))
1562 return 0;
1563
1564 if (!(features & NETIF_F_LRO))
1565 mtk_hwlro_netdev_disable(dev);
1566
1567 return err;
1568}
1569
John Crispin656e7052016-03-08 11:29:55 +01001570/* wait for DMA to finish whatever it is doing before we start using it again */
1571static int mtk_dma_busy_wait(struct mtk_eth *eth)
1572{
1573 unsigned long t_start = jiffies;
1574
1575 while (1) {
1576 if (!(mtk_r32(eth, MTK_QDMA_GLO_CFG) &
1577 (MTK_RX_DMA_BUSY | MTK_TX_DMA_BUSY)))
1578 return 0;
1579 if (time_after(jiffies, t_start + MTK_DMA_BUSY_TIMEOUT))
1580 break;
1581 }
1582
1583 dev_err(eth->dev, "DMA init timeout\n");
1584 return -1;
1585}
1586
1587static int mtk_dma_init(struct mtk_eth *eth)
1588{
1589 int err;
Nelson Changee406812016-09-17 23:50:55 +08001590 u32 i;
John Crispin656e7052016-03-08 11:29:55 +01001591
1592 if (mtk_dma_busy_wait(eth))
1593 return -EBUSY;
1594
1595 /* QDMA needs scratch memory for internal reordering of the
1596 * descriptors
1597 */
1598 err = mtk_init_fq_dma(eth);
1599 if (err)
1600 return err;
1601
1602 err = mtk_tx_alloc(eth);
1603 if (err)
1604 return err;
1605
Nelson Changee406812016-09-17 23:50:55 +08001606 err = mtk_rx_alloc(eth, 0, MTK_RX_FLAGS_NORMAL);
John Crispin656e7052016-03-08 11:29:55 +01001607 if (err)
1608 return err;
1609
Nelson Changee406812016-09-17 23:50:55 +08001610 if (eth->hwlro) {
1611 for (i = 1; i < MTK_MAX_RX_RING_NUM; i++) {
1612 err = mtk_rx_alloc(eth, i, MTK_RX_FLAGS_HWLRO);
1613 if (err)
1614 return err;
1615 }
1616 err = mtk_hwlro_rx_init(eth);
1617 if (err)
1618 return err;
1619 }
1620
John Crispin656e7052016-03-08 11:29:55 +01001621 /* Enable random early drop and set drop threshold automatically */
1622 mtk_w32(eth, FC_THRES_DROP_MODE | FC_THRES_DROP_EN | FC_THRES_MIN,
1623 MTK_QDMA_FC_THRES);
1624 mtk_w32(eth, 0x0, MTK_QDMA_HRED2);
1625
1626 return 0;
1627}
1628
1629static void mtk_dma_free(struct mtk_eth *eth)
1630{
1631 int i;
1632
1633 for (i = 0; i < MTK_MAC_COUNT; i++)
1634 if (eth->netdev[i])
1635 netdev_reset_queue(eth->netdev[i]);
John Crispin605e4fe2016-06-10 13:27:59 +02001636 if (eth->scratch_ring) {
1637 dma_free_coherent(eth->dev,
1638 MTK_DMA_SIZE * sizeof(struct mtk_tx_dma),
1639 eth->scratch_ring,
1640 eth->phy_scratch_ring);
1641 eth->scratch_ring = NULL;
1642 eth->phy_scratch_ring = 0;
1643 }
John Crispin656e7052016-03-08 11:29:55 +01001644 mtk_tx_clean(eth);
Nelson Changee406812016-09-17 23:50:55 +08001645 mtk_rx_clean(eth, 0);
1646
1647 if (eth->hwlro) {
1648 mtk_hwlro_rx_uninit(eth);
1649 for (i = 1; i < MTK_MAX_RX_RING_NUM; i++)
1650 mtk_rx_clean(eth, i);
1651 }
1652
John Crispin656e7052016-03-08 11:29:55 +01001653 kfree(eth->scratch_head);
1654}
1655
1656static void mtk_tx_timeout(struct net_device *dev)
1657{
1658 struct mtk_mac *mac = netdev_priv(dev);
1659 struct mtk_eth *eth = mac->hw;
1660
1661 eth->netdev[mac->id]->stats.tx_errors++;
1662 netif_err(eth, tx_err, dev,
1663 "transmit timed out\n");
John Crispin7c78b4a2016-04-08 00:54:10 +02001664 schedule_work(&eth->pending_work);
John Crispin656e7052016-03-08 11:29:55 +01001665}
1666
John Crispin80673022016-06-29 13:38:11 +02001667static irqreturn_t mtk_handle_irq_rx(int irq, void *_eth)
John Crispin656e7052016-03-08 11:29:55 +01001668{
1669 struct mtk_eth *eth = _eth;
John Crispin656e7052016-03-08 11:29:55 +01001670
John Crispin80673022016-06-29 13:38:11 +02001671 if (likely(napi_schedule_prep(&eth->rx_napi))) {
1672 __napi_schedule(&eth->rx_napi);
Nelson Changbacfd112016-08-26 01:09:42 +08001673 mtk_irq_disable(eth, MTK_PDMA_INT_MASK, MTK_RX_DONE_INT);
John Crispin656e7052016-03-08 11:29:55 +01001674 }
John Crispin80673022016-06-29 13:38:11 +02001675
1676 return IRQ_HANDLED;
1677}
1678
1679static irqreturn_t mtk_handle_irq_tx(int irq, void *_eth)
1680{
1681 struct mtk_eth *eth = _eth;
1682
1683 if (likely(napi_schedule_prep(&eth->tx_napi))) {
1684 __napi_schedule(&eth->tx_napi);
Nelson Changbacfd112016-08-26 01:09:42 +08001685 mtk_irq_disable(eth, MTK_QDMA_INT_MASK, MTK_TX_DONE_INT);
John Crispin80673022016-06-29 13:38:11 +02001686 }
John Crispin656e7052016-03-08 11:29:55 +01001687
1688 return IRQ_HANDLED;
1689}
1690
1691#ifdef CONFIG_NET_POLL_CONTROLLER
1692static void mtk_poll_controller(struct net_device *dev)
1693{
1694 struct mtk_mac *mac = netdev_priv(dev);
1695 struct mtk_eth *eth = mac->hw;
John Crispin656e7052016-03-08 11:29:55 +01001696
Nelson Changbacfd112016-08-26 01:09:42 +08001697 mtk_irq_disable(eth, MTK_QDMA_INT_MASK, MTK_TX_DONE_INT);
1698 mtk_irq_disable(eth, MTK_PDMA_INT_MASK, MTK_RX_DONE_INT);
John Crispin8186f6e2016-07-02 08:00:50 +02001699 mtk_handle_irq_rx(eth->irq[2], dev);
Nelson Changbacfd112016-08-26 01:09:42 +08001700 mtk_irq_enable(eth, MTK_QDMA_INT_MASK, MTK_TX_DONE_INT);
1701 mtk_irq_enable(eth, MTK_PDMA_INT_MASK, MTK_RX_DONE_INT);
John Crispin656e7052016-03-08 11:29:55 +01001702}
1703#endif
1704
1705static int mtk_start_dma(struct mtk_eth *eth)
1706{
1707 int err;
1708
1709 err = mtk_dma_init(eth);
1710 if (err) {
1711 mtk_dma_free(eth);
1712 return err;
1713 }
1714
1715 mtk_w32(eth,
Nelson Changbacfd112016-08-26 01:09:42 +08001716 MTK_TX_WB_DDONE | MTK_TX_DMA_EN |
1717 MTK_DMA_SIZE_16DWORDS | MTK_NDP_CO_PRO,
John Crispin656e7052016-03-08 11:29:55 +01001718 MTK_QDMA_GLO_CFG);
1719
Nelson Changbacfd112016-08-26 01:09:42 +08001720 mtk_w32(eth,
1721 MTK_RX_DMA_EN | MTK_RX_2B_OFFSET |
1722 MTK_RX_BT_32DWORDS | MTK_MULTI_EN,
1723 MTK_PDMA_GLO_CFG);
1724
John Crispin656e7052016-03-08 11:29:55 +01001725 return 0;
1726}
1727
1728static int mtk_open(struct net_device *dev)
1729{
1730 struct mtk_mac *mac = netdev_priv(dev);
1731 struct mtk_eth *eth = mac->hw;
1732
1733 /* we run 2 netdevs on the same dma ring so we only bring it up once */
1734 if (!atomic_read(&eth->dma_refcnt)) {
1735 int err = mtk_start_dma(eth);
1736
1737 if (err)
1738 return err;
1739
John Crispin80673022016-06-29 13:38:11 +02001740 napi_enable(&eth->tx_napi);
John Crispin656e7052016-03-08 11:29:55 +01001741 napi_enable(&eth->rx_napi);
Nelson Changbacfd112016-08-26 01:09:42 +08001742 mtk_irq_enable(eth, MTK_QDMA_INT_MASK, MTK_TX_DONE_INT);
1743 mtk_irq_enable(eth, MTK_PDMA_INT_MASK, MTK_RX_DONE_INT);
John Crispin656e7052016-03-08 11:29:55 +01001744 }
1745 atomic_inc(&eth->dma_refcnt);
1746
Sean Wang2364c5c2016-09-22 16:33:35 +08001747 phy_start(dev->phydev);
John Crispin656e7052016-03-08 11:29:55 +01001748 netif_start_queue(dev);
1749
1750 return 0;
1751}
1752
1753static void mtk_stop_dma(struct mtk_eth *eth, u32 glo_cfg)
1754{
John Crispin656e7052016-03-08 11:29:55 +01001755 u32 val;
1756 int i;
1757
1758 /* stop the dma engine */
Sean Wange3e96522016-08-11 17:51:00 +08001759 spin_lock_bh(&eth->page_lock);
John Crispin656e7052016-03-08 11:29:55 +01001760 val = mtk_r32(eth, glo_cfg);
1761 mtk_w32(eth, val & ~(MTK_TX_WB_DDONE | MTK_RX_DMA_EN | MTK_TX_DMA_EN),
1762 glo_cfg);
Sean Wange3e96522016-08-11 17:51:00 +08001763 spin_unlock_bh(&eth->page_lock);
John Crispin656e7052016-03-08 11:29:55 +01001764
1765 /* wait for dma stop */
1766 for (i = 0; i < 10; i++) {
1767 val = mtk_r32(eth, glo_cfg);
1768 if (val & (MTK_TX_DMA_BUSY | MTK_RX_DMA_BUSY)) {
1769 msleep(20);
1770 continue;
1771 }
1772 break;
1773 }
1774}
1775
1776static int mtk_stop(struct net_device *dev)
1777{
1778 struct mtk_mac *mac = netdev_priv(dev);
1779 struct mtk_eth *eth = mac->hw;
1780
1781 netif_tx_disable(dev);
Sean Wang2364c5c2016-09-22 16:33:35 +08001782 phy_stop(dev->phydev);
John Crispin656e7052016-03-08 11:29:55 +01001783
1784 /* only shutdown DMA if this is the last user */
1785 if (!atomic_dec_and_test(&eth->dma_refcnt))
1786 return 0;
1787
Nelson Changbacfd112016-08-26 01:09:42 +08001788 mtk_irq_disable(eth, MTK_QDMA_INT_MASK, MTK_TX_DONE_INT);
1789 mtk_irq_disable(eth, MTK_PDMA_INT_MASK, MTK_RX_DONE_INT);
John Crispin80673022016-06-29 13:38:11 +02001790 napi_disable(&eth->tx_napi);
John Crispin656e7052016-03-08 11:29:55 +01001791 napi_disable(&eth->rx_napi);
1792
1793 mtk_stop_dma(eth, MTK_QDMA_GLO_CFG);
Nelson Chang6bf563d2016-09-26 14:33:49 +08001794 mtk_stop_dma(eth, MTK_PDMA_GLO_CFG);
John Crispin656e7052016-03-08 11:29:55 +01001795
1796 mtk_dma_free(eth);
1797
1798 return 0;
1799}
1800
Sean Wang2a8307a2016-09-14 23:13:20 +08001801static void ethsys_reset(struct mtk_eth *eth, u32 reset_bits)
1802{
1803 regmap_update_bits(eth->ethsys, ETHSYS_RSTCTRL,
1804 reset_bits,
1805 reset_bits);
1806
1807 usleep_range(1000, 1100);
1808 regmap_update_bits(eth->ethsys, ETHSYS_RSTCTRL,
1809 reset_bits,
1810 ~reset_bits);
1811 mdelay(10);
1812}
1813
Sean Wang9ea4d312016-09-14 23:13:19 +08001814static int mtk_hw_init(struct mtk_eth *eth)
John Crispin656e7052016-03-08 11:29:55 +01001815{
Sean Wang9ea4d312016-09-14 23:13:19 +08001816 int i, val;
1817
1818 if (test_and_set_bit(MTK_HW_INIT, &eth->state))
1819 return 0;
Sean Wang85574db2016-09-14 23:13:15 +08001820
Sean Wang26a2ad82016-09-14 23:13:18 +08001821 pm_runtime_enable(eth->dev);
1822 pm_runtime_get_sync(eth->dev);
1823
Sean Wang85574db2016-09-14 23:13:15 +08001824 clk_prepare_enable(eth->clks[MTK_CLK_ETHIF]);
1825 clk_prepare_enable(eth->clks[MTK_CLK_ESW]);
1826 clk_prepare_enable(eth->clks[MTK_CLK_GP1]);
1827 clk_prepare_enable(eth->clks[MTK_CLK_GP2]);
Sean Wang2a8307a2016-09-14 23:13:20 +08001828 ethsys_reset(eth, RSTCTRL_FE);
1829 ethsys_reset(eth, RSTCTRL_PPE);
John Crispin656e7052016-03-08 11:29:55 +01001830
Sean Wang9ea4d312016-09-14 23:13:19 +08001831 regmap_read(eth->ethsys, ETHSYS_SYSCFG0, &val);
1832 for (i = 0; i < MTK_MAC_COUNT; i++) {
1833 if (!eth->mac[i])
1834 continue;
1835 val &= ~SYSCFG0_GE_MODE(SYSCFG0_GE_MASK, eth->mac[i]->id);
1836 val |= SYSCFG0_GE_MODE(eth->mac[i]->ge_mode, eth->mac[i]->id);
1837 }
1838 regmap_write(eth->ethsys, ETHSYS_SYSCFG0, val);
1839
John Crispin656e7052016-03-08 11:29:55 +01001840 /* Set GE2 driving and slew rate */
1841 regmap_write(eth->pctl, GPIO_DRV_SEL10, 0xa00);
1842
1843 /* set GE2 TDSEL */
1844 regmap_write(eth->pctl, GPIO_OD33_CTRL8, 0x5);
1845
1846 /* set GE2 TUNE */
1847 regmap_write(eth->pctl, GPIO_BIAS_CTRL, 0x0);
1848
1849 /* GE1, Force 1000M/FD, FC ON */
1850 mtk_w32(eth, MAC_MCR_FIXED_LINK, MTK_MAC_MCR(0));
1851
1852 /* GE2, Force 1000M/FD, FC ON */
1853 mtk_w32(eth, MAC_MCR_FIXED_LINK, MTK_MAC_MCR(1));
1854
Sean Wang87e3df42017-04-07 16:45:07 +08001855 /* Indicates CDM to parse the MTK special tag from CPU
1856 * which also is working out for untag packets.
1857 */
1858 val = mtk_r32(eth, MTK_CDMQ_IG_CTRL);
1859 mtk_w32(eth, val | MTK_CDMQ_STAG_EN, MTK_CDMQ_IG_CTRL);
1860
John Crispin656e7052016-03-08 11:29:55 +01001861 /* Enable RX VLan Offloading */
1862 mtk_w32(eth, 1, MTK_CDMP_EG_CTRL);
1863
John Crispin671d41e2017-06-19 15:37:04 +02001864 /* enable interrupt delay for RX */
1865 mtk_w32(eth, MTK_PDMA_DELAY_RX_DELAY, MTK_PDMA_DELAY_INT);
1866
John Crispin656e7052016-03-08 11:29:55 +01001867 /* disable delay and normal interrupt */
1868 mtk_w32(eth, 0, MTK_QDMA_DELAY_INT);
Nelson Changbacfd112016-08-26 01:09:42 +08001869 mtk_irq_disable(eth, MTK_QDMA_INT_MASK, ~0);
1870 mtk_irq_disable(eth, MTK_PDMA_INT_MASK, ~0);
John Crispin656e7052016-03-08 11:29:55 +01001871 mtk_w32(eth, RST_GL_PSE, MTK_RST_GL);
1872 mtk_w32(eth, 0, MTK_RST_GL);
1873
1874 /* FE int grouping */
John Crispin80673022016-06-29 13:38:11 +02001875 mtk_w32(eth, MTK_TX_DONE_INT, MTK_PDMA_INT_GRP1);
1876 mtk_w32(eth, MTK_RX_DONE_INT, MTK_PDMA_INT_GRP2);
1877 mtk_w32(eth, MTK_TX_DONE_INT, MTK_QDMA_INT_GRP1);
1878 mtk_w32(eth, MTK_RX_DONE_INT, MTK_QDMA_INT_GRP2);
1879 mtk_w32(eth, 0x21021000, MTK_FE_INT_GRP);
John Crispin656e7052016-03-08 11:29:55 +01001880
1881 for (i = 0; i < 2; i++) {
1882 u32 val = mtk_r32(eth, MTK_GDMA_FWD_CFG(i));
1883
Nelson Chang9c084352016-08-26 01:09:43 +08001884 /* setup the forward port to send frame to PDMA */
John Crispin656e7052016-03-08 11:29:55 +01001885 val &= ~0xffff;
John Crispin656e7052016-03-08 11:29:55 +01001886
1887 /* Enable RX checksum */
1888 val |= MTK_GDMA_ICS_EN | MTK_GDMA_TCS_EN | MTK_GDMA_UCS_EN;
1889
1890 /* setup the mac dma */
1891 mtk_w32(eth, val, MTK_GDMA_FWD_CFG(i));
1892 }
1893
1894 return 0;
1895}
1896
Sean Wangbf253fb2016-09-14 23:13:16 +08001897static int mtk_hw_deinit(struct mtk_eth *eth)
1898{
Sean Wang9ea4d312016-09-14 23:13:19 +08001899 if (!test_and_clear_bit(MTK_HW_INIT, &eth->state))
1900 return 0;
1901
Sean Wangbf253fb2016-09-14 23:13:16 +08001902 clk_disable_unprepare(eth->clks[MTK_CLK_GP2]);
1903 clk_disable_unprepare(eth->clks[MTK_CLK_GP1]);
1904 clk_disable_unprepare(eth->clks[MTK_CLK_ESW]);
1905 clk_disable_unprepare(eth->clks[MTK_CLK_ETHIF]);
1906
Sean Wang26a2ad82016-09-14 23:13:18 +08001907 pm_runtime_put_sync(eth->dev);
1908 pm_runtime_disable(eth->dev);
1909
Sean Wangbf253fb2016-09-14 23:13:16 +08001910 return 0;
1911}
1912
John Crispin656e7052016-03-08 11:29:55 +01001913static int __init mtk_init(struct net_device *dev)
1914{
1915 struct mtk_mac *mac = netdev_priv(dev);
1916 struct mtk_eth *eth = mac->hw;
1917 const char *mac_addr;
1918
1919 mac_addr = of_get_mac_address(mac->of_node);
1920 if (mac_addr)
1921 ether_addr_copy(dev->dev_addr, mac_addr);
1922
1923 /* If the mac address is invalid, use random mac address */
1924 if (!is_valid_ether_addr(dev->dev_addr)) {
Tobias Klausere3c36e42017-03-07 16:27:10 +01001925 eth_hw_addr_random(dev);
John Crispin656e7052016-03-08 11:29:55 +01001926 dev_err(eth->dev, "generated random MAC address %pM\n",
1927 dev->dev_addr);
John Crispin656e7052016-03-08 11:29:55 +01001928 }
1929
Sean Wang2364c5c2016-09-22 16:33:35 +08001930 return mtk_phy_connect(dev);
John Crispin656e7052016-03-08 11:29:55 +01001931}
1932
1933static void mtk_uninit(struct net_device *dev)
1934{
1935 struct mtk_mac *mac = netdev_priv(dev);
1936 struct mtk_eth *eth = mac->hw;
1937
Sean Wang2364c5c2016-09-22 16:33:35 +08001938 phy_disconnect(dev->phydev);
Johan Hovold16a67eb2016-11-28 19:25:05 +01001939 if (of_phy_is_fixed_link(mac->of_node))
1940 of_phy_deregister_fixed_link(mac->of_node);
Nelson Changbacfd112016-08-26 01:09:42 +08001941 mtk_irq_disable(eth, MTK_QDMA_INT_MASK, ~0);
1942 mtk_irq_disable(eth, MTK_PDMA_INT_MASK, ~0);
John Crispin656e7052016-03-08 11:29:55 +01001943}
1944
1945static int mtk_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1946{
John Crispin656e7052016-03-08 11:29:55 +01001947 switch (cmd) {
1948 case SIOCGMIIPHY:
1949 case SIOCGMIIREG:
1950 case SIOCSMIIREG:
Sean Wang2364c5c2016-09-22 16:33:35 +08001951 return phy_mii_ioctl(dev->phydev, ifr, cmd);
John Crispin656e7052016-03-08 11:29:55 +01001952 default:
1953 break;
1954 }
1955
1956 return -EOPNOTSUPP;
1957}
1958
1959static void mtk_pending_work(struct work_struct *work)
1960{
John Crispin7c78b4a2016-04-08 00:54:10 +02001961 struct mtk_eth *eth = container_of(work, struct mtk_eth, pending_work);
John Crispine7d425d2016-04-08 00:54:09 +02001962 int err, i;
1963 unsigned long restart = 0;
John Crispin656e7052016-03-08 11:29:55 +01001964
1965 rtnl_lock();
John Crispin656e7052016-03-08 11:29:55 +01001966
Sean Wangdce6fa42016-09-14 23:13:21 +08001967 dev_dbg(eth->dev, "[%s][%d] reset\n", __func__, __LINE__);
1968
1969 while (test_and_set_bit_lock(MTK_RESETTING, &eth->state))
1970 cpu_relax();
1971
1972 dev_dbg(eth->dev, "[%s][%d] mtk_stop starts\n", __func__, __LINE__);
John Crispine7d425d2016-04-08 00:54:09 +02001973 /* stop all devices to make sure that dma is properly shut down */
1974 for (i = 0; i < MTK_MAC_COUNT; i++) {
John Crispin7c78b4a2016-04-08 00:54:10 +02001975 if (!eth->netdev[i])
John Crispine7d425d2016-04-08 00:54:09 +02001976 continue;
1977 mtk_stop(eth->netdev[i]);
1978 __set_bit(i, &restart);
1979 }
Sean Wangdce6fa42016-09-14 23:13:21 +08001980 dev_dbg(eth->dev, "[%s][%d] mtk_stop ends\n", __func__, __LINE__);
John Crispine7d425d2016-04-08 00:54:09 +02001981
Sean Wang9ea4d312016-09-14 23:13:19 +08001982 /* restart underlying hardware such as power, clock, pin mux
1983 * and the connected phy
1984 */
1985 mtk_hw_deinit(eth);
1986
1987 if (eth->dev->pins)
1988 pinctrl_select_state(eth->dev->pins->p,
1989 eth->dev->pins->default_state);
1990 mtk_hw_init(eth);
1991
1992 for (i = 0; i < MTK_MAC_COUNT; i++) {
1993 if (!eth->mac[i] ||
1994 of_phy_is_fixed_link(eth->mac[i]->of_node))
1995 continue;
Sean Wang2364c5c2016-09-22 16:33:35 +08001996 err = phy_init_hw(eth->netdev[i]->phydev);
Sean Wang9ea4d312016-09-14 23:13:19 +08001997 if (err)
1998 dev_err(eth->dev, "%s: PHY init failed.\n",
1999 eth->netdev[i]->name);
2000 }
2001
John Crispine7d425d2016-04-08 00:54:09 +02002002 /* restart DMA and enable IRQs */
2003 for (i = 0; i < MTK_MAC_COUNT; i++) {
2004 if (!test_bit(i, &restart))
2005 continue;
2006 err = mtk_open(eth->netdev[i]);
2007 if (err) {
2008 netif_alert(eth, ifup, eth->netdev[i],
2009 "Driver up/down cycle failed, closing device.\n");
2010 dev_close(eth->netdev[i]);
2011 }
John Crispin656e7052016-03-08 11:29:55 +01002012 }
Sean Wangdce6fa42016-09-14 23:13:21 +08002013
2014 dev_dbg(eth->dev, "[%s][%d] reset done\n", __func__, __LINE__);
2015
2016 clear_bit_unlock(MTK_RESETTING, &eth->state);
2017
John Crispin656e7052016-03-08 11:29:55 +01002018 rtnl_unlock();
2019}
2020
Sean Wang8a8a9e82016-09-14 23:13:17 +08002021static int mtk_free_dev(struct mtk_eth *eth)
John Crispin656e7052016-03-08 11:29:55 +01002022{
2023 int i;
2024
2025 for (i = 0; i < MTK_MAC_COUNT; i++) {
John Crispin656e7052016-03-08 11:29:55 +01002026 if (!eth->netdev[i])
2027 continue;
John Crispin656e7052016-03-08 11:29:55 +01002028 free_netdev(eth->netdev[i]);
John Crispin656e7052016-03-08 11:29:55 +01002029 }
Sean Wang8a8a9e82016-09-14 23:13:17 +08002030
2031 return 0;
2032}
2033
2034static int mtk_unreg_dev(struct mtk_eth *eth)
2035{
2036 int i;
2037
2038 for (i = 0; i < MTK_MAC_COUNT; i++) {
2039 if (!eth->netdev[i])
2040 continue;
2041 unregister_netdev(eth->netdev[i]);
2042 }
2043
2044 return 0;
2045}
2046
2047static int mtk_cleanup(struct mtk_eth *eth)
2048{
2049 mtk_unreg_dev(eth);
2050 mtk_free_dev(eth);
John Crispin7c78b4a2016-04-08 00:54:10 +02002051 cancel_work_sync(&eth->pending_work);
John Crispin656e7052016-03-08 11:29:55 +01002052
2053 return 0;
2054}
2055
Baoyou Xie3a82e782016-09-30 15:48:50 +08002056static int mtk_get_link_ksettings(struct net_device *ndev,
2057 struct ethtool_link_ksettings *cmd)
John Crispin656e7052016-03-08 11:29:55 +01002058{
Sean Wang3e60b742016-09-22 16:42:03 +08002059 struct mtk_mac *mac = netdev_priv(ndev);
John Crispin656e7052016-03-08 11:29:55 +01002060
Sean Wangdce6fa42016-09-14 23:13:21 +08002061 if (unlikely(test_bit(MTK_RESETTING, &mac->hw->state)))
2062 return -EBUSY;
2063
yuval.shaia@oracle.com55141742017-06-13 10:09:46 +03002064 phy_ethtool_ksettings_get(ndev->phydev, cmd);
2065
2066 return 0;
John Crispin656e7052016-03-08 11:29:55 +01002067}
2068
Baoyou Xie3a82e782016-09-30 15:48:50 +08002069static int mtk_set_link_ksettings(struct net_device *ndev,
2070 const struct ethtool_link_ksettings *cmd)
John Crispin656e7052016-03-08 11:29:55 +01002071{
Sean Wang3e60b742016-09-22 16:42:03 +08002072 struct mtk_mac *mac = netdev_priv(ndev);
John Crispin656e7052016-03-08 11:29:55 +01002073
Sean Wang3e60b742016-09-22 16:42:03 +08002074 if (unlikely(test_bit(MTK_RESETTING, &mac->hw->state)))
2075 return -EBUSY;
John Crispin656e7052016-03-08 11:29:55 +01002076
Sean Wang3e60b742016-09-22 16:42:03 +08002077 return phy_ethtool_ksettings_set(ndev->phydev, cmd);
John Crispin656e7052016-03-08 11:29:55 +01002078}
2079
2080static void mtk_get_drvinfo(struct net_device *dev,
2081 struct ethtool_drvinfo *info)
2082{
2083 struct mtk_mac *mac = netdev_priv(dev);
2084
2085 strlcpy(info->driver, mac->hw->dev->driver->name, sizeof(info->driver));
2086 strlcpy(info->bus_info, dev_name(mac->hw->dev), sizeof(info->bus_info));
2087 info->n_stats = ARRAY_SIZE(mtk_ethtool_stats);
2088}
2089
2090static u32 mtk_get_msglevel(struct net_device *dev)
2091{
2092 struct mtk_mac *mac = netdev_priv(dev);
2093
2094 return mac->hw->msg_enable;
2095}
2096
2097static void mtk_set_msglevel(struct net_device *dev, u32 value)
2098{
2099 struct mtk_mac *mac = netdev_priv(dev);
2100
2101 mac->hw->msg_enable = value;
2102}
2103
2104static int mtk_nway_reset(struct net_device *dev)
2105{
2106 struct mtk_mac *mac = netdev_priv(dev);
2107
Sean Wangdce6fa42016-09-14 23:13:21 +08002108 if (unlikely(test_bit(MTK_RESETTING, &mac->hw->state)))
2109 return -EBUSY;
2110
Sean Wang2364c5c2016-09-22 16:33:35 +08002111 return genphy_restart_aneg(dev->phydev);
John Crispin656e7052016-03-08 11:29:55 +01002112}
2113
2114static u32 mtk_get_link(struct net_device *dev)
2115{
2116 struct mtk_mac *mac = netdev_priv(dev);
2117 int err;
2118
Sean Wangdce6fa42016-09-14 23:13:21 +08002119 if (unlikely(test_bit(MTK_RESETTING, &mac->hw->state)))
2120 return -EBUSY;
2121
Sean Wang2364c5c2016-09-22 16:33:35 +08002122 err = genphy_update_link(dev->phydev);
John Crispin656e7052016-03-08 11:29:55 +01002123 if (err)
2124 return ethtool_op_get_link(dev);
2125
Sean Wang2364c5c2016-09-22 16:33:35 +08002126 return dev->phydev->link;
John Crispin656e7052016-03-08 11:29:55 +01002127}
2128
2129static void mtk_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2130{
2131 int i;
2132
2133 switch (stringset) {
2134 case ETH_SS_STATS:
2135 for (i = 0; i < ARRAY_SIZE(mtk_ethtool_stats); i++) {
2136 memcpy(data, mtk_ethtool_stats[i].str, ETH_GSTRING_LEN);
2137 data += ETH_GSTRING_LEN;
2138 }
2139 break;
2140 }
2141}
2142
2143static int mtk_get_sset_count(struct net_device *dev, int sset)
2144{
2145 switch (sset) {
2146 case ETH_SS_STATS:
2147 return ARRAY_SIZE(mtk_ethtool_stats);
2148 default:
2149 return -EOPNOTSUPP;
2150 }
2151}
2152
2153static void mtk_get_ethtool_stats(struct net_device *dev,
2154 struct ethtool_stats *stats, u64 *data)
2155{
2156 struct mtk_mac *mac = netdev_priv(dev);
2157 struct mtk_hw_stats *hwstats = mac->hw_stats;
2158 u64 *data_src, *data_dst;
2159 unsigned int start;
2160 int i;
2161
Sean Wangdce6fa42016-09-14 23:13:21 +08002162 if (unlikely(test_bit(MTK_RESETTING, &mac->hw->state)))
2163 return;
2164
John Crispin656e7052016-03-08 11:29:55 +01002165 if (netif_running(dev) && netif_device_present(dev)) {
2166 if (spin_trylock(&hwstats->stats_lock)) {
2167 mtk_stats_update_mac(mac);
2168 spin_unlock(&hwstats->stats_lock);
2169 }
2170 }
2171
Sean Wang94d308d2016-09-20 11:26:48 +08002172 data_src = (u64 *)hwstats;
2173
John Crispin656e7052016-03-08 11:29:55 +01002174 do {
John Crispin656e7052016-03-08 11:29:55 +01002175 data_dst = data;
2176 start = u64_stats_fetch_begin_irq(&hwstats->syncp);
2177
2178 for (i = 0; i < ARRAY_SIZE(mtk_ethtool_stats); i++)
2179 *data_dst++ = *(data_src + mtk_ethtool_stats[i].offset);
2180 } while (u64_stats_fetch_retry_irq(&hwstats->syncp, start));
2181}
2182
Nelson Chang7aab7472016-09-17 23:50:56 +08002183static int mtk_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd,
2184 u32 *rule_locs)
2185{
2186 int ret = -EOPNOTSUPP;
2187
2188 switch (cmd->cmd) {
2189 case ETHTOOL_GRXRINGS:
2190 if (dev->features & NETIF_F_LRO) {
2191 cmd->data = MTK_MAX_RX_RING_NUM;
2192 ret = 0;
2193 }
2194 break;
2195 case ETHTOOL_GRXCLSRLCNT:
2196 if (dev->features & NETIF_F_LRO) {
2197 struct mtk_mac *mac = netdev_priv(dev);
2198
2199 cmd->rule_cnt = mac->hwlro_ip_cnt;
2200 ret = 0;
2201 }
2202 break;
2203 case ETHTOOL_GRXCLSRULE:
2204 if (dev->features & NETIF_F_LRO)
2205 ret = mtk_hwlro_get_fdir_entry(dev, cmd);
2206 break;
2207 case ETHTOOL_GRXCLSRLALL:
2208 if (dev->features & NETIF_F_LRO)
2209 ret = mtk_hwlro_get_fdir_all(dev, cmd,
2210 rule_locs);
2211 break;
2212 default:
2213 break;
2214 }
2215
2216 return ret;
2217}
2218
2219static int mtk_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd)
2220{
2221 int ret = -EOPNOTSUPP;
2222
2223 switch (cmd->cmd) {
2224 case ETHTOOL_SRXCLSRLINS:
2225 if (dev->features & NETIF_F_LRO)
2226 ret = mtk_hwlro_add_ipaddr(dev, cmd);
2227 break;
2228 case ETHTOOL_SRXCLSRLDEL:
2229 if (dev->features & NETIF_F_LRO)
2230 ret = mtk_hwlro_del_ipaddr(dev, cmd);
2231 break;
2232 default:
2233 break;
2234 }
2235
2236 return ret;
2237}
2238
Julia Lawall6a38cb12016-09-01 00:21:19 +02002239static const struct ethtool_ops mtk_ethtool_ops = {
Sean Wang3e60b742016-09-22 16:42:03 +08002240 .get_link_ksettings = mtk_get_link_ksettings,
2241 .set_link_ksettings = mtk_set_link_ksettings,
John Crispin656e7052016-03-08 11:29:55 +01002242 .get_drvinfo = mtk_get_drvinfo,
2243 .get_msglevel = mtk_get_msglevel,
2244 .set_msglevel = mtk_set_msglevel,
2245 .nway_reset = mtk_nway_reset,
2246 .get_link = mtk_get_link,
2247 .get_strings = mtk_get_strings,
2248 .get_sset_count = mtk_get_sset_count,
2249 .get_ethtool_stats = mtk_get_ethtool_stats,
Nelson Chang7aab7472016-09-17 23:50:56 +08002250 .get_rxnfc = mtk_get_rxnfc,
2251 .set_rxnfc = mtk_set_rxnfc,
John Crispin656e7052016-03-08 11:29:55 +01002252};
2253
2254static const struct net_device_ops mtk_netdev_ops = {
2255 .ndo_init = mtk_init,
2256 .ndo_uninit = mtk_uninit,
2257 .ndo_open = mtk_open,
2258 .ndo_stop = mtk_stop,
2259 .ndo_start_xmit = mtk_start_xmit,
2260 .ndo_set_mac_address = mtk_set_mac_address,
2261 .ndo_validate_addr = eth_validate_addr,
2262 .ndo_do_ioctl = mtk_do_ioctl,
John Crispin656e7052016-03-08 11:29:55 +01002263 .ndo_tx_timeout = mtk_tx_timeout,
2264 .ndo_get_stats64 = mtk_get_stats64,
Nelson Chang7aab7472016-09-17 23:50:56 +08002265 .ndo_fix_features = mtk_fix_features,
2266 .ndo_set_features = mtk_set_features,
John Crispin656e7052016-03-08 11:29:55 +01002267#ifdef CONFIG_NET_POLL_CONTROLLER
2268 .ndo_poll_controller = mtk_poll_controller,
2269#endif
2270};
2271
2272static int mtk_add_mac(struct mtk_eth *eth, struct device_node *np)
2273{
2274 struct mtk_mac *mac;
2275 const __be32 *_id = of_get_property(np, "reg", NULL);
2276 int id, err;
2277
2278 if (!_id) {
2279 dev_err(eth->dev, "missing mac id\n");
2280 return -EINVAL;
2281 }
2282
2283 id = be32_to_cpup(_id);
2284 if (id >= MTK_MAC_COUNT) {
2285 dev_err(eth->dev, "%d is not a valid mac id\n", id);
2286 return -EINVAL;
2287 }
2288
2289 if (eth->netdev[id]) {
2290 dev_err(eth->dev, "duplicate mac id found: %d\n", id);
2291 return -EINVAL;
2292 }
2293
2294 eth->netdev[id] = alloc_etherdev(sizeof(*mac));
2295 if (!eth->netdev[id]) {
2296 dev_err(eth->dev, "alloc_etherdev failed\n");
2297 return -ENOMEM;
2298 }
2299 mac = netdev_priv(eth->netdev[id]);
2300 eth->mac[id] = mac;
2301 mac->id = id;
2302 mac->hw = eth;
2303 mac->of_node = np;
John Crispin656e7052016-03-08 11:29:55 +01002304
Nelson Changee406812016-09-17 23:50:55 +08002305 memset(mac->hwlro_ip, 0, sizeof(mac->hwlro_ip));
2306 mac->hwlro_ip_cnt = 0;
2307
John Crispin656e7052016-03-08 11:29:55 +01002308 mac->hw_stats = devm_kzalloc(eth->dev,
2309 sizeof(*mac->hw_stats),
2310 GFP_KERNEL);
2311 if (!mac->hw_stats) {
2312 dev_err(eth->dev, "failed to allocate counter memory\n");
2313 err = -ENOMEM;
2314 goto free_netdev;
2315 }
2316 spin_lock_init(&mac->hw_stats->stats_lock);
sean.wang@mediatek.comd70056522016-08-13 19:16:18 +08002317 u64_stats_init(&mac->hw_stats->syncp);
John Crispin656e7052016-03-08 11:29:55 +01002318 mac->hw_stats->reg_offset = id * MTK_STAT_OFFSET;
2319
2320 SET_NETDEV_DEV(eth->netdev[id], eth->dev);
John Crispineaadf9f2016-06-10 13:28:05 +02002321 eth->netdev[id]->watchdog_timeo = 5 * HZ;
John Crispin656e7052016-03-08 11:29:55 +01002322 eth->netdev[id]->netdev_ops = &mtk_netdev_ops;
2323 eth->netdev[id]->base_addr = (unsigned long)eth->base;
Nelson Changee406812016-09-17 23:50:55 +08002324
2325 eth->netdev[id]->hw_features = MTK_HW_FEATURES;
2326 if (eth->hwlro)
2327 eth->netdev[id]->hw_features |= NETIF_F_LRO;
2328
John Crispin656e7052016-03-08 11:29:55 +01002329 eth->netdev[id]->vlan_features = MTK_HW_FEATURES &
2330 ~(NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX);
2331 eth->netdev[id]->features |= MTK_HW_FEATURES;
2332 eth->netdev[id]->ethtool_ops = &mtk_ethtool_ops;
2333
John Crispin80673022016-06-29 13:38:11 +02002334 eth->netdev[id]->irq = eth->irq[0];
Sean Wang3174b3b2017-04-07 16:45:08 +08002335 eth->netdev[id]->dev.of_node = np;
2336
John Crispin656e7052016-03-08 11:29:55 +01002337 return 0;
2338
2339free_netdev:
2340 free_netdev(eth->netdev[id]);
2341 return err;
2342}
2343
Nelson Changb95b6d92016-10-06 19:44:01 +08002344static int mtk_get_chip_id(struct mtk_eth *eth, u32 *chip_id)
2345{
2346 u32 val[2], id[4];
2347
2348 regmap_read(eth->ethsys, ETHSYS_CHIPID0_3, &val[0]);
2349 regmap_read(eth->ethsys, ETHSYS_CHIPID4_7, &val[1]);
2350
2351 id[3] = ((val[0] >> 16) & 0xff) - '0';
2352 id[2] = ((val[0] >> 24) & 0xff) - '0';
2353 id[1] = (val[1] & 0xff) - '0';
2354 id[0] = ((val[1] >> 8) & 0xff) - '0';
2355
2356 *chip_id = (id[3] * 1000) + (id[2] * 100) +
2357 (id[1] * 10) + id[0];
2358
2359 if (!(*chip_id)) {
2360 dev_err(eth->dev, "failed to get chip id\n");
2361 return -ENODEV;
2362 }
2363
2364 dev_info(eth->dev, "chip id = %d\n", *chip_id);
2365
2366 return 0;
2367}
2368
Nelson Chang983e1a62016-10-06 19:44:02 +08002369static bool mtk_is_hwlro_supported(struct mtk_eth *eth)
2370{
2371 switch (eth->chip_id) {
2372 case MT7623_ETH:
2373 return true;
2374 }
2375
2376 return false;
2377}
2378
John Crispin656e7052016-03-08 11:29:55 +01002379static int mtk_probe(struct platform_device *pdev)
2380{
2381 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2382 struct device_node *mac_np;
2383 const struct of_device_id *match;
2384 struct mtk_soc_data *soc;
2385 struct mtk_eth *eth;
2386 int err;
John Crispin80673022016-06-29 13:38:11 +02002387 int i;
John Crispin656e7052016-03-08 11:29:55 +01002388
John Crispin656e7052016-03-08 11:29:55 +01002389 match = of_match_device(of_mtk_match, &pdev->dev);
2390 soc = (struct mtk_soc_data *)match->data;
2391
2392 eth = devm_kzalloc(&pdev->dev, sizeof(*eth), GFP_KERNEL);
2393 if (!eth)
2394 return -ENOMEM;
2395
Sean Wang549e5492016-09-01 10:47:28 +08002396 eth->dev = &pdev->dev;
John Crispin656e7052016-03-08 11:29:55 +01002397 eth->base = devm_ioremap_resource(&pdev->dev, res);
Vladimir Zapolskiy621e49f2016-03-23 01:06:04 +02002398 if (IS_ERR(eth->base))
2399 return PTR_ERR(eth->base);
John Crispin656e7052016-03-08 11:29:55 +01002400
2401 spin_lock_init(&eth->page_lock);
John Crispin7bc9cce2016-06-29 13:38:10 +02002402 spin_lock_init(&eth->irq_lock);
John Crispin656e7052016-03-08 11:29:55 +01002403
2404 eth->ethsys = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
2405 "mediatek,ethsys");
2406 if (IS_ERR(eth->ethsys)) {
2407 dev_err(&pdev->dev, "no ethsys regmap found\n");
2408 return PTR_ERR(eth->ethsys);
2409 }
2410
2411 eth->pctl = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
2412 "mediatek,pctl");
2413 if (IS_ERR(eth->pctl)) {
2414 dev_err(&pdev->dev, "no pctl regmap found\n");
2415 return PTR_ERR(eth->pctl);
2416 }
2417
John Crispin80673022016-06-29 13:38:11 +02002418 for (i = 0; i < 3; i++) {
2419 eth->irq[i] = platform_get_irq(pdev, i);
2420 if (eth->irq[i] < 0) {
2421 dev_err(&pdev->dev, "no IRQ%d resource found\n", i);
2422 return -ENXIO;
2423 }
John Crispin656e7052016-03-08 11:29:55 +01002424 }
Sean Wang549e5492016-09-01 10:47:28 +08002425 for (i = 0; i < ARRAY_SIZE(eth->clks); i++) {
2426 eth->clks[i] = devm_clk_get(eth->dev,
2427 mtk_clks_source_name[i]);
2428 if (IS_ERR(eth->clks[i])) {
2429 if (PTR_ERR(eth->clks[i]) == -EPROBE_DEFER)
2430 return -EPROBE_DEFER;
2431 return -ENODEV;
2432 }
2433 }
John Crispin656e7052016-03-08 11:29:55 +01002434
John Crispin656e7052016-03-08 11:29:55 +01002435 eth->msg_enable = netif_msg_init(mtk_msg_level, MTK_DEFAULT_MSG_ENABLE);
John Crispin7c78b4a2016-04-08 00:54:10 +02002436 INIT_WORK(&eth->pending_work, mtk_pending_work);
John Crispin656e7052016-03-08 11:29:55 +01002437
2438 err = mtk_hw_init(eth);
2439 if (err)
2440 return err;
2441
Nelson Changb95b6d92016-10-06 19:44:01 +08002442 err = mtk_get_chip_id(eth, &eth->chip_id);
2443 if (err)
2444 return err;
2445
Nelson Chang983e1a62016-10-06 19:44:02 +08002446 eth->hwlro = mtk_is_hwlro_supported(eth);
2447
John Crispin656e7052016-03-08 11:29:55 +01002448 for_each_child_of_node(pdev->dev.of_node, mac_np) {
2449 if (!of_device_is_compatible(mac_np,
2450 "mediatek,eth-mac"))
2451 continue;
2452
2453 if (!of_device_is_available(mac_np))
2454 continue;
2455
2456 err = mtk_add_mac(eth, mac_np);
2457 if (err)
Sean Wang8a8a9e82016-09-14 23:13:17 +08002458 goto err_deinit_hw;
John Crispin656e7052016-03-08 11:29:55 +01002459 }
2460
Sean Wang85574db2016-09-14 23:13:15 +08002461 err = devm_request_irq(eth->dev, eth->irq[1], mtk_handle_irq_tx, 0,
2462 dev_name(eth->dev), eth);
2463 if (err)
2464 goto err_free_dev;
2465
2466 err = devm_request_irq(eth->dev, eth->irq[2], mtk_handle_irq_rx, 0,
2467 dev_name(eth->dev), eth);
2468 if (err)
2469 goto err_free_dev;
2470
2471 err = mtk_mdio_init(eth);
2472 if (err)
2473 goto err_free_dev;
2474
2475 for (i = 0; i < MTK_MAX_DEVS; i++) {
2476 if (!eth->netdev[i])
2477 continue;
2478
2479 err = register_netdev(eth->netdev[i]);
2480 if (err) {
2481 dev_err(eth->dev, "error bringing up device\n");
Sean Wang8a8a9e82016-09-14 23:13:17 +08002482 goto err_deinit_mdio;
Sean Wang85574db2016-09-14 23:13:15 +08002483 } else
2484 netif_info(eth, probe, eth->netdev[i],
2485 "mediatek frame engine at 0x%08lx, irq %d\n",
2486 eth->netdev[i]->base_addr, eth->irq[0]);
2487 }
2488
John Crispin656e7052016-03-08 11:29:55 +01002489 /* we run 2 devices on the same DMA ring so we need a dummy device
2490 * for NAPI to work
2491 */
2492 init_dummy_netdev(&eth->dummy_dev);
John Crispin80673022016-06-29 13:38:11 +02002493 netif_napi_add(&eth->dummy_dev, &eth->tx_napi, mtk_napi_tx,
2494 MTK_NAPI_WEIGHT);
2495 netif_napi_add(&eth->dummy_dev, &eth->rx_napi, mtk_napi_rx,
John Crispin656e7052016-03-08 11:29:55 +01002496 MTK_NAPI_WEIGHT);
2497
2498 platform_set_drvdata(pdev, eth);
2499
2500 return 0;
2501
Sean Wang8a8a9e82016-09-14 23:13:17 +08002502err_deinit_mdio:
2503 mtk_mdio_cleanup(eth);
John Crispin656e7052016-03-08 11:29:55 +01002504err_free_dev:
Sean Wang8a8a9e82016-09-14 23:13:17 +08002505 mtk_free_dev(eth);
2506err_deinit_hw:
2507 mtk_hw_deinit(eth);
2508
John Crispin656e7052016-03-08 11:29:55 +01002509 return err;
2510}
2511
2512static int mtk_remove(struct platform_device *pdev)
2513{
2514 struct mtk_eth *eth = platform_get_drvdata(pdev);
Sean Wang79e9a412016-09-01 10:47:32 +08002515 int i;
John Crispin656e7052016-03-08 11:29:55 +01002516
Sean Wang79e9a412016-09-01 10:47:32 +08002517 /* stop all devices to make sure that dma is properly shut down */
2518 for (i = 0; i < MTK_MAC_COUNT; i++) {
2519 if (!eth->netdev[i])
2520 continue;
2521 mtk_stop(eth->netdev[i]);
2522 }
John Crispin656e7052016-03-08 11:29:55 +01002523
Sean Wangbf253fb2016-09-14 23:13:16 +08002524 mtk_hw_deinit(eth);
John Crispin656e7052016-03-08 11:29:55 +01002525
John Crispin80673022016-06-29 13:38:11 +02002526 netif_napi_del(&eth->tx_napi);
John Crispin656e7052016-03-08 11:29:55 +01002527 netif_napi_del(&eth->rx_napi);
2528 mtk_cleanup(eth);
Sean Wange82f7142016-09-20 23:53:24 +08002529 mtk_mdio_cleanup(eth);
John Crispin656e7052016-03-08 11:29:55 +01002530
2531 return 0;
2532}
2533
2534const struct of_device_id of_mtk_match[] = {
John Crispin8b901f62017-01-25 09:20:55 +01002535 { .compatible = "mediatek,mt2701-eth" },
John Crispin656e7052016-03-08 11:29:55 +01002536 {},
2537};
Sean Wang7077dc42016-09-14 21:29:34 +08002538MODULE_DEVICE_TABLE(of, of_mtk_match);
John Crispin656e7052016-03-08 11:29:55 +01002539
2540static struct platform_driver mtk_driver = {
2541 .probe = mtk_probe,
2542 .remove = mtk_remove,
2543 .driver = {
2544 .name = "mtk_soc_eth",
John Crispin656e7052016-03-08 11:29:55 +01002545 .of_match_table = of_mtk_match,
2546 },
2547};
2548
2549module_platform_driver(mtk_driver);
2550
2551MODULE_LICENSE("GPL");
2552MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
2553MODULE_DESCRIPTION("Ethernet driver for MediaTek SoC");