blob: 421ebdaf12089da578a2806a186bb28981cd69e8 [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 Khoronzhuk2a05a622016-08-10 02:22:44 +03001216static void cpsw_slave_stop(struct cpsw_slave *slave, struct cpsw_common *cpsw)
Sebastian Siewioraacebbf2013-04-23 07:31:36 +00001217{
Schuyler Patton3995d262014-03-03 16:19:06 +05301218 u32 slave_port;
1219
Ivan Khoronzhuk6f1f5832016-08-10 02:22:34 +03001220 slave_port = cpsw_get_slave_port(slave->slave_num);
Schuyler Patton3995d262014-03-03 16:19:06 +05301221
Sebastian Siewioraacebbf2013-04-23 07:31:36 +00001222 if (!slave->phy)
1223 return;
1224 phy_stop(slave->phy);
1225 phy_disconnect(slave->phy);
1226 slave->phy = NULL;
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001227 cpsw_ale_control_set(cpsw->ale, slave_port,
Schuyler Patton3995d262014-03-03 16:19:06 +05301228 ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
Grygorii Strashko1f95ba02016-06-24 21:23:41 +03001229 soft_reset_slave(slave);
Sebastian Siewioraacebbf2013-04-23 07:31:36 +00001230}
1231
Mugunthan V Ndf828592012-03-18 20:17:54 +00001232static int cpsw_ndo_open(struct net_device *ndev)
1233{
1234 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03001235 struct cpsw_common *cpsw = priv->cpsw;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001236 int i, ret;
1237 u32 reg;
1238
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001239 ret = pm_runtime_get_sync(cpsw->dev);
Grygorii Strashko108a6532016-06-24 21:23:42 +03001240 if (ret < 0) {
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001241 pm_runtime_put_noidle(cpsw->dev);
Grygorii Strashko108a6532016-06-24 21:23:42 +03001242 return ret;
1243 }
Grygorii Strashko3fa88c52016-04-19 21:09:49 +03001244
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001245 if (!cpsw_common_res_usage_state(cpsw))
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001246 cpsw_intr_disable(cpsw);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001247 netif_carrier_off(ndev);
1248
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001249 reg = cpsw->version;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001250
1251 dev_info(priv->dev, "initializing cpsw version %d.%d (%d)\n",
1252 CPSW_MAJOR_VERSION(reg), CPSW_MINOR_VERSION(reg),
1253 CPSW_RTL_VERSION(reg));
1254
1255 /* initialize host and slave ports */
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001256 if (!cpsw_common_res_usage_state(cpsw))
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001257 cpsw_init_host_port(priv);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001258 for_each_slave(priv, cpsw_slave_open, priv);
1259
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001260 /* Add default VLAN */
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001261 if (!cpsw->data.dual_emac)
Mugunthan V Ne6afea02014-06-18 17:21:48 +05301262 cpsw_add_default_vlan(priv);
1263 else
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001264 cpsw_ale_add_vlan(cpsw->ale, cpsw->data.default_vlan,
Grygorii Strashko61f1cef2016-04-07 15:16:43 +03001265 ALE_ALL_PORTS, ALE_ALL_PORTS, 0, 0);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001266
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001267 if (!cpsw_common_res_usage_state(cpsw)) {
Ivan Khoronzhuk17933312016-06-17 13:25:39 +03001268 int buf_num;
Mugunthan V Nd354eb82015-08-04 16:06:19 +05301269
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001270 /* setup tx dma to fixed prio and zero offset */
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001271 cpdma_control_set(cpsw->dma, CPDMA_TX_PRIO_FIXED, 1);
1272 cpdma_control_set(cpsw->dma, CPDMA_RX_BUFFER_OFFSET, 0);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001273
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001274 /* disable priority elevation */
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001275 __raw_writel(0, &cpsw->regs->ptype);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001276
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001277 /* enable statistics collection only on all ports */
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001278 __raw_writel(0x7, &cpsw->regs->stat_port_en);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001279
Mugunthan V N1923d6e2014-09-08 22:54:02 +05301280 /* Enable internal fifo flow control */
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001281 writel(0x7, &cpsw->regs->flow_control);
Mugunthan V N1923d6e2014-09-08 22:54:02 +05301282
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +03001283 napi_enable(&cpsw->napi_rx);
1284 napi_enable(&cpsw->napi_tx);
Mugunthan V Nd354eb82015-08-04 16:06:19 +05301285
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +03001286 if (cpsw->tx_irq_disabled) {
1287 cpsw->tx_irq_disabled = false;
1288 enable_irq(cpsw->irqs_table[1]);
Mugunthan V N7da11602015-08-12 15:22:53 +05301289 }
1290
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +03001291 if (cpsw->rx_irq_disabled) {
1292 cpsw->rx_irq_disabled = false;
1293 enable_irq(cpsw->irqs_table[0]);
Mugunthan V N7da11602015-08-12 15:22:53 +05301294 }
1295
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001296 buf_num = cpdma_chan_get_rx_buf_num(cpsw->dma);
Ivan Khoronzhuk17933312016-06-17 13:25:39 +03001297 for (i = 0; i < buf_num; i++) {
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001298 struct sk_buff *skb;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001299
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001300 ret = -ENOMEM;
Sebastian Siewioraacebbf2013-04-23 07:31:36 +00001301 skb = __netdev_alloc_skb_ip_align(priv->ndev,
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001302 cpsw->rx_packet_max, GFP_KERNEL);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001303 if (!skb)
Sebastian Siewioraacebbf2013-04-23 07:31:36 +00001304 goto err_cleanup;
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001305 ret = cpdma_chan_submit(cpsw->rxch, skb, skb->data,
1306 skb_tailroom(skb), 0);
Sebastian Siewioraacebbf2013-04-23 07:31:36 +00001307 if (ret < 0) {
1308 kfree_skb(skb);
1309 goto err_cleanup;
1310 }
Grygorii Strashko254a49d2016-08-09 15:09:44 +03001311 kmemleak_not_leak(skb);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001312 }
1313 /* continue even if we didn't manage to submit all
1314 * receive descs
1315 */
1316 cpsw_info(priv, ifup, "submitted %d rx descriptors\n", i);
Mugunthan V Nf280e892013-12-11 22:09:05 -06001317
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001318 if (cpts_register(cpsw->dev, cpsw->cpts,
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001319 cpsw->data.cpts_clock_mult,
1320 cpsw->data.cpts_clock_shift))
Mugunthan V Nf280e892013-12-11 22:09:05 -06001321 dev_err(priv->dev, "error registering cpts device\n");
1322
Mugunthan V Ndf828592012-03-18 20:17:54 +00001323 }
Mugunthan V Ndf828592012-03-18 20:17:54 +00001324
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001325 /* Enable Interrupt pacing if configured */
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001326 if (cpsw->coal_intvl != 0) {
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001327 struct ethtool_coalesce coal;
1328
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001329 coal.rx_coalesce_usecs = cpsw->coal_intvl;
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001330 cpsw_set_coalesce(ndev, &coal);
1331 }
1332
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001333 cpdma_ctlr_start(cpsw->dma);
1334 cpsw_intr_enable(cpsw);
Mugunthan V Nf63a9752014-04-10 14:23:24 +05301335
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001336 if (cpsw->data.dual_emac)
1337 cpsw->slaves[priv->emac_port].open_stat = true;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001338 return 0;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001339
Sebastian Siewioraacebbf2013-04-23 07:31:36 +00001340err_cleanup:
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001341 cpdma_ctlr_stop(cpsw->dma);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001342 for_each_slave(priv, cpsw_slave_stop, cpsw);
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001343 pm_runtime_put_sync(cpsw->dev);
Sebastian Siewioraacebbf2013-04-23 07:31:36 +00001344 netif_carrier_off(priv->ndev);
1345 return ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001346}
1347
1348static int cpsw_ndo_stop(struct net_device *ndev)
1349{
1350 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03001351 struct cpsw_common *cpsw = priv->cpsw;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001352
1353 cpsw_info(priv, ifdown, "shutting down cpsw device\n");
Mugunthan V Ndf828592012-03-18 20:17:54 +00001354 netif_stop_queue(priv->ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001355 netif_carrier_off(priv->ndev);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001356
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001357 if (cpsw_common_res_usage_state(cpsw) <= 1) {
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +03001358 napi_disable(&cpsw->napi_rx);
1359 napi_disable(&cpsw->napi_tx);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001360 cpts_unregister(cpsw->cpts);
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001361 cpsw_intr_disable(cpsw);
1362 cpdma_ctlr_stop(cpsw->dma);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001363 cpsw_ale_stop(cpsw->ale);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001364 }
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001365 for_each_slave(priv, cpsw_slave_stop, cpsw);
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001366 pm_runtime_put_sync(cpsw->dev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001367 if (cpsw->data.dual_emac)
1368 cpsw->slaves[priv->emac_port].open_stat = false;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001369 return 0;
1370}
1371
1372static netdev_tx_t cpsw_ndo_start_xmit(struct sk_buff *skb,
1373 struct net_device *ndev)
1374{
1375 struct cpsw_priv *priv = netdev_priv(ndev);
1376 int ret;
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001377 struct cpsw_common *cpsw = priv->cpsw;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001378
Florian Westphal860e9532016-05-03 16:33:13 +02001379 netif_trans_update(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001380
1381 if (skb_padto(skb, CPSW_MIN_PACKET_SIZE)) {
1382 cpsw_err(priv, tx_err, "packet pad failed\n");
Tobias Klauser8dc43dd2014-03-10 13:12:23 +01001383 ndev->stats.tx_dropped++;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001384 return NETDEV_TX_OK;
1385 }
1386
Mugunthan V N9232b162013-02-11 09:52:19 +00001387 if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001388 cpsw->cpts->tx_enable)
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001389 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
1390
1391 skb_tx_timestamp(skb);
1392
Ivan Khoronzhuk27e9e102016-08-10 02:22:32 +03001393 ret = cpsw_tx_packet_submit(priv, skb);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001394 if (unlikely(ret != 0)) {
1395 cpsw_err(priv, tx_err, "desc submit failed\n");
1396 goto fail;
1397 }
1398
Mugunthan V Nfae50822013-01-17 06:31:34 +00001399 /* If there is no more tx desc left free then we need to
1400 * tell the kernel to stop sending us tx frames.
1401 */
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001402 if (unlikely(!cpdma_check_free_tx_desc(cpsw->txch)))
Mugunthan V Nfae50822013-01-17 06:31:34 +00001403 netif_stop_queue(ndev);
1404
Mugunthan V Ndf828592012-03-18 20:17:54 +00001405 return NETDEV_TX_OK;
1406fail:
Tobias Klauser8dc43dd2014-03-10 13:12:23 +01001407 ndev->stats.tx_dropped++;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001408 netif_stop_queue(ndev);
1409 return NETDEV_TX_BUSY;
1410}
1411
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001412#ifdef CONFIG_TI_CPTS
1413
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001414static void cpsw_hwtstamp_v1(struct cpsw_common *cpsw)
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001415{
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001416 struct cpsw_slave *slave = &cpsw->slaves[cpsw->data.active_slave];
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001417 u32 ts_en, seq_id;
1418
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001419 if (!cpsw->cpts->tx_enable && !cpsw->cpts->rx_enable) {
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001420 slave_write(slave, 0, CPSW1_TS_CTL);
1421 return;
1422 }
1423
1424 seq_id = (30 << CPSW_V1_SEQ_ID_OFS_SHIFT) | ETH_P_1588;
1425 ts_en = EVENT_MSG_BITS << CPSW_V1_MSG_TYPE_OFS;
1426
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001427 if (cpsw->cpts->tx_enable)
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001428 ts_en |= CPSW_V1_TS_TX_EN;
1429
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001430 if (cpsw->cpts->rx_enable)
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001431 ts_en |= CPSW_V1_TS_RX_EN;
1432
1433 slave_write(slave, ts_en, CPSW1_TS_CTL);
1434 slave_write(slave, seq_id, CPSW1_TS_SEQ_LTYPE);
1435}
1436
1437static void cpsw_hwtstamp_v2(struct cpsw_priv *priv)
1438{
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001439 struct cpsw_slave *slave;
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001440 struct cpsw_common *cpsw = priv->cpsw;
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001441 u32 ctrl, mtype;
1442
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001443 if (cpsw->data.dual_emac)
1444 slave = &cpsw->slaves[priv->emac_port];
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001445 else
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001446 slave = &cpsw->slaves[cpsw->data.active_slave];
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001447
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001448 ctrl = slave_read(slave, CPSW2_CONTROL);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001449 switch (cpsw->version) {
George Cherian09c55372014-05-02 12:02:02 +05301450 case CPSW_VERSION_2:
1451 ctrl &= ~CTRL_V2_ALL_TS_MASK;
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001452
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001453 if (cpsw->cpts->tx_enable)
George Cherian09c55372014-05-02 12:02:02 +05301454 ctrl |= CTRL_V2_TX_TS_BITS;
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001455
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001456 if (cpsw->cpts->rx_enable)
George Cherian09c55372014-05-02 12:02:02 +05301457 ctrl |= CTRL_V2_RX_TS_BITS;
Richard Cochran26fe7eb2015-05-25 11:02:13 +02001458 break;
George Cherian09c55372014-05-02 12:02:02 +05301459 case CPSW_VERSION_3:
1460 default:
1461 ctrl &= ~CTRL_V3_ALL_TS_MASK;
1462
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001463 if (cpsw->cpts->tx_enable)
George Cherian09c55372014-05-02 12:02:02 +05301464 ctrl |= CTRL_V3_TX_TS_BITS;
1465
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001466 if (cpsw->cpts->rx_enable)
George Cherian09c55372014-05-02 12:02:02 +05301467 ctrl |= CTRL_V3_RX_TS_BITS;
Richard Cochran26fe7eb2015-05-25 11:02:13 +02001468 break;
George Cherian09c55372014-05-02 12:02:02 +05301469 }
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001470
1471 mtype = (30 << TS_SEQ_ID_OFFSET_SHIFT) | EVENT_MSG_BITS;
1472
1473 slave_write(slave, mtype, CPSW2_TS_SEQ_MTYPE);
1474 slave_write(slave, ctrl, CPSW2_CONTROL);
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001475 __raw_writel(ETH_P_1588, &cpsw->regs->ts_ltype);
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001476}
1477
Ben Hutchingsa5b41452013-11-18 23:23:40 +00001478static int cpsw_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001479{
Mugunthan V N3177bf62012-11-27 07:53:40 +00001480 struct cpsw_priv *priv = netdev_priv(dev);
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001481 struct hwtstamp_config cfg;
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001482 struct cpsw_common *cpsw = priv->cpsw;
1483 struct cpts *cpts = cpsw->cpts;
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001484
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001485 if (cpsw->version != CPSW_VERSION_1 &&
1486 cpsw->version != CPSW_VERSION_2 &&
1487 cpsw->version != CPSW_VERSION_3)
Ben Hutchings2ee91e52013-11-14 00:47:36 +00001488 return -EOPNOTSUPP;
1489
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001490 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
1491 return -EFAULT;
1492
1493 /* reserved for future extensions */
1494 if (cfg.flags)
1495 return -EINVAL;
1496
Ben Hutchings2ee91e52013-11-14 00:47:36 +00001497 if (cfg.tx_type != HWTSTAMP_TX_OFF && cfg.tx_type != HWTSTAMP_TX_ON)
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001498 return -ERANGE;
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001499
1500 switch (cfg.rx_filter) {
1501 case HWTSTAMP_FILTER_NONE:
1502 cpts->rx_enable = 0;
1503 break;
1504 case HWTSTAMP_FILTER_ALL:
1505 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
1506 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
1507 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
1508 return -ERANGE;
1509 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
1510 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
1511 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
1512 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1513 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
1514 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
1515 case HWTSTAMP_FILTER_PTP_V2_EVENT:
1516 case HWTSTAMP_FILTER_PTP_V2_SYNC:
1517 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
1518 cpts->rx_enable = 1;
1519 cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
1520 break;
1521 default:
1522 return -ERANGE;
1523 }
1524
Ben Hutchings2ee91e52013-11-14 00:47:36 +00001525 cpts->tx_enable = cfg.tx_type == HWTSTAMP_TX_ON;
1526
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001527 switch (cpsw->version) {
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001528 case CPSW_VERSION_1:
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001529 cpsw_hwtstamp_v1(cpsw);
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001530 break;
1531 case CPSW_VERSION_2:
George Cherianf7d403c2014-05-02 12:02:01 +05301532 case CPSW_VERSION_3:
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001533 cpsw_hwtstamp_v2(priv);
1534 break;
1535 default:
Ben Hutchings2ee91e52013-11-14 00:47:36 +00001536 WARN_ON(1);
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001537 }
1538
1539 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
1540}
1541
Ben Hutchingsa5b41452013-11-18 23:23:40 +00001542static int cpsw_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
1543{
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001544 struct cpsw_common *cpsw = ndev_to_cpsw(dev);
1545 struct cpts *cpts = cpsw->cpts;
Ben Hutchingsa5b41452013-11-18 23:23:40 +00001546 struct hwtstamp_config cfg;
1547
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001548 if (cpsw->version != CPSW_VERSION_1 &&
1549 cpsw->version != CPSW_VERSION_2 &&
1550 cpsw->version != CPSW_VERSION_3)
Ben Hutchingsa5b41452013-11-18 23:23:40 +00001551 return -EOPNOTSUPP;
1552
1553 cfg.flags = 0;
1554 cfg.tx_type = cpts->tx_enable ? HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF;
1555 cfg.rx_filter = (cpts->rx_enable ?
1556 HWTSTAMP_FILTER_PTP_V2_EVENT : HWTSTAMP_FILTER_NONE);
1557
1558 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
1559}
1560
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001561#endif /*CONFIG_TI_CPTS*/
1562
1563static int cpsw_ndo_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
1564{
Mugunthan V N11f2c982013-03-11 23:16:38 +00001565 struct cpsw_priv *priv = netdev_priv(dev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001566 struct cpsw_common *cpsw = priv->cpsw;
1567 int slave_no = cpsw_slave_index(cpsw, priv);
Mugunthan V N11f2c982013-03-11 23:16:38 +00001568
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001569 if (!netif_running(dev))
1570 return -EINVAL;
1571
Mugunthan V N11f2c982013-03-11 23:16:38 +00001572 switch (cmd) {
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001573#ifdef CONFIG_TI_CPTS
Mugunthan V N11f2c982013-03-11 23:16:38 +00001574 case SIOCSHWTSTAMP:
Ben Hutchingsa5b41452013-11-18 23:23:40 +00001575 return cpsw_hwtstamp_set(dev, req);
1576 case SIOCGHWTSTAMP:
1577 return cpsw_hwtstamp_get(dev, req);
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001578#endif
Mugunthan V N11f2c982013-03-11 23:16:38 +00001579 }
1580
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001581 if (!cpsw->slaves[slave_no].phy)
Stefan Sørensenc1b59942014-02-16 14:54:25 +01001582 return -EOPNOTSUPP;
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001583 return phy_mii_ioctl(cpsw->slaves[slave_no].phy, req, cmd);
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001584}
1585
Mugunthan V Ndf828592012-03-18 20:17:54 +00001586static void cpsw_ndo_tx_timeout(struct net_device *ndev)
1587{
1588 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001589 struct cpsw_common *cpsw = priv->cpsw;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001590
1591 cpsw_err(priv, tx_err, "transmit timeout, restarting dma\n");
Tobias Klauser8dc43dd2014-03-10 13:12:23 +01001592 ndev->stats.tx_errors++;
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001593 cpsw_intr_disable(cpsw);
1594 cpdma_chan_stop(cpsw->txch);
1595 cpdma_chan_start(cpsw->txch);
1596 cpsw_intr_enable(cpsw);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001597}
1598
Mugunthan V Ndcfd8d52013-07-25 23:44:01 +05301599static int cpsw_ndo_set_mac_address(struct net_device *ndev, void *p)
1600{
1601 struct cpsw_priv *priv = netdev_priv(ndev);
1602 struct sockaddr *addr = (struct sockaddr *)p;
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03001603 struct cpsw_common *cpsw = priv->cpsw;
Mugunthan V Ndcfd8d52013-07-25 23:44:01 +05301604 int flags = 0;
1605 u16 vid = 0;
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001606 int ret;
Mugunthan V Ndcfd8d52013-07-25 23:44:01 +05301607
1608 if (!is_valid_ether_addr(addr->sa_data))
1609 return -EADDRNOTAVAIL;
1610
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001611 ret = pm_runtime_get_sync(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001612 if (ret < 0) {
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001613 pm_runtime_put_noidle(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001614 return ret;
1615 }
1616
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001617 if (cpsw->data.dual_emac) {
1618 vid = cpsw->slaves[priv->emac_port].port_vlan;
Mugunthan V Ndcfd8d52013-07-25 23:44:01 +05301619 flags = ALE_VLAN;
1620 }
1621
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001622 cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr, HOST_PORT_NUM,
Mugunthan V Ndcfd8d52013-07-25 23:44:01 +05301623 flags, vid);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001624 cpsw_ale_add_ucast(cpsw->ale, addr->sa_data, HOST_PORT_NUM,
Mugunthan V Ndcfd8d52013-07-25 23:44:01 +05301625 flags, vid);
1626
1627 memcpy(priv->mac_addr, addr->sa_data, ETH_ALEN);
1628 memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
1629 for_each_slave(priv, cpsw_set_slave_mac, priv);
1630
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001631 pm_runtime_put(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001632
Mugunthan V Ndcfd8d52013-07-25 23:44:01 +05301633 return 0;
1634}
1635
Mugunthan V Ndf828592012-03-18 20:17:54 +00001636#ifdef CONFIG_NET_POLL_CONTROLLER
1637static void cpsw_ndo_poll_controller(struct net_device *ndev)
1638{
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +03001639 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001640
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +03001641 cpsw_intr_disable(cpsw);
1642 cpsw_rx_interrupt(cpsw->irqs_table[0], cpsw);
1643 cpsw_tx_interrupt(cpsw->irqs_table[1], cpsw);
1644 cpsw_intr_enable(cpsw);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001645}
1646#endif
1647
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001648static inline int cpsw_add_vlan_ale_entry(struct cpsw_priv *priv,
1649 unsigned short vid)
1650{
1651 int ret;
Mugunthan V N9f6bd8f2015-01-15 14:59:28 +05301652 int unreg_mcast_mask = 0;
1653 u32 port_mask;
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001654 struct cpsw_common *cpsw = priv->cpsw;
Lennart Sorensen1e5c4bc2014-10-31 13:38:52 -04001655
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001656 if (cpsw->data.dual_emac) {
Mugunthan V N9f6bd8f2015-01-15 14:59:28 +05301657 port_mask = (1 << (priv->emac_port + 1)) | ALE_PORT_HOST;
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001658
Mugunthan V N9f6bd8f2015-01-15 14:59:28 +05301659 if (priv->ndev->flags & IFF_ALLMULTI)
1660 unreg_mcast_mask = port_mask;
1661 } else {
1662 port_mask = ALE_ALL_PORTS;
1663
1664 if (priv->ndev->flags & IFF_ALLMULTI)
1665 unreg_mcast_mask = ALE_ALL_PORTS;
1666 else
1667 unreg_mcast_mask = ALE_PORT_1 | ALE_PORT_2;
1668 }
1669
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001670 ret = cpsw_ale_add_vlan(cpsw->ale, vid, port_mask, 0, port_mask,
Grygorii Strashko61f1cef2016-04-07 15:16:43 +03001671 unreg_mcast_mask);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001672 if (ret != 0)
1673 return ret;
1674
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001675 ret = cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr,
Grygorii Strashko71a2cbb2016-04-07 15:16:44 +03001676 HOST_PORT_NUM, ALE_VLAN, vid);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001677 if (ret != 0)
1678 goto clean_vid;
1679
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001680 ret = cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
Mugunthan V N9f6bd8f2015-01-15 14:59:28 +05301681 port_mask, ALE_VLAN, vid, 0);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001682 if (ret != 0)
1683 goto clean_vlan_ucast;
1684 return 0;
1685
1686clean_vlan_ucast:
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001687 cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr,
Grygorii Strashko71a2cbb2016-04-07 15:16:44 +03001688 HOST_PORT_NUM, ALE_VLAN, vid);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001689clean_vid:
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001690 cpsw_ale_del_vlan(cpsw->ale, vid, 0);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001691 return ret;
1692}
1693
1694static int cpsw_ndo_vlan_rx_add_vid(struct net_device *ndev,
Patrick McHardy80d5c362013-04-19 02:04:28 +00001695 __be16 proto, u16 vid)
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001696{
1697 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03001698 struct cpsw_common *cpsw = priv->cpsw;
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001699 int ret;
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001700
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001701 if (vid == cpsw->data.default_vlan)
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001702 return 0;
1703
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001704 ret = pm_runtime_get_sync(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001705 if (ret < 0) {
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001706 pm_runtime_put_noidle(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001707 return ret;
1708 }
1709
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001710 if (cpsw->data.dual_emac) {
Mugunthan V N02a54162015-01-22 15:19:22 +05301711 /* In dual EMAC, reserved VLAN id should not be used for
1712 * creating VLAN interfaces as this can break the dual
1713 * EMAC port separation
1714 */
1715 int i;
1716
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001717 for (i = 0; i < cpsw->data.slaves; i++) {
1718 if (vid == cpsw->slaves[i].port_vlan)
Mugunthan V N02a54162015-01-22 15:19:22 +05301719 return -EINVAL;
1720 }
1721 }
1722
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001723 dev_info(priv->dev, "Adding vlanid %d to vlan filter\n", vid);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001724 ret = cpsw_add_vlan_ale_entry(priv, vid);
1725
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001726 pm_runtime_put(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001727 return ret;
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001728}
1729
1730static int cpsw_ndo_vlan_rx_kill_vid(struct net_device *ndev,
Patrick McHardy80d5c362013-04-19 02:04:28 +00001731 __be16 proto, u16 vid)
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001732{
1733 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03001734 struct cpsw_common *cpsw = priv->cpsw;
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001735 int ret;
1736
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001737 if (vid == cpsw->data.default_vlan)
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001738 return 0;
1739
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001740 ret = pm_runtime_get_sync(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001741 if (ret < 0) {
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001742 pm_runtime_put_noidle(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001743 return ret;
1744 }
1745
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001746 if (cpsw->data.dual_emac) {
Mugunthan V N02a54162015-01-22 15:19:22 +05301747 int i;
1748
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001749 for (i = 0; i < cpsw->data.slaves; i++) {
1750 if (vid == cpsw->slaves[i].port_vlan)
Mugunthan V N02a54162015-01-22 15:19:22 +05301751 return -EINVAL;
1752 }
1753 }
1754
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001755 dev_info(priv->dev, "removing vlanid %d from vlan filter\n", vid);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001756 ret = cpsw_ale_del_vlan(cpsw->ale, vid, 0);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001757 if (ret != 0)
1758 return ret;
1759
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001760 ret = cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr,
Grygorii Strashko61f1cef2016-04-07 15:16:43 +03001761 HOST_PORT_NUM, ALE_VLAN, vid);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001762 if (ret != 0)
1763 return ret;
1764
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001765 ret = cpsw_ale_del_mcast(cpsw->ale, priv->ndev->broadcast,
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001766 0, ALE_VLAN, vid);
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001767 pm_runtime_put(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001768 return ret;
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001769}
1770
Mugunthan V Ndf828592012-03-18 20:17:54 +00001771static const struct net_device_ops cpsw_netdev_ops = {
1772 .ndo_open = cpsw_ndo_open,
1773 .ndo_stop = cpsw_ndo_stop,
1774 .ndo_start_xmit = cpsw_ndo_start_xmit,
Mugunthan V Ndcfd8d52013-07-25 23:44:01 +05301775 .ndo_set_mac_address = cpsw_ndo_set_mac_address,
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001776 .ndo_do_ioctl = cpsw_ndo_ioctl,
Mugunthan V Ndf828592012-03-18 20:17:54 +00001777 .ndo_validate_addr = eth_validate_addr,
David S. Miller5c473ed2012-03-20 00:33:59 -04001778 .ndo_change_mtu = eth_change_mtu,
Mugunthan V Ndf828592012-03-18 20:17:54 +00001779 .ndo_tx_timeout = cpsw_ndo_tx_timeout,
Mugunthan V N5c50a852012-10-29 08:45:11 +00001780 .ndo_set_rx_mode = cpsw_ndo_set_rx_mode,
Mugunthan V Ndf828592012-03-18 20:17:54 +00001781#ifdef CONFIG_NET_POLL_CONTROLLER
1782 .ndo_poll_controller = cpsw_ndo_poll_controller,
1783#endif
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001784 .ndo_vlan_rx_add_vid = cpsw_ndo_vlan_rx_add_vid,
1785 .ndo_vlan_rx_kill_vid = cpsw_ndo_vlan_rx_kill_vid,
Mugunthan V Ndf828592012-03-18 20:17:54 +00001786};
1787
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05301788static int cpsw_get_regs_len(struct net_device *ndev)
1789{
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001790 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05301791
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001792 return cpsw->data.ale_entries * ALE_ENTRY_WORDS * sizeof(u32);
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05301793}
1794
1795static void cpsw_get_regs(struct net_device *ndev,
1796 struct ethtool_regs *regs, void *p)
1797{
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05301798 u32 *reg = p;
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001799 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05301800
1801 /* update CPSW IP version */
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001802 regs->version = cpsw->version;
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05301803
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001804 cpsw_ale_dump(cpsw->ale, reg);
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05301805}
1806
Mugunthan V Ndf828592012-03-18 20:17:54 +00001807static void cpsw_get_drvinfo(struct net_device *ndev,
1808 struct ethtool_drvinfo *info)
1809{
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03001810 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001811 struct platform_device *pdev = to_platform_device(cpsw->dev);
Jiri Pirko7826d432013-01-06 00:44:26 +00001812
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05301813 strlcpy(info->driver, "cpsw", sizeof(info->driver));
Jiri Pirko7826d432013-01-06 00:44:26 +00001814 strlcpy(info->version, "1.0", sizeof(info->version));
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001815 strlcpy(info->bus_info, pdev->name, sizeof(info->bus_info));
Mugunthan V Ndf828592012-03-18 20:17:54 +00001816}
1817
1818static u32 cpsw_get_msglevel(struct net_device *ndev)
1819{
1820 struct cpsw_priv *priv = netdev_priv(ndev);
1821 return priv->msg_enable;
1822}
1823
1824static void cpsw_set_msglevel(struct net_device *ndev, u32 value)
1825{
1826 struct cpsw_priv *priv = netdev_priv(ndev);
1827 priv->msg_enable = value;
1828}
1829
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001830static int cpsw_get_ts_info(struct net_device *ndev,
1831 struct ethtool_ts_info *info)
1832{
1833#ifdef CONFIG_TI_CPTS
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001834 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001835
1836 info->so_timestamping =
1837 SOF_TIMESTAMPING_TX_HARDWARE |
1838 SOF_TIMESTAMPING_TX_SOFTWARE |
1839 SOF_TIMESTAMPING_RX_HARDWARE |
1840 SOF_TIMESTAMPING_RX_SOFTWARE |
1841 SOF_TIMESTAMPING_SOFTWARE |
1842 SOF_TIMESTAMPING_RAW_HARDWARE;
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001843 info->phc_index = cpsw->cpts->phc_index;
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001844 info->tx_types =
1845 (1 << HWTSTAMP_TX_OFF) |
1846 (1 << HWTSTAMP_TX_ON);
1847 info->rx_filters =
1848 (1 << HWTSTAMP_FILTER_NONE) |
1849 (1 << HWTSTAMP_FILTER_PTP_V2_EVENT);
1850#else
1851 info->so_timestamping =
1852 SOF_TIMESTAMPING_TX_SOFTWARE |
1853 SOF_TIMESTAMPING_RX_SOFTWARE |
1854 SOF_TIMESTAMPING_SOFTWARE;
1855 info->phc_index = -1;
1856 info->tx_types = 0;
1857 info->rx_filters = 0;
1858#endif
1859 return 0;
1860}
1861
Mugunthan V Nd3bb9c52013-03-11 23:16:36 +00001862static int cpsw_get_settings(struct net_device *ndev,
1863 struct ethtool_cmd *ecmd)
1864{
1865 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001866 struct cpsw_common *cpsw = priv->cpsw;
1867 int slave_no = cpsw_slave_index(cpsw, priv);
Mugunthan V Nd3bb9c52013-03-11 23:16:36 +00001868
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001869 if (cpsw->slaves[slave_no].phy)
1870 return phy_ethtool_gset(cpsw->slaves[slave_no].phy, ecmd);
Mugunthan V Nd3bb9c52013-03-11 23:16:36 +00001871 else
1872 return -EOPNOTSUPP;
1873}
1874
1875static int cpsw_set_settings(struct net_device *ndev, struct ethtool_cmd *ecmd)
1876{
1877 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001878 struct cpsw_common *cpsw = priv->cpsw;
1879 int slave_no = cpsw_slave_index(cpsw, priv);
Mugunthan V Nd3bb9c52013-03-11 23:16:36 +00001880
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001881 if (cpsw->slaves[slave_no].phy)
1882 return phy_ethtool_sset(cpsw->slaves[slave_no].phy, ecmd);
Mugunthan V Nd3bb9c52013-03-11 23:16:36 +00001883 else
1884 return -EOPNOTSUPP;
1885}
1886
Matus Ujhelyid8a64422013-08-20 07:59:38 +02001887static void cpsw_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
1888{
1889 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001890 struct cpsw_common *cpsw = priv->cpsw;
1891 int slave_no = cpsw_slave_index(cpsw, priv);
Matus Ujhelyid8a64422013-08-20 07:59:38 +02001892
1893 wol->supported = 0;
1894 wol->wolopts = 0;
1895
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001896 if (cpsw->slaves[slave_no].phy)
1897 phy_ethtool_get_wol(cpsw->slaves[slave_no].phy, wol);
Matus Ujhelyid8a64422013-08-20 07:59:38 +02001898}
1899
1900static int cpsw_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
1901{
1902 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001903 struct cpsw_common *cpsw = priv->cpsw;
1904 int slave_no = cpsw_slave_index(cpsw, priv);
Matus Ujhelyid8a64422013-08-20 07:59:38 +02001905
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001906 if (cpsw->slaves[slave_no].phy)
1907 return phy_ethtool_set_wol(cpsw->slaves[slave_no].phy, wol);
Matus Ujhelyid8a64422013-08-20 07:59:38 +02001908 else
1909 return -EOPNOTSUPP;
1910}
1911
Mugunthan V N1923d6e2014-09-08 22:54:02 +05301912static void cpsw_get_pauseparam(struct net_device *ndev,
1913 struct ethtool_pauseparam *pause)
1914{
1915 struct cpsw_priv *priv = netdev_priv(ndev);
1916
1917 pause->autoneg = AUTONEG_DISABLE;
1918 pause->rx_pause = priv->rx_pause ? true : false;
1919 pause->tx_pause = priv->tx_pause ? true : false;
1920}
1921
1922static int cpsw_set_pauseparam(struct net_device *ndev,
1923 struct ethtool_pauseparam *pause)
1924{
1925 struct cpsw_priv *priv = netdev_priv(ndev);
1926 bool link;
1927
1928 priv->rx_pause = pause->rx_pause ? true : false;
1929 priv->tx_pause = pause->tx_pause ? true : false;
1930
1931 for_each_slave(priv, _cpsw_adjust_link, priv, &link);
Mugunthan V N1923d6e2014-09-08 22:54:02 +05301932 return 0;
1933}
1934
Grygorii Strashko7898b1d2016-06-24 21:23:44 +03001935static int cpsw_ethtool_op_begin(struct net_device *ndev)
1936{
1937 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03001938 struct cpsw_common *cpsw = priv->cpsw;
Grygorii Strashko7898b1d2016-06-24 21:23:44 +03001939 int ret;
1940
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001941 ret = pm_runtime_get_sync(cpsw->dev);
Grygorii Strashko7898b1d2016-06-24 21:23:44 +03001942 if (ret < 0) {
1943 cpsw_err(priv, drv, "ethtool begin failed %d\n", ret);
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001944 pm_runtime_put_noidle(cpsw->dev);
Grygorii Strashko7898b1d2016-06-24 21:23:44 +03001945 }
1946
1947 return ret;
1948}
1949
1950static void cpsw_ethtool_op_complete(struct net_device *ndev)
1951{
1952 struct cpsw_priv *priv = netdev_priv(ndev);
1953 int ret;
1954
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001955 ret = pm_runtime_put(priv->cpsw->dev);
Grygorii Strashko7898b1d2016-06-24 21:23:44 +03001956 if (ret < 0)
1957 cpsw_err(priv, drv, "ethtool complete failed %d\n", ret);
1958}
1959
Mugunthan V Ndf828592012-03-18 20:17:54 +00001960static const struct ethtool_ops cpsw_ethtool_ops = {
1961 .get_drvinfo = cpsw_get_drvinfo,
1962 .get_msglevel = cpsw_get_msglevel,
1963 .set_msglevel = cpsw_set_msglevel,
1964 .get_link = ethtool_op_get_link,
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001965 .get_ts_info = cpsw_get_ts_info,
Mugunthan V Nd3bb9c52013-03-11 23:16:36 +00001966 .get_settings = cpsw_get_settings,
1967 .set_settings = cpsw_set_settings,
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001968 .get_coalesce = cpsw_get_coalesce,
1969 .set_coalesce = cpsw_set_coalesce,
Mugunthan V Nd9718542013-07-23 15:38:17 +05301970 .get_sset_count = cpsw_get_sset_count,
1971 .get_strings = cpsw_get_strings,
1972 .get_ethtool_stats = cpsw_get_ethtool_stats,
Mugunthan V N1923d6e2014-09-08 22:54:02 +05301973 .get_pauseparam = cpsw_get_pauseparam,
1974 .set_pauseparam = cpsw_set_pauseparam,
Matus Ujhelyid8a64422013-08-20 07:59:38 +02001975 .get_wol = cpsw_get_wol,
1976 .set_wol = cpsw_set_wol,
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05301977 .get_regs_len = cpsw_get_regs_len,
1978 .get_regs = cpsw_get_regs,
Grygorii Strashko7898b1d2016-06-24 21:23:44 +03001979 .begin = cpsw_ethtool_op_begin,
1980 .complete = cpsw_ethtool_op_complete,
Mugunthan V Ndf828592012-03-18 20:17:54 +00001981};
1982
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001983static void cpsw_slave_init(struct cpsw_slave *slave, struct cpsw_common *cpsw,
Richard Cochran549985e2012-11-14 09:07:56 +00001984 u32 slave_reg_ofs, u32 sliver_reg_ofs)
Mugunthan V Ndf828592012-03-18 20:17:54 +00001985{
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001986 void __iomem *regs = cpsw->regs;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001987 int slave_num = slave->slave_num;
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001988 struct cpsw_slave_data *data = cpsw->data.slave_data + slave_num;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001989
1990 slave->data = data;
Richard Cochran549985e2012-11-14 09:07:56 +00001991 slave->regs = regs + slave_reg_ofs;
1992 slave->sliver = regs + sliver_reg_ofs;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001993 slave->port_vlan = data->dual_emac_res_vlan;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001994}
1995
David Rivshin552165b2016-04-27 21:25:25 -04001996static int cpsw_probe_dt(struct cpsw_platform_data *data,
Mugunthan V N2eb32b02012-07-30 10:17:14 +00001997 struct platform_device *pdev)
1998{
1999 struct device_node *node = pdev->dev.of_node;
2000 struct device_node *slave_node;
2001 int i = 0, ret;
2002 u32 prop;
2003
2004 if (!node)
2005 return -EINVAL;
2006
2007 if (of_property_read_u32(node, "slaves", &prop)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302008 dev_err(&pdev->dev, "Missing slaves property in the DT.\n");
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002009 return -EINVAL;
2010 }
2011 data->slaves = prop;
2012
Mugunthan V Ne86ac132013-03-11 23:16:35 +00002013 if (of_property_read_u32(node, "active_slave", &prop)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302014 dev_err(&pdev->dev, "Missing active_slave property in the DT.\n");
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302015 return -EINVAL;
Richard Cochran78ca0b22012-10-29 08:45:18 +00002016 }
Mugunthan V Ne86ac132013-03-11 23:16:35 +00002017 data->active_slave = prop;
Richard Cochran78ca0b22012-10-29 08:45:18 +00002018
Richard Cochran00ab94e2012-10-29 08:45:19 +00002019 if (of_property_read_u32(node, "cpts_clock_mult", &prop)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302020 dev_err(&pdev->dev, "Missing cpts_clock_mult property in the DT.\n");
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302021 return -EINVAL;
Richard Cochran00ab94e2012-10-29 08:45:19 +00002022 }
2023 data->cpts_clock_mult = prop;
2024
2025 if (of_property_read_u32(node, "cpts_clock_shift", &prop)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302026 dev_err(&pdev->dev, "Missing cpts_clock_shift property in the DT.\n");
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302027 return -EINVAL;
Richard Cochran00ab94e2012-10-29 08:45:19 +00002028 }
2029 data->cpts_clock_shift = prop;
2030
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302031 data->slave_data = devm_kzalloc(&pdev->dev, data->slaves
2032 * sizeof(struct cpsw_slave_data),
2033 GFP_KERNEL);
Joe Perchesb2adaca2013-02-03 17:43:58 +00002034 if (!data->slave_data)
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302035 return -ENOMEM;
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002036
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002037 if (of_property_read_u32(node, "cpdma_channels", &prop)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302038 dev_err(&pdev->dev, "Missing cpdma_channels property in the DT.\n");
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302039 return -EINVAL;
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002040 }
2041 data->channels = prop;
2042
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002043 if (of_property_read_u32(node, "ale_entries", &prop)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302044 dev_err(&pdev->dev, "Missing ale_entries property in the DT.\n");
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302045 return -EINVAL;
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002046 }
2047 data->ale_entries = prop;
2048
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002049 if (of_property_read_u32(node, "bd_ram_size", &prop)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302050 dev_err(&pdev->dev, "Missing bd_ram_size property in the DT.\n");
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302051 return -EINVAL;
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002052 }
2053 data->bd_ram_size = prop;
2054
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002055 if (of_property_read_u32(node, "mac_control", &prop)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302056 dev_err(&pdev->dev, "Missing mac_control property in the DT.\n");
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302057 return -EINVAL;
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002058 }
2059 data->mac_control = prop;
2060
Markus Pargmann281abd92013-10-04 14:44:40 +02002061 if (of_property_read_bool(node, "dual_emac"))
2062 data->dual_emac = 1;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002063
Vaibhav Hiremath1fb19aa2012-11-14 09:07:55 +00002064 /*
2065 * Populate all the child nodes here...
2066 */
2067 ret = of_platform_populate(node, NULL, NULL, &pdev->dev);
2068 /* We do not want to force this, as in some cases may not have child */
2069 if (ret)
George Cherian88c99ff2014-05-12 10:21:19 +05302070 dev_warn(&pdev->dev, "Doesn't have any child node\n");
Vaibhav Hiremath1fb19aa2012-11-14 09:07:55 +00002071
Ben Hutchings8658aaf2016-06-21 01:16:31 +01002072 for_each_available_child_of_node(node, slave_node) {
Richard Cochran549985e2012-11-14 09:07:56 +00002073 struct cpsw_slave_data *slave_data = data->slave_data + i;
2074 const void *mac_addr = NULL;
Richard Cochran549985e2012-11-14 09:07:56 +00002075 int lenp;
2076 const __be32 *parp;
Richard Cochran549985e2012-11-14 09:07:56 +00002077
Markus Pargmannf468b102013-10-04 14:44:39 +02002078 /* This is no slave child node, continue */
2079 if (strcmp(slave_node->name, "slave"))
2080 continue;
2081
David Rivshin552165b2016-04-27 21:25:25 -04002082 slave_data->phy_node = of_parse_phandle(slave_node,
2083 "phy-handle", 0);
David Rivshinf1eea5c2015-12-16 23:02:10 -05002084 parp = of_get_property(slave_node, "phy_id", &lenp);
David Rivshinae092b52016-04-27 21:38:26 -04002085 if (slave_data->phy_node) {
2086 dev_dbg(&pdev->dev,
2087 "slave[%d] using phy-handle=\"%s\"\n",
2088 i, slave_data->phy_node->full_name);
2089 } else if (of_phy_is_fixed_link(slave_node)) {
David Rivshindfc0a6d2015-12-16 23:02:11 -05002090 /* In the case of a fixed PHY, the DT node associated
2091 * to the PHY is the Ethernet MAC DT node.
2092 */
Markus Brunner1f71e8c2015-11-03 22:09:51 +01002093 ret = of_phy_register_fixed_link(slave_node);
2094 if (ret)
2095 return ret;
David Rivshin06cd6d62016-04-27 21:45:45 -04002096 slave_data->phy_node = of_node_get(slave_node);
David Rivshinf1eea5c2015-12-16 23:02:10 -05002097 } else if (parp) {
2098 u32 phyid;
2099 struct device_node *mdio_node;
2100 struct platform_device *mdio;
2101
2102 if (lenp != (sizeof(__be32) * 2)) {
2103 dev_err(&pdev->dev, "Invalid slave[%d] phy_id property\n", i);
2104 goto no_phy_slave;
2105 }
2106 mdio_node = of_find_node_by_phandle(be32_to_cpup(parp));
2107 phyid = be32_to_cpup(parp+1);
2108 mdio = of_find_device_by_node(mdio_node);
2109 of_node_put(mdio_node);
2110 if (!mdio) {
2111 dev_err(&pdev->dev, "Missing mdio platform device\n");
2112 return -EINVAL;
2113 }
2114 snprintf(slave_data->phy_id, sizeof(slave_data->phy_id),
2115 PHY_ID_FMT, mdio->name, phyid);
2116 } else {
David Rivshinae092b52016-04-27 21:38:26 -04002117 dev_err(&pdev->dev,
2118 "No slave[%d] phy_id, phy-handle, or fixed-link property\n",
2119 i);
Markus Brunner1f71e8c2015-11-03 22:09:51 +01002120 goto no_phy_slave;
2121 }
Mugunthan V N47276fc2014-10-24 18:51:33 +05302122 slave_data->phy_if = of_get_phy_mode(slave_node);
2123 if (slave_data->phy_if < 0) {
2124 dev_err(&pdev->dev, "Missing or malformed slave[%d] phy-mode property\n",
2125 i);
2126 return slave_data->phy_if;
2127 }
2128
2129no_phy_slave:
Richard Cochran549985e2012-11-14 09:07:56 +00002130 mac_addr = of_get_mac_address(slave_node);
Markus Pargmann0ba517b2014-09-29 08:53:17 +02002131 if (mac_addr) {
Richard Cochran549985e2012-11-14 09:07:56 +00002132 memcpy(slave_data->mac_addr, mac_addr, ETH_ALEN);
Markus Pargmann0ba517b2014-09-29 08:53:17 +02002133 } else {
Mugunthan V Nb6745f62015-09-21 15:56:50 +05302134 ret = ti_cm_get_macid(&pdev->dev, i,
2135 slave_data->mac_addr);
2136 if (ret)
2137 return ret;
Markus Pargmann0ba517b2014-09-29 08:53:17 +02002138 }
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002139 if (data->dual_emac) {
Mugunthan V N91c41662013-04-15 07:31:28 +00002140 if (of_property_read_u32(slave_node, "dual_emac_res_vlan",
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002141 &prop)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302142 dev_err(&pdev->dev, "Missing dual_emac_res_vlan in DT.\n");
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002143 slave_data->dual_emac_res_vlan = i+1;
George Cherian88c99ff2014-05-12 10:21:19 +05302144 dev_err(&pdev->dev, "Using %d as Reserved VLAN for %d slave\n",
2145 slave_data->dual_emac_res_vlan, i);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002146 } else {
2147 slave_data->dual_emac_res_vlan = prop;
2148 }
2149 }
2150
Richard Cochran549985e2012-11-14 09:07:56 +00002151 i++;
Mugunthan V N3a27bfa2013-12-02 12:53:39 +05302152 if (i == data->slaves)
2153 break;
Richard Cochran549985e2012-11-14 09:07:56 +00002154 }
2155
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002156 return 0;
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002157}
2158
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002159static int cpsw_probe_dual_emac(struct cpsw_priv *priv)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002160{
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002161 struct cpsw_common *cpsw = priv->cpsw;
2162 struct cpsw_platform_data *data = &cpsw->data;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002163 struct net_device *ndev;
2164 struct cpsw_priv *priv_sl2;
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +03002165 int ret = 0;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002166
2167 ndev = alloc_etherdev(sizeof(struct cpsw_priv));
2168 if (!ndev) {
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002169 dev_err(cpsw->dev, "cpsw: error allocating net_device\n");
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002170 return -ENOMEM;
2171 }
2172
2173 priv_sl2 = netdev_priv(ndev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002174 priv_sl2->cpsw = cpsw;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002175 priv_sl2->ndev = ndev;
2176 priv_sl2->dev = &ndev->dev;
2177 priv_sl2->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002178
2179 if (is_valid_ether_addr(data->slave_data[1].mac_addr)) {
2180 memcpy(priv_sl2->mac_addr, data->slave_data[1].mac_addr,
2181 ETH_ALEN);
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002182 dev_info(cpsw->dev, "cpsw: Detected MACID = %pM\n",
2183 priv_sl2->mac_addr);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002184 } else {
2185 random_ether_addr(priv_sl2->mac_addr);
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002186 dev_info(cpsw->dev, "cpsw: Random MACID = %pM\n",
2187 priv_sl2->mac_addr);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002188 }
2189 memcpy(ndev->dev_addr, priv_sl2->mac_addr, ETH_ALEN);
2190
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002191 priv_sl2->emac_port = 1;
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002192 cpsw->slaves[1].ndev = ndev;
Patrick McHardyf6469682013-04-19 02:04:27 +00002193 ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002194
2195 ndev->netdev_ops = &cpsw_netdev_ops;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00002196 ndev->ethtool_ops = &cpsw_ethtool_ops;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002197
2198 /* register the network device */
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002199 SET_NETDEV_DEV(ndev, cpsw->dev);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002200 ret = register_netdev(ndev);
2201 if (ret) {
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002202 dev_err(cpsw->dev, "cpsw: error registering net device\n");
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002203 free_netdev(ndev);
2204 ret = -ENODEV;
2205 }
2206
2207 return ret;
2208}
2209
Mugunthan V N7da11602015-08-12 15:22:53 +05302210#define CPSW_QUIRK_IRQ BIT(0)
2211
2212static struct platform_device_id cpsw_devtype[] = {
2213 {
2214 /* keep it for existing comaptibles */
2215 .name = "cpsw",
2216 .driver_data = CPSW_QUIRK_IRQ,
2217 }, {
2218 .name = "am335x-cpsw",
2219 .driver_data = CPSW_QUIRK_IRQ,
2220 }, {
2221 .name = "am4372-cpsw",
2222 .driver_data = 0,
2223 }, {
2224 .name = "dra7-cpsw",
2225 .driver_data = 0,
2226 }, {
2227 /* sentinel */
2228 }
2229};
2230MODULE_DEVICE_TABLE(platform, cpsw_devtype);
2231
2232enum ti_cpsw_type {
2233 CPSW = 0,
2234 AM335X_CPSW,
2235 AM4372_CPSW,
2236 DRA7_CPSW,
2237};
2238
2239static const struct of_device_id cpsw_of_mtable[] = {
2240 { .compatible = "ti,cpsw", .data = &cpsw_devtype[CPSW], },
2241 { .compatible = "ti,am335x-cpsw", .data = &cpsw_devtype[AM335X_CPSW], },
2242 { .compatible = "ti,am4372-cpsw", .data = &cpsw_devtype[AM4372_CPSW], },
2243 { .compatible = "ti,dra7-cpsw", .data = &cpsw_devtype[DRA7_CPSW], },
2244 { /* sentinel */ },
2245};
2246MODULE_DEVICE_TABLE(of, cpsw_of_mtable);
2247
Bill Pemberton663e12e2012-12-03 09:23:45 -05002248static int cpsw_probe(struct platform_device *pdev)
Mugunthan V Ndf828592012-03-18 20:17:54 +00002249{
Ivan Khoronzhukef4183a2016-08-10 02:22:35 +03002250 struct clk *clk;
Sebastian Siewiord1bd9ac2013-04-24 08:48:23 +00002251 struct cpsw_platform_data *data;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002252 struct net_device *ndev;
2253 struct cpsw_priv *priv;
2254 struct cpdma_params dma_params;
2255 struct cpsw_ale_params ale_params;
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302256 void __iomem *ss_regs;
2257 struct resource *res, *ss_res;
Mugunthan V N7da11602015-08-12 15:22:53 +05302258 const struct of_device_id *of_id;
Mugunthan V N1d147cc2015-09-07 15:16:44 +05302259 struct gpio_descs *mode;
Richard Cochran549985e2012-11-14 09:07:56 +00002260 u32 slave_offset, sliver_offset, slave_size;
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03002261 struct cpsw_common *cpsw;
Felipe Balbi5087b912015-01-16 10:11:11 -06002262 int ret = 0, i;
2263 int irq;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002264
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03002265 cpsw = devm_kzalloc(&pdev->dev, sizeof(struct cpsw_common), GFP_KERNEL);
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002266 cpsw->dev = &pdev->dev;
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03002267
Mugunthan V Ndf828592012-03-18 20:17:54 +00002268 ndev = alloc_etherdev(sizeof(struct cpsw_priv));
2269 if (!ndev) {
George Cherian88c99ff2014-05-12 10:21:19 +05302270 dev_err(&pdev->dev, "error allocating net_device\n");
Mugunthan V Ndf828592012-03-18 20:17:54 +00002271 return -ENOMEM;
2272 }
2273
2274 platform_set_drvdata(pdev, ndev);
2275 priv = netdev_priv(ndev);
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03002276 priv->cpsw = cpsw;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002277 priv->ndev = ndev;
2278 priv->dev = &ndev->dev;
2279 priv->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002280 cpsw->rx_packet_max = max(rx_packet_max, 128);
2281 cpsw->cpts = devm_kzalloc(&pdev->dev, sizeof(struct cpts), GFP_KERNEL);
2282 if (!cpsw->cpts) {
George Cherian88c99ff2014-05-12 10:21:19 +05302283 dev_err(&pdev->dev, "error allocating cpts\n");
Markus Pargmann4d507df2014-09-29 08:53:14 +02002284 ret = -ENOMEM;
Mugunthan V N9232b162013-02-11 09:52:19 +00002285 goto clean_ndev_ret;
2286 }
Mugunthan V Ndf828592012-03-18 20:17:54 +00002287
Mugunthan V N1d147cc2015-09-07 15:16:44 +05302288 mode = devm_gpiod_get_array_optional(&pdev->dev, "mode", GPIOD_OUT_LOW);
2289 if (IS_ERR(mode)) {
2290 ret = PTR_ERR(mode);
2291 dev_err(&pdev->dev, "gpio request failed, ret %d\n", ret);
2292 goto clean_ndev_ret;
2293 }
2294
Vaibhav Hiremath1fb19aa2012-11-14 09:07:55 +00002295 /*
2296 * This may be required here for child devices.
2297 */
2298 pm_runtime_enable(&pdev->dev);
2299
Mugunthan V N739683b2013-06-06 23:45:14 +05302300 /* Select default pin state */
2301 pinctrl_pm_select_default_state(&pdev->dev);
2302
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002303 if (cpsw_probe_dt(&cpsw->data, pdev)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302304 dev_err(&pdev->dev, "cpsw: platform data missing\n");
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002305 ret = -ENODEV;
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302306 goto clean_runtime_disable_ret;
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002307 }
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002308 data = &cpsw->data;
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002309
Mugunthan V Ndf828592012-03-18 20:17:54 +00002310 if (is_valid_ether_addr(data->slave_data[0].mac_addr)) {
2311 memcpy(priv->mac_addr, data->slave_data[0].mac_addr, ETH_ALEN);
George Cherian88c99ff2014-05-12 10:21:19 +05302312 dev_info(&pdev->dev, "Detected MACID = %pM\n", priv->mac_addr);
Mugunthan V Ndf828592012-03-18 20:17:54 +00002313 } else {
Joe Perches7efd26d2012-07-12 19:33:06 +00002314 eth_random_addr(priv->mac_addr);
George Cherian88c99ff2014-05-12 10:21:19 +05302315 dev_info(&pdev->dev, "Random MACID = %pM\n", priv->mac_addr);
Mugunthan V Ndf828592012-03-18 20:17:54 +00002316 }
2317
2318 memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
2319
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002320 cpsw->slaves = devm_kzalloc(&pdev->dev,
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302321 sizeof(struct cpsw_slave) * data->slaves,
2322 GFP_KERNEL);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002323 if (!cpsw->slaves) {
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302324 ret = -ENOMEM;
2325 goto clean_runtime_disable_ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002326 }
2327 for (i = 0; i < data->slaves; i++)
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002328 cpsw->slaves[i].slave_num = i;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002329
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002330 cpsw->slaves[0].ndev = ndev;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002331 priv->emac_port = 0;
2332
Ivan Khoronzhukef4183a2016-08-10 02:22:35 +03002333 clk = devm_clk_get(&pdev->dev, "fck");
2334 if (IS_ERR(clk)) {
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302335 dev_err(priv->dev, "fck is not found\n");
Mugunthan V Nf150bd72012-07-17 08:09:50 +00002336 ret = -ENODEV;
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302337 goto clean_runtime_disable_ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002338 }
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002339 cpsw->bus_freq_mhz = clk_get_rate(clk) / 1000000;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002340
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302341 ss_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2342 ss_regs = devm_ioremap_resource(&pdev->dev, ss_res);
2343 if (IS_ERR(ss_regs)) {
2344 ret = PTR_ERR(ss_regs);
2345 goto clean_runtime_disable_ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002346 }
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03002347 cpsw->regs = ss_regs;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002348
Mugunthan V Nf280e892013-12-11 22:09:05 -06002349 /* Need to enable clocks with runtime PM api to access module
2350 * registers
2351 */
Grygorii Strashko108a6532016-06-24 21:23:42 +03002352 ret = pm_runtime_get_sync(&pdev->dev);
2353 if (ret < 0) {
2354 pm_runtime_put_noidle(&pdev->dev);
2355 goto clean_runtime_disable_ret;
2356 }
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002357 cpsw->version = readl(&cpsw->regs->id_ver);
Mugunthan V Nf280e892013-12-11 22:09:05 -06002358 pm_runtime_put_sync(&pdev->dev);
2359
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302360 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03002361 cpsw->wr_regs = devm_ioremap_resource(&pdev->dev, res);
2362 if (IS_ERR(cpsw->wr_regs)) {
2363 ret = PTR_ERR(cpsw->wr_regs);
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302364 goto clean_runtime_disable_ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002365 }
Mugunthan V Ndf828592012-03-18 20:17:54 +00002366
2367 memset(&dma_params, 0, sizeof(dma_params));
Richard Cochran549985e2012-11-14 09:07:56 +00002368 memset(&ale_params, 0, sizeof(ale_params));
2369
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002370 switch (cpsw->version) {
Richard Cochran549985e2012-11-14 09:07:56 +00002371 case CPSW_VERSION_1:
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03002372 cpsw->host_port_regs = ss_regs + CPSW1_HOST_PORT_OFFSET;
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002373 cpsw->cpts->reg = ss_regs + CPSW1_CPTS_OFFSET;
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03002374 cpsw->hw_stats = ss_regs + CPSW1_HW_STATS;
Richard Cochran549985e2012-11-14 09:07:56 +00002375 dma_params.dmaregs = ss_regs + CPSW1_CPDMA_OFFSET;
2376 dma_params.txhdp = ss_regs + CPSW1_STATERAM_OFFSET;
2377 ale_params.ale_regs = ss_regs + CPSW1_ALE_OFFSET;
2378 slave_offset = CPSW1_SLAVE_OFFSET;
2379 slave_size = CPSW1_SLAVE_SIZE;
2380 sliver_offset = CPSW1_SLIVER_OFFSET;
2381 dma_params.desc_mem_phys = 0;
2382 break;
2383 case CPSW_VERSION_2:
Mugunthan V Nc193f362013-08-05 17:30:05 +05302384 case CPSW_VERSION_3:
Mugunthan V N926489b2013-08-12 17:11:15 +05302385 case CPSW_VERSION_4:
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03002386 cpsw->host_port_regs = ss_regs + CPSW2_HOST_PORT_OFFSET;
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002387 cpsw->cpts->reg = ss_regs + CPSW2_CPTS_OFFSET;
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03002388 cpsw->hw_stats = ss_regs + CPSW2_HW_STATS;
Richard Cochran549985e2012-11-14 09:07:56 +00002389 dma_params.dmaregs = ss_regs + CPSW2_CPDMA_OFFSET;
2390 dma_params.txhdp = ss_regs + CPSW2_STATERAM_OFFSET;
2391 ale_params.ale_regs = ss_regs + CPSW2_ALE_OFFSET;
2392 slave_offset = CPSW2_SLAVE_OFFSET;
2393 slave_size = CPSW2_SLAVE_SIZE;
2394 sliver_offset = CPSW2_SLIVER_OFFSET;
2395 dma_params.desc_mem_phys =
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302396 (u32 __force) ss_res->start + CPSW2_BD_OFFSET;
Richard Cochran549985e2012-11-14 09:07:56 +00002397 break;
2398 default:
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002399 dev_err(priv->dev, "unknown version 0x%08x\n", cpsw->version);
Richard Cochran549985e2012-11-14 09:07:56 +00002400 ret = -ENODEV;
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302401 goto clean_runtime_disable_ret;
Richard Cochran549985e2012-11-14 09:07:56 +00002402 }
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002403 for (i = 0; i < cpsw->data.slaves; i++) {
2404 struct cpsw_slave *slave = &cpsw->slaves[i];
2405
2406 cpsw_slave_init(slave, cpsw, slave_offset, sliver_offset);
Richard Cochran549985e2012-11-14 09:07:56 +00002407 slave_offset += slave_size;
2408 sliver_offset += SLIVER_SIZE;
2409 }
2410
Mugunthan V Ndf828592012-03-18 20:17:54 +00002411 dma_params.dev = &pdev->dev;
Richard Cochran549985e2012-11-14 09:07:56 +00002412 dma_params.rxthresh = dma_params.dmaregs + CPDMA_RXTHRESH;
2413 dma_params.rxfree = dma_params.dmaregs + CPDMA_RXFREE;
2414 dma_params.rxhdp = dma_params.txhdp + CPDMA_RXHDP;
2415 dma_params.txcp = dma_params.txhdp + CPDMA_TXCP;
2416 dma_params.rxcp = dma_params.txhdp + CPDMA_RXCP;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002417
2418 dma_params.num_chan = data->channels;
2419 dma_params.has_soft_reset = true;
2420 dma_params.min_packet_size = CPSW_MIN_PACKET_SIZE;
2421 dma_params.desc_mem_size = data->bd_ram_size;
2422 dma_params.desc_align = 16;
2423 dma_params.has_ext_regs = true;
Richard Cochran549985e2012-11-14 09:07:56 +00002424 dma_params.desc_hw_addr = dma_params.desc_mem_phys;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002425
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03002426 cpsw->dma = cpdma_ctlr_create(&dma_params);
2427 if (!cpsw->dma) {
Mugunthan V Ndf828592012-03-18 20:17:54 +00002428 dev_err(priv->dev, "error initializing dma\n");
2429 ret = -ENOMEM;
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302430 goto clean_runtime_disable_ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002431 }
2432
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03002433 cpsw->txch = cpdma_chan_create(cpsw->dma, tx_chan_num(0),
Mugunthan V Ndf828592012-03-18 20:17:54 +00002434 cpsw_tx_handler);
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03002435 cpsw->rxch = cpdma_chan_create(cpsw->dma, rx_chan_num(0),
Mugunthan V Ndf828592012-03-18 20:17:54 +00002436 cpsw_rx_handler);
2437
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03002438 if (WARN_ON(!cpsw->txch || !cpsw->rxch)) {
Mugunthan V Ndf828592012-03-18 20:17:54 +00002439 dev_err(priv->dev, "error initializing dma channels\n");
2440 ret = -ENOMEM;
2441 goto clean_dma_ret;
2442 }
2443
Mugunthan V Ndf828592012-03-18 20:17:54 +00002444 ale_params.dev = &ndev->dev;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002445 ale_params.ale_ageout = ale_ageout;
2446 ale_params.ale_entries = data->ale_entries;
2447 ale_params.ale_ports = data->slaves;
2448
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002449 cpsw->ale = cpsw_ale_create(&ale_params);
2450 if (!cpsw->ale) {
Mugunthan V Ndf828592012-03-18 20:17:54 +00002451 dev_err(priv->dev, "error initializing ale engine\n");
2452 ret = -ENODEV;
2453 goto clean_dma_ret;
2454 }
2455
Felipe Balbic03abd82015-01-16 10:11:12 -06002456 ndev->irq = platform_get_irq(pdev, 1);
Mugunthan V Ndf828592012-03-18 20:17:54 +00002457 if (ndev->irq < 0) {
2458 dev_err(priv->dev, "error getting irq resource\n");
Julia Lawallc1e33342015-12-26 20:12:13 +01002459 ret = ndev->irq;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002460 goto clean_ale_ret;
2461 }
2462
Mugunthan V N7da11602015-08-12 15:22:53 +05302463 of_id = of_match_device(cpsw_of_mtable, &pdev->dev);
2464 if (of_id) {
2465 pdev->id_entry = of_id->data;
2466 if (pdev->id_entry->driver_data)
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +03002467 cpsw->quirk_irq = true;
Mugunthan V N7da11602015-08-12 15:22:53 +05302468 }
2469
Felipe Balbic03abd82015-01-16 10:11:12 -06002470 /* Grab RX and TX IRQs. Note that we also have RX_THRESHOLD and
2471 * MISC IRQs which are always kept disabled with this driver so
2472 * we will not request them.
2473 *
2474 * If anyone wants to implement support for those, make sure to
2475 * first request and append them to irqs_table array.
2476 */
Daniel Mackc2b32e52014-09-04 09:00:23 +02002477
Felipe Balbic03abd82015-01-16 10:11:12 -06002478 /* RX IRQ */
Felipe Balbi5087b912015-01-16 10:11:11 -06002479 irq = platform_get_irq(pdev, 1);
Julia Lawallc1e33342015-12-26 20:12:13 +01002480 if (irq < 0) {
2481 ret = irq;
Felipe Balbi5087b912015-01-16 10:11:11 -06002482 goto clean_ale_ret;
Julia Lawallc1e33342015-12-26 20:12:13 +01002483 }
Felipe Balbi5087b912015-01-16 10:11:11 -06002484
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +03002485 cpsw->irqs_table[0] = irq;
Felipe Balbic03abd82015-01-16 10:11:12 -06002486 ret = devm_request_irq(&pdev->dev, irq, cpsw_rx_interrupt,
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +03002487 0, dev_name(&pdev->dev), cpsw);
Felipe Balbi5087b912015-01-16 10:11:11 -06002488 if (ret < 0) {
2489 dev_err(priv->dev, "error attaching irq (%d)\n", ret);
2490 goto clean_ale_ret;
2491 }
2492
Felipe Balbic03abd82015-01-16 10:11:12 -06002493 /* TX IRQ */
Felipe Balbi5087b912015-01-16 10:11:11 -06002494 irq = platform_get_irq(pdev, 2);
Julia Lawallc1e33342015-12-26 20:12:13 +01002495 if (irq < 0) {
2496 ret = irq;
Felipe Balbi5087b912015-01-16 10:11:11 -06002497 goto clean_ale_ret;
Julia Lawallc1e33342015-12-26 20:12:13 +01002498 }
Felipe Balbi5087b912015-01-16 10:11:11 -06002499
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +03002500 cpsw->irqs_table[1] = irq;
Felipe Balbic03abd82015-01-16 10:11:12 -06002501 ret = devm_request_irq(&pdev->dev, irq, cpsw_tx_interrupt,
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +03002502 0, dev_name(&pdev->dev), cpsw);
Felipe Balbi5087b912015-01-16 10:11:11 -06002503 if (ret < 0) {
2504 dev_err(priv->dev, "error attaching irq (%d)\n", ret);
2505 goto clean_ale_ret;
2506 }
Daniel Mackc2b32e52014-09-04 09:00:23 +02002507
Patrick McHardyf6469682013-04-19 02:04:27 +00002508 ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002509
2510 ndev->netdev_ops = &cpsw_netdev_ops;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00002511 ndev->ethtool_ops = &cpsw_ethtool_ops;
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +03002512 netif_napi_add(ndev, &cpsw->napi_rx, cpsw_rx_poll, CPSW_POLL_WEIGHT);
2513 netif_tx_napi_add(ndev, &cpsw->napi_tx, cpsw_tx_poll, CPSW_POLL_WEIGHT);
Mugunthan V Ndf828592012-03-18 20:17:54 +00002514
2515 /* register the network device */
2516 SET_NETDEV_DEV(ndev, &pdev->dev);
2517 ret = register_netdev(ndev);
2518 if (ret) {
2519 dev_err(priv->dev, "error registering net device\n");
2520 ret = -ENODEV;
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302521 goto clean_ale_ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002522 }
2523
Olof Johansson1a3b5052013-12-11 15:58:07 -08002524 cpsw_notice(priv, probe, "initialized device (regs %pa, irq %d)\n",
2525 &ss_res->start, ndev->irq);
Mugunthan V Ndf828592012-03-18 20:17:54 +00002526
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002527 if (cpsw->data.dual_emac) {
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002528 ret = cpsw_probe_dual_emac(priv);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002529 if (ret) {
2530 cpsw_err(priv, probe, "error probe slave 2 emac interface\n");
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302531 goto clean_ale_ret;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002532 }
2533 }
2534
Mugunthan V Ndf828592012-03-18 20:17:54 +00002535 return 0;
2536
Mugunthan V Ndf828592012-03-18 20:17:54 +00002537clean_ale_ret:
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002538 cpsw_ale_destroy(cpsw->ale);
Mugunthan V Ndf828592012-03-18 20:17:54 +00002539clean_dma_ret:
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03002540 cpdma_ctlr_destroy(cpsw->dma);
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302541clean_runtime_disable_ret:
Mugunthan V Nf150bd72012-07-17 08:09:50 +00002542 pm_runtime_disable(&pdev->dev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00002543clean_ndev_ret:
Sebastian Siewiord1bd9ac2013-04-24 08:48:23 +00002544 free_netdev(priv->ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00002545 return ret;
2546}
2547
Bill Pemberton663e12e2012-12-03 09:23:45 -05002548static int cpsw_remove(struct platform_device *pdev)
Mugunthan V Ndf828592012-03-18 20:17:54 +00002549{
2550 struct net_device *ndev = platform_get_drvdata(pdev);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002551 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Grygorii Strashko8a0b6dc2016-07-28 20:50:35 +03002552 int ret;
2553
2554 ret = pm_runtime_get_sync(&pdev->dev);
2555 if (ret < 0) {
2556 pm_runtime_put_noidle(&pdev->dev);
2557 return ret;
2558 }
Mugunthan V Ndf828592012-03-18 20:17:54 +00002559
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002560 if (cpsw->data.dual_emac)
2561 unregister_netdev(cpsw->slaves[1].ndev);
Sebastian Siewiord1bd9ac2013-04-24 08:48:23 +00002562 unregister_netdev(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00002563
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002564 cpsw_ale_destroy(cpsw->ale);
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03002565 cpdma_ctlr_destroy(cpsw->dma);
Grygorii Strashko3bf2cb32016-07-28 20:50:36 +03002566 of_platform_depopulate(&pdev->dev);
Grygorii Strashko8a0b6dc2016-07-28 20:50:35 +03002567 pm_runtime_put_sync(&pdev->dev);
2568 pm_runtime_disable(&pdev->dev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002569 if (cpsw->data.dual_emac)
2570 free_netdev(cpsw->slaves[1].ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00002571 free_netdev(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00002572 return 0;
2573}
2574
Grygorii Strashko8963a502015-02-27 13:19:45 +02002575#ifdef CONFIG_PM_SLEEP
Mugunthan V Ndf828592012-03-18 20:17:54 +00002576static int cpsw_suspend(struct device *dev)
2577{
2578 struct platform_device *pdev = to_platform_device(dev);
2579 struct net_device *ndev = platform_get_drvdata(pdev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002580 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00002581
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002582 if (cpsw->data.dual_emac) {
Mugunthan V N618073e2014-09-11 22:52:38 +05302583 int i;
Daniel Mack1e7a2e22013-11-15 08:29:16 +01002584
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002585 for (i = 0; i < cpsw->data.slaves; i++) {
2586 if (netif_running(cpsw->slaves[i].ndev))
2587 cpsw_ndo_stop(cpsw->slaves[i].ndev);
Mugunthan V N618073e2014-09-11 22:52:38 +05302588 }
2589 } else {
2590 if (netif_running(ndev))
2591 cpsw_ndo_stop(ndev);
Mugunthan V N618073e2014-09-11 22:52:38 +05302592 }
Daniel Mack1e7a2e22013-11-15 08:29:16 +01002593
Mugunthan V N739683b2013-06-06 23:45:14 +05302594 /* Select sleep pin state */
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002595 pinctrl_pm_select_sleep_state(dev);
Mugunthan V N739683b2013-06-06 23:45:14 +05302596
Mugunthan V Ndf828592012-03-18 20:17:54 +00002597 return 0;
2598}
2599
2600static int cpsw_resume(struct device *dev)
2601{
2602 struct platform_device *pdev = to_platform_device(dev);
2603 struct net_device *ndev = platform_get_drvdata(pdev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002604 struct cpsw_common *cpsw = netdev_priv(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00002605
Mugunthan V N739683b2013-06-06 23:45:14 +05302606 /* Select default pin state */
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002607 pinctrl_pm_select_default_state(dev);
Mugunthan V N739683b2013-06-06 23:45:14 +05302608
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002609 if (cpsw->data.dual_emac) {
Mugunthan V N618073e2014-09-11 22:52:38 +05302610 int i;
2611
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002612 for (i = 0; i < cpsw->data.slaves; i++) {
2613 if (netif_running(cpsw->slaves[i].ndev))
2614 cpsw_ndo_open(cpsw->slaves[i].ndev);
Mugunthan V N618073e2014-09-11 22:52:38 +05302615 }
2616 } else {
2617 if (netif_running(ndev))
2618 cpsw_ndo_open(ndev);
2619 }
Mugunthan V Ndf828592012-03-18 20:17:54 +00002620 return 0;
2621}
Grygorii Strashko8963a502015-02-27 13:19:45 +02002622#endif
Mugunthan V Ndf828592012-03-18 20:17:54 +00002623
Grygorii Strashko8963a502015-02-27 13:19:45 +02002624static SIMPLE_DEV_PM_OPS(cpsw_pm_ops, cpsw_suspend, cpsw_resume);
Mugunthan V Ndf828592012-03-18 20:17:54 +00002625
2626static struct platform_driver cpsw_driver = {
2627 .driver = {
2628 .name = "cpsw",
Mugunthan V Ndf828592012-03-18 20:17:54 +00002629 .pm = &cpsw_pm_ops,
Sachin Kamat1e5c76d2013-09-30 09:55:12 +05302630 .of_match_table = cpsw_of_mtable,
Mugunthan V Ndf828592012-03-18 20:17:54 +00002631 },
2632 .probe = cpsw_probe,
Bill Pemberton663e12e2012-12-03 09:23:45 -05002633 .remove = cpsw_remove,
Mugunthan V Ndf828592012-03-18 20:17:54 +00002634};
2635
Grygorii Strashko6fb3b6b52015-10-23 14:41:12 +03002636module_platform_driver(cpsw_driver);
Mugunthan V Ndf828592012-03-18 20:17:54 +00002637
2638MODULE_LICENSE("GPL");
2639MODULE_AUTHOR("Cyril Chemparathy <cyril@ti.com>");
2640MODULE_AUTHOR("Mugunthan V N <mugunthanvnm@ti.com>");
2641MODULE_DESCRIPTION("TI CPSW Ethernet driver");