blob: b4d3b410c32adcba1b02708aa69a4d519c824fdc [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++;
Sebastian Siewiorb4727e62013-04-23 07:31:39 +0000733 } else {
Tobias Klauser8dc43dd2014-03-10 13:12:23 +0100734 ndev->stats.rx_dropped++;
Sebastian Siewiorb4727e62013-04-23 07:31:39 +0000735 new_skb = skb;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000736 }
737
Mugunthan V Na0e2c822014-09-10 16:38:09 +0530738requeue:
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +0300739 ret = cpdma_chan_submit(cpsw->rxch, new_skb, new_skb->data,
740 skb_tailroom(new_skb), 0);
Sebastian Siewiorb4727e62013-04-23 07:31:39 +0000741 if (WARN_ON(ret < 0))
742 dev_kfree_skb_any(new_skb);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000743}
744
Felipe Balbic03abd82015-01-16 10:11:12 -0600745static irqreturn_t cpsw_tx_interrupt(int irq, void *dev_id)
Mugunthan V Ndf828592012-03-18 20:17:54 +0000746{
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +0300747 struct cpsw_common *cpsw = dev_id;
Felipe Balbi7ce67a32015-01-02 16:15:59 -0600748
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +0300749 writel(0, &cpsw->wr_regs->tx_en);
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +0300750 cpdma_ctlr_eoi(cpsw->dma, CPDMA_EOI_TX);
Felipe Balbic03abd82015-01-16 10:11:12 -0600751
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +0300752 if (cpsw->quirk_irq) {
753 disable_irq_nosync(cpsw->irqs_table[1]);
754 cpsw->tx_irq_disabled = true;
Mugunthan V N7da11602015-08-12 15:22:53 +0530755 }
756
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +0300757 napi_schedule(&cpsw->napi_tx);
Felipe Balbic03abd82015-01-16 10:11:12 -0600758 return IRQ_HANDLED;
759}
760
761static irqreturn_t cpsw_rx_interrupt(int irq, void *dev_id)
762{
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +0300763 struct cpsw_common *cpsw = dev_id;
Felipe Balbic03abd82015-01-16 10:11:12 -0600764
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +0300765 cpdma_ctlr_eoi(cpsw->dma, CPDMA_EOI_RX);
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +0300766 writel(0, &cpsw->wr_regs->rx_en);
Sebastian Siewiorfd51cf12013-04-23 07:31:37 +0000767
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +0300768 if (cpsw->quirk_irq) {
769 disable_irq_nosync(cpsw->irqs_table[0]);
770 cpsw->rx_irq_disabled = true;
Mugunthan V N7da11602015-08-12 15:22:53 +0530771 }
772
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +0300773 napi_schedule(&cpsw->napi_rx);
Mugunthan V Nd354eb82015-08-04 16:06:19 +0530774 return IRQ_HANDLED;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000775}
776
Mugunthan V N32a74322015-08-04 16:06:20 +0530777static int cpsw_tx_poll(struct napi_struct *napi_tx, int budget)
Mugunthan V Ndf828592012-03-18 20:17:54 +0000778{
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +0300779 struct cpsw_common *cpsw = napi_to_cpsw(napi_tx);
Mugunthan V N32a74322015-08-04 16:06:20 +0530780 int num_tx;
781
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +0300782 num_tx = cpdma_chan_process(cpsw->txch, budget);
Mugunthan V N32a74322015-08-04 16:06:20 +0530783 if (num_tx < budget) {
784 napi_complete(napi_tx);
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +0300785 writel(0xff, &cpsw->wr_regs->tx_en);
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +0300786 if (cpsw->quirk_irq && cpsw->tx_irq_disabled) {
787 cpsw->tx_irq_disabled = false;
788 enable_irq(cpsw->irqs_table[1]);
Mugunthan V N7da11602015-08-12 15:22:53 +0530789 }
Mugunthan V N32a74322015-08-04 16:06:20 +0530790 }
791
Mugunthan V N32a74322015-08-04 16:06:20 +0530792 return num_tx;
793}
794
795static int cpsw_rx_poll(struct napi_struct *napi_rx, int budget)
796{
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +0300797 struct cpsw_common *cpsw = napi_to_cpsw(napi_rx);
Mugunthan V N1e353cd2015-07-21 16:00:42 +0530798 int num_rx;
Mugunthan V N510a1e722013-02-17 22:19:20 +0000799
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +0300800 num_rx = cpdma_chan_process(cpsw->rxch, budget);
Mugunthan V N510a1e722013-02-17 22:19:20 +0000801 if (num_rx < budget) {
Mugunthan V N32a74322015-08-04 16:06:20 +0530802 napi_complete(napi_rx);
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +0300803 writel(0xff, &cpsw->wr_regs->rx_en);
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +0300804 if (cpsw->quirk_irq && cpsw->rx_irq_disabled) {
805 cpsw->rx_irq_disabled = false;
806 enable_irq(cpsw->irqs_table[0]);
Mugunthan V N7da11602015-08-12 15:22:53 +0530807 }
Mugunthan V N510a1e722013-02-17 22:19:20 +0000808 }
Mugunthan V Ndf828592012-03-18 20:17:54 +0000809
Mugunthan V Ndf828592012-03-18 20:17:54 +0000810 return num_rx;
811}
812
813static inline void soft_reset(const char *module, void __iomem *reg)
814{
815 unsigned long timeout = jiffies + HZ;
816
817 __raw_writel(1, reg);
818 do {
819 cpu_relax();
820 } while ((__raw_readl(reg) & 1) && time_after(timeout, jiffies));
821
822 WARN(__raw_readl(reg) & 1, "failed to soft-reset %s\n", module);
823}
824
825#define mac_hi(mac) (((mac)[0] << 0) | ((mac)[1] << 8) | \
826 ((mac)[2] << 16) | ((mac)[3] << 24))
827#define mac_lo(mac) (((mac)[4] << 0) | ((mac)[5] << 8))
828
829static void cpsw_set_slave_mac(struct cpsw_slave *slave,
830 struct cpsw_priv *priv)
831{
Richard Cochran9750a3a2012-10-29 08:45:15 +0000832 slave_write(slave, mac_hi(priv->mac_addr), SA_HI);
833 slave_write(slave, mac_lo(priv->mac_addr), SA_LO);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000834}
835
836static void _cpsw_adjust_link(struct cpsw_slave *slave,
837 struct cpsw_priv *priv, bool *link)
838{
839 struct phy_device *phy = slave->phy;
840 u32 mac_control = 0;
841 u32 slave_port;
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300842 struct cpsw_common *cpsw = priv->cpsw;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000843
844 if (!phy)
845 return;
846
Ivan Khoronzhuk6f1f5832016-08-10 02:22:34 +0300847 slave_port = cpsw_get_slave_port(slave->slave_num);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000848
849 if (phy->link) {
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300850 mac_control = cpsw->data.mac_control;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000851
852 /* enable forwarding */
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300853 cpsw_ale_control_set(cpsw->ale, slave_port,
Mugunthan V Ndf828592012-03-18 20:17:54 +0000854 ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
855
856 if (phy->speed == 1000)
857 mac_control |= BIT(7); /* GIGABITEN */
858 if (phy->duplex)
859 mac_control |= BIT(0); /* FULLDUPLEXEN */
Daniel Mack342b7b72012-09-27 09:19:34 +0000860
861 /* set speed_in input in case RMII mode is used in 100Mbps */
862 if (phy->speed == 100)
863 mac_control |= BIT(15);
Mugunthan V Na81d8762013-12-13 18:42:55 +0530864 else if (phy->speed == 10)
865 mac_control |= BIT(18); /* In Band mode */
Daniel Mack342b7b72012-09-27 09:19:34 +0000866
Mugunthan V N1923d6e2014-09-08 22:54:02 +0530867 if (priv->rx_pause)
868 mac_control |= BIT(3);
869
870 if (priv->tx_pause)
871 mac_control |= BIT(4);
872
Mugunthan V Ndf828592012-03-18 20:17:54 +0000873 *link = true;
874 } else {
875 mac_control = 0;
876 /* disable forwarding */
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300877 cpsw_ale_control_set(cpsw->ale, slave_port,
Mugunthan V Ndf828592012-03-18 20:17:54 +0000878 ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
879 }
880
881 if (mac_control != slave->mac_control) {
882 phy_print_status(phy);
883 __raw_writel(mac_control, &slave->sliver->mac_control);
884 }
885
886 slave->mac_control = mac_control;
887}
888
889static void cpsw_adjust_link(struct net_device *ndev)
890{
891 struct cpsw_priv *priv = netdev_priv(ndev);
892 bool link = false;
893
894 for_each_slave(priv, _cpsw_adjust_link, priv, &link);
895
896 if (link) {
897 netif_carrier_on(ndev);
898 if (netif_running(ndev))
899 netif_wake_queue(ndev);
900 } else {
901 netif_carrier_off(ndev);
902 netif_stop_queue(ndev);
903 }
904}
905
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +0000906static int cpsw_get_coalesce(struct net_device *ndev,
907 struct ethtool_coalesce *coal)
908{
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300909 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +0000910
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300911 coal->rx_coalesce_usecs = cpsw->coal_intvl;
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +0000912 return 0;
913}
914
915static int cpsw_set_coalesce(struct net_device *ndev,
916 struct ethtool_coalesce *coal)
917{
918 struct cpsw_priv *priv = netdev_priv(ndev);
919 u32 int_ctrl;
920 u32 num_interrupts = 0;
921 u32 prescale = 0;
922 u32 addnl_dvdr = 1;
923 u32 coal_intvl = 0;
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +0300924 struct cpsw_common *cpsw = priv->cpsw;
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +0000925
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +0000926 coal_intvl = coal->rx_coalesce_usecs;
927
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +0300928 int_ctrl = readl(&cpsw->wr_regs->int_control);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300929 prescale = cpsw->bus_freq_mhz * 4;
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +0000930
Mugunthan V Na84bc2a2014-07-15 20:26:53 +0530931 if (!coal->rx_coalesce_usecs) {
932 int_ctrl &= ~(CPSW_INTPRESCALE_MASK | CPSW_INTPACEEN);
933 goto update_return;
934 }
935
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +0000936 if (coal_intvl < CPSW_CMINTMIN_INTVL)
937 coal_intvl = CPSW_CMINTMIN_INTVL;
938
939 if (coal_intvl > CPSW_CMINTMAX_INTVL) {
940 /* Interrupt pacer works with 4us Pulse, we can
941 * throttle further by dilating the 4us pulse.
942 */
943 addnl_dvdr = CPSW_INTPRESCALE_MASK / prescale;
944
945 if (addnl_dvdr > 1) {
946 prescale *= addnl_dvdr;
947 if (coal_intvl > (CPSW_CMINTMAX_INTVL * addnl_dvdr))
948 coal_intvl = (CPSW_CMINTMAX_INTVL
949 * addnl_dvdr);
950 } else {
951 addnl_dvdr = 1;
952 coal_intvl = CPSW_CMINTMAX_INTVL;
953 }
954 }
955
956 num_interrupts = (1000 * addnl_dvdr) / coal_intvl;
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +0300957 writel(num_interrupts, &cpsw->wr_regs->rx_imax);
958 writel(num_interrupts, &cpsw->wr_regs->tx_imax);
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +0000959
960 int_ctrl |= CPSW_INTPACEEN;
961 int_ctrl &= (~CPSW_INTPRESCALE_MASK);
962 int_ctrl |= (prescale & CPSW_INTPRESCALE_MASK);
Mugunthan V Na84bc2a2014-07-15 20:26:53 +0530963
964update_return:
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +0300965 writel(int_ctrl, &cpsw->wr_regs->int_control);
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +0000966
967 cpsw_notice(priv, timer, "Set coalesce to %d usecs.\n", coal_intvl);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300968 cpsw->coal_intvl = coal_intvl;
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +0000969
970 return 0;
971}
972
Mugunthan V Nd9718542013-07-23 15:38:17 +0530973static int cpsw_get_sset_count(struct net_device *ndev, int sset)
974{
975 switch (sset) {
976 case ETH_SS_STATS:
977 return CPSW_STATS_LEN;
978 default:
979 return -EOPNOTSUPP;
980 }
981}
982
983static void cpsw_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
984{
985 u8 *p = data;
986 int i;
987
988 switch (stringset) {
989 case ETH_SS_STATS:
990 for (i = 0; i < CPSW_STATS_LEN; i++) {
991 memcpy(p, cpsw_gstrings_stats[i].stat_string,
992 ETH_GSTRING_LEN);
993 p += ETH_GSTRING_LEN;
994 }
995 break;
996 }
997}
998
999static void cpsw_get_ethtool_stats(struct net_device *ndev,
1000 struct ethtool_stats *stats, u64 *data)
1001{
Mugunthan V Nd9718542013-07-23 15:38:17 +05301002 struct cpdma_chan_stats rx_stats;
1003 struct cpdma_chan_stats tx_stats;
1004 u32 val;
1005 u8 *p;
1006 int i;
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001007 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Mugunthan V Nd9718542013-07-23 15:38:17 +05301008
1009 /* Collect Davinci CPDMA stats for Rx and Tx Channel */
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001010 cpdma_chan_get_stats(cpsw->rxch, &rx_stats);
1011 cpdma_chan_get_stats(cpsw->txch, &tx_stats);
Mugunthan V Nd9718542013-07-23 15:38:17 +05301012
1013 for (i = 0; i < CPSW_STATS_LEN; i++) {
1014 switch (cpsw_gstrings_stats[i].type) {
1015 case CPSW_STATS:
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001016 val = readl(cpsw->hw_stats +
Mugunthan V Nd9718542013-07-23 15:38:17 +05301017 cpsw_gstrings_stats[i].stat_offset);
1018 data[i] = val;
1019 break;
1020
1021 case CPDMA_RX_STATS:
1022 p = (u8 *)&rx_stats +
1023 cpsw_gstrings_stats[i].stat_offset;
1024 data[i] = *(u32 *)p;
1025 break;
1026
1027 case CPDMA_TX_STATS:
1028 p = (u8 *)&tx_stats +
1029 cpsw_gstrings_stats[i].stat_offset;
1030 data[i] = *(u32 *)p;
1031 break;
1032 }
1033 }
1034}
1035
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001036static int cpsw_common_res_usage_state(struct cpsw_common *cpsw)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001037{
1038 u32 i;
1039 u32 usage_count = 0;
1040
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001041 if (!cpsw->data.dual_emac)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001042 return 0;
1043
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001044 for (i = 0; i < cpsw->data.slaves; i++)
1045 if (cpsw->slaves[i].open_stat)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001046 usage_count++;
1047
1048 return usage_count;
1049}
1050
Ivan Khoronzhuk27e9e102016-08-10 02:22:32 +03001051static inline int cpsw_tx_packet_submit(struct cpsw_priv *priv,
1052 struct sk_buff *skb)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001053{
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001054 struct cpsw_common *cpsw = priv->cpsw;
1055
1056 return cpdma_chan_submit(cpsw->txch, skb, skb->data, skb->len,
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001057 priv->emac_port + cpsw->data.dual_emac);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001058}
1059
1060static inline void cpsw_add_dual_emac_def_ale_entries(
1061 struct cpsw_priv *priv, struct cpsw_slave *slave,
1062 u32 slave_port)
1063{
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001064 struct cpsw_common *cpsw = priv->cpsw;
Grygorii Strashko71a2cbb2016-04-07 15:16:44 +03001065 u32 port_mask = 1 << slave_port | ALE_PORT_HOST;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001066
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001067 if (cpsw->version == CPSW_VERSION_1)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001068 slave_write(slave, slave->port_vlan, CPSW1_PORT_VLAN);
1069 else
1070 slave_write(slave, slave->port_vlan, CPSW2_PORT_VLAN);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001071 cpsw_ale_add_vlan(cpsw->ale, slave->port_vlan, port_mask,
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001072 port_mask, port_mask, 0);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001073 cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001074 port_mask, ALE_VLAN, slave->port_vlan, 0);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001075 cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr,
1076 HOST_PORT_NUM, ALE_VLAN |
1077 ALE_SECURE, slave->port_vlan);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001078}
1079
Daniel Mack1e7a2e22013-11-15 08:29:16 +01001080static void soft_reset_slave(struct cpsw_slave *slave)
Mugunthan V Ndf828592012-03-18 20:17:54 +00001081{
1082 char name[32];
Daniel Mack1e7a2e22013-11-15 08:29:16 +01001083
1084 snprintf(name, sizeof(name), "slave-%d", slave->slave_num);
1085 soft_reset(name, &slave->sliver->soft_reset);
1086}
1087
1088static void cpsw_slave_open(struct cpsw_slave *slave, struct cpsw_priv *priv)
1089{
Mugunthan V Ndf828592012-03-18 20:17:54 +00001090 u32 slave_port;
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03001091 struct cpsw_common *cpsw = priv->cpsw;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001092
Daniel Mack1e7a2e22013-11-15 08:29:16 +01001093 soft_reset_slave(slave);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001094
1095 /* setup priority mapping */
1096 __raw_writel(RX_PRIORITY_MAPPING, &slave->sliver->rx_pri_map);
Richard Cochran9750a3a2012-10-29 08:45:15 +00001097
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001098 switch (cpsw->version) {
Richard Cochran9750a3a2012-10-29 08:45:15 +00001099 case CPSW_VERSION_1:
1100 slave_write(slave, TX_PRIORITY_MAPPING, CPSW1_TX_PRI_MAP);
1101 break;
1102 case CPSW_VERSION_2:
Mugunthan V Nc193f362013-08-05 17:30:05 +05301103 case CPSW_VERSION_3:
Mugunthan V N926489b2013-08-12 17:11:15 +05301104 case CPSW_VERSION_4:
Richard Cochran9750a3a2012-10-29 08:45:15 +00001105 slave_write(slave, TX_PRIORITY_MAPPING, CPSW2_TX_PRI_MAP);
1106 break;
1107 }
Mugunthan V Ndf828592012-03-18 20:17:54 +00001108
1109 /* setup max packet size, and mac address */
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001110 __raw_writel(cpsw->rx_packet_max, &slave->sliver->rx_maxlen);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001111 cpsw_set_slave_mac(slave, priv);
1112
1113 slave->mac_control = 0; /* no link yet */
1114
Ivan Khoronzhuk6f1f5832016-08-10 02:22:34 +03001115 slave_port = cpsw_get_slave_port(slave->slave_num);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001116
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001117 if (cpsw->data.dual_emac)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001118 cpsw_add_dual_emac_def_ale_entries(priv, slave, slave_port);
1119 else
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001120 cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001121 1 << slave_port, 0, 0, ALE_MCAST_FWD_2);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001122
David Rivshind733f7542016-04-27 21:32:31 -04001123 if (slave->data->phy_node) {
David Rivshin552165b2016-04-27 21:25:25 -04001124 slave->phy = of_phy_connect(priv->ndev, slave->data->phy_node,
Heiko Schocher9e42f712015-10-17 06:04:35 +02001125 &cpsw_adjust_link, 0, slave->data->phy_if);
David Rivshind733f7542016-04-27 21:32:31 -04001126 if (!slave->phy) {
1127 dev_err(priv->dev, "phy \"%s\" not found on slave %d\n",
1128 slave->data->phy_node->full_name,
1129 slave->slave_num);
1130 return;
1131 }
1132 } else {
Heiko Schocher9e42f712015-10-17 06:04:35 +02001133 slave->phy = phy_connect(priv->ndev, slave->data->phy_id,
Florian Fainellif9a8f832013-01-14 00:52:52 +00001134 &cpsw_adjust_link, slave->data->phy_if);
David Rivshind733f7542016-04-27 21:32:31 -04001135 if (IS_ERR(slave->phy)) {
1136 dev_err(priv->dev,
1137 "phy \"%s\" not found on slave %d, err %ld\n",
1138 slave->data->phy_id, slave->slave_num,
1139 PTR_ERR(slave->phy));
1140 slave->phy = NULL;
1141 return;
1142 }
Mugunthan V Ndf828592012-03-18 20:17:54 +00001143 }
David Rivshind733f7542016-04-27 21:32:31 -04001144
1145 phy_attached_info(slave->phy);
1146
1147 phy_start(slave->phy);
1148
1149 /* Configure GMII_SEL register */
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001150 cpsw_phy_sel(cpsw->dev, slave->phy->interface, slave->slave_num);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001151}
1152
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001153static inline void cpsw_add_default_vlan(struct cpsw_priv *priv)
1154{
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001155 struct cpsw_common *cpsw = priv->cpsw;
1156 const int vlan = cpsw->data.default_vlan;
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001157 u32 reg;
1158 int i;
Lennart Sorensen1e5c4bc2014-10-31 13:38:52 -04001159 int unreg_mcast_mask;
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001160
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001161 reg = (cpsw->version == CPSW_VERSION_1) ? CPSW1_PORT_VLAN :
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001162 CPSW2_PORT_VLAN;
1163
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001164 writel(vlan, &cpsw->host_port_regs->port_vlan);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001165
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001166 for (i = 0; i < cpsw->data.slaves; i++)
1167 slave_write(cpsw->slaves + i, vlan, reg);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001168
Lennart Sorensen1e5c4bc2014-10-31 13:38:52 -04001169 if (priv->ndev->flags & IFF_ALLMULTI)
1170 unreg_mcast_mask = ALE_ALL_PORTS;
1171 else
1172 unreg_mcast_mask = ALE_PORT_1 | ALE_PORT_2;
1173
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001174 cpsw_ale_add_vlan(cpsw->ale, vlan, ALE_ALL_PORTS,
Grygorii Strashko61f1cef2016-04-07 15:16:43 +03001175 ALE_ALL_PORTS, ALE_ALL_PORTS,
1176 unreg_mcast_mask);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001177}
1178
Mugunthan V Ndf828592012-03-18 20:17:54 +00001179static void cpsw_init_host_port(struct cpsw_priv *priv)
1180{
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001181 u32 fifo_mode;
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001182 u32 control_reg;
1183 struct cpsw_common *cpsw = priv->cpsw;
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001184
Mugunthan V Ndf828592012-03-18 20:17:54 +00001185 /* soft reset the controller and initialize ale */
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001186 soft_reset("cpsw", &cpsw->regs->soft_reset);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001187 cpsw_ale_start(cpsw->ale);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001188
1189 /* switch to vlan unaware mode */
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001190 cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_VLAN_AWARE,
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001191 CPSW_ALE_VLAN_AWARE);
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001192 control_reg = readl(&cpsw->regs->control);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001193 control_reg |= CPSW_VLAN_AWARE;
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001194 writel(control_reg, &cpsw->regs->control);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001195 fifo_mode = (cpsw->data.dual_emac) ? CPSW_FIFO_DUAL_MAC_MODE :
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001196 CPSW_FIFO_NORMAL_MODE;
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001197 writel(fifo_mode, &cpsw->host_port_regs->tx_in_ctl);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001198
1199 /* setup host port priority mapping */
1200 __raw_writel(CPDMA_TX_PRIORITY_MAP,
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001201 &cpsw->host_port_regs->cpdma_tx_pri_map);
1202 __raw_writel(0, &cpsw->host_port_regs->cpdma_rx_chan_map);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001203
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001204 cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM,
Mugunthan V Ndf828592012-03-18 20:17:54 +00001205 ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
1206
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001207 if (!cpsw->data.dual_emac) {
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001208 cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr, HOST_PORT_NUM,
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001209 0, 0);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001210 cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
Grygorii Strashko71a2cbb2016-04-07 15:16:44 +03001211 ALE_PORT_HOST, 0, 0, ALE_MCAST_FWD_2);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001212 }
Mugunthan V Ndf828592012-03-18 20:17:54 +00001213}
1214
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001215static void cpsw_slave_stop(struct cpsw_slave *slave, struct cpsw_common *cpsw)
Sebastian Siewioraacebbf2013-04-23 07:31:36 +00001216{
Schuyler Patton3995d262014-03-03 16:19:06 +05301217 u32 slave_port;
1218
Ivan Khoronzhuk6f1f5832016-08-10 02:22:34 +03001219 slave_port = cpsw_get_slave_port(slave->slave_num);
Schuyler Patton3995d262014-03-03 16:19:06 +05301220
Sebastian Siewioraacebbf2013-04-23 07:31:36 +00001221 if (!slave->phy)
1222 return;
1223 phy_stop(slave->phy);
1224 phy_disconnect(slave->phy);
1225 slave->phy = NULL;
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001226 cpsw_ale_control_set(cpsw->ale, slave_port,
Schuyler Patton3995d262014-03-03 16:19:06 +05301227 ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
Grygorii Strashko1f95ba02016-06-24 21:23:41 +03001228 soft_reset_slave(slave);
Sebastian Siewioraacebbf2013-04-23 07:31:36 +00001229}
1230
Mugunthan V Ndf828592012-03-18 20:17:54 +00001231static int cpsw_ndo_open(struct net_device *ndev)
1232{
1233 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03001234 struct cpsw_common *cpsw = priv->cpsw;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001235 int i, ret;
1236 u32 reg;
1237
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001238 ret = pm_runtime_get_sync(cpsw->dev);
Grygorii Strashko108a6532016-06-24 21:23:42 +03001239 if (ret < 0) {
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001240 pm_runtime_put_noidle(cpsw->dev);
Grygorii Strashko108a6532016-06-24 21:23:42 +03001241 return ret;
1242 }
Grygorii Strashko3fa88c52016-04-19 21:09:49 +03001243
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001244 if (!cpsw_common_res_usage_state(cpsw))
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001245 cpsw_intr_disable(cpsw);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001246 netif_carrier_off(ndev);
1247
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001248 reg = cpsw->version;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001249
1250 dev_info(priv->dev, "initializing cpsw version %d.%d (%d)\n",
1251 CPSW_MAJOR_VERSION(reg), CPSW_MINOR_VERSION(reg),
1252 CPSW_RTL_VERSION(reg));
1253
1254 /* initialize host and slave ports */
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001255 if (!cpsw_common_res_usage_state(cpsw))
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001256 cpsw_init_host_port(priv);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001257 for_each_slave(priv, cpsw_slave_open, priv);
1258
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001259 /* Add default VLAN */
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001260 if (!cpsw->data.dual_emac)
Mugunthan V Ne6afea02014-06-18 17:21:48 +05301261 cpsw_add_default_vlan(priv);
1262 else
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001263 cpsw_ale_add_vlan(cpsw->ale, cpsw->data.default_vlan,
Grygorii Strashko61f1cef2016-04-07 15:16:43 +03001264 ALE_ALL_PORTS, ALE_ALL_PORTS, 0, 0);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001265
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001266 if (!cpsw_common_res_usage_state(cpsw)) {
Ivan Khoronzhuk17933312016-06-17 13:25:39 +03001267 int buf_num;
Mugunthan V Nd354eb82015-08-04 16:06:19 +05301268
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001269 /* setup tx dma to fixed prio and zero offset */
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001270 cpdma_control_set(cpsw->dma, CPDMA_TX_PRIO_FIXED, 1);
1271 cpdma_control_set(cpsw->dma, CPDMA_RX_BUFFER_OFFSET, 0);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001272
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001273 /* disable priority elevation */
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001274 __raw_writel(0, &cpsw->regs->ptype);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001275
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001276 /* enable statistics collection only on all ports */
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001277 __raw_writel(0x7, &cpsw->regs->stat_port_en);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001278
Mugunthan V N1923d6e2014-09-08 22:54:02 +05301279 /* Enable internal fifo flow control */
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001280 writel(0x7, &cpsw->regs->flow_control);
Mugunthan V N1923d6e2014-09-08 22:54:02 +05301281
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +03001282 napi_enable(&cpsw->napi_rx);
1283 napi_enable(&cpsw->napi_tx);
Mugunthan V Nd354eb82015-08-04 16:06:19 +05301284
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +03001285 if (cpsw->tx_irq_disabled) {
1286 cpsw->tx_irq_disabled = false;
1287 enable_irq(cpsw->irqs_table[1]);
Mugunthan V N7da11602015-08-12 15:22:53 +05301288 }
1289
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +03001290 if (cpsw->rx_irq_disabled) {
1291 cpsw->rx_irq_disabled = false;
1292 enable_irq(cpsw->irqs_table[0]);
Mugunthan V N7da11602015-08-12 15:22:53 +05301293 }
1294
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001295 buf_num = cpdma_chan_get_rx_buf_num(cpsw->dma);
Ivan Khoronzhuk17933312016-06-17 13:25:39 +03001296 for (i = 0; i < buf_num; i++) {
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001297 struct sk_buff *skb;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001298
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001299 ret = -ENOMEM;
Sebastian Siewioraacebbf2013-04-23 07:31:36 +00001300 skb = __netdev_alloc_skb_ip_align(priv->ndev,
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001301 cpsw->rx_packet_max, GFP_KERNEL);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001302 if (!skb)
Sebastian Siewioraacebbf2013-04-23 07:31:36 +00001303 goto err_cleanup;
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001304 ret = cpdma_chan_submit(cpsw->rxch, skb, skb->data,
1305 skb_tailroom(skb), 0);
Sebastian Siewioraacebbf2013-04-23 07:31:36 +00001306 if (ret < 0) {
1307 kfree_skb(skb);
1308 goto err_cleanup;
1309 }
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001310 }
1311 /* continue even if we didn't manage to submit all
1312 * receive descs
1313 */
1314 cpsw_info(priv, ifup, "submitted %d rx descriptors\n", i);
Mugunthan V Nf280e892013-12-11 22:09:05 -06001315
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001316 if (cpts_register(cpsw->dev, cpsw->cpts,
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001317 cpsw->data.cpts_clock_mult,
1318 cpsw->data.cpts_clock_shift))
Mugunthan V Nf280e892013-12-11 22:09:05 -06001319 dev_err(priv->dev, "error registering cpts device\n");
1320
Mugunthan V Ndf828592012-03-18 20:17:54 +00001321 }
Mugunthan V Ndf828592012-03-18 20:17:54 +00001322
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001323 /* Enable Interrupt pacing if configured */
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001324 if (cpsw->coal_intvl != 0) {
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001325 struct ethtool_coalesce coal;
1326
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001327 coal.rx_coalesce_usecs = cpsw->coal_intvl;
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001328 cpsw_set_coalesce(ndev, &coal);
1329 }
1330
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001331 cpdma_ctlr_start(cpsw->dma);
1332 cpsw_intr_enable(cpsw);
Mugunthan V Nf63a9752014-04-10 14:23:24 +05301333
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001334 if (cpsw->data.dual_emac)
1335 cpsw->slaves[priv->emac_port].open_stat = true;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001336 return 0;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001337
Sebastian Siewioraacebbf2013-04-23 07:31:36 +00001338err_cleanup:
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001339 cpdma_ctlr_stop(cpsw->dma);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001340 for_each_slave(priv, cpsw_slave_stop, cpsw);
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001341 pm_runtime_put_sync(cpsw->dev);
Sebastian Siewioraacebbf2013-04-23 07:31:36 +00001342 netif_carrier_off(priv->ndev);
1343 return ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001344}
1345
1346static int cpsw_ndo_stop(struct net_device *ndev)
1347{
1348 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03001349 struct cpsw_common *cpsw = priv->cpsw;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001350
1351 cpsw_info(priv, ifdown, "shutting down cpsw device\n");
Mugunthan V Ndf828592012-03-18 20:17:54 +00001352 netif_stop_queue(priv->ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001353 netif_carrier_off(priv->ndev);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001354
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001355 if (cpsw_common_res_usage_state(cpsw) <= 1) {
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +03001356 napi_disable(&cpsw->napi_rx);
1357 napi_disable(&cpsw->napi_tx);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001358 cpts_unregister(cpsw->cpts);
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001359 cpsw_intr_disable(cpsw);
1360 cpdma_ctlr_stop(cpsw->dma);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001361 cpsw_ale_stop(cpsw->ale);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001362 }
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001363 for_each_slave(priv, cpsw_slave_stop, cpsw);
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001364 pm_runtime_put_sync(cpsw->dev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001365 if (cpsw->data.dual_emac)
1366 cpsw->slaves[priv->emac_port].open_stat = false;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001367 return 0;
1368}
1369
1370static netdev_tx_t cpsw_ndo_start_xmit(struct sk_buff *skb,
1371 struct net_device *ndev)
1372{
1373 struct cpsw_priv *priv = netdev_priv(ndev);
1374 int ret;
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001375 struct cpsw_common *cpsw = priv->cpsw;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001376
Florian Westphal860e9532016-05-03 16:33:13 +02001377 netif_trans_update(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001378
1379 if (skb_padto(skb, CPSW_MIN_PACKET_SIZE)) {
1380 cpsw_err(priv, tx_err, "packet pad failed\n");
Tobias Klauser8dc43dd2014-03-10 13:12:23 +01001381 ndev->stats.tx_dropped++;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001382 return NETDEV_TX_OK;
1383 }
1384
Mugunthan V N9232b162013-02-11 09:52:19 +00001385 if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001386 cpsw->cpts->tx_enable)
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001387 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
1388
1389 skb_tx_timestamp(skb);
1390
Ivan Khoronzhuk27e9e102016-08-10 02:22:32 +03001391 ret = cpsw_tx_packet_submit(priv, skb);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001392 if (unlikely(ret != 0)) {
1393 cpsw_err(priv, tx_err, "desc submit failed\n");
1394 goto fail;
1395 }
1396
Mugunthan V Nfae50822013-01-17 06:31:34 +00001397 /* If there is no more tx desc left free then we need to
1398 * tell the kernel to stop sending us tx frames.
1399 */
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001400 if (unlikely(!cpdma_check_free_tx_desc(cpsw->txch)))
Mugunthan V Nfae50822013-01-17 06:31:34 +00001401 netif_stop_queue(ndev);
1402
Mugunthan V Ndf828592012-03-18 20:17:54 +00001403 return NETDEV_TX_OK;
1404fail:
Tobias Klauser8dc43dd2014-03-10 13:12:23 +01001405 ndev->stats.tx_dropped++;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001406 netif_stop_queue(ndev);
1407 return NETDEV_TX_BUSY;
1408}
1409
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001410#ifdef CONFIG_TI_CPTS
1411
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001412static void cpsw_hwtstamp_v1(struct cpsw_common *cpsw)
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001413{
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001414 struct cpsw_slave *slave = &cpsw->slaves[cpsw->data.active_slave];
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001415 u32 ts_en, seq_id;
1416
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001417 if (!cpsw->cpts->tx_enable && !cpsw->cpts->rx_enable) {
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001418 slave_write(slave, 0, CPSW1_TS_CTL);
1419 return;
1420 }
1421
1422 seq_id = (30 << CPSW_V1_SEQ_ID_OFS_SHIFT) | ETH_P_1588;
1423 ts_en = EVENT_MSG_BITS << CPSW_V1_MSG_TYPE_OFS;
1424
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001425 if (cpsw->cpts->tx_enable)
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001426 ts_en |= CPSW_V1_TS_TX_EN;
1427
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001428 if (cpsw->cpts->rx_enable)
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001429 ts_en |= CPSW_V1_TS_RX_EN;
1430
1431 slave_write(slave, ts_en, CPSW1_TS_CTL);
1432 slave_write(slave, seq_id, CPSW1_TS_SEQ_LTYPE);
1433}
1434
1435static void cpsw_hwtstamp_v2(struct cpsw_priv *priv)
1436{
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001437 struct cpsw_slave *slave;
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001438 struct cpsw_common *cpsw = priv->cpsw;
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001439 u32 ctrl, mtype;
1440
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001441 if (cpsw->data.dual_emac)
1442 slave = &cpsw->slaves[priv->emac_port];
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001443 else
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001444 slave = &cpsw->slaves[cpsw->data.active_slave];
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001445
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001446 ctrl = slave_read(slave, CPSW2_CONTROL);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001447 switch (cpsw->version) {
George Cherian09c55372014-05-02 12:02:02 +05301448 case CPSW_VERSION_2:
1449 ctrl &= ~CTRL_V2_ALL_TS_MASK;
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001450
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001451 if (cpsw->cpts->tx_enable)
George Cherian09c55372014-05-02 12:02:02 +05301452 ctrl |= CTRL_V2_TX_TS_BITS;
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001453
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001454 if (cpsw->cpts->rx_enable)
George Cherian09c55372014-05-02 12:02:02 +05301455 ctrl |= CTRL_V2_RX_TS_BITS;
Richard Cochran26fe7eb2015-05-25 11:02:13 +02001456 break;
George Cherian09c55372014-05-02 12:02:02 +05301457 case CPSW_VERSION_3:
1458 default:
1459 ctrl &= ~CTRL_V3_ALL_TS_MASK;
1460
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001461 if (cpsw->cpts->tx_enable)
George Cherian09c55372014-05-02 12:02:02 +05301462 ctrl |= CTRL_V3_TX_TS_BITS;
1463
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001464 if (cpsw->cpts->rx_enable)
George Cherian09c55372014-05-02 12:02:02 +05301465 ctrl |= CTRL_V3_RX_TS_BITS;
Richard Cochran26fe7eb2015-05-25 11:02:13 +02001466 break;
George Cherian09c55372014-05-02 12:02:02 +05301467 }
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001468
1469 mtype = (30 << TS_SEQ_ID_OFFSET_SHIFT) | EVENT_MSG_BITS;
1470
1471 slave_write(slave, mtype, CPSW2_TS_SEQ_MTYPE);
1472 slave_write(slave, ctrl, CPSW2_CONTROL);
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001473 __raw_writel(ETH_P_1588, &cpsw->regs->ts_ltype);
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001474}
1475
Ben Hutchingsa5b41452013-11-18 23:23:40 +00001476static int cpsw_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001477{
Mugunthan V N3177bf62012-11-27 07:53:40 +00001478 struct cpsw_priv *priv = netdev_priv(dev);
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001479 struct hwtstamp_config cfg;
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001480 struct cpsw_common *cpsw = priv->cpsw;
1481 struct cpts *cpts = cpsw->cpts;
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001482
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001483 if (cpsw->version != CPSW_VERSION_1 &&
1484 cpsw->version != CPSW_VERSION_2 &&
1485 cpsw->version != CPSW_VERSION_3)
Ben Hutchings2ee91e52013-11-14 00:47:36 +00001486 return -EOPNOTSUPP;
1487
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001488 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
1489 return -EFAULT;
1490
1491 /* reserved for future extensions */
1492 if (cfg.flags)
1493 return -EINVAL;
1494
Ben Hutchings2ee91e52013-11-14 00:47:36 +00001495 if (cfg.tx_type != HWTSTAMP_TX_OFF && cfg.tx_type != HWTSTAMP_TX_ON)
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001496 return -ERANGE;
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001497
1498 switch (cfg.rx_filter) {
1499 case HWTSTAMP_FILTER_NONE:
1500 cpts->rx_enable = 0;
1501 break;
1502 case HWTSTAMP_FILTER_ALL:
1503 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
1504 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
1505 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
1506 return -ERANGE;
1507 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
1508 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
1509 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
1510 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1511 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
1512 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
1513 case HWTSTAMP_FILTER_PTP_V2_EVENT:
1514 case HWTSTAMP_FILTER_PTP_V2_SYNC:
1515 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
1516 cpts->rx_enable = 1;
1517 cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
1518 break;
1519 default:
1520 return -ERANGE;
1521 }
1522
Ben Hutchings2ee91e52013-11-14 00:47:36 +00001523 cpts->tx_enable = cfg.tx_type == HWTSTAMP_TX_ON;
1524
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001525 switch (cpsw->version) {
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001526 case CPSW_VERSION_1:
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001527 cpsw_hwtstamp_v1(cpsw);
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001528 break;
1529 case CPSW_VERSION_2:
George Cherianf7d403c2014-05-02 12:02:01 +05301530 case CPSW_VERSION_3:
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001531 cpsw_hwtstamp_v2(priv);
1532 break;
1533 default:
Ben Hutchings2ee91e52013-11-14 00:47:36 +00001534 WARN_ON(1);
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001535 }
1536
1537 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
1538}
1539
Ben Hutchingsa5b41452013-11-18 23:23:40 +00001540static int cpsw_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
1541{
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001542 struct cpsw_common *cpsw = ndev_to_cpsw(dev);
1543 struct cpts *cpts = cpsw->cpts;
Ben Hutchingsa5b41452013-11-18 23:23:40 +00001544 struct hwtstamp_config cfg;
1545
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001546 if (cpsw->version != CPSW_VERSION_1 &&
1547 cpsw->version != CPSW_VERSION_2 &&
1548 cpsw->version != CPSW_VERSION_3)
Ben Hutchingsa5b41452013-11-18 23:23:40 +00001549 return -EOPNOTSUPP;
1550
1551 cfg.flags = 0;
1552 cfg.tx_type = cpts->tx_enable ? HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF;
1553 cfg.rx_filter = (cpts->rx_enable ?
1554 HWTSTAMP_FILTER_PTP_V2_EVENT : HWTSTAMP_FILTER_NONE);
1555
1556 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
1557}
1558
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001559#endif /*CONFIG_TI_CPTS*/
1560
1561static int cpsw_ndo_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
1562{
Mugunthan V N11f2c982013-03-11 23:16:38 +00001563 struct cpsw_priv *priv = netdev_priv(dev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001564 struct cpsw_common *cpsw = priv->cpsw;
1565 int slave_no = cpsw_slave_index(cpsw, priv);
Mugunthan V N11f2c982013-03-11 23:16:38 +00001566
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001567 if (!netif_running(dev))
1568 return -EINVAL;
1569
Mugunthan V N11f2c982013-03-11 23:16:38 +00001570 switch (cmd) {
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001571#ifdef CONFIG_TI_CPTS
Mugunthan V N11f2c982013-03-11 23:16:38 +00001572 case SIOCSHWTSTAMP:
Ben Hutchingsa5b41452013-11-18 23:23:40 +00001573 return cpsw_hwtstamp_set(dev, req);
1574 case SIOCGHWTSTAMP:
1575 return cpsw_hwtstamp_get(dev, req);
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001576#endif
Mugunthan V N11f2c982013-03-11 23:16:38 +00001577 }
1578
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001579 if (!cpsw->slaves[slave_no].phy)
Stefan Sørensenc1b59942014-02-16 14:54:25 +01001580 return -EOPNOTSUPP;
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001581 return phy_mii_ioctl(cpsw->slaves[slave_no].phy, req, cmd);
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001582}
1583
Mugunthan V Ndf828592012-03-18 20:17:54 +00001584static void cpsw_ndo_tx_timeout(struct net_device *ndev)
1585{
1586 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001587 struct cpsw_common *cpsw = priv->cpsw;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001588
1589 cpsw_err(priv, tx_err, "transmit timeout, restarting dma\n");
Tobias Klauser8dc43dd2014-03-10 13:12:23 +01001590 ndev->stats.tx_errors++;
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001591 cpsw_intr_disable(cpsw);
1592 cpdma_chan_stop(cpsw->txch);
1593 cpdma_chan_start(cpsw->txch);
1594 cpsw_intr_enable(cpsw);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001595}
1596
Mugunthan V Ndcfd8d52013-07-25 23:44:01 +05301597static int cpsw_ndo_set_mac_address(struct net_device *ndev, void *p)
1598{
1599 struct cpsw_priv *priv = netdev_priv(ndev);
1600 struct sockaddr *addr = (struct sockaddr *)p;
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03001601 struct cpsw_common *cpsw = priv->cpsw;
Mugunthan V Ndcfd8d52013-07-25 23:44:01 +05301602 int flags = 0;
1603 u16 vid = 0;
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001604 int ret;
Mugunthan V Ndcfd8d52013-07-25 23:44:01 +05301605
1606 if (!is_valid_ether_addr(addr->sa_data))
1607 return -EADDRNOTAVAIL;
1608
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001609 ret = pm_runtime_get_sync(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001610 if (ret < 0) {
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001611 pm_runtime_put_noidle(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001612 return ret;
1613 }
1614
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001615 if (cpsw->data.dual_emac) {
1616 vid = cpsw->slaves[priv->emac_port].port_vlan;
Mugunthan V Ndcfd8d52013-07-25 23:44:01 +05301617 flags = ALE_VLAN;
1618 }
1619
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001620 cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr, HOST_PORT_NUM,
Mugunthan V Ndcfd8d52013-07-25 23:44:01 +05301621 flags, vid);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001622 cpsw_ale_add_ucast(cpsw->ale, addr->sa_data, HOST_PORT_NUM,
Mugunthan V Ndcfd8d52013-07-25 23:44:01 +05301623 flags, vid);
1624
1625 memcpy(priv->mac_addr, addr->sa_data, ETH_ALEN);
1626 memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
1627 for_each_slave(priv, cpsw_set_slave_mac, priv);
1628
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001629 pm_runtime_put(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001630
Mugunthan V Ndcfd8d52013-07-25 23:44:01 +05301631 return 0;
1632}
1633
Mugunthan V Ndf828592012-03-18 20:17:54 +00001634#ifdef CONFIG_NET_POLL_CONTROLLER
1635static void cpsw_ndo_poll_controller(struct net_device *ndev)
1636{
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +03001637 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001638
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +03001639 cpsw_intr_disable(cpsw);
1640 cpsw_rx_interrupt(cpsw->irqs_table[0], cpsw);
1641 cpsw_tx_interrupt(cpsw->irqs_table[1], cpsw);
1642 cpsw_intr_enable(cpsw);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001643}
1644#endif
1645
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001646static inline int cpsw_add_vlan_ale_entry(struct cpsw_priv *priv,
1647 unsigned short vid)
1648{
1649 int ret;
Mugunthan V N9f6bd8f2015-01-15 14:59:28 +05301650 int unreg_mcast_mask = 0;
1651 u32 port_mask;
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001652 struct cpsw_common *cpsw = priv->cpsw;
Lennart Sorensen1e5c4bc2014-10-31 13:38:52 -04001653
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001654 if (cpsw->data.dual_emac) {
Mugunthan V N9f6bd8f2015-01-15 14:59:28 +05301655 port_mask = (1 << (priv->emac_port + 1)) | ALE_PORT_HOST;
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001656
Mugunthan V N9f6bd8f2015-01-15 14:59:28 +05301657 if (priv->ndev->flags & IFF_ALLMULTI)
1658 unreg_mcast_mask = port_mask;
1659 } else {
1660 port_mask = ALE_ALL_PORTS;
1661
1662 if (priv->ndev->flags & IFF_ALLMULTI)
1663 unreg_mcast_mask = ALE_ALL_PORTS;
1664 else
1665 unreg_mcast_mask = ALE_PORT_1 | ALE_PORT_2;
1666 }
1667
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001668 ret = cpsw_ale_add_vlan(cpsw->ale, vid, port_mask, 0, port_mask,
Grygorii Strashko61f1cef2016-04-07 15:16:43 +03001669 unreg_mcast_mask);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001670 if (ret != 0)
1671 return ret;
1672
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001673 ret = cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr,
Grygorii Strashko71a2cbb2016-04-07 15:16:44 +03001674 HOST_PORT_NUM, ALE_VLAN, vid);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001675 if (ret != 0)
1676 goto clean_vid;
1677
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001678 ret = cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
Mugunthan V N9f6bd8f2015-01-15 14:59:28 +05301679 port_mask, ALE_VLAN, vid, 0);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001680 if (ret != 0)
1681 goto clean_vlan_ucast;
1682 return 0;
1683
1684clean_vlan_ucast:
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001685 cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr,
Grygorii Strashko71a2cbb2016-04-07 15:16:44 +03001686 HOST_PORT_NUM, ALE_VLAN, vid);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001687clean_vid:
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001688 cpsw_ale_del_vlan(cpsw->ale, vid, 0);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001689 return ret;
1690}
1691
1692static int cpsw_ndo_vlan_rx_add_vid(struct net_device *ndev,
Patrick McHardy80d5c362013-04-19 02:04:28 +00001693 __be16 proto, u16 vid)
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001694{
1695 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03001696 struct cpsw_common *cpsw = priv->cpsw;
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001697 int ret;
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001698
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001699 if (vid == cpsw->data.default_vlan)
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001700 return 0;
1701
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001702 ret = pm_runtime_get_sync(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001703 if (ret < 0) {
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001704 pm_runtime_put_noidle(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001705 return ret;
1706 }
1707
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001708 if (cpsw->data.dual_emac) {
Mugunthan V N02a54162015-01-22 15:19:22 +05301709 /* In dual EMAC, reserved VLAN id should not be used for
1710 * creating VLAN interfaces as this can break the dual
1711 * EMAC port separation
1712 */
1713 int i;
1714
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001715 for (i = 0; i < cpsw->data.slaves; i++) {
1716 if (vid == cpsw->slaves[i].port_vlan)
Mugunthan V N02a54162015-01-22 15:19:22 +05301717 return -EINVAL;
1718 }
1719 }
1720
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001721 dev_info(priv->dev, "Adding vlanid %d to vlan filter\n", vid);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001722 ret = cpsw_add_vlan_ale_entry(priv, vid);
1723
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001724 pm_runtime_put(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001725 return ret;
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001726}
1727
1728static int cpsw_ndo_vlan_rx_kill_vid(struct net_device *ndev,
Patrick McHardy80d5c362013-04-19 02:04:28 +00001729 __be16 proto, u16 vid)
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001730{
1731 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03001732 struct cpsw_common *cpsw = priv->cpsw;
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001733 int ret;
1734
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001735 if (vid == cpsw->data.default_vlan)
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001736 return 0;
1737
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001738 ret = pm_runtime_get_sync(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001739 if (ret < 0) {
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001740 pm_runtime_put_noidle(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001741 return ret;
1742 }
1743
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001744 if (cpsw->data.dual_emac) {
Mugunthan V N02a54162015-01-22 15:19:22 +05301745 int i;
1746
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001747 for (i = 0; i < cpsw->data.slaves; i++) {
1748 if (vid == cpsw->slaves[i].port_vlan)
Mugunthan V N02a54162015-01-22 15:19:22 +05301749 return -EINVAL;
1750 }
1751 }
1752
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001753 dev_info(priv->dev, "removing vlanid %d from vlan filter\n", vid);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001754 ret = cpsw_ale_del_vlan(cpsw->ale, vid, 0);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001755 if (ret != 0)
1756 return ret;
1757
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001758 ret = cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr,
Grygorii Strashko61f1cef2016-04-07 15:16:43 +03001759 HOST_PORT_NUM, ALE_VLAN, vid);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001760 if (ret != 0)
1761 return ret;
1762
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001763 ret = cpsw_ale_del_mcast(cpsw->ale, priv->ndev->broadcast,
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001764 0, ALE_VLAN, vid);
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001765 pm_runtime_put(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001766 return ret;
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001767}
1768
Mugunthan V Ndf828592012-03-18 20:17:54 +00001769static const struct net_device_ops cpsw_netdev_ops = {
1770 .ndo_open = cpsw_ndo_open,
1771 .ndo_stop = cpsw_ndo_stop,
1772 .ndo_start_xmit = cpsw_ndo_start_xmit,
Mugunthan V Ndcfd8d52013-07-25 23:44:01 +05301773 .ndo_set_mac_address = cpsw_ndo_set_mac_address,
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001774 .ndo_do_ioctl = cpsw_ndo_ioctl,
Mugunthan V Ndf828592012-03-18 20:17:54 +00001775 .ndo_validate_addr = eth_validate_addr,
David S. Miller5c473ed2012-03-20 00:33:59 -04001776 .ndo_change_mtu = eth_change_mtu,
Mugunthan V Ndf828592012-03-18 20:17:54 +00001777 .ndo_tx_timeout = cpsw_ndo_tx_timeout,
Mugunthan V N5c50a852012-10-29 08:45:11 +00001778 .ndo_set_rx_mode = cpsw_ndo_set_rx_mode,
Mugunthan V Ndf828592012-03-18 20:17:54 +00001779#ifdef CONFIG_NET_POLL_CONTROLLER
1780 .ndo_poll_controller = cpsw_ndo_poll_controller,
1781#endif
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001782 .ndo_vlan_rx_add_vid = cpsw_ndo_vlan_rx_add_vid,
1783 .ndo_vlan_rx_kill_vid = cpsw_ndo_vlan_rx_kill_vid,
Mugunthan V Ndf828592012-03-18 20:17:54 +00001784};
1785
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05301786static int cpsw_get_regs_len(struct net_device *ndev)
1787{
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001788 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05301789
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001790 return cpsw->data.ale_entries * ALE_ENTRY_WORDS * sizeof(u32);
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05301791}
1792
1793static void cpsw_get_regs(struct net_device *ndev,
1794 struct ethtool_regs *regs, void *p)
1795{
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05301796 u32 *reg = p;
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001797 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05301798
1799 /* update CPSW IP version */
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001800 regs->version = cpsw->version;
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05301801
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001802 cpsw_ale_dump(cpsw->ale, reg);
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05301803}
1804
Mugunthan V Ndf828592012-03-18 20:17:54 +00001805static void cpsw_get_drvinfo(struct net_device *ndev,
1806 struct ethtool_drvinfo *info)
1807{
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03001808 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001809 struct platform_device *pdev = to_platform_device(cpsw->dev);
Jiri Pirko7826d432013-01-06 00:44:26 +00001810
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05301811 strlcpy(info->driver, "cpsw", sizeof(info->driver));
Jiri Pirko7826d432013-01-06 00:44:26 +00001812 strlcpy(info->version, "1.0", sizeof(info->version));
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001813 strlcpy(info->bus_info, pdev->name, sizeof(info->bus_info));
Mugunthan V Ndf828592012-03-18 20:17:54 +00001814}
1815
1816static u32 cpsw_get_msglevel(struct net_device *ndev)
1817{
1818 struct cpsw_priv *priv = netdev_priv(ndev);
1819 return priv->msg_enable;
1820}
1821
1822static void cpsw_set_msglevel(struct net_device *ndev, u32 value)
1823{
1824 struct cpsw_priv *priv = netdev_priv(ndev);
1825 priv->msg_enable = value;
1826}
1827
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001828static int cpsw_get_ts_info(struct net_device *ndev,
1829 struct ethtool_ts_info *info)
1830{
1831#ifdef CONFIG_TI_CPTS
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001832 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001833
1834 info->so_timestamping =
1835 SOF_TIMESTAMPING_TX_HARDWARE |
1836 SOF_TIMESTAMPING_TX_SOFTWARE |
1837 SOF_TIMESTAMPING_RX_HARDWARE |
1838 SOF_TIMESTAMPING_RX_SOFTWARE |
1839 SOF_TIMESTAMPING_SOFTWARE |
1840 SOF_TIMESTAMPING_RAW_HARDWARE;
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001841 info->phc_index = cpsw->cpts->phc_index;
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001842 info->tx_types =
1843 (1 << HWTSTAMP_TX_OFF) |
1844 (1 << HWTSTAMP_TX_ON);
1845 info->rx_filters =
1846 (1 << HWTSTAMP_FILTER_NONE) |
1847 (1 << HWTSTAMP_FILTER_PTP_V2_EVENT);
1848#else
1849 info->so_timestamping =
1850 SOF_TIMESTAMPING_TX_SOFTWARE |
1851 SOF_TIMESTAMPING_RX_SOFTWARE |
1852 SOF_TIMESTAMPING_SOFTWARE;
1853 info->phc_index = -1;
1854 info->tx_types = 0;
1855 info->rx_filters = 0;
1856#endif
1857 return 0;
1858}
1859
Mugunthan V Nd3bb9c52013-03-11 23:16:36 +00001860static int cpsw_get_settings(struct net_device *ndev,
1861 struct ethtool_cmd *ecmd)
1862{
1863 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001864 struct cpsw_common *cpsw = priv->cpsw;
1865 int slave_no = cpsw_slave_index(cpsw, priv);
Mugunthan V Nd3bb9c52013-03-11 23:16:36 +00001866
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001867 if (cpsw->slaves[slave_no].phy)
1868 return phy_ethtool_gset(cpsw->slaves[slave_no].phy, ecmd);
Mugunthan V Nd3bb9c52013-03-11 23:16:36 +00001869 else
1870 return -EOPNOTSUPP;
1871}
1872
1873static int cpsw_set_settings(struct net_device *ndev, struct ethtool_cmd *ecmd)
1874{
1875 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001876 struct cpsw_common *cpsw = priv->cpsw;
1877 int slave_no = cpsw_slave_index(cpsw, priv);
Mugunthan V Nd3bb9c52013-03-11 23:16:36 +00001878
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001879 if (cpsw->slaves[slave_no].phy)
1880 return phy_ethtool_sset(cpsw->slaves[slave_no].phy, ecmd);
Mugunthan V Nd3bb9c52013-03-11 23:16:36 +00001881 else
1882 return -EOPNOTSUPP;
1883}
1884
Matus Ujhelyid8a64422013-08-20 07:59:38 +02001885static void cpsw_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
1886{
1887 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001888 struct cpsw_common *cpsw = priv->cpsw;
1889 int slave_no = cpsw_slave_index(cpsw, priv);
Matus Ujhelyid8a64422013-08-20 07:59:38 +02001890
1891 wol->supported = 0;
1892 wol->wolopts = 0;
1893
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001894 if (cpsw->slaves[slave_no].phy)
1895 phy_ethtool_get_wol(cpsw->slaves[slave_no].phy, wol);
Matus Ujhelyid8a64422013-08-20 07:59:38 +02001896}
1897
1898static int cpsw_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
1899{
1900 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001901 struct cpsw_common *cpsw = priv->cpsw;
1902 int slave_no = cpsw_slave_index(cpsw, priv);
Matus Ujhelyid8a64422013-08-20 07:59:38 +02001903
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001904 if (cpsw->slaves[slave_no].phy)
1905 return phy_ethtool_set_wol(cpsw->slaves[slave_no].phy, wol);
Matus Ujhelyid8a64422013-08-20 07:59:38 +02001906 else
1907 return -EOPNOTSUPP;
1908}
1909
Mugunthan V N1923d6e2014-09-08 22:54:02 +05301910static void cpsw_get_pauseparam(struct net_device *ndev,
1911 struct ethtool_pauseparam *pause)
1912{
1913 struct cpsw_priv *priv = netdev_priv(ndev);
1914
1915 pause->autoneg = AUTONEG_DISABLE;
1916 pause->rx_pause = priv->rx_pause ? true : false;
1917 pause->tx_pause = priv->tx_pause ? true : false;
1918}
1919
1920static int cpsw_set_pauseparam(struct net_device *ndev,
1921 struct ethtool_pauseparam *pause)
1922{
1923 struct cpsw_priv *priv = netdev_priv(ndev);
1924 bool link;
1925
1926 priv->rx_pause = pause->rx_pause ? true : false;
1927 priv->tx_pause = pause->tx_pause ? true : false;
1928
1929 for_each_slave(priv, _cpsw_adjust_link, priv, &link);
Mugunthan V N1923d6e2014-09-08 22:54:02 +05301930 return 0;
1931}
1932
Grygorii Strashko7898b1d2016-06-24 21:23:44 +03001933static int cpsw_ethtool_op_begin(struct net_device *ndev)
1934{
1935 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03001936 struct cpsw_common *cpsw = priv->cpsw;
Grygorii Strashko7898b1d2016-06-24 21:23:44 +03001937 int ret;
1938
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001939 ret = pm_runtime_get_sync(cpsw->dev);
Grygorii Strashko7898b1d2016-06-24 21:23:44 +03001940 if (ret < 0) {
1941 cpsw_err(priv, drv, "ethtool begin failed %d\n", ret);
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001942 pm_runtime_put_noidle(cpsw->dev);
Grygorii Strashko7898b1d2016-06-24 21:23:44 +03001943 }
1944
1945 return ret;
1946}
1947
1948static void cpsw_ethtool_op_complete(struct net_device *ndev)
1949{
1950 struct cpsw_priv *priv = netdev_priv(ndev);
1951 int ret;
1952
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001953 ret = pm_runtime_put(priv->cpsw->dev);
Grygorii Strashko7898b1d2016-06-24 21:23:44 +03001954 if (ret < 0)
1955 cpsw_err(priv, drv, "ethtool complete failed %d\n", ret);
1956}
1957
Mugunthan V Ndf828592012-03-18 20:17:54 +00001958static const struct ethtool_ops cpsw_ethtool_ops = {
1959 .get_drvinfo = cpsw_get_drvinfo,
1960 .get_msglevel = cpsw_get_msglevel,
1961 .set_msglevel = cpsw_set_msglevel,
1962 .get_link = ethtool_op_get_link,
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001963 .get_ts_info = cpsw_get_ts_info,
Mugunthan V Nd3bb9c52013-03-11 23:16:36 +00001964 .get_settings = cpsw_get_settings,
1965 .set_settings = cpsw_set_settings,
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001966 .get_coalesce = cpsw_get_coalesce,
1967 .set_coalesce = cpsw_set_coalesce,
Mugunthan V Nd9718542013-07-23 15:38:17 +05301968 .get_sset_count = cpsw_get_sset_count,
1969 .get_strings = cpsw_get_strings,
1970 .get_ethtool_stats = cpsw_get_ethtool_stats,
Mugunthan V N1923d6e2014-09-08 22:54:02 +05301971 .get_pauseparam = cpsw_get_pauseparam,
1972 .set_pauseparam = cpsw_set_pauseparam,
Matus Ujhelyid8a64422013-08-20 07:59:38 +02001973 .get_wol = cpsw_get_wol,
1974 .set_wol = cpsw_set_wol,
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05301975 .get_regs_len = cpsw_get_regs_len,
1976 .get_regs = cpsw_get_regs,
Grygorii Strashko7898b1d2016-06-24 21:23:44 +03001977 .begin = cpsw_ethtool_op_begin,
1978 .complete = cpsw_ethtool_op_complete,
Mugunthan V Ndf828592012-03-18 20:17:54 +00001979};
1980
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001981static void cpsw_slave_init(struct cpsw_slave *slave, struct cpsw_common *cpsw,
Richard Cochran549985e2012-11-14 09:07:56 +00001982 u32 slave_reg_ofs, u32 sliver_reg_ofs)
Mugunthan V Ndf828592012-03-18 20:17:54 +00001983{
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001984 void __iomem *regs = cpsw->regs;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001985 int slave_num = slave->slave_num;
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001986 struct cpsw_slave_data *data = cpsw->data.slave_data + slave_num;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001987
1988 slave->data = data;
Richard Cochran549985e2012-11-14 09:07:56 +00001989 slave->regs = regs + slave_reg_ofs;
1990 slave->sliver = regs + sliver_reg_ofs;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001991 slave->port_vlan = data->dual_emac_res_vlan;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001992}
1993
David Rivshin552165b2016-04-27 21:25:25 -04001994static int cpsw_probe_dt(struct cpsw_platform_data *data,
Mugunthan V N2eb32b02012-07-30 10:17:14 +00001995 struct platform_device *pdev)
1996{
1997 struct device_node *node = pdev->dev.of_node;
1998 struct device_node *slave_node;
1999 int i = 0, ret;
2000 u32 prop;
2001
2002 if (!node)
2003 return -EINVAL;
2004
2005 if (of_property_read_u32(node, "slaves", &prop)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302006 dev_err(&pdev->dev, "Missing slaves property in the DT.\n");
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002007 return -EINVAL;
2008 }
2009 data->slaves = prop;
2010
Mugunthan V Ne86ac132013-03-11 23:16:35 +00002011 if (of_property_read_u32(node, "active_slave", &prop)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302012 dev_err(&pdev->dev, "Missing active_slave property in the DT.\n");
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302013 return -EINVAL;
Richard Cochran78ca0b22012-10-29 08:45:18 +00002014 }
Mugunthan V Ne86ac132013-03-11 23:16:35 +00002015 data->active_slave = prop;
Richard Cochran78ca0b22012-10-29 08:45:18 +00002016
Richard Cochran00ab94e2012-10-29 08:45:19 +00002017 if (of_property_read_u32(node, "cpts_clock_mult", &prop)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302018 dev_err(&pdev->dev, "Missing cpts_clock_mult property in the DT.\n");
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302019 return -EINVAL;
Richard Cochran00ab94e2012-10-29 08:45:19 +00002020 }
2021 data->cpts_clock_mult = prop;
2022
2023 if (of_property_read_u32(node, "cpts_clock_shift", &prop)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302024 dev_err(&pdev->dev, "Missing cpts_clock_shift property in the DT.\n");
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302025 return -EINVAL;
Richard Cochran00ab94e2012-10-29 08:45:19 +00002026 }
2027 data->cpts_clock_shift = prop;
2028
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302029 data->slave_data = devm_kzalloc(&pdev->dev, data->slaves
2030 * sizeof(struct cpsw_slave_data),
2031 GFP_KERNEL);
Joe Perchesb2adaca2013-02-03 17:43:58 +00002032 if (!data->slave_data)
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302033 return -ENOMEM;
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002034
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002035 if (of_property_read_u32(node, "cpdma_channels", &prop)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302036 dev_err(&pdev->dev, "Missing cpdma_channels property in the DT.\n");
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302037 return -EINVAL;
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002038 }
2039 data->channels = prop;
2040
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002041 if (of_property_read_u32(node, "ale_entries", &prop)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302042 dev_err(&pdev->dev, "Missing ale_entries property in the DT.\n");
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302043 return -EINVAL;
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002044 }
2045 data->ale_entries = prop;
2046
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002047 if (of_property_read_u32(node, "bd_ram_size", &prop)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302048 dev_err(&pdev->dev, "Missing bd_ram_size property in the DT.\n");
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302049 return -EINVAL;
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002050 }
2051 data->bd_ram_size = prop;
2052
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002053 if (of_property_read_u32(node, "mac_control", &prop)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302054 dev_err(&pdev->dev, "Missing mac_control property in the DT.\n");
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302055 return -EINVAL;
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002056 }
2057 data->mac_control = prop;
2058
Markus Pargmann281abd92013-10-04 14:44:40 +02002059 if (of_property_read_bool(node, "dual_emac"))
2060 data->dual_emac = 1;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002061
Vaibhav Hiremath1fb19aa2012-11-14 09:07:55 +00002062 /*
2063 * Populate all the child nodes here...
2064 */
2065 ret = of_platform_populate(node, NULL, NULL, &pdev->dev);
2066 /* We do not want to force this, as in some cases may not have child */
2067 if (ret)
George Cherian88c99ff2014-05-12 10:21:19 +05302068 dev_warn(&pdev->dev, "Doesn't have any child node\n");
Vaibhav Hiremath1fb19aa2012-11-14 09:07:55 +00002069
Ben Hutchings8658aaf2016-06-21 01:16:31 +01002070 for_each_available_child_of_node(node, slave_node) {
Richard Cochran549985e2012-11-14 09:07:56 +00002071 struct cpsw_slave_data *slave_data = data->slave_data + i;
2072 const void *mac_addr = NULL;
Richard Cochran549985e2012-11-14 09:07:56 +00002073 int lenp;
2074 const __be32 *parp;
Richard Cochran549985e2012-11-14 09:07:56 +00002075
Markus Pargmannf468b102013-10-04 14:44:39 +02002076 /* This is no slave child node, continue */
2077 if (strcmp(slave_node->name, "slave"))
2078 continue;
2079
David Rivshin552165b2016-04-27 21:25:25 -04002080 slave_data->phy_node = of_parse_phandle(slave_node,
2081 "phy-handle", 0);
David Rivshinf1eea5c2015-12-16 23:02:10 -05002082 parp = of_get_property(slave_node, "phy_id", &lenp);
David Rivshinae092b52016-04-27 21:38:26 -04002083 if (slave_data->phy_node) {
2084 dev_dbg(&pdev->dev,
2085 "slave[%d] using phy-handle=\"%s\"\n",
2086 i, slave_data->phy_node->full_name);
2087 } else if (of_phy_is_fixed_link(slave_node)) {
David Rivshindfc0a6d2015-12-16 23:02:11 -05002088 /* In the case of a fixed PHY, the DT node associated
2089 * to the PHY is the Ethernet MAC DT node.
2090 */
Markus Brunner1f71e8c2015-11-03 22:09:51 +01002091 ret = of_phy_register_fixed_link(slave_node);
2092 if (ret)
2093 return ret;
David Rivshin06cd6d62016-04-27 21:45:45 -04002094 slave_data->phy_node = of_node_get(slave_node);
David Rivshinf1eea5c2015-12-16 23:02:10 -05002095 } else if (parp) {
2096 u32 phyid;
2097 struct device_node *mdio_node;
2098 struct platform_device *mdio;
2099
2100 if (lenp != (sizeof(__be32) * 2)) {
2101 dev_err(&pdev->dev, "Invalid slave[%d] phy_id property\n", i);
2102 goto no_phy_slave;
2103 }
2104 mdio_node = of_find_node_by_phandle(be32_to_cpup(parp));
2105 phyid = be32_to_cpup(parp+1);
2106 mdio = of_find_device_by_node(mdio_node);
2107 of_node_put(mdio_node);
2108 if (!mdio) {
2109 dev_err(&pdev->dev, "Missing mdio platform device\n");
2110 return -EINVAL;
2111 }
2112 snprintf(slave_data->phy_id, sizeof(slave_data->phy_id),
2113 PHY_ID_FMT, mdio->name, phyid);
2114 } else {
David Rivshinae092b52016-04-27 21:38:26 -04002115 dev_err(&pdev->dev,
2116 "No slave[%d] phy_id, phy-handle, or fixed-link property\n",
2117 i);
Markus Brunner1f71e8c2015-11-03 22:09:51 +01002118 goto no_phy_slave;
2119 }
Mugunthan V N47276fc2014-10-24 18:51:33 +05302120 slave_data->phy_if = of_get_phy_mode(slave_node);
2121 if (slave_data->phy_if < 0) {
2122 dev_err(&pdev->dev, "Missing or malformed slave[%d] phy-mode property\n",
2123 i);
2124 return slave_data->phy_if;
2125 }
2126
2127no_phy_slave:
Richard Cochran549985e2012-11-14 09:07:56 +00002128 mac_addr = of_get_mac_address(slave_node);
Markus Pargmann0ba517b2014-09-29 08:53:17 +02002129 if (mac_addr) {
Richard Cochran549985e2012-11-14 09:07:56 +00002130 memcpy(slave_data->mac_addr, mac_addr, ETH_ALEN);
Markus Pargmann0ba517b2014-09-29 08:53:17 +02002131 } else {
Mugunthan V Nb6745f62015-09-21 15:56:50 +05302132 ret = ti_cm_get_macid(&pdev->dev, i,
2133 slave_data->mac_addr);
2134 if (ret)
2135 return ret;
Markus Pargmann0ba517b2014-09-29 08:53:17 +02002136 }
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002137 if (data->dual_emac) {
Mugunthan V N91c41662013-04-15 07:31:28 +00002138 if (of_property_read_u32(slave_node, "dual_emac_res_vlan",
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002139 &prop)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302140 dev_err(&pdev->dev, "Missing dual_emac_res_vlan in DT.\n");
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002141 slave_data->dual_emac_res_vlan = i+1;
George Cherian88c99ff2014-05-12 10:21:19 +05302142 dev_err(&pdev->dev, "Using %d as Reserved VLAN for %d slave\n",
2143 slave_data->dual_emac_res_vlan, i);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002144 } else {
2145 slave_data->dual_emac_res_vlan = prop;
2146 }
2147 }
2148
Richard Cochran549985e2012-11-14 09:07:56 +00002149 i++;
Mugunthan V N3a27bfa2013-12-02 12:53:39 +05302150 if (i == data->slaves)
2151 break;
Richard Cochran549985e2012-11-14 09:07:56 +00002152 }
2153
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002154 return 0;
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002155}
2156
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002157static int cpsw_probe_dual_emac(struct cpsw_priv *priv)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002158{
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002159 struct cpsw_common *cpsw = priv->cpsw;
2160 struct cpsw_platform_data *data = &cpsw->data;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002161 struct net_device *ndev;
2162 struct cpsw_priv *priv_sl2;
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +03002163 int ret = 0;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002164
2165 ndev = alloc_etherdev(sizeof(struct cpsw_priv));
2166 if (!ndev) {
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002167 dev_err(cpsw->dev, "cpsw: error allocating net_device\n");
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002168 return -ENOMEM;
2169 }
2170
2171 priv_sl2 = netdev_priv(ndev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002172 priv_sl2->cpsw = cpsw;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002173 priv_sl2->ndev = ndev;
2174 priv_sl2->dev = &ndev->dev;
2175 priv_sl2->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002176
2177 if (is_valid_ether_addr(data->slave_data[1].mac_addr)) {
2178 memcpy(priv_sl2->mac_addr, data->slave_data[1].mac_addr,
2179 ETH_ALEN);
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002180 dev_info(cpsw->dev, "cpsw: Detected MACID = %pM\n",
2181 priv_sl2->mac_addr);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002182 } else {
2183 random_ether_addr(priv_sl2->mac_addr);
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002184 dev_info(cpsw->dev, "cpsw: Random MACID = %pM\n",
2185 priv_sl2->mac_addr);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002186 }
2187 memcpy(ndev->dev_addr, priv_sl2->mac_addr, ETH_ALEN);
2188
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002189 priv_sl2->emac_port = 1;
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002190 cpsw->slaves[1].ndev = ndev;
Patrick McHardyf6469682013-04-19 02:04:27 +00002191 ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002192
2193 ndev->netdev_ops = &cpsw_netdev_ops;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00002194 ndev->ethtool_ops = &cpsw_ethtool_ops;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002195
2196 /* register the network device */
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002197 SET_NETDEV_DEV(ndev, cpsw->dev);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002198 ret = register_netdev(ndev);
2199 if (ret) {
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002200 dev_err(cpsw->dev, "cpsw: error registering net device\n");
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002201 free_netdev(ndev);
2202 ret = -ENODEV;
2203 }
2204
2205 return ret;
2206}
2207
Mugunthan V N7da11602015-08-12 15:22:53 +05302208#define CPSW_QUIRK_IRQ BIT(0)
2209
2210static struct platform_device_id cpsw_devtype[] = {
2211 {
2212 /* keep it for existing comaptibles */
2213 .name = "cpsw",
2214 .driver_data = CPSW_QUIRK_IRQ,
2215 }, {
2216 .name = "am335x-cpsw",
2217 .driver_data = CPSW_QUIRK_IRQ,
2218 }, {
2219 .name = "am4372-cpsw",
2220 .driver_data = 0,
2221 }, {
2222 .name = "dra7-cpsw",
2223 .driver_data = 0,
2224 }, {
2225 /* sentinel */
2226 }
2227};
2228MODULE_DEVICE_TABLE(platform, cpsw_devtype);
2229
2230enum ti_cpsw_type {
2231 CPSW = 0,
2232 AM335X_CPSW,
2233 AM4372_CPSW,
2234 DRA7_CPSW,
2235};
2236
2237static const struct of_device_id cpsw_of_mtable[] = {
2238 { .compatible = "ti,cpsw", .data = &cpsw_devtype[CPSW], },
2239 { .compatible = "ti,am335x-cpsw", .data = &cpsw_devtype[AM335X_CPSW], },
2240 { .compatible = "ti,am4372-cpsw", .data = &cpsw_devtype[AM4372_CPSW], },
2241 { .compatible = "ti,dra7-cpsw", .data = &cpsw_devtype[DRA7_CPSW], },
2242 { /* sentinel */ },
2243};
2244MODULE_DEVICE_TABLE(of, cpsw_of_mtable);
2245
Bill Pemberton663e12e2012-12-03 09:23:45 -05002246static int cpsw_probe(struct platform_device *pdev)
Mugunthan V Ndf828592012-03-18 20:17:54 +00002247{
Ivan Khoronzhukef4183a2016-08-10 02:22:35 +03002248 struct clk *clk;
Sebastian Siewiord1bd9ac2013-04-24 08:48:23 +00002249 struct cpsw_platform_data *data;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002250 struct net_device *ndev;
2251 struct cpsw_priv *priv;
2252 struct cpdma_params dma_params;
2253 struct cpsw_ale_params ale_params;
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302254 void __iomem *ss_regs;
2255 struct resource *res, *ss_res;
Mugunthan V N7da11602015-08-12 15:22:53 +05302256 const struct of_device_id *of_id;
Mugunthan V N1d147cc2015-09-07 15:16:44 +05302257 struct gpio_descs *mode;
Richard Cochran549985e2012-11-14 09:07:56 +00002258 u32 slave_offset, sliver_offset, slave_size;
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03002259 struct cpsw_common *cpsw;
Felipe Balbi5087b912015-01-16 10:11:11 -06002260 int ret = 0, i;
2261 int irq;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002262
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03002263 cpsw = devm_kzalloc(&pdev->dev, sizeof(struct cpsw_common), GFP_KERNEL);
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002264 cpsw->dev = &pdev->dev;
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03002265
Mugunthan V Ndf828592012-03-18 20:17:54 +00002266 ndev = alloc_etherdev(sizeof(struct cpsw_priv));
2267 if (!ndev) {
George Cherian88c99ff2014-05-12 10:21:19 +05302268 dev_err(&pdev->dev, "error allocating net_device\n");
Mugunthan V Ndf828592012-03-18 20:17:54 +00002269 return -ENOMEM;
2270 }
2271
2272 platform_set_drvdata(pdev, ndev);
2273 priv = netdev_priv(ndev);
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03002274 priv->cpsw = cpsw;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002275 priv->ndev = ndev;
2276 priv->dev = &ndev->dev;
2277 priv->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002278 cpsw->rx_packet_max = max(rx_packet_max, 128);
2279 cpsw->cpts = devm_kzalloc(&pdev->dev, sizeof(struct cpts), GFP_KERNEL);
2280 if (!cpsw->cpts) {
George Cherian88c99ff2014-05-12 10:21:19 +05302281 dev_err(&pdev->dev, "error allocating cpts\n");
Markus Pargmann4d507df2014-09-29 08:53:14 +02002282 ret = -ENOMEM;
Mugunthan V N9232b162013-02-11 09:52:19 +00002283 goto clean_ndev_ret;
2284 }
Mugunthan V Ndf828592012-03-18 20:17:54 +00002285
Mugunthan V N1d147cc2015-09-07 15:16:44 +05302286 mode = devm_gpiod_get_array_optional(&pdev->dev, "mode", GPIOD_OUT_LOW);
2287 if (IS_ERR(mode)) {
2288 ret = PTR_ERR(mode);
2289 dev_err(&pdev->dev, "gpio request failed, ret %d\n", ret);
2290 goto clean_ndev_ret;
2291 }
2292
Vaibhav Hiremath1fb19aa2012-11-14 09:07:55 +00002293 /*
2294 * This may be required here for child devices.
2295 */
2296 pm_runtime_enable(&pdev->dev);
2297
Mugunthan V N739683b2013-06-06 23:45:14 +05302298 /* Select default pin state */
2299 pinctrl_pm_select_default_state(&pdev->dev);
2300
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002301 if (cpsw_probe_dt(&cpsw->data, pdev)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302302 dev_err(&pdev->dev, "cpsw: platform data missing\n");
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002303 ret = -ENODEV;
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302304 goto clean_runtime_disable_ret;
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002305 }
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002306 data = &cpsw->data;
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002307
Mugunthan V Ndf828592012-03-18 20:17:54 +00002308 if (is_valid_ether_addr(data->slave_data[0].mac_addr)) {
2309 memcpy(priv->mac_addr, data->slave_data[0].mac_addr, ETH_ALEN);
George Cherian88c99ff2014-05-12 10:21:19 +05302310 dev_info(&pdev->dev, "Detected MACID = %pM\n", priv->mac_addr);
Mugunthan V Ndf828592012-03-18 20:17:54 +00002311 } else {
Joe Perches7efd26d2012-07-12 19:33:06 +00002312 eth_random_addr(priv->mac_addr);
George Cherian88c99ff2014-05-12 10:21:19 +05302313 dev_info(&pdev->dev, "Random MACID = %pM\n", priv->mac_addr);
Mugunthan V Ndf828592012-03-18 20:17:54 +00002314 }
2315
2316 memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
2317
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002318 cpsw->slaves = devm_kzalloc(&pdev->dev,
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302319 sizeof(struct cpsw_slave) * data->slaves,
2320 GFP_KERNEL);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002321 if (!cpsw->slaves) {
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302322 ret = -ENOMEM;
2323 goto clean_runtime_disable_ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002324 }
2325 for (i = 0; i < data->slaves; i++)
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002326 cpsw->slaves[i].slave_num = i;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002327
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002328 cpsw->slaves[0].ndev = ndev;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002329 priv->emac_port = 0;
2330
Ivan Khoronzhukef4183a2016-08-10 02:22:35 +03002331 clk = devm_clk_get(&pdev->dev, "fck");
2332 if (IS_ERR(clk)) {
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302333 dev_err(priv->dev, "fck is not found\n");
Mugunthan V Nf150bd72012-07-17 08:09:50 +00002334 ret = -ENODEV;
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302335 goto clean_runtime_disable_ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002336 }
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002337 cpsw->bus_freq_mhz = clk_get_rate(clk) / 1000000;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002338
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302339 ss_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2340 ss_regs = devm_ioremap_resource(&pdev->dev, ss_res);
2341 if (IS_ERR(ss_regs)) {
2342 ret = PTR_ERR(ss_regs);
2343 goto clean_runtime_disable_ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002344 }
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03002345 cpsw->regs = ss_regs;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002346
Mugunthan V Nf280e892013-12-11 22:09:05 -06002347 /* Need to enable clocks with runtime PM api to access module
2348 * registers
2349 */
Grygorii Strashko108a6532016-06-24 21:23:42 +03002350 ret = pm_runtime_get_sync(&pdev->dev);
2351 if (ret < 0) {
2352 pm_runtime_put_noidle(&pdev->dev);
2353 goto clean_runtime_disable_ret;
2354 }
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002355 cpsw->version = readl(&cpsw->regs->id_ver);
Mugunthan V Nf280e892013-12-11 22:09:05 -06002356 pm_runtime_put_sync(&pdev->dev);
2357
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302358 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03002359 cpsw->wr_regs = devm_ioremap_resource(&pdev->dev, res);
2360 if (IS_ERR(cpsw->wr_regs)) {
2361 ret = PTR_ERR(cpsw->wr_regs);
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302362 goto clean_runtime_disable_ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002363 }
Mugunthan V Ndf828592012-03-18 20:17:54 +00002364
2365 memset(&dma_params, 0, sizeof(dma_params));
Richard Cochran549985e2012-11-14 09:07:56 +00002366 memset(&ale_params, 0, sizeof(ale_params));
2367
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002368 switch (cpsw->version) {
Richard Cochran549985e2012-11-14 09:07:56 +00002369 case CPSW_VERSION_1:
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03002370 cpsw->host_port_regs = ss_regs + CPSW1_HOST_PORT_OFFSET;
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002371 cpsw->cpts->reg = ss_regs + CPSW1_CPTS_OFFSET;
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03002372 cpsw->hw_stats = ss_regs + CPSW1_HW_STATS;
Richard Cochran549985e2012-11-14 09:07:56 +00002373 dma_params.dmaregs = ss_regs + CPSW1_CPDMA_OFFSET;
2374 dma_params.txhdp = ss_regs + CPSW1_STATERAM_OFFSET;
2375 ale_params.ale_regs = ss_regs + CPSW1_ALE_OFFSET;
2376 slave_offset = CPSW1_SLAVE_OFFSET;
2377 slave_size = CPSW1_SLAVE_SIZE;
2378 sliver_offset = CPSW1_SLIVER_OFFSET;
2379 dma_params.desc_mem_phys = 0;
2380 break;
2381 case CPSW_VERSION_2:
Mugunthan V Nc193f362013-08-05 17:30:05 +05302382 case CPSW_VERSION_3:
Mugunthan V N926489b2013-08-12 17:11:15 +05302383 case CPSW_VERSION_4:
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03002384 cpsw->host_port_regs = ss_regs + CPSW2_HOST_PORT_OFFSET;
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002385 cpsw->cpts->reg = ss_regs + CPSW2_CPTS_OFFSET;
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03002386 cpsw->hw_stats = ss_regs + CPSW2_HW_STATS;
Richard Cochran549985e2012-11-14 09:07:56 +00002387 dma_params.dmaregs = ss_regs + CPSW2_CPDMA_OFFSET;
2388 dma_params.txhdp = ss_regs + CPSW2_STATERAM_OFFSET;
2389 ale_params.ale_regs = ss_regs + CPSW2_ALE_OFFSET;
2390 slave_offset = CPSW2_SLAVE_OFFSET;
2391 slave_size = CPSW2_SLAVE_SIZE;
2392 sliver_offset = CPSW2_SLIVER_OFFSET;
2393 dma_params.desc_mem_phys =
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302394 (u32 __force) ss_res->start + CPSW2_BD_OFFSET;
Richard Cochran549985e2012-11-14 09:07:56 +00002395 break;
2396 default:
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002397 dev_err(priv->dev, "unknown version 0x%08x\n", cpsw->version);
Richard Cochran549985e2012-11-14 09:07:56 +00002398 ret = -ENODEV;
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302399 goto clean_runtime_disable_ret;
Richard Cochran549985e2012-11-14 09:07:56 +00002400 }
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002401 for (i = 0; i < cpsw->data.slaves; i++) {
2402 struct cpsw_slave *slave = &cpsw->slaves[i];
2403
2404 cpsw_slave_init(slave, cpsw, slave_offset, sliver_offset);
Richard Cochran549985e2012-11-14 09:07:56 +00002405 slave_offset += slave_size;
2406 sliver_offset += SLIVER_SIZE;
2407 }
2408
Mugunthan V Ndf828592012-03-18 20:17:54 +00002409 dma_params.dev = &pdev->dev;
Richard Cochran549985e2012-11-14 09:07:56 +00002410 dma_params.rxthresh = dma_params.dmaregs + CPDMA_RXTHRESH;
2411 dma_params.rxfree = dma_params.dmaregs + CPDMA_RXFREE;
2412 dma_params.rxhdp = dma_params.txhdp + CPDMA_RXHDP;
2413 dma_params.txcp = dma_params.txhdp + CPDMA_TXCP;
2414 dma_params.rxcp = dma_params.txhdp + CPDMA_RXCP;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002415
2416 dma_params.num_chan = data->channels;
2417 dma_params.has_soft_reset = true;
2418 dma_params.min_packet_size = CPSW_MIN_PACKET_SIZE;
2419 dma_params.desc_mem_size = data->bd_ram_size;
2420 dma_params.desc_align = 16;
2421 dma_params.has_ext_regs = true;
Richard Cochran549985e2012-11-14 09:07:56 +00002422 dma_params.desc_hw_addr = dma_params.desc_mem_phys;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002423
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03002424 cpsw->dma = cpdma_ctlr_create(&dma_params);
2425 if (!cpsw->dma) {
Mugunthan V Ndf828592012-03-18 20:17:54 +00002426 dev_err(priv->dev, "error initializing dma\n");
2427 ret = -ENOMEM;
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302428 goto clean_runtime_disable_ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002429 }
2430
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03002431 cpsw->txch = cpdma_chan_create(cpsw->dma, tx_chan_num(0),
Mugunthan V Ndf828592012-03-18 20:17:54 +00002432 cpsw_tx_handler);
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03002433 cpsw->rxch = cpdma_chan_create(cpsw->dma, rx_chan_num(0),
Mugunthan V Ndf828592012-03-18 20:17:54 +00002434 cpsw_rx_handler);
2435
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03002436 if (WARN_ON(!cpsw->txch || !cpsw->rxch)) {
Mugunthan V Ndf828592012-03-18 20:17:54 +00002437 dev_err(priv->dev, "error initializing dma channels\n");
2438 ret = -ENOMEM;
2439 goto clean_dma_ret;
2440 }
2441
Mugunthan V Ndf828592012-03-18 20:17:54 +00002442 ale_params.dev = &ndev->dev;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002443 ale_params.ale_ageout = ale_ageout;
2444 ale_params.ale_entries = data->ale_entries;
2445 ale_params.ale_ports = data->slaves;
2446
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002447 cpsw->ale = cpsw_ale_create(&ale_params);
2448 if (!cpsw->ale) {
Mugunthan V Ndf828592012-03-18 20:17:54 +00002449 dev_err(priv->dev, "error initializing ale engine\n");
2450 ret = -ENODEV;
2451 goto clean_dma_ret;
2452 }
2453
Felipe Balbic03abd82015-01-16 10:11:12 -06002454 ndev->irq = platform_get_irq(pdev, 1);
Mugunthan V Ndf828592012-03-18 20:17:54 +00002455 if (ndev->irq < 0) {
2456 dev_err(priv->dev, "error getting irq resource\n");
Julia Lawallc1e33342015-12-26 20:12:13 +01002457 ret = ndev->irq;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002458 goto clean_ale_ret;
2459 }
2460
Mugunthan V N7da11602015-08-12 15:22:53 +05302461 of_id = of_match_device(cpsw_of_mtable, &pdev->dev);
2462 if (of_id) {
2463 pdev->id_entry = of_id->data;
2464 if (pdev->id_entry->driver_data)
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +03002465 cpsw->quirk_irq = true;
Mugunthan V N7da11602015-08-12 15:22:53 +05302466 }
2467
Felipe Balbic03abd82015-01-16 10:11:12 -06002468 /* Grab RX and TX IRQs. Note that we also have RX_THRESHOLD and
2469 * MISC IRQs which are always kept disabled with this driver so
2470 * we will not request them.
2471 *
2472 * If anyone wants to implement support for those, make sure to
2473 * first request and append them to irqs_table array.
2474 */
Daniel Mackc2b32e52014-09-04 09:00:23 +02002475
Felipe Balbic03abd82015-01-16 10:11:12 -06002476 /* RX IRQ */
Felipe Balbi5087b912015-01-16 10:11:11 -06002477 irq = platform_get_irq(pdev, 1);
Julia Lawallc1e33342015-12-26 20:12:13 +01002478 if (irq < 0) {
2479 ret = irq;
Felipe Balbi5087b912015-01-16 10:11:11 -06002480 goto clean_ale_ret;
Julia Lawallc1e33342015-12-26 20:12:13 +01002481 }
Felipe Balbi5087b912015-01-16 10:11:11 -06002482
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +03002483 cpsw->irqs_table[0] = irq;
Felipe Balbic03abd82015-01-16 10:11:12 -06002484 ret = devm_request_irq(&pdev->dev, irq, cpsw_rx_interrupt,
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +03002485 0, dev_name(&pdev->dev), cpsw);
Felipe Balbi5087b912015-01-16 10:11:11 -06002486 if (ret < 0) {
2487 dev_err(priv->dev, "error attaching irq (%d)\n", ret);
2488 goto clean_ale_ret;
2489 }
2490
Felipe Balbic03abd82015-01-16 10:11:12 -06002491 /* TX IRQ */
Felipe Balbi5087b912015-01-16 10:11:11 -06002492 irq = platform_get_irq(pdev, 2);
Julia Lawallc1e33342015-12-26 20:12:13 +01002493 if (irq < 0) {
2494 ret = irq;
Felipe Balbi5087b912015-01-16 10:11:11 -06002495 goto clean_ale_ret;
Julia Lawallc1e33342015-12-26 20:12:13 +01002496 }
Felipe Balbi5087b912015-01-16 10:11:11 -06002497
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +03002498 cpsw->irqs_table[1] = irq;
Felipe Balbic03abd82015-01-16 10:11:12 -06002499 ret = devm_request_irq(&pdev->dev, irq, cpsw_tx_interrupt,
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +03002500 0, dev_name(&pdev->dev), cpsw);
Felipe Balbi5087b912015-01-16 10:11:11 -06002501 if (ret < 0) {
2502 dev_err(priv->dev, "error attaching irq (%d)\n", ret);
2503 goto clean_ale_ret;
2504 }
Daniel Mackc2b32e52014-09-04 09:00:23 +02002505
Patrick McHardyf6469682013-04-19 02:04:27 +00002506 ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002507
2508 ndev->netdev_ops = &cpsw_netdev_ops;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00002509 ndev->ethtool_ops = &cpsw_ethtool_ops;
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +03002510 netif_napi_add(ndev, &cpsw->napi_rx, cpsw_rx_poll, CPSW_POLL_WEIGHT);
2511 netif_tx_napi_add(ndev, &cpsw->napi_tx, cpsw_tx_poll, CPSW_POLL_WEIGHT);
Mugunthan V Ndf828592012-03-18 20:17:54 +00002512
2513 /* register the network device */
2514 SET_NETDEV_DEV(ndev, &pdev->dev);
2515 ret = register_netdev(ndev);
2516 if (ret) {
2517 dev_err(priv->dev, "error registering net device\n");
2518 ret = -ENODEV;
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302519 goto clean_ale_ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002520 }
2521
Olof Johansson1a3b5052013-12-11 15:58:07 -08002522 cpsw_notice(priv, probe, "initialized device (regs %pa, irq %d)\n",
2523 &ss_res->start, ndev->irq);
Mugunthan V Ndf828592012-03-18 20:17:54 +00002524
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002525 if (cpsw->data.dual_emac) {
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002526 ret = cpsw_probe_dual_emac(priv);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002527 if (ret) {
2528 cpsw_err(priv, probe, "error probe slave 2 emac interface\n");
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302529 goto clean_ale_ret;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002530 }
2531 }
2532
Mugunthan V Ndf828592012-03-18 20:17:54 +00002533 return 0;
2534
Mugunthan V Ndf828592012-03-18 20:17:54 +00002535clean_ale_ret:
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002536 cpsw_ale_destroy(cpsw->ale);
Mugunthan V Ndf828592012-03-18 20:17:54 +00002537clean_dma_ret:
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03002538 cpdma_ctlr_destroy(cpsw->dma);
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302539clean_runtime_disable_ret:
Mugunthan V Nf150bd72012-07-17 08:09:50 +00002540 pm_runtime_disable(&pdev->dev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00002541clean_ndev_ret:
Sebastian Siewiord1bd9ac2013-04-24 08:48:23 +00002542 free_netdev(priv->ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00002543 return ret;
2544}
2545
Bill Pemberton663e12e2012-12-03 09:23:45 -05002546static int cpsw_remove(struct platform_device *pdev)
Mugunthan V Ndf828592012-03-18 20:17:54 +00002547{
2548 struct net_device *ndev = platform_get_drvdata(pdev);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002549 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Grygorii Strashko8a0b6dc2016-07-28 20:50:35 +03002550 int ret;
2551
2552 ret = pm_runtime_get_sync(&pdev->dev);
2553 if (ret < 0) {
2554 pm_runtime_put_noidle(&pdev->dev);
2555 return ret;
2556 }
Mugunthan V Ndf828592012-03-18 20:17:54 +00002557
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002558 if (cpsw->data.dual_emac)
2559 unregister_netdev(cpsw->slaves[1].ndev);
Sebastian Siewiord1bd9ac2013-04-24 08:48:23 +00002560 unregister_netdev(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00002561
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002562 cpsw_ale_destroy(cpsw->ale);
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03002563 cpdma_ctlr_destroy(cpsw->dma);
Grygorii Strashko3bf2cb32016-07-28 20:50:36 +03002564 of_platform_depopulate(&pdev->dev);
Grygorii Strashko8a0b6dc2016-07-28 20:50:35 +03002565 pm_runtime_put_sync(&pdev->dev);
2566 pm_runtime_disable(&pdev->dev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002567 if (cpsw->data.dual_emac)
2568 free_netdev(cpsw->slaves[1].ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00002569 free_netdev(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00002570 return 0;
2571}
2572
Grygorii Strashko8963a502015-02-27 13:19:45 +02002573#ifdef CONFIG_PM_SLEEP
Mugunthan V Ndf828592012-03-18 20:17:54 +00002574static int cpsw_suspend(struct device *dev)
2575{
2576 struct platform_device *pdev = to_platform_device(dev);
2577 struct net_device *ndev = platform_get_drvdata(pdev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002578 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00002579
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002580 if (cpsw->data.dual_emac) {
Mugunthan V N618073e2014-09-11 22:52:38 +05302581 int i;
Daniel Mack1e7a2e22013-11-15 08:29:16 +01002582
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002583 for (i = 0; i < cpsw->data.slaves; i++) {
2584 if (netif_running(cpsw->slaves[i].ndev))
2585 cpsw_ndo_stop(cpsw->slaves[i].ndev);
Mugunthan V N618073e2014-09-11 22:52:38 +05302586 }
2587 } else {
2588 if (netif_running(ndev))
2589 cpsw_ndo_stop(ndev);
Mugunthan V N618073e2014-09-11 22:52:38 +05302590 }
Daniel Mack1e7a2e22013-11-15 08:29:16 +01002591
Mugunthan V N739683b2013-06-06 23:45:14 +05302592 /* Select sleep pin state */
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002593 pinctrl_pm_select_sleep_state(dev);
Mugunthan V N739683b2013-06-06 23:45:14 +05302594
Mugunthan V Ndf828592012-03-18 20:17:54 +00002595 return 0;
2596}
2597
2598static int cpsw_resume(struct device *dev)
2599{
2600 struct platform_device *pdev = to_platform_device(dev);
2601 struct net_device *ndev = platform_get_drvdata(pdev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002602 struct cpsw_common *cpsw = netdev_priv(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00002603
Mugunthan V N739683b2013-06-06 23:45:14 +05302604 /* Select default pin state */
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002605 pinctrl_pm_select_default_state(dev);
Mugunthan V N739683b2013-06-06 23:45:14 +05302606
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002607 if (cpsw->data.dual_emac) {
Mugunthan V N618073e2014-09-11 22:52:38 +05302608 int i;
2609
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002610 for (i = 0; i < cpsw->data.slaves; i++) {
2611 if (netif_running(cpsw->slaves[i].ndev))
2612 cpsw_ndo_open(cpsw->slaves[i].ndev);
Mugunthan V N618073e2014-09-11 22:52:38 +05302613 }
2614 } else {
2615 if (netif_running(ndev))
2616 cpsw_ndo_open(ndev);
2617 }
Mugunthan V Ndf828592012-03-18 20:17:54 +00002618 return 0;
2619}
Grygorii Strashko8963a502015-02-27 13:19:45 +02002620#endif
Mugunthan V Ndf828592012-03-18 20:17:54 +00002621
Grygorii Strashko8963a502015-02-27 13:19:45 +02002622static SIMPLE_DEV_PM_OPS(cpsw_pm_ops, cpsw_suspend, cpsw_resume);
Mugunthan V Ndf828592012-03-18 20:17:54 +00002623
2624static struct platform_driver cpsw_driver = {
2625 .driver = {
2626 .name = "cpsw",
Mugunthan V Ndf828592012-03-18 20:17:54 +00002627 .pm = &cpsw_pm_ops,
Sachin Kamat1e5c76d2013-09-30 09:55:12 +05302628 .of_match_table = cpsw_of_mtable,
Mugunthan V Ndf828592012-03-18 20:17:54 +00002629 },
2630 .probe = cpsw_probe,
Bill Pemberton663e12e2012-12-03 09:23:45 -05002631 .remove = cpsw_remove,
Mugunthan V Ndf828592012-03-18 20:17:54 +00002632};
2633
Grygorii Strashko6fb3b6b52015-10-23 14:41:12 +03002634module_platform_driver(cpsw_driver);
Mugunthan V Ndf828592012-03-18 20:17:54 +00002635
2636MODULE_LICENSE("GPL");
2637MODULE_AUTHOR("Cyril Chemparathy <cyril@ti.com>");
2638MODULE_AUTHOR("Mugunthan V N <mugunthanvnm@ti.com>");
2639MODULE_DESCRIPTION("TI CPSW Ethernet driver");