blob: a9a8354202814a140b5ae9651dbc4d3ae53c2637 [file] [log] [blame]
Mugunthan V Ndf828592012-03-18 20:17:54 +00001/*
2 * Texas Instruments Ethernet Switch Driver
3 *
4 * Copyright (C) 2012 Texas Instruments
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation version 2.
9 *
10 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
11 * kind, whether express or implied; without even the implied warranty
12 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16#include <linux/kernel.h>
17#include <linux/io.h>
18#include <linux/clk.h>
19#include <linux/timer.h>
20#include <linux/module.h>
21#include <linux/platform_device.h>
22#include <linux/irqreturn.h>
23#include <linux/interrupt.h>
24#include <linux/if_ether.h>
25#include <linux/etherdevice.h>
26#include <linux/netdevice.h>
Richard Cochran2e5b38a2012-10-29 08:45:20 +000027#include <linux/net_tstamp.h>
Mugunthan V Ndf828592012-03-18 20:17:54 +000028#include <linux/phy.h>
29#include <linux/workqueue.h>
30#include <linux/delay.h>
Mugunthan V Nf150bd72012-07-17 08:09:50 +000031#include <linux/pm_runtime.h>
Mugunthan V N1d147cc2015-09-07 15:16:44 +053032#include <linux/gpio.h>
Mugunthan V N2eb32b02012-07-30 10:17:14 +000033#include <linux/of.h>
Heiko Schocher9e42f712015-10-17 06:04:35 +020034#include <linux/of_mdio.h>
Mugunthan V N2eb32b02012-07-30 10:17:14 +000035#include <linux/of_net.h>
36#include <linux/of_device.h>
Mugunthan V N3b72c2f2013-02-05 08:26:48 +000037#include <linux/if_vlan.h>
Mugunthan V Ndf828592012-03-18 20:17:54 +000038
Mugunthan V N739683b2013-06-06 23:45:14 +053039#include <linux/pinctrl/consumer.h>
Mugunthan V Ndf828592012-03-18 20:17:54 +000040
Mugunthan V Ndbe34722013-08-19 17:47:40 +053041#include "cpsw.h"
Mugunthan V Ndf828592012-03-18 20:17:54 +000042#include "cpsw_ale.h"
Richard Cochran2e5b38a2012-10-29 08:45:20 +000043#include "cpts.h"
Mugunthan V Ndf828592012-03-18 20:17:54 +000044#include "davinci_cpdma.h"
45
46#define CPSW_DEBUG (NETIF_MSG_HW | NETIF_MSG_WOL | \
47 NETIF_MSG_DRV | NETIF_MSG_LINK | \
48 NETIF_MSG_IFUP | NETIF_MSG_INTR | \
49 NETIF_MSG_PROBE | NETIF_MSG_TIMER | \
50 NETIF_MSG_IFDOWN | NETIF_MSG_RX_ERR | \
51 NETIF_MSG_TX_ERR | NETIF_MSG_TX_DONE | \
52 NETIF_MSG_PKTDATA | NETIF_MSG_TX_QUEUED | \
53 NETIF_MSG_RX_STATUS)
54
55#define cpsw_info(priv, type, format, ...) \
56do { \
57 if (netif_msg_##type(priv) && net_ratelimit()) \
58 dev_info(priv->dev, format, ## __VA_ARGS__); \
59} while (0)
60
61#define cpsw_err(priv, type, format, ...) \
62do { \
63 if (netif_msg_##type(priv) && net_ratelimit()) \
64 dev_err(priv->dev, format, ## __VA_ARGS__); \
65} while (0)
66
67#define cpsw_dbg(priv, type, format, ...) \
68do { \
69 if (netif_msg_##type(priv) && net_ratelimit()) \
70 dev_dbg(priv->dev, format, ## __VA_ARGS__); \
71} while (0)
72
73#define cpsw_notice(priv, type, format, ...) \
74do { \
75 if (netif_msg_##type(priv) && net_ratelimit()) \
76 dev_notice(priv->dev, format, ## __VA_ARGS__); \
77} while (0)
78
Mugunthan V N5c50a852012-10-29 08:45:11 +000079#define ALE_ALL_PORTS 0x7
80
Mugunthan V Ndf828592012-03-18 20:17:54 +000081#define CPSW_MAJOR_VERSION(reg) (reg >> 8 & 0x7)
82#define CPSW_MINOR_VERSION(reg) (reg & 0xff)
83#define CPSW_RTL_VERSION(reg) ((reg >> 11) & 0x1f)
84
Richard Cochrane90cfac2012-10-29 08:45:14 +000085#define CPSW_VERSION_1 0x19010a
86#define CPSW_VERSION_2 0x19010c
Mugunthan V Nc193f362013-08-05 17:30:05 +053087#define CPSW_VERSION_3 0x19010f
Mugunthan V N926489b2013-08-12 17:11:15 +053088#define CPSW_VERSION_4 0x190112
Richard Cochran549985e2012-11-14 09:07:56 +000089
90#define HOST_PORT_NUM 0
91#define SLIVER_SIZE 0x40
92
93#define CPSW1_HOST_PORT_OFFSET 0x028
94#define CPSW1_SLAVE_OFFSET 0x050
95#define CPSW1_SLAVE_SIZE 0x040
96#define CPSW1_CPDMA_OFFSET 0x100
97#define CPSW1_STATERAM_OFFSET 0x200
Mugunthan V Nd9718542013-07-23 15:38:17 +053098#define CPSW1_HW_STATS 0x400
Richard Cochran549985e2012-11-14 09:07:56 +000099#define CPSW1_CPTS_OFFSET 0x500
100#define CPSW1_ALE_OFFSET 0x600
101#define CPSW1_SLIVER_OFFSET 0x700
102
103#define CPSW2_HOST_PORT_OFFSET 0x108
104#define CPSW2_SLAVE_OFFSET 0x200
105#define CPSW2_SLAVE_SIZE 0x100
106#define CPSW2_CPDMA_OFFSET 0x800
Mugunthan V Nd9718542013-07-23 15:38:17 +0530107#define CPSW2_HW_STATS 0x900
Richard Cochran549985e2012-11-14 09:07:56 +0000108#define CPSW2_STATERAM_OFFSET 0xa00
109#define CPSW2_CPTS_OFFSET 0xc00
110#define CPSW2_ALE_OFFSET 0xd00
111#define CPSW2_SLIVER_OFFSET 0xd80
112#define CPSW2_BD_OFFSET 0x2000
113
Mugunthan V Ndf828592012-03-18 20:17:54 +0000114#define CPDMA_RXTHRESH 0x0c0
115#define CPDMA_RXFREE 0x0e0
116#define CPDMA_TXHDP 0x00
117#define CPDMA_RXHDP 0x20
118#define CPDMA_TXCP 0x40
119#define CPDMA_RXCP 0x60
120
Mugunthan V Ndf828592012-03-18 20:17:54 +0000121#define CPSW_POLL_WEIGHT 64
122#define CPSW_MIN_PACKET_SIZE 60
123#define CPSW_MAX_PACKET_SIZE (1500 + 14 + 4 + 4)
124
125#define RX_PRIORITY_MAPPING 0x76543210
126#define TX_PRIORITY_MAPPING 0x33221100
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300127#define CPDMA_TX_PRIORITY_MAP 0x01234567
Mugunthan V Ndf828592012-03-18 20:17:54 +0000128
Mugunthan V N3b72c2f2013-02-05 08:26:48 +0000129#define CPSW_VLAN_AWARE BIT(1)
130#define CPSW_ALE_VLAN_AWARE 1
131
John Ogness35717d82014-11-14 15:42:52 +0100132#define CPSW_FIFO_NORMAL_MODE (0 << 16)
133#define CPSW_FIFO_DUAL_MAC_MODE (1 << 16)
134#define CPSW_FIFO_RATE_LIMIT_MODE (2 << 16)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000135
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +0000136#define CPSW_INTPACEEN (0x3f << 16)
137#define CPSW_INTPRESCALE_MASK (0x7FF << 0)
138#define CPSW_CMINTMAX_CNT 63
139#define CPSW_CMINTMIN_CNT 2
140#define CPSW_CMINTMAX_INTVL (1000 / CPSW_CMINTMIN_CNT)
141#define CPSW_CMINTMIN_INTVL ((1000 / CPSW_CMINTMAX_CNT) + 1)
142
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300143#define cpsw_slave_index(cpsw, priv) \
144 ((cpsw->data.dual_emac) ? priv->emac_port : \
145 cpsw->data.active_slave)
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +0300146#define IRQ_NUM 2
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300147#define CPSW_MAX_QUEUES 8
Mugunthan V Nd3bb9c52013-03-11 23:16:36 +0000148
Mugunthan V Ndf828592012-03-18 20:17:54 +0000149static int debug_level;
150module_param(debug_level, int, 0);
151MODULE_PARM_DESC(debug_level, "cpsw debug level (NETIF_MSG bits)");
152
153static int ale_ageout = 10;
154module_param(ale_ageout, int, 0);
155MODULE_PARM_DESC(ale_ageout, "cpsw ale ageout interval (seconds)");
156
157static int rx_packet_max = CPSW_MAX_PACKET_SIZE;
158module_param(rx_packet_max, int, 0);
159MODULE_PARM_DESC(rx_packet_max, "maximum receive packet size (bytes)");
160
Richard Cochran996a5c22012-10-29 08:45:12 +0000161struct cpsw_wr_regs {
Mugunthan V Ndf828592012-03-18 20:17:54 +0000162 u32 id_ver;
163 u32 soft_reset;
164 u32 control;
165 u32 int_control;
166 u32 rx_thresh_en;
167 u32 rx_en;
168 u32 tx_en;
169 u32 misc_en;
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +0000170 u32 mem_allign1[8];
171 u32 rx_thresh_stat;
172 u32 rx_stat;
173 u32 tx_stat;
174 u32 misc_stat;
175 u32 mem_allign2[8];
176 u32 rx_imax;
177 u32 tx_imax;
178
Mugunthan V Ndf828592012-03-18 20:17:54 +0000179};
180
Richard Cochran996a5c22012-10-29 08:45:12 +0000181struct cpsw_ss_regs {
Mugunthan V Ndf828592012-03-18 20:17:54 +0000182 u32 id_ver;
183 u32 control;
184 u32 soft_reset;
185 u32 stat_port_en;
186 u32 ptype;
Richard Cochranbd357af2012-10-29 08:45:13 +0000187 u32 soft_idle;
188 u32 thru_rate;
189 u32 gap_thresh;
190 u32 tx_start_wds;
191 u32 flow_control;
192 u32 vlan_ltype;
193 u32 ts_ltype;
194 u32 dlr_ltype;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000195};
196
Richard Cochran9750a3a2012-10-29 08:45:15 +0000197/* CPSW_PORT_V1 */
198#define CPSW1_MAX_BLKS 0x00 /* Maximum FIFO Blocks */
199#define CPSW1_BLK_CNT 0x04 /* FIFO Block Usage Count (Read Only) */
200#define CPSW1_TX_IN_CTL 0x08 /* Transmit FIFO Control */
201#define CPSW1_PORT_VLAN 0x0c /* VLAN Register */
202#define CPSW1_TX_PRI_MAP 0x10 /* Tx Header Priority to Switch Pri Mapping */
203#define CPSW1_TS_CTL 0x14 /* Time Sync Control */
204#define CPSW1_TS_SEQ_LTYPE 0x18 /* Time Sync Sequence ID Offset and Msg Type */
205#define CPSW1_TS_VLAN 0x1c /* Time Sync VLAN1 and VLAN2 */
206
207/* CPSW_PORT_V2 */
208#define CPSW2_CONTROL 0x00 /* Control Register */
209#define CPSW2_MAX_BLKS 0x08 /* Maximum FIFO Blocks */
210#define CPSW2_BLK_CNT 0x0c /* FIFO Block Usage Count (Read Only) */
211#define CPSW2_TX_IN_CTL 0x10 /* Transmit FIFO Control */
212#define CPSW2_PORT_VLAN 0x14 /* VLAN Register */
213#define CPSW2_TX_PRI_MAP 0x18 /* Tx Header Priority to Switch Pri Mapping */
214#define CPSW2_TS_SEQ_MTYPE 0x1c /* Time Sync Sequence ID Offset and Msg Type */
215
216/* CPSW_PORT_V1 and V2 */
217#define SA_LO 0x20 /* CPGMAC_SL Source Address Low */
218#define SA_HI 0x24 /* CPGMAC_SL Source Address High */
219#define SEND_PERCENT 0x28 /* Transmit Queue Send Percentages */
220
221/* CPSW_PORT_V2 only */
222#define RX_DSCP_PRI_MAP0 0x30 /* Rx DSCP Priority to Rx Packet Mapping */
223#define RX_DSCP_PRI_MAP1 0x34 /* Rx DSCP Priority to Rx Packet Mapping */
224#define RX_DSCP_PRI_MAP2 0x38 /* Rx DSCP Priority to Rx Packet Mapping */
225#define RX_DSCP_PRI_MAP3 0x3c /* Rx DSCP Priority to Rx Packet Mapping */
226#define RX_DSCP_PRI_MAP4 0x40 /* Rx DSCP Priority to Rx Packet Mapping */
227#define RX_DSCP_PRI_MAP5 0x44 /* Rx DSCP Priority to Rx Packet Mapping */
228#define RX_DSCP_PRI_MAP6 0x48 /* Rx DSCP Priority to Rx Packet Mapping */
229#define RX_DSCP_PRI_MAP7 0x4c /* Rx DSCP Priority to Rx Packet Mapping */
230
231/* Bit definitions for the CPSW2_CONTROL register */
232#define PASS_PRI_TAGGED (1<<24) /* Pass Priority Tagged */
233#define VLAN_LTYPE2_EN (1<<21) /* VLAN LTYPE 2 enable */
234#define VLAN_LTYPE1_EN (1<<20) /* VLAN LTYPE 1 enable */
235#define DSCP_PRI_EN (1<<16) /* DSCP Priority Enable */
236#define TS_320 (1<<14) /* Time Sync Dest Port 320 enable */
237#define TS_319 (1<<13) /* Time Sync Dest Port 319 enable */
238#define TS_132 (1<<12) /* Time Sync Dest IP Addr 132 enable */
239#define TS_131 (1<<11) /* Time Sync Dest IP Addr 131 enable */
240#define TS_130 (1<<10) /* Time Sync Dest IP Addr 130 enable */
241#define TS_129 (1<<9) /* Time Sync Dest IP Addr 129 enable */
George Cherian09c55372014-05-02 12:02:02 +0530242#define TS_TTL_NONZERO (1<<8) /* Time Sync Time To Live Non-zero enable */
243#define TS_ANNEX_F_EN (1<<6) /* Time Sync Annex F enable */
Richard Cochran9750a3a2012-10-29 08:45:15 +0000244#define TS_ANNEX_D_EN (1<<4) /* Time Sync Annex D enable */
245#define TS_LTYPE2_EN (1<<3) /* Time Sync LTYPE 2 enable */
246#define TS_LTYPE1_EN (1<<2) /* Time Sync LTYPE 1 enable */
247#define TS_TX_EN (1<<1) /* Time Sync Transmit Enable */
248#define TS_RX_EN (1<<0) /* Time Sync Receive Enable */
249
George Cherian09c55372014-05-02 12:02:02 +0530250#define CTRL_V2_TS_BITS \
251 (TS_320 | TS_319 | TS_132 | TS_131 | TS_130 | TS_129 |\
252 TS_TTL_NONZERO | TS_ANNEX_D_EN | TS_LTYPE1_EN)
Richard Cochran9750a3a2012-10-29 08:45:15 +0000253
George Cherian09c55372014-05-02 12:02:02 +0530254#define CTRL_V2_ALL_TS_MASK (CTRL_V2_TS_BITS | TS_TX_EN | TS_RX_EN)
255#define CTRL_V2_TX_TS_BITS (CTRL_V2_TS_BITS | TS_TX_EN)
256#define CTRL_V2_RX_TS_BITS (CTRL_V2_TS_BITS | TS_RX_EN)
257
258
259#define CTRL_V3_TS_BITS \
260 (TS_320 | TS_319 | TS_132 | TS_131 | TS_130 | TS_129 |\
261 TS_TTL_NONZERO | TS_ANNEX_F_EN | TS_ANNEX_D_EN |\
262 TS_LTYPE1_EN)
263
264#define CTRL_V3_ALL_TS_MASK (CTRL_V3_TS_BITS | TS_TX_EN | TS_RX_EN)
265#define CTRL_V3_TX_TS_BITS (CTRL_V3_TS_BITS | TS_TX_EN)
266#define CTRL_V3_RX_TS_BITS (CTRL_V3_TS_BITS | TS_RX_EN)
Richard Cochran9750a3a2012-10-29 08:45:15 +0000267
268/* Bit definitions for the CPSW2_TS_SEQ_MTYPE register */
269#define TS_SEQ_ID_OFFSET_SHIFT (16) /* Time Sync Sequence ID Offset */
270#define TS_SEQ_ID_OFFSET_MASK (0x3f)
271#define TS_MSG_TYPE_EN_SHIFT (0) /* Time Sync Message Type Enable */
272#define TS_MSG_TYPE_EN_MASK (0xffff)
273
274/* The PTP event messages - Sync, Delay_Req, Pdelay_Req, and Pdelay_Resp. */
275#define EVENT_MSG_BITS ((1<<0) | (1<<1) | (1<<2) | (1<<3))
Mugunthan V Ndf828592012-03-18 20:17:54 +0000276
Richard Cochran2e5b38a2012-10-29 08:45:20 +0000277/* Bit definitions for the CPSW1_TS_CTL register */
278#define CPSW_V1_TS_RX_EN BIT(0)
279#define CPSW_V1_TS_TX_EN BIT(4)
280#define CPSW_V1_MSG_TYPE_OFS 16
281
282/* Bit definitions for the CPSW1_TS_SEQ_LTYPE register */
283#define CPSW_V1_SEQ_ID_OFS_SHIFT 16
284
Mugunthan V Ndf828592012-03-18 20:17:54 +0000285struct cpsw_host_regs {
286 u32 max_blks;
287 u32 blk_cnt;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000288 u32 tx_in_ctl;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000289 u32 port_vlan;
290 u32 tx_pri_map;
291 u32 cpdma_tx_pri_map;
292 u32 cpdma_rx_chan_map;
293};
294
295struct cpsw_sliver_regs {
296 u32 id_ver;
297 u32 mac_control;
298 u32 mac_status;
299 u32 soft_reset;
300 u32 rx_maxlen;
301 u32 __reserved_0;
302 u32 rx_pause;
303 u32 tx_pause;
304 u32 __reserved_1;
305 u32 rx_pri_map;
306};
307
Mugunthan V Nd9718542013-07-23 15:38:17 +0530308struct cpsw_hw_stats {
309 u32 rxgoodframes;
310 u32 rxbroadcastframes;
311 u32 rxmulticastframes;
312 u32 rxpauseframes;
313 u32 rxcrcerrors;
314 u32 rxaligncodeerrors;
315 u32 rxoversizedframes;
316 u32 rxjabberframes;
317 u32 rxundersizedframes;
318 u32 rxfragments;
319 u32 __pad_0[2];
320 u32 rxoctets;
321 u32 txgoodframes;
322 u32 txbroadcastframes;
323 u32 txmulticastframes;
324 u32 txpauseframes;
325 u32 txdeferredframes;
326 u32 txcollisionframes;
327 u32 txsinglecollframes;
328 u32 txmultcollframes;
329 u32 txexcessivecollisions;
330 u32 txlatecollisions;
331 u32 txunderrun;
332 u32 txcarriersenseerrors;
333 u32 txoctets;
334 u32 octetframes64;
335 u32 octetframes65t127;
336 u32 octetframes128t255;
337 u32 octetframes256t511;
338 u32 octetframes512t1023;
339 u32 octetframes1024tup;
340 u32 netoctets;
341 u32 rxsofoverruns;
342 u32 rxmofoverruns;
343 u32 rxdmaoverruns;
344};
345
Mugunthan V Ndf828592012-03-18 20:17:54 +0000346struct cpsw_slave {
Richard Cochran9750a3a2012-10-29 08:45:15 +0000347 void __iomem *regs;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000348 struct cpsw_sliver_regs __iomem *sliver;
349 int slave_num;
350 u32 mac_control;
351 struct cpsw_slave_data *data;
352 struct phy_device *phy;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000353 struct net_device *ndev;
354 u32 port_vlan;
355 u32 open_stat;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000356};
357
Richard Cochran9750a3a2012-10-29 08:45:15 +0000358static inline u32 slave_read(struct cpsw_slave *slave, u32 offset)
359{
360 return __raw_readl(slave->regs + offset);
361}
362
363static inline void slave_write(struct cpsw_slave *slave, u32 val, u32 offset)
364{
365 __raw_writel(val, slave->regs + offset);
366}
367
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +0200368struct cpsw_vector {
369 struct cpdma_chan *ch;
370 int budget;
371};
372
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +0300373struct cpsw_common {
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +0300374 struct device *dev;
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300375 struct cpsw_platform_data data;
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +0300376 struct napi_struct napi_rx;
377 struct napi_struct napi_tx;
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +0300378 struct cpsw_ss_regs __iomem *regs;
379 struct cpsw_wr_regs __iomem *wr_regs;
380 u8 __iomem *hw_stats;
381 struct cpsw_host_regs __iomem *host_port_regs;
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300382 u32 version;
383 u32 coal_intvl;
384 u32 bus_freq_mhz;
385 int rx_packet_max;
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300386 struct cpsw_slave *slaves;
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +0300387 struct cpdma_ctlr *dma;
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +0200388 struct cpsw_vector txv[CPSW_MAX_QUEUES];
389 struct cpsw_vector rxv[CPSW_MAX_QUEUES];
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300390 struct cpsw_ale *ale;
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +0300391 bool quirk_irq;
392 bool rx_irq_disabled;
393 bool tx_irq_disabled;
394 u32 irqs_table[IRQ_NUM];
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300395 struct cpts *cpts;
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300396 int rx_ch_num, tx_ch_num;
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +0300397};
398
399struct cpsw_priv {
Mugunthan V Ndf828592012-03-18 20:17:54 +0000400 struct net_device *ndev;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000401 struct device *dev;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000402 u32 msg_enable;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000403 u8 mac_addr[ETH_ALEN];
Mugunthan V N1923d6e2014-09-08 22:54:02 +0530404 bool rx_pause;
405 bool tx_pause;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000406 u32 emac_port;
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +0300407 struct cpsw_common *cpsw;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000408};
409
Mugunthan V Nd9718542013-07-23 15:38:17 +0530410struct cpsw_stats {
411 char stat_string[ETH_GSTRING_LEN];
412 int type;
413 int sizeof_stat;
414 int stat_offset;
415};
416
417enum {
418 CPSW_STATS,
419 CPDMA_RX_STATS,
420 CPDMA_TX_STATS,
421};
422
423#define CPSW_STAT(m) CPSW_STATS, \
424 sizeof(((struct cpsw_hw_stats *)0)->m), \
425 offsetof(struct cpsw_hw_stats, m)
426#define CPDMA_RX_STAT(m) CPDMA_RX_STATS, \
427 sizeof(((struct cpdma_chan_stats *)0)->m), \
428 offsetof(struct cpdma_chan_stats, m)
429#define CPDMA_TX_STAT(m) CPDMA_TX_STATS, \
430 sizeof(((struct cpdma_chan_stats *)0)->m), \
431 offsetof(struct cpdma_chan_stats, m)
432
433static const struct cpsw_stats cpsw_gstrings_stats[] = {
434 { "Good Rx Frames", CPSW_STAT(rxgoodframes) },
435 { "Broadcast Rx Frames", CPSW_STAT(rxbroadcastframes) },
436 { "Multicast Rx Frames", CPSW_STAT(rxmulticastframes) },
437 { "Pause Rx Frames", CPSW_STAT(rxpauseframes) },
438 { "Rx CRC Errors", CPSW_STAT(rxcrcerrors) },
439 { "Rx Align/Code Errors", CPSW_STAT(rxaligncodeerrors) },
440 { "Oversize Rx Frames", CPSW_STAT(rxoversizedframes) },
441 { "Rx Jabbers", CPSW_STAT(rxjabberframes) },
442 { "Undersize (Short) Rx Frames", CPSW_STAT(rxundersizedframes) },
443 { "Rx Fragments", CPSW_STAT(rxfragments) },
444 { "Rx Octets", CPSW_STAT(rxoctets) },
445 { "Good Tx Frames", CPSW_STAT(txgoodframes) },
446 { "Broadcast Tx Frames", CPSW_STAT(txbroadcastframes) },
447 { "Multicast Tx Frames", CPSW_STAT(txmulticastframes) },
448 { "Pause Tx Frames", CPSW_STAT(txpauseframes) },
449 { "Deferred Tx Frames", CPSW_STAT(txdeferredframes) },
450 { "Collisions", CPSW_STAT(txcollisionframes) },
451 { "Single Collision Tx Frames", CPSW_STAT(txsinglecollframes) },
452 { "Multiple Collision Tx Frames", CPSW_STAT(txmultcollframes) },
453 { "Excessive Collisions", CPSW_STAT(txexcessivecollisions) },
454 { "Late Collisions", CPSW_STAT(txlatecollisions) },
455 { "Tx Underrun", CPSW_STAT(txunderrun) },
456 { "Carrier Sense Errors", CPSW_STAT(txcarriersenseerrors) },
457 { "Tx Octets", CPSW_STAT(txoctets) },
458 { "Rx + Tx 64 Octet Frames", CPSW_STAT(octetframes64) },
459 { "Rx + Tx 65-127 Octet Frames", CPSW_STAT(octetframes65t127) },
460 { "Rx + Tx 128-255 Octet Frames", CPSW_STAT(octetframes128t255) },
461 { "Rx + Tx 256-511 Octet Frames", CPSW_STAT(octetframes256t511) },
462 { "Rx + Tx 512-1023 Octet Frames", CPSW_STAT(octetframes512t1023) },
463 { "Rx + Tx 1024-Up Octet Frames", CPSW_STAT(octetframes1024tup) },
464 { "Net Octets", CPSW_STAT(netoctets) },
465 { "Rx Start of Frame Overruns", CPSW_STAT(rxsofoverruns) },
466 { "Rx Middle of Frame Overruns", CPSW_STAT(rxmofoverruns) },
467 { "Rx DMA Overruns", CPSW_STAT(rxdmaoverruns) },
Mugunthan V Nd9718542013-07-23 15:38:17 +0530468};
469
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300470static const struct cpsw_stats cpsw_gstrings_ch_stats[] = {
471 { "head_enqueue", CPDMA_RX_STAT(head_enqueue) },
472 { "tail_enqueue", CPDMA_RX_STAT(tail_enqueue) },
473 { "pad_enqueue", CPDMA_RX_STAT(pad_enqueue) },
474 { "misqueued", CPDMA_RX_STAT(misqueued) },
475 { "desc_alloc_fail", CPDMA_RX_STAT(desc_alloc_fail) },
476 { "pad_alloc_fail", CPDMA_RX_STAT(pad_alloc_fail) },
477 { "runt_receive_buf", CPDMA_RX_STAT(runt_receive_buff) },
478 { "runt_transmit_buf", CPDMA_RX_STAT(runt_transmit_buff) },
479 { "empty_dequeue", CPDMA_RX_STAT(empty_dequeue) },
480 { "busy_dequeue", CPDMA_RX_STAT(busy_dequeue) },
481 { "good_dequeue", CPDMA_RX_STAT(good_dequeue) },
482 { "requeue", CPDMA_RX_STAT(requeue) },
483 { "teardown_dequeue", CPDMA_RX_STAT(teardown_dequeue) },
484};
485
486#define CPSW_STATS_COMMON_LEN ARRAY_SIZE(cpsw_gstrings_stats)
487#define CPSW_STATS_CH_LEN ARRAY_SIZE(cpsw_gstrings_ch_stats)
Mugunthan V Nd9718542013-07-23 15:38:17 +0530488
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +0300489#define ndev_to_cpsw(ndev) (((struct cpsw_priv *)netdev_priv(ndev))->cpsw)
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +0300490#define napi_to_cpsw(napi) container_of(napi, struct cpsw_common, napi)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000491#define for_each_slave(priv, func, arg...) \
492 do { \
Sebastian Siewior6e6ceae2013-04-24 08:48:24 +0000493 struct cpsw_slave *slave; \
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300494 struct cpsw_common *cpsw = (priv)->cpsw; \
Sebastian Siewior6e6ceae2013-04-24 08:48:24 +0000495 int n; \
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300496 if (cpsw->data.dual_emac) \
497 (func)((cpsw)->slaves + priv->emac_port, ##arg);\
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000498 else \
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300499 for (n = cpsw->data.slaves, \
500 slave = cpsw->slaves; \
Sebastian Siewior6e6ceae2013-04-24 08:48:24 +0000501 n; n--) \
502 (func)(slave++, ##arg); \
Mugunthan V Ndf828592012-03-18 20:17:54 +0000503 } while (0)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000504
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300505#define cpsw_dual_emac_src_port_detect(cpsw, status, ndev, skb) \
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000506 do { \
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300507 if (!cpsw->data.dual_emac) \
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000508 break; \
509 if (CPDMA_RX_SOURCE_PORT(status) == 1) { \
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300510 ndev = cpsw->slaves[0].ndev; \
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000511 skb->dev = ndev; \
512 } else if (CPDMA_RX_SOURCE_PORT(status) == 2) { \
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300513 ndev = cpsw->slaves[1].ndev; \
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000514 skb->dev = ndev; \
515 } \
516 } while (0)
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300517#define cpsw_add_mcast(cpsw, priv, addr) \
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000518 do { \
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300519 if (cpsw->data.dual_emac) { \
520 struct cpsw_slave *slave = cpsw->slaves + \
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000521 priv->emac_port; \
Ivan Khoronzhuk6f1f5832016-08-10 02:22:34 +0300522 int slave_port = cpsw_get_slave_port( \
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000523 slave->slave_num); \
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300524 cpsw_ale_add_mcast(cpsw->ale, addr, \
Grygorii Strashko71a2cbb2016-04-07 15:16:44 +0300525 1 << slave_port | ALE_PORT_HOST, \
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000526 ALE_VLAN, slave->port_vlan, 0); \
527 } else { \
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300528 cpsw_ale_add_mcast(cpsw->ale, addr, \
Grygorii Strashko61f1cef2016-04-07 15:16:43 +0300529 ALE_ALL_PORTS, \
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000530 0, 0, 0); \
531 } \
532 } while (0)
533
Ivan Khoronzhuk6f1f5832016-08-10 02:22:34 +0300534static inline int cpsw_get_slave_port(u32 slave_num)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000535{
Grygorii Strashko71a2cbb2016-04-07 15:16:44 +0300536 return slave_num + 1;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000537}
Mugunthan V Ndf828592012-03-18 20:17:54 +0000538
Mugunthan V N0cd8f9c2014-01-23 00:03:12 +0530539static void cpsw_set_promiscious(struct net_device *ndev, bool enable)
540{
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300541 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
542 struct cpsw_ale *ale = cpsw->ale;
Mugunthan V N0cd8f9c2014-01-23 00:03:12 +0530543 int i;
544
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300545 if (cpsw->data.dual_emac) {
Mugunthan V N0cd8f9c2014-01-23 00:03:12 +0530546 bool flag = false;
547
548 /* Enabling promiscuous mode for one interface will be
549 * common for both the interface as the interface shares
550 * the same hardware resource.
551 */
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300552 for (i = 0; i < cpsw->data.slaves; i++)
553 if (cpsw->slaves[i].ndev->flags & IFF_PROMISC)
Mugunthan V N0cd8f9c2014-01-23 00:03:12 +0530554 flag = true;
555
556 if (!enable && flag) {
557 enable = true;
558 dev_err(&ndev->dev, "promiscuity not disabled as the other interface is still in promiscuity mode\n");
559 }
560
561 if (enable) {
562 /* Enable Bypass */
563 cpsw_ale_control_set(ale, 0, ALE_BYPASS, 1);
564
565 dev_dbg(&ndev->dev, "promiscuity enabled\n");
566 } else {
567 /* Disable Bypass */
568 cpsw_ale_control_set(ale, 0, ALE_BYPASS, 0);
569 dev_dbg(&ndev->dev, "promiscuity disabled\n");
570 }
571 } else {
572 if (enable) {
573 unsigned long timeout = jiffies + HZ;
574
Lennart Sorensen6f979eb2014-10-31 13:28:54 -0400575 /* Disable Learn for all ports (host is port 0 and slaves are port 1 and up */
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300576 for (i = 0; i <= cpsw->data.slaves; i++) {
Mugunthan V N0cd8f9c2014-01-23 00:03:12 +0530577 cpsw_ale_control_set(ale, i,
578 ALE_PORT_NOLEARN, 1);
579 cpsw_ale_control_set(ale, i,
580 ALE_PORT_NO_SA_UPDATE, 1);
581 }
582
583 /* Clear All Untouched entries */
584 cpsw_ale_control_set(ale, 0, ALE_AGEOUT, 1);
585 do {
586 cpu_relax();
587 if (cpsw_ale_control_get(ale, 0, ALE_AGEOUT))
588 break;
589 } while (time_after(timeout, jiffies));
590 cpsw_ale_control_set(ale, 0, ALE_AGEOUT, 1);
591
592 /* Clear all mcast from ALE */
Grygorii Strashko61f1cef2016-04-07 15:16:43 +0300593 cpsw_ale_flush_multicast(ale, ALE_ALL_PORTS, -1);
Mugunthan V N0cd8f9c2014-01-23 00:03:12 +0530594
595 /* Flood All Unicast Packets to Host port */
596 cpsw_ale_control_set(ale, 0, ALE_P0_UNI_FLOOD, 1);
597 dev_dbg(&ndev->dev, "promiscuity enabled\n");
598 } else {
Lennart Sorensen6f979eb2014-10-31 13:28:54 -0400599 /* Don't Flood All Unicast Packets to Host port */
Mugunthan V N0cd8f9c2014-01-23 00:03:12 +0530600 cpsw_ale_control_set(ale, 0, ALE_P0_UNI_FLOOD, 0);
601
Lennart Sorensen6f979eb2014-10-31 13:28:54 -0400602 /* Enable Learn for all ports (host is port 0 and slaves are port 1 and up */
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300603 for (i = 0; i <= cpsw->data.slaves; i++) {
Mugunthan V N0cd8f9c2014-01-23 00:03:12 +0530604 cpsw_ale_control_set(ale, i,
605 ALE_PORT_NOLEARN, 0);
606 cpsw_ale_control_set(ale, i,
607 ALE_PORT_NO_SA_UPDATE, 0);
608 }
609 dev_dbg(&ndev->dev, "promiscuity disabled\n");
610 }
611 }
612}
613
Mugunthan V N5c50a852012-10-29 08:45:11 +0000614static void cpsw_ndo_set_rx_mode(struct net_device *ndev)
615{
616 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300617 struct cpsw_common *cpsw = priv->cpsw;
Mugunthan V N25906052015-01-13 17:35:49 +0530618 int vid;
619
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300620 if (cpsw->data.dual_emac)
621 vid = cpsw->slaves[priv->emac_port].port_vlan;
Mugunthan V N25906052015-01-13 17:35:49 +0530622 else
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300623 vid = cpsw->data.default_vlan;
Mugunthan V N5c50a852012-10-29 08:45:11 +0000624
625 if (ndev->flags & IFF_PROMISC) {
626 /* Enable promiscuous mode */
Mugunthan V N0cd8f9c2014-01-23 00:03:12 +0530627 cpsw_set_promiscious(ndev, true);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300628 cpsw_ale_set_allmulti(cpsw->ale, IFF_ALLMULTI);
Mugunthan V N5c50a852012-10-29 08:45:11 +0000629 return;
Mugunthan V N0cd8f9c2014-01-23 00:03:12 +0530630 } else {
631 /* Disable promiscuous mode */
632 cpsw_set_promiscious(ndev, false);
Mugunthan V N5c50a852012-10-29 08:45:11 +0000633 }
634
Lennart Sorensen1e5c4bc2014-10-31 13:38:52 -0400635 /* Restore allmulti on vlans if necessary */
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300636 cpsw_ale_set_allmulti(cpsw->ale, priv->ndev->flags & IFF_ALLMULTI);
Lennart Sorensen1e5c4bc2014-10-31 13:38:52 -0400637
Mugunthan V N5c50a852012-10-29 08:45:11 +0000638 /* Clear all mcast from ALE */
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300639 cpsw_ale_flush_multicast(cpsw->ale, ALE_ALL_PORTS, vid);
Mugunthan V N5c50a852012-10-29 08:45:11 +0000640
641 if (!netdev_mc_empty(ndev)) {
642 struct netdev_hw_addr *ha;
643
644 /* program multicast address list into ALE register */
645 netdev_for_each_mc_addr(ha, ndev) {
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300646 cpsw_add_mcast(cpsw, priv, (u8 *)ha->addr);
Mugunthan V N5c50a852012-10-29 08:45:11 +0000647 }
648 }
649}
650
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +0300651static void cpsw_intr_enable(struct cpsw_common *cpsw)
Mugunthan V Ndf828592012-03-18 20:17:54 +0000652{
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +0300653 __raw_writel(0xFF, &cpsw->wr_regs->tx_en);
654 __raw_writel(0xFF, &cpsw->wr_regs->rx_en);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000655
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +0300656 cpdma_ctlr_int_ctrl(cpsw->dma, true);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000657 return;
658}
659
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +0300660static void cpsw_intr_disable(struct cpsw_common *cpsw)
Mugunthan V Ndf828592012-03-18 20:17:54 +0000661{
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +0300662 __raw_writel(0, &cpsw->wr_regs->tx_en);
663 __raw_writel(0, &cpsw->wr_regs->rx_en);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000664
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +0300665 cpdma_ctlr_int_ctrl(cpsw->dma, false);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000666 return;
667}
668
Olof Johansson1a3b5052013-12-11 15:58:07 -0800669static void cpsw_tx_handler(void *token, int len, int status)
Mugunthan V Ndf828592012-03-18 20:17:54 +0000670{
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300671 struct netdev_queue *txq;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000672 struct sk_buff *skb = token;
673 struct net_device *ndev = skb->dev;
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300674 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000675
Mugunthan V Nfae50822013-01-17 06:31:34 +0000676 /* Check whether the queue is stopped due to stalled tx dma, if the
677 * queue is stopped then start the queue as we have free desc for tx
678 */
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300679 txq = netdev_get_tx_queue(ndev, skb_get_queue_mapping(skb));
680 if (unlikely(netif_tx_queue_stopped(txq)))
681 netif_tx_wake_queue(txq);
682
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300683 cpts_tx_timestamp(cpsw->cpts, skb);
Tobias Klauser8dc43dd2014-03-10 13:12:23 +0100684 ndev->stats.tx_packets++;
685 ndev->stats.tx_bytes += len;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000686 dev_kfree_skb_any(skb);
687}
688
Olof Johansson1a3b5052013-12-11 15:58:07 -0800689static void cpsw_rx_handler(void *token, int len, int status)
Mugunthan V Ndf828592012-03-18 20:17:54 +0000690{
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300691 struct cpdma_chan *ch;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000692 struct sk_buff *skb = token;
Sebastian Siewiorb4727e62013-04-23 07:31:39 +0000693 struct sk_buff *new_skb;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000694 struct net_device *ndev = skb->dev;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000695 int ret = 0;
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300696 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000697
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300698 cpsw_dual_emac_src_port_detect(cpsw, status, ndev, skb);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000699
Mugunthan V N16e5c572014-04-10 14:23:23 +0530700 if (unlikely(status < 0) || unlikely(!netif_running(ndev))) {
Mugunthan V Na0e2c822014-09-10 16:38:09 +0530701 bool ndev_status = false;
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300702 struct cpsw_slave *slave = cpsw->slaves;
Mugunthan V Na0e2c822014-09-10 16:38:09 +0530703 int n;
704
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300705 if (cpsw->data.dual_emac) {
Mugunthan V Na0e2c822014-09-10 16:38:09 +0530706 /* In dual emac mode check for all interfaces */
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300707 for (n = cpsw->data.slaves; n; n--, slave++)
Mugunthan V Na0e2c822014-09-10 16:38:09 +0530708 if (netif_running(slave->ndev))
709 ndev_status = true;
710 }
711
712 if (ndev_status && (status >= 0)) {
713 /* The packet received is for the interface which
714 * is already down and the other interface is up
Joe Perchesdbedd442015-03-06 20:49:12 -0800715 * and running, instead of freeing which results
Mugunthan V Na0e2c822014-09-10 16:38:09 +0530716 * in reducing of the number of rx descriptor in
717 * DMA engine, requeue skb back to cpdma.
718 */
719 new_skb = skb;
720 goto requeue;
721 }
722
Sebastian Siewiorb4727e62013-04-23 07:31:39 +0000723 /* the interface is going down, skbs are purged */
Mugunthan V Ndf828592012-03-18 20:17:54 +0000724 dev_kfree_skb_any(skb);
725 return;
726 }
Sebastian Siewiorb4727e62013-04-23 07:31:39 +0000727
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300728 new_skb = netdev_alloc_skb_ip_align(ndev, cpsw->rx_packet_max);
Sebastian Siewiorb4727e62013-04-23 07:31:39 +0000729 if (new_skb) {
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300730 skb_copy_queue_mapping(new_skb, skb);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000731 skb_put(skb, len);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300732 cpts_rx_timestamp(cpsw->cpts, skb);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000733 skb->protocol = eth_type_trans(skb, ndev);
734 netif_receive_skb(skb);
Tobias Klauser8dc43dd2014-03-10 13:12:23 +0100735 ndev->stats.rx_bytes += len;
736 ndev->stats.rx_packets++;
Grygorii Strashko254a49d2016-08-09 15:09:44 +0300737 kmemleak_not_leak(new_skb);
Sebastian Siewiorb4727e62013-04-23 07:31:39 +0000738 } else {
Tobias Klauser8dc43dd2014-03-10 13:12:23 +0100739 ndev->stats.rx_dropped++;
Sebastian Siewiorb4727e62013-04-23 07:31:39 +0000740 new_skb = skb;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000741 }
742
Mugunthan V Na0e2c822014-09-10 16:38:09 +0530743requeue:
Ivan Khoronzhukce52c742016-08-22 21:18:28 +0300744 if (netif_dormant(ndev)) {
745 dev_kfree_skb_any(new_skb);
746 return;
747 }
748
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +0200749 ch = cpsw->rxv[skb_get_queue_mapping(new_skb)].ch;
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300750 ret = cpdma_chan_submit(ch, new_skb, new_skb->data,
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +0300751 skb_tailroom(new_skb), 0);
Sebastian Siewiorb4727e62013-04-23 07:31:39 +0000752 if (WARN_ON(ret < 0))
753 dev_kfree_skb_any(new_skb);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000754}
755
Ivan Khoronzhuk48e0a832016-12-06 03:45:00 +0200756/* split budget depending on channel rates */
757static void cpsw_split_budget(struct net_device *ndev)
758{
759 struct cpsw_priv *priv = netdev_priv(ndev);
760 struct cpsw_common *cpsw = priv->cpsw;
761 struct cpsw_vector *txv = cpsw->txv;
762 u32 consumed_rate, bigest_rate = 0;
763 int budget, bigest_rate_ch = 0;
764 struct cpsw_slave *slave;
765 int i, rlim_ch_num = 0;
766 u32 ch_rate, max_rate;
767 int ch_budget = 0;
768
769 if (cpsw->data.dual_emac)
770 slave = &cpsw->slaves[priv->emac_port];
771 else
772 slave = &cpsw->slaves[cpsw->data.active_slave];
773
774 max_rate = slave->phy->speed * 1000;
775
776 consumed_rate = 0;
777 for (i = 0; i < cpsw->tx_ch_num; i++) {
778 ch_rate = cpdma_chan_get_rate(txv[i].ch);
779 if (!ch_rate)
780 continue;
781
782 rlim_ch_num++;
783 consumed_rate += ch_rate;
784 }
785
786 if (cpsw->tx_ch_num == rlim_ch_num) {
787 max_rate = consumed_rate;
788 } else {
789 ch_budget = (consumed_rate * CPSW_POLL_WEIGHT) / max_rate;
790 ch_budget = (CPSW_POLL_WEIGHT - ch_budget) /
791 (cpsw->tx_ch_num - rlim_ch_num);
792 bigest_rate = (max_rate - consumed_rate) /
793 (cpsw->tx_ch_num - rlim_ch_num);
794 }
795
796 /* split tx budget */
797 budget = CPSW_POLL_WEIGHT;
798 for (i = 0; i < cpsw->tx_ch_num; i++) {
799 ch_rate = cpdma_chan_get_rate(txv[i].ch);
800 if (ch_rate) {
801 txv[i].budget = (ch_rate * CPSW_POLL_WEIGHT) / max_rate;
802 if (!txv[i].budget)
803 txv[i].budget = 1;
804 if (ch_rate > bigest_rate) {
805 bigest_rate_ch = i;
806 bigest_rate = ch_rate;
807 }
808 } else {
809 txv[i].budget = ch_budget;
810 if (!bigest_rate_ch)
811 bigest_rate_ch = i;
812 }
813
814 budget -= txv[i].budget;
815 }
816
817 if (budget)
818 txv[bigest_rate_ch].budget += budget;
819
820 /* split rx budget */
821 budget = CPSW_POLL_WEIGHT;
822 ch_budget = budget / cpsw->rx_ch_num;
823 for (i = 0; i < cpsw->rx_ch_num; i++) {
824 cpsw->rxv[i].budget = ch_budget;
825 budget -= ch_budget;
826 }
827
828 if (budget)
829 cpsw->rxv[0].budget += budget;
830}
831
Felipe Balbic03abd82015-01-16 10:11:12 -0600832static irqreturn_t cpsw_tx_interrupt(int irq, void *dev_id)
Mugunthan V Ndf828592012-03-18 20:17:54 +0000833{
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +0300834 struct cpsw_common *cpsw = dev_id;
Felipe Balbi7ce67a32015-01-02 16:15:59 -0600835
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +0300836 writel(0, &cpsw->wr_regs->tx_en);
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +0300837 cpdma_ctlr_eoi(cpsw->dma, CPDMA_EOI_TX);
Felipe Balbic03abd82015-01-16 10:11:12 -0600838
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +0300839 if (cpsw->quirk_irq) {
840 disable_irq_nosync(cpsw->irqs_table[1]);
841 cpsw->tx_irq_disabled = true;
Mugunthan V N7da11602015-08-12 15:22:53 +0530842 }
843
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +0300844 napi_schedule(&cpsw->napi_tx);
Felipe Balbic03abd82015-01-16 10:11:12 -0600845 return IRQ_HANDLED;
846}
847
848static irqreturn_t cpsw_rx_interrupt(int irq, void *dev_id)
849{
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +0300850 struct cpsw_common *cpsw = dev_id;
Felipe Balbic03abd82015-01-16 10:11:12 -0600851
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +0300852 cpdma_ctlr_eoi(cpsw->dma, CPDMA_EOI_RX);
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +0300853 writel(0, &cpsw->wr_regs->rx_en);
Sebastian Siewiorfd51cf12013-04-23 07:31:37 +0000854
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +0300855 if (cpsw->quirk_irq) {
856 disable_irq_nosync(cpsw->irqs_table[0]);
857 cpsw->rx_irq_disabled = true;
Mugunthan V N7da11602015-08-12 15:22:53 +0530858 }
859
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +0300860 napi_schedule(&cpsw->napi_rx);
Mugunthan V Nd354eb82015-08-04 16:06:19 +0530861 return IRQ_HANDLED;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000862}
863
Mugunthan V N32a74322015-08-04 16:06:20 +0530864static int cpsw_tx_poll(struct napi_struct *napi_tx, int budget)
Mugunthan V Ndf828592012-03-18 20:17:54 +0000865{
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300866 u32 ch_map;
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +0200867 int num_tx, cur_budget, ch;
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +0300868 struct cpsw_common *cpsw = napi_to_cpsw(napi_tx);
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +0200869 struct cpsw_vector *txv;
Mugunthan V N32a74322015-08-04 16:06:20 +0530870
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300871 /* process every unprocessed channel */
872 ch_map = cpdma_ctrl_txchs_state(cpsw->dma);
Ivan Khoronzhuk342934a2016-11-29 17:00:50 +0200873 for (ch = 0, num_tx = 0; ch_map; ch_map >>= 1, ch++) {
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300874 if (!(ch_map & 0x01))
875 continue;
876
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +0200877 txv = &cpsw->txv[ch];
878 if (unlikely(txv->budget > budget - num_tx))
879 cur_budget = budget - num_tx;
880 else
881 cur_budget = txv->budget;
882
883 num_tx += cpdma_chan_process(txv->ch, cur_budget);
Ivan Khoronzhuk342934a2016-11-29 17:00:50 +0200884 if (num_tx >= budget)
885 break;
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300886 }
887
Mugunthan V N32a74322015-08-04 16:06:20 +0530888 if (num_tx < budget) {
889 napi_complete(napi_tx);
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +0300890 writel(0xff, &cpsw->wr_regs->tx_en);
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +0300891 if (cpsw->quirk_irq && cpsw->tx_irq_disabled) {
892 cpsw->tx_irq_disabled = false;
893 enable_irq(cpsw->irqs_table[1]);
Mugunthan V N7da11602015-08-12 15:22:53 +0530894 }
Mugunthan V N32a74322015-08-04 16:06:20 +0530895 }
896
Mugunthan V N32a74322015-08-04 16:06:20 +0530897 return num_tx;
898}
899
900static int cpsw_rx_poll(struct napi_struct *napi_rx, int budget)
901{
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300902 u32 ch_map;
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +0200903 int num_rx, cur_budget, ch;
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +0300904 struct cpsw_common *cpsw = napi_to_cpsw(napi_rx);
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +0200905 struct cpsw_vector *rxv;
Mugunthan V N510a1e722013-02-17 22:19:20 +0000906
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300907 /* process every unprocessed channel */
908 ch_map = cpdma_ctrl_rxchs_state(cpsw->dma);
Ivan Khoronzhuk342934a2016-11-29 17:00:50 +0200909 for (ch = 0, num_rx = 0; ch_map; ch_map >>= 1, ch++) {
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300910 if (!(ch_map & 0x01))
911 continue;
912
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +0200913 rxv = &cpsw->rxv[ch];
914 if (unlikely(rxv->budget > budget - num_rx))
915 cur_budget = budget - num_rx;
916 else
917 cur_budget = rxv->budget;
918
919 num_rx += cpdma_chan_process(rxv->ch, cur_budget);
Ivan Khoronzhuk342934a2016-11-29 17:00:50 +0200920 if (num_rx >= budget)
921 break;
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300922 }
923
Mugunthan V N510a1e722013-02-17 22:19:20 +0000924 if (num_rx < budget) {
Mugunthan V N32a74322015-08-04 16:06:20 +0530925 napi_complete(napi_rx);
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +0300926 writel(0xff, &cpsw->wr_regs->rx_en);
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +0300927 if (cpsw->quirk_irq && cpsw->rx_irq_disabled) {
928 cpsw->rx_irq_disabled = false;
929 enable_irq(cpsw->irqs_table[0]);
Mugunthan V N7da11602015-08-12 15:22:53 +0530930 }
Mugunthan V N510a1e722013-02-17 22:19:20 +0000931 }
Mugunthan V Ndf828592012-03-18 20:17:54 +0000932
Mugunthan V Ndf828592012-03-18 20:17:54 +0000933 return num_rx;
934}
935
936static inline void soft_reset(const char *module, void __iomem *reg)
937{
938 unsigned long timeout = jiffies + HZ;
939
940 __raw_writel(1, reg);
941 do {
942 cpu_relax();
943 } while ((__raw_readl(reg) & 1) && time_after(timeout, jiffies));
944
945 WARN(__raw_readl(reg) & 1, "failed to soft-reset %s\n", module);
946}
947
948#define mac_hi(mac) (((mac)[0] << 0) | ((mac)[1] << 8) | \
949 ((mac)[2] << 16) | ((mac)[3] << 24))
950#define mac_lo(mac) (((mac)[4] << 0) | ((mac)[5] << 8))
951
952static void cpsw_set_slave_mac(struct cpsw_slave *slave,
953 struct cpsw_priv *priv)
954{
Richard Cochran9750a3a2012-10-29 08:45:15 +0000955 slave_write(slave, mac_hi(priv->mac_addr), SA_HI);
956 slave_write(slave, mac_lo(priv->mac_addr), SA_LO);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000957}
958
959static void _cpsw_adjust_link(struct cpsw_slave *slave,
960 struct cpsw_priv *priv, bool *link)
961{
962 struct phy_device *phy = slave->phy;
963 u32 mac_control = 0;
964 u32 slave_port;
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300965 struct cpsw_common *cpsw = priv->cpsw;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000966
967 if (!phy)
968 return;
969
Ivan Khoronzhuk6f1f5832016-08-10 02:22:34 +0300970 slave_port = cpsw_get_slave_port(slave->slave_num);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000971
972 if (phy->link) {
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300973 mac_control = cpsw->data.mac_control;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000974
975 /* enable forwarding */
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300976 cpsw_ale_control_set(cpsw->ale, slave_port,
Mugunthan V Ndf828592012-03-18 20:17:54 +0000977 ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
978
979 if (phy->speed == 1000)
980 mac_control |= BIT(7); /* GIGABITEN */
981 if (phy->duplex)
982 mac_control |= BIT(0); /* FULLDUPLEXEN */
Daniel Mack342b7b72012-09-27 09:19:34 +0000983
984 /* set speed_in input in case RMII mode is used in 100Mbps */
985 if (phy->speed == 100)
986 mac_control |= BIT(15);
Mugunthan V Na81d8762013-12-13 18:42:55 +0530987 else if (phy->speed == 10)
988 mac_control |= BIT(18); /* In Band mode */
Daniel Mack342b7b72012-09-27 09:19:34 +0000989
Mugunthan V N1923d6e2014-09-08 22:54:02 +0530990 if (priv->rx_pause)
991 mac_control |= BIT(3);
992
993 if (priv->tx_pause)
994 mac_control |= BIT(4);
995
Mugunthan V Ndf828592012-03-18 20:17:54 +0000996 *link = true;
997 } else {
998 mac_control = 0;
999 /* disable forwarding */
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001000 cpsw_ale_control_set(cpsw->ale, slave_port,
Mugunthan V Ndf828592012-03-18 20:17:54 +00001001 ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
1002 }
1003
1004 if (mac_control != slave->mac_control) {
1005 phy_print_status(phy);
1006 __raw_writel(mac_control, &slave->sliver->mac_control);
1007 }
1008
1009 slave->mac_control = mac_control;
1010}
1011
1012static void cpsw_adjust_link(struct net_device *ndev)
1013{
1014 struct cpsw_priv *priv = netdev_priv(ndev);
1015 bool link = false;
1016
1017 for_each_slave(priv, _cpsw_adjust_link, priv, &link);
1018
1019 if (link) {
Ivan Khoronzhuk48e0a832016-12-06 03:45:00 +02001020 cpsw_split_budget(priv->ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001021 netif_carrier_on(ndev);
1022 if (netif_running(ndev))
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001023 netif_tx_wake_all_queues(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001024 } else {
1025 netif_carrier_off(ndev);
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001026 netif_tx_stop_all_queues(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001027 }
1028}
1029
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001030static int cpsw_get_coalesce(struct net_device *ndev,
1031 struct ethtool_coalesce *coal)
1032{
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001033 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001034
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001035 coal->rx_coalesce_usecs = cpsw->coal_intvl;
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001036 return 0;
1037}
1038
1039static int cpsw_set_coalesce(struct net_device *ndev,
1040 struct ethtool_coalesce *coal)
1041{
1042 struct cpsw_priv *priv = netdev_priv(ndev);
1043 u32 int_ctrl;
1044 u32 num_interrupts = 0;
1045 u32 prescale = 0;
1046 u32 addnl_dvdr = 1;
1047 u32 coal_intvl = 0;
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001048 struct cpsw_common *cpsw = priv->cpsw;
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001049
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001050 coal_intvl = coal->rx_coalesce_usecs;
1051
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001052 int_ctrl = readl(&cpsw->wr_regs->int_control);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001053 prescale = cpsw->bus_freq_mhz * 4;
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001054
Mugunthan V Na84bc2a2014-07-15 20:26:53 +05301055 if (!coal->rx_coalesce_usecs) {
1056 int_ctrl &= ~(CPSW_INTPRESCALE_MASK | CPSW_INTPACEEN);
1057 goto update_return;
1058 }
1059
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001060 if (coal_intvl < CPSW_CMINTMIN_INTVL)
1061 coal_intvl = CPSW_CMINTMIN_INTVL;
1062
1063 if (coal_intvl > CPSW_CMINTMAX_INTVL) {
1064 /* Interrupt pacer works with 4us Pulse, we can
1065 * throttle further by dilating the 4us pulse.
1066 */
1067 addnl_dvdr = CPSW_INTPRESCALE_MASK / prescale;
1068
1069 if (addnl_dvdr > 1) {
1070 prescale *= addnl_dvdr;
1071 if (coal_intvl > (CPSW_CMINTMAX_INTVL * addnl_dvdr))
1072 coal_intvl = (CPSW_CMINTMAX_INTVL
1073 * addnl_dvdr);
1074 } else {
1075 addnl_dvdr = 1;
1076 coal_intvl = CPSW_CMINTMAX_INTVL;
1077 }
1078 }
1079
1080 num_interrupts = (1000 * addnl_dvdr) / coal_intvl;
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001081 writel(num_interrupts, &cpsw->wr_regs->rx_imax);
1082 writel(num_interrupts, &cpsw->wr_regs->tx_imax);
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001083
1084 int_ctrl |= CPSW_INTPACEEN;
1085 int_ctrl &= (~CPSW_INTPRESCALE_MASK);
1086 int_ctrl |= (prescale & CPSW_INTPRESCALE_MASK);
Mugunthan V Na84bc2a2014-07-15 20:26:53 +05301087
1088update_return:
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001089 writel(int_ctrl, &cpsw->wr_regs->int_control);
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001090
1091 cpsw_notice(priv, timer, "Set coalesce to %d usecs.\n", coal_intvl);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001092 cpsw->coal_intvl = coal_intvl;
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001093
1094 return 0;
1095}
1096
Mugunthan V Nd9718542013-07-23 15:38:17 +05301097static int cpsw_get_sset_count(struct net_device *ndev, int sset)
1098{
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001099 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1100
Mugunthan V Nd9718542013-07-23 15:38:17 +05301101 switch (sset) {
1102 case ETH_SS_STATS:
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001103 return (CPSW_STATS_COMMON_LEN +
1104 (cpsw->rx_ch_num + cpsw->tx_ch_num) *
1105 CPSW_STATS_CH_LEN);
Mugunthan V Nd9718542013-07-23 15:38:17 +05301106 default:
1107 return -EOPNOTSUPP;
1108 }
1109}
1110
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001111static void cpsw_add_ch_strings(u8 **p, int ch_num, int rx_dir)
1112{
1113 int ch_stats_len;
1114 int line;
1115 int i;
1116
1117 ch_stats_len = CPSW_STATS_CH_LEN * ch_num;
1118 for (i = 0; i < ch_stats_len; i++) {
1119 line = i % CPSW_STATS_CH_LEN;
1120 snprintf(*p, ETH_GSTRING_LEN,
1121 "%s DMA chan %d: %s", rx_dir ? "Rx" : "Tx",
1122 i / CPSW_STATS_CH_LEN,
1123 cpsw_gstrings_ch_stats[line].stat_string);
1124 *p += ETH_GSTRING_LEN;
1125 }
1126}
1127
Mugunthan V Nd9718542013-07-23 15:38:17 +05301128static void cpsw_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
1129{
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001130 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Mugunthan V Nd9718542013-07-23 15:38:17 +05301131 u8 *p = data;
1132 int i;
1133
1134 switch (stringset) {
1135 case ETH_SS_STATS:
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001136 for (i = 0; i < CPSW_STATS_COMMON_LEN; i++) {
Mugunthan V Nd9718542013-07-23 15:38:17 +05301137 memcpy(p, cpsw_gstrings_stats[i].stat_string,
1138 ETH_GSTRING_LEN);
1139 p += ETH_GSTRING_LEN;
1140 }
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001141
1142 cpsw_add_ch_strings(&p, cpsw->rx_ch_num, 1);
1143 cpsw_add_ch_strings(&p, cpsw->tx_ch_num, 0);
Mugunthan V Nd9718542013-07-23 15:38:17 +05301144 break;
1145 }
1146}
1147
1148static void cpsw_get_ethtool_stats(struct net_device *ndev,
1149 struct ethtool_stats *stats, u64 *data)
1150{
Mugunthan V Nd9718542013-07-23 15:38:17 +05301151 u8 *p;
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001152 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001153 struct cpdma_chan_stats ch_stats;
1154 int i, l, ch;
Mugunthan V Nd9718542013-07-23 15:38:17 +05301155
1156 /* Collect Davinci CPDMA stats for Rx and Tx Channel */
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001157 for (l = 0; l < CPSW_STATS_COMMON_LEN; l++)
1158 data[l] = readl(cpsw->hw_stats +
1159 cpsw_gstrings_stats[l].stat_offset);
Mugunthan V Nd9718542013-07-23 15:38:17 +05301160
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001161 for (ch = 0; ch < cpsw->rx_ch_num; ch++) {
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02001162 cpdma_chan_get_stats(cpsw->rxv[ch].ch, &ch_stats);
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001163 for (i = 0; i < CPSW_STATS_CH_LEN; i++, l++) {
1164 p = (u8 *)&ch_stats +
1165 cpsw_gstrings_ch_stats[i].stat_offset;
1166 data[l] = *(u32 *)p;
1167 }
1168 }
Mugunthan V Nd9718542013-07-23 15:38:17 +05301169
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001170 for (ch = 0; ch < cpsw->tx_ch_num; ch++) {
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02001171 cpdma_chan_get_stats(cpsw->txv[ch].ch, &ch_stats);
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001172 for (i = 0; i < CPSW_STATS_CH_LEN; i++, l++) {
1173 p = (u8 *)&ch_stats +
1174 cpsw_gstrings_ch_stats[i].stat_offset;
1175 data[l] = *(u32 *)p;
Mugunthan V Nd9718542013-07-23 15:38:17 +05301176 }
1177 }
1178}
1179
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001180static int cpsw_common_res_usage_state(struct cpsw_common *cpsw)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001181{
1182 u32 i;
1183 u32 usage_count = 0;
1184
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001185 if (!cpsw->data.dual_emac)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001186 return 0;
1187
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001188 for (i = 0; i < cpsw->data.slaves; i++)
1189 if (cpsw->slaves[i].open_stat)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001190 usage_count++;
1191
1192 return usage_count;
1193}
1194
Ivan Khoronzhuk27e9e102016-08-10 02:22:32 +03001195static inline int cpsw_tx_packet_submit(struct cpsw_priv *priv,
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001196 struct sk_buff *skb,
1197 struct cpdma_chan *txch)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001198{
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001199 struct cpsw_common *cpsw = priv->cpsw;
1200
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001201 return cpdma_chan_submit(txch, skb, skb->data, skb->len,
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001202 priv->emac_port + cpsw->data.dual_emac);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001203}
1204
1205static inline void cpsw_add_dual_emac_def_ale_entries(
1206 struct cpsw_priv *priv, struct cpsw_slave *slave,
1207 u32 slave_port)
1208{
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001209 struct cpsw_common *cpsw = priv->cpsw;
Grygorii Strashko71a2cbb2016-04-07 15:16:44 +03001210 u32 port_mask = 1 << slave_port | ALE_PORT_HOST;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001211
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001212 if (cpsw->version == CPSW_VERSION_1)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001213 slave_write(slave, slave->port_vlan, CPSW1_PORT_VLAN);
1214 else
1215 slave_write(slave, slave->port_vlan, CPSW2_PORT_VLAN);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001216 cpsw_ale_add_vlan(cpsw->ale, slave->port_vlan, port_mask,
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001217 port_mask, port_mask, 0);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001218 cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001219 port_mask, ALE_VLAN, slave->port_vlan, 0);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001220 cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr,
1221 HOST_PORT_NUM, ALE_VLAN |
1222 ALE_SECURE, slave->port_vlan);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001223}
1224
Daniel Mack1e7a2e22013-11-15 08:29:16 +01001225static void soft_reset_slave(struct cpsw_slave *slave)
Mugunthan V Ndf828592012-03-18 20:17:54 +00001226{
1227 char name[32];
Daniel Mack1e7a2e22013-11-15 08:29:16 +01001228
1229 snprintf(name, sizeof(name), "slave-%d", slave->slave_num);
1230 soft_reset(name, &slave->sliver->soft_reset);
1231}
1232
1233static void cpsw_slave_open(struct cpsw_slave *slave, struct cpsw_priv *priv)
1234{
Mugunthan V Ndf828592012-03-18 20:17:54 +00001235 u32 slave_port;
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03001236 struct cpsw_common *cpsw = priv->cpsw;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001237
Daniel Mack1e7a2e22013-11-15 08:29:16 +01001238 soft_reset_slave(slave);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001239
1240 /* setup priority mapping */
1241 __raw_writel(RX_PRIORITY_MAPPING, &slave->sliver->rx_pri_map);
Richard Cochran9750a3a2012-10-29 08:45:15 +00001242
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001243 switch (cpsw->version) {
Richard Cochran9750a3a2012-10-29 08:45:15 +00001244 case CPSW_VERSION_1:
1245 slave_write(slave, TX_PRIORITY_MAPPING, CPSW1_TX_PRI_MAP);
1246 break;
1247 case CPSW_VERSION_2:
Mugunthan V Nc193f362013-08-05 17:30:05 +05301248 case CPSW_VERSION_3:
Mugunthan V N926489b2013-08-12 17:11:15 +05301249 case CPSW_VERSION_4:
Richard Cochran9750a3a2012-10-29 08:45:15 +00001250 slave_write(slave, TX_PRIORITY_MAPPING, CPSW2_TX_PRI_MAP);
1251 break;
1252 }
Mugunthan V Ndf828592012-03-18 20:17:54 +00001253
1254 /* setup max packet size, and mac address */
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001255 __raw_writel(cpsw->rx_packet_max, &slave->sliver->rx_maxlen);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001256 cpsw_set_slave_mac(slave, priv);
1257
1258 slave->mac_control = 0; /* no link yet */
1259
Ivan Khoronzhuk6f1f5832016-08-10 02:22:34 +03001260 slave_port = cpsw_get_slave_port(slave->slave_num);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001261
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001262 if (cpsw->data.dual_emac)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001263 cpsw_add_dual_emac_def_ale_entries(priv, slave, slave_port);
1264 else
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001265 cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001266 1 << slave_port, 0, 0, ALE_MCAST_FWD_2);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001267
David Rivshind733f7542016-04-27 21:32:31 -04001268 if (slave->data->phy_node) {
David Rivshin552165b2016-04-27 21:25:25 -04001269 slave->phy = of_phy_connect(priv->ndev, slave->data->phy_node,
Heiko Schocher9e42f712015-10-17 06:04:35 +02001270 &cpsw_adjust_link, 0, slave->data->phy_if);
David Rivshind733f7542016-04-27 21:32:31 -04001271 if (!slave->phy) {
1272 dev_err(priv->dev, "phy \"%s\" not found on slave %d\n",
1273 slave->data->phy_node->full_name,
1274 slave->slave_num);
1275 return;
1276 }
1277 } else {
Heiko Schocher9e42f712015-10-17 06:04:35 +02001278 slave->phy = phy_connect(priv->ndev, slave->data->phy_id,
Florian Fainellif9a8f832013-01-14 00:52:52 +00001279 &cpsw_adjust_link, slave->data->phy_if);
David Rivshind733f7542016-04-27 21:32:31 -04001280 if (IS_ERR(slave->phy)) {
1281 dev_err(priv->dev,
1282 "phy \"%s\" not found on slave %d, err %ld\n",
1283 slave->data->phy_id, slave->slave_num,
1284 PTR_ERR(slave->phy));
1285 slave->phy = NULL;
1286 return;
1287 }
Mugunthan V Ndf828592012-03-18 20:17:54 +00001288 }
David Rivshind733f7542016-04-27 21:32:31 -04001289
1290 phy_attached_info(slave->phy);
1291
1292 phy_start(slave->phy);
1293
1294 /* Configure GMII_SEL register */
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001295 cpsw_phy_sel(cpsw->dev, slave->phy->interface, slave->slave_num);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001296}
1297
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001298static inline void cpsw_add_default_vlan(struct cpsw_priv *priv)
1299{
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001300 struct cpsw_common *cpsw = priv->cpsw;
1301 const int vlan = cpsw->data.default_vlan;
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001302 u32 reg;
1303 int i;
Lennart Sorensen1e5c4bc2014-10-31 13:38:52 -04001304 int unreg_mcast_mask;
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001305
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001306 reg = (cpsw->version == CPSW_VERSION_1) ? CPSW1_PORT_VLAN :
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001307 CPSW2_PORT_VLAN;
1308
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001309 writel(vlan, &cpsw->host_port_regs->port_vlan);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001310
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001311 for (i = 0; i < cpsw->data.slaves; i++)
1312 slave_write(cpsw->slaves + i, vlan, reg);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001313
Lennart Sorensen1e5c4bc2014-10-31 13:38:52 -04001314 if (priv->ndev->flags & IFF_ALLMULTI)
1315 unreg_mcast_mask = ALE_ALL_PORTS;
1316 else
1317 unreg_mcast_mask = ALE_PORT_1 | ALE_PORT_2;
1318
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001319 cpsw_ale_add_vlan(cpsw->ale, vlan, ALE_ALL_PORTS,
Grygorii Strashko61f1cef2016-04-07 15:16:43 +03001320 ALE_ALL_PORTS, ALE_ALL_PORTS,
1321 unreg_mcast_mask);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001322}
1323
Mugunthan V Ndf828592012-03-18 20:17:54 +00001324static void cpsw_init_host_port(struct cpsw_priv *priv)
1325{
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001326 u32 fifo_mode;
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001327 u32 control_reg;
1328 struct cpsw_common *cpsw = priv->cpsw;
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001329
Mugunthan V Ndf828592012-03-18 20:17:54 +00001330 /* soft reset the controller and initialize ale */
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001331 soft_reset("cpsw", &cpsw->regs->soft_reset);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001332 cpsw_ale_start(cpsw->ale);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001333
1334 /* switch to vlan unaware mode */
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001335 cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_VLAN_AWARE,
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001336 CPSW_ALE_VLAN_AWARE);
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001337 control_reg = readl(&cpsw->regs->control);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001338 control_reg |= CPSW_VLAN_AWARE;
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001339 writel(control_reg, &cpsw->regs->control);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001340 fifo_mode = (cpsw->data.dual_emac) ? CPSW_FIFO_DUAL_MAC_MODE :
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001341 CPSW_FIFO_NORMAL_MODE;
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001342 writel(fifo_mode, &cpsw->host_port_regs->tx_in_ctl);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001343
1344 /* setup host port priority mapping */
1345 __raw_writel(CPDMA_TX_PRIORITY_MAP,
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001346 &cpsw->host_port_regs->cpdma_tx_pri_map);
1347 __raw_writel(0, &cpsw->host_port_regs->cpdma_rx_chan_map);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001348
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001349 cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM,
Mugunthan V Ndf828592012-03-18 20:17:54 +00001350 ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
1351
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001352 if (!cpsw->data.dual_emac) {
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001353 cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr, HOST_PORT_NUM,
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001354 0, 0);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001355 cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
Grygorii Strashko71a2cbb2016-04-07 15:16:44 +03001356 ALE_PORT_HOST, 0, 0, ALE_MCAST_FWD_2);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001357 }
Mugunthan V Ndf828592012-03-18 20:17:54 +00001358}
1359
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +03001360static int cpsw_fill_rx_channels(struct cpsw_priv *priv)
1361{
1362 struct cpsw_common *cpsw = priv->cpsw;
1363 struct sk_buff *skb;
1364 int ch_buf_num;
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001365 int ch, i, ret;
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +03001366
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001367 for (ch = 0; ch < cpsw->rx_ch_num; ch++) {
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02001368 ch_buf_num = cpdma_chan_get_rx_buf_num(cpsw->rxv[ch].ch);
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001369 for (i = 0; i < ch_buf_num; i++) {
1370 skb = __netdev_alloc_skb_ip_align(priv->ndev,
1371 cpsw->rx_packet_max,
1372 GFP_KERNEL);
1373 if (!skb) {
1374 cpsw_err(priv, ifup, "cannot allocate skb\n");
1375 return -ENOMEM;
1376 }
1377
1378 skb_set_queue_mapping(skb, ch);
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02001379 ret = cpdma_chan_submit(cpsw->rxv[ch].ch, skb,
1380 skb->data, skb_tailroom(skb),
1381 0);
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001382 if (ret < 0) {
1383 cpsw_err(priv, ifup,
1384 "cannot submit skb to channel %d rx, error %d\n",
1385 ch, ret);
1386 kfree_skb(skb);
1387 return ret;
1388 }
1389 kmemleak_not_leak(skb);
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +03001390 }
1391
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001392 cpsw_info(priv, ifup, "ch %d rx, submitted %d descriptors\n",
1393 ch, ch_buf_num);
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +03001394 }
1395
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001396 return 0;
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +03001397}
1398
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001399static void cpsw_slave_stop(struct cpsw_slave *slave, struct cpsw_common *cpsw)
Sebastian Siewioraacebbf2013-04-23 07:31:36 +00001400{
Schuyler Patton3995d262014-03-03 16:19:06 +05301401 u32 slave_port;
1402
Ivan Khoronzhuk6f1f5832016-08-10 02:22:34 +03001403 slave_port = cpsw_get_slave_port(slave->slave_num);
Schuyler Patton3995d262014-03-03 16:19:06 +05301404
Sebastian Siewioraacebbf2013-04-23 07:31:36 +00001405 if (!slave->phy)
1406 return;
1407 phy_stop(slave->phy);
1408 phy_disconnect(slave->phy);
1409 slave->phy = NULL;
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001410 cpsw_ale_control_set(cpsw->ale, slave_port,
Schuyler Patton3995d262014-03-03 16:19:06 +05301411 ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
Grygorii Strashko1f95ba02016-06-24 21:23:41 +03001412 soft_reset_slave(slave);
Sebastian Siewioraacebbf2013-04-23 07:31:36 +00001413}
1414
Mugunthan V Ndf828592012-03-18 20:17:54 +00001415static int cpsw_ndo_open(struct net_device *ndev)
1416{
1417 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03001418 struct cpsw_common *cpsw = priv->cpsw;
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +03001419 int ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001420 u32 reg;
1421
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001422 ret = pm_runtime_get_sync(cpsw->dev);
Grygorii Strashko108a6532016-06-24 21:23:42 +03001423 if (ret < 0) {
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001424 pm_runtime_put_noidle(cpsw->dev);
Grygorii Strashko108a6532016-06-24 21:23:42 +03001425 return ret;
1426 }
Grygorii Strashko3fa88c52016-04-19 21:09:49 +03001427
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001428 if (!cpsw_common_res_usage_state(cpsw))
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001429 cpsw_intr_disable(cpsw);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001430 netif_carrier_off(ndev);
1431
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001432 /* Notify the stack of the actual queue counts. */
1433 ret = netif_set_real_num_tx_queues(ndev, cpsw->tx_ch_num);
1434 if (ret) {
1435 dev_err(priv->dev, "cannot set real number of tx queues\n");
1436 goto err_cleanup;
1437 }
1438
1439 ret = netif_set_real_num_rx_queues(ndev, cpsw->rx_ch_num);
1440 if (ret) {
1441 dev_err(priv->dev, "cannot set real number of rx queues\n");
1442 goto err_cleanup;
1443 }
1444
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001445 reg = cpsw->version;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001446
1447 dev_info(priv->dev, "initializing cpsw version %d.%d (%d)\n",
1448 CPSW_MAJOR_VERSION(reg), CPSW_MINOR_VERSION(reg),
1449 CPSW_RTL_VERSION(reg));
1450
1451 /* initialize host and slave ports */
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001452 if (!cpsw_common_res_usage_state(cpsw))
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001453 cpsw_init_host_port(priv);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001454 for_each_slave(priv, cpsw_slave_open, priv);
1455
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001456 /* Add default VLAN */
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001457 if (!cpsw->data.dual_emac)
Mugunthan V Ne6afea02014-06-18 17:21:48 +05301458 cpsw_add_default_vlan(priv);
1459 else
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001460 cpsw_ale_add_vlan(cpsw->ale, cpsw->data.default_vlan,
Grygorii Strashko61f1cef2016-04-07 15:16:43 +03001461 ALE_ALL_PORTS, ALE_ALL_PORTS, 0, 0);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001462
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001463 if (!cpsw_common_res_usage_state(cpsw)) {
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001464 /* disable priority elevation */
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001465 __raw_writel(0, &cpsw->regs->ptype);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001466
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001467 /* enable statistics collection only on all ports */
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001468 __raw_writel(0x7, &cpsw->regs->stat_port_en);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001469
Mugunthan V N1923d6e2014-09-08 22:54:02 +05301470 /* Enable internal fifo flow control */
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001471 writel(0x7, &cpsw->regs->flow_control);
Mugunthan V N1923d6e2014-09-08 22:54:02 +05301472
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +03001473 napi_enable(&cpsw->napi_rx);
1474 napi_enable(&cpsw->napi_tx);
Mugunthan V Nd354eb82015-08-04 16:06:19 +05301475
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +03001476 if (cpsw->tx_irq_disabled) {
1477 cpsw->tx_irq_disabled = false;
1478 enable_irq(cpsw->irqs_table[1]);
Mugunthan V N7da11602015-08-12 15:22:53 +05301479 }
1480
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +03001481 if (cpsw->rx_irq_disabled) {
1482 cpsw->rx_irq_disabled = false;
1483 enable_irq(cpsw->irqs_table[0]);
Mugunthan V N7da11602015-08-12 15:22:53 +05301484 }
1485
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +03001486 ret = cpsw_fill_rx_channels(priv);
1487 if (ret < 0)
1488 goto err_cleanup;
Mugunthan V Nf280e892013-12-11 22:09:05 -06001489
Grygorii Strashko8a2c9a52016-12-06 18:00:41 -06001490 if (cpts_register(cpsw->cpts))
Mugunthan V Nf280e892013-12-11 22:09:05 -06001491 dev_err(priv->dev, "error registering cpts device\n");
1492
Mugunthan V Ndf828592012-03-18 20:17:54 +00001493 }
Mugunthan V Ndf828592012-03-18 20:17:54 +00001494
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001495 /* Enable Interrupt pacing if configured */
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001496 if (cpsw->coal_intvl != 0) {
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001497 struct ethtool_coalesce coal;
1498
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001499 coal.rx_coalesce_usecs = cpsw->coal_intvl;
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001500 cpsw_set_coalesce(ndev, &coal);
1501 }
1502
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001503 cpdma_ctlr_start(cpsw->dma);
1504 cpsw_intr_enable(cpsw);
Mugunthan V Nf63a9752014-04-10 14:23:24 +05301505
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001506 if (cpsw->data.dual_emac)
1507 cpsw->slaves[priv->emac_port].open_stat = true;
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001508
1509 netif_tx_start_all_queues(ndev);
1510
Mugunthan V Ndf828592012-03-18 20:17:54 +00001511 return 0;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001512
Sebastian Siewioraacebbf2013-04-23 07:31:36 +00001513err_cleanup:
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001514 cpdma_ctlr_stop(cpsw->dma);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001515 for_each_slave(priv, cpsw_slave_stop, cpsw);
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001516 pm_runtime_put_sync(cpsw->dev);
Sebastian Siewioraacebbf2013-04-23 07:31:36 +00001517 netif_carrier_off(priv->ndev);
1518 return ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001519}
1520
1521static int cpsw_ndo_stop(struct net_device *ndev)
1522{
1523 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03001524 struct cpsw_common *cpsw = priv->cpsw;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001525
1526 cpsw_info(priv, ifdown, "shutting down cpsw device\n");
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001527 netif_tx_stop_all_queues(priv->ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001528 netif_carrier_off(priv->ndev);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001529
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001530 if (cpsw_common_res_usage_state(cpsw) <= 1) {
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +03001531 napi_disable(&cpsw->napi_rx);
1532 napi_disable(&cpsw->napi_tx);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001533 cpts_unregister(cpsw->cpts);
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001534 cpsw_intr_disable(cpsw);
1535 cpdma_ctlr_stop(cpsw->dma);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001536 cpsw_ale_stop(cpsw->ale);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001537 }
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001538 for_each_slave(priv, cpsw_slave_stop, cpsw);
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001539 pm_runtime_put_sync(cpsw->dev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001540 if (cpsw->data.dual_emac)
1541 cpsw->slaves[priv->emac_port].open_stat = false;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001542 return 0;
1543}
1544
1545static netdev_tx_t cpsw_ndo_start_xmit(struct sk_buff *skb,
1546 struct net_device *ndev)
1547{
1548 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001549 struct cpsw_common *cpsw = priv->cpsw;
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001550 struct netdev_queue *txq;
1551 struct cpdma_chan *txch;
1552 int ret, q_idx;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001553
Florian Westphal860e9532016-05-03 16:33:13 +02001554 netif_trans_update(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001555
1556 if (skb_padto(skb, CPSW_MIN_PACKET_SIZE)) {
1557 cpsw_err(priv, tx_err, "packet pad failed\n");
Tobias Klauser8dc43dd2014-03-10 13:12:23 +01001558 ndev->stats.tx_dropped++;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001559 return NETDEV_TX_OK;
1560 }
1561
Mugunthan V N9232b162013-02-11 09:52:19 +00001562 if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
Grygorii Strashkob63ba582016-12-06 18:00:35 -06001563 cpts_is_tx_enabled(cpsw->cpts))
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001564 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
1565
1566 skb_tx_timestamp(skb);
1567
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001568 q_idx = skb_get_queue_mapping(skb);
1569 if (q_idx >= cpsw->tx_ch_num)
1570 q_idx = q_idx % cpsw->tx_ch_num;
1571
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02001572 txch = cpsw->txv[q_idx].ch;
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001573 ret = cpsw_tx_packet_submit(priv, skb, txch);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001574 if (unlikely(ret != 0)) {
1575 cpsw_err(priv, tx_err, "desc submit failed\n");
1576 goto fail;
1577 }
1578
Mugunthan V Nfae50822013-01-17 06:31:34 +00001579 /* If there is no more tx desc left free then we need to
1580 * tell the kernel to stop sending us tx frames.
1581 */
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001582 if (unlikely(!cpdma_check_free_tx_desc(txch))) {
1583 txq = netdev_get_tx_queue(ndev, q_idx);
1584 netif_tx_stop_queue(txq);
1585 }
Mugunthan V Nfae50822013-01-17 06:31:34 +00001586
Mugunthan V Ndf828592012-03-18 20:17:54 +00001587 return NETDEV_TX_OK;
1588fail:
Tobias Klauser8dc43dd2014-03-10 13:12:23 +01001589 ndev->stats.tx_dropped++;
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001590 txq = netdev_get_tx_queue(ndev, skb_get_queue_mapping(skb));
1591 netif_tx_stop_queue(txq);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001592 return NETDEV_TX_BUSY;
1593}
1594
Grygorii Strashkoc8395d42016-12-06 18:00:34 -06001595#if IS_ENABLED(CONFIG_TI_CPTS)
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001596
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001597static void cpsw_hwtstamp_v1(struct cpsw_common *cpsw)
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001598{
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001599 struct cpsw_slave *slave = &cpsw->slaves[cpsw->data.active_slave];
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001600 u32 ts_en, seq_id;
1601
Grygorii Strashkob63ba582016-12-06 18:00:35 -06001602 if (!cpts_is_tx_enabled(cpsw->cpts) &&
1603 !cpts_is_rx_enabled(cpsw->cpts)) {
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001604 slave_write(slave, 0, CPSW1_TS_CTL);
1605 return;
1606 }
1607
1608 seq_id = (30 << CPSW_V1_SEQ_ID_OFS_SHIFT) | ETH_P_1588;
1609 ts_en = EVENT_MSG_BITS << CPSW_V1_MSG_TYPE_OFS;
1610
Grygorii Strashkob63ba582016-12-06 18:00:35 -06001611 if (cpts_is_tx_enabled(cpsw->cpts))
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001612 ts_en |= CPSW_V1_TS_TX_EN;
1613
Grygorii Strashkob63ba582016-12-06 18:00:35 -06001614 if (cpts_is_rx_enabled(cpsw->cpts))
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001615 ts_en |= CPSW_V1_TS_RX_EN;
1616
1617 slave_write(slave, ts_en, CPSW1_TS_CTL);
1618 slave_write(slave, seq_id, CPSW1_TS_SEQ_LTYPE);
1619}
1620
1621static void cpsw_hwtstamp_v2(struct cpsw_priv *priv)
1622{
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001623 struct cpsw_slave *slave;
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001624 struct cpsw_common *cpsw = priv->cpsw;
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001625 u32 ctrl, mtype;
1626
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001627 if (cpsw->data.dual_emac)
1628 slave = &cpsw->slaves[priv->emac_port];
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001629 else
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001630 slave = &cpsw->slaves[cpsw->data.active_slave];
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001631
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001632 ctrl = slave_read(slave, CPSW2_CONTROL);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001633 switch (cpsw->version) {
George Cherian09c55372014-05-02 12:02:02 +05301634 case CPSW_VERSION_2:
1635 ctrl &= ~CTRL_V2_ALL_TS_MASK;
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001636
Grygorii Strashkob63ba582016-12-06 18:00:35 -06001637 if (cpts_is_tx_enabled(cpsw->cpts))
George Cherian09c55372014-05-02 12:02:02 +05301638 ctrl |= CTRL_V2_TX_TS_BITS;
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001639
Grygorii Strashkob63ba582016-12-06 18:00:35 -06001640 if (cpts_is_rx_enabled(cpsw->cpts))
George Cherian09c55372014-05-02 12:02:02 +05301641 ctrl |= CTRL_V2_RX_TS_BITS;
Richard Cochran26fe7eb2015-05-25 11:02:13 +02001642 break;
George Cherian09c55372014-05-02 12:02:02 +05301643 case CPSW_VERSION_3:
1644 default:
1645 ctrl &= ~CTRL_V3_ALL_TS_MASK;
1646
Grygorii Strashkob63ba582016-12-06 18:00:35 -06001647 if (cpts_is_tx_enabled(cpsw->cpts))
George Cherian09c55372014-05-02 12:02:02 +05301648 ctrl |= CTRL_V3_TX_TS_BITS;
1649
Grygorii Strashkob63ba582016-12-06 18:00:35 -06001650 if (cpts_is_rx_enabled(cpsw->cpts))
George Cherian09c55372014-05-02 12:02:02 +05301651 ctrl |= CTRL_V3_RX_TS_BITS;
Richard Cochran26fe7eb2015-05-25 11:02:13 +02001652 break;
George Cherian09c55372014-05-02 12:02:02 +05301653 }
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001654
1655 mtype = (30 << TS_SEQ_ID_OFFSET_SHIFT) | EVENT_MSG_BITS;
1656
1657 slave_write(slave, mtype, CPSW2_TS_SEQ_MTYPE);
1658 slave_write(slave, ctrl, CPSW2_CONTROL);
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001659 __raw_writel(ETH_P_1588, &cpsw->regs->ts_ltype);
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001660}
1661
Ben Hutchingsa5b41452013-11-18 23:23:40 +00001662static int cpsw_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001663{
Mugunthan V N3177bf62012-11-27 07:53:40 +00001664 struct cpsw_priv *priv = netdev_priv(dev);
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001665 struct hwtstamp_config cfg;
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001666 struct cpsw_common *cpsw = priv->cpsw;
1667 struct cpts *cpts = cpsw->cpts;
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001668
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001669 if (cpsw->version != CPSW_VERSION_1 &&
1670 cpsw->version != CPSW_VERSION_2 &&
1671 cpsw->version != CPSW_VERSION_3)
Ben Hutchings2ee91e52013-11-14 00:47:36 +00001672 return -EOPNOTSUPP;
1673
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001674 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
1675 return -EFAULT;
1676
1677 /* reserved for future extensions */
1678 if (cfg.flags)
1679 return -EINVAL;
1680
Ben Hutchings2ee91e52013-11-14 00:47:36 +00001681 if (cfg.tx_type != HWTSTAMP_TX_OFF && cfg.tx_type != HWTSTAMP_TX_ON)
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001682 return -ERANGE;
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001683
1684 switch (cfg.rx_filter) {
1685 case HWTSTAMP_FILTER_NONE:
Grygorii Strashkob63ba582016-12-06 18:00:35 -06001686 cpts_rx_enable(cpts, 0);
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001687 break;
1688 case HWTSTAMP_FILTER_ALL:
1689 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
1690 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
1691 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
1692 return -ERANGE;
1693 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
1694 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
1695 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
1696 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1697 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
1698 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
1699 case HWTSTAMP_FILTER_PTP_V2_EVENT:
1700 case HWTSTAMP_FILTER_PTP_V2_SYNC:
1701 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
Grygorii Strashkob63ba582016-12-06 18:00:35 -06001702 cpts_rx_enable(cpts, 1);
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001703 cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
1704 break;
1705 default:
1706 return -ERANGE;
1707 }
1708
Grygorii Strashkob63ba582016-12-06 18:00:35 -06001709 cpts_tx_enable(cpts, cfg.tx_type == HWTSTAMP_TX_ON);
Ben Hutchings2ee91e52013-11-14 00:47:36 +00001710
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001711 switch (cpsw->version) {
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001712 case CPSW_VERSION_1:
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001713 cpsw_hwtstamp_v1(cpsw);
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001714 break;
1715 case CPSW_VERSION_2:
George Cherianf7d403c2014-05-02 12:02:01 +05301716 case CPSW_VERSION_3:
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001717 cpsw_hwtstamp_v2(priv);
1718 break;
1719 default:
Ben Hutchings2ee91e52013-11-14 00:47:36 +00001720 WARN_ON(1);
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001721 }
1722
1723 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
1724}
1725
Ben Hutchingsa5b41452013-11-18 23:23:40 +00001726static int cpsw_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
1727{
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001728 struct cpsw_common *cpsw = ndev_to_cpsw(dev);
1729 struct cpts *cpts = cpsw->cpts;
Ben Hutchingsa5b41452013-11-18 23:23:40 +00001730 struct hwtstamp_config cfg;
1731
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001732 if (cpsw->version != CPSW_VERSION_1 &&
1733 cpsw->version != CPSW_VERSION_2 &&
1734 cpsw->version != CPSW_VERSION_3)
Ben Hutchingsa5b41452013-11-18 23:23:40 +00001735 return -EOPNOTSUPP;
1736
1737 cfg.flags = 0;
Grygorii Strashkob63ba582016-12-06 18:00:35 -06001738 cfg.tx_type = cpts_is_tx_enabled(cpts) ?
1739 HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF;
1740 cfg.rx_filter = (cpts_is_rx_enabled(cpts) ?
Ben Hutchingsa5b41452013-11-18 23:23:40 +00001741 HWTSTAMP_FILTER_PTP_V2_EVENT : HWTSTAMP_FILTER_NONE);
1742
1743 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
1744}
Grygorii Strashkoc8395d42016-12-06 18:00:34 -06001745#else
1746static int cpsw_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
1747{
1748 return -EOPNOTSUPP;
1749}
Ben Hutchingsa5b41452013-11-18 23:23:40 +00001750
Grygorii Strashkoc8395d42016-12-06 18:00:34 -06001751static int cpsw_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
1752{
1753 return -EOPNOTSUPP;
1754}
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001755#endif /*CONFIG_TI_CPTS*/
1756
1757static int cpsw_ndo_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
1758{
Mugunthan V N11f2c982013-03-11 23:16:38 +00001759 struct cpsw_priv *priv = netdev_priv(dev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001760 struct cpsw_common *cpsw = priv->cpsw;
1761 int slave_no = cpsw_slave_index(cpsw, priv);
Mugunthan V N11f2c982013-03-11 23:16:38 +00001762
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001763 if (!netif_running(dev))
1764 return -EINVAL;
1765
Mugunthan V N11f2c982013-03-11 23:16:38 +00001766 switch (cmd) {
Mugunthan V N11f2c982013-03-11 23:16:38 +00001767 case SIOCSHWTSTAMP:
Ben Hutchingsa5b41452013-11-18 23:23:40 +00001768 return cpsw_hwtstamp_set(dev, req);
1769 case SIOCGHWTSTAMP:
1770 return cpsw_hwtstamp_get(dev, req);
Mugunthan V N11f2c982013-03-11 23:16:38 +00001771 }
1772
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001773 if (!cpsw->slaves[slave_no].phy)
Stefan Sørensenc1b59942014-02-16 14:54:25 +01001774 return -EOPNOTSUPP;
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001775 return phy_mii_ioctl(cpsw->slaves[slave_no].phy, req, cmd);
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001776}
1777
Mugunthan V Ndf828592012-03-18 20:17:54 +00001778static void cpsw_ndo_tx_timeout(struct net_device *ndev)
1779{
1780 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001781 struct cpsw_common *cpsw = priv->cpsw;
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001782 int ch;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001783
1784 cpsw_err(priv, tx_err, "transmit timeout, restarting dma\n");
Tobias Klauser8dc43dd2014-03-10 13:12:23 +01001785 ndev->stats.tx_errors++;
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001786 cpsw_intr_disable(cpsw);
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001787 for (ch = 0; ch < cpsw->tx_ch_num; ch++) {
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02001788 cpdma_chan_stop(cpsw->txv[ch].ch);
1789 cpdma_chan_start(cpsw->txv[ch].ch);
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001790 }
1791
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001792 cpsw_intr_enable(cpsw);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001793}
1794
Mugunthan V Ndcfd8d52013-07-25 23:44:01 +05301795static int cpsw_ndo_set_mac_address(struct net_device *ndev, void *p)
1796{
1797 struct cpsw_priv *priv = netdev_priv(ndev);
1798 struct sockaddr *addr = (struct sockaddr *)p;
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03001799 struct cpsw_common *cpsw = priv->cpsw;
Mugunthan V Ndcfd8d52013-07-25 23:44:01 +05301800 int flags = 0;
1801 u16 vid = 0;
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001802 int ret;
Mugunthan V Ndcfd8d52013-07-25 23:44:01 +05301803
1804 if (!is_valid_ether_addr(addr->sa_data))
1805 return -EADDRNOTAVAIL;
1806
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001807 ret = pm_runtime_get_sync(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001808 if (ret < 0) {
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001809 pm_runtime_put_noidle(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001810 return ret;
1811 }
1812
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001813 if (cpsw->data.dual_emac) {
1814 vid = cpsw->slaves[priv->emac_port].port_vlan;
Mugunthan V Ndcfd8d52013-07-25 23:44:01 +05301815 flags = ALE_VLAN;
1816 }
1817
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001818 cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr, HOST_PORT_NUM,
Mugunthan V Ndcfd8d52013-07-25 23:44:01 +05301819 flags, vid);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001820 cpsw_ale_add_ucast(cpsw->ale, addr->sa_data, HOST_PORT_NUM,
Mugunthan V Ndcfd8d52013-07-25 23:44:01 +05301821 flags, vid);
1822
1823 memcpy(priv->mac_addr, addr->sa_data, ETH_ALEN);
1824 memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
1825 for_each_slave(priv, cpsw_set_slave_mac, priv);
1826
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001827 pm_runtime_put(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001828
Mugunthan V Ndcfd8d52013-07-25 23:44:01 +05301829 return 0;
1830}
1831
Mugunthan V Ndf828592012-03-18 20:17:54 +00001832#ifdef CONFIG_NET_POLL_CONTROLLER
1833static void cpsw_ndo_poll_controller(struct net_device *ndev)
1834{
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +03001835 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001836
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +03001837 cpsw_intr_disable(cpsw);
1838 cpsw_rx_interrupt(cpsw->irqs_table[0], cpsw);
1839 cpsw_tx_interrupt(cpsw->irqs_table[1], cpsw);
1840 cpsw_intr_enable(cpsw);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001841}
1842#endif
1843
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001844static inline int cpsw_add_vlan_ale_entry(struct cpsw_priv *priv,
1845 unsigned short vid)
1846{
1847 int ret;
Mugunthan V N9f6bd8f2015-01-15 14:59:28 +05301848 int unreg_mcast_mask = 0;
1849 u32 port_mask;
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001850 struct cpsw_common *cpsw = priv->cpsw;
Lennart Sorensen1e5c4bc2014-10-31 13:38:52 -04001851
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001852 if (cpsw->data.dual_emac) {
Mugunthan V N9f6bd8f2015-01-15 14:59:28 +05301853 port_mask = (1 << (priv->emac_port + 1)) | ALE_PORT_HOST;
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001854
Mugunthan V N9f6bd8f2015-01-15 14:59:28 +05301855 if (priv->ndev->flags & IFF_ALLMULTI)
1856 unreg_mcast_mask = port_mask;
1857 } else {
1858 port_mask = ALE_ALL_PORTS;
1859
1860 if (priv->ndev->flags & IFF_ALLMULTI)
1861 unreg_mcast_mask = ALE_ALL_PORTS;
1862 else
1863 unreg_mcast_mask = ALE_PORT_1 | ALE_PORT_2;
1864 }
1865
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001866 ret = cpsw_ale_add_vlan(cpsw->ale, vid, port_mask, 0, port_mask,
Grygorii Strashko61f1cef2016-04-07 15:16:43 +03001867 unreg_mcast_mask);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001868 if (ret != 0)
1869 return ret;
1870
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001871 ret = cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr,
Grygorii Strashko71a2cbb2016-04-07 15:16:44 +03001872 HOST_PORT_NUM, ALE_VLAN, vid);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001873 if (ret != 0)
1874 goto clean_vid;
1875
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001876 ret = cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
Mugunthan V N9f6bd8f2015-01-15 14:59:28 +05301877 port_mask, ALE_VLAN, vid, 0);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001878 if (ret != 0)
1879 goto clean_vlan_ucast;
1880 return 0;
1881
1882clean_vlan_ucast:
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001883 cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr,
Grygorii Strashko71a2cbb2016-04-07 15:16:44 +03001884 HOST_PORT_NUM, ALE_VLAN, vid);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001885clean_vid:
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001886 cpsw_ale_del_vlan(cpsw->ale, vid, 0);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001887 return ret;
1888}
1889
1890static int cpsw_ndo_vlan_rx_add_vid(struct net_device *ndev,
Patrick McHardy80d5c362013-04-19 02:04:28 +00001891 __be16 proto, u16 vid)
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001892{
1893 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03001894 struct cpsw_common *cpsw = priv->cpsw;
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001895 int ret;
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001896
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001897 if (vid == cpsw->data.default_vlan)
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001898 return 0;
1899
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001900 ret = pm_runtime_get_sync(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001901 if (ret < 0) {
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001902 pm_runtime_put_noidle(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001903 return ret;
1904 }
1905
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001906 if (cpsw->data.dual_emac) {
Mugunthan V N02a54162015-01-22 15:19:22 +05301907 /* In dual EMAC, reserved VLAN id should not be used for
1908 * creating VLAN interfaces as this can break the dual
1909 * EMAC port separation
1910 */
1911 int i;
1912
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001913 for (i = 0; i < cpsw->data.slaves; i++) {
1914 if (vid == cpsw->slaves[i].port_vlan)
Mugunthan V N02a54162015-01-22 15:19:22 +05301915 return -EINVAL;
1916 }
1917 }
1918
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001919 dev_info(priv->dev, "Adding vlanid %d to vlan filter\n", vid);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001920 ret = cpsw_add_vlan_ale_entry(priv, vid);
1921
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001922 pm_runtime_put(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001923 return ret;
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001924}
1925
1926static int cpsw_ndo_vlan_rx_kill_vid(struct net_device *ndev,
Patrick McHardy80d5c362013-04-19 02:04:28 +00001927 __be16 proto, u16 vid)
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001928{
1929 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03001930 struct cpsw_common *cpsw = priv->cpsw;
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001931 int ret;
1932
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001933 if (vid == cpsw->data.default_vlan)
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001934 return 0;
1935
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001936 ret = pm_runtime_get_sync(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001937 if (ret < 0) {
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001938 pm_runtime_put_noidle(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001939 return ret;
1940 }
1941
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001942 if (cpsw->data.dual_emac) {
Mugunthan V N02a54162015-01-22 15:19:22 +05301943 int i;
1944
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001945 for (i = 0; i < cpsw->data.slaves; i++) {
1946 if (vid == cpsw->slaves[i].port_vlan)
Mugunthan V N02a54162015-01-22 15:19:22 +05301947 return -EINVAL;
1948 }
1949 }
1950
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001951 dev_info(priv->dev, "removing vlanid %d from vlan filter\n", vid);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001952 ret = cpsw_ale_del_vlan(cpsw->ale, vid, 0);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001953 if (ret != 0)
1954 return ret;
1955
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001956 ret = cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr,
Grygorii Strashko61f1cef2016-04-07 15:16:43 +03001957 HOST_PORT_NUM, ALE_VLAN, vid);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001958 if (ret != 0)
1959 return ret;
1960
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001961 ret = cpsw_ale_del_mcast(cpsw->ale, priv->ndev->broadcast,
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001962 0, ALE_VLAN, vid);
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001963 pm_runtime_put(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001964 return ret;
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001965}
1966
Ivan Khoronzhuk83fcad02016-11-29 17:00:49 +02001967static int cpsw_ndo_set_tx_maxrate(struct net_device *ndev, int queue, u32 rate)
1968{
1969 struct cpsw_priv *priv = netdev_priv(ndev);
1970 int tx_ch_num = ndev->real_num_tx_queues;
1971 u32 consumed_rate, min_rate, max_rate;
1972 struct cpsw_common *cpsw = priv->cpsw;
1973 struct cpsw_slave *slave;
1974 int ret, i, weight;
1975 int rlim_num = 0;
1976 u32 ch_rate;
1977
1978 ch_rate = netdev_get_tx_queue(ndev, queue)->tx_maxrate;
1979 if (ch_rate == rate)
1980 return 0;
1981
1982 if (cpsw->data.dual_emac)
1983 slave = &cpsw->slaves[priv->emac_port];
1984 else
1985 slave = &cpsw->slaves[cpsw->data.active_slave];
1986 max_rate = slave->phy->speed;
1987
1988 consumed_rate = 0;
1989 for (i = 0; i < tx_ch_num; i++) {
1990 if (i == queue)
1991 ch_rate = rate;
1992 else
1993 ch_rate = netdev_get_tx_queue(ndev, i)->tx_maxrate;
Ivan Khoronzhuk83fcad02016-11-29 17:00:49 +02001994 if (!ch_rate)
1995 continue;
1996
1997 rlim_num++;
1998 consumed_rate += ch_rate;
1999 }
2000
2001 if (consumed_rate > max_rate)
2002 dev_info(priv->dev, "The common rate shouldn't be more than %dMbps",
2003 max_rate);
2004
2005 if (consumed_rate > max_rate) {
2006 if (max_rate == 10 && consumed_rate <= 100) {
2007 max_rate = 100;
2008 } else if (max_rate <= 100 && consumed_rate <= 1000) {
2009 max_rate = 1000;
2010 } else {
2011 dev_err(priv->dev, "The common rate cannot be more than %dMbps",
2012 max_rate);
2013 return -EINVAL;
2014 }
2015 }
2016
2017 if (consumed_rate > max_rate) {
2018 dev_err(priv->dev, "The common rate cannot be more than %dMbps",
2019 max_rate);
2020 return -EINVAL;
2021 }
2022
2023 rate *= 1000;
2024 min_rate = cpdma_chan_get_min_rate(cpsw->dma);
2025 if ((rate < min_rate && rate)) {
2026 dev_err(priv->dev, "The common rate cannot be less than %dMbps",
2027 min_rate);
2028 return -EINVAL;
2029 }
2030
2031 ret = pm_runtime_get_sync(cpsw->dev);
2032 if (ret < 0) {
2033 pm_runtime_put_noidle(cpsw->dev);
2034 return ret;
2035 }
2036
2037 if (rlim_num == tx_ch_num)
2038 max_rate = consumed_rate;
2039
2040 weight = (rate * 100) / (max_rate * 1000);
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02002041 cpdma_chan_set_weight(cpsw->txv[queue].ch, weight);
2042 ret = cpdma_chan_set_rate(cpsw->txv[queue].ch, rate);
Ivan Khoronzhuk83fcad02016-11-29 17:00:49 +02002043
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02002044 /* re-split budget between channels */
2045 if (!rate)
2046 cpsw_split_budget(ndev);
Ivan Khoronzhuk83fcad02016-11-29 17:00:49 +02002047 pm_runtime_put(cpsw->dev);
2048 return ret;
2049}
2050
Mugunthan V Ndf828592012-03-18 20:17:54 +00002051static const struct net_device_ops cpsw_netdev_ops = {
2052 .ndo_open = cpsw_ndo_open,
2053 .ndo_stop = cpsw_ndo_stop,
2054 .ndo_start_xmit = cpsw_ndo_start_xmit,
Mugunthan V Ndcfd8d52013-07-25 23:44:01 +05302055 .ndo_set_mac_address = cpsw_ndo_set_mac_address,
Richard Cochran2e5b38a2012-10-29 08:45:20 +00002056 .ndo_do_ioctl = cpsw_ndo_ioctl,
Mugunthan V Ndf828592012-03-18 20:17:54 +00002057 .ndo_validate_addr = eth_validate_addr,
Mugunthan V Ndf828592012-03-18 20:17:54 +00002058 .ndo_tx_timeout = cpsw_ndo_tx_timeout,
Mugunthan V N5c50a852012-10-29 08:45:11 +00002059 .ndo_set_rx_mode = cpsw_ndo_set_rx_mode,
Ivan Khoronzhuk83fcad02016-11-29 17:00:49 +02002060 .ndo_set_tx_maxrate = cpsw_ndo_set_tx_maxrate,
Mugunthan V Ndf828592012-03-18 20:17:54 +00002061#ifdef CONFIG_NET_POLL_CONTROLLER
2062 .ndo_poll_controller = cpsw_ndo_poll_controller,
2063#endif
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00002064 .ndo_vlan_rx_add_vid = cpsw_ndo_vlan_rx_add_vid,
2065 .ndo_vlan_rx_kill_vid = cpsw_ndo_vlan_rx_kill_vid,
Mugunthan V Ndf828592012-03-18 20:17:54 +00002066};
2067
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05302068static int cpsw_get_regs_len(struct net_device *ndev)
2069{
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002070 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05302071
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002072 return cpsw->data.ale_entries * ALE_ENTRY_WORDS * sizeof(u32);
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05302073}
2074
2075static void cpsw_get_regs(struct net_device *ndev,
2076 struct ethtool_regs *regs, void *p)
2077{
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05302078 u32 *reg = p;
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002079 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05302080
2081 /* update CPSW IP version */
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002082 regs->version = cpsw->version;
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05302083
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002084 cpsw_ale_dump(cpsw->ale, reg);
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05302085}
2086
Mugunthan V Ndf828592012-03-18 20:17:54 +00002087static void cpsw_get_drvinfo(struct net_device *ndev,
2088 struct ethtool_drvinfo *info)
2089{
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03002090 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002091 struct platform_device *pdev = to_platform_device(cpsw->dev);
Jiri Pirko7826d432013-01-06 00:44:26 +00002092
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05302093 strlcpy(info->driver, "cpsw", sizeof(info->driver));
Jiri Pirko7826d432013-01-06 00:44:26 +00002094 strlcpy(info->version, "1.0", sizeof(info->version));
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002095 strlcpy(info->bus_info, pdev->name, sizeof(info->bus_info));
Mugunthan V Ndf828592012-03-18 20:17:54 +00002096}
2097
2098static u32 cpsw_get_msglevel(struct net_device *ndev)
2099{
2100 struct cpsw_priv *priv = netdev_priv(ndev);
2101 return priv->msg_enable;
2102}
2103
2104static void cpsw_set_msglevel(struct net_device *ndev, u32 value)
2105{
2106 struct cpsw_priv *priv = netdev_priv(ndev);
2107 priv->msg_enable = value;
2108}
2109
Grygorii Strashkoc8395d42016-12-06 18:00:34 -06002110#if IS_ENABLED(CONFIG_TI_CPTS)
Richard Cochran2e5b38a2012-10-29 08:45:20 +00002111static int cpsw_get_ts_info(struct net_device *ndev,
2112 struct ethtool_ts_info *info)
2113{
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002114 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Richard Cochran2e5b38a2012-10-29 08:45:20 +00002115
2116 info->so_timestamping =
2117 SOF_TIMESTAMPING_TX_HARDWARE |
2118 SOF_TIMESTAMPING_TX_SOFTWARE |
2119 SOF_TIMESTAMPING_RX_HARDWARE |
2120 SOF_TIMESTAMPING_RX_SOFTWARE |
2121 SOF_TIMESTAMPING_SOFTWARE |
2122 SOF_TIMESTAMPING_RAW_HARDWARE;
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002123 info->phc_index = cpsw->cpts->phc_index;
Richard Cochran2e5b38a2012-10-29 08:45:20 +00002124 info->tx_types =
2125 (1 << HWTSTAMP_TX_OFF) |
2126 (1 << HWTSTAMP_TX_ON);
2127 info->rx_filters =
2128 (1 << HWTSTAMP_FILTER_NONE) |
2129 (1 << HWTSTAMP_FILTER_PTP_V2_EVENT);
Grygorii Strashkoc8395d42016-12-06 18:00:34 -06002130 return 0;
2131}
Richard Cochran2e5b38a2012-10-29 08:45:20 +00002132#else
Grygorii Strashkoc8395d42016-12-06 18:00:34 -06002133static int cpsw_get_ts_info(struct net_device *ndev,
2134 struct ethtool_ts_info *info)
2135{
Richard Cochran2e5b38a2012-10-29 08:45:20 +00002136 info->so_timestamping =
2137 SOF_TIMESTAMPING_TX_SOFTWARE |
2138 SOF_TIMESTAMPING_RX_SOFTWARE |
2139 SOF_TIMESTAMPING_SOFTWARE;
2140 info->phc_index = -1;
2141 info->tx_types = 0;
2142 info->rx_filters = 0;
Richard Cochran2e5b38a2012-10-29 08:45:20 +00002143 return 0;
2144}
Grygorii Strashkoc8395d42016-12-06 18:00:34 -06002145#endif
Richard Cochran2e5b38a2012-10-29 08:45:20 +00002146
Philippe Reynes24798762016-10-08 17:46:15 +02002147static int cpsw_get_link_ksettings(struct net_device *ndev,
2148 struct ethtool_link_ksettings *ecmd)
Mugunthan V Nd3bb9c52013-03-11 23:16:36 +00002149{
2150 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002151 struct cpsw_common *cpsw = priv->cpsw;
2152 int slave_no = cpsw_slave_index(cpsw, priv);
Mugunthan V Nd3bb9c52013-03-11 23:16:36 +00002153
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002154 if (cpsw->slaves[slave_no].phy)
Philippe Reynes24798762016-10-08 17:46:15 +02002155 return phy_ethtool_ksettings_get(cpsw->slaves[slave_no].phy,
2156 ecmd);
Mugunthan V Nd3bb9c52013-03-11 23:16:36 +00002157 else
2158 return -EOPNOTSUPP;
2159}
2160
Philippe Reynes24798762016-10-08 17:46:15 +02002161static int cpsw_set_link_ksettings(struct net_device *ndev,
2162 const struct ethtool_link_ksettings *ecmd)
Mugunthan V Nd3bb9c52013-03-11 23:16:36 +00002163{
2164 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002165 struct cpsw_common *cpsw = priv->cpsw;
2166 int slave_no = cpsw_slave_index(cpsw, priv);
Mugunthan V Nd3bb9c52013-03-11 23:16:36 +00002167
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002168 if (cpsw->slaves[slave_no].phy)
Philippe Reynes24798762016-10-08 17:46:15 +02002169 return phy_ethtool_ksettings_set(cpsw->slaves[slave_no].phy,
2170 ecmd);
Mugunthan V Nd3bb9c52013-03-11 23:16:36 +00002171 else
2172 return -EOPNOTSUPP;
2173}
2174
Matus Ujhelyid8a64422013-08-20 07:59:38 +02002175static void cpsw_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2176{
2177 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002178 struct cpsw_common *cpsw = priv->cpsw;
2179 int slave_no = cpsw_slave_index(cpsw, priv);
Matus Ujhelyid8a64422013-08-20 07:59:38 +02002180
2181 wol->supported = 0;
2182 wol->wolopts = 0;
2183
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002184 if (cpsw->slaves[slave_no].phy)
2185 phy_ethtool_get_wol(cpsw->slaves[slave_no].phy, wol);
Matus Ujhelyid8a64422013-08-20 07:59:38 +02002186}
2187
2188static int cpsw_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2189{
2190 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002191 struct cpsw_common *cpsw = priv->cpsw;
2192 int slave_no = cpsw_slave_index(cpsw, priv);
Matus Ujhelyid8a64422013-08-20 07:59:38 +02002193
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002194 if (cpsw->slaves[slave_no].phy)
2195 return phy_ethtool_set_wol(cpsw->slaves[slave_no].phy, wol);
Matus Ujhelyid8a64422013-08-20 07:59:38 +02002196 else
2197 return -EOPNOTSUPP;
2198}
2199
Mugunthan V N1923d6e2014-09-08 22:54:02 +05302200static void cpsw_get_pauseparam(struct net_device *ndev,
2201 struct ethtool_pauseparam *pause)
2202{
2203 struct cpsw_priv *priv = netdev_priv(ndev);
2204
2205 pause->autoneg = AUTONEG_DISABLE;
2206 pause->rx_pause = priv->rx_pause ? true : false;
2207 pause->tx_pause = priv->tx_pause ? true : false;
2208}
2209
2210static int cpsw_set_pauseparam(struct net_device *ndev,
2211 struct ethtool_pauseparam *pause)
2212{
2213 struct cpsw_priv *priv = netdev_priv(ndev);
2214 bool link;
2215
2216 priv->rx_pause = pause->rx_pause ? true : false;
2217 priv->tx_pause = pause->tx_pause ? true : false;
2218
2219 for_each_slave(priv, _cpsw_adjust_link, priv, &link);
Mugunthan V N1923d6e2014-09-08 22:54:02 +05302220 return 0;
2221}
2222
Grygorii Strashko7898b1d2016-06-24 21:23:44 +03002223static int cpsw_ethtool_op_begin(struct net_device *ndev)
2224{
2225 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03002226 struct cpsw_common *cpsw = priv->cpsw;
Grygorii Strashko7898b1d2016-06-24 21:23:44 +03002227 int ret;
2228
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002229 ret = pm_runtime_get_sync(cpsw->dev);
Grygorii Strashko7898b1d2016-06-24 21:23:44 +03002230 if (ret < 0) {
2231 cpsw_err(priv, drv, "ethtool begin failed %d\n", ret);
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002232 pm_runtime_put_noidle(cpsw->dev);
Grygorii Strashko7898b1d2016-06-24 21:23:44 +03002233 }
2234
2235 return ret;
2236}
2237
2238static void cpsw_ethtool_op_complete(struct net_device *ndev)
2239{
2240 struct cpsw_priv *priv = netdev_priv(ndev);
2241 int ret;
2242
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002243 ret = pm_runtime_put(priv->cpsw->dev);
Grygorii Strashko7898b1d2016-06-24 21:23:44 +03002244 if (ret < 0)
2245 cpsw_err(priv, drv, "ethtool complete failed %d\n", ret);
2246}
2247
Ivan Khoronzhukce52c742016-08-22 21:18:28 +03002248static void cpsw_get_channels(struct net_device *ndev,
2249 struct ethtool_channels *ch)
2250{
2251 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
2252
2253 ch->max_combined = 0;
2254 ch->max_rx = CPSW_MAX_QUEUES;
2255 ch->max_tx = CPSW_MAX_QUEUES;
2256 ch->max_other = 0;
2257 ch->other_count = 0;
2258 ch->rx_count = cpsw->rx_ch_num;
2259 ch->tx_count = cpsw->tx_ch_num;
2260 ch->combined_count = 0;
2261}
2262
2263static int cpsw_check_ch_settings(struct cpsw_common *cpsw,
2264 struct ethtool_channels *ch)
2265{
2266 if (ch->combined_count)
2267 return -EINVAL;
2268
2269 /* verify we have at least one channel in each direction */
2270 if (!ch->rx_count || !ch->tx_count)
2271 return -EINVAL;
2272
2273 if (ch->rx_count > cpsw->data.channels ||
2274 ch->tx_count > cpsw->data.channels)
2275 return -EINVAL;
2276
2277 return 0;
2278}
2279
2280static int cpsw_update_channels_res(struct cpsw_priv *priv, int ch_num, int rx)
2281{
2282 int (*poll)(struct napi_struct *, int);
2283 struct cpsw_common *cpsw = priv->cpsw;
2284 void (*handler)(void *, int, int);
Ivan Khoronzhuk83fcad02016-11-29 17:00:49 +02002285 struct netdev_queue *queue;
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02002286 struct cpsw_vector *vec;
Ivan Khoronzhukce52c742016-08-22 21:18:28 +03002287 int ret, *ch;
2288
2289 if (rx) {
2290 ch = &cpsw->rx_ch_num;
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02002291 vec = cpsw->rxv;
Ivan Khoronzhukce52c742016-08-22 21:18:28 +03002292 handler = cpsw_rx_handler;
2293 poll = cpsw_rx_poll;
2294 } else {
2295 ch = &cpsw->tx_ch_num;
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02002296 vec = cpsw->txv;
Ivan Khoronzhukce52c742016-08-22 21:18:28 +03002297 handler = cpsw_tx_handler;
2298 poll = cpsw_tx_poll;
2299 }
2300
2301 while (*ch < ch_num) {
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02002302 vec[*ch].ch = cpdma_chan_create(cpsw->dma, *ch, handler, rx);
Ivan Khoronzhuk83fcad02016-11-29 17:00:49 +02002303 queue = netdev_get_tx_queue(priv->ndev, *ch);
2304 queue->tx_maxrate = 0;
Ivan Khoronzhukce52c742016-08-22 21:18:28 +03002305
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02002306 if (IS_ERR(vec[*ch].ch))
2307 return PTR_ERR(vec[*ch].ch);
Ivan Khoronzhukce52c742016-08-22 21:18:28 +03002308
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02002309 if (!vec[*ch].ch)
Ivan Khoronzhukce52c742016-08-22 21:18:28 +03002310 return -EINVAL;
2311
2312 cpsw_info(priv, ifup, "created new %d %s channel\n", *ch,
2313 (rx ? "rx" : "tx"));
2314 (*ch)++;
2315 }
2316
2317 while (*ch > ch_num) {
2318 (*ch)--;
2319
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02002320 ret = cpdma_chan_destroy(vec[*ch].ch);
Ivan Khoronzhukce52c742016-08-22 21:18:28 +03002321 if (ret)
2322 return ret;
2323
2324 cpsw_info(priv, ifup, "destroyed %d %s channel\n", *ch,
2325 (rx ? "rx" : "tx"));
2326 }
2327
2328 return 0;
2329}
2330
2331static int cpsw_update_channels(struct cpsw_priv *priv,
2332 struct ethtool_channels *ch)
2333{
2334 int ret;
2335
2336 ret = cpsw_update_channels_res(priv, ch->rx_count, 1);
2337 if (ret)
2338 return ret;
2339
2340 ret = cpsw_update_channels_res(priv, ch->tx_count, 0);
2341 if (ret)
2342 return ret;
2343
2344 return 0;
2345}
2346
2347static int cpsw_set_channels(struct net_device *ndev,
2348 struct ethtool_channels *chs)
2349{
2350 struct cpsw_priv *priv = netdev_priv(ndev);
2351 struct cpsw_common *cpsw = priv->cpsw;
2352 struct cpsw_slave *slave;
2353 int i, ret;
2354
2355 ret = cpsw_check_ch_settings(cpsw, chs);
2356 if (ret < 0)
2357 return ret;
2358
2359 /* Disable NAPI scheduling */
2360 cpsw_intr_disable(cpsw);
2361
2362 /* Stop all transmit queues for every network device.
2363 * Disable re-using rx descriptors with dormant_on.
2364 */
2365 for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++) {
2366 if (!(slave->ndev && netif_running(slave->ndev)))
2367 continue;
2368
2369 netif_tx_stop_all_queues(slave->ndev);
2370 netif_dormant_on(slave->ndev);
2371 }
2372
2373 /* Handle rest of tx packets and stop cpdma channels */
2374 cpdma_ctlr_stop(cpsw->dma);
2375 ret = cpsw_update_channels(priv, chs);
2376 if (ret)
2377 goto err;
2378
2379 for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++) {
2380 if (!(slave->ndev && netif_running(slave->ndev)))
2381 continue;
2382
2383 /* Inform stack about new count of queues */
2384 ret = netif_set_real_num_tx_queues(slave->ndev,
2385 cpsw->tx_ch_num);
2386 if (ret) {
2387 dev_err(priv->dev, "cannot set real number of tx queues\n");
2388 goto err;
2389 }
2390
2391 ret = netif_set_real_num_rx_queues(slave->ndev,
2392 cpsw->rx_ch_num);
2393 if (ret) {
2394 dev_err(priv->dev, "cannot set real number of rx queues\n");
2395 goto err;
2396 }
2397
2398 /* Enable rx packets handling */
2399 netif_dormant_off(slave->ndev);
2400 }
2401
2402 if (cpsw_common_res_usage_state(cpsw)) {
Wei Yongjune19ac152016-08-26 14:35:43 +00002403 ret = cpsw_fill_rx_channels(priv);
2404 if (ret)
Ivan Khoronzhukce52c742016-08-22 21:18:28 +03002405 goto err;
2406
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02002407 cpsw_split_budget(ndev);
2408
Ivan Khoronzhukce52c742016-08-22 21:18:28 +03002409 /* After this receive is started */
2410 cpdma_ctlr_start(cpsw->dma);
2411 cpsw_intr_enable(cpsw);
2412 }
2413
2414 /* Resume transmit for every affected interface */
2415 for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++) {
2416 if (!(slave->ndev && netif_running(slave->ndev)))
2417 continue;
2418 netif_tx_start_all_queues(slave->ndev);
2419 }
2420 return 0;
2421err:
2422 dev_err(priv->dev, "cannot update channels number, closing device\n");
2423 dev_close(ndev);
2424 return ret;
2425}
2426
Yegor Yefremova0909942016-11-28 09:41:33 +01002427static int cpsw_get_eee(struct net_device *ndev, struct ethtool_eee *edata)
2428{
2429 struct cpsw_priv *priv = netdev_priv(ndev);
2430 struct cpsw_common *cpsw = priv->cpsw;
2431 int slave_no = cpsw_slave_index(cpsw, priv);
2432
2433 if (cpsw->slaves[slave_no].phy)
2434 return phy_ethtool_get_eee(cpsw->slaves[slave_no].phy, edata);
2435 else
2436 return -EOPNOTSUPP;
2437}
2438
2439static int cpsw_set_eee(struct net_device *ndev, struct ethtool_eee *edata)
2440{
2441 struct cpsw_priv *priv = netdev_priv(ndev);
2442 struct cpsw_common *cpsw = priv->cpsw;
2443 int slave_no = cpsw_slave_index(cpsw, priv);
2444
2445 if (cpsw->slaves[slave_no].phy)
2446 return phy_ethtool_set_eee(cpsw->slaves[slave_no].phy, edata);
2447 else
2448 return -EOPNOTSUPP;
2449}
2450
Yegor Yefremov6bb10c22016-11-28 10:47:52 +01002451static int cpsw_nway_reset(struct net_device *ndev)
2452{
2453 struct cpsw_priv *priv = netdev_priv(ndev);
2454 struct cpsw_common *cpsw = priv->cpsw;
2455 int slave_no = cpsw_slave_index(cpsw, priv);
2456
2457 if (cpsw->slaves[slave_no].phy)
2458 return genphy_restart_aneg(cpsw->slaves[slave_no].phy);
2459 else
2460 return -EOPNOTSUPP;
2461}
2462
Mugunthan V Ndf828592012-03-18 20:17:54 +00002463static const struct ethtool_ops cpsw_ethtool_ops = {
2464 .get_drvinfo = cpsw_get_drvinfo,
2465 .get_msglevel = cpsw_get_msglevel,
2466 .set_msglevel = cpsw_set_msglevel,
2467 .get_link = ethtool_op_get_link,
Richard Cochran2e5b38a2012-10-29 08:45:20 +00002468 .get_ts_info = cpsw_get_ts_info,
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00002469 .get_coalesce = cpsw_get_coalesce,
2470 .set_coalesce = cpsw_set_coalesce,
Mugunthan V Nd9718542013-07-23 15:38:17 +05302471 .get_sset_count = cpsw_get_sset_count,
2472 .get_strings = cpsw_get_strings,
2473 .get_ethtool_stats = cpsw_get_ethtool_stats,
Mugunthan V N1923d6e2014-09-08 22:54:02 +05302474 .get_pauseparam = cpsw_get_pauseparam,
2475 .set_pauseparam = cpsw_set_pauseparam,
Matus Ujhelyid8a64422013-08-20 07:59:38 +02002476 .get_wol = cpsw_get_wol,
2477 .set_wol = cpsw_set_wol,
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05302478 .get_regs_len = cpsw_get_regs_len,
2479 .get_regs = cpsw_get_regs,
Grygorii Strashko7898b1d2016-06-24 21:23:44 +03002480 .begin = cpsw_ethtool_op_begin,
2481 .complete = cpsw_ethtool_op_complete,
Ivan Khoronzhukce52c742016-08-22 21:18:28 +03002482 .get_channels = cpsw_get_channels,
2483 .set_channels = cpsw_set_channels,
Philippe Reynes24798762016-10-08 17:46:15 +02002484 .get_link_ksettings = cpsw_get_link_ksettings,
2485 .set_link_ksettings = cpsw_set_link_ksettings,
Yegor Yefremova0909942016-11-28 09:41:33 +01002486 .get_eee = cpsw_get_eee,
2487 .set_eee = cpsw_set_eee,
Yegor Yefremov6bb10c22016-11-28 10:47:52 +01002488 .nway_reset = cpsw_nway_reset,
Mugunthan V Ndf828592012-03-18 20:17:54 +00002489};
2490
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002491static void cpsw_slave_init(struct cpsw_slave *slave, struct cpsw_common *cpsw,
Richard Cochran549985e2012-11-14 09:07:56 +00002492 u32 slave_reg_ofs, u32 sliver_reg_ofs)
Mugunthan V Ndf828592012-03-18 20:17:54 +00002493{
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03002494 void __iomem *regs = cpsw->regs;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002495 int slave_num = slave->slave_num;
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002496 struct cpsw_slave_data *data = cpsw->data.slave_data + slave_num;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002497
2498 slave->data = data;
Richard Cochran549985e2012-11-14 09:07:56 +00002499 slave->regs = regs + slave_reg_ofs;
2500 slave->sliver = regs + sliver_reg_ofs;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002501 slave->port_vlan = data->dual_emac_res_vlan;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002502}
2503
David Rivshin552165b2016-04-27 21:25:25 -04002504static int cpsw_probe_dt(struct cpsw_platform_data *data,
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002505 struct platform_device *pdev)
2506{
2507 struct device_node *node = pdev->dev.of_node;
2508 struct device_node *slave_node;
2509 int i = 0, ret;
2510 u32 prop;
2511
2512 if (!node)
2513 return -EINVAL;
2514
2515 if (of_property_read_u32(node, "slaves", &prop)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302516 dev_err(&pdev->dev, "Missing slaves property in the DT.\n");
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002517 return -EINVAL;
2518 }
2519 data->slaves = prop;
2520
Mugunthan V Ne86ac132013-03-11 23:16:35 +00002521 if (of_property_read_u32(node, "active_slave", &prop)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302522 dev_err(&pdev->dev, "Missing active_slave property in the DT.\n");
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302523 return -EINVAL;
Richard Cochran78ca0b22012-10-29 08:45:18 +00002524 }
Mugunthan V Ne86ac132013-03-11 23:16:35 +00002525 data->active_slave = prop;
Richard Cochran78ca0b22012-10-29 08:45:18 +00002526
Richard Cochran00ab94e2012-10-29 08:45:19 +00002527 if (of_property_read_u32(node, "cpts_clock_mult", &prop)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302528 dev_err(&pdev->dev, "Missing cpts_clock_mult property in the DT.\n");
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302529 return -EINVAL;
Richard Cochran00ab94e2012-10-29 08:45:19 +00002530 }
2531 data->cpts_clock_mult = prop;
2532
2533 if (of_property_read_u32(node, "cpts_clock_shift", &prop)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302534 dev_err(&pdev->dev, "Missing cpts_clock_shift property in the DT.\n");
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302535 return -EINVAL;
Richard Cochran00ab94e2012-10-29 08:45:19 +00002536 }
2537 data->cpts_clock_shift = prop;
2538
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302539 data->slave_data = devm_kzalloc(&pdev->dev, data->slaves
2540 * sizeof(struct cpsw_slave_data),
2541 GFP_KERNEL);
Joe Perchesb2adaca2013-02-03 17:43:58 +00002542 if (!data->slave_data)
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302543 return -ENOMEM;
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002544
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002545 if (of_property_read_u32(node, "cpdma_channels", &prop)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302546 dev_err(&pdev->dev, "Missing cpdma_channels property in the DT.\n");
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302547 return -EINVAL;
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002548 }
2549 data->channels = prop;
2550
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002551 if (of_property_read_u32(node, "ale_entries", &prop)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302552 dev_err(&pdev->dev, "Missing ale_entries property in the DT.\n");
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302553 return -EINVAL;
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002554 }
2555 data->ale_entries = prop;
2556
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002557 if (of_property_read_u32(node, "bd_ram_size", &prop)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302558 dev_err(&pdev->dev, "Missing bd_ram_size property in the DT.\n");
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302559 return -EINVAL;
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002560 }
2561 data->bd_ram_size = prop;
2562
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002563 if (of_property_read_u32(node, "mac_control", &prop)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302564 dev_err(&pdev->dev, "Missing mac_control property in the DT.\n");
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302565 return -EINVAL;
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002566 }
2567 data->mac_control = prop;
2568
Markus Pargmann281abd92013-10-04 14:44:40 +02002569 if (of_property_read_bool(node, "dual_emac"))
2570 data->dual_emac = 1;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002571
Vaibhav Hiremath1fb19aa2012-11-14 09:07:55 +00002572 /*
2573 * Populate all the child nodes here...
2574 */
2575 ret = of_platform_populate(node, NULL, NULL, &pdev->dev);
2576 /* We do not want to force this, as in some cases may not have child */
2577 if (ret)
George Cherian88c99ff2014-05-12 10:21:19 +05302578 dev_warn(&pdev->dev, "Doesn't have any child node\n");
Vaibhav Hiremath1fb19aa2012-11-14 09:07:55 +00002579
Ben Hutchings8658aaf2016-06-21 01:16:31 +01002580 for_each_available_child_of_node(node, slave_node) {
Richard Cochran549985e2012-11-14 09:07:56 +00002581 struct cpsw_slave_data *slave_data = data->slave_data + i;
2582 const void *mac_addr = NULL;
Richard Cochran549985e2012-11-14 09:07:56 +00002583 int lenp;
2584 const __be32 *parp;
Richard Cochran549985e2012-11-14 09:07:56 +00002585
Markus Pargmannf468b102013-10-04 14:44:39 +02002586 /* This is no slave child node, continue */
2587 if (strcmp(slave_node->name, "slave"))
2588 continue;
2589
David Rivshin552165b2016-04-27 21:25:25 -04002590 slave_data->phy_node = of_parse_phandle(slave_node,
2591 "phy-handle", 0);
David Rivshinf1eea5c2015-12-16 23:02:10 -05002592 parp = of_get_property(slave_node, "phy_id", &lenp);
David Rivshinae092b52016-04-27 21:38:26 -04002593 if (slave_data->phy_node) {
2594 dev_dbg(&pdev->dev,
2595 "slave[%d] using phy-handle=\"%s\"\n",
2596 i, slave_data->phy_node->full_name);
2597 } else if (of_phy_is_fixed_link(slave_node)) {
David Rivshindfc0a6d2015-12-16 23:02:11 -05002598 /* In the case of a fixed PHY, the DT node associated
2599 * to the PHY is the Ethernet MAC DT node.
2600 */
Markus Brunner1f71e8c2015-11-03 22:09:51 +01002601 ret = of_phy_register_fixed_link(slave_node);
Johan Hovold23a09872016-11-17 17:40:04 +01002602 if (ret) {
2603 if (ret != -EPROBE_DEFER)
2604 dev_err(&pdev->dev, "failed to register fixed-link phy: %d\n", ret);
Markus Brunner1f71e8c2015-11-03 22:09:51 +01002605 return ret;
Johan Hovold23a09872016-11-17 17:40:04 +01002606 }
David Rivshin06cd6d62016-04-27 21:45:45 -04002607 slave_data->phy_node = of_node_get(slave_node);
David Rivshinf1eea5c2015-12-16 23:02:10 -05002608 } else if (parp) {
2609 u32 phyid;
2610 struct device_node *mdio_node;
2611 struct platform_device *mdio;
2612
2613 if (lenp != (sizeof(__be32) * 2)) {
2614 dev_err(&pdev->dev, "Invalid slave[%d] phy_id property\n", i);
2615 goto no_phy_slave;
2616 }
2617 mdio_node = of_find_node_by_phandle(be32_to_cpup(parp));
2618 phyid = be32_to_cpup(parp+1);
2619 mdio = of_find_device_by_node(mdio_node);
2620 of_node_put(mdio_node);
2621 if (!mdio) {
2622 dev_err(&pdev->dev, "Missing mdio platform device\n");
2623 return -EINVAL;
2624 }
2625 snprintf(slave_data->phy_id, sizeof(slave_data->phy_id),
2626 PHY_ID_FMT, mdio->name, phyid);
Johan Hovold86e1d5a2016-11-17 17:39:59 +01002627 put_device(&mdio->dev);
David Rivshinf1eea5c2015-12-16 23:02:10 -05002628 } else {
David Rivshinae092b52016-04-27 21:38:26 -04002629 dev_err(&pdev->dev,
2630 "No slave[%d] phy_id, phy-handle, or fixed-link property\n",
2631 i);
Markus Brunner1f71e8c2015-11-03 22:09:51 +01002632 goto no_phy_slave;
2633 }
Mugunthan V N47276fc2014-10-24 18:51:33 +05302634 slave_data->phy_if = of_get_phy_mode(slave_node);
2635 if (slave_data->phy_if < 0) {
2636 dev_err(&pdev->dev, "Missing or malformed slave[%d] phy-mode property\n",
2637 i);
2638 return slave_data->phy_if;
2639 }
2640
2641no_phy_slave:
Richard Cochran549985e2012-11-14 09:07:56 +00002642 mac_addr = of_get_mac_address(slave_node);
Markus Pargmann0ba517b2014-09-29 08:53:17 +02002643 if (mac_addr) {
Richard Cochran549985e2012-11-14 09:07:56 +00002644 memcpy(slave_data->mac_addr, mac_addr, ETH_ALEN);
Markus Pargmann0ba517b2014-09-29 08:53:17 +02002645 } else {
Mugunthan V Nb6745f62015-09-21 15:56:50 +05302646 ret = ti_cm_get_macid(&pdev->dev, i,
2647 slave_data->mac_addr);
2648 if (ret)
2649 return ret;
Markus Pargmann0ba517b2014-09-29 08:53:17 +02002650 }
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002651 if (data->dual_emac) {
Mugunthan V N91c41662013-04-15 07:31:28 +00002652 if (of_property_read_u32(slave_node, "dual_emac_res_vlan",
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002653 &prop)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302654 dev_err(&pdev->dev, "Missing dual_emac_res_vlan in DT.\n");
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002655 slave_data->dual_emac_res_vlan = i+1;
George Cherian88c99ff2014-05-12 10:21:19 +05302656 dev_err(&pdev->dev, "Using %d as Reserved VLAN for %d slave\n",
2657 slave_data->dual_emac_res_vlan, i);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002658 } else {
2659 slave_data->dual_emac_res_vlan = prop;
2660 }
2661 }
2662
Richard Cochran549985e2012-11-14 09:07:56 +00002663 i++;
Mugunthan V N3a27bfa2013-12-02 12:53:39 +05302664 if (i == data->slaves)
2665 break;
Richard Cochran549985e2012-11-14 09:07:56 +00002666 }
2667
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002668 return 0;
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002669}
2670
Johan Hovolda4e32b02016-11-17 17:40:00 +01002671static void cpsw_remove_dt(struct platform_device *pdev)
2672{
Johan Hovold8cbcc462016-11-17 17:40:01 +01002673 struct net_device *ndev = platform_get_drvdata(pdev);
2674 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
2675 struct cpsw_platform_data *data = &cpsw->data;
2676 struct device_node *node = pdev->dev.of_node;
2677 struct device_node *slave_node;
2678 int i = 0;
2679
2680 for_each_available_child_of_node(node, slave_node) {
2681 struct cpsw_slave_data *slave_data = &data->slave_data[i];
2682
2683 if (strcmp(slave_node->name, "slave"))
2684 continue;
2685
Johan Hovold3f650472016-11-28 19:24:55 +01002686 if (of_phy_is_fixed_link(slave_node))
2687 of_phy_deregister_fixed_link(slave_node);
Johan Hovold8cbcc462016-11-17 17:40:01 +01002688
2689 of_node_put(slave_data->phy_node);
2690
2691 i++;
2692 if (i == data->slaves)
2693 break;
2694 }
2695
Johan Hovolda4e32b02016-11-17 17:40:00 +01002696 of_platform_depopulate(&pdev->dev);
2697}
2698
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002699static int cpsw_probe_dual_emac(struct cpsw_priv *priv)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002700{
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002701 struct cpsw_common *cpsw = priv->cpsw;
2702 struct cpsw_platform_data *data = &cpsw->data;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002703 struct net_device *ndev;
2704 struct cpsw_priv *priv_sl2;
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +03002705 int ret = 0;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002706
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03002707 ndev = alloc_etherdev_mq(sizeof(struct cpsw_priv), CPSW_MAX_QUEUES);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002708 if (!ndev) {
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002709 dev_err(cpsw->dev, "cpsw: error allocating net_device\n");
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002710 return -ENOMEM;
2711 }
2712
2713 priv_sl2 = netdev_priv(ndev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002714 priv_sl2->cpsw = cpsw;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002715 priv_sl2->ndev = ndev;
2716 priv_sl2->dev = &ndev->dev;
2717 priv_sl2->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002718
2719 if (is_valid_ether_addr(data->slave_data[1].mac_addr)) {
2720 memcpy(priv_sl2->mac_addr, data->slave_data[1].mac_addr,
2721 ETH_ALEN);
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002722 dev_info(cpsw->dev, "cpsw: Detected MACID = %pM\n",
2723 priv_sl2->mac_addr);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002724 } else {
2725 random_ether_addr(priv_sl2->mac_addr);
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002726 dev_info(cpsw->dev, "cpsw: Random MACID = %pM\n",
2727 priv_sl2->mac_addr);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002728 }
2729 memcpy(ndev->dev_addr, priv_sl2->mac_addr, ETH_ALEN);
2730
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002731 priv_sl2->emac_port = 1;
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002732 cpsw->slaves[1].ndev = ndev;
Patrick McHardyf6469682013-04-19 02:04:27 +00002733 ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002734
2735 ndev->netdev_ops = &cpsw_netdev_ops;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00002736 ndev->ethtool_ops = &cpsw_ethtool_ops;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002737
2738 /* register the network device */
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002739 SET_NETDEV_DEV(ndev, cpsw->dev);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002740 ret = register_netdev(ndev);
2741 if (ret) {
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002742 dev_err(cpsw->dev, "cpsw: error registering net device\n");
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002743 free_netdev(ndev);
2744 ret = -ENODEV;
2745 }
2746
2747 return ret;
2748}
2749
Mugunthan V N7da11602015-08-12 15:22:53 +05302750#define CPSW_QUIRK_IRQ BIT(0)
2751
2752static struct platform_device_id cpsw_devtype[] = {
2753 {
2754 /* keep it for existing comaptibles */
2755 .name = "cpsw",
2756 .driver_data = CPSW_QUIRK_IRQ,
2757 }, {
2758 .name = "am335x-cpsw",
2759 .driver_data = CPSW_QUIRK_IRQ,
2760 }, {
2761 .name = "am4372-cpsw",
2762 .driver_data = 0,
2763 }, {
2764 .name = "dra7-cpsw",
2765 .driver_data = 0,
2766 }, {
2767 /* sentinel */
2768 }
2769};
2770MODULE_DEVICE_TABLE(platform, cpsw_devtype);
2771
2772enum ti_cpsw_type {
2773 CPSW = 0,
2774 AM335X_CPSW,
2775 AM4372_CPSW,
2776 DRA7_CPSW,
2777};
2778
2779static const struct of_device_id cpsw_of_mtable[] = {
2780 { .compatible = "ti,cpsw", .data = &cpsw_devtype[CPSW], },
2781 { .compatible = "ti,am335x-cpsw", .data = &cpsw_devtype[AM335X_CPSW], },
2782 { .compatible = "ti,am4372-cpsw", .data = &cpsw_devtype[AM4372_CPSW], },
2783 { .compatible = "ti,dra7-cpsw", .data = &cpsw_devtype[DRA7_CPSW], },
2784 { /* sentinel */ },
2785};
2786MODULE_DEVICE_TABLE(of, cpsw_of_mtable);
2787
Bill Pemberton663e12e2012-12-03 09:23:45 -05002788static int cpsw_probe(struct platform_device *pdev)
Mugunthan V Ndf828592012-03-18 20:17:54 +00002789{
Ivan Khoronzhukef4183a2016-08-10 02:22:35 +03002790 struct clk *clk;
Sebastian Siewiord1bd9ac2013-04-24 08:48:23 +00002791 struct cpsw_platform_data *data;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002792 struct net_device *ndev;
2793 struct cpsw_priv *priv;
2794 struct cpdma_params dma_params;
2795 struct cpsw_ale_params ale_params;
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302796 void __iomem *ss_regs;
Grygorii Strashko8a2c9a52016-12-06 18:00:41 -06002797 void __iomem *cpts_regs;
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302798 struct resource *res, *ss_res;
Mugunthan V N7da11602015-08-12 15:22:53 +05302799 const struct of_device_id *of_id;
Mugunthan V N1d147cc2015-09-07 15:16:44 +05302800 struct gpio_descs *mode;
Richard Cochran549985e2012-11-14 09:07:56 +00002801 u32 slave_offset, sliver_offset, slave_size;
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03002802 struct cpsw_common *cpsw;
Felipe Balbi5087b912015-01-16 10:11:11 -06002803 int ret = 0, i;
2804 int irq;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002805
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03002806 cpsw = devm_kzalloc(&pdev->dev, sizeof(struct cpsw_common), GFP_KERNEL);
Johan Hovold3420ea82016-11-17 17:40:03 +01002807 if (!cpsw)
2808 return -ENOMEM;
2809
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002810 cpsw->dev = &pdev->dev;
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03002811
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03002812 ndev = alloc_etherdev_mq(sizeof(struct cpsw_priv), CPSW_MAX_QUEUES);
Mugunthan V Ndf828592012-03-18 20:17:54 +00002813 if (!ndev) {
George Cherian88c99ff2014-05-12 10:21:19 +05302814 dev_err(&pdev->dev, "error allocating net_device\n");
Mugunthan V Ndf828592012-03-18 20:17:54 +00002815 return -ENOMEM;
2816 }
2817
2818 platform_set_drvdata(pdev, ndev);
2819 priv = netdev_priv(ndev);
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03002820 priv->cpsw = cpsw;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002821 priv->ndev = ndev;
2822 priv->dev = &ndev->dev;
2823 priv->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002824 cpsw->rx_packet_max = max(rx_packet_max, 128);
Mugunthan V Ndf828592012-03-18 20:17:54 +00002825
Mugunthan V N1d147cc2015-09-07 15:16:44 +05302826 mode = devm_gpiod_get_array_optional(&pdev->dev, "mode", GPIOD_OUT_LOW);
2827 if (IS_ERR(mode)) {
2828 ret = PTR_ERR(mode);
2829 dev_err(&pdev->dev, "gpio request failed, ret %d\n", ret);
2830 goto clean_ndev_ret;
2831 }
2832
Vaibhav Hiremath1fb19aa2012-11-14 09:07:55 +00002833 /*
2834 * This may be required here for child devices.
2835 */
2836 pm_runtime_enable(&pdev->dev);
2837
Mugunthan V N739683b2013-06-06 23:45:14 +05302838 /* Select default pin state */
2839 pinctrl_pm_select_default_state(&pdev->dev);
2840
Johan Hovolda4e32b02016-11-17 17:40:00 +01002841 /* Need to enable clocks with runtime PM api to access module
2842 * registers
2843 */
2844 ret = pm_runtime_get_sync(&pdev->dev);
2845 if (ret < 0) {
2846 pm_runtime_put_noidle(&pdev->dev);
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302847 goto clean_runtime_disable_ret;
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002848 }
Johan Hovolda4e32b02016-11-17 17:40:00 +01002849
Johan Hovold23a09872016-11-17 17:40:04 +01002850 ret = cpsw_probe_dt(&cpsw->data, pdev);
2851 if (ret)
Johan Hovolda4e32b02016-11-17 17:40:00 +01002852 goto clean_dt_ret;
Johan Hovold23a09872016-11-17 17:40:04 +01002853
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002854 data = &cpsw->data;
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03002855 cpsw->rx_ch_num = 1;
2856 cpsw->tx_ch_num = 1;
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002857
Mugunthan V Ndf828592012-03-18 20:17:54 +00002858 if (is_valid_ether_addr(data->slave_data[0].mac_addr)) {
2859 memcpy(priv->mac_addr, data->slave_data[0].mac_addr, ETH_ALEN);
George Cherian88c99ff2014-05-12 10:21:19 +05302860 dev_info(&pdev->dev, "Detected MACID = %pM\n", priv->mac_addr);
Mugunthan V Ndf828592012-03-18 20:17:54 +00002861 } else {
Joe Perches7efd26d2012-07-12 19:33:06 +00002862 eth_random_addr(priv->mac_addr);
George Cherian88c99ff2014-05-12 10:21:19 +05302863 dev_info(&pdev->dev, "Random MACID = %pM\n", priv->mac_addr);
Mugunthan V Ndf828592012-03-18 20:17:54 +00002864 }
2865
2866 memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
2867
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002868 cpsw->slaves = devm_kzalloc(&pdev->dev,
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302869 sizeof(struct cpsw_slave) * data->slaves,
2870 GFP_KERNEL);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002871 if (!cpsw->slaves) {
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302872 ret = -ENOMEM;
Johan Hovolda4e32b02016-11-17 17:40:00 +01002873 goto clean_dt_ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002874 }
2875 for (i = 0; i < data->slaves; i++)
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002876 cpsw->slaves[i].slave_num = i;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002877
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002878 cpsw->slaves[0].ndev = ndev;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002879 priv->emac_port = 0;
2880
Ivan Khoronzhukef4183a2016-08-10 02:22:35 +03002881 clk = devm_clk_get(&pdev->dev, "fck");
2882 if (IS_ERR(clk)) {
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302883 dev_err(priv->dev, "fck is not found\n");
Mugunthan V Nf150bd72012-07-17 08:09:50 +00002884 ret = -ENODEV;
Johan Hovolda4e32b02016-11-17 17:40:00 +01002885 goto clean_dt_ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002886 }
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002887 cpsw->bus_freq_mhz = clk_get_rate(clk) / 1000000;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002888
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302889 ss_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2890 ss_regs = devm_ioremap_resource(&pdev->dev, ss_res);
2891 if (IS_ERR(ss_regs)) {
2892 ret = PTR_ERR(ss_regs);
Johan Hovolda4e32b02016-11-17 17:40:00 +01002893 goto clean_dt_ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002894 }
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03002895 cpsw->regs = ss_regs;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002896
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002897 cpsw->version = readl(&cpsw->regs->id_ver);
Mugunthan V Nf280e892013-12-11 22:09:05 -06002898
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302899 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03002900 cpsw->wr_regs = devm_ioremap_resource(&pdev->dev, res);
2901 if (IS_ERR(cpsw->wr_regs)) {
2902 ret = PTR_ERR(cpsw->wr_regs);
Johan Hovolda4e32b02016-11-17 17:40:00 +01002903 goto clean_dt_ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002904 }
Mugunthan V Ndf828592012-03-18 20:17:54 +00002905
2906 memset(&dma_params, 0, sizeof(dma_params));
Richard Cochran549985e2012-11-14 09:07:56 +00002907 memset(&ale_params, 0, sizeof(ale_params));
2908
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002909 switch (cpsw->version) {
Richard Cochran549985e2012-11-14 09:07:56 +00002910 case CPSW_VERSION_1:
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03002911 cpsw->host_port_regs = ss_regs + CPSW1_HOST_PORT_OFFSET;
Grygorii Strashko8a2c9a52016-12-06 18:00:41 -06002912 cpts_regs = ss_regs + CPSW1_CPTS_OFFSET;
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03002913 cpsw->hw_stats = ss_regs + CPSW1_HW_STATS;
Richard Cochran549985e2012-11-14 09:07:56 +00002914 dma_params.dmaregs = ss_regs + CPSW1_CPDMA_OFFSET;
2915 dma_params.txhdp = ss_regs + CPSW1_STATERAM_OFFSET;
2916 ale_params.ale_regs = ss_regs + CPSW1_ALE_OFFSET;
2917 slave_offset = CPSW1_SLAVE_OFFSET;
2918 slave_size = CPSW1_SLAVE_SIZE;
2919 sliver_offset = CPSW1_SLIVER_OFFSET;
2920 dma_params.desc_mem_phys = 0;
2921 break;
2922 case CPSW_VERSION_2:
Mugunthan V Nc193f362013-08-05 17:30:05 +05302923 case CPSW_VERSION_3:
Mugunthan V N926489b2013-08-12 17:11:15 +05302924 case CPSW_VERSION_4:
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03002925 cpsw->host_port_regs = ss_regs + CPSW2_HOST_PORT_OFFSET;
Grygorii Strashko8a2c9a52016-12-06 18:00:41 -06002926 cpts_regs = ss_regs + CPSW2_CPTS_OFFSET;
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03002927 cpsw->hw_stats = ss_regs + CPSW2_HW_STATS;
Richard Cochran549985e2012-11-14 09:07:56 +00002928 dma_params.dmaregs = ss_regs + CPSW2_CPDMA_OFFSET;
2929 dma_params.txhdp = ss_regs + CPSW2_STATERAM_OFFSET;
2930 ale_params.ale_regs = ss_regs + CPSW2_ALE_OFFSET;
2931 slave_offset = CPSW2_SLAVE_OFFSET;
2932 slave_size = CPSW2_SLAVE_SIZE;
2933 sliver_offset = CPSW2_SLIVER_OFFSET;
2934 dma_params.desc_mem_phys =
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302935 (u32 __force) ss_res->start + CPSW2_BD_OFFSET;
Richard Cochran549985e2012-11-14 09:07:56 +00002936 break;
2937 default:
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002938 dev_err(priv->dev, "unknown version 0x%08x\n", cpsw->version);
Richard Cochran549985e2012-11-14 09:07:56 +00002939 ret = -ENODEV;
Johan Hovolda4e32b02016-11-17 17:40:00 +01002940 goto clean_dt_ret;
Richard Cochran549985e2012-11-14 09:07:56 +00002941 }
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002942 for (i = 0; i < cpsw->data.slaves; i++) {
2943 struct cpsw_slave *slave = &cpsw->slaves[i];
2944
2945 cpsw_slave_init(slave, cpsw, slave_offset, sliver_offset);
Richard Cochran549985e2012-11-14 09:07:56 +00002946 slave_offset += slave_size;
2947 sliver_offset += SLIVER_SIZE;
2948 }
2949
Mugunthan V Ndf828592012-03-18 20:17:54 +00002950 dma_params.dev = &pdev->dev;
Richard Cochran549985e2012-11-14 09:07:56 +00002951 dma_params.rxthresh = dma_params.dmaregs + CPDMA_RXTHRESH;
2952 dma_params.rxfree = dma_params.dmaregs + CPDMA_RXFREE;
2953 dma_params.rxhdp = dma_params.txhdp + CPDMA_RXHDP;
2954 dma_params.txcp = dma_params.txhdp + CPDMA_TXCP;
2955 dma_params.rxcp = dma_params.txhdp + CPDMA_RXCP;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002956
2957 dma_params.num_chan = data->channels;
2958 dma_params.has_soft_reset = true;
2959 dma_params.min_packet_size = CPSW_MIN_PACKET_SIZE;
2960 dma_params.desc_mem_size = data->bd_ram_size;
2961 dma_params.desc_align = 16;
2962 dma_params.has_ext_regs = true;
Richard Cochran549985e2012-11-14 09:07:56 +00002963 dma_params.desc_hw_addr = dma_params.desc_mem_phys;
Ivan Khoronzhuk83fcad02016-11-29 17:00:49 +02002964 dma_params.bus_freq_mhz = cpsw->bus_freq_mhz;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002965
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03002966 cpsw->dma = cpdma_ctlr_create(&dma_params);
2967 if (!cpsw->dma) {
Mugunthan V Ndf828592012-03-18 20:17:54 +00002968 dev_err(priv->dev, "error initializing dma\n");
2969 ret = -ENOMEM;
Johan Hovolda4e32b02016-11-17 17:40:00 +01002970 goto clean_dt_ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002971 }
2972
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02002973 cpsw->txv[0].ch = cpdma_chan_create(cpsw->dma, 0, cpsw_tx_handler, 0);
2974 cpsw->rxv[0].ch = cpdma_chan_create(cpsw->dma, 0, cpsw_rx_handler, 1);
2975 if (WARN_ON(!cpsw->rxv[0].ch || !cpsw->txv[0].ch)) {
Mugunthan V Ndf828592012-03-18 20:17:54 +00002976 dev_err(priv->dev, "error initializing dma channels\n");
2977 ret = -ENOMEM;
2978 goto clean_dma_ret;
2979 }
2980
Mugunthan V Ndf828592012-03-18 20:17:54 +00002981 ale_params.dev = &ndev->dev;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002982 ale_params.ale_ageout = ale_ageout;
2983 ale_params.ale_entries = data->ale_entries;
2984 ale_params.ale_ports = data->slaves;
2985
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002986 cpsw->ale = cpsw_ale_create(&ale_params);
2987 if (!cpsw->ale) {
Mugunthan V Ndf828592012-03-18 20:17:54 +00002988 dev_err(priv->dev, "error initializing ale engine\n");
2989 ret = -ENODEV;
2990 goto clean_dma_ret;
2991 }
2992
Grygorii Strashko8a2c9a52016-12-06 18:00:41 -06002993 cpsw->cpts = cpts_create(cpsw->dev, cpts_regs,
2994 cpsw->data.cpts_clock_mult,
2995 cpsw->data.cpts_clock_shift);
2996 if (IS_ERR(cpsw->cpts)) {
2997 ret = PTR_ERR(cpsw->cpts);
2998 goto clean_ale_ret;
2999 }
3000
Felipe Balbic03abd82015-01-16 10:11:12 -06003001 ndev->irq = platform_get_irq(pdev, 1);
Mugunthan V Ndf828592012-03-18 20:17:54 +00003002 if (ndev->irq < 0) {
3003 dev_err(priv->dev, "error getting irq resource\n");
Julia Lawallc1e33342015-12-26 20:12:13 +01003004 ret = ndev->irq;
Mugunthan V Ndf828592012-03-18 20:17:54 +00003005 goto clean_ale_ret;
3006 }
3007
Mugunthan V N7da11602015-08-12 15:22:53 +05303008 of_id = of_match_device(cpsw_of_mtable, &pdev->dev);
3009 if (of_id) {
3010 pdev->id_entry = of_id->data;
3011 if (pdev->id_entry->driver_data)
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +03003012 cpsw->quirk_irq = true;
Mugunthan V N7da11602015-08-12 15:22:53 +05303013 }
3014
Felipe Balbic03abd82015-01-16 10:11:12 -06003015 /* Grab RX and TX IRQs. Note that we also have RX_THRESHOLD and
3016 * MISC IRQs which are always kept disabled with this driver so
3017 * we will not request them.
3018 *
3019 * If anyone wants to implement support for those, make sure to
3020 * first request and append them to irqs_table array.
3021 */
Daniel Mackc2b32e52014-09-04 09:00:23 +02003022
Felipe Balbic03abd82015-01-16 10:11:12 -06003023 /* RX IRQ */
Felipe Balbi5087b912015-01-16 10:11:11 -06003024 irq = platform_get_irq(pdev, 1);
Julia Lawallc1e33342015-12-26 20:12:13 +01003025 if (irq < 0) {
3026 ret = irq;
Felipe Balbi5087b912015-01-16 10:11:11 -06003027 goto clean_ale_ret;
Julia Lawallc1e33342015-12-26 20:12:13 +01003028 }
Felipe Balbi5087b912015-01-16 10:11:11 -06003029
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +03003030 cpsw->irqs_table[0] = irq;
Felipe Balbic03abd82015-01-16 10:11:12 -06003031 ret = devm_request_irq(&pdev->dev, irq, cpsw_rx_interrupt,
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +03003032 0, dev_name(&pdev->dev), cpsw);
Felipe Balbi5087b912015-01-16 10:11:11 -06003033 if (ret < 0) {
3034 dev_err(priv->dev, "error attaching irq (%d)\n", ret);
3035 goto clean_ale_ret;
3036 }
3037
Felipe Balbic03abd82015-01-16 10:11:12 -06003038 /* TX IRQ */
Felipe Balbi5087b912015-01-16 10:11:11 -06003039 irq = platform_get_irq(pdev, 2);
Julia Lawallc1e33342015-12-26 20:12:13 +01003040 if (irq < 0) {
3041 ret = irq;
Felipe Balbi5087b912015-01-16 10:11:11 -06003042 goto clean_ale_ret;
Julia Lawallc1e33342015-12-26 20:12:13 +01003043 }
Felipe Balbi5087b912015-01-16 10:11:11 -06003044
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +03003045 cpsw->irqs_table[1] = irq;
Felipe Balbic03abd82015-01-16 10:11:12 -06003046 ret = devm_request_irq(&pdev->dev, irq, cpsw_tx_interrupt,
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +03003047 0, dev_name(&pdev->dev), cpsw);
Felipe Balbi5087b912015-01-16 10:11:11 -06003048 if (ret < 0) {
3049 dev_err(priv->dev, "error attaching irq (%d)\n", ret);
3050 goto clean_ale_ret;
3051 }
Daniel Mackc2b32e52014-09-04 09:00:23 +02003052
Patrick McHardyf6469682013-04-19 02:04:27 +00003053 ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
Mugunthan V Ndf828592012-03-18 20:17:54 +00003054
3055 ndev->netdev_ops = &cpsw_netdev_ops;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00003056 ndev->ethtool_ops = &cpsw_ethtool_ops;
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +03003057 netif_napi_add(ndev, &cpsw->napi_rx, cpsw_rx_poll, CPSW_POLL_WEIGHT);
3058 netif_tx_napi_add(ndev, &cpsw->napi_tx, cpsw_tx_poll, CPSW_POLL_WEIGHT);
Mugunthan V Ndf828592012-03-18 20:17:54 +00003059
3060 /* register the network device */
3061 SET_NETDEV_DEV(ndev, &pdev->dev);
3062 ret = register_netdev(ndev);
3063 if (ret) {
3064 dev_err(priv->dev, "error registering net device\n");
3065 ret = -ENODEV;
Daniel Mackaa1a15e2013-09-21 00:50:38 +05303066 goto clean_ale_ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00003067 }
3068
Olof Johansson1a3b5052013-12-11 15:58:07 -08003069 cpsw_notice(priv, probe, "initialized device (regs %pa, irq %d)\n",
3070 &ss_res->start, ndev->irq);
Mugunthan V Ndf828592012-03-18 20:17:54 +00003071
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03003072 if (cpsw->data.dual_emac) {
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03003073 ret = cpsw_probe_dual_emac(priv);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00003074 if (ret) {
3075 cpsw_err(priv, probe, "error probe slave 2 emac interface\n");
Johan Hovolda7fe9d42016-11-17 17:40:02 +01003076 goto clean_unregister_netdev_ret;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00003077 }
3078 }
3079
Johan Hovoldc46ab7e2016-11-17 17:39:58 +01003080 pm_runtime_put(&pdev->dev);
3081
Mugunthan V Ndf828592012-03-18 20:17:54 +00003082 return 0;
3083
Johan Hovolda7fe9d42016-11-17 17:40:02 +01003084clean_unregister_netdev_ret:
3085 unregister_netdev(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00003086clean_ale_ret:
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03003087 cpsw_ale_destroy(cpsw->ale);
Mugunthan V Ndf828592012-03-18 20:17:54 +00003088clean_dma_ret:
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03003089 cpdma_ctlr_destroy(cpsw->dma);
Johan Hovolda4e32b02016-11-17 17:40:00 +01003090clean_dt_ret:
3091 cpsw_remove_dt(pdev);
Johan Hovoldc46ab7e2016-11-17 17:39:58 +01003092 pm_runtime_put_sync(&pdev->dev);
Daniel Mackaa1a15e2013-09-21 00:50:38 +05303093clean_runtime_disable_ret:
Mugunthan V Nf150bd72012-07-17 08:09:50 +00003094 pm_runtime_disable(&pdev->dev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00003095clean_ndev_ret:
Sebastian Siewiord1bd9ac2013-04-24 08:48:23 +00003096 free_netdev(priv->ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00003097 return ret;
3098}
3099
Bill Pemberton663e12e2012-12-03 09:23:45 -05003100static int cpsw_remove(struct platform_device *pdev)
Mugunthan V Ndf828592012-03-18 20:17:54 +00003101{
3102 struct net_device *ndev = platform_get_drvdata(pdev);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03003103 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Grygorii Strashko8a0b6dc2016-07-28 20:50:35 +03003104 int ret;
3105
3106 ret = pm_runtime_get_sync(&pdev->dev);
3107 if (ret < 0) {
3108 pm_runtime_put_noidle(&pdev->dev);
3109 return ret;
3110 }
Mugunthan V Ndf828592012-03-18 20:17:54 +00003111
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03003112 if (cpsw->data.dual_emac)
3113 unregister_netdev(cpsw->slaves[1].ndev);
Sebastian Siewiord1bd9ac2013-04-24 08:48:23 +00003114 unregister_netdev(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00003115
Grygorii Strashko8a2c9a52016-12-06 18:00:41 -06003116 cpts_release(cpsw->cpts);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03003117 cpsw_ale_destroy(cpsw->ale);
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03003118 cpdma_ctlr_destroy(cpsw->dma);
Johan Hovolda4e32b02016-11-17 17:40:00 +01003119 cpsw_remove_dt(pdev);
Grygorii Strashko8a0b6dc2016-07-28 20:50:35 +03003120 pm_runtime_put_sync(&pdev->dev);
3121 pm_runtime_disable(&pdev->dev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03003122 if (cpsw->data.dual_emac)
3123 free_netdev(cpsw->slaves[1].ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00003124 free_netdev(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00003125 return 0;
3126}
3127
Grygorii Strashko8963a502015-02-27 13:19:45 +02003128#ifdef CONFIG_PM_SLEEP
Mugunthan V Ndf828592012-03-18 20:17:54 +00003129static int cpsw_suspend(struct device *dev)
3130{
3131 struct platform_device *pdev = to_platform_device(dev);
3132 struct net_device *ndev = platform_get_drvdata(pdev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03003133 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00003134
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03003135 if (cpsw->data.dual_emac) {
Mugunthan V N618073e2014-09-11 22:52:38 +05303136 int i;
Daniel Mack1e7a2e22013-11-15 08:29:16 +01003137
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03003138 for (i = 0; i < cpsw->data.slaves; i++) {
3139 if (netif_running(cpsw->slaves[i].ndev))
3140 cpsw_ndo_stop(cpsw->slaves[i].ndev);
Mugunthan V N618073e2014-09-11 22:52:38 +05303141 }
3142 } else {
3143 if (netif_running(ndev))
3144 cpsw_ndo_stop(ndev);
Mugunthan V N618073e2014-09-11 22:52:38 +05303145 }
Daniel Mack1e7a2e22013-11-15 08:29:16 +01003146
Mugunthan V N739683b2013-06-06 23:45:14 +05303147 /* Select sleep pin state */
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03003148 pinctrl_pm_select_sleep_state(dev);
Mugunthan V N739683b2013-06-06 23:45:14 +05303149
Mugunthan V Ndf828592012-03-18 20:17:54 +00003150 return 0;
3151}
3152
3153static int cpsw_resume(struct device *dev)
3154{
3155 struct platform_device *pdev = to_platform_device(dev);
3156 struct net_device *ndev = platform_get_drvdata(pdev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03003157 struct cpsw_common *cpsw = netdev_priv(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00003158
Mugunthan V N739683b2013-06-06 23:45:14 +05303159 /* Select default pin state */
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03003160 pinctrl_pm_select_default_state(dev);
Mugunthan V N739683b2013-06-06 23:45:14 +05303161
Grygorii Strashko4ccfd632016-11-29 16:27:03 -06003162 /* shut up ASSERT_RTNL() warning in netif_set_real_num_tx/rx_queues */
3163 rtnl_lock();
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03003164 if (cpsw->data.dual_emac) {
Mugunthan V N618073e2014-09-11 22:52:38 +05303165 int i;
3166
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03003167 for (i = 0; i < cpsw->data.slaves; i++) {
3168 if (netif_running(cpsw->slaves[i].ndev))
3169 cpsw_ndo_open(cpsw->slaves[i].ndev);
Mugunthan V N618073e2014-09-11 22:52:38 +05303170 }
3171 } else {
3172 if (netif_running(ndev))
3173 cpsw_ndo_open(ndev);
3174 }
Grygorii Strashko4ccfd632016-11-29 16:27:03 -06003175 rtnl_unlock();
3176
Mugunthan V Ndf828592012-03-18 20:17:54 +00003177 return 0;
3178}
Grygorii Strashko8963a502015-02-27 13:19:45 +02003179#endif
Mugunthan V Ndf828592012-03-18 20:17:54 +00003180
Grygorii Strashko8963a502015-02-27 13:19:45 +02003181static SIMPLE_DEV_PM_OPS(cpsw_pm_ops, cpsw_suspend, cpsw_resume);
Mugunthan V Ndf828592012-03-18 20:17:54 +00003182
3183static struct platform_driver cpsw_driver = {
3184 .driver = {
3185 .name = "cpsw",
Mugunthan V Ndf828592012-03-18 20:17:54 +00003186 .pm = &cpsw_pm_ops,
Sachin Kamat1e5c76d2013-09-30 09:55:12 +05303187 .of_match_table = cpsw_of_mtable,
Mugunthan V Ndf828592012-03-18 20:17:54 +00003188 },
3189 .probe = cpsw_probe,
Bill Pemberton663e12e2012-12-03 09:23:45 -05003190 .remove = cpsw_remove,
Mugunthan V Ndf828592012-03-18 20:17:54 +00003191};
3192
Grygorii Strashko6fb3b6b52015-10-23 14:41:12 +03003193module_platform_driver(cpsw_driver);
Mugunthan V Ndf828592012-03-18 20:17:54 +00003194
3195MODULE_LICENSE("GPL");
3196MODULE_AUTHOR("Cyril Chemparathy <cyril@ti.com>");
3197MODULE_AUTHOR("Mugunthan V N <mugunthanvnm@ti.com>");
3198MODULE_DESCRIPTION("TI CPSW Ethernet driver");