blob: 83ce890d6e971a58082cd7146c3d9ec72101050b [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 N2eb32b02012-07-30 10:17:14 +000032#include <linux/of.h>
33#include <linux/of_net.h>
34#include <linux/of_device.h>
Mugunthan V N3b72c2f2013-02-05 08:26:48 +000035#include <linux/if_vlan.h>
Mugunthan V Ndf828592012-03-18 20:17:54 +000036
37#include <linux/platform_data/cpsw.h>
38
39#include "cpsw_ale.h"
Richard Cochran2e5b38a2012-10-29 08:45:20 +000040#include "cpts.h"
Mugunthan V Ndf828592012-03-18 20:17:54 +000041#include "davinci_cpdma.h"
42
43#define CPSW_DEBUG (NETIF_MSG_HW | NETIF_MSG_WOL | \
44 NETIF_MSG_DRV | NETIF_MSG_LINK | \
45 NETIF_MSG_IFUP | NETIF_MSG_INTR | \
46 NETIF_MSG_PROBE | NETIF_MSG_TIMER | \
47 NETIF_MSG_IFDOWN | NETIF_MSG_RX_ERR | \
48 NETIF_MSG_TX_ERR | NETIF_MSG_TX_DONE | \
49 NETIF_MSG_PKTDATA | NETIF_MSG_TX_QUEUED | \
50 NETIF_MSG_RX_STATUS)
51
52#define cpsw_info(priv, type, format, ...) \
53do { \
54 if (netif_msg_##type(priv) && net_ratelimit()) \
55 dev_info(priv->dev, format, ## __VA_ARGS__); \
56} while (0)
57
58#define cpsw_err(priv, type, format, ...) \
59do { \
60 if (netif_msg_##type(priv) && net_ratelimit()) \
61 dev_err(priv->dev, format, ## __VA_ARGS__); \
62} while (0)
63
64#define cpsw_dbg(priv, type, format, ...) \
65do { \
66 if (netif_msg_##type(priv) && net_ratelimit()) \
67 dev_dbg(priv->dev, format, ## __VA_ARGS__); \
68} while (0)
69
70#define cpsw_notice(priv, type, format, ...) \
71do { \
72 if (netif_msg_##type(priv) && net_ratelimit()) \
73 dev_notice(priv->dev, format, ## __VA_ARGS__); \
74} while (0)
75
Mugunthan V N5c50a852012-10-29 08:45:11 +000076#define ALE_ALL_PORTS 0x7
77
Mugunthan V Ndf828592012-03-18 20:17:54 +000078#define CPSW_MAJOR_VERSION(reg) (reg >> 8 & 0x7)
79#define CPSW_MINOR_VERSION(reg) (reg & 0xff)
80#define CPSW_RTL_VERSION(reg) ((reg >> 11) & 0x1f)
81
Richard Cochrane90cfac2012-10-29 08:45:14 +000082#define CPSW_VERSION_1 0x19010a
83#define CPSW_VERSION_2 0x19010c
Richard Cochran549985e2012-11-14 09:07:56 +000084
85#define HOST_PORT_NUM 0
86#define SLIVER_SIZE 0x40
87
88#define CPSW1_HOST_PORT_OFFSET 0x028
89#define CPSW1_SLAVE_OFFSET 0x050
90#define CPSW1_SLAVE_SIZE 0x040
91#define CPSW1_CPDMA_OFFSET 0x100
92#define CPSW1_STATERAM_OFFSET 0x200
93#define CPSW1_CPTS_OFFSET 0x500
94#define CPSW1_ALE_OFFSET 0x600
95#define CPSW1_SLIVER_OFFSET 0x700
96
97#define CPSW2_HOST_PORT_OFFSET 0x108
98#define CPSW2_SLAVE_OFFSET 0x200
99#define CPSW2_SLAVE_SIZE 0x100
100#define CPSW2_CPDMA_OFFSET 0x800
101#define CPSW2_STATERAM_OFFSET 0xa00
102#define CPSW2_CPTS_OFFSET 0xc00
103#define CPSW2_ALE_OFFSET 0xd00
104#define CPSW2_SLIVER_OFFSET 0xd80
105#define CPSW2_BD_OFFSET 0x2000
106
Mugunthan V Ndf828592012-03-18 20:17:54 +0000107#define CPDMA_RXTHRESH 0x0c0
108#define CPDMA_RXFREE 0x0e0
109#define CPDMA_TXHDP 0x00
110#define CPDMA_RXHDP 0x20
111#define CPDMA_TXCP 0x40
112#define CPDMA_RXCP 0x60
113
Mugunthan V Ndf828592012-03-18 20:17:54 +0000114#define CPSW_POLL_WEIGHT 64
115#define CPSW_MIN_PACKET_SIZE 60
116#define CPSW_MAX_PACKET_SIZE (1500 + 14 + 4 + 4)
117
118#define RX_PRIORITY_MAPPING 0x76543210
119#define TX_PRIORITY_MAPPING 0x33221100
120#define CPDMA_TX_PRIORITY_MAP 0x76543210
121
Mugunthan V N3b72c2f2013-02-05 08:26:48 +0000122#define CPSW_VLAN_AWARE BIT(1)
123#define CPSW_ALE_VLAN_AWARE 1
124
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000125#define CPSW_FIFO_NORMAL_MODE (0 << 15)
126#define CPSW_FIFO_DUAL_MAC_MODE (1 << 15)
127#define CPSW_FIFO_RATE_LIMIT_MODE (2 << 15)
128
Mugunthan V Ndf828592012-03-18 20:17:54 +0000129#define cpsw_enable_irq(priv) \
130 do { \
131 u32 i; \
132 for (i = 0; i < priv->num_irqs; i++) \
133 enable_irq(priv->irqs_table[i]); \
134 } while (0);
135#define cpsw_disable_irq(priv) \
136 do { \
137 u32 i; \
138 for (i = 0; i < priv->num_irqs; i++) \
139 disable_irq_nosync(priv->irqs_table[i]); \
140 } while (0);
141
Mugunthan V Nd3bb9c52013-03-11 23:16:36 +0000142#define cpsw_slave_index(priv) \
143 ((priv->data.dual_emac) ? priv->emac_port : \
144 priv->data.active_slave)
145
Mugunthan V Ndf828592012-03-18 20:17:54 +0000146static int debug_level;
147module_param(debug_level, int, 0);
148MODULE_PARM_DESC(debug_level, "cpsw debug level (NETIF_MSG bits)");
149
150static int ale_ageout = 10;
151module_param(ale_ageout, int, 0);
152MODULE_PARM_DESC(ale_ageout, "cpsw ale ageout interval (seconds)");
153
154static int rx_packet_max = CPSW_MAX_PACKET_SIZE;
155module_param(rx_packet_max, int, 0);
156MODULE_PARM_DESC(rx_packet_max, "maximum receive packet size (bytes)");
157
Richard Cochran996a5c22012-10-29 08:45:12 +0000158struct cpsw_wr_regs {
Mugunthan V Ndf828592012-03-18 20:17:54 +0000159 u32 id_ver;
160 u32 soft_reset;
161 u32 control;
162 u32 int_control;
163 u32 rx_thresh_en;
164 u32 rx_en;
165 u32 tx_en;
166 u32 misc_en;
167};
168
Richard Cochran996a5c22012-10-29 08:45:12 +0000169struct cpsw_ss_regs {
Mugunthan V Ndf828592012-03-18 20:17:54 +0000170 u32 id_ver;
171 u32 control;
172 u32 soft_reset;
173 u32 stat_port_en;
174 u32 ptype;
Richard Cochranbd357af2012-10-29 08:45:13 +0000175 u32 soft_idle;
176 u32 thru_rate;
177 u32 gap_thresh;
178 u32 tx_start_wds;
179 u32 flow_control;
180 u32 vlan_ltype;
181 u32 ts_ltype;
182 u32 dlr_ltype;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000183};
184
Richard Cochran9750a3a2012-10-29 08:45:15 +0000185/* CPSW_PORT_V1 */
186#define CPSW1_MAX_BLKS 0x00 /* Maximum FIFO Blocks */
187#define CPSW1_BLK_CNT 0x04 /* FIFO Block Usage Count (Read Only) */
188#define CPSW1_TX_IN_CTL 0x08 /* Transmit FIFO Control */
189#define CPSW1_PORT_VLAN 0x0c /* VLAN Register */
190#define CPSW1_TX_PRI_MAP 0x10 /* Tx Header Priority to Switch Pri Mapping */
191#define CPSW1_TS_CTL 0x14 /* Time Sync Control */
192#define CPSW1_TS_SEQ_LTYPE 0x18 /* Time Sync Sequence ID Offset and Msg Type */
193#define CPSW1_TS_VLAN 0x1c /* Time Sync VLAN1 and VLAN2 */
194
195/* CPSW_PORT_V2 */
196#define CPSW2_CONTROL 0x00 /* Control Register */
197#define CPSW2_MAX_BLKS 0x08 /* Maximum FIFO Blocks */
198#define CPSW2_BLK_CNT 0x0c /* FIFO Block Usage Count (Read Only) */
199#define CPSW2_TX_IN_CTL 0x10 /* Transmit FIFO Control */
200#define CPSW2_PORT_VLAN 0x14 /* VLAN Register */
201#define CPSW2_TX_PRI_MAP 0x18 /* Tx Header Priority to Switch Pri Mapping */
202#define CPSW2_TS_SEQ_MTYPE 0x1c /* Time Sync Sequence ID Offset and Msg Type */
203
204/* CPSW_PORT_V1 and V2 */
205#define SA_LO 0x20 /* CPGMAC_SL Source Address Low */
206#define SA_HI 0x24 /* CPGMAC_SL Source Address High */
207#define SEND_PERCENT 0x28 /* Transmit Queue Send Percentages */
208
209/* CPSW_PORT_V2 only */
210#define RX_DSCP_PRI_MAP0 0x30 /* Rx DSCP Priority to Rx Packet Mapping */
211#define RX_DSCP_PRI_MAP1 0x34 /* Rx DSCP Priority to Rx Packet Mapping */
212#define RX_DSCP_PRI_MAP2 0x38 /* Rx DSCP Priority to Rx Packet Mapping */
213#define RX_DSCP_PRI_MAP3 0x3c /* Rx DSCP Priority to Rx Packet Mapping */
214#define RX_DSCP_PRI_MAP4 0x40 /* Rx DSCP Priority to Rx Packet Mapping */
215#define RX_DSCP_PRI_MAP5 0x44 /* Rx DSCP Priority to Rx Packet Mapping */
216#define RX_DSCP_PRI_MAP6 0x48 /* Rx DSCP Priority to Rx Packet Mapping */
217#define RX_DSCP_PRI_MAP7 0x4c /* Rx DSCP Priority to Rx Packet Mapping */
218
219/* Bit definitions for the CPSW2_CONTROL register */
220#define PASS_PRI_TAGGED (1<<24) /* Pass Priority Tagged */
221#define VLAN_LTYPE2_EN (1<<21) /* VLAN LTYPE 2 enable */
222#define VLAN_LTYPE1_EN (1<<20) /* VLAN LTYPE 1 enable */
223#define DSCP_PRI_EN (1<<16) /* DSCP Priority Enable */
224#define TS_320 (1<<14) /* Time Sync Dest Port 320 enable */
225#define TS_319 (1<<13) /* Time Sync Dest Port 319 enable */
226#define TS_132 (1<<12) /* Time Sync Dest IP Addr 132 enable */
227#define TS_131 (1<<11) /* Time Sync Dest IP Addr 131 enable */
228#define TS_130 (1<<10) /* Time Sync Dest IP Addr 130 enable */
229#define TS_129 (1<<9) /* Time Sync Dest IP Addr 129 enable */
230#define TS_BIT8 (1<<8) /* ts_ttl_nonzero? */
231#define TS_ANNEX_D_EN (1<<4) /* Time Sync Annex D enable */
232#define TS_LTYPE2_EN (1<<3) /* Time Sync LTYPE 2 enable */
233#define TS_LTYPE1_EN (1<<2) /* Time Sync LTYPE 1 enable */
234#define TS_TX_EN (1<<1) /* Time Sync Transmit Enable */
235#define TS_RX_EN (1<<0) /* Time Sync Receive Enable */
236
237#define CTRL_TS_BITS \
238 (TS_320 | TS_319 | TS_132 | TS_131 | TS_130 | TS_129 | TS_BIT8 | \
239 TS_ANNEX_D_EN | TS_LTYPE1_EN)
240
241#define CTRL_ALL_TS_MASK (CTRL_TS_BITS | TS_TX_EN | TS_RX_EN)
242#define CTRL_TX_TS_BITS (CTRL_TS_BITS | TS_TX_EN)
243#define CTRL_RX_TS_BITS (CTRL_TS_BITS | TS_RX_EN)
244
245/* Bit definitions for the CPSW2_TS_SEQ_MTYPE register */
246#define TS_SEQ_ID_OFFSET_SHIFT (16) /* Time Sync Sequence ID Offset */
247#define TS_SEQ_ID_OFFSET_MASK (0x3f)
248#define TS_MSG_TYPE_EN_SHIFT (0) /* Time Sync Message Type Enable */
249#define TS_MSG_TYPE_EN_MASK (0xffff)
250
251/* The PTP event messages - Sync, Delay_Req, Pdelay_Req, and Pdelay_Resp. */
252#define EVENT_MSG_BITS ((1<<0) | (1<<1) | (1<<2) | (1<<3))
Mugunthan V Ndf828592012-03-18 20:17:54 +0000253
Richard Cochran2e5b38a2012-10-29 08:45:20 +0000254/* Bit definitions for the CPSW1_TS_CTL register */
255#define CPSW_V1_TS_RX_EN BIT(0)
256#define CPSW_V1_TS_TX_EN BIT(4)
257#define CPSW_V1_MSG_TYPE_OFS 16
258
259/* Bit definitions for the CPSW1_TS_SEQ_LTYPE register */
260#define CPSW_V1_SEQ_ID_OFS_SHIFT 16
261
Mugunthan V Ndf828592012-03-18 20:17:54 +0000262struct cpsw_host_regs {
263 u32 max_blks;
264 u32 blk_cnt;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000265 u32 tx_in_ctl;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000266 u32 port_vlan;
267 u32 tx_pri_map;
268 u32 cpdma_tx_pri_map;
269 u32 cpdma_rx_chan_map;
270};
271
272struct cpsw_sliver_regs {
273 u32 id_ver;
274 u32 mac_control;
275 u32 mac_status;
276 u32 soft_reset;
277 u32 rx_maxlen;
278 u32 __reserved_0;
279 u32 rx_pause;
280 u32 tx_pause;
281 u32 __reserved_1;
282 u32 rx_pri_map;
283};
284
285struct cpsw_slave {
Richard Cochran9750a3a2012-10-29 08:45:15 +0000286 void __iomem *regs;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000287 struct cpsw_sliver_regs __iomem *sliver;
288 int slave_num;
289 u32 mac_control;
290 struct cpsw_slave_data *data;
291 struct phy_device *phy;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000292 struct net_device *ndev;
293 u32 port_vlan;
294 u32 open_stat;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000295};
296
Richard Cochran9750a3a2012-10-29 08:45:15 +0000297static inline u32 slave_read(struct cpsw_slave *slave, u32 offset)
298{
299 return __raw_readl(slave->regs + offset);
300}
301
302static inline void slave_write(struct cpsw_slave *slave, u32 val, u32 offset)
303{
304 __raw_writel(val, slave->regs + offset);
305}
306
Mugunthan V Ndf828592012-03-18 20:17:54 +0000307struct cpsw_priv {
308 spinlock_t lock;
309 struct platform_device *pdev;
310 struct net_device *ndev;
311 struct resource *cpsw_res;
Richard Cochrana65dd5b2012-11-02 22:25:29 +0000312 struct resource *cpsw_wr_res;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000313 struct napi_struct napi;
314 struct device *dev;
315 struct cpsw_platform_data data;
Richard Cochran996a5c22012-10-29 08:45:12 +0000316 struct cpsw_ss_regs __iomem *regs;
317 struct cpsw_wr_regs __iomem *wr_regs;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000318 struct cpsw_host_regs __iomem *host_port_regs;
319 u32 msg_enable;
Richard Cochrane90cfac2012-10-29 08:45:14 +0000320 u32 version;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000321 struct net_device_stats stats;
322 int rx_packet_max;
323 int host_port;
324 struct clk *clk;
325 u8 mac_addr[ETH_ALEN];
326 struct cpsw_slave *slaves;
327 struct cpdma_ctlr *dma;
328 struct cpdma_chan *txch, *rxch;
329 struct cpsw_ale *ale;
330 /* snapshot of IRQ numbers */
331 u32 irqs_table[4];
332 u32 num_irqs;
Mugunthan V N9232b162013-02-11 09:52:19 +0000333 struct cpts *cpts;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000334 u32 emac_port;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000335};
336
337#define napi_to_priv(napi) container_of(napi, struct cpsw_priv, napi)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000338#define for_each_slave(priv, func, arg...) \
339 do { \
340 int idx; \
341 if (priv->data.dual_emac) \
342 (func)((priv)->slaves + priv->emac_port, ##arg);\
343 else \
344 for (idx = 0; idx < (priv)->data.slaves; idx++) \
345 (func)((priv)->slaves + idx, ##arg); \
Mugunthan V Ndf828592012-03-18 20:17:54 +0000346 } while (0)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000347#define cpsw_get_slave_ndev(priv, __slave_no__) \
348 (priv->slaves[__slave_no__].ndev)
349#define cpsw_get_slave_priv(priv, __slave_no__) \
350 ((priv->slaves[__slave_no__].ndev) ? \
351 netdev_priv(priv->slaves[__slave_no__].ndev) : NULL) \
352
353#define cpsw_dual_emac_src_port_detect(status, priv, ndev, skb) \
354 do { \
355 if (!priv->data.dual_emac) \
356 break; \
357 if (CPDMA_RX_SOURCE_PORT(status) == 1) { \
358 ndev = cpsw_get_slave_ndev(priv, 0); \
359 priv = netdev_priv(ndev); \
360 skb->dev = ndev; \
361 } else if (CPDMA_RX_SOURCE_PORT(status) == 2) { \
362 ndev = cpsw_get_slave_ndev(priv, 1); \
363 priv = netdev_priv(ndev); \
364 skb->dev = ndev; \
365 } \
366 } while (0)
367#define cpsw_add_mcast(priv, addr) \
368 do { \
369 if (priv->data.dual_emac) { \
370 struct cpsw_slave *slave = priv->slaves + \
371 priv->emac_port; \
372 int slave_port = cpsw_get_slave_port(priv, \
373 slave->slave_num); \
374 cpsw_ale_add_mcast(priv->ale, addr, \
375 1 << slave_port | 1 << priv->host_port, \
376 ALE_VLAN, slave->port_vlan, 0); \
377 } else { \
378 cpsw_ale_add_mcast(priv->ale, addr, \
379 ALE_ALL_PORTS << priv->host_port, \
380 0, 0, 0); \
381 } \
382 } while (0)
383
384static inline int cpsw_get_slave_port(struct cpsw_priv *priv, u32 slave_num)
385{
386 if (priv->host_port == 0)
387 return slave_num + 1;
388 else
389 return slave_num;
390}
Mugunthan V Ndf828592012-03-18 20:17:54 +0000391
Mugunthan V N5c50a852012-10-29 08:45:11 +0000392static void cpsw_ndo_set_rx_mode(struct net_device *ndev)
393{
394 struct cpsw_priv *priv = netdev_priv(ndev);
395
396 if (ndev->flags & IFF_PROMISC) {
397 /* Enable promiscuous mode */
398 dev_err(priv->dev, "Ignoring Promiscuous mode\n");
399 return;
400 }
401
402 /* Clear all mcast from ALE */
403 cpsw_ale_flush_multicast(priv->ale, ALE_ALL_PORTS << priv->host_port);
404
405 if (!netdev_mc_empty(ndev)) {
406 struct netdev_hw_addr *ha;
407
408 /* program multicast address list into ALE register */
409 netdev_for_each_mc_addr(ha, ndev) {
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000410 cpsw_add_mcast(priv, (u8 *)ha->addr);
Mugunthan V N5c50a852012-10-29 08:45:11 +0000411 }
412 }
413}
414
Mugunthan V Ndf828592012-03-18 20:17:54 +0000415static void cpsw_intr_enable(struct cpsw_priv *priv)
416{
Richard Cochran996a5c22012-10-29 08:45:12 +0000417 __raw_writel(0xFF, &priv->wr_regs->tx_en);
418 __raw_writel(0xFF, &priv->wr_regs->rx_en);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000419
420 cpdma_ctlr_int_ctrl(priv->dma, true);
421 return;
422}
423
424static void cpsw_intr_disable(struct cpsw_priv *priv)
425{
Richard Cochran996a5c22012-10-29 08:45:12 +0000426 __raw_writel(0, &priv->wr_regs->tx_en);
427 __raw_writel(0, &priv->wr_regs->rx_en);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000428
429 cpdma_ctlr_int_ctrl(priv->dma, false);
430 return;
431}
432
433void cpsw_tx_handler(void *token, int len, int status)
434{
435 struct sk_buff *skb = token;
436 struct net_device *ndev = skb->dev;
437 struct cpsw_priv *priv = netdev_priv(ndev);
438
Mugunthan V Nfae50822013-01-17 06:31:34 +0000439 /* Check whether the queue is stopped due to stalled tx dma, if the
440 * queue is stopped then start the queue as we have free desc for tx
441 */
Mugunthan V Ndf828592012-03-18 20:17:54 +0000442 if (unlikely(netif_queue_stopped(ndev)))
443 netif_start_queue(ndev);
Mugunthan V N9232b162013-02-11 09:52:19 +0000444 cpts_tx_timestamp(priv->cpts, skb);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000445 priv->stats.tx_packets++;
446 priv->stats.tx_bytes += len;
447 dev_kfree_skb_any(skb);
448}
449
450void cpsw_rx_handler(void *token, int len, int status)
451{
452 struct sk_buff *skb = token;
453 struct net_device *ndev = skb->dev;
454 struct cpsw_priv *priv = netdev_priv(ndev);
455 int ret = 0;
456
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000457 cpsw_dual_emac_src_port_detect(status, priv, ndev, skb);
458
Mugunthan V Ndf828592012-03-18 20:17:54 +0000459 /* free and bail if we are shutting down */
460 if (unlikely(!netif_running(ndev)) ||
461 unlikely(!netif_carrier_ok(ndev))) {
462 dev_kfree_skb_any(skb);
463 return;
464 }
465 if (likely(status >= 0)) {
466 skb_put(skb, len);
Mugunthan V N9232b162013-02-11 09:52:19 +0000467 cpts_rx_timestamp(priv->cpts, skb);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000468 skb->protocol = eth_type_trans(skb, ndev);
469 netif_receive_skb(skb);
470 priv->stats.rx_bytes += len;
471 priv->stats.rx_packets++;
472 skb = NULL;
473 }
474
475 if (unlikely(!netif_running(ndev))) {
476 if (skb)
477 dev_kfree_skb_any(skb);
478 return;
479 }
480
481 if (likely(!skb)) {
482 skb = netdev_alloc_skb_ip_align(ndev, priv->rx_packet_max);
483 if (WARN_ON(!skb))
484 return;
485
486 ret = cpdma_chan_submit(priv->rxch, skb, skb->data,
Mugunthan V Nf6e135c2013-02-11 09:52:18 +0000487 skb_tailroom(skb), 0, GFP_KERNEL);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000488 }
489 WARN_ON(ret < 0);
490}
491
492static irqreturn_t cpsw_interrupt(int irq, void *dev_id)
493{
494 struct cpsw_priv *priv = dev_id;
495
496 if (likely(netif_running(priv->ndev))) {
497 cpsw_intr_disable(priv);
498 cpsw_disable_irq(priv);
499 napi_schedule(&priv->napi);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000500 } else {
501 priv = cpsw_get_slave_priv(priv, 1);
502 if (likely(priv) && likely(netif_running(priv->ndev))) {
503 cpsw_intr_disable(priv);
504 cpsw_disable_irq(priv);
505 napi_schedule(&priv->napi);
506 }
Mugunthan V Ndf828592012-03-18 20:17:54 +0000507 }
508 return IRQ_HANDLED;
509}
510
Mugunthan V Ndf828592012-03-18 20:17:54 +0000511static int cpsw_poll(struct napi_struct *napi, int budget)
512{
513 struct cpsw_priv *priv = napi_to_priv(napi);
514 int num_tx, num_rx;
515
516 num_tx = cpdma_chan_process(priv->txch, 128);
Mugunthan V N510a1e72013-02-17 22:19:20 +0000517 if (num_tx)
518 cpdma_ctlr_eoi(priv->dma, CPDMA_EOI_TX);
519
Mugunthan V Ndf828592012-03-18 20:17:54 +0000520 num_rx = cpdma_chan_process(priv->rxch, budget);
Mugunthan V N510a1e72013-02-17 22:19:20 +0000521 if (num_rx < budget) {
522 napi_complete(napi);
523 cpsw_intr_enable(priv);
524 cpdma_ctlr_eoi(priv->dma, CPDMA_EOI_RX);
525 cpsw_enable_irq(priv);
526 }
Mugunthan V Ndf828592012-03-18 20:17:54 +0000527
528 if (num_rx || num_tx)
529 cpsw_dbg(priv, intr, "poll %d rx, %d tx pkts\n",
530 num_rx, num_tx);
531
Mugunthan V Ndf828592012-03-18 20:17:54 +0000532 return num_rx;
533}
534
535static inline void soft_reset(const char *module, void __iomem *reg)
536{
537 unsigned long timeout = jiffies + HZ;
538
539 __raw_writel(1, reg);
540 do {
541 cpu_relax();
542 } while ((__raw_readl(reg) & 1) && time_after(timeout, jiffies));
543
544 WARN(__raw_readl(reg) & 1, "failed to soft-reset %s\n", module);
545}
546
547#define mac_hi(mac) (((mac)[0] << 0) | ((mac)[1] << 8) | \
548 ((mac)[2] << 16) | ((mac)[3] << 24))
549#define mac_lo(mac) (((mac)[4] << 0) | ((mac)[5] << 8))
550
551static void cpsw_set_slave_mac(struct cpsw_slave *slave,
552 struct cpsw_priv *priv)
553{
Richard Cochran9750a3a2012-10-29 08:45:15 +0000554 slave_write(slave, mac_hi(priv->mac_addr), SA_HI);
555 slave_write(slave, mac_lo(priv->mac_addr), SA_LO);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000556}
557
558static void _cpsw_adjust_link(struct cpsw_slave *slave,
559 struct cpsw_priv *priv, bool *link)
560{
561 struct phy_device *phy = slave->phy;
562 u32 mac_control = 0;
563 u32 slave_port;
564
565 if (!phy)
566 return;
567
568 slave_port = cpsw_get_slave_port(priv, slave->slave_num);
569
570 if (phy->link) {
571 mac_control = priv->data.mac_control;
572
573 /* enable forwarding */
574 cpsw_ale_control_set(priv->ale, slave_port,
575 ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
576
577 if (phy->speed == 1000)
578 mac_control |= BIT(7); /* GIGABITEN */
579 if (phy->duplex)
580 mac_control |= BIT(0); /* FULLDUPLEXEN */
Daniel Mack342b7b72012-09-27 09:19:34 +0000581
582 /* set speed_in input in case RMII mode is used in 100Mbps */
583 if (phy->speed == 100)
584 mac_control |= BIT(15);
585
Mugunthan V Ndf828592012-03-18 20:17:54 +0000586 *link = true;
587 } else {
588 mac_control = 0;
589 /* disable forwarding */
590 cpsw_ale_control_set(priv->ale, slave_port,
591 ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
592 }
593
594 if (mac_control != slave->mac_control) {
595 phy_print_status(phy);
596 __raw_writel(mac_control, &slave->sliver->mac_control);
597 }
598
599 slave->mac_control = mac_control;
600}
601
602static void cpsw_adjust_link(struct net_device *ndev)
603{
604 struct cpsw_priv *priv = netdev_priv(ndev);
605 bool link = false;
606
607 for_each_slave(priv, _cpsw_adjust_link, priv, &link);
608
609 if (link) {
610 netif_carrier_on(ndev);
611 if (netif_running(ndev))
612 netif_wake_queue(ndev);
613 } else {
614 netif_carrier_off(ndev);
615 netif_stop_queue(ndev);
616 }
617}
618
619static inline int __show_stat(char *buf, int maxlen, const char *name, u32 val)
620{
621 static char *leader = "........................................";
622
623 if (!val)
624 return 0;
625 else
626 return snprintf(buf, maxlen, "%s %s %10d\n", name,
627 leader + strlen(name), val);
628}
629
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000630static int cpsw_common_res_usage_state(struct cpsw_priv *priv)
631{
632 u32 i;
633 u32 usage_count = 0;
634
635 if (!priv->data.dual_emac)
636 return 0;
637
638 for (i = 0; i < priv->data.slaves; i++)
639 if (priv->slaves[i].open_stat)
640 usage_count++;
641
642 return usage_count;
643}
644
645static inline int cpsw_tx_packet_submit(struct net_device *ndev,
646 struct cpsw_priv *priv, struct sk_buff *skb)
647{
648 if (!priv->data.dual_emac)
649 return cpdma_chan_submit(priv->txch, skb, skb->data,
650 skb->len, 0, GFP_KERNEL);
651
652 if (ndev == cpsw_get_slave_ndev(priv, 0))
653 return cpdma_chan_submit(priv->txch, skb, skb->data,
654 skb->len, 1, GFP_KERNEL);
655 else
656 return cpdma_chan_submit(priv->txch, skb, skb->data,
657 skb->len, 2, GFP_KERNEL);
658}
659
660static inline void cpsw_add_dual_emac_def_ale_entries(
661 struct cpsw_priv *priv, struct cpsw_slave *slave,
662 u32 slave_port)
663{
664 u32 port_mask = 1 << slave_port | 1 << priv->host_port;
665
666 if (priv->version == CPSW_VERSION_1)
667 slave_write(slave, slave->port_vlan, CPSW1_PORT_VLAN);
668 else
669 slave_write(slave, slave->port_vlan, CPSW2_PORT_VLAN);
670 cpsw_ale_add_vlan(priv->ale, slave->port_vlan, port_mask,
671 port_mask, port_mask, 0);
672 cpsw_ale_add_mcast(priv->ale, priv->ndev->broadcast,
673 port_mask, ALE_VLAN, slave->port_vlan, 0);
674 cpsw_ale_add_ucast(priv->ale, priv->mac_addr,
675 priv->host_port, ALE_VLAN, slave->port_vlan);
676}
677
Mugunthan V Ndf828592012-03-18 20:17:54 +0000678static void cpsw_slave_open(struct cpsw_slave *slave, struct cpsw_priv *priv)
679{
680 char name[32];
681 u32 slave_port;
682
683 sprintf(name, "slave-%d", slave->slave_num);
684
685 soft_reset(name, &slave->sliver->soft_reset);
686
687 /* setup priority mapping */
688 __raw_writel(RX_PRIORITY_MAPPING, &slave->sliver->rx_pri_map);
Richard Cochran9750a3a2012-10-29 08:45:15 +0000689
690 switch (priv->version) {
691 case CPSW_VERSION_1:
692 slave_write(slave, TX_PRIORITY_MAPPING, CPSW1_TX_PRI_MAP);
693 break;
694 case CPSW_VERSION_2:
695 slave_write(slave, TX_PRIORITY_MAPPING, CPSW2_TX_PRI_MAP);
696 break;
697 }
Mugunthan V Ndf828592012-03-18 20:17:54 +0000698
699 /* setup max packet size, and mac address */
700 __raw_writel(priv->rx_packet_max, &slave->sliver->rx_maxlen);
701 cpsw_set_slave_mac(slave, priv);
702
703 slave->mac_control = 0; /* no link yet */
704
705 slave_port = cpsw_get_slave_port(priv, slave->slave_num);
706
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000707 if (priv->data.dual_emac)
708 cpsw_add_dual_emac_def_ale_entries(priv, slave, slave_port);
709 else
710 cpsw_ale_add_mcast(priv->ale, priv->ndev->broadcast,
711 1 << slave_port, 0, 0, ALE_MCAST_FWD_2);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000712
713 slave->phy = phy_connect(priv->ndev, slave->data->phy_id,
Florian Fainellif9a8f832013-01-14 00:52:52 +0000714 &cpsw_adjust_link, slave->data->phy_if);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000715 if (IS_ERR(slave->phy)) {
716 dev_err(priv->dev, "phy %s not found on slave %d\n",
717 slave->data->phy_id, slave->slave_num);
718 slave->phy = NULL;
719 } else {
720 dev_info(priv->dev, "phy found : id is : 0x%x\n",
721 slave->phy->phy_id);
722 phy_start(slave->phy);
723 }
724}
725
Mugunthan V N3b72c2f2013-02-05 08:26:48 +0000726static inline void cpsw_add_default_vlan(struct cpsw_priv *priv)
727{
728 const int vlan = priv->data.default_vlan;
729 const int port = priv->host_port;
730 u32 reg;
731 int i;
732
733 reg = (priv->version == CPSW_VERSION_1) ? CPSW1_PORT_VLAN :
734 CPSW2_PORT_VLAN;
735
736 writel(vlan, &priv->host_port_regs->port_vlan);
737
Daniel Mack0237c112013-02-26 04:06:20 +0000738 for (i = 0; i < priv->data.slaves; i++)
Mugunthan V N3b72c2f2013-02-05 08:26:48 +0000739 slave_write(priv->slaves + i, vlan, reg);
740
741 cpsw_ale_add_vlan(priv->ale, vlan, ALE_ALL_PORTS << port,
742 ALE_ALL_PORTS << port, ALE_ALL_PORTS << port,
743 (ALE_PORT_1 | ALE_PORT_2) << port);
744}
745
Mugunthan V Ndf828592012-03-18 20:17:54 +0000746static void cpsw_init_host_port(struct cpsw_priv *priv)
747{
Mugunthan V N3b72c2f2013-02-05 08:26:48 +0000748 u32 control_reg;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000749 u32 fifo_mode;
Mugunthan V N3b72c2f2013-02-05 08:26:48 +0000750
Mugunthan V Ndf828592012-03-18 20:17:54 +0000751 /* soft reset the controller and initialize ale */
752 soft_reset("cpsw", &priv->regs->soft_reset);
753 cpsw_ale_start(priv->ale);
754
755 /* switch to vlan unaware mode */
Mugunthan V N3b72c2f2013-02-05 08:26:48 +0000756 cpsw_ale_control_set(priv->ale, priv->host_port, ALE_VLAN_AWARE,
757 CPSW_ALE_VLAN_AWARE);
758 control_reg = readl(&priv->regs->control);
759 control_reg |= CPSW_VLAN_AWARE;
760 writel(control_reg, &priv->regs->control);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000761 fifo_mode = (priv->data.dual_emac) ? CPSW_FIFO_DUAL_MAC_MODE :
762 CPSW_FIFO_NORMAL_MODE;
763 writel(fifo_mode, &priv->host_port_regs->tx_in_ctl);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000764
765 /* setup host port priority mapping */
766 __raw_writel(CPDMA_TX_PRIORITY_MAP,
767 &priv->host_port_regs->cpdma_tx_pri_map);
768 __raw_writel(0, &priv->host_port_regs->cpdma_rx_chan_map);
769
770 cpsw_ale_control_set(priv->ale, priv->host_port,
771 ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
772
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000773 if (!priv->data.dual_emac) {
774 cpsw_ale_add_ucast(priv->ale, priv->mac_addr, priv->host_port,
775 0, 0);
776 cpsw_ale_add_mcast(priv->ale, priv->ndev->broadcast,
777 1 << priv->host_port, 0, 0, ALE_MCAST_FWD_2);
778 }
Mugunthan V Ndf828592012-03-18 20:17:54 +0000779}
780
781static int cpsw_ndo_open(struct net_device *ndev)
782{
783 struct cpsw_priv *priv = netdev_priv(ndev);
784 int i, ret;
785 u32 reg;
786
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000787 if (!cpsw_common_res_usage_state(priv))
788 cpsw_intr_disable(priv);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000789 netif_carrier_off(ndev);
790
Mugunthan V Nf150bd72012-07-17 08:09:50 +0000791 pm_runtime_get_sync(&priv->pdev->dev);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000792
Richard Cochran549985e2012-11-14 09:07:56 +0000793 reg = priv->version;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000794
795 dev_info(priv->dev, "initializing cpsw version %d.%d (%d)\n",
796 CPSW_MAJOR_VERSION(reg), CPSW_MINOR_VERSION(reg),
797 CPSW_RTL_VERSION(reg));
798
799 /* initialize host and slave ports */
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000800 if (!cpsw_common_res_usage_state(priv))
801 cpsw_init_host_port(priv);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000802 for_each_slave(priv, cpsw_slave_open, priv);
803
Mugunthan V N3b72c2f2013-02-05 08:26:48 +0000804 /* Add default VLAN */
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000805 if (!priv->data.dual_emac)
806 cpsw_add_default_vlan(priv);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +0000807
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000808 if (!cpsw_common_res_usage_state(priv)) {
809 /* setup tx dma to fixed prio and zero offset */
810 cpdma_control_set(priv->dma, CPDMA_TX_PRIO_FIXED, 1);
811 cpdma_control_set(priv->dma, CPDMA_RX_BUFFER_OFFSET, 0);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000812
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000813 /* disable priority elevation */
814 __raw_writel(0, &priv->regs->ptype);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000815
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000816 /* enable statistics collection only on all ports */
817 __raw_writel(0x7, &priv->regs->stat_port_en);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000818
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000819 if (WARN_ON(!priv->data.rx_descs))
820 priv->data.rx_descs = 128;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000821
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000822 for (i = 0; i < priv->data.rx_descs; i++) {
823 struct sk_buff *skb;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000824
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000825 ret = -ENOMEM;
826 skb = netdev_alloc_skb_ip_align(priv->ndev,
827 priv->rx_packet_max);
828 if (!skb)
829 break;
830 ret = cpdma_chan_submit(priv->rxch, skb, skb->data,
Mugunthan V Nf6e135c2013-02-11 09:52:18 +0000831 skb_tailroom(skb), 0, GFP_KERNEL);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000832 if (WARN_ON(ret < 0))
833 break;
834 }
835 /* continue even if we didn't manage to submit all
836 * receive descs
837 */
838 cpsw_info(priv, ifup, "submitted %d rx descriptors\n", i);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000839 }
Mugunthan V Ndf828592012-03-18 20:17:54 +0000840
841 cpdma_ctlr_start(priv->dma);
842 cpsw_intr_enable(priv);
843 napi_enable(&priv->napi);
Mugunthan V N510a1e72013-02-17 22:19:20 +0000844 cpdma_ctlr_eoi(priv->dma, CPDMA_EOI_RX);
845 cpdma_ctlr_eoi(priv->dma, CPDMA_EOI_TX);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000846
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000847 if (priv->data.dual_emac)
848 priv->slaves[priv->emac_port].open_stat = true;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000849 return 0;
850}
851
852static void cpsw_slave_stop(struct cpsw_slave *slave, struct cpsw_priv *priv)
853{
854 if (!slave->phy)
855 return;
856 phy_stop(slave->phy);
857 phy_disconnect(slave->phy);
858 slave->phy = NULL;
859}
860
861static int cpsw_ndo_stop(struct net_device *ndev)
862{
863 struct cpsw_priv *priv = netdev_priv(ndev);
864
865 cpsw_info(priv, ifdown, "shutting down cpsw device\n");
Mugunthan V Ndf828592012-03-18 20:17:54 +0000866 netif_stop_queue(priv->ndev);
867 napi_disable(&priv->napi);
868 netif_carrier_off(priv->ndev);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000869
870 if (cpsw_common_res_usage_state(priv) <= 1) {
871 cpsw_intr_disable(priv);
872 cpdma_ctlr_int_ctrl(priv->dma, false);
873 cpdma_ctlr_stop(priv->dma);
874 cpsw_ale_stop(priv->ale);
875 }
Mugunthan V Ndf828592012-03-18 20:17:54 +0000876 for_each_slave(priv, cpsw_slave_stop, priv);
Mugunthan V Nf150bd72012-07-17 08:09:50 +0000877 pm_runtime_put_sync(&priv->pdev->dev);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000878 if (priv->data.dual_emac)
879 priv->slaves[priv->emac_port].open_stat = false;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000880 return 0;
881}
882
883static netdev_tx_t cpsw_ndo_start_xmit(struct sk_buff *skb,
884 struct net_device *ndev)
885{
886 struct cpsw_priv *priv = netdev_priv(ndev);
887 int ret;
888
889 ndev->trans_start = jiffies;
890
891 if (skb_padto(skb, CPSW_MIN_PACKET_SIZE)) {
892 cpsw_err(priv, tx_err, "packet pad failed\n");
893 priv->stats.tx_dropped++;
894 return NETDEV_TX_OK;
895 }
896
Mugunthan V N9232b162013-02-11 09:52:19 +0000897 if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
898 priv->cpts->tx_enable)
Richard Cochran2e5b38a2012-10-29 08:45:20 +0000899 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
900
901 skb_tx_timestamp(skb);
902
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000903 ret = cpsw_tx_packet_submit(ndev, priv, skb);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000904 if (unlikely(ret != 0)) {
905 cpsw_err(priv, tx_err, "desc submit failed\n");
906 goto fail;
907 }
908
Mugunthan V Nfae50822013-01-17 06:31:34 +0000909 /* If there is no more tx desc left free then we need to
910 * tell the kernel to stop sending us tx frames.
911 */
912 if (unlikely(cpdma_check_free_tx_desc(priv->txch)))
913 netif_stop_queue(ndev);
914
Mugunthan V Ndf828592012-03-18 20:17:54 +0000915 return NETDEV_TX_OK;
916fail:
917 priv->stats.tx_dropped++;
918 netif_stop_queue(ndev);
919 return NETDEV_TX_BUSY;
920}
921
922static void cpsw_ndo_change_rx_flags(struct net_device *ndev, int flags)
923{
924 /*
925 * The switch cannot operate in promiscuous mode without substantial
926 * headache. For promiscuous mode to work, we would need to put the
927 * ALE in bypass mode and route all traffic to the host port.
928 * Subsequently, the host will need to operate as a "bridge", learn,
929 * and flood as needed. For now, we simply complain here and
930 * do nothing about it :-)
931 */
932 if ((flags & IFF_PROMISC) && (ndev->flags & IFF_PROMISC))
933 dev_err(&ndev->dev, "promiscuity ignored!\n");
934
935 /*
936 * The switch cannot filter multicast traffic unless it is configured
937 * in "VLAN Aware" mode. Unfortunately, VLAN awareness requires a
938 * whole bunch of additional logic that this driver does not implement
939 * at present.
940 */
941 if ((flags & IFF_ALLMULTI) && !(ndev->flags & IFF_ALLMULTI))
942 dev_err(&ndev->dev, "multicast traffic cannot be filtered!\n");
943}
944
Richard Cochran2e5b38a2012-10-29 08:45:20 +0000945#ifdef CONFIG_TI_CPTS
946
947static void cpsw_hwtstamp_v1(struct cpsw_priv *priv)
948{
Mugunthan V Ne86ac132013-03-11 23:16:35 +0000949 struct cpsw_slave *slave = &priv->slaves[priv->data.active_slave];
Richard Cochran2e5b38a2012-10-29 08:45:20 +0000950 u32 ts_en, seq_id;
951
Mugunthan V N9232b162013-02-11 09:52:19 +0000952 if (!priv->cpts->tx_enable && !priv->cpts->rx_enable) {
Richard Cochran2e5b38a2012-10-29 08:45:20 +0000953 slave_write(slave, 0, CPSW1_TS_CTL);
954 return;
955 }
956
957 seq_id = (30 << CPSW_V1_SEQ_ID_OFS_SHIFT) | ETH_P_1588;
958 ts_en = EVENT_MSG_BITS << CPSW_V1_MSG_TYPE_OFS;
959
Mugunthan V N9232b162013-02-11 09:52:19 +0000960 if (priv->cpts->tx_enable)
Richard Cochran2e5b38a2012-10-29 08:45:20 +0000961 ts_en |= CPSW_V1_TS_TX_EN;
962
Mugunthan V N9232b162013-02-11 09:52:19 +0000963 if (priv->cpts->rx_enable)
Richard Cochran2e5b38a2012-10-29 08:45:20 +0000964 ts_en |= CPSW_V1_TS_RX_EN;
965
966 slave_write(slave, ts_en, CPSW1_TS_CTL);
967 slave_write(slave, seq_id, CPSW1_TS_SEQ_LTYPE);
968}
969
970static void cpsw_hwtstamp_v2(struct cpsw_priv *priv)
971{
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000972 struct cpsw_slave *slave;
Richard Cochran2e5b38a2012-10-29 08:45:20 +0000973 u32 ctrl, mtype;
974
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000975 if (priv->data.dual_emac)
976 slave = &priv->slaves[priv->emac_port];
977 else
Mugunthan V Ne86ac132013-03-11 23:16:35 +0000978 slave = &priv->slaves[priv->data.active_slave];
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000979
Richard Cochran2e5b38a2012-10-29 08:45:20 +0000980 ctrl = slave_read(slave, CPSW2_CONTROL);
981 ctrl &= ~CTRL_ALL_TS_MASK;
982
Mugunthan V N9232b162013-02-11 09:52:19 +0000983 if (priv->cpts->tx_enable)
Richard Cochran2e5b38a2012-10-29 08:45:20 +0000984 ctrl |= CTRL_TX_TS_BITS;
985
Mugunthan V N9232b162013-02-11 09:52:19 +0000986 if (priv->cpts->rx_enable)
Richard Cochran2e5b38a2012-10-29 08:45:20 +0000987 ctrl |= CTRL_RX_TS_BITS;
988
989 mtype = (30 << TS_SEQ_ID_OFFSET_SHIFT) | EVENT_MSG_BITS;
990
991 slave_write(slave, mtype, CPSW2_TS_SEQ_MTYPE);
992 slave_write(slave, ctrl, CPSW2_CONTROL);
993 __raw_writel(ETH_P_1588, &priv->regs->ts_ltype);
994}
995
Mugunthan V N3177bf62012-11-27 07:53:40 +0000996static int cpsw_hwtstamp_ioctl(struct net_device *dev, struct ifreq *ifr)
Richard Cochran2e5b38a2012-10-29 08:45:20 +0000997{
Mugunthan V N3177bf62012-11-27 07:53:40 +0000998 struct cpsw_priv *priv = netdev_priv(dev);
Mugunthan V N9232b162013-02-11 09:52:19 +0000999 struct cpts *cpts = priv->cpts;
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001000 struct hwtstamp_config cfg;
1001
1002 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
1003 return -EFAULT;
1004
1005 /* reserved for future extensions */
1006 if (cfg.flags)
1007 return -EINVAL;
1008
1009 switch (cfg.tx_type) {
1010 case HWTSTAMP_TX_OFF:
1011 cpts->tx_enable = 0;
1012 break;
1013 case HWTSTAMP_TX_ON:
1014 cpts->tx_enable = 1;
1015 break;
1016 default:
1017 return -ERANGE;
1018 }
1019
1020 switch (cfg.rx_filter) {
1021 case HWTSTAMP_FILTER_NONE:
1022 cpts->rx_enable = 0;
1023 break;
1024 case HWTSTAMP_FILTER_ALL:
1025 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
1026 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
1027 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
1028 return -ERANGE;
1029 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
1030 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
1031 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
1032 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1033 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
1034 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
1035 case HWTSTAMP_FILTER_PTP_V2_EVENT:
1036 case HWTSTAMP_FILTER_PTP_V2_SYNC:
1037 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
1038 cpts->rx_enable = 1;
1039 cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
1040 break;
1041 default:
1042 return -ERANGE;
1043 }
1044
1045 switch (priv->version) {
1046 case CPSW_VERSION_1:
1047 cpsw_hwtstamp_v1(priv);
1048 break;
1049 case CPSW_VERSION_2:
1050 cpsw_hwtstamp_v2(priv);
1051 break;
1052 default:
1053 return -ENOTSUPP;
1054 }
1055
1056 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
1057}
1058
1059#endif /*CONFIG_TI_CPTS*/
1060
1061static int cpsw_ndo_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
1062{
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001063 if (!netif_running(dev))
1064 return -EINVAL;
1065
1066#ifdef CONFIG_TI_CPTS
1067 if (cmd == SIOCSHWTSTAMP)
Mugunthan V N3177bf62012-11-27 07:53:40 +00001068 return cpsw_hwtstamp_ioctl(dev, req);
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001069#endif
1070 return -ENOTSUPP;
1071}
1072
Mugunthan V Ndf828592012-03-18 20:17:54 +00001073static void cpsw_ndo_tx_timeout(struct net_device *ndev)
1074{
1075 struct cpsw_priv *priv = netdev_priv(ndev);
1076
1077 cpsw_err(priv, tx_err, "transmit timeout, restarting dma\n");
1078 priv->stats.tx_errors++;
1079 cpsw_intr_disable(priv);
1080 cpdma_ctlr_int_ctrl(priv->dma, false);
1081 cpdma_chan_stop(priv->txch);
1082 cpdma_chan_start(priv->txch);
1083 cpdma_ctlr_int_ctrl(priv->dma, true);
1084 cpsw_intr_enable(priv);
Mugunthan V N510a1e72013-02-17 22:19:20 +00001085 cpdma_ctlr_eoi(priv->dma, CPDMA_EOI_RX);
1086 cpdma_ctlr_eoi(priv->dma, CPDMA_EOI_TX);
1087
Mugunthan V Ndf828592012-03-18 20:17:54 +00001088}
1089
1090static struct net_device_stats *cpsw_ndo_get_stats(struct net_device *ndev)
1091{
1092 struct cpsw_priv *priv = netdev_priv(ndev);
1093 return &priv->stats;
1094}
1095
1096#ifdef CONFIG_NET_POLL_CONTROLLER
1097static void cpsw_ndo_poll_controller(struct net_device *ndev)
1098{
1099 struct cpsw_priv *priv = netdev_priv(ndev);
1100
1101 cpsw_intr_disable(priv);
1102 cpdma_ctlr_int_ctrl(priv->dma, false);
1103 cpsw_interrupt(ndev->irq, priv);
1104 cpdma_ctlr_int_ctrl(priv->dma, true);
1105 cpsw_intr_enable(priv);
Mugunthan V N510a1e72013-02-17 22:19:20 +00001106 cpdma_ctlr_eoi(priv->dma, CPDMA_EOI_RX);
1107 cpdma_ctlr_eoi(priv->dma, CPDMA_EOI_TX);
1108
Mugunthan V Ndf828592012-03-18 20:17:54 +00001109}
1110#endif
1111
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001112static inline int cpsw_add_vlan_ale_entry(struct cpsw_priv *priv,
1113 unsigned short vid)
1114{
1115 int ret;
1116
1117 ret = cpsw_ale_add_vlan(priv->ale, vid,
1118 ALE_ALL_PORTS << priv->host_port,
1119 0, ALE_ALL_PORTS << priv->host_port,
1120 (ALE_PORT_1 | ALE_PORT_2) << priv->host_port);
1121 if (ret != 0)
1122 return ret;
1123
1124 ret = cpsw_ale_add_ucast(priv->ale, priv->mac_addr,
1125 priv->host_port, ALE_VLAN, vid);
1126 if (ret != 0)
1127 goto clean_vid;
1128
1129 ret = cpsw_ale_add_mcast(priv->ale, priv->ndev->broadcast,
1130 ALE_ALL_PORTS << priv->host_port,
1131 ALE_VLAN, vid, 0);
1132 if (ret != 0)
1133 goto clean_vlan_ucast;
1134 return 0;
1135
1136clean_vlan_ucast:
1137 cpsw_ale_del_ucast(priv->ale, priv->mac_addr,
1138 priv->host_port, ALE_VLAN, vid);
1139clean_vid:
1140 cpsw_ale_del_vlan(priv->ale, vid, 0);
1141 return ret;
1142}
1143
1144static int cpsw_ndo_vlan_rx_add_vid(struct net_device *ndev,
1145 unsigned short vid)
1146{
1147 struct cpsw_priv *priv = netdev_priv(ndev);
1148
1149 if (vid == priv->data.default_vlan)
1150 return 0;
1151
1152 dev_info(priv->dev, "Adding vlanid %d to vlan filter\n", vid);
1153 return cpsw_add_vlan_ale_entry(priv, vid);
1154}
1155
1156static int cpsw_ndo_vlan_rx_kill_vid(struct net_device *ndev,
1157 unsigned short vid)
1158{
1159 struct cpsw_priv *priv = netdev_priv(ndev);
1160 int ret;
1161
1162 if (vid == priv->data.default_vlan)
1163 return 0;
1164
1165 dev_info(priv->dev, "removing vlanid %d from vlan filter\n", vid);
1166 ret = cpsw_ale_del_vlan(priv->ale, vid, 0);
1167 if (ret != 0)
1168 return ret;
1169
1170 ret = cpsw_ale_del_ucast(priv->ale, priv->mac_addr,
1171 priv->host_port, ALE_VLAN, vid);
1172 if (ret != 0)
1173 return ret;
1174
1175 return cpsw_ale_del_mcast(priv->ale, priv->ndev->broadcast,
1176 0, ALE_VLAN, vid);
1177}
1178
Mugunthan V Ndf828592012-03-18 20:17:54 +00001179static const struct net_device_ops cpsw_netdev_ops = {
1180 .ndo_open = cpsw_ndo_open,
1181 .ndo_stop = cpsw_ndo_stop,
1182 .ndo_start_xmit = cpsw_ndo_start_xmit,
1183 .ndo_change_rx_flags = cpsw_ndo_change_rx_flags,
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001184 .ndo_do_ioctl = cpsw_ndo_ioctl,
Mugunthan V Ndf828592012-03-18 20:17:54 +00001185 .ndo_validate_addr = eth_validate_addr,
David S. Miller5c473ed2012-03-20 00:33:59 -04001186 .ndo_change_mtu = eth_change_mtu,
Mugunthan V Ndf828592012-03-18 20:17:54 +00001187 .ndo_tx_timeout = cpsw_ndo_tx_timeout,
1188 .ndo_get_stats = cpsw_ndo_get_stats,
Mugunthan V N5c50a852012-10-29 08:45:11 +00001189 .ndo_set_rx_mode = cpsw_ndo_set_rx_mode,
Mugunthan V Ndf828592012-03-18 20:17:54 +00001190#ifdef CONFIG_NET_POLL_CONTROLLER
1191 .ndo_poll_controller = cpsw_ndo_poll_controller,
1192#endif
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001193 .ndo_vlan_rx_add_vid = cpsw_ndo_vlan_rx_add_vid,
1194 .ndo_vlan_rx_kill_vid = cpsw_ndo_vlan_rx_kill_vid,
Mugunthan V Ndf828592012-03-18 20:17:54 +00001195};
1196
1197static void cpsw_get_drvinfo(struct net_device *ndev,
1198 struct ethtool_drvinfo *info)
1199{
1200 struct cpsw_priv *priv = netdev_priv(ndev);
Jiri Pirko7826d432013-01-06 00:44:26 +00001201
1202 strlcpy(info->driver, "TI CPSW Driver v1.0", sizeof(info->driver));
1203 strlcpy(info->version, "1.0", sizeof(info->version));
1204 strlcpy(info->bus_info, priv->pdev->name, sizeof(info->bus_info));
Mugunthan V Ndf828592012-03-18 20:17:54 +00001205}
1206
1207static u32 cpsw_get_msglevel(struct net_device *ndev)
1208{
1209 struct cpsw_priv *priv = netdev_priv(ndev);
1210 return priv->msg_enable;
1211}
1212
1213static void cpsw_set_msglevel(struct net_device *ndev, u32 value)
1214{
1215 struct cpsw_priv *priv = netdev_priv(ndev);
1216 priv->msg_enable = value;
1217}
1218
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001219static int cpsw_get_ts_info(struct net_device *ndev,
1220 struct ethtool_ts_info *info)
1221{
1222#ifdef CONFIG_TI_CPTS
1223 struct cpsw_priv *priv = netdev_priv(ndev);
1224
1225 info->so_timestamping =
1226 SOF_TIMESTAMPING_TX_HARDWARE |
1227 SOF_TIMESTAMPING_TX_SOFTWARE |
1228 SOF_TIMESTAMPING_RX_HARDWARE |
1229 SOF_TIMESTAMPING_RX_SOFTWARE |
1230 SOF_TIMESTAMPING_SOFTWARE |
1231 SOF_TIMESTAMPING_RAW_HARDWARE;
Mugunthan V N9232b162013-02-11 09:52:19 +00001232 info->phc_index = priv->cpts->phc_index;
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001233 info->tx_types =
1234 (1 << HWTSTAMP_TX_OFF) |
1235 (1 << HWTSTAMP_TX_ON);
1236 info->rx_filters =
1237 (1 << HWTSTAMP_FILTER_NONE) |
1238 (1 << HWTSTAMP_FILTER_PTP_V2_EVENT);
1239#else
1240 info->so_timestamping =
1241 SOF_TIMESTAMPING_TX_SOFTWARE |
1242 SOF_TIMESTAMPING_RX_SOFTWARE |
1243 SOF_TIMESTAMPING_SOFTWARE;
1244 info->phc_index = -1;
1245 info->tx_types = 0;
1246 info->rx_filters = 0;
1247#endif
1248 return 0;
1249}
1250
Mugunthan V Nd3bb9c52013-03-11 23:16:36 +00001251static int cpsw_get_settings(struct net_device *ndev,
1252 struct ethtool_cmd *ecmd)
1253{
1254 struct cpsw_priv *priv = netdev_priv(ndev);
1255 int slave_no = cpsw_slave_index(priv);
1256
1257 if (priv->slaves[slave_no].phy)
1258 return phy_ethtool_gset(priv->slaves[slave_no].phy, ecmd);
1259 else
1260 return -EOPNOTSUPP;
1261}
1262
1263static int cpsw_set_settings(struct net_device *ndev, struct ethtool_cmd *ecmd)
1264{
1265 struct cpsw_priv *priv = netdev_priv(ndev);
1266 int slave_no = cpsw_slave_index(priv);
1267
1268 if (priv->slaves[slave_no].phy)
1269 return phy_ethtool_sset(priv->slaves[slave_no].phy, ecmd);
1270 else
1271 return -EOPNOTSUPP;
1272}
1273
Mugunthan V Ndf828592012-03-18 20:17:54 +00001274static const struct ethtool_ops cpsw_ethtool_ops = {
1275 .get_drvinfo = cpsw_get_drvinfo,
1276 .get_msglevel = cpsw_get_msglevel,
1277 .set_msglevel = cpsw_set_msglevel,
1278 .get_link = ethtool_op_get_link,
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001279 .get_ts_info = cpsw_get_ts_info,
Mugunthan V Nd3bb9c52013-03-11 23:16:36 +00001280 .get_settings = cpsw_get_settings,
1281 .set_settings = cpsw_set_settings,
Mugunthan V Ndf828592012-03-18 20:17:54 +00001282};
1283
Richard Cochran549985e2012-11-14 09:07:56 +00001284static void cpsw_slave_init(struct cpsw_slave *slave, struct cpsw_priv *priv,
1285 u32 slave_reg_ofs, u32 sliver_reg_ofs)
Mugunthan V Ndf828592012-03-18 20:17:54 +00001286{
1287 void __iomem *regs = priv->regs;
1288 int slave_num = slave->slave_num;
1289 struct cpsw_slave_data *data = priv->data.slave_data + slave_num;
1290
1291 slave->data = data;
Richard Cochran549985e2012-11-14 09:07:56 +00001292 slave->regs = regs + slave_reg_ofs;
1293 slave->sliver = regs + sliver_reg_ofs;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001294 slave->port_vlan = data->dual_emac_res_vlan;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001295}
1296
Mugunthan V N2eb32b02012-07-30 10:17:14 +00001297static int cpsw_probe_dt(struct cpsw_platform_data *data,
1298 struct platform_device *pdev)
1299{
1300 struct device_node *node = pdev->dev.of_node;
1301 struct device_node *slave_node;
1302 int i = 0, ret;
1303 u32 prop;
1304
1305 if (!node)
1306 return -EINVAL;
1307
1308 if (of_property_read_u32(node, "slaves", &prop)) {
1309 pr_err("Missing slaves property in the DT.\n");
1310 return -EINVAL;
1311 }
1312 data->slaves = prop;
1313
Mugunthan V Ne86ac132013-03-11 23:16:35 +00001314 if (of_property_read_u32(node, "active_slave", &prop)) {
1315 pr_err("Missing active_slave property in the DT.\n");
Richard Cochran78ca0b22012-10-29 08:45:18 +00001316 ret = -EINVAL;
1317 goto error_ret;
1318 }
Mugunthan V Ne86ac132013-03-11 23:16:35 +00001319 data->active_slave = prop;
Richard Cochran78ca0b22012-10-29 08:45:18 +00001320
Richard Cochran00ab94e2012-10-29 08:45:19 +00001321 if (of_property_read_u32(node, "cpts_clock_mult", &prop)) {
1322 pr_err("Missing cpts_clock_mult property in the DT.\n");
1323 ret = -EINVAL;
1324 goto error_ret;
1325 }
1326 data->cpts_clock_mult = prop;
1327
1328 if (of_property_read_u32(node, "cpts_clock_shift", &prop)) {
1329 pr_err("Missing cpts_clock_shift property in the DT.\n");
1330 ret = -EINVAL;
1331 goto error_ret;
1332 }
1333 data->cpts_clock_shift = prop;
1334
Joe Perchesb2adaca2013-02-03 17:43:58 +00001335 data->slave_data = kcalloc(data->slaves, sizeof(struct cpsw_slave_data),
1336 GFP_KERNEL);
1337 if (!data->slave_data)
Mugunthan V N2eb32b02012-07-30 10:17:14 +00001338 return -EINVAL;
Mugunthan V N2eb32b02012-07-30 10:17:14 +00001339
Mugunthan V N2eb32b02012-07-30 10:17:14 +00001340 if (of_property_read_u32(node, "cpdma_channels", &prop)) {
1341 pr_err("Missing cpdma_channels property in the DT.\n");
1342 ret = -EINVAL;
1343 goto error_ret;
1344 }
1345 data->channels = prop;
1346
Mugunthan V N2eb32b02012-07-30 10:17:14 +00001347 if (of_property_read_u32(node, "ale_entries", &prop)) {
1348 pr_err("Missing ale_entries property in the DT.\n");
1349 ret = -EINVAL;
1350 goto error_ret;
1351 }
1352 data->ale_entries = prop;
1353
Mugunthan V N2eb32b02012-07-30 10:17:14 +00001354 if (of_property_read_u32(node, "bd_ram_size", &prop)) {
1355 pr_err("Missing bd_ram_size property in the DT.\n");
1356 ret = -EINVAL;
1357 goto error_ret;
1358 }
1359 data->bd_ram_size = prop;
1360
1361 if (of_property_read_u32(node, "rx_descs", &prop)) {
1362 pr_err("Missing rx_descs property in the DT.\n");
1363 ret = -EINVAL;
1364 goto error_ret;
1365 }
1366 data->rx_descs = prop;
1367
1368 if (of_property_read_u32(node, "mac_control", &prop)) {
1369 pr_err("Missing mac_control property in the DT.\n");
1370 ret = -EINVAL;
1371 goto error_ret;
1372 }
1373 data->mac_control = prop;
1374
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001375 if (!of_property_read_u32(node, "dual_emac", &prop))
1376 data->dual_emac = prop;
1377
Vaibhav Hiremath1fb19aa2012-11-14 09:07:55 +00001378 /*
1379 * Populate all the child nodes here...
1380 */
1381 ret = of_platform_populate(node, NULL, NULL, &pdev->dev);
1382 /* We do not want to force this, as in some cases may not have child */
1383 if (ret)
1384 pr_warn("Doesn't have any child node\n");
1385
Richard Cochran549985e2012-11-14 09:07:56 +00001386 for_each_node_by_name(slave_node, "slave") {
1387 struct cpsw_slave_data *slave_data = data->slave_data + i;
1388 const void *mac_addr = NULL;
1389 u32 phyid;
1390 int lenp;
1391 const __be32 *parp;
1392 struct device_node *mdio_node;
1393 struct platform_device *mdio;
1394
1395 parp = of_get_property(slave_node, "phy_id", &lenp);
1396 if ((parp == NULL) && (lenp != (sizeof(void *) * 2))) {
1397 pr_err("Missing slave[%d] phy_id property\n", i);
1398 ret = -EINVAL;
1399 goto error_ret;
1400 }
1401 mdio_node = of_find_node_by_phandle(be32_to_cpup(parp));
1402 phyid = be32_to_cpup(parp+1);
1403 mdio = of_find_device_by_node(mdio_node);
1404 snprintf(slave_data->phy_id, sizeof(slave_data->phy_id),
1405 PHY_ID_FMT, mdio->name, phyid);
1406
1407 mac_addr = of_get_mac_address(slave_node);
1408 if (mac_addr)
1409 memcpy(slave_data->mac_addr, mac_addr, ETH_ALEN);
1410
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001411 if (data->dual_emac) {
1412 if (of_property_read_u32(node, "dual_emac_res_vlan",
1413 &prop)) {
1414 pr_err("Missing dual_emac_res_vlan in DT.\n");
1415 slave_data->dual_emac_res_vlan = i+1;
1416 pr_err("Using %d as Reserved VLAN for %d slave\n",
1417 slave_data->dual_emac_res_vlan, i);
1418 } else {
1419 slave_data->dual_emac_res_vlan = prop;
1420 }
1421 }
1422
Richard Cochran549985e2012-11-14 09:07:56 +00001423 i++;
1424 }
1425
Mugunthan V N2eb32b02012-07-30 10:17:14 +00001426 return 0;
1427
1428error_ret:
1429 kfree(data->slave_data);
1430 return ret;
1431}
1432
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001433static int cpsw_probe_dual_emac(struct platform_device *pdev,
1434 struct cpsw_priv *priv)
1435{
1436 struct cpsw_platform_data *data = &priv->data;
1437 struct net_device *ndev;
1438 struct cpsw_priv *priv_sl2;
1439 int ret = 0, i;
1440
1441 ndev = alloc_etherdev(sizeof(struct cpsw_priv));
1442 if (!ndev) {
1443 pr_err("cpsw: error allocating net_device\n");
1444 return -ENOMEM;
1445 }
1446
1447 priv_sl2 = netdev_priv(ndev);
1448 spin_lock_init(&priv_sl2->lock);
1449 priv_sl2->data = *data;
1450 priv_sl2->pdev = pdev;
1451 priv_sl2->ndev = ndev;
1452 priv_sl2->dev = &ndev->dev;
1453 priv_sl2->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
1454 priv_sl2->rx_packet_max = max(rx_packet_max, 128);
1455
1456 if (is_valid_ether_addr(data->slave_data[1].mac_addr)) {
1457 memcpy(priv_sl2->mac_addr, data->slave_data[1].mac_addr,
1458 ETH_ALEN);
1459 pr_info("cpsw: Detected MACID = %pM\n", priv_sl2->mac_addr);
1460 } else {
1461 random_ether_addr(priv_sl2->mac_addr);
1462 pr_info("cpsw: Random MACID = %pM\n", priv_sl2->mac_addr);
1463 }
1464 memcpy(ndev->dev_addr, priv_sl2->mac_addr, ETH_ALEN);
1465
1466 priv_sl2->slaves = priv->slaves;
1467 priv_sl2->clk = priv->clk;
1468
1469 priv_sl2->cpsw_res = priv->cpsw_res;
1470 priv_sl2->regs = priv->regs;
1471 priv_sl2->host_port = priv->host_port;
1472 priv_sl2->host_port_regs = priv->host_port_regs;
1473 priv_sl2->wr_regs = priv->wr_regs;
1474 priv_sl2->dma = priv->dma;
1475 priv_sl2->txch = priv->txch;
1476 priv_sl2->rxch = priv->rxch;
1477 priv_sl2->ale = priv->ale;
1478 priv_sl2->emac_port = 1;
1479 priv->slaves[1].ndev = ndev;
1480 priv_sl2->cpts = priv->cpts;
1481 priv_sl2->version = priv->version;
1482
1483 for (i = 0; i < priv->num_irqs; i++) {
1484 priv_sl2->irqs_table[i] = priv->irqs_table[i];
1485 priv_sl2->num_irqs = priv->num_irqs;
1486 }
1487
1488 ndev->features |= NETIF_F_HW_VLAN_FILTER;
1489
1490 ndev->netdev_ops = &cpsw_netdev_ops;
1491 SET_ETHTOOL_OPS(ndev, &cpsw_ethtool_ops);
1492 netif_napi_add(ndev, &priv_sl2->napi, cpsw_poll, CPSW_POLL_WEIGHT);
1493
1494 /* register the network device */
1495 SET_NETDEV_DEV(ndev, &pdev->dev);
1496 ret = register_netdev(ndev);
1497 if (ret) {
1498 pr_err("cpsw: error registering net device\n");
1499 free_netdev(ndev);
1500 ret = -ENODEV;
1501 }
1502
1503 return ret;
1504}
1505
Bill Pemberton663e12e2012-12-03 09:23:45 -05001506static int cpsw_probe(struct platform_device *pdev)
Mugunthan V Ndf828592012-03-18 20:17:54 +00001507{
1508 struct cpsw_platform_data *data = pdev->dev.platform_data;
1509 struct net_device *ndev;
1510 struct cpsw_priv *priv;
1511 struct cpdma_params dma_params;
1512 struct cpsw_ale_params ale_params;
Richard Cochran549985e2012-11-14 09:07:56 +00001513 void __iomem *ss_regs, *wr_regs;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001514 struct resource *res;
Richard Cochran549985e2012-11-14 09:07:56 +00001515 u32 slave_offset, sliver_offset, slave_size;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001516 int ret = 0, i, k = 0;
1517
Mugunthan V Ndf828592012-03-18 20:17:54 +00001518 ndev = alloc_etherdev(sizeof(struct cpsw_priv));
1519 if (!ndev) {
1520 pr_err("error allocating net_device\n");
1521 return -ENOMEM;
1522 }
1523
1524 platform_set_drvdata(pdev, ndev);
1525 priv = netdev_priv(ndev);
1526 spin_lock_init(&priv->lock);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001527 priv->pdev = pdev;
1528 priv->ndev = ndev;
1529 priv->dev = &ndev->dev;
1530 priv->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
1531 priv->rx_packet_max = max(rx_packet_max, 128);
Mugunthan V N9232b162013-02-11 09:52:19 +00001532 priv->cpts = devm_kzalloc(&pdev->dev, sizeof(struct cpts), GFP_KERNEL);
1533 if (!ndev) {
1534 pr_err("error allocating cpts\n");
1535 goto clean_ndev_ret;
1536 }
Mugunthan V Ndf828592012-03-18 20:17:54 +00001537
Vaibhav Hiremath1fb19aa2012-11-14 09:07:55 +00001538 /*
1539 * This may be required here for child devices.
1540 */
1541 pm_runtime_enable(&pdev->dev);
1542
Mugunthan V N2eb32b02012-07-30 10:17:14 +00001543 if (cpsw_probe_dt(&priv->data, pdev)) {
1544 pr_err("cpsw: platform data missing\n");
1545 ret = -ENODEV;
1546 goto clean_ndev_ret;
1547 }
1548 data = &priv->data;
1549
Mugunthan V Ndf828592012-03-18 20:17:54 +00001550 if (is_valid_ether_addr(data->slave_data[0].mac_addr)) {
1551 memcpy(priv->mac_addr, data->slave_data[0].mac_addr, ETH_ALEN);
1552 pr_info("Detected MACID = %pM", priv->mac_addr);
1553 } else {
Joe Perches7efd26d2012-07-12 19:33:06 +00001554 eth_random_addr(priv->mac_addr);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001555 pr_info("Random MACID = %pM", priv->mac_addr);
1556 }
1557
1558 memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
1559
1560 priv->slaves = kzalloc(sizeof(struct cpsw_slave) * data->slaves,
1561 GFP_KERNEL);
1562 if (!priv->slaves) {
1563 ret = -EBUSY;
1564 goto clean_ndev_ret;
1565 }
1566 for (i = 0; i < data->slaves; i++)
1567 priv->slaves[i].slave_num = i;
1568
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001569 priv->slaves[0].ndev = ndev;
1570 priv->emac_port = 0;
1571
Mugunthan V Nf150bd72012-07-17 08:09:50 +00001572 priv->clk = clk_get(&pdev->dev, "fck");
Mugunthan V Ndf828592012-03-18 20:17:54 +00001573 if (IS_ERR(priv->clk)) {
Mugunthan V Nf150bd72012-07-17 08:09:50 +00001574 dev_err(&pdev->dev, "fck is not found\n");
1575 ret = -ENODEV;
1576 goto clean_slave_ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001577 }
1578
1579 priv->cpsw_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1580 if (!priv->cpsw_res) {
1581 dev_err(priv->dev, "error getting i/o resource\n");
1582 ret = -ENOENT;
1583 goto clean_clk_ret;
1584 }
Mugunthan V Ndf828592012-03-18 20:17:54 +00001585 if (!request_mem_region(priv->cpsw_res->start,
1586 resource_size(priv->cpsw_res), ndev->name)) {
1587 dev_err(priv->dev, "failed request i/o region\n");
1588 ret = -ENXIO;
1589 goto clean_clk_ret;
1590 }
Richard Cochran549985e2012-11-14 09:07:56 +00001591 ss_regs = ioremap(priv->cpsw_res->start, resource_size(priv->cpsw_res));
1592 if (!ss_regs) {
Mugunthan V Ndf828592012-03-18 20:17:54 +00001593 dev_err(priv->dev, "unable to map i/o region\n");
1594 goto clean_cpsw_iores_ret;
1595 }
Richard Cochran549985e2012-11-14 09:07:56 +00001596 priv->regs = ss_regs;
1597 priv->version = __raw_readl(&priv->regs->id_ver);
1598 priv->host_port = HOST_PORT_NUM;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001599
Richard Cochrana65dd5b2012-11-02 22:25:29 +00001600 priv->cpsw_wr_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1601 if (!priv->cpsw_wr_res) {
Mugunthan V Ndf828592012-03-18 20:17:54 +00001602 dev_err(priv->dev, "error getting i/o resource\n");
1603 ret = -ENOENT;
Richard Cochran5250c962012-11-02 22:25:30 +00001604 goto clean_iomap_ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001605 }
Richard Cochrana65dd5b2012-11-02 22:25:29 +00001606 if (!request_mem_region(priv->cpsw_wr_res->start,
1607 resource_size(priv->cpsw_wr_res), ndev->name)) {
Mugunthan V Ndf828592012-03-18 20:17:54 +00001608 dev_err(priv->dev, "failed request i/o region\n");
1609 ret = -ENXIO;
Richard Cochran5250c962012-11-02 22:25:30 +00001610 goto clean_iomap_ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001611 }
Richard Cochran549985e2012-11-14 09:07:56 +00001612 wr_regs = ioremap(priv->cpsw_wr_res->start,
Richard Cochrana65dd5b2012-11-02 22:25:29 +00001613 resource_size(priv->cpsw_wr_res));
Richard Cochran549985e2012-11-14 09:07:56 +00001614 if (!wr_regs) {
Mugunthan V Ndf828592012-03-18 20:17:54 +00001615 dev_err(priv->dev, "unable to map i/o region\n");
Richard Cochrana65dd5b2012-11-02 22:25:29 +00001616 goto clean_cpsw_wr_iores_ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001617 }
Richard Cochran549985e2012-11-14 09:07:56 +00001618 priv->wr_regs = wr_regs;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001619
1620 memset(&dma_params, 0, sizeof(dma_params));
Richard Cochran549985e2012-11-14 09:07:56 +00001621 memset(&ale_params, 0, sizeof(ale_params));
1622
1623 switch (priv->version) {
1624 case CPSW_VERSION_1:
1625 priv->host_port_regs = ss_regs + CPSW1_HOST_PORT_OFFSET;
Mugunthan V N9232b162013-02-11 09:52:19 +00001626 priv->cpts->reg = ss_regs + CPSW1_CPTS_OFFSET;
Richard Cochran549985e2012-11-14 09:07:56 +00001627 dma_params.dmaregs = ss_regs + CPSW1_CPDMA_OFFSET;
1628 dma_params.txhdp = ss_regs + CPSW1_STATERAM_OFFSET;
1629 ale_params.ale_regs = ss_regs + CPSW1_ALE_OFFSET;
1630 slave_offset = CPSW1_SLAVE_OFFSET;
1631 slave_size = CPSW1_SLAVE_SIZE;
1632 sliver_offset = CPSW1_SLIVER_OFFSET;
1633 dma_params.desc_mem_phys = 0;
1634 break;
1635 case CPSW_VERSION_2:
1636 priv->host_port_regs = ss_regs + CPSW2_HOST_PORT_OFFSET;
Mugunthan V N9232b162013-02-11 09:52:19 +00001637 priv->cpts->reg = ss_regs + CPSW2_CPTS_OFFSET;
Richard Cochran549985e2012-11-14 09:07:56 +00001638 dma_params.dmaregs = ss_regs + CPSW2_CPDMA_OFFSET;
1639 dma_params.txhdp = ss_regs + CPSW2_STATERAM_OFFSET;
1640 ale_params.ale_regs = ss_regs + CPSW2_ALE_OFFSET;
1641 slave_offset = CPSW2_SLAVE_OFFSET;
1642 slave_size = CPSW2_SLAVE_SIZE;
1643 sliver_offset = CPSW2_SLIVER_OFFSET;
1644 dma_params.desc_mem_phys =
1645 (u32 __force) priv->cpsw_res->start + CPSW2_BD_OFFSET;
1646 break;
1647 default:
1648 dev_err(priv->dev, "unknown version 0x%08x\n", priv->version);
1649 ret = -ENODEV;
1650 goto clean_cpsw_wr_iores_ret;
1651 }
1652 for (i = 0; i < priv->data.slaves; i++) {
1653 struct cpsw_slave *slave = &priv->slaves[i];
1654 cpsw_slave_init(slave, priv, slave_offset, sliver_offset);
1655 slave_offset += slave_size;
1656 sliver_offset += SLIVER_SIZE;
1657 }
1658
Mugunthan V Ndf828592012-03-18 20:17:54 +00001659 dma_params.dev = &pdev->dev;
Richard Cochran549985e2012-11-14 09:07:56 +00001660 dma_params.rxthresh = dma_params.dmaregs + CPDMA_RXTHRESH;
1661 dma_params.rxfree = dma_params.dmaregs + CPDMA_RXFREE;
1662 dma_params.rxhdp = dma_params.txhdp + CPDMA_RXHDP;
1663 dma_params.txcp = dma_params.txhdp + CPDMA_TXCP;
1664 dma_params.rxcp = dma_params.txhdp + CPDMA_RXCP;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001665
1666 dma_params.num_chan = data->channels;
1667 dma_params.has_soft_reset = true;
1668 dma_params.min_packet_size = CPSW_MIN_PACKET_SIZE;
1669 dma_params.desc_mem_size = data->bd_ram_size;
1670 dma_params.desc_align = 16;
1671 dma_params.has_ext_regs = true;
Richard Cochran549985e2012-11-14 09:07:56 +00001672 dma_params.desc_hw_addr = dma_params.desc_mem_phys;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001673
1674 priv->dma = cpdma_ctlr_create(&dma_params);
1675 if (!priv->dma) {
1676 dev_err(priv->dev, "error initializing dma\n");
1677 ret = -ENOMEM;
Richard Cochran5250c962012-11-02 22:25:30 +00001678 goto clean_wr_iomap_ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001679 }
1680
1681 priv->txch = cpdma_chan_create(priv->dma, tx_chan_num(0),
1682 cpsw_tx_handler);
1683 priv->rxch = cpdma_chan_create(priv->dma, rx_chan_num(0),
1684 cpsw_rx_handler);
1685
1686 if (WARN_ON(!priv->txch || !priv->rxch)) {
1687 dev_err(priv->dev, "error initializing dma channels\n");
1688 ret = -ENOMEM;
1689 goto clean_dma_ret;
1690 }
1691
Mugunthan V Ndf828592012-03-18 20:17:54 +00001692 ale_params.dev = &ndev->dev;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001693 ale_params.ale_ageout = ale_ageout;
1694 ale_params.ale_entries = data->ale_entries;
1695 ale_params.ale_ports = data->slaves;
1696
1697 priv->ale = cpsw_ale_create(&ale_params);
1698 if (!priv->ale) {
1699 dev_err(priv->dev, "error initializing ale engine\n");
1700 ret = -ENODEV;
1701 goto clean_dma_ret;
1702 }
1703
1704 ndev->irq = platform_get_irq(pdev, 0);
1705 if (ndev->irq < 0) {
1706 dev_err(priv->dev, "error getting irq resource\n");
1707 ret = -ENOENT;
1708 goto clean_ale_ret;
1709 }
1710
1711 while ((res = platform_get_resource(priv->pdev, IORESOURCE_IRQ, k))) {
1712 for (i = res->start; i <= res->end; i++) {
1713 if (request_irq(i, cpsw_interrupt, IRQF_DISABLED,
1714 dev_name(&pdev->dev), priv)) {
1715 dev_err(priv->dev, "error attaching irq\n");
1716 goto clean_ale_ret;
1717 }
1718 priv->irqs_table[k] = i;
1719 priv->num_irqs = k;
1720 }
1721 k++;
1722 }
1723
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001724 ndev->features |= NETIF_F_HW_VLAN_FILTER;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001725
1726 ndev->netdev_ops = &cpsw_netdev_ops;
1727 SET_ETHTOOL_OPS(ndev, &cpsw_ethtool_ops);
1728 netif_napi_add(ndev, &priv->napi, cpsw_poll, CPSW_POLL_WEIGHT);
1729
1730 /* register the network device */
1731 SET_NETDEV_DEV(ndev, &pdev->dev);
1732 ret = register_netdev(ndev);
1733 if (ret) {
1734 dev_err(priv->dev, "error registering net device\n");
1735 ret = -ENODEV;
1736 goto clean_irq_ret;
1737 }
1738
Mugunthan V N9232b162013-02-11 09:52:19 +00001739 if (cpts_register(&pdev->dev, priv->cpts,
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001740 data->cpts_clock_mult, data->cpts_clock_shift))
1741 dev_err(priv->dev, "error registering cpts device\n");
1742
Mugunthan V Ndf828592012-03-18 20:17:54 +00001743 cpsw_notice(priv, probe, "initialized device (regs %x, irq %d)\n",
1744 priv->cpsw_res->start, ndev->irq);
1745
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001746 if (priv->data.dual_emac) {
1747 ret = cpsw_probe_dual_emac(pdev, priv);
1748 if (ret) {
1749 cpsw_err(priv, probe, "error probe slave 2 emac interface\n");
1750 goto clean_irq_ret;
1751 }
1752 }
1753
Mugunthan V Ndf828592012-03-18 20:17:54 +00001754 return 0;
1755
1756clean_irq_ret:
1757 free_irq(ndev->irq, priv);
1758clean_ale_ret:
1759 cpsw_ale_destroy(priv->ale);
1760clean_dma_ret:
1761 cpdma_chan_destroy(priv->txch);
1762 cpdma_chan_destroy(priv->rxch);
1763 cpdma_ctlr_destroy(priv->dma);
Richard Cochran5250c962012-11-02 22:25:30 +00001764clean_wr_iomap_ret:
1765 iounmap(priv->wr_regs);
Richard Cochrana65dd5b2012-11-02 22:25:29 +00001766clean_cpsw_wr_iores_ret:
1767 release_mem_region(priv->cpsw_wr_res->start,
1768 resource_size(priv->cpsw_wr_res));
Richard Cochran5250c962012-11-02 22:25:30 +00001769clean_iomap_ret:
1770 iounmap(priv->regs);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001771clean_cpsw_iores_ret:
1772 release_mem_region(priv->cpsw_res->start,
1773 resource_size(priv->cpsw_res));
1774clean_clk_ret:
1775 clk_put(priv->clk);
Mugunthan V Nf150bd72012-07-17 08:09:50 +00001776clean_slave_ret:
1777 pm_runtime_disable(&pdev->dev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001778 kfree(priv->slaves);
1779clean_ndev_ret:
1780 free_netdev(ndev);
1781 return ret;
1782}
1783
Bill Pemberton663e12e2012-12-03 09:23:45 -05001784static int cpsw_remove(struct platform_device *pdev)
Mugunthan V Ndf828592012-03-18 20:17:54 +00001785{
1786 struct net_device *ndev = platform_get_drvdata(pdev);
1787 struct cpsw_priv *priv = netdev_priv(ndev);
1788
1789 pr_info("removing device");
1790 platform_set_drvdata(pdev, NULL);
1791
Mugunthan V N9232b162013-02-11 09:52:19 +00001792 cpts_unregister(priv->cpts);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001793 free_irq(ndev->irq, priv);
1794 cpsw_ale_destroy(priv->ale);
1795 cpdma_chan_destroy(priv->txch);
1796 cpdma_chan_destroy(priv->rxch);
1797 cpdma_ctlr_destroy(priv->dma);
1798 iounmap(priv->regs);
1799 release_mem_region(priv->cpsw_res->start,
1800 resource_size(priv->cpsw_res));
Richard Cochran5250c962012-11-02 22:25:30 +00001801 iounmap(priv->wr_regs);
Richard Cochrana65dd5b2012-11-02 22:25:29 +00001802 release_mem_region(priv->cpsw_wr_res->start,
1803 resource_size(priv->cpsw_wr_res));
Mugunthan V Nf150bd72012-07-17 08:09:50 +00001804 pm_runtime_disable(&pdev->dev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001805 clk_put(priv->clk);
1806 kfree(priv->slaves);
1807 free_netdev(ndev);
1808
1809 return 0;
1810}
1811
1812static int cpsw_suspend(struct device *dev)
1813{
1814 struct platform_device *pdev = to_platform_device(dev);
1815 struct net_device *ndev = platform_get_drvdata(pdev);
1816
1817 if (netif_running(ndev))
1818 cpsw_ndo_stop(ndev);
Mugunthan V Nf150bd72012-07-17 08:09:50 +00001819 pm_runtime_put_sync(&pdev->dev);
1820
Mugunthan V Ndf828592012-03-18 20:17:54 +00001821 return 0;
1822}
1823
1824static int cpsw_resume(struct device *dev)
1825{
1826 struct platform_device *pdev = to_platform_device(dev);
1827 struct net_device *ndev = platform_get_drvdata(pdev);
1828
Mugunthan V Nf150bd72012-07-17 08:09:50 +00001829 pm_runtime_get_sync(&pdev->dev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001830 if (netif_running(ndev))
1831 cpsw_ndo_open(ndev);
1832 return 0;
1833}
1834
1835static const struct dev_pm_ops cpsw_pm_ops = {
1836 .suspend = cpsw_suspend,
1837 .resume = cpsw_resume,
1838};
1839
Mugunthan V N2eb32b02012-07-30 10:17:14 +00001840static const struct of_device_id cpsw_of_mtable[] = {
1841 { .compatible = "ti,cpsw", },
1842 { /* sentinel */ },
1843};
1844
Mugunthan V Ndf828592012-03-18 20:17:54 +00001845static struct platform_driver cpsw_driver = {
1846 .driver = {
1847 .name = "cpsw",
1848 .owner = THIS_MODULE,
1849 .pm = &cpsw_pm_ops,
Mugunthan V N2eb32b02012-07-30 10:17:14 +00001850 .of_match_table = of_match_ptr(cpsw_of_mtable),
Mugunthan V Ndf828592012-03-18 20:17:54 +00001851 },
1852 .probe = cpsw_probe,
Bill Pemberton663e12e2012-12-03 09:23:45 -05001853 .remove = cpsw_remove,
Mugunthan V Ndf828592012-03-18 20:17:54 +00001854};
1855
1856static int __init cpsw_init(void)
1857{
1858 return platform_driver_register(&cpsw_driver);
1859}
1860late_initcall(cpsw_init);
1861
1862static void __exit cpsw_exit(void)
1863{
1864 platform_driver_unregister(&cpsw_driver);
1865}
1866module_exit(cpsw_exit);
1867
1868MODULE_LICENSE("GPL");
1869MODULE_AUTHOR("Cyril Chemparathy <cyril@ti.com>");
1870MODULE_AUTHOR("Mugunthan V N <mugunthanvnm@ti.com>");
1871MODULE_DESCRIPTION("TI CPSW Ethernet driver");