blob: b2b30c9df037700728a51f2eb1ae007f8368ba3e [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
Grygorii Strashkoc6395f12017-11-30 18:21:14 -060091#define CPSW_ALE_PORTS_NUM 3
Richard Cochran549985e2012-11-14 09:07:56 +000092#define SLIVER_SIZE 0x40
93
94#define CPSW1_HOST_PORT_OFFSET 0x028
95#define CPSW1_SLAVE_OFFSET 0x050
96#define CPSW1_SLAVE_SIZE 0x040
97#define CPSW1_CPDMA_OFFSET 0x100
98#define CPSW1_STATERAM_OFFSET 0x200
Mugunthan V Nd9718542013-07-23 15:38:17 +053099#define CPSW1_HW_STATS 0x400
Richard Cochran549985e2012-11-14 09:07:56 +0000100#define CPSW1_CPTS_OFFSET 0x500
101#define CPSW1_ALE_OFFSET 0x600
102#define CPSW1_SLIVER_OFFSET 0x700
103
104#define CPSW2_HOST_PORT_OFFSET 0x108
105#define CPSW2_SLAVE_OFFSET 0x200
106#define CPSW2_SLAVE_SIZE 0x100
107#define CPSW2_CPDMA_OFFSET 0x800
Mugunthan V Nd9718542013-07-23 15:38:17 +0530108#define CPSW2_HW_STATS 0x900
Richard Cochran549985e2012-11-14 09:07:56 +0000109#define CPSW2_STATERAM_OFFSET 0xa00
110#define CPSW2_CPTS_OFFSET 0xc00
111#define CPSW2_ALE_OFFSET 0xd00
112#define CPSW2_SLIVER_OFFSET 0xd80
113#define CPSW2_BD_OFFSET 0x2000
114
Mugunthan V Ndf828592012-03-18 20:17:54 +0000115#define CPDMA_RXTHRESH 0x0c0
116#define CPDMA_RXFREE 0x0e0
117#define CPDMA_TXHDP 0x00
118#define CPDMA_RXHDP 0x20
119#define CPDMA_TXCP 0x40
120#define CPDMA_RXCP 0x60
121
Mugunthan V Ndf828592012-03-18 20:17:54 +0000122#define CPSW_POLL_WEIGHT 64
Grygorii Strashko9421c902017-11-15 09:46:35 -0600123#define CPSW_MIN_PACKET_SIZE (VLAN_ETH_ZLEN)
124#define CPSW_MAX_PACKET_SIZE (VLAN_ETH_FRAME_LEN + ETH_FCS_LEN)
Mugunthan V Ndf828592012-03-18 20:17:54 +0000125
126#define RX_PRIORITY_MAPPING 0x76543210
127#define TX_PRIORITY_MAPPING 0x33221100
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300128#define CPDMA_TX_PRIORITY_MAP 0x01234567
Mugunthan V Ndf828592012-03-18 20:17:54 +0000129
Mugunthan V N3b72c2f2013-02-05 08:26:48 +0000130#define CPSW_VLAN_AWARE BIT(1)
131#define CPSW_ALE_VLAN_AWARE 1
132
John Ogness35717d82014-11-14 15:42:52 +0100133#define CPSW_FIFO_NORMAL_MODE (0 << 16)
134#define CPSW_FIFO_DUAL_MAC_MODE (1 << 16)
135#define CPSW_FIFO_RATE_LIMIT_MODE (2 << 16)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000136
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +0000137#define CPSW_INTPACEEN (0x3f << 16)
138#define CPSW_INTPRESCALE_MASK (0x7FF << 0)
139#define CPSW_CMINTMAX_CNT 63
140#define CPSW_CMINTMIN_CNT 2
141#define CPSW_CMINTMAX_INTVL (1000 / CPSW_CMINTMIN_CNT)
142#define CPSW_CMINTMIN_INTVL ((1000 / CPSW_CMINTMAX_CNT) + 1)
143
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300144#define cpsw_slave_index(cpsw, priv) \
145 ((cpsw->data.dual_emac) ? priv->emac_port : \
146 cpsw->data.active_slave)
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +0300147#define IRQ_NUM 2
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300148#define CPSW_MAX_QUEUES 8
Grygorii Strashko90225bf2017-01-06 14:07:33 -0600149#define CPSW_CPDMA_DESCS_POOL_SIZE_DEFAULT 256
Mugunthan V Nd3bb9c52013-03-11 23:16:36 +0000150
Mugunthan V Ndf828592012-03-18 20:17:54 +0000151static int debug_level;
152module_param(debug_level, int, 0);
153MODULE_PARM_DESC(debug_level, "cpsw debug level (NETIF_MSG bits)");
154
155static int ale_ageout = 10;
156module_param(ale_ageout, int, 0);
157MODULE_PARM_DESC(ale_ageout, "cpsw ale ageout interval (seconds)");
158
159static int rx_packet_max = CPSW_MAX_PACKET_SIZE;
160module_param(rx_packet_max, int, 0);
161MODULE_PARM_DESC(rx_packet_max, "maximum receive packet size (bytes)");
162
Grygorii Strashko90225bf2017-01-06 14:07:33 -0600163static int descs_pool_size = CPSW_CPDMA_DESCS_POOL_SIZE_DEFAULT;
164module_param(descs_pool_size, int, 0444);
165MODULE_PARM_DESC(descs_pool_size, "Number of CPDMA CPPI descriptors in pool");
166
Richard Cochran996a5c22012-10-29 08:45:12 +0000167struct cpsw_wr_regs {
Mugunthan V Ndf828592012-03-18 20:17:54 +0000168 u32 id_ver;
169 u32 soft_reset;
170 u32 control;
171 u32 int_control;
172 u32 rx_thresh_en;
173 u32 rx_en;
174 u32 tx_en;
175 u32 misc_en;
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +0000176 u32 mem_allign1[8];
177 u32 rx_thresh_stat;
178 u32 rx_stat;
179 u32 tx_stat;
180 u32 misc_stat;
181 u32 mem_allign2[8];
182 u32 rx_imax;
183 u32 tx_imax;
184
Mugunthan V Ndf828592012-03-18 20:17:54 +0000185};
186
Richard Cochran996a5c22012-10-29 08:45:12 +0000187struct cpsw_ss_regs {
Mugunthan V Ndf828592012-03-18 20:17:54 +0000188 u32 id_ver;
189 u32 control;
190 u32 soft_reset;
191 u32 stat_port_en;
192 u32 ptype;
Richard Cochranbd357af2012-10-29 08:45:13 +0000193 u32 soft_idle;
194 u32 thru_rate;
195 u32 gap_thresh;
196 u32 tx_start_wds;
197 u32 flow_control;
198 u32 vlan_ltype;
199 u32 ts_ltype;
200 u32 dlr_ltype;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000201};
202
Richard Cochran9750a3a2012-10-29 08:45:15 +0000203/* CPSW_PORT_V1 */
204#define CPSW1_MAX_BLKS 0x00 /* Maximum FIFO Blocks */
205#define CPSW1_BLK_CNT 0x04 /* FIFO Block Usage Count (Read Only) */
206#define CPSW1_TX_IN_CTL 0x08 /* Transmit FIFO Control */
207#define CPSW1_PORT_VLAN 0x0c /* VLAN Register */
208#define CPSW1_TX_PRI_MAP 0x10 /* Tx Header Priority to Switch Pri Mapping */
209#define CPSW1_TS_CTL 0x14 /* Time Sync Control */
210#define CPSW1_TS_SEQ_LTYPE 0x18 /* Time Sync Sequence ID Offset and Msg Type */
211#define CPSW1_TS_VLAN 0x1c /* Time Sync VLAN1 and VLAN2 */
212
213/* CPSW_PORT_V2 */
214#define CPSW2_CONTROL 0x00 /* Control Register */
215#define CPSW2_MAX_BLKS 0x08 /* Maximum FIFO Blocks */
216#define CPSW2_BLK_CNT 0x0c /* FIFO Block Usage Count (Read Only) */
217#define CPSW2_TX_IN_CTL 0x10 /* Transmit FIFO Control */
218#define CPSW2_PORT_VLAN 0x14 /* VLAN Register */
219#define CPSW2_TX_PRI_MAP 0x18 /* Tx Header Priority to Switch Pri Mapping */
220#define CPSW2_TS_SEQ_MTYPE 0x1c /* Time Sync Sequence ID Offset and Msg Type */
221
222/* CPSW_PORT_V1 and V2 */
223#define SA_LO 0x20 /* CPGMAC_SL Source Address Low */
224#define SA_HI 0x24 /* CPGMAC_SL Source Address High */
225#define SEND_PERCENT 0x28 /* Transmit Queue Send Percentages */
226
227/* CPSW_PORT_V2 only */
228#define RX_DSCP_PRI_MAP0 0x30 /* Rx DSCP Priority to Rx Packet Mapping */
229#define RX_DSCP_PRI_MAP1 0x34 /* Rx DSCP Priority to Rx Packet Mapping */
230#define RX_DSCP_PRI_MAP2 0x38 /* Rx DSCP Priority to Rx Packet Mapping */
231#define RX_DSCP_PRI_MAP3 0x3c /* Rx DSCP Priority to Rx Packet Mapping */
232#define RX_DSCP_PRI_MAP4 0x40 /* Rx DSCP Priority to Rx Packet Mapping */
233#define RX_DSCP_PRI_MAP5 0x44 /* Rx DSCP Priority to Rx Packet Mapping */
234#define RX_DSCP_PRI_MAP6 0x48 /* Rx DSCP Priority to Rx Packet Mapping */
235#define RX_DSCP_PRI_MAP7 0x4c /* Rx DSCP Priority to Rx Packet Mapping */
236
237/* Bit definitions for the CPSW2_CONTROL register */
238#define PASS_PRI_TAGGED (1<<24) /* Pass Priority Tagged */
239#define VLAN_LTYPE2_EN (1<<21) /* VLAN LTYPE 2 enable */
240#define VLAN_LTYPE1_EN (1<<20) /* VLAN LTYPE 1 enable */
241#define DSCP_PRI_EN (1<<16) /* DSCP Priority Enable */
242#define TS_320 (1<<14) /* Time Sync Dest Port 320 enable */
243#define TS_319 (1<<13) /* Time Sync Dest Port 319 enable */
244#define TS_132 (1<<12) /* Time Sync Dest IP Addr 132 enable */
245#define TS_131 (1<<11) /* Time Sync Dest IP Addr 131 enable */
246#define TS_130 (1<<10) /* Time Sync Dest IP Addr 130 enable */
247#define TS_129 (1<<9) /* Time Sync Dest IP Addr 129 enable */
George Cherian09c55372014-05-02 12:02:02 +0530248#define TS_TTL_NONZERO (1<<8) /* Time Sync Time To Live Non-zero enable */
249#define TS_ANNEX_F_EN (1<<6) /* Time Sync Annex F enable */
Richard Cochran9750a3a2012-10-29 08:45:15 +0000250#define TS_ANNEX_D_EN (1<<4) /* Time Sync Annex D enable */
251#define TS_LTYPE2_EN (1<<3) /* Time Sync LTYPE 2 enable */
252#define TS_LTYPE1_EN (1<<2) /* Time Sync LTYPE 1 enable */
253#define TS_TX_EN (1<<1) /* Time Sync Transmit Enable */
254#define TS_RX_EN (1<<0) /* Time Sync Receive Enable */
255
George Cherian09c55372014-05-02 12:02:02 +0530256#define CTRL_V2_TS_BITS \
257 (TS_320 | TS_319 | TS_132 | TS_131 | TS_130 | TS_129 |\
258 TS_TTL_NONZERO | TS_ANNEX_D_EN | TS_LTYPE1_EN)
Richard Cochran9750a3a2012-10-29 08:45:15 +0000259
George Cherian09c55372014-05-02 12:02:02 +0530260#define CTRL_V2_ALL_TS_MASK (CTRL_V2_TS_BITS | TS_TX_EN | TS_RX_EN)
261#define CTRL_V2_TX_TS_BITS (CTRL_V2_TS_BITS | TS_TX_EN)
262#define CTRL_V2_RX_TS_BITS (CTRL_V2_TS_BITS | TS_RX_EN)
263
264
265#define CTRL_V3_TS_BITS \
266 (TS_320 | TS_319 | TS_132 | TS_131 | TS_130 | TS_129 |\
267 TS_TTL_NONZERO | TS_ANNEX_F_EN | TS_ANNEX_D_EN |\
268 TS_LTYPE1_EN)
269
270#define CTRL_V3_ALL_TS_MASK (CTRL_V3_TS_BITS | TS_TX_EN | TS_RX_EN)
271#define CTRL_V3_TX_TS_BITS (CTRL_V3_TS_BITS | TS_TX_EN)
272#define CTRL_V3_RX_TS_BITS (CTRL_V3_TS_BITS | TS_RX_EN)
Richard Cochran9750a3a2012-10-29 08:45:15 +0000273
274/* Bit definitions for the CPSW2_TS_SEQ_MTYPE register */
275#define TS_SEQ_ID_OFFSET_SHIFT (16) /* Time Sync Sequence ID Offset */
276#define TS_SEQ_ID_OFFSET_MASK (0x3f)
277#define TS_MSG_TYPE_EN_SHIFT (0) /* Time Sync Message Type Enable */
278#define TS_MSG_TYPE_EN_MASK (0xffff)
279
280/* The PTP event messages - Sync, Delay_Req, Pdelay_Req, and Pdelay_Resp. */
281#define EVENT_MSG_BITS ((1<<0) | (1<<1) | (1<<2) | (1<<3))
Mugunthan V Ndf828592012-03-18 20:17:54 +0000282
Richard Cochran2e5b38a2012-10-29 08:45:20 +0000283/* Bit definitions for the CPSW1_TS_CTL register */
284#define CPSW_V1_TS_RX_EN BIT(0)
285#define CPSW_V1_TS_TX_EN BIT(4)
286#define CPSW_V1_MSG_TYPE_OFS 16
287
288/* Bit definitions for the CPSW1_TS_SEQ_LTYPE register */
289#define CPSW_V1_SEQ_ID_OFS_SHIFT 16
290
Grygorii Strashko48f5bcc2017-05-08 14:21:21 -0500291#define CPSW_MAX_BLKS_TX 15
292#define CPSW_MAX_BLKS_TX_SHIFT 4
293#define CPSW_MAX_BLKS_RX 5
294
Mugunthan V Ndf828592012-03-18 20:17:54 +0000295struct cpsw_host_regs {
296 u32 max_blks;
297 u32 blk_cnt;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000298 u32 tx_in_ctl;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000299 u32 port_vlan;
300 u32 tx_pri_map;
301 u32 cpdma_tx_pri_map;
302 u32 cpdma_rx_chan_map;
303};
304
305struct cpsw_sliver_regs {
306 u32 id_ver;
307 u32 mac_control;
308 u32 mac_status;
309 u32 soft_reset;
310 u32 rx_maxlen;
311 u32 __reserved_0;
312 u32 rx_pause;
313 u32 tx_pause;
314 u32 __reserved_1;
315 u32 rx_pri_map;
316};
317
Mugunthan V Nd9718542013-07-23 15:38:17 +0530318struct cpsw_hw_stats {
319 u32 rxgoodframes;
320 u32 rxbroadcastframes;
321 u32 rxmulticastframes;
322 u32 rxpauseframes;
323 u32 rxcrcerrors;
324 u32 rxaligncodeerrors;
325 u32 rxoversizedframes;
326 u32 rxjabberframes;
327 u32 rxundersizedframes;
328 u32 rxfragments;
329 u32 __pad_0[2];
330 u32 rxoctets;
331 u32 txgoodframes;
332 u32 txbroadcastframes;
333 u32 txmulticastframes;
334 u32 txpauseframes;
335 u32 txdeferredframes;
336 u32 txcollisionframes;
337 u32 txsinglecollframes;
338 u32 txmultcollframes;
339 u32 txexcessivecollisions;
340 u32 txlatecollisions;
341 u32 txunderrun;
342 u32 txcarriersenseerrors;
343 u32 txoctets;
344 u32 octetframes64;
345 u32 octetframes65t127;
346 u32 octetframes128t255;
347 u32 octetframes256t511;
348 u32 octetframes512t1023;
349 u32 octetframes1024tup;
350 u32 netoctets;
351 u32 rxsofoverruns;
352 u32 rxmofoverruns;
353 u32 rxdmaoverruns;
354};
355
Grygorii Strashko2c8a14d2017-11-30 18:21:12 -0600356struct cpsw_slave_data {
357 struct device_node *phy_node;
358 char phy_id[MII_BUS_ID_SIZE];
359 int phy_if;
360 u8 mac_addr[ETH_ALEN];
361 u16 dual_emac_res_vlan; /* Reserved VLAN for DualEMAC */
362};
363
364struct cpsw_platform_data {
365 struct cpsw_slave_data *slave_data;
366 u32 ss_reg_ofs; /* Subsystem control register offset */
367 u32 channels; /* number of cpdma channels (symmetric) */
368 u32 slaves; /* number of slave cpgmac ports */
369 u32 active_slave; /* time stamping, ethtool and SIOCGMIIPHY slave */
370 u32 ale_entries; /* ale table size */
371 u32 bd_ram_size; /*buffer descriptor ram size */
372 u32 mac_control; /* Mac control register */
373 u16 default_vlan; /* Def VLAN for ALE lookup in VLAN aware mode*/
374 bool dual_emac; /* Enable Dual EMAC mode */
375};
376
Mugunthan V Ndf828592012-03-18 20:17:54 +0000377struct cpsw_slave {
Richard Cochran9750a3a2012-10-29 08:45:15 +0000378 void __iomem *regs;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000379 struct cpsw_sliver_regs __iomem *sliver;
380 int slave_num;
381 u32 mac_control;
382 struct cpsw_slave_data *data;
383 struct phy_device *phy;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000384 struct net_device *ndev;
385 u32 port_vlan;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000386};
387
Richard Cochran9750a3a2012-10-29 08:45:15 +0000388static inline u32 slave_read(struct cpsw_slave *slave, u32 offset)
389{
Grygorii Strashkodda5f5fe2017-11-30 18:21:11 -0600390 return readl_relaxed(slave->regs + offset);
Richard Cochran9750a3a2012-10-29 08:45:15 +0000391}
392
393static inline void slave_write(struct cpsw_slave *slave, u32 val, u32 offset)
394{
Grygorii Strashkodda5f5fe2017-11-30 18:21:11 -0600395 writel_relaxed(val, slave->regs + offset);
Richard Cochran9750a3a2012-10-29 08:45:15 +0000396}
397
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +0200398struct cpsw_vector {
399 struct cpdma_chan *ch;
400 int budget;
401};
402
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +0300403struct cpsw_common {
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +0300404 struct device *dev;
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300405 struct cpsw_platform_data data;
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +0300406 struct napi_struct napi_rx;
407 struct napi_struct napi_tx;
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +0300408 struct cpsw_ss_regs __iomem *regs;
409 struct cpsw_wr_regs __iomem *wr_regs;
410 u8 __iomem *hw_stats;
411 struct cpsw_host_regs __iomem *host_port_regs;
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300412 u32 version;
413 u32 coal_intvl;
414 u32 bus_freq_mhz;
415 int rx_packet_max;
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300416 struct cpsw_slave *slaves;
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +0300417 struct cpdma_ctlr *dma;
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +0200418 struct cpsw_vector txv[CPSW_MAX_QUEUES];
419 struct cpsw_vector rxv[CPSW_MAX_QUEUES];
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300420 struct cpsw_ale *ale;
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +0300421 bool quirk_irq;
422 bool rx_irq_disabled;
423 bool tx_irq_disabled;
424 u32 irqs_table[IRQ_NUM];
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300425 struct cpts *cpts;
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300426 int rx_ch_num, tx_ch_num;
Ivan Khoronzhuk0be01b82016-12-10 14:23:49 +0200427 int speed;
Ivan Khoronzhukd5bc1612017-02-14 16:02:36 +0200428 int usage_count;
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +0300429};
430
431struct cpsw_priv {
Mugunthan V Ndf828592012-03-18 20:17:54 +0000432 struct net_device *ndev;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000433 struct device *dev;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000434 u32 msg_enable;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000435 u8 mac_addr[ETH_ALEN];
Mugunthan V N1923d6e2014-09-08 22:54:02 +0530436 bool rx_pause;
437 bool tx_pause;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000438 u32 emac_port;
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +0300439 struct cpsw_common *cpsw;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000440};
441
Mugunthan V Nd9718542013-07-23 15:38:17 +0530442struct cpsw_stats {
443 char stat_string[ETH_GSTRING_LEN];
444 int type;
445 int sizeof_stat;
446 int stat_offset;
447};
448
449enum {
450 CPSW_STATS,
451 CPDMA_RX_STATS,
452 CPDMA_TX_STATS,
453};
454
455#define CPSW_STAT(m) CPSW_STATS, \
456 sizeof(((struct cpsw_hw_stats *)0)->m), \
457 offsetof(struct cpsw_hw_stats, m)
458#define CPDMA_RX_STAT(m) CPDMA_RX_STATS, \
459 sizeof(((struct cpdma_chan_stats *)0)->m), \
460 offsetof(struct cpdma_chan_stats, m)
461#define CPDMA_TX_STAT(m) CPDMA_TX_STATS, \
462 sizeof(((struct cpdma_chan_stats *)0)->m), \
463 offsetof(struct cpdma_chan_stats, m)
464
465static const struct cpsw_stats cpsw_gstrings_stats[] = {
466 { "Good Rx Frames", CPSW_STAT(rxgoodframes) },
467 { "Broadcast Rx Frames", CPSW_STAT(rxbroadcastframes) },
468 { "Multicast Rx Frames", CPSW_STAT(rxmulticastframes) },
469 { "Pause Rx Frames", CPSW_STAT(rxpauseframes) },
470 { "Rx CRC Errors", CPSW_STAT(rxcrcerrors) },
471 { "Rx Align/Code Errors", CPSW_STAT(rxaligncodeerrors) },
472 { "Oversize Rx Frames", CPSW_STAT(rxoversizedframes) },
473 { "Rx Jabbers", CPSW_STAT(rxjabberframes) },
474 { "Undersize (Short) Rx Frames", CPSW_STAT(rxundersizedframes) },
475 { "Rx Fragments", CPSW_STAT(rxfragments) },
476 { "Rx Octets", CPSW_STAT(rxoctets) },
477 { "Good Tx Frames", CPSW_STAT(txgoodframes) },
478 { "Broadcast Tx Frames", CPSW_STAT(txbroadcastframes) },
479 { "Multicast Tx Frames", CPSW_STAT(txmulticastframes) },
480 { "Pause Tx Frames", CPSW_STAT(txpauseframes) },
481 { "Deferred Tx Frames", CPSW_STAT(txdeferredframes) },
482 { "Collisions", CPSW_STAT(txcollisionframes) },
483 { "Single Collision Tx Frames", CPSW_STAT(txsinglecollframes) },
484 { "Multiple Collision Tx Frames", CPSW_STAT(txmultcollframes) },
485 { "Excessive Collisions", CPSW_STAT(txexcessivecollisions) },
486 { "Late Collisions", CPSW_STAT(txlatecollisions) },
487 { "Tx Underrun", CPSW_STAT(txunderrun) },
488 { "Carrier Sense Errors", CPSW_STAT(txcarriersenseerrors) },
489 { "Tx Octets", CPSW_STAT(txoctets) },
490 { "Rx + Tx 64 Octet Frames", CPSW_STAT(octetframes64) },
491 { "Rx + Tx 65-127 Octet Frames", CPSW_STAT(octetframes65t127) },
492 { "Rx + Tx 128-255 Octet Frames", CPSW_STAT(octetframes128t255) },
493 { "Rx + Tx 256-511 Octet Frames", CPSW_STAT(octetframes256t511) },
494 { "Rx + Tx 512-1023 Octet Frames", CPSW_STAT(octetframes512t1023) },
495 { "Rx + Tx 1024-Up Octet Frames", CPSW_STAT(octetframes1024tup) },
496 { "Net Octets", CPSW_STAT(netoctets) },
497 { "Rx Start of Frame Overruns", CPSW_STAT(rxsofoverruns) },
498 { "Rx Middle of Frame Overruns", CPSW_STAT(rxmofoverruns) },
499 { "Rx DMA Overruns", CPSW_STAT(rxdmaoverruns) },
Mugunthan V Nd9718542013-07-23 15:38:17 +0530500};
501
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300502static const struct cpsw_stats cpsw_gstrings_ch_stats[] = {
503 { "head_enqueue", CPDMA_RX_STAT(head_enqueue) },
504 { "tail_enqueue", CPDMA_RX_STAT(tail_enqueue) },
505 { "pad_enqueue", CPDMA_RX_STAT(pad_enqueue) },
506 { "misqueued", CPDMA_RX_STAT(misqueued) },
507 { "desc_alloc_fail", CPDMA_RX_STAT(desc_alloc_fail) },
508 { "pad_alloc_fail", CPDMA_RX_STAT(pad_alloc_fail) },
509 { "runt_receive_buf", CPDMA_RX_STAT(runt_receive_buff) },
510 { "runt_transmit_buf", CPDMA_RX_STAT(runt_transmit_buff) },
511 { "empty_dequeue", CPDMA_RX_STAT(empty_dequeue) },
512 { "busy_dequeue", CPDMA_RX_STAT(busy_dequeue) },
513 { "good_dequeue", CPDMA_RX_STAT(good_dequeue) },
514 { "requeue", CPDMA_RX_STAT(requeue) },
515 { "teardown_dequeue", CPDMA_RX_STAT(teardown_dequeue) },
516};
517
518#define CPSW_STATS_COMMON_LEN ARRAY_SIZE(cpsw_gstrings_stats)
519#define CPSW_STATS_CH_LEN ARRAY_SIZE(cpsw_gstrings_ch_stats)
Mugunthan V Nd9718542013-07-23 15:38:17 +0530520
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +0300521#define ndev_to_cpsw(ndev) (((struct cpsw_priv *)netdev_priv(ndev))->cpsw)
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +0300522#define napi_to_cpsw(napi) container_of(napi, struct cpsw_common, napi)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000523#define for_each_slave(priv, func, arg...) \
524 do { \
Sebastian Siewior6e6ceae2013-04-24 08:48:24 +0000525 struct cpsw_slave *slave; \
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300526 struct cpsw_common *cpsw = (priv)->cpsw; \
Sebastian Siewior6e6ceae2013-04-24 08:48:24 +0000527 int n; \
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300528 if (cpsw->data.dual_emac) \
529 (func)((cpsw)->slaves + priv->emac_port, ##arg);\
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000530 else \
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300531 for (n = cpsw->data.slaves, \
532 slave = cpsw->slaves; \
Sebastian Siewior6e6ceae2013-04-24 08:48:24 +0000533 n; n--) \
534 (func)(slave++, ##arg); \
Mugunthan V Ndf828592012-03-18 20:17:54 +0000535 } while (0)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000536
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300537#define cpsw_dual_emac_src_port_detect(cpsw, status, ndev, skb) \
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000538 do { \
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300539 if (!cpsw->data.dual_emac) \
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000540 break; \
541 if (CPDMA_RX_SOURCE_PORT(status) == 1) { \
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300542 ndev = cpsw->slaves[0].ndev; \
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000543 skb->dev = ndev; \
544 } else if (CPDMA_RX_SOURCE_PORT(status) == 2) { \
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300545 ndev = cpsw->slaves[1].ndev; \
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000546 skb->dev = ndev; \
547 } \
548 } while (0)
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300549#define cpsw_add_mcast(cpsw, priv, addr) \
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000550 do { \
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300551 if (cpsw->data.dual_emac) { \
552 struct cpsw_slave *slave = cpsw->slaves + \
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000553 priv->emac_port; \
Ivan Khoronzhuk6f1f5832016-08-10 02:22:34 +0300554 int slave_port = cpsw_get_slave_port( \
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000555 slave->slave_num); \
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300556 cpsw_ale_add_mcast(cpsw->ale, addr, \
Grygorii Strashko71a2cbb2016-04-07 15:16:44 +0300557 1 << slave_port | ALE_PORT_HOST, \
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000558 ALE_VLAN, slave->port_vlan, 0); \
559 } else { \
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300560 cpsw_ale_add_mcast(cpsw->ale, addr, \
Grygorii Strashko61f1cef2016-04-07 15:16:43 +0300561 ALE_ALL_PORTS, \
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000562 0, 0, 0); \
563 } \
564 } while (0)
565
Ivan Khoronzhuk6f1f5832016-08-10 02:22:34 +0300566static inline int cpsw_get_slave_port(u32 slave_num)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000567{
Grygorii Strashko71a2cbb2016-04-07 15:16:44 +0300568 return slave_num + 1;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000569}
Mugunthan V Ndf828592012-03-18 20:17:54 +0000570
Mugunthan V N0cd8f9c2014-01-23 00:03:12 +0530571static void cpsw_set_promiscious(struct net_device *ndev, bool enable)
572{
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300573 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
574 struct cpsw_ale *ale = cpsw->ale;
Mugunthan V N0cd8f9c2014-01-23 00:03:12 +0530575 int i;
576
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300577 if (cpsw->data.dual_emac) {
Mugunthan V N0cd8f9c2014-01-23 00:03:12 +0530578 bool flag = false;
579
580 /* Enabling promiscuous mode for one interface will be
581 * common for both the interface as the interface shares
582 * the same hardware resource.
583 */
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300584 for (i = 0; i < cpsw->data.slaves; i++)
585 if (cpsw->slaves[i].ndev->flags & IFF_PROMISC)
Mugunthan V N0cd8f9c2014-01-23 00:03:12 +0530586 flag = true;
587
588 if (!enable && flag) {
589 enable = true;
590 dev_err(&ndev->dev, "promiscuity not disabled as the other interface is still in promiscuity mode\n");
591 }
592
593 if (enable) {
594 /* Enable Bypass */
595 cpsw_ale_control_set(ale, 0, ALE_BYPASS, 1);
596
597 dev_dbg(&ndev->dev, "promiscuity enabled\n");
598 } else {
599 /* Disable Bypass */
600 cpsw_ale_control_set(ale, 0, ALE_BYPASS, 0);
601 dev_dbg(&ndev->dev, "promiscuity disabled\n");
602 }
603 } else {
604 if (enable) {
605 unsigned long timeout = jiffies + HZ;
606
Lennart Sorensen6f979eb2014-10-31 13:28:54 -0400607 /* Disable Learn for all ports (host is port 0 and slaves are port 1 and up */
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300608 for (i = 0; i <= cpsw->data.slaves; i++) {
Mugunthan V N0cd8f9c2014-01-23 00:03:12 +0530609 cpsw_ale_control_set(ale, i,
610 ALE_PORT_NOLEARN, 1);
611 cpsw_ale_control_set(ale, i,
612 ALE_PORT_NO_SA_UPDATE, 1);
613 }
614
615 /* Clear All Untouched entries */
616 cpsw_ale_control_set(ale, 0, ALE_AGEOUT, 1);
617 do {
618 cpu_relax();
619 if (cpsw_ale_control_get(ale, 0, ALE_AGEOUT))
620 break;
621 } while (time_after(timeout, jiffies));
622 cpsw_ale_control_set(ale, 0, ALE_AGEOUT, 1);
623
624 /* Clear all mcast from ALE */
Grygorii Strashko61f1cef2016-04-07 15:16:43 +0300625 cpsw_ale_flush_multicast(ale, ALE_ALL_PORTS, -1);
Mugunthan V N0cd8f9c2014-01-23 00:03:12 +0530626
627 /* Flood All Unicast Packets to Host port */
628 cpsw_ale_control_set(ale, 0, ALE_P0_UNI_FLOOD, 1);
629 dev_dbg(&ndev->dev, "promiscuity enabled\n");
630 } else {
Lennart Sorensen6f979eb2014-10-31 13:28:54 -0400631 /* Don't Flood All Unicast Packets to Host port */
Mugunthan V N0cd8f9c2014-01-23 00:03:12 +0530632 cpsw_ale_control_set(ale, 0, ALE_P0_UNI_FLOOD, 0);
633
Lennart Sorensen6f979eb2014-10-31 13:28:54 -0400634 /* Enable Learn for all ports (host is port 0 and slaves are port 1 and up */
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300635 for (i = 0; i <= cpsw->data.slaves; i++) {
Mugunthan V N0cd8f9c2014-01-23 00:03:12 +0530636 cpsw_ale_control_set(ale, i,
637 ALE_PORT_NOLEARN, 0);
638 cpsw_ale_control_set(ale, i,
639 ALE_PORT_NO_SA_UPDATE, 0);
640 }
641 dev_dbg(&ndev->dev, "promiscuity disabled\n");
642 }
643 }
644}
645
Mugunthan V N5c50a852012-10-29 08:45:11 +0000646static void cpsw_ndo_set_rx_mode(struct net_device *ndev)
647{
648 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300649 struct cpsw_common *cpsw = priv->cpsw;
Mugunthan V N25906052015-01-13 17:35:49 +0530650 int vid;
651
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300652 if (cpsw->data.dual_emac)
653 vid = cpsw->slaves[priv->emac_port].port_vlan;
Mugunthan V N25906052015-01-13 17:35:49 +0530654 else
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300655 vid = cpsw->data.default_vlan;
Mugunthan V N5c50a852012-10-29 08:45:11 +0000656
657 if (ndev->flags & IFF_PROMISC) {
658 /* Enable promiscuous mode */
Mugunthan V N0cd8f9c2014-01-23 00:03:12 +0530659 cpsw_set_promiscious(ndev, true);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300660 cpsw_ale_set_allmulti(cpsw->ale, IFF_ALLMULTI);
Mugunthan V N5c50a852012-10-29 08:45:11 +0000661 return;
Mugunthan V N0cd8f9c2014-01-23 00:03:12 +0530662 } else {
663 /* Disable promiscuous mode */
664 cpsw_set_promiscious(ndev, false);
Mugunthan V N5c50a852012-10-29 08:45:11 +0000665 }
666
Lennart Sorensen1e5c4bc2014-10-31 13:38:52 -0400667 /* Restore allmulti on vlans if necessary */
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300668 cpsw_ale_set_allmulti(cpsw->ale, priv->ndev->flags & IFF_ALLMULTI);
Lennart Sorensen1e5c4bc2014-10-31 13:38:52 -0400669
Mugunthan V N5c50a852012-10-29 08:45:11 +0000670 /* Clear all mcast from ALE */
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300671 cpsw_ale_flush_multicast(cpsw->ale, ALE_ALL_PORTS, vid);
Mugunthan V N5c50a852012-10-29 08:45:11 +0000672
673 if (!netdev_mc_empty(ndev)) {
674 struct netdev_hw_addr *ha;
675
676 /* program multicast address list into ALE register */
677 netdev_for_each_mc_addr(ha, ndev) {
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300678 cpsw_add_mcast(cpsw, priv, (u8 *)ha->addr);
Mugunthan V N5c50a852012-10-29 08:45:11 +0000679 }
680 }
681}
682
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +0300683static void cpsw_intr_enable(struct cpsw_common *cpsw)
Mugunthan V Ndf828592012-03-18 20:17:54 +0000684{
Grygorii Strashkodda5f5fe2017-11-30 18:21:11 -0600685 writel_relaxed(0xFF, &cpsw->wr_regs->tx_en);
686 writel_relaxed(0xFF, &cpsw->wr_regs->rx_en);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000687
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +0300688 cpdma_ctlr_int_ctrl(cpsw->dma, true);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000689 return;
690}
691
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +0300692static void cpsw_intr_disable(struct cpsw_common *cpsw)
Mugunthan V Ndf828592012-03-18 20:17:54 +0000693{
Grygorii Strashkodda5f5fe2017-11-30 18:21:11 -0600694 writel_relaxed(0, &cpsw->wr_regs->tx_en);
695 writel_relaxed(0, &cpsw->wr_regs->rx_en);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000696
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +0300697 cpdma_ctlr_int_ctrl(cpsw->dma, false);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000698 return;
699}
700
Olof Johansson1a3b5052013-12-11 15:58:07 -0800701static void cpsw_tx_handler(void *token, int len, int status)
Mugunthan V Ndf828592012-03-18 20:17:54 +0000702{
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300703 struct netdev_queue *txq;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000704 struct sk_buff *skb = token;
705 struct net_device *ndev = skb->dev;
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300706 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000707
Mugunthan V Nfae50822013-01-17 06:31:34 +0000708 /* Check whether the queue is stopped due to stalled tx dma, if the
709 * queue is stopped then start the queue as we have free desc for tx
710 */
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300711 txq = netdev_get_tx_queue(ndev, skb_get_queue_mapping(skb));
712 if (unlikely(netif_tx_queue_stopped(txq)))
713 netif_tx_wake_queue(txq);
714
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300715 cpts_tx_timestamp(cpsw->cpts, skb);
Tobias Klauser8dc43dd2014-03-10 13:12:23 +0100716 ndev->stats.tx_packets++;
717 ndev->stats.tx_bytes += len;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000718 dev_kfree_skb_any(skb);
719}
720
Olof Johansson1a3b5052013-12-11 15:58:07 -0800721static void cpsw_rx_handler(void *token, int len, int status)
Mugunthan V Ndf828592012-03-18 20:17:54 +0000722{
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300723 struct cpdma_chan *ch;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000724 struct sk_buff *skb = token;
Sebastian Siewiorb4727e62013-04-23 07:31:39 +0000725 struct sk_buff *new_skb;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000726 struct net_device *ndev = skb->dev;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000727 int ret = 0;
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300728 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000729
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300730 cpsw_dual_emac_src_port_detect(cpsw, status, ndev, skb);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000731
Mugunthan V N16e5c572014-04-10 14:23:23 +0530732 if (unlikely(status < 0) || unlikely(!netif_running(ndev))) {
Ivan Khoronzhukfe734d02017-01-19 18:58:26 +0200733 /* In dual emac mode check for all interfaces */
Ivan Khoronzhukd5bc1612017-02-14 16:02:36 +0200734 if (cpsw->data.dual_emac && cpsw->usage_count &&
Ivan Khoronzhukfe734d02017-01-19 18:58:26 +0200735 (status >= 0)) {
Mugunthan V Na0e2c822014-09-10 16:38:09 +0530736 /* The packet received is for the interface which
737 * is already down and the other interface is up
Joe Perchesdbedd442015-03-06 20:49:12 -0800738 * and running, instead of freeing which results
Mugunthan V Na0e2c822014-09-10 16:38:09 +0530739 * in reducing of the number of rx descriptor in
740 * DMA engine, requeue skb back to cpdma.
741 */
742 new_skb = skb;
743 goto requeue;
744 }
745
Sebastian Siewiorb4727e62013-04-23 07:31:39 +0000746 /* the interface is going down, skbs are purged */
Mugunthan V Ndf828592012-03-18 20:17:54 +0000747 dev_kfree_skb_any(skb);
748 return;
749 }
Sebastian Siewiorb4727e62013-04-23 07:31:39 +0000750
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300751 new_skb = netdev_alloc_skb_ip_align(ndev, cpsw->rx_packet_max);
Sebastian Siewiorb4727e62013-04-23 07:31:39 +0000752 if (new_skb) {
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300753 skb_copy_queue_mapping(new_skb, skb);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000754 skb_put(skb, len);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300755 cpts_rx_timestamp(cpsw->cpts, skb);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000756 skb->protocol = eth_type_trans(skb, ndev);
757 netif_receive_skb(skb);
Tobias Klauser8dc43dd2014-03-10 13:12:23 +0100758 ndev->stats.rx_bytes += len;
759 ndev->stats.rx_packets++;
Grygorii Strashko254a49d2016-08-09 15:09:44 +0300760 kmemleak_not_leak(new_skb);
Sebastian Siewiorb4727e62013-04-23 07:31:39 +0000761 } else {
Tobias Klauser8dc43dd2014-03-10 13:12:23 +0100762 ndev->stats.rx_dropped++;
Sebastian Siewiorb4727e62013-04-23 07:31:39 +0000763 new_skb = skb;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000764 }
765
Mugunthan V Na0e2c822014-09-10 16:38:09 +0530766requeue:
Ivan Khoronzhukce52c742016-08-22 21:18:28 +0300767 if (netif_dormant(ndev)) {
768 dev_kfree_skb_any(new_skb);
769 return;
770 }
771
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +0200772 ch = cpsw->rxv[skb_get_queue_mapping(new_skb)].ch;
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300773 ret = cpdma_chan_submit(ch, new_skb, new_skb->data,
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +0300774 skb_tailroom(new_skb), 0);
Sebastian Siewiorb4727e62013-04-23 07:31:39 +0000775 if (WARN_ON(ret < 0))
776 dev_kfree_skb_any(new_skb);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000777}
778
Ivan Khoronzhuk32b78d82016-12-10 14:23:48 +0200779static void cpsw_split_res(struct net_device *ndev)
Ivan Khoronzhuk48e0a832016-12-06 03:45:00 +0200780{
781 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk32b78d82016-12-10 14:23:48 +0200782 u32 consumed_rate = 0, bigest_rate = 0;
Ivan Khoronzhuk48e0a832016-12-06 03:45:00 +0200783 struct cpsw_common *cpsw = priv->cpsw;
784 struct cpsw_vector *txv = cpsw->txv;
Ivan Khoronzhuk32b78d82016-12-10 14:23:48 +0200785 int i, ch_weight, rlim_ch_num = 0;
Ivan Khoronzhuk48e0a832016-12-06 03:45:00 +0200786 int budget, bigest_rate_ch = 0;
Ivan Khoronzhuk48e0a832016-12-06 03:45:00 +0200787 u32 ch_rate, max_rate;
788 int ch_budget = 0;
789
Ivan Khoronzhuk48e0a832016-12-06 03:45:00 +0200790 for (i = 0; i < cpsw->tx_ch_num; i++) {
791 ch_rate = cpdma_chan_get_rate(txv[i].ch);
792 if (!ch_rate)
793 continue;
794
795 rlim_ch_num++;
796 consumed_rate += ch_rate;
797 }
798
799 if (cpsw->tx_ch_num == rlim_ch_num) {
800 max_rate = consumed_rate;
Ivan Khoronzhuk32b78d82016-12-10 14:23:48 +0200801 } else if (!rlim_ch_num) {
802 ch_budget = CPSW_POLL_WEIGHT / cpsw->tx_ch_num;
803 bigest_rate = 0;
804 max_rate = consumed_rate;
Ivan Khoronzhuk48e0a832016-12-06 03:45:00 +0200805 } else {
Ivan Khoronzhuk0be01b82016-12-10 14:23:49 +0200806 max_rate = cpsw->speed * 1000;
807
808 /* if max_rate is less then expected due to reduced link speed,
809 * split proportionally according next potential max speed
810 */
811 if (max_rate < consumed_rate)
812 max_rate *= 10;
813
814 if (max_rate < consumed_rate)
815 max_rate *= 10;
Ivan Khoronzhuk32b78d82016-12-10 14:23:48 +0200816
Ivan Khoronzhuk48e0a832016-12-06 03:45:00 +0200817 ch_budget = (consumed_rate * CPSW_POLL_WEIGHT) / max_rate;
818 ch_budget = (CPSW_POLL_WEIGHT - ch_budget) /
819 (cpsw->tx_ch_num - rlim_ch_num);
820 bigest_rate = (max_rate - consumed_rate) /
821 (cpsw->tx_ch_num - rlim_ch_num);
822 }
823
Ivan Khoronzhuk32b78d82016-12-10 14:23:48 +0200824 /* split tx weight/budget */
Ivan Khoronzhuk48e0a832016-12-06 03:45:00 +0200825 budget = CPSW_POLL_WEIGHT;
826 for (i = 0; i < cpsw->tx_ch_num; i++) {
827 ch_rate = cpdma_chan_get_rate(txv[i].ch);
828 if (ch_rate) {
829 txv[i].budget = (ch_rate * CPSW_POLL_WEIGHT) / max_rate;
830 if (!txv[i].budget)
Ivan Khoronzhuk32b78d82016-12-10 14:23:48 +0200831 txv[i].budget++;
Ivan Khoronzhuk48e0a832016-12-06 03:45:00 +0200832 if (ch_rate > bigest_rate) {
833 bigest_rate_ch = i;
834 bigest_rate = ch_rate;
835 }
Ivan Khoronzhuk32b78d82016-12-10 14:23:48 +0200836
837 ch_weight = (ch_rate * 100) / max_rate;
838 if (!ch_weight)
839 ch_weight++;
840 cpdma_chan_set_weight(cpsw->txv[i].ch, ch_weight);
Ivan Khoronzhuk48e0a832016-12-06 03:45:00 +0200841 } else {
842 txv[i].budget = ch_budget;
843 if (!bigest_rate_ch)
844 bigest_rate_ch = i;
Ivan Khoronzhuk32b78d82016-12-10 14:23:48 +0200845 cpdma_chan_set_weight(cpsw->txv[i].ch, 0);
Ivan Khoronzhuk48e0a832016-12-06 03:45:00 +0200846 }
847
848 budget -= txv[i].budget;
849 }
850
851 if (budget)
852 txv[bigest_rate_ch].budget += budget;
853
854 /* split rx budget */
855 budget = CPSW_POLL_WEIGHT;
856 ch_budget = budget / cpsw->rx_ch_num;
857 for (i = 0; i < cpsw->rx_ch_num; i++) {
858 cpsw->rxv[i].budget = ch_budget;
859 budget -= ch_budget;
860 }
861
862 if (budget)
863 cpsw->rxv[0].budget += budget;
864}
865
Felipe Balbic03abd82015-01-16 10:11:12 -0600866static irqreturn_t cpsw_tx_interrupt(int irq, void *dev_id)
Mugunthan V Ndf828592012-03-18 20:17:54 +0000867{
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +0300868 struct cpsw_common *cpsw = dev_id;
Felipe Balbi7ce67a32015-01-02 16:15:59 -0600869
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +0300870 writel(0, &cpsw->wr_regs->tx_en);
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +0300871 cpdma_ctlr_eoi(cpsw->dma, CPDMA_EOI_TX);
Felipe Balbic03abd82015-01-16 10:11:12 -0600872
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +0300873 if (cpsw->quirk_irq) {
874 disable_irq_nosync(cpsw->irqs_table[1]);
875 cpsw->tx_irq_disabled = true;
Mugunthan V N7da11602015-08-12 15:22:53 +0530876 }
877
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +0300878 napi_schedule(&cpsw->napi_tx);
Felipe Balbic03abd82015-01-16 10:11:12 -0600879 return IRQ_HANDLED;
880}
881
882static irqreturn_t cpsw_rx_interrupt(int irq, void *dev_id)
883{
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +0300884 struct cpsw_common *cpsw = dev_id;
Felipe Balbic03abd82015-01-16 10:11:12 -0600885
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +0300886 cpdma_ctlr_eoi(cpsw->dma, CPDMA_EOI_RX);
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +0300887 writel(0, &cpsw->wr_regs->rx_en);
Sebastian Siewiorfd51cf12013-04-23 07:31:37 +0000888
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +0300889 if (cpsw->quirk_irq) {
890 disable_irq_nosync(cpsw->irqs_table[0]);
891 cpsw->rx_irq_disabled = true;
Mugunthan V N7da11602015-08-12 15:22:53 +0530892 }
893
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +0300894 napi_schedule(&cpsw->napi_rx);
Mugunthan V Nd354eb82015-08-04 16:06:19 +0530895 return IRQ_HANDLED;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000896}
897
Mugunthan V N32a74322015-08-04 16:06:20 +0530898static int cpsw_tx_poll(struct napi_struct *napi_tx, int budget)
Mugunthan V Ndf828592012-03-18 20:17:54 +0000899{
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300900 u32 ch_map;
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +0200901 int num_tx, cur_budget, ch;
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +0300902 struct cpsw_common *cpsw = napi_to_cpsw(napi_tx);
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +0200903 struct cpsw_vector *txv;
Mugunthan V N32a74322015-08-04 16:06:20 +0530904
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300905 /* process every unprocessed channel */
906 ch_map = cpdma_ctrl_txchs_state(cpsw->dma);
Ivan Khoronzhuk342934a2016-11-29 17:00:50 +0200907 for (ch = 0, num_tx = 0; ch_map; ch_map >>= 1, ch++) {
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300908 if (!(ch_map & 0x01))
909 continue;
910
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +0200911 txv = &cpsw->txv[ch];
912 if (unlikely(txv->budget > budget - num_tx))
913 cur_budget = budget - num_tx;
914 else
915 cur_budget = txv->budget;
916
917 num_tx += cpdma_chan_process(txv->ch, cur_budget);
Ivan Khoronzhuk342934a2016-11-29 17:00:50 +0200918 if (num_tx >= budget)
919 break;
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300920 }
921
Mugunthan V N32a74322015-08-04 16:06:20 +0530922 if (num_tx < budget) {
923 napi_complete(napi_tx);
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +0300924 writel(0xff, &cpsw->wr_regs->tx_en);
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +0300925 if (cpsw->quirk_irq && cpsw->tx_irq_disabled) {
926 cpsw->tx_irq_disabled = false;
927 enable_irq(cpsw->irqs_table[1]);
Mugunthan V N7da11602015-08-12 15:22:53 +0530928 }
Mugunthan V N32a74322015-08-04 16:06:20 +0530929 }
930
Mugunthan V N32a74322015-08-04 16:06:20 +0530931 return num_tx;
932}
933
934static int cpsw_rx_poll(struct napi_struct *napi_rx, int budget)
935{
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300936 u32 ch_map;
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +0200937 int num_rx, cur_budget, ch;
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +0300938 struct cpsw_common *cpsw = napi_to_cpsw(napi_rx);
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +0200939 struct cpsw_vector *rxv;
Mugunthan V N510a1e722013-02-17 22:19:20 +0000940
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300941 /* process every unprocessed channel */
942 ch_map = cpdma_ctrl_rxchs_state(cpsw->dma);
Ivan Khoronzhuk342934a2016-11-29 17:00:50 +0200943 for (ch = 0, num_rx = 0; ch_map; ch_map >>= 1, ch++) {
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300944 if (!(ch_map & 0x01))
945 continue;
946
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +0200947 rxv = &cpsw->rxv[ch];
948 if (unlikely(rxv->budget > budget - num_rx))
949 cur_budget = budget - num_rx;
950 else
951 cur_budget = rxv->budget;
952
953 num_rx += cpdma_chan_process(rxv->ch, cur_budget);
Ivan Khoronzhuk342934a2016-11-29 17:00:50 +0200954 if (num_rx >= budget)
955 break;
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300956 }
957
Mugunthan V N510a1e722013-02-17 22:19:20 +0000958 if (num_rx < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -0800959 napi_complete_done(napi_rx, num_rx);
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +0300960 writel(0xff, &cpsw->wr_regs->rx_en);
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +0300961 if (cpsw->quirk_irq && cpsw->rx_irq_disabled) {
962 cpsw->rx_irq_disabled = false;
963 enable_irq(cpsw->irqs_table[0]);
Mugunthan V N7da11602015-08-12 15:22:53 +0530964 }
Mugunthan V N510a1e722013-02-17 22:19:20 +0000965 }
Mugunthan V Ndf828592012-03-18 20:17:54 +0000966
Mugunthan V Ndf828592012-03-18 20:17:54 +0000967 return num_rx;
968}
969
970static inline void soft_reset(const char *module, void __iomem *reg)
971{
972 unsigned long timeout = jiffies + HZ;
973
Grygorii Strashkodda5f5fe2017-11-30 18:21:11 -0600974 writel_relaxed(1, reg);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000975 do {
976 cpu_relax();
Grygorii Strashkodda5f5fe2017-11-30 18:21:11 -0600977 } while ((readl_relaxed(reg) & 1) && time_after(timeout, jiffies));
Mugunthan V Ndf828592012-03-18 20:17:54 +0000978
Grygorii Strashkodda5f5fe2017-11-30 18:21:11 -0600979 WARN(readl_relaxed(reg) & 1, "failed to soft-reset %s\n", module);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000980}
981
Mugunthan V Ndf828592012-03-18 20:17:54 +0000982static void cpsw_set_slave_mac(struct cpsw_slave *slave,
983 struct cpsw_priv *priv)
984{
Richard Cochran9750a3a2012-10-29 08:45:15 +0000985 slave_write(slave, mac_hi(priv->mac_addr), SA_HI);
986 slave_write(slave, mac_lo(priv->mac_addr), SA_LO);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000987}
988
989static void _cpsw_adjust_link(struct cpsw_slave *slave,
990 struct cpsw_priv *priv, bool *link)
991{
992 struct phy_device *phy = slave->phy;
993 u32 mac_control = 0;
994 u32 slave_port;
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300995 struct cpsw_common *cpsw = priv->cpsw;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000996
997 if (!phy)
998 return;
999
Ivan Khoronzhuk6f1f5832016-08-10 02:22:34 +03001000 slave_port = cpsw_get_slave_port(slave->slave_num);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001001
1002 if (phy->link) {
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001003 mac_control = cpsw->data.mac_control;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001004
1005 /* enable forwarding */
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001006 cpsw_ale_control_set(cpsw->ale, slave_port,
Mugunthan V Ndf828592012-03-18 20:17:54 +00001007 ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
1008
1009 if (phy->speed == 1000)
1010 mac_control |= BIT(7); /* GIGABITEN */
1011 if (phy->duplex)
1012 mac_control |= BIT(0); /* FULLDUPLEXEN */
Daniel Mack342b7b72012-09-27 09:19:34 +00001013
1014 /* set speed_in input in case RMII mode is used in 100Mbps */
1015 if (phy->speed == 100)
1016 mac_control |= BIT(15);
SZ Lin (林上智)f9db5062018-03-16 00:56:01 +08001017 /* in band mode only works in 10Mbps RGMII mode */
1018 else if ((phy->speed == 10) && phy_interface_is_rgmii(phy))
Mugunthan V Na81d8762013-12-13 18:42:55 +05301019 mac_control |= BIT(18); /* In Band mode */
Daniel Mack342b7b72012-09-27 09:19:34 +00001020
Mugunthan V N1923d6e2014-09-08 22:54:02 +05301021 if (priv->rx_pause)
1022 mac_control |= BIT(3);
1023
1024 if (priv->tx_pause)
1025 mac_control |= BIT(4);
1026
Mugunthan V Ndf828592012-03-18 20:17:54 +00001027 *link = true;
1028 } else {
1029 mac_control = 0;
1030 /* disable forwarding */
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001031 cpsw_ale_control_set(cpsw->ale, slave_port,
Mugunthan V Ndf828592012-03-18 20:17:54 +00001032 ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
1033 }
1034
1035 if (mac_control != slave->mac_control) {
1036 phy_print_status(phy);
Grygorii Strashkodda5f5fe2017-11-30 18:21:11 -06001037 writel_relaxed(mac_control, &slave->sliver->mac_control);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001038 }
1039
1040 slave->mac_control = mac_control;
1041}
1042
Ivan Khoronzhuk0be01b82016-12-10 14:23:49 +02001043static int cpsw_get_common_speed(struct cpsw_common *cpsw)
1044{
1045 int i, speed;
1046
1047 for (i = 0, speed = 0; i < cpsw->data.slaves; i++)
1048 if (cpsw->slaves[i].phy && cpsw->slaves[i].phy->link)
1049 speed += cpsw->slaves[i].phy->speed;
1050
1051 return speed;
1052}
1053
1054static int cpsw_need_resplit(struct cpsw_common *cpsw)
1055{
1056 int i, rlim_ch_num;
1057 int speed, ch_rate;
1058
1059 /* re-split resources only in case speed was changed */
1060 speed = cpsw_get_common_speed(cpsw);
1061 if (speed == cpsw->speed || !speed)
1062 return 0;
1063
1064 cpsw->speed = speed;
1065
1066 for (i = 0, rlim_ch_num = 0; i < cpsw->tx_ch_num; i++) {
1067 ch_rate = cpdma_chan_get_rate(cpsw->txv[i].ch);
1068 if (!ch_rate)
1069 break;
1070
1071 rlim_ch_num++;
1072 }
1073
1074 /* cases not dependent on speed */
1075 if (!rlim_ch_num || rlim_ch_num == cpsw->tx_ch_num)
1076 return 0;
1077
1078 return 1;
1079}
1080
Mugunthan V Ndf828592012-03-18 20:17:54 +00001081static void cpsw_adjust_link(struct net_device *ndev)
1082{
1083 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk0be01b82016-12-10 14:23:49 +02001084 struct cpsw_common *cpsw = priv->cpsw;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001085 bool link = false;
1086
1087 for_each_slave(priv, _cpsw_adjust_link, priv, &link);
1088
1089 if (link) {
Ivan Khoronzhuk0be01b82016-12-10 14:23:49 +02001090 if (cpsw_need_resplit(cpsw))
1091 cpsw_split_res(ndev);
1092
Mugunthan V Ndf828592012-03-18 20:17:54 +00001093 netif_carrier_on(ndev);
1094 if (netif_running(ndev))
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001095 netif_tx_wake_all_queues(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001096 } else {
1097 netif_carrier_off(ndev);
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001098 netif_tx_stop_all_queues(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001099 }
1100}
1101
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001102static int cpsw_get_coalesce(struct net_device *ndev,
1103 struct ethtool_coalesce *coal)
1104{
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001105 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001106
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001107 coal->rx_coalesce_usecs = cpsw->coal_intvl;
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001108 return 0;
1109}
1110
1111static int cpsw_set_coalesce(struct net_device *ndev,
1112 struct ethtool_coalesce *coal)
1113{
1114 struct cpsw_priv *priv = netdev_priv(ndev);
1115 u32 int_ctrl;
1116 u32 num_interrupts = 0;
1117 u32 prescale = 0;
1118 u32 addnl_dvdr = 1;
1119 u32 coal_intvl = 0;
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001120 struct cpsw_common *cpsw = priv->cpsw;
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001121
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001122 coal_intvl = coal->rx_coalesce_usecs;
1123
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001124 int_ctrl = readl(&cpsw->wr_regs->int_control);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001125 prescale = cpsw->bus_freq_mhz * 4;
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001126
Mugunthan V Na84bc2a2014-07-15 20:26:53 +05301127 if (!coal->rx_coalesce_usecs) {
1128 int_ctrl &= ~(CPSW_INTPRESCALE_MASK | CPSW_INTPACEEN);
1129 goto update_return;
1130 }
1131
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001132 if (coal_intvl < CPSW_CMINTMIN_INTVL)
1133 coal_intvl = CPSW_CMINTMIN_INTVL;
1134
1135 if (coal_intvl > CPSW_CMINTMAX_INTVL) {
1136 /* Interrupt pacer works with 4us Pulse, we can
1137 * throttle further by dilating the 4us pulse.
1138 */
1139 addnl_dvdr = CPSW_INTPRESCALE_MASK / prescale;
1140
1141 if (addnl_dvdr > 1) {
1142 prescale *= addnl_dvdr;
1143 if (coal_intvl > (CPSW_CMINTMAX_INTVL * addnl_dvdr))
1144 coal_intvl = (CPSW_CMINTMAX_INTVL
1145 * addnl_dvdr);
1146 } else {
1147 addnl_dvdr = 1;
1148 coal_intvl = CPSW_CMINTMAX_INTVL;
1149 }
1150 }
1151
1152 num_interrupts = (1000 * addnl_dvdr) / coal_intvl;
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001153 writel(num_interrupts, &cpsw->wr_regs->rx_imax);
1154 writel(num_interrupts, &cpsw->wr_regs->tx_imax);
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001155
1156 int_ctrl |= CPSW_INTPACEEN;
1157 int_ctrl &= (~CPSW_INTPRESCALE_MASK);
1158 int_ctrl |= (prescale & CPSW_INTPRESCALE_MASK);
Mugunthan V Na84bc2a2014-07-15 20:26:53 +05301159
1160update_return:
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001161 writel(int_ctrl, &cpsw->wr_regs->int_control);
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001162
1163 cpsw_notice(priv, timer, "Set coalesce to %d usecs.\n", coal_intvl);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001164 cpsw->coal_intvl = coal_intvl;
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001165
1166 return 0;
1167}
1168
Mugunthan V Nd9718542013-07-23 15:38:17 +05301169static int cpsw_get_sset_count(struct net_device *ndev, int sset)
1170{
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001171 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1172
Mugunthan V Nd9718542013-07-23 15:38:17 +05301173 switch (sset) {
1174 case ETH_SS_STATS:
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001175 return (CPSW_STATS_COMMON_LEN +
1176 (cpsw->rx_ch_num + cpsw->tx_ch_num) *
1177 CPSW_STATS_CH_LEN);
Mugunthan V Nd9718542013-07-23 15:38:17 +05301178 default:
1179 return -EOPNOTSUPP;
1180 }
1181}
1182
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001183static void cpsw_add_ch_strings(u8 **p, int ch_num, int rx_dir)
1184{
1185 int ch_stats_len;
1186 int line;
1187 int i;
1188
1189 ch_stats_len = CPSW_STATS_CH_LEN * ch_num;
1190 for (i = 0; i < ch_stats_len; i++) {
1191 line = i % CPSW_STATS_CH_LEN;
1192 snprintf(*p, ETH_GSTRING_LEN,
1193 "%s DMA chan %d: %s", rx_dir ? "Rx" : "Tx",
1194 i / CPSW_STATS_CH_LEN,
1195 cpsw_gstrings_ch_stats[line].stat_string);
1196 *p += ETH_GSTRING_LEN;
1197 }
1198}
1199
Mugunthan V Nd9718542013-07-23 15:38:17 +05301200static void cpsw_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
1201{
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001202 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Mugunthan V Nd9718542013-07-23 15:38:17 +05301203 u8 *p = data;
1204 int i;
1205
1206 switch (stringset) {
1207 case ETH_SS_STATS:
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001208 for (i = 0; i < CPSW_STATS_COMMON_LEN; i++) {
Mugunthan V Nd9718542013-07-23 15:38:17 +05301209 memcpy(p, cpsw_gstrings_stats[i].stat_string,
1210 ETH_GSTRING_LEN);
1211 p += ETH_GSTRING_LEN;
1212 }
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001213
1214 cpsw_add_ch_strings(&p, cpsw->rx_ch_num, 1);
1215 cpsw_add_ch_strings(&p, cpsw->tx_ch_num, 0);
Mugunthan V Nd9718542013-07-23 15:38:17 +05301216 break;
1217 }
1218}
1219
1220static void cpsw_get_ethtool_stats(struct net_device *ndev,
1221 struct ethtool_stats *stats, u64 *data)
1222{
Mugunthan V Nd9718542013-07-23 15:38:17 +05301223 u8 *p;
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001224 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001225 struct cpdma_chan_stats ch_stats;
1226 int i, l, ch;
Mugunthan V Nd9718542013-07-23 15:38:17 +05301227
1228 /* Collect Davinci CPDMA stats for Rx and Tx Channel */
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001229 for (l = 0; l < CPSW_STATS_COMMON_LEN; l++)
1230 data[l] = readl(cpsw->hw_stats +
1231 cpsw_gstrings_stats[l].stat_offset);
Mugunthan V Nd9718542013-07-23 15:38:17 +05301232
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001233 for (ch = 0; ch < cpsw->rx_ch_num; ch++) {
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02001234 cpdma_chan_get_stats(cpsw->rxv[ch].ch, &ch_stats);
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001235 for (i = 0; i < CPSW_STATS_CH_LEN; i++, l++) {
1236 p = (u8 *)&ch_stats +
1237 cpsw_gstrings_ch_stats[i].stat_offset;
1238 data[l] = *(u32 *)p;
1239 }
1240 }
Mugunthan V Nd9718542013-07-23 15:38:17 +05301241
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001242 for (ch = 0; ch < cpsw->tx_ch_num; ch++) {
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02001243 cpdma_chan_get_stats(cpsw->txv[ch].ch, &ch_stats);
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001244 for (i = 0; i < CPSW_STATS_CH_LEN; i++, l++) {
1245 p = (u8 *)&ch_stats +
1246 cpsw_gstrings_ch_stats[i].stat_offset;
1247 data[l] = *(u32 *)p;
Mugunthan V Nd9718542013-07-23 15:38:17 +05301248 }
1249 }
1250}
1251
Ivan Khoronzhuk27e9e102016-08-10 02:22:32 +03001252static inline int cpsw_tx_packet_submit(struct cpsw_priv *priv,
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001253 struct sk_buff *skb,
1254 struct cpdma_chan *txch)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001255{
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001256 struct cpsw_common *cpsw = priv->cpsw;
1257
Ivan Khoronzhuk98fdd852017-06-27 16:58:51 +03001258 skb_tx_timestamp(skb);
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001259 return cpdma_chan_submit(txch, skb, skb->data, skb->len,
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001260 priv->emac_port + cpsw->data.dual_emac);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001261}
1262
1263static inline void cpsw_add_dual_emac_def_ale_entries(
1264 struct cpsw_priv *priv, struct cpsw_slave *slave,
1265 u32 slave_port)
1266{
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001267 struct cpsw_common *cpsw = priv->cpsw;
Grygorii Strashko71a2cbb2016-04-07 15:16:44 +03001268 u32 port_mask = 1 << slave_port | ALE_PORT_HOST;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001269
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001270 if (cpsw->version == CPSW_VERSION_1)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001271 slave_write(slave, slave->port_vlan, CPSW1_PORT_VLAN);
1272 else
1273 slave_write(slave, slave->port_vlan, CPSW2_PORT_VLAN);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001274 cpsw_ale_add_vlan(cpsw->ale, slave->port_vlan, port_mask,
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001275 port_mask, port_mask, 0);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001276 cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001277 port_mask, ALE_VLAN, slave->port_vlan, 0);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001278 cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr,
1279 HOST_PORT_NUM, ALE_VLAN |
1280 ALE_SECURE, slave->port_vlan);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001281}
1282
Daniel Mack1e7a2e22013-11-15 08:29:16 +01001283static void soft_reset_slave(struct cpsw_slave *slave)
Mugunthan V Ndf828592012-03-18 20:17:54 +00001284{
1285 char name[32];
Daniel Mack1e7a2e22013-11-15 08:29:16 +01001286
1287 snprintf(name, sizeof(name), "slave-%d", slave->slave_num);
1288 soft_reset(name, &slave->sliver->soft_reset);
1289}
1290
1291static void cpsw_slave_open(struct cpsw_slave *slave, struct cpsw_priv *priv)
1292{
Mugunthan V Ndf828592012-03-18 20:17:54 +00001293 u32 slave_port;
Sekhar Nori30c57f02017-04-03 17:34:28 +05301294 struct phy_device *phy;
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03001295 struct cpsw_common *cpsw = priv->cpsw;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001296
Daniel Mack1e7a2e22013-11-15 08:29:16 +01001297 soft_reset_slave(slave);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001298
1299 /* setup priority mapping */
Grygorii Strashkodda5f5fe2017-11-30 18:21:11 -06001300 writel_relaxed(RX_PRIORITY_MAPPING, &slave->sliver->rx_pri_map);
Richard Cochran9750a3a2012-10-29 08:45:15 +00001301
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001302 switch (cpsw->version) {
Richard Cochran9750a3a2012-10-29 08:45:15 +00001303 case CPSW_VERSION_1:
1304 slave_write(slave, TX_PRIORITY_MAPPING, CPSW1_TX_PRI_MAP);
Grygorii Strashko48f5bcc2017-05-08 14:21:21 -05001305 /* Increase RX FIFO size to 5 for supporting fullduplex
1306 * flow control mode
1307 */
1308 slave_write(slave,
1309 (CPSW_MAX_BLKS_TX << CPSW_MAX_BLKS_TX_SHIFT) |
1310 CPSW_MAX_BLKS_RX, CPSW1_MAX_BLKS);
Richard Cochran9750a3a2012-10-29 08:45:15 +00001311 break;
1312 case CPSW_VERSION_2:
Mugunthan V Nc193f362013-08-05 17:30:05 +05301313 case CPSW_VERSION_3:
Mugunthan V N926489b2013-08-12 17:11:15 +05301314 case CPSW_VERSION_4:
Richard Cochran9750a3a2012-10-29 08:45:15 +00001315 slave_write(slave, TX_PRIORITY_MAPPING, CPSW2_TX_PRI_MAP);
Grygorii Strashko48f5bcc2017-05-08 14:21:21 -05001316 /* Increase RX FIFO size to 5 for supporting fullduplex
1317 * flow control mode
1318 */
1319 slave_write(slave,
1320 (CPSW_MAX_BLKS_TX << CPSW_MAX_BLKS_TX_SHIFT) |
1321 CPSW_MAX_BLKS_RX, CPSW2_MAX_BLKS);
Richard Cochran9750a3a2012-10-29 08:45:15 +00001322 break;
1323 }
Mugunthan V Ndf828592012-03-18 20:17:54 +00001324
1325 /* setup max packet size, and mac address */
Grygorii Strashkodda5f5fe2017-11-30 18:21:11 -06001326 writel_relaxed(cpsw->rx_packet_max, &slave->sliver->rx_maxlen);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001327 cpsw_set_slave_mac(slave, priv);
1328
1329 slave->mac_control = 0; /* no link yet */
1330
Ivan Khoronzhuk6f1f5832016-08-10 02:22:34 +03001331 slave_port = cpsw_get_slave_port(slave->slave_num);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001332
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001333 if (cpsw->data.dual_emac)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001334 cpsw_add_dual_emac_def_ale_entries(priv, slave, slave_port);
1335 else
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001336 cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001337 1 << slave_port, 0, 0, ALE_MCAST_FWD_2);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001338
David Rivshind733f7542016-04-27 21:32:31 -04001339 if (slave->data->phy_node) {
Sekhar Nori30c57f02017-04-03 17:34:28 +05301340 phy = of_phy_connect(priv->ndev, slave->data->phy_node,
Heiko Schocher9e42f712015-10-17 06:04:35 +02001341 &cpsw_adjust_link, 0, slave->data->phy_if);
Sekhar Nori30c57f02017-04-03 17:34:28 +05301342 if (!phy) {
Rob Herringf7ce9102017-07-18 16:43:19 -05001343 dev_err(priv->dev, "phy \"%pOF\" not found on slave %d\n",
1344 slave->data->phy_node,
David Rivshind733f7542016-04-27 21:32:31 -04001345 slave->slave_num);
1346 return;
1347 }
1348 } else {
Sekhar Nori30c57f02017-04-03 17:34:28 +05301349 phy = phy_connect(priv->ndev, slave->data->phy_id,
Florian Fainellif9a8f832013-01-14 00:52:52 +00001350 &cpsw_adjust_link, slave->data->phy_if);
Sekhar Nori30c57f02017-04-03 17:34:28 +05301351 if (IS_ERR(phy)) {
David Rivshind733f7542016-04-27 21:32:31 -04001352 dev_err(priv->dev,
1353 "phy \"%s\" not found on slave %d, err %ld\n",
1354 slave->data->phy_id, slave->slave_num,
Sekhar Nori30c57f02017-04-03 17:34:28 +05301355 PTR_ERR(phy));
David Rivshind733f7542016-04-27 21:32:31 -04001356 return;
1357 }
Mugunthan V Ndf828592012-03-18 20:17:54 +00001358 }
David Rivshind733f7542016-04-27 21:32:31 -04001359
Sekhar Nori30c57f02017-04-03 17:34:28 +05301360 slave->phy = phy;
1361
David Rivshind733f7542016-04-27 21:32:31 -04001362 phy_attached_info(slave->phy);
1363
1364 phy_start(slave->phy);
1365
1366 /* Configure GMII_SEL register */
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001367 cpsw_phy_sel(cpsw->dev, slave->phy->interface, slave->slave_num);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001368}
1369
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001370static inline void cpsw_add_default_vlan(struct cpsw_priv *priv)
1371{
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001372 struct cpsw_common *cpsw = priv->cpsw;
1373 const int vlan = cpsw->data.default_vlan;
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001374 u32 reg;
1375 int i;
Lennart Sorensen1e5c4bc2014-10-31 13:38:52 -04001376 int unreg_mcast_mask;
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001377
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001378 reg = (cpsw->version == CPSW_VERSION_1) ? CPSW1_PORT_VLAN :
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001379 CPSW2_PORT_VLAN;
1380
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001381 writel(vlan, &cpsw->host_port_regs->port_vlan);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001382
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001383 for (i = 0; i < cpsw->data.slaves; i++)
1384 slave_write(cpsw->slaves + i, vlan, reg);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001385
Lennart Sorensen1e5c4bc2014-10-31 13:38:52 -04001386 if (priv->ndev->flags & IFF_ALLMULTI)
1387 unreg_mcast_mask = ALE_ALL_PORTS;
1388 else
1389 unreg_mcast_mask = ALE_PORT_1 | ALE_PORT_2;
1390
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001391 cpsw_ale_add_vlan(cpsw->ale, vlan, ALE_ALL_PORTS,
Grygorii Strashko61f1cef2016-04-07 15:16:43 +03001392 ALE_ALL_PORTS, ALE_ALL_PORTS,
1393 unreg_mcast_mask);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001394}
1395
Mugunthan V Ndf828592012-03-18 20:17:54 +00001396static void cpsw_init_host_port(struct cpsw_priv *priv)
1397{
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001398 u32 fifo_mode;
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001399 u32 control_reg;
1400 struct cpsw_common *cpsw = priv->cpsw;
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001401
Mugunthan V Ndf828592012-03-18 20:17:54 +00001402 /* soft reset the controller and initialize ale */
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001403 soft_reset("cpsw", &cpsw->regs->soft_reset);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001404 cpsw_ale_start(cpsw->ale);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001405
1406 /* switch to vlan unaware mode */
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001407 cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_VLAN_AWARE,
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001408 CPSW_ALE_VLAN_AWARE);
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001409 control_reg = readl(&cpsw->regs->control);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001410 control_reg |= CPSW_VLAN_AWARE;
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001411 writel(control_reg, &cpsw->regs->control);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001412 fifo_mode = (cpsw->data.dual_emac) ? CPSW_FIFO_DUAL_MAC_MODE :
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001413 CPSW_FIFO_NORMAL_MODE;
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001414 writel(fifo_mode, &cpsw->host_port_regs->tx_in_ctl);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001415
1416 /* setup host port priority mapping */
Grygorii Strashkodda5f5fe2017-11-30 18:21:11 -06001417 writel_relaxed(CPDMA_TX_PRIORITY_MAP,
1418 &cpsw->host_port_regs->cpdma_tx_pri_map);
1419 writel_relaxed(0, &cpsw->host_port_regs->cpdma_rx_chan_map);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001420
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001421 cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM,
Mugunthan V Ndf828592012-03-18 20:17:54 +00001422 ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
1423
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001424 if (!cpsw->data.dual_emac) {
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001425 cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr, HOST_PORT_NUM,
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001426 0, 0);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001427 cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
Grygorii Strashko71a2cbb2016-04-07 15:16:44 +03001428 ALE_PORT_HOST, 0, 0, ALE_MCAST_FWD_2);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001429 }
Mugunthan V Ndf828592012-03-18 20:17:54 +00001430}
1431
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +03001432static int cpsw_fill_rx_channels(struct cpsw_priv *priv)
1433{
1434 struct cpsw_common *cpsw = priv->cpsw;
1435 struct sk_buff *skb;
1436 int ch_buf_num;
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001437 int ch, i, ret;
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +03001438
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001439 for (ch = 0; ch < cpsw->rx_ch_num; ch++) {
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02001440 ch_buf_num = cpdma_chan_get_rx_buf_num(cpsw->rxv[ch].ch);
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001441 for (i = 0; i < ch_buf_num; i++) {
1442 skb = __netdev_alloc_skb_ip_align(priv->ndev,
1443 cpsw->rx_packet_max,
1444 GFP_KERNEL);
1445 if (!skb) {
1446 cpsw_err(priv, ifup, "cannot allocate skb\n");
1447 return -ENOMEM;
1448 }
1449
1450 skb_set_queue_mapping(skb, ch);
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02001451 ret = cpdma_chan_submit(cpsw->rxv[ch].ch, skb,
1452 skb->data, skb_tailroom(skb),
1453 0);
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001454 if (ret < 0) {
1455 cpsw_err(priv, ifup,
1456 "cannot submit skb to channel %d rx, error %d\n",
1457 ch, ret);
1458 kfree_skb(skb);
1459 return ret;
1460 }
1461 kmemleak_not_leak(skb);
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +03001462 }
1463
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001464 cpsw_info(priv, ifup, "ch %d rx, submitted %d descriptors\n",
1465 ch, ch_buf_num);
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +03001466 }
1467
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001468 return 0;
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +03001469}
1470
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001471static void cpsw_slave_stop(struct cpsw_slave *slave, struct cpsw_common *cpsw)
Sebastian Siewioraacebbf2013-04-23 07:31:36 +00001472{
Schuyler Patton3995d262014-03-03 16:19:06 +05301473 u32 slave_port;
1474
Ivan Khoronzhuk6f1f5832016-08-10 02:22:34 +03001475 slave_port = cpsw_get_slave_port(slave->slave_num);
Schuyler Patton3995d262014-03-03 16:19:06 +05301476
Sebastian Siewioraacebbf2013-04-23 07:31:36 +00001477 if (!slave->phy)
1478 return;
1479 phy_stop(slave->phy);
1480 phy_disconnect(slave->phy);
1481 slave->phy = NULL;
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001482 cpsw_ale_control_set(cpsw->ale, slave_port,
Schuyler Patton3995d262014-03-03 16:19:06 +05301483 ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
Grygorii Strashko1f95ba02016-06-24 21:23:41 +03001484 soft_reset_slave(slave);
Sebastian Siewioraacebbf2013-04-23 07:31:36 +00001485}
1486
Mugunthan V Ndf828592012-03-18 20:17:54 +00001487static int cpsw_ndo_open(struct net_device *ndev)
1488{
1489 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03001490 struct cpsw_common *cpsw = priv->cpsw;
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +03001491 int ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001492 u32 reg;
1493
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001494 ret = pm_runtime_get_sync(cpsw->dev);
Grygorii Strashko108a6532016-06-24 21:23:42 +03001495 if (ret < 0) {
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001496 pm_runtime_put_noidle(cpsw->dev);
Grygorii Strashko108a6532016-06-24 21:23:42 +03001497 return ret;
1498 }
Grygorii Strashko3fa88c52016-04-19 21:09:49 +03001499
Mugunthan V Ndf828592012-03-18 20:17:54 +00001500 netif_carrier_off(ndev);
1501
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001502 /* Notify the stack of the actual queue counts. */
1503 ret = netif_set_real_num_tx_queues(ndev, cpsw->tx_ch_num);
1504 if (ret) {
1505 dev_err(priv->dev, "cannot set real number of tx queues\n");
1506 goto err_cleanup;
1507 }
1508
1509 ret = netif_set_real_num_rx_queues(ndev, cpsw->rx_ch_num);
1510 if (ret) {
1511 dev_err(priv->dev, "cannot set real number of rx queues\n");
1512 goto err_cleanup;
1513 }
1514
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001515 reg = cpsw->version;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001516
1517 dev_info(priv->dev, "initializing cpsw version %d.%d (%d)\n",
1518 CPSW_MAJOR_VERSION(reg), CPSW_MINOR_VERSION(reg),
1519 CPSW_RTL_VERSION(reg));
1520
Ivan Khoronzhukd5bc1612017-02-14 16:02:36 +02001521 /* Initialize host and slave ports */
1522 if (!cpsw->usage_count)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001523 cpsw_init_host_port(priv);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001524 for_each_slave(priv, cpsw_slave_open, priv);
1525
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001526 /* Add default VLAN */
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001527 if (!cpsw->data.dual_emac)
Mugunthan V Ne6afea02014-06-18 17:21:48 +05301528 cpsw_add_default_vlan(priv);
1529 else
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001530 cpsw_ale_add_vlan(cpsw->ale, cpsw->data.default_vlan,
Grygorii Strashko61f1cef2016-04-07 15:16:43 +03001531 ALE_ALL_PORTS, ALE_ALL_PORTS, 0, 0);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001532
Ivan Khoronzhukd5bc1612017-02-14 16:02:36 +02001533 /* initialize shared resources for every ndev */
1534 if (!cpsw->usage_count) {
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001535 /* disable priority elevation */
Grygorii Strashkodda5f5fe2017-11-30 18:21:11 -06001536 writel_relaxed(0, &cpsw->regs->ptype);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001537
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001538 /* enable statistics collection only on all ports */
Grygorii Strashkodda5f5fe2017-11-30 18:21:11 -06001539 writel_relaxed(0x7, &cpsw->regs->stat_port_en);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001540
Mugunthan V N1923d6e2014-09-08 22:54:02 +05301541 /* Enable internal fifo flow control */
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001542 writel(0x7, &cpsw->regs->flow_control);
Mugunthan V N1923d6e2014-09-08 22:54:02 +05301543
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +03001544 napi_enable(&cpsw->napi_rx);
1545 napi_enable(&cpsw->napi_tx);
Mugunthan V Nd354eb82015-08-04 16:06:19 +05301546
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +03001547 if (cpsw->tx_irq_disabled) {
1548 cpsw->tx_irq_disabled = false;
1549 enable_irq(cpsw->irqs_table[1]);
Mugunthan V N7da11602015-08-12 15:22:53 +05301550 }
1551
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +03001552 if (cpsw->rx_irq_disabled) {
1553 cpsw->rx_irq_disabled = false;
1554 enable_irq(cpsw->irqs_table[0]);
Mugunthan V N7da11602015-08-12 15:22:53 +05301555 }
1556
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +03001557 ret = cpsw_fill_rx_channels(priv);
1558 if (ret < 0)
1559 goto err_cleanup;
Mugunthan V Nf280e892013-12-11 22:09:05 -06001560
Grygorii Strashko8a2c9a52016-12-06 18:00:41 -06001561 if (cpts_register(cpsw->cpts))
Mugunthan V Nf280e892013-12-11 22:09:05 -06001562 dev_err(priv->dev, "error registering cpts device\n");
1563
Mugunthan V Ndf828592012-03-18 20:17:54 +00001564 }
Mugunthan V Ndf828592012-03-18 20:17:54 +00001565
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001566 /* Enable Interrupt pacing if configured */
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001567 if (cpsw->coal_intvl != 0) {
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001568 struct ethtool_coalesce coal;
1569
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001570 coal.rx_coalesce_usecs = cpsw->coal_intvl;
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001571 cpsw_set_coalesce(ndev, &coal);
1572 }
1573
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001574 cpdma_ctlr_start(cpsw->dma);
1575 cpsw_intr_enable(cpsw);
Ivan Khoronzhukd5bc1612017-02-14 16:02:36 +02001576 cpsw->usage_count++;
Mugunthan V Nf63a9752014-04-10 14:23:24 +05301577
Mugunthan V Ndf828592012-03-18 20:17:54 +00001578 return 0;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001579
Sebastian Siewioraacebbf2013-04-23 07:31:36 +00001580err_cleanup:
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001581 cpdma_ctlr_stop(cpsw->dma);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001582 for_each_slave(priv, cpsw_slave_stop, cpsw);
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001583 pm_runtime_put_sync(cpsw->dev);
Sebastian Siewioraacebbf2013-04-23 07:31:36 +00001584 netif_carrier_off(priv->ndev);
1585 return ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001586}
1587
1588static int cpsw_ndo_stop(struct net_device *ndev)
1589{
1590 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03001591 struct cpsw_common *cpsw = priv->cpsw;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001592
1593 cpsw_info(priv, ifdown, "shutting down cpsw device\n");
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001594 netif_tx_stop_all_queues(priv->ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001595 netif_carrier_off(priv->ndev);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001596
Ivan Khoronzhukd5bc1612017-02-14 16:02:36 +02001597 if (cpsw->usage_count <= 1) {
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +03001598 napi_disable(&cpsw->napi_rx);
1599 napi_disable(&cpsw->napi_tx);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001600 cpts_unregister(cpsw->cpts);
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001601 cpsw_intr_disable(cpsw);
1602 cpdma_ctlr_stop(cpsw->dma);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001603 cpsw_ale_stop(cpsw->ale);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001604 }
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001605 for_each_slave(priv, cpsw_slave_stop, cpsw);
Ivan Khoronzhuk0be01b82016-12-10 14:23:49 +02001606
1607 if (cpsw_need_resplit(cpsw))
1608 cpsw_split_res(ndev);
1609
Ivan Khoronzhukd5bc1612017-02-14 16:02:36 +02001610 cpsw->usage_count--;
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001611 pm_runtime_put_sync(cpsw->dev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001612 return 0;
1613}
1614
1615static netdev_tx_t cpsw_ndo_start_xmit(struct sk_buff *skb,
1616 struct net_device *ndev)
1617{
1618 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001619 struct cpsw_common *cpsw = priv->cpsw;
Ivan Khoronzhukf44f8412017-06-27 16:58:52 +03001620 struct cpts *cpts = cpsw->cpts;
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001621 struct netdev_queue *txq;
1622 struct cpdma_chan *txch;
1623 int ret, q_idx;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001624
Mugunthan V Ndf828592012-03-18 20:17:54 +00001625 if (skb_padto(skb, CPSW_MIN_PACKET_SIZE)) {
1626 cpsw_err(priv, tx_err, "packet pad failed\n");
Tobias Klauser8dc43dd2014-03-10 13:12:23 +01001627 ndev->stats.tx_dropped++;
Ivan Khoronzhuk1bf96052017-02-11 03:49:57 +02001628 return NET_XMIT_DROP;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001629 }
1630
Mugunthan V N9232b162013-02-11 09:52:19 +00001631 if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
Ivan Khoronzhukf44f8412017-06-27 16:58:52 +03001632 cpts_is_tx_enabled(cpts) && cpts_can_timestamp(cpts, skb))
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001633 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
1634
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001635 q_idx = skb_get_queue_mapping(skb);
1636 if (q_idx >= cpsw->tx_ch_num)
1637 q_idx = q_idx % cpsw->tx_ch_num;
1638
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02001639 txch = cpsw->txv[q_idx].ch;
Grygorii Strashko62f94c22018-02-06 19:17:06 -06001640 txq = netdev_get_tx_queue(ndev, q_idx);
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001641 ret = cpsw_tx_packet_submit(priv, skb, txch);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001642 if (unlikely(ret != 0)) {
1643 cpsw_err(priv, tx_err, "desc submit failed\n");
1644 goto fail;
1645 }
1646
Mugunthan V Nfae50822013-01-17 06:31:34 +00001647 /* If there is no more tx desc left free then we need to
1648 * tell the kernel to stop sending us tx frames.
1649 */
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001650 if (unlikely(!cpdma_check_free_tx_desc(txch))) {
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001651 netif_tx_stop_queue(txq);
Grygorii Strashko62f94c22018-02-06 19:17:06 -06001652
1653 /* Barrier, so that stop_queue visible to other cpus */
1654 smp_mb__after_atomic();
1655
1656 if (cpdma_check_free_tx_desc(txch))
1657 netif_tx_wake_queue(txq);
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001658 }
Mugunthan V Nfae50822013-01-17 06:31:34 +00001659
Mugunthan V Ndf828592012-03-18 20:17:54 +00001660 return NETDEV_TX_OK;
1661fail:
Tobias Klauser8dc43dd2014-03-10 13:12:23 +01001662 ndev->stats.tx_dropped++;
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001663 netif_tx_stop_queue(txq);
Grygorii Strashko62f94c22018-02-06 19:17:06 -06001664
1665 /* Barrier, so that stop_queue visible to other cpus */
1666 smp_mb__after_atomic();
1667
1668 if (cpdma_check_free_tx_desc(txch))
1669 netif_tx_wake_queue(txq);
1670
Mugunthan V Ndf828592012-03-18 20:17:54 +00001671 return NETDEV_TX_BUSY;
1672}
1673
Grygorii Strashkoc8395d42016-12-06 18:00:34 -06001674#if IS_ENABLED(CONFIG_TI_CPTS)
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001675
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001676static void cpsw_hwtstamp_v1(struct cpsw_common *cpsw)
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001677{
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001678 struct cpsw_slave *slave = &cpsw->slaves[cpsw->data.active_slave];
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001679 u32 ts_en, seq_id;
1680
Grygorii Strashkob63ba582016-12-06 18:00:35 -06001681 if (!cpts_is_tx_enabled(cpsw->cpts) &&
1682 !cpts_is_rx_enabled(cpsw->cpts)) {
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001683 slave_write(slave, 0, CPSW1_TS_CTL);
1684 return;
1685 }
1686
1687 seq_id = (30 << CPSW_V1_SEQ_ID_OFS_SHIFT) | ETH_P_1588;
1688 ts_en = EVENT_MSG_BITS << CPSW_V1_MSG_TYPE_OFS;
1689
Grygorii Strashkob63ba582016-12-06 18:00:35 -06001690 if (cpts_is_tx_enabled(cpsw->cpts))
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001691 ts_en |= CPSW_V1_TS_TX_EN;
1692
Grygorii Strashkob63ba582016-12-06 18:00:35 -06001693 if (cpts_is_rx_enabled(cpsw->cpts))
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001694 ts_en |= CPSW_V1_TS_RX_EN;
1695
1696 slave_write(slave, ts_en, CPSW1_TS_CTL);
1697 slave_write(slave, seq_id, CPSW1_TS_SEQ_LTYPE);
1698}
1699
1700static void cpsw_hwtstamp_v2(struct cpsw_priv *priv)
1701{
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001702 struct cpsw_slave *slave;
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001703 struct cpsw_common *cpsw = priv->cpsw;
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001704 u32 ctrl, mtype;
1705
Ivan Khoronzhukcb7d78d02016-12-10 14:23:46 +02001706 slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001707
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001708 ctrl = slave_read(slave, CPSW2_CONTROL);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001709 switch (cpsw->version) {
George Cherian09c55372014-05-02 12:02:02 +05301710 case CPSW_VERSION_2:
1711 ctrl &= ~CTRL_V2_ALL_TS_MASK;
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001712
Grygorii Strashkob63ba582016-12-06 18:00:35 -06001713 if (cpts_is_tx_enabled(cpsw->cpts))
George Cherian09c55372014-05-02 12:02:02 +05301714 ctrl |= CTRL_V2_TX_TS_BITS;
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001715
Grygorii Strashkob63ba582016-12-06 18:00:35 -06001716 if (cpts_is_rx_enabled(cpsw->cpts))
George Cherian09c55372014-05-02 12:02:02 +05301717 ctrl |= CTRL_V2_RX_TS_BITS;
Richard Cochran26fe7eb2015-05-25 11:02:13 +02001718 break;
George Cherian09c55372014-05-02 12:02:02 +05301719 case CPSW_VERSION_3:
1720 default:
1721 ctrl &= ~CTRL_V3_ALL_TS_MASK;
1722
Grygorii Strashkob63ba582016-12-06 18:00:35 -06001723 if (cpts_is_tx_enabled(cpsw->cpts))
George Cherian09c55372014-05-02 12:02:02 +05301724 ctrl |= CTRL_V3_TX_TS_BITS;
1725
Grygorii Strashkob63ba582016-12-06 18:00:35 -06001726 if (cpts_is_rx_enabled(cpsw->cpts))
George Cherian09c55372014-05-02 12:02:02 +05301727 ctrl |= CTRL_V3_RX_TS_BITS;
Richard Cochran26fe7eb2015-05-25 11:02:13 +02001728 break;
George Cherian09c55372014-05-02 12:02:02 +05301729 }
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001730
1731 mtype = (30 << TS_SEQ_ID_OFFSET_SHIFT) | EVENT_MSG_BITS;
1732
1733 slave_write(slave, mtype, CPSW2_TS_SEQ_MTYPE);
1734 slave_write(slave, ctrl, CPSW2_CONTROL);
Grygorii Strashkodda5f5fe2017-11-30 18:21:11 -06001735 writel_relaxed(ETH_P_1588, &cpsw->regs->ts_ltype);
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001736}
1737
Ben Hutchingsa5b41452013-11-18 23:23:40 +00001738static int cpsw_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001739{
Mugunthan V N3177bf62012-11-27 07:53:40 +00001740 struct cpsw_priv *priv = netdev_priv(dev);
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001741 struct hwtstamp_config cfg;
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001742 struct cpsw_common *cpsw = priv->cpsw;
1743 struct cpts *cpts = cpsw->cpts;
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001744
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001745 if (cpsw->version != CPSW_VERSION_1 &&
1746 cpsw->version != CPSW_VERSION_2 &&
1747 cpsw->version != CPSW_VERSION_3)
Ben Hutchings2ee91e52013-11-14 00:47:36 +00001748 return -EOPNOTSUPP;
1749
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001750 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
1751 return -EFAULT;
1752
1753 /* reserved for future extensions */
1754 if (cfg.flags)
1755 return -EINVAL;
1756
Ben Hutchings2ee91e52013-11-14 00:47:36 +00001757 if (cfg.tx_type != HWTSTAMP_TX_OFF && cfg.tx_type != HWTSTAMP_TX_ON)
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001758 return -ERANGE;
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001759
1760 switch (cfg.rx_filter) {
1761 case HWTSTAMP_FILTER_NONE:
Grygorii Strashkob63ba582016-12-06 18:00:35 -06001762 cpts_rx_enable(cpts, 0);
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001763 break;
1764 case HWTSTAMP_FILTER_ALL:
Grygorii Strashkoe9523a52017-06-08 13:51:31 -05001765 case HWTSTAMP_FILTER_NTP_ALL:
1766 return -ERANGE;
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001767 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
1768 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
1769 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
Grygorii Strashkoe9523a52017-06-08 13:51:31 -05001770 cpts_rx_enable(cpts, HWTSTAMP_FILTER_PTP_V1_L4_EVENT);
1771 cfg.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
1772 break;
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001773 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
1774 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
1775 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
1776 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1777 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
1778 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
1779 case HWTSTAMP_FILTER_PTP_V2_EVENT:
1780 case HWTSTAMP_FILTER_PTP_V2_SYNC:
1781 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
Grygorii Strashkoe9523a52017-06-08 13:51:31 -05001782 cpts_rx_enable(cpts, HWTSTAMP_FILTER_PTP_V2_EVENT);
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001783 cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
1784 break;
1785 default:
1786 return -ERANGE;
1787 }
1788
Grygorii Strashkob63ba582016-12-06 18:00:35 -06001789 cpts_tx_enable(cpts, cfg.tx_type == HWTSTAMP_TX_ON);
Ben Hutchings2ee91e52013-11-14 00:47:36 +00001790
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001791 switch (cpsw->version) {
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001792 case CPSW_VERSION_1:
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001793 cpsw_hwtstamp_v1(cpsw);
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001794 break;
1795 case CPSW_VERSION_2:
George Cherianf7d403c2014-05-02 12:02:01 +05301796 case CPSW_VERSION_3:
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001797 cpsw_hwtstamp_v2(priv);
1798 break;
1799 default:
Ben Hutchings2ee91e52013-11-14 00:47:36 +00001800 WARN_ON(1);
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001801 }
1802
1803 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
1804}
1805
Ben Hutchingsa5b41452013-11-18 23:23:40 +00001806static int cpsw_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
1807{
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001808 struct cpsw_common *cpsw = ndev_to_cpsw(dev);
1809 struct cpts *cpts = cpsw->cpts;
Ben Hutchingsa5b41452013-11-18 23:23:40 +00001810 struct hwtstamp_config cfg;
1811
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001812 if (cpsw->version != CPSW_VERSION_1 &&
1813 cpsw->version != CPSW_VERSION_2 &&
1814 cpsw->version != CPSW_VERSION_3)
Ben Hutchingsa5b41452013-11-18 23:23:40 +00001815 return -EOPNOTSUPP;
1816
1817 cfg.flags = 0;
Grygorii Strashkob63ba582016-12-06 18:00:35 -06001818 cfg.tx_type = cpts_is_tx_enabled(cpts) ?
1819 HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF;
1820 cfg.rx_filter = (cpts_is_rx_enabled(cpts) ?
Grygorii Strashkoe9523a52017-06-08 13:51:31 -05001821 cpts->rx_enable : HWTSTAMP_FILTER_NONE);
Ben Hutchingsa5b41452013-11-18 23:23:40 +00001822
1823 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
1824}
Grygorii Strashkoc8395d42016-12-06 18:00:34 -06001825#else
1826static int cpsw_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
1827{
1828 return -EOPNOTSUPP;
1829}
Ben Hutchingsa5b41452013-11-18 23:23:40 +00001830
Grygorii Strashkoc8395d42016-12-06 18:00:34 -06001831static int cpsw_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
1832{
1833 return -EOPNOTSUPP;
1834}
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001835#endif /*CONFIG_TI_CPTS*/
1836
1837static int cpsw_ndo_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
1838{
Mugunthan V N11f2c982013-03-11 23:16:38 +00001839 struct cpsw_priv *priv = netdev_priv(dev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001840 struct cpsw_common *cpsw = priv->cpsw;
1841 int slave_no = cpsw_slave_index(cpsw, priv);
Mugunthan V N11f2c982013-03-11 23:16:38 +00001842
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001843 if (!netif_running(dev))
1844 return -EINVAL;
1845
Mugunthan V N11f2c982013-03-11 23:16:38 +00001846 switch (cmd) {
Mugunthan V N11f2c982013-03-11 23:16:38 +00001847 case SIOCSHWTSTAMP:
Ben Hutchingsa5b41452013-11-18 23:23:40 +00001848 return cpsw_hwtstamp_set(dev, req);
1849 case SIOCGHWTSTAMP:
1850 return cpsw_hwtstamp_get(dev, req);
Mugunthan V N11f2c982013-03-11 23:16:38 +00001851 }
1852
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001853 if (!cpsw->slaves[slave_no].phy)
Stefan Sørensenc1b59942014-02-16 14:54:25 +01001854 return -EOPNOTSUPP;
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001855 return phy_mii_ioctl(cpsw->slaves[slave_no].phy, req, cmd);
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001856}
1857
Mugunthan V Ndf828592012-03-18 20:17:54 +00001858static void cpsw_ndo_tx_timeout(struct net_device *ndev)
1859{
1860 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001861 struct cpsw_common *cpsw = priv->cpsw;
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001862 int ch;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001863
1864 cpsw_err(priv, tx_err, "transmit timeout, restarting dma\n");
Tobias Klauser8dc43dd2014-03-10 13:12:23 +01001865 ndev->stats.tx_errors++;
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001866 cpsw_intr_disable(cpsw);
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001867 for (ch = 0; ch < cpsw->tx_ch_num; ch++) {
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02001868 cpdma_chan_stop(cpsw->txv[ch].ch);
1869 cpdma_chan_start(cpsw->txv[ch].ch);
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001870 }
1871
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001872 cpsw_intr_enable(cpsw);
Grygorii Strashko75514b62017-03-31 18:41:23 -05001873 netif_trans_update(ndev);
1874 netif_tx_wake_all_queues(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001875}
1876
Mugunthan V Ndcfd8d52013-07-25 23:44:01 +05301877static int cpsw_ndo_set_mac_address(struct net_device *ndev, void *p)
1878{
1879 struct cpsw_priv *priv = netdev_priv(ndev);
1880 struct sockaddr *addr = (struct sockaddr *)p;
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03001881 struct cpsw_common *cpsw = priv->cpsw;
Mugunthan V Ndcfd8d52013-07-25 23:44:01 +05301882 int flags = 0;
1883 u16 vid = 0;
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001884 int ret;
Mugunthan V Ndcfd8d52013-07-25 23:44:01 +05301885
1886 if (!is_valid_ether_addr(addr->sa_data))
1887 return -EADDRNOTAVAIL;
1888
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001889 ret = pm_runtime_get_sync(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001890 if (ret < 0) {
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001891 pm_runtime_put_noidle(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001892 return ret;
1893 }
1894
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001895 if (cpsw->data.dual_emac) {
1896 vid = cpsw->slaves[priv->emac_port].port_vlan;
Mugunthan V Ndcfd8d52013-07-25 23:44:01 +05301897 flags = ALE_VLAN;
1898 }
1899
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001900 cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr, HOST_PORT_NUM,
Mugunthan V Ndcfd8d52013-07-25 23:44:01 +05301901 flags, vid);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001902 cpsw_ale_add_ucast(cpsw->ale, addr->sa_data, HOST_PORT_NUM,
Mugunthan V Ndcfd8d52013-07-25 23:44:01 +05301903 flags, vid);
1904
1905 memcpy(priv->mac_addr, addr->sa_data, ETH_ALEN);
1906 memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
1907 for_each_slave(priv, cpsw_set_slave_mac, priv);
1908
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001909 pm_runtime_put(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001910
Mugunthan V Ndcfd8d52013-07-25 23:44:01 +05301911 return 0;
1912}
1913
Mugunthan V Ndf828592012-03-18 20:17:54 +00001914#ifdef CONFIG_NET_POLL_CONTROLLER
1915static void cpsw_ndo_poll_controller(struct net_device *ndev)
1916{
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +03001917 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001918
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +03001919 cpsw_intr_disable(cpsw);
1920 cpsw_rx_interrupt(cpsw->irqs_table[0], cpsw);
1921 cpsw_tx_interrupt(cpsw->irqs_table[1], cpsw);
1922 cpsw_intr_enable(cpsw);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001923}
1924#endif
1925
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001926static inline int cpsw_add_vlan_ale_entry(struct cpsw_priv *priv,
1927 unsigned short vid)
1928{
1929 int ret;
Mugunthan V N9f6bd8f2015-01-15 14:59:28 +05301930 int unreg_mcast_mask = 0;
1931 u32 port_mask;
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001932 struct cpsw_common *cpsw = priv->cpsw;
Lennart Sorensen1e5c4bc2014-10-31 13:38:52 -04001933
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001934 if (cpsw->data.dual_emac) {
Mugunthan V N9f6bd8f2015-01-15 14:59:28 +05301935 port_mask = (1 << (priv->emac_port + 1)) | ALE_PORT_HOST;
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001936
Mugunthan V N9f6bd8f2015-01-15 14:59:28 +05301937 if (priv->ndev->flags & IFF_ALLMULTI)
1938 unreg_mcast_mask = port_mask;
1939 } else {
1940 port_mask = ALE_ALL_PORTS;
1941
1942 if (priv->ndev->flags & IFF_ALLMULTI)
1943 unreg_mcast_mask = ALE_ALL_PORTS;
1944 else
1945 unreg_mcast_mask = ALE_PORT_1 | ALE_PORT_2;
1946 }
1947
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001948 ret = cpsw_ale_add_vlan(cpsw->ale, vid, port_mask, 0, port_mask,
Grygorii Strashko61f1cef2016-04-07 15:16:43 +03001949 unreg_mcast_mask);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001950 if (ret != 0)
1951 return ret;
1952
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001953 ret = cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr,
Grygorii Strashko71a2cbb2016-04-07 15:16:44 +03001954 HOST_PORT_NUM, ALE_VLAN, vid);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001955 if (ret != 0)
1956 goto clean_vid;
1957
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001958 ret = cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
Mugunthan V N9f6bd8f2015-01-15 14:59:28 +05301959 port_mask, ALE_VLAN, vid, 0);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001960 if (ret != 0)
1961 goto clean_vlan_ucast;
1962 return 0;
1963
1964clean_vlan_ucast:
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001965 cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr,
Grygorii Strashko71a2cbb2016-04-07 15:16:44 +03001966 HOST_PORT_NUM, ALE_VLAN, vid);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001967clean_vid:
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001968 cpsw_ale_del_vlan(cpsw->ale, vid, 0);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001969 return ret;
1970}
1971
1972static int cpsw_ndo_vlan_rx_add_vid(struct net_device *ndev,
Patrick McHardy80d5c362013-04-19 02:04:28 +00001973 __be16 proto, u16 vid)
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001974{
1975 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03001976 struct cpsw_common *cpsw = priv->cpsw;
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001977 int ret;
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001978
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001979 if (vid == cpsw->data.default_vlan)
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001980 return 0;
1981
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001982 ret = pm_runtime_get_sync(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001983 if (ret < 0) {
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001984 pm_runtime_put_noidle(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001985 return ret;
1986 }
1987
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001988 if (cpsw->data.dual_emac) {
Mugunthan V N02a54162015-01-22 15:19:22 +05301989 /* In dual EMAC, reserved VLAN id should not be used for
1990 * creating VLAN interfaces as this can break the dual
1991 * EMAC port separation
1992 */
1993 int i;
1994
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001995 for (i = 0; i < cpsw->data.slaves; i++) {
1996 if (vid == cpsw->slaves[i].port_vlan)
Mugunthan V N02a54162015-01-22 15:19:22 +05301997 return -EINVAL;
1998 }
1999 }
2000
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00002001 dev_info(priv->dev, "Adding vlanid %d to vlan filter\n", vid);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03002002 ret = cpsw_add_vlan_ale_entry(priv, vid);
2003
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002004 pm_runtime_put(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03002005 return ret;
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00002006}
2007
2008static int cpsw_ndo_vlan_rx_kill_vid(struct net_device *ndev,
Patrick McHardy80d5c362013-04-19 02:04:28 +00002009 __be16 proto, u16 vid)
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00002010{
2011 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03002012 struct cpsw_common *cpsw = priv->cpsw;
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00002013 int ret;
2014
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002015 if (vid == cpsw->data.default_vlan)
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00002016 return 0;
2017
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002018 ret = pm_runtime_get_sync(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03002019 if (ret < 0) {
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002020 pm_runtime_put_noidle(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03002021 return ret;
2022 }
2023
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002024 if (cpsw->data.dual_emac) {
Mugunthan V N02a54162015-01-22 15:19:22 +05302025 int i;
2026
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002027 for (i = 0; i < cpsw->data.slaves; i++) {
2028 if (vid == cpsw->slaves[i].port_vlan)
Mugunthan V N02a54162015-01-22 15:19:22 +05302029 return -EINVAL;
2030 }
2031 }
2032
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00002033 dev_info(priv->dev, "removing vlanid %d from vlan filter\n", vid);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002034 ret = cpsw_ale_del_vlan(cpsw->ale, vid, 0);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00002035 if (ret != 0)
2036 return ret;
2037
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002038 ret = cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr,
Grygorii Strashko61f1cef2016-04-07 15:16:43 +03002039 HOST_PORT_NUM, ALE_VLAN, vid);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00002040 if (ret != 0)
2041 return ret;
2042
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002043 ret = cpsw_ale_del_mcast(cpsw->ale, priv->ndev->broadcast,
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03002044 0, ALE_VLAN, vid);
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002045 pm_runtime_put(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03002046 return ret;
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00002047}
2048
Ivan Khoronzhuk83fcad02016-11-29 17:00:49 +02002049static int cpsw_ndo_set_tx_maxrate(struct net_device *ndev, int queue, u32 rate)
2050{
2051 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk83fcad02016-11-29 17:00:49 +02002052 struct cpsw_common *cpsw = priv->cpsw;
Ivan Khoronzhuk52986a22016-12-10 14:23:50 +02002053 struct cpsw_slave *slave;
Ivan Khoronzhuk32b78d82016-12-10 14:23:48 +02002054 u32 min_rate;
Ivan Khoronzhuk83fcad02016-11-29 17:00:49 +02002055 u32 ch_rate;
Ivan Khoronzhuk52986a22016-12-10 14:23:50 +02002056 int i, ret;
Ivan Khoronzhuk83fcad02016-11-29 17:00:49 +02002057
2058 ch_rate = netdev_get_tx_queue(ndev, queue)->tx_maxrate;
2059 if (ch_rate == rate)
2060 return 0;
2061
Ivan Khoronzhuk32b78d82016-12-10 14:23:48 +02002062 ch_rate = rate * 1000;
2063 min_rate = cpdma_chan_get_min_rate(cpsw->dma);
2064 if ((ch_rate < min_rate && ch_rate)) {
2065 dev_err(priv->dev, "The channel rate cannot be less than %dMbps",
2066 min_rate);
Ivan Khoronzhuk83fcad02016-11-29 17:00:49 +02002067 return -EINVAL;
2068 }
2069
Ivan Khoronzhuk0be01b82016-12-10 14:23:49 +02002070 if (rate > cpsw->speed) {
Ivan Khoronzhuk32b78d82016-12-10 14:23:48 +02002071 dev_err(priv->dev, "The channel rate cannot be more than 2Gbps");
Ivan Khoronzhuk83fcad02016-11-29 17:00:49 +02002072 return -EINVAL;
2073 }
2074
2075 ret = pm_runtime_get_sync(cpsw->dev);
2076 if (ret < 0) {
2077 pm_runtime_put_noidle(cpsw->dev);
2078 return ret;
2079 }
2080
Ivan Khoronzhuk32b78d82016-12-10 14:23:48 +02002081 ret = cpdma_chan_set_rate(cpsw->txv[queue].ch, ch_rate);
Ivan Khoronzhuk83fcad02016-11-29 17:00:49 +02002082 pm_runtime_put(cpsw->dev);
Ivan Khoronzhuk32b78d82016-12-10 14:23:48 +02002083
2084 if (ret)
2085 return ret;
2086
Ivan Khoronzhuk52986a22016-12-10 14:23:50 +02002087 /* update rates for slaves tx queues */
2088 for (i = 0; i < cpsw->data.slaves; i++) {
2089 slave = &cpsw->slaves[i];
2090 if (!slave->ndev)
2091 continue;
2092
2093 netdev_get_tx_queue(slave->ndev, queue)->tx_maxrate = rate;
2094 }
2095
Ivan Khoronzhuk32b78d82016-12-10 14:23:48 +02002096 cpsw_split_res(ndev);
Ivan Khoronzhuk83fcad02016-11-29 17:00:49 +02002097 return ret;
2098}
2099
Mugunthan V Ndf828592012-03-18 20:17:54 +00002100static const struct net_device_ops cpsw_netdev_ops = {
2101 .ndo_open = cpsw_ndo_open,
2102 .ndo_stop = cpsw_ndo_stop,
2103 .ndo_start_xmit = cpsw_ndo_start_xmit,
Mugunthan V Ndcfd8d52013-07-25 23:44:01 +05302104 .ndo_set_mac_address = cpsw_ndo_set_mac_address,
Richard Cochran2e5b38a2012-10-29 08:45:20 +00002105 .ndo_do_ioctl = cpsw_ndo_ioctl,
Mugunthan V Ndf828592012-03-18 20:17:54 +00002106 .ndo_validate_addr = eth_validate_addr,
Mugunthan V Ndf828592012-03-18 20:17:54 +00002107 .ndo_tx_timeout = cpsw_ndo_tx_timeout,
Mugunthan V N5c50a852012-10-29 08:45:11 +00002108 .ndo_set_rx_mode = cpsw_ndo_set_rx_mode,
Ivan Khoronzhuk83fcad02016-11-29 17:00:49 +02002109 .ndo_set_tx_maxrate = cpsw_ndo_set_tx_maxrate,
Mugunthan V Ndf828592012-03-18 20:17:54 +00002110#ifdef CONFIG_NET_POLL_CONTROLLER
2111 .ndo_poll_controller = cpsw_ndo_poll_controller,
2112#endif
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00002113 .ndo_vlan_rx_add_vid = cpsw_ndo_vlan_rx_add_vid,
2114 .ndo_vlan_rx_kill_vid = cpsw_ndo_vlan_rx_kill_vid,
Mugunthan V Ndf828592012-03-18 20:17:54 +00002115};
2116
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05302117static int cpsw_get_regs_len(struct net_device *ndev)
2118{
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002119 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05302120
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002121 return cpsw->data.ale_entries * ALE_ENTRY_WORDS * sizeof(u32);
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05302122}
2123
2124static void cpsw_get_regs(struct net_device *ndev,
2125 struct ethtool_regs *regs, void *p)
2126{
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05302127 u32 *reg = p;
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002128 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05302129
2130 /* update CPSW IP version */
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002131 regs->version = cpsw->version;
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05302132
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002133 cpsw_ale_dump(cpsw->ale, reg);
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05302134}
2135
Mugunthan V Ndf828592012-03-18 20:17:54 +00002136static void cpsw_get_drvinfo(struct net_device *ndev,
2137 struct ethtool_drvinfo *info)
2138{
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03002139 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002140 struct platform_device *pdev = to_platform_device(cpsw->dev);
Jiri Pirko7826d432013-01-06 00:44:26 +00002141
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05302142 strlcpy(info->driver, "cpsw", sizeof(info->driver));
Jiri Pirko7826d432013-01-06 00:44:26 +00002143 strlcpy(info->version, "1.0", sizeof(info->version));
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002144 strlcpy(info->bus_info, pdev->name, sizeof(info->bus_info));
Mugunthan V Ndf828592012-03-18 20:17:54 +00002145}
2146
2147static u32 cpsw_get_msglevel(struct net_device *ndev)
2148{
2149 struct cpsw_priv *priv = netdev_priv(ndev);
2150 return priv->msg_enable;
2151}
2152
2153static void cpsw_set_msglevel(struct net_device *ndev, u32 value)
2154{
2155 struct cpsw_priv *priv = netdev_priv(ndev);
2156 priv->msg_enable = value;
2157}
2158
Grygorii Strashkoc8395d42016-12-06 18:00:34 -06002159#if IS_ENABLED(CONFIG_TI_CPTS)
Richard Cochran2e5b38a2012-10-29 08:45:20 +00002160static int cpsw_get_ts_info(struct net_device *ndev,
2161 struct ethtool_ts_info *info)
2162{
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002163 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Richard Cochran2e5b38a2012-10-29 08:45:20 +00002164
2165 info->so_timestamping =
2166 SOF_TIMESTAMPING_TX_HARDWARE |
2167 SOF_TIMESTAMPING_TX_SOFTWARE |
2168 SOF_TIMESTAMPING_RX_HARDWARE |
2169 SOF_TIMESTAMPING_RX_SOFTWARE |
2170 SOF_TIMESTAMPING_SOFTWARE |
2171 SOF_TIMESTAMPING_RAW_HARDWARE;
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002172 info->phc_index = cpsw->cpts->phc_index;
Richard Cochran2e5b38a2012-10-29 08:45:20 +00002173 info->tx_types =
2174 (1 << HWTSTAMP_TX_OFF) |
2175 (1 << HWTSTAMP_TX_ON);
2176 info->rx_filters =
2177 (1 << HWTSTAMP_FILTER_NONE) |
Grygorii Strashkoe9523a52017-06-08 13:51:31 -05002178 (1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) |
Richard Cochran2e5b38a2012-10-29 08:45:20 +00002179 (1 << HWTSTAMP_FILTER_PTP_V2_EVENT);
Grygorii Strashkoc8395d42016-12-06 18:00:34 -06002180 return 0;
2181}
Richard Cochran2e5b38a2012-10-29 08:45:20 +00002182#else
Grygorii Strashkoc8395d42016-12-06 18:00:34 -06002183static int cpsw_get_ts_info(struct net_device *ndev,
2184 struct ethtool_ts_info *info)
2185{
Richard Cochran2e5b38a2012-10-29 08:45:20 +00002186 info->so_timestamping =
2187 SOF_TIMESTAMPING_TX_SOFTWARE |
2188 SOF_TIMESTAMPING_RX_SOFTWARE |
2189 SOF_TIMESTAMPING_SOFTWARE;
2190 info->phc_index = -1;
2191 info->tx_types = 0;
2192 info->rx_filters = 0;
Richard Cochran2e5b38a2012-10-29 08:45:20 +00002193 return 0;
2194}
Grygorii Strashkoc8395d42016-12-06 18:00:34 -06002195#endif
Richard Cochran2e5b38a2012-10-29 08:45:20 +00002196
Philippe Reynes24798762016-10-08 17:46:15 +02002197static int cpsw_get_link_ksettings(struct net_device *ndev,
2198 struct ethtool_link_ksettings *ecmd)
Mugunthan V Nd3bb9c52013-03-11 23:16:36 +00002199{
2200 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002201 struct cpsw_common *cpsw = priv->cpsw;
2202 int slave_no = cpsw_slave_index(cpsw, priv);
Mugunthan V Nd3bb9c52013-03-11 23:16:36 +00002203
yuval.shaia@oracle.com55141742017-06-13 10:09:46 +03002204 if (!cpsw->slaves[slave_no].phy)
Mugunthan V Nd3bb9c52013-03-11 23:16:36 +00002205 return -EOPNOTSUPP;
yuval.shaia@oracle.com55141742017-06-13 10:09:46 +03002206
2207 phy_ethtool_ksettings_get(cpsw->slaves[slave_no].phy, ecmd);
2208 return 0;
Mugunthan V Nd3bb9c52013-03-11 23:16:36 +00002209}
2210
Philippe Reynes24798762016-10-08 17:46:15 +02002211static int cpsw_set_link_ksettings(struct net_device *ndev,
2212 const struct ethtool_link_ksettings *ecmd)
Mugunthan V Nd3bb9c52013-03-11 23:16:36 +00002213{
2214 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002215 struct cpsw_common *cpsw = priv->cpsw;
2216 int slave_no = cpsw_slave_index(cpsw, priv);
Mugunthan V Nd3bb9c52013-03-11 23:16:36 +00002217
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002218 if (cpsw->slaves[slave_no].phy)
Philippe Reynes24798762016-10-08 17:46:15 +02002219 return phy_ethtool_ksettings_set(cpsw->slaves[slave_no].phy,
2220 ecmd);
Mugunthan V Nd3bb9c52013-03-11 23:16:36 +00002221 else
2222 return -EOPNOTSUPP;
2223}
2224
Matus Ujhelyid8a64422013-08-20 07:59:38 +02002225static void cpsw_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2226{
2227 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002228 struct cpsw_common *cpsw = priv->cpsw;
2229 int slave_no = cpsw_slave_index(cpsw, priv);
Matus Ujhelyid8a64422013-08-20 07:59:38 +02002230
2231 wol->supported = 0;
2232 wol->wolopts = 0;
2233
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002234 if (cpsw->slaves[slave_no].phy)
2235 phy_ethtool_get_wol(cpsw->slaves[slave_no].phy, wol);
Matus Ujhelyid8a64422013-08-20 07:59:38 +02002236}
2237
2238static int cpsw_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2239{
2240 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002241 struct cpsw_common *cpsw = priv->cpsw;
2242 int slave_no = cpsw_slave_index(cpsw, priv);
Matus Ujhelyid8a64422013-08-20 07:59:38 +02002243
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002244 if (cpsw->slaves[slave_no].phy)
2245 return phy_ethtool_set_wol(cpsw->slaves[slave_no].phy, wol);
Matus Ujhelyid8a64422013-08-20 07:59:38 +02002246 else
2247 return -EOPNOTSUPP;
2248}
2249
Mugunthan V N1923d6e2014-09-08 22:54:02 +05302250static void cpsw_get_pauseparam(struct net_device *ndev,
2251 struct ethtool_pauseparam *pause)
2252{
2253 struct cpsw_priv *priv = netdev_priv(ndev);
2254
2255 pause->autoneg = AUTONEG_DISABLE;
2256 pause->rx_pause = priv->rx_pause ? true : false;
2257 pause->tx_pause = priv->tx_pause ? true : false;
2258}
2259
2260static int cpsw_set_pauseparam(struct net_device *ndev,
2261 struct ethtool_pauseparam *pause)
2262{
2263 struct cpsw_priv *priv = netdev_priv(ndev);
2264 bool link;
2265
2266 priv->rx_pause = pause->rx_pause ? true : false;
2267 priv->tx_pause = pause->tx_pause ? true : false;
2268
2269 for_each_slave(priv, _cpsw_adjust_link, priv, &link);
Mugunthan V N1923d6e2014-09-08 22:54:02 +05302270 return 0;
2271}
2272
Grygorii Strashko7898b1d2016-06-24 21:23:44 +03002273static int cpsw_ethtool_op_begin(struct net_device *ndev)
2274{
2275 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03002276 struct cpsw_common *cpsw = priv->cpsw;
Grygorii Strashko7898b1d2016-06-24 21:23:44 +03002277 int ret;
2278
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002279 ret = pm_runtime_get_sync(cpsw->dev);
Grygorii Strashko7898b1d2016-06-24 21:23:44 +03002280 if (ret < 0) {
2281 cpsw_err(priv, drv, "ethtool begin failed %d\n", ret);
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002282 pm_runtime_put_noidle(cpsw->dev);
Grygorii Strashko7898b1d2016-06-24 21:23:44 +03002283 }
2284
2285 return ret;
2286}
2287
2288static void cpsw_ethtool_op_complete(struct net_device *ndev)
2289{
2290 struct cpsw_priv *priv = netdev_priv(ndev);
2291 int ret;
2292
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002293 ret = pm_runtime_put(priv->cpsw->dev);
Grygorii Strashko7898b1d2016-06-24 21:23:44 +03002294 if (ret < 0)
2295 cpsw_err(priv, drv, "ethtool complete failed %d\n", ret);
2296}
2297
Ivan Khoronzhukce52c742016-08-22 21:18:28 +03002298static void cpsw_get_channels(struct net_device *ndev,
2299 struct ethtool_channels *ch)
2300{
2301 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
2302
2303 ch->max_combined = 0;
2304 ch->max_rx = CPSW_MAX_QUEUES;
2305 ch->max_tx = CPSW_MAX_QUEUES;
2306 ch->max_other = 0;
2307 ch->other_count = 0;
2308 ch->rx_count = cpsw->rx_ch_num;
2309 ch->tx_count = cpsw->tx_ch_num;
2310 ch->combined_count = 0;
2311}
2312
2313static int cpsw_check_ch_settings(struct cpsw_common *cpsw,
2314 struct ethtool_channels *ch)
2315{
2316 if (ch->combined_count)
2317 return -EINVAL;
2318
2319 /* verify we have at least one channel in each direction */
2320 if (!ch->rx_count || !ch->tx_count)
2321 return -EINVAL;
2322
2323 if (ch->rx_count > cpsw->data.channels ||
2324 ch->tx_count > cpsw->data.channels)
2325 return -EINVAL;
2326
2327 return 0;
2328}
2329
2330static int cpsw_update_channels_res(struct cpsw_priv *priv, int ch_num, int rx)
2331{
Ivan Khoronzhukce52c742016-08-22 21:18:28 +03002332 struct cpsw_common *cpsw = priv->cpsw;
2333 void (*handler)(void *, int, int);
Ivan Khoronzhuk83fcad02016-11-29 17:00:49 +02002334 struct netdev_queue *queue;
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02002335 struct cpsw_vector *vec;
Ivan Khoronzhukce52c742016-08-22 21:18:28 +03002336 int ret, *ch;
2337
2338 if (rx) {
2339 ch = &cpsw->rx_ch_num;
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02002340 vec = cpsw->rxv;
Ivan Khoronzhukce52c742016-08-22 21:18:28 +03002341 handler = cpsw_rx_handler;
Ivan Khoronzhukce52c742016-08-22 21:18:28 +03002342 } else {
2343 ch = &cpsw->tx_ch_num;
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02002344 vec = cpsw->txv;
Ivan Khoronzhukce52c742016-08-22 21:18:28 +03002345 handler = cpsw_tx_handler;
Ivan Khoronzhukce52c742016-08-22 21:18:28 +03002346 }
2347
2348 while (*ch < ch_num) {
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02002349 vec[*ch].ch = cpdma_chan_create(cpsw->dma, *ch, handler, rx);
Ivan Khoronzhuk83fcad02016-11-29 17:00:49 +02002350 queue = netdev_get_tx_queue(priv->ndev, *ch);
2351 queue->tx_maxrate = 0;
Ivan Khoronzhukce52c742016-08-22 21:18:28 +03002352
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02002353 if (IS_ERR(vec[*ch].ch))
2354 return PTR_ERR(vec[*ch].ch);
Ivan Khoronzhukce52c742016-08-22 21:18:28 +03002355
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02002356 if (!vec[*ch].ch)
Ivan Khoronzhukce52c742016-08-22 21:18:28 +03002357 return -EINVAL;
2358
2359 cpsw_info(priv, ifup, "created new %d %s channel\n", *ch,
2360 (rx ? "rx" : "tx"));
2361 (*ch)++;
2362 }
2363
2364 while (*ch > ch_num) {
2365 (*ch)--;
2366
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02002367 ret = cpdma_chan_destroy(vec[*ch].ch);
Ivan Khoronzhukce52c742016-08-22 21:18:28 +03002368 if (ret)
2369 return ret;
2370
2371 cpsw_info(priv, ifup, "destroyed %d %s channel\n", *ch,
2372 (rx ? "rx" : "tx"));
2373 }
2374
2375 return 0;
2376}
2377
2378static int cpsw_update_channels(struct cpsw_priv *priv,
2379 struct ethtool_channels *ch)
2380{
2381 int ret;
2382
2383 ret = cpsw_update_channels_res(priv, ch->rx_count, 1);
2384 if (ret)
2385 return ret;
2386
2387 ret = cpsw_update_channels_res(priv, ch->tx_count, 0);
2388 if (ret)
2389 return ret;
2390
2391 return 0;
2392}
2393
Ivan Khoronzhuk022d7ad2017-01-19 18:58:27 +02002394static void cpsw_suspend_data_pass(struct net_device *ndev)
Ivan Khoronzhukce52c742016-08-22 21:18:28 +03002395{
Ivan Khoronzhuk022d7ad2017-01-19 18:58:27 +02002396 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Ivan Khoronzhukce52c742016-08-22 21:18:28 +03002397 struct cpsw_slave *slave;
Ivan Khoronzhuk022d7ad2017-01-19 18:58:27 +02002398 int i;
Ivan Khoronzhukce52c742016-08-22 21:18:28 +03002399
2400 /* Disable NAPI scheduling */
2401 cpsw_intr_disable(cpsw);
2402
2403 /* Stop all transmit queues for every network device.
2404 * Disable re-using rx descriptors with dormant_on.
2405 */
2406 for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++) {
2407 if (!(slave->ndev && netif_running(slave->ndev)))
2408 continue;
2409
2410 netif_tx_stop_all_queues(slave->ndev);
2411 netif_dormant_on(slave->ndev);
2412 }
2413
2414 /* Handle rest of tx packets and stop cpdma channels */
2415 cpdma_ctlr_stop(cpsw->dma);
Ivan Khoronzhuk022d7ad2017-01-19 18:58:27 +02002416}
2417
2418static int cpsw_resume_data_pass(struct net_device *ndev)
2419{
2420 struct cpsw_priv *priv = netdev_priv(ndev);
2421 struct cpsw_common *cpsw = priv->cpsw;
2422 struct cpsw_slave *slave;
2423 int i, ret;
2424
2425 /* Allow rx packets handling */
2426 for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++)
2427 if (slave->ndev && netif_running(slave->ndev))
2428 netif_dormant_off(slave->ndev);
2429
2430 /* After this receive is started */
Ivan Khoronzhukd5bc1612017-02-14 16:02:36 +02002431 if (cpsw->usage_count) {
Ivan Khoronzhuk022d7ad2017-01-19 18:58:27 +02002432 ret = cpsw_fill_rx_channels(priv);
2433 if (ret)
2434 return ret;
2435
2436 cpdma_ctlr_start(cpsw->dma);
2437 cpsw_intr_enable(cpsw);
2438 }
2439
2440 /* Resume transmit for every affected interface */
2441 for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++)
2442 if (slave->ndev && netif_running(slave->ndev))
2443 netif_tx_start_all_queues(slave->ndev);
2444
2445 return 0;
2446}
2447
2448static int cpsw_set_channels(struct net_device *ndev,
2449 struct ethtool_channels *chs)
2450{
2451 struct cpsw_priv *priv = netdev_priv(ndev);
2452 struct cpsw_common *cpsw = priv->cpsw;
2453 struct cpsw_slave *slave;
2454 int i, ret;
2455
2456 ret = cpsw_check_ch_settings(cpsw, chs);
2457 if (ret < 0)
2458 return ret;
2459
2460 cpsw_suspend_data_pass(ndev);
Ivan Khoronzhukce52c742016-08-22 21:18:28 +03002461 ret = cpsw_update_channels(priv, chs);
2462 if (ret)
2463 goto err;
2464
2465 for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++) {
2466 if (!(slave->ndev && netif_running(slave->ndev)))
2467 continue;
2468
2469 /* Inform stack about new count of queues */
2470 ret = netif_set_real_num_tx_queues(slave->ndev,
2471 cpsw->tx_ch_num);
2472 if (ret) {
2473 dev_err(priv->dev, "cannot set real number of tx queues\n");
2474 goto err;
2475 }
2476
2477 ret = netif_set_real_num_rx_queues(slave->ndev,
2478 cpsw->rx_ch_num);
2479 if (ret) {
2480 dev_err(priv->dev, "cannot set real number of rx queues\n");
2481 goto err;
2482 }
Ivan Khoronzhukce52c742016-08-22 21:18:28 +03002483 }
2484
Ivan Khoronzhukd5bc1612017-02-14 16:02:36 +02002485 if (cpsw->usage_count)
Ivan Khoronzhuk32b78d82016-12-10 14:23:48 +02002486 cpsw_split_res(ndev);
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02002487
Ivan Khoronzhuk022d7ad2017-01-19 18:58:27 +02002488 ret = cpsw_resume_data_pass(ndev);
2489 if (!ret)
2490 return 0;
Ivan Khoronzhukce52c742016-08-22 21:18:28 +03002491err:
2492 dev_err(priv->dev, "cannot update channels number, closing device\n");
2493 dev_close(ndev);
2494 return ret;
2495}
2496
Yegor Yefremova0909942016-11-28 09:41:33 +01002497static int cpsw_get_eee(struct net_device *ndev, struct ethtool_eee *edata)
2498{
2499 struct cpsw_priv *priv = netdev_priv(ndev);
2500 struct cpsw_common *cpsw = priv->cpsw;
2501 int slave_no = cpsw_slave_index(cpsw, priv);
2502
2503 if (cpsw->slaves[slave_no].phy)
2504 return phy_ethtool_get_eee(cpsw->slaves[slave_no].phy, edata);
2505 else
2506 return -EOPNOTSUPP;
2507}
2508
2509static int cpsw_set_eee(struct net_device *ndev, struct ethtool_eee *edata)
2510{
2511 struct cpsw_priv *priv = netdev_priv(ndev);
2512 struct cpsw_common *cpsw = priv->cpsw;
2513 int slave_no = cpsw_slave_index(cpsw, priv);
2514
2515 if (cpsw->slaves[slave_no].phy)
2516 return phy_ethtool_set_eee(cpsw->slaves[slave_no].phy, edata);
2517 else
2518 return -EOPNOTSUPP;
2519}
2520
Yegor Yefremov6bb10c22016-11-28 10:47:52 +01002521static int cpsw_nway_reset(struct net_device *ndev)
2522{
2523 struct cpsw_priv *priv = netdev_priv(ndev);
2524 struct cpsw_common *cpsw = priv->cpsw;
2525 int slave_no = cpsw_slave_index(cpsw, priv);
2526
2527 if (cpsw->slaves[slave_no].phy)
2528 return genphy_restart_aneg(cpsw->slaves[slave_no].phy);
2529 else
2530 return -EOPNOTSUPP;
2531}
2532
Grygorii Strashkobe034fc2017-01-06 14:07:34 -06002533static void cpsw_get_ringparam(struct net_device *ndev,
2534 struct ethtool_ringparam *ering)
2535{
2536 struct cpsw_priv *priv = netdev_priv(ndev);
2537 struct cpsw_common *cpsw = priv->cpsw;
2538
2539 /* not supported */
2540 ering->tx_max_pending = 0;
2541 ering->tx_pending = cpdma_get_num_tx_descs(cpsw->dma);
Ivan Khoronzhukf89d21b2017-01-08 22:12:27 +02002542 ering->rx_max_pending = descs_pool_size - CPSW_MAX_QUEUES;
Grygorii Strashkobe034fc2017-01-06 14:07:34 -06002543 ering->rx_pending = cpdma_get_num_rx_descs(cpsw->dma);
2544}
2545
2546static int cpsw_set_ringparam(struct net_device *ndev,
2547 struct ethtool_ringparam *ering)
2548{
2549 struct cpsw_priv *priv = netdev_priv(ndev);
2550 struct cpsw_common *cpsw = priv->cpsw;
Ivan Khoronzhuk022d7ad2017-01-19 18:58:27 +02002551 int ret;
Grygorii Strashkobe034fc2017-01-06 14:07:34 -06002552
2553 /* ignore ering->tx_pending - only rx_pending adjustment is supported */
2554
2555 if (ering->rx_mini_pending || ering->rx_jumbo_pending ||
Ivan Khoronzhukf89d21b2017-01-08 22:12:27 +02002556 ering->rx_pending < CPSW_MAX_QUEUES ||
2557 ering->rx_pending > (descs_pool_size - CPSW_MAX_QUEUES))
Grygorii Strashkobe034fc2017-01-06 14:07:34 -06002558 return -EINVAL;
2559
2560 if (ering->rx_pending == cpdma_get_num_rx_descs(cpsw->dma))
2561 return 0;
2562
Ivan Khoronzhuk022d7ad2017-01-19 18:58:27 +02002563 cpsw_suspend_data_pass(ndev);
Grygorii Strashkobe034fc2017-01-06 14:07:34 -06002564
2565 cpdma_set_num_rx_descs(cpsw->dma, ering->rx_pending);
2566
Ivan Khoronzhukd5bc1612017-02-14 16:02:36 +02002567 if (cpsw->usage_count)
Grygorii Strashkobe034fc2017-01-06 14:07:34 -06002568 cpdma_chan_split_pool(cpsw->dma);
2569
Ivan Khoronzhuk022d7ad2017-01-19 18:58:27 +02002570 ret = cpsw_resume_data_pass(ndev);
2571 if (!ret)
2572 return 0;
Grygorii Strashkobe034fc2017-01-06 14:07:34 -06002573
Ivan Khoronzhuk022d7ad2017-01-19 18:58:27 +02002574 dev_err(&ndev->dev, "cannot set ring params, closing device\n");
Grygorii Strashkobe034fc2017-01-06 14:07:34 -06002575 dev_close(ndev);
2576 return ret;
2577}
2578
Mugunthan V Ndf828592012-03-18 20:17:54 +00002579static const struct ethtool_ops cpsw_ethtool_ops = {
2580 .get_drvinfo = cpsw_get_drvinfo,
2581 .get_msglevel = cpsw_get_msglevel,
2582 .set_msglevel = cpsw_set_msglevel,
2583 .get_link = ethtool_op_get_link,
Richard Cochran2e5b38a2012-10-29 08:45:20 +00002584 .get_ts_info = cpsw_get_ts_info,
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00002585 .get_coalesce = cpsw_get_coalesce,
2586 .set_coalesce = cpsw_set_coalesce,
Mugunthan V Nd9718542013-07-23 15:38:17 +05302587 .get_sset_count = cpsw_get_sset_count,
2588 .get_strings = cpsw_get_strings,
2589 .get_ethtool_stats = cpsw_get_ethtool_stats,
Mugunthan V N1923d6e2014-09-08 22:54:02 +05302590 .get_pauseparam = cpsw_get_pauseparam,
2591 .set_pauseparam = cpsw_set_pauseparam,
Matus Ujhelyid8a64422013-08-20 07:59:38 +02002592 .get_wol = cpsw_get_wol,
2593 .set_wol = cpsw_set_wol,
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05302594 .get_regs_len = cpsw_get_regs_len,
2595 .get_regs = cpsw_get_regs,
Grygorii Strashko7898b1d2016-06-24 21:23:44 +03002596 .begin = cpsw_ethtool_op_begin,
2597 .complete = cpsw_ethtool_op_complete,
Ivan Khoronzhukce52c742016-08-22 21:18:28 +03002598 .get_channels = cpsw_get_channels,
2599 .set_channels = cpsw_set_channels,
Philippe Reynes24798762016-10-08 17:46:15 +02002600 .get_link_ksettings = cpsw_get_link_ksettings,
2601 .set_link_ksettings = cpsw_set_link_ksettings,
Yegor Yefremova0909942016-11-28 09:41:33 +01002602 .get_eee = cpsw_get_eee,
2603 .set_eee = cpsw_set_eee,
Yegor Yefremov6bb10c22016-11-28 10:47:52 +01002604 .nway_reset = cpsw_nway_reset,
Grygorii Strashkobe034fc2017-01-06 14:07:34 -06002605 .get_ringparam = cpsw_get_ringparam,
2606 .set_ringparam = cpsw_set_ringparam,
Mugunthan V Ndf828592012-03-18 20:17:54 +00002607};
2608
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002609static void cpsw_slave_init(struct cpsw_slave *slave, struct cpsw_common *cpsw,
Richard Cochran549985e2012-11-14 09:07:56 +00002610 u32 slave_reg_ofs, u32 sliver_reg_ofs)
Mugunthan V Ndf828592012-03-18 20:17:54 +00002611{
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03002612 void __iomem *regs = cpsw->regs;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002613 int slave_num = slave->slave_num;
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002614 struct cpsw_slave_data *data = cpsw->data.slave_data + slave_num;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002615
2616 slave->data = data;
Richard Cochran549985e2012-11-14 09:07:56 +00002617 slave->regs = regs + slave_reg_ofs;
2618 slave->sliver = regs + sliver_reg_ofs;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002619 slave->port_vlan = data->dual_emac_res_vlan;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002620}
2621
David Rivshin552165b2016-04-27 21:25:25 -04002622static int cpsw_probe_dt(struct cpsw_platform_data *data,
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002623 struct platform_device *pdev)
2624{
2625 struct device_node *node = pdev->dev.of_node;
2626 struct device_node *slave_node;
2627 int i = 0, ret;
2628 u32 prop;
2629
2630 if (!node)
2631 return -EINVAL;
2632
2633 if (of_property_read_u32(node, "slaves", &prop)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302634 dev_err(&pdev->dev, "Missing slaves property in the DT.\n");
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002635 return -EINVAL;
2636 }
2637 data->slaves = prop;
2638
Mugunthan V Ne86ac132013-03-11 23:16:35 +00002639 if (of_property_read_u32(node, "active_slave", &prop)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302640 dev_err(&pdev->dev, "Missing active_slave property in the DT.\n");
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302641 return -EINVAL;
Richard Cochran78ca0b22012-10-29 08:45:18 +00002642 }
Mugunthan V Ne86ac132013-03-11 23:16:35 +00002643 data->active_slave = prop;
Richard Cochran78ca0b22012-10-29 08:45:18 +00002644
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302645 data->slave_data = devm_kzalloc(&pdev->dev, data->slaves
2646 * sizeof(struct cpsw_slave_data),
2647 GFP_KERNEL);
Joe Perchesb2adaca2013-02-03 17:43:58 +00002648 if (!data->slave_data)
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302649 return -ENOMEM;
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002650
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002651 if (of_property_read_u32(node, "cpdma_channels", &prop)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302652 dev_err(&pdev->dev, "Missing cpdma_channels property in the DT.\n");
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302653 return -EINVAL;
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002654 }
2655 data->channels = prop;
2656
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002657 if (of_property_read_u32(node, "ale_entries", &prop)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302658 dev_err(&pdev->dev, "Missing ale_entries property in the DT.\n");
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302659 return -EINVAL;
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002660 }
2661 data->ale_entries = prop;
2662
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002663 if (of_property_read_u32(node, "bd_ram_size", &prop)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302664 dev_err(&pdev->dev, "Missing bd_ram_size property in the DT.\n");
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302665 return -EINVAL;
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002666 }
2667 data->bd_ram_size = prop;
2668
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002669 if (of_property_read_u32(node, "mac_control", &prop)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302670 dev_err(&pdev->dev, "Missing mac_control property in the DT.\n");
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302671 return -EINVAL;
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002672 }
2673 data->mac_control = prop;
2674
Markus Pargmann281abd92013-10-04 14:44:40 +02002675 if (of_property_read_bool(node, "dual_emac"))
2676 data->dual_emac = 1;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002677
Vaibhav Hiremath1fb19aa2012-11-14 09:07:55 +00002678 /*
2679 * Populate all the child nodes here...
2680 */
2681 ret = of_platform_populate(node, NULL, NULL, &pdev->dev);
2682 /* We do not want to force this, as in some cases may not have child */
2683 if (ret)
George Cherian88c99ff2014-05-12 10:21:19 +05302684 dev_warn(&pdev->dev, "Doesn't have any child node\n");
Vaibhav Hiremath1fb19aa2012-11-14 09:07:55 +00002685
Ben Hutchings8658aaf2016-06-21 01:16:31 +01002686 for_each_available_child_of_node(node, slave_node) {
Richard Cochran549985e2012-11-14 09:07:56 +00002687 struct cpsw_slave_data *slave_data = data->slave_data + i;
2688 const void *mac_addr = NULL;
Richard Cochran549985e2012-11-14 09:07:56 +00002689 int lenp;
2690 const __be32 *parp;
Richard Cochran549985e2012-11-14 09:07:56 +00002691
Markus Pargmannf468b102013-10-04 14:44:39 +02002692 /* This is no slave child node, continue */
2693 if (strcmp(slave_node->name, "slave"))
2694 continue;
2695
David Rivshin552165b2016-04-27 21:25:25 -04002696 slave_data->phy_node = of_parse_phandle(slave_node,
2697 "phy-handle", 0);
David Rivshinf1eea5c2015-12-16 23:02:10 -05002698 parp = of_get_property(slave_node, "phy_id", &lenp);
David Rivshinae092b52016-04-27 21:38:26 -04002699 if (slave_data->phy_node) {
2700 dev_dbg(&pdev->dev,
Rob Herringf7ce9102017-07-18 16:43:19 -05002701 "slave[%d] using phy-handle=\"%pOF\"\n",
2702 i, slave_data->phy_node);
David Rivshinae092b52016-04-27 21:38:26 -04002703 } else if (of_phy_is_fixed_link(slave_node)) {
David Rivshindfc0a6d2015-12-16 23:02:11 -05002704 /* In the case of a fixed PHY, the DT node associated
2705 * to the PHY is the Ethernet MAC DT node.
2706 */
Markus Brunner1f71e8c2015-11-03 22:09:51 +01002707 ret = of_phy_register_fixed_link(slave_node);
Johan Hovold23a09872016-11-17 17:40:04 +01002708 if (ret) {
2709 if (ret != -EPROBE_DEFER)
2710 dev_err(&pdev->dev, "failed to register fixed-link phy: %d\n", ret);
Markus Brunner1f71e8c2015-11-03 22:09:51 +01002711 return ret;
Johan Hovold23a09872016-11-17 17:40:04 +01002712 }
David Rivshin06cd6d62016-04-27 21:45:45 -04002713 slave_data->phy_node = of_node_get(slave_node);
David Rivshinf1eea5c2015-12-16 23:02:10 -05002714 } else if (parp) {
2715 u32 phyid;
2716 struct device_node *mdio_node;
2717 struct platform_device *mdio;
2718
2719 if (lenp != (sizeof(__be32) * 2)) {
2720 dev_err(&pdev->dev, "Invalid slave[%d] phy_id property\n", i);
2721 goto no_phy_slave;
2722 }
2723 mdio_node = of_find_node_by_phandle(be32_to_cpup(parp));
2724 phyid = be32_to_cpup(parp+1);
2725 mdio = of_find_device_by_node(mdio_node);
2726 of_node_put(mdio_node);
2727 if (!mdio) {
2728 dev_err(&pdev->dev, "Missing mdio platform device\n");
2729 return -EINVAL;
2730 }
2731 snprintf(slave_data->phy_id, sizeof(slave_data->phy_id),
2732 PHY_ID_FMT, mdio->name, phyid);
Johan Hovold86e1d5a2016-11-17 17:39:59 +01002733 put_device(&mdio->dev);
David Rivshinf1eea5c2015-12-16 23:02:10 -05002734 } else {
David Rivshinae092b52016-04-27 21:38:26 -04002735 dev_err(&pdev->dev,
2736 "No slave[%d] phy_id, phy-handle, or fixed-link property\n",
2737 i);
Markus Brunner1f71e8c2015-11-03 22:09:51 +01002738 goto no_phy_slave;
2739 }
Mugunthan V N47276fc2014-10-24 18:51:33 +05302740 slave_data->phy_if = of_get_phy_mode(slave_node);
2741 if (slave_data->phy_if < 0) {
2742 dev_err(&pdev->dev, "Missing or malformed slave[%d] phy-mode property\n",
2743 i);
2744 return slave_data->phy_if;
2745 }
2746
2747no_phy_slave:
Richard Cochran549985e2012-11-14 09:07:56 +00002748 mac_addr = of_get_mac_address(slave_node);
Markus Pargmann0ba517b2014-09-29 08:53:17 +02002749 if (mac_addr) {
Richard Cochran549985e2012-11-14 09:07:56 +00002750 memcpy(slave_data->mac_addr, mac_addr, ETH_ALEN);
Markus Pargmann0ba517b2014-09-29 08:53:17 +02002751 } else {
Mugunthan V Nb6745f62015-09-21 15:56:50 +05302752 ret = ti_cm_get_macid(&pdev->dev, i,
2753 slave_data->mac_addr);
2754 if (ret)
2755 return ret;
Markus Pargmann0ba517b2014-09-29 08:53:17 +02002756 }
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002757 if (data->dual_emac) {
Mugunthan V N91c41662013-04-15 07:31:28 +00002758 if (of_property_read_u32(slave_node, "dual_emac_res_vlan",
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002759 &prop)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302760 dev_err(&pdev->dev, "Missing dual_emac_res_vlan in DT.\n");
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002761 slave_data->dual_emac_res_vlan = i+1;
George Cherian88c99ff2014-05-12 10:21:19 +05302762 dev_err(&pdev->dev, "Using %d as Reserved VLAN for %d slave\n",
2763 slave_data->dual_emac_res_vlan, i);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002764 } else {
2765 slave_data->dual_emac_res_vlan = prop;
2766 }
2767 }
2768
Richard Cochran549985e2012-11-14 09:07:56 +00002769 i++;
Mugunthan V N3a27bfa2013-12-02 12:53:39 +05302770 if (i == data->slaves)
2771 break;
Richard Cochran549985e2012-11-14 09:07:56 +00002772 }
2773
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002774 return 0;
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002775}
2776
Johan Hovolda4e32b02016-11-17 17:40:00 +01002777static void cpsw_remove_dt(struct platform_device *pdev)
2778{
Johan Hovold8cbcc462016-11-17 17:40:01 +01002779 struct net_device *ndev = platform_get_drvdata(pdev);
2780 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
2781 struct cpsw_platform_data *data = &cpsw->data;
2782 struct device_node *node = pdev->dev.of_node;
2783 struct device_node *slave_node;
2784 int i = 0;
2785
2786 for_each_available_child_of_node(node, slave_node) {
2787 struct cpsw_slave_data *slave_data = &data->slave_data[i];
2788
2789 if (strcmp(slave_node->name, "slave"))
2790 continue;
2791
Johan Hovold3f650472016-11-28 19:24:55 +01002792 if (of_phy_is_fixed_link(slave_node))
2793 of_phy_deregister_fixed_link(slave_node);
Johan Hovold8cbcc462016-11-17 17:40:01 +01002794
2795 of_node_put(slave_data->phy_node);
2796
2797 i++;
2798 if (i == data->slaves)
2799 break;
2800 }
2801
Johan Hovolda4e32b02016-11-17 17:40:00 +01002802 of_platform_depopulate(&pdev->dev);
2803}
2804
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002805static int cpsw_probe_dual_emac(struct cpsw_priv *priv)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002806{
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002807 struct cpsw_common *cpsw = priv->cpsw;
2808 struct cpsw_platform_data *data = &cpsw->data;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002809 struct net_device *ndev;
2810 struct cpsw_priv *priv_sl2;
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +03002811 int ret = 0;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002812
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03002813 ndev = alloc_etherdev_mq(sizeof(struct cpsw_priv), CPSW_MAX_QUEUES);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002814 if (!ndev) {
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002815 dev_err(cpsw->dev, "cpsw: error allocating net_device\n");
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002816 return -ENOMEM;
2817 }
2818
2819 priv_sl2 = netdev_priv(ndev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002820 priv_sl2->cpsw = cpsw;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002821 priv_sl2->ndev = ndev;
2822 priv_sl2->dev = &ndev->dev;
2823 priv_sl2->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002824
2825 if (is_valid_ether_addr(data->slave_data[1].mac_addr)) {
2826 memcpy(priv_sl2->mac_addr, data->slave_data[1].mac_addr,
2827 ETH_ALEN);
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002828 dev_info(cpsw->dev, "cpsw: Detected MACID = %pM\n",
2829 priv_sl2->mac_addr);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002830 } else {
2831 random_ether_addr(priv_sl2->mac_addr);
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002832 dev_info(cpsw->dev, "cpsw: Random MACID = %pM\n",
2833 priv_sl2->mac_addr);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002834 }
2835 memcpy(ndev->dev_addr, priv_sl2->mac_addr, ETH_ALEN);
2836
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002837 priv_sl2->emac_port = 1;
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002838 cpsw->slaves[1].ndev = ndev;
Patrick McHardyf6469682013-04-19 02:04:27 +00002839 ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002840
2841 ndev->netdev_ops = &cpsw_netdev_ops;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00002842 ndev->ethtool_ops = &cpsw_ethtool_ops;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002843
2844 /* register the network device */
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002845 SET_NETDEV_DEV(ndev, cpsw->dev);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002846 ret = register_netdev(ndev);
2847 if (ret) {
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002848 dev_err(cpsw->dev, "cpsw: error registering net device\n");
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002849 free_netdev(ndev);
2850 ret = -ENODEV;
2851 }
2852
2853 return ret;
2854}
2855
Mugunthan V N7da11602015-08-12 15:22:53 +05302856#define CPSW_QUIRK_IRQ BIT(0)
2857
Arvind Yadavf5b58942017-08-13 16:43:18 +05302858static const struct platform_device_id cpsw_devtype[] = {
Mugunthan V N7da11602015-08-12 15:22:53 +05302859 {
2860 /* keep it for existing comaptibles */
2861 .name = "cpsw",
2862 .driver_data = CPSW_QUIRK_IRQ,
2863 }, {
2864 .name = "am335x-cpsw",
2865 .driver_data = CPSW_QUIRK_IRQ,
2866 }, {
2867 .name = "am4372-cpsw",
2868 .driver_data = 0,
2869 }, {
2870 .name = "dra7-cpsw",
2871 .driver_data = 0,
2872 }, {
2873 /* sentinel */
2874 }
2875};
2876MODULE_DEVICE_TABLE(platform, cpsw_devtype);
2877
2878enum ti_cpsw_type {
2879 CPSW = 0,
2880 AM335X_CPSW,
2881 AM4372_CPSW,
2882 DRA7_CPSW,
2883};
2884
2885static const struct of_device_id cpsw_of_mtable[] = {
2886 { .compatible = "ti,cpsw", .data = &cpsw_devtype[CPSW], },
2887 { .compatible = "ti,am335x-cpsw", .data = &cpsw_devtype[AM335X_CPSW], },
2888 { .compatible = "ti,am4372-cpsw", .data = &cpsw_devtype[AM4372_CPSW], },
2889 { .compatible = "ti,dra7-cpsw", .data = &cpsw_devtype[DRA7_CPSW], },
2890 { /* sentinel */ },
2891};
2892MODULE_DEVICE_TABLE(of, cpsw_of_mtable);
2893
Bill Pemberton663e12e2012-12-03 09:23:45 -05002894static int cpsw_probe(struct platform_device *pdev)
Mugunthan V Ndf828592012-03-18 20:17:54 +00002895{
Ivan Khoronzhukef4183a2016-08-10 02:22:35 +03002896 struct clk *clk;
Sebastian Siewiord1bd9ac2013-04-24 08:48:23 +00002897 struct cpsw_platform_data *data;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002898 struct net_device *ndev;
2899 struct cpsw_priv *priv;
2900 struct cpdma_params dma_params;
2901 struct cpsw_ale_params ale_params;
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302902 void __iomem *ss_regs;
Grygorii Strashko8a2c9a52016-12-06 18:00:41 -06002903 void __iomem *cpts_regs;
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302904 struct resource *res, *ss_res;
Mugunthan V N7da11602015-08-12 15:22:53 +05302905 const struct of_device_id *of_id;
Mugunthan V N1d147cc2015-09-07 15:16:44 +05302906 struct gpio_descs *mode;
Richard Cochran549985e2012-11-14 09:07:56 +00002907 u32 slave_offset, sliver_offset, slave_size;
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03002908 struct cpsw_common *cpsw;
Felipe Balbi5087b912015-01-16 10:11:11 -06002909 int ret = 0, i;
2910 int irq;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002911
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03002912 cpsw = devm_kzalloc(&pdev->dev, sizeof(struct cpsw_common), GFP_KERNEL);
Johan Hovold3420ea82016-11-17 17:40:03 +01002913 if (!cpsw)
2914 return -ENOMEM;
2915
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002916 cpsw->dev = &pdev->dev;
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03002917
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03002918 ndev = alloc_etherdev_mq(sizeof(struct cpsw_priv), CPSW_MAX_QUEUES);
Mugunthan V Ndf828592012-03-18 20:17:54 +00002919 if (!ndev) {
George Cherian88c99ff2014-05-12 10:21:19 +05302920 dev_err(&pdev->dev, "error allocating net_device\n");
Mugunthan V Ndf828592012-03-18 20:17:54 +00002921 return -ENOMEM;
2922 }
2923
2924 platform_set_drvdata(pdev, ndev);
2925 priv = netdev_priv(ndev);
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03002926 priv->cpsw = cpsw;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002927 priv->ndev = ndev;
2928 priv->dev = &ndev->dev;
2929 priv->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002930 cpsw->rx_packet_max = max(rx_packet_max, 128);
Mugunthan V Ndf828592012-03-18 20:17:54 +00002931
Mugunthan V N1d147cc2015-09-07 15:16:44 +05302932 mode = devm_gpiod_get_array_optional(&pdev->dev, "mode", GPIOD_OUT_LOW);
2933 if (IS_ERR(mode)) {
2934 ret = PTR_ERR(mode);
2935 dev_err(&pdev->dev, "gpio request failed, ret %d\n", ret);
2936 goto clean_ndev_ret;
2937 }
2938
Vaibhav Hiremath1fb19aa2012-11-14 09:07:55 +00002939 /*
2940 * This may be required here for child devices.
2941 */
2942 pm_runtime_enable(&pdev->dev);
2943
Mugunthan V N739683b2013-06-06 23:45:14 +05302944 /* Select default pin state */
2945 pinctrl_pm_select_default_state(&pdev->dev);
2946
Johan Hovolda4e32b02016-11-17 17:40:00 +01002947 /* Need to enable clocks with runtime PM api to access module
2948 * registers
2949 */
2950 ret = pm_runtime_get_sync(&pdev->dev);
2951 if (ret < 0) {
2952 pm_runtime_put_noidle(&pdev->dev);
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302953 goto clean_runtime_disable_ret;
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002954 }
Johan Hovolda4e32b02016-11-17 17:40:00 +01002955
Johan Hovold23a09872016-11-17 17:40:04 +01002956 ret = cpsw_probe_dt(&cpsw->data, pdev);
2957 if (ret)
Johan Hovolda4e32b02016-11-17 17:40:00 +01002958 goto clean_dt_ret;
Johan Hovold23a09872016-11-17 17:40:04 +01002959
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002960 data = &cpsw->data;
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03002961 cpsw->rx_ch_num = 1;
2962 cpsw->tx_ch_num = 1;
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002963
Mugunthan V Ndf828592012-03-18 20:17:54 +00002964 if (is_valid_ether_addr(data->slave_data[0].mac_addr)) {
2965 memcpy(priv->mac_addr, data->slave_data[0].mac_addr, ETH_ALEN);
George Cherian88c99ff2014-05-12 10:21:19 +05302966 dev_info(&pdev->dev, "Detected MACID = %pM\n", priv->mac_addr);
Mugunthan V Ndf828592012-03-18 20:17:54 +00002967 } else {
Joe Perches7efd26d2012-07-12 19:33:06 +00002968 eth_random_addr(priv->mac_addr);
George Cherian88c99ff2014-05-12 10:21:19 +05302969 dev_info(&pdev->dev, "Random MACID = %pM\n", priv->mac_addr);
Mugunthan V Ndf828592012-03-18 20:17:54 +00002970 }
2971
2972 memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
2973
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002974 cpsw->slaves = devm_kzalloc(&pdev->dev,
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302975 sizeof(struct cpsw_slave) * data->slaves,
2976 GFP_KERNEL);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002977 if (!cpsw->slaves) {
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302978 ret = -ENOMEM;
Johan Hovolda4e32b02016-11-17 17:40:00 +01002979 goto clean_dt_ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002980 }
2981 for (i = 0; i < data->slaves; i++)
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002982 cpsw->slaves[i].slave_num = i;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002983
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002984 cpsw->slaves[0].ndev = ndev;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002985 priv->emac_port = 0;
2986
Ivan Khoronzhukef4183a2016-08-10 02:22:35 +03002987 clk = devm_clk_get(&pdev->dev, "fck");
2988 if (IS_ERR(clk)) {
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302989 dev_err(priv->dev, "fck is not found\n");
Mugunthan V Nf150bd72012-07-17 08:09:50 +00002990 ret = -ENODEV;
Johan Hovolda4e32b02016-11-17 17:40:00 +01002991 goto clean_dt_ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002992 }
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002993 cpsw->bus_freq_mhz = clk_get_rate(clk) / 1000000;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002994
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302995 ss_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2996 ss_regs = devm_ioremap_resource(&pdev->dev, ss_res);
2997 if (IS_ERR(ss_regs)) {
2998 ret = PTR_ERR(ss_regs);
Johan Hovolda4e32b02016-11-17 17:40:00 +01002999 goto clean_dt_ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00003000 }
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03003001 cpsw->regs = ss_regs;
Mugunthan V Ndf828592012-03-18 20:17:54 +00003002
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03003003 cpsw->version = readl(&cpsw->regs->id_ver);
Mugunthan V Nf280e892013-12-11 22:09:05 -06003004
Daniel Mackaa1a15e2013-09-21 00:50:38 +05303005 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03003006 cpsw->wr_regs = devm_ioremap_resource(&pdev->dev, res);
3007 if (IS_ERR(cpsw->wr_regs)) {
3008 ret = PTR_ERR(cpsw->wr_regs);
Johan Hovolda4e32b02016-11-17 17:40:00 +01003009 goto clean_dt_ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00003010 }
Mugunthan V Ndf828592012-03-18 20:17:54 +00003011
3012 memset(&dma_params, 0, sizeof(dma_params));
Richard Cochran549985e2012-11-14 09:07:56 +00003013 memset(&ale_params, 0, sizeof(ale_params));
3014
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03003015 switch (cpsw->version) {
Richard Cochran549985e2012-11-14 09:07:56 +00003016 case CPSW_VERSION_1:
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03003017 cpsw->host_port_regs = ss_regs + CPSW1_HOST_PORT_OFFSET;
Grygorii Strashko8a2c9a52016-12-06 18:00:41 -06003018 cpts_regs = ss_regs + CPSW1_CPTS_OFFSET;
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03003019 cpsw->hw_stats = ss_regs + CPSW1_HW_STATS;
Richard Cochran549985e2012-11-14 09:07:56 +00003020 dma_params.dmaregs = ss_regs + CPSW1_CPDMA_OFFSET;
3021 dma_params.txhdp = ss_regs + CPSW1_STATERAM_OFFSET;
3022 ale_params.ale_regs = ss_regs + CPSW1_ALE_OFFSET;
3023 slave_offset = CPSW1_SLAVE_OFFSET;
3024 slave_size = CPSW1_SLAVE_SIZE;
3025 sliver_offset = CPSW1_SLIVER_OFFSET;
3026 dma_params.desc_mem_phys = 0;
3027 break;
3028 case CPSW_VERSION_2:
Mugunthan V Nc193f362013-08-05 17:30:05 +05303029 case CPSW_VERSION_3:
Mugunthan V N926489b2013-08-12 17:11:15 +05303030 case CPSW_VERSION_4:
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03003031 cpsw->host_port_regs = ss_regs + CPSW2_HOST_PORT_OFFSET;
Grygorii Strashko8a2c9a52016-12-06 18:00:41 -06003032 cpts_regs = ss_regs + CPSW2_CPTS_OFFSET;
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03003033 cpsw->hw_stats = ss_regs + CPSW2_HW_STATS;
Richard Cochran549985e2012-11-14 09:07:56 +00003034 dma_params.dmaregs = ss_regs + CPSW2_CPDMA_OFFSET;
3035 dma_params.txhdp = ss_regs + CPSW2_STATERAM_OFFSET;
3036 ale_params.ale_regs = ss_regs + CPSW2_ALE_OFFSET;
3037 slave_offset = CPSW2_SLAVE_OFFSET;
3038 slave_size = CPSW2_SLAVE_SIZE;
3039 sliver_offset = CPSW2_SLIVER_OFFSET;
3040 dma_params.desc_mem_phys =
Daniel Mackaa1a15e2013-09-21 00:50:38 +05303041 (u32 __force) ss_res->start + CPSW2_BD_OFFSET;
Richard Cochran549985e2012-11-14 09:07:56 +00003042 break;
3043 default:
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03003044 dev_err(priv->dev, "unknown version 0x%08x\n", cpsw->version);
Richard Cochran549985e2012-11-14 09:07:56 +00003045 ret = -ENODEV;
Johan Hovolda4e32b02016-11-17 17:40:00 +01003046 goto clean_dt_ret;
Richard Cochran549985e2012-11-14 09:07:56 +00003047 }
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03003048 for (i = 0; i < cpsw->data.slaves; i++) {
3049 struct cpsw_slave *slave = &cpsw->slaves[i];
3050
3051 cpsw_slave_init(slave, cpsw, slave_offset, sliver_offset);
Richard Cochran549985e2012-11-14 09:07:56 +00003052 slave_offset += slave_size;
3053 sliver_offset += SLIVER_SIZE;
3054 }
3055
Mugunthan V Ndf828592012-03-18 20:17:54 +00003056 dma_params.dev = &pdev->dev;
Richard Cochran549985e2012-11-14 09:07:56 +00003057 dma_params.rxthresh = dma_params.dmaregs + CPDMA_RXTHRESH;
3058 dma_params.rxfree = dma_params.dmaregs + CPDMA_RXFREE;
3059 dma_params.rxhdp = dma_params.txhdp + CPDMA_RXHDP;
3060 dma_params.txcp = dma_params.txhdp + CPDMA_TXCP;
3061 dma_params.rxcp = dma_params.txhdp + CPDMA_RXCP;
Mugunthan V Ndf828592012-03-18 20:17:54 +00003062
3063 dma_params.num_chan = data->channels;
3064 dma_params.has_soft_reset = true;
3065 dma_params.min_packet_size = CPSW_MIN_PACKET_SIZE;
3066 dma_params.desc_mem_size = data->bd_ram_size;
3067 dma_params.desc_align = 16;
3068 dma_params.has_ext_regs = true;
Richard Cochran549985e2012-11-14 09:07:56 +00003069 dma_params.desc_hw_addr = dma_params.desc_mem_phys;
Ivan Khoronzhuk83fcad02016-11-29 17:00:49 +02003070 dma_params.bus_freq_mhz = cpsw->bus_freq_mhz;
Grygorii Strashko90225bf2017-01-06 14:07:33 -06003071 dma_params.descs_pool_size = descs_pool_size;
Mugunthan V Ndf828592012-03-18 20:17:54 +00003072
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03003073 cpsw->dma = cpdma_ctlr_create(&dma_params);
3074 if (!cpsw->dma) {
Mugunthan V Ndf828592012-03-18 20:17:54 +00003075 dev_err(priv->dev, "error initializing dma\n");
3076 ret = -ENOMEM;
Johan Hovolda4e32b02016-11-17 17:40:00 +01003077 goto clean_dt_ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00003078 }
3079
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02003080 cpsw->txv[0].ch = cpdma_chan_create(cpsw->dma, 0, cpsw_tx_handler, 0);
Ivan Khoronzhuk8a83c5d2017-12-12 23:06:35 +02003081 if (IS_ERR(cpsw->txv[0].ch)) {
3082 dev_err(priv->dev, "error initializing tx dma channel\n");
3083 ret = PTR_ERR(cpsw->txv[0].ch);
3084 goto clean_dma_ret;
3085 }
3086
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02003087 cpsw->rxv[0].ch = cpdma_chan_create(cpsw->dma, 0, cpsw_rx_handler, 1);
Ivan Khoronzhuk8a83c5d2017-12-12 23:06:35 +02003088 if (IS_ERR(cpsw->rxv[0].ch)) {
3089 dev_err(priv->dev, "error initializing rx dma channel\n");
3090 ret = PTR_ERR(cpsw->rxv[0].ch);
Mugunthan V Ndf828592012-03-18 20:17:54 +00003091 goto clean_dma_ret;
3092 }
3093
Ivan Khoronzhuk9fe9aa02017-02-15 19:45:02 +02003094 ale_params.dev = &pdev->dev;
Mugunthan V Ndf828592012-03-18 20:17:54 +00003095 ale_params.ale_ageout = ale_ageout;
3096 ale_params.ale_entries = data->ale_entries;
Grygorii Strashkoc6395f12017-11-30 18:21:14 -06003097 ale_params.ale_ports = CPSW_ALE_PORTS_NUM;
Mugunthan V Ndf828592012-03-18 20:17:54 +00003098
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03003099 cpsw->ale = cpsw_ale_create(&ale_params);
3100 if (!cpsw->ale) {
Mugunthan V Ndf828592012-03-18 20:17:54 +00003101 dev_err(priv->dev, "error initializing ale engine\n");
3102 ret = -ENODEV;
3103 goto clean_dma_ret;
3104 }
3105
Grygorii Strashko4a88fb92016-12-06 18:00:42 -06003106 cpsw->cpts = cpts_create(cpsw->dev, cpts_regs, cpsw->dev->of_node);
Grygorii Strashko8a2c9a52016-12-06 18:00:41 -06003107 if (IS_ERR(cpsw->cpts)) {
3108 ret = PTR_ERR(cpsw->cpts);
Grygorii Strashko1971ab52017-11-30 18:21:19 -06003109 goto clean_dma_ret;
Grygorii Strashko8a2c9a52016-12-06 18:00:41 -06003110 }
3111
Felipe Balbic03abd82015-01-16 10:11:12 -06003112 ndev->irq = platform_get_irq(pdev, 1);
Mugunthan V Ndf828592012-03-18 20:17:54 +00003113 if (ndev->irq < 0) {
3114 dev_err(priv->dev, "error getting irq resource\n");
Julia Lawallc1e33342015-12-26 20:12:13 +01003115 ret = ndev->irq;
Grygorii Strashko1971ab52017-11-30 18:21:19 -06003116 goto clean_dma_ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00003117 }
3118
Mugunthan V N7da11602015-08-12 15:22:53 +05303119 of_id = of_match_device(cpsw_of_mtable, &pdev->dev);
3120 if (of_id) {
3121 pdev->id_entry = of_id->data;
3122 if (pdev->id_entry->driver_data)
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +03003123 cpsw->quirk_irq = true;
Mugunthan V N7da11602015-08-12 15:22:53 +05303124 }
3125
Keerthy070f9c62017-07-20 16:59:52 +05303126 ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
3127
3128 ndev->netdev_ops = &cpsw_netdev_ops;
3129 ndev->ethtool_ops = &cpsw_ethtool_ops;
3130 netif_napi_add(ndev, &cpsw->napi_rx, cpsw_rx_poll, CPSW_POLL_WEIGHT);
3131 netif_tx_napi_add(ndev, &cpsw->napi_tx, cpsw_tx_poll, CPSW_POLL_WEIGHT);
3132 cpsw_split_res(ndev);
3133
3134 /* register the network device */
3135 SET_NETDEV_DEV(ndev, &pdev->dev);
3136 ret = register_netdev(ndev);
3137 if (ret) {
3138 dev_err(priv->dev, "error registering net device\n");
3139 ret = -ENODEV;
Grygorii Strashko1971ab52017-11-30 18:21:19 -06003140 goto clean_dma_ret;
Keerthy070f9c62017-07-20 16:59:52 +05303141 }
3142
3143 if (cpsw->data.dual_emac) {
3144 ret = cpsw_probe_dual_emac(priv);
3145 if (ret) {
3146 cpsw_err(priv, probe, "error probe slave 2 emac interface\n");
3147 goto clean_unregister_netdev_ret;
3148 }
3149 }
3150
Felipe Balbic03abd82015-01-16 10:11:12 -06003151 /* Grab RX and TX IRQs. Note that we also have RX_THRESHOLD and
3152 * MISC IRQs which are always kept disabled with this driver so
3153 * we will not request them.
3154 *
3155 * If anyone wants to implement support for those, make sure to
3156 * first request and append them to irqs_table array.
3157 */
Daniel Mackc2b32e52014-09-04 09:00:23 +02003158
Felipe Balbic03abd82015-01-16 10:11:12 -06003159 /* RX IRQ */
Felipe Balbi5087b912015-01-16 10:11:11 -06003160 irq = platform_get_irq(pdev, 1);
Julia Lawallc1e33342015-12-26 20:12:13 +01003161 if (irq < 0) {
3162 ret = irq;
Grygorii Strashko1971ab52017-11-30 18:21:19 -06003163 goto clean_dma_ret;
Julia Lawallc1e33342015-12-26 20:12:13 +01003164 }
Felipe Balbi5087b912015-01-16 10:11:11 -06003165
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +03003166 cpsw->irqs_table[0] = irq;
Felipe Balbic03abd82015-01-16 10:11:12 -06003167 ret = devm_request_irq(&pdev->dev, irq, cpsw_rx_interrupt,
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +03003168 0, dev_name(&pdev->dev), cpsw);
Felipe Balbi5087b912015-01-16 10:11:11 -06003169 if (ret < 0) {
3170 dev_err(priv->dev, "error attaching irq (%d)\n", ret);
Grygorii Strashko1971ab52017-11-30 18:21:19 -06003171 goto clean_dma_ret;
Felipe Balbi5087b912015-01-16 10:11:11 -06003172 }
3173
Felipe Balbic03abd82015-01-16 10:11:12 -06003174 /* TX IRQ */
Felipe Balbi5087b912015-01-16 10:11:11 -06003175 irq = platform_get_irq(pdev, 2);
Julia Lawallc1e33342015-12-26 20:12:13 +01003176 if (irq < 0) {
3177 ret = irq;
Grygorii Strashko1971ab52017-11-30 18:21:19 -06003178 goto clean_dma_ret;
Julia Lawallc1e33342015-12-26 20:12:13 +01003179 }
Felipe Balbi5087b912015-01-16 10:11:11 -06003180
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +03003181 cpsw->irqs_table[1] = irq;
Felipe Balbic03abd82015-01-16 10:11:12 -06003182 ret = devm_request_irq(&pdev->dev, irq, cpsw_tx_interrupt,
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +03003183 0, dev_name(&pdev->dev), cpsw);
Felipe Balbi5087b912015-01-16 10:11:11 -06003184 if (ret < 0) {
3185 dev_err(priv->dev, "error attaching irq (%d)\n", ret);
Grygorii Strashko1971ab52017-11-30 18:21:19 -06003186 goto clean_dma_ret;
Felipe Balbi5087b912015-01-16 10:11:11 -06003187 }
Daniel Mackc2b32e52014-09-04 09:00:23 +02003188
Grygorii Strashko90225bf2017-01-06 14:07:33 -06003189 cpsw_notice(priv, probe,
3190 "initialized device (regs %pa, irq %d, pool size %d)\n",
3191 &ss_res->start, ndev->irq, dma_params.descs_pool_size);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00003192
Johan Hovoldc46ab7e2016-11-17 17:39:58 +01003193 pm_runtime_put(&pdev->dev);
3194
Mugunthan V Ndf828592012-03-18 20:17:54 +00003195 return 0;
3196
Johan Hovolda7fe9d42016-11-17 17:40:02 +01003197clean_unregister_netdev_ret:
3198 unregister_netdev(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00003199clean_dma_ret:
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03003200 cpdma_ctlr_destroy(cpsw->dma);
Johan Hovolda4e32b02016-11-17 17:40:00 +01003201clean_dt_ret:
3202 cpsw_remove_dt(pdev);
Johan Hovoldc46ab7e2016-11-17 17:39:58 +01003203 pm_runtime_put_sync(&pdev->dev);
Daniel Mackaa1a15e2013-09-21 00:50:38 +05303204clean_runtime_disable_ret:
Mugunthan V Nf150bd72012-07-17 08:09:50 +00003205 pm_runtime_disable(&pdev->dev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00003206clean_ndev_ret:
Sebastian Siewiord1bd9ac2013-04-24 08:48:23 +00003207 free_netdev(priv->ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00003208 return ret;
3209}
3210
Bill Pemberton663e12e2012-12-03 09:23:45 -05003211static int cpsw_remove(struct platform_device *pdev)
Mugunthan V Ndf828592012-03-18 20:17:54 +00003212{
3213 struct net_device *ndev = platform_get_drvdata(pdev);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03003214 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Grygorii Strashko8a0b6dc2016-07-28 20:50:35 +03003215 int ret;
3216
3217 ret = pm_runtime_get_sync(&pdev->dev);
3218 if (ret < 0) {
3219 pm_runtime_put_noidle(&pdev->dev);
3220 return ret;
3221 }
Mugunthan V Ndf828592012-03-18 20:17:54 +00003222
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03003223 if (cpsw->data.dual_emac)
3224 unregister_netdev(cpsw->slaves[1].ndev);
Sebastian Siewiord1bd9ac2013-04-24 08:48:23 +00003225 unregister_netdev(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00003226
Grygorii Strashko8a2c9a52016-12-06 18:00:41 -06003227 cpts_release(cpsw->cpts);
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03003228 cpdma_ctlr_destroy(cpsw->dma);
Johan Hovolda4e32b02016-11-17 17:40:00 +01003229 cpsw_remove_dt(pdev);
Grygorii Strashko8a0b6dc2016-07-28 20:50:35 +03003230 pm_runtime_put_sync(&pdev->dev);
3231 pm_runtime_disable(&pdev->dev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03003232 if (cpsw->data.dual_emac)
3233 free_netdev(cpsw->slaves[1].ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00003234 free_netdev(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00003235 return 0;
3236}
3237
Grygorii Strashko8963a502015-02-27 13:19:45 +02003238#ifdef CONFIG_PM_SLEEP
Mugunthan V Ndf828592012-03-18 20:17:54 +00003239static int cpsw_suspend(struct device *dev)
3240{
3241 struct platform_device *pdev = to_platform_device(dev);
3242 struct net_device *ndev = platform_get_drvdata(pdev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03003243 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00003244
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03003245 if (cpsw->data.dual_emac) {
Mugunthan V N618073e2014-09-11 22:52:38 +05303246 int i;
Daniel Mack1e7a2e22013-11-15 08:29:16 +01003247
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03003248 for (i = 0; i < cpsw->data.slaves; i++) {
3249 if (netif_running(cpsw->slaves[i].ndev))
3250 cpsw_ndo_stop(cpsw->slaves[i].ndev);
Mugunthan V N618073e2014-09-11 22:52:38 +05303251 }
3252 } else {
3253 if (netif_running(ndev))
3254 cpsw_ndo_stop(ndev);
Mugunthan V N618073e2014-09-11 22:52:38 +05303255 }
Daniel Mack1e7a2e22013-11-15 08:29:16 +01003256
Mugunthan V N739683b2013-06-06 23:45:14 +05303257 /* Select sleep pin state */
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03003258 pinctrl_pm_select_sleep_state(dev);
Mugunthan V N739683b2013-06-06 23:45:14 +05303259
Mugunthan V Ndf828592012-03-18 20:17:54 +00003260 return 0;
3261}
3262
3263static int cpsw_resume(struct device *dev)
3264{
3265 struct platform_device *pdev = to_platform_device(dev);
3266 struct net_device *ndev = platform_get_drvdata(pdev);
Ivan Khoronzhuka60ced92017-02-14 14:42:15 +02003267 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00003268
Mugunthan V N739683b2013-06-06 23:45:14 +05303269 /* Select default pin state */
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03003270 pinctrl_pm_select_default_state(dev);
Mugunthan V N739683b2013-06-06 23:45:14 +05303271
Grygorii Strashko4ccfd632016-11-29 16:27:03 -06003272 /* shut up ASSERT_RTNL() warning in netif_set_real_num_tx/rx_queues */
3273 rtnl_lock();
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03003274 if (cpsw->data.dual_emac) {
Mugunthan V N618073e2014-09-11 22:52:38 +05303275 int i;
3276
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03003277 for (i = 0; i < cpsw->data.slaves; i++) {
3278 if (netif_running(cpsw->slaves[i].ndev))
3279 cpsw_ndo_open(cpsw->slaves[i].ndev);
Mugunthan V N618073e2014-09-11 22:52:38 +05303280 }
3281 } else {
3282 if (netif_running(ndev))
3283 cpsw_ndo_open(ndev);
3284 }
Grygorii Strashko4ccfd632016-11-29 16:27:03 -06003285 rtnl_unlock();
3286
Mugunthan V Ndf828592012-03-18 20:17:54 +00003287 return 0;
3288}
Grygorii Strashko8963a502015-02-27 13:19:45 +02003289#endif
Mugunthan V Ndf828592012-03-18 20:17:54 +00003290
Grygorii Strashko8963a502015-02-27 13:19:45 +02003291static SIMPLE_DEV_PM_OPS(cpsw_pm_ops, cpsw_suspend, cpsw_resume);
Mugunthan V Ndf828592012-03-18 20:17:54 +00003292
3293static struct platform_driver cpsw_driver = {
3294 .driver = {
3295 .name = "cpsw",
Mugunthan V Ndf828592012-03-18 20:17:54 +00003296 .pm = &cpsw_pm_ops,
Sachin Kamat1e5c76d2013-09-30 09:55:12 +05303297 .of_match_table = cpsw_of_mtable,
Mugunthan V Ndf828592012-03-18 20:17:54 +00003298 },
3299 .probe = cpsw_probe,
Bill Pemberton663e12e2012-12-03 09:23:45 -05003300 .remove = cpsw_remove,
Mugunthan V Ndf828592012-03-18 20:17:54 +00003301};
3302
Grygorii Strashko6fb3b6b52015-10-23 14:41:12 +03003303module_platform_driver(cpsw_driver);
Mugunthan V Ndf828592012-03-18 20:17:54 +00003304
3305MODULE_LICENSE("GPL");
3306MODULE_AUTHOR("Cyril Chemparathy <cyril@ti.com>");
3307MODULE_AUTHOR("Mugunthan V N <mugunthanvnm@ti.com>");
3308MODULE_DESCRIPTION("TI CPSW Ethernet driver");