blob: f099e0ed138d351f6d7fa75ff4484bb8c87e23c5 [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>
Arnd Bergmanne2b3e492018-05-30 23:51:54 +020032#include <linux/gpio/consumer.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>
Randy Dunlap514c6032018-04-05 16:25:34 -070038#include <linux/kmemleak.h>
Ivan Khoronzhuk9611d6d2018-05-17 01:21:45 +030039#include <linux/sys_soc.h>
Mugunthan V Ndf828592012-03-18 20:17:54 +000040
Mugunthan V N739683b2013-06-06 23:45:14 +053041#include <linux/pinctrl/consumer.h>
Ivan Khoronzhuk7929a662018-07-24 00:26:31 +030042#include <net/pkt_cls.h>
Mugunthan V Ndf828592012-03-18 20:17:54 +000043
Mugunthan V Ndbe34722013-08-19 17:47:40 +053044#include "cpsw.h"
Mugunthan V Ndf828592012-03-18 20:17:54 +000045#include "cpsw_ale.h"
Richard Cochran2e5b38a2012-10-29 08:45:20 +000046#include "cpts.h"
Mugunthan V Ndf828592012-03-18 20:17:54 +000047#include "davinci_cpdma.h"
48
49#define CPSW_DEBUG (NETIF_MSG_HW | NETIF_MSG_WOL | \
50 NETIF_MSG_DRV | NETIF_MSG_LINK | \
51 NETIF_MSG_IFUP | NETIF_MSG_INTR | \
52 NETIF_MSG_PROBE | NETIF_MSG_TIMER | \
53 NETIF_MSG_IFDOWN | NETIF_MSG_RX_ERR | \
54 NETIF_MSG_TX_ERR | NETIF_MSG_TX_DONE | \
55 NETIF_MSG_PKTDATA | NETIF_MSG_TX_QUEUED | \
56 NETIF_MSG_RX_STATUS)
57
58#define cpsw_info(priv, type, format, ...) \
59do { \
60 if (netif_msg_##type(priv) && net_ratelimit()) \
61 dev_info(priv->dev, format, ## __VA_ARGS__); \
62} while (0)
63
64#define cpsw_err(priv, type, format, ...) \
65do { \
66 if (netif_msg_##type(priv) && net_ratelimit()) \
67 dev_err(priv->dev, format, ## __VA_ARGS__); \
68} while (0)
69
70#define cpsw_dbg(priv, type, format, ...) \
71do { \
72 if (netif_msg_##type(priv) && net_ratelimit()) \
73 dev_dbg(priv->dev, format, ## __VA_ARGS__); \
74} while (0)
75
76#define cpsw_notice(priv, type, format, ...) \
77do { \
78 if (netif_msg_##type(priv) && net_ratelimit()) \
79 dev_notice(priv->dev, format, ## __VA_ARGS__); \
80} while (0)
81
Mugunthan V N5c50a852012-10-29 08:45:11 +000082#define ALE_ALL_PORTS 0x7
83
Mugunthan V Ndf828592012-03-18 20:17:54 +000084#define CPSW_MAJOR_VERSION(reg) (reg >> 8 & 0x7)
85#define CPSW_MINOR_VERSION(reg) (reg & 0xff)
86#define CPSW_RTL_VERSION(reg) ((reg >> 11) & 0x1f)
87
Richard Cochrane90cfac2012-10-29 08:45:14 +000088#define CPSW_VERSION_1 0x19010a
89#define CPSW_VERSION_2 0x19010c
Mugunthan V Nc193f362013-08-05 17:30:05 +053090#define CPSW_VERSION_3 0x19010f
Mugunthan V N926489b2013-08-12 17:11:15 +053091#define CPSW_VERSION_4 0x190112
Richard Cochran549985e2012-11-14 09:07:56 +000092
93#define HOST_PORT_NUM 0
Grygorii Strashkoc6395f12017-11-30 18:21:14 -060094#define CPSW_ALE_PORTS_NUM 3
Richard Cochran549985e2012-11-14 09:07:56 +000095#define SLIVER_SIZE 0x40
96
97#define CPSW1_HOST_PORT_OFFSET 0x028
98#define CPSW1_SLAVE_OFFSET 0x050
99#define CPSW1_SLAVE_SIZE 0x040
100#define CPSW1_CPDMA_OFFSET 0x100
101#define CPSW1_STATERAM_OFFSET 0x200
Mugunthan V Nd9718542013-07-23 15:38:17 +0530102#define CPSW1_HW_STATS 0x400
Richard Cochran549985e2012-11-14 09:07:56 +0000103#define CPSW1_CPTS_OFFSET 0x500
104#define CPSW1_ALE_OFFSET 0x600
105#define CPSW1_SLIVER_OFFSET 0x700
106
107#define CPSW2_HOST_PORT_OFFSET 0x108
108#define CPSW2_SLAVE_OFFSET 0x200
109#define CPSW2_SLAVE_SIZE 0x100
110#define CPSW2_CPDMA_OFFSET 0x800
Mugunthan V Nd9718542013-07-23 15:38:17 +0530111#define CPSW2_HW_STATS 0x900
Richard Cochran549985e2012-11-14 09:07:56 +0000112#define CPSW2_STATERAM_OFFSET 0xa00
113#define CPSW2_CPTS_OFFSET 0xc00
114#define CPSW2_ALE_OFFSET 0xd00
115#define CPSW2_SLIVER_OFFSET 0xd80
116#define CPSW2_BD_OFFSET 0x2000
117
Mugunthan V Ndf828592012-03-18 20:17:54 +0000118#define CPDMA_RXTHRESH 0x0c0
119#define CPDMA_RXFREE 0x0e0
120#define CPDMA_TXHDP 0x00
121#define CPDMA_RXHDP 0x20
122#define CPDMA_TXCP 0x40
123#define CPDMA_RXCP 0x60
124
Mugunthan V Ndf828592012-03-18 20:17:54 +0000125#define CPSW_POLL_WEIGHT 64
Grygorii Strashkoa3a41d22018-03-15 15:15:50 -0500126#define CPSW_RX_VLAN_ENCAP_HDR_SIZE 4
Grygorii Strashko9421c902017-11-15 09:46:35 -0600127#define CPSW_MIN_PACKET_SIZE (VLAN_ETH_ZLEN)
Grygorii Strashkoa3a41d22018-03-15 15:15:50 -0500128#define CPSW_MAX_PACKET_SIZE (VLAN_ETH_FRAME_LEN +\
129 ETH_FCS_LEN +\
130 CPSW_RX_VLAN_ENCAP_HDR_SIZE)
Mugunthan V Ndf828592012-03-18 20:17:54 +0000131
132#define RX_PRIORITY_MAPPING 0x76543210
133#define TX_PRIORITY_MAPPING 0x33221100
Ivan Khoronzhuk5e391dc52018-04-19 22:49:09 +0300134#define CPDMA_TX_PRIORITY_MAP 0x76543210
Mugunthan V Ndf828592012-03-18 20:17:54 +0000135
Mugunthan V N3b72c2f2013-02-05 08:26:48 +0000136#define CPSW_VLAN_AWARE BIT(1)
Grygorii Strashkoa3a41d22018-03-15 15:15:50 -0500137#define CPSW_RX_VLAN_ENCAP BIT(2)
Mugunthan V N3b72c2f2013-02-05 08:26:48 +0000138#define CPSW_ALE_VLAN_AWARE 1
139
John Ogness35717d82014-11-14 15:42:52 +0100140#define CPSW_FIFO_NORMAL_MODE (0 << 16)
141#define CPSW_FIFO_DUAL_MAC_MODE (1 << 16)
142#define CPSW_FIFO_RATE_LIMIT_MODE (2 << 16)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000143
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +0000144#define CPSW_INTPACEEN (0x3f << 16)
145#define CPSW_INTPRESCALE_MASK (0x7FF << 0)
146#define CPSW_CMINTMAX_CNT 63
147#define CPSW_CMINTMIN_CNT 2
148#define CPSW_CMINTMAX_INTVL (1000 / CPSW_CMINTMIN_CNT)
149#define CPSW_CMINTMIN_INTVL ((1000 / CPSW_CMINTMAX_CNT) + 1)
150
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300151#define cpsw_slave_index(cpsw, priv) \
152 ((cpsw->data.dual_emac) ? priv->emac_port : \
153 cpsw->data.active_slave)
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +0300154#define IRQ_NUM 2
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300155#define CPSW_MAX_QUEUES 8
Grygorii Strashko90225bf2017-01-06 14:07:33 -0600156#define CPSW_CPDMA_DESCS_POOL_SIZE_DEFAULT 256
Ivan Khoronzhuk7929a662018-07-24 00:26:31 +0300157#define CPSW_TC_NUM 4
158#define CPSW_FIFO_SHAPERS_NUM (CPSW_TC_NUM - 1)
Mugunthan V Nd3bb9c52013-03-11 23:16:36 +0000159
Grygorii Strashkoa3a41d22018-03-15 15:15:50 -0500160#define CPSW_RX_VLAN_ENCAP_HDR_PRIO_SHIFT 29
161#define CPSW_RX_VLAN_ENCAP_HDR_PRIO_MSK GENMASK(2, 0)
162#define CPSW_RX_VLAN_ENCAP_HDR_VID_SHIFT 16
163#define CPSW_RX_VLAN_ENCAP_HDR_PKT_TYPE_SHIFT 8
164#define CPSW_RX_VLAN_ENCAP_HDR_PKT_TYPE_MSK GENMASK(1, 0)
165enum {
166 CPSW_RX_VLAN_ENCAP_HDR_PKT_VLAN_TAG = 0,
167 CPSW_RX_VLAN_ENCAP_HDR_PKT_RESERV,
168 CPSW_RX_VLAN_ENCAP_HDR_PKT_PRIO_TAG,
169 CPSW_RX_VLAN_ENCAP_HDR_PKT_UNTAG,
170};
171
Mugunthan V Ndf828592012-03-18 20:17:54 +0000172static int debug_level;
173module_param(debug_level, int, 0);
174MODULE_PARM_DESC(debug_level, "cpsw debug level (NETIF_MSG bits)");
175
176static int ale_ageout = 10;
177module_param(ale_ageout, int, 0);
178MODULE_PARM_DESC(ale_ageout, "cpsw ale ageout interval (seconds)");
179
180static int rx_packet_max = CPSW_MAX_PACKET_SIZE;
181module_param(rx_packet_max, int, 0);
182MODULE_PARM_DESC(rx_packet_max, "maximum receive packet size (bytes)");
183
Grygorii Strashko90225bf2017-01-06 14:07:33 -0600184static int descs_pool_size = CPSW_CPDMA_DESCS_POOL_SIZE_DEFAULT;
185module_param(descs_pool_size, int, 0444);
186MODULE_PARM_DESC(descs_pool_size, "Number of CPDMA CPPI descriptors in pool");
187
Richard Cochran996a5c22012-10-29 08:45:12 +0000188struct cpsw_wr_regs {
Mugunthan V Ndf828592012-03-18 20:17:54 +0000189 u32 id_ver;
190 u32 soft_reset;
191 u32 control;
192 u32 int_control;
193 u32 rx_thresh_en;
194 u32 rx_en;
195 u32 tx_en;
196 u32 misc_en;
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +0000197 u32 mem_allign1[8];
198 u32 rx_thresh_stat;
199 u32 rx_stat;
200 u32 tx_stat;
201 u32 misc_stat;
202 u32 mem_allign2[8];
203 u32 rx_imax;
204 u32 tx_imax;
205
Mugunthan V Ndf828592012-03-18 20:17:54 +0000206};
207
Richard Cochran996a5c22012-10-29 08:45:12 +0000208struct cpsw_ss_regs {
Mugunthan V Ndf828592012-03-18 20:17:54 +0000209 u32 id_ver;
210 u32 control;
211 u32 soft_reset;
212 u32 stat_port_en;
213 u32 ptype;
Richard Cochranbd357af2012-10-29 08:45:13 +0000214 u32 soft_idle;
215 u32 thru_rate;
216 u32 gap_thresh;
217 u32 tx_start_wds;
218 u32 flow_control;
219 u32 vlan_ltype;
220 u32 ts_ltype;
221 u32 dlr_ltype;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000222};
223
Richard Cochran9750a3a2012-10-29 08:45:15 +0000224/* CPSW_PORT_V1 */
225#define CPSW1_MAX_BLKS 0x00 /* Maximum FIFO Blocks */
226#define CPSW1_BLK_CNT 0x04 /* FIFO Block Usage Count (Read Only) */
227#define CPSW1_TX_IN_CTL 0x08 /* Transmit FIFO Control */
228#define CPSW1_PORT_VLAN 0x0c /* VLAN Register */
229#define CPSW1_TX_PRI_MAP 0x10 /* Tx Header Priority to Switch Pri Mapping */
230#define CPSW1_TS_CTL 0x14 /* Time Sync Control */
231#define CPSW1_TS_SEQ_LTYPE 0x18 /* Time Sync Sequence ID Offset and Msg Type */
232#define CPSW1_TS_VLAN 0x1c /* Time Sync VLAN1 and VLAN2 */
233
234/* CPSW_PORT_V2 */
235#define CPSW2_CONTROL 0x00 /* Control Register */
236#define CPSW2_MAX_BLKS 0x08 /* Maximum FIFO Blocks */
237#define CPSW2_BLK_CNT 0x0c /* FIFO Block Usage Count (Read Only) */
238#define CPSW2_TX_IN_CTL 0x10 /* Transmit FIFO Control */
239#define CPSW2_PORT_VLAN 0x14 /* VLAN Register */
240#define CPSW2_TX_PRI_MAP 0x18 /* Tx Header Priority to Switch Pri Mapping */
241#define CPSW2_TS_SEQ_MTYPE 0x1c /* Time Sync Sequence ID Offset and Msg Type */
242
243/* CPSW_PORT_V1 and V2 */
244#define SA_LO 0x20 /* CPGMAC_SL Source Address Low */
245#define SA_HI 0x24 /* CPGMAC_SL Source Address High */
246#define SEND_PERCENT 0x28 /* Transmit Queue Send Percentages */
247
248/* CPSW_PORT_V2 only */
249#define RX_DSCP_PRI_MAP0 0x30 /* Rx DSCP Priority to Rx Packet Mapping */
250#define RX_DSCP_PRI_MAP1 0x34 /* Rx DSCP Priority to Rx Packet Mapping */
251#define RX_DSCP_PRI_MAP2 0x38 /* Rx DSCP Priority to Rx Packet Mapping */
252#define RX_DSCP_PRI_MAP3 0x3c /* Rx DSCP Priority to Rx Packet Mapping */
253#define RX_DSCP_PRI_MAP4 0x40 /* Rx DSCP Priority to Rx Packet Mapping */
254#define RX_DSCP_PRI_MAP5 0x44 /* Rx DSCP Priority to Rx Packet Mapping */
255#define RX_DSCP_PRI_MAP6 0x48 /* Rx DSCP Priority to Rx Packet Mapping */
256#define RX_DSCP_PRI_MAP7 0x4c /* Rx DSCP Priority to Rx Packet Mapping */
257
258/* Bit definitions for the CPSW2_CONTROL register */
Ivan Khoronzhuk1239a962018-07-06 21:44:44 +0300259#define PASS_PRI_TAGGED BIT(24) /* Pass Priority Tagged */
260#define VLAN_LTYPE2_EN BIT(21) /* VLAN LTYPE 2 enable */
261#define VLAN_LTYPE1_EN BIT(20) /* VLAN LTYPE 1 enable */
262#define DSCP_PRI_EN BIT(16) /* DSCP Priority Enable */
Ivan Khoronzhuk1c0e8122018-07-06 21:44:45 +0300263#define TS_107 BIT(15) /* Tyme Sync Dest IP Address 107 */
Ivan Khoronzhuk1239a962018-07-06 21:44:44 +0300264#define TS_320 BIT(14) /* Time Sync Dest Port 320 enable */
265#define TS_319 BIT(13) /* Time Sync Dest Port 319 enable */
266#define TS_132 BIT(12) /* Time Sync Dest IP Addr 132 enable */
267#define TS_131 BIT(11) /* Time Sync Dest IP Addr 131 enable */
268#define TS_130 BIT(10) /* Time Sync Dest IP Addr 130 enable */
269#define TS_129 BIT(9) /* Time Sync Dest IP Addr 129 enable */
270#define TS_TTL_NONZERO BIT(8) /* Time Sync Time To Live Non-zero enable */
271#define TS_ANNEX_F_EN BIT(6) /* Time Sync Annex F enable */
272#define TS_ANNEX_D_EN BIT(4) /* Time Sync Annex D enable */
273#define TS_LTYPE2_EN BIT(3) /* Time Sync LTYPE 2 enable */
274#define TS_LTYPE1_EN BIT(2) /* Time Sync LTYPE 1 enable */
275#define TS_TX_EN BIT(1) /* Time Sync Transmit Enable */
276#define TS_RX_EN BIT(0) /* Time Sync Receive Enable */
Richard Cochran9750a3a2012-10-29 08:45:15 +0000277
George Cherian09c55372014-05-02 12:02:02 +0530278#define CTRL_V2_TS_BITS \
279 (TS_320 | TS_319 | TS_132 | TS_131 | TS_130 | TS_129 |\
280 TS_TTL_NONZERO | TS_ANNEX_D_EN | TS_LTYPE1_EN)
Richard Cochran9750a3a2012-10-29 08:45:15 +0000281
George Cherian09c55372014-05-02 12:02:02 +0530282#define CTRL_V2_ALL_TS_MASK (CTRL_V2_TS_BITS | TS_TX_EN | TS_RX_EN)
283#define CTRL_V2_TX_TS_BITS (CTRL_V2_TS_BITS | TS_TX_EN)
284#define CTRL_V2_RX_TS_BITS (CTRL_V2_TS_BITS | TS_RX_EN)
285
286
287#define CTRL_V3_TS_BITS \
Ivan Khoronzhuk1c0e8122018-07-06 21:44:45 +0300288 (TS_107 | TS_320 | TS_319 | TS_132 | TS_131 | TS_130 | TS_129 |\
George Cherian09c55372014-05-02 12:02:02 +0530289 TS_TTL_NONZERO | TS_ANNEX_F_EN | TS_ANNEX_D_EN |\
290 TS_LTYPE1_EN)
291
292#define CTRL_V3_ALL_TS_MASK (CTRL_V3_TS_BITS | TS_TX_EN | TS_RX_EN)
293#define CTRL_V3_TX_TS_BITS (CTRL_V3_TS_BITS | TS_TX_EN)
294#define CTRL_V3_RX_TS_BITS (CTRL_V3_TS_BITS | TS_RX_EN)
Richard Cochran9750a3a2012-10-29 08:45:15 +0000295
296/* Bit definitions for the CPSW2_TS_SEQ_MTYPE register */
297#define TS_SEQ_ID_OFFSET_SHIFT (16) /* Time Sync Sequence ID Offset */
298#define TS_SEQ_ID_OFFSET_MASK (0x3f)
299#define TS_MSG_TYPE_EN_SHIFT (0) /* Time Sync Message Type Enable */
300#define TS_MSG_TYPE_EN_MASK (0xffff)
301
302/* The PTP event messages - Sync, Delay_Req, Pdelay_Req, and Pdelay_Resp. */
303#define EVENT_MSG_BITS ((1<<0) | (1<<1) | (1<<2) | (1<<3))
Mugunthan V Ndf828592012-03-18 20:17:54 +0000304
Richard Cochran2e5b38a2012-10-29 08:45:20 +0000305/* Bit definitions for the CPSW1_TS_CTL register */
306#define CPSW_V1_TS_RX_EN BIT(0)
307#define CPSW_V1_TS_TX_EN BIT(4)
308#define CPSW_V1_MSG_TYPE_OFS 16
309
310/* Bit definitions for the CPSW1_TS_SEQ_LTYPE register */
311#define CPSW_V1_SEQ_ID_OFS_SHIFT 16
312
Grygorii Strashko48f5bcc2017-05-08 14:21:21 -0500313#define CPSW_MAX_BLKS_TX 15
314#define CPSW_MAX_BLKS_TX_SHIFT 4
315#define CPSW_MAX_BLKS_RX 5
316
Mugunthan V Ndf828592012-03-18 20:17:54 +0000317struct cpsw_host_regs {
318 u32 max_blks;
319 u32 blk_cnt;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000320 u32 tx_in_ctl;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000321 u32 port_vlan;
322 u32 tx_pri_map;
323 u32 cpdma_tx_pri_map;
324 u32 cpdma_rx_chan_map;
325};
326
327struct cpsw_sliver_regs {
328 u32 id_ver;
329 u32 mac_control;
330 u32 mac_status;
331 u32 soft_reset;
332 u32 rx_maxlen;
333 u32 __reserved_0;
334 u32 rx_pause;
335 u32 tx_pause;
336 u32 __reserved_1;
337 u32 rx_pri_map;
338};
339
Mugunthan V Nd9718542013-07-23 15:38:17 +0530340struct cpsw_hw_stats {
341 u32 rxgoodframes;
342 u32 rxbroadcastframes;
343 u32 rxmulticastframes;
344 u32 rxpauseframes;
345 u32 rxcrcerrors;
346 u32 rxaligncodeerrors;
347 u32 rxoversizedframes;
348 u32 rxjabberframes;
349 u32 rxundersizedframes;
350 u32 rxfragments;
351 u32 __pad_0[2];
352 u32 rxoctets;
353 u32 txgoodframes;
354 u32 txbroadcastframes;
355 u32 txmulticastframes;
356 u32 txpauseframes;
357 u32 txdeferredframes;
358 u32 txcollisionframes;
359 u32 txsinglecollframes;
360 u32 txmultcollframes;
361 u32 txexcessivecollisions;
362 u32 txlatecollisions;
363 u32 txunderrun;
364 u32 txcarriersenseerrors;
365 u32 txoctets;
366 u32 octetframes64;
367 u32 octetframes65t127;
368 u32 octetframes128t255;
369 u32 octetframes256t511;
370 u32 octetframes512t1023;
371 u32 octetframes1024tup;
372 u32 netoctets;
373 u32 rxsofoverruns;
374 u32 rxmofoverruns;
375 u32 rxdmaoverruns;
376};
377
Grygorii Strashko2c8a14d2017-11-30 18:21:12 -0600378struct cpsw_slave_data {
379 struct device_node *phy_node;
380 char phy_id[MII_BUS_ID_SIZE];
381 int phy_if;
382 u8 mac_addr[ETH_ALEN];
383 u16 dual_emac_res_vlan; /* Reserved VLAN for DualEMAC */
384};
385
386struct cpsw_platform_data {
387 struct cpsw_slave_data *slave_data;
388 u32 ss_reg_ofs; /* Subsystem control register offset */
389 u32 channels; /* number of cpdma channels (symmetric) */
390 u32 slaves; /* number of slave cpgmac ports */
391 u32 active_slave; /* time stamping, ethtool and SIOCGMIIPHY slave */
392 u32 ale_entries; /* ale table size */
393 u32 bd_ram_size; /*buffer descriptor ram size */
394 u32 mac_control; /* Mac control register */
395 u16 default_vlan; /* Def VLAN for ALE lookup in VLAN aware mode*/
396 bool dual_emac; /* Enable Dual EMAC mode */
397};
398
Mugunthan V Ndf828592012-03-18 20:17:54 +0000399struct cpsw_slave {
Richard Cochran9750a3a2012-10-29 08:45:15 +0000400 void __iomem *regs;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000401 struct cpsw_sliver_regs __iomem *sliver;
402 int slave_num;
403 u32 mac_control;
404 struct cpsw_slave_data *data;
405 struct phy_device *phy;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000406 struct net_device *ndev;
407 u32 port_vlan;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000408};
409
Richard Cochran9750a3a2012-10-29 08:45:15 +0000410static inline u32 slave_read(struct cpsw_slave *slave, u32 offset)
411{
Grygorii Strashkodda5f5fe2017-11-30 18:21:11 -0600412 return readl_relaxed(slave->regs + offset);
Richard Cochran9750a3a2012-10-29 08:45:15 +0000413}
414
415static inline void slave_write(struct cpsw_slave *slave, u32 val, u32 offset)
416{
Grygorii Strashkodda5f5fe2017-11-30 18:21:11 -0600417 writel_relaxed(val, slave->regs + offset);
Richard Cochran9750a3a2012-10-29 08:45:15 +0000418}
419
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +0200420struct cpsw_vector {
421 struct cpdma_chan *ch;
422 int budget;
423};
424
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +0300425struct cpsw_common {
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +0300426 struct device *dev;
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300427 struct cpsw_platform_data data;
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +0300428 struct napi_struct napi_rx;
429 struct napi_struct napi_tx;
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +0300430 struct cpsw_ss_regs __iomem *regs;
431 struct cpsw_wr_regs __iomem *wr_regs;
432 u8 __iomem *hw_stats;
433 struct cpsw_host_regs __iomem *host_port_regs;
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300434 u32 version;
435 u32 coal_intvl;
436 u32 bus_freq_mhz;
437 int rx_packet_max;
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300438 struct cpsw_slave *slaves;
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +0300439 struct cpdma_ctlr *dma;
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +0200440 struct cpsw_vector txv[CPSW_MAX_QUEUES];
441 struct cpsw_vector rxv[CPSW_MAX_QUEUES];
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300442 struct cpsw_ale *ale;
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +0300443 bool quirk_irq;
444 bool rx_irq_disabled;
445 bool tx_irq_disabled;
446 u32 irqs_table[IRQ_NUM];
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300447 struct cpts *cpts;
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300448 int rx_ch_num, tx_ch_num;
Ivan Khoronzhuk0be01b82016-12-10 14:23:49 +0200449 int speed;
Ivan Khoronzhukd5bc1612017-02-14 16:02:36 +0200450 int usage_count;
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +0300451};
452
453struct cpsw_priv {
Mugunthan V Ndf828592012-03-18 20:17:54 +0000454 struct net_device *ndev;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000455 struct device *dev;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000456 u32 msg_enable;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000457 u8 mac_addr[ETH_ALEN];
Mugunthan V N1923d6e2014-09-08 22:54:02 +0530458 bool rx_pause;
459 bool tx_pause;
Ivan Khoronzhuk7929a662018-07-24 00:26:31 +0300460 bool mqprio_hw;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000461 u32 emac_port;
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +0300462 struct cpsw_common *cpsw;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000463};
464
Mugunthan V Nd9718542013-07-23 15:38:17 +0530465struct cpsw_stats {
466 char stat_string[ETH_GSTRING_LEN];
467 int type;
468 int sizeof_stat;
469 int stat_offset;
470};
471
472enum {
473 CPSW_STATS,
474 CPDMA_RX_STATS,
475 CPDMA_TX_STATS,
476};
477
478#define CPSW_STAT(m) CPSW_STATS, \
479 sizeof(((struct cpsw_hw_stats *)0)->m), \
480 offsetof(struct cpsw_hw_stats, m)
481#define CPDMA_RX_STAT(m) CPDMA_RX_STATS, \
482 sizeof(((struct cpdma_chan_stats *)0)->m), \
483 offsetof(struct cpdma_chan_stats, m)
484#define CPDMA_TX_STAT(m) CPDMA_TX_STATS, \
485 sizeof(((struct cpdma_chan_stats *)0)->m), \
486 offsetof(struct cpdma_chan_stats, m)
487
488static const struct cpsw_stats cpsw_gstrings_stats[] = {
489 { "Good Rx Frames", CPSW_STAT(rxgoodframes) },
490 { "Broadcast Rx Frames", CPSW_STAT(rxbroadcastframes) },
491 { "Multicast Rx Frames", CPSW_STAT(rxmulticastframes) },
492 { "Pause Rx Frames", CPSW_STAT(rxpauseframes) },
493 { "Rx CRC Errors", CPSW_STAT(rxcrcerrors) },
494 { "Rx Align/Code Errors", CPSW_STAT(rxaligncodeerrors) },
495 { "Oversize Rx Frames", CPSW_STAT(rxoversizedframes) },
496 { "Rx Jabbers", CPSW_STAT(rxjabberframes) },
497 { "Undersize (Short) Rx Frames", CPSW_STAT(rxundersizedframes) },
498 { "Rx Fragments", CPSW_STAT(rxfragments) },
499 { "Rx Octets", CPSW_STAT(rxoctets) },
500 { "Good Tx Frames", CPSW_STAT(txgoodframes) },
501 { "Broadcast Tx Frames", CPSW_STAT(txbroadcastframes) },
502 { "Multicast Tx Frames", CPSW_STAT(txmulticastframes) },
503 { "Pause Tx Frames", CPSW_STAT(txpauseframes) },
504 { "Deferred Tx Frames", CPSW_STAT(txdeferredframes) },
505 { "Collisions", CPSW_STAT(txcollisionframes) },
506 { "Single Collision Tx Frames", CPSW_STAT(txsinglecollframes) },
507 { "Multiple Collision Tx Frames", CPSW_STAT(txmultcollframes) },
508 { "Excessive Collisions", CPSW_STAT(txexcessivecollisions) },
509 { "Late Collisions", CPSW_STAT(txlatecollisions) },
510 { "Tx Underrun", CPSW_STAT(txunderrun) },
511 { "Carrier Sense Errors", CPSW_STAT(txcarriersenseerrors) },
512 { "Tx Octets", CPSW_STAT(txoctets) },
513 { "Rx + Tx 64 Octet Frames", CPSW_STAT(octetframes64) },
514 { "Rx + Tx 65-127 Octet Frames", CPSW_STAT(octetframes65t127) },
515 { "Rx + Tx 128-255 Octet Frames", CPSW_STAT(octetframes128t255) },
516 { "Rx + Tx 256-511 Octet Frames", CPSW_STAT(octetframes256t511) },
517 { "Rx + Tx 512-1023 Octet Frames", CPSW_STAT(octetframes512t1023) },
518 { "Rx + Tx 1024-Up Octet Frames", CPSW_STAT(octetframes1024tup) },
519 { "Net Octets", CPSW_STAT(netoctets) },
520 { "Rx Start of Frame Overruns", CPSW_STAT(rxsofoverruns) },
521 { "Rx Middle of Frame Overruns", CPSW_STAT(rxmofoverruns) },
522 { "Rx DMA Overruns", CPSW_STAT(rxdmaoverruns) },
Mugunthan V Nd9718542013-07-23 15:38:17 +0530523};
524
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300525static const struct cpsw_stats cpsw_gstrings_ch_stats[] = {
526 { "head_enqueue", CPDMA_RX_STAT(head_enqueue) },
527 { "tail_enqueue", CPDMA_RX_STAT(tail_enqueue) },
528 { "pad_enqueue", CPDMA_RX_STAT(pad_enqueue) },
529 { "misqueued", CPDMA_RX_STAT(misqueued) },
530 { "desc_alloc_fail", CPDMA_RX_STAT(desc_alloc_fail) },
531 { "pad_alloc_fail", CPDMA_RX_STAT(pad_alloc_fail) },
532 { "runt_receive_buf", CPDMA_RX_STAT(runt_receive_buff) },
533 { "runt_transmit_buf", CPDMA_RX_STAT(runt_transmit_buff) },
534 { "empty_dequeue", CPDMA_RX_STAT(empty_dequeue) },
535 { "busy_dequeue", CPDMA_RX_STAT(busy_dequeue) },
536 { "good_dequeue", CPDMA_RX_STAT(good_dequeue) },
537 { "requeue", CPDMA_RX_STAT(requeue) },
538 { "teardown_dequeue", CPDMA_RX_STAT(teardown_dequeue) },
539};
540
541#define CPSW_STATS_COMMON_LEN ARRAY_SIZE(cpsw_gstrings_stats)
542#define CPSW_STATS_CH_LEN ARRAY_SIZE(cpsw_gstrings_ch_stats)
Mugunthan V Nd9718542013-07-23 15:38:17 +0530543
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +0300544#define ndev_to_cpsw(ndev) (((struct cpsw_priv *)netdev_priv(ndev))->cpsw)
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +0300545#define napi_to_cpsw(napi) container_of(napi, struct cpsw_common, napi)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000546#define for_each_slave(priv, func, arg...) \
547 do { \
Sebastian Siewior6e6ceae2013-04-24 08:48:24 +0000548 struct cpsw_slave *slave; \
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300549 struct cpsw_common *cpsw = (priv)->cpsw; \
Sebastian Siewior6e6ceae2013-04-24 08:48:24 +0000550 int n; \
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300551 if (cpsw->data.dual_emac) \
552 (func)((cpsw)->slaves + priv->emac_port, ##arg);\
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000553 else \
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300554 for (n = cpsw->data.slaves, \
555 slave = cpsw->slaves; \
Sebastian Siewior6e6ceae2013-04-24 08:48:24 +0000556 n; n--) \
557 (func)(slave++, ##arg); \
Mugunthan V Ndf828592012-03-18 20:17:54 +0000558 } while (0)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000559
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300560#define cpsw_dual_emac_src_port_detect(cpsw, status, ndev, skb) \
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000561 do { \
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300562 if (!cpsw->data.dual_emac) \
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000563 break; \
564 if (CPDMA_RX_SOURCE_PORT(status) == 1) { \
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300565 ndev = cpsw->slaves[0].ndev; \
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000566 skb->dev = ndev; \
567 } else if (CPDMA_RX_SOURCE_PORT(status) == 2) { \
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300568 ndev = cpsw->slaves[1].ndev; \
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000569 skb->dev = ndev; \
570 } \
571 } while (0)
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300572#define cpsw_add_mcast(cpsw, priv, addr) \
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000573 do { \
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300574 if (cpsw->data.dual_emac) { \
575 struct cpsw_slave *slave = cpsw->slaves + \
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000576 priv->emac_port; \
Ivan Khoronzhuk6f1f5832016-08-10 02:22:34 +0300577 int slave_port = cpsw_get_slave_port( \
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000578 slave->slave_num); \
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300579 cpsw_ale_add_mcast(cpsw->ale, addr, \
Grygorii Strashko71a2cbb2016-04-07 15:16:44 +0300580 1 << slave_port | ALE_PORT_HOST, \
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000581 ALE_VLAN, slave->port_vlan, 0); \
582 } else { \
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300583 cpsw_ale_add_mcast(cpsw->ale, addr, \
Grygorii Strashko61f1cef2016-04-07 15:16:43 +0300584 ALE_ALL_PORTS, \
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000585 0, 0, 0); \
586 } \
587 } while (0)
588
Ivan Khoronzhuk6f1f5832016-08-10 02:22:34 +0300589static inline int cpsw_get_slave_port(u32 slave_num)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000590{
Grygorii Strashko71a2cbb2016-04-07 15:16:44 +0300591 return slave_num + 1;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000592}
Mugunthan V Ndf828592012-03-18 20:17:54 +0000593
Mugunthan V N0cd8f9c2014-01-23 00:03:12 +0530594static void cpsw_set_promiscious(struct net_device *ndev, bool enable)
595{
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300596 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
597 struct cpsw_ale *ale = cpsw->ale;
Mugunthan V N0cd8f9c2014-01-23 00:03:12 +0530598 int i;
599
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300600 if (cpsw->data.dual_emac) {
Mugunthan V N0cd8f9c2014-01-23 00:03:12 +0530601 bool flag = false;
602
603 /* Enabling promiscuous mode for one interface will be
604 * common for both the interface as the interface shares
605 * the same hardware resource.
606 */
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300607 for (i = 0; i < cpsw->data.slaves; i++)
608 if (cpsw->slaves[i].ndev->flags & IFF_PROMISC)
Mugunthan V N0cd8f9c2014-01-23 00:03:12 +0530609 flag = true;
610
611 if (!enable && flag) {
612 enable = true;
613 dev_err(&ndev->dev, "promiscuity not disabled as the other interface is still in promiscuity mode\n");
614 }
615
616 if (enable) {
617 /* Enable Bypass */
618 cpsw_ale_control_set(ale, 0, ALE_BYPASS, 1);
619
620 dev_dbg(&ndev->dev, "promiscuity enabled\n");
621 } else {
622 /* Disable Bypass */
623 cpsw_ale_control_set(ale, 0, ALE_BYPASS, 0);
624 dev_dbg(&ndev->dev, "promiscuity disabled\n");
625 }
626 } else {
627 if (enable) {
628 unsigned long timeout = jiffies + HZ;
629
Lennart Sorensen6f979eb2014-10-31 13:28:54 -0400630 /* Disable Learn for all ports (host is port 0 and slaves are port 1 and up */
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300631 for (i = 0; i <= cpsw->data.slaves; i++) {
Mugunthan V N0cd8f9c2014-01-23 00:03:12 +0530632 cpsw_ale_control_set(ale, i,
633 ALE_PORT_NOLEARN, 1);
634 cpsw_ale_control_set(ale, i,
635 ALE_PORT_NO_SA_UPDATE, 1);
636 }
637
638 /* Clear All Untouched entries */
639 cpsw_ale_control_set(ale, 0, ALE_AGEOUT, 1);
640 do {
641 cpu_relax();
642 if (cpsw_ale_control_get(ale, 0, ALE_AGEOUT))
643 break;
644 } while (time_after(timeout, jiffies));
645 cpsw_ale_control_set(ale, 0, ALE_AGEOUT, 1);
646
647 /* Clear all mcast from ALE */
Grygorii Strashko61f1cef2016-04-07 15:16:43 +0300648 cpsw_ale_flush_multicast(ale, ALE_ALL_PORTS, -1);
Mugunthan V N0cd8f9c2014-01-23 00:03:12 +0530649
650 /* Flood All Unicast Packets to Host port */
651 cpsw_ale_control_set(ale, 0, ALE_P0_UNI_FLOOD, 1);
652 dev_dbg(&ndev->dev, "promiscuity enabled\n");
653 } else {
Lennart Sorensen6f979eb2014-10-31 13:28:54 -0400654 /* Don't Flood All Unicast Packets to Host port */
Mugunthan V N0cd8f9c2014-01-23 00:03:12 +0530655 cpsw_ale_control_set(ale, 0, ALE_P0_UNI_FLOOD, 0);
656
Lennart Sorensen6f979eb2014-10-31 13:28:54 -0400657 /* Enable Learn for all ports (host is port 0 and slaves are port 1 and up */
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300658 for (i = 0; i <= cpsw->data.slaves; i++) {
Mugunthan V N0cd8f9c2014-01-23 00:03:12 +0530659 cpsw_ale_control_set(ale, i,
660 ALE_PORT_NOLEARN, 0);
661 cpsw_ale_control_set(ale, i,
662 ALE_PORT_NO_SA_UPDATE, 0);
663 }
664 dev_dbg(&ndev->dev, "promiscuity disabled\n");
665 }
666 }
667}
668
Mugunthan V N5c50a852012-10-29 08:45:11 +0000669static void cpsw_ndo_set_rx_mode(struct net_device *ndev)
670{
671 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300672 struct cpsw_common *cpsw = priv->cpsw;
Mugunthan V N25906052015-01-13 17:35:49 +0530673 int vid;
674
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300675 if (cpsw->data.dual_emac)
676 vid = cpsw->slaves[priv->emac_port].port_vlan;
Mugunthan V N25906052015-01-13 17:35:49 +0530677 else
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300678 vid = cpsw->data.default_vlan;
Mugunthan V N5c50a852012-10-29 08:45:11 +0000679
680 if (ndev->flags & IFF_PROMISC) {
681 /* Enable promiscuous mode */
Mugunthan V N0cd8f9c2014-01-23 00:03:12 +0530682 cpsw_set_promiscious(ndev, true);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300683 cpsw_ale_set_allmulti(cpsw->ale, IFF_ALLMULTI);
Mugunthan V N5c50a852012-10-29 08:45:11 +0000684 return;
Mugunthan V N0cd8f9c2014-01-23 00:03:12 +0530685 } else {
686 /* Disable promiscuous mode */
687 cpsw_set_promiscious(ndev, false);
Mugunthan V N5c50a852012-10-29 08:45:11 +0000688 }
689
Lennart Sorensen1e5c4bc2014-10-31 13:38:52 -0400690 /* Restore allmulti on vlans if necessary */
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300691 cpsw_ale_set_allmulti(cpsw->ale, priv->ndev->flags & IFF_ALLMULTI);
Lennart Sorensen1e5c4bc2014-10-31 13:38:52 -0400692
Mugunthan V N5c50a852012-10-29 08:45:11 +0000693 /* Clear all mcast from ALE */
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300694 cpsw_ale_flush_multicast(cpsw->ale, ALE_ALL_PORTS, vid);
Mugunthan V N5c50a852012-10-29 08:45:11 +0000695
696 if (!netdev_mc_empty(ndev)) {
697 struct netdev_hw_addr *ha;
698
699 /* program multicast address list into ALE register */
700 netdev_for_each_mc_addr(ha, ndev) {
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300701 cpsw_add_mcast(cpsw, priv, (u8 *)ha->addr);
Mugunthan V N5c50a852012-10-29 08:45:11 +0000702 }
703 }
704}
705
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +0300706static void cpsw_intr_enable(struct cpsw_common *cpsw)
Mugunthan V Ndf828592012-03-18 20:17:54 +0000707{
Grygorii Strashkodda5f5fe2017-11-30 18:21:11 -0600708 writel_relaxed(0xFF, &cpsw->wr_regs->tx_en);
709 writel_relaxed(0xFF, &cpsw->wr_regs->rx_en);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000710
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +0300711 cpdma_ctlr_int_ctrl(cpsw->dma, true);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000712 return;
713}
714
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +0300715static void cpsw_intr_disable(struct cpsw_common *cpsw)
Mugunthan V Ndf828592012-03-18 20:17:54 +0000716{
Grygorii Strashkodda5f5fe2017-11-30 18:21:11 -0600717 writel_relaxed(0, &cpsw->wr_regs->tx_en);
718 writel_relaxed(0, &cpsw->wr_regs->rx_en);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000719
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +0300720 cpdma_ctlr_int_ctrl(cpsw->dma, false);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000721 return;
722}
723
Olof Johansson1a3b5052013-12-11 15:58:07 -0800724static void cpsw_tx_handler(void *token, int len, int status)
Mugunthan V Ndf828592012-03-18 20:17:54 +0000725{
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300726 struct netdev_queue *txq;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000727 struct sk_buff *skb = token;
728 struct net_device *ndev = skb->dev;
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300729 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000730
Mugunthan V Nfae50822013-01-17 06:31:34 +0000731 /* Check whether the queue is stopped due to stalled tx dma, if the
732 * queue is stopped then start the queue as we have free desc for tx
733 */
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300734 txq = netdev_get_tx_queue(ndev, skb_get_queue_mapping(skb));
735 if (unlikely(netif_tx_queue_stopped(txq)))
736 netif_tx_wake_queue(txq);
737
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300738 cpts_tx_timestamp(cpsw->cpts, skb);
Tobias Klauser8dc43dd2014-03-10 13:12:23 +0100739 ndev->stats.tx_packets++;
740 ndev->stats.tx_bytes += len;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000741 dev_kfree_skb_any(skb);
742}
743
Grygorii Strashkoa3a41d22018-03-15 15:15:50 -0500744static void cpsw_rx_vlan_encap(struct sk_buff *skb)
745{
746 struct cpsw_priv *priv = netdev_priv(skb->dev);
747 struct cpsw_common *cpsw = priv->cpsw;
748 u32 rx_vlan_encap_hdr = *((u32 *)skb->data);
749 u16 vtag, vid, prio, pkt_type;
750
751 /* Remove VLAN header encapsulation word */
752 skb_pull(skb, CPSW_RX_VLAN_ENCAP_HDR_SIZE);
753
754 pkt_type = (rx_vlan_encap_hdr >>
755 CPSW_RX_VLAN_ENCAP_HDR_PKT_TYPE_SHIFT) &
756 CPSW_RX_VLAN_ENCAP_HDR_PKT_TYPE_MSK;
757 /* Ignore unknown & Priority-tagged packets*/
758 if (pkt_type == CPSW_RX_VLAN_ENCAP_HDR_PKT_RESERV ||
759 pkt_type == CPSW_RX_VLAN_ENCAP_HDR_PKT_PRIO_TAG)
760 return;
761
762 vid = (rx_vlan_encap_hdr >>
763 CPSW_RX_VLAN_ENCAP_HDR_VID_SHIFT) &
764 VLAN_VID_MASK;
765 /* Ignore vid 0 and pass packet as is */
766 if (!vid)
767 return;
768 /* Ignore default vlans in dual mac mode */
769 if (cpsw->data.dual_emac &&
770 vid == cpsw->slaves[priv->emac_port].port_vlan)
771 return;
772
773 prio = (rx_vlan_encap_hdr >>
774 CPSW_RX_VLAN_ENCAP_HDR_PRIO_SHIFT) &
775 CPSW_RX_VLAN_ENCAP_HDR_PRIO_MSK;
776
777 vtag = (prio << VLAN_PRIO_SHIFT) | vid;
778 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vtag);
779
780 /* strip vlan tag for VLAN-tagged packet */
781 if (pkt_type == CPSW_RX_VLAN_ENCAP_HDR_PKT_VLAN_TAG) {
782 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
783 skb_pull(skb, VLAN_HLEN);
784 }
785}
786
Olof Johansson1a3b5052013-12-11 15:58:07 -0800787static void cpsw_rx_handler(void *token, int len, int status)
Mugunthan V Ndf828592012-03-18 20:17:54 +0000788{
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300789 struct cpdma_chan *ch;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000790 struct sk_buff *skb = token;
Sebastian Siewiorb4727e62013-04-23 07:31:39 +0000791 struct sk_buff *new_skb;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000792 struct net_device *ndev = skb->dev;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000793 int ret = 0;
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300794 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000795
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300796 cpsw_dual_emac_src_port_detect(cpsw, status, ndev, skb);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000797
Mugunthan V N16e5c572014-04-10 14:23:23 +0530798 if (unlikely(status < 0) || unlikely(!netif_running(ndev))) {
Ivan Khoronzhukfe734d02017-01-19 18:58:26 +0200799 /* In dual emac mode check for all interfaces */
Ivan Khoronzhukd5bc1612017-02-14 16:02:36 +0200800 if (cpsw->data.dual_emac && cpsw->usage_count &&
Ivan Khoronzhukfe734d02017-01-19 18:58:26 +0200801 (status >= 0)) {
Mugunthan V Na0e2c822014-09-10 16:38:09 +0530802 /* The packet received is for the interface which
803 * is already down and the other interface is up
Joe Perchesdbedd442015-03-06 20:49:12 -0800804 * and running, instead of freeing which results
Mugunthan V Na0e2c822014-09-10 16:38:09 +0530805 * in reducing of the number of rx descriptor in
806 * DMA engine, requeue skb back to cpdma.
807 */
808 new_skb = skb;
809 goto requeue;
810 }
811
Sebastian Siewiorb4727e62013-04-23 07:31:39 +0000812 /* the interface is going down, skbs are purged */
Mugunthan V Ndf828592012-03-18 20:17:54 +0000813 dev_kfree_skb_any(skb);
814 return;
815 }
Sebastian Siewiorb4727e62013-04-23 07:31:39 +0000816
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300817 new_skb = netdev_alloc_skb_ip_align(ndev, cpsw->rx_packet_max);
Sebastian Siewiorb4727e62013-04-23 07:31:39 +0000818 if (new_skb) {
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300819 skb_copy_queue_mapping(new_skb, skb);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000820 skb_put(skb, len);
Grygorii Strashkoa3a41d22018-03-15 15:15:50 -0500821 if (status & CPDMA_RX_VLAN_ENCAP)
822 cpsw_rx_vlan_encap(skb);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300823 cpts_rx_timestamp(cpsw->cpts, skb);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000824 skb->protocol = eth_type_trans(skb, ndev);
825 netif_receive_skb(skb);
Tobias Klauser8dc43dd2014-03-10 13:12:23 +0100826 ndev->stats.rx_bytes += len;
827 ndev->stats.rx_packets++;
Grygorii Strashko254a49d2016-08-09 15:09:44 +0300828 kmemleak_not_leak(new_skb);
Sebastian Siewiorb4727e62013-04-23 07:31:39 +0000829 } else {
Tobias Klauser8dc43dd2014-03-10 13:12:23 +0100830 ndev->stats.rx_dropped++;
Sebastian Siewiorb4727e62013-04-23 07:31:39 +0000831 new_skb = skb;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000832 }
833
Mugunthan V Na0e2c822014-09-10 16:38:09 +0530834requeue:
Ivan Khoronzhukce52c742016-08-22 21:18:28 +0300835 if (netif_dormant(ndev)) {
836 dev_kfree_skb_any(new_skb);
837 return;
838 }
839
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +0200840 ch = cpsw->rxv[skb_get_queue_mapping(new_skb)].ch;
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300841 ret = cpdma_chan_submit(ch, new_skb, new_skb->data,
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +0300842 skb_tailroom(new_skb), 0);
Sebastian Siewiorb4727e62013-04-23 07:31:39 +0000843 if (WARN_ON(ret < 0))
844 dev_kfree_skb_any(new_skb);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000845}
846
Ivan Khoronzhuk32b78d82016-12-10 14:23:48 +0200847static void cpsw_split_res(struct net_device *ndev)
Ivan Khoronzhuk48e0a832016-12-06 03:45:00 +0200848{
849 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk32b78d82016-12-10 14:23:48 +0200850 u32 consumed_rate = 0, bigest_rate = 0;
Ivan Khoronzhuk48e0a832016-12-06 03:45:00 +0200851 struct cpsw_common *cpsw = priv->cpsw;
852 struct cpsw_vector *txv = cpsw->txv;
Ivan Khoronzhuk32b78d82016-12-10 14:23:48 +0200853 int i, ch_weight, rlim_ch_num = 0;
Ivan Khoronzhuk48e0a832016-12-06 03:45:00 +0200854 int budget, bigest_rate_ch = 0;
Ivan Khoronzhuk48e0a832016-12-06 03:45:00 +0200855 u32 ch_rate, max_rate;
856 int ch_budget = 0;
857
Ivan Khoronzhuk48e0a832016-12-06 03:45:00 +0200858 for (i = 0; i < cpsw->tx_ch_num; i++) {
859 ch_rate = cpdma_chan_get_rate(txv[i].ch);
860 if (!ch_rate)
861 continue;
862
863 rlim_ch_num++;
864 consumed_rate += ch_rate;
865 }
866
867 if (cpsw->tx_ch_num == rlim_ch_num) {
868 max_rate = consumed_rate;
Ivan Khoronzhuk32b78d82016-12-10 14:23:48 +0200869 } else if (!rlim_ch_num) {
870 ch_budget = CPSW_POLL_WEIGHT / cpsw->tx_ch_num;
871 bigest_rate = 0;
872 max_rate = consumed_rate;
Ivan Khoronzhuk48e0a832016-12-06 03:45:00 +0200873 } else {
Ivan Khoronzhuk0be01b82016-12-10 14:23:49 +0200874 max_rate = cpsw->speed * 1000;
875
876 /* if max_rate is less then expected due to reduced link speed,
877 * split proportionally according next potential max speed
878 */
879 if (max_rate < consumed_rate)
880 max_rate *= 10;
881
882 if (max_rate < consumed_rate)
883 max_rate *= 10;
Ivan Khoronzhuk32b78d82016-12-10 14:23:48 +0200884
Ivan Khoronzhuk48e0a832016-12-06 03:45:00 +0200885 ch_budget = (consumed_rate * CPSW_POLL_WEIGHT) / max_rate;
886 ch_budget = (CPSW_POLL_WEIGHT - ch_budget) /
887 (cpsw->tx_ch_num - rlim_ch_num);
888 bigest_rate = (max_rate - consumed_rate) /
889 (cpsw->tx_ch_num - rlim_ch_num);
890 }
891
Ivan Khoronzhuk32b78d82016-12-10 14:23:48 +0200892 /* split tx weight/budget */
Ivan Khoronzhuk48e0a832016-12-06 03:45:00 +0200893 budget = CPSW_POLL_WEIGHT;
894 for (i = 0; i < cpsw->tx_ch_num; i++) {
895 ch_rate = cpdma_chan_get_rate(txv[i].ch);
896 if (ch_rate) {
897 txv[i].budget = (ch_rate * CPSW_POLL_WEIGHT) / max_rate;
898 if (!txv[i].budget)
Ivan Khoronzhuk32b78d82016-12-10 14:23:48 +0200899 txv[i].budget++;
Ivan Khoronzhuk48e0a832016-12-06 03:45:00 +0200900 if (ch_rate > bigest_rate) {
901 bigest_rate_ch = i;
902 bigest_rate = ch_rate;
903 }
Ivan Khoronzhuk32b78d82016-12-10 14:23:48 +0200904
905 ch_weight = (ch_rate * 100) / max_rate;
906 if (!ch_weight)
907 ch_weight++;
908 cpdma_chan_set_weight(cpsw->txv[i].ch, ch_weight);
Ivan Khoronzhuk48e0a832016-12-06 03:45:00 +0200909 } else {
910 txv[i].budget = ch_budget;
911 if (!bigest_rate_ch)
912 bigest_rate_ch = i;
Ivan Khoronzhuk32b78d82016-12-10 14:23:48 +0200913 cpdma_chan_set_weight(cpsw->txv[i].ch, 0);
Ivan Khoronzhuk48e0a832016-12-06 03:45:00 +0200914 }
915
916 budget -= txv[i].budget;
917 }
918
919 if (budget)
920 txv[bigest_rate_ch].budget += budget;
921
922 /* split rx budget */
923 budget = CPSW_POLL_WEIGHT;
924 ch_budget = budget / cpsw->rx_ch_num;
925 for (i = 0; i < cpsw->rx_ch_num; i++) {
926 cpsw->rxv[i].budget = ch_budget;
927 budget -= ch_budget;
928 }
929
930 if (budget)
931 cpsw->rxv[0].budget += budget;
932}
933
Felipe Balbic03abd82015-01-16 10:11:12 -0600934static irqreturn_t cpsw_tx_interrupt(int irq, void *dev_id)
Mugunthan V Ndf828592012-03-18 20:17:54 +0000935{
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +0300936 struct cpsw_common *cpsw = dev_id;
Felipe Balbi7ce67a32015-01-02 16:15:59 -0600937
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +0300938 writel(0, &cpsw->wr_regs->tx_en);
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +0300939 cpdma_ctlr_eoi(cpsw->dma, CPDMA_EOI_TX);
Felipe Balbic03abd82015-01-16 10:11:12 -0600940
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +0300941 if (cpsw->quirk_irq) {
942 disable_irq_nosync(cpsw->irqs_table[1]);
943 cpsw->tx_irq_disabled = true;
Mugunthan V N7da11602015-08-12 15:22:53 +0530944 }
945
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +0300946 napi_schedule(&cpsw->napi_tx);
Felipe Balbic03abd82015-01-16 10:11:12 -0600947 return IRQ_HANDLED;
948}
949
950static irqreturn_t cpsw_rx_interrupt(int irq, void *dev_id)
951{
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +0300952 struct cpsw_common *cpsw = dev_id;
Felipe Balbic03abd82015-01-16 10:11:12 -0600953
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +0300954 cpdma_ctlr_eoi(cpsw->dma, CPDMA_EOI_RX);
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +0300955 writel(0, &cpsw->wr_regs->rx_en);
Sebastian Siewiorfd51cf12013-04-23 07:31:37 +0000956
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +0300957 if (cpsw->quirk_irq) {
958 disable_irq_nosync(cpsw->irqs_table[0]);
959 cpsw->rx_irq_disabled = true;
Mugunthan V N7da11602015-08-12 15:22:53 +0530960 }
961
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +0300962 napi_schedule(&cpsw->napi_rx);
Mugunthan V Nd354eb82015-08-04 16:06:19 +0530963 return IRQ_HANDLED;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000964}
965
Ivan Khoronzhuk9611d6d2018-05-17 01:21:45 +0300966static int cpsw_tx_mq_poll(struct napi_struct *napi_tx, int budget)
Mugunthan V Ndf828592012-03-18 20:17:54 +0000967{
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300968 u32 ch_map;
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +0200969 int num_tx, cur_budget, ch;
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +0300970 struct cpsw_common *cpsw = napi_to_cpsw(napi_tx);
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +0200971 struct cpsw_vector *txv;
Mugunthan V N32a74322015-08-04 16:06:20 +0530972
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300973 /* process every unprocessed channel */
974 ch_map = cpdma_ctrl_txchs_state(cpsw->dma);
Ivan Khoronzhuk79b33252018-07-24 00:26:29 +0300975 for (ch = 0, num_tx = 0; ch_map & 0xff; ch_map <<= 1, ch++) {
976 if (!(ch_map & 0x80))
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300977 continue;
978
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +0200979 txv = &cpsw->txv[ch];
980 if (unlikely(txv->budget > budget - num_tx))
981 cur_budget = budget - num_tx;
982 else
983 cur_budget = txv->budget;
984
985 num_tx += cpdma_chan_process(txv->ch, cur_budget);
Ivan Khoronzhuk342934a2016-11-29 17:00:50 +0200986 if (num_tx >= budget)
987 break;
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300988 }
989
Mugunthan V N32a74322015-08-04 16:06:20 +0530990 if (num_tx < budget) {
991 napi_complete(napi_tx);
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +0300992 writel(0xff, &cpsw->wr_regs->tx_en);
Ivan Khoronzhuk9611d6d2018-05-17 01:21:45 +0300993 }
994
995 return num_tx;
996}
997
998static int cpsw_tx_poll(struct napi_struct *napi_tx, int budget)
999{
1000 struct cpsw_common *cpsw = napi_to_cpsw(napi_tx);
1001 int num_tx;
1002
1003 num_tx = cpdma_chan_process(cpsw->txv[0].ch, budget);
1004 if (num_tx < budget) {
1005 napi_complete(napi_tx);
1006 writel(0xff, &cpsw->wr_regs->tx_en);
1007 if (cpsw->tx_irq_disabled) {
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +03001008 cpsw->tx_irq_disabled = false;
1009 enable_irq(cpsw->irqs_table[1]);
Mugunthan V N7da11602015-08-12 15:22:53 +05301010 }
Mugunthan V N32a74322015-08-04 16:06:20 +05301011 }
1012
Mugunthan V N32a74322015-08-04 16:06:20 +05301013 return num_tx;
1014}
1015
Ivan Khoronzhuk9611d6d2018-05-17 01:21:45 +03001016static int cpsw_rx_mq_poll(struct napi_struct *napi_rx, int budget)
Mugunthan V N32a74322015-08-04 16:06:20 +05301017{
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001018 u32 ch_map;
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02001019 int num_rx, cur_budget, ch;
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +03001020 struct cpsw_common *cpsw = napi_to_cpsw(napi_rx);
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02001021 struct cpsw_vector *rxv;
Mugunthan V N510a1e722013-02-17 22:19:20 +00001022
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001023 /* process every unprocessed channel */
1024 ch_map = cpdma_ctrl_rxchs_state(cpsw->dma);
Ivan Khoronzhuk342934a2016-11-29 17:00:50 +02001025 for (ch = 0, num_rx = 0; ch_map; ch_map >>= 1, ch++) {
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001026 if (!(ch_map & 0x01))
1027 continue;
1028
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02001029 rxv = &cpsw->rxv[ch];
1030 if (unlikely(rxv->budget > budget - num_rx))
1031 cur_budget = budget - num_rx;
1032 else
1033 cur_budget = rxv->budget;
1034
1035 num_rx += cpdma_chan_process(rxv->ch, cur_budget);
Ivan Khoronzhuk342934a2016-11-29 17:00:50 +02001036 if (num_rx >= budget)
1037 break;
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001038 }
1039
Mugunthan V N510a1e722013-02-17 22:19:20 +00001040 if (num_rx < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08001041 napi_complete_done(napi_rx, num_rx);
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001042 writel(0xff, &cpsw->wr_regs->rx_en);
Ivan Khoronzhuk9611d6d2018-05-17 01:21:45 +03001043 }
1044
1045 return num_rx;
1046}
1047
1048static int cpsw_rx_poll(struct napi_struct *napi_rx, int budget)
1049{
1050 struct cpsw_common *cpsw = napi_to_cpsw(napi_rx);
1051 int num_rx;
1052
1053 num_rx = cpdma_chan_process(cpsw->rxv[0].ch, budget);
1054 if (num_rx < budget) {
1055 napi_complete_done(napi_rx, num_rx);
1056 writel(0xff, &cpsw->wr_regs->rx_en);
1057 if (cpsw->rx_irq_disabled) {
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +03001058 cpsw->rx_irq_disabled = false;
1059 enable_irq(cpsw->irqs_table[0]);
Mugunthan V N7da11602015-08-12 15:22:53 +05301060 }
Mugunthan V N510a1e722013-02-17 22:19:20 +00001061 }
Mugunthan V Ndf828592012-03-18 20:17:54 +00001062
Mugunthan V Ndf828592012-03-18 20:17:54 +00001063 return num_rx;
1064}
1065
1066static inline void soft_reset(const char *module, void __iomem *reg)
1067{
1068 unsigned long timeout = jiffies + HZ;
1069
Grygorii Strashkodda5f5fe2017-11-30 18:21:11 -06001070 writel_relaxed(1, reg);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001071 do {
1072 cpu_relax();
Grygorii Strashkodda5f5fe2017-11-30 18:21:11 -06001073 } while ((readl_relaxed(reg) & 1) && time_after(timeout, jiffies));
Mugunthan V Ndf828592012-03-18 20:17:54 +00001074
Grygorii Strashkodda5f5fe2017-11-30 18:21:11 -06001075 WARN(readl_relaxed(reg) & 1, "failed to soft-reset %s\n", module);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001076}
1077
Mugunthan V Ndf828592012-03-18 20:17:54 +00001078static void cpsw_set_slave_mac(struct cpsw_slave *slave,
1079 struct cpsw_priv *priv)
1080{
Richard Cochran9750a3a2012-10-29 08:45:15 +00001081 slave_write(slave, mac_hi(priv->mac_addr), SA_HI);
1082 slave_write(slave, mac_lo(priv->mac_addr), SA_LO);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001083}
1084
1085static void _cpsw_adjust_link(struct cpsw_slave *slave,
1086 struct cpsw_priv *priv, bool *link)
1087{
1088 struct phy_device *phy = slave->phy;
1089 u32 mac_control = 0;
1090 u32 slave_port;
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001091 struct cpsw_common *cpsw = priv->cpsw;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001092
1093 if (!phy)
1094 return;
1095
Ivan Khoronzhuk6f1f5832016-08-10 02:22:34 +03001096 slave_port = cpsw_get_slave_port(slave->slave_num);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001097
1098 if (phy->link) {
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001099 mac_control = cpsw->data.mac_control;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001100
1101 /* enable forwarding */
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001102 cpsw_ale_control_set(cpsw->ale, slave_port,
Mugunthan V Ndf828592012-03-18 20:17:54 +00001103 ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
1104
1105 if (phy->speed == 1000)
1106 mac_control |= BIT(7); /* GIGABITEN */
1107 if (phy->duplex)
1108 mac_control |= BIT(0); /* FULLDUPLEXEN */
Daniel Mack342b7b72012-09-27 09:19:34 +00001109
1110 /* set speed_in input in case RMII mode is used in 100Mbps */
1111 if (phy->speed == 100)
1112 mac_control |= BIT(15);
SZ Lin (林上智)f9db5062018-03-16 00:56:01 +08001113 /* in band mode only works in 10Mbps RGMII mode */
1114 else if ((phy->speed == 10) && phy_interface_is_rgmii(phy))
Mugunthan V Na81d8762013-12-13 18:42:55 +05301115 mac_control |= BIT(18); /* In Band mode */
Daniel Mack342b7b72012-09-27 09:19:34 +00001116
Mugunthan V N1923d6e2014-09-08 22:54:02 +05301117 if (priv->rx_pause)
1118 mac_control |= BIT(3);
1119
1120 if (priv->tx_pause)
1121 mac_control |= BIT(4);
1122
Mugunthan V Ndf828592012-03-18 20:17:54 +00001123 *link = true;
1124 } else {
1125 mac_control = 0;
1126 /* disable forwarding */
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001127 cpsw_ale_control_set(cpsw->ale, slave_port,
Mugunthan V Ndf828592012-03-18 20:17:54 +00001128 ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
1129 }
1130
1131 if (mac_control != slave->mac_control) {
1132 phy_print_status(phy);
Grygorii Strashkodda5f5fe2017-11-30 18:21:11 -06001133 writel_relaxed(mac_control, &slave->sliver->mac_control);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001134 }
1135
1136 slave->mac_control = mac_control;
1137}
1138
Ivan Khoronzhuk0be01b82016-12-10 14:23:49 +02001139static int cpsw_get_common_speed(struct cpsw_common *cpsw)
1140{
1141 int i, speed;
1142
1143 for (i = 0, speed = 0; i < cpsw->data.slaves; i++)
1144 if (cpsw->slaves[i].phy && cpsw->slaves[i].phy->link)
1145 speed += cpsw->slaves[i].phy->speed;
1146
1147 return speed;
1148}
1149
1150static int cpsw_need_resplit(struct cpsw_common *cpsw)
1151{
1152 int i, rlim_ch_num;
1153 int speed, ch_rate;
1154
1155 /* re-split resources only in case speed was changed */
1156 speed = cpsw_get_common_speed(cpsw);
1157 if (speed == cpsw->speed || !speed)
1158 return 0;
1159
1160 cpsw->speed = speed;
1161
1162 for (i = 0, rlim_ch_num = 0; i < cpsw->tx_ch_num; i++) {
1163 ch_rate = cpdma_chan_get_rate(cpsw->txv[i].ch);
1164 if (!ch_rate)
1165 break;
1166
1167 rlim_ch_num++;
1168 }
1169
1170 /* cases not dependent on speed */
1171 if (!rlim_ch_num || rlim_ch_num == cpsw->tx_ch_num)
1172 return 0;
1173
1174 return 1;
1175}
1176
Mugunthan V Ndf828592012-03-18 20:17:54 +00001177static void cpsw_adjust_link(struct net_device *ndev)
1178{
1179 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk0be01b82016-12-10 14:23:49 +02001180 struct cpsw_common *cpsw = priv->cpsw;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001181 bool link = false;
1182
1183 for_each_slave(priv, _cpsw_adjust_link, priv, &link);
1184
1185 if (link) {
Ivan Khoronzhuk0be01b82016-12-10 14:23:49 +02001186 if (cpsw_need_resplit(cpsw))
1187 cpsw_split_res(ndev);
1188
Mugunthan V Ndf828592012-03-18 20:17:54 +00001189 netif_carrier_on(ndev);
1190 if (netif_running(ndev))
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001191 netif_tx_wake_all_queues(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001192 } else {
1193 netif_carrier_off(ndev);
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001194 netif_tx_stop_all_queues(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001195 }
1196}
1197
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001198static int cpsw_get_coalesce(struct net_device *ndev,
1199 struct ethtool_coalesce *coal)
1200{
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001201 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001202
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001203 coal->rx_coalesce_usecs = cpsw->coal_intvl;
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001204 return 0;
1205}
1206
1207static int cpsw_set_coalesce(struct net_device *ndev,
1208 struct ethtool_coalesce *coal)
1209{
1210 struct cpsw_priv *priv = netdev_priv(ndev);
1211 u32 int_ctrl;
1212 u32 num_interrupts = 0;
1213 u32 prescale = 0;
1214 u32 addnl_dvdr = 1;
1215 u32 coal_intvl = 0;
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001216 struct cpsw_common *cpsw = priv->cpsw;
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001217
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001218 coal_intvl = coal->rx_coalesce_usecs;
1219
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001220 int_ctrl = readl(&cpsw->wr_regs->int_control);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001221 prescale = cpsw->bus_freq_mhz * 4;
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001222
Mugunthan V Na84bc2a2014-07-15 20:26:53 +05301223 if (!coal->rx_coalesce_usecs) {
1224 int_ctrl &= ~(CPSW_INTPRESCALE_MASK | CPSW_INTPACEEN);
1225 goto update_return;
1226 }
1227
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001228 if (coal_intvl < CPSW_CMINTMIN_INTVL)
1229 coal_intvl = CPSW_CMINTMIN_INTVL;
1230
1231 if (coal_intvl > CPSW_CMINTMAX_INTVL) {
1232 /* Interrupt pacer works with 4us Pulse, we can
1233 * throttle further by dilating the 4us pulse.
1234 */
1235 addnl_dvdr = CPSW_INTPRESCALE_MASK / prescale;
1236
1237 if (addnl_dvdr > 1) {
1238 prescale *= addnl_dvdr;
1239 if (coal_intvl > (CPSW_CMINTMAX_INTVL * addnl_dvdr))
1240 coal_intvl = (CPSW_CMINTMAX_INTVL
1241 * addnl_dvdr);
1242 } else {
1243 addnl_dvdr = 1;
1244 coal_intvl = CPSW_CMINTMAX_INTVL;
1245 }
1246 }
1247
1248 num_interrupts = (1000 * addnl_dvdr) / coal_intvl;
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001249 writel(num_interrupts, &cpsw->wr_regs->rx_imax);
1250 writel(num_interrupts, &cpsw->wr_regs->tx_imax);
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001251
1252 int_ctrl |= CPSW_INTPACEEN;
1253 int_ctrl &= (~CPSW_INTPRESCALE_MASK);
1254 int_ctrl |= (prescale & CPSW_INTPRESCALE_MASK);
Mugunthan V Na84bc2a2014-07-15 20:26:53 +05301255
1256update_return:
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001257 writel(int_ctrl, &cpsw->wr_regs->int_control);
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001258
1259 cpsw_notice(priv, timer, "Set coalesce to %d usecs.\n", coal_intvl);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001260 cpsw->coal_intvl = coal_intvl;
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001261
1262 return 0;
1263}
1264
Mugunthan V Nd9718542013-07-23 15:38:17 +05301265static int cpsw_get_sset_count(struct net_device *ndev, int sset)
1266{
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001267 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1268
Mugunthan V Nd9718542013-07-23 15:38:17 +05301269 switch (sset) {
1270 case ETH_SS_STATS:
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001271 return (CPSW_STATS_COMMON_LEN +
1272 (cpsw->rx_ch_num + cpsw->tx_ch_num) *
1273 CPSW_STATS_CH_LEN);
Mugunthan V Nd9718542013-07-23 15:38:17 +05301274 default:
1275 return -EOPNOTSUPP;
1276 }
1277}
1278
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001279static void cpsw_add_ch_strings(u8 **p, int ch_num, int rx_dir)
1280{
1281 int ch_stats_len;
1282 int line;
1283 int i;
1284
1285 ch_stats_len = CPSW_STATS_CH_LEN * ch_num;
1286 for (i = 0; i < ch_stats_len; i++) {
1287 line = i % CPSW_STATS_CH_LEN;
1288 snprintf(*p, ETH_GSTRING_LEN,
Florian Fainellibf2ce3f2018-05-21 11:45:53 -07001289 "%s DMA chan %ld: %s", rx_dir ? "Rx" : "Tx",
1290 (long)(i / CPSW_STATS_CH_LEN),
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001291 cpsw_gstrings_ch_stats[line].stat_string);
1292 *p += ETH_GSTRING_LEN;
1293 }
1294}
1295
Mugunthan V Nd9718542013-07-23 15:38:17 +05301296static void cpsw_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
1297{
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001298 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Mugunthan V Nd9718542013-07-23 15:38:17 +05301299 u8 *p = data;
1300 int i;
1301
1302 switch (stringset) {
1303 case ETH_SS_STATS:
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001304 for (i = 0; i < CPSW_STATS_COMMON_LEN; i++) {
Mugunthan V Nd9718542013-07-23 15:38:17 +05301305 memcpy(p, cpsw_gstrings_stats[i].stat_string,
1306 ETH_GSTRING_LEN);
1307 p += ETH_GSTRING_LEN;
1308 }
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001309
1310 cpsw_add_ch_strings(&p, cpsw->rx_ch_num, 1);
1311 cpsw_add_ch_strings(&p, cpsw->tx_ch_num, 0);
Mugunthan V Nd9718542013-07-23 15:38:17 +05301312 break;
1313 }
1314}
1315
1316static void cpsw_get_ethtool_stats(struct net_device *ndev,
1317 struct ethtool_stats *stats, u64 *data)
1318{
Mugunthan V Nd9718542013-07-23 15:38:17 +05301319 u8 *p;
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001320 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001321 struct cpdma_chan_stats ch_stats;
1322 int i, l, ch;
Mugunthan V Nd9718542013-07-23 15:38:17 +05301323
1324 /* Collect Davinci CPDMA stats for Rx and Tx Channel */
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001325 for (l = 0; l < CPSW_STATS_COMMON_LEN; l++)
1326 data[l] = readl(cpsw->hw_stats +
1327 cpsw_gstrings_stats[l].stat_offset);
Mugunthan V Nd9718542013-07-23 15:38:17 +05301328
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001329 for (ch = 0; ch < cpsw->rx_ch_num; ch++) {
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02001330 cpdma_chan_get_stats(cpsw->rxv[ch].ch, &ch_stats);
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001331 for (i = 0; i < CPSW_STATS_CH_LEN; i++, l++) {
1332 p = (u8 *)&ch_stats +
1333 cpsw_gstrings_ch_stats[i].stat_offset;
1334 data[l] = *(u32 *)p;
1335 }
1336 }
Mugunthan V Nd9718542013-07-23 15:38:17 +05301337
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001338 for (ch = 0; ch < cpsw->tx_ch_num; ch++) {
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02001339 cpdma_chan_get_stats(cpsw->txv[ch].ch, &ch_stats);
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001340 for (i = 0; i < CPSW_STATS_CH_LEN; i++, l++) {
1341 p = (u8 *)&ch_stats +
1342 cpsw_gstrings_ch_stats[i].stat_offset;
1343 data[l] = *(u32 *)p;
Mugunthan V Nd9718542013-07-23 15:38:17 +05301344 }
1345 }
1346}
1347
Ivan Khoronzhuk27e9e102016-08-10 02:22:32 +03001348static inline int cpsw_tx_packet_submit(struct cpsw_priv *priv,
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001349 struct sk_buff *skb,
1350 struct cpdma_chan *txch)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001351{
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001352 struct cpsw_common *cpsw = priv->cpsw;
1353
Ivan Khoronzhuk98fdd852017-06-27 16:58:51 +03001354 skb_tx_timestamp(skb);
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001355 return cpdma_chan_submit(txch, skb, skb->data, skb->len,
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001356 priv->emac_port + cpsw->data.dual_emac);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001357}
1358
1359static inline void cpsw_add_dual_emac_def_ale_entries(
1360 struct cpsw_priv *priv, struct cpsw_slave *slave,
1361 u32 slave_port)
1362{
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001363 struct cpsw_common *cpsw = priv->cpsw;
Grygorii Strashko71a2cbb2016-04-07 15:16:44 +03001364 u32 port_mask = 1 << slave_port | ALE_PORT_HOST;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001365
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001366 if (cpsw->version == CPSW_VERSION_1)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001367 slave_write(slave, slave->port_vlan, CPSW1_PORT_VLAN);
1368 else
1369 slave_write(slave, slave->port_vlan, CPSW2_PORT_VLAN);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001370 cpsw_ale_add_vlan(cpsw->ale, slave->port_vlan, port_mask,
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001371 port_mask, port_mask, 0);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001372 cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001373 port_mask, ALE_VLAN, slave->port_vlan, 0);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001374 cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr,
1375 HOST_PORT_NUM, ALE_VLAN |
1376 ALE_SECURE, slave->port_vlan);
Grygorii Strashko5e5add12018-05-01 12:41:22 -05001377 cpsw_ale_control_set(cpsw->ale, slave_port,
1378 ALE_PORT_DROP_UNKNOWN_VLAN, 1);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001379}
1380
Daniel Mack1e7a2e22013-11-15 08:29:16 +01001381static void soft_reset_slave(struct cpsw_slave *slave)
Mugunthan V Ndf828592012-03-18 20:17:54 +00001382{
1383 char name[32];
Daniel Mack1e7a2e22013-11-15 08:29:16 +01001384
1385 snprintf(name, sizeof(name), "slave-%d", slave->slave_num);
1386 soft_reset(name, &slave->sliver->soft_reset);
1387}
1388
1389static void cpsw_slave_open(struct cpsw_slave *slave, struct cpsw_priv *priv)
1390{
Mugunthan V Ndf828592012-03-18 20:17:54 +00001391 u32 slave_port;
Sekhar Nori30c57f02017-04-03 17:34:28 +05301392 struct phy_device *phy;
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03001393 struct cpsw_common *cpsw = priv->cpsw;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001394
Daniel Mack1e7a2e22013-11-15 08:29:16 +01001395 soft_reset_slave(slave);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001396
1397 /* setup priority mapping */
Grygorii Strashkodda5f5fe2017-11-30 18:21:11 -06001398 writel_relaxed(RX_PRIORITY_MAPPING, &slave->sliver->rx_pri_map);
Richard Cochran9750a3a2012-10-29 08:45:15 +00001399
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001400 switch (cpsw->version) {
Richard Cochran9750a3a2012-10-29 08:45:15 +00001401 case CPSW_VERSION_1:
1402 slave_write(slave, TX_PRIORITY_MAPPING, CPSW1_TX_PRI_MAP);
Grygorii Strashko48f5bcc2017-05-08 14:21:21 -05001403 /* Increase RX FIFO size to 5 for supporting fullduplex
1404 * flow control mode
1405 */
1406 slave_write(slave,
1407 (CPSW_MAX_BLKS_TX << CPSW_MAX_BLKS_TX_SHIFT) |
1408 CPSW_MAX_BLKS_RX, CPSW1_MAX_BLKS);
Richard Cochran9750a3a2012-10-29 08:45:15 +00001409 break;
1410 case CPSW_VERSION_2:
Mugunthan V Nc193f362013-08-05 17:30:05 +05301411 case CPSW_VERSION_3:
Mugunthan V N926489b2013-08-12 17:11:15 +05301412 case CPSW_VERSION_4:
Richard Cochran9750a3a2012-10-29 08:45:15 +00001413 slave_write(slave, TX_PRIORITY_MAPPING, CPSW2_TX_PRI_MAP);
Grygorii Strashko48f5bcc2017-05-08 14:21:21 -05001414 /* Increase RX FIFO size to 5 for supporting fullduplex
1415 * flow control mode
1416 */
1417 slave_write(slave,
1418 (CPSW_MAX_BLKS_TX << CPSW_MAX_BLKS_TX_SHIFT) |
1419 CPSW_MAX_BLKS_RX, CPSW2_MAX_BLKS);
Richard Cochran9750a3a2012-10-29 08:45:15 +00001420 break;
1421 }
Mugunthan V Ndf828592012-03-18 20:17:54 +00001422
1423 /* setup max packet size, and mac address */
Grygorii Strashkodda5f5fe2017-11-30 18:21:11 -06001424 writel_relaxed(cpsw->rx_packet_max, &slave->sliver->rx_maxlen);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001425 cpsw_set_slave_mac(slave, priv);
1426
1427 slave->mac_control = 0; /* no link yet */
1428
Ivan Khoronzhuk6f1f5832016-08-10 02:22:34 +03001429 slave_port = cpsw_get_slave_port(slave->slave_num);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001430
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001431 if (cpsw->data.dual_emac)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001432 cpsw_add_dual_emac_def_ale_entries(priv, slave, slave_port);
1433 else
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001434 cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001435 1 << slave_port, 0, 0, ALE_MCAST_FWD_2);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001436
David Rivshind733f7542016-04-27 21:32:31 -04001437 if (slave->data->phy_node) {
Sekhar Nori30c57f02017-04-03 17:34:28 +05301438 phy = of_phy_connect(priv->ndev, slave->data->phy_node,
Heiko Schocher9e42f712015-10-17 06:04:35 +02001439 &cpsw_adjust_link, 0, slave->data->phy_if);
Sekhar Nori30c57f02017-04-03 17:34:28 +05301440 if (!phy) {
Rob Herringf7ce9102017-07-18 16:43:19 -05001441 dev_err(priv->dev, "phy \"%pOF\" not found on slave %d\n",
1442 slave->data->phy_node,
David Rivshind733f7542016-04-27 21:32:31 -04001443 slave->slave_num);
1444 return;
1445 }
1446 } else {
Sekhar Nori30c57f02017-04-03 17:34:28 +05301447 phy = phy_connect(priv->ndev, slave->data->phy_id,
Florian Fainellif9a8f832013-01-14 00:52:52 +00001448 &cpsw_adjust_link, slave->data->phy_if);
Sekhar Nori30c57f02017-04-03 17:34:28 +05301449 if (IS_ERR(phy)) {
David Rivshind733f7542016-04-27 21:32:31 -04001450 dev_err(priv->dev,
1451 "phy \"%s\" not found on slave %d, err %ld\n",
1452 slave->data->phy_id, slave->slave_num,
Sekhar Nori30c57f02017-04-03 17:34:28 +05301453 PTR_ERR(phy));
David Rivshind733f7542016-04-27 21:32:31 -04001454 return;
1455 }
Mugunthan V Ndf828592012-03-18 20:17:54 +00001456 }
David Rivshind733f7542016-04-27 21:32:31 -04001457
Sekhar Nori30c57f02017-04-03 17:34:28 +05301458 slave->phy = phy;
1459
David Rivshind733f7542016-04-27 21:32:31 -04001460 phy_attached_info(slave->phy);
1461
1462 phy_start(slave->phy);
1463
1464 /* Configure GMII_SEL register */
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001465 cpsw_phy_sel(cpsw->dev, slave->phy->interface, slave->slave_num);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001466}
1467
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001468static inline void cpsw_add_default_vlan(struct cpsw_priv *priv)
1469{
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001470 struct cpsw_common *cpsw = priv->cpsw;
1471 const int vlan = cpsw->data.default_vlan;
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001472 u32 reg;
1473 int i;
Lennart Sorensen1e5c4bc2014-10-31 13:38:52 -04001474 int unreg_mcast_mask;
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001475
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001476 reg = (cpsw->version == CPSW_VERSION_1) ? CPSW1_PORT_VLAN :
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001477 CPSW2_PORT_VLAN;
1478
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001479 writel(vlan, &cpsw->host_port_regs->port_vlan);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001480
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001481 for (i = 0; i < cpsw->data.slaves; i++)
1482 slave_write(cpsw->slaves + i, vlan, reg);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001483
Lennart Sorensen1e5c4bc2014-10-31 13:38:52 -04001484 if (priv->ndev->flags & IFF_ALLMULTI)
1485 unreg_mcast_mask = ALE_ALL_PORTS;
1486 else
1487 unreg_mcast_mask = ALE_PORT_1 | ALE_PORT_2;
1488
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001489 cpsw_ale_add_vlan(cpsw->ale, vlan, ALE_ALL_PORTS,
Grygorii Strashko61f1cef2016-04-07 15:16:43 +03001490 ALE_ALL_PORTS, ALE_ALL_PORTS,
1491 unreg_mcast_mask);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001492}
1493
Mugunthan V Ndf828592012-03-18 20:17:54 +00001494static void cpsw_init_host_port(struct cpsw_priv *priv)
1495{
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001496 u32 fifo_mode;
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001497 u32 control_reg;
1498 struct cpsw_common *cpsw = priv->cpsw;
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001499
Mugunthan V Ndf828592012-03-18 20:17:54 +00001500 /* soft reset the controller and initialize ale */
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001501 soft_reset("cpsw", &cpsw->regs->soft_reset);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001502 cpsw_ale_start(cpsw->ale);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001503
1504 /* switch to vlan unaware mode */
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001505 cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_VLAN_AWARE,
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001506 CPSW_ALE_VLAN_AWARE);
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001507 control_reg = readl(&cpsw->regs->control);
Grygorii Strashkoa3a41d22018-03-15 15:15:50 -05001508 control_reg |= CPSW_VLAN_AWARE | CPSW_RX_VLAN_ENCAP;
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001509 writel(control_reg, &cpsw->regs->control);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001510 fifo_mode = (cpsw->data.dual_emac) ? CPSW_FIFO_DUAL_MAC_MODE :
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001511 CPSW_FIFO_NORMAL_MODE;
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001512 writel(fifo_mode, &cpsw->host_port_regs->tx_in_ctl);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001513
1514 /* setup host port priority mapping */
Grygorii Strashkodda5f5fe2017-11-30 18:21:11 -06001515 writel_relaxed(CPDMA_TX_PRIORITY_MAP,
1516 &cpsw->host_port_regs->cpdma_tx_pri_map);
1517 writel_relaxed(0, &cpsw->host_port_regs->cpdma_rx_chan_map);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001518
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001519 cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM,
Mugunthan V Ndf828592012-03-18 20:17:54 +00001520 ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
1521
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001522 if (!cpsw->data.dual_emac) {
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001523 cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr, HOST_PORT_NUM,
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001524 0, 0);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001525 cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
Grygorii Strashko71a2cbb2016-04-07 15:16:44 +03001526 ALE_PORT_HOST, 0, 0, ALE_MCAST_FWD_2);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001527 }
Mugunthan V Ndf828592012-03-18 20:17:54 +00001528}
1529
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +03001530static int cpsw_fill_rx_channels(struct cpsw_priv *priv)
1531{
1532 struct cpsw_common *cpsw = priv->cpsw;
1533 struct sk_buff *skb;
1534 int ch_buf_num;
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001535 int ch, i, ret;
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +03001536
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001537 for (ch = 0; ch < cpsw->rx_ch_num; ch++) {
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02001538 ch_buf_num = cpdma_chan_get_rx_buf_num(cpsw->rxv[ch].ch);
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001539 for (i = 0; i < ch_buf_num; i++) {
1540 skb = __netdev_alloc_skb_ip_align(priv->ndev,
1541 cpsw->rx_packet_max,
1542 GFP_KERNEL);
1543 if (!skb) {
1544 cpsw_err(priv, ifup, "cannot allocate skb\n");
1545 return -ENOMEM;
1546 }
1547
1548 skb_set_queue_mapping(skb, ch);
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02001549 ret = cpdma_chan_submit(cpsw->rxv[ch].ch, skb,
1550 skb->data, skb_tailroom(skb),
1551 0);
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001552 if (ret < 0) {
1553 cpsw_err(priv, ifup,
1554 "cannot submit skb to channel %d rx, error %d\n",
1555 ch, ret);
1556 kfree_skb(skb);
1557 return ret;
1558 }
1559 kmemleak_not_leak(skb);
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +03001560 }
1561
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001562 cpsw_info(priv, ifup, "ch %d rx, submitted %d descriptors\n",
1563 ch, ch_buf_num);
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +03001564 }
1565
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001566 return 0;
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +03001567}
1568
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001569static void cpsw_slave_stop(struct cpsw_slave *slave, struct cpsw_common *cpsw)
Sebastian Siewioraacebbf2013-04-23 07:31:36 +00001570{
Schuyler Patton3995d262014-03-03 16:19:06 +05301571 u32 slave_port;
1572
Ivan Khoronzhuk6f1f5832016-08-10 02:22:34 +03001573 slave_port = cpsw_get_slave_port(slave->slave_num);
Schuyler Patton3995d262014-03-03 16:19:06 +05301574
Sebastian Siewioraacebbf2013-04-23 07:31:36 +00001575 if (!slave->phy)
1576 return;
1577 phy_stop(slave->phy);
1578 phy_disconnect(slave->phy);
1579 slave->phy = NULL;
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001580 cpsw_ale_control_set(cpsw->ale, slave_port,
Schuyler Patton3995d262014-03-03 16:19:06 +05301581 ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
Grygorii Strashko1f95ba02016-06-24 21:23:41 +03001582 soft_reset_slave(slave);
Sebastian Siewioraacebbf2013-04-23 07:31:36 +00001583}
1584
Ivan Khoronzhuk7929a662018-07-24 00:26:31 +03001585static int cpsw_tc_to_fifo(int tc, int num_tc)
1586{
1587 if (tc == num_tc - 1)
1588 return 0;
1589
1590 return CPSW_FIFO_SHAPERS_NUM - tc;
1591}
1592
Mugunthan V Ndf828592012-03-18 20:17:54 +00001593static int cpsw_ndo_open(struct net_device *ndev)
1594{
1595 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03001596 struct cpsw_common *cpsw = priv->cpsw;
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +03001597 int ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001598 u32 reg;
1599
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001600 ret = pm_runtime_get_sync(cpsw->dev);
Grygorii Strashko108a6532016-06-24 21:23:42 +03001601 if (ret < 0) {
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001602 pm_runtime_put_noidle(cpsw->dev);
Grygorii Strashko108a6532016-06-24 21:23:42 +03001603 return ret;
1604 }
Grygorii Strashko3fa88c52016-04-19 21:09:49 +03001605
Mugunthan V Ndf828592012-03-18 20:17:54 +00001606 netif_carrier_off(ndev);
1607
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001608 /* Notify the stack of the actual queue counts. */
1609 ret = netif_set_real_num_tx_queues(ndev, cpsw->tx_ch_num);
1610 if (ret) {
1611 dev_err(priv->dev, "cannot set real number of tx queues\n");
1612 goto err_cleanup;
1613 }
1614
1615 ret = netif_set_real_num_rx_queues(ndev, cpsw->rx_ch_num);
1616 if (ret) {
1617 dev_err(priv->dev, "cannot set real number of rx queues\n");
1618 goto err_cleanup;
1619 }
1620
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001621 reg = cpsw->version;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001622
1623 dev_info(priv->dev, "initializing cpsw version %d.%d (%d)\n",
1624 CPSW_MAJOR_VERSION(reg), CPSW_MINOR_VERSION(reg),
1625 CPSW_RTL_VERSION(reg));
1626
Ivan Khoronzhukd5bc1612017-02-14 16:02:36 +02001627 /* Initialize host and slave ports */
1628 if (!cpsw->usage_count)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001629 cpsw_init_host_port(priv);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001630 for_each_slave(priv, cpsw_slave_open, priv);
1631
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001632 /* Add default VLAN */
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001633 if (!cpsw->data.dual_emac)
Mugunthan V Ne6afea02014-06-18 17:21:48 +05301634 cpsw_add_default_vlan(priv);
1635 else
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001636 cpsw_ale_add_vlan(cpsw->ale, cpsw->data.default_vlan,
Grygorii Strashko61f1cef2016-04-07 15:16:43 +03001637 ALE_ALL_PORTS, ALE_ALL_PORTS, 0, 0);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001638
Ivan Khoronzhukd5bc1612017-02-14 16:02:36 +02001639 /* initialize shared resources for every ndev */
1640 if (!cpsw->usage_count) {
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001641 /* disable priority elevation */
Grygorii Strashkodda5f5fe2017-11-30 18:21:11 -06001642 writel_relaxed(0, &cpsw->regs->ptype);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001643
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001644 /* enable statistics collection only on all ports */
Grygorii Strashkodda5f5fe2017-11-30 18:21:11 -06001645 writel_relaxed(0x7, &cpsw->regs->stat_port_en);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001646
Mugunthan V N1923d6e2014-09-08 22:54:02 +05301647 /* Enable internal fifo flow control */
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001648 writel(0x7, &cpsw->regs->flow_control);
Mugunthan V N1923d6e2014-09-08 22:54:02 +05301649
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +03001650 napi_enable(&cpsw->napi_rx);
1651 napi_enable(&cpsw->napi_tx);
Mugunthan V Nd354eb82015-08-04 16:06:19 +05301652
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +03001653 if (cpsw->tx_irq_disabled) {
1654 cpsw->tx_irq_disabled = false;
1655 enable_irq(cpsw->irqs_table[1]);
Mugunthan V N7da11602015-08-12 15:22:53 +05301656 }
1657
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +03001658 if (cpsw->rx_irq_disabled) {
1659 cpsw->rx_irq_disabled = false;
1660 enable_irq(cpsw->irqs_table[0]);
Mugunthan V N7da11602015-08-12 15:22:53 +05301661 }
1662
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +03001663 ret = cpsw_fill_rx_channels(priv);
1664 if (ret < 0)
1665 goto err_cleanup;
Mugunthan V Nf280e892013-12-11 22:09:05 -06001666
Grygorii Strashko8a2c9a52016-12-06 18:00:41 -06001667 if (cpts_register(cpsw->cpts))
Mugunthan V Nf280e892013-12-11 22:09:05 -06001668 dev_err(priv->dev, "error registering cpts device\n");
1669
Mugunthan V Ndf828592012-03-18 20:17:54 +00001670 }
Mugunthan V Ndf828592012-03-18 20:17:54 +00001671
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001672 /* Enable Interrupt pacing if configured */
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001673 if (cpsw->coal_intvl != 0) {
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001674 struct ethtool_coalesce coal;
1675
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001676 coal.rx_coalesce_usecs = cpsw->coal_intvl;
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001677 cpsw_set_coalesce(ndev, &coal);
1678 }
1679
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001680 cpdma_ctlr_start(cpsw->dma);
1681 cpsw_intr_enable(cpsw);
Ivan Khoronzhukd5bc1612017-02-14 16:02:36 +02001682 cpsw->usage_count++;
Mugunthan V Nf63a9752014-04-10 14:23:24 +05301683
Mugunthan V Ndf828592012-03-18 20:17:54 +00001684 return 0;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001685
Sebastian Siewioraacebbf2013-04-23 07:31:36 +00001686err_cleanup:
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001687 cpdma_ctlr_stop(cpsw->dma);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001688 for_each_slave(priv, cpsw_slave_stop, cpsw);
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001689 pm_runtime_put_sync(cpsw->dev);
Sebastian Siewioraacebbf2013-04-23 07:31:36 +00001690 netif_carrier_off(priv->ndev);
1691 return ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001692}
1693
1694static int cpsw_ndo_stop(struct net_device *ndev)
1695{
1696 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03001697 struct cpsw_common *cpsw = priv->cpsw;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001698
1699 cpsw_info(priv, ifdown, "shutting down cpsw device\n");
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001700 netif_tx_stop_all_queues(priv->ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001701 netif_carrier_off(priv->ndev);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001702
Ivan Khoronzhukd5bc1612017-02-14 16:02:36 +02001703 if (cpsw->usage_count <= 1) {
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +03001704 napi_disable(&cpsw->napi_rx);
1705 napi_disable(&cpsw->napi_tx);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001706 cpts_unregister(cpsw->cpts);
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001707 cpsw_intr_disable(cpsw);
1708 cpdma_ctlr_stop(cpsw->dma);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001709 cpsw_ale_stop(cpsw->ale);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001710 }
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001711 for_each_slave(priv, cpsw_slave_stop, cpsw);
Ivan Khoronzhuk0be01b82016-12-10 14:23:49 +02001712
1713 if (cpsw_need_resplit(cpsw))
1714 cpsw_split_res(ndev);
1715
Ivan Khoronzhukd5bc1612017-02-14 16:02:36 +02001716 cpsw->usage_count--;
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001717 pm_runtime_put_sync(cpsw->dev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001718 return 0;
1719}
1720
1721static netdev_tx_t cpsw_ndo_start_xmit(struct sk_buff *skb,
1722 struct net_device *ndev)
1723{
1724 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001725 struct cpsw_common *cpsw = priv->cpsw;
Ivan Khoronzhukf44f8412017-06-27 16:58:52 +03001726 struct cpts *cpts = cpsw->cpts;
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001727 struct netdev_queue *txq;
1728 struct cpdma_chan *txch;
1729 int ret, q_idx;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001730
Mugunthan V Ndf828592012-03-18 20:17:54 +00001731 if (skb_padto(skb, CPSW_MIN_PACKET_SIZE)) {
1732 cpsw_err(priv, tx_err, "packet pad failed\n");
Tobias Klauser8dc43dd2014-03-10 13:12:23 +01001733 ndev->stats.tx_dropped++;
Ivan Khoronzhuk1bf96052017-02-11 03:49:57 +02001734 return NET_XMIT_DROP;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001735 }
1736
Mugunthan V N9232b162013-02-11 09:52:19 +00001737 if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
Ivan Khoronzhukf44f8412017-06-27 16:58:52 +03001738 cpts_is_tx_enabled(cpts) && cpts_can_timestamp(cpts, skb))
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001739 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
1740
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001741 q_idx = skb_get_queue_mapping(skb);
1742 if (q_idx >= cpsw->tx_ch_num)
1743 q_idx = q_idx % cpsw->tx_ch_num;
1744
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02001745 txch = cpsw->txv[q_idx].ch;
Grygorii Strashko62f94c22018-02-06 19:17:06 -06001746 txq = netdev_get_tx_queue(ndev, q_idx);
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001747 ret = cpsw_tx_packet_submit(priv, skb, txch);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001748 if (unlikely(ret != 0)) {
1749 cpsw_err(priv, tx_err, "desc submit failed\n");
1750 goto fail;
1751 }
1752
Mugunthan V Nfae50822013-01-17 06:31:34 +00001753 /* If there is no more tx desc left free then we need to
1754 * tell the kernel to stop sending us tx frames.
1755 */
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001756 if (unlikely(!cpdma_check_free_tx_desc(txch))) {
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001757 netif_tx_stop_queue(txq);
Grygorii Strashko62f94c22018-02-06 19:17:06 -06001758
1759 /* Barrier, so that stop_queue visible to other cpus */
1760 smp_mb__after_atomic();
1761
1762 if (cpdma_check_free_tx_desc(txch))
1763 netif_tx_wake_queue(txq);
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001764 }
Mugunthan V Nfae50822013-01-17 06:31:34 +00001765
Mugunthan V Ndf828592012-03-18 20:17:54 +00001766 return NETDEV_TX_OK;
1767fail:
Tobias Klauser8dc43dd2014-03-10 13:12:23 +01001768 ndev->stats.tx_dropped++;
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001769 netif_tx_stop_queue(txq);
Grygorii Strashko62f94c22018-02-06 19:17:06 -06001770
1771 /* Barrier, so that stop_queue visible to other cpus */
1772 smp_mb__after_atomic();
1773
1774 if (cpdma_check_free_tx_desc(txch))
1775 netif_tx_wake_queue(txq);
1776
Mugunthan V Ndf828592012-03-18 20:17:54 +00001777 return NETDEV_TX_BUSY;
1778}
1779
Grygorii Strashkoc8395d42016-12-06 18:00:34 -06001780#if IS_ENABLED(CONFIG_TI_CPTS)
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001781
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001782static void cpsw_hwtstamp_v1(struct cpsw_common *cpsw)
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001783{
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001784 struct cpsw_slave *slave = &cpsw->slaves[cpsw->data.active_slave];
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001785 u32 ts_en, seq_id;
1786
Grygorii Strashkob63ba582016-12-06 18:00:35 -06001787 if (!cpts_is_tx_enabled(cpsw->cpts) &&
1788 !cpts_is_rx_enabled(cpsw->cpts)) {
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001789 slave_write(slave, 0, CPSW1_TS_CTL);
1790 return;
1791 }
1792
1793 seq_id = (30 << CPSW_V1_SEQ_ID_OFS_SHIFT) | ETH_P_1588;
1794 ts_en = EVENT_MSG_BITS << CPSW_V1_MSG_TYPE_OFS;
1795
Grygorii Strashkob63ba582016-12-06 18:00:35 -06001796 if (cpts_is_tx_enabled(cpsw->cpts))
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001797 ts_en |= CPSW_V1_TS_TX_EN;
1798
Grygorii Strashkob63ba582016-12-06 18:00:35 -06001799 if (cpts_is_rx_enabled(cpsw->cpts))
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001800 ts_en |= CPSW_V1_TS_RX_EN;
1801
1802 slave_write(slave, ts_en, CPSW1_TS_CTL);
1803 slave_write(slave, seq_id, CPSW1_TS_SEQ_LTYPE);
1804}
1805
1806static void cpsw_hwtstamp_v2(struct cpsw_priv *priv)
1807{
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001808 struct cpsw_slave *slave;
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001809 struct cpsw_common *cpsw = priv->cpsw;
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001810 u32 ctrl, mtype;
1811
Ivan Khoronzhukcb7d78d02016-12-10 14:23:46 +02001812 slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001813
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001814 ctrl = slave_read(slave, CPSW2_CONTROL);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001815 switch (cpsw->version) {
George Cherian09c55372014-05-02 12:02:02 +05301816 case CPSW_VERSION_2:
1817 ctrl &= ~CTRL_V2_ALL_TS_MASK;
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001818
Grygorii Strashkob63ba582016-12-06 18:00:35 -06001819 if (cpts_is_tx_enabled(cpsw->cpts))
George Cherian09c55372014-05-02 12:02:02 +05301820 ctrl |= CTRL_V2_TX_TS_BITS;
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001821
Grygorii Strashkob63ba582016-12-06 18:00:35 -06001822 if (cpts_is_rx_enabled(cpsw->cpts))
George Cherian09c55372014-05-02 12:02:02 +05301823 ctrl |= CTRL_V2_RX_TS_BITS;
Richard Cochran26fe7eb2015-05-25 11:02:13 +02001824 break;
George Cherian09c55372014-05-02 12:02:02 +05301825 case CPSW_VERSION_3:
1826 default:
1827 ctrl &= ~CTRL_V3_ALL_TS_MASK;
1828
Grygorii Strashkob63ba582016-12-06 18:00:35 -06001829 if (cpts_is_tx_enabled(cpsw->cpts))
George Cherian09c55372014-05-02 12:02:02 +05301830 ctrl |= CTRL_V3_TX_TS_BITS;
1831
Grygorii Strashkob63ba582016-12-06 18:00:35 -06001832 if (cpts_is_rx_enabled(cpsw->cpts))
George Cherian09c55372014-05-02 12:02:02 +05301833 ctrl |= CTRL_V3_RX_TS_BITS;
Richard Cochran26fe7eb2015-05-25 11:02:13 +02001834 break;
George Cherian09c55372014-05-02 12:02:02 +05301835 }
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001836
1837 mtype = (30 << TS_SEQ_ID_OFFSET_SHIFT) | EVENT_MSG_BITS;
1838
1839 slave_write(slave, mtype, CPSW2_TS_SEQ_MTYPE);
1840 slave_write(slave, ctrl, CPSW2_CONTROL);
Grygorii Strashkodda5f5fe2017-11-30 18:21:11 -06001841 writel_relaxed(ETH_P_1588, &cpsw->regs->ts_ltype);
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001842}
1843
Ben Hutchingsa5b41452013-11-18 23:23:40 +00001844static int cpsw_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001845{
Mugunthan V N3177bf62012-11-27 07:53:40 +00001846 struct cpsw_priv *priv = netdev_priv(dev);
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001847 struct hwtstamp_config cfg;
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001848 struct cpsw_common *cpsw = priv->cpsw;
1849 struct cpts *cpts = cpsw->cpts;
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001850
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001851 if (cpsw->version != CPSW_VERSION_1 &&
1852 cpsw->version != CPSW_VERSION_2 &&
1853 cpsw->version != CPSW_VERSION_3)
Ben Hutchings2ee91e52013-11-14 00:47:36 +00001854 return -EOPNOTSUPP;
1855
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001856 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
1857 return -EFAULT;
1858
1859 /* reserved for future extensions */
1860 if (cfg.flags)
1861 return -EINVAL;
1862
Ben Hutchings2ee91e52013-11-14 00:47:36 +00001863 if (cfg.tx_type != HWTSTAMP_TX_OFF && cfg.tx_type != HWTSTAMP_TX_ON)
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001864 return -ERANGE;
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001865
1866 switch (cfg.rx_filter) {
1867 case HWTSTAMP_FILTER_NONE:
Grygorii Strashkob63ba582016-12-06 18:00:35 -06001868 cpts_rx_enable(cpts, 0);
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001869 break;
1870 case HWTSTAMP_FILTER_ALL:
Grygorii Strashkoe9523a52017-06-08 13:51:31 -05001871 case HWTSTAMP_FILTER_NTP_ALL:
1872 return -ERANGE;
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001873 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
1874 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
1875 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
Grygorii Strashkoe9523a52017-06-08 13:51:31 -05001876 cpts_rx_enable(cpts, HWTSTAMP_FILTER_PTP_V1_L4_EVENT);
1877 cfg.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
1878 break;
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001879 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
1880 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
1881 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
1882 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1883 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
1884 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
1885 case HWTSTAMP_FILTER_PTP_V2_EVENT:
1886 case HWTSTAMP_FILTER_PTP_V2_SYNC:
1887 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
Grygorii Strashkoe9523a52017-06-08 13:51:31 -05001888 cpts_rx_enable(cpts, HWTSTAMP_FILTER_PTP_V2_EVENT);
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001889 cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
1890 break;
1891 default:
1892 return -ERANGE;
1893 }
1894
Grygorii Strashkob63ba582016-12-06 18:00:35 -06001895 cpts_tx_enable(cpts, cfg.tx_type == HWTSTAMP_TX_ON);
Ben Hutchings2ee91e52013-11-14 00:47:36 +00001896
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001897 switch (cpsw->version) {
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001898 case CPSW_VERSION_1:
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001899 cpsw_hwtstamp_v1(cpsw);
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001900 break;
1901 case CPSW_VERSION_2:
George Cherianf7d403c2014-05-02 12:02:01 +05301902 case CPSW_VERSION_3:
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001903 cpsw_hwtstamp_v2(priv);
1904 break;
1905 default:
Ben Hutchings2ee91e52013-11-14 00:47:36 +00001906 WARN_ON(1);
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001907 }
1908
1909 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
1910}
1911
Ben Hutchingsa5b41452013-11-18 23:23:40 +00001912static int cpsw_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
1913{
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001914 struct cpsw_common *cpsw = ndev_to_cpsw(dev);
1915 struct cpts *cpts = cpsw->cpts;
Ben Hutchingsa5b41452013-11-18 23:23:40 +00001916 struct hwtstamp_config cfg;
1917
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001918 if (cpsw->version != CPSW_VERSION_1 &&
1919 cpsw->version != CPSW_VERSION_2 &&
1920 cpsw->version != CPSW_VERSION_3)
Ben Hutchingsa5b41452013-11-18 23:23:40 +00001921 return -EOPNOTSUPP;
1922
1923 cfg.flags = 0;
Grygorii Strashkob63ba582016-12-06 18:00:35 -06001924 cfg.tx_type = cpts_is_tx_enabled(cpts) ?
1925 HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF;
1926 cfg.rx_filter = (cpts_is_rx_enabled(cpts) ?
Grygorii Strashkoe9523a52017-06-08 13:51:31 -05001927 cpts->rx_enable : HWTSTAMP_FILTER_NONE);
Ben Hutchingsa5b41452013-11-18 23:23:40 +00001928
1929 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
1930}
Grygorii Strashkoc8395d42016-12-06 18:00:34 -06001931#else
1932static int cpsw_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
1933{
1934 return -EOPNOTSUPP;
1935}
Ben Hutchingsa5b41452013-11-18 23:23:40 +00001936
Grygorii Strashkoc8395d42016-12-06 18:00:34 -06001937static int cpsw_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
1938{
1939 return -EOPNOTSUPP;
1940}
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001941#endif /*CONFIG_TI_CPTS*/
1942
1943static int cpsw_ndo_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
1944{
Mugunthan V N11f2c982013-03-11 23:16:38 +00001945 struct cpsw_priv *priv = netdev_priv(dev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001946 struct cpsw_common *cpsw = priv->cpsw;
1947 int slave_no = cpsw_slave_index(cpsw, priv);
Mugunthan V N11f2c982013-03-11 23:16:38 +00001948
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001949 if (!netif_running(dev))
1950 return -EINVAL;
1951
Mugunthan V N11f2c982013-03-11 23:16:38 +00001952 switch (cmd) {
Mugunthan V N11f2c982013-03-11 23:16:38 +00001953 case SIOCSHWTSTAMP:
Ben Hutchingsa5b41452013-11-18 23:23:40 +00001954 return cpsw_hwtstamp_set(dev, req);
1955 case SIOCGHWTSTAMP:
1956 return cpsw_hwtstamp_get(dev, req);
Mugunthan V N11f2c982013-03-11 23:16:38 +00001957 }
1958
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001959 if (!cpsw->slaves[slave_no].phy)
Stefan Sørensenc1b59942014-02-16 14:54:25 +01001960 return -EOPNOTSUPP;
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001961 return phy_mii_ioctl(cpsw->slaves[slave_no].phy, req, cmd);
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001962}
1963
Mugunthan V Ndf828592012-03-18 20:17:54 +00001964static void cpsw_ndo_tx_timeout(struct net_device *ndev)
1965{
1966 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001967 struct cpsw_common *cpsw = priv->cpsw;
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001968 int ch;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001969
1970 cpsw_err(priv, tx_err, "transmit timeout, restarting dma\n");
Tobias Klauser8dc43dd2014-03-10 13:12:23 +01001971 ndev->stats.tx_errors++;
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001972 cpsw_intr_disable(cpsw);
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001973 for (ch = 0; ch < cpsw->tx_ch_num; ch++) {
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02001974 cpdma_chan_stop(cpsw->txv[ch].ch);
1975 cpdma_chan_start(cpsw->txv[ch].ch);
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001976 }
1977
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001978 cpsw_intr_enable(cpsw);
Grygorii Strashko75514b62017-03-31 18:41:23 -05001979 netif_trans_update(ndev);
1980 netif_tx_wake_all_queues(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001981}
1982
Mugunthan V Ndcfd8d52013-07-25 23:44:01 +05301983static int cpsw_ndo_set_mac_address(struct net_device *ndev, void *p)
1984{
1985 struct cpsw_priv *priv = netdev_priv(ndev);
1986 struct sockaddr *addr = (struct sockaddr *)p;
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03001987 struct cpsw_common *cpsw = priv->cpsw;
Mugunthan V Ndcfd8d52013-07-25 23:44:01 +05301988 int flags = 0;
1989 u16 vid = 0;
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001990 int ret;
Mugunthan V Ndcfd8d52013-07-25 23:44:01 +05301991
1992 if (!is_valid_ether_addr(addr->sa_data))
1993 return -EADDRNOTAVAIL;
1994
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001995 ret = pm_runtime_get_sync(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001996 if (ret < 0) {
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001997 pm_runtime_put_noidle(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001998 return ret;
1999 }
2000
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002001 if (cpsw->data.dual_emac) {
2002 vid = cpsw->slaves[priv->emac_port].port_vlan;
Mugunthan V Ndcfd8d52013-07-25 23:44:01 +05302003 flags = ALE_VLAN;
2004 }
2005
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002006 cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr, HOST_PORT_NUM,
Mugunthan V Ndcfd8d52013-07-25 23:44:01 +05302007 flags, vid);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002008 cpsw_ale_add_ucast(cpsw->ale, addr->sa_data, HOST_PORT_NUM,
Mugunthan V Ndcfd8d52013-07-25 23:44:01 +05302009 flags, vid);
2010
2011 memcpy(priv->mac_addr, addr->sa_data, ETH_ALEN);
2012 memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
2013 for_each_slave(priv, cpsw_set_slave_mac, priv);
2014
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002015 pm_runtime_put(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03002016
Mugunthan V Ndcfd8d52013-07-25 23:44:01 +05302017 return 0;
2018}
2019
Mugunthan V Ndf828592012-03-18 20:17:54 +00002020#ifdef CONFIG_NET_POLL_CONTROLLER
2021static void cpsw_ndo_poll_controller(struct net_device *ndev)
2022{
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +03002023 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00002024
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +03002025 cpsw_intr_disable(cpsw);
2026 cpsw_rx_interrupt(cpsw->irqs_table[0], cpsw);
2027 cpsw_tx_interrupt(cpsw->irqs_table[1], cpsw);
2028 cpsw_intr_enable(cpsw);
Mugunthan V Ndf828592012-03-18 20:17:54 +00002029}
2030#endif
2031
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00002032static inline int cpsw_add_vlan_ale_entry(struct cpsw_priv *priv,
2033 unsigned short vid)
2034{
2035 int ret;
Mugunthan V N9f6bd8f2015-01-15 14:59:28 +05302036 int unreg_mcast_mask = 0;
2037 u32 port_mask;
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002038 struct cpsw_common *cpsw = priv->cpsw;
Lennart Sorensen1e5c4bc2014-10-31 13:38:52 -04002039
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002040 if (cpsw->data.dual_emac) {
Mugunthan V N9f6bd8f2015-01-15 14:59:28 +05302041 port_mask = (1 << (priv->emac_port + 1)) | ALE_PORT_HOST;
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00002042
Mugunthan V N9f6bd8f2015-01-15 14:59:28 +05302043 if (priv->ndev->flags & IFF_ALLMULTI)
2044 unreg_mcast_mask = port_mask;
2045 } else {
2046 port_mask = ALE_ALL_PORTS;
2047
2048 if (priv->ndev->flags & IFF_ALLMULTI)
2049 unreg_mcast_mask = ALE_ALL_PORTS;
2050 else
2051 unreg_mcast_mask = ALE_PORT_1 | ALE_PORT_2;
2052 }
2053
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002054 ret = cpsw_ale_add_vlan(cpsw->ale, vid, port_mask, 0, port_mask,
Grygorii Strashko61f1cef2016-04-07 15:16:43 +03002055 unreg_mcast_mask);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00002056 if (ret != 0)
2057 return ret;
2058
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002059 ret = cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr,
Grygorii Strashko71a2cbb2016-04-07 15:16:44 +03002060 HOST_PORT_NUM, ALE_VLAN, vid);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00002061 if (ret != 0)
2062 goto clean_vid;
2063
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002064 ret = cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
Mugunthan V N9f6bd8f2015-01-15 14:59:28 +05302065 port_mask, ALE_VLAN, vid, 0);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00002066 if (ret != 0)
2067 goto clean_vlan_ucast;
2068 return 0;
2069
2070clean_vlan_ucast:
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002071 cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr,
Grygorii Strashko71a2cbb2016-04-07 15:16:44 +03002072 HOST_PORT_NUM, ALE_VLAN, vid);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00002073clean_vid:
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002074 cpsw_ale_del_vlan(cpsw->ale, vid, 0);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00002075 return ret;
2076}
2077
2078static int cpsw_ndo_vlan_rx_add_vid(struct net_device *ndev,
Patrick McHardy80d5c362013-04-19 02:04:28 +00002079 __be16 proto, u16 vid)
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00002080{
2081 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03002082 struct cpsw_common *cpsw = priv->cpsw;
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03002083 int ret;
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00002084
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002085 if (vid == cpsw->data.default_vlan)
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00002086 return 0;
2087
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002088 ret = pm_runtime_get_sync(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03002089 if (ret < 0) {
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002090 pm_runtime_put_noidle(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03002091 return ret;
2092 }
2093
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002094 if (cpsw->data.dual_emac) {
Mugunthan V N02a54162015-01-22 15:19:22 +05302095 /* In dual EMAC, reserved VLAN id should not be used for
2096 * creating VLAN interfaces as this can break the dual
2097 * EMAC port separation
2098 */
2099 int i;
2100
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002101 for (i = 0; i < cpsw->data.slaves; i++) {
2102 if (vid == cpsw->slaves[i].port_vlan)
Mugunthan V N02a54162015-01-22 15:19:22 +05302103 return -EINVAL;
2104 }
2105 }
2106
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00002107 dev_info(priv->dev, "Adding vlanid %d to vlan filter\n", vid);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03002108 ret = cpsw_add_vlan_ale_entry(priv, vid);
2109
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002110 pm_runtime_put(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03002111 return ret;
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00002112}
2113
2114static int cpsw_ndo_vlan_rx_kill_vid(struct net_device *ndev,
Patrick McHardy80d5c362013-04-19 02:04:28 +00002115 __be16 proto, u16 vid)
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00002116{
2117 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03002118 struct cpsw_common *cpsw = priv->cpsw;
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00002119 int ret;
2120
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002121 if (vid == cpsw->data.default_vlan)
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00002122 return 0;
2123
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002124 ret = pm_runtime_get_sync(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03002125 if (ret < 0) {
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002126 pm_runtime_put_noidle(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03002127 return ret;
2128 }
2129
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002130 if (cpsw->data.dual_emac) {
Mugunthan V N02a54162015-01-22 15:19:22 +05302131 int i;
2132
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002133 for (i = 0; i < cpsw->data.slaves; i++) {
2134 if (vid == cpsw->slaves[i].port_vlan)
Mugunthan V N02a54162015-01-22 15:19:22 +05302135 return -EINVAL;
2136 }
2137 }
2138
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00002139 dev_info(priv->dev, "removing vlanid %d from vlan filter\n", vid);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002140 ret = cpsw_ale_del_vlan(cpsw->ale, vid, 0);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00002141 if (ret != 0)
2142 return ret;
2143
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002144 ret = cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr,
Grygorii Strashko61f1cef2016-04-07 15:16:43 +03002145 HOST_PORT_NUM, ALE_VLAN, vid);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00002146 if (ret != 0)
2147 return ret;
2148
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002149 ret = cpsw_ale_del_mcast(cpsw->ale, priv->ndev->broadcast,
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03002150 0, ALE_VLAN, vid);
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002151 pm_runtime_put(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03002152 return ret;
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00002153}
2154
Ivan Khoronzhuk83fcad02016-11-29 17:00:49 +02002155static int cpsw_ndo_set_tx_maxrate(struct net_device *ndev, int queue, u32 rate)
2156{
2157 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk83fcad02016-11-29 17:00:49 +02002158 struct cpsw_common *cpsw = priv->cpsw;
Ivan Khoronzhuk52986a22016-12-10 14:23:50 +02002159 struct cpsw_slave *slave;
Ivan Khoronzhuk32b78d82016-12-10 14:23:48 +02002160 u32 min_rate;
Ivan Khoronzhuk83fcad02016-11-29 17:00:49 +02002161 u32 ch_rate;
Ivan Khoronzhuk52986a22016-12-10 14:23:50 +02002162 int i, ret;
Ivan Khoronzhuk83fcad02016-11-29 17:00:49 +02002163
2164 ch_rate = netdev_get_tx_queue(ndev, queue)->tx_maxrate;
2165 if (ch_rate == rate)
2166 return 0;
2167
Ivan Khoronzhuk32b78d82016-12-10 14:23:48 +02002168 ch_rate = rate * 1000;
2169 min_rate = cpdma_chan_get_min_rate(cpsw->dma);
2170 if ((ch_rate < min_rate && ch_rate)) {
2171 dev_err(priv->dev, "The channel rate cannot be less than %dMbps",
2172 min_rate);
Ivan Khoronzhuk83fcad02016-11-29 17:00:49 +02002173 return -EINVAL;
2174 }
2175
Ivan Khoronzhuk0be01b82016-12-10 14:23:49 +02002176 if (rate > cpsw->speed) {
Ivan Khoronzhuk32b78d82016-12-10 14:23:48 +02002177 dev_err(priv->dev, "The channel rate cannot be more than 2Gbps");
Ivan Khoronzhuk83fcad02016-11-29 17:00:49 +02002178 return -EINVAL;
2179 }
2180
2181 ret = pm_runtime_get_sync(cpsw->dev);
2182 if (ret < 0) {
2183 pm_runtime_put_noidle(cpsw->dev);
2184 return ret;
2185 }
2186
Ivan Khoronzhuk32b78d82016-12-10 14:23:48 +02002187 ret = cpdma_chan_set_rate(cpsw->txv[queue].ch, ch_rate);
Ivan Khoronzhuk83fcad02016-11-29 17:00:49 +02002188 pm_runtime_put(cpsw->dev);
Ivan Khoronzhuk32b78d82016-12-10 14:23:48 +02002189
2190 if (ret)
2191 return ret;
2192
Ivan Khoronzhuk52986a22016-12-10 14:23:50 +02002193 /* update rates for slaves tx queues */
2194 for (i = 0; i < cpsw->data.slaves; i++) {
2195 slave = &cpsw->slaves[i];
2196 if (!slave->ndev)
2197 continue;
2198
2199 netdev_get_tx_queue(slave->ndev, queue)->tx_maxrate = rate;
2200 }
2201
Ivan Khoronzhuk32b78d82016-12-10 14:23:48 +02002202 cpsw_split_res(ndev);
Ivan Khoronzhuk83fcad02016-11-29 17:00:49 +02002203 return ret;
2204}
2205
Ivan Khoronzhuk7929a662018-07-24 00:26:31 +03002206static int cpsw_set_mqprio(struct net_device *ndev, void *type_data)
2207{
2208 struct tc_mqprio_qopt_offload *mqprio = type_data;
2209 struct cpsw_priv *priv = netdev_priv(ndev);
2210 struct cpsw_common *cpsw = priv->cpsw;
2211 int fifo, num_tc, count, offset;
2212 struct cpsw_slave *slave;
2213 u32 tx_prio_map = 0;
2214 int i, tc, ret;
2215
2216 num_tc = mqprio->qopt.num_tc;
2217 if (num_tc > CPSW_TC_NUM)
2218 return -EINVAL;
2219
2220 if (mqprio->mode != TC_MQPRIO_MODE_DCB)
2221 return -EINVAL;
2222
2223 ret = pm_runtime_get_sync(cpsw->dev);
2224 if (ret < 0) {
2225 pm_runtime_put_noidle(cpsw->dev);
2226 return ret;
2227 }
2228
2229 if (num_tc) {
2230 for (i = 0; i < 8; i++) {
2231 tc = mqprio->qopt.prio_tc_map[i];
2232 fifo = cpsw_tc_to_fifo(tc, num_tc);
2233 tx_prio_map |= fifo << (4 * i);
2234 }
2235
2236 netdev_set_num_tc(ndev, num_tc);
2237 for (i = 0; i < num_tc; i++) {
2238 count = mqprio->qopt.count[i];
2239 offset = mqprio->qopt.offset[i];
2240 netdev_set_tc_queue(ndev, i, count, offset);
2241 }
2242 }
2243
2244 if (!mqprio->qopt.hw) {
2245 /* restore default configuration */
2246 netdev_reset_tc(ndev);
2247 tx_prio_map = TX_PRIORITY_MAPPING;
2248 }
2249
2250 priv->mqprio_hw = mqprio->qopt.hw;
2251
2252 offset = cpsw->version == CPSW_VERSION_1 ?
2253 CPSW1_TX_PRI_MAP : CPSW2_TX_PRI_MAP;
2254
2255 slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
2256 slave_write(slave, tx_prio_map, offset);
2257
2258 pm_runtime_put_sync(cpsw->dev);
2259
2260 return 0;
2261}
2262
2263static int cpsw_ndo_setup_tc(struct net_device *ndev, enum tc_setup_type type,
2264 void *type_data)
2265{
2266 switch (type) {
2267 case TC_SETUP_QDISC_MQPRIO:
2268 return cpsw_set_mqprio(ndev, type_data);
2269
2270 default:
2271 return -EOPNOTSUPP;
2272 }
2273}
2274
Mugunthan V Ndf828592012-03-18 20:17:54 +00002275static const struct net_device_ops cpsw_netdev_ops = {
2276 .ndo_open = cpsw_ndo_open,
2277 .ndo_stop = cpsw_ndo_stop,
2278 .ndo_start_xmit = cpsw_ndo_start_xmit,
Mugunthan V Ndcfd8d52013-07-25 23:44:01 +05302279 .ndo_set_mac_address = cpsw_ndo_set_mac_address,
Richard Cochran2e5b38a2012-10-29 08:45:20 +00002280 .ndo_do_ioctl = cpsw_ndo_ioctl,
Mugunthan V Ndf828592012-03-18 20:17:54 +00002281 .ndo_validate_addr = eth_validate_addr,
Mugunthan V Ndf828592012-03-18 20:17:54 +00002282 .ndo_tx_timeout = cpsw_ndo_tx_timeout,
Mugunthan V N5c50a852012-10-29 08:45:11 +00002283 .ndo_set_rx_mode = cpsw_ndo_set_rx_mode,
Ivan Khoronzhuk83fcad02016-11-29 17:00:49 +02002284 .ndo_set_tx_maxrate = cpsw_ndo_set_tx_maxrate,
Mugunthan V Ndf828592012-03-18 20:17:54 +00002285#ifdef CONFIG_NET_POLL_CONTROLLER
2286 .ndo_poll_controller = cpsw_ndo_poll_controller,
2287#endif
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00002288 .ndo_vlan_rx_add_vid = cpsw_ndo_vlan_rx_add_vid,
2289 .ndo_vlan_rx_kill_vid = cpsw_ndo_vlan_rx_kill_vid,
Ivan Khoronzhuk7929a662018-07-24 00:26:31 +03002290 .ndo_setup_tc = cpsw_ndo_setup_tc,
Mugunthan V Ndf828592012-03-18 20:17:54 +00002291};
2292
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05302293static int cpsw_get_regs_len(struct net_device *ndev)
2294{
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002295 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05302296
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002297 return cpsw->data.ale_entries * ALE_ENTRY_WORDS * sizeof(u32);
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05302298}
2299
2300static void cpsw_get_regs(struct net_device *ndev,
2301 struct ethtool_regs *regs, void *p)
2302{
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05302303 u32 *reg = p;
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002304 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05302305
2306 /* update CPSW IP version */
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002307 regs->version = cpsw->version;
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05302308
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002309 cpsw_ale_dump(cpsw->ale, reg);
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05302310}
2311
Mugunthan V Ndf828592012-03-18 20:17:54 +00002312static void cpsw_get_drvinfo(struct net_device *ndev,
2313 struct ethtool_drvinfo *info)
2314{
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03002315 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002316 struct platform_device *pdev = to_platform_device(cpsw->dev);
Jiri Pirko7826d432013-01-06 00:44:26 +00002317
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05302318 strlcpy(info->driver, "cpsw", sizeof(info->driver));
Jiri Pirko7826d432013-01-06 00:44:26 +00002319 strlcpy(info->version, "1.0", sizeof(info->version));
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002320 strlcpy(info->bus_info, pdev->name, sizeof(info->bus_info));
Mugunthan V Ndf828592012-03-18 20:17:54 +00002321}
2322
2323static u32 cpsw_get_msglevel(struct net_device *ndev)
2324{
2325 struct cpsw_priv *priv = netdev_priv(ndev);
2326 return priv->msg_enable;
2327}
2328
2329static void cpsw_set_msglevel(struct net_device *ndev, u32 value)
2330{
2331 struct cpsw_priv *priv = netdev_priv(ndev);
2332 priv->msg_enable = value;
2333}
2334
Grygorii Strashkoc8395d42016-12-06 18:00:34 -06002335#if IS_ENABLED(CONFIG_TI_CPTS)
Richard Cochran2e5b38a2012-10-29 08:45:20 +00002336static int cpsw_get_ts_info(struct net_device *ndev,
2337 struct ethtool_ts_info *info)
2338{
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002339 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Richard Cochran2e5b38a2012-10-29 08:45:20 +00002340
2341 info->so_timestamping =
2342 SOF_TIMESTAMPING_TX_HARDWARE |
2343 SOF_TIMESTAMPING_TX_SOFTWARE |
2344 SOF_TIMESTAMPING_RX_HARDWARE |
2345 SOF_TIMESTAMPING_RX_SOFTWARE |
2346 SOF_TIMESTAMPING_SOFTWARE |
2347 SOF_TIMESTAMPING_RAW_HARDWARE;
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002348 info->phc_index = cpsw->cpts->phc_index;
Richard Cochran2e5b38a2012-10-29 08:45:20 +00002349 info->tx_types =
2350 (1 << HWTSTAMP_TX_OFF) |
2351 (1 << HWTSTAMP_TX_ON);
2352 info->rx_filters =
2353 (1 << HWTSTAMP_FILTER_NONE) |
Grygorii Strashkoe9523a52017-06-08 13:51:31 -05002354 (1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) |
Richard Cochran2e5b38a2012-10-29 08:45:20 +00002355 (1 << HWTSTAMP_FILTER_PTP_V2_EVENT);
Grygorii Strashkoc8395d42016-12-06 18:00:34 -06002356 return 0;
2357}
Richard Cochran2e5b38a2012-10-29 08:45:20 +00002358#else
Grygorii Strashkoc8395d42016-12-06 18:00:34 -06002359static int cpsw_get_ts_info(struct net_device *ndev,
2360 struct ethtool_ts_info *info)
2361{
Richard Cochran2e5b38a2012-10-29 08:45:20 +00002362 info->so_timestamping =
2363 SOF_TIMESTAMPING_TX_SOFTWARE |
2364 SOF_TIMESTAMPING_RX_SOFTWARE |
2365 SOF_TIMESTAMPING_SOFTWARE;
2366 info->phc_index = -1;
2367 info->tx_types = 0;
2368 info->rx_filters = 0;
Richard Cochran2e5b38a2012-10-29 08:45:20 +00002369 return 0;
2370}
Grygorii Strashkoc8395d42016-12-06 18:00:34 -06002371#endif
Richard Cochran2e5b38a2012-10-29 08:45:20 +00002372
Philippe Reynes24798762016-10-08 17:46:15 +02002373static int cpsw_get_link_ksettings(struct net_device *ndev,
2374 struct ethtool_link_ksettings *ecmd)
Mugunthan V Nd3bb9c52013-03-11 23:16:36 +00002375{
2376 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002377 struct cpsw_common *cpsw = priv->cpsw;
2378 int slave_no = cpsw_slave_index(cpsw, priv);
Mugunthan V Nd3bb9c52013-03-11 23:16:36 +00002379
yuval.shaia@oracle.com55141742017-06-13 10:09:46 +03002380 if (!cpsw->slaves[slave_no].phy)
Mugunthan V Nd3bb9c52013-03-11 23:16:36 +00002381 return -EOPNOTSUPP;
yuval.shaia@oracle.com55141742017-06-13 10:09:46 +03002382
2383 phy_ethtool_ksettings_get(cpsw->slaves[slave_no].phy, ecmd);
2384 return 0;
Mugunthan V Nd3bb9c52013-03-11 23:16:36 +00002385}
2386
Philippe Reynes24798762016-10-08 17:46:15 +02002387static int cpsw_set_link_ksettings(struct net_device *ndev,
2388 const struct ethtool_link_ksettings *ecmd)
Mugunthan V Nd3bb9c52013-03-11 23:16:36 +00002389{
2390 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002391 struct cpsw_common *cpsw = priv->cpsw;
2392 int slave_no = cpsw_slave_index(cpsw, priv);
Mugunthan V Nd3bb9c52013-03-11 23:16:36 +00002393
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002394 if (cpsw->slaves[slave_no].phy)
Philippe Reynes24798762016-10-08 17:46:15 +02002395 return phy_ethtool_ksettings_set(cpsw->slaves[slave_no].phy,
2396 ecmd);
Mugunthan V Nd3bb9c52013-03-11 23:16:36 +00002397 else
2398 return -EOPNOTSUPP;
2399}
2400
Matus Ujhelyid8a64422013-08-20 07:59:38 +02002401static void cpsw_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2402{
2403 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002404 struct cpsw_common *cpsw = priv->cpsw;
2405 int slave_no = cpsw_slave_index(cpsw, priv);
Matus Ujhelyid8a64422013-08-20 07:59:38 +02002406
2407 wol->supported = 0;
2408 wol->wolopts = 0;
2409
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002410 if (cpsw->slaves[slave_no].phy)
2411 phy_ethtool_get_wol(cpsw->slaves[slave_no].phy, wol);
Matus Ujhelyid8a64422013-08-20 07:59:38 +02002412}
2413
2414static int cpsw_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2415{
2416 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002417 struct cpsw_common *cpsw = priv->cpsw;
2418 int slave_no = cpsw_slave_index(cpsw, priv);
Matus Ujhelyid8a64422013-08-20 07:59:38 +02002419
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002420 if (cpsw->slaves[slave_no].phy)
2421 return phy_ethtool_set_wol(cpsw->slaves[slave_no].phy, wol);
Matus Ujhelyid8a64422013-08-20 07:59:38 +02002422 else
2423 return -EOPNOTSUPP;
2424}
2425
Mugunthan V N1923d6e2014-09-08 22:54:02 +05302426static void cpsw_get_pauseparam(struct net_device *ndev,
2427 struct ethtool_pauseparam *pause)
2428{
2429 struct cpsw_priv *priv = netdev_priv(ndev);
2430
2431 pause->autoneg = AUTONEG_DISABLE;
2432 pause->rx_pause = priv->rx_pause ? true : false;
2433 pause->tx_pause = priv->tx_pause ? true : false;
2434}
2435
2436static int cpsw_set_pauseparam(struct net_device *ndev,
2437 struct ethtool_pauseparam *pause)
2438{
2439 struct cpsw_priv *priv = netdev_priv(ndev);
2440 bool link;
2441
2442 priv->rx_pause = pause->rx_pause ? true : false;
2443 priv->tx_pause = pause->tx_pause ? true : false;
2444
2445 for_each_slave(priv, _cpsw_adjust_link, priv, &link);
Mugunthan V N1923d6e2014-09-08 22:54:02 +05302446 return 0;
2447}
2448
Grygorii Strashko7898b1d2016-06-24 21:23:44 +03002449static int cpsw_ethtool_op_begin(struct net_device *ndev)
2450{
2451 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03002452 struct cpsw_common *cpsw = priv->cpsw;
Grygorii Strashko7898b1d2016-06-24 21:23:44 +03002453 int ret;
2454
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002455 ret = pm_runtime_get_sync(cpsw->dev);
Grygorii Strashko7898b1d2016-06-24 21:23:44 +03002456 if (ret < 0) {
2457 cpsw_err(priv, drv, "ethtool begin failed %d\n", ret);
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002458 pm_runtime_put_noidle(cpsw->dev);
Grygorii Strashko7898b1d2016-06-24 21:23:44 +03002459 }
2460
2461 return ret;
2462}
2463
2464static void cpsw_ethtool_op_complete(struct net_device *ndev)
2465{
2466 struct cpsw_priv *priv = netdev_priv(ndev);
2467 int ret;
2468
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002469 ret = pm_runtime_put(priv->cpsw->dev);
Grygorii Strashko7898b1d2016-06-24 21:23:44 +03002470 if (ret < 0)
2471 cpsw_err(priv, drv, "ethtool complete failed %d\n", ret);
2472}
2473
Ivan Khoronzhukce52c742016-08-22 21:18:28 +03002474static void cpsw_get_channels(struct net_device *ndev,
2475 struct ethtool_channels *ch)
2476{
2477 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
2478
Ivan Khoronzhuk9611d6d2018-05-17 01:21:45 +03002479 ch->max_rx = cpsw->quirk_irq ? 1 : CPSW_MAX_QUEUES;
2480 ch->max_tx = cpsw->quirk_irq ? 1 : CPSW_MAX_QUEUES;
Ivan Khoronzhukce52c742016-08-22 21:18:28 +03002481 ch->max_combined = 0;
Ivan Khoronzhukce52c742016-08-22 21:18:28 +03002482 ch->max_other = 0;
2483 ch->other_count = 0;
2484 ch->rx_count = cpsw->rx_ch_num;
2485 ch->tx_count = cpsw->tx_ch_num;
2486 ch->combined_count = 0;
2487}
2488
2489static int cpsw_check_ch_settings(struct cpsw_common *cpsw,
2490 struct ethtool_channels *ch)
2491{
Ivan Khoronzhuk9611d6d2018-05-17 01:21:45 +03002492 if (cpsw->quirk_irq) {
2493 dev_err(cpsw->dev, "Maximum one tx/rx queue is allowed");
2494 return -EOPNOTSUPP;
2495 }
2496
Ivan Khoronzhukce52c742016-08-22 21:18:28 +03002497 if (ch->combined_count)
2498 return -EINVAL;
2499
2500 /* verify we have at least one channel in each direction */
2501 if (!ch->rx_count || !ch->tx_count)
2502 return -EINVAL;
2503
2504 if (ch->rx_count > cpsw->data.channels ||
2505 ch->tx_count > cpsw->data.channels)
2506 return -EINVAL;
2507
2508 return 0;
2509}
2510
2511static int cpsw_update_channels_res(struct cpsw_priv *priv, int ch_num, int rx)
2512{
Ivan Khoronzhukce52c742016-08-22 21:18:28 +03002513 struct cpsw_common *cpsw = priv->cpsw;
2514 void (*handler)(void *, int, int);
Ivan Khoronzhuk83fcad02016-11-29 17:00:49 +02002515 struct netdev_queue *queue;
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02002516 struct cpsw_vector *vec;
Ivan Khoronzhuk79b33252018-07-24 00:26:29 +03002517 int ret, *ch, vch;
Ivan Khoronzhukce52c742016-08-22 21:18:28 +03002518
2519 if (rx) {
2520 ch = &cpsw->rx_ch_num;
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02002521 vec = cpsw->rxv;
Ivan Khoronzhukce52c742016-08-22 21:18:28 +03002522 handler = cpsw_rx_handler;
Ivan Khoronzhukce52c742016-08-22 21:18:28 +03002523 } else {
2524 ch = &cpsw->tx_ch_num;
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02002525 vec = cpsw->txv;
Ivan Khoronzhukce52c742016-08-22 21:18:28 +03002526 handler = cpsw_tx_handler;
Ivan Khoronzhukce52c742016-08-22 21:18:28 +03002527 }
2528
2529 while (*ch < ch_num) {
Ivan Khoronzhuk79b33252018-07-24 00:26:29 +03002530 vch = rx ? *ch : 7 - *ch;
2531 vec[*ch].ch = cpdma_chan_create(cpsw->dma, vch, handler, rx);
Ivan Khoronzhuk83fcad02016-11-29 17:00:49 +02002532 queue = netdev_get_tx_queue(priv->ndev, *ch);
2533 queue->tx_maxrate = 0;
Ivan Khoronzhukce52c742016-08-22 21:18:28 +03002534
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02002535 if (IS_ERR(vec[*ch].ch))
2536 return PTR_ERR(vec[*ch].ch);
Ivan Khoronzhukce52c742016-08-22 21:18:28 +03002537
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02002538 if (!vec[*ch].ch)
Ivan Khoronzhukce52c742016-08-22 21:18:28 +03002539 return -EINVAL;
2540
2541 cpsw_info(priv, ifup, "created new %d %s channel\n", *ch,
2542 (rx ? "rx" : "tx"));
2543 (*ch)++;
2544 }
2545
2546 while (*ch > ch_num) {
2547 (*ch)--;
2548
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02002549 ret = cpdma_chan_destroy(vec[*ch].ch);
Ivan Khoronzhukce52c742016-08-22 21:18:28 +03002550 if (ret)
2551 return ret;
2552
2553 cpsw_info(priv, ifup, "destroyed %d %s channel\n", *ch,
2554 (rx ? "rx" : "tx"));
2555 }
2556
2557 return 0;
2558}
2559
2560static int cpsw_update_channels(struct cpsw_priv *priv,
2561 struct ethtool_channels *ch)
2562{
2563 int ret;
2564
2565 ret = cpsw_update_channels_res(priv, ch->rx_count, 1);
2566 if (ret)
2567 return ret;
2568
2569 ret = cpsw_update_channels_res(priv, ch->tx_count, 0);
2570 if (ret)
2571 return ret;
2572
2573 return 0;
2574}
2575
Ivan Khoronzhuk022d7ad2017-01-19 18:58:27 +02002576static void cpsw_suspend_data_pass(struct net_device *ndev)
Ivan Khoronzhukce52c742016-08-22 21:18:28 +03002577{
Ivan Khoronzhuk022d7ad2017-01-19 18:58:27 +02002578 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Ivan Khoronzhukce52c742016-08-22 21:18:28 +03002579 struct cpsw_slave *slave;
Ivan Khoronzhuk022d7ad2017-01-19 18:58:27 +02002580 int i;
Ivan Khoronzhukce52c742016-08-22 21:18:28 +03002581
2582 /* Disable NAPI scheduling */
2583 cpsw_intr_disable(cpsw);
2584
2585 /* Stop all transmit queues for every network device.
2586 * Disable re-using rx descriptors with dormant_on.
2587 */
2588 for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++) {
2589 if (!(slave->ndev && netif_running(slave->ndev)))
2590 continue;
2591
2592 netif_tx_stop_all_queues(slave->ndev);
2593 netif_dormant_on(slave->ndev);
2594 }
2595
2596 /* Handle rest of tx packets and stop cpdma channels */
2597 cpdma_ctlr_stop(cpsw->dma);
Ivan Khoronzhuk022d7ad2017-01-19 18:58:27 +02002598}
2599
2600static int cpsw_resume_data_pass(struct net_device *ndev)
2601{
2602 struct cpsw_priv *priv = netdev_priv(ndev);
2603 struct cpsw_common *cpsw = priv->cpsw;
2604 struct cpsw_slave *slave;
2605 int i, ret;
2606
2607 /* Allow rx packets handling */
2608 for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++)
2609 if (slave->ndev && netif_running(slave->ndev))
2610 netif_dormant_off(slave->ndev);
2611
2612 /* After this receive is started */
Ivan Khoronzhukd5bc1612017-02-14 16:02:36 +02002613 if (cpsw->usage_count) {
Ivan Khoronzhuk022d7ad2017-01-19 18:58:27 +02002614 ret = cpsw_fill_rx_channels(priv);
2615 if (ret)
2616 return ret;
2617
2618 cpdma_ctlr_start(cpsw->dma);
2619 cpsw_intr_enable(cpsw);
2620 }
2621
2622 /* Resume transmit for every affected interface */
2623 for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++)
2624 if (slave->ndev && netif_running(slave->ndev))
2625 netif_tx_start_all_queues(slave->ndev);
2626
2627 return 0;
2628}
2629
2630static int cpsw_set_channels(struct net_device *ndev,
2631 struct ethtool_channels *chs)
2632{
2633 struct cpsw_priv *priv = netdev_priv(ndev);
2634 struct cpsw_common *cpsw = priv->cpsw;
2635 struct cpsw_slave *slave;
2636 int i, ret;
2637
2638 ret = cpsw_check_ch_settings(cpsw, chs);
2639 if (ret < 0)
2640 return ret;
2641
2642 cpsw_suspend_data_pass(ndev);
Ivan Khoronzhukce52c742016-08-22 21:18:28 +03002643 ret = cpsw_update_channels(priv, chs);
2644 if (ret)
2645 goto err;
2646
2647 for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++) {
2648 if (!(slave->ndev && netif_running(slave->ndev)))
2649 continue;
2650
2651 /* Inform stack about new count of queues */
2652 ret = netif_set_real_num_tx_queues(slave->ndev,
2653 cpsw->tx_ch_num);
2654 if (ret) {
2655 dev_err(priv->dev, "cannot set real number of tx queues\n");
2656 goto err;
2657 }
2658
2659 ret = netif_set_real_num_rx_queues(slave->ndev,
2660 cpsw->rx_ch_num);
2661 if (ret) {
2662 dev_err(priv->dev, "cannot set real number of rx queues\n");
2663 goto err;
2664 }
Ivan Khoronzhukce52c742016-08-22 21:18:28 +03002665 }
2666
Ivan Khoronzhukd5bc1612017-02-14 16:02:36 +02002667 if (cpsw->usage_count)
Ivan Khoronzhuk32b78d82016-12-10 14:23:48 +02002668 cpsw_split_res(ndev);
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02002669
Ivan Khoronzhuk022d7ad2017-01-19 18:58:27 +02002670 ret = cpsw_resume_data_pass(ndev);
2671 if (!ret)
2672 return 0;
Ivan Khoronzhukce52c742016-08-22 21:18:28 +03002673err:
2674 dev_err(priv->dev, "cannot update channels number, closing device\n");
2675 dev_close(ndev);
2676 return ret;
2677}
2678
Yegor Yefremova0909942016-11-28 09:41:33 +01002679static int cpsw_get_eee(struct net_device *ndev, struct ethtool_eee *edata)
2680{
2681 struct cpsw_priv *priv = netdev_priv(ndev);
2682 struct cpsw_common *cpsw = priv->cpsw;
2683 int slave_no = cpsw_slave_index(cpsw, priv);
2684
2685 if (cpsw->slaves[slave_no].phy)
2686 return phy_ethtool_get_eee(cpsw->slaves[slave_no].phy, edata);
2687 else
2688 return -EOPNOTSUPP;
2689}
2690
2691static int cpsw_set_eee(struct net_device *ndev, struct ethtool_eee *edata)
2692{
2693 struct cpsw_priv *priv = netdev_priv(ndev);
2694 struct cpsw_common *cpsw = priv->cpsw;
2695 int slave_no = cpsw_slave_index(cpsw, priv);
2696
2697 if (cpsw->slaves[slave_no].phy)
2698 return phy_ethtool_set_eee(cpsw->slaves[slave_no].phy, edata);
2699 else
2700 return -EOPNOTSUPP;
2701}
2702
Yegor Yefremov6bb10c22016-11-28 10:47:52 +01002703static int cpsw_nway_reset(struct net_device *ndev)
2704{
2705 struct cpsw_priv *priv = netdev_priv(ndev);
2706 struct cpsw_common *cpsw = priv->cpsw;
2707 int slave_no = cpsw_slave_index(cpsw, priv);
2708
2709 if (cpsw->slaves[slave_no].phy)
2710 return genphy_restart_aneg(cpsw->slaves[slave_no].phy);
2711 else
2712 return -EOPNOTSUPP;
2713}
2714
Grygorii Strashkobe034fc2017-01-06 14:07:34 -06002715static void cpsw_get_ringparam(struct net_device *ndev,
2716 struct ethtool_ringparam *ering)
2717{
2718 struct cpsw_priv *priv = netdev_priv(ndev);
2719 struct cpsw_common *cpsw = priv->cpsw;
2720
2721 /* not supported */
2722 ering->tx_max_pending = 0;
2723 ering->tx_pending = cpdma_get_num_tx_descs(cpsw->dma);
Ivan Khoronzhukf89d21b2017-01-08 22:12:27 +02002724 ering->rx_max_pending = descs_pool_size - CPSW_MAX_QUEUES;
Grygorii Strashkobe034fc2017-01-06 14:07:34 -06002725 ering->rx_pending = cpdma_get_num_rx_descs(cpsw->dma);
2726}
2727
2728static int cpsw_set_ringparam(struct net_device *ndev,
2729 struct ethtool_ringparam *ering)
2730{
2731 struct cpsw_priv *priv = netdev_priv(ndev);
2732 struct cpsw_common *cpsw = priv->cpsw;
Ivan Khoronzhuk022d7ad2017-01-19 18:58:27 +02002733 int ret;
Grygorii Strashkobe034fc2017-01-06 14:07:34 -06002734
2735 /* ignore ering->tx_pending - only rx_pending adjustment is supported */
2736
2737 if (ering->rx_mini_pending || ering->rx_jumbo_pending ||
Ivan Khoronzhukf89d21b2017-01-08 22:12:27 +02002738 ering->rx_pending < CPSW_MAX_QUEUES ||
2739 ering->rx_pending > (descs_pool_size - CPSW_MAX_QUEUES))
Grygorii Strashkobe034fc2017-01-06 14:07:34 -06002740 return -EINVAL;
2741
2742 if (ering->rx_pending == cpdma_get_num_rx_descs(cpsw->dma))
2743 return 0;
2744
Ivan Khoronzhuk022d7ad2017-01-19 18:58:27 +02002745 cpsw_suspend_data_pass(ndev);
Grygorii Strashkobe034fc2017-01-06 14:07:34 -06002746
2747 cpdma_set_num_rx_descs(cpsw->dma, ering->rx_pending);
2748
Ivan Khoronzhukd5bc1612017-02-14 16:02:36 +02002749 if (cpsw->usage_count)
Grygorii Strashkobe034fc2017-01-06 14:07:34 -06002750 cpdma_chan_split_pool(cpsw->dma);
2751
Ivan Khoronzhuk022d7ad2017-01-19 18:58:27 +02002752 ret = cpsw_resume_data_pass(ndev);
2753 if (!ret)
2754 return 0;
Grygorii Strashkobe034fc2017-01-06 14:07:34 -06002755
Ivan Khoronzhuk022d7ad2017-01-19 18:58:27 +02002756 dev_err(&ndev->dev, "cannot set ring params, closing device\n");
Grygorii Strashkobe034fc2017-01-06 14:07:34 -06002757 dev_close(ndev);
2758 return ret;
2759}
2760
Mugunthan V Ndf828592012-03-18 20:17:54 +00002761static const struct ethtool_ops cpsw_ethtool_ops = {
2762 .get_drvinfo = cpsw_get_drvinfo,
2763 .get_msglevel = cpsw_get_msglevel,
2764 .set_msglevel = cpsw_set_msglevel,
2765 .get_link = ethtool_op_get_link,
Richard Cochran2e5b38a2012-10-29 08:45:20 +00002766 .get_ts_info = cpsw_get_ts_info,
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00002767 .get_coalesce = cpsw_get_coalesce,
2768 .set_coalesce = cpsw_set_coalesce,
Mugunthan V Nd9718542013-07-23 15:38:17 +05302769 .get_sset_count = cpsw_get_sset_count,
2770 .get_strings = cpsw_get_strings,
2771 .get_ethtool_stats = cpsw_get_ethtool_stats,
Mugunthan V N1923d6e2014-09-08 22:54:02 +05302772 .get_pauseparam = cpsw_get_pauseparam,
2773 .set_pauseparam = cpsw_set_pauseparam,
Matus Ujhelyid8a64422013-08-20 07:59:38 +02002774 .get_wol = cpsw_get_wol,
2775 .set_wol = cpsw_set_wol,
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05302776 .get_regs_len = cpsw_get_regs_len,
2777 .get_regs = cpsw_get_regs,
Grygorii Strashko7898b1d2016-06-24 21:23:44 +03002778 .begin = cpsw_ethtool_op_begin,
2779 .complete = cpsw_ethtool_op_complete,
Ivan Khoronzhukce52c742016-08-22 21:18:28 +03002780 .get_channels = cpsw_get_channels,
2781 .set_channels = cpsw_set_channels,
Philippe Reynes24798762016-10-08 17:46:15 +02002782 .get_link_ksettings = cpsw_get_link_ksettings,
2783 .set_link_ksettings = cpsw_set_link_ksettings,
Yegor Yefremova0909942016-11-28 09:41:33 +01002784 .get_eee = cpsw_get_eee,
2785 .set_eee = cpsw_set_eee,
Yegor Yefremov6bb10c22016-11-28 10:47:52 +01002786 .nway_reset = cpsw_nway_reset,
Grygorii Strashkobe034fc2017-01-06 14:07:34 -06002787 .get_ringparam = cpsw_get_ringparam,
2788 .set_ringparam = cpsw_set_ringparam,
Mugunthan V Ndf828592012-03-18 20:17:54 +00002789};
2790
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002791static void cpsw_slave_init(struct cpsw_slave *slave, struct cpsw_common *cpsw,
Richard Cochran549985e2012-11-14 09:07:56 +00002792 u32 slave_reg_ofs, u32 sliver_reg_ofs)
Mugunthan V Ndf828592012-03-18 20:17:54 +00002793{
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03002794 void __iomem *regs = cpsw->regs;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002795 int slave_num = slave->slave_num;
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002796 struct cpsw_slave_data *data = cpsw->data.slave_data + slave_num;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002797
2798 slave->data = data;
Richard Cochran549985e2012-11-14 09:07:56 +00002799 slave->regs = regs + slave_reg_ofs;
2800 slave->sliver = regs + sliver_reg_ofs;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002801 slave->port_vlan = data->dual_emac_res_vlan;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002802}
2803
David Rivshin552165b2016-04-27 21:25:25 -04002804static int cpsw_probe_dt(struct cpsw_platform_data *data,
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002805 struct platform_device *pdev)
2806{
2807 struct device_node *node = pdev->dev.of_node;
2808 struct device_node *slave_node;
2809 int i = 0, ret;
2810 u32 prop;
2811
2812 if (!node)
2813 return -EINVAL;
2814
2815 if (of_property_read_u32(node, "slaves", &prop)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302816 dev_err(&pdev->dev, "Missing slaves property in the DT.\n");
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002817 return -EINVAL;
2818 }
2819 data->slaves = prop;
2820
Mugunthan V Ne86ac132013-03-11 23:16:35 +00002821 if (of_property_read_u32(node, "active_slave", &prop)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302822 dev_err(&pdev->dev, "Missing active_slave property in the DT.\n");
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302823 return -EINVAL;
Richard Cochran78ca0b22012-10-29 08:45:18 +00002824 }
Mugunthan V Ne86ac132013-03-11 23:16:35 +00002825 data->active_slave = prop;
Richard Cochran78ca0b22012-10-29 08:45:18 +00002826
Kees Cooka86854d2018-06-12 14:07:58 -07002827 data->slave_data = devm_kcalloc(&pdev->dev,
2828 data->slaves,
2829 sizeof(struct cpsw_slave_data),
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302830 GFP_KERNEL);
Joe Perchesb2adaca2013-02-03 17:43:58 +00002831 if (!data->slave_data)
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302832 return -ENOMEM;
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002833
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002834 if (of_property_read_u32(node, "cpdma_channels", &prop)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302835 dev_err(&pdev->dev, "Missing cpdma_channels property in the DT.\n");
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302836 return -EINVAL;
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002837 }
2838 data->channels = prop;
2839
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002840 if (of_property_read_u32(node, "ale_entries", &prop)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302841 dev_err(&pdev->dev, "Missing ale_entries property in the DT.\n");
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302842 return -EINVAL;
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002843 }
2844 data->ale_entries = prop;
2845
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002846 if (of_property_read_u32(node, "bd_ram_size", &prop)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302847 dev_err(&pdev->dev, "Missing bd_ram_size property in the DT.\n");
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302848 return -EINVAL;
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002849 }
2850 data->bd_ram_size = prop;
2851
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002852 if (of_property_read_u32(node, "mac_control", &prop)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302853 dev_err(&pdev->dev, "Missing mac_control property in the DT.\n");
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302854 return -EINVAL;
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002855 }
2856 data->mac_control = prop;
2857
Markus Pargmann281abd92013-10-04 14:44:40 +02002858 if (of_property_read_bool(node, "dual_emac"))
2859 data->dual_emac = 1;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002860
Vaibhav Hiremath1fb19aa2012-11-14 09:07:55 +00002861 /*
2862 * Populate all the child nodes here...
2863 */
2864 ret = of_platform_populate(node, NULL, NULL, &pdev->dev);
2865 /* We do not want to force this, as in some cases may not have child */
2866 if (ret)
George Cherian88c99ff2014-05-12 10:21:19 +05302867 dev_warn(&pdev->dev, "Doesn't have any child node\n");
Vaibhav Hiremath1fb19aa2012-11-14 09:07:55 +00002868
Ben Hutchings8658aaf2016-06-21 01:16:31 +01002869 for_each_available_child_of_node(node, slave_node) {
Richard Cochran549985e2012-11-14 09:07:56 +00002870 struct cpsw_slave_data *slave_data = data->slave_data + i;
2871 const void *mac_addr = NULL;
Richard Cochran549985e2012-11-14 09:07:56 +00002872 int lenp;
2873 const __be32 *parp;
Richard Cochran549985e2012-11-14 09:07:56 +00002874
Markus Pargmannf468b102013-10-04 14:44:39 +02002875 /* This is no slave child node, continue */
2876 if (strcmp(slave_node->name, "slave"))
2877 continue;
2878
David Rivshin552165b2016-04-27 21:25:25 -04002879 slave_data->phy_node = of_parse_phandle(slave_node,
2880 "phy-handle", 0);
David Rivshinf1eea5c2015-12-16 23:02:10 -05002881 parp = of_get_property(slave_node, "phy_id", &lenp);
David Rivshinae092b52016-04-27 21:38:26 -04002882 if (slave_data->phy_node) {
2883 dev_dbg(&pdev->dev,
Rob Herringf7ce9102017-07-18 16:43:19 -05002884 "slave[%d] using phy-handle=\"%pOF\"\n",
2885 i, slave_data->phy_node);
David Rivshinae092b52016-04-27 21:38:26 -04002886 } else if (of_phy_is_fixed_link(slave_node)) {
David Rivshindfc0a6d2015-12-16 23:02:11 -05002887 /* In the case of a fixed PHY, the DT node associated
2888 * to the PHY is the Ethernet MAC DT node.
2889 */
Markus Brunner1f71e8c2015-11-03 22:09:51 +01002890 ret = of_phy_register_fixed_link(slave_node);
Johan Hovold23a09872016-11-17 17:40:04 +01002891 if (ret) {
2892 if (ret != -EPROBE_DEFER)
2893 dev_err(&pdev->dev, "failed to register fixed-link phy: %d\n", ret);
Markus Brunner1f71e8c2015-11-03 22:09:51 +01002894 return ret;
Johan Hovold23a09872016-11-17 17:40:04 +01002895 }
David Rivshin06cd6d62016-04-27 21:45:45 -04002896 slave_data->phy_node = of_node_get(slave_node);
David Rivshinf1eea5c2015-12-16 23:02:10 -05002897 } else if (parp) {
2898 u32 phyid;
2899 struct device_node *mdio_node;
2900 struct platform_device *mdio;
2901
2902 if (lenp != (sizeof(__be32) * 2)) {
2903 dev_err(&pdev->dev, "Invalid slave[%d] phy_id property\n", i);
2904 goto no_phy_slave;
2905 }
2906 mdio_node = of_find_node_by_phandle(be32_to_cpup(parp));
2907 phyid = be32_to_cpup(parp+1);
2908 mdio = of_find_device_by_node(mdio_node);
2909 of_node_put(mdio_node);
2910 if (!mdio) {
2911 dev_err(&pdev->dev, "Missing mdio platform device\n");
2912 return -EINVAL;
2913 }
2914 snprintf(slave_data->phy_id, sizeof(slave_data->phy_id),
2915 PHY_ID_FMT, mdio->name, phyid);
Johan Hovold86e1d5a2016-11-17 17:39:59 +01002916 put_device(&mdio->dev);
David Rivshinf1eea5c2015-12-16 23:02:10 -05002917 } else {
David Rivshinae092b52016-04-27 21:38:26 -04002918 dev_err(&pdev->dev,
2919 "No slave[%d] phy_id, phy-handle, or fixed-link property\n",
2920 i);
Markus Brunner1f71e8c2015-11-03 22:09:51 +01002921 goto no_phy_slave;
2922 }
Mugunthan V N47276fc2014-10-24 18:51:33 +05302923 slave_data->phy_if = of_get_phy_mode(slave_node);
2924 if (slave_data->phy_if < 0) {
2925 dev_err(&pdev->dev, "Missing or malformed slave[%d] phy-mode property\n",
2926 i);
2927 return slave_data->phy_if;
2928 }
2929
2930no_phy_slave:
Richard Cochran549985e2012-11-14 09:07:56 +00002931 mac_addr = of_get_mac_address(slave_node);
Markus Pargmann0ba517b2014-09-29 08:53:17 +02002932 if (mac_addr) {
Richard Cochran549985e2012-11-14 09:07:56 +00002933 memcpy(slave_data->mac_addr, mac_addr, ETH_ALEN);
Markus Pargmann0ba517b2014-09-29 08:53:17 +02002934 } else {
Mugunthan V Nb6745f62015-09-21 15:56:50 +05302935 ret = ti_cm_get_macid(&pdev->dev, i,
2936 slave_data->mac_addr);
2937 if (ret)
2938 return ret;
Markus Pargmann0ba517b2014-09-29 08:53:17 +02002939 }
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002940 if (data->dual_emac) {
Mugunthan V N91c41662013-04-15 07:31:28 +00002941 if (of_property_read_u32(slave_node, "dual_emac_res_vlan",
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002942 &prop)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302943 dev_err(&pdev->dev, "Missing dual_emac_res_vlan in DT.\n");
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002944 slave_data->dual_emac_res_vlan = i+1;
George Cherian88c99ff2014-05-12 10:21:19 +05302945 dev_err(&pdev->dev, "Using %d as Reserved VLAN for %d slave\n",
2946 slave_data->dual_emac_res_vlan, i);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002947 } else {
2948 slave_data->dual_emac_res_vlan = prop;
2949 }
2950 }
2951
Richard Cochran549985e2012-11-14 09:07:56 +00002952 i++;
Mugunthan V N3a27bfa2013-12-02 12:53:39 +05302953 if (i == data->slaves)
2954 break;
Richard Cochran549985e2012-11-14 09:07:56 +00002955 }
2956
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002957 return 0;
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002958}
2959
Johan Hovolda4e32b02016-11-17 17:40:00 +01002960static void cpsw_remove_dt(struct platform_device *pdev)
2961{
Johan Hovold8cbcc462016-11-17 17:40:01 +01002962 struct net_device *ndev = platform_get_drvdata(pdev);
2963 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
2964 struct cpsw_platform_data *data = &cpsw->data;
2965 struct device_node *node = pdev->dev.of_node;
2966 struct device_node *slave_node;
2967 int i = 0;
2968
2969 for_each_available_child_of_node(node, slave_node) {
2970 struct cpsw_slave_data *slave_data = &data->slave_data[i];
2971
2972 if (strcmp(slave_node->name, "slave"))
2973 continue;
2974
Johan Hovold3f650472016-11-28 19:24:55 +01002975 if (of_phy_is_fixed_link(slave_node))
2976 of_phy_deregister_fixed_link(slave_node);
Johan Hovold8cbcc462016-11-17 17:40:01 +01002977
2978 of_node_put(slave_data->phy_node);
2979
2980 i++;
2981 if (i == data->slaves)
2982 break;
2983 }
2984
Johan Hovolda4e32b02016-11-17 17:40:00 +01002985 of_platform_depopulate(&pdev->dev);
2986}
2987
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002988static int cpsw_probe_dual_emac(struct cpsw_priv *priv)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002989{
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002990 struct cpsw_common *cpsw = priv->cpsw;
2991 struct cpsw_platform_data *data = &cpsw->data;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002992 struct net_device *ndev;
2993 struct cpsw_priv *priv_sl2;
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +03002994 int ret = 0;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002995
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03002996 ndev = alloc_etherdev_mq(sizeof(struct cpsw_priv), CPSW_MAX_QUEUES);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002997 if (!ndev) {
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002998 dev_err(cpsw->dev, "cpsw: error allocating net_device\n");
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002999 return -ENOMEM;
3000 }
3001
3002 priv_sl2 = netdev_priv(ndev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03003003 priv_sl2->cpsw = cpsw;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00003004 priv_sl2->ndev = ndev;
3005 priv_sl2->dev = &ndev->dev;
3006 priv_sl2->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00003007
3008 if (is_valid_ether_addr(data->slave_data[1].mac_addr)) {
3009 memcpy(priv_sl2->mac_addr, data->slave_data[1].mac_addr,
3010 ETH_ALEN);
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03003011 dev_info(cpsw->dev, "cpsw: Detected MACID = %pM\n",
3012 priv_sl2->mac_addr);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00003013 } else {
Joe Perches6c1f0a12018-06-22 10:51:00 -07003014 eth_random_addr(priv_sl2->mac_addr);
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03003015 dev_info(cpsw->dev, "cpsw: Random MACID = %pM\n",
3016 priv_sl2->mac_addr);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00003017 }
3018 memcpy(ndev->dev_addr, priv_sl2->mac_addr, ETH_ALEN);
3019
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00003020 priv_sl2->emac_port = 1;
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03003021 cpsw->slaves[1].ndev = ndev;
Patrick McHardyf6469682013-04-19 02:04:27 +00003022 ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00003023
3024 ndev->netdev_ops = &cpsw_netdev_ops;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00003025 ndev->ethtool_ops = &cpsw_ethtool_ops;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00003026
3027 /* register the network device */
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03003028 SET_NETDEV_DEV(ndev, cpsw->dev);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00003029 ret = register_netdev(ndev);
3030 if (ret) {
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03003031 dev_err(cpsw->dev, "cpsw: error registering net device\n");
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00003032 free_netdev(ndev);
3033 ret = -ENODEV;
3034 }
3035
3036 return ret;
3037}
3038
Mugunthan V N7da11602015-08-12 15:22:53 +05303039static const struct of_device_id cpsw_of_mtable[] = {
Ivan Khoronzhuk9611d6d2018-05-17 01:21:45 +03003040 { .compatible = "ti,cpsw"},
3041 { .compatible = "ti,am335x-cpsw"},
3042 { .compatible = "ti,am4372-cpsw"},
3043 { .compatible = "ti,dra7-cpsw"},
Mugunthan V N7da11602015-08-12 15:22:53 +05303044 { /* sentinel */ },
3045};
3046MODULE_DEVICE_TABLE(of, cpsw_of_mtable);
3047
Ivan Khoronzhuk9611d6d2018-05-17 01:21:45 +03003048static const struct soc_device_attribute cpsw_soc_devices[] = {
3049 { .family = "AM33xx", .revision = "ES1.0"},
3050 { /* sentinel */ }
3051};
3052
Bill Pemberton663e12e2012-12-03 09:23:45 -05003053static int cpsw_probe(struct platform_device *pdev)
Mugunthan V Ndf828592012-03-18 20:17:54 +00003054{
Ivan Khoronzhukef4183a2016-08-10 02:22:35 +03003055 struct clk *clk;
Sebastian Siewiord1bd9ac2013-04-24 08:48:23 +00003056 struct cpsw_platform_data *data;
Mugunthan V Ndf828592012-03-18 20:17:54 +00003057 struct net_device *ndev;
3058 struct cpsw_priv *priv;
3059 struct cpdma_params dma_params;
3060 struct cpsw_ale_params ale_params;
Daniel Mackaa1a15e2013-09-21 00:50:38 +05303061 void __iomem *ss_regs;
Grygorii Strashko8a2c9a52016-12-06 18:00:41 -06003062 void __iomem *cpts_regs;
Daniel Mackaa1a15e2013-09-21 00:50:38 +05303063 struct resource *res, *ss_res;
Mugunthan V N1d147cc2015-09-07 15:16:44 +05303064 struct gpio_descs *mode;
Richard Cochran549985e2012-11-14 09:07:56 +00003065 u32 slave_offset, sliver_offset, slave_size;
Ivan Khoronzhuk9611d6d2018-05-17 01:21:45 +03003066 const struct soc_device_attribute *soc;
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03003067 struct cpsw_common *cpsw;
Ivan Khoronzhuk79b33252018-07-24 00:26:29 +03003068 int ret = 0, i, ch;
Felipe Balbi5087b912015-01-16 10:11:11 -06003069 int irq;
Mugunthan V Ndf828592012-03-18 20:17:54 +00003070
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03003071 cpsw = devm_kzalloc(&pdev->dev, sizeof(struct cpsw_common), GFP_KERNEL);
Johan Hovold3420ea82016-11-17 17:40:03 +01003072 if (!cpsw)
3073 return -ENOMEM;
3074
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03003075 cpsw->dev = &pdev->dev;
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03003076
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03003077 ndev = alloc_etherdev_mq(sizeof(struct cpsw_priv), CPSW_MAX_QUEUES);
Mugunthan V Ndf828592012-03-18 20:17:54 +00003078 if (!ndev) {
George Cherian88c99ff2014-05-12 10:21:19 +05303079 dev_err(&pdev->dev, "error allocating net_device\n");
Mugunthan V Ndf828592012-03-18 20:17:54 +00003080 return -ENOMEM;
3081 }
3082
3083 platform_set_drvdata(pdev, ndev);
3084 priv = netdev_priv(ndev);
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03003085 priv->cpsw = cpsw;
Mugunthan V Ndf828592012-03-18 20:17:54 +00003086 priv->ndev = ndev;
3087 priv->dev = &ndev->dev;
3088 priv->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03003089 cpsw->rx_packet_max = max(rx_packet_max, 128);
Mugunthan V Ndf828592012-03-18 20:17:54 +00003090
Mugunthan V N1d147cc2015-09-07 15:16:44 +05303091 mode = devm_gpiod_get_array_optional(&pdev->dev, "mode", GPIOD_OUT_LOW);
3092 if (IS_ERR(mode)) {
3093 ret = PTR_ERR(mode);
3094 dev_err(&pdev->dev, "gpio request failed, ret %d\n", ret);
3095 goto clean_ndev_ret;
3096 }
3097
Vaibhav Hiremath1fb19aa2012-11-14 09:07:55 +00003098 /*
3099 * This may be required here for child devices.
3100 */
3101 pm_runtime_enable(&pdev->dev);
3102
Mugunthan V N739683b2013-06-06 23:45:14 +05303103 /* Select default pin state */
3104 pinctrl_pm_select_default_state(&pdev->dev);
3105
Johan Hovolda4e32b02016-11-17 17:40:00 +01003106 /* Need to enable clocks with runtime PM api to access module
3107 * registers
3108 */
3109 ret = pm_runtime_get_sync(&pdev->dev);
3110 if (ret < 0) {
3111 pm_runtime_put_noidle(&pdev->dev);
Daniel Mackaa1a15e2013-09-21 00:50:38 +05303112 goto clean_runtime_disable_ret;
Mugunthan V N2eb32b02012-07-30 10:17:14 +00003113 }
Johan Hovolda4e32b02016-11-17 17:40:00 +01003114
Johan Hovold23a09872016-11-17 17:40:04 +01003115 ret = cpsw_probe_dt(&cpsw->data, pdev);
3116 if (ret)
Johan Hovolda4e32b02016-11-17 17:40:00 +01003117 goto clean_dt_ret;
Johan Hovold23a09872016-11-17 17:40:04 +01003118
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03003119 data = &cpsw->data;
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03003120 cpsw->rx_ch_num = 1;
3121 cpsw->tx_ch_num = 1;
Mugunthan V N2eb32b02012-07-30 10:17:14 +00003122
Mugunthan V Ndf828592012-03-18 20:17:54 +00003123 if (is_valid_ether_addr(data->slave_data[0].mac_addr)) {
3124 memcpy(priv->mac_addr, data->slave_data[0].mac_addr, ETH_ALEN);
George Cherian88c99ff2014-05-12 10:21:19 +05303125 dev_info(&pdev->dev, "Detected MACID = %pM\n", priv->mac_addr);
Mugunthan V Ndf828592012-03-18 20:17:54 +00003126 } else {
Joe Perches7efd26d2012-07-12 19:33:06 +00003127 eth_random_addr(priv->mac_addr);
George Cherian88c99ff2014-05-12 10:21:19 +05303128 dev_info(&pdev->dev, "Random MACID = %pM\n", priv->mac_addr);
Mugunthan V Ndf828592012-03-18 20:17:54 +00003129 }
3130
3131 memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
3132
Kees Cooka86854d2018-06-12 14:07:58 -07003133 cpsw->slaves = devm_kcalloc(&pdev->dev,
3134 data->slaves, sizeof(struct cpsw_slave),
Daniel Mackaa1a15e2013-09-21 00:50:38 +05303135 GFP_KERNEL);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03003136 if (!cpsw->slaves) {
Daniel Mackaa1a15e2013-09-21 00:50:38 +05303137 ret = -ENOMEM;
Johan Hovolda4e32b02016-11-17 17:40:00 +01003138 goto clean_dt_ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00003139 }
3140 for (i = 0; i < data->slaves; i++)
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03003141 cpsw->slaves[i].slave_num = i;
Mugunthan V Ndf828592012-03-18 20:17:54 +00003142
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03003143 cpsw->slaves[0].ndev = ndev;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00003144 priv->emac_port = 0;
3145
Ivan Khoronzhukef4183a2016-08-10 02:22:35 +03003146 clk = devm_clk_get(&pdev->dev, "fck");
3147 if (IS_ERR(clk)) {
Daniel Mackaa1a15e2013-09-21 00:50:38 +05303148 dev_err(priv->dev, "fck is not found\n");
Mugunthan V Nf150bd72012-07-17 08:09:50 +00003149 ret = -ENODEV;
Johan Hovolda4e32b02016-11-17 17:40:00 +01003150 goto clean_dt_ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00003151 }
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03003152 cpsw->bus_freq_mhz = clk_get_rate(clk) / 1000000;
Mugunthan V Ndf828592012-03-18 20:17:54 +00003153
Daniel Mackaa1a15e2013-09-21 00:50:38 +05303154 ss_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3155 ss_regs = devm_ioremap_resource(&pdev->dev, ss_res);
3156 if (IS_ERR(ss_regs)) {
3157 ret = PTR_ERR(ss_regs);
Johan Hovolda4e32b02016-11-17 17:40:00 +01003158 goto clean_dt_ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00003159 }
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03003160 cpsw->regs = ss_regs;
Mugunthan V Ndf828592012-03-18 20:17:54 +00003161
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03003162 cpsw->version = readl(&cpsw->regs->id_ver);
Mugunthan V Nf280e892013-12-11 22:09:05 -06003163
Daniel Mackaa1a15e2013-09-21 00:50:38 +05303164 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03003165 cpsw->wr_regs = devm_ioremap_resource(&pdev->dev, res);
3166 if (IS_ERR(cpsw->wr_regs)) {
3167 ret = PTR_ERR(cpsw->wr_regs);
Johan Hovolda4e32b02016-11-17 17:40:00 +01003168 goto clean_dt_ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00003169 }
Mugunthan V Ndf828592012-03-18 20:17:54 +00003170
3171 memset(&dma_params, 0, sizeof(dma_params));
Richard Cochran549985e2012-11-14 09:07:56 +00003172 memset(&ale_params, 0, sizeof(ale_params));
3173
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03003174 switch (cpsw->version) {
Richard Cochran549985e2012-11-14 09:07:56 +00003175 case CPSW_VERSION_1:
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03003176 cpsw->host_port_regs = ss_regs + CPSW1_HOST_PORT_OFFSET;
Grygorii Strashko8a2c9a52016-12-06 18:00:41 -06003177 cpts_regs = ss_regs + CPSW1_CPTS_OFFSET;
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03003178 cpsw->hw_stats = ss_regs + CPSW1_HW_STATS;
Richard Cochran549985e2012-11-14 09:07:56 +00003179 dma_params.dmaregs = ss_regs + CPSW1_CPDMA_OFFSET;
3180 dma_params.txhdp = ss_regs + CPSW1_STATERAM_OFFSET;
3181 ale_params.ale_regs = ss_regs + CPSW1_ALE_OFFSET;
3182 slave_offset = CPSW1_SLAVE_OFFSET;
3183 slave_size = CPSW1_SLAVE_SIZE;
3184 sliver_offset = CPSW1_SLIVER_OFFSET;
3185 dma_params.desc_mem_phys = 0;
3186 break;
3187 case CPSW_VERSION_2:
Mugunthan V Nc193f362013-08-05 17:30:05 +05303188 case CPSW_VERSION_3:
Mugunthan V N926489b2013-08-12 17:11:15 +05303189 case CPSW_VERSION_4:
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03003190 cpsw->host_port_regs = ss_regs + CPSW2_HOST_PORT_OFFSET;
Grygorii Strashko8a2c9a52016-12-06 18:00:41 -06003191 cpts_regs = ss_regs + CPSW2_CPTS_OFFSET;
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03003192 cpsw->hw_stats = ss_regs + CPSW2_HW_STATS;
Richard Cochran549985e2012-11-14 09:07:56 +00003193 dma_params.dmaregs = ss_regs + CPSW2_CPDMA_OFFSET;
3194 dma_params.txhdp = ss_regs + CPSW2_STATERAM_OFFSET;
3195 ale_params.ale_regs = ss_regs + CPSW2_ALE_OFFSET;
3196 slave_offset = CPSW2_SLAVE_OFFSET;
3197 slave_size = CPSW2_SLAVE_SIZE;
3198 sliver_offset = CPSW2_SLIVER_OFFSET;
3199 dma_params.desc_mem_phys =
Daniel Mackaa1a15e2013-09-21 00:50:38 +05303200 (u32 __force) ss_res->start + CPSW2_BD_OFFSET;
Richard Cochran549985e2012-11-14 09:07:56 +00003201 break;
3202 default:
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03003203 dev_err(priv->dev, "unknown version 0x%08x\n", cpsw->version);
Richard Cochran549985e2012-11-14 09:07:56 +00003204 ret = -ENODEV;
Johan Hovolda4e32b02016-11-17 17:40:00 +01003205 goto clean_dt_ret;
Richard Cochran549985e2012-11-14 09:07:56 +00003206 }
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03003207 for (i = 0; i < cpsw->data.slaves; i++) {
3208 struct cpsw_slave *slave = &cpsw->slaves[i];
3209
3210 cpsw_slave_init(slave, cpsw, slave_offset, sliver_offset);
Richard Cochran549985e2012-11-14 09:07:56 +00003211 slave_offset += slave_size;
3212 sliver_offset += SLIVER_SIZE;
3213 }
3214
Mugunthan V Ndf828592012-03-18 20:17:54 +00003215 dma_params.dev = &pdev->dev;
Richard Cochran549985e2012-11-14 09:07:56 +00003216 dma_params.rxthresh = dma_params.dmaregs + CPDMA_RXTHRESH;
3217 dma_params.rxfree = dma_params.dmaregs + CPDMA_RXFREE;
3218 dma_params.rxhdp = dma_params.txhdp + CPDMA_RXHDP;
3219 dma_params.txcp = dma_params.txhdp + CPDMA_TXCP;
3220 dma_params.rxcp = dma_params.txhdp + CPDMA_RXCP;
Mugunthan V Ndf828592012-03-18 20:17:54 +00003221
3222 dma_params.num_chan = data->channels;
3223 dma_params.has_soft_reset = true;
3224 dma_params.min_packet_size = CPSW_MIN_PACKET_SIZE;
3225 dma_params.desc_mem_size = data->bd_ram_size;
3226 dma_params.desc_align = 16;
3227 dma_params.has_ext_regs = true;
Richard Cochran549985e2012-11-14 09:07:56 +00003228 dma_params.desc_hw_addr = dma_params.desc_mem_phys;
Ivan Khoronzhuk83fcad02016-11-29 17:00:49 +02003229 dma_params.bus_freq_mhz = cpsw->bus_freq_mhz;
Grygorii Strashko90225bf2017-01-06 14:07:33 -06003230 dma_params.descs_pool_size = descs_pool_size;
Mugunthan V Ndf828592012-03-18 20:17:54 +00003231
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03003232 cpsw->dma = cpdma_ctlr_create(&dma_params);
3233 if (!cpsw->dma) {
Mugunthan V Ndf828592012-03-18 20:17:54 +00003234 dev_err(priv->dev, "error initializing dma\n");
3235 ret = -ENOMEM;
Johan Hovolda4e32b02016-11-17 17:40:00 +01003236 goto clean_dt_ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00003237 }
3238
Ivan Khoronzhuk9611d6d2018-05-17 01:21:45 +03003239 soc = soc_device_match(cpsw_soc_devices);
3240 if (soc)
3241 cpsw->quirk_irq = 1;
3242
Ivan Khoronzhuk79b33252018-07-24 00:26:29 +03003243 ch = cpsw->quirk_irq ? 0 : 7;
3244 cpsw->txv[0].ch = cpdma_chan_create(cpsw->dma, ch, cpsw_tx_handler, 0);
Ivan Khoronzhuk8a83c5d2017-12-12 23:06:35 +02003245 if (IS_ERR(cpsw->txv[0].ch)) {
3246 dev_err(priv->dev, "error initializing tx dma channel\n");
3247 ret = PTR_ERR(cpsw->txv[0].ch);
3248 goto clean_dma_ret;
3249 }
3250
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02003251 cpsw->rxv[0].ch = cpdma_chan_create(cpsw->dma, 0, cpsw_rx_handler, 1);
Ivan Khoronzhuk8a83c5d2017-12-12 23:06:35 +02003252 if (IS_ERR(cpsw->rxv[0].ch)) {
3253 dev_err(priv->dev, "error initializing rx dma channel\n");
3254 ret = PTR_ERR(cpsw->rxv[0].ch);
Mugunthan V Ndf828592012-03-18 20:17:54 +00003255 goto clean_dma_ret;
3256 }
3257
Ivan Khoronzhuk9fe9aa02017-02-15 19:45:02 +02003258 ale_params.dev = &pdev->dev;
Mugunthan V Ndf828592012-03-18 20:17:54 +00003259 ale_params.ale_ageout = ale_ageout;
3260 ale_params.ale_entries = data->ale_entries;
Grygorii Strashkoc6395f12017-11-30 18:21:14 -06003261 ale_params.ale_ports = CPSW_ALE_PORTS_NUM;
Mugunthan V Ndf828592012-03-18 20:17:54 +00003262
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03003263 cpsw->ale = cpsw_ale_create(&ale_params);
3264 if (!cpsw->ale) {
Mugunthan V Ndf828592012-03-18 20:17:54 +00003265 dev_err(priv->dev, "error initializing ale engine\n");
3266 ret = -ENODEV;
3267 goto clean_dma_ret;
3268 }
3269
Grygorii Strashko4a88fb92016-12-06 18:00:42 -06003270 cpsw->cpts = cpts_create(cpsw->dev, cpts_regs, cpsw->dev->of_node);
Grygorii Strashko8a2c9a52016-12-06 18:00:41 -06003271 if (IS_ERR(cpsw->cpts)) {
3272 ret = PTR_ERR(cpsw->cpts);
Grygorii Strashko1971ab52017-11-30 18:21:19 -06003273 goto clean_dma_ret;
Grygorii Strashko8a2c9a52016-12-06 18:00:41 -06003274 }
3275
Felipe Balbic03abd82015-01-16 10:11:12 -06003276 ndev->irq = platform_get_irq(pdev, 1);
Mugunthan V Ndf828592012-03-18 20:17:54 +00003277 if (ndev->irq < 0) {
3278 dev_err(priv->dev, "error getting irq resource\n");
Julia Lawallc1e33342015-12-26 20:12:13 +01003279 ret = ndev->irq;
Grygorii Strashko1971ab52017-11-30 18:21:19 -06003280 goto clean_dma_ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00003281 }
3282
Grygorii Strashkoa3a41d22018-03-15 15:15:50 -05003283 ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_CTAG_RX;
Keerthy070f9c62017-07-20 16:59:52 +05303284
3285 ndev->netdev_ops = &cpsw_netdev_ops;
3286 ndev->ethtool_ops = &cpsw_ethtool_ops;
Ivan Khoronzhuk9611d6d2018-05-17 01:21:45 +03003287 netif_napi_add(ndev, &cpsw->napi_rx,
3288 cpsw->quirk_irq ? cpsw_rx_poll : cpsw_rx_mq_poll,
3289 CPSW_POLL_WEIGHT);
3290 netif_tx_napi_add(ndev, &cpsw->napi_tx,
3291 cpsw->quirk_irq ? cpsw_tx_poll : cpsw_tx_mq_poll,
3292 CPSW_POLL_WEIGHT);
Keerthy070f9c62017-07-20 16:59:52 +05303293 cpsw_split_res(ndev);
3294
3295 /* register the network device */
3296 SET_NETDEV_DEV(ndev, &pdev->dev);
3297 ret = register_netdev(ndev);
3298 if (ret) {
3299 dev_err(priv->dev, "error registering net device\n");
3300 ret = -ENODEV;
Grygorii Strashko1971ab52017-11-30 18:21:19 -06003301 goto clean_dma_ret;
Keerthy070f9c62017-07-20 16:59:52 +05303302 }
3303
3304 if (cpsw->data.dual_emac) {
3305 ret = cpsw_probe_dual_emac(priv);
3306 if (ret) {
3307 cpsw_err(priv, probe, "error probe slave 2 emac interface\n");
3308 goto clean_unregister_netdev_ret;
3309 }
3310 }
3311
Felipe Balbic03abd82015-01-16 10:11:12 -06003312 /* Grab RX and TX IRQs. Note that we also have RX_THRESHOLD and
3313 * MISC IRQs which are always kept disabled with this driver so
3314 * we will not request them.
3315 *
3316 * If anyone wants to implement support for those, make sure to
3317 * first request and append them to irqs_table array.
3318 */
Daniel Mackc2b32e52014-09-04 09:00:23 +02003319
Felipe Balbic03abd82015-01-16 10:11:12 -06003320 /* RX IRQ */
Felipe Balbi5087b912015-01-16 10:11:11 -06003321 irq = platform_get_irq(pdev, 1);
Julia Lawallc1e33342015-12-26 20:12:13 +01003322 if (irq < 0) {
3323 ret = irq;
Grygorii Strashko1971ab52017-11-30 18:21:19 -06003324 goto clean_dma_ret;
Julia Lawallc1e33342015-12-26 20:12:13 +01003325 }
Felipe Balbi5087b912015-01-16 10:11:11 -06003326
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +03003327 cpsw->irqs_table[0] = irq;
Felipe Balbic03abd82015-01-16 10:11:12 -06003328 ret = devm_request_irq(&pdev->dev, irq, cpsw_rx_interrupt,
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +03003329 0, dev_name(&pdev->dev), cpsw);
Felipe Balbi5087b912015-01-16 10:11:11 -06003330 if (ret < 0) {
3331 dev_err(priv->dev, "error attaching irq (%d)\n", ret);
Grygorii Strashko1971ab52017-11-30 18:21:19 -06003332 goto clean_dma_ret;
Felipe Balbi5087b912015-01-16 10:11:11 -06003333 }
3334
Felipe Balbic03abd82015-01-16 10:11:12 -06003335 /* TX IRQ */
Felipe Balbi5087b912015-01-16 10:11:11 -06003336 irq = platform_get_irq(pdev, 2);
Julia Lawallc1e33342015-12-26 20:12:13 +01003337 if (irq < 0) {
3338 ret = irq;
Grygorii Strashko1971ab52017-11-30 18:21:19 -06003339 goto clean_dma_ret;
Julia Lawallc1e33342015-12-26 20:12:13 +01003340 }
Felipe Balbi5087b912015-01-16 10:11:11 -06003341
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +03003342 cpsw->irqs_table[1] = irq;
Felipe Balbic03abd82015-01-16 10:11:12 -06003343 ret = devm_request_irq(&pdev->dev, irq, cpsw_tx_interrupt,
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +03003344 0, dev_name(&pdev->dev), cpsw);
Felipe Balbi5087b912015-01-16 10:11:11 -06003345 if (ret < 0) {
3346 dev_err(priv->dev, "error attaching irq (%d)\n", ret);
Grygorii Strashko1971ab52017-11-30 18:21:19 -06003347 goto clean_dma_ret;
Felipe Balbi5087b912015-01-16 10:11:11 -06003348 }
Daniel Mackc2b32e52014-09-04 09:00:23 +02003349
Grygorii Strashko90225bf2017-01-06 14:07:33 -06003350 cpsw_notice(priv, probe,
3351 "initialized device (regs %pa, irq %d, pool size %d)\n",
3352 &ss_res->start, ndev->irq, dma_params.descs_pool_size);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00003353
Johan Hovoldc46ab7e2016-11-17 17:39:58 +01003354 pm_runtime_put(&pdev->dev);
3355
Mugunthan V Ndf828592012-03-18 20:17:54 +00003356 return 0;
3357
Johan Hovolda7fe9d42016-11-17 17:40:02 +01003358clean_unregister_netdev_ret:
3359 unregister_netdev(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00003360clean_dma_ret:
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03003361 cpdma_ctlr_destroy(cpsw->dma);
Johan Hovolda4e32b02016-11-17 17:40:00 +01003362clean_dt_ret:
3363 cpsw_remove_dt(pdev);
Johan Hovoldc46ab7e2016-11-17 17:39:58 +01003364 pm_runtime_put_sync(&pdev->dev);
Daniel Mackaa1a15e2013-09-21 00:50:38 +05303365clean_runtime_disable_ret:
Mugunthan V Nf150bd72012-07-17 08:09:50 +00003366 pm_runtime_disable(&pdev->dev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00003367clean_ndev_ret:
Sebastian Siewiord1bd9ac2013-04-24 08:48:23 +00003368 free_netdev(priv->ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00003369 return ret;
3370}
3371
Bill Pemberton663e12e2012-12-03 09:23:45 -05003372static int cpsw_remove(struct platform_device *pdev)
Mugunthan V Ndf828592012-03-18 20:17:54 +00003373{
3374 struct net_device *ndev = platform_get_drvdata(pdev);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03003375 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Grygorii Strashko8a0b6dc2016-07-28 20:50:35 +03003376 int ret;
3377
3378 ret = pm_runtime_get_sync(&pdev->dev);
3379 if (ret < 0) {
3380 pm_runtime_put_noidle(&pdev->dev);
3381 return ret;
3382 }
Mugunthan V Ndf828592012-03-18 20:17:54 +00003383
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03003384 if (cpsw->data.dual_emac)
3385 unregister_netdev(cpsw->slaves[1].ndev);
Sebastian Siewiord1bd9ac2013-04-24 08:48:23 +00003386 unregister_netdev(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00003387
Grygorii Strashko8a2c9a52016-12-06 18:00:41 -06003388 cpts_release(cpsw->cpts);
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03003389 cpdma_ctlr_destroy(cpsw->dma);
Johan Hovolda4e32b02016-11-17 17:40:00 +01003390 cpsw_remove_dt(pdev);
Grygorii Strashko8a0b6dc2016-07-28 20:50:35 +03003391 pm_runtime_put_sync(&pdev->dev);
3392 pm_runtime_disable(&pdev->dev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03003393 if (cpsw->data.dual_emac)
3394 free_netdev(cpsw->slaves[1].ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00003395 free_netdev(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00003396 return 0;
3397}
3398
Grygorii Strashko8963a502015-02-27 13:19:45 +02003399#ifdef CONFIG_PM_SLEEP
Mugunthan V Ndf828592012-03-18 20:17:54 +00003400static int cpsw_suspend(struct device *dev)
3401{
3402 struct platform_device *pdev = to_platform_device(dev);
3403 struct net_device *ndev = platform_get_drvdata(pdev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03003404 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00003405
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03003406 if (cpsw->data.dual_emac) {
Mugunthan V N618073e2014-09-11 22:52:38 +05303407 int i;
Daniel Mack1e7a2e22013-11-15 08:29:16 +01003408
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03003409 for (i = 0; i < cpsw->data.slaves; i++) {
3410 if (netif_running(cpsw->slaves[i].ndev))
3411 cpsw_ndo_stop(cpsw->slaves[i].ndev);
Mugunthan V N618073e2014-09-11 22:52:38 +05303412 }
3413 } else {
3414 if (netif_running(ndev))
3415 cpsw_ndo_stop(ndev);
Mugunthan V N618073e2014-09-11 22:52:38 +05303416 }
Daniel Mack1e7a2e22013-11-15 08:29:16 +01003417
Mugunthan V N739683b2013-06-06 23:45:14 +05303418 /* Select sleep pin state */
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03003419 pinctrl_pm_select_sleep_state(dev);
Mugunthan V N739683b2013-06-06 23:45:14 +05303420
Mugunthan V Ndf828592012-03-18 20:17:54 +00003421 return 0;
3422}
3423
3424static int cpsw_resume(struct device *dev)
3425{
3426 struct platform_device *pdev = to_platform_device(dev);
3427 struct net_device *ndev = platform_get_drvdata(pdev);
Ivan Khoronzhuka60ced92017-02-14 14:42:15 +02003428 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00003429
Mugunthan V N739683b2013-06-06 23:45:14 +05303430 /* Select default pin state */
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03003431 pinctrl_pm_select_default_state(dev);
Mugunthan V N739683b2013-06-06 23:45:14 +05303432
Grygorii Strashko4ccfd632016-11-29 16:27:03 -06003433 /* shut up ASSERT_RTNL() warning in netif_set_real_num_tx/rx_queues */
3434 rtnl_lock();
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03003435 if (cpsw->data.dual_emac) {
Mugunthan V N618073e2014-09-11 22:52:38 +05303436 int i;
3437
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03003438 for (i = 0; i < cpsw->data.slaves; i++) {
3439 if (netif_running(cpsw->slaves[i].ndev))
3440 cpsw_ndo_open(cpsw->slaves[i].ndev);
Mugunthan V N618073e2014-09-11 22:52:38 +05303441 }
3442 } else {
3443 if (netif_running(ndev))
3444 cpsw_ndo_open(ndev);
3445 }
Grygorii Strashko4ccfd632016-11-29 16:27:03 -06003446 rtnl_unlock();
3447
Mugunthan V Ndf828592012-03-18 20:17:54 +00003448 return 0;
3449}
Grygorii Strashko8963a502015-02-27 13:19:45 +02003450#endif
Mugunthan V Ndf828592012-03-18 20:17:54 +00003451
Grygorii Strashko8963a502015-02-27 13:19:45 +02003452static SIMPLE_DEV_PM_OPS(cpsw_pm_ops, cpsw_suspend, cpsw_resume);
Mugunthan V Ndf828592012-03-18 20:17:54 +00003453
3454static struct platform_driver cpsw_driver = {
3455 .driver = {
3456 .name = "cpsw",
Mugunthan V Ndf828592012-03-18 20:17:54 +00003457 .pm = &cpsw_pm_ops,
Sachin Kamat1e5c76d2013-09-30 09:55:12 +05303458 .of_match_table = cpsw_of_mtable,
Mugunthan V Ndf828592012-03-18 20:17:54 +00003459 },
3460 .probe = cpsw_probe,
Bill Pemberton663e12e2012-12-03 09:23:45 -05003461 .remove = cpsw_remove,
Mugunthan V Ndf828592012-03-18 20:17:54 +00003462};
3463
Grygorii Strashko6fb3b6b52015-10-23 14:41:12 +03003464module_platform_driver(cpsw_driver);
Mugunthan V Ndf828592012-03-18 20:17:54 +00003465
3466MODULE_LICENSE("GPL");
3467MODULE_AUTHOR("Cyril Chemparathy <cyril@ti.com>");
3468MODULE_AUTHOR("Mugunthan V N <mugunthanvnm@ti.com>");
3469MODULE_DESCRIPTION("TI CPSW Ethernet driver");