blob: 4065f9662bad380110ab01ee89b72a12e6b8adaa [file] [log] [blame]
Mugunthan V Ndf828592012-03-18 20:17:54 +00001/*
2 * Texas Instruments Ethernet Switch Driver
3 *
4 * Copyright (C) 2012 Texas Instruments
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation version 2.
9 *
10 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
11 * kind, whether express or implied; without even the implied warranty
12 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16#include <linux/kernel.h>
17#include <linux/io.h>
18#include <linux/clk.h>
19#include <linux/timer.h>
20#include <linux/module.h>
21#include <linux/platform_device.h>
22#include <linux/irqreturn.h>
23#include <linux/interrupt.h>
24#include <linux/if_ether.h>
25#include <linux/etherdevice.h>
26#include <linux/netdevice.h>
Richard Cochran2e5b38a2012-10-29 08:45:20 +000027#include <linux/net_tstamp.h>
Mugunthan V Ndf828592012-03-18 20:17:54 +000028#include <linux/phy.h>
29#include <linux/workqueue.h>
30#include <linux/delay.h>
Mugunthan V Nf150bd72012-07-17 08:09:50 +000031#include <linux/pm_runtime.h>
Mugunthan V N1d147cc2015-09-07 15:16:44 +053032#include <linux/gpio.h>
Mugunthan V N2eb32b02012-07-30 10:17:14 +000033#include <linux/of.h>
Heiko Schocher9e42f712015-10-17 06:04:35 +020034#include <linux/of_mdio.h>
Mugunthan V N2eb32b02012-07-30 10:17:14 +000035#include <linux/of_net.h>
36#include <linux/of_device.h>
Mugunthan V N3b72c2f2013-02-05 08:26:48 +000037#include <linux/if_vlan.h>
Mugunthan V Ndf828592012-03-18 20:17:54 +000038
Mugunthan V N739683b2013-06-06 23:45:14 +053039#include <linux/pinctrl/consumer.h>
Mugunthan V Ndf828592012-03-18 20:17:54 +000040
Mugunthan V Ndbe34722013-08-19 17:47:40 +053041#include "cpsw.h"
Mugunthan V Ndf828592012-03-18 20:17:54 +000042#include "cpsw_ale.h"
Richard Cochran2e5b38a2012-10-29 08:45:20 +000043#include "cpts.h"
Mugunthan V Ndf828592012-03-18 20:17:54 +000044#include "davinci_cpdma.h"
45
46#define CPSW_DEBUG (NETIF_MSG_HW | NETIF_MSG_WOL | \
47 NETIF_MSG_DRV | NETIF_MSG_LINK | \
48 NETIF_MSG_IFUP | NETIF_MSG_INTR | \
49 NETIF_MSG_PROBE | NETIF_MSG_TIMER | \
50 NETIF_MSG_IFDOWN | NETIF_MSG_RX_ERR | \
51 NETIF_MSG_TX_ERR | NETIF_MSG_TX_DONE | \
52 NETIF_MSG_PKTDATA | NETIF_MSG_TX_QUEUED | \
53 NETIF_MSG_RX_STATUS)
54
55#define cpsw_info(priv, type, format, ...) \
56do { \
57 if (netif_msg_##type(priv) && net_ratelimit()) \
58 dev_info(priv->dev, format, ## __VA_ARGS__); \
59} while (0)
60
61#define cpsw_err(priv, type, format, ...) \
62do { \
63 if (netif_msg_##type(priv) && net_ratelimit()) \
64 dev_err(priv->dev, format, ## __VA_ARGS__); \
65} while (0)
66
67#define cpsw_dbg(priv, type, format, ...) \
68do { \
69 if (netif_msg_##type(priv) && net_ratelimit()) \
70 dev_dbg(priv->dev, format, ## __VA_ARGS__); \
71} while (0)
72
73#define cpsw_notice(priv, type, format, ...) \
74do { \
75 if (netif_msg_##type(priv) && net_ratelimit()) \
76 dev_notice(priv->dev, format, ## __VA_ARGS__); \
77} while (0)
78
Mugunthan V N5c50a852012-10-29 08:45:11 +000079#define ALE_ALL_PORTS 0x7
80
Mugunthan V Ndf828592012-03-18 20:17:54 +000081#define CPSW_MAJOR_VERSION(reg) (reg >> 8 & 0x7)
82#define CPSW_MINOR_VERSION(reg) (reg & 0xff)
83#define CPSW_RTL_VERSION(reg) ((reg >> 11) & 0x1f)
84
Richard Cochrane90cfac2012-10-29 08:45:14 +000085#define CPSW_VERSION_1 0x19010a
86#define CPSW_VERSION_2 0x19010c
Mugunthan V Nc193f362013-08-05 17:30:05 +053087#define CPSW_VERSION_3 0x19010f
Mugunthan V N926489b2013-08-12 17:11:15 +053088#define CPSW_VERSION_4 0x190112
Richard Cochran549985e2012-11-14 09:07:56 +000089
90#define HOST_PORT_NUM 0
91#define SLIVER_SIZE 0x40
92
93#define CPSW1_HOST_PORT_OFFSET 0x028
94#define CPSW1_SLAVE_OFFSET 0x050
95#define CPSW1_SLAVE_SIZE 0x040
96#define CPSW1_CPDMA_OFFSET 0x100
97#define CPSW1_STATERAM_OFFSET 0x200
Mugunthan V Nd9718542013-07-23 15:38:17 +053098#define CPSW1_HW_STATS 0x400
Richard Cochran549985e2012-11-14 09:07:56 +000099#define CPSW1_CPTS_OFFSET 0x500
100#define CPSW1_ALE_OFFSET 0x600
101#define CPSW1_SLIVER_OFFSET 0x700
102
103#define CPSW2_HOST_PORT_OFFSET 0x108
104#define CPSW2_SLAVE_OFFSET 0x200
105#define CPSW2_SLAVE_SIZE 0x100
106#define CPSW2_CPDMA_OFFSET 0x800
Mugunthan V Nd9718542013-07-23 15:38:17 +0530107#define CPSW2_HW_STATS 0x900
Richard Cochran549985e2012-11-14 09:07:56 +0000108#define CPSW2_STATERAM_OFFSET 0xa00
109#define CPSW2_CPTS_OFFSET 0xc00
110#define CPSW2_ALE_OFFSET 0xd00
111#define CPSW2_SLIVER_OFFSET 0xd80
112#define CPSW2_BD_OFFSET 0x2000
113
Mugunthan V Ndf828592012-03-18 20:17:54 +0000114#define CPDMA_RXTHRESH 0x0c0
115#define CPDMA_RXFREE 0x0e0
116#define CPDMA_TXHDP 0x00
117#define CPDMA_RXHDP 0x20
118#define CPDMA_TXCP 0x40
119#define CPDMA_RXCP 0x60
120
Mugunthan V Ndf828592012-03-18 20:17:54 +0000121#define CPSW_POLL_WEIGHT 64
122#define CPSW_MIN_PACKET_SIZE 60
123#define CPSW_MAX_PACKET_SIZE (1500 + 14 + 4 + 4)
124
125#define RX_PRIORITY_MAPPING 0x76543210
126#define TX_PRIORITY_MAPPING 0x33221100
127#define CPDMA_TX_PRIORITY_MAP 0x76543210
128
Mugunthan V N3b72c2f2013-02-05 08:26:48 +0000129#define CPSW_VLAN_AWARE BIT(1)
130#define CPSW_ALE_VLAN_AWARE 1
131
John Ogness35717d82014-11-14 15:42:52 +0100132#define CPSW_FIFO_NORMAL_MODE (0 << 16)
133#define CPSW_FIFO_DUAL_MAC_MODE (1 << 16)
134#define CPSW_FIFO_RATE_LIMIT_MODE (2 << 16)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000135
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +0000136#define CPSW_INTPACEEN (0x3f << 16)
137#define CPSW_INTPRESCALE_MASK (0x7FF << 0)
138#define CPSW_CMINTMAX_CNT 63
139#define CPSW_CMINTMIN_CNT 2
140#define CPSW_CMINTMAX_INTVL (1000 / CPSW_CMINTMIN_CNT)
141#define CPSW_CMINTMIN_INTVL ((1000 / CPSW_CMINTMAX_CNT) + 1)
142
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300143#define cpsw_slave_index(cpsw, priv) \
144 ((cpsw->data.dual_emac) ? priv->emac_port : \
145 cpsw->data.active_slave)
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +0300146#define IRQ_NUM 2
Mugunthan V Nd3bb9c52013-03-11 23:16:36 +0000147
Mugunthan V Ndf828592012-03-18 20:17:54 +0000148static int debug_level;
149module_param(debug_level, int, 0);
150MODULE_PARM_DESC(debug_level, "cpsw debug level (NETIF_MSG bits)");
151
152static int ale_ageout = 10;
153module_param(ale_ageout, int, 0);
154MODULE_PARM_DESC(ale_ageout, "cpsw ale ageout interval (seconds)");
155
156static int rx_packet_max = CPSW_MAX_PACKET_SIZE;
157module_param(rx_packet_max, int, 0);
158MODULE_PARM_DESC(rx_packet_max, "maximum receive packet size (bytes)");
159
Richard Cochran996a5c22012-10-29 08:45:12 +0000160struct cpsw_wr_regs {
Mugunthan V Ndf828592012-03-18 20:17:54 +0000161 u32 id_ver;
162 u32 soft_reset;
163 u32 control;
164 u32 int_control;
165 u32 rx_thresh_en;
166 u32 rx_en;
167 u32 tx_en;
168 u32 misc_en;
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +0000169 u32 mem_allign1[8];
170 u32 rx_thresh_stat;
171 u32 rx_stat;
172 u32 tx_stat;
173 u32 misc_stat;
174 u32 mem_allign2[8];
175 u32 rx_imax;
176 u32 tx_imax;
177
Mugunthan V Ndf828592012-03-18 20:17:54 +0000178};
179
Richard Cochran996a5c22012-10-29 08:45:12 +0000180struct cpsw_ss_regs {
Mugunthan V Ndf828592012-03-18 20:17:54 +0000181 u32 id_ver;
182 u32 control;
183 u32 soft_reset;
184 u32 stat_port_en;
185 u32 ptype;
Richard Cochranbd357af2012-10-29 08:45:13 +0000186 u32 soft_idle;
187 u32 thru_rate;
188 u32 gap_thresh;
189 u32 tx_start_wds;
190 u32 flow_control;
191 u32 vlan_ltype;
192 u32 ts_ltype;
193 u32 dlr_ltype;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000194};
195
Richard Cochran9750a3a2012-10-29 08:45:15 +0000196/* CPSW_PORT_V1 */
197#define CPSW1_MAX_BLKS 0x00 /* Maximum FIFO Blocks */
198#define CPSW1_BLK_CNT 0x04 /* FIFO Block Usage Count (Read Only) */
199#define CPSW1_TX_IN_CTL 0x08 /* Transmit FIFO Control */
200#define CPSW1_PORT_VLAN 0x0c /* VLAN Register */
201#define CPSW1_TX_PRI_MAP 0x10 /* Tx Header Priority to Switch Pri Mapping */
202#define CPSW1_TS_CTL 0x14 /* Time Sync Control */
203#define CPSW1_TS_SEQ_LTYPE 0x18 /* Time Sync Sequence ID Offset and Msg Type */
204#define CPSW1_TS_VLAN 0x1c /* Time Sync VLAN1 and VLAN2 */
205
206/* CPSW_PORT_V2 */
207#define CPSW2_CONTROL 0x00 /* Control Register */
208#define CPSW2_MAX_BLKS 0x08 /* Maximum FIFO Blocks */
209#define CPSW2_BLK_CNT 0x0c /* FIFO Block Usage Count (Read Only) */
210#define CPSW2_TX_IN_CTL 0x10 /* Transmit FIFO Control */
211#define CPSW2_PORT_VLAN 0x14 /* VLAN Register */
212#define CPSW2_TX_PRI_MAP 0x18 /* Tx Header Priority to Switch Pri Mapping */
213#define CPSW2_TS_SEQ_MTYPE 0x1c /* Time Sync Sequence ID Offset and Msg Type */
214
215/* CPSW_PORT_V1 and V2 */
216#define SA_LO 0x20 /* CPGMAC_SL Source Address Low */
217#define SA_HI 0x24 /* CPGMAC_SL Source Address High */
218#define SEND_PERCENT 0x28 /* Transmit Queue Send Percentages */
219
220/* CPSW_PORT_V2 only */
221#define RX_DSCP_PRI_MAP0 0x30 /* Rx DSCP Priority to Rx Packet Mapping */
222#define RX_DSCP_PRI_MAP1 0x34 /* Rx DSCP Priority to Rx Packet Mapping */
223#define RX_DSCP_PRI_MAP2 0x38 /* Rx DSCP Priority to Rx Packet Mapping */
224#define RX_DSCP_PRI_MAP3 0x3c /* Rx DSCP Priority to Rx Packet Mapping */
225#define RX_DSCP_PRI_MAP4 0x40 /* Rx DSCP Priority to Rx Packet Mapping */
226#define RX_DSCP_PRI_MAP5 0x44 /* Rx DSCP Priority to Rx Packet Mapping */
227#define RX_DSCP_PRI_MAP6 0x48 /* Rx DSCP Priority to Rx Packet Mapping */
228#define RX_DSCP_PRI_MAP7 0x4c /* Rx DSCP Priority to Rx Packet Mapping */
229
230/* Bit definitions for the CPSW2_CONTROL register */
231#define PASS_PRI_TAGGED (1<<24) /* Pass Priority Tagged */
232#define VLAN_LTYPE2_EN (1<<21) /* VLAN LTYPE 2 enable */
233#define VLAN_LTYPE1_EN (1<<20) /* VLAN LTYPE 1 enable */
234#define DSCP_PRI_EN (1<<16) /* DSCP Priority Enable */
235#define TS_320 (1<<14) /* Time Sync Dest Port 320 enable */
236#define TS_319 (1<<13) /* Time Sync Dest Port 319 enable */
237#define TS_132 (1<<12) /* Time Sync Dest IP Addr 132 enable */
238#define TS_131 (1<<11) /* Time Sync Dest IP Addr 131 enable */
239#define TS_130 (1<<10) /* Time Sync Dest IP Addr 130 enable */
240#define TS_129 (1<<9) /* Time Sync Dest IP Addr 129 enable */
George Cherian09c55372014-05-02 12:02:02 +0530241#define TS_TTL_NONZERO (1<<8) /* Time Sync Time To Live Non-zero enable */
242#define TS_ANNEX_F_EN (1<<6) /* Time Sync Annex F enable */
Richard Cochran9750a3a2012-10-29 08:45:15 +0000243#define TS_ANNEX_D_EN (1<<4) /* Time Sync Annex D enable */
244#define TS_LTYPE2_EN (1<<3) /* Time Sync LTYPE 2 enable */
245#define TS_LTYPE1_EN (1<<2) /* Time Sync LTYPE 1 enable */
246#define TS_TX_EN (1<<1) /* Time Sync Transmit Enable */
247#define TS_RX_EN (1<<0) /* Time Sync Receive Enable */
248
George Cherian09c55372014-05-02 12:02:02 +0530249#define CTRL_V2_TS_BITS \
250 (TS_320 | TS_319 | TS_132 | TS_131 | TS_130 | TS_129 |\
251 TS_TTL_NONZERO | TS_ANNEX_D_EN | TS_LTYPE1_EN)
Richard Cochran9750a3a2012-10-29 08:45:15 +0000252
George Cherian09c55372014-05-02 12:02:02 +0530253#define CTRL_V2_ALL_TS_MASK (CTRL_V2_TS_BITS | TS_TX_EN | TS_RX_EN)
254#define CTRL_V2_TX_TS_BITS (CTRL_V2_TS_BITS | TS_TX_EN)
255#define CTRL_V2_RX_TS_BITS (CTRL_V2_TS_BITS | TS_RX_EN)
256
257
258#define CTRL_V3_TS_BITS \
259 (TS_320 | TS_319 | TS_132 | TS_131 | TS_130 | TS_129 |\
260 TS_TTL_NONZERO | TS_ANNEX_F_EN | TS_ANNEX_D_EN |\
261 TS_LTYPE1_EN)
262
263#define CTRL_V3_ALL_TS_MASK (CTRL_V3_TS_BITS | TS_TX_EN | TS_RX_EN)
264#define CTRL_V3_TX_TS_BITS (CTRL_V3_TS_BITS | TS_TX_EN)
265#define CTRL_V3_RX_TS_BITS (CTRL_V3_TS_BITS | TS_RX_EN)
Richard Cochran9750a3a2012-10-29 08:45:15 +0000266
267/* Bit definitions for the CPSW2_TS_SEQ_MTYPE register */
268#define TS_SEQ_ID_OFFSET_SHIFT (16) /* Time Sync Sequence ID Offset */
269#define TS_SEQ_ID_OFFSET_MASK (0x3f)
270#define TS_MSG_TYPE_EN_SHIFT (0) /* Time Sync Message Type Enable */
271#define TS_MSG_TYPE_EN_MASK (0xffff)
272
273/* The PTP event messages - Sync, Delay_Req, Pdelay_Req, and Pdelay_Resp. */
274#define EVENT_MSG_BITS ((1<<0) | (1<<1) | (1<<2) | (1<<3))
Mugunthan V Ndf828592012-03-18 20:17:54 +0000275
Richard Cochran2e5b38a2012-10-29 08:45:20 +0000276/* Bit definitions for the CPSW1_TS_CTL register */
277#define CPSW_V1_TS_RX_EN BIT(0)
278#define CPSW_V1_TS_TX_EN BIT(4)
279#define CPSW_V1_MSG_TYPE_OFS 16
280
281/* Bit definitions for the CPSW1_TS_SEQ_LTYPE register */
282#define CPSW_V1_SEQ_ID_OFS_SHIFT 16
283
Mugunthan V Ndf828592012-03-18 20:17:54 +0000284struct cpsw_host_regs {
285 u32 max_blks;
286 u32 blk_cnt;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000287 u32 tx_in_ctl;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000288 u32 port_vlan;
289 u32 tx_pri_map;
290 u32 cpdma_tx_pri_map;
291 u32 cpdma_rx_chan_map;
292};
293
294struct cpsw_sliver_regs {
295 u32 id_ver;
296 u32 mac_control;
297 u32 mac_status;
298 u32 soft_reset;
299 u32 rx_maxlen;
300 u32 __reserved_0;
301 u32 rx_pause;
302 u32 tx_pause;
303 u32 __reserved_1;
304 u32 rx_pri_map;
305};
306
Mugunthan V Nd9718542013-07-23 15:38:17 +0530307struct cpsw_hw_stats {
308 u32 rxgoodframes;
309 u32 rxbroadcastframes;
310 u32 rxmulticastframes;
311 u32 rxpauseframes;
312 u32 rxcrcerrors;
313 u32 rxaligncodeerrors;
314 u32 rxoversizedframes;
315 u32 rxjabberframes;
316 u32 rxundersizedframes;
317 u32 rxfragments;
318 u32 __pad_0[2];
319 u32 rxoctets;
320 u32 txgoodframes;
321 u32 txbroadcastframes;
322 u32 txmulticastframes;
323 u32 txpauseframes;
324 u32 txdeferredframes;
325 u32 txcollisionframes;
326 u32 txsinglecollframes;
327 u32 txmultcollframes;
328 u32 txexcessivecollisions;
329 u32 txlatecollisions;
330 u32 txunderrun;
331 u32 txcarriersenseerrors;
332 u32 txoctets;
333 u32 octetframes64;
334 u32 octetframes65t127;
335 u32 octetframes128t255;
336 u32 octetframes256t511;
337 u32 octetframes512t1023;
338 u32 octetframes1024tup;
339 u32 netoctets;
340 u32 rxsofoverruns;
341 u32 rxmofoverruns;
342 u32 rxdmaoverruns;
343};
344
Mugunthan V Ndf828592012-03-18 20:17:54 +0000345struct cpsw_slave {
Richard Cochran9750a3a2012-10-29 08:45:15 +0000346 void __iomem *regs;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000347 struct cpsw_sliver_regs __iomem *sliver;
348 int slave_num;
349 u32 mac_control;
350 struct cpsw_slave_data *data;
351 struct phy_device *phy;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000352 struct net_device *ndev;
353 u32 port_vlan;
354 u32 open_stat;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000355};
356
Richard Cochran9750a3a2012-10-29 08:45:15 +0000357static inline u32 slave_read(struct cpsw_slave *slave, u32 offset)
358{
359 return __raw_readl(slave->regs + offset);
360}
361
362static inline void slave_write(struct cpsw_slave *slave, u32 val, u32 offset)
363{
364 __raw_writel(val, slave->regs + offset);
365}
366
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +0300367struct cpsw_common {
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +0300368 struct device *dev;
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300369 struct cpsw_platform_data data;
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +0300370 struct napi_struct napi_rx;
371 struct napi_struct napi_tx;
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +0300372 struct cpsw_ss_regs __iomem *regs;
373 struct cpsw_wr_regs __iomem *wr_regs;
374 u8 __iomem *hw_stats;
375 struct cpsw_host_regs __iomem *host_port_regs;
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300376 u32 version;
377 u32 coal_intvl;
378 u32 bus_freq_mhz;
379 int rx_packet_max;
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300380 struct cpsw_slave *slaves;
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +0300381 struct cpdma_ctlr *dma;
382 struct cpdma_chan *txch, *rxch;
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300383 struct cpsw_ale *ale;
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +0300384 bool quirk_irq;
385 bool rx_irq_disabled;
386 bool tx_irq_disabled;
387 u32 irqs_table[IRQ_NUM];
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300388 struct cpts *cpts;
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +0300389};
390
391struct cpsw_priv {
Mugunthan V Ndf828592012-03-18 20:17:54 +0000392 struct net_device *ndev;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000393 struct device *dev;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000394 u32 msg_enable;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000395 u8 mac_addr[ETH_ALEN];
Mugunthan V N1923d6e2014-09-08 22:54:02 +0530396 bool rx_pause;
397 bool tx_pause;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000398 u32 emac_port;
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +0300399 struct cpsw_common *cpsw;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000400};
401
Mugunthan V Nd9718542013-07-23 15:38:17 +0530402struct cpsw_stats {
403 char stat_string[ETH_GSTRING_LEN];
404 int type;
405 int sizeof_stat;
406 int stat_offset;
407};
408
409enum {
410 CPSW_STATS,
411 CPDMA_RX_STATS,
412 CPDMA_TX_STATS,
413};
414
415#define CPSW_STAT(m) CPSW_STATS, \
416 sizeof(((struct cpsw_hw_stats *)0)->m), \
417 offsetof(struct cpsw_hw_stats, m)
418#define CPDMA_RX_STAT(m) CPDMA_RX_STATS, \
419 sizeof(((struct cpdma_chan_stats *)0)->m), \
420 offsetof(struct cpdma_chan_stats, m)
421#define CPDMA_TX_STAT(m) CPDMA_TX_STATS, \
422 sizeof(((struct cpdma_chan_stats *)0)->m), \
423 offsetof(struct cpdma_chan_stats, m)
424
425static const struct cpsw_stats cpsw_gstrings_stats[] = {
426 { "Good Rx Frames", CPSW_STAT(rxgoodframes) },
427 { "Broadcast Rx Frames", CPSW_STAT(rxbroadcastframes) },
428 { "Multicast Rx Frames", CPSW_STAT(rxmulticastframes) },
429 { "Pause Rx Frames", CPSW_STAT(rxpauseframes) },
430 { "Rx CRC Errors", CPSW_STAT(rxcrcerrors) },
431 { "Rx Align/Code Errors", CPSW_STAT(rxaligncodeerrors) },
432 { "Oversize Rx Frames", CPSW_STAT(rxoversizedframes) },
433 { "Rx Jabbers", CPSW_STAT(rxjabberframes) },
434 { "Undersize (Short) Rx Frames", CPSW_STAT(rxundersizedframes) },
435 { "Rx Fragments", CPSW_STAT(rxfragments) },
436 { "Rx Octets", CPSW_STAT(rxoctets) },
437 { "Good Tx Frames", CPSW_STAT(txgoodframes) },
438 { "Broadcast Tx Frames", CPSW_STAT(txbroadcastframes) },
439 { "Multicast Tx Frames", CPSW_STAT(txmulticastframes) },
440 { "Pause Tx Frames", CPSW_STAT(txpauseframes) },
441 { "Deferred Tx Frames", CPSW_STAT(txdeferredframes) },
442 { "Collisions", CPSW_STAT(txcollisionframes) },
443 { "Single Collision Tx Frames", CPSW_STAT(txsinglecollframes) },
444 { "Multiple Collision Tx Frames", CPSW_STAT(txmultcollframes) },
445 { "Excessive Collisions", CPSW_STAT(txexcessivecollisions) },
446 { "Late Collisions", CPSW_STAT(txlatecollisions) },
447 { "Tx Underrun", CPSW_STAT(txunderrun) },
448 { "Carrier Sense Errors", CPSW_STAT(txcarriersenseerrors) },
449 { "Tx Octets", CPSW_STAT(txoctets) },
450 { "Rx + Tx 64 Octet Frames", CPSW_STAT(octetframes64) },
451 { "Rx + Tx 65-127 Octet Frames", CPSW_STAT(octetframes65t127) },
452 { "Rx + Tx 128-255 Octet Frames", CPSW_STAT(octetframes128t255) },
453 { "Rx + Tx 256-511 Octet Frames", CPSW_STAT(octetframes256t511) },
454 { "Rx + Tx 512-1023 Octet Frames", CPSW_STAT(octetframes512t1023) },
455 { "Rx + Tx 1024-Up Octet Frames", CPSW_STAT(octetframes1024tup) },
456 { "Net Octets", CPSW_STAT(netoctets) },
457 { "Rx Start of Frame Overruns", CPSW_STAT(rxsofoverruns) },
458 { "Rx Middle of Frame Overruns", CPSW_STAT(rxmofoverruns) },
459 { "Rx DMA Overruns", CPSW_STAT(rxdmaoverruns) },
460 { "Rx DMA chan: head_enqueue", CPDMA_RX_STAT(head_enqueue) },
461 { "Rx DMA chan: tail_enqueue", CPDMA_RX_STAT(tail_enqueue) },
462 { "Rx DMA chan: pad_enqueue", CPDMA_RX_STAT(pad_enqueue) },
463 { "Rx DMA chan: misqueued", CPDMA_RX_STAT(misqueued) },
464 { "Rx DMA chan: desc_alloc_fail", CPDMA_RX_STAT(desc_alloc_fail) },
465 { "Rx DMA chan: pad_alloc_fail", CPDMA_RX_STAT(pad_alloc_fail) },
466 { "Rx DMA chan: runt_receive_buf", CPDMA_RX_STAT(runt_receive_buff) },
467 { "Rx DMA chan: runt_transmit_buf", CPDMA_RX_STAT(runt_transmit_buff) },
468 { "Rx DMA chan: empty_dequeue", CPDMA_RX_STAT(empty_dequeue) },
469 { "Rx DMA chan: busy_dequeue", CPDMA_RX_STAT(busy_dequeue) },
470 { "Rx DMA chan: good_dequeue", CPDMA_RX_STAT(good_dequeue) },
471 { "Rx DMA chan: requeue", CPDMA_RX_STAT(requeue) },
472 { "Rx DMA chan: teardown_dequeue", CPDMA_RX_STAT(teardown_dequeue) },
473 { "Tx DMA chan: head_enqueue", CPDMA_TX_STAT(head_enqueue) },
474 { "Tx DMA chan: tail_enqueue", CPDMA_TX_STAT(tail_enqueue) },
475 { "Tx DMA chan: pad_enqueue", CPDMA_TX_STAT(pad_enqueue) },
476 { "Tx DMA chan: misqueued", CPDMA_TX_STAT(misqueued) },
477 { "Tx DMA chan: desc_alloc_fail", CPDMA_TX_STAT(desc_alloc_fail) },
478 { "Tx DMA chan: pad_alloc_fail", CPDMA_TX_STAT(pad_alloc_fail) },
479 { "Tx DMA chan: runt_receive_buf", CPDMA_TX_STAT(runt_receive_buff) },
480 { "Tx DMA chan: runt_transmit_buf", CPDMA_TX_STAT(runt_transmit_buff) },
481 { "Tx DMA chan: empty_dequeue", CPDMA_TX_STAT(empty_dequeue) },
482 { "Tx DMA chan: busy_dequeue", CPDMA_TX_STAT(busy_dequeue) },
483 { "Tx DMA chan: good_dequeue", CPDMA_TX_STAT(good_dequeue) },
484 { "Tx DMA chan: requeue", CPDMA_TX_STAT(requeue) },
485 { "Tx DMA chan: teardown_dequeue", CPDMA_TX_STAT(teardown_dequeue) },
486};
487
488#define CPSW_STATS_LEN ARRAY_SIZE(cpsw_gstrings_stats)
489
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +0300490#define ndev_to_cpsw(ndev) (((struct cpsw_priv *)netdev_priv(ndev))->cpsw)
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +0300491#define napi_to_cpsw(napi) container_of(napi, struct cpsw_common, napi)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000492#define for_each_slave(priv, func, arg...) \
493 do { \
Sebastian Siewior6e6ceae2013-04-24 08:48:24 +0000494 struct cpsw_slave *slave; \
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300495 struct cpsw_common *cpsw = (priv)->cpsw; \
Sebastian Siewior6e6ceae2013-04-24 08:48:24 +0000496 int n; \
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300497 if (cpsw->data.dual_emac) \
498 (func)((cpsw)->slaves + priv->emac_port, ##arg);\
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000499 else \
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300500 for (n = cpsw->data.slaves, \
501 slave = cpsw->slaves; \
Sebastian Siewior6e6ceae2013-04-24 08:48:24 +0000502 n; n--) \
503 (func)(slave++, ##arg); \
Mugunthan V Ndf828592012-03-18 20:17:54 +0000504 } while (0)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000505
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300506#define cpsw_dual_emac_src_port_detect(cpsw, status, ndev, skb) \
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000507 do { \
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300508 if (!cpsw->data.dual_emac) \
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000509 break; \
510 if (CPDMA_RX_SOURCE_PORT(status) == 1) { \
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300511 ndev = cpsw->slaves[0].ndev; \
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000512 skb->dev = ndev; \
513 } else if (CPDMA_RX_SOURCE_PORT(status) == 2) { \
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300514 ndev = cpsw->slaves[1].ndev; \
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000515 skb->dev = ndev; \
516 } \
517 } while (0)
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300518#define cpsw_add_mcast(cpsw, priv, addr) \
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000519 do { \
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300520 if (cpsw->data.dual_emac) { \
521 struct cpsw_slave *slave = cpsw->slaves + \
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000522 priv->emac_port; \
Ivan Khoronzhuk6f1f5832016-08-10 02:22:34 +0300523 int slave_port = cpsw_get_slave_port( \
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000524 slave->slave_num); \
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300525 cpsw_ale_add_mcast(cpsw->ale, addr, \
Grygorii Strashko71a2cbb2016-04-07 15:16:44 +0300526 1 << slave_port | ALE_PORT_HOST, \
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000527 ALE_VLAN, slave->port_vlan, 0); \
528 } else { \
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300529 cpsw_ale_add_mcast(cpsw->ale, addr, \
Grygorii Strashko61f1cef2016-04-07 15:16:43 +0300530 ALE_ALL_PORTS, \
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000531 0, 0, 0); \
532 } \
533 } while (0)
534
Ivan Khoronzhuk6f1f5832016-08-10 02:22:34 +0300535static inline int cpsw_get_slave_port(u32 slave_num)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000536{
Grygorii Strashko71a2cbb2016-04-07 15:16:44 +0300537 return slave_num + 1;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000538}
Mugunthan V Ndf828592012-03-18 20:17:54 +0000539
Mugunthan V N0cd8f9c2014-01-23 00:03:12 +0530540static void cpsw_set_promiscious(struct net_device *ndev, bool enable)
541{
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300542 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
543 struct cpsw_ale *ale = cpsw->ale;
Mugunthan V N0cd8f9c2014-01-23 00:03:12 +0530544 int i;
545
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300546 if (cpsw->data.dual_emac) {
Mugunthan V N0cd8f9c2014-01-23 00:03:12 +0530547 bool flag = false;
548
549 /* Enabling promiscuous mode for one interface will be
550 * common for both the interface as the interface shares
551 * the same hardware resource.
552 */
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300553 for (i = 0; i < cpsw->data.slaves; i++)
554 if (cpsw->slaves[i].ndev->flags & IFF_PROMISC)
Mugunthan V N0cd8f9c2014-01-23 00:03:12 +0530555 flag = true;
556
557 if (!enable && flag) {
558 enable = true;
559 dev_err(&ndev->dev, "promiscuity not disabled as the other interface is still in promiscuity mode\n");
560 }
561
562 if (enable) {
563 /* Enable Bypass */
564 cpsw_ale_control_set(ale, 0, ALE_BYPASS, 1);
565
566 dev_dbg(&ndev->dev, "promiscuity enabled\n");
567 } else {
568 /* Disable Bypass */
569 cpsw_ale_control_set(ale, 0, ALE_BYPASS, 0);
570 dev_dbg(&ndev->dev, "promiscuity disabled\n");
571 }
572 } else {
573 if (enable) {
574 unsigned long timeout = jiffies + HZ;
575
Lennart Sorensen6f979eb2014-10-31 13:28:54 -0400576 /* Disable Learn for all ports (host is port 0 and slaves are port 1 and up */
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300577 for (i = 0; i <= cpsw->data.slaves; i++) {
Mugunthan V N0cd8f9c2014-01-23 00:03:12 +0530578 cpsw_ale_control_set(ale, i,
579 ALE_PORT_NOLEARN, 1);
580 cpsw_ale_control_set(ale, i,
581 ALE_PORT_NO_SA_UPDATE, 1);
582 }
583
584 /* Clear All Untouched entries */
585 cpsw_ale_control_set(ale, 0, ALE_AGEOUT, 1);
586 do {
587 cpu_relax();
588 if (cpsw_ale_control_get(ale, 0, ALE_AGEOUT))
589 break;
590 } while (time_after(timeout, jiffies));
591 cpsw_ale_control_set(ale, 0, ALE_AGEOUT, 1);
592
593 /* Clear all mcast from ALE */
Grygorii Strashko61f1cef2016-04-07 15:16:43 +0300594 cpsw_ale_flush_multicast(ale, ALE_ALL_PORTS, -1);
Mugunthan V N0cd8f9c2014-01-23 00:03:12 +0530595
596 /* Flood All Unicast Packets to Host port */
597 cpsw_ale_control_set(ale, 0, ALE_P0_UNI_FLOOD, 1);
598 dev_dbg(&ndev->dev, "promiscuity enabled\n");
599 } else {
Lennart Sorensen6f979eb2014-10-31 13:28:54 -0400600 /* Don't Flood All Unicast Packets to Host port */
Mugunthan V N0cd8f9c2014-01-23 00:03:12 +0530601 cpsw_ale_control_set(ale, 0, ALE_P0_UNI_FLOOD, 0);
602
Lennart Sorensen6f979eb2014-10-31 13:28:54 -0400603 /* Enable Learn for all ports (host is port 0 and slaves are port 1 and up */
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300604 for (i = 0; i <= cpsw->data.slaves; i++) {
Mugunthan V N0cd8f9c2014-01-23 00:03:12 +0530605 cpsw_ale_control_set(ale, i,
606 ALE_PORT_NOLEARN, 0);
607 cpsw_ale_control_set(ale, i,
608 ALE_PORT_NO_SA_UPDATE, 0);
609 }
610 dev_dbg(&ndev->dev, "promiscuity disabled\n");
611 }
612 }
613}
614
Mugunthan V N5c50a852012-10-29 08:45:11 +0000615static void cpsw_ndo_set_rx_mode(struct net_device *ndev)
616{
617 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300618 struct cpsw_common *cpsw = priv->cpsw;
Mugunthan V N25906052015-01-13 17:35:49 +0530619 int vid;
620
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300621 if (cpsw->data.dual_emac)
622 vid = cpsw->slaves[priv->emac_port].port_vlan;
Mugunthan V N25906052015-01-13 17:35:49 +0530623 else
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300624 vid = cpsw->data.default_vlan;
Mugunthan V N5c50a852012-10-29 08:45:11 +0000625
626 if (ndev->flags & IFF_PROMISC) {
627 /* Enable promiscuous mode */
Mugunthan V N0cd8f9c2014-01-23 00:03:12 +0530628 cpsw_set_promiscious(ndev, true);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300629 cpsw_ale_set_allmulti(cpsw->ale, IFF_ALLMULTI);
Mugunthan V N5c50a852012-10-29 08:45:11 +0000630 return;
Mugunthan V N0cd8f9c2014-01-23 00:03:12 +0530631 } else {
632 /* Disable promiscuous mode */
633 cpsw_set_promiscious(ndev, false);
Mugunthan V N5c50a852012-10-29 08:45:11 +0000634 }
635
Lennart Sorensen1e5c4bc2014-10-31 13:38:52 -0400636 /* Restore allmulti on vlans if necessary */
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300637 cpsw_ale_set_allmulti(cpsw->ale, priv->ndev->flags & IFF_ALLMULTI);
Lennart Sorensen1e5c4bc2014-10-31 13:38:52 -0400638
Mugunthan V N5c50a852012-10-29 08:45:11 +0000639 /* Clear all mcast from ALE */
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300640 cpsw_ale_flush_multicast(cpsw->ale, ALE_ALL_PORTS, vid);
Mugunthan V N5c50a852012-10-29 08:45:11 +0000641
642 if (!netdev_mc_empty(ndev)) {
643 struct netdev_hw_addr *ha;
644
645 /* program multicast address list into ALE register */
646 netdev_for_each_mc_addr(ha, ndev) {
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300647 cpsw_add_mcast(cpsw, priv, (u8 *)ha->addr);
Mugunthan V N5c50a852012-10-29 08:45:11 +0000648 }
649 }
650}
651
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +0300652static void cpsw_intr_enable(struct cpsw_common *cpsw)
Mugunthan V Ndf828592012-03-18 20:17:54 +0000653{
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +0300654 __raw_writel(0xFF, &cpsw->wr_regs->tx_en);
655 __raw_writel(0xFF, &cpsw->wr_regs->rx_en);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000656
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +0300657 cpdma_ctlr_int_ctrl(cpsw->dma, true);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000658 return;
659}
660
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +0300661static void cpsw_intr_disable(struct cpsw_common *cpsw)
Mugunthan V Ndf828592012-03-18 20:17:54 +0000662{
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +0300663 __raw_writel(0, &cpsw->wr_regs->tx_en);
664 __raw_writel(0, &cpsw->wr_regs->rx_en);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000665
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +0300666 cpdma_ctlr_int_ctrl(cpsw->dma, false);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000667 return;
668}
669
Olof Johansson1a3b5052013-12-11 15:58:07 -0800670static void cpsw_tx_handler(void *token, int len, int status)
Mugunthan V Ndf828592012-03-18 20:17:54 +0000671{
672 struct sk_buff *skb = token;
673 struct net_device *ndev = skb->dev;
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300674 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000675
Mugunthan V Nfae50822013-01-17 06:31:34 +0000676 /* Check whether the queue is stopped due to stalled tx dma, if the
677 * queue is stopped then start the queue as we have free desc for tx
678 */
Mugunthan V Ndf828592012-03-18 20:17:54 +0000679 if (unlikely(netif_queue_stopped(ndev)))
Mugunthan V Nb56d6b3f2013-03-27 04:41:59 +0000680 netif_wake_queue(ndev);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300681 cpts_tx_timestamp(cpsw->cpts, skb);
Tobias Klauser8dc43dd2014-03-10 13:12:23 +0100682 ndev->stats.tx_packets++;
683 ndev->stats.tx_bytes += len;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000684 dev_kfree_skb_any(skb);
685}
686
Olof Johansson1a3b5052013-12-11 15:58:07 -0800687static void cpsw_rx_handler(void *token, int len, int status)
Mugunthan V Ndf828592012-03-18 20:17:54 +0000688{
689 struct sk_buff *skb = token;
Sebastian Siewiorb4727e62013-04-23 07:31:39 +0000690 struct sk_buff *new_skb;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000691 struct net_device *ndev = skb->dev;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000692 int ret = 0;
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300693 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000694
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300695 cpsw_dual_emac_src_port_detect(cpsw, status, ndev, skb);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000696
Mugunthan V N16e5c572014-04-10 14:23:23 +0530697 if (unlikely(status < 0) || unlikely(!netif_running(ndev))) {
Mugunthan V Na0e2c822014-09-10 16:38:09 +0530698 bool ndev_status = false;
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300699 struct cpsw_slave *slave = cpsw->slaves;
Mugunthan V Na0e2c822014-09-10 16:38:09 +0530700 int n;
701
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300702 if (cpsw->data.dual_emac) {
Mugunthan V Na0e2c822014-09-10 16:38:09 +0530703 /* In dual emac mode check for all interfaces */
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300704 for (n = cpsw->data.slaves; n; n--, slave++)
Mugunthan V Na0e2c822014-09-10 16:38:09 +0530705 if (netif_running(slave->ndev))
706 ndev_status = true;
707 }
708
709 if (ndev_status && (status >= 0)) {
710 /* The packet received is for the interface which
711 * is already down and the other interface is up
Joe Perchesdbedd442015-03-06 20:49:12 -0800712 * and running, instead of freeing which results
Mugunthan V Na0e2c822014-09-10 16:38:09 +0530713 * in reducing of the number of rx descriptor in
714 * DMA engine, requeue skb back to cpdma.
715 */
716 new_skb = skb;
717 goto requeue;
718 }
719
Sebastian Siewiorb4727e62013-04-23 07:31:39 +0000720 /* the interface is going down, skbs are purged */
Mugunthan V Ndf828592012-03-18 20:17:54 +0000721 dev_kfree_skb_any(skb);
722 return;
723 }
Sebastian Siewiorb4727e62013-04-23 07:31:39 +0000724
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300725 new_skb = netdev_alloc_skb_ip_align(ndev, cpsw->rx_packet_max);
Sebastian Siewiorb4727e62013-04-23 07:31:39 +0000726 if (new_skb) {
Mugunthan V Ndf828592012-03-18 20:17:54 +0000727 skb_put(skb, len);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300728 cpts_rx_timestamp(cpsw->cpts, skb);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000729 skb->protocol = eth_type_trans(skb, ndev);
730 netif_receive_skb(skb);
Tobias Klauser8dc43dd2014-03-10 13:12:23 +0100731 ndev->stats.rx_bytes += len;
732 ndev->stats.rx_packets++;
Grygorii Strashko254a49d2016-08-09 15:09:44 +0300733 kmemleak_not_leak(new_skb);
Sebastian Siewiorb4727e62013-04-23 07:31:39 +0000734 } else {
Tobias Klauser8dc43dd2014-03-10 13:12:23 +0100735 ndev->stats.rx_dropped++;
Sebastian Siewiorb4727e62013-04-23 07:31:39 +0000736 new_skb = skb;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000737 }
738
Mugunthan V Na0e2c822014-09-10 16:38:09 +0530739requeue:
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +0300740 ret = cpdma_chan_submit(cpsw->rxch, new_skb, new_skb->data,
741 skb_tailroom(new_skb), 0);
Sebastian Siewiorb4727e62013-04-23 07:31:39 +0000742 if (WARN_ON(ret < 0))
743 dev_kfree_skb_any(new_skb);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000744}
745
Felipe Balbic03abd82015-01-16 10:11:12 -0600746static irqreturn_t cpsw_tx_interrupt(int irq, void *dev_id)
Mugunthan V Ndf828592012-03-18 20:17:54 +0000747{
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +0300748 struct cpsw_common *cpsw = dev_id;
Felipe Balbi7ce67a32015-01-02 16:15:59 -0600749
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +0300750 writel(0, &cpsw->wr_regs->tx_en);
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +0300751 cpdma_ctlr_eoi(cpsw->dma, CPDMA_EOI_TX);
Felipe Balbic03abd82015-01-16 10:11:12 -0600752
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +0300753 if (cpsw->quirk_irq) {
754 disable_irq_nosync(cpsw->irqs_table[1]);
755 cpsw->tx_irq_disabled = true;
Mugunthan V N7da11602015-08-12 15:22:53 +0530756 }
757
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +0300758 napi_schedule(&cpsw->napi_tx);
Felipe Balbic03abd82015-01-16 10:11:12 -0600759 return IRQ_HANDLED;
760}
761
762static irqreturn_t cpsw_rx_interrupt(int irq, void *dev_id)
763{
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +0300764 struct cpsw_common *cpsw = dev_id;
Felipe Balbic03abd82015-01-16 10:11:12 -0600765
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +0300766 cpdma_ctlr_eoi(cpsw->dma, CPDMA_EOI_RX);
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +0300767 writel(0, &cpsw->wr_regs->rx_en);
Sebastian Siewiorfd51cf12013-04-23 07:31:37 +0000768
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +0300769 if (cpsw->quirk_irq) {
770 disable_irq_nosync(cpsw->irqs_table[0]);
771 cpsw->rx_irq_disabled = true;
Mugunthan V N7da11602015-08-12 15:22:53 +0530772 }
773
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +0300774 napi_schedule(&cpsw->napi_rx);
Mugunthan V Nd354eb82015-08-04 16:06:19 +0530775 return IRQ_HANDLED;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000776}
777
Mugunthan V N32a74322015-08-04 16:06:20 +0530778static int cpsw_tx_poll(struct napi_struct *napi_tx, int budget)
Mugunthan V Ndf828592012-03-18 20:17:54 +0000779{
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +0300780 struct cpsw_common *cpsw = napi_to_cpsw(napi_tx);
Mugunthan V N32a74322015-08-04 16:06:20 +0530781 int num_tx;
782
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +0300783 num_tx = cpdma_chan_process(cpsw->txch, budget);
Mugunthan V N32a74322015-08-04 16:06:20 +0530784 if (num_tx < budget) {
785 napi_complete(napi_tx);
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +0300786 writel(0xff, &cpsw->wr_regs->tx_en);
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +0300787 if (cpsw->quirk_irq && cpsw->tx_irq_disabled) {
788 cpsw->tx_irq_disabled = false;
789 enable_irq(cpsw->irqs_table[1]);
Mugunthan V N7da11602015-08-12 15:22:53 +0530790 }
Mugunthan V N32a74322015-08-04 16:06:20 +0530791 }
792
Mugunthan V N32a74322015-08-04 16:06:20 +0530793 return num_tx;
794}
795
796static int cpsw_rx_poll(struct napi_struct *napi_rx, int budget)
797{
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +0300798 struct cpsw_common *cpsw = napi_to_cpsw(napi_rx);
Mugunthan V N1e353cd2015-07-21 16:00:42 +0530799 int num_rx;
Mugunthan V N510a1e722013-02-17 22:19:20 +0000800
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +0300801 num_rx = cpdma_chan_process(cpsw->rxch, budget);
Mugunthan V N510a1e722013-02-17 22:19:20 +0000802 if (num_rx < budget) {
Mugunthan V N32a74322015-08-04 16:06:20 +0530803 napi_complete(napi_rx);
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +0300804 writel(0xff, &cpsw->wr_regs->rx_en);
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +0300805 if (cpsw->quirk_irq && cpsw->rx_irq_disabled) {
806 cpsw->rx_irq_disabled = false;
807 enable_irq(cpsw->irqs_table[0]);
Mugunthan V N7da11602015-08-12 15:22:53 +0530808 }
Mugunthan V N510a1e722013-02-17 22:19:20 +0000809 }
Mugunthan V Ndf828592012-03-18 20:17:54 +0000810
Mugunthan V Ndf828592012-03-18 20:17:54 +0000811 return num_rx;
812}
813
814static inline void soft_reset(const char *module, void __iomem *reg)
815{
816 unsigned long timeout = jiffies + HZ;
817
818 __raw_writel(1, reg);
819 do {
820 cpu_relax();
821 } while ((__raw_readl(reg) & 1) && time_after(timeout, jiffies));
822
823 WARN(__raw_readl(reg) & 1, "failed to soft-reset %s\n", module);
824}
825
826#define mac_hi(mac) (((mac)[0] << 0) | ((mac)[1] << 8) | \
827 ((mac)[2] << 16) | ((mac)[3] << 24))
828#define mac_lo(mac) (((mac)[4] << 0) | ((mac)[5] << 8))
829
830static void cpsw_set_slave_mac(struct cpsw_slave *slave,
831 struct cpsw_priv *priv)
832{
Richard Cochran9750a3a2012-10-29 08:45:15 +0000833 slave_write(slave, mac_hi(priv->mac_addr), SA_HI);
834 slave_write(slave, mac_lo(priv->mac_addr), SA_LO);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000835}
836
837static void _cpsw_adjust_link(struct cpsw_slave *slave,
838 struct cpsw_priv *priv, bool *link)
839{
840 struct phy_device *phy = slave->phy;
841 u32 mac_control = 0;
842 u32 slave_port;
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300843 struct cpsw_common *cpsw = priv->cpsw;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000844
845 if (!phy)
846 return;
847
Ivan Khoronzhuk6f1f5832016-08-10 02:22:34 +0300848 slave_port = cpsw_get_slave_port(slave->slave_num);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000849
850 if (phy->link) {
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300851 mac_control = cpsw->data.mac_control;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000852
853 /* enable forwarding */
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300854 cpsw_ale_control_set(cpsw->ale, slave_port,
Mugunthan V Ndf828592012-03-18 20:17:54 +0000855 ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
856
857 if (phy->speed == 1000)
858 mac_control |= BIT(7); /* GIGABITEN */
859 if (phy->duplex)
860 mac_control |= BIT(0); /* FULLDUPLEXEN */
Daniel Mack342b7b72012-09-27 09:19:34 +0000861
862 /* set speed_in input in case RMII mode is used in 100Mbps */
863 if (phy->speed == 100)
864 mac_control |= BIT(15);
Mugunthan V Na81d8762013-12-13 18:42:55 +0530865 else if (phy->speed == 10)
866 mac_control |= BIT(18); /* In Band mode */
Daniel Mack342b7b72012-09-27 09:19:34 +0000867
Mugunthan V N1923d6e2014-09-08 22:54:02 +0530868 if (priv->rx_pause)
869 mac_control |= BIT(3);
870
871 if (priv->tx_pause)
872 mac_control |= BIT(4);
873
Mugunthan V Ndf828592012-03-18 20:17:54 +0000874 *link = true;
875 } else {
876 mac_control = 0;
877 /* disable forwarding */
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300878 cpsw_ale_control_set(cpsw->ale, slave_port,
Mugunthan V Ndf828592012-03-18 20:17:54 +0000879 ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
880 }
881
882 if (mac_control != slave->mac_control) {
883 phy_print_status(phy);
884 __raw_writel(mac_control, &slave->sliver->mac_control);
885 }
886
887 slave->mac_control = mac_control;
888}
889
890static void cpsw_adjust_link(struct net_device *ndev)
891{
892 struct cpsw_priv *priv = netdev_priv(ndev);
893 bool link = false;
894
895 for_each_slave(priv, _cpsw_adjust_link, priv, &link);
896
897 if (link) {
898 netif_carrier_on(ndev);
899 if (netif_running(ndev))
900 netif_wake_queue(ndev);
901 } else {
902 netif_carrier_off(ndev);
903 netif_stop_queue(ndev);
904 }
905}
906
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +0000907static int cpsw_get_coalesce(struct net_device *ndev,
908 struct ethtool_coalesce *coal)
909{
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300910 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +0000911
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300912 coal->rx_coalesce_usecs = cpsw->coal_intvl;
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +0000913 return 0;
914}
915
916static int cpsw_set_coalesce(struct net_device *ndev,
917 struct ethtool_coalesce *coal)
918{
919 struct cpsw_priv *priv = netdev_priv(ndev);
920 u32 int_ctrl;
921 u32 num_interrupts = 0;
922 u32 prescale = 0;
923 u32 addnl_dvdr = 1;
924 u32 coal_intvl = 0;
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +0300925 struct cpsw_common *cpsw = priv->cpsw;
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +0000926
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +0000927 coal_intvl = coal->rx_coalesce_usecs;
928
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +0300929 int_ctrl = readl(&cpsw->wr_regs->int_control);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300930 prescale = cpsw->bus_freq_mhz * 4;
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +0000931
Mugunthan V Na84bc2a2014-07-15 20:26:53 +0530932 if (!coal->rx_coalesce_usecs) {
933 int_ctrl &= ~(CPSW_INTPRESCALE_MASK | CPSW_INTPACEEN);
934 goto update_return;
935 }
936
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +0000937 if (coal_intvl < CPSW_CMINTMIN_INTVL)
938 coal_intvl = CPSW_CMINTMIN_INTVL;
939
940 if (coal_intvl > CPSW_CMINTMAX_INTVL) {
941 /* Interrupt pacer works with 4us Pulse, we can
942 * throttle further by dilating the 4us pulse.
943 */
944 addnl_dvdr = CPSW_INTPRESCALE_MASK / prescale;
945
946 if (addnl_dvdr > 1) {
947 prescale *= addnl_dvdr;
948 if (coal_intvl > (CPSW_CMINTMAX_INTVL * addnl_dvdr))
949 coal_intvl = (CPSW_CMINTMAX_INTVL
950 * addnl_dvdr);
951 } else {
952 addnl_dvdr = 1;
953 coal_intvl = CPSW_CMINTMAX_INTVL;
954 }
955 }
956
957 num_interrupts = (1000 * addnl_dvdr) / coal_intvl;
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +0300958 writel(num_interrupts, &cpsw->wr_regs->rx_imax);
959 writel(num_interrupts, &cpsw->wr_regs->tx_imax);
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +0000960
961 int_ctrl |= CPSW_INTPACEEN;
962 int_ctrl &= (~CPSW_INTPRESCALE_MASK);
963 int_ctrl |= (prescale & CPSW_INTPRESCALE_MASK);
Mugunthan V Na84bc2a2014-07-15 20:26:53 +0530964
965update_return:
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +0300966 writel(int_ctrl, &cpsw->wr_regs->int_control);
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +0000967
968 cpsw_notice(priv, timer, "Set coalesce to %d usecs.\n", coal_intvl);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300969 cpsw->coal_intvl = coal_intvl;
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +0000970
971 return 0;
972}
973
Mugunthan V Nd9718542013-07-23 15:38:17 +0530974static int cpsw_get_sset_count(struct net_device *ndev, int sset)
975{
976 switch (sset) {
977 case ETH_SS_STATS:
978 return CPSW_STATS_LEN;
979 default:
980 return -EOPNOTSUPP;
981 }
982}
983
984static void cpsw_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
985{
986 u8 *p = data;
987 int i;
988
989 switch (stringset) {
990 case ETH_SS_STATS:
991 for (i = 0; i < CPSW_STATS_LEN; i++) {
992 memcpy(p, cpsw_gstrings_stats[i].stat_string,
993 ETH_GSTRING_LEN);
994 p += ETH_GSTRING_LEN;
995 }
996 break;
997 }
998}
999
1000static void cpsw_get_ethtool_stats(struct net_device *ndev,
1001 struct ethtool_stats *stats, u64 *data)
1002{
Mugunthan V Nd9718542013-07-23 15:38:17 +05301003 struct cpdma_chan_stats rx_stats;
1004 struct cpdma_chan_stats tx_stats;
1005 u32 val;
1006 u8 *p;
1007 int i;
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001008 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Mugunthan V Nd9718542013-07-23 15:38:17 +05301009
1010 /* Collect Davinci CPDMA stats for Rx and Tx Channel */
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001011 cpdma_chan_get_stats(cpsw->rxch, &rx_stats);
1012 cpdma_chan_get_stats(cpsw->txch, &tx_stats);
Mugunthan V Nd9718542013-07-23 15:38:17 +05301013
1014 for (i = 0; i < CPSW_STATS_LEN; i++) {
1015 switch (cpsw_gstrings_stats[i].type) {
1016 case CPSW_STATS:
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001017 val = readl(cpsw->hw_stats +
Mugunthan V Nd9718542013-07-23 15:38:17 +05301018 cpsw_gstrings_stats[i].stat_offset);
1019 data[i] = val;
1020 break;
1021
1022 case CPDMA_RX_STATS:
1023 p = (u8 *)&rx_stats +
1024 cpsw_gstrings_stats[i].stat_offset;
1025 data[i] = *(u32 *)p;
1026 break;
1027
1028 case CPDMA_TX_STATS:
1029 p = (u8 *)&tx_stats +
1030 cpsw_gstrings_stats[i].stat_offset;
1031 data[i] = *(u32 *)p;
1032 break;
1033 }
1034 }
1035}
1036
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001037static int cpsw_common_res_usage_state(struct cpsw_common *cpsw)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001038{
1039 u32 i;
1040 u32 usage_count = 0;
1041
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001042 if (!cpsw->data.dual_emac)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001043 return 0;
1044
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001045 for (i = 0; i < cpsw->data.slaves; i++)
1046 if (cpsw->slaves[i].open_stat)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001047 usage_count++;
1048
1049 return usage_count;
1050}
1051
Ivan Khoronzhuk27e9e102016-08-10 02:22:32 +03001052static inline int cpsw_tx_packet_submit(struct cpsw_priv *priv,
1053 struct sk_buff *skb)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001054{
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001055 struct cpsw_common *cpsw = priv->cpsw;
1056
1057 return cpdma_chan_submit(cpsw->txch, skb, skb->data, skb->len,
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001058 priv->emac_port + cpsw->data.dual_emac);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001059}
1060
1061static inline void cpsw_add_dual_emac_def_ale_entries(
1062 struct cpsw_priv *priv, struct cpsw_slave *slave,
1063 u32 slave_port)
1064{
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001065 struct cpsw_common *cpsw = priv->cpsw;
Grygorii Strashko71a2cbb2016-04-07 15:16:44 +03001066 u32 port_mask = 1 << slave_port | ALE_PORT_HOST;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001067
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001068 if (cpsw->version == CPSW_VERSION_1)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001069 slave_write(slave, slave->port_vlan, CPSW1_PORT_VLAN);
1070 else
1071 slave_write(slave, slave->port_vlan, CPSW2_PORT_VLAN);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001072 cpsw_ale_add_vlan(cpsw->ale, slave->port_vlan, port_mask,
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001073 port_mask, port_mask, 0);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001074 cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001075 port_mask, ALE_VLAN, slave->port_vlan, 0);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001076 cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr,
1077 HOST_PORT_NUM, ALE_VLAN |
1078 ALE_SECURE, slave->port_vlan);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001079}
1080
Daniel Mack1e7a2e22013-11-15 08:29:16 +01001081static void soft_reset_slave(struct cpsw_slave *slave)
Mugunthan V Ndf828592012-03-18 20:17:54 +00001082{
1083 char name[32];
Daniel Mack1e7a2e22013-11-15 08:29:16 +01001084
1085 snprintf(name, sizeof(name), "slave-%d", slave->slave_num);
1086 soft_reset(name, &slave->sliver->soft_reset);
1087}
1088
1089static void cpsw_slave_open(struct cpsw_slave *slave, struct cpsw_priv *priv)
1090{
Mugunthan V Ndf828592012-03-18 20:17:54 +00001091 u32 slave_port;
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03001092 struct cpsw_common *cpsw = priv->cpsw;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001093
Daniel Mack1e7a2e22013-11-15 08:29:16 +01001094 soft_reset_slave(slave);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001095
1096 /* setup priority mapping */
1097 __raw_writel(RX_PRIORITY_MAPPING, &slave->sliver->rx_pri_map);
Richard Cochran9750a3a2012-10-29 08:45:15 +00001098
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001099 switch (cpsw->version) {
Richard Cochran9750a3a2012-10-29 08:45:15 +00001100 case CPSW_VERSION_1:
1101 slave_write(slave, TX_PRIORITY_MAPPING, CPSW1_TX_PRI_MAP);
1102 break;
1103 case CPSW_VERSION_2:
Mugunthan V Nc193f362013-08-05 17:30:05 +05301104 case CPSW_VERSION_3:
Mugunthan V N926489b2013-08-12 17:11:15 +05301105 case CPSW_VERSION_4:
Richard Cochran9750a3a2012-10-29 08:45:15 +00001106 slave_write(slave, TX_PRIORITY_MAPPING, CPSW2_TX_PRI_MAP);
1107 break;
1108 }
Mugunthan V Ndf828592012-03-18 20:17:54 +00001109
1110 /* setup max packet size, and mac address */
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001111 __raw_writel(cpsw->rx_packet_max, &slave->sliver->rx_maxlen);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001112 cpsw_set_slave_mac(slave, priv);
1113
1114 slave->mac_control = 0; /* no link yet */
1115
Ivan Khoronzhuk6f1f5832016-08-10 02:22:34 +03001116 slave_port = cpsw_get_slave_port(slave->slave_num);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001117
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001118 if (cpsw->data.dual_emac)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001119 cpsw_add_dual_emac_def_ale_entries(priv, slave, slave_port);
1120 else
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001121 cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001122 1 << slave_port, 0, 0, ALE_MCAST_FWD_2);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001123
David Rivshind733f7542016-04-27 21:32:31 -04001124 if (slave->data->phy_node) {
David Rivshin552165b2016-04-27 21:25:25 -04001125 slave->phy = of_phy_connect(priv->ndev, slave->data->phy_node,
Heiko Schocher9e42f712015-10-17 06:04:35 +02001126 &cpsw_adjust_link, 0, slave->data->phy_if);
David Rivshind733f7542016-04-27 21:32:31 -04001127 if (!slave->phy) {
1128 dev_err(priv->dev, "phy \"%s\" not found on slave %d\n",
1129 slave->data->phy_node->full_name,
1130 slave->slave_num);
1131 return;
1132 }
1133 } else {
Heiko Schocher9e42f712015-10-17 06:04:35 +02001134 slave->phy = phy_connect(priv->ndev, slave->data->phy_id,
Florian Fainellif9a8f832013-01-14 00:52:52 +00001135 &cpsw_adjust_link, slave->data->phy_if);
David Rivshind733f7542016-04-27 21:32:31 -04001136 if (IS_ERR(slave->phy)) {
1137 dev_err(priv->dev,
1138 "phy \"%s\" not found on slave %d, err %ld\n",
1139 slave->data->phy_id, slave->slave_num,
1140 PTR_ERR(slave->phy));
1141 slave->phy = NULL;
1142 return;
1143 }
Mugunthan V Ndf828592012-03-18 20:17:54 +00001144 }
David Rivshind733f7542016-04-27 21:32:31 -04001145
1146 phy_attached_info(slave->phy);
1147
1148 phy_start(slave->phy);
1149
1150 /* Configure GMII_SEL register */
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001151 cpsw_phy_sel(cpsw->dev, slave->phy->interface, slave->slave_num);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001152}
1153
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001154static inline void cpsw_add_default_vlan(struct cpsw_priv *priv)
1155{
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001156 struct cpsw_common *cpsw = priv->cpsw;
1157 const int vlan = cpsw->data.default_vlan;
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001158 u32 reg;
1159 int i;
Lennart Sorensen1e5c4bc2014-10-31 13:38:52 -04001160 int unreg_mcast_mask;
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001161
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001162 reg = (cpsw->version == CPSW_VERSION_1) ? CPSW1_PORT_VLAN :
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001163 CPSW2_PORT_VLAN;
1164
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001165 writel(vlan, &cpsw->host_port_regs->port_vlan);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001166
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001167 for (i = 0; i < cpsw->data.slaves; i++)
1168 slave_write(cpsw->slaves + i, vlan, reg);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001169
Lennart Sorensen1e5c4bc2014-10-31 13:38:52 -04001170 if (priv->ndev->flags & IFF_ALLMULTI)
1171 unreg_mcast_mask = ALE_ALL_PORTS;
1172 else
1173 unreg_mcast_mask = ALE_PORT_1 | ALE_PORT_2;
1174
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001175 cpsw_ale_add_vlan(cpsw->ale, vlan, ALE_ALL_PORTS,
Grygorii Strashko61f1cef2016-04-07 15:16:43 +03001176 ALE_ALL_PORTS, ALE_ALL_PORTS,
1177 unreg_mcast_mask);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001178}
1179
Mugunthan V Ndf828592012-03-18 20:17:54 +00001180static void cpsw_init_host_port(struct cpsw_priv *priv)
1181{
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001182 u32 fifo_mode;
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001183 u32 control_reg;
1184 struct cpsw_common *cpsw = priv->cpsw;
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001185
Mugunthan V Ndf828592012-03-18 20:17:54 +00001186 /* soft reset the controller and initialize ale */
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001187 soft_reset("cpsw", &cpsw->regs->soft_reset);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001188 cpsw_ale_start(cpsw->ale);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001189
1190 /* switch to vlan unaware mode */
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001191 cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_VLAN_AWARE,
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001192 CPSW_ALE_VLAN_AWARE);
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001193 control_reg = readl(&cpsw->regs->control);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001194 control_reg |= CPSW_VLAN_AWARE;
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001195 writel(control_reg, &cpsw->regs->control);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001196 fifo_mode = (cpsw->data.dual_emac) ? CPSW_FIFO_DUAL_MAC_MODE :
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001197 CPSW_FIFO_NORMAL_MODE;
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001198 writel(fifo_mode, &cpsw->host_port_regs->tx_in_ctl);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001199
1200 /* setup host port priority mapping */
1201 __raw_writel(CPDMA_TX_PRIORITY_MAP,
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001202 &cpsw->host_port_regs->cpdma_tx_pri_map);
1203 __raw_writel(0, &cpsw->host_port_regs->cpdma_rx_chan_map);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001204
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001205 cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM,
Mugunthan V Ndf828592012-03-18 20:17:54 +00001206 ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
1207
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001208 if (!cpsw->data.dual_emac) {
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001209 cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr, HOST_PORT_NUM,
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001210 0, 0);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001211 cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
Grygorii Strashko71a2cbb2016-04-07 15:16:44 +03001212 ALE_PORT_HOST, 0, 0, ALE_MCAST_FWD_2);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001213 }
Mugunthan V Ndf828592012-03-18 20:17:54 +00001214}
1215
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +03001216static int cpsw_fill_rx_channels(struct cpsw_priv *priv)
1217{
1218 struct cpsw_common *cpsw = priv->cpsw;
1219 struct sk_buff *skb;
1220 int ch_buf_num;
1221 int i, ret;
1222
1223 ch_buf_num = cpdma_chan_get_rx_buf_num(cpsw->rxch);
1224 for (i = 0; i < ch_buf_num; i++) {
1225 skb = __netdev_alloc_skb_ip_align(priv->ndev,
1226 cpsw->rx_packet_max,
1227 GFP_KERNEL);
1228 if (!skb) {
1229 cpsw_err(priv, ifup, "cannot allocate skb\n");
1230 return -ENOMEM;
1231 }
1232
1233 ret = cpdma_chan_submit(cpsw->rxch, skb, skb->data,
1234 skb_tailroom(skb), 0);
1235 if (ret < 0) {
1236 cpsw_err(priv, ifup,
1237 "cannot submit skb to rx channel, error %d\n",
1238 ret);
1239 kfree_skb(skb);
1240 return ret;
1241 }
1242 kmemleak_not_leak(skb);
1243 }
1244
1245 cpsw_info(priv, ifup, "submitted %d rx descriptors\n", ch_buf_num);
1246
1247 return ch_buf_num;
1248}
1249
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001250static void cpsw_slave_stop(struct cpsw_slave *slave, struct cpsw_common *cpsw)
Sebastian Siewioraacebbf2013-04-23 07:31:36 +00001251{
Schuyler Patton3995d262014-03-03 16:19:06 +05301252 u32 slave_port;
1253
Ivan Khoronzhuk6f1f5832016-08-10 02:22:34 +03001254 slave_port = cpsw_get_slave_port(slave->slave_num);
Schuyler Patton3995d262014-03-03 16:19:06 +05301255
Sebastian Siewioraacebbf2013-04-23 07:31:36 +00001256 if (!slave->phy)
1257 return;
1258 phy_stop(slave->phy);
1259 phy_disconnect(slave->phy);
1260 slave->phy = NULL;
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001261 cpsw_ale_control_set(cpsw->ale, slave_port,
Schuyler Patton3995d262014-03-03 16:19:06 +05301262 ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
Grygorii Strashko1f95ba02016-06-24 21:23:41 +03001263 soft_reset_slave(slave);
Sebastian Siewioraacebbf2013-04-23 07:31:36 +00001264}
1265
Mugunthan V Ndf828592012-03-18 20:17:54 +00001266static int cpsw_ndo_open(struct net_device *ndev)
1267{
1268 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03001269 struct cpsw_common *cpsw = priv->cpsw;
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +03001270 int ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001271 u32 reg;
1272
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001273 ret = pm_runtime_get_sync(cpsw->dev);
Grygorii Strashko108a6532016-06-24 21:23:42 +03001274 if (ret < 0) {
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001275 pm_runtime_put_noidle(cpsw->dev);
Grygorii Strashko108a6532016-06-24 21:23:42 +03001276 return ret;
1277 }
Grygorii Strashko3fa88c52016-04-19 21:09:49 +03001278
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001279 if (!cpsw_common_res_usage_state(cpsw))
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001280 cpsw_intr_disable(cpsw);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001281 netif_carrier_off(ndev);
1282
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001283 reg = cpsw->version;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001284
1285 dev_info(priv->dev, "initializing cpsw version %d.%d (%d)\n",
1286 CPSW_MAJOR_VERSION(reg), CPSW_MINOR_VERSION(reg),
1287 CPSW_RTL_VERSION(reg));
1288
1289 /* initialize host and slave ports */
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001290 if (!cpsw_common_res_usage_state(cpsw))
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001291 cpsw_init_host_port(priv);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001292 for_each_slave(priv, cpsw_slave_open, priv);
1293
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001294 /* Add default VLAN */
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001295 if (!cpsw->data.dual_emac)
Mugunthan V Ne6afea02014-06-18 17:21:48 +05301296 cpsw_add_default_vlan(priv);
1297 else
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001298 cpsw_ale_add_vlan(cpsw->ale, cpsw->data.default_vlan,
Grygorii Strashko61f1cef2016-04-07 15:16:43 +03001299 ALE_ALL_PORTS, ALE_ALL_PORTS, 0, 0);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001300
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001301 if (!cpsw_common_res_usage_state(cpsw)) {
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001302 /* setup tx dma to fixed prio and zero offset */
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001303 cpdma_control_set(cpsw->dma, CPDMA_TX_PRIO_FIXED, 1);
1304 cpdma_control_set(cpsw->dma, CPDMA_RX_BUFFER_OFFSET, 0);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001305
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001306 /* disable priority elevation */
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001307 __raw_writel(0, &cpsw->regs->ptype);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001308
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001309 /* enable statistics collection only on all ports */
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001310 __raw_writel(0x7, &cpsw->regs->stat_port_en);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001311
Mugunthan V N1923d6e2014-09-08 22:54:02 +05301312 /* Enable internal fifo flow control */
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001313 writel(0x7, &cpsw->regs->flow_control);
Mugunthan V N1923d6e2014-09-08 22:54:02 +05301314
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +03001315 napi_enable(&cpsw->napi_rx);
1316 napi_enable(&cpsw->napi_tx);
Mugunthan V Nd354eb82015-08-04 16:06:19 +05301317
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +03001318 if (cpsw->tx_irq_disabled) {
1319 cpsw->tx_irq_disabled = false;
1320 enable_irq(cpsw->irqs_table[1]);
Mugunthan V N7da11602015-08-12 15:22:53 +05301321 }
1322
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +03001323 if (cpsw->rx_irq_disabled) {
1324 cpsw->rx_irq_disabled = false;
1325 enable_irq(cpsw->irqs_table[0]);
Mugunthan V N7da11602015-08-12 15:22:53 +05301326 }
1327
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +03001328 ret = cpsw_fill_rx_channels(priv);
1329 if (ret < 0)
1330 goto err_cleanup;
Mugunthan V Nf280e892013-12-11 22:09:05 -06001331
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001332 if (cpts_register(cpsw->dev, cpsw->cpts,
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001333 cpsw->data.cpts_clock_mult,
1334 cpsw->data.cpts_clock_shift))
Mugunthan V Nf280e892013-12-11 22:09:05 -06001335 dev_err(priv->dev, "error registering cpts device\n");
1336
Mugunthan V Ndf828592012-03-18 20:17:54 +00001337 }
Mugunthan V Ndf828592012-03-18 20:17:54 +00001338
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001339 /* Enable Interrupt pacing if configured */
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001340 if (cpsw->coal_intvl != 0) {
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001341 struct ethtool_coalesce coal;
1342
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001343 coal.rx_coalesce_usecs = cpsw->coal_intvl;
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001344 cpsw_set_coalesce(ndev, &coal);
1345 }
1346
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001347 cpdma_ctlr_start(cpsw->dma);
1348 cpsw_intr_enable(cpsw);
Mugunthan V Nf63a9752014-04-10 14:23:24 +05301349
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001350 if (cpsw->data.dual_emac)
1351 cpsw->slaves[priv->emac_port].open_stat = true;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001352 return 0;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001353
Sebastian Siewioraacebbf2013-04-23 07:31:36 +00001354err_cleanup:
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001355 cpdma_ctlr_stop(cpsw->dma);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001356 for_each_slave(priv, cpsw_slave_stop, cpsw);
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001357 pm_runtime_put_sync(cpsw->dev);
Sebastian Siewioraacebbf2013-04-23 07:31:36 +00001358 netif_carrier_off(priv->ndev);
1359 return ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001360}
1361
1362static int cpsw_ndo_stop(struct net_device *ndev)
1363{
1364 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03001365 struct cpsw_common *cpsw = priv->cpsw;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001366
1367 cpsw_info(priv, ifdown, "shutting down cpsw device\n");
Mugunthan V Ndf828592012-03-18 20:17:54 +00001368 netif_stop_queue(priv->ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001369 netif_carrier_off(priv->ndev);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001370
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001371 if (cpsw_common_res_usage_state(cpsw) <= 1) {
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +03001372 napi_disable(&cpsw->napi_rx);
1373 napi_disable(&cpsw->napi_tx);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001374 cpts_unregister(cpsw->cpts);
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001375 cpsw_intr_disable(cpsw);
1376 cpdma_ctlr_stop(cpsw->dma);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001377 cpsw_ale_stop(cpsw->ale);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001378 }
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001379 for_each_slave(priv, cpsw_slave_stop, cpsw);
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001380 pm_runtime_put_sync(cpsw->dev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001381 if (cpsw->data.dual_emac)
1382 cpsw->slaves[priv->emac_port].open_stat = false;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001383 return 0;
1384}
1385
1386static netdev_tx_t cpsw_ndo_start_xmit(struct sk_buff *skb,
1387 struct net_device *ndev)
1388{
1389 struct cpsw_priv *priv = netdev_priv(ndev);
1390 int ret;
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001391 struct cpsw_common *cpsw = priv->cpsw;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001392
Florian Westphal860e9532016-05-03 16:33:13 +02001393 netif_trans_update(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001394
1395 if (skb_padto(skb, CPSW_MIN_PACKET_SIZE)) {
1396 cpsw_err(priv, tx_err, "packet pad failed\n");
Tobias Klauser8dc43dd2014-03-10 13:12:23 +01001397 ndev->stats.tx_dropped++;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001398 return NETDEV_TX_OK;
1399 }
1400
Mugunthan V N9232b162013-02-11 09:52:19 +00001401 if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001402 cpsw->cpts->tx_enable)
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001403 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
1404
1405 skb_tx_timestamp(skb);
1406
Ivan Khoronzhuk27e9e102016-08-10 02:22:32 +03001407 ret = cpsw_tx_packet_submit(priv, skb);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001408 if (unlikely(ret != 0)) {
1409 cpsw_err(priv, tx_err, "desc submit failed\n");
1410 goto fail;
1411 }
1412
Mugunthan V Nfae50822013-01-17 06:31:34 +00001413 /* If there is no more tx desc left free then we need to
1414 * tell the kernel to stop sending us tx frames.
1415 */
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001416 if (unlikely(!cpdma_check_free_tx_desc(cpsw->txch)))
Mugunthan V Nfae50822013-01-17 06:31:34 +00001417 netif_stop_queue(ndev);
1418
Mugunthan V Ndf828592012-03-18 20:17:54 +00001419 return NETDEV_TX_OK;
1420fail:
Tobias Klauser8dc43dd2014-03-10 13:12:23 +01001421 ndev->stats.tx_dropped++;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001422 netif_stop_queue(ndev);
1423 return NETDEV_TX_BUSY;
1424}
1425
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001426#ifdef CONFIG_TI_CPTS
1427
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001428static void cpsw_hwtstamp_v1(struct cpsw_common *cpsw)
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001429{
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001430 struct cpsw_slave *slave = &cpsw->slaves[cpsw->data.active_slave];
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001431 u32 ts_en, seq_id;
1432
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001433 if (!cpsw->cpts->tx_enable && !cpsw->cpts->rx_enable) {
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001434 slave_write(slave, 0, CPSW1_TS_CTL);
1435 return;
1436 }
1437
1438 seq_id = (30 << CPSW_V1_SEQ_ID_OFS_SHIFT) | ETH_P_1588;
1439 ts_en = EVENT_MSG_BITS << CPSW_V1_MSG_TYPE_OFS;
1440
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001441 if (cpsw->cpts->tx_enable)
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001442 ts_en |= CPSW_V1_TS_TX_EN;
1443
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001444 if (cpsw->cpts->rx_enable)
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001445 ts_en |= CPSW_V1_TS_RX_EN;
1446
1447 slave_write(slave, ts_en, CPSW1_TS_CTL);
1448 slave_write(slave, seq_id, CPSW1_TS_SEQ_LTYPE);
1449}
1450
1451static void cpsw_hwtstamp_v2(struct cpsw_priv *priv)
1452{
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001453 struct cpsw_slave *slave;
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001454 struct cpsw_common *cpsw = priv->cpsw;
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001455 u32 ctrl, mtype;
1456
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001457 if (cpsw->data.dual_emac)
1458 slave = &cpsw->slaves[priv->emac_port];
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001459 else
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001460 slave = &cpsw->slaves[cpsw->data.active_slave];
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001461
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001462 ctrl = slave_read(slave, CPSW2_CONTROL);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001463 switch (cpsw->version) {
George Cherian09c55372014-05-02 12:02:02 +05301464 case CPSW_VERSION_2:
1465 ctrl &= ~CTRL_V2_ALL_TS_MASK;
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001466
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001467 if (cpsw->cpts->tx_enable)
George Cherian09c55372014-05-02 12:02:02 +05301468 ctrl |= CTRL_V2_TX_TS_BITS;
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001469
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001470 if (cpsw->cpts->rx_enable)
George Cherian09c55372014-05-02 12:02:02 +05301471 ctrl |= CTRL_V2_RX_TS_BITS;
Richard Cochran26fe7eb2015-05-25 11:02:13 +02001472 break;
George Cherian09c55372014-05-02 12:02:02 +05301473 case CPSW_VERSION_3:
1474 default:
1475 ctrl &= ~CTRL_V3_ALL_TS_MASK;
1476
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001477 if (cpsw->cpts->tx_enable)
George Cherian09c55372014-05-02 12:02:02 +05301478 ctrl |= CTRL_V3_TX_TS_BITS;
1479
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001480 if (cpsw->cpts->rx_enable)
George Cherian09c55372014-05-02 12:02:02 +05301481 ctrl |= CTRL_V3_RX_TS_BITS;
Richard Cochran26fe7eb2015-05-25 11:02:13 +02001482 break;
George Cherian09c55372014-05-02 12:02:02 +05301483 }
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001484
1485 mtype = (30 << TS_SEQ_ID_OFFSET_SHIFT) | EVENT_MSG_BITS;
1486
1487 slave_write(slave, mtype, CPSW2_TS_SEQ_MTYPE);
1488 slave_write(slave, ctrl, CPSW2_CONTROL);
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001489 __raw_writel(ETH_P_1588, &cpsw->regs->ts_ltype);
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001490}
1491
Ben Hutchingsa5b41452013-11-18 23:23:40 +00001492static int cpsw_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001493{
Mugunthan V N3177bf62012-11-27 07:53:40 +00001494 struct cpsw_priv *priv = netdev_priv(dev);
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001495 struct hwtstamp_config cfg;
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001496 struct cpsw_common *cpsw = priv->cpsw;
1497 struct cpts *cpts = cpsw->cpts;
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001498
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001499 if (cpsw->version != CPSW_VERSION_1 &&
1500 cpsw->version != CPSW_VERSION_2 &&
1501 cpsw->version != CPSW_VERSION_3)
Ben Hutchings2ee91e52013-11-14 00:47:36 +00001502 return -EOPNOTSUPP;
1503
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001504 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
1505 return -EFAULT;
1506
1507 /* reserved for future extensions */
1508 if (cfg.flags)
1509 return -EINVAL;
1510
Ben Hutchings2ee91e52013-11-14 00:47:36 +00001511 if (cfg.tx_type != HWTSTAMP_TX_OFF && cfg.tx_type != HWTSTAMP_TX_ON)
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001512 return -ERANGE;
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001513
1514 switch (cfg.rx_filter) {
1515 case HWTSTAMP_FILTER_NONE:
1516 cpts->rx_enable = 0;
1517 break;
1518 case HWTSTAMP_FILTER_ALL:
1519 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
1520 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
1521 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
1522 return -ERANGE;
1523 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
1524 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
1525 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
1526 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1527 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
1528 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
1529 case HWTSTAMP_FILTER_PTP_V2_EVENT:
1530 case HWTSTAMP_FILTER_PTP_V2_SYNC:
1531 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
1532 cpts->rx_enable = 1;
1533 cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
1534 break;
1535 default:
1536 return -ERANGE;
1537 }
1538
Ben Hutchings2ee91e52013-11-14 00:47:36 +00001539 cpts->tx_enable = cfg.tx_type == HWTSTAMP_TX_ON;
1540
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001541 switch (cpsw->version) {
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001542 case CPSW_VERSION_1:
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001543 cpsw_hwtstamp_v1(cpsw);
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001544 break;
1545 case CPSW_VERSION_2:
George Cherianf7d403c2014-05-02 12:02:01 +05301546 case CPSW_VERSION_3:
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001547 cpsw_hwtstamp_v2(priv);
1548 break;
1549 default:
Ben Hutchings2ee91e52013-11-14 00:47:36 +00001550 WARN_ON(1);
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001551 }
1552
1553 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
1554}
1555
Ben Hutchingsa5b41452013-11-18 23:23:40 +00001556static int cpsw_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
1557{
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001558 struct cpsw_common *cpsw = ndev_to_cpsw(dev);
1559 struct cpts *cpts = cpsw->cpts;
Ben Hutchingsa5b41452013-11-18 23:23:40 +00001560 struct hwtstamp_config cfg;
1561
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001562 if (cpsw->version != CPSW_VERSION_1 &&
1563 cpsw->version != CPSW_VERSION_2 &&
1564 cpsw->version != CPSW_VERSION_3)
Ben Hutchingsa5b41452013-11-18 23:23:40 +00001565 return -EOPNOTSUPP;
1566
1567 cfg.flags = 0;
1568 cfg.tx_type = cpts->tx_enable ? HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF;
1569 cfg.rx_filter = (cpts->rx_enable ?
1570 HWTSTAMP_FILTER_PTP_V2_EVENT : HWTSTAMP_FILTER_NONE);
1571
1572 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
1573}
1574
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001575#endif /*CONFIG_TI_CPTS*/
1576
1577static int cpsw_ndo_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
1578{
Mugunthan V N11f2c982013-03-11 23:16:38 +00001579 struct cpsw_priv *priv = netdev_priv(dev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001580 struct cpsw_common *cpsw = priv->cpsw;
1581 int slave_no = cpsw_slave_index(cpsw, priv);
Mugunthan V N11f2c982013-03-11 23:16:38 +00001582
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001583 if (!netif_running(dev))
1584 return -EINVAL;
1585
Mugunthan V N11f2c982013-03-11 23:16:38 +00001586 switch (cmd) {
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001587#ifdef CONFIG_TI_CPTS
Mugunthan V N11f2c982013-03-11 23:16:38 +00001588 case SIOCSHWTSTAMP:
Ben Hutchingsa5b41452013-11-18 23:23:40 +00001589 return cpsw_hwtstamp_set(dev, req);
1590 case SIOCGHWTSTAMP:
1591 return cpsw_hwtstamp_get(dev, req);
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001592#endif
Mugunthan V N11f2c982013-03-11 23:16:38 +00001593 }
1594
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001595 if (!cpsw->slaves[slave_no].phy)
Stefan Sørensenc1b59942014-02-16 14:54:25 +01001596 return -EOPNOTSUPP;
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001597 return phy_mii_ioctl(cpsw->slaves[slave_no].phy, req, cmd);
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001598}
1599
Mugunthan V Ndf828592012-03-18 20:17:54 +00001600static void cpsw_ndo_tx_timeout(struct net_device *ndev)
1601{
1602 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001603 struct cpsw_common *cpsw = priv->cpsw;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001604
1605 cpsw_err(priv, tx_err, "transmit timeout, restarting dma\n");
Tobias Klauser8dc43dd2014-03-10 13:12:23 +01001606 ndev->stats.tx_errors++;
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001607 cpsw_intr_disable(cpsw);
1608 cpdma_chan_stop(cpsw->txch);
1609 cpdma_chan_start(cpsw->txch);
1610 cpsw_intr_enable(cpsw);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001611}
1612
Mugunthan V Ndcfd8d52013-07-25 23:44:01 +05301613static int cpsw_ndo_set_mac_address(struct net_device *ndev, void *p)
1614{
1615 struct cpsw_priv *priv = netdev_priv(ndev);
1616 struct sockaddr *addr = (struct sockaddr *)p;
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03001617 struct cpsw_common *cpsw = priv->cpsw;
Mugunthan V Ndcfd8d52013-07-25 23:44:01 +05301618 int flags = 0;
1619 u16 vid = 0;
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001620 int ret;
Mugunthan V Ndcfd8d52013-07-25 23:44:01 +05301621
1622 if (!is_valid_ether_addr(addr->sa_data))
1623 return -EADDRNOTAVAIL;
1624
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001625 ret = pm_runtime_get_sync(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001626 if (ret < 0) {
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001627 pm_runtime_put_noidle(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001628 return ret;
1629 }
1630
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001631 if (cpsw->data.dual_emac) {
1632 vid = cpsw->slaves[priv->emac_port].port_vlan;
Mugunthan V Ndcfd8d52013-07-25 23:44:01 +05301633 flags = ALE_VLAN;
1634 }
1635
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001636 cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr, HOST_PORT_NUM,
Mugunthan V Ndcfd8d52013-07-25 23:44:01 +05301637 flags, vid);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001638 cpsw_ale_add_ucast(cpsw->ale, addr->sa_data, HOST_PORT_NUM,
Mugunthan V Ndcfd8d52013-07-25 23:44:01 +05301639 flags, vid);
1640
1641 memcpy(priv->mac_addr, addr->sa_data, ETH_ALEN);
1642 memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
1643 for_each_slave(priv, cpsw_set_slave_mac, priv);
1644
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001645 pm_runtime_put(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001646
Mugunthan V Ndcfd8d52013-07-25 23:44:01 +05301647 return 0;
1648}
1649
Mugunthan V Ndf828592012-03-18 20:17:54 +00001650#ifdef CONFIG_NET_POLL_CONTROLLER
1651static void cpsw_ndo_poll_controller(struct net_device *ndev)
1652{
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +03001653 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001654
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +03001655 cpsw_intr_disable(cpsw);
1656 cpsw_rx_interrupt(cpsw->irqs_table[0], cpsw);
1657 cpsw_tx_interrupt(cpsw->irqs_table[1], cpsw);
1658 cpsw_intr_enable(cpsw);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001659}
1660#endif
1661
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001662static inline int cpsw_add_vlan_ale_entry(struct cpsw_priv *priv,
1663 unsigned short vid)
1664{
1665 int ret;
Mugunthan V N9f6bd8f2015-01-15 14:59:28 +05301666 int unreg_mcast_mask = 0;
1667 u32 port_mask;
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001668 struct cpsw_common *cpsw = priv->cpsw;
Lennart Sorensen1e5c4bc2014-10-31 13:38:52 -04001669
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001670 if (cpsw->data.dual_emac) {
Mugunthan V N9f6bd8f2015-01-15 14:59:28 +05301671 port_mask = (1 << (priv->emac_port + 1)) | ALE_PORT_HOST;
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001672
Mugunthan V N9f6bd8f2015-01-15 14:59:28 +05301673 if (priv->ndev->flags & IFF_ALLMULTI)
1674 unreg_mcast_mask = port_mask;
1675 } else {
1676 port_mask = ALE_ALL_PORTS;
1677
1678 if (priv->ndev->flags & IFF_ALLMULTI)
1679 unreg_mcast_mask = ALE_ALL_PORTS;
1680 else
1681 unreg_mcast_mask = ALE_PORT_1 | ALE_PORT_2;
1682 }
1683
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001684 ret = cpsw_ale_add_vlan(cpsw->ale, vid, port_mask, 0, port_mask,
Grygorii Strashko61f1cef2016-04-07 15:16:43 +03001685 unreg_mcast_mask);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001686 if (ret != 0)
1687 return ret;
1688
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001689 ret = cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr,
Grygorii Strashko71a2cbb2016-04-07 15:16:44 +03001690 HOST_PORT_NUM, ALE_VLAN, vid);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001691 if (ret != 0)
1692 goto clean_vid;
1693
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001694 ret = cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
Mugunthan V N9f6bd8f2015-01-15 14:59:28 +05301695 port_mask, ALE_VLAN, vid, 0);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001696 if (ret != 0)
1697 goto clean_vlan_ucast;
1698 return 0;
1699
1700clean_vlan_ucast:
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001701 cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr,
Grygorii Strashko71a2cbb2016-04-07 15:16:44 +03001702 HOST_PORT_NUM, ALE_VLAN, vid);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001703clean_vid:
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001704 cpsw_ale_del_vlan(cpsw->ale, vid, 0);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001705 return ret;
1706}
1707
1708static int cpsw_ndo_vlan_rx_add_vid(struct net_device *ndev,
Patrick McHardy80d5c362013-04-19 02:04:28 +00001709 __be16 proto, u16 vid)
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001710{
1711 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03001712 struct cpsw_common *cpsw = priv->cpsw;
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001713 int ret;
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001714
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001715 if (vid == cpsw->data.default_vlan)
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001716 return 0;
1717
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001718 ret = pm_runtime_get_sync(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001719 if (ret < 0) {
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001720 pm_runtime_put_noidle(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001721 return ret;
1722 }
1723
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001724 if (cpsw->data.dual_emac) {
Mugunthan V N02a54162015-01-22 15:19:22 +05301725 /* In dual EMAC, reserved VLAN id should not be used for
1726 * creating VLAN interfaces as this can break the dual
1727 * EMAC port separation
1728 */
1729 int i;
1730
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001731 for (i = 0; i < cpsw->data.slaves; i++) {
1732 if (vid == cpsw->slaves[i].port_vlan)
Mugunthan V N02a54162015-01-22 15:19:22 +05301733 return -EINVAL;
1734 }
1735 }
1736
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001737 dev_info(priv->dev, "Adding vlanid %d to vlan filter\n", vid);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001738 ret = cpsw_add_vlan_ale_entry(priv, vid);
1739
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001740 pm_runtime_put(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001741 return ret;
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001742}
1743
1744static int cpsw_ndo_vlan_rx_kill_vid(struct net_device *ndev,
Patrick McHardy80d5c362013-04-19 02:04:28 +00001745 __be16 proto, u16 vid)
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001746{
1747 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03001748 struct cpsw_common *cpsw = priv->cpsw;
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001749 int ret;
1750
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001751 if (vid == cpsw->data.default_vlan)
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001752 return 0;
1753
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001754 ret = pm_runtime_get_sync(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001755 if (ret < 0) {
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001756 pm_runtime_put_noidle(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001757 return ret;
1758 }
1759
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001760 if (cpsw->data.dual_emac) {
Mugunthan V N02a54162015-01-22 15:19:22 +05301761 int i;
1762
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001763 for (i = 0; i < cpsw->data.slaves; i++) {
1764 if (vid == cpsw->slaves[i].port_vlan)
Mugunthan V N02a54162015-01-22 15:19:22 +05301765 return -EINVAL;
1766 }
1767 }
1768
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001769 dev_info(priv->dev, "removing vlanid %d from vlan filter\n", vid);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001770 ret = cpsw_ale_del_vlan(cpsw->ale, vid, 0);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001771 if (ret != 0)
1772 return ret;
1773
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001774 ret = cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr,
Grygorii Strashko61f1cef2016-04-07 15:16:43 +03001775 HOST_PORT_NUM, ALE_VLAN, vid);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001776 if (ret != 0)
1777 return ret;
1778
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001779 ret = cpsw_ale_del_mcast(cpsw->ale, priv->ndev->broadcast,
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001780 0, ALE_VLAN, vid);
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001781 pm_runtime_put(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001782 return ret;
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001783}
1784
Mugunthan V Ndf828592012-03-18 20:17:54 +00001785static const struct net_device_ops cpsw_netdev_ops = {
1786 .ndo_open = cpsw_ndo_open,
1787 .ndo_stop = cpsw_ndo_stop,
1788 .ndo_start_xmit = cpsw_ndo_start_xmit,
Mugunthan V Ndcfd8d52013-07-25 23:44:01 +05301789 .ndo_set_mac_address = cpsw_ndo_set_mac_address,
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001790 .ndo_do_ioctl = cpsw_ndo_ioctl,
Mugunthan V Ndf828592012-03-18 20:17:54 +00001791 .ndo_validate_addr = eth_validate_addr,
David S. Miller5c473ed2012-03-20 00:33:59 -04001792 .ndo_change_mtu = eth_change_mtu,
Mugunthan V Ndf828592012-03-18 20:17:54 +00001793 .ndo_tx_timeout = cpsw_ndo_tx_timeout,
Mugunthan V N5c50a852012-10-29 08:45:11 +00001794 .ndo_set_rx_mode = cpsw_ndo_set_rx_mode,
Mugunthan V Ndf828592012-03-18 20:17:54 +00001795#ifdef CONFIG_NET_POLL_CONTROLLER
1796 .ndo_poll_controller = cpsw_ndo_poll_controller,
1797#endif
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001798 .ndo_vlan_rx_add_vid = cpsw_ndo_vlan_rx_add_vid,
1799 .ndo_vlan_rx_kill_vid = cpsw_ndo_vlan_rx_kill_vid,
Mugunthan V Ndf828592012-03-18 20:17:54 +00001800};
1801
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05301802static int cpsw_get_regs_len(struct net_device *ndev)
1803{
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001804 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05301805
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001806 return cpsw->data.ale_entries * ALE_ENTRY_WORDS * sizeof(u32);
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05301807}
1808
1809static void cpsw_get_regs(struct net_device *ndev,
1810 struct ethtool_regs *regs, void *p)
1811{
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05301812 u32 *reg = p;
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001813 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05301814
1815 /* update CPSW IP version */
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001816 regs->version = cpsw->version;
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05301817
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001818 cpsw_ale_dump(cpsw->ale, reg);
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05301819}
1820
Mugunthan V Ndf828592012-03-18 20:17:54 +00001821static void cpsw_get_drvinfo(struct net_device *ndev,
1822 struct ethtool_drvinfo *info)
1823{
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03001824 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001825 struct platform_device *pdev = to_platform_device(cpsw->dev);
Jiri Pirko7826d432013-01-06 00:44:26 +00001826
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05301827 strlcpy(info->driver, "cpsw", sizeof(info->driver));
Jiri Pirko7826d432013-01-06 00:44:26 +00001828 strlcpy(info->version, "1.0", sizeof(info->version));
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001829 strlcpy(info->bus_info, pdev->name, sizeof(info->bus_info));
Mugunthan V Ndf828592012-03-18 20:17:54 +00001830}
1831
1832static u32 cpsw_get_msglevel(struct net_device *ndev)
1833{
1834 struct cpsw_priv *priv = netdev_priv(ndev);
1835 return priv->msg_enable;
1836}
1837
1838static void cpsw_set_msglevel(struct net_device *ndev, u32 value)
1839{
1840 struct cpsw_priv *priv = netdev_priv(ndev);
1841 priv->msg_enable = value;
1842}
1843
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001844static int cpsw_get_ts_info(struct net_device *ndev,
1845 struct ethtool_ts_info *info)
1846{
1847#ifdef CONFIG_TI_CPTS
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001848 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001849
1850 info->so_timestamping =
1851 SOF_TIMESTAMPING_TX_HARDWARE |
1852 SOF_TIMESTAMPING_TX_SOFTWARE |
1853 SOF_TIMESTAMPING_RX_HARDWARE |
1854 SOF_TIMESTAMPING_RX_SOFTWARE |
1855 SOF_TIMESTAMPING_SOFTWARE |
1856 SOF_TIMESTAMPING_RAW_HARDWARE;
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001857 info->phc_index = cpsw->cpts->phc_index;
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001858 info->tx_types =
1859 (1 << HWTSTAMP_TX_OFF) |
1860 (1 << HWTSTAMP_TX_ON);
1861 info->rx_filters =
1862 (1 << HWTSTAMP_FILTER_NONE) |
1863 (1 << HWTSTAMP_FILTER_PTP_V2_EVENT);
1864#else
1865 info->so_timestamping =
1866 SOF_TIMESTAMPING_TX_SOFTWARE |
1867 SOF_TIMESTAMPING_RX_SOFTWARE |
1868 SOF_TIMESTAMPING_SOFTWARE;
1869 info->phc_index = -1;
1870 info->tx_types = 0;
1871 info->rx_filters = 0;
1872#endif
1873 return 0;
1874}
1875
Mugunthan V Nd3bb9c52013-03-11 23:16:36 +00001876static int cpsw_get_settings(struct net_device *ndev,
1877 struct ethtool_cmd *ecmd)
1878{
1879 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001880 struct cpsw_common *cpsw = priv->cpsw;
1881 int slave_no = cpsw_slave_index(cpsw, priv);
Mugunthan V Nd3bb9c52013-03-11 23:16:36 +00001882
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001883 if (cpsw->slaves[slave_no].phy)
1884 return phy_ethtool_gset(cpsw->slaves[slave_no].phy, ecmd);
Mugunthan V Nd3bb9c52013-03-11 23:16:36 +00001885 else
1886 return -EOPNOTSUPP;
1887}
1888
1889static int cpsw_set_settings(struct net_device *ndev, struct ethtool_cmd *ecmd)
1890{
1891 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001892 struct cpsw_common *cpsw = priv->cpsw;
1893 int slave_no = cpsw_slave_index(cpsw, priv);
Mugunthan V Nd3bb9c52013-03-11 23:16:36 +00001894
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001895 if (cpsw->slaves[slave_no].phy)
1896 return phy_ethtool_sset(cpsw->slaves[slave_no].phy, ecmd);
Mugunthan V Nd3bb9c52013-03-11 23:16:36 +00001897 else
1898 return -EOPNOTSUPP;
1899}
1900
Matus Ujhelyid8a64422013-08-20 07:59:38 +02001901static void cpsw_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
1902{
1903 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001904 struct cpsw_common *cpsw = priv->cpsw;
1905 int slave_no = cpsw_slave_index(cpsw, priv);
Matus Ujhelyid8a64422013-08-20 07:59:38 +02001906
1907 wol->supported = 0;
1908 wol->wolopts = 0;
1909
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001910 if (cpsw->slaves[slave_no].phy)
1911 phy_ethtool_get_wol(cpsw->slaves[slave_no].phy, wol);
Matus Ujhelyid8a64422013-08-20 07:59:38 +02001912}
1913
1914static int cpsw_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
1915{
1916 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001917 struct cpsw_common *cpsw = priv->cpsw;
1918 int slave_no = cpsw_slave_index(cpsw, priv);
Matus Ujhelyid8a64422013-08-20 07:59:38 +02001919
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001920 if (cpsw->slaves[slave_no].phy)
1921 return phy_ethtool_set_wol(cpsw->slaves[slave_no].phy, wol);
Matus Ujhelyid8a64422013-08-20 07:59:38 +02001922 else
1923 return -EOPNOTSUPP;
1924}
1925
Mugunthan V N1923d6e2014-09-08 22:54:02 +05301926static void cpsw_get_pauseparam(struct net_device *ndev,
1927 struct ethtool_pauseparam *pause)
1928{
1929 struct cpsw_priv *priv = netdev_priv(ndev);
1930
1931 pause->autoneg = AUTONEG_DISABLE;
1932 pause->rx_pause = priv->rx_pause ? true : false;
1933 pause->tx_pause = priv->tx_pause ? true : false;
1934}
1935
1936static int cpsw_set_pauseparam(struct net_device *ndev,
1937 struct ethtool_pauseparam *pause)
1938{
1939 struct cpsw_priv *priv = netdev_priv(ndev);
1940 bool link;
1941
1942 priv->rx_pause = pause->rx_pause ? true : false;
1943 priv->tx_pause = pause->tx_pause ? true : false;
1944
1945 for_each_slave(priv, _cpsw_adjust_link, priv, &link);
Mugunthan V N1923d6e2014-09-08 22:54:02 +05301946 return 0;
1947}
1948
Grygorii Strashko7898b1d2016-06-24 21:23:44 +03001949static int cpsw_ethtool_op_begin(struct net_device *ndev)
1950{
1951 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03001952 struct cpsw_common *cpsw = priv->cpsw;
Grygorii Strashko7898b1d2016-06-24 21:23:44 +03001953 int ret;
1954
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001955 ret = pm_runtime_get_sync(cpsw->dev);
Grygorii Strashko7898b1d2016-06-24 21:23:44 +03001956 if (ret < 0) {
1957 cpsw_err(priv, drv, "ethtool begin failed %d\n", ret);
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001958 pm_runtime_put_noidle(cpsw->dev);
Grygorii Strashko7898b1d2016-06-24 21:23:44 +03001959 }
1960
1961 return ret;
1962}
1963
1964static void cpsw_ethtool_op_complete(struct net_device *ndev)
1965{
1966 struct cpsw_priv *priv = netdev_priv(ndev);
1967 int ret;
1968
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001969 ret = pm_runtime_put(priv->cpsw->dev);
Grygorii Strashko7898b1d2016-06-24 21:23:44 +03001970 if (ret < 0)
1971 cpsw_err(priv, drv, "ethtool complete failed %d\n", ret);
1972}
1973
Mugunthan V Ndf828592012-03-18 20:17:54 +00001974static const struct ethtool_ops cpsw_ethtool_ops = {
1975 .get_drvinfo = cpsw_get_drvinfo,
1976 .get_msglevel = cpsw_get_msglevel,
1977 .set_msglevel = cpsw_set_msglevel,
1978 .get_link = ethtool_op_get_link,
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001979 .get_ts_info = cpsw_get_ts_info,
Mugunthan V Nd3bb9c52013-03-11 23:16:36 +00001980 .get_settings = cpsw_get_settings,
1981 .set_settings = cpsw_set_settings,
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001982 .get_coalesce = cpsw_get_coalesce,
1983 .set_coalesce = cpsw_set_coalesce,
Mugunthan V Nd9718542013-07-23 15:38:17 +05301984 .get_sset_count = cpsw_get_sset_count,
1985 .get_strings = cpsw_get_strings,
1986 .get_ethtool_stats = cpsw_get_ethtool_stats,
Mugunthan V N1923d6e2014-09-08 22:54:02 +05301987 .get_pauseparam = cpsw_get_pauseparam,
1988 .set_pauseparam = cpsw_set_pauseparam,
Matus Ujhelyid8a64422013-08-20 07:59:38 +02001989 .get_wol = cpsw_get_wol,
1990 .set_wol = cpsw_set_wol,
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05301991 .get_regs_len = cpsw_get_regs_len,
1992 .get_regs = cpsw_get_regs,
Grygorii Strashko7898b1d2016-06-24 21:23:44 +03001993 .begin = cpsw_ethtool_op_begin,
1994 .complete = cpsw_ethtool_op_complete,
Mugunthan V Ndf828592012-03-18 20:17:54 +00001995};
1996
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001997static void cpsw_slave_init(struct cpsw_slave *slave, struct cpsw_common *cpsw,
Richard Cochran549985e2012-11-14 09:07:56 +00001998 u32 slave_reg_ofs, u32 sliver_reg_ofs)
Mugunthan V Ndf828592012-03-18 20:17:54 +00001999{
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03002000 void __iomem *regs = cpsw->regs;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002001 int slave_num = slave->slave_num;
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002002 struct cpsw_slave_data *data = cpsw->data.slave_data + slave_num;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002003
2004 slave->data = data;
Richard Cochran549985e2012-11-14 09:07:56 +00002005 slave->regs = regs + slave_reg_ofs;
2006 slave->sliver = regs + sliver_reg_ofs;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002007 slave->port_vlan = data->dual_emac_res_vlan;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002008}
2009
David Rivshin552165b2016-04-27 21:25:25 -04002010static int cpsw_probe_dt(struct cpsw_platform_data *data,
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002011 struct platform_device *pdev)
2012{
2013 struct device_node *node = pdev->dev.of_node;
2014 struct device_node *slave_node;
2015 int i = 0, ret;
2016 u32 prop;
2017
2018 if (!node)
2019 return -EINVAL;
2020
2021 if (of_property_read_u32(node, "slaves", &prop)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302022 dev_err(&pdev->dev, "Missing slaves property in the DT.\n");
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002023 return -EINVAL;
2024 }
2025 data->slaves = prop;
2026
Mugunthan V Ne86ac132013-03-11 23:16:35 +00002027 if (of_property_read_u32(node, "active_slave", &prop)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302028 dev_err(&pdev->dev, "Missing active_slave property in the DT.\n");
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302029 return -EINVAL;
Richard Cochran78ca0b22012-10-29 08:45:18 +00002030 }
Mugunthan V Ne86ac132013-03-11 23:16:35 +00002031 data->active_slave = prop;
Richard Cochran78ca0b22012-10-29 08:45:18 +00002032
Richard Cochran00ab94e2012-10-29 08:45:19 +00002033 if (of_property_read_u32(node, "cpts_clock_mult", &prop)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302034 dev_err(&pdev->dev, "Missing cpts_clock_mult property in the DT.\n");
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302035 return -EINVAL;
Richard Cochran00ab94e2012-10-29 08:45:19 +00002036 }
2037 data->cpts_clock_mult = prop;
2038
2039 if (of_property_read_u32(node, "cpts_clock_shift", &prop)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302040 dev_err(&pdev->dev, "Missing cpts_clock_shift property in the DT.\n");
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302041 return -EINVAL;
Richard Cochran00ab94e2012-10-29 08:45:19 +00002042 }
2043 data->cpts_clock_shift = prop;
2044
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302045 data->slave_data = devm_kzalloc(&pdev->dev, data->slaves
2046 * sizeof(struct cpsw_slave_data),
2047 GFP_KERNEL);
Joe Perchesb2adaca2013-02-03 17:43:58 +00002048 if (!data->slave_data)
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302049 return -ENOMEM;
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002050
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002051 if (of_property_read_u32(node, "cpdma_channels", &prop)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302052 dev_err(&pdev->dev, "Missing cpdma_channels property in the DT.\n");
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302053 return -EINVAL;
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002054 }
2055 data->channels = prop;
2056
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002057 if (of_property_read_u32(node, "ale_entries", &prop)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302058 dev_err(&pdev->dev, "Missing ale_entries property in the DT.\n");
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302059 return -EINVAL;
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002060 }
2061 data->ale_entries = prop;
2062
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002063 if (of_property_read_u32(node, "bd_ram_size", &prop)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302064 dev_err(&pdev->dev, "Missing bd_ram_size property in the DT.\n");
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302065 return -EINVAL;
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002066 }
2067 data->bd_ram_size = prop;
2068
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002069 if (of_property_read_u32(node, "mac_control", &prop)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302070 dev_err(&pdev->dev, "Missing mac_control property in the DT.\n");
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302071 return -EINVAL;
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002072 }
2073 data->mac_control = prop;
2074
Markus Pargmann281abd92013-10-04 14:44:40 +02002075 if (of_property_read_bool(node, "dual_emac"))
2076 data->dual_emac = 1;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002077
Vaibhav Hiremath1fb19aa2012-11-14 09:07:55 +00002078 /*
2079 * Populate all the child nodes here...
2080 */
2081 ret = of_platform_populate(node, NULL, NULL, &pdev->dev);
2082 /* We do not want to force this, as in some cases may not have child */
2083 if (ret)
George Cherian88c99ff2014-05-12 10:21:19 +05302084 dev_warn(&pdev->dev, "Doesn't have any child node\n");
Vaibhav Hiremath1fb19aa2012-11-14 09:07:55 +00002085
Ben Hutchings8658aaf2016-06-21 01:16:31 +01002086 for_each_available_child_of_node(node, slave_node) {
Richard Cochran549985e2012-11-14 09:07:56 +00002087 struct cpsw_slave_data *slave_data = data->slave_data + i;
2088 const void *mac_addr = NULL;
Richard Cochran549985e2012-11-14 09:07:56 +00002089 int lenp;
2090 const __be32 *parp;
Richard Cochran549985e2012-11-14 09:07:56 +00002091
Markus Pargmannf468b102013-10-04 14:44:39 +02002092 /* This is no slave child node, continue */
2093 if (strcmp(slave_node->name, "slave"))
2094 continue;
2095
David Rivshin552165b2016-04-27 21:25:25 -04002096 slave_data->phy_node = of_parse_phandle(slave_node,
2097 "phy-handle", 0);
David Rivshinf1eea5c2015-12-16 23:02:10 -05002098 parp = of_get_property(slave_node, "phy_id", &lenp);
David Rivshinae092b52016-04-27 21:38:26 -04002099 if (slave_data->phy_node) {
2100 dev_dbg(&pdev->dev,
2101 "slave[%d] using phy-handle=\"%s\"\n",
2102 i, slave_data->phy_node->full_name);
2103 } else if (of_phy_is_fixed_link(slave_node)) {
David Rivshindfc0a6d2015-12-16 23:02:11 -05002104 /* In the case of a fixed PHY, the DT node associated
2105 * to the PHY is the Ethernet MAC DT node.
2106 */
Markus Brunner1f71e8c2015-11-03 22:09:51 +01002107 ret = of_phy_register_fixed_link(slave_node);
2108 if (ret)
2109 return ret;
David Rivshin06cd6d62016-04-27 21:45:45 -04002110 slave_data->phy_node = of_node_get(slave_node);
David Rivshinf1eea5c2015-12-16 23:02:10 -05002111 } else if (parp) {
2112 u32 phyid;
2113 struct device_node *mdio_node;
2114 struct platform_device *mdio;
2115
2116 if (lenp != (sizeof(__be32) * 2)) {
2117 dev_err(&pdev->dev, "Invalid slave[%d] phy_id property\n", i);
2118 goto no_phy_slave;
2119 }
2120 mdio_node = of_find_node_by_phandle(be32_to_cpup(parp));
2121 phyid = be32_to_cpup(parp+1);
2122 mdio = of_find_device_by_node(mdio_node);
2123 of_node_put(mdio_node);
2124 if (!mdio) {
2125 dev_err(&pdev->dev, "Missing mdio platform device\n");
2126 return -EINVAL;
2127 }
2128 snprintf(slave_data->phy_id, sizeof(slave_data->phy_id),
2129 PHY_ID_FMT, mdio->name, phyid);
2130 } else {
David Rivshinae092b52016-04-27 21:38:26 -04002131 dev_err(&pdev->dev,
2132 "No slave[%d] phy_id, phy-handle, or fixed-link property\n",
2133 i);
Markus Brunner1f71e8c2015-11-03 22:09:51 +01002134 goto no_phy_slave;
2135 }
Mugunthan V N47276fc2014-10-24 18:51:33 +05302136 slave_data->phy_if = of_get_phy_mode(slave_node);
2137 if (slave_data->phy_if < 0) {
2138 dev_err(&pdev->dev, "Missing or malformed slave[%d] phy-mode property\n",
2139 i);
2140 return slave_data->phy_if;
2141 }
2142
2143no_phy_slave:
Richard Cochran549985e2012-11-14 09:07:56 +00002144 mac_addr = of_get_mac_address(slave_node);
Markus Pargmann0ba517b2014-09-29 08:53:17 +02002145 if (mac_addr) {
Richard Cochran549985e2012-11-14 09:07:56 +00002146 memcpy(slave_data->mac_addr, mac_addr, ETH_ALEN);
Markus Pargmann0ba517b2014-09-29 08:53:17 +02002147 } else {
Mugunthan V Nb6745f62015-09-21 15:56:50 +05302148 ret = ti_cm_get_macid(&pdev->dev, i,
2149 slave_data->mac_addr);
2150 if (ret)
2151 return ret;
Markus Pargmann0ba517b2014-09-29 08:53:17 +02002152 }
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002153 if (data->dual_emac) {
Mugunthan V N91c41662013-04-15 07:31:28 +00002154 if (of_property_read_u32(slave_node, "dual_emac_res_vlan",
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002155 &prop)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302156 dev_err(&pdev->dev, "Missing dual_emac_res_vlan in DT.\n");
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002157 slave_data->dual_emac_res_vlan = i+1;
George Cherian88c99ff2014-05-12 10:21:19 +05302158 dev_err(&pdev->dev, "Using %d as Reserved VLAN for %d slave\n",
2159 slave_data->dual_emac_res_vlan, i);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002160 } else {
2161 slave_data->dual_emac_res_vlan = prop;
2162 }
2163 }
2164
Richard Cochran549985e2012-11-14 09:07:56 +00002165 i++;
Mugunthan V N3a27bfa2013-12-02 12:53:39 +05302166 if (i == data->slaves)
2167 break;
Richard Cochran549985e2012-11-14 09:07:56 +00002168 }
2169
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002170 return 0;
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002171}
2172
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002173static int cpsw_probe_dual_emac(struct cpsw_priv *priv)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002174{
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002175 struct cpsw_common *cpsw = priv->cpsw;
2176 struct cpsw_platform_data *data = &cpsw->data;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002177 struct net_device *ndev;
2178 struct cpsw_priv *priv_sl2;
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +03002179 int ret = 0;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002180
2181 ndev = alloc_etherdev(sizeof(struct cpsw_priv));
2182 if (!ndev) {
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002183 dev_err(cpsw->dev, "cpsw: error allocating net_device\n");
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002184 return -ENOMEM;
2185 }
2186
2187 priv_sl2 = netdev_priv(ndev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002188 priv_sl2->cpsw = cpsw;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002189 priv_sl2->ndev = ndev;
2190 priv_sl2->dev = &ndev->dev;
2191 priv_sl2->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002192
2193 if (is_valid_ether_addr(data->slave_data[1].mac_addr)) {
2194 memcpy(priv_sl2->mac_addr, data->slave_data[1].mac_addr,
2195 ETH_ALEN);
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002196 dev_info(cpsw->dev, "cpsw: Detected MACID = %pM\n",
2197 priv_sl2->mac_addr);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002198 } else {
2199 random_ether_addr(priv_sl2->mac_addr);
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002200 dev_info(cpsw->dev, "cpsw: Random MACID = %pM\n",
2201 priv_sl2->mac_addr);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002202 }
2203 memcpy(ndev->dev_addr, priv_sl2->mac_addr, ETH_ALEN);
2204
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002205 priv_sl2->emac_port = 1;
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002206 cpsw->slaves[1].ndev = ndev;
Patrick McHardyf6469682013-04-19 02:04:27 +00002207 ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002208
2209 ndev->netdev_ops = &cpsw_netdev_ops;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00002210 ndev->ethtool_ops = &cpsw_ethtool_ops;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002211
2212 /* register the network device */
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002213 SET_NETDEV_DEV(ndev, cpsw->dev);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002214 ret = register_netdev(ndev);
2215 if (ret) {
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002216 dev_err(cpsw->dev, "cpsw: error registering net device\n");
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002217 free_netdev(ndev);
2218 ret = -ENODEV;
2219 }
2220
2221 return ret;
2222}
2223
Mugunthan V N7da11602015-08-12 15:22:53 +05302224#define CPSW_QUIRK_IRQ BIT(0)
2225
2226static struct platform_device_id cpsw_devtype[] = {
2227 {
2228 /* keep it for existing comaptibles */
2229 .name = "cpsw",
2230 .driver_data = CPSW_QUIRK_IRQ,
2231 }, {
2232 .name = "am335x-cpsw",
2233 .driver_data = CPSW_QUIRK_IRQ,
2234 }, {
2235 .name = "am4372-cpsw",
2236 .driver_data = 0,
2237 }, {
2238 .name = "dra7-cpsw",
2239 .driver_data = 0,
2240 }, {
2241 /* sentinel */
2242 }
2243};
2244MODULE_DEVICE_TABLE(platform, cpsw_devtype);
2245
2246enum ti_cpsw_type {
2247 CPSW = 0,
2248 AM335X_CPSW,
2249 AM4372_CPSW,
2250 DRA7_CPSW,
2251};
2252
2253static const struct of_device_id cpsw_of_mtable[] = {
2254 { .compatible = "ti,cpsw", .data = &cpsw_devtype[CPSW], },
2255 { .compatible = "ti,am335x-cpsw", .data = &cpsw_devtype[AM335X_CPSW], },
2256 { .compatible = "ti,am4372-cpsw", .data = &cpsw_devtype[AM4372_CPSW], },
2257 { .compatible = "ti,dra7-cpsw", .data = &cpsw_devtype[DRA7_CPSW], },
2258 { /* sentinel */ },
2259};
2260MODULE_DEVICE_TABLE(of, cpsw_of_mtable);
2261
Bill Pemberton663e12e2012-12-03 09:23:45 -05002262static int cpsw_probe(struct platform_device *pdev)
Mugunthan V Ndf828592012-03-18 20:17:54 +00002263{
Ivan Khoronzhukef4183a2016-08-10 02:22:35 +03002264 struct clk *clk;
Sebastian Siewiord1bd9ac2013-04-24 08:48:23 +00002265 struct cpsw_platform_data *data;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002266 struct net_device *ndev;
2267 struct cpsw_priv *priv;
2268 struct cpdma_params dma_params;
2269 struct cpsw_ale_params ale_params;
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302270 void __iomem *ss_regs;
2271 struct resource *res, *ss_res;
Mugunthan V N7da11602015-08-12 15:22:53 +05302272 const struct of_device_id *of_id;
Mugunthan V N1d147cc2015-09-07 15:16:44 +05302273 struct gpio_descs *mode;
Richard Cochran549985e2012-11-14 09:07:56 +00002274 u32 slave_offset, sliver_offset, slave_size;
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03002275 struct cpsw_common *cpsw;
Felipe Balbi5087b912015-01-16 10:11:11 -06002276 int ret = 0, i;
2277 int irq;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002278
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03002279 cpsw = devm_kzalloc(&pdev->dev, sizeof(struct cpsw_common), GFP_KERNEL);
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002280 cpsw->dev = &pdev->dev;
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03002281
Mugunthan V Ndf828592012-03-18 20:17:54 +00002282 ndev = alloc_etherdev(sizeof(struct cpsw_priv));
2283 if (!ndev) {
George Cherian88c99ff2014-05-12 10:21:19 +05302284 dev_err(&pdev->dev, "error allocating net_device\n");
Mugunthan V Ndf828592012-03-18 20:17:54 +00002285 return -ENOMEM;
2286 }
2287
2288 platform_set_drvdata(pdev, ndev);
2289 priv = netdev_priv(ndev);
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03002290 priv->cpsw = cpsw;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002291 priv->ndev = ndev;
2292 priv->dev = &ndev->dev;
2293 priv->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002294 cpsw->rx_packet_max = max(rx_packet_max, 128);
2295 cpsw->cpts = devm_kzalloc(&pdev->dev, sizeof(struct cpts), GFP_KERNEL);
2296 if (!cpsw->cpts) {
George Cherian88c99ff2014-05-12 10:21:19 +05302297 dev_err(&pdev->dev, "error allocating cpts\n");
Markus Pargmann4d507df2014-09-29 08:53:14 +02002298 ret = -ENOMEM;
Mugunthan V N9232b162013-02-11 09:52:19 +00002299 goto clean_ndev_ret;
2300 }
Mugunthan V Ndf828592012-03-18 20:17:54 +00002301
Mugunthan V N1d147cc2015-09-07 15:16:44 +05302302 mode = devm_gpiod_get_array_optional(&pdev->dev, "mode", GPIOD_OUT_LOW);
2303 if (IS_ERR(mode)) {
2304 ret = PTR_ERR(mode);
2305 dev_err(&pdev->dev, "gpio request failed, ret %d\n", ret);
2306 goto clean_ndev_ret;
2307 }
2308
Vaibhav Hiremath1fb19aa2012-11-14 09:07:55 +00002309 /*
2310 * This may be required here for child devices.
2311 */
2312 pm_runtime_enable(&pdev->dev);
2313
Mugunthan V N739683b2013-06-06 23:45:14 +05302314 /* Select default pin state */
2315 pinctrl_pm_select_default_state(&pdev->dev);
2316
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002317 if (cpsw_probe_dt(&cpsw->data, pdev)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302318 dev_err(&pdev->dev, "cpsw: platform data missing\n");
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002319 ret = -ENODEV;
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302320 goto clean_runtime_disable_ret;
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002321 }
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002322 data = &cpsw->data;
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002323
Mugunthan V Ndf828592012-03-18 20:17:54 +00002324 if (is_valid_ether_addr(data->slave_data[0].mac_addr)) {
2325 memcpy(priv->mac_addr, data->slave_data[0].mac_addr, ETH_ALEN);
George Cherian88c99ff2014-05-12 10:21:19 +05302326 dev_info(&pdev->dev, "Detected MACID = %pM\n", priv->mac_addr);
Mugunthan V Ndf828592012-03-18 20:17:54 +00002327 } else {
Joe Perches7efd26d2012-07-12 19:33:06 +00002328 eth_random_addr(priv->mac_addr);
George Cherian88c99ff2014-05-12 10:21:19 +05302329 dev_info(&pdev->dev, "Random MACID = %pM\n", priv->mac_addr);
Mugunthan V Ndf828592012-03-18 20:17:54 +00002330 }
2331
2332 memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
2333
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002334 cpsw->slaves = devm_kzalloc(&pdev->dev,
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302335 sizeof(struct cpsw_slave) * data->slaves,
2336 GFP_KERNEL);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002337 if (!cpsw->slaves) {
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302338 ret = -ENOMEM;
2339 goto clean_runtime_disable_ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002340 }
2341 for (i = 0; i < data->slaves; i++)
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002342 cpsw->slaves[i].slave_num = i;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002343
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002344 cpsw->slaves[0].ndev = ndev;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002345 priv->emac_port = 0;
2346
Ivan Khoronzhukef4183a2016-08-10 02:22:35 +03002347 clk = devm_clk_get(&pdev->dev, "fck");
2348 if (IS_ERR(clk)) {
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302349 dev_err(priv->dev, "fck is not found\n");
Mugunthan V Nf150bd72012-07-17 08:09:50 +00002350 ret = -ENODEV;
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302351 goto clean_runtime_disable_ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002352 }
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002353 cpsw->bus_freq_mhz = clk_get_rate(clk) / 1000000;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002354
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302355 ss_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2356 ss_regs = devm_ioremap_resource(&pdev->dev, ss_res);
2357 if (IS_ERR(ss_regs)) {
2358 ret = PTR_ERR(ss_regs);
2359 goto clean_runtime_disable_ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002360 }
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03002361 cpsw->regs = ss_regs;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002362
Mugunthan V Nf280e892013-12-11 22:09:05 -06002363 /* Need to enable clocks with runtime PM api to access module
2364 * registers
2365 */
Grygorii Strashko108a6532016-06-24 21:23:42 +03002366 ret = pm_runtime_get_sync(&pdev->dev);
2367 if (ret < 0) {
2368 pm_runtime_put_noidle(&pdev->dev);
2369 goto clean_runtime_disable_ret;
2370 }
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002371 cpsw->version = readl(&cpsw->regs->id_ver);
Mugunthan V Nf280e892013-12-11 22:09:05 -06002372 pm_runtime_put_sync(&pdev->dev);
2373
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302374 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03002375 cpsw->wr_regs = devm_ioremap_resource(&pdev->dev, res);
2376 if (IS_ERR(cpsw->wr_regs)) {
2377 ret = PTR_ERR(cpsw->wr_regs);
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302378 goto clean_runtime_disable_ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002379 }
Mugunthan V Ndf828592012-03-18 20:17:54 +00002380
2381 memset(&dma_params, 0, sizeof(dma_params));
Richard Cochran549985e2012-11-14 09:07:56 +00002382 memset(&ale_params, 0, sizeof(ale_params));
2383
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002384 switch (cpsw->version) {
Richard Cochran549985e2012-11-14 09:07:56 +00002385 case CPSW_VERSION_1:
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03002386 cpsw->host_port_regs = ss_regs + CPSW1_HOST_PORT_OFFSET;
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002387 cpsw->cpts->reg = ss_regs + CPSW1_CPTS_OFFSET;
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03002388 cpsw->hw_stats = ss_regs + CPSW1_HW_STATS;
Richard Cochran549985e2012-11-14 09:07:56 +00002389 dma_params.dmaregs = ss_regs + CPSW1_CPDMA_OFFSET;
2390 dma_params.txhdp = ss_regs + CPSW1_STATERAM_OFFSET;
2391 ale_params.ale_regs = ss_regs + CPSW1_ALE_OFFSET;
2392 slave_offset = CPSW1_SLAVE_OFFSET;
2393 slave_size = CPSW1_SLAVE_SIZE;
2394 sliver_offset = CPSW1_SLIVER_OFFSET;
2395 dma_params.desc_mem_phys = 0;
2396 break;
2397 case CPSW_VERSION_2:
Mugunthan V Nc193f362013-08-05 17:30:05 +05302398 case CPSW_VERSION_3:
Mugunthan V N926489b2013-08-12 17:11:15 +05302399 case CPSW_VERSION_4:
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03002400 cpsw->host_port_regs = ss_regs + CPSW2_HOST_PORT_OFFSET;
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002401 cpsw->cpts->reg = ss_regs + CPSW2_CPTS_OFFSET;
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03002402 cpsw->hw_stats = ss_regs + CPSW2_HW_STATS;
Richard Cochran549985e2012-11-14 09:07:56 +00002403 dma_params.dmaregs = ss_regs + CPSW2_CPDMA_OFFSET;
2404 dma_params.txhdp = ss_regs + CPSW2_STATERAM_OFFSET;
2405 ale_params.ale_regs = ss_regs + CPSW2_ALE_OFFSET;
2406 slave_offset = CPSW2_SLAVE_OFFSET;
2407 slave_size = CPSW2_SLAVE_SIZE;
2408 sliver_offset = CPSW2_SLIVER_OFFSET;
2409 dma_params.desc_mem_phys =
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302410 (u32 __force) ss_res->start + CPSW2_BD_OFFSET;
Richard Cochran549985e2012-11-14 09:07:56 +00002411 break;
2412 default:
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002413 dev_err(priv->dev, "unknown version 0x%08x\n", cpsw->version);
Richard Cochran549985e2012-11-14 09:07:56 +00002414 ret = -ENODEV;
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302415 goto clean_runtime_disable_ret;
Richard Cochran549985e2012-11-14 09:07:56 +00002416 }
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002417 for (i = 0; i < cpsw->data.slaves; i++) {
2418 struct cpsw_slave *slave = &cpsw->slaves[i];
2419
2420 cpsw_slave_init(slave, cpsw, slave_offset, sliver_offset);
Richard Cochran549985e2012-11-14 09:07:56 +00002421 slave_offset += slave_size;
2422 sliver_offset += SLIVER_SIZE;
2423 }
2424
Mugunthan V Ndf828592012-03-18 20:17:54 +00002425 dma_params.dev = &pdev->dev;
Richard Cochran549985e2012-11-14 09:07:56 +00002426 dma_params.rxthresh = dma_params.dmaregs + CPDMA_RXTHRESH;
2427 dma_params.rxfree = dma_params.dmaregs + CPDMA_RXFREE;
2428 dma_params.rxhdp = dma_params.txhdp + CPDMA_RXHDP;
2429 dma_params.txcp = dma_params.txhdp + CPDMA_TXCP;
2430 dma_params.rxcp = dma_params.txhdp + CPDMA_RXCP;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002431
2432 dma_params.num_chan = data->channels;
2433 dma_params.has_soft_reset = true;
2434 dma_params.min_packet_size = CPSW_MIN_PACKET_SIZE;
2435 dma_params.desc_mem_size = data->bd_ram_size;
2436 dma_params.desc_align = 16;
2437 dma_params.has_ext_regs = true;
Richard Cochran549985e2012-11-14 09:07:56 +00002438 dma_params.desc_hw_addr = dma_params.desc_mem_phys;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002439
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03002440 cpsw->dma = cpdma_ctlr_create(&dma_params);
2441 if (!cpsw->dma) {
Mugunthan V Ndf828592012-03-18 20:17:54 +00002442 dev_err(priv->dev, "error initializing dma\n");
2443 ret = -ENOMEM;
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302444 goto clean_runtime_disable_ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002445 }
2446
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03002447 cpsw->txch = cpdma_chan_create(cpsw->dma, tx_chan_num(0),
Mugunthan V Ndf828592012-03-18 20:17:54 +00002448 cpsw_tx_handler);
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03002449 cpsw->rxch = cpdma_chan_create(cpsw->dma, rx_chan_num(0),
Mugunthan V Ndf828592012-03-18 20:17:54 +00002450 cpsw_rx_handler);
2451
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03002452 if (WARN_ON(!cpsw->txch || !cpsw->rxch)) {
Mugunthan V Ndf828592012-03-18 20:17:54 +00002453 dev_err(priv->dev, "error initializing dma channels\n");
2454 ret = -ENOMEM;
2455 goto clean_dma_ret;
2456 }
2457
Mugunthan V Ndf828592012-03-18 20:17:54 +00002458 ale_params.dev = &ndev->dev;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002459 ale_params.ale_ageout = ale_ageout;
2460 ale_params.ale_entries = data->ale_entries;
2461 ale_params.ale_ports = data->slaves;
2462
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002463 cpsw->ale = cpsw_ale_create(&ale_params);
2464 if (!cpsw->ale) {
Mugunthan V Ndf828592012-03-18 20:17:54 +00002465 dev_err(priv->dev, "error initializing ale engine\n");
2466 ret = -ENODEV;
2467 goto clean_dma_ret;
2468 }
2469
Felipe Balbic03abd82015-01-16 10:11:12 -06002470 ndev->irq = platform_get_irq(pdev, 1);
Mugunthan V Ndf828592012-03-18 20:17:54 +00002471 if (ndev->irq < 0) {
2472 dev_err(priv->dev, "error getting irq resource\n");
Julia Lawallc1e33342015-12-26 20:12:13 +01002473 ret = ndev->irq;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002474 goto clean_ale_ret;
2475 }
2476
Mugunthan V N7da11602015-08-12 15:22:53 +05302477 of_id = of_match_device(cpsw_of_mtable, &pdev->dev);
2478 if (of_id) {
2479 pdev->id_entry = of_id->data;
2480 if (pdev->id_entry->driver_data)
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +03002481 cpsw->quirk_irq = true;
Mugunthan V N7da11602015-08-12 15:22:53 +05302482 }
2483
Felipe Balbic03abd82015-01-16 10:11:12 -06002484 /* Grab RX and TX IRQs. Note that we also have RX_THRESHOLD and
2485 * MISC IRQs which are always kept disabled with this driver so
2486 * we will not request them.
2487 *
2488 * If anyone wants to implement support for those, make sure to
2489 * first request and append them to irqs_table array.
2490 */
Daniel Mackc2b32e52014-09-04 09:00:23 +02002491
Felipe Balbic03abd82015-01-16 10:11:12 -06002492 /* RX IRQ */
Felipe Balbi5087b912015-01-16 10:11:11 -06002493 irq = platform_get_irq(pdev, 1);
Julia Lawallc1e33342015-12-26 20:12:13 +01002494 if (irq < 0) {
2495 ret = irq;
Felipe Balbi5087b912015-01-16 10:11:11 -06002496 goto clean_ale_ret;
Julia Lawallc1e33342015-12-26 20:12:13 +01002497 }
Felipe Balbi5087b912015-01-16 10:11:11 -06002498
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +03002499 cpsw->irqs_table[0] = irq;
Felipe Balbic03abd82015-01-16 10:11:12 -06002500 ret = devm_request_irq(&pdev->dev, irq, cpsw_rx_interrupt,
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +03002501 0, dev_name(&pdev->dev), cpsw);
Felipe Balbi5087b912015-01-16 10:11:11 -06002502 if (ret < 0) {
2503 dev_err(priv->dev, "error attaching irq (%d)\n", ret);
2504 goto clean_ale_ret;
2505 }
2506
Felipe Balbic03abd82015-01-16 10:11:12 -06002507 /* TX IRQ */
Felipe Balbi5087b912015-01-16 10:11:11 -06002508 irq = platform_get_irq(pdev, 2);
Julia Lawallc1e33342015-12-26 20:12:13 +01002509 if (irq < 0) {
2510 ret = irq;
Felipe Balbi5087b912015-01-16 10:11:11 -06002511 goto clean_ale_ret;
Julia Lawallc1e33342015-12-26 20:12:13 +01002512 }
Felipe Balbi5087b912015-01-16 10:11:11 -06002513
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +03002514 cpsw->irqs_table[1] = irq;
Felipe Balbic03abd82015-01-16 10:11:12 -06002515 ret = devm_request_irq(&pdev->dev, irq, cpsw_tx_interrupt,
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +03002516 0, dev_name(&pdev->dev), cpsw);
Felipe Balbi5087b912015-01-16 10:11:11 -06002517 if (ret < 0) {
2518 dev_err(priv->dev, "error attaching irq (%d)\n", ret);
2519 goto clean_ale_ret;
2520 }
Daniel Mackc2b32e52014-09-04 09:00:23 +02002521
Patrick McHardyf6469682013-04-19 02:04:27 +00002522 ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002523
2524 ndev->netdev_ops = &cpsw_netdev_ops;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00002525 ndev->ethtool_ops = &cpsw_ethtool_ops;
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +03002526 netif_napi_add(ndev, &cpsw->napi_rx, cpsw_rx_poll, CPSW_POLL_WEIGHT);
2527 netif_tx_napi_add(ndev, &cpsw->napi_tx, cpsw_tx_poll, CPSW_POLL_WEIGHT);
Mugunthan V Ndf828592012-03-18 20:17:54 +00002528
2529 /* register the network device */
2530 SET_NETDEV_DEV(ndev, &pdev->dev);
2531 ret = register_netdev(ndev);
2532 if (ret) {
2533 dev_err(priv->dev, "error registering net device\n");
2534 ret = -ENODEV;
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302535 goto clean_ale_ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002536 }
2537
Olof Johansson1a3b5052013-12-11 15:58:07 -08002538 cpsw_notice(priv, probe, "initialized device (regs %pa, irq %d)\n",
2539 &ss_res->start, ndev->irq);
Mugunthan V Ndf828592012-03-18 20:17:54 +00002540
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002541 if (cpsw->data.dual_emac) {
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002542 ret = cpsw_probe_dual_emac(priv);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002543 if (ret) {
2544 cpsw_err(priv, probe, "error probe slave 2 emac interface\n");
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302545 goto clean_ale_ret;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002546 }
2547 }
2548
Mugunthan V Ndf828592012-03-18 20:17:54 +00002549 return 0;
2550
Mugunthan V Ndf828592012-03-18 20:17:54 +00002551clean_ale_ret:
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002552 cpsw_ale_destroy(cpsw->ale);
Mugunthan V Ndf828592012-03-18 20:17:54 +00002553clean_dma_ret:
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03002554 cpdma_ctlr_destroy(cpsw->dma);
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302555clean_runtime_disable_ret:
Mugunthan V Nf150bd72012-07-17 08:09:50 +00002556 pm_runtime_disable(&pdev->dev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00002557clean_ndev_ret:
Sebastian Siewiord1bd9ac2013-04-24 08:48:23 +00002558 free_netdev(priv->ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00002559 return ret;
2560}
2561
Bill Pemberton663e12e2012-12-03 09:23:45 -05002562static int cpsw_remove(struct platform_device *pdev)
Mugunthan V Ndf828592012-03-18 20:17:54 +00002563{
2564 struct net_device *ndev = platform_get_drvdata(pdev);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002565 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Grygorii Strashko8a0b6dc2016-07-28 20:50:35 +03002566 int ret;
2567
2568 ret = pm_runtime_get_sync(&pdev->dev);
2569 if (ret < 0) {
2570 pm_runtime_put_noidle(&pdev->dev);
2571 return ret;
2572 }
Mugunthan V Ndf828592012-03-18 20:17:54 +00002573
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002574 if (cpsw->data.dual_emac)
2575 unregister_netdev(cpsw->slaves[1].ndev);
Sebastian Siewiord1bd9ac2013-04-24 08:48:23 +00002576 unregister_netdev(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00002577
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002578 cpsw_ale_destroy(cpsw->ale);
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03002579 cpdma_ctlr_destroy(cpsw->dma);
Grygorii Strashko3bf2cb32016-07-28 20:50:36 +03002580 of_platform_depopulate(&pdev->dev);
Grygorii Strashko8a0b6dc2016-07-28 20:50:35 +03002581 pm_runtime_put_sync(&pdev->dev);
2582 pm_runtime_disable(&pdev->dev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002583 if (cpsw->data.dual_emac)
2584 free_netdev(cpsw->slaves[1].ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00002585 free_netdev(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00002586 return 0;
2587}
2588
Grygorii Strashko8963a502015-02-27 13:19:45 +02002589#ifdef CONFIG_PM_SLEEP
Mugunthan V Ndf828592012-03-18 20:17:54 +00002590static int cpsw_suspend(struct device *dev)
2591{
2592 struct platform_device *pdev = to_platform_device(dev);
2593 struct net_device *ndev = platform_get_drvdata(pdev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002594 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00002595
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002596 if (cpsw->data.dual_emac) {
Mugunthan V N618073e2014-09-11 22:52:38 +05302597 int i;
Daniel Mack1e7a2e22013-11-15 08:29:16 +01002598
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002599 for (i = 0; i < cpsw->data.slaves; i++) {
2600 if (netif_running(cpsw->slaves[i].ndev))
2601 cpsw_ndo_stop(cpsw->slaves[i].ndev);
Mugunthan V N618073e2014-09-11 22:52:38 +05302602 }
2603 } else {
2604 if (netif_running(ndev))
2605 cpsw_ndo_stop(ndev);
Mugunthan V N618073e2014-09-11 22:52:38 +05302606 }
Daniel Mack1e7a2e22013-11-15 08:29:16 +01002607
Mugunthan V N739683b2013-06-06 23:45:14 +05302608 /* Select sleep pin state */
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002609 pinctrl_pm_select_sleep_state(dev);
Mugunthan V N739683b2013-06-06 23:45:14 +05302610
Mugunthan V Ndf828592012-03-18 20:17:54 +00002611 return 0;
2612}
2613
2614static int cpsw_resume(struct device *dev)
2615{
2616 struct platform_device *pdev = to_platform_device(dev);
2617 struct net_device *ndev = platform_get_drvdata(pdev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002618 struct cpsw_common *cpsw = netdev_priv(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00002619
Mugunthan V N739683b2013-06-06 23:45:14 +05302620 /* Select default pin state */
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002621 pinctrl_pm_select_default_state(dev);
Mugunthan V N739683b2013-06-06 23:45:14 +05302622
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002623 if (cpsw->data.dual_emac) {
Mugunthan V N618073e2014-09-11 22:52:38 +05302624 int i;
2625
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002626 for (i = 0; i < cpsw->data.slaves; i++) {
2627 if (netif_running(cpsw->slaves[i].ndev))
2628 cpsw_ndo_open(cpsw->slaves[i].ndev);
Mugunthan V N618073e2014-09-11 22:52:38 +05302629 }
2630 } else {
2631 if (netif_running(ndev))
2632 cpsw_ndo_open(ndev);
2633 }
Mugunthan V Ndf828592012-03-18 20:17:54 +00002634 return 0;
2635}
Grygorii Strashko8963a502015-02-27 13:19:45 +02002636#endif
Mugunthan V Ndf828592012-03-18 20:17:54 +00002637
Grygorii Strashko8963a502015-02-27 13:19:45 +02002638static SIMPLE_DEV_PM_OPS(cpsw_pm_ops, cpsw_suspend, cpsw_resume);
Mugunthan V Ndf828592012-03-18 20:17:54 +00002639
2640static struct platform_driver cpsw_driver = {
2641 .driver = {
2642 .name = "cpsw",
Mugunthan V Ndf828592012-03-18 20:17:54 +00002643 .pm = &cpsw_pm_ops,
Sachin Kamat1e5c76d2013-09-30 09:55:12 +05302644 .of_match_table = cpsw_of_mtable,
Mugunthan V Ndf828592012-03-18 20:17:54 +00002645 },
2646 .probe = cpsw_probe,
Bill Pemberton663e12e2012-12-03 09:23:45 -05002647 .remove = cpsw_remove,
Mugunthan V Ndf828592012-03-18 20:17:54 +00002648};
2649
Grygorii Strashko6fb3b6b52015-10-23 14:41:12 +03002650module_platform_driver(cpsw_driver);
Mugunthan V Ndf828592012-03-18 20:17:54 +00002651
2652MODULE_LICENSE("GPL");
2653MODULE_AUTHOR("Cyril Chemparathy <cyril@ti.com>");
2654MODULE_AUTHOR("Mugunthan V N <mugunthanvnm@ti.com>");
2655MODULE_DESCRIPTION("TI CPSW Ethernet driver");