blob: 7599895d29b8ed0e05d0da8f27cd7ea9bdc2a20f [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
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300127#define CPDMA_TX_PRIORITY_MAP 0x01234567
Mugunthan V Ndf828592012-03-18 20:17:54 +0000128
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
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300147#define CPSW_MAX_QUEUES 8
Mugunthan V Nd3bb9c52013-03-11 23:16:36 +0000148
Mugunthan V Ndf828592012-03-18 20:17:54 +0000149static int debug_level;
150module_param(debug_level, int, 0);
151MODULE_PARM_DESC(debug_level, "cpsw debug level (NETIF_MSG bits)");
152
153static int ale_ageout = 10;
154module_param(ale_ageout, int, 0);
155MODULE_PARM_DESC(ale_ageout, "cpsw ale ageout interval (seconds)");
156
157static int rx_packet_max = CPSW_MAX_PACKET_SIZE;
158module_param(rx_packet_max, int, 0);
159MODULE_PARM_DESC(rx_packet_max, "maximum receive packet size (bytes)");
160
Richard Cochran996a5c22012-10-29 08:45:12 +0000161struct cpsw_wr_regs {
Mugunthan V Ndf828592012-03-18 20:17:54 +0000162 u32 id_ver;
163 u32 soft_reset;
164 u32 control;
165 u32 int_control;
166 u32 rx_thresh_en;
167 u32 rx_en;
168 u32 tx_en;
169 u32 misc_en;
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +0000170 u32 mem_allign1[8];
171 u32 rx_thresh_stat;
172 u32 rx_stat;
173 u32 tx_stat;
174 u32 misc_stat;
175 u32 mem_allign2[8];
176 u32 rx_imax;
177 u32 tx_imax;
178
Mugunthan V Ndf828592012-03-18 20:17:54 +0000179};
180
Richard Cochran996a5c22012-10-29 08:45:12 +0000181struct cpsw_ss_regs {
Mugunthan V Ndf828592012-03-18 20:17:54 +0000182 u32 id_ver;
183 u32 control;
184 u32 soft_reset;
185 u32 stat_port_en;
186 u32 ptype;
Richard Cochranbd357af2012-10-29 08:45:13 +0000187 u32 soft_idle;
188 u32 thru_rate;
189 u32 gap_thresh;
190 u32 tx_start_wds;
191 u32 flow_control;
192 u32 vlan_ltype;
193 u32 ts_ltype;
194 u32 dlr_ltype;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000195};
196
Richard Cochran9750a3a2012-10-29 08:45:15 +0000197/* CPSW_PORT_V1 */
198#define CPSW1_MAX_BLKS 0x00 /* Maximum FIFO Blocks */
199#define CPSW1_BLK_CNT 0x04 /* FIFO Block Usage Count (Read Only) */
200#define CPSW1_TX_IN_CTL 0x08 /* Transmit FIFO Control */
201#define CPSW1_PORT_VLAN 0x0c /* VLAN Register */
202#define CPSW1_TX_PRI_MAP 0x10 /* Tx Header Priority to Switch Pri Mapping */
203#define CPSW1_TS_CTL 0x14 /* Time Sync Control */
204#define CPSW1_TS_SEQ_LTYPE 0x18 /* Time Sync Sequence ID Offset and Msg Type */
205#define CPSW1_TS_VLAN 0x1c /* Time Sync VLAN1 and VLAN2 */
206
207/* CPSW_PORT_V2 */
208#define CPSW2_CONTROL 0x00 /* Control Register */
209#define CPSW2_MAX_BLKS 0x08 /* Maximum FIFO Blocks */
210#define CPSW2_BLK_CNT 0x0c /* FIFO Block Usage Count (Read Only) */
211#define CPSW2_TX_IN_CTL 0x10 /* Transmit FIFO Control */
212#define CPSW2_PORT_VLAN 0x14 /* VLAN Register */
213#define CPSW2_TX_PRI_MAP 0x18 /* Tx Header Priority to Switch Pri Mapping */
214#define CPSW2_TS_SEQ_MTYPE 0x1c /* Time Sync Sequence ID Offset and Msg Type */
215
216/* CPSW_PORT_V1 and V2 */
217#define SA_LO 0x20 /* CPGMAC_SL Source Address Low */
218#define SA_HI 0x24 /* CPGMAC_SL Source Address High */
219#define SEND_PERCENT 0x28 /* Transmit Queue Send Percentages */
220
221/* CPSW_PORT_V2 only */
222#define RX_DSCP_PRI_MAP0 0x30 /* Rx DSCP Priority to Rx Packet Mapping */
223#define RX_DSCP_PRI_MAP1 0x34 /* Rx DSCP Priority to Rx Packet Mapping */
224#define RX_DSCP_PRI_MAP2 0x38 /* Rx DSCP Priority to Rx Packet Mapping */
225#define RX_DSCP_PRI_MAP3 0x3c /* Rx DSCP Priority to Rx Packet Mapping */
226#define RX_DSCP_PRI_MAP4 0x40 /* Rx DSCP Priority to Rx Packet Mapping */
227#define RX_DSCP_PRI_MAP5 0x44 /* Rx DSCP Priority to Rx Packet Mapping */
228#define RX_DSCP_PRI_MAP6 0x48 /* Rx DSCP Priority to Rx Packet Mapping */
229#define RX_DSCP_PRI_MAP7 0x4c /* Rx DSCP Priority to Rx Packet Mapping */
230
231/* Bit definitions for the CPSW2_CONTROL register */
232#define PASS_PRI_TAGGED (1<<24) /* Pass Priority Tagged */
233#define VLAN_LTYPE2_EN (1<<21) /* VLAN LTYPE 2 enable */
234#define VLAN_LTYPE1_EN (1<<20) /* VLAN LTYPE 1 enable */
235#define DSCP_PRI_EN (1<<16) /* DSCP Priority Enable */
236#define TS_320 (1<<14) /* Time Sync Dest Port 320 enable */
237#define TS_319 (1<<13) /* Time Sync Dest Port 319 enable */
238#define TS_132 (1<<12) /* Time Sync Dest IP Addr 132 enable */
239#define TS_131 (1<<11) /* Time Sync Dest IP Addr 131 enable */
240#define TS_130 (1<<10) /* Time Sync Dest IP Addr 130 enable */
241#define TS_129 (1<<9) /* Time Sync Dest IP Addr 129 enable */
George Cherian09c55372014-05-02 12:02:02 +0530242#define TS_TTL_NONZERO (1<<8) /* Time Sync Time To Live Non-zero enable */
243#define TS_ANNEX_F_EN (1<<6) /* Time Sync Annex F enable */
Richard Cochran9750a3a2012-10-29 08:45:15 +0000244#define TS_ANNEX_D_EN (1<<4) /* Time Sync Annex D enable */
245#define TS_LTYPE2_EN (1<<3) /* Time Sync LTYPE 2 enable */
246#define TS_LTYPE1_EN (1<<2) /* Time Sync LTYPE 1 enable */
247#define TS_TX_EN (1<<1) /* Time Sync Transmit Enable */
248#define TS_RX_EN (1<<0) /* Time Sync Receive Enable */
249
George Cherian09c55372014-05-02 12:02:02 +0530250#define CTRL_V2_TS_BITS \
251 (TS_320 | TS_319 | TS_132 | TS_131 | TS_130 | TS_129 |\
252 TS_TTL_NONZERO | TS_ANNEX_D_EN | TS_LTYPE1_EN)
Richard Cochran9750a3a2012-10-29 08:45:15 +0000253
George Cherian09c55372014-05-02 12:02:02 +0530254#define CTRL_V2_ALL_TS_MASK (CTRL_V2_TS_BITS | TS_TX_EN | TS_RX_EN)
255#define CTRL_V2_TX_TS_BITS (CTRL_V2_TS_BITS | TS_TX_EN)
256#define CTRL_V2_RX_TS_BITS (CTRL_V2_TS_BITS | TS_RX_EN)
257
258
259#define CTRL_V3_TS_BITS \
260 (TS_320 | TS_319 | TS_132 | TS_131 | TS_130 | TS_129 |\
261 TS_TTL_NONZERO | TS_ANNEX_F_EN | TS_ANNEX_D_EN |\
262 TS_LTYPE1_EN)
263
264#define CTRL_V3_ALL_TS_MASK (CTRL_V3_TS_BITS | TS_TX_EN | TS_RX_EN)
265#define CTRL_V3_TX_TS_BITS (CTRL_V3_TS_BITS | TS_TX_EN)
266#define CTRL_V3_RX_TS_BITS (CTRL_V3_TS_BITS | TS_RX_EN)
Richard Cochran9750a3a2012-10-29 08:45:15 +0000267
268/* Bit definitions for the CPSW2_TS_SEQ_MTYPE register */
269#define TS_SEQ_ID_OFFSET_SHIFT (16) /* Time Sync Sequence ID Offset */
270#define TS_SEQ_ID_OFFSET_MASK (0x3f)
271#define TS_MSG_TYPE_EN_SHIFT (0) /* Time Sync Message Type Enable */
272#define TS_MSG_TYPE_EN_MASK (0xffff)
273
274/* The PTP event messages - Sync, Delay_Req, Pdelay_Req, and Pdelay_Resp. */
275#define EVENT_MSG_BITS ((1<<0) | (1<<1) | (1<<2) | (1<<3))
Mugunthan V Ndf828592012-03-18 20:17:54 +0000276
Richard Cochran2e5b38a2012-10-29 08:45:20 +0000277/* Bit definitions for the CPSW1_TS_CTL register */
278#define CPSW_V1_TS_RX_EN BIT(0)
279#define CPSW_V1_TS_TX_EN BIT(4)
280#define CPSW_V1_MSG_TYPE_OFS 16
281
282/* Bit definitions for the CPSW1_TS_SEQ_LTYPE register */
283#define CPSW_V1_SEQ_ID_OFS_SHIFT 16
284
Mugunthan V Ndf828592012-03-18 20:17:54 +0000285struct cpsw_host_regs {
286 u32 max_blks;
287 u32 blk_cnt;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000288 u32 tx_in_ctl;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000289 u32 port_vlan;
290 u32 tx_pri_map;
291 u32 cpdma_tx_pri_map;
292 u32 cpdma_rx_chan_map;
293};
294
295struct cpsw_sliver_regs {
296 u32 id_ver;
297 u32 mac_control;
298 u32 mac_status;
299 u32 soft_reset;
300 u32 rx_maxlen;
301 u32 __reserved_0;
302 u32 rx_pause;
303 u32 tx_pause;
304 u32 __reserved_1;
305 u32 rx_pri_map;
306};
307
Mugunthan V Nd9718542013-07-23 15:38:17 +0530308struct cpsw_hw_stats {
309 u32 rxgoodframes;
310 u32 rxbroadcastframes;
311 u32 rxmulticastframes;
312 u32 rxpauseframes;
313 u32 rxcrcerrors;
314 u32 rxaligncodeerrors;
315 u32 rxoversizedframes;
316 u32 rxjabberframes;
317 u32 rxundersizedframes;
318 u32 rxfragments;
319 u32 __pad_0[2];
320 u32 rxoctets;
321 u32 txgoodframes;
322 u32 txbroadcastframes;
323 u32 txmulticastframes;
324 u32 txpauseframes;
325 u32 txdeferredframes;
326 u32 txcollisionframes;
327 u32 txsinglecollframes;
328 u32 txmultcollframes;
329 u32 txexcessivecollisions;
330 u32 txlatecollisions;
331 u32 txunderrun;
332 u32 txcarriersenseerrors;
333 u32 txoctets;
334 u32 octetframes64;
335 u32 octetframes65t127;
336 u32 octetframes128t255;
337 u32 octetframes256t511;
338 u32 octetframes512t1023;
339 u32 octetframes1024tup;
340 u32 netoctets;
341 u32 rxsofoverruns;
342 u32 rxmofoverruns;
343 u32 rxdmaoverruns;
344};
345
Mugunthan V Ndf828592012-03-18 20:17:54 +0000346struct cpsw_slave {
Richard Cochran9750a3a2012-10-29 08:45:15 +0000347 void __iomem *regs;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000348 struct cpsw_sliver_regs __iomem *sliver;
349 int slave_num;
350 u32 mac_control;
351 struct cpsw_slave_data *data;
352 struct phy_device *phy;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000353 struct net_device *ndev;
354 u32 port_vlan;
355 u32 open_stat;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000356};
357
Richard Cochran9750a3a2012-10-29 08:45:15 +0000358static inline u32 slave_read(struct cpsw_slave *slave, u32 offset)
359{
360 return __raw_readl(slave->regs + offset);
361}
362
363static inline void slave_write(struct cpsw_slave *slave, u32 val, u32 offset)
364{
365 __raw_writel(val, slave->regs + offset);
366}
367
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +0200368struct cpsw_vector {
369 struct cpdma_chan *ch;
370 int budget;
371};
372
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +0300373struct cpsw_common {
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +0300374 struct device *dev;
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300375 struct cpsw_platform_data data;
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +0300376 struct napi_struct napi_rx;
377 struct napi_struct napi_tx;
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +0300378 struct cpsw_ss_regs __iomem *regs;
379 struct cpsw_wr_regs __iomem *wr_regs;
380 u8 __iomem *hw_stats;
381 struct cpsw_host_regs __iomem *host_port_regs;
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300382 u32 version;
383 u32 coal_intvl;
384 u32 bus_freq_mhz;
385 int rx_packet_max;
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300386 struct cpsw_slave *slaves;
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +0300387 struct cpdma_ctlr *dma;
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +0200388 struct cpsw_vector txv[CPSW_MAX_QUEUES];
389 struct cpsw_vector rxv[CPSW_MAX_QUEUES];
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300390 struct cpsw_ale *ale;
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +0300391 bool quirk_irq;
392 bool rx_irq_disabled;
393 bool tx_irq_disabled;
394 u32 irqs_table[IRQ_NUM];
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300395 struct cpts *cpts;
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300396 int rx_ch_num, tx_ch_num;
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +0300397};
398
399struct cpsw_priv {
Mugunthan V Ndf828592012-03-18 20:17:54 +0000400 struct net_device *ndev;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000401 struct device *dev;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000402 u32 msg_enable;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000403 u8 mac_addr[ETH_ALEN];
Mugunthan V N1923d6e2014-09-08 22:54:02 +0530404 bool rx_pause;
405 bool tx_pause;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000406 u32 emac_port;
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +0300407 struct cpsw_common *cpsw;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000408};
409
Mugunthan V Nd9718542013-07-23 15:38:17 +0530410struct cpsw_stats {
411 char stat_string[ETH_GSTRING_LEN];
412 int type;
413 int sizeof_stat;
414 int stat_offset;
415};
416
417enum {
418 CPSW_STATS,
419 CPDMA_RX_STATS,
420 CPDMA_TX_STATS,
421};
422
423#define CPSW_STAT(m) CPSW_STATS, \
424 sizeof(((struct cpsw_hw_stats *)0)->m), \
425 offsetof(struct cpsw_hw_stats, m)
426#define CPDMA_RX_STAT(m) CPDMA_RX_STATS, \
427 sizeof(((struct cpdma_chan_stats *)0)->m), \
428 offsetof(struct cpdma_chan_stats, m)
429#define CPDMA_TX_STAT(m) CPDMA_TX_STATS, \
430 sizeof(((struct cpdma_chan_stats *)0)->m), \
431 offsetof(struct cpdma_chan_stats, m)
432
433static const struct cpsw_stats cpsw_gstrings_stats[] = {
434 { "Good Rx Frames", CPSW_STAT(rxgoodframes) },
435 { "Broadcast Rx Frames", CPSW_STAT(rxbroadcastframes) },
436 { "Multicast Rx Frames", CPSW_STAT(rxmulticastframes) },
437 { "Pause Rx Frames", CPSW_STAT(rxpauseframes) },
438 { "Rx CRC Errors", CPSW_STAT(rxcrcerrors) },
439 { "Rx Align/Code Errors", CPSW_STAT(rxaligncodeerrors) },
440 { "Oversize Rx Frames", CPSW_STAT(rxoversizedframes) },
441 { "Rx Jabbers", CPSW_STAT(rxjabberframes) },
442 { "Undersize (Short) Rx Frames", CPSW_STAT(rxundersizedframes) },
443 { "Rx Fragments", CPSW_STAT(rxfragments) },
444 { "Rx Octets", CPSW_STAT(rxoctets) },
445 { "Good Tx Frames", CPSW_STAT(txgoodframes) },
446 { "Broadcast Tx Frames", CPSW_STAT(txbroadcastframes) },
447 { "Multicast Tx Frames", CPSW_STAT(txmulticastframes) },
448 { "Pause Tx Frames", CPSW_STAT(txpauseframes) },
449 { "Deferred Tx Frames", CPSW_STAT(txdeferredframes) },
450 { "Collisions", CPSW_STAT(txcollisionframes) },
451 { "Single Collision Tx Frames", CPSW_STAT(txsinglecollframes) },
452 { "Multiple Collision Tx Frames", CPSW_STAT(txmultcollframes) },
453 { "Excessive Collisions", CPSW_STAT(txexcessivecollisions) },
454 { "Late Collisions", CPSW_STAT(txlatecollisions) },
455 { "Tx Underrun", CPSW_STAT(txunderrun) },
456 { "Carrier Sense Errors", CPSW_STAT(txcarriersenseerrors) },
457 { "Tx Octets", CPSW_STAT(txoctets) },
458 { "Rx + Tx 64 Octet Frames", CPSW_STAT(octetframes64) },
459 { "Rx + Tx 65-127 Octet Frames", CPSW_STAT(octetframes65t127) },
460 { "Rx + Tx 128-255 Octet Frames", CPSW_STAT(octetframes128t255) },
461 { "Rx + Tx 256-511 Octet Frames", CPSW_STAT(octetframes256t511) },
462 { "Rx + Tx 512-1023 Octet Frames", CPSW_STAT(octetframes512t1023) },
463 { "Rx + Tx 1024-Up Octet Frames", CPSW_STAT(octetframes1024tup) },
464 { "Net Octets", CPSW_STAT(netoctets) },
465 { "Rx Start of Frame Overruns", CPSW_STAT(rxsofoverruns) },
466 { "Rx Middle of Frame Overruns", CPSW_STAT(rxmofoverruns) },
467 { "Rx DMA Overruns", CPSW_STAT(rxdmaoverruns) },
Mugunthan V Nd9718542013-07-23 15:38:17 +0530468};
469
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300470static const struct cpsw_stats cpsw_gstrings_ch_stats[] = {
471 { "head_enqueue", CPDMA_RX_STAT(head_enqueue) },
472 { "tail_enqueue", CPDMA_RX_STAT(tail_enqueue) },
473 { "pad_enqueue", CPDMA_RX_STAT(pad_enqueue) },
474 { "misqueued", CPDMA_RX_STAT(misqueued) },
475 { "desc_alloc_fail", CPDMA_RX_STAT(desc_alloc_fail) },
476 { "pad_alloc_fail", CPDMA_RX_STAT(pad_alloc_fail) },
477 { "runt_receive_buf", CPDMA_RX_STAT(runt_receive_buff) },
478 { "runt_transmit_buf", CPDMA_RX_STAT(runt_transmit_buff) },
479 { "empty_dequeue", CPDMA_RX_STAT(empty_dequeue) },
480 { "busy_dequeue", CPDMA_RX_STAT(busy_dequeue) },
481 { "good_dequeue", CPDMA_RX_STAT(good_dequeue) },
482 { "requeue", CPDMA_RX_STAT(requeue) },
483 { "teardown_dequeue", CPDMA_RX_STAT(teardown_dequeue) },
484};
485
486#define CPSW_STATS_COMMON_LEN ARRAY_SIZE(cpsw_gstrings_stats)
487#define CPSW_STATS_CH_LEN ARRAY_SIZE(cpsw_gstrings_ch_stats)
Mugunthan V Nd9718542013-07-23 15:38:17 +0530488
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +0300489#define ndev_to_cpsw(ndev) (((struct cpsw_priv *)netdev_priv(ndev))->cpsw)
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +0300490#define napi_to_cpsw(napi) container_of(napi, struct cpsw_common, napi)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000491#define for_each_slave(priv, func, arg...) \
492 do { \
Sebastian Siewior6e6ceae2013-04-24 08:48:24 +0000493 struct cpsw_slave *slave; \
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300494 struct cpsw_common *cpsw = (priv)->cpsw; \
Sebastian Siewior6e6ceae2013-04-24 08:48:24 +0000495 int n; \
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300496 if (cpsw->data.dual_emac) \
497 (func)((cpsw)->slaves + priv->emac_port, ##arg);\
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000498 else \
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300499 for (n = cpsw->data.slaves, \
500 slave = cpsw->slaves; \
Sebastian Siewior6e6ceae2013-04-24 08:48:24 +0000501 n; n--) \
502 (func)(slave++, ##arg); \
Mugunthan V Ndf828592012-03-18 20:17:54 +0000503 } while (0)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000504
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300505#define cpsw_dual_emac_src_port_detect(cpsw, status, ndev, skb) \
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000506 do { \
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300507 if (!cpsw->data.dual_emac) \
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000508 break; \
509 if (CPDMA_RX_SOURCE_PORT(status) == 1) { \
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300510 ndev = cpsw->slaves[0].ndev; \
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000511 skb->dev = ndev; \
512 } else if (CPDMA_RX_SOURCE_PORT(status) == 2) { \
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300513 ndev = cpsw->slaves[1].ndev; \
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000514 skb->dev = ndev; \
515 } \
516 } while (0)
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300517#define cpsw_add_mcast(cpsw, priv, addr) \
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000518 do { \
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300519 if (cpsw->data.dual_emac) { \
520 struct cpsw_slave *slave = cpsw->slaves + \
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000521 priv->emac_port; \
Ivan Khoronzhuk6f1f5832016-08-10 02:22:34 +0300522 int slave_port = cpsw_get_slave_port( \
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000523 slave->slave_num); \
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300524 cpsw_ale_add_mcast(cpsw->ale, addr, \
Grygorii Strashko71a2cbb2016-04-07 15:16:44 +0300525 1 << slave_port | ALE_PORT_HOST, \
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000526 ALE_VLAN, slave->port_vlan, 0); \
527 } else { \
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300528 cpsw_ale_add_mcast(cpsw->ale, addr, \
Grygorii Strashko61f1cef2016-04-07 15:16:43 +0300529 ALE_ALL_PORTS, \
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000530 0, 0, 0); \
531 } \
532 } while (0)
533
Ivan Khoronzhuk6f1f5832016-08-10 02:22:34 +0300534static inline int cpsw_get_slave_port(u32 slave_num)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000535{
Grygorii Strashko71a2cbb2016-04-07 15:16:44 +0300536 return slave_num + 1;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000537}
Mugunthan V Ndf828592012-03-18 20:17:54 +0000538
Mugunthan V N0cd8f9c2014-01-23 00:03:12 +0530539static void cpsw_set_promiscious(struct net_device *ndev, bool enable)
540{
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300541 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
542 struct cpsw_ale *ale = cpsw->ale;
Mugunthan V N0cd8f9c2014-01-23 00:03:12 +0530543 int i;
544
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300545 if (cpsw->data.dual_emac) {
Mugunthan V N0cd8f9c2014-01-23 00:03:12 +0530546 bool flag = false;
547
548 /* Enabling promiscuous mode for one interface will be
549 * common for both the interface as the interface shares
550 * the same hardware resource.
551 */
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300552 for (i = 0; i < cpsw->data.slaves; i++)
553 if (cpsw->slaves[i].ndev->flags & IFF_PROMISC)
Mugunthan V N0cd8f9c2014-01-23 00:03:12 +0530554 flag = true;
555
556 if (!enable && flag) {
557 enable = true;
558 dev_err(&ndev->dev, "promiscuity not disabled as the other interface is still in promiscuity mode\n");
559 }
560
561 if (enable) {
562 /* Enable Bypass */
563 cpsw_ale_control_set(ale, 0, ALE_BYPASS, 1);
564
565 dev_dbg(&ndev->dev, "promiscuity enabled\n");
566 } else {
567 /* Disable Bypass */
568 cpsw_ale_control_set(ale, 0, ALE_BYPASS, 0);
569 dev_dbg(&ndev->dev, "promiscuity disabled\n");
570 }
571 } else {
572 if (enable) {
573 unsigned long timeout = jiffies + HZ;
574
Lennart Sorensen6f979eb2014-10-31 13:28:54 -0400575 /* Disable Learn for all ports (host is port 0 and slaves are port 1 and up */
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300576 for (i = 0; i <= cpsw->data.slaves; i++) {
Mugunthan V N0cd8f9c2014-01-23 00:03:12 +0530577 cpsw_ale_control_set(ale, i,
578 ALE_PORT_NOLEARN, 1);
579 cpsw_ale_control_set(ale, i,
580 ALE_PORT_NO_SA_UPDATE, 1);
581 }
582
583 /* Clear All Untouched entries */
584 cpsw_ale_control_set(ale, 0, ALE_AGEOUT, 1);
585 do {
586 cpu_relax();
587 if (cpsw_ale_control_get(ale, 0, ALE_AGEOUT))
588 break;
589 } while (time_after(timeout, jiffies));
590 cpsw_ale_control_set(ale, 0, ALE_AGEOUT, 1);
591
592 /* Clear all mcast from ALE */
Grygorii Strashko61f1cef2016-04-07 15:16:43 +0300593 cpsw_ale_flush_multicast(ale, ALE_ALL_PORTS, -1);
Mugunthan V N0cd8f9c2014-01-23 00:03:12 +0530594
595 /* Flood All Unicast Packets to Host port */
596 cpsw_ale_control_set(ale, 0, ALE_P0_UNI_FLOOD, 1);
597 dev_dbg(&ndev->dev, "promiscuity enabled\n");
598 } else {
Lennart Sorensen6f979eb2014-10-31 13:28:54 -0400599 /* Don't Flood All Unicast Packets to Host port */
Mugunthan V N0cd8f9c2014-01-23 00:03:12 +0530600 cpsw_ale_control_set(ale, 0, ALE_P0_UNI_FLOOD, 0);
601
Lennart Sorensen6f979eb2014-10-31 13:28:54 -0400602 /* Enable Learn for all ports (host is port 0 and slaves are port 1 and up */
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300603 for (i = 0; i <= cpsw->data.slaves; i++) {
Mugunthan V N0cd8f9c2014-01-23 00:03:12 +0530604 cpsw_ale_control_set(ale, i,
605 ALE_PORT_NOLEARN, 0);
606 cpsw_ale_control_set(ale, i,
607 ALE_PORT_NO_SA_UPDATE, 0);
608 }
609 dev_dbg(&ndev->dev, "promiscuity disabled\n");
610 }
611 }
612}
613
Mugunthan V N5c50a852012-10-29 08:45:11 +0000614static void cpsw_ndo_set_rx_mode(struct net_device *ndev)
615{
616 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300617 struct cpsw_common *cpsw = priv->cpsw;
Mugunthan V N25906052015-01-13 17:35:49 +0530618 int vid;
619
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300620 if (cpsw->data.dual_emac)
621 vid = cpsw->slaves[priv->emac_port].port_vlan;
Mugunthan V N25906052015-01-13 17:35:49 +0530622 else
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300623 vid = cpsw->data.default_vlan;
Mugunthan V N5c50a852012-10-29 08:45:11 +0000624
625 if (ndev->flags & IFF_PROMISC) {
626 /* Enable promiscuous mode */
Mugunthan V N0cd8f9c2014-01-23 00:03:12 +0530627 cpsw_set_promiscious(ndev, true);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300628 cpsw_ale_set_allmulti(cpsw->ale, IFF_ALLMULTI);
Mugunthan V N5c50a852012-10-29 08:45:11 +0000629 return;
Mugunthan V N0cd8f9c2014-01-23 00:03:12 +0530630 } else {
631 /* Disable promiscuous mode */
632 cpsw_set_promiscious(ndev, false);
Mugunthan V N5c50a852012-10-29 08:45:11 +0000633 }
634
Lennart Sorensen1e5c4bc2014-10-31 13:38:52 -0400635 /* Restore allmulti on vlans if necessary */
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300636 cpsw_ale_set_allmulti(cpsw->ale, priv->ndev->flags & IFF_ALLMULTI);
Lennart Sorensen1e5c4bc2014-10-31 13:38:52 -0400637
Mugunthan V N5c50a852012-10-29 08:45:11 +0000638 /* Clear all mcast from ALE */
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300639 cpsw_ale_flush_multicast(cpsw->ale, ALE_ALL_PORTS, vid);
Mugunthan V N5c50a852012-10-29 08:45:11 +0000640
641 if (!netdev_mc_empty(ndev)) {
642 struct netdev_hw_addr *ha;
643
644 /* program multicast address list into ALE register */
645 netdev_for_each_mc_addr(ha, ndev) {
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300646 cpsw_add_mcast(cpsw, priv, (u8 *)ha->addr);
Mugunthan V N5c50a852012-10-29 08:45:11 +0000647 }
648 }
649}
650
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +0300651static void cpsw_intr_enable(struct cpsw_common *cpsw)
Mugunthan V Ndf828592012-03-18 20:17:54 +0000652{
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +0300653 __raw_writel(0xFF, &cpsw->wr_regs->tx_en);
654 __raw_writel(0xFF, &cpsw->wr_regs->rx_en);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000655
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +0300656 cpdma_ctlr_int_ctrl(cpsw->dma, true);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000657 return;
658}
659
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +0300660static void cpsw_intr_disable(struct cpsw_common *cpsw)
Mugunthan V Ndf828592012-03-18 20:17:54 +0000661{
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +0300662 __raw_writel(0, &cpsw->wr_regs->tx_en);
663 __raw_writel(0, &cpsw->wr_regs->rx_en);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000664
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +0300665 cpdma_ctlr_int_ctrl(cpsw->dma, false);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000666 return;
667}
668
Olof Johansson1a3b5052013-12-11 15:58:07 -0800669static void cpsw_tx_handler(void *token, int len, int status)
Mugunthan V Ndf828592012-03-18 20:17:54 +0000670{
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300671 struct netdev_queue *txq;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000672 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 */
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300679 txq = netdev_get_tx_queue(ndev, skb_get_queue_mapping(skb));
680 if (unlikely(netif_tx_queue_stopped(txq)))
681 netif_tx_wake_queue(txq);
682
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300683 cpts_tx_timestamp(cpsw->cpts, skb);
Tobias Klauser8dc43dd2014-03-10 13:12:23 +0100684 ndev->stats.tx_packets++;
685 ndev->stats.tx_bytes += len;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000686 dev_kfree_skb_any(skb);
687}
688
Olof Johansson1a3b5052013-12-11 15:58:07 -0800689static void cpsw_rx_handler(void *token, int len, int status)
Mugunthan V Ndf828592012-03-18 20:17:54 +0000690{
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300691 struct cpdma_chan *ch;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000692 struct sk_buff *skb = token;
Sebastian Siewiorb4727e62013-04-23 07:31:39 +0000693 struct sk_buff *new_skb;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000694 struct net_device *ndev = skb->dev;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000695 int ret = 0;
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300696 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000697
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300698 cpsw_dual_emac_src_port_detect(cpsw, status, ndev, skb);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000699
Mugunthan V N16e5c572014-04-10 14:23:23 +0530700 if (unlikely(status < 0) || unlikely(!netif_running(ndev))) {
Mugunthan V Na0e2c822014-09-10 16:38:09 +0530701 bool ndev_status = false;
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300702 struct cpsw_slave *slave = cpsw->slaves;
Mugunthan V Na0e2c822014-09-10 16:38:09 +0530703 int n;
704
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300705 if (cpsw->data.dual_emac) {
Mugunthan V Na0e2c822014-09-10 16:38:09 +0530706 /* In dual emac mode check for all interfaces */
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300707 for (n = cpsw->data.slaves; n; n--, slave++)
Mugunthan V Na0e2c822014-09-10 16:38:09 +0530708 if (netif_running(slave->ndev))
709 ndev_status = true;
710 }
711
712 if (ndev_status && (status >= 0)) {
713 /* The packet received is for the interface which
714 * is already down and the other interface is up
Joe Perchesdbedd442015-03-06 20:49:12 -0800715 * and running, instead of freeing which results
Mugunthan V Na0e2c822014-09-10 16:38:09 +0530716 * in reducing of the number of rx descriptor in
717 * DMA engine, requeue skb back to cpdma.
718 */
719 new_skb = skb;
720 goto requeue;
721 }
722
Sebastian Siewiorb4727e62013-04-23 07:31:39 +0000723 /* the interface is going down, skbs are purged */
Mugunthan V Ndf828592012-03-18 20:17:54 +0000724 dev_kfree_skb_any(skb);
725 return;
726 }
Sebastian Siewiorb4727e62013-04-23 07:31:39 +0000727
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300728 new_skb = netdev_alloc_skb_ip_align(ndev, cpsw->rx_packet_max);
Sebastian Siewiorb4727e62013-04-23 07:31:39 +0000729 if (new_skb) {
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300730 skb_copy_queue_mapping(new_skb, skb);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000731 skb_put(skb, len);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300732 cpts_rx_timestamp(cpsw->cpts, skb);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000733 skb->protocol = eth_type_trans(skb, ndev);
734 netif_receive_skb(skb);
Tobias Klauser8dc43dd2014-03-10 13:12:23 +0100735 ndev->stats.rx_bytes += len;
736 ndev->stats.rx_packets++;
Grygorii Strashko254a49d2016-08-09 15:09:44 +0300737 kmemleak_not_leak(new_skb);
Sebastian Siewiorb4727e62013-04-23 07:31:39 +0000738 } else {
Tobias Klauser8dc43dd2014-03-10 13:12:23 +0100739 ndev->stats.rx_dropped++;
Sebastian Siewiorb4727e62013-04-23 07:31:39 +0000740 new_skb = skb;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000741 }
742
Mugunthan V Na0e2c822014-09-10 16:38:09 +0530743requeue:
Ivan Khoronzhukce52c742016-08-22 21:18:28 +0300744 if (netif_dormant(ndev)) {
745 dev_kfree_skb_any(new_skb);
746 return;
747 }
748
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +0200749 ch = cpsw->rxv[skb_get_queue_mapping(new_skb)].ch;
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300750 ret = cpdma_chan_submit(ch, new_skb, new_skb->data,
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +0300751 skb_tailroom(new_skb), 0);
Sebastian Siewiorb4727e62013-04-23 07:31:39 +0000752 if (WARN_ON(ret < 0))
753 dev_kfree_skb_any(new_skb);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000754}
755
Ivan Khoronzhuk48e0a832016-12-06 03:45:00 +0200756/* split budget depending on channel rates */
757static void cpsw_split_budget(struct net_device *ndev)
758{
759 struct cpsw_priv *priv = netdev_priv(ndev);
760 struct cpsw_common *cpsw = priv->cpsw;
761 struct cpsw_vector *txv = cpsw->txv;
762 u32 consumed_rate, bigest_rate = 0;
763 int budget, bigest_rate_ch = 0;
764 struct cpsw_slave *slave;
765 int i, rlim_ch_num = 0;
766 u32 ch_rate, max_rate;
767 int ch_budget = 0;
768
769 if (cpsw->data.dual_emac)
770 slave = &cpsw->slaves[priv->emac_port];
771 else
772 slave = &cpsw->slaves[cpsw->data.active_slave];
773
774 max_rate = slave->phy->speed * 1000;
775
776 consumed_rate = 0;
777 for (i = 0; i < cpsw->tx_ch_num; i++) {
778 ch_rate = cpdma_chan_get_rate(txv[i].ch);
779 if (!ch_rate)
780 continue;
781
782 rlim_ch_num++;
783 consumed_rate += ch_rate;
784 }
785
786 if (cpsw->tx_ch_num == rlim_ch_num) {
787 max_rate = consumed_rate;
788 } else {
789 ch_budget = (consumed_rate * CPSW_POLL_WEIGHT) / max_rate;
790 ch_budget = (CPSW_POLL_WEIGHT - ch_budget) /
791 (cpsw->tx_ch_num - rlim_ch_num);
792 bigest_rate = (max_rate - consumed_rate) /
793 (cpsw->tx_ch_num - rlim_ch_num);
794 }
795
796 /* split tx budget */
797 budget = CPSW_POLL_WEIGHT;
798 for (i = 0; i < cpsw->tx_ch_num; i++) {
799 ch_rate = cpdma_chan_get_rate(txv[i].ch);
800 if (ch_rate) {
801 txv[i].budget = (ch_rate * CPSW_POLL_WEIGHT) / max_rate;
802 if (!txv[i].budget)
803 txv[i].budget = 1;
804 if (ch_rate > bigest_rate) {
805 bigest_rate_ch = i;
806 bigest_rate = ch_rate;
807 }
808 } else {
809 txv[i].budget = ch_budget;
810 if (!bigest_rate_ch)
811 bigest_rate_ch = i;
812 }
813
814 budget -= txv[i].budget;
815 }
816
817 if (budget)
818 txv[bigest_rate_ch].budget += budget;
819
820 /* split rx budget */
821 budget = CPSW_POLL_WEIGHT;
822 ch_budget = budget / cpsw->rx_ch_num;
823 for (i = 0; i < cpsw->rx_ch_num; i++) {
824 cpsw->rxv[i].budget = ch_budget;
825 budget -= ch_budget;
826 }
827
828 if (budget)
829 cpsw->rxv[0].budget += budget;
830}
831
Felipe Balbic03abd82015-01-16 10:11:12 -0600832static irqreturn_t cpsw_tx_interrupt(int irq, void *dev_id)
Mugunthan V Ndf828592012-03-18 20:17:54 +0000833{
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +0300834 struct cpsw_common *cpsw = dev_id;
Felipe Balbi7ce67a32015-01-02 16:15:59 -0600835
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +0300836 writel(0, &cpsw->wr_regs->tx_en);
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +0300837 cpdma_ctlr_eoi(cpsw->dma, CPDMA_EOI_TX);
Felipe Balbic03abd82015-01-16 10:11:12 -0600838
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +0300839 if (cpsw->quirk_irq) {
840 disable_irq_nosync(cpsw->irqs_table[1]);
841 cpsw->tx_irq_disabled = true;
Mugunthan V N7da11602015-08-12 15:22:53 +0530842 }
843
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +0300844 napi_schedule(&cpsw->napi_tx);
Felipe Balbic03abd82015-01-16 10:11:12 -0600845 return IRQ_HANDLED;
846}
847
848static irqreturn_t cpsw_rx_interrupt(int irq, void *dev_id)
849{
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +0300850 struct cpsw_common *cpsw = dev_id;
Felipe Balbic03abd82015-01-16 10:11:12 -0600851
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +0300852 cpdma_ctlr_eoi(cpsw->dma, CPDMA_EOI_RX);
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +0300853 writel(0, &cpsw->wr_regs->rx_en);
Sebastian Siewiorfd51cf12013-04-23 07:31:37 +0000854
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +0300855 if (cpsw->quirk_irq) {
856 disable_irq_nosync(cpsw->irqs_table[0]);
857 cpsw->rx_irq_disabled = true;
Mugunthan V N7da11602015-08-12 15:22:53 +0530858 }
859
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +0300860 napi_schedule(&cpsw->napi_rx);
Mugunthan V Nd354eb82015-08-04 16:06:19 +0530861 return IRQ_HANDLED;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000862}
863
Mugunthan V N32a74322015-08-04 16:06:20 +0530864static int cpsw_tx_poll(struct napi_struct *napi_tx, int budget)
Mugunthan V Ndf828592012-03-18 20:17:54 +0000865{
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300866 u32 ch_map;
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +0200867 int num_tx, cur_budget, ch;
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +0300868 struct cpsw_common *cpsw = napi_to_cpsw(napi_tx);
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +0200869 struct cpsw_vector *txv;
Mugunthan V N32a74322015-08-04 16:06:20 +0530870
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300871 /* process every unprocessed channel */
872 ch_map = cpdma_ctrl_txchs_state(cpsw->dma);
Ivan Khoronzhuk342934a2016-11-29 17:00:50 +0200873 for (ch = 0, num_tx = 0; ch_map; ch_map >>= 1, ch++) {
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300874 if (!(ch_map & 0x01))
875 continue;
876
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +0200877 txv = &cpsw->txv[ch];
878 if (unlikely(txv->budget > budget - num_tx))
879 cur_budget = budget - num_tx;
880 else
881 cur_budget = txv->budget;
882
883 num_tx += cpdma_chan_process(txv->ch, cur_budget);
Ivan Khoronzhuk342934a2016-11-29 17:00:50 +0200884 if (num_tx >= budget)
885 break;
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300886 }
887
Mugunthan V N32a74322015-08-04 16:06:20 +0530888 if (num_tx < budget) {
889 napi_complete(napi_tx);
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +0300890 writel(0xff, &cpsw->wr_regs->tx_en);
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +0300891 if (cpsw->quirk_irq && cpsw->tx_irq_disabled) {
892 cpsw->tx_irq_disabled = false;
893 enable_irq(cpsw->irqs_table[1]);
Mugunthan V N7da11602015-08-12 15:22:53 +0530894 }
Mugunthan V N32a74322015-08-04 16:06:20 +0530895 }
896
Mugunthan V N32a74322015-08-04 16:06:20 +0530897 return num_tx;
898}
899
900static int cpsw_rx_poll(struct napi_struct *napi_rx, int budget)
901{
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300902 u32 ch_map;
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +0200903 int num_rx, cur_budget, ch;
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +0300904 struct cpsw_common *cpsw = napi_to_cpsw(napi_rx);
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +0200905 struct cpsw_vector *rxv;
Mugunthan V N510a1e722013-02-17 22:19:20 +0000906
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300907 /* process every unprocessed channel */
908 ch_map = cpdma_ctrl_rxchs_state(cpsw->dma);
Ivan Khoronzhuk342934a2016-11-29 17:00:50 +0200909 for (ch = 0, num_rx = 0; ch_map; ch_map >>= 1, ch++) {
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300910 if (!(ch_map & 0x01))
911 continue;
912
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +0200913 rxv = &cpsw->rxv[ch];
914 if (unlikely(rxv->budget > budget - num_rx))
915 cur_budget = budget - num_rx;
916 else
917 cur_budget = rxv->budget;
918
919 num_rx += cpdma_chan_process(rxv->ch, cur_budget);
Ivan Khoronzhuk342934a2016-11-29 17:00:50 +0200920 if (num_rx >= budget)
921 break;
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300922 }
923
Mugunthan V N510a1e722013-02-17 22:19:20 +0000924 if (num_rx < budget) {
Mugunthan V N32a74322015-08-04 16:06:20 +0530925 napi_complete(napi_rx);
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +0300926 writel(0xff, &cpsw->wr_regs->rx_en);
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +0300927 if (cpsw->quirk_irq && cpsw->rx_irq_disabled) {
928 cpsw->rx_irq_disabled = false;
929 enable_irq(cpsw->irqs_table[0]);
Mugunthan V N7da11602015-08-12 15:22:53 +0530930 }
Mugunthan V N510a1e722013-02-17 22:19:20 +0000931 }
Mugunthan V Ndf828592012-03-18 20:17:54 +0000932
Mugunthan V Ndf828592012-03-18 20:17:54 +0000933 return num_rx;
934}
935
936static inline void soft_reset(const char *module, void __iomem *reg)
937{
938 unsigned long timeout = jiffies + HZ;
939
940 __raw_writel(1, reg);
941 do {
942 cpu_relax();
943 } while ((__raw_readl(reg) & 1) && time_after(timeout, jiffies));
944
945 WARN(__raw_readl(reg) & 1, "failed to soft-reset %s\n", module);
946}
947
948#define mac_hi(mac) (((mac)[0] << 0) | ((mac)[1] << 8) | \
949 ((mac)[2] << 16) | ((mac)[3] << 24))
950#define mac_lo(mac) (((mac)[4] << 0) | ((mac)[5] << 8))
951
952static void cpsw_set_slave_mac(struct cpsw_slave *slave,
953 struct cpsw_priv *priv)
954{
Richard Cochran9750a3a2012-10-29 08:45:15 +0000955 slave_write(slave, mac_hi(priv->mac_addr), SA_HI);
956 slave_write(slave, mac_lo(priv->mac_addr), SA_LO);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000957}
958
959static void _cpsw_adjust_link(struct cpsw_slave *slave,
960 struct cpsw_priv *priv, bool *link)
961{
962 struct phy_device *phy = slave->phy;
963 u32 mac_control = 0;
964 u32 slave_port;
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300965 struct cpsw_common *cpsw = priv->cpsw;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000966
967 if (!phy)
968 return;
969
Ivan Khoronzhuk6f1f5832016-08-10 02:22:34 +0300970 slave_port = cpsw_get_slave_port(slave->slave_num);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000971
972 if (phy->link) {
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +0300973 mac_control = cpsw->data.mac_control;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000974
975 /* enable forwarding */
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +0300976 cpsw_ale_control_set(cpsw->ale, slave_port,
Mugunthan V Ndf828592012-03-18 20:17:54 +0000977 ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
978
979 if (phy->speed == 1000)
980 mac_control |= BIT(7); /* GIGABITEN */
981 if (phy->duplex)
982 mac_control |= BIT(0); /* FULLDUPLEXEN */
Daniel Mack342b7b72012-09-27 09:19:34 +0000983
984 /* set speed_in input in case RMII mode is used in 100Mbps */
985 if (phy->speed == 100)
986 mac_control |= BIT(15);
Mugunthan V Na81d8762013-12-13 18:42:55 +0530987 else if (phy->speed == 10)
988 mac_control |= BIT(18); /* In Band mode */
Daniel Mack342b7b72012-09-27 09:19:34 +0000989
Mugunthan V N1923d6e2014-09-08 22:54:02 +0530990 if (priv->rx_pause)
991 mac_control |= BIT(3);
992
993 if (priv->tx_pause)
994 mac_control |= BIT(4);
995
Mugunthan V Ndf828592012-03-18 20:17:54 +0000996 *link = true;
997 } else {
998 mac_control = 0;
999 /* disable forwarding */
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001000 cpsw_ale_control_set(cpsw->ale, slave_port,
Mugunthan V Ndf828592012-03-18 20:17:54 +00001001 ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
1002 }
1003
1004 if (mac_control != slave->mac_control) {
1005 phy_print_status(phy);
1006 __raw_writel(mac_control, &slave->sliver->mac_control);
1007 }
1008
1009 slave->mac_control = mac_control;
1010}
1011
1012static void cpsw_adjust_link(struct net_device *ndev)
1013{
1014 struct cpsw_priv *priv = netdev_priv(ndev);
1015 bool link = false;
1016
1017 for_each_slave(priv, _cpsw_adjust_link, priv, &link);
1018
1019 if (link) {
Ivan Khoronzhuk48e0a832016-12-06 03:45:00 +02001020 cpsw_split_budget(priv->ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001021 netif_carrier_on(ndev);
1022 if (netif_running(ndev))
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001023 netif_tx_wake_all_queues(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001024 } else {
1025 netif_carrier_off(ndev);
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001026 netif_tx_stop_all_queues(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001027 }
1028}
1029
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001030static int cpsw_get_coalesce(struct net_device *ndev,
1031 struct ethtool_coalesce *coal)
1032{
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001033 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001034
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001035 coal->rx_coalesce_usecs = cpsw->coal_intvl;
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001036 return 0;
1037}
1038
1039static int cpsw_set_coalesce(struct net_device *ndev,
1040 struct ethtool_coalesce *coal)
1041{
1042 struct cpsw_priv *priv = netdev_priv(ndev);
1043 u32 int_ctrl;
1044 u32 num_interrupts = 0;
1045 u32 prescale = 0;
1046 u32 addnl_dvdr = 1;
1047 u32 coal_intvl = 0;
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001048 struct cpsw_common *cpsw = priv->cpsw;
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001049
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001050 coal_intvl = coal->rx_coalesce_usecs;
1051
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001052 int_ctrl = readl(&cpsw->wr_regs->int_control);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001053 prescale = cpsw->bus_freq_mhz * 4;
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001054
Mugunthan V Na84bc2a2014-07-15 20:26:53 +05301055 if (!coal->rx_coalesce_usecs) {
1056 int_ctrl &= ~(CPSW_INTPRESCALE_MASK | CPSW_INTPACEEN);
1057 goto update_return;
1058 }
1059
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001060 if (coal_intvl < CPSW_CMINTMIN_INTVL)
1061 coal_intvl = CPSW_CMINTMIN_INTVL;
1062
1063 if (coal_intvl > CPSW_CMINTMAX_INTVL) {
1064 /* Interrupt pacer works with 4us Pulse, we can
1065 * throttle further by dilating the 4us pulse.
1066 */
1067 addnl_dvdr = CPSW_INTPRESCALE_MASK / prescale;
1068
1069 if (addnl_dvdr > 1) {
1070 prescale *= addnl_dvdr;
1071 if (coal_intvl > (CPSW_CMINTMAX_INTVL * addnl_dvdr))
1072 coal_intvl = (CPSW_CMINTMAX_INTVL
1073 * addnl_dvdr);
1074 } else {
1075 addnl_dvdr = 1;
1076 coal_intvl = CPSW_CMINTMAX_INTVL;
1077 }
1078 }
1079
1080 num_interrupts = (1000 * addnl_dvdr) / coal_intvl;
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001081 writel(num_interrupts, &cpsw->wr_regs->rx_imax);
1082 writel(num_interrupts, &cpsw->wr_regs->tx_imax);
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001083
1084 int_ctrl |= CPSW_INTPACEEN;
1085 int_ctrl &= (~CPSW_INTPRESCALE_MASK);
1086 int_ctrl |= (prescale & CPSW_INTPRESCALE_MASK);
Mugunthan V Na84bc2a2014-07-15 20:26:53 +05301087
1088update_return:
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001089 writel(int_ctrl, &cpsw->wr_regs->int_control);
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001090
1091 cpsw_notice(priv, timer, "Set coalesce to %d usecs.\n", coal_intvl);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001092 cpsw->coal_intvl = coal_intvl;
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001093
1094 return 0;
1095}
1096
Mugunthan V Nd9718542013-07-23 15:38:17 +05301097static int cpsw_get_sset_count(struct net_device *ndev, int sset)
1098{
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001099 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1100
Mugunthan V Nd9718542013-07-23 15:38:17 +05301101 switch (sset) {
1102 case ETH_SS_STATS:
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001103 return (CPSW_STATS_COMMON_LEN +
1104 (cpsw->rx_ch_num + cpsw->tx_ch_num) *
1105 CPSW_STATS_CH_LEN);
Mugunthan V Nd9718542013-07-23 15:38:17 +05301106 default:
1107 return -EOPNOTSUPP;
1108 }
1109}
1110
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001111static void cpsw_add_ch_strings(u8 **p, int ch_num, int rx_dir)
1112{
1113 int ch_stats_len;
1114 int line;
1115 int i;
1116
1117 ch_stats_len = CPSW_STATS_CH_LEN * ch_num;
1118 for (i = 0; i < ch_stats_len; i++) {
1119 line = i % CPSW_STATS_CH_LEN;
1120 snprintf(*p, ETH_GSTRING_LEN,
1121 "%s DMA chan %d: %s", rx_dir ? "Rx" : "Tx",
1122 i / CPSW_STATS_CH_LEN,
1123 cpsw_gstrings_ch_stats[line].stat_string);
1124 *p += ETH_GSTRING_LEN;
1125 }
1126}
1127
Mugunthan V Nd9718542013-07-23 15:38:17 +05301128static void cpsw_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
1129{
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001130 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Mugunthan V Nd9718542013-07-23 15:38:17 +05301131 u8 *p = data;
1132 int i;
1133
1134 switch (stringset) {
1135 case ETH_SS_STATS:
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001136 for (i = 0; i < CPSW_STATS_COMMON_LEN; i++) {
Mugunthan V Nd9718542013-07-23 15:38:17 +05301137 memcpy(p, cpsw_gstrings_stats[i].stat_string,
1138 ETH_GSTRING_LEN);
1139 p += ETH_GSTRING_LEN;
1140 }
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001141
1142 cpsw_add_ch_strings(&p, cpsw->rx_ch_num, 1);
1143 cpsw_add_ch_strings(&p, cpsw->tx_ch_num, 0);
Mugunthan V Nd9718542013-07-23 15:38:17 +05301144 break;
1145 }
1146}
1147
1148static void cpsw_get_ethtool_stats(struct net_device *ndev,
1149 struct ethtool_stats *stats, u64 *data)
1150{
Mugunthan V Nd9718542013-07-23 15:38:17 +05301151 u8 *p;
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001152 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001153 struct cpdma_chan_stats ch_stats;
1154 int i, l, ch;
Mugunthan V Nd9718542013-07-23 15:38:17 +05301155
1156 /* Collect Davinci CPDMA stats for Rx and Tx Channel */
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001157 for (l = 0; l < CPSW_STATS_COMMON_LEN; l++)
1158 data[l] = readl(cpsw->hw_stats +
1159 cpsw_gstrings_stats[l].stat_offset);
Mugunthan V Nd9718542013-07-23 15:38:17 +05301160
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001161 for (ch = 0; ch < cpsw->rx_ch_num; ch++) {
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02001162 cpdma_chan_get_stats(cpsw->rxv[ch].ch, &ch_stats);
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001163 for (i = 0; i < CPSW_STATS_CH_LEN; i++, l++) {
1164 p = (u8 *)&ch_stats +
1165 cpsw_gstrings_ch_stats[i].stat_offset;
1166 data[l] = *(u32 *)p;
1167 }
1168 }
Mugunthan V Nd9718542013-07-23 15:38:17 +05301169
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001170 for (ch = 0; ch < cpsw->tx_ch_num; ch++) {
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02001171 cpdma_chan_get_stats(cpsw->txv[ch].ch, &ch_stats);
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001172 for (i = 0; i < CPSW_STATS_CH_LEN; i++, l++) {
1173 p = (u8 *)&ch_stats +
1174 cpsw_gstrings_ch_stats[i].stat_offset;
1175 data[l] = *(u32 *)p;
Mugunthan V Nd9718542013-07-23 15:38:17 +05301176 }
1177 }
1178}
1179
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001180static int cpsw_common_res_usage_state(struct cpsw_common *cpsw)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001181{
1182 u32 i;
1183 u32 usage_count = 0;
1184
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001185 if (!cpsw->data.dual_emac)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001186 return 0;
1187
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001188 for (i = 0; i < cpsw->data.slaves; i++)
1189 if (cpsw->slaves[i].open_stat)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001190 usage_count++;
1191
1192 return usage_count;
1193}
1194
Ivan Khoronzhuk27e9e102016-08-10 02:22:32 +03001195static inline int cpsw_tx_packet_submit(struct cpsw_priv *priv,
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001196 struct sk_buff *skb,
1197 struct cpdma_chan *txch)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001198{
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001199 struct cpsw_common *cpsw = priv->cpsw;
1200
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001201 return cpdma_chan_submit(txch, skb, skb->data, skb->len,
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001202 priv->emac_port + cpsw->data.dual_emac);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001203}
1204
1205static inline void cpsw_add_dual_emac_def_ale_entries(
1206 struct cpsw_priv *priv, struct cpsw_slave *slave,
1207 u32 slave_port)
1208{
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001209 struct cpsw_common *cpsw = priv->cpsw;
Grygorii Strashko71a2cbb2016-04-07 15:16:44 +03001210 u32 port_mask = 1 << slave_port | ALE_PORT_HOST;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001211
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001212 if (cpsw->version == CPSW_VERSION_1)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001213 slave_write(slave, slave->port_vlan, CPSW1_PORT_VLAN);
1214 else
1215 slave_write(slave, slave->port_vlan, CPSW2_PORT_VLAN);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001216 cpsw_ale_add_vlan(cpsw->ale, slave->port_vlan, port_mask,
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001217 port_mask, port_mask, 0);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001218 cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001219 port_mask, ALE_VLAN, slave->port_vlan, 0);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001220 cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr,
1221 HOST_PORT_NUM, ALE_VLAN |
1222 ALE_SECURE, slave->port_vlan);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001223}
1224
Daniel Mack1e7a2e22013-11-15 08:29:16 +01001225static void soft_reset_slave(struct cpsw_slave *slave)
Mugunthan V Ndf828592012-03-18 20:17:54 +00001226{
1227 char name[32];
Daniel Mack1e7a2e22013-11-15 08:29:16 +01001228
1229 snprintf(name, sizeof(name), "slave-%d", slave->slave_num);
1230 soft_reset(name, &slave->sliver->soft_reset);
1231}
1232
1233static void cpsw_slave_open(struct cpsw_slave *slave, struct cpsw_priv *priv)
1234{
Mugunthan V Ndf828592012-03-18 20:17:54 +00001235 u32 slave_port;
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03001236 struct cpsw_common *cpsw = priv->cpsw;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001237
Daniel Mack1e7a2e22013-11-15 08:29:16 +01001238 soft_reset_slave(slave);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001239
1240 /* setup priority mapping */
1241 __raw_writel(RX_PRIORITY_MAPPING, &slave->sliver->rx_pri_map);
Richard Cochran9750a3a2012-10-29 08:45:15 +00001242
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001243 switch (cpsw->version) {
Richard Cochran9750a3a2012-10-29 08:45:15 +00001244 case CPSW_VERSION_1:
1245 slave_write(slave, TX_PRIORITY_MAPPING, CPSW1_TX_PRI_MAP);
1246 break;
1247 case CPSW_VERSION_2:
Mugunthan V Nc193f362013-08-05 17:30:05 +05301248 case CPSW_VERSION_3:
Mugunthan V N926489b2013-08-12 17:11:15 +05301249 case CPSW_VERSION_4:
Richard Cochran9750a3a2012-10-29 08:45:15 +00001250 slave_write(slave, TX_PRIORITY_MAPPING, CPSW2_TX_PRI_MAP);
1251 break;
1252 }
Mugunthan V Ndf828592012-03-18 20:17:54 +00001253
1254 /* setup max packet size, and mac address */
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001255 __raw_writel(cpsw->rx_packet_max, &slave->sliver->rx_maxlen);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001256 cpsw_set_slave_mac(slave, priv);
1257
1258 slave->mac_control = 0; /* no link yet */
1259
Ivan Khoronzhuk6f1f5832016-08-10 02:22:34 +03001260 slave_port = cpsw_get_slave_port(slave->slave_num);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001261
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001262 if (cpsw->data.dual_emac)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001263 cpsw_add_dual_emac_def_ale_entries(priv, slave, slave_port);
1264 else
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001265 cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001266 1 << slave_port, 0, 0, ALE_MCAST_FWD_2);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001267
David Rivshind733f7542016-04-27 21:32:31 -04001268 if (slave->data->phy_node) {
David Rivshin552165b2016-04-27 21:25:25 -04001269 slave->phy = of_phy_connect(priv->ndev, slave->data->phy_node,
Heiko Schocher9e42f712015-10-17 06:04:35 +02001270 &cpsw_adjust_link, 0, slave->data->phy_if);
David Rivshind733f7542016-04-27 21:32:31 -04001271 if (!slave->phy) {
1272 dev_err(priv->dev, "phy \"%s\" not found on slave %d\n",
1273 slave->data->phy_node->full_name,
1274 slave->slave_num);
1275 return;
1276 }
1277 } else {
Heiko Schocher9e42f712015-10-17 06:04:35 +02001278 slave->phy = phy_connect(priv->ndev, slave->data->phy_id,
Florian Fainellif9a8f832013-01-14 00:52:52 +00001279 &cpsw_adjust_link, slave->data->phy_if);
David Rivshind733f7542016-04-27 21:32:31 -04001280 if (IS_ERR(slave->phy)) {
1281 dev_err(priv->dev,
1282 "phy \"%s\" not found on slave %d, err %ld\n",
1283 slave->data->phy_id, slave->slave_num,
1284 PTR_ERR(slave->phy));
1285 slave->phy = NULL;
1286 return;
1287 }
Mugunthan V Ndf828592012-03-18 20:17:54 +00001288 }
David Rivshind733f7542016-04-27 21:32:31 -04001289
1290 phy_attached_info(slave->phy);
1291
1292 phy_start(slave->phy);
1293
1294 /* Configure GMII_SEL register */
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001295 cpsw_phy_sel(cpsw->dev, slave->phy->interface, slave->slave_num);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001296}
1297
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001298static inline void cpsw_add_default_vlan(struct cpsw_priv *priv)
1299{
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001300 struct cpsw_common *cpsw = priv->cpsw;
1301 const int vlan = cpsw->data.default_vlan;
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001302 u32 reg;
1303 int i;
Lennart Sorensen1e5c4bc2014-10-31 13:38:52 -04001304 int unreg_mcast_mask;
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001305
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001306 reg = (cpsw->version == CPSW_VERSION_1) ? CPSW1_PORT_VLAN :
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001307 CPSW2_PORT_VLAN;
1308
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001309 writel(vlan, &cpsw->host_port_regs->port_vlan);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001310
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001311 for (i = 0; i < cpsw->data.slaves; i++)
1312 slave_write(cpsw->slaves + i, vlan, reg);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001313
Lennart Sorensen1e5c4bc2014-10-31 13:38:52 -04001314 if (priv->ndev->flags & IFF_ALLMULTI)
1315 unreg_mcast_mask = ALE_ALL_PORTS;
1316 else
1317 unreg_mcast_mask = ALE_PORT_1 | ALE_PORT_2;
1318
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001319 cpsw_ale_add_vlan(cpsw->ale, vlan, ALE_ALL_PORTS,
Grygorii Strashko61f1cef2016-04-07 15:16:43 +03001320 ALE_ALL_PORTS, ALE_ALL_PORTS,
1321 unreg_mcast_mask);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001322}
1323
Mugunthan V Ndf828592012-03-18 20:17:54 +00001324static void cpsw_init_host_port(struct cpsw_priv *priv)
1325{
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001326 u32 fifo_mode;
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001327 u32 control_reg;
1328 struct cpsw_common *cpsw = priv->cpsw;
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001329
Mugunthan V Ndf828592012-03-18 20:17:54 +00001330 /* soft reset the controller and initialize ale */
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001331 soft_reset("cpsw", &cpsw->regs->soft_reset);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001332 cpsw_ale_start(cpsw->ale);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001333
1334 /* switch to vlan unaware mode */
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001335 cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_VLAN_AWARE,
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001336 CPSW_ALE_VLAN_AWARE);
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001337 control_reg = readl(&cpsw->regs->control);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001338 control_reg |= CPSW_VLAN_AWARE;
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001339 writel(control_reg, &cpsw->regs->control);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001340 fifo_mode = (cpsw->data.dual_emac) ? CPSW_FIFO_DUAL_MAC_MODE :
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001341 CPSW_FIFO_NORMAL_MODE;
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001342 writel(fifo_mode, &cpsw->host_port_regs->tx_in_ctl);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001343
1344 /* setup host port priority mapping */
1345 __raw_writel(CPDMA_TX_PRIORITY_MAP,
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001346 &cpsw->host_port_regs->cpdma_tx_pri_map);
1347 __raw_writel(0, &cpsw->host_port_regs->cpdma_rx_chan_map);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001348
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001349 cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM,
Mugunthan V Ndf828592012-03-18 20:17:54 +00001350 ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
1351
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001352 if (!cpsw->data.dual_emac) {
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001353 cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr, HOST_PORT_NUM,
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001354 0, 0);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001355 cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
Grygorii Strashko71a2cbb2016-04-07 15:16:44 +03001356 ALE_PORT_HOST, 0, 0, ALE_MCAST_FWD_2);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001357 }
Mugunthan V Ndf828592012-03-18 20:17:54 +00001358}
1359
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +03001360static int cpsw_fill_rx_channels(struct cpsw_priv *priv)
1361{
1362 struct cpsw_common *cpsw = priv->cpsw;
1363 struct sk_buff *skb;
1364 int ch_buf_num;
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001365 int ch, i, ret;
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +03001366
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001367 for (ch = 0; ch < cpsw->rx_ch_num; ch++) {
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02001368 ch_buf_num = cpdma_chan_get_rx_buf_num(cpsw->rxv[ch].ch);
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001369 for (i = 0; i < ch_buf_num; i++) {
1370 skb = __netdev_alloc_skb_ip_align(priv->ndev,
1371 cpsw->rx_packet_max,
1372 GFP_KERNEL);
1373 if (!skb) {
1374 cpsw_err(priv, ifup, "cannot allocate skb\n");
1375 return -ENOMEM;
1376 }
1377
1378 skb_set_queue_mapping(skb, ch);
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02001379 ret = cpdma_chan_submit(cpsw->rxv[ch].ch, skb,
1380 skb->data, skb_tailroom(skb),
1381 0);
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001382 if (ret < 0) {
1383 cpsw_err(priv, ifup,
1384 "cannot submit skb to channel %d rx, error %d\n",
1385 ch, ret);
1386 kfree_skb(skb);
1387 return ret;
1388 }
1389 kmemleak_not_leak(skb);
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +03001390 }
1391
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001392 cpsw_info(priv, ifup, "ch %d rx, submitted %d descriptors\n",
1393 ch, ch_buf_num);
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +03001394 }
1395
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001396 return 0;
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +03001397}
1398
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001399static void cpsw_slave_stop(struct cpsw_slave *slave, struct cpsw_common *cpsw)
Sebastian Siewioraacebbf2013-04-23 07:31:36 +00001400{
Schuyler Patton3995d262014-03-03 16:19:06 +05301401 u32 slave_port;
1402
Ivan Khoronzhuk6f1f5832016-08-10 02:22:34 +03001403 slave_port = cpsw_get_slave_port(slave->slave_num);
Schuyler Patton3995d262014-03-03 16:19:06 +05301404
Sebastian Siewioraacebbf2013-04-23 07:31:36 +00001405 if (!slave->phy)
1406 return;
1407 phy_stop(slave->phy);
1408 phy_disconnect(slave->phy);
1409 slave->phy = NULL;
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001410 cpsw_ale_control_set(cpsw->ale, slave_port,
Schuyler Patton3995d262014-03-03 16:19:06 +05301411 ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
Grygorii Strashko1f95ba02016-06-24 21:23:41 +03001412 soft_reset_slave(slave);
Sebastian Siewioraacebbf2013-04-23 07:31:36 +00001413}
1414
Mugunthan V Ndf828592012-03-18 20:17:54 +00001415static int cpsw_ndo_open(struct net_device *ndev)
1416{
1417 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03001418 struct cpsw_common *cpsw = priv->cpsw;
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +03001419 int ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001420 u32 reg;
1421
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001422 ret = pm_runtime_get_sync(cpsw->dev);
Grygorii Strashko108a6532016-06-24 21:23:42 +03001423 if (ret < 0) {
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001424 pm_runtime_put_noidle(cpsw->dev);
Grygorii Strashko108a6532016-06-24 21:23:42 +03001425 return ret;
1426 }
Grygorii Strashko3fa88c52016-04-19 21:09:49 +03001427
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001428 if (!cpsw_common_res_usage_state(cpsw))
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001429 cpsw_intr_disable(cpsw);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001430 netif_carrier_off(ndev);
1431
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001432 /* Notify the stack of the actual queue counts. */
1433 ret = netif_set_real_num_tx_queues(ndev, cpsw->tx_ch_num);
1434 if (ret) {
1435 dev_err(priv->dev, "cannot set real number of tx queues\n");
1436 goto err_cleanup;
1437 }
1438
1439 ret = netif_set_real_num_rx_queues(ndev, cpsw->rx_ch_num);
1440 if (ret) {
1441 dev_err(priv->dev, "cannot set real number of rx queues\n");
1442 goto err_cleanup;
1443 }
1444
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001445 reg = cpsw->version;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001446
1447 dev_info(priv->dev, "initializing cpsw version %d.%d (%d)\n",
1448 CPSW_MAJOR_VERSION(reg), CPSW_MINOR_VERSION(reg),
1449 CPSW_RTL_VERSION(reg));
1450
1451 /* initialize host and slave ports */
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001452 if (!cpsw_common_res_usage_state(cpsw))
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001453 cpsw_init_host_port(priv);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001454 for_each_slave(priv, cpsw_slave_open, priv);
1455
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001456 /* Add default VLAN */
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001457 if (!cpsw->data.dual_emac)
Mugunthan V Ne6afea02014-06-18 17:21:48 +05301458 cpsw_add_default_vlan(priv);
1459 else
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001460 cpsw_ale_add_vlan(cpsw->ale, cpsw->data.default_vlan,
Grygorii Strashko61f1cef2016-04-07 15:16:43 +03001461 ALE_ALL_PORTS, ALE_ALL_PORTS, 0, 0);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001462
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001463 if (!cpsw_common_res_usage_state(cpsw)) {
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001464 /* disable priority elevation */
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001465 __raw_writel(0, &cpsw->regs->ptype);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001466
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001467 /* enable statistics collection only on all ports */
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001468 __raw_writel(0x7, &cpsw->regs->stat_port_en);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001469
Mugunthan V N1923d6e2014-09-08 22:54:02 +05301470 /* Enable internal fifo flow control */
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001471 writel(0x7, &cpsw->regs->flow_control);
Mugunthan V N1923d6e2014-09-08 22:54:02 +05301472
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +03001473 napi_enable(&cpsw->napi_rx);
1474 napi_enable(&cpsw->napi_tx);
Mugunthan V Nd354eb82015-08-04 16:06:19 +05301475
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +03001476 if (cpsw->tx_irq_disabled) {
1477 cpsw->tx_irq_disabled = false;
1478 enable_irq(cpsw->irqs_table[1]);
Mugunthan V N7da11602015-08-12 15:22:53 +05301479 }
1480
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +03001481 if (cpsw->rx_irq_disabled) {
1482 cpsw->rx_irq_disabled = false;
1483 enable_irq(cpsw->irqs_table[0]);
Mugunthan V N7da11602015-08-12 15:22:53 +05301484 }
1485
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +03001486 ret = cpsw_fill_rx_channels(priv);
1487 if (ret < 0)
1488 goto err_cleanup;
Mugunthan V Nf280e892013-12-11 22:09:05 -06001489
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001490 if (cpts_register(cpsw->dev, cpsw->cpts,
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001491 cpsw->data.cpts_clock_mult,
1492 cpsw->data.cpts_clock_shift))
Mugunthan V Nf280e892013-12-11 22:09:05 -06001493 dev_err(priv->dev, "error registering cpts device\n");
1494
Mugunthan V Ndf828592012-03-18 20:17:54 +00001495 }
Mugunthan V Ndf828592012-03-18 20:17:54 +00001496
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001497 /* Enable Interrupt pacing if configured */
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001498 if (cpsw->coal_intvl != 0) {
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001499 struct ethtool_coalesce coal;
1500
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001501 coal.rx_coalesce_usecs = cpsw->coal_intvl;
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00001502 cpsw_set_coalesce(ndev, &coal);
1503 }
1504
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001505 cpdma_ctlr_start(cpsw->dma);
1506 cpsw_intr_enable(cpsw);
Mugunthan V Nf63a9752014-04-10 14:23:24 +05301507
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001508 if (cpsw->data.dual_emac)
1509 cpsw->slaves[priv->emac_port].open_stat = true;
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001510
1511 netif_tx_start_all_queues(ndev);
1512
Mugunthan V Ndf828592012-03-18 20:17:54 +00001513 return 0;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001514
Sebastian Siewioraacebbf2013-04-23 07:31:36 +00001515err_cleanup:
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001516 cpdma_ctlr_stop(cpsw->dma);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001517 for_each_slave(priv, cpsw_slave_stop, cpsw);
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001518 pm_runtime_put_sync(cpsw->dev);
Sebastian Siewioraacebbf2013-04-23 07:31:36 +00001519 netif_carrier_off(priv->ndev);
1520 return ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001521}
1522
1523static int cpsw_ndo_stop(struct net_device *ndev)
1524{
1525 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03001526 struct cpsw_common *cpsw = priv->cpsw;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001527
1528 cpsw_info(priv, ifdown, "shutting down cpsw device\n");
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001529 netif_tx_stop_all_queues(priv->ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001530 netif_carrier_off(priv->ndev);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001531
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001532 if (cpsw_common_res_usage_state(cpsw) <= 1) {
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +03001533 napi_disable(&cpsw->napi_rx);
1534 napi_disable(&cpsw->napi_tx);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001535 cpts_unregister(cpsw->cpts);
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001536 cpsw_intr_disable(cpsw);
1537 cpdma_ctlr_stop(cpsw->dma);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001538 cpsw_ale_stop(cpsw->ale);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001539 }
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001540 for_each_slave(priv, cpsw_slave_stop, cpsw);
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001541 pm_runtime_put_sync(cpsw->dev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001542 if (cpsw->data.dual_emac)
1543 cpsw->slaves[priv->emac_port].open_stat = false;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001544 return 0;
1545}
1546
1547static netdev_tx_t cpsw_ndo_start_xmit(struct sk_buff *skb,
1548 struct net_device *ndev)
1549{
1550 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001551 struct cpsw_common *cpsw = priv->cpsw;
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001552 struct netdev_queue *txq;
1553 struct cpdma_chan *txch;
1554 int ret, q_idx;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001555
Florian Westphal860e9532016-05-03 16:33:13 +02001556 netif_trans_update(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001557
1558 if (skb_padto(skb, CPSW_MIN_PACKET_SIZE)) {
1559 cpsw_err(priv, tx_err, "packet pad failed\n");
Tobias Klauser8dc43dd2014-03-10 13:12:23 +01001560 ndev->stats.tx_dropped++;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001561 return NETDEV_TX_OK;
1562 }
1563
Mugunthan V N9232b162013-02-11 09:52:19 +00001564 if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
Grygorii Strashkob63ba582016-12-06 18:00:35 -06001565 cpts_is_tx_enabled(cpsw->cpts))
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001566 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
1567
1568 skb_tx_timestamp(skb);
1569
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001570 q_idx = skb_get_queue_mapping(skb);
1571 if (q_idx >= cpsw->tx_ch_num)
1572 q_idx = q_idx % cpsw->tx_ch_num;
1573
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02001574 txch = cpsw->txv[q_idx].ch;
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001575 ret = cpsw_tx_packet_submit(priv, skb, txch);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001576 if (unlikely(ret != 0)) {
1577 cpsw_err(priv, tx_err, "desc submit failed\n");
1578 goto fail;
1579 }
1580
Mugunthan V Nfae50822013-01-17 06:31:34 +00001581 /* If there is no more tx desc left free then we need to
1582 * tell the kernel to stop sending us tx frames.
1583 */
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001584 if (unlikely(!cpdma_check_free_tx_desc(txch))) {
1585 txq = netdev_get_tx_queue(ndev, q_idx);
1586 netif_tx_stop_queue(txq);
1587 }
Mugunthan V Nfae50822013-01-17 06:31:34 +00001588
Mugunthan V Ndf828592012-03-18 20:17:54 +00001589 return NETDEV_TX_OK;
1590fail:
Tobias Klauser8dc43dd2014-03-10 13:12:23 +01001591 ndev->stats.tx_dropped++;
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001592 txq = netdev_get_tx_queue(ndev, skb_get_queue_mapping(skb));
1593 netif_tx_stop_queue(txq);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001594 return NETDEV_TX_BUSY;
1595}
1596
Grygorii Strashkoc8395d42016-12-06 18:00:34 -06001597#if IS_ENABLED(CONFIG_TI_CPTS)
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001598
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001599static void cpsw_hwtstamp_v1(struct cpsw_common *cpsw)
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001600{
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001601 struct cpsw_slave *slave = &cpsw->slaves[cpsw->data.active_slave];
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001602 u32 ts_en, seq_id;
1603
Grygorii Strashkob63ba582016-12-06 18:00:35 -06001604 if (!cpts_is_tx_enabled(cpsw->cpts) &&
1605 !cpts_is_rx_enabled(cpsw->cpts)) {
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001606 slave_write(slave, 0, CPSW1_TS_CTL);
1607 return;
1608 }
1609
1610 seq_id = (30 << CPSW_V1_SEQ_ID_OFS_SHIFT) | ETH_P_1588;
1611 ts_en = EVENT_MSG_BITS << CPSW_V1_MSG_TYPE_OFS;
1612
Grygorii Strashkob63ba582016-12-06 18:00:35 -06001613 if (cpts_is_tx_enabled(cpsw->cpts))
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001614 ts_en |= CPSW_V1_TS_TX_EN;
1615
Grygorii Strashkob63ba582016-12-06 18:00:35 -06001616 if (cpts_is_rx_enabled(cpsw->cpts))
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001617 ts_en |= CPSW_V1_TS_RX_EN;
1618
1619 slave_write(slave, ts_en, CPSW1_TS_CTL);
1620 slave_write(slave, seq_id, CPSW1_TS_SEQ_LTYPE);
1621}
1622
1623static void cpsw_hwtstamp_v2(struct cpsw_priv *priv)
1624{
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001625 struct cpsw_slave *slave;
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001626 struct cpsw_common *cpsw = priv->cpsw;
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001627 u32 ctrl, mtype;
1628
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001629 if (cpsw->data.dual_emac)
1630 slave = &cpsw->slaves[priv->emac_port];
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001631 else
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001632 slave = &cpsw->slaves[cpsw->data.active_slave];
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001633
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001634 ctrl = slave_read(slave, CPSW2_CONTROL);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001635 switch (cpsw->version) {
George Cherian09c55372014-05-02 12:02:02 +05301636 case CPSW_VERSION_2:
1637 ctrl &= ~CTRL_V2_ALL_TS_MASK;
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001638
Grygorii Strashkob63ba582016-12-06 18:00:35 -06001639 if (cpts_is_tx_enabled(cpsw->cpts))
George Cherian09c55372014-05-02 12:02:02 +05301640 ctrl |= CTRL_V2_TX_TS_BITS;
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001641
Grygorii Strashkob63ba582016-12-06 18:00:35 -06001642 if (cpts_is_rx_enabled(cpsw->cpts))
George Cherian09c55372014-05-02 12:02:02 +05301643 ctrl |= CTRL_V2_RX_TS_BITS;
Richard Cochran26fe7eb2015-05-25 11:02:13 +02001644 break;
George Cherian09c55372014-05-02 12:02:02 +05301645 case CPSW_VERSION_3:
1646 default:
1647 ctrl &= ~CTRL_V3_ALL_TS_MASK;
1648
Grygorii Strashkob63ba582016-12-06 18:00:35 -06001649 if (cpts_is_tx_enabled(cpsw->cpts))
George Cherian09c55372014-05-02 12:02:02 +05301650 ctrl |= CTRL_V3_TX_TS_BITS;
1651
Grygorii Strashkob63ba582016-12-06 18:00:35 -06001652 if (cpts_is_rx_enabled(cpsw->cpts))
George Cherian09c55372014-05-02 12:02:02 +05301653 ctrl |= CTRL_V3_RX_TS_BITS;
Richard Cochran26fe7eb2015-05-25 11:02:13 +02001654 break;
George Cherian09c55372014-05-02 12:02:02 +05301655 }
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001656
1657 mtype = (30 << TS_SEQ_ID_OFFSET_SHIFT) | EVENT_MSG_BITS;
1658
1659 slave_write(slave, mtype, CPSW2_TS_SEQ_MTYPE);
1660 slave_write(slave, ctrl, CPSW2_CONTROL);
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03001661 __raw_writel(ETH_P_1588, &cpsw->regs->ts_ltype);
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001662}
1663
Ben Hutchingsa5b41452013-11-18 23:23:40 +00001664static int cpsw_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001665{
Mugunthan V N3177bf62012-11-27 07:53:40 +00001666 struct cpsw_priv *priv = netdev_priv(dev);
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001667 struct hwtstamp_config cfg;
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001668 struct cpsw_common *cpsw = priv->cpsw;
1669 struct cpts *cpts = cpsw->cpts;
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001670
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001671 if (cpsw->version != CPSW_VERSION_1 &&
1672 cpsw->version != CPSW_VERSION_2 &&
1673 cpsw->version != CPSW_VERSION_3)
Ben Hutchings2ee91e52013-11-14 00:47:36 +00001674 return -EOPNOTSUPP;
1675
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001676 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
1677 return -EFAULT;
1678
1679 /* reserved for future extensions */
1680 if (cfg.flags)
1681 return -EINVAL;
1682
Ben Hutchings2ee91e52013-11-14 00:47:36 +00001683 if (cfg.tx_type != HWTSTAMP_TX_OFF && cfg.tx_type != HWTSTAMP_TX_ON)
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001684 return -ERANGE;
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001685
1686 switch (cfg.rx_filter) {
1687 case HWTSTAMP_FILTER_NONE:
Grygorii Strashkob63ba582016-12-06 18:00:35 -06001688 cpts_rx_enable(cpts, 0);
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001689 break;
1690 case HWTSTAMP_FILTER_ALL:
1691 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
1692 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
1693 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
1694 return -ERANGE;
1695 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
1696 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
1697 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
1698 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1699 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
1700 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
1701 case HWTSTAMP_FILTER_PTP_V2_EVENT:
1702 case HWTSTAMP_FILTER_PTP_V2_SYNC:
1703 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
Grygorii Strashkob63ba582016-12-06 18:00:35 -06001704 cpts_rx_enable(cpts, 1);
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001705 cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
1706 break;
1707 default:
1708 return -ERANGE;
1709 }
1710
Grygorii Strashkob63ba582016-12-06 18:00:35 -06001711 cpts_tx_enable(cpts, cfg.tx_type == HWTSTAMP_TX_ON);
Ben Hutchings2ee91e52013-11-14 00:47:36 +00001712
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001713 switch (cpsw->version) {
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001714 case CPSW_VERSION_1:
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001715 cpsw_hwtstamp_v1(cpsw);
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001716 break;
1717 case CPSW_VERSION_2:
George Cherianf7d403c2014-05-02 12:02:01 +05301718 case CPSW_VERSION_3:
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001719 cpsw_hwtstamp_v2(priv);
1720 break;
1721 default:
Ben Hutchings2ee91e52013-11-14 00:47:36 +00001722 WARN_ON(1);
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001723 }
1724
1725 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
1726}
1727
Ben Hutchingsa5b41452013-11-18 23:23:40 +00001728static int cpsw_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
1729{
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001730 struct cpsw_common *cpsw = ndev_to_cpsw(dev);
1731 struct cpts *cpts = cpsw->cpts;
Ben Hutchingsa5b41452013-11-18 23:23:40 +00001732 struct hwtstamp_config cfg;
1733
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001734 if (cpsw->version != CPSW_VERSION_1 &&
1735 cpsw->version != CPSW_VERSION_2 &&
1736 cpsw->version != CPSW_VERSION_3)
Ben Hutchingsa5b41452013-11-18 23:23:40 +00001737 return -EOPNOTSUPP;
1738
1739 cfg.flags = 0;
Grygorii Strashkob63ba582016-12-06 18:00:35 -06001740 cfg.tx_type = cpts_is_tx_enabled(cpts) ?
1741 HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF;
1742 cfg.rx_filter = (cpts_is_rx_enabled(cpts) ?
Ben Hutchingsa5b41452013-11-18 23:23:40 +00001743 HWTSTAMP_FILTER_PTP_V2_EVENT : HWTSTAMP_FILTER_NONE);
1744
1745 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
1746}
Grygorii Strashkoc8395d42016-12-06 18:00:34 -06001747#else
1748static int cpsw_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
1749{
1750 return -EOPNOTSUPP;
1751}
Ben Hutchingsa5b41452013-11-18 23:23:40 +00001752
Grygorii Strashkoc8395d42016-12-06 18:00:34 -06001753static int cpsw_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
1754{
1755 return -EOPNOTSUPP;
1756}
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001757#endif /*CONFIG_TI_CPTS*/
1758
1759static int cpsw_ndo_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
1760{
Mugunthan V N11f2c982013-03-11 23:16:38 +00001761 struct cpsw_priv *priv = netdev_priv(dev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001762 struct cpsw_common *cpsw = priv->cpsw;
1763 int slave_no = cpsw_slave_index(cpsw, priv);
Mugunthan V N11f2c982013-03-11 23:16:38 +00001764
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001765 if (!netif_running(dev))
1766 return -EINVAL;
1767
Mugunthan V N11f2c982013-03-11 23:16:38 +00001768 switch (cmd) {
Mugunthan V N11f2c982013-03-11 23:16:38 +00001769 case SIOCSHWTSTAMP:
Ben Hutchingsa5b41452013-11-18 23:23:40 +00001770 return cpsw_hwtstamp_set(dev, req);
1771 case SIOCGHWTSTAMP:
1772 return cpsw_hwtstamp_get(dev, req);
Mugunthan V N11f2c982013-03-11 23:16:38 +00001773 }
1774
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001775 if (!cpsw->slaves[slave_no].phy)
Stefan Sørensenc1b59942014-02-16 14:54:25 +01001776 return -EOPNOTSUPP;
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001777 return phy_mii_ioctl(cpsw->slaves[slave_no].phy, req, cmd);
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001778}
1779
Mugunthan V Ndf828592012-03-18 20:17:54 +00001780static void cpsw_ndo_tx_timeout(struct net_device *ndev)
1781{
1782 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001783 struct cpsw_common *cpsw = priv->cpsw;
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001784 int ch;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001785
1786 cpsw_err(priv, tx_err, "transmit timeout, restarting dma\n");
Tobias Klauser8dc43dd2014-03-10 13:12:23 +01001787 ndev->stats.tx_errors++;
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001788 cpsw_intr_disable(cpsw);
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001789 for (ch = 0; ch < cpsw->tx_ch_num; ch++) {
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02001790 cpdma_chan_stop(cpsw->txv[ch].ch);
1791 cpdma_chan_start(cpsw->txv[ch].ch);
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03001792 }
1793
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03001794 cpsw_intr_enable(cpsw);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001795}
1796
Mugunthan V Ndcfd8d52013-07-25 23:44:01 +05301797static int cpsw_ndo_set_mac_address(struct net_device *ndev, void *p)
1798{
1799 struct cpsw_priv *priv = netdev_priv(ndev);
1800 struct sockaddr *addr = (struct sockaddr *)p;
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03001801 struct cpsw_common *cpsw = priv->cpsw;
Mugunthan V Ndcfd8d52013-07-25 23:44:01 +05301802 int flags = 0;
1803 u16 vid = 0;
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001804 int ret;
Mugunthan V Ndcfd8d52013-07-25 23:44:01 +05301805
1806 if (!is_valid_ether_addr(addr->sa_data))
1807 return -EADDRNOTAVAIL;
1808
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001809 ret = pm_runtime_get_sync(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001810 if (ret < 0) {
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001811 pm_runtime_put_noidle(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001812 return ret;
1813 }
1814
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001815 if (cpsw->data.dual_emac) {
1816 vid = cpsw->slaves[priv->emac_port].port_vlan;
Mugunthan V Ndcfd8d52013-07-25 23:44:01 +05301817 flags = ALE_VLAN;
1818 }
1819
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001820 cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr, HOST_PORT_NUM,
Mugunthan V Ndcfd8d52013-07-25 23:44:01 +05301821 flags, vid);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001822 cpsw_ale_add_ucast(cpsw->ale, addr->sa_data, HOST_PORT_NUM,
Mugunthan V Ndcfd8d52013-07-25 23:44:01 +05301823 flags, vid);
1824
1825 memcpy(priv->mac_addr, addr->sa_data, ETH_ALEN);
1826 memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
1827 for_each_slave(priv, cpsw_set_slave_mac, priv);
1828
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001829 pm_runtime_put(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001830
Mugunthan V Ndcfd8d52013-07-25 23:44:01 +05301831 return 0;
1832}
1833
Mugunthan V Ndf828592012-03-18 20:17:54 +00001834#ifdef CONFIG_NET_POLL_CONTROLLER
1835static void cpsw_ndo_poll_controller(struct net_device *ndev)
1836{
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +03001837 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001838
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +03001839 cpsw_intr_disable(cpsw);
1840 cpsw_rx_interrupt(cpsw->irqs_table[0], cpsw);
1841 cpsw_tx_interrupt(cpsw->irqs_table[1], cpsw);
1842 cpsw_intr_enable(cpsw);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001843}
1844#endif
1845
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001846static inline int cpsw_add_vlan_ale_entry(struct cpsw_priv *priv,
1847 unsigned short vid)
1848{
1849 int ret;
Mugunthan V N9f6bd8f2015-01-15 14:59:28 +05301850 int unreg_mcast_mask = 0;
1851 u32 port_mask;
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001852 struct cpsw_common *cpsw = priv->cpsw;
Lennart Sorensen1e5c4bc2014-10-31 13:38:52 -04001853
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001854 if (cpsw->data.dual_emac) {
Mugunthan V N9f6bd8f2015-01-15 14:59:28 +05301855 port_mask = (1 << (priv->emac_port + 1)) | ALE_PORT_HOST;
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001856
Mugunthan V N9f6bd8f2015-01-15 14:59:28 +05301857 if (priv->ndev->flags & IFF_ALLMULTI)
1858 unreg_mcast_mask = port_mask;
1859 } else {
1860 port_mask = ALE_ALL_PORTS;
1861
1862 if (priv->ndev->flags & IFF_ALLMULTI)
1863 unreg_mcast_mask = ALE_ALL_PORTS;
1864 else
1865 unreg_mcast_mask = ALE_PORT_1 | ALE_PORT_2;
1866 }
1867
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001868 ret = cpsw_ale_add_vlan(cpsw->ale, vid, port_mask, 0, port_mask,
Grygorii Strashko61f1cef2016-04-07 15:16:43 +03001869 unreg_mcast_mask);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001870 if (ret != 0)
1871 return ret;
1872
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001873 ret = cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr,
Grygorii Strashko71a2cbb2016-04-07 15:16:44 +03001874 HOST_PORT_NUM, ALE_VLAN, vid);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001875 if (ret != 0)
1876 goto clean_vid;
1877
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001878 ret = cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
Mugunthan V N9f6bd8f2015-01-15 14:59:28 +05301879 port_mask, ALE_VLAN, vid, 0);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001880 if (ret != 0)
1881 goto clean_vlan_ucast;
1882 return 0;
1883
1884clean_vlan_ucast:
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001885 cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr,
Grygorii Strashko71a2cbb2016-04-07 15:16:44 +03001886 HOST_PORT_NUM, ALE_VLAN, vid);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001887clean_vid:
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001888 cpsw_ale_del_vlan(cpsw->ale, vid, 0);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001889 return ret;
1890}
1891
1892static int cpsw_ndo_vlan_rx_add_vid(struct net_device *ndev,
Patrick McHardy80d5c362013-04-19 02:04:28 +00001893 __be16 proto, u16 vid)
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001894{
1895 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03001896 struct cpsw_common *cpsw = priv->cpsw;
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001897 int ret;
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001898
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001899 if (vid == cpsw->data.default_vlan)
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001900 return 0;
1901
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001902 ret = pm_runtime_get_sync(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001903 if (ret < 0) {
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001904 pm_runtime_put_noidle(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001905 return ret;
1906 }
1907
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001908 if (cpsw->data.dual_emac) {
Mugunthan V N02a54162015-01-22 15:19:22 +05301909 /* In dual EMAC, reserved VLAN id should not be used for
1910 * creating VLAN interfaces as this can break the dual
1911 * EMAC port separation
1912 */
1913 int i;
1914
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001915 for (i = 0; i < cpsw->data.slaves; i++) {
1916 if (vid == cpsw->slaves[i].port_vlan)
Mugunthan V N02a54162015-01-22 15:19:22 +05301917 return -EINVAL;
1918 }
1919 }
1920
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001921 dev_info(priv->dev, "Adding vlanid %d to vlan filter\n", vid);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001922 ret = cpsw_add_vlan_ale_entry(priv, vid);
1923
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001924 pm_runtime_put(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001925 return ret;
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001926}
1927
1928static int cpsw_ndo_vlan_rx_kill_vid(struct net_device *ndev,
Patrick McHardy80d5c362013-04-19 02:04:28 +00001929 __be16 proto, u16 vid)
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001930{
1931 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03001932 struct cpsw_common *cpsw = priv->cpsw;
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001933 int ret;
1934
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001935 if (vid == cpsw->data.default_vlan)
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001936 return 0;
1937
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001938 ret = pm_runtime_get_sync(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001939 if (ret < 0) {
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001940 pm_runtime_put_noidle(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001941 return ret;
1942 }
1943
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001944 if (cpsw->data.dual_emac) {
Mugunthan V N02a54162015-01-22 15:19:22 +05301945 int i;
1946
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03001947 for (i = 0; i < cpsw->data.slaves; i++) {
1948 if (vid == cpsw->slaves[i].port_vlan)
Mugunthan V N02a54162015-01-22 15:19:22 +05301949 return -EINVAL;
1950 }
1951 }
1952
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001953 dev_info(priv->dev, "removing vlanid %d from vlan filter\n", vid);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001954 ret = cpsw_ale_del_vlan(cpsw->ale, vid, 0);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001955 if (ret != 0)
1956 return ret;
1957
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001958 ret = cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr,
Grygorii Strashko61f1cef2016-04-07 15:16:43 +03001959 HOST_PORT_NUM, ALE_VLAN, vid);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001960 if (ret != 0)
1961 return ret;
1962
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03001963 ret = cpsw_ale_del_mcast(cpsw->ale, priv->ndev->broadcast,
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001964 0, ALE_VLAN, vid);
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03001965 pm_runtime_put(cpsw->dev);
Grygorii Strashkoa6c5d142016-06-24 21:23:45 +03001966 return ret;
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001967}
1968
Ivan Khoronzhuk83fcad02016-11-29 17:00:49 +02001969static int cpsw_ndo_set_tx_maxrate(struct net_device *ndev, int queue, u32 rate)
1970{
1971 struct cpsw_priv *priv = netdev_priv(ndev);
1972 int tx_ch_num = ndev->real_num_tx_queues;
1973 u32 consumed_rate, min_rate, max_rate;
1974 struct cpsw_common *cpsw = priv->cpsw;
1975 struct cpsw_slave *slave;
1976 int ret, i, weight;
1977 int rlim_num = 0;
1978 u32 ch_rate;
1979
1980 ch_rate = netdev_get_tx_queue(ndev, queue)->tx_maxrate;
1981 if (ch_rate == rate)
1982 return 0;
1983
1984 if (cpsw->data.dual_emac)
1985 slave = &cpsw->slaves[priv->emac_port];
1986 else
1987 slave = &cpsw->slaves[cpsw->data.active_slave];
1988 max_rate = slave->phy->speed;
1989
1990 consumed_rate = 0;
1991 for (i = 0; i < tx_ch_num; i++) {
1992 if (i == queue)
1993 ch_rate = rate;
1994 else
1995 ch_rate = netdev_get_tx_queue(ndev, i)->tx_maxrate;
Ivan Khoronzhuk83fcad02016-11-29 17:00:49 +02001996 if (!ch_rate)
1997 continue;
1998
1999 rlim_num++;
2000 consumed_rate += ch_rate;
2001 }
2002
2003 if (consumed_rate > max_rate)
2004 dev_info(priv->dev, "The common rate shouldn't be more than %dMbps",
2005 max_rate);
2006
2007 if (consumed_rate > max_rate) {
2008 if (max_rate == 10 && consumed_rate <= 100) {
2009 max_rate = 100;
2010 } else if (max_rate <= 100 && consumed_rate <= 1000) {
2011 max_rate = 1000;
2012 } else {
2013 dev_err(priv->dev, "The common rate cannot be more than %dMbps",
2014 max_rate);
2015 return -EINVAL;
2016 }
2017 }
2018
2019 if (consumed_rate > max_rate) {
2020 dev_err(priv->dev, "The common rate cannot be more than %dMbps",
2021 max_rate);
2022 return -EINVAL;
2023 }
2024
2025 rate *= 1000;
2026 min_rate = cpdma_chan_get_min_rate(cpsw->dma);
2027 if ((rate < min_rate && rate)) {
2028 dev_err(priv->dev, "The common rate cannot be less than %dMbps",
2029 min_rate);
2030 return -EINVAL;
2031 }
2032
2033 ret = pm_runtime_get_sync(cpsw->dev);
2034 if (ret < 0) {
2035 pm_runtime_put_noidle(cpsw->dev);
2036 return ret;
2037 }
2038
2039 if (rlim_num == tx_ch_num)
2040 max_rate = consumed_rate;
2041
2042 weight = (rate * 100) / (max_rate * 1000);
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02002043 cpdma_chan_set_weight(cpsw->txv[queue].ch, weight);
2044 ret = cpdma_chan_set_rate(cpsw->txv[queue].ch, rate);
Ivan Khoronzhuk83fcad02016-11-29 17:00:49 +02002045
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02002046 /* re-split budget between channels */
2047 if (!rate)
2048 cpsw_split_budget(ndev);
Ivan Khoronzhuk83fcad02016-11-29 17:00:49 +02002049 pm_runtime_put(cpsw->dev);
2050 return ret;
2051}
2052
Mugunthan V Ndf828592012-03-18 20:17:54 +00002053static const struct net_device_ops cpsw_netdev_ops = {
2054 .ndo_open = cpsw_ndo_open,
2055 .ndo_stop = cpsw_ndo_stop,
2056 .ndo_start_xmit = cpsw_ndo_start_xmit,
Mugunthan V Ndcfd8d52013-07-25 23:44:01 +05302057 .ndo_set_mac_address = cpsw_ndo_set_mac_address,
Richard Cochran2e5b38a2012-10-29 08:45:20 +00002058 .ndo_do_ioctl = cpsw_ndo_ioctl,
Mugunthan V Ndf828592012-03-18 20:17:54 +00002059 .ndo_validate_addr = eth_validate_addr,
Mugunthan V Ndf828592012-03-18 20:17:54 +00002060 .ndo_tx_timeout = cpsw_ndo_tx_timeout,
Mugunthan V N5c50a852012-10-29 08:45:11 +00002061 .ndo_set_rx_mode = cpsw_ndo_set_rx_mode,
Ivan Khoronzhuk83fcad02016-11-29 17:00:49 +02002062 .ndo_set_tx_maxrate = cpsw_ndo_set_tx_maxrate,
Mugunthan V Ndf828592012-03-18 20:17:54 +00002063#ifdef CONFIG_NET_POLL_CONTROLLER
2064 .ndo_poll_controller = cpsw_ndo_poll_controller,
2065#endif
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00002066 .ndo_vlan_rx_add_vid = cpsw_ndo_vlan_rx_add_vid,
2067 .ndo_vlan_rx_kill_vid = cpsw_ndo_vlan_rx_kill_vid,
Mugunthan V Ndf828592012-03-18 20:17:54 +00002068};
2069
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05302070static int cpsw_get_regs_len(struct net_device *ndev)
2071{
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002072 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05302073
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002074 return cpsw->data.ale_entries * ALE_ENTRY_WORDS * sizeof(u32);
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05302075}
2076
2077static void cpsw_get_regs(struct net_device *ndev,
2078 struct ethtool_regs *regs, void *p)
2079{
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05302080 u32 *reg = p;
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002081 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05302082
2083 /* update CPSW IP version */
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002084 regs->version = cpsw->version;
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05302085
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002086 cpsw_ale_dump(cpsw->ale, reg);
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05302087}
2088
Mugunthan V Ndf828592012-03-18 20:17:54 +00002089static void cpsw_get_drvinfo(struct net_device *ndev,
2090 struct ethtool_drvinfo *info)
2091{
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03002092 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002093 struct platform_device *pdev = to_platform_device(cpsw->dev);
Jiri Pirko7826d432013-01-06 00:44:26 +00002094
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05302095 strlcpy(info->driver, "cpsw", sizeof(info->driver));
Jiri Pirko7826d432013-01-06 00:44:26 +00002096 strlcpy(info->version, "1.0", sizeof(info->version));
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002097 strlcpy(info->bus_info, pdev->name, sizeof(info->bus_info));
Mugunthan V Ndf828592012-03-18 20:17:54 +00002098}
2099
2100static u32 cpsw_get_msglevel(struct net_device *ndev)
2101{
2102 struct cpsw_priv *priv = netdev_priv(ndev);
2103 return priv->msg_enable;
2104}
2105
2106static void cpsw_set_msglevel(struct net_device *ndev, u32 value)
2107{
2108 struct cpsw_priv *priv = netdev_priv(ndev);
2109 priv->msg_enable = value;
2110}
2111
Grygorii Strashkoc8395d42016-12-06 18:00:34 -06002112#if IS_ENABLED(CONFIG_TI_CPTS)
Richard Cochran2e5b38a2012-10-29 08:45:20 +00002113static int cpsw_get_ts_info(struct net_device *ndev,
2114 struct ethtool_ts_info *info)
2115{
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002116 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Richard Cochran2e5b38a2012-10-29 08:45:20 +00002117
2118 info->so_timestamping =
2119 SOF_TIMESTAMPING_TX_HARDWARE |
2120 SOF_TIMESTAMPING_TX_SOFTWARE |
2121 SOF_TIMESTAMPING_RX_HARDWARE |
2122 SOF_TIMESTAMPING_RX_SOFTWARE |
2123 SOF_TIMESTAMPING_SOFTWARE |
2124 SOF_TIMESTAMPING_RAW_HARDWARE;
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002125 info->phc_index = cpsw->cpts->phc_index;
Richard Cochran2e5b38a2012-10-29 08:45:20 +00002126 info->tx_types =
2127 (1 << HWTSTAMP_TX_OFF) |
2128 (1 << HWTSTAMP_TX_ON);
2129 info->rx_filters =
2130 (1 << HWTSTAMP_FILTER_NONE) |
2131 (1 << HWTSTAMP_FILTER_PTP_V2_EVENT);
Grygorii Strashkoc8395d42016-12-06 18:00:34 -06002132 return 0;
2133}
Richard Cochran2e5b38a2012-10-29 08:45:20 +00002134#else
Grygorii Strashkoc8395d42016-12-06 18:00:34 -06002135static int cpsw_get_ts_info(struct net_device *ndev,
2136 struct ethtool_ts_info *info)
2137{
Richard Cochran2e5b38a2012-10-29 08:45:20 +00002138 info->so_timestamping =
2139 SOF_TIMESTAMPING_TX_SOFTWARE |
2140 SOF_TIMESTAMPING_RX_SOFTWARE |
2141 SOF_TIMESTAMPING_SOFTWARE;
2142 info->phc_index = -1;
2143 info->tx_types = 0;
2144 info->rx_filters = 0;
Richard Cochran2e5b38a2012-10-29 08:45:20 +00002145 return 0;
2146}
Grygorii Strashkoc8395d42016-12-06 18:00:34 -06002147#endif
Richard Cochran2e5b38a2012-10-29 08:45:20 +00002148
Philippe Reynes24798762016-10-08 17:46:15 +02002149static int cpsw_get_link_ksettings(struct net_device *ndev,
2150 struct ethtool_link_ksettings *ecmd)
Mugunthan V Nd3bb9c52013-03-11 23:16:36 +00002151{
2152 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002153 struct cpsw_common *cpsw = priv->cpsw;
2154 int slave_no = cpsw_slave_index(cpsw, priv);
Mugunthan V Nd3bb9c52013-03-11 23:16:36 +00002155
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002156 if (cpsw->slaves[slave_no].phy)
Philippe Reynes24798762016-10-08 17:46:15 +02002157 return phy_ethtool_ksettings_get(cpsw->slaves[slave_no].phy,
2158 ecmd);
Mugunthan V Nd3bb9c52013-03-11 23:16:36 +00002159 else
2160 return -EOPNOTSUPP;
2161}
2162
Philippe Reynes24798762016-10-08 17:46:15 +02002163static int cpsw_set_link_ksettings(struct net_device *ndev,
2164 const struct ethtool_link_ksettings *ecmd)
Mugunthan V Nd3bb9c52013-03-11 23:16:36 +00002165{
2166 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002167 struct cpsw_common *cpsw = priv->cpsw;
2168 int slave_no = cpsw_slave_index(cpsw, priv);
Mugunthan V Nd3bb9c52013-03-11 23:16:36 +00002169
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002170 if (cpsw->slaves[slave_no].phy)
Philippe Reynes24798762016-10-08 17:46:15 +02002171 return phy_ethtool_ksettings_set(cpsw->slaves[slave_no].phy,
2172 ecmd);
Mugunthan V Nd3bb9c52013-03-11 23:16:36 +00002173 else
2174 return -EOPNOTSUPP;
2175}
2176
Matus Ujhelyid8a64422013-08-20 07:59:38 +02002177static void cpsw_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2178{
2179 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002180 struct cpsw_common *cpsw = priv->cpsw;
2181 int slave_no = cpsw_slave_index(cpsw, priv);
Matus Ujhelyid8a64422013-08-20 07:59:38 +02002182
2183 wol->supported = 0;
2184 wol->wolopts = 0;
2185
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002186 if (cpsw->slaves[slave_no].phy)
2187 phy_ethtool_get_wol(cpsw->slaves[slave_no].phy, wol);
Matus Ujhelyid8a64422013-08-20 07:59:38 +02002188}
2189
2190static int cpsw_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2191{
2192 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002193 struct cpsw_common *cpsw = priv->cpsw;
2194 int slave_no = cpsw_slave_index(cpsw, priv);
Matus Ujhelyid8a64422013-08-20 07:59:38 +02002195
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002196 if (cpsw->slaves[slave_no].phy)
2197 return phy_ethtool_set_wol(cpsw->slaves[slave_no].phy, wol);
Matus Ujhelyid8a64422013-08-20 07:59:38 +02002198 else
2199 return -EOPNOTSUPP;
2200}
2201
Mugunthan V N1923d6e2014-09-08 22:54:02 +05302202static void cpsw_get_pauseparam(struct net_device *ndev,
2203 struct ethtool_pauseparam *pause)
2204{
2205 struct cpsw_priv *priv = netdev_priv(ndev);
2206
2207 pause->autoneg = AUTONEG_DISABLE;
2208 pause->rx_pause = priv->rx_pause ? true : false;
2209 pause->tx_pause = priv->tx_pause ? true : false;
2210}
2211
2212static int cpsw_set_pauseparam(struct net_device *ndev,
2213 struct ethtool_pauseparam *pause)
2214{
2215 struct cpsw_priv *priv = netdev_priv(ndev);
2216 bool link;
2217
2218 priv->rx_pause = pause->rx_pause ? true : false;
2219 priv->tx_pause = pause->tx_pause ? true : false;
2220
2221 for_each_slave(priv, _cpsw_adjust_link, priv, &link);
Mugunthan V N1923d6e2014-09-08 22:54:02 +05302222 return 0;
2223}
2224
Grygorii Strashko7898b1d2016-06-24 21:23:44 +03002225static int cpsw_ethtool_op_begin(struct net_device *ndev)
2226{
2227 struct cpsw_priv *priv = netdev_priv(ndev);
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03002228 struct cpsw_common *cpsw = priv->cpsw;
Grygorii Strashko7898b1d2016-06-24 21:23:44 +03002229 int ret;
2230
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002231 ret = pm_runtime_get_sync(cpsw->dev);
Grygorii Strashko7898b1d2016-06-24 21:23:44 +03002232 if (ret < 0) {
2233 cpsw_err(priv, drv, "ethtool begin failed %d\n", ret);
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002234 pm_runtime_put_noidle(cpsw->dev);
Grygorii Strashko7898b1d2016-06-24 21:23:44 +03002235 }
2236
2237 return ret;
2238}
2239
2240static void cpsw_ethtool_op_complete(struct net_device *ndev)
2241{
2242 struct cpsw_priv *priv = netdev_priv(ndev);
2243 int ret;
2244
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002245 ret = pm_runtime_put(priv->cpsw->dev);
Grygorii Strashko7898b1d2016-06-24 21:23:44 +03002246 if (ret < 0)
2247 cpsw_err(priv, drv, "ethtool complete failed %d\n", ret);
2248}
2249
Ivan Khoronzhukce52c742016-08-22 21:18:28 +03002250static void cpsw_get_channels(struct net_device *ndev,
2251 struct ethtool_channels *ch)
2252{
2253 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
2254
2255 ch->max_combined = 0;
2256 ch->max_rx = CPSW_MAX_QUEUES;
2257 ch->max_tx = CPSW_MAX_QUEUES;
2258 ch->max_other = 0;
2259 ch->other_count = 0;
2260 ch->rx_count = cpsw->rx_ch_num;
2261 ch->tx_count = cpsw->tx_ch_num;
2262 ch->combined_count = 0;
2263}
2264
2265static int cpsw_check_ch_settings(struct cpsw_common *cpsw,
2266 struct ethtool_channels *ch)
2267{
2268 if (ch->combined_count)
2269 return -EINVAL;
2270
2271 /* verify we have at least one channel in each direction */
2272 if (!ch->rx_count || !ch->tx_count)
2273 return -EINVAL;
2274
2275 if (ch->rx_count > cpsw->data.channels ||
2276 ch->tx_count > cpsw->data.channels)
2277 return -EINVAL;
2278
2279 return 0;
2280}
2281
2282static int cpsw_update_channels_res(struct cpsw_priv *priv, int ch_num, int rx)
2283{
2284 int (*poll)(struct napi_struct *, int);
2285 struct cpsw_common *cpsw = priv->cpsw;
2286 void (*handler)(void *, int, int);
Ivan Khoronzhuk83fcad02016-11-29 17:00:49 +02002287 struct netdev_queue *queue;
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02002288 struct cpsw_vector *vec;
Ivan Khoronzhukce52c742016-08-22 21:18:28 +03002289 int ret, *ch;
2290
2291 if (rx) {
2292 ch = &cpsw->rx_ch_num;
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02002293 vec = cpsw->rxv;
Ivan Khoronzhukce52c742016-08-22 21:18:28 +03002294 handler = cpsw_rx_handler;
2295 poll = cpsw_rx_poll;
2296 } else {
2297 ch = &cpsw->tx_ch_num;
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02002298 vec = cpsw->txv;
Ivan Khoronzhukce52c742016-08-22 21:18:28 +03002299 handler = cpsw_tx_handler;
2300 poll = cpsw_tx_poll;
2301 }
2302
2303 while (*ch < ch_num) {
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02002304 vec[*ch].ch = cpdma_chan_create(cpsw->dma, *ch, handler, rx);
Ivan Khoronzhuk83fcad02016-11-29 17:00:49 +02002305 queue = netdev_get_tx_queue(priv->ndev, *ch);
2306 queue->tx_maxrate = 0;
Ivan Khoronzhukce52c742016-08-22 21:18:28 +03002307
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02002308 if (IS_ERR(vec[*ch].ch))
2309 return PTR_ERR(vec[*ch].ch);
Ivan Khoronzhukce52c742016-08-22 21:18:28 +03002310
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02002311 if (!vec[*ch].ch)
Ivan Khoronzhukce52c742016-08-22 21:18:28 +03002312 return -EINVAL;
2313
2314 cpsw_info(priv, ifup, "created new %d %s channel\n", *ch,
2315 (rx ? "rx" : "tx"));
2316 (*ch)++;
2317 }
2318
2319 while (*ch > ch_num) {
2320 (*ch)--;
2321
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02002322 ret = cpdma_chan_destroy(vec[*ch].ch);
Ivan Khoronzhukce52c742016-08-22 21:18:28 +03002323 if (ret)
2324 return ret;
2325
2326 cpsw_info(priv, ifup, "destroyed %d %s channel\n", *ch,
2327 (rx ? "rx" : "tx"));
2328 }
2329
2330 return 0;
2331}
2332
2333static int cpsw_update_channels(struct cpsw_priv *priv,
2334 struct ethtool_channels *ch)
2335{
2336 int ret;
2337
2338 ret = cpsw_update_channels_res(priv, ch->rx_count, 1);
2339 if (ret)
2340 return ret;
2341
2342 ret = cpsw_update_channels_res(priv, ch->tx_count, 0);
2343 if (ret)
2344 return ret;
2345
2346 return 0;
2347}
2348
2349static int cpsw_set_channels(struct net_device *ndev,
2350 struct ethtool_channels *chs)
2351{
2352 struct cpsw_priv *priv = netdev_priv(ndev);
2353 struct cpsw_common *cpsw = priv->cpsw;
2354 struct cpsw_slave *slave;
2355 int i, ret;
2356
2357 ret = cpsw_check_ch_settings(cpsw, chs);
2358 if (ret < 0)
2359 return ret;
2360
2361 /* Disable NAPI scheduling */
2362 cpsw_intr_disable(cpsw);
2363
2364 /* Stop all transmit queues for every network device.
2365 * Disable re-using rx descriptors with dormant_on.
2366 */
2367 for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++) {
2368 if (!(slave->ndev && netif_running(slave->ndev)))
2369 continue;
2370
2371 netif_tx_stop_all_queues(slave->ndev);
2372 netif_dormant_on(slave->ndev);
2373 }
2374
2375 /* Handle rest of tx packets and stop cpdma channels */
2376 cpdma_ctlr_stop(cpsw->dma);
2377 ret = cpsw_update_channels(priv, chs);
2378 if (ret)
2379 goto err;
2380
2381 for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++) {
2382 if (!(slave->ndev && netif_running(slave->ndev)))
2383 continue;
2384
2385 /* Inform stack about new count of queues */
2386 ret = netif_set_real_num_tx_queues(slave->ndev,
2387 cpsw->tx_ch_num);
2388 if (ret) {
2389 dev_err(priv->dev, "cannot set real number of tx queues\n");
2390 goto err;
2391 }
2392
2393 ret = netif_set_real_num_rx_queues(slave->ndev,
2394 cpsw->rx_ch_num);
2395 if (ret) {
2396 dev_err(priv->dev, "cannot set real number of rx queues\n");
2397 goto err;
2398 }
2399
2400 /* Enable rx packets handling */
2401 netif_dormant_off(slave->ndev);
2402 }
2403
2404 if (cpsw_common_res_usage_state(cpsw)) {
Wei Yongjune19ac152016-08-26 14:35:43 +00002405 ret = cpsw_fill_rx_channels(priv);
2406 if (ret)
Ivan Khoronzhukce52c742016-08-22 21:18:28 +03002407 goto err;
2408
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02002409 cpsw_split_budget(ndev);
2410
Ivan Khoronzhukce52c742016-08-22 21:18:28 +03002411 /* After this receive is started */
2412 cpdma_ctlr_start(cpsw->dma);
2413 cpsw_intr_enable(cpsw);
2414 }
2415
2416 /* Resume transmit for every affected interface */
2417 for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++) {
2418 if (!(slave->ndev && netif_running(slave->ndev)))
2419 continue;
2420 netif_tx_start_all_queues(slave->ndev);
2421 }
2422 return 0;
2423err:
2424 dev_err(priv->dev, "cannot update channels number, closing device\n");
2425 dev_close(ndev);
2426 return ret;
2427}
2428
Yegor Yefremova0909942016-11-28 09:41:33 +01002429static int cpsw_get_eee(struct net_device *ndev, struct ethtool_eee *edata)
2430{
2431 struct cpsw_priv *priv = netdev_priv(ndev);
2432 struct cpsw_common *cpsw = priv->cpsw;
2433 int slave_no = cpsw_slave_index(cpsw, priv);
2434
2435 if (cpsw->slaves[slave_no].phy)
2436 return phy_ethtool_get_eee(cpsw->slaves[slave_no].phy, edata);
2437 else
2438 return -EOPNOTSUPP;
2439}
2440
2441static int cpsw_set_eee(struct net_device *ndev, struct ethtool_eee *edata)
2442{
2443 struct cpsw_priv *priv = netdev_priv(ndev);
2444 struct cpsw_common *cpsw = priv->cpsw;
2445 int slave_no = cpsw_slave_index(cpsw, priv);
2446
2447 if (cpsw->slaves[slave_no].phy)
2448 return phy_ethtool_set_eee(cpsw->slaves[slave_no].phy, edata);
2449 else
2450 return -EOPNOTSUPP;
2451}
2452
Yegor Yefremov6bb10c22016-11-28 10:47:52 +01002453static int cpsw_nway_reset(struct net_device *ndev)
2454{
2455 struct cpsw_priv *priv = netdev_priv(ndev);
2456 struct cpsw_common *cpsw = priv->cpsw;
2457 int slave_no = cpsw_slave_index(cpsw, priv);
2458
2459 if (cpsw->slaves[slave_no].phy)
2460 return genphy_restart_aneg(cpsw->slaves[slave_no].phy);
2461 else
2462 return -EOPNOTSUPP;
2463}
2464
Mugunthan V Ndf828592012-03-18 20:17:54 +00002465static const struct ethtool_ops cpsw_ethtool_ops = {
2466 .get_drvinfo = cpsw_get_drvinfo,
2467 .get_msglevel = cpsw_get_msglevel,
2468 .set_msglevel = cpsw_set_msglevel,
2469 .get_link = ethtool_op_get_link,
Richard Cochran2e5b38a2012-10-29 08:45:20 +00002470 .get_ts_info = cpsw_get_ts_info,
Mugunthan V Nff5b8ef2013-03-11 23:16:37 +00002471 .get_coalesce = cpsw_get_coalesce,
2472 .set_coalesce = cpsw_set_coalesce,
Mugunthan V Nd9718542013-07-23 15:38:17 +05302473 .get_sset_count = cpsw_get_sset_count,
2474 .get_strings = cpsw_get_strings,
2475 .get_ethtool_stats = cpsw_get_ethtool_stats,
Mugunthan V N1923d6e2014-09-08 22:54:02 +05302476 .get_pauseparam = cpsw_get_pauseparam,
2477 .set_pauseparam = cpsw_set_pauseparam,
Matus Ujhelyid8a64422013-08-20 07:59:38 +02002478 .get_wol = cpsw_get_wol,
2479 .set_wol = cpsw_set_wol,
Mugunthan V N52c4f0e2014-07-22 23:25:07 +05302480 .get_regs_len = cpsw_get_regs_len,
2481 .get_regs = cpsw_get_regs,
Grygorii Strashko7898b1d2016-06-24 21:23:44 +03002482 .begin = cpsw_ethtool_op_begin,
2483 .complete = cpsw_ethtool_op_complete,
Ivan Khoronzhukce52c742016-08-22 21:18:28 +03002484 .get_channels = cpsw_get_channels,
2485 .set_channels = cpsw_set_channels,
Philippe Reynes24798762016-10-08 17:46:15 +02002486 .get_link_ksettings = cpsw_get_link_ksettings,
2487 .set_link_ksettings = cpsw_set_link_ksettings,
Yegor Yefremova0909942016-11-28 09:41:33 +01002488 .get_eee = cpsw_get_eee,
2489 .set_eee = cpsw_set_eee,
Yegor Yefremov6bb10c22016-11-28 10:47:52 +01002490 .nway_reset = cpsw_nway_reset,
Mugunthan V Ndf828592012-03-18 20:17:54 +00002491};
2492
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002493static void cpsw_slave_init(struct cpsw_slave *slave, struct cpsw_common *cpsw,
Richard Cochran549985e2012-11-14 09:07:56 +00002494 u32 slave_reg_ofs, u32 sliver_reg_ofs)
Mugunthan V Ndf828592012-03-18 20:17:54 +00002495{
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03002496 void __iomem *regs = cpsw->regs;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002497 int slave_num = slave->slave_num;
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002498 struct cpsw_slave_data *data = cpsw->data.slave_data + slave_num;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002499
2500 slave->data = data;
Richard Cochran549985e2012-11-14 09:07:56 +00002501 slave->regs = regs + slave_reg_ofs;
2502 slave->sliver = regs + sliver_reg_ofs;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002503 slave->port_vlan = data->dual_emac_res_vlan;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002504}
2505
David Rivshin552165b2016-04-27 21:25:25 -04002506static int cpsw_probe_dt(struct cpsw_platform_data *data,
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002507 struct platform_device *pdev)
2508{
2509 struct device_node *node = pdev->dev.of_node;
2510 struct device_node *slave_node;
2511 int i = 0, ret;
2512 u32 prop;
2513
2514 if (!node)
2515 return -EINVAL;
2516
2517 if (of_property_read_u32(node, "slaves", &prop)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302518 dev_err(&pdev->dev, "Missing slaves property in the DT.\n");
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002519 return -EINVAL;
2520 }
2521 data->slaves = prop;
2522
Mugunthan V Ne86ac132013-03-11 23:16:35 +00002523 if (of_property_read_u32(node, "active_slave", &prop)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302524 dev_err(&pdev->dev, "Missing active_slave property in the DT.\n");
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302525 return -EINVAL;
Richard Cochran78ca0b22012-10-29 08:45:18 +00002526 }
Mugunthan V Ne86ac132013-03-11 23:16:35 +00002527 data->active_slave = prop;
Richard Cochran78ca0b22012-10-29 08:45:18 +00002528
Richard Cochran00ab94e2012-10-29 08:45:19 +00002529 if (of_property_read_u32(node, "cpts_clock_mult", &prop)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302530 dev_err(&pdev->dev, "Missing cpts_clock_mult property in the DT.\n");
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302531 return -EINVAL;
Richard Cochran00ab94e2012-10-29 08:45:19 +00002532 }
2533 data->cpts_clock_mult = prop;
2534
2535 if (of_property_read_u32(node, "cpts_clock_shift", &prop)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302536 dev_err(&pdev->dev, "Missing cpts_clock_shift property in the DT.\n");
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302537 return -EINVAL;
Richard Cochran00ab94e2012-10-29 08:45:19 +00002538 }
2539 data->cpts_clock_shift = prop;
2540
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302541 data->slave_data = devm_kzalloc(&pdev->dev, data->slaves
2542 * sizeof(struct cpsw_slave_data),
2543 GFP_KERNEL);
Joe Perchesb2adaca2013-02-03 17:43:58 +00002544 if (!data->slave_data)
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302545 return -ENOMEM;
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002546
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002547 if (of_property_read_u32(node, "cpdma_channels", &prop)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302548 dev_err(&pdev->dev, "Missing cpdma_channels property in the DT.\n");
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302549 return -EINVAL;
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002550 }
2551 data->channels = prop;
2552
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002553 if (of_property_read_u32(node, "ale_entries", &prop)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302554 dev_err(&pdev->dev, "Missing ale_entries property in the DT.\n");
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302555 return -EINVAL;
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002556 }
2557 data->ale_entries = prop;
2558
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002559 if (of_property_read_u32(node, "bd_ram_size", &prop)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302560 dev_err(&pdev->dev, "Missing bd_ram_size property in the DT.\n");
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302561 return -EINVAL;
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002562 }
2563 data->bd_ram_size = prop;
2564
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002565 if (of_property_read_u32(node, "mac_control", &prop)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302566 dev_err(&pdev->dev, "Missing mac_control property in the DT.\n");
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302567 return -EINVAL;
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002568 }
2569 data->mac_control = prop;
2570
Markus Pargmann281abd92013-10-04 14:44:40 +02002571 if (of_property_read_bool(node, "dual_emac"))
2572 data->dual_emac = 1;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002573
Vaibhav Hiremath1fb19aa2012-11-14 09:07:55 +00002574 /*
2575 * Populate all the child nodes here...
2576 */
2577 ret = of_platform_populate(node, NULL, NULL, &pdev->dev);
2578 /* We do not want to force this, as in some cases may not have child */
2579 if (ret)
George Cherian88c99ff2014-05-12 10:21:19 +05302580 dev_warn(&pdev->dev, "Doesn't have any child node\n");
Vaibhav Hiremath1fb19aa2012-11-14 09:07:55 +00002581
Ben Hutchings8658aaf2016-06-21 01:16:31 +01002582 for_each_available_child_of_node(node, slave_node) {
Richard Cochran549985e2012-11-14 09:07:56 +00002583 struct cpsw_slave_data *slave_data = data->slave_data + i;
2584 const void *mac_addr = NULL;
Richard Cochran549985e2012-11-14 09:07:56 +00002585 int lenp;
2586 const __be32 *parp;
Richard Cochran549985e2012-11-14 09:07:56 +00002587
Markus Pargmannf468b102013-10-04 14:44:39 +02002588 /* This is no slave child node, continue */
2589 if (strcmp(slave_node->name, "slave"))
2590 continue;
2591
David Rivshin552165b2016-04-27 21:25:25 -04002592 slave_data->phy_node = of_parse_phandle(slave_node,
2593 "phy-handle", 0);
David Rivshinf1eea5c2015-12-16 23:02:10 -05002594 parp = of_get_property(slave_node, "phy_id", &lenp);
David Rivshinae092b52016-04-27 21:38:26 -04002595 if (slave_data->phy_node) {
2596 dev_dbg(&pdev->dev,
2597 "slave[%d] using phy-handle=\"%s\"\n",
2598 i, slave_data->phy_node->full_name);
2599 } else if (of_phy_is_fixed_link(slave_node)) {
David Rivshindfc0a6d2015-12-16 23:02:11 -05002600 /* In the case of a fixed PHY, the DT node associated
2601 * to the PHY is the Ethernet MAC DT node.
2602 */
Markus Brunner1f71e8c2015-11-03 22:09:51 +01002603 ret = of_phy_register_fixed_link(slave_node);
Johan Hovold23a09872016-11-17 17:40:04 +01002604 if (ret) {
2605 if (ret != -EPROBE_DEFER)
2606 dev_err(&pdev->dev, "failed to register fixed-link phy: %d\n", ret);
Markus Brunner1f71e8c2015-11-03 22:09:51 +01002607 return ret;
Johan Hovold23a09872016-11-17 17:40:04 +01002608 }
David Rivshin06cd6d62016-04-27 21:45:45 -04002609 slave_data->phy_node = of_node_get(slave_node);
David Rivshinf1eea5c2015-12-16 23:02:10 -05002610 } else if (parp) {
2611 u32 phyid;
2612 struct device_node *mdio_node;
2613 struct platform_device *mdio;
2614
2615 if (lenp != (sizeof(__be32) * 2)) {
2616 dev_err(&pdev->dev, "Invalid slave[%d] phy_id property\n", i);
2617 goto no_phy_slave;
2618 }
2619 mdio_node = of_find_node_by_phandle(be32_to_cpup(parp));
2620 phyid = be32_to_cpup(parp+1);
2621 mdio = of_find_device_by_node(mdio_node);
2622 of_node_put(mdio_node);
2623 if (!mdio) {
2624 dev_err(&pdev->dev, "Missing mdio platform device\n");
2625 return -EINVAL;
2626 }
2627 snprintf(slave_data->phy_id, sizeof(slave_data->phy_id),
2628 PHY_ID_FMT, mdio->name, phyid);
Johan Hovold86e1d5a2016-11-17 17:39:59 +01002629 put_device(&mdio->dev);
David Rivshinf1eea5c2015-12-16 23:02:10 -05002630 } else {
David Rivshinae092b52016-04-27 21:38:26 -04002631 dev_err(&pdev->dev,
2632 "No slave[%d] phy_id, phy-handle, or fixed-link property\n",
2633 i);
Markus Brunner1f71e8c2015-11-03 22:09:51 +01002634 goto no_phy_slave;
2635 }
Mugunthan V N47276fc2014-10-24 18:51:33 +05302636 slave_data->phy_if = of_get_phy_mode(slave_node);
2637 if (slave_data->phy_if < 0) {
2638 dev_err(&pdev->dev, "Missing or malformed slave[%d] phy-mode property\n",
2639 i);
2640 return slave_data->phy_if;
2641 }
2642
2643no_phy_slave:
Richard Cochran549985e2012-11-14 09:07:56 +00002644 mac_addr = of_get_mac_address(slave_node);
Markus Pargmann0ba517b2014-09-29 08:53:17 +02002645 if (mac_addr) {
Richard Cochran549985e2012-11-14 09:07:56 +00002646 memcpy(slave_data->mac_addr, mac_addr, ETH_ALEN);
Markus Pargmann0ba517b2014-09-29 08:53:17 +02002647 } else {
Mugunthan V Nb6745f62015-09-21 15:56:50 +05302648 ret = ti_cm_get_macid(&pdev->dev, i,
2649 slave_data->mac_addr);
2650 if (ret)
2651 return ret;
Markus Pargmann0ba517b2014-09-29 08:53:17 +02002652 }
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002653 if (data->dual_emac) {
Mugunthan V N91c41662013-04-15 07:31:28 +00002654 if (of_property_read_u32(slave_node, "dual_emac_res_vlan",
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002655 &prop)) {
George Cherian88c99ff2014-05-12 10:21:19 +05302656 dev_err(&pdev->dev, "Missing dual_emac_res_vlan in DT.\n");
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002657 slave_data->dual_emac_res_vlan = i+1;
George Cherian88c99ff2014-05-12 10:21:19 +05302658 dev_err(&pdev->dev, "Using %d as Reserved VLAN for %d slave\n",
2659 slave_data->dual_emac_res_vlan, i);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002660 } else {
2661 slave_data->dual_emac_res_vlan = prop;
2662 }
2663 }
2664
Richard Cochran549985e2012-11-14 09:07:56 +00002665 i++;
Mugunthan V N3a27bfa2013-12-02 12:53:39 +05302666 if (i == data->slaves)
2667 break;
Richard Cochran549985e2012-11-14 09:07:56 +00002668 }
2669
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002670 return 0;
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002671}
2672
Johan Hovolda4e32b02016-11-17 17:40:00 +01002673static void cpsw_remove_dt(struct platform_device *pdev)
2674{
Johan Hovold8cbcc462016-11-17 17:40:01 +01002675 struct net_device *ndev = platform_get_drvdata(pdev);
2676 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
2677 struct cpsw_platform_data *data = &cpsw->data;
2678 struct device_node *node = pdev->dev.of_node;
2679 struct device_node *slave_node;
2680 int i = 0;
2681
2682 for_each_available_child_of_node(node, slave_node) {
2683 struct cpsw_slave_data *slave_data = &data->slave_data[i];
2684
2685 if (strcmp(slave_node->name, "slave"))
2686 continue;
2687
Johan Hovold3f650472016-11-28 19:24:55 +01002688 if (of_phy_is_fixed_link(slave_node))
2689 of_phy_deregister_fixed_link(slave_node);
Johan Hovold8cbcc462016-11-17 17:40:01 +01002690
2691 of_node_put(slave_data->phy_node);
2692
2693 i++;
2694 if (i == data->slaves)
2695 break;
2696 }
2697
Johan Hovolda4e32b02016-11-17 17:40:00 +01002698 of_platform_depopulate(&pdev->dev);
2699}
2700
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002701static int cpsw_probe_dual_emac(struct cpsw_priv *priv)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002702{
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002703 struct cpsw_common *cpsw = priv->cpsw;
2704 struct cpsw_platform_data *data = &cpsw->data;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002705 struct net_device *ndev;
2706 struct cpsw_priv *priv_sl2;
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +03002707 int ret = 0;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002708
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03002709 ndev = alloc_etherdev_mq(sizeof(struct cpsw_priv), CPSW_MAX_QUEUES);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002710 if (!ndev) {
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002711 dev_err(cpsw->dev, "cpsw: error allocating net_device\n");
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002712 return -ENOMEM;
2713 }
2714
2715 priv_sl2 = netdev_priv(ndev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002716 priv_sl2->cpsw = cpsw;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002717 priv_sl2->ndev = ndev;
2718 priv_sl2->dev = &ndev->dev;
2719 priv_sl2->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002720
2721 if (is_valid_ether_addr(data->slave_data[1].mac_addr)) {
2722 memcpy(priv_sl2->mac_addr, data->slave_data[1].mac_addr,
2723 ETH_ALEN);
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002724 dev_info(cpsw->dev, "cpsw: Detected MACID = %pM\n",
2725 priv_sl2->mac_addr);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002726 } else {
2727 random_ether_addr(priv_sl2->mac_addr);
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002728 dev_info(cpsw->dev, "cpsw: Random MACID = %pM\n",
2729 priv_sl2->mac_addr);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002730 }
2731 memcpy(ndev->dev_addr, priv_sl2->mac_addr, ETH_ALEN);
2732
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002733 priv_sl2->emac_port = 1;
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002734 cpsw->slaves[1].ndev = ndev;
Patrick McHardyf6469682013-04-19 02:04:27 +00002735 ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002736
2737 ndev->netdev_ops = &cpsw_netdev_ops;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00002738 ndev->ethtool_ops = &cpsw_ethtool_ops;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002739
2740 /* register the network device */
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002741 SET_NETDEV_DEV(ndev, cpsw->dev);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002742 ret = register_netdev(ndev);
2743 if (ret) {
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002744 dev_err(cpsw->dev, "cpsw: error registering net device\n");
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002745 free_netdev(ndev);
2746 ret = -ENODEV;
2747 }
2748
2749 return ret;
2750}
2751
Mugunthan V N7da11602015-08-12 15:22:53 +05302752#define CPSW_QUIRK_IRQ BIT(0)
2753
2754static struct platform_device_id cpsw_devtype[] = {
2755 {
2756 /* keep it for existing comaptibles */
2757 .name = "cpsw",
2758 .driver_data = CPSW_QUIRK_IRQ,
2759 }, {
2760 .name = "am335x-cpsw",
2761 .driver_data = CPSW_QUIRK_IRQ,
2762 }, {
2763 .name = "am4372-cpsw",
2764 .driver_data = 0,
2765 }, {
2766 .name = "dra7-cpsw",
2767 .driver_data = 0,
2768 }, {
2769 /* sentinel */
2770 }
2771};
2772MODULE_DEVICE_TABLE(platform, cpsw_devtype);
2773
2774enum ti_cpsw_type {
2775 CPSW = 0,
2776 AM335X_CPSW,
2777 AM4372_CPSW,
2778 DRA7_CPSW,
2779};
2780
2781static const struct of_device_id cpsw_of_mtable[] = {
2782 { .compatible = "ti,cpsw", .data = &cpsw_devtype[CPSW], },
2783 { .compatible = "ti,am335x-cpsw", .data = &cpsw_devtype[AM335X_CPSW], },
2784 { .compatible = "ti,am4372-cpsw", .data = &cpsw_devtype[AM4372_CPSW], },
2785 { .compatible = "ti,dra7-cpsw", .data = &cpsw_devtype[DRA7_CPSW], },
2786 { /* sentinel */ },
2787};
2788MODULE_DEVICE_TABLE(of, cpsw_of_mtable);
2789
Bill Pemberton663e12e2012-12-03 09:23:45 -05002790static int cpsw_probe(struct platform_device *pdev)
Mugunthan V Ndf828592012-03-18 20:17:54 +00002791{
Ivan Khoronzhukef4183a2016-08-10 02:22:35 +03002792 struct clk *clk;
Sebastian Siewiord1bd9ac2013-04-24 08:48:23 +00002793 struct cpsw_platform_data *data;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002794 struct net_device *ndev;
2795 struct cpsw_priv *priv;
2796 struct cpdma_params dma_params;
2797 struct cpsw_ale_params ale_params;
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302798 void __iomem *ss_regs;
2799 struct resource *res, *ss_res;
Mugunthan V N7da11602015-08-12 15:22:53 +05302800 const struct of_device_id *of_id;
Mugunthan V N1d147cc2015-09-07 15:16:44 +05302801 struct gpio_descs *mode;
Richard Cochran549985e2012-11-14 09:07:56 +00002802 u32 slave_offset, sliver_offset, slave_size;
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03002803 struct cpsw_common *cpsw;
Felipe Balbi5087b912015-01-16 10:11:11 -06002804 int ret = 0, i;
2805 int irq;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002806
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03002807 cpsw = devm_kzalloc(&pdev->dev, sizeof(struct cpsw_common), GFP_KERNEL);
Johan Hovold3420ea82016-11-17 17:40:03 +01002808 if (!cpsw)
2809 return -ENOMEM;
2810
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03002811 cpsw->dev = &pdev->dev;
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03002812
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03002813 ndev = alloc_etherdev_mq(sizeof(struct cpsw_priv), CPSW_MAX_QUEUES);
Mugunthan V Ndf828592012-03-18 20:17:54 +00002814 if (!ndev) {
George Cherian88c99ff2014-05-12 10:21:19 +05302815 dev_err(&pdev->dev, "error allocating net_device\n");
Mugunthan V Ndf828592012-03-18 20:17:54 +00002816 return -ENOMEM;
2817 }
2818
2819 platform_set_drvdata(pdev, ndev);
2820 priv = netdev_priv(ndev);
Ivan Khoronzhuk649a1682016-08-10 02:22:37 +03002821 priv->cpsw = cpsw;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002822 priv->ndev = ndev;
2823 priv->dev = &ndev->dev;
2824 priv->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002825 cpsw->rx_packet_max = max(rx_packet_max, 128);
2826 cpsw->cpts = devm_kzalloc(&pdev->dev, sizeof(struct cpts), GFP_KERNEL);
2827 if (!cpsw->cpts) {
George Cherian88c99ff2014-05-12 10:21:19 +05302828 dev_err(&pdev->dev, "error allocating cpts\n");
Markus Pargmann4d507df2014-09-29 08:53:14 +02002829 ret = -ENOMEM;
Mugunthan V N9232b162013-02-11 09:52:19 +00002830 goto clean_ndev_ret;
2831 }
Mugunthan V Ndf828592012-03-18 20:17:54 +00002832
Mugunthan V N1d147cc2015-09-07 15:16:44 +05302833 mode = devm_gpiod_get_array_optional(&pdev->dev, "mode", GPIOD_OUT_LOW);
2834 if (IS_ERR(mode)) {
2835 ret = PTR_ERR(mode);
2836 dev_err(&pdev->dev, "gpio request failed, ret %d\n", ret);
2837 goto clean_ndev_ret;
2838 }
2839
Vaibhav Hiremath1fb19aa2012-11-14 09:07:55 +00002840 /*
2841 * This may be required here for child devices.
2842 */
2843 pm_runtime_enable(&pdev->dev);
2844
Mugunthan V N739683b2013-06-06 23:45:14 +05302845 /* Select default pin state */
2846 pinctrl_pm_select_default_state(&pdev->dev);
2847
Johan Hovolda4e32b02016-11-17 17:40:00 +01002848 /* Need to enable clocks with runtime PM api to access module
2849 * registers
2850 */
2851 ret = pm_runtime_get_sync(&pdev->dev);
2852 if (ret < 0) {
2853 pm_runtime_put_noidle(&pdev->dev);
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302854 goto clean_runtime_disable_ret;
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002855 }
Johan Hovolda4e32b02016-11-17 17:40:00 +01002856
Johan Hovold23a09872016-11-17 17:40:04 +01002857 ret = cpsw_probe_dt(&cpsw->data, pdev);
2858 if (ret)
Johan Hovolda4e32b02016-11-17 17:40:00 +01002859 goto clean_dt_ret;
Johan Hovold23a09872016-11-17 17:40:04 +01002860
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002861 data = &cpsw->data;
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +03002862 cpsw->rx_ch_num = 1;
2863 cpsw->tx_ch_num = 1;
Mugunthan V N2eb32b02012-07-30 10:17:14 +00002864
Mugunthan V Ndf828592012-03-18 20:17:54 +00002865 if (is_valid_ether_addr(data->slave_data[0].mac_addr)) {
2866 memcpy(priv->mac_addr, data->slave_data[0].mac_addr, ETH_ALEN);
George Cherian88c99ff2014-05-12 10:21:19 +05302867 dev_info(&pdev->dev, "Detected MACID = %pM\n", priv->mac_addr);
Mugunthan V Ndf828592012-03-18 20:17:54 +00002868 } else {
Joe Perches7efd26d2012-07-12 19:33:06 +00002869 eth_random_addr(priv->mac_addr);
George Cherian88c99ff2014-05-12 10:21:19 +05302870 dev_info(&pdev->dev, "Random MACID = %pM\n", priv->mac_addr);
Mugunthan V Ndf828592012-03-18 20:17:54 +00002871 }
2872
2873 memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
2874
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002875 cpsw->slaves = devm_kzalloc(&pdev->dev,
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302876 sizeof(struct cpsw_slave) * data->slaves,
2877 GFP_KERNEL);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002878 if (!cpsw->slaves) {
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302879 ret = -ENOMEM;
Johan Hovolda4e32b02016-11-17 17:40:00 +01002880 goto clean_dt_ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002881 }
2882 for (i = 0; i < data->slaves; i++)
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002883 cpsw->slaves[i].slave_num = i;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002884
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002885 cpsw->slaves[0].ndev = ndev;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00002886 priv->emac_port = 0;
2887
Ivan Khoronzhukef4183a2016-08-10 02:22:35 +03002888 clk = devm_clk_get(&pdev->dev, "fck");
2889 if (IS_ERR(clk)) {
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302890 dev_err(priv->dev, "fck is not found\n");
Mugunthan V Nf150bd72012-07-17 08:09:50 +00002891 ret = -ENODEV;
Johan Hovolda4e32b02016-11-17 17:40:00 +01002892 goto clean_dt_ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002893 }
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002894 cpsw->bus_freq_mhz = clk_get_rate(clk) / 1000000;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002895
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302896 ss_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2897 ss_regs = devm_ioremap_resource(&pdev->dev, ss_res);
2898 if (IS_ERR(ss_regs)) {
2899 ret = PTR_ERR(ss_regs);
Johan Hovolda4e32b02016-11-17 17:40:00 +01002900 goto clean_dt_ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002901 }
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03002902 cpsw->regs = ss_regs;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002903
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002904 cpsw->version = readl(&cpsw->regs->id_ver);
Mugunthan V Nf280e892013-12-11 22:09:05 -06002905
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302906 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03002907 cpsw->wr_regs = devm_ioremap_resource(&pdev->dev, res);
2908 if (IS_ERR(cpsw->wr_regs)) {
2909 ret = PTR_ERR(cpsw->wr_regs);
Johan Hovolda4e32b02016-11-17 17:40:00 +01002910 goto clean_dt_ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002911 }
Mugunthan V Ndf828592012-03-18 20:17:54 +00002912
2913 memset(&dma_params, 0, sizeof(dma_params));
Richard Cochran549985e2012-11-14 09:07:56 +00002914 memset(&ale_params, 0, sizeof(ale_params));
2915
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002916 switch (cpsw->version) {
Richard Cochran549985e2012-11-14 09:07:56 +00002917 case CPSW_VERSION_1:
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03002918 cpsw->host_port_regs = ss_regs + CPSW1_HOST_PORT_OFFSET;
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002919 cpsw->cpts->reg = ss_regs + CPSW1_CPTS_OFFSET;
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03002920 cpsw->hw_stats = ss_regs + CPSW1_HW_STATS;
Richard Cochran549985e2012-11-14 09:07:56 +00002921 dma_params.dmaregs = ss_regs + CPSW1_CPDMA_OFFSET;
2922 dma_params.txhdp = ss_regs + CPSW1_STATERAM_OFFSET;
2923 ale_params.ale_regs = ss_regs + CPSW1_ALE_OFFSET;
2924 slave_offset = CPSW1_SLAVE_OFFSET;
2925 slave_size = CPSW1_SLAVE_SIZE;
2926 sliver_offset = CPSW1_SLIVER_OFFSET;
2927 dma_params.desc_mem_phys = 0;
2928 break;
2929 case CPSW_VERSION_2:
Mugunthan V Nc193f362013-08-05 17:30:05 +05302930 case CPSW_VERSION_3:
Mugunthan V N926489b2013-08-12 17:11:15 +05302931 case CPSW_VERSION_4:
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03002932 cpsw->host_port_regs = ss_regs + CPSW2_HOST_PORT_OFFSET;
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002933 cpsw->cpts->reg = ss_regs + CPSW2_CPTS_OFFSET;
Ivan Khoronzhuk5d8d0d42016-08-10 02:22:39 +03002934 cpsw->hw_stats = ss_regs + CPSW2_HW_STATS;
Richard Cochran549985e2012-11-14 09:07:56 +00002935 dma_params.dmaregs = ss_regs + CPSW2_CPDMA_OFFSET;
2936 dma_params.txhdp = ss_regs + CPSW2_STATERAM_OFFSET;
2937 ale_params.ale_regs = ss_regs + CPSW2_ALE_OFFSET;
2938 slave_offset = CPSW2_SLAVE_OFFSET;
2939 slave_size = CPSW2_SLAVE_SIZE;
2940 sliver_offset = CPSW2_SLIVER_OFFSET;
2941 dma_params.desc_mem_phys =
Daniel Mackaa1a15e2013-09-21 00:50:38 +05302942 (u32 __force) ss_res->start + CPSW2_BD_OFFSET;
Richard Cochran549985e2012-11-14 09:07:56 +00002943 break;
2944 default:
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002945 dev_err(priv->dev, "unknown version 0x%08x\n", cpsw->version);
Richard Cochran549985e2012-11-14 09:07:56 +00002946 ret = -ENODEV;
Johan Hovolda4e32b02016-11-17 17:40:00 +01002947 goto clean_dt_ret;
Richard Cochran549985e2012-11-14 09:07:56 +00002948 }
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03002949 for (i = 0; i < cpsw->data.slaves; i++) {
2950 struct cpsw_slave *slave = &cpsw->slaves[i];
2951
2952 cpsw_slave_init(slave, cpsw, slave_offset, sliver_offset);
Richard Cochran549985e2012-11-14 09:07:56 +00002953 slave_offset += slave_size;
2954 sliver_offset += SLIVER_SIZE;
2955 }
2956
Mugunthan V Ndf828592012-03-18 20:17:54 +00002957 dma_params.dev = &pdev->dev;
Richard Cochran549985e2012-11-14 09:07:56 +00002958 dma_params.rxthresh = dma_params.dmaregs + CPDMA_RXTHRESH;
2959 dma_params.rxfree = dma_params.dmaregs + CPDMA_RXFREE;
2960 dma_params.rxhdp = dma_params.txhdp + CPDMA_RXHDP;
2961 dma_params.txcp = dma_params.txhdp + CPDMA_TXCP;
2962 dma_params.rxcp = dma_params.txhdp + CPDMA_RXCP;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002963
2964 dma_params.num_chan = data->channels;
2965 dma_params.has_soft_reset = true;
2966 dma_params.min_packet_size = CPSW_MIN_PACKET_SIZE;
2967 dma_params.desc_mem_size = data->bd_ram_size;
2968 dma_params.desc_align = 16;
2969 dma_params.has_ext_regs = true;
Richard Cochran549985e2012-11-14 09:07:56 +00002970 dma_params.desc_hw_addr = dma_params.desc_mem_phys;
Ivan Khoronzhuk83fcad02016-11-29 17:00:49 +02002971 dma_params.bus_freq_mhz = cpsw->bus_freq_mhz;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002972
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03002973 cpsw->dma = cpdma_ctlr_create(&dma_params);
2974 if (!cpsw->dma) {
Mugunthan V Ndf828592012-03-18 20:17:54 +00002975 dev_err(priv->dev, "error initializing dma\n");
2976 ret = -ENOMEM;
Johan Hovolda4e32b02016-11-17 17:40:00 +01002977 goto clean_dt_ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002978 }
2979
Ivan Khoronzhuk8feb0a12016-11-29 17:00:51 +02002980 cpsw->txv[0].ch = cpdma_chan_create(cpsw->dma, 0, cpsw_tx_handler, 0);
2981 cpsw->rxv[0].ch = cpdma_chan_create(cpsw->dma, 0, cpsw_rx_handler, 1);
2982 if (WARN_ON(!cpsw->rxv[0].ch || !cpsw->txv[0].ch)) {
Mugunthan V Ndf828592012-03-18 20:17:54 +00002983 dev_err(priv->dev, "error initializing dma channels\n");
2984 ret = -ENOMEM;
2985 goto clean_dma_ret;
2986 }
2987
Mugunthan V Ndf828592012-03-18 20:17:54 +00002988 ale_params.dev = &ndev->dev;
Mugunthan V Ndf828592012-03-18 20:17:54 +00002989 ale_params.ale_ageout = ale_ageout;
2990 ale_params.ale_entries = data->ale_entries;
2991 ale_params.ale_ports = data->slaves;
2992
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03002993 cpsw->ale = cpsw_ale_create(&ale_params);
2994 if (!cpsw->ale) {
Mugunthan V Ndf828592012-03-18 20:17:54 +00002995 dev_err(priv->dev, "error initializing ale engine\n");
2996 ret = -ENODEV;
2997 goto clean_dma_ret;
2998 }
2999
Felipe Balbic03abd82015-01-16 10:11:12 -06003000 ndev->irq = platform_get_irq(pdev, 1);
Mugunthan V Ndf828592012-03-18 20:17:54 +00003001 if (ndev->irq < 0) {
3002 dev_err(priv->dev, "error getting irq resource\n");
Julia Lawallc1e33342015-12-26 20:12:13 +01003003 ret = ndev->irq;
Mugunthan V Ndf828592012-03-18 20:17:54 +00003004 goto clean_ale_ret;
3005 }
3006
Mugunthan V N7da11602015-08-12 15:22:53 +05303007 of_id = of_match_device(cpsw_of_mtable, &pdev->dev);
3008 if (of_id) {
3009 pdev->id_entry = of_id->data;
3010 if (pdev->id_entry->driver_data)
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +03003011 cpsw->quirk_irq = true;
Mugunthan V N7da11602015-08-12 15:22:53 +05303012 }
3013
Felipe Balbic03abd82015-01-16 10:11:12 -06003014 /* Grab RX and TX IRQs. Note that we also have RX_THRESHOLD and
3015 * MISC IRQs which are always kept disabled with this driver so
3016 * we will not request them.
3017 *
3018 * If anyone wants to implement support for those, make sure to
3019 * first request and append them to irqs_table array.
3020 */
Daniel Mackc2b32e52014-09-04 09:00:23 +02003021
Felipe Balbic03abd82015-01-16 10:11:12 -06003022 /* RX IRQ */
Felipe Balbi5087b912015-01-16 10:11:11 -06003023 irq = platform_get_irq(pdev, 1);
Julia Lawallc1e33342015-12-26 20:12:13 +01003024 if (irq < 0) {
3025 ret = irq;
Felipe Balbi5087b912015-01-16 10:11:11 -06003026 goto clean_ale_ret;
Julia Lawallc1e33342015-12-26 20:12:13 +01003027 }
Felipe Balbi5087b912015-01-16 10:11:11 -06003028
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +03003029 cpsw->irqs_table[0] = irq;
Felipe Balbic03abd82015-01-16 10:11:12 -06003030 ret = devm_request_irq(&pdev->dev, irq, cpsw_rx_interrupt,
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +03003031 0, dev_name(&pdev->dev), cpsw);
Felipe Balbi5087b912015-01-16 10:11:11 -06003032 if (ret < 0) {
3033 dev_err(priv->dev, "error attaching irq (%d)\n", ret);
3034 goto clean_ale_ret;
3035 }
3036
Felipe Balbic03abd82015-01-16 10:11:12 -06003037 /* TX IRQ */
Felipe Balbi5087b912015-01-16 10:11:11 -06003038 irq = platform_get_irq(pdev, 2);
Julia Lawallc1e33342015-12-26 20:12:13 +01003039 if (irq < 0) {
3040 ret = irq;
Felipe Balbi5087b912015-01-16 10:11:11 -06003041 goto clean_ale_ret;
Julia Lawallc1e33342015-12-26 20:12:13 +01003042 }
Felipe Balbi5087b912015-01-16 10:11:11 -06003043
Ivan Khoronzhuke38b5a32016-08-10 02:22:41 +03003044 cpsw->irqs_table[1] = irq;
Felipe Balbic03abd82015-01-16 10:11:12 -06003045 ret = devm_request_irq(&pdev->dev, irq, cpsw_tx_interrupt,
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +03003046 0, dev_name(&pdev->dev), cpsw);
Felipe Balbi5087b912015-01-16 10:11:11 -06003047 if (ret < 0) {
3048 dev_err(priv->dev, "error attaching irq (%d)\n", ret);
3049 goto clean_ale_ret;
3050 }
Daniel Mackc2b32e52014-09-04 09:00:23 +02003051
Patrick McHardyf6469682013-04-19 02:04:27 +00003052 ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
Mugunthan V Ndf828592012-03-18 20:17:54 +00003053
3054 ndev->netdev_ops = &cpsw_netdev_ops;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00003055 ndev->ethtool_ops = &cpsw_ethtool_ops;
Ivan Khoronzhukdbc4ec52016-08-10 02:22:43 +03003056 netif_napi_add(ndev, &cpsw->napi_rx, cpsw_rx_poll, CPSW_POLL_WEIGHT);
3057 netif_tx_napi_add(ndev, &cpsw->napi_tx, cpsw_tx_poll, CPSW_POLL_WEIGHT);
Mugunthan V Ndf828592012-03-18 20:17:54 +00003058
3059 /* register the network device */
3060 SET_NETDEV_DEV(ndev, &pdev->dev);
3061 ret = register_netdev(ndev);
3062 if (ret) {
3063 dev_err(priv->dev, "error registering net device\n");
3064 ret = -ENODEV;
Daniel Mackaa1a15e2013-09-21 00:50:38 +05303065 goto clean_ale_ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00003066 }
3067
Olof Johansson1a3b5052013-12-11 15:58:07 -08003068 cpsw_notice(priv, probe, "initialized device (regs %pa, irq %d)\n",
3069 &ss_res->start, ndev->irq);
Mugunthan V Ndf828592012-03-18 20:17:54 +00003070
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03003071 if (cpsw->data.dual_emac) {
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03003072 ret = cpsw_probe_dual_emac(priv);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00003073 if (ret) {
3074 cpsw_err(priv, probe, "error probe slave 2 emac interface\n");
Johan Hovolda7fe9d42016-11-17 17:40:02 +01003075 goto clean_unregister_netdev_ret;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00003076 }
3077 }
3078
Johan Hovoldc46ab7e2016-11-17 17:39:58 +01003079 pm_runtime_put(&pdev->dev);
3080
Mugunthan V Ndf828592012-03-18 20:17:54 +00003081 return 0;
3082
Johan Hovolda7fe9d42016-11-17 17:40:02 +01003083clean_unregister_netdev_ret:
3084 unregister_netdev(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00003085clean_ale_ret:
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03003086 cpsw_ale_destroy(cpsw->ale);
Mugunthan V Ndf828592012-03-18 20:17:54 +00003087clean_dma_ret:
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03003088 cpdma_ctlr_destroy(cpsw->dma);
Johan Hovolda4e32b02016-11-17 17:40:00 +01003089clean_dt_ret:
3090 cpsw_remove_dt(pdev);
Johan Hovoldc46ab7e2016-11-17 17:39:58 +01003091 pm_runtime_put_sync(&pdev->dev);
Daniel Mackaa1a15e2013-09-21 00:50:38 +05303092clean_runtime_disable_ret:
Mugunthan V Nf150bd72012-07-17 08:09:50 +00003093 pm_runtime_disable(&pdev->dev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00003094clean_ndev_ret:
Sebastian Siewiord1bd9ac2013-04-24 08:48:23 +00003095 free_netdev(priv->ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00003096 return ret;
3097}
3098
Bill Pemberton663e12e2012-12-03 09:23:45 -05003099static int cpsw_remove(struct platform_device *pdev)
Mugunthan V Ndf828592012-03-18 20:17:54 +00003100{
3101 struct net_device *ndev = platform_get_drvdata(pdev);
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03003102 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Grygorii Strashko8a0b6dc2016-07-28 20:50:35 +03003103 int ret;
3104
3105 ret = pm_runtime_get_sync(&pdev->dev);
3106 if (ret < 0) {
3107 pm_runtime_put_noidle(&pdev->dev);
3108 return ret;
3109 }
Mugunthan V Ndf828592012-03-18 20:17:54 +00003110
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03003111 if (cpsw->data.dual_emac)
3112 unregister_netdev(cpsw->slaves[1].ndev);
Sebastian Siewiord1bd9ac2013-04-24 08:48:23 +00003113 unregister_netdev(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00003114
Ivan Khoronzhuk2a05a622016-08-10 02:22:44 +03003115 cpsw_ale_destroy(cpsw->ale);
Ivan Khoronzhuk2c836bd2016-08-10 02:22:40 +03003116 cpdma_ctlr_destroy(cpsw->dma);
Johan Hovolda4e32b02016-11-17 17:40:00 +01003117 cpsw_remove_dt(pdev);
Grygorii Strashko8a0b6dc2016-07-28 20:50:35 +03003118 pm_runtime_put_sync(&pdev->dev);
3119 pm_runtime_disable(&pdev->dev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03003120 if (cpsw->data.dual_emac)
3121 free_netdev(cpsw->slaves[1].ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00003122 free_netdev(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00003123 return 0;
3124}
3125
Grygorii Strashko8963a502015-02-27 13:19:45 +02003126#ifdef CONFIG_PM_SLEEP
Mugunthan V Ndf828592012-03-18 20:17:54 +00003127static int cpsw_suspend(struct device *dev)
3128{
3129 struct platform_device *pdev = to_platform_device(dev);
3130 struct net_device *ndev = platform_get_drvdata(pdev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03003131 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00003132
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03003133 if (cpsw->data.dual_emac) {
Mugunthan V N618073e2014-09-11 22:52:38 +05303134 int i;
Daniel Mack1e7a2e22013-11-15 08:29:16 +01003135
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03003136 for (i = 0; i < cpsw->data.slaves; i++) {
3137 if (netif_running(cpsw->slaves[i].ndev))
3138 cpsw_ndo_stop(cpsw->slaves[i].ndev);
Mugunthan V N618073e2014-09-11 22:52:38 +05303139 }
3140 } else {
3141 if (netif_running(ndev))
3142 cpsw_ndo_stop(ndev);
Mugunthan V N618073e2014-09-11 22:52:38 +05303143 }
Daniel Mack1e7a2e22013-11-15 08:29:16 +01003144
Mugunthan V N739683b2013-06-06 23:45:14 +05303145 /* Select sleep pin state */
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03003146 pinctrl_pm_select_sleep_state(dev);
Mugunthan V N739683b2013-06-06 23:45:14 +05303147
Mugunthan V Ndf828592012-03-18 20:17:54 +00003148 return 0;
3149}
3150
3151static int cpsw_resume(struct device *dev)
3152{
3153 struct platform_device *pdev = to_platform_device(dev);
3154 struct net_device *ndev = platform_get_drvdata(pdev);
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03003155 struct cpsw_common *cpsw = netdev_priv(ndev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00003156
Mugunthan V N739683b2013-06-06 23:45:14 +05303157 /* Select default pin state */
Ivan Khoronzhuk56e31bd2016-08-10 02:22:38 +03003158 pinctrl_pm_select_default_state(dev);
Mugunthan V N739683b2013-06-06 23:45:14 +05303159
Grygorii Strashko4ccfd632016-11-29 16:27:03 -06003160 /* shut up ASSERT_RTNL() warning in netif_set_real_num_tx/rx_queues */
3161 rtnl_lock();
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03003162 if (cpsw->data.dual_emac) {
Mugunthan V N618073e2014-09-11 22:52:38 +05303163 int i;
3164
Ivan Khoronzhuk606f3992016-08-10 02:22:42 +03003165 for (i = 0; i < cpsw->data.slaves; i++) {
3166 if (netif_running(cpsw->slaves[i].ndev))
3167 cpsw_ndo_open(cpsw->slaves[i].ndev);
Mugunthan V N618073e2014-09-11 22:52:38 +05303168 }
3169 } else {
3170 if (netif_running(ndev))
3171 cpsw_ndo_open(ndev);
3172 }
Grygorii Strashko4ccfd632016-11-29 16:27:03 -06003173 rtnl_unlock();
3174
Mugunthan V Ndf828592012-03-18 20:17:54 +00003175 return 0;
3176}
Grygorii Strashko8963a502015-02-27 13:19:45 +02003177#endif
Mugunthan V Ndf828592012-03-18 20:17:54 +00003178
Grygorii Strashko8963a502015-02-27 13:19:45 +02003179static SIMPLE_DEV_PM_OPS(cpsw_pm_ops, cpsw_suspend, cpsw_resume);
Mugunthan V Ndf828592012-03-18 20:17:54 +00003180
3181static struct platform_driver cpsw_driver = {
3182 .driver = {
3183 .name = "cpsw",
Mugunthan V Ndf828592012-03-18 20:17:54 +00003184 .pm = &cpsw_pm_ops,
Sachin Kamat1e5c76d2013-09-30 09:55:12 +05303185 .of_match_table = cpsw_of_mtable,
Mugunthan V Ndf828592012-03-18 20:17:54 +00003186 },
3187 .probe = cpsw_probe,
Bill Pemberton663e12e2012-12-03 09:23:45 -05003188 .remove = cpsw_remove,
Mugunthan V Ndf828592012-03-18 20:17:54 +00003189};
3190
Grygorii Strashko6fb3b6b52015-10-23 14:41:12 +03003191module_platform_driver(cpsw_driver);
Mugunthan V Ndf828592012-03-18 20:17:54 +00003192
3193MODULE_LICENSE("GPL");
3194MODULE_AUTHOR("Cyril Chemparathy <cyril@ti.com>");
3195MODULE_AUTHOR("Mugunthan V N <mugunthanvnm@ti.com>");
3196MODULE_DESCRIPTION("TI CPSW Ethernet driver");